1 | /* |
2 | * MessagePack packing routine template |
3 | * |
4 | * Copyright (C) 2008-2010 FURUHASHI Sadayuki |
5 | * |
6 | * Distributed under the Boost Software License, Version 1.0. |
7 | * (See accompanying file LICENSE_1_0.txt or copy at |
8 | * http://www.boost.org/LICENSE_1_0.txt) |
9 | */ |
10 | |
11 | #if MSGPACK_ENDIAN_LITTLE_BYTE |
12 | #define TAKE8_8(d) ((uint8_t*)&d)[0] |
13 | #define TAKE8_16(d) ((uint8_t*)&d)[0] |
14 | #define TAKE8_32(d) ((uint8_t*)&d)[0] |
15 | #define TAKE8_64(d) ((uint8_t*)&d)[0] |
16 | #elif MSGPACK_ENDIAN_BIG_BYTE |
17 | #define TAKE8_8(d) ((uint8_t*)&d)[0] |
18 | #define TAKE8_16(d) ((uint8_t*)&d)[1] |
19 | #define TAKE8_32(d) ((uint8_t*)&d)[3] |
20 | #define TAKE8_64(d) ((uint8_t*)&d)[7] |
21 | #else |
22 | #error msgpack-c supports only big endian and little endian |
23 | #endif |
24 | |
25 | #ifndef msgpack_pack_inline_func |
26 | #error msgpack_pack_inline_func template is not defined |
27 | #endif |
28 | |
29 | #ifndef msgpack_pack_user |
30 | #error msgpack_pack_user type is not defined |
31 | #endif |
32 | |
33 | #ifndef msgpack_pack_append_buffer |
34 | #error msgpack_pack_append_buffer callback is not defined |
35 | #endif |
36 | |
37 | |
38 | /* |
39 | * Integer |
40 | */ |
41 | |
42 | #define msgpack_pack_real_uint8(x, d) \ |
43 | do { \ |
44 | if(d < (1<<7)) { \ |
45 | /* fixnum */ \ |
46 | msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); \ |
47 | } else { \ |
48 | /* unsigned 8 */ \ |
49 | unsigned char buf[2] = {0xcc, TAKE8_8(d)}; \ |
50 | msgpack_pack_append_buffer(x, buf, 2); \ |
51 | } \ |
52 | } while(0) |
53 | |
54 | #define msgpack_pack_real_uint16(x, d) \ |
55 | do { \ |
56 | if(d < (1<<7)) { \ |
57 | /* fixnum */ \ |
58 | msgpack_pack_append_buffer(x, &TAKE8_16(d), 1); \ |
59 | } else if(d < (1<<8)) { \ |
60 | /* unsigned 8 */ \ |
61 | unsigned char buf[2] = {0xcc, TAKE8_16(d)}; \ |
62 | msgpack_pack_append_buffer(x, buf, 2); \ |
63 | } else { \ |
64 | /* unsigned 16 */ \ |
65 | unsigned char buf[3]; \ |
66 | buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ |
67 | msgpack_pack_append_buffer(x, buf, 3); \ |
68 | } \ |
69 | } while(0) |
70 | |
71 | #define msgpack_pack_real_uint32(x, d) \ |
72 | do { \ |
73 | if(d < (1<<8)) { \ |
74 | if(d < (1<<7)) { \ |
75 | /* fixnum */ \ |
76 | msgpack_pack_append_buffer(x, &TAKE8_32(d), 1); \ |
77 | } else { \ |
78 | /* unsigned 8 */ \ |
79 | unsigned char buf[2] = {0xcc, TAKE8_32(d)}; \ |
80 | msgpack_pack_append_buffer(x, buf, 2); \ |
81 | } \ |
82 | } else { \ |
83 | if(d < (1<<16)) { \ |
84 | /* unsigned 16 */ \ |
85 | unsigned char buf[3]; \ |
86 | buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ |
87 | msgpack_pack_append_buffer(x, buf, 3); \ |
88 | } else { \ |
89 | /* unsigned 32 */ \ |
90 | unsigned char buf[5]; \ |
91 | buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \ |
92 | msgpack_pack_append_buffer(x, buf, 5); \ |
93 | } \ |
94 | } \ |
95 | } while(0) |
96 | |
97 | #define msgpack_pack_real_uint64(x, d) \ |
98 | do { \ |
99 | if(d < (1ULL<<8)) { \ |
100 | if(d < (1ULL<<7)) { \ |
101 | /* fixnum */ \ |
102 | msgpack_pack_append_buffer(x, &TAKE8_64(d), 1); \ |
103 | } else { \ |
104 | /* unsigned 8 */ \ |
105 | unsigned char buf[2] = {0xcc, TAKE8_64(d)}; \ |
106 | msgpack_pack_append_buffer(x, buf, 2); \ |
107 | } \ |
108 | } else { \ |
109 | if(d < (1ULL<<16)) { \ |
110 | /* unsigned 16 */ \ |
111 | unsigned char buf[3]; \ |
112 | buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ |
113 | msgpack_pack_append_buffer(x, buf, 3); \ |
114 | } else if(d < (1ULL<<32)) { \ |
115 | /* unsigned 32 */ \ |
116 | unsigned char buf[5]; \ |
117 | buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \ |
118 | msgpack_pack_append_buffer(x, buf, 5); \ |
119 | } else { \ |
120 | /* unsigned 64 */ \ |
121 | unsigned char buf[9]; \ |
122 | buf[0] = 0xcf; _msgpack_store64(&buf[1], d); \ |
123 | msgpack_pack_append_buffer(x, buf, 9); \ |
124 | } \ |
125 | } \ |
126 | } while(0) |
127 | |
128 | #define msgpack_pack_real_int8(x, d) \ |
129 | do { \ |
130 | if(d < -(1<<5)) { \ |
131 | /* signed 8 */ \ |
132 | unsigned char buf[2] = {0xd0, TAKE8_8(d)}; \ |
133 | msgpack_pack_append_buffer(x, buf, 2); \ |
134 | } else { \ |
135 | /* fixnum */ \ |
136 | msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); \ |
137 | } \ |
138 | } while(0) |
139 | |
140 | #define msgpack_pack_real_int16(x, d) \ |
141 | do { \ |
142 | if(d < -(1<<5)) { \ |
143 | if(d < -(1<<7)) { \ |
144 | /* signed 16 */ \ |
145 | unsigned char buf[3]; \ |
146 | buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \ |
147 | msgpack_pack_append_buffer(x, buf, 3); \ |
148 | } else { \ |
149 | /* signed 8 */ \ |
150 | unsigned char buf[2] = {0xd0, TAKE8_16(d)}; \ |
151 | msgpack_pack_append_buffer(x, buf, 2); \ |
152 | } \ |
153 | } else if(d < (1<<7)) { \ |
154 | /* fixnum */ \ |
155 | msgpack_pack_append_buffer(x, &TAKE8_16(d), 1); \ |
156 | } else { \ |
157 | if(d < (1<<8)) { \ |
158 | /* unsigned 8 */ \ |
159 | unsigned char buf[2] = {0xcc, TAKE8_16(d)}; \ |
160 | msgpack_pack_append_buffer(x, buf, 2); \ |
161 | } else { \ |
162 | /* unsigned 16 */ \ |
163 | unsigned char buf[3]; \ |
164 | buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ |
165 | msgpack_pack_append_buffer(x, buf, 3); \ |
166 | } \ |
167 | } \ |
168 | } while(0) |
169 | |
170 | #define msgpack_pack_real_int32(x, d) \ |
171 | do { \ |
172 | if(d < -(1<<5)) { \ |
173 | if(d < -(1<<15)) { \ |
174 | /* signed 32 */ \ |
175 | unsigned char buf[5]; \ |
176 | buf[0] = 0xd2; _msgpack_store32(&buf[1], (int32_t)d); \ |
177 | msgpack_pack_append_buffer(x, buf, 5); \ |
178 | } else if(d < -(1<<7)) { \ |
179 | /* signed 16 */ \ |
180 | unsigned char buf[3]; \ |
181 | buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \ |
182 | msgpack_pack_append_buffer(x, buf, 3); \ |
183 | } else { \ |
184 | /* signed 8 */ \ |
185 | unsigned char buf[2] = {0xd0, TAKE8_32(d)}; \ |
186 | msgpack_pack_append_buffer(x, buf, 2); \ |
187 | } \ |
188 | } else if(d < (1<<7)) { \ |
189 | /* fixnum */ \ |
190 | msgpack_pack_append_buffer(x, &TAKE8_32(d), 1); \ |
191 | } else { \ |
192 | if(d < (1<<8)) { \ |
193 | /* unsigned 8 */ \ |
194 | unsigned char buf[2] = {0xcc, TAKE8_32(d)}; \ |
195 | msgpack_pack_append_buffer(x, buf, 2); \ |
196 | } else if(d < (1<<16)) { \ |
197 | /* unsigned 16 */ \ |
198 | unsigned char buf[3]; \ |
199 | buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ |
200 | msgpack_pack_append_buffer(x, buf, 3); \ |
201 | } else { \ |
202 | /* unsigned 32 */ \ |
203 | unsigned char buf[5]; \ |
204 | buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \ |
205 | msgpack_pack_append_buffer(x, buf, 5); \ |
206 | } \ |
207 | } \ |
208 | } while(0) |
209 | |
210 | #define msgpack_pack_real_int64(x, d) \ |
211 | do { \ |
212 | if(d < -(1LL<<5)) { \ |
213 | if(d < -(1LL<<15)) { \ |
214 | if(d < -(1LL<<31)) { \ |
215 | /* signed 64 */ \ |
216 | unsigned char buf[9]; \ |
217 | buf[0] = 0xd3; _msgpack_store64(&buf[1], d); \ |
218 | msgpack_pack_append_buffer(x, buf, 9); \ |
219 | } else { \ |
220 | /* signed 32 */ \ |
221 | unsigned char buf[5]; \ |
222 | buf[0] = 0xd2; _msgpack_store32(&buf[1], (int32_t)d); \ |
223 | msgpack_pack_append_buffer(x, buf, 5); \ |
224 | } \ |
225 | } else { \ |
226 | if(d < -(1<<7)) { \ |
227 | /* signed 16 */ \ |
228 | unsigned char buf[3]; \ |
229 | buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \ |
230 | msgpack_pack_append_buffer(x, buf, 3); \ |
231 | } else { \ |
232 | /* signed 8 */ \ |
233 | unsigned char buf[2] = {0xd0, TAKE8_64(d)}; \ |
234 | msgpack_pack_append_buffer(x, buf, 2); \ |
235 | } \ |
236 | } \ |
237 | } else if(d < (1<<7)) { \ |
238 | /* fixnum */ \ |
239 | msgpack_pack_append_buffer(x, &TAKE8_64(d), 1); \ |
240 | } else { \ |
241 | if(d < (1LL<<16)) { \ |
242 | if(d < (1<<8)) { \ |
243 | /* unsigned 8 */ \ |
244 | unsigned char buf[2] = {0xcc, TAKE8_64(d)}; \ |
245 | msgpack_pack_append_buffer(x, buf, 2); \ |
246 | } else { \ |
247 | /* unsigned 16 */ \ |
248 | unsigned char buf[3]; \ |
249 | buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ |
250 | msgpack_pack_append_buffer(x, buf, 3); \ |
251 | } \ |
252 | } else { \ |
253 | if(d < (1LL<<32)) { \ |
254 | /* unsigned 32 */ \ |
255 | unsigned char buf[5]; \ |
256 | buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \ |
257 | msgpack_pack_append_buffer(x, buf, 5); \ |
258 | } else { \ |
259 | /* unsigned 64 */ \ |
260 | unsigned char buf[9]; \ |
261 | buf[0] = 0xcf; _msgpack_store64(&buf[1], d); \ |
262 | msgpack_pack_append_buffer(x, buf, 9); \ |
263 | } \ |
264 | } \ |
265 | } \ |
266 | } while(0) |
267 | |
268 | |
269 | #ifdef msgpack_pack_inline_func_fixint |
270 | |
271 | msgpack_pack_inline_func_fixint(_uint8)(msgpack_pack_user x, uint8_t d) |
272 | { |
273 | unsigned char buf[2] = {0xcc, TAKE8_8(d)}; |
274 | msgpack_pack_append_buffer(x, buf, 2); |
275 | } |
276 | |
277 | msgpack_pack_inline_func_fixint(_uint16)(msgpack_pack_user x, uint16_t d) |
278 | { |
279 | unsigned char buf[3]; |
280 | buf[0] = 0xcd; _msgpack_store16(&buf[1], d); |
281 | msgpack_pack_append_buffer(x, buf, 3); |
282 | } |
283 | |
284 | msgpack_pack_inline_func_fixint(_uint32)(msgpack_pack_user x, uint32_t d) |
285 | { |
286 | unsigned char buf[5]; |
287 | buf[0] = 0xce; _msgpack_store32(&buf[1], d); |
288 | msgpack_pack_append_buffer(x, buf, 5); |
289 | } |
290 | |
291 | msgpack_pack_inline_func_fixint(_uint64)(msgpack_pack_user x, uint64_t d) |
292 | { |
293 | unsigned char buf[9]; |
294 | buf[0] = 0xcf; _msgpack_store64(&buf[1], d); |
295 | msgpack_pack_append_buffer(x, buf, 9); |
296 | } |
297 | |
298 | msgpack_pack_inline_func_fixint(_int8)(msgpack_pack_user x, int8_t d) |
299 | { |
300 | unsigned char buf[2] = {0xd0, TAKE8_8(d)}; |
301 | msgpack_pack_append_buffer(x, buf, 2); |
302 | } |
303 | |
304 | msgpack_pack_inline_func_fixint(_int16)(msgpack_pack_user x, int16_t d) |
305 | { |
306 | unsigned char buf[3]; |
307 | buf[0] = 0xd1; _msgpack_store16(&buf[1], d); |
308 | msgpack_pack_append_buffer(x, buf, 3); |
309 | } |
310 | |
311 | msgpack_pack_inline_func_fixint(_int32)(msgpack_pack_user x, int32_t d) |
312 | { |
313 | unsigned char buf[5]; |
314 | buf[0] = 0xd2; _msgpack_store32(&buf[1], d); |
315 | msgpack_pack_append_buffer(x, buf, 5); |
316 | } |
317 | |
318 | msgpack_pack_inline_func_fixint(_int64)(msgpack_pack_user x, int64_t d) |
319 | { |
320 | unsigned char buf[9]; |
321 | buf[0] = 0xd3; _msgpack_store64(&buf[1], d); |
322 | msgpack_pack_append_buffer(x, buf, 9); |
323 | } |
324 | |
325 | #undef msgpack_pack_inline_func_fixint |
326 | #endif |
327 | |
328 | |
329 | msgpack_pack_inline_func(_uint8)(msgpack_pack_user x, uint8_t d) |
330 | { |
331 | msgpack_pack_real_uint8(x, d); |
332 | } |
333 | |
334 | msgpack_pack_inline_func(_uint16)(msgpack_pack_user x, uint16_t d) |
335 | { |
336 | msgpack_pack_real_uint16(x, d); |
337 | } |
338 | |
339 | msgpack_pack_inline_func(_uint32)(msgpack_pack_user x, uint32_t d) |
340 | { |
341 | msgpack_pack_real_uint32(x, d); |
342 | } |
343 | |
344 | msgpack_pack_inline_func(_uint64)(msgpack_pack_user x, uint64_t d) |
345 | { |
346 | msgpack_pack_real_uint64(x, d); |
347 | } |
348 | |
349 | msgpack_pack_inline_func(_int8)(msgpack_pack_user x, int8_t d) |
350 | { |
351 | msgpack_pack_real_int8(x, d); |
352 | } |
353 | |
354 | msgpack_pack_inline_func(_int16)(msgpack_pack_user x, int16_t d) |
355 | { |
356 | msgpack_pack_real_int16(x, d); |
357 | } |
358 | |
359 | msgpack_pack_inline_func(_int32)(msgpack_pack_user x, int32_t d) |
360 | { |
361 | msgpack_pack_real_int32(x, d); |
362 | } |
363 | |
364 | msgpack_pack_inline_func(_int64)(msgpack_pack_user x, int64_t d) |
365 | { |
366 | msgpack_pack_real_int64(x, d); |
367 | } |
368 | |
369 | msgpack_pack_inline_func(_char)(msgpack_pack_user x, char d) |
370 | { |
371 | #if defined(CHAR_MIN) |
372 | #if CHAR_MIN < 0 |
373 | msgpack_pack_real_int8(x, d); |
374 | #else |
375 | msgpack_pack_real_uint8(x, d); |
376 | #endif |
377 | #else |
378 | #error CHAR_MIN is not defined |
379 | #endif |
380 | } |
381 | |
382 | msgpack_pack_inline_func(_signed_char)(msgpack_pack_user x, signed char d) |
383 | { |
384 | msgpack_pack_real_int8(x, d); |
385 | } |
386 | |
387 | msgpack_pack_inline_func(_unsigned_char)(msgpack_pack_user x, unsigned char d) |
388 | { |
389 | msgpack_pack_real_uint8(x, d); |
390 | } |
391 | |
392 | #ifdef msgpack_pack_inline_func_cint |
393 | |
394 | msgpack_pack_inline_func_cint(_short)(msgpack_pack_user x, short d) |
395 | { |
396 | #if defined(SIZEOF_SHORT) |
397 | #if SIZEOF_SHORT == 2 |
398 | msgpack_pack_real_int16(x, d); |
399 | #elif SIZEOF_SHORT == 4 |
400 | msgpack_pack_real_int32(x, d); |
401 | #else |
402 | msgpack_pack_real_int64(x, d); |
403 | #endif |
404 | |
405 | #elif defined(SHRT_MAX) |
406 | #if SHRT_MAX == 0x7fff |
407 | msgpack_pack_real_int16(x, d); |
408 | #elif SHRT_MAX == 0x7fffffff |
409 | msgpack_pack_real_int32(x, d); |
410 | #else |
411 | msgpack_pack_real_int64(x, d); |
412 | #endif |
413 | |
414 | #else |
415 | if(sizeof(short) == 2) { |
416 | msgpack_pack_real_int16(x, d); |
417 | } else if(sizeof(short) == 4) { |
418 | msgpack_pack_real_int32(x, d); |
419 | } else { |
420 | msgpack_pack_real_int64(x, d); |
421 | } |
422 | #endif |
423 | } |
424 | |
425 | msgpack_pack_inline_func_cint(_int)(msgpack_pack_user x, int d) |
426 | { |
427 | #if defined(SIZEOF_INT) |
428 | #if SIZEOF_INT == 2 |
429 | msgpack_pack_real_int16(x, d); |
430 | #elif SIZEOF_INT == 4 |
431 | msgpack_pack_real_int32(x, d); |
432 | #else |
433 | msgpack_pack_real_int64(x, d); |
434 | #endif |
435 | |
436 | #elif defined(INT_MAX) |
437 | #if INT_MAX == 0x7fff |
438 | msgpack_pack_real_int16(x, d); |
439 | #elif INT_MAX == 0x7fffffff |
440 | msgpack_pack_real_int32(x, d); |
441 | #else |
442 | msgpack_pack_real_int64(x, d); |
443 | #endif |
444 | |
445 | #else |
446 | if(sizeof(int) == 2) { |
447 | msgpack_pack_real_int16(x, d); |
448 | } else if(sizeof(int) == 4) { |
449 | msgpack_pack_real_int32(x, d); |
450 | } else { |
451 | msgpack_pack_real_int64(x, d); |
452 | } |
453 | #endif |
454 | } |
455 | |
456 | msgpack_pack_inline_func_cint(_long)(msgpack_pack_user x, long d) |
457 | { |
458 | #if defined(SIZEOF_LONG) |
459 | #if SIZEOF_LONG == 2 |
460 | msgpack_pack_real_int16(x, d); |
461 | #elif SIZEOF_LONG == 4 |
462 | msgpack_pack_real_int32(x, d); |
463 | #else |
464 | msgpack_pack_real_int64(x, d); |
465 | #endif |
466 | |
467 | #elif defined(LONG_MAX) |
468 | #if LONG_MAX == 0x7fffL |
469 | msgpack_pack_real_int16(x, d); |
470 | #elif LONG_MAX == 0x7fffffffL |
471 | msgpack_pack_real_int32(x, d); |
472 | #else |
473 | msgpack_pack_real_int64(x, d); |
474 | #endif |
475 | |
476 | #else |
477 | if(sizeof(long) == 2) { |
478 | msgpack_pack_real_int16(x, d); |
479 | } else if(sizeof(long) == 4) { |
480 | msgpack_pack_real_int32(x, d); |
481 | } else { |
482 | msgpack_pack_real_int64(x, d); |
483 | } |
484 | #endif |
485 | } |
486 | |
487 | msgpack_pack_inline_func_cint(_long_long)(msgpack_pack_user x, long long d) |
488 | { |
489 | #if defined(SIZEOF_LONG_LONG) |
490 | #if SIZEOF_LONG_LONG == 2 |
491 | msgpack_pack_real_int16(x, d); |
492 | #elif SIZEOF_LONG_LONG == 4 |
493 | msgpack_pack_real_int32(x, d); |
494 | #else |
495 | msgpack_pack_real_int64(x, d); |
496 | #endif |
497 | |
498 | #elif defined(LLONG_MAX) |
499 | #if LLONG_MAX == 0x7fffL |
500 | msgpack_pack_real_int16(x, d); |
501 | #elif LLONG_MAX == 0x7fffffffL |
502 | msgpack_pack_real_int32(x, d); |
503 | #else |
504 | msgpack_pack_real_int64(x, d); |
505 | #endif |
506 | |
507 | #else |
508 | if(sizeof(long long) == 2) { |
509 | msgpack_pack_real_int16(x, d); |
510 | } else if(sizeof(long long) == 4) { |
511 | msgpack_pack_real_int32(x, d); |
512 | } else { |
513 | msgpack_pack_real_int64(x, d); |
514 | } |
515 | #endif |
516 | } |
517 | |
518 | msgpack_pack_inline_func_cint(_unsigned_short)(msgpack_pack_user x, unsigned short d) |
519 | { |
520 | #if defined(SIZEOF_SHORT) |
521 | #if SIZEOF_SHORT == 2 |
522 | msgpack_pack_real_uint16(x, d); |
523 | #elif SIZEOF_SHORT == 4 |
524 | msgpack_pack_real_uint32(x, d); |
525 | #else |
526 | msgpack_pack_real_uint64(x, d); |
527 | #endif |
528 | |
529 | #elif defined(USHRT_MAX) |
530 | #if USHRT_MAX == 0xffffU |
531 | msgpack_pack_real_uint16(x, d); |
532 | #elif USHRT_MAX == 0xffffffffU |
533 | msgpack_pack_real_uint32(x, d); |
534 | #else |
535 | msgpack_pack_real_uint64(x, d); |
536 | #endif |
537 | |
538 | #else |
539 | if(sizeof(unsigned short) == 2) { |
540 | msgpack_pack_real_uint16(x, d); |
541 | } else if(sizeof(unsigned short) == 4) { |
542 | msgpack_pack_real_uint32(x, d); |
543 | } else { |
544 | msgpack_pack_real_uint64(x, d); |
545 | } |
546 | #endif |
547 | } |
548 | |
549 | msgpack_pack_inline_func_cint(_unsigned_int)(msgpack_pack_user x, unsigned int d) |
550 | { |
551 | #if defined(SIZEOF_INT) |
552 | #if SIZEOF_INT == 2 |
553 | msgpack_pack_real_uint16(x, d); |
554 | #elif SIZEOF_INT == 4 |
555 | msgpack_pack_real_uint32(x, d); |
556 | #else |
557 | msgpack_pack_real_uint64(x, d); |
558 | #endif |
559 | |
560 | #elif defined(UINT_MAX) |
561 | #if UINT_MAX == 0xffffU |
562 | msgpack_pack_real_uint16(x, d); |
563 | #elif UINT_MAX == 0xffffffffU |
564 | msgpack_pack_real_uint32(x, d); |
565 | #else |
566 | msgpack_pack_real_uint64(x, d); |
567 | #endif |
568 | |
569 | #else |
570 | if(sizeof(unsigned int) == 2) { |
571 | msgpack_pack_real_uint16(x, d); |
572 | } else if(sizeof(unsigned int) == 4) { |
573 | msgpack_pack_real_uint32(x, d); |
574 | } else { |
575 | msgpack_pack_real_uint64(x, d); |
576 | } |
577 | #endif |
578 | } |
579 | |
580 | msgpack_pack_inline_func_cint(_unsigned_long)(msgpack_pack_user x, unsigned long d) |
581 | { |
582 | #if defined(SIZEOF_LONG) |
583 | #if SIZEOF_LONG == 2 |
584 | msgpack_pack_real_uint16(x, d); |
585 | #elif SIZEOF_LONG == 4 |
586 | msgpack_pack_real_uint32(x, d); |
587 | #else |
588 | msgpack_pack_real_uint64(x, d); |
589 | #endif |
590 | |
591 | #elif defined(ULONG_MAX) |
592 | #if ULONG_MAX == 0xffffUL |
593 | msgpack_pack_real_uint16(x, d); |
594 | #elif ULONG_MAX == 0xffffffffUL |
595 | msgpack_pack_real_uint32(x, d); |
596 | #else |
597 | msgpack_pack_real_uint64(x, d); |
598 | #endif |
599 | |
600 | #else |
601 | if(sizeof(unsigned long) == 2) { |
602 | msgpack_pack_real_uint16(x, d); |
603 | } else if(sizeof(unsigned long) == 4) { |
604 | msgpack_pack_real_uint32(x, d); |
605 | } else { |
606 | msgpack_pack_real_uint64(x, d); |
607 | } |
608 | #endif |
609 | } |
610 | |
611 | msgpack_pack_inline_func_cint(_unsigned_long_long)(msgpack_pack_user x, unsigned long long d) |
612 | { |
613 | #if defined(SIZEOF_LONG_LONG) |
614 | #if SIZEOF_LONG_LONG == 2 |
615 | msgpack_pack_real_uint16(x, d); |
616 | #elif SIZEOF_LONG_LONG == 4 |
617 | msgpack_pack_real_uint32(x, d); |
618 | #else |
619 | msgpack_pack_real_uint64(x, d); |
620 | #endif |
621 | |
622 | #elif defined(ULLONG_MAX) |
623 | #if ULLONG_MAX == 0xffffUL |
624 | msgpack_pack_real_uint16(x, d); |
625 | #elif ULLONG_MAX == 0xffffffffUL |
626 | msgpack_pack_real_uint32(x, d); |
627 | #else |
628 | msgpack_pack_real_uint64(x, d); |
629 | #endif |
630 | |
631 | #else |
632 | if(sizeof(unsigned long long) == 2) { |
633 | msgpack_pack_real_uint16(x, d); |
634 | } else if(sizeof(unsigned long long) == 4) { |
635 | msgpack_pack_real_uint32(x, d); |
636 | } else { |
637 | msgpack_pack_real_uint64(x, d); |
638 | } |
639 | #endif |
640 | } |
641 | |
642 | #undef msgpack_pack_inline_func_cint |
643 | #endif |
644 | |
645 | |
646 | |
647 | /* |
648 | * Float |
649 | */ |
650 | |
651 | msgpack_pack_inline_func(_float)(msgpack_pack_user x, float d) |
652 | { |
653 | unsigned char buf[5]; |
654 | union { float f; uint32_t i; } mem; |
655 | mem.f = d; |
656 | buf[0] = 0xca; _msgpack_store32(&buf[1], mem.i); |
657 | msgpack_pack_append_buffer(x, buf, 5); |
658 | } |
659 | |
660 | msgpack_pack_inline_func(_double)(msgpack_pack_user x, double d) |
661 | { |
662 | unsigned char buf[9]; |
663 | union { double f; uint64_t i; } mem; |
664 | mem.f = d; |
665 | buf[0] = 0xcb; |
666 | #if defined(TARGET_OS_IPHONE) |
667 | // ok |
668 | #elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi |
669 | // https://github.com/msgpack/msgpack-perl/pull/1 |
670 | mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL); |
671 | #endif |
672 | _msgpack_store64(&buf[1], mem.i); |
673 | msgpack_pack_append_buffer(x, buf, 9); |
674 | } |
675 | |
676 | |
677 | /* |
678 | * Nil |
679 | */ |
680 | |
681 | msgpack_pack_inline_func(_nil)(msgpack_pack_user x) |
682 | { |
683 | static const unsigned char d = 0xc0; |
684 | msgpack_pack_append_buffer(x, &d, 1); |
685 | } |
686 | |
687 | |
688 | /* |
689 | * Boolean |
690 | */ |
691 | |
692 | msgpack_pack_inline_func(_true)(msgpack_pack_user x) |
693 | { |
694 | static const unsigned char d = 0xc3; |
695 | msgpack_pack_append_buffer(x, &d, 1); |
696 | } |
697 | |
698 | msgpack_pack_inline_func(_false)(msgpack_pack_user x) |
699 | { |
700 | static const unsigned char d = 0xc2; |
701 | msgpack_pack_append_buffer(x, &d, 1); |
702 | } |
703 | |
704 | |
705 | /* |
706 | * Array |
707 | */ |
708 | |
709 | msgpack_pack_inline_func(_array)(msgpack_pack_user x, size_t n) |
710 | { |
711 | if(n < 16) { |
712 | unsigned char d = 0x90 | (uint8_t)n; |
713 | msgpack_pack_append_buffer(x, &d, 1); |
714 | } else if(n < 65536) { |
715 | unsigned char buf[3]; |
716 | buf[0] = 0xdc; _msgpack_store16(&buf[1], (uint16_t)n); |
717 | msgpack_pack_append_buffer(x, buf, 3); |
718 | } else { |
719 | unsigned char buf[5]; |
720 | buf[0] = 0xdd; _msgpack_store32(&buf[1], (uint32_t)n); |
721 | msgpack_pack_append_buffer(x, buf, 5); |
722 | } |
723 | } |
724 | |
725 | |
726 | /* |
727 | * Map |
728 | */ |
729 | |
730 | msgpack_pack_inline_func(_map)(msgpack_pack_user x, size_t n) |
731 | { |
732 | if(n < 16) { |
733 | unsigned char d = 0x80 | (uint8_t)n; |
734 | msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); |
735 | } else if(n < 65536) { |
736 | unsigned char buf[3]; |
737 | buf[0] = 0xde; _msgpack_store16(&buf[1], (uint16_t)n); |
738 | msgpack_pack_append_buffer(x, buf, 3); |
739 | } else { |
740 | unsigned char buf[5]; |
741 | buf[0] = 0xdf; _msgpack_store32(&buf[1], (uint32_t)n); |
742 | msgpack_pack_append_buffer(x, buf, 5); |
743 | } |
744 | } |
745 | |
746 | |
747 | /* |
748 | * Str |
749 | */ |
750 | |
751 | msgpack_pack_inline_func(_str)(msgpack_pack_user x, size_t l) |
752 | { |
753 | if(l < 32) { |
754 | unsigned char d = 0xa0 | (uint8_t)l; |
755 | msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); |
756 | } else if(l < 256) { |
757 | unsigned char buf[2]; |
758 | buf[0] = 0xd9; buf[1] = (uint8_t)l; |
759 | msgpack_pack_append_buffer(x, buf, 2); |
760 | } else if(l < 65536) { |
761 | unsigned char buf[3]; |
762 | buf[0] = 0xda; _msgpack_store16(&buf[1], (uint16_t)l); |
763 | msgpack_pack_append_buffer(x, buf, 3); |
764 | } else { |
765 | unsigned char buf[5]; |
766 | buf[0] = 0xdb; _msgpack_store32(&buf[1], (uint32_t)l); |
767 | msgpack_pack_append_buffer(x, buf, 5); |
768 | } |
769 | } |
770 | |
771 | msgpack_pack_inline_func(_str_body)(msgpack_pack_user x, const void* b, size_t l) |
772 | { |
773 | msgpack_pack_append_buffer(x, (const unsigned char*)b, l); |
774 | } |
775 | |
776 | /* |
777 | * Raw (V4) |
778 | */ |
779 | |
780 | msgpack_pack_inline_func(_v4raw)(msgpack_pack_user x, size_t l) |
781 | { |
782 | if(l < 32) { |
783 | unsigned char d = 0xa0 | (uint8_t)l; |
784 | msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); |
785 | } else if(l < 65536) { |
786 | unsigned char buf[3]; |
787 | buf[0] = 0xda; _msgpack_store16(&buf[1], (uint16_t)l); |
788 | msgpack_pack_append_buffer(x, buf, 3); |
789 | } else { |
790 | unsigned char buf[5]; |
791 | buf[0] = 0xdb; _msgpack_store32(&buf[1], (uint32_t)l); |
792 | msgpack_pack_append_buffer(x, buf, 5); |
793 | } |
794 | } |
795 | |
796 | msgpack_pack_inline_func(_v4raw_body)(msgpack_pack_user x, const void* b, size_t l) |
797 | { |
798 | msgpack_pack_append_buffer(x, (const unsigned char*)b, l); |
799 | } |
800 | |
801 | /* |
802 | * Bin |
803 | */ |
804 | |
805 | msgpack_pack_inline_func(_bin)(msgpack_pack_user x, size_t l) |
806 | { |
807 | if(l < 256) { |
808 | unsigned char buf[2]; |
809 | buf[0] = 0xc4; buf[1] = (uint8_t)l; |
810 | msgpack_pack_append_buffer(x, buf, 2); |
811 | } else if(l < 65536) { |
812 | unsigned char buf[3]; |
813 | buf[0] = 0xc5; _msgpack_store16(&buf[1], (uint16_t)l); |
814 | msgpack_pack_append_buffer(x, buf, 3); |
815 | } else { |
816 | unsigned char buf[5]; |
817 | buf[0] = 0xc6; _msgpack_store32(&buf[1], (uint32_t)l); |
818 | msgpack_pack_append_buffer(x, buf, 5); |
819 | } |
820 | } |
821 | |
822 | msgpack_pack_inline_func(_bin_body)(msgpack_pack_user x, const void* b, size_t l) |
823 | { |
824 | msgpack_pack_append_buffer(x, (const unsigned char*)b, l); |
825 | } |
826 | |
827 | /* |
828 | * Ext |
829 | */ |
830 | |
831 | msgpack_pack_inline_func(_ext)(msgpack_pack_user x, size_t l, int8_t type) |
832 | { |
833 | switch(l) { |
834 | case 1: { |
835 | unsigned char buf[2]; |
836 | buf[0] = 0xd4; |
837 | buf[1] = type; |
838 | msgpack_pack_append_buffer(x, buf, 2); |
839 | } break; |
840 | case 2: { |
841 | unsigned char buf[2]; |
842 | buf[0] = 0xd5; |
843 | buf[1] = type; |
844 | msgpack_pack_append_buffer(x, buf, 2); |
845 | } break; |
846 | case 4: { |
847 | unsigned char buf[2]; |
848 | buf[0] = 0xd6; |
849 | buf[1] = type; |
850 | msgpack_pack_append_buffer(x, buf, 2); |
851 | } break; |
852 | case 8: { |
853 | unsigned char buf[2]; |
854 | buf[0] = 0xd7; |
855 | buf[1] = type; |
856 | msgpack_pack_append_buffer(x, buf, 2); |
857 | } break; |
858 | case 16: { |
859 | unsigned char buf[2]; |
860 | buf[0] = 0xd8; |
861 | buf[1] = type; |
862 | msgpack_pack_append_buffer(x, buf, 2); |
863 | } break; |
864 | default: |
865 | if(l < 256) { |
866 | unsigned char buf[3]; |
867 | buf[0] = 0xc7; |
868 | buf[1] = (unsigned char)l; |
869 | buf[2] = type; |
870 | msgpack_pack_append_buffer(x, buf, 3); |
871 | } else if(l < 65536) { |
872 | unsigned char buf[4]; |
873 | buf[0] = 0xc8; |
874 | _msgpack_store16(&buf[1], l); |
875 | buf[3] = type; |
876 | msgpack_pack_append_buffer(x, buf, 4); |
877 | } else { |
878 | unsigned char buf[6]; |
879 | buf[0] = 0xc9; |
880 | _msgpack_store32(&buf[1], l); |
881 | buf[5] = type; |
882 | msgpack_pack_append_buffer(x, buf, 6); |
883 | } |
884 | break; |
885 | } |
886 | } |
887 | |
888 | msgpack_pack_inline_func(_ext_body)(msgpack_pack_user x, const void* b, size_t l) |
889 | { |
890 | msgpack_pack_append_buffer(x, (const unsigned char*)b, l); |
891 | } |
892 | |
893 | #undef msgpack_pack_inline_func |
894 | #undef msgpack_pack_user |
895 | #undef msgpack_pack_append_buffer |
896 | |
897 | #undef TAKE8_8 |
898 | #undef TAKE8_16 |
899 | #undef TAKE8_32 |
900 | #undef TAKE8_64 |
901 | |
902 | #undef msgpack_pack_real_uint8 |
903 | #undef msgpack_pack_real_uint16 |
904 | #undef msgpack_pack_real_uint32 |
905 | #undef msgpack_pack_real_uint64 |
906 | #undef msgpack_pack_real_int8 |
907 | #undef msgpack_pack_real_int16 |
908 | #undef msgpack_pack_real_int32 |
909 | #undef msgpack_pack_real_int64 |
910 | |