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