pack_template.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. #if defined(__LITTLE_ENDIAN__)
  19. #define TAKE8_8(d) ((uint8_t*)&d)[0]
  20. #define TAKE8_16(d) ((uint8_t*)&d)[0]
  21. #define TAKE8_32(d) ((uint8_t*)&d)[0]
  22. #define TAKE8_64(d) ((uint8_t*)&d)[0]
  23. #elif defined(__BIG_ENDIAN__)
  24. #define TAKE8_8(d) ((uint8_t*)&d)[0]
  25. #define TAKE8_16(d) ((uint8_t*)&d)[1]
  26. #define TAKE8_32(d) ((uint8_t*)&d)[3]
  27. #define TAKE8_64(d) ((uint8_t*)&d)[7]
  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. static inline int msgpack_pack_uint8(msgpack_packer* x, uint8_t d)
  254. {
  255. msgpack_pack_real_uint8(x, d);
  256. }
  257. static inline int msgpack_pack_uint16(msgpack_packer* x, uint16_t d)
  258. {
  259. msgpack_pack_real_uint16(x, d);
  260. }
  261. static inline int msgpack_pack_uint32(msgpack_packer* x, uint32_t d)
  262. {
  263. msgpack_pack_real_uint32(x, d);
  264. }
  265. static inline int msgpack_pack_uint64(msgpack_packer* x, uint64_t d)
  266. {
  267. msgpack_pack_real_uint64(x, d);
  268. }
  269. static inline int msgpack_pack_int8(msgpack_packer* x, int8_t d)
  270. {
  271. msgpack_pack_real_int8(x, d);
  272. }
  273. static inline int msgpack_pack_int16(msgpack_packer* x, int16_t d)
  274. {
  275. msgpack_pack_real_int16(x, d);
  276. }
  277. static inline int msgpack_pack_int32(msgpack_packer* x, int32_t d)
  278. {
  279. msgpack_pack_real_int32(x, d);
  280. }
  281. static inline int msgpack_pack_int64(msgpack_packer* x, int64_t d)
  282. {
  283. msgpack_pack_real_int64(x, d);
  284. }
  285. //#ifdef msgpack_pack_inline_func_cint
  286. static inline int msgpack_pack_short(msgpack_packer* x, short d)
  287. {
  288. #if defined(SIZEOF_SHORT)
  289. #if SIZEOF_SHORT == 2
  290. msgpack_pack_real_int16(x, d);
  291. #elif SIZEOF_SHORT == 4
  292. msgpack_pack_real_int32(x, d);
  293. #else
  294. msgpack_pack_real_int64(x, d);
  295. #endif
  296. #elif defined(SHRT_MAX)
  297. #if SHRT_MAX == 0x7fff
  298. msgpack_pack_real_int16(x, d);
  299. #elif SHRT_MAX == 0x7fffffff
  300. msgpack_pack_real_int32(x, d);
  301. #else
  302. msgpack_pack_real_int64(x, d);
  303. #endif
  304. #else
  305. if(sizeof(short) == 2) {
  306. msgpack_pack_real_int16(x, d);
  307. } else if(sizeof(short) == 4) {
  308. msgpack_pack_real_int32(x, d);
  309. } else {
  310. msgpack_pack_real_int64(x, d);
  311. }
  312. #endif
  313. }
  314. static inline int msgpack_pack_int(msgpack_packer* x, int d)
  315. {
  316. #if defined(SIZEOF_INT)
  317. #if SIZEOF_INT == 2
  318. msgpack_pack_real_int16(x, d);
  319. #elif SIZEOF_INT == 4
  320. msgpack_pack_real_int32(x, d);
  321. #else
  322. msgpack_pack_real_int64(x, d);
  323. #endif
  324. #elif defined(INT_MAX)
  325. #if INT_MAX == 0x7fff
  326. msgpack_pack_real_int16(x, d);
  327. #elif INT_MAX == 0x7fffffff
  328. msgpack_pack_real_int32(x, d);
  329. #else
  330. msgpack_pack_real_int64(x, d);
  331. #endif
  332. #else
  333. if(sizeof(int) == 2) {
  334. msgpack_pack_real_int16(x, d);
  335. } else if(sizeof(int) == 4) {
  336. msgpack_pack_real_int32(x, d);
  337. } else {
  338. msgpack_pack_real_int64(x, d);
  339. }
  340. #endif
  341. }
  342. static inline int msgpack_pack_long(msgpack_packer* x, long d)
  343. {
  344. #if defined(SIZEOF_LONG)
  345. #if SIZEOF_LONG == 2
  346. msgpack_pack_real_int16(x, d);
  347. #elif SIZEOF_LONG == 4
  348. msgpack_pack_real_int32(x, d);
  349. #else
  350. msgpack_pack_real_int64(x, d);
  351. #endif
  352. #elif defined(LONG_MAX)
  353. #if LONG_MAX == 0x7fffL
  354. msgpack_pack_real_int16(x, d);
  355. #elif LONG_MAX == 0x7fffffffL
  356. msgpack_pack_real_int32(x, d);
  357. #else
  358. msgpack_pack_real_int64(x, d);
  359. #endif
  360. #else
  361. if(sizeof(long) == 2) {
  362. msgpack_pack_real_int16(x, d);
  363. } else if(sizeof(long) == 4) {
  364. msgpack_pack_real_int32(x, d);
  365. } else {
  366. msgpack_pack_real_int64(x, d);
  367. }
  368. #endif
  369. }
  370. static inline int msgpack_pack_long_long(msgpack_packer* x, long long d)
  371. {
  372. #if defined(SIZEOF_LONG_LONG)
  373. #if SIZEOF_LONG_LONG == 2
  374. msgpack_pack_real_int16(x, d);
  375. #elif SIZEOF_LONG_LONG == 4
  376. msgpack_pack_real_int32(x, d);
  377. #else
  378. msgpack_pack_real_int64(x, d);
  379. #endif
  380. #elif defined(LLONG_MAX)
  381. #if LLONG_MAX == 0x7fffL
  382. msgpack_pack_real_int16(x, d);
  383. #elif LLONG_MAX == 0x7fffffffL
  384. msgpack_pack_real_int32(x, d);
  385. #else
  386. msgpack_pack_real_int64(x, d);
  387. #endif
  388. #else
  389. if(sizeof(long long) == 2) {
  390. msgpack_pack_real_int16(x, d);
  391. } else if(sizeof(long long) == 4) {
  392. msgpack_pack_real_int32(x, d);
  393. } else {
  394. msgpack_pack_real_int64(x, d);
  395. }
  396. #endif
  397. }
  398. static inline int msgpack_pack_unsigned_short(msgpack_packer* x, unsigned short d)
  399. {
  400. #if defined(SIZEOF_SHORT)
  401. #if SIZEOF_SHORT == 2
  402. msgpack_pack_real_uint16(x, d);
  403. #elif SIZEOF_SHORT == 4
  404. msgpack_pack_real_uint32(x, d);
  405. #else
  406. msgpack_pack_real_uint64(x, d);
  407. #endif
  408. #elif defined(USHRT_MAX)
  409. #if USHRT_MAX == 0xffffU
  410. msgpack_pack_real_uint16(x, d);
  411. #elif USHRT_MAX == 0xffffffffU
  412. msgpack_pack_real_uint32(x, d);
  413. #else
  414. msgpack_pack_real_uint64(x, d);
  415. #endif
  416. #else
  417. if(sizeof(unsigned short) == 2) {
  418. msgpack_pack_real_uint16(x, d);
  419. } else if(sizeof(unsigned short) == 4) {
  420. msgpack_pack_real_uint32(x, d);
  421. } else {
  422. msgpack_pack_real_uint64(x, d);
  423. }
  424. #endif
  425. }
  426. static inline int msgpack_pack_unsigned_int(msgpack_packer* x, unsigned int d)
  427. {
  428. #if defined(SIZEOF_INT)
  429. #if SIZEOF_INT == 2
  430. msgpack_pack_real_uint16(x, d);
  431. #elif SIZEOF_INT == 4
  432. msgpack_pack_real_uint32(x, d);
  433. #else
  434. msgpack_pack_real_uint64(x, d);
  435. #endif
  436. #elif defined(UINT_MAX)
  437. #if UINT_MAX == 0xffffU
  438. msgpack_pack_real_uint16(x, d);
  439. #elif UINT_MAX == 0xffffffffU
  440. msgpack_pack_real_uint32(x, d);
  441. #else
  442. msgpack_pack_real_uint64(x, d);
  443. #endif
  444. #else
  445. if(sizeof(unsigned int) == 2) {
  446. msgpack_pack_real_uint16(x, d);
  447. } else if(sizeof(unsigned int) == 4) {
  448. msgpack_pack_real_uint32(x, d);
  449. } else {
  450. msgpack_pack_real_uint64(x, d);
  451. }
  452. #endif
  453. }
  454. static inline int msgpack_pack_unsigned_long(msgpack_packer* x, unsigned long d)
  455. {
  456. #if defined(SIZEOF_LONG)
  457. #if SIZEOF_LONG == 2
  458. msgpack_pack_real_uint16(x, d);
  459. #elif SIZEOF_LONG == 4
  460. msgpack_pack_real_uint32(x, d);
  461. #else
  462. msgpack_pack_real_uint64(x, d);
  463. #endif
  464. #elif defined(ULONG_MAX)
  465. #if ULONG_MAX == 0xffffUL
  466. msgpack_pack_real_uint16(x, d);
  467. #elif ULONG_MAX == 0xffffffffUL
  468. msgpack_pack_real_uint32(x, d);
  469. #else
  470. msgpack_pack_real_uint64(x, d);
  471. #endif
  472. #else
  473. if(sizeof(unsigned long) == 2) {
  474. msgpack_pack_real_uint16(x, d);
  475. } else if(sizeof(unsigned long) == 4) {
  476. msgpack_pack_real_uint32(x, d);
  477. } else {
  478. msgpack_pack_real_uint64(x, d);
  479. }
  480. #endif
  481. }
  482. static inline int msgpack_pack_unsigned_long_long(msgpack_packer* x, unsigned long long d)
  483. {
  484. #if defined(SIZEOF_LONG_LONG)
  485. #if SIZEOF_LONG_LONG == 2
  486. msgpack_pack_real_uint16(x, d);
  487. #elif SIZEOF_LONG_LONG == 4
  488. msgpack_pack_real_uint32(x, d);
  489. #else
  490. msgpack_pack_real_uint64(x, d);
  491. #endif
  492. #elif defined(ULLONG_MAX)
  493. #if ULLONG_MAX == 0xffffUL
  494. msgpack_pack_real_uint16(x, d);
  495. #elif ULLONG_MAX == 0xffffffffUL
  496. msgpack_pack_real_uint32(x, d);
  497. #else
  498. msgpack_pack_real_uint64(x, d);
  499. #endif
  500. #else
  501. if(sizeof(unsigned long long) == 2) {
  502. msgpack_pack_real_uint16(x, d);
  503. } else if(sizeof(unsigned long long) == 4) {
  504. msgpack_pack_real_uint32(x, d);
  505. } else {
  506. msgpack_pack_real_uint64(x, d);
  507. }
  508. #endif
  509. }
  510. //#undef msgpack_pack_inline_func_cint
  511. //#endif
  512. /*
  513. * Float
  514. */
  515. static inline int msgpack_pack_float(msgpack_packer* x, float d)
  516. {
  517. union { float f; uint32_t i; } mem;
  518. mem.f = d;
  519. unsigned char buf[5];
  520. buf[0] = 0xca; _msgpack_store32(&buf[1], mem.i);
  521. msgpack_pack_append_buffer(x, buf, 5);
  522. }
  523. static inline int msgpack_pack_double(msgpack_packer* x, double d)
  524. {
  525. union { double f; uint64_t i; } mem;
  526. mem.f = d;
  527. unsigned char buf[9];
  528. buf[0] = 0xcb;
  529. #if defined(__arm__) && !(__ARM_EABI__) // arm-oabi
  530. // https://github.com/msgpack/msgpack-perl/pull/1
  531. mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
  532. #endif
  533. _msgpack_store64(&buf[1], mem.i);
  534. msgpack_pack_append_buffer(x, buf, 9);
  535. }
  536. /*
  537. * Nil
  538. */
  539. static inline int msgpack_pack_nil(msgpack_packer* x)
  540. {
  541. static const unsigned char d = 0xc0;
  542. msgpack_pack_append_buffer(x, &d, 1);
  543. }
  544. /*
  545. * Boolean
  546. */
  547. static inline int msgpack_pack_true(msgpack_packer* x)
  548. {
  549. static const unsigned char d = 0xc3;
  550. msgpack_pack_append_buffer(x, &d, 1);
  551. }
  552. static inline int msgpack_pack_false(msgpack_packer* x)
  553. {
  554. static const unsigned char d = 0xc2;
  555. msgpack_pack_append_buffer(x, &d, 1);
  556. }
  557. /*
  558. * Array
  559. */
  560. static inline int msgpack_pack_array(msgpack_packer* x, unsigned int n)
  561. {
  562. if(n < 16) {
  563. unsigned char d = 0x90 | n;
  564. msgpack_pack_append_buffer(x, &d, 1);
  565. } else if(n < 65536) {
  566. unsigned char buf[3];
  567. buf[0] = 0xdc; _msgpack_store16(&buf[1], (uint16_t)n);
  568. msgpack_pack_append_buffer(x, buf, 3);
  569. } else {
  570. unsigned char buf[5];
  571. buf[0] = 0xdd; _msgpack_store32(&buf[1], (uint32_t)n);
  572. msgpack_pack_append_buffer(x, buf, 5);
  573. }
  574. }
  575. /*
  576. * Map
  577. */
  578. static inline int msgpack_pack_map(msgpack_packer* x, unsigned int n)
  579. {
  580. if(n < 16) {
  581. unsigned char d = 0x80 | n;
  582. msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
  583. } else if(n < 65536) {
  584. unsigned char buf[3];
  585. buf[0] = 0xde; _msgpack_store16(&buf[1], (uint16_t)n);
  586. msgpack_pack_append_buffer(x, buf, 3);
  587. } else {
  588. unsigned char buf[5];
  589. buf[0] = 0xdf; _msgpack_store32(&buf[1], (uint32_t)n);
  590. msgpack_pack_append_buffer(x, buf, 5);
  591. }
  592. }
  593. /*
  594. * Raw
  595. */
  596. static inline int msgpack_pack_raw(msgpack_packer* x, size_t l)
  597. {
  598. if (l < 32) {
  599. unsigned char d = 0xa0 | (uint8_t)l;
  600. msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
  601. } else if (x->use_bin_type && l < 256) { // str8 is new format introduced with bin.
  602. unsigned char buf[2] = {0xd9, (uint8_t)l};
  603. msgpack_pack_append_buffer(x, buf, 2);
  604. } else if (l < 65536) {
  605. unsigned char buf[3];
  606. buf[0] = 0xda; _msgpack_store16(&buf[1], (uint16_t)l);
  607. msgpack_pack_append_buffer(x, buf, 3);
  608. } else {
  609. unsigned char buf[5];
  610. buf[0] = 0xdb; _msgpack_store32(&buf[1], (uint32_t)l);
  611. msgpack_pack_append_buffer(x, buf, 5);
  612. }
  613. }
  614. /*
  615. * bin
  616. */
  617. static inline int msgpack_pack_bin(msgpack_packer *x, size_t l)
  618. {
  619. if (!x->use_bin_type) {
  620. return msgpack_pack_raw(x, l);
  621. }
  622. if (l < 256) {
  623. unsigned char buf[2] = {0xc4, (unsigned char)l};
  624. msgpack_pack_append_buffer(x, buf, 2);
  625. } else if (l < 65536) {
  626. unsigned char buf[3] = {0xc5};
  627. _msgpack_store16(&buf[1], (uint16_t)l);
  628. msgpack_pack_append_buffer(x, buf, 3);
  629. } else {
  630. unsigned char buf[5] = {0xc6};
  631. _msgpack_store32(&buf[1], (uint32_t)l);
  632. msgpack_pack_append_buffer(x, buf, 5);
  633. }
  634. }
  635. static inline int msgpack_pack_raw_body(msgpack_packer* x, const void* b, size_t l)
  636. {
  637. if (l > 0) msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
  638. return 0;
  639. }
  640. /*
  641. * Ext
  642. */
  643. static inline int msgpack_pack_ext(msgpack_packer* x, int8_t typecode, size_t l)
  644. {
  645. if (l == 1) {
  646. unsigned char buf[2];
  647. buf[0] = 0xd4;
  648. buf[1] = (unsigned char)typecode;
  649. msgpack_pack_append_buffer(x, buf, 2);
  650. }
  651. else if(l == 2) {
  652. unsigned char buf[2];
  653. buf[0] = 0xd5;
  654. buf[1] = (unsigned char)typecode;
  655. msgpack_pack_append_buffer(x, buf, 2);
  656. }
  657. else if(l == 4) {
  658. unsigned char buf[2];
  659. buf[0] = 0xd6;
  660. buf[1] = (unsigned char)typecode;
  661. msgpack_pack_append_buffer(x, buf, 2);
  662. }
  663. else if(l == 8) {
  664. unsigned char buf[2];
  665. buf[0] = 0xd7;
  666. buf[1] = (unsigned char)typecode;
  667. msgpack_pack_append_buffer(x, buf, 2);
  668. }
  669. else if(l == 16) {
  670. unsigned char buf[2];
  671. buf[0] = 0xd8;
  672. buf[1] = (unsigned char)typecode;
  673. msgpack_pack_append_buffer(x, buf, 2);
  674. }
  675. else if(l < 256) {
  676. unsigned char buf[3];
  677. buf[0] = 0xc7;
  678. buf[1] = l;
  679. buf[2] = (unsigned char)typecode;
  680. msgpack_pack_append_buffer(x, buf, 3);
  681. } else if(l < 65536) {
  682. unsigned char buf[4];
  683. buf[0] = 0xc8;
  684. _msgpack_store16(&buf[1], (uint16_t)l);
  685. buf[3] = (unsigned char)typecode;
  686. msgpack_pack_append_buffer(x, buf, 4);
  687. } else {
  688. unsigned char buf[6];
  689. buf[0] = 0xc9;
  690. _msgpack_store32(&buf[1], (uint32_t)l);
  691. buf[5] = (unsigned char)typecode;
  692. msgpack_pack_append_buffer(x, buf, 6);
  693. }
  694. }
  695. #undef msgpack_pack_append_buffer
  696. #undef TAKE8_8
  697. #undef TAKE8_16
  698. #undef TAKE8_32
  699. #undef TAKE8_64
  700. #undef msgpack_pack_real_uint8
  701. #undef msgpack_pack_real_uint16
  702. #undef msgpack_pack_real_uint32
  703. #undef msgpack_pack_real_uint64
  704. #undef msgpack_pack_real_int8
  705. #undef msgpack_pack_real_int16
  706. #undef msgpack_pack_real_int32
  707. #undef msgpack_pack_real_int64