pack_template.h 24 KB

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