-
Notifications
You must be signed in to change notification settings - Fork 7
/
bin_pack.c
477 lines (399 loc) · 10.5 KB
/
bin_pack.c
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
471
472
473
474
475
476
477
/*
+----------------------------------------------------------------------+
| BinPack for PHP |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Huqiu Liao <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include "bin_pack.h"
#include "bin_pack_endian.h"
#include <stdio.h>
#ifdef CUBE_LIB_RCSID
static const char rcsid[] = "$Id: bin_pack.c, huqiu Exp $";
#endif
#define TMPBUF_SIZE 24
#ifndef SSIZE_MAX
#define SSIZE_MAX (SIZE_MAX/2)
#endif
static BINPACK_INLINE size_t do_pack_intstr(char *buf, int type, uintmax_t num)
{
char *p = buf;
while (num >= BIN_TAG_PACK_INTERGER)
{
*p++ = 0x80 | num;
num >>= 7;
}
*p++ = type | num;
return (p - buf);
}
static BINPACK_INLINE size_t do_pack_uint_len(char *buf, int type, uintmax_t num)
{
char *p = buf;
/* 0001 xxxx */
while (num >= BIN_TAG_PACK_UINT_LEN)
{
*p++ = 0x80 | num;
num >>= 7;
}
*p++ = type | num;
return (p - buf);
}
static BINPACK_INLINE size_t do_pack_tag(char *buf, int type, uintmax_t num)
{
char *p = buf;
if (num)
{
do
{
*p++ = 0x80 | num;
num >>= 7;
} while (num);
}
*p++ = type;
return (p - buf);
}
BINPACK_INLINE size_t bin_int_buffer(char *buf, intmax_t value)
{
return value >= 0 ? do_pack_intstr(buf, BIN_TYPE_INTEGER, value)
: do_pack_intstr(buf, BIN_TYPE_INTEGER_NEGATIVE, -(uintmax_t)value);
}
BINPACK_INLINE size_t bin_uint_buffer(char *buf, uintmax_t value)
{
return do_pack_intstr(buf, BIN_TYPE_INTEGER, value);
}
BINPACK_INLINE size_t bin_strhead_buffer(char *buf, size_t strlen)
{
assert(strlen <= SSIZE_MAX);
return do_pack_uint_len(buf, BIN_TYPE_STRING, strlen);
}
BINPACK_INLINE size_t bin_blobhead_buffer(char *buf, size_t bloblen)
{
assert(bloblen <= SSIZE_MAX);
return do_pack_uint_len(buf, BIN_TYPE_BLOB, bloblen);
}
size_t bin_float_double_buffer(char *buf, double value)
{
union { double v; uint64_t c; } f;
f.v = value;
uint64_t x = do_binpack_be64(f.c);
*buf++ = BIN_TYPE_FLOAT_DOUBLE;
memcpy(buf, &x, 8);
return 9;
}
size_t bin_float_single_buffer(char *buf, float value)
{
*buf++ = BIN_TYPE_FLOAT_SINGLE;
union { float v; uint32_t c; } f;
f.v = value;
uint32_t x = do_binpack_be32(f.c);
memcpy(buf, &x, 4);
return 5;
}
int bin_unpack_type(bin_unpacker_t *packer, uintmax_t *p_num)
{
if (packer->pos < packer->size)
{
char *p = packer->buf + packer->pos;
uintmax_t num = 0;
int type = 0;
int x = *(unsigned char *)p++;
int shift = 0;
/* 1xxx xxxx */
if (x >= 0x80)
{
int left = sizeof(uintmax_t) * 8 ;
while (x >= 0x80)
{
x &= 0x7f;
num |= ((uintmax_t)x) << shift;
shift += 7;
left -= 7;
if (left <= 0)
{
packer->error = __LINE__;
return -1;
}
x = *(unsigned char *)p++;
}
}
/* 0001 xxxx */
if (x < 0x10)
{
type = x;
}
else
{
/* pack: 000x xxxx
* type: 0xx0 0000
*/
if (x >= BIN_TYPE_INTEGER)
{
type = x & BIN_MASK_TYPE_INTEGER;
num |= (uintmax_t)(x & BIN_MASK_LAST_INTEGER) << shift;
}
/* pack: 0000 xxxx, only 4 bits to pack data
* type: 00xx 0000, string / blob
*/
else
{
type = x & BIN_MASK_TYPE_STRING_OR_BLOB;
num |= (uintmax_t)(x & BIN_MASK_LAST_UINT_LEN) << shift;
}
}
packer->pos = p - packer->buf;
*p_num = num;
return type;
}
packer->error = __LINE__;
return -1;
}
static BINPACK_INLINE int do_unpack_int(bin_unpacker_t *packer, intmax_t *p_value)
{
int type = bin_unpack_type(packer, (uintmax_t*)p_value);
int sign = type & BIN_MASK_INTEGER_SIGN;
if (type < BIN_TYPE_INTEGER)
{
packer->error = __LINE__;
return -1;
}
if (!sign)
{
if (*p_value < 0)
{
packer->error = __LINE__;
return -1;
}
}
else
{
*p_value = -*p_value;
if (*p_value > 0)
{
packer->error = __LINE__;
return -1;
}
}
return 0;
}
static BINPACK_INLINE int do_unpack_unit(bin_unpacker_t *packer, uintmax_t *p_value)
{
uintmax_t num;
int type = bin_unpack_type(packer, &num);
int sign = type & BIN_MASK_INTEGER_SIGN;
if (type < BIN_TYPE_INTEGER || sign)
{
packer->error = __LINE__;
return -1;
}
*p_value = num;
return 0;
}
int bin_pack_integer(bin_packer_t *packer, intmax_t value)
{
char tmpbuf[TMPBUF_SIZE];
size_t n = bin_int_buffer(tmpbuf, value);
if (packer->write(packer->buf, tmpbuf, n) != n)
{
packer->error = __LINE__;
return -1;
}
return 0;
}
int bin_pack_uinteger(bin_packer_t *packer, uintmax_t value)
{
char tmpbuf[TMPBUF_SIZE];
size_t n = bin_uint_buffer(tmpbuf, value);
if (packer->write(packer->buf, tmpbuf, n) != n)
{
packer->error = __LINE__;
return -1;
}
return 0;
}
BINPACK_INLINE int bin_pack_lstring(bin_packer_t *packer, const char *str, size_t len)
{
char tmpbuf[TMPBUF_SIZE];
size_t n = bin_strhead_buffer(tmpbuf, len);
if (packer->write(packer->buf, tmpbuf, n) != n)
{
packer->error = __LINE__;
return -1;
}
if (len > 0 && packer->write(packer->buf, str, len) != len)
{
packer->error = __LINE__;
return -1;
}
return 0;
}
int bin_pack_string(bin_packer_t *packer, const char *str)
{
return bin_pack_lstring(packer, str, strlen(str));
}
int bin_pack_blob(bin_packer_t *packer, const void *data, size_t len)
{
char tmpbuf[TMPBUF_SIZE];
size_t n = bin_blobhead_buffer(tmpbuf, len);
if (packer->write(packer->buf, tmpbuf, n) != n)
{
packer->error = __LINE__;
return -1;
}
if (len > 0 && packer->write(packer->buf, data, len) != len)
{
packer->error = __LINE__;
return -1;
}
return 0;
}
int bin_pack_float_double(bin_packer_t *packer, double value)
{
char tmpbuf[9];
size_t n = bin_float_double_buffer(tmpbuf, value);
if (packer->write(packer->buf, tmpbuf, n) != n)
{
packer->error = __LINE__;
return -1;
}
return 0;
}
int bin_pack_float_single(bin_packer_t *packer, float value)
{
char tmpbuf[5];
size_t n = bin_float_single_buffer(tmpbuf, value);
if (packer->write(packer->buf, tmpbuf, n) != n)
{
packer->error = __LINE__;
return -1;
}
return 0;
}
int bin_unpack_integer(bin_unpacker_t *packer, intmax_t *p_value)
{
return do_unpack_int(packer, p_value);
}
int bin_unpack_uinteger(bin_unpacker_t *packer, uintmax_t *p_value)
{
return do_unpack_unit(packer, p_value);
}
BINPACK_INLINE int bin_unpack_lstring(bin_unpacker_t *packer, char **p_str, size_t *p_len)
{
uintmax_t num;
if (BIN_TYPE_STRING != bin_unpack_type(packer, &num) || num > SSIZE_MAX)
{
packer->error = __LINE__;
return -1;
}
if (num > packer->size - packer->pos)
{
packer->error = __LINE__;
return -1;
}
*p_str = packer->buf + packer->pos;
*p_len = num;
packer->pos += num;
return 0;
}
int bin_unpack_blob(bin_unpacker_t *packer, void **p_data, size_t *p_len)
{
uintmax_t num;
if (BIN_TYPE_BLOB != bin_unpack_type(packer, &num) || num > SSIZE_MAX)
{
packer->error = __LINE__;
return -1;
}
if (num > packer->size - packer->pos)
{
packer->error = __LINE__;
return -1;
}
*p_data = packer->buf + packer->pos;
*p_len = num;
packer->pos += num;
return 0;
}
double bin_make_double(bin_unpacker_t *packer)
{
union { uint64_t c; double v;} f;
char *p = packer->buf + packer->pos;
f.c = do_binpack_be64(*(uint64_t *)p);
packer->pos += 8;
return f.v;
}
float bin_make_float(bin_unpacker_t *packer)
{
union { uint32_t c; float v;} f;
char *p = packer->buf + packer->pos;
f.c = do_binpack_be32(*(uint32_t *)p);
return f.v;
}
int bin_unpack_float_double(bin_unpacker_t *packer, double *p_value)
{
uintmax_t num;
if (BIN_TYPE_FLOAT_DOUBLE != bin_unpack_type(packer, &num))
{
packer->error = __LINE__;
return -1;
}
*p_value = bin_make_double(packer);
return 0;
}
int bin_unpack_float_single(bin_unpacker_t *packer, float *p_value)
{
uintmax_t num;
if (BIN_TYPE_FLOAT_SINGLE != bin_unpack_type(packer, &num))
{
packer->error = __LINE__;
return -1;
}
*p_value = bin_make_float(packer);
return 0;
}
int bin_unpack_bool(bin_unpacker_t *packer, bool *p_value)
{
if (packer->pos >= packer->size)
{
packer->error = __LINE__;
return -1;
}
/* xxxx x0x0, any x is not 0 */
if ((packer->buf[packer->pos] & ~0x05) != 0)
{
packer->error = __LINE__;
return -1;
}
*p_value = (packer->buf[packer->pos] == BIN_TYPE_BOOL);
packer->pos++;
return 0;
}
static BINPACK_INLINE int do_unpack_verify_simple_tag(bin_unpacker_t *packer, int tag)
{
if (packer->pos >= packer->size || packer->buf[packer->pos] != tag)
{
packer->error = __LINE__;
return -1;
}
packer->pos++;
return 0;
}
int bin_unpack_null(bin_unpacker_t *packer)
{
return do_unpack_verify_simple_tag(packer, BIN_TYPE_NULL);
}