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
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2001-2016. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*
*/
/*
* Function:
* ei_print_term to print out a binary coded term
*/
#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 "eiext.h"
#include "ei_printterm.h"
#include "ei_malloc.h"
#define BINPRINTSIZE 30
/*
* PRINT out a binary term (hacked from 'erl'_print_term)
*/
static int print_string(FILE* fp, ei_x_buff* x, char* s, int len);
static int print_term(FILE* fp, ei_x_buff* x,
const char* buf, int* index);
static void xputc(char c, FILE* fp, ei_x_buff* x)
{
if (fp != NULL)
putc(c, fp);
else
ei_x_append_buf(x, &c, 1);
}
static void xputs(const char* s, FILE* fp, ei_x_buff* x)
{
if (fp != NULL)
fputs(s, fp);
else
ei_x_append_buf(x, s, strlen(s));
}
static int xprintf(FILE* fp, ei_x_buff* x, const char* fmt, ...)
{
int r = 0;
va_list ap;
va_start(ap, fmt);
if (fp != NULL) {
r = vfprintf(fp, fmt, ap);
} else {
/* FIXME always enough in buffer??? */
char tmpbuf[2000];
r = vsprintf(tmpbuf, fmt, ap);
ei_x_append_buf(x, tmpbuf, strlen(tmpbuf));
}
va_end(ap);
return r;
}
static char *ei_big_to_str(erlang_big *b)
{
int buf_len;
char *s,*buf;
unsigned short *sp;
int i;
buf_len = 64+b->is_neg+10*b->arity;
if ( (buf=malloc(buf_len)) == NULL) return NULL;
memset(buf,(char)0,buf_len);
s = buf;
if ( b->is_neg )
s += sprintf(s,"-");
s += sprintf(s,"#integer(%d) = {",b->arity);
for(sp=b->digits,i=0;i<b->arity;i++) {
s += sprintf(s,"%d",sp[i]);
if ( (i+1) != b->arity )
s += sprintf(s,",");
}
s += sprintf(s,"}");
return buf;
}
static int print_term(FILE* fp, ei_x_buff* x,
const char* buf, int* index)
{
int i, doquote, n, m, ty, r;
char a[MAXATOMLEN], *p;
int ch_written = 0; /* counter of written chars */
erlang_pid pid;
erlang_port port;
erlang_ref ref;
double d;
long l;
int tindex = *index;
/* use temporary index for multiple (and failable) decodes */
if (fp == NULL && x == NULL) return -1;
doquote = 0;
ei_get_type(buf, index, &ty, &n);
switch (ty) {
case ERL_ATOM_EXT:
case ERL_ATOM_UTF8_EXT:
case ERL_SMALL_ATOM_EXT:
case ERL_SMALL_ATOM_UTF8_EXT:
if (ei_decode_atom(buf, index, a) < 0)
goto err;
doquote = !islower((int)a[0]);
for (p = a; !doquote && *p != '\0'; ++p)
doquote = !(isalnum((int)*p) || *p == '_' || *p == '@');
if (doquote) {
xputc('\'', fp, x); ++ch_written;
}
xputs(a, fp, x); ch_written += strlen(a);
if (doquote) {
xputc('\'', fp, x); ++ch_written;
}
break;
case ERL_PID_EXT:
case ERL_NEW_PID_EXT:
if (ei_decode_pid(buf, index, &pid) < 0) goto err;
ch_written += xprintf(fp, x, "<%s.%d.%d>", pid.node,
pid.num, pid.serial);
break;
case ERL_PORT_EXT:
case ERL_NEW_PORT_EXT:
if (ei_decode_port(buf, index, &port) < 0) goto err;
ch_written += xprintf(fp, x, "#Port<%d.%d>", port.id, port.creation);
break;
case ERL_NEW_REFERENCE_EXT:
case ERL_NEWER_REFERENCE_EXT:
case ERL_REFERENCE_EXT:
if (ei_decode_ref(buf, index, &ref) < 0) goto err;
ch_written += xprintf(fp, x, "#Ref<");
for (i = 0; i < ref.len; ++i) {
ch_written += xprintf(fp, x, "%d", ref.n[i]);
if (i < ref.len - 1) {
xputc('.', fp, x); ++ch_written;
}
}
xputc('>', fp, x); ++ch_written;
break;
case ERL_NIL_EXT:
if (ei_decode_list_header(buf, index, &n) < 0) goto err;
ch_written += xprintf(fp, x, "[]");
break;
case ERL_LIST_EXT:
if (ei_decode_list_header(buf, &tindex, &n) < 0) goto err;
xputc('[', fp, x); ch_written++;
for (i = 0; i < n; ++i) {
r = print_term(fp, x, buf, &tindex);
if (r < 0) goto err;
ch_written += r;
if (i < n - 1) {
xputs(", ", fp, x); ch_written += 2;
}
}
if (ei_get_type(buf, &tindex, &ty, &n) < 0) goto err;
if (ty != ERL_NIL_EXT) {
xputs(" | ", fp, x); ch_written += 3;
r = print_term(fp, x, buf, &tindex);
if (r < 0) goto err;
ch_written += r;
} else {
if (ei_decode_list_header(buf, &tindex, &n) < 0) goto err;
}
xputc(']', fp, x); ch_written++;
*index = tindex;
break;
case ERL_STRING_EXT:
p = ei_malloc(n+1);
if (p == NULL) goto err;
if (ei_decode_string(buf, index, p) < 0) {
ei_free(p);
goto err;
}
ch_written += print_string(fp, x, p, n);
ei_free(p);
break;
case ERL_SMALL_TUPLE_EXT:
case ERL_LARGE_TUPLE_EXT:
if (ei_decode_tuple_header(buf, &tindex, &n) < 0) goto err;
xputc('{', fp, x); ch_written++;
for (i = 0; i < n; ++i) {
r = print_term(fp, x, buf, &tindex);
if (r < 0) goto err;
ch_written += r;
if (i < n-1) {
xputs(", ", fp, x); ch_written += 2;
}
}
*index = tindex;
xputc('}', fp, x); ch_written++;
break;
case ERL_BINARY_EXT:
p = ei_malloc(n);
if (p == NULL) goto err;
if (ei_decode_binary(buf, index, p, &l) < 0) {
ei_free(p);
goto err;
}
ch_written += xprintf(fp, x, "#Bin<");
if (l > BINPRINTSIZE)
m = BINPRINTSIZE;
else
m = l;
--m;
for (i = 0; i < m; ++i) {
ch_written += xprintf(fp, x, "%d,", p[i]);
}
ch_written += xprintf(fp, x, "%d", p[i]);
if (l > BINPRINTSIZE)
ch_written += xprintf(fp, x, ",...");
xputc('>', fp, x); ++ch_written;
ei_free(p);
break;
case ERL_BIT_BINARY_EXT: {
const char* cp;
size_t bits;
unsigned int bitoffs;
int trunc = 0;
if (ei_decode_bitstring(buf, index, &cp, &bitoffs, &bits) < 0
|| bitoffs != 0) {
goto err;
}
ch_written += xprintf(fp, x, "#Bits<");
m = (bits+7) / 8;
if (m > BINPRINTSIZE) {
m = BINPRINTSIZE;
trunc = 1;
}
--m;
for (i = 0; i < m; ++i) {
ch_written += xprintf(fp, x, "%d,", cp[i]);
}
ch_written += xprintf(fp, x, "%d", cp[i]);
if (trunc)
ch_written += xprintf(fp, x, ",...");
else if (bits % 8 != 0)
ch_written += xprintf(fp, x, ":%u", (unsigned)(bits % 8));
xputc('>', fp, x); ++ch_written;
break;
}
case ERL_SMALL_INTEGER_EXT:
case ERL_INTEGER_EXT:
if (ei_decode_long(buf, index, &l) < 0) goto err;
ch_written += xprintf(fp, x, "%ld", l);
break;
case ERL_SMALL_BIG_EXT:
case ERL_LARGE_BIG_EXT:
{
erlang_big *b;
char *ds;
if ( (b = ei_alloc_big(n)) == NULL) goto err;
if (ei_decode_big(buf, index, b) < 0) {
ei_free_big(b);
goto err;
}
if ( (ds = ei_big_to_str(b)) == NULL ) {
ei_free_big(b);
goto err;
}
ch_written += xprintf(fp, x, ds);
free(ds);
ei_free_big(b);
}
break;
case ERL_FLOAT_EXT:
case NEW_FLOAT_EXT:
if (ei_decode_double(buf, index, &d) < 0) goto err;
ch_written += xprintf(fp, x, "%f", d);
break;
default:
goto err;
}
return ch_written;
err:
return -1;
}
static int print_string(FILE* fp, ei_x_buff* x, char* s, int len)
{
int ch_written = 0; /* counter of written chars */
xputc('"', fp, x);
++ch_written;
for (; len > 0; ++s, --len) {
int c = *s;
if (c >= ' ') {
xputc((char)c, fp, x); ++ch_written; }
else {
switch (c) {
case '\n': xputs("\\n", fp, x); ch_written += 2; break;
case '\r': xputs("\\r", fp, x); ch_written += 2; break;
case '\t': xputs("\\t", fp, x); ch_written += 2; break;
case '\v': xputs("\\v", fp, x); ch_written += 2; break;
case '\b': xputs("\\b", fp, x); ch_written += 2; break;
case '\f': xputs("\\f", fp, x); ch_written += 2; break;
break;
default:
ch_written += xprintf(fp, x, "\\x%x", c);
break;
}
}
}
xputc('"', fp, x); ch_written++;
return ch_written;
}
/* ------------------------------------------ */
/*
* skip a binary term
*/
int ei_print_term(FILE *fp, const char* buf, int* index)
{
return print_term(fp, NULL, buf, index);
}
int ei_s_print_term(char** s, const char* buf, int* index)
{
int r;
ei_x_buff x;
if (*s != NULL) {
x.buff = *s;
x.index = 0;
x.buffsz = BUFSIZ;
} else {
ei_x_new(&x);
}
r = print_term(NULL, &x, buf, index);
ei_x_append_buf(&x, "", 1); /* append '\0' */
*s = x.buff;
return r;
}
|