Debian Codegraph @

msgpack/pack_template.h

raw
1 /*
2 * MessagePack packing routine template
3 *
4 * Copyright (C) 2008-2010 FURUHASHI Sadayuki
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #if defined(__LITTLE_ENDIAN__)
20 #define TAKE8_8(d) ((uint8_t*)&d)[0]
21 #define TAKE8_16(d) ((uint8_t*)&d)[0]
22 #define TAKE8_32(d) ((uint8_t*)&d)[0]
23 #define TAKE8_64(d) ((uint8_t*)&d)[0]
24 #elif defined(__BIG_ENDIAN__)
25 #define TAKE8_8(d) ((uint8_t*)&d)[0]
26 #define TAKE8_16(d) ((uint8_t*)&d)[1]
27 #define TAKE8_32(d) ((uint8_t*)&d)[3]
28 #define TAKE8_64(d) ((uint8_t*)&d)[7]
29 #endif
30
31 #ifndef msgpack_pack_append_buffer
32 #error msgpack_pack_append_buffer callback is not defined
33 #endif
34
35
36 /*
37 * Integer
38 */
39
40 #define msgpack_pack_real_uint16(x, d) \
41 do { \
42 if(d < (1<<7)) { \
43 /* fixnum */ \
44 msgpack_pack_append_buffer(x, &TAKE8_16(d), 1); \
45 } else if(d < (1<<8)) { \
46 /* unsigned 8 */ \
47 unsigned char buf[2] = {0xcc, TAKE8_16(d)}; \
48 msgpack_pack_append_buffer(x, buf, 2); \
49 } else { \
50 /* unsigned 16 */ \
51 unsigned char buf[3]; \
52 buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \
53 msgpack_pack_append_buffer(x, buf, 3); \
54 } \
55 } while(0)
56
57 #define msgpack_pack_real_uint32(x, d) \
58 do { \
59 if(d < (1<<8)) { \
60 if(d < (1<<7)) { \
61 /* fixnum */ \
62 msgpack_pack_append_buffer(x, &TAKE8_32(d), 1); \
63 } else { \
64 /* unsigned 8 */ \
65 unsigned char buf[2] = {0xcc, TAKE8_32(d)}; \
66 msgpack_pack_append_buffer(x, buf, 2); \
67 } \
68 } else { \
69 if(d < (1<<16)) { \
70 /* unsigned 16 */ \
71 unsigned char buf[3]; \
72 buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \
73 msgpack_pack_append_buffer(x, buf, 3); \
74 } else { \
75 /* unsigned 32 */ \
76 unsigned char buf[5]; \
77 buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \
78 msgpack_pack_append_buffer(x, buf, 5); \
79 } \
80 } \
81 } while(0)
82
83 #define msgpack_pack_real_uint64(x, d) \
84 do { \
85 if(d < (1ULL<<8)) { \
86 if(d < (1ULL<<7)) { \
87 /* fixnum */ \
88 msgpack_pack_append_buffer(x, &TAKE8_64(d), 1); \
89 } else { \
90 /* unsigned 8 */ \
91 unsigned char buf[2] = {0xcc, TAKE8_64(d)}; \
92 msgpack_pack_append_buffer(x, buf, 2); \
93 } \
94 } else { \
95 if(d < (1ULL<<16)) { \
96 /* unsigned 16 */ \
97 unsigned char buf[3]; \
98 buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \
99 msgpack_pack_append_buffer(x, buf, 3); \
100 } else if(d < (1ULL<<32)) { \
101 /* unsigned 32 */ \
102 unsigned char buf[5]; \
103 buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \
104 msgpack_pack_append_buffer(x, buf, 5); \
105 } else { \
106 /* unsigned 64 */ \
107 unsigned char buf[9]; \
108 buf[0] = 0xcf; _msgpack_store64(&buf[1], d); \
109 msgpack_pack_append_buffer(x, buf, 9); \
110 } \
111 } \
112 } while(0)
113
114 #define msgpack_pack_real_int16(x, d) \
115 do { \
116 if(d < -(1<<5)) { \
117 if(d < -(1<<7)) { \
118 /* signed 16 */ \
119 unsigned char buf[3]; \
120 buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \
121 msgpack_pack_append_buffer(x, buf, 3); \
122 } else { \
123 /* signed 8 */ \
124 unsigned char buf[2] = {0xd0, TAKE8_16(d)}; \
125 msgpack_pack_append_buffer(x, buf, 2); \
126 } \
127 } else if(d < (1<<7)) { \
128 /* fixnum */ \
129 msgpack_pack_append_buffer(x, &TAKE8_16(d), 1); \
130 } else { \
131 if(d < (1<<8)) { \
132 /* unsigned 8 */ \
133 unsigned char buf[2] = {0xcc, TAKE8_16(d)}; \
134 msgpack_pack_append_buffer(x, buf, 2); \
135 } else { \
136 /* unsigned 16 */ \
137 unsigned char buf[3]; \
138 buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \
139 msgpack_pack_append_buffer(x, buf, 3); \
140 } \
141 } \
142 } while(0)
143
144 #define msgpack_pack_real_int32(x, d) \
145 do { \
146 if(d < -(1<<5)) { \
147 if(d < -(1<<15)) { \
148 /* signed 32 */ \
149 unsigned char buf[5]; \
150 buf[0] = 0xd2; _msgpack_store32(&buf[1], (int32_t)d); \
151 msgpack_pack_append_buffer(x, buf, 5); \
152 } else if(d < -(1<<7)) { \
153 /* signed 16 */ \
154 unsigned char buf[3]; \
155 buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \
156 msgpack_pack_append_buffer(x, buf, 3); \
157 } else { \
158 /* signed 8 */ \
159 unsigned char buf[2] = {0xd0, TAKE8_32(d)}; \
160 msgpack_pack_append_buffer(x, buf, 2); \
161 } \
162 } else if(d < (1<<7)) { \
163 /* fixnum */ \
164 msgpack_pack_append_buffer(x, &TAKE8_32(d), 1); \
165 } else { \
166 if(d < (1<<8)) { \
167 /* unsigned 8 */ \
168 unsigned char buf[2] = {0xcc, TAKE8_32(d)}; \
169 msgpack_pack_append_buffer(x, buf, 2); \
170 } else if(d < (1<<16)) { \
171 /* unsigned 16 */ \
172 unsigned char buf[3]; \
173 buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \
174 msgpack_pack_append_buffer(x, buf, 3); \
175 } else { \
176 /* unsigned 32 */ \
177 unsigned char buf[5]; \
178 buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \
179 msgpack_pack_append_buffer(x, buf, 5); \
180 } \
181 } \
182 } while(0)
183
184 #define msgpack_pack_real_int64(x, d) \
185 do { \
186 if(d < -(1LL<<5)) { \
187 if(d < -(1LL<<15)) { \
188 if(d < -(1LL<<31)) { \
189 /* signed 64 */ \
190 unsigned char buf[9]; \
191 buf[0] = 0xd3; _msgpack_store64(&buf[1], d); \
192 msgpack_pack_append_buffer(x, buf, 9); \
193 } else { \
194 /* signed 32 */ \
195 unsigned char buf[5]; \
196 buf[0] = 0xd2; _msgpack_store32(&buf[1], (int32_t)d); \
197 msgpack_pack_append_buffer(x, buf, 5); \
198 } \
199 } else { \
200 if(d < -(1<<7)) { \
201 /* signed 16 */ \
202 unsigned char buf[3]; \
203 buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \
204 msgpack_pack_append_buffer(x, buf, 3); \
205 } else { \
206 /* signed 8 */ \
207 unsigned char buf[2] = {0xd0, TAKE8_64(d)}; \
208 msgpack_pack_append_buffer(x, buf, 2); \
209 } \
210 } \
211 } else if(d < (1<<7)) { \
212 /* fixnum */ \
213 msgpack_pack_append_buffer(x, &TAKE8_64(d), 1); \
214 } else { \
215 if(d < (1LL<<16)) { \
216 if(d < (1<<8)) { \
217 /* unsigned 8 */ \
218 unsigned char buf[2] = {0xcc, TAKE8_64(d)}; \
219 msgpack_pack_append_buffer(x, buf, 2); \
220 } else { \
221 /* unsigned 16 */ \
222 unsigned char buf[3]; \
223 buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \
224 msgpack_pack_append_buffer(x, buf, 3); \
225 } \
226 } else { \
227 if(d < (1LL<<32)) { \
228 /* unsigned 32 */ \
229 unsigned char buf[5]; \
230 buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \
231 msgpack_pack_append_buffer(x, buf, 5); \
232 } else { \
233 /* unsigned 64 */ \
234 unsigned char buf[9]; \
235 buf[0] = 0xcf; _msgpack_store64(&buf[1], d); \
236 msgpack_pack_append_buffer(x, buf, 9); \
237 } \
238 } \
239 } \
240 } while(0)
241
242
243 static inline int msgpack_pack_short(msgpack_packer* x, short d)
244 {
245 #if defined(SIZEOF_SHORT)
246 #if SIZEOF_SHORT == 2
247 msgpack_pack_real_int16(x, d);
248 #elif SIZEOF_SHORT == 4
249 msgpack_pack_real_int32(x, d);
250 #else
251 msgpack_pack_real_int64(x, d);
252 #endif
253
254 #elif defined(SHRT_MAX)
255 #if SHRT_MAX == 0x7fff
256 msgpack_pack_real_int16(x, d);
257 #elif SHRT_MAX == 0x7fffffff
258 msgpack_pack_real_int32(x, d);
259 #else
260 msgpack_pack_real_int64(x, d);
261 #endif
262
263 #else
264 if(sizeof(short) == 2) {
265 msgpack_pack_real_int16(x, d);
266 } else if(sizeof(short) == 4) {
267 msgpack_pack_real_int32(x, d);
268 } else {
269 msgpack_pack_real_int64(x, d);
270 }
271 #endif
272 }
273
274 static inline int msgpack_pack_int(msgpack_packer* x, int d)
275 {
276 #if defined(SIZEOF_INT)
277 #if SIZEOF_INT == 2
278 msgpack_pack_real_int16(x, d);
279 #elif SIZEOF_INT == 4
280 msgpack_pack_real_int32(x, d);
281 #else
282 msgpack_pack_real_int64(x, d);
283 #endif
284
285 #elif defined(INT_MAX)
286 #if INT_MAX == 0x7fff
287 msgpack_pack_real_int16(x, d);
288 #elif INT_MAX == 0x7fffffff
289 msgpack_pack_real_int32(x, d);
290 #else
291 msgpack_pack_real_int64(x, d);
292 #endif
293
294 #else
295 if(sizeof(int) == 2) {
296 msgpack_pack_real_int16(x, d);
297 } else if(sizeof(int) == 4) {
298 msgpack_pack_real_int32(x, d);
299 } else {
300 msgpack_pack_real_int64(x, d);
301 }
302 #endif
303 }
304
305 static inline int msgpack_pack_long(msgpack_packer* x, long d)
306 {
307 #if defined(SIZEOF_LONG)
308 #if SIZEOF_LONG == 4
309 msgpack_pack_real_int32(x, d);
310 #else
311 msgpack_pack_real_int64(x, d);
312 #endif
313
314 #elif defined(LONG_MAX)
315 #if LONG_MAX == 0x7fffffffL
316 msgpack_pack_real_int32(x, d);
317 #else
318 msgpack_pack_real_int64(x, d);
319 #endif
320
321 #else
322 if (sizeof(long) == 4) {
323 msgpack_pack_real_int32(x, d);
324 } else {
325 msgpack_pack_real_int64(x, d);
326 }
327 #endif
328 }
329
330 static inline int msgpack_pack_long_long(msgpack_packer* x, long long d)
331 {
332 msgpack_pack_real_int64(x, d);
333 }
334
335 static inline int msgpack_pack_unsigned_long_long(msgpack_packer* x, unsigned long long d)
336 {
337 msgpack_pack_real_uint64(x, d);
338 }
339
340
341 /*
342 * Float
343 */
344
345 static inline int msgpack_pack_float(msgpack_packer* x, float d)
346 {
347 unsigned char buf[5];
348 buf[0] = 0xca;
349
350 #if PY_VERSION_HEX >= 0x030B00A7
351 PyFloat_Pack4(d, (char *)&buf[1], 0);
352 #else
353 _PyFloat_Pack4(d, &buf[1], 0);
354 #endif
355 msgpack_pack_append_buffer(x, buf, 5);
356 }
357
358 static inline int msgpack_pack_double(msgpack_packer* x, double d)
359 {
360 unsigned char buf[9];
361 buf[0] = 0xcb;
362 #if PY_VERSION_HEX >= 0x030B00A7
363 PyFloat_Pack8(d, (char *)&buf[1], 0);
364 #else
365 _PyFloat_Pack8(d, &buf[1], 0);
366 #endif
367 msgpack_pack_append_buffer(x, buf, 9);
368 }
369
370
371 /*
372 * Nil
373 */
374
375 static inline int msgpack_pack_nil(msgpack_packer* x)
376 {
377 static const unsigned char d = 0xc0;
378 msgpack_pack_append_buffer(x, &d, 1);
379 }
380
381
382 /*
383 * Boolean
384 */
385
386 static inline int msgpack_pack_true(msgpack_packer* x)
387 {
388 static const unsigned char d = 0xc3;
389 msgpack_pack_append_buffer(x, &d, 1);
390 }
391
392 static inline int msgpack_pack_false(msgpack_packer* x)
393 {
394 static const unsigned char d = 0xc2;
395 msgpack_pack_append_buffer(x, &d, 1);
396 }
397
398
399 /*
400 * Array
401 */
402
403 static inline int msgpack_pack_array(msgpack_packer* x, unsigned int n)
404 {
405 if(n < 16) {
406 unsigned char d = 0x90 | n;
407 msgpack_pack_append_buffer(x, &d, 1);
408 } else if(n < 65536) {
409 unsigned char buf[3];
410 buf[0] = 0xdc; _msgpack_store16(&buf[1], (uint16_t)n);
411 msgpack_pack_append_buffer(x, buf, 3);
412 } else {
413 unsigned char buf[5];
414 buf[0] = 0xdd; _msgpack_store32(&buf[1], (uint32_t)n);
415 msgpack_pack_append_buffer(x, buf, 5);
416 }
417 }
418
419
420 /*
421 * Map
422 */
423
424 static inline int msgpack_pack_map(msgpack_packer* x, unsigned int n)
425 {
426 if(n < 16) {
427 unsigned char d = 0x80 | n;
428 msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
429 } else if(n < 65536) {
430 unsigned char buf[3];
431 buf[0] = 0xde; _msgpack_store16(&buf[1], (uint16_t)n);
432 msgpack_pack_append_buffer(x, buf, 3);
433 } else {
434 unsigned char buf[5];
435 buf[0] = 0xdf; _msgpack_store32(&buf[1], (uint32_t)n);
436 msgpack_pack_append_buffer(x, buf, 5);
437 }
438 }
439
440
441 /*
442 * Raw
443 */
444
445 static inline int msgpack_pack_raw(msgpack_packer* x, size_t l)
446 {
447 if (l < 32) {
448 unsigned char d = 0xa0 | (uint8_t)l;
449 msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
450 } else if (x->use_bin_type && l < 256) { // str8 is new format introduced with bin.
451 unsigned char buf[2] = {0xd9, (uint8_t)l};
452 msgpack_pack_append_buffer(x, buf, 2);
453 } else if (l < 65536) {
454 unsigned char buf[3];
455 buf[0] = 0xda; _msgpack_store16(&buf[1], (uint16_t)l);
456 msgpack_pack_append_buffer(x, buf, 3);
457 } else {
458 unsigned char buf[5];
459 buf[0] = 0xdb; _msgpack_store32(&buf[1], (uint32_t)l);
460 msgpack_pack_append_buffer(x, buf, 5);
461 }
462 }
463
464 /*
465 * bin
466 */
467 static inline int msgpack_pack_bin(msgpack_packer *x, size_t l)
468 {
469 if (!x->use_bin_type) {
470 return msgpack_pack_raw(x, l);
471 }
472 if (l < 256) {
473 unsigned char buf[2] = {0xc4, (unsigned char)l};
474 msgpack_pack_append_buffer(x, buf, 2);
475 } else if (l < 65536) {
476 unsigned char buf[3] = {0xc5};
477 _msgpack_store16(&buf[1], (uint16_t)l);
478 msgpack_pack_append_buffer(x, buf, 3);
479 } else {
480 unsigned char buf[5] = {0xc6};
481 _msgpack_store32(&buf[1], (uint32_t)l);
482 msgpack_pack_append_buffer(x, buf, 5);
483 }
484 }
485
486 static inline int msgpack_pack_raw_body(msgpack_packer* x, const void* b, size_t l)
487 {
488 if (l > 0) msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
489 return 0;
490 }
491
492 /*
493 * Ext
494 */
495 static inline int msgpack_pack_ext(msgpack_packer* x, char typecode, size_t l)
496 {
497 if (l == 1) {
498 unsigned char buf[2];
499 buf[0] = 0xd4;
500 buf[1] = (unsigned char)typecode;
501 msgpack_pack_append_buffer(x, buf, 2);
502 }
503 else if(l == 2) {
504 unsigned char buf[2];
505 buf[0] = 0xd5;
506 buf[1] = (unsigned char)typecode;
507 msgpack_pack_append_buffer(x, buf, 2);
508 }
509 else if(l == 4) {
510 unsigned char buf[2];
511 buf[0] = 0xd6;
512 buf[1] = (unsigned char)typecode;
513 msgpack_pack_append_buffer(x, buf, 2);
514 }
515 else if(l == 8) {
516 unsigned char buf[2];
517 buf[0] = 0xd7;
518 buf[1] = (unsigned char)typecode;
519 msgpack_pack_append_buffer(x, buf, 2);
520 }
521 else if(l == 16) {
522 unsigned char buf[2];
523 buf[0] = 0xd8;
524 buf[1] = (unsigned char)typecode;
525 msgpack_pack_append_buffer(x, buf, 2);
526 }
527 else if(l < 256) {
528 unsigned char buf[3];
529 buf[0] = 0xc7;
530 buf[1] = l;
531 buf[2] = (unsigned char)typecode;
532 msgpack_pack_append_buffer(x, buf, 3);
533 } else if(l < 65536) {
534 unsigned char buf[4];
535 buf[0] = 0xc8;
536 _msgpack_store16(&buf[1], (uint16_t)l);
537 buf[3] = (unsigned char)typecode;
538 msgpack_pack_append_buffer(x, buf, 4);
539 } else {
540 unsigned char buf[6];
541 buf[0] = 0xc9;
542 _msgpack_store32(&buf[1], (uint32_t)l);
543 buf[5] = (unsigned char)typecode;
544 msgpack_pack_append_buffer(x, buf, 6);
545 }
546
547 }
548
549 /*
550 * Pack Timestamp extension type. Follows msgpack-c pack_template.h.
551 */
552 static inline int msgpack_pack_timestamp(msgpack_packer* x, int64_t seconds, uint32_t nanoseconds)
553 {
554 int ret;
555 if ((seconds >> 34) == 0) {
556 /* seconds is unsigned and fits in 34 bits */
557 uint64_t data64 = ((uint64_t)nanoseconds << 34) | (uint64_t)seconds;
558 if ((data64 & 0xffffffff00000000L) == 0) {
559 /* no nanoseconds and seconds is 32bits or smaller. timestamp32. */
560 unsigned char buf[4];
561 uint32_t data32 = (uint32_t)data64;
562 ret = msgpack_pack_ext(x, -1, 4);
563 if (ret != 0)
564 return ret;
565
566 _msgpack_store32(buf, data32);
567 return msgpack_pack_raw_body(x, buf, 4);
568 } else {
569 /* timestamp64 */
570 unsigned char buf[8];
571 ret = msgpack_pack_ext(x, -1, 8);
572 if (ret != 0)
573 return ret;
574
575 _msgpack_store64(buf, data64);
576 return msgpack_pack_raw_body(x, buf, 8);
577 }
578 } else {
579 /* seconds is signed or >34bits */
580 unsigned char buf[12];
581 _msgpack_store32(&buf[0], nanoseconds);
582 _msgpack_store64(&buf[4], seconds);
583 ret = msgpack_pack_ext(x, -1, 12);
584 if (ret != 0)
585 return ret;
586
587 return msgpack_pack_raw_body(x, buf, 12);
588 }
589 }
590
591
592 #undef msgpack_pack_append_buffer
593
594 #undef TAKE8_8
595 #undef TAKE8_16
596 #undef TAKE8_32
597 #undef TAKE8_64
598
599 #undef msgpack_pack_real_uint16
600 #undef msgpack_pack_real_uint32
601 #undef msgpack_pack_real_uint64
602 #undef msgpack_pack_real_int16
603 #undef msgpack_pack_real_int32
604 #undef msgpack_pack_real_int64