1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2001-2009. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*
*/
/*
* Function:
* ei_format to build binary format terms a bit like printf
*/
#ifdef VXWORKS
#include <vxWorks.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#ifdef VRTX
#define __READY_EXTENSIONS__
#include <errno.h>
#endif
#include "eidef.h"
#include "ei_malloc.h"
#include "ei_format.h"
/*
* To avoid problems we read the variable number of arguments to an
* array of unions.
*/
union arg {
char* s;
long l;
unsigned long u;
double d;
};
static int eiformat(const char** s, union arg** args, ei_x_buff* x);
/* forwards of parse functions */
static int pformat(const char** fmt, union arg**, ei_x_buff* x);
static int plist(const char** fmt, union arg**, ei_x_buff* x, int size);
static int ptuple(const char** fmt, union arg**, ei_x_buff* x, int size);
static int pquotedatom(const char** fmt, ei_x_buff* x);
static int pdigit(const char** fmt, ei_x_buff* x);
static int patom(const char** fmt, ei_x_buff* x);
static int pstring(const char** fmt, ei_x_buff* x);
/* format a string into an ei_x_buff, except the version token */
static int eiformat(const char** fmt, union arg** args, ei_x_buff* x)
{
const char* p = *fmt;
int res;
ei_x_buff x2;
while (isspace((int)*p))
++p;
switch (*p) {
case '~':
res = pformat(&p, args, x);
break;
case '[':
res = ei_x_new(&x2);
if (res >= 0)
res = plist(&p, args, &x2, 0);
if (res > 0)
res = ei_x_encode_list_header(x, res);
if (res >= 0)
res = ei_x_append(x, &x2);
ei_x_free(&x2);
break;
case '{':
res = ei_x_new(&x2);
if (res >= 0)
res = ptuple(&p, args, &x2, 0);
if (res >= 0)
res = ei_x_encode_tuple_header(x, res);
if (res >= 0)
res = ei_x_append(x, &x2);
ei_x_free(&x2);
break;
case '"':
res = pstring(&p, x);
break;
case '\'':
res = pquotedatom(&p, x);
break;
default:
if (isdigit((int)*p))
res = pdigit(&p, x);
else if ((*p == '-' || *p == '+') && isdigit((int)*(p+1)))
res = pdigit(&p, x);
else if (islower((int)*p))
res = patom(&p, x);
else
res = -1;
break;
/*
Variables
*/
}
*fmt = p;
return res;
}
static int patom(const char** fmt, ei_x_buff* x)
{
const char* start = *fmt;
char c;
int len;
for (;;) {
c = *(*fmt)++;
if (isalnum((int) c) || (c == '_') || (c == '@'))
continue;
else
break;
}
--(*fmt);
len = *fmt - start;
/* FIXME why truncate atom name and not fail?! */
if (len > MAXATOMLEN)
len = MAXATOMLEN;
return ei_x_encode_atom_len(x, start, len);
}
/* Check if integer or float */
static int pdigit(const char** fmt, ei_x_buff* x)
{
const char* start = *fmt;
char c;
int len, dotp=0;
double d;
long l;
if (**fmt == '-' || **fmt == '+')
(*fmt)++;
for (;;) {
c = *(*fmt)++;
if (isdigit((int)c))
continue;
else if (!dotp && (c == '.')) {
dotp = 1;
continue;
} else
break;
}
--(*fmt);
len = *fmt - start;
if (dotp) {
sscanf(start, "%lf", &d);
return ei_x_encode_double(x, d);
} else {
sscanf(start, "%ld", &l);
return ei_x_encode_long(x, l);
}
}
/* "string" */
static int pstring(const char** fmt, ei_x_buff* x)
{
const char* start = ++(*fmt); /* skip first quote */
char c;
int res;
for (;;) {
c = *(*fmt)++;
if (c == '\0')
return -1;
if (c == '"') {
if (*((*fmt)-1) == '\\')
continue;
else
break;
} else
continue;
}
res = ei_x_encode_string_len(x, start, *fmt - start - 1);
return res;
}
/* 'atom' */
static int pquotedatom(const char** fmt, ei_x_buff* x)
{
const char* start = ++(*fmt); /* skip first quote */
char c;
int res;
for (;;) {
c = *(*fmt)++;
if (c == 0)
return -1;
if (c == '\'') {
if (*((*fmt)-1) == '\\')
continue;
else
break;
} else
continue;
}
res = ei_x_encode_atom_len(x, start, *fmt - start - 1);
return res;
}
/*
* The format letters are:
* a - An atom
* s - A string
* i - An integer
* l - A long integer
* u - An unsigned long integer
* f - A float
* d - A double float
*/
static int pformat(const char** fmt, union arg** args, ei_x_buff* x)
{
int res = 0;
++(*fmt); /* skip tilde */
switch (*(*fmt)++) {
case 'a':
res = ei_x_encode_atom(x, (*args)->s);
(*args)++;
break;
case 's':
res = ei_x_encode_string(x, (*args)->s);
(*args)++;
break;
case 'i':
res = ei_x_encode_long(x, (*args)->l);
(*args)++;
break;
case 'l':
res = ei_x_encode_long(x, (*args)->l);
(*args)++;
break;
case 'u':
res = ei_x_encode_ulong(x, (*args)->u);
(*args)++;
break;
case 'f': /* float is expanded to double (C calling conventions) */
case 'd':
res = ei_x_encode_double(x, (*args)->d);
(*args)++;
break;
default:
res = -1;
break;
}
return res;
}
/* encode a tuple */
static int ptuple(const char** fmt, union arg** args, ei_x_buff* x, int size)
{
int res = 0;
const char* p = *fmt;
char after = *p++;
if (after == '}') {
*fmt = p;
return size;
}
while (isspace((int)*p))
++p;
switch (*p++) {
case '}':
if (after == ',')
res = -1;
else
res = size;
break;
case ',':
if (after == ',' || after == '{')
res = -1;
else
res = ptuple(&p, args, x, size);
break;
default:
--p;
res = eiformat(&p, args, x);
if (res >= 0)
res = ptuple(&p, args, x, size + 1);
break;
/*
Variables
*/
}
*fmt = p;
return res;
}
/* encode a list */
static int plist(const char** fmt, union arg** args, ei_x_buff* x, int size)
{
int res = 0;
const char* p = *fmt;
char after = *p++;
if (after == ']')
--p;
while (isspace((int)*p))
++p;
switch (*p++) {
case ']':
if (after == ',')
res = -1;
else {
if (after != '|')
ei_x_encode_empty_list(x);
res = size;
}
break;
case '|':
if (after == '|' || after == ',')
res = -1;
else
res = plist(&p, args, x, size);
break;
case ',':
if (after == '|' || after == ',')
res = -1;
else
res = plist(&p, args, x, size);
break;
default:
--p;
res = eiformat(&p, args, x);
++size;
if (res >= 0) {
if (after == '|') {
while (isspace((int)*p))
++p;
if (*p != ']')
res = -1;
} else
res = plist(&p, args, x, size);
}
break;
/*
Variables
*/
}
*fmt = p;
return res;
}
static int read_args(const char* fmt, va_list ap, union arg **argp)
{
const char* p = fmt;
int arg_count = 0;
union arg* args;
int i = 0;
/* Count the number of format strings. Assume null terminated string. */
*argp = NULL;
while (*p) if (*p++ == '~') arg_count++;
if (!arg_count) {
return 0;
}
/* Allocate space for the arguments */
args = (union arg*)ei_malloc(arg_count * sizeof(union arg));
if (!args)
return -1;
p = fmt; /* Start again and fill array */
while (*p) {
if (*p++ == '~') {
if (!*p) {
ei_free(args);
return -1; /* Error, string not complete */
}
switch (*p++) {
case 'a':
case 's':
args[i++].s = va_arg(ap, char*);
break;
case 'i':
#ifdef EI_64BIT
args[i++].l = (long) va_arg(ap, int);
break;
#endif
case 'l':
args[i++].l = va_arg(ap, long);
break;
case 'u':
args[i++].u = va_arg(ap, unsigned long);
break;
case 'f': /* float is expanded to double (C calling conventions) */
case 'd':
args[i++].d = va_arg(ap, double);
break;
default:
ei_free(args); /* Invalid specifier */
return -1;
}
}
}
*argp = args;
return 0;
}
int ei_x_format(ei_x_buff* x, const char* fmt, ... )
{
va_list ap;
union arg* args;
union arg* saved_args;
int res;
res = ei_x_encode_version(x);
if (res < 0) return res;
va_start(ap, fmt);
res = read_args(fmt,ap,&args);
saved_args = args;
va_end(ap);
if (res < 0) {
return -1;
}
res = eiformat(&fmt, &args, x);
ei_free(saved_args);
return res;
}
int ei_x_format_wo_ver(ei_x_buff* x, const char* fmt, ... )
{
va_list ap;
union arg* args;
union arg* saved_args;
int res;
va_start(ap, fmt);
res = read_args(fmt,ap,&args);
saved_args = args;
va_end(ap);
if (res < 0) {
return -1;
}
res = eiformat(&fmt, &args, x);
ei_free(saved_args);
return res;
}
|