PageRenderTime 86ms CodeModel.GetById 45ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/lua/src/lua_cmsgpack.c

https://bitbucket.org/eastfox2002/annotated_redis_source
C | 729 lines | 567 code | 70 blank | 92 comment | 85 complexity | cf874289f280ec4f40771d05c456649f MD5 | raw file
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "lua.h"
  7. #include "lauxlib.h"
  8. #define LUACMSGPACK_VERSION "lua-cmsgpack 0.3.0"
  9. #define LUACMSGPACK_COPYRIGHT "Copyright (C) 2012, Salvatore Sanfilippo"
  10. #define LUACMSGPACK_DESCRIPTION "MessagePack C implementation for Lua"
  11. #define LUACMSGPACK_MAX_NESTING 16 /* Max tables nesting. */
  12. /* ==============================================================================
  13. * MessagePack implementation and bindings for Lua 5.1.
  14. * Copyright(C) 2012 Salvatore Sanfilippo <antirez@gmail.com>
  15. *
  16. * http://github.com/antirez/lua-cmsgpack
  17. *
  18. * For MessagePack specification check the following web site:
  19. * http://wiki.msgpack.org/display/MSGPACK/Format+specification
  20. *
  21. * See Copyright Notice at the end of this file.
  22. *
  23. * CHANGELOG:
  24. * 19-Feb-2012 (ver 0.1.0): Initial release.
  25. * 20-Feb-2012 (ver 0.2.0): Tables encoding improved.
  26. * 20-Feb-2012 (ver 0.2.1): Minor bug fixing.
  27. * 20-Feb-2012 (ver 0.3.0): Module renamed lua-cmsgpack (was lua-msgpack).
  28. * ============================================================================ */
  29. /* --------------------------- Endian conversion --------------------------------
  30. * We use it only for floats and doubles, all the other conversions are performed
  31. * in an endian independent fashion. So the only thing we need is a function
  32. * that swaps a binary string if the arch is little endian (and left it untouched
  33. * otherwise). */
  34. /* Reverse memory bytes if arch is little endian. Given the conceptual
  35. * simplicity of the Lua build system we prefer to check for endianess at runtime.
  36. * The performance difference should be acceptable. */
  37. static void memrevifle(void *ptr, size_t len) {
  38. unsigned char *p = ptr, *e = p+len-1, aux;
  39. int test = 1;
  40. unsigned char *testp = (unsigned char*) &test;
  41. if (testp[0] == 0) return; /* Big endian, nothign to do. */
  42. len /= 2;
  43. while(len--) {
  44. aux = *p;
  45. *p = *e;
  46. *e = aux;
  47. p++;
  48. e--;
  49. }
  50. }
  51. /* ----------------------------- String buffer ----------------------------------
  52. * This is a simple implementation of string buffers. The only opereation
  53. * supported is creating empty buffers and appending bytes to it.
  54. * The string buffer uses 2x preallocation on every realloc for O(N) append
  55. * behavior. */
  56. typedef struct mp_buf {
  57. unsigned char *b;
  58. size_t len, free;
  59. } mp_buf;
  60. static mp_buf *mp_buf_new(void) {
  61. mp_buf *buf = malloc(sizeof(*buf));
  62. buf->b = NULL;
  63. buf->len = buf->free = 0;
  64. return buf;
  65. }
  66. void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) {
  67. if (buf->free < len) {
  68. size_t newlen = buf->len+len;
  69. buf->b = realloc(buf->b,newlen*2);
  70. buf->free = newlen;
  71. }
  72. memcpy(buf->b+buf->len,s,len);
  73. buf->len += len;
  74. buf->free -= len;
  75. }
  76. void mp_buf_free(mp_buf *buf) {
  77. free(buf->b);
  78. free(buf);
  79. }
  80. /* ------------------------------ String cursor ----------------------------------
  81. * This simple data structure is used for parsing. Basically you create a cursor
  82. * using a string pointer and a length, then it is possible to access the
  83. * current string position with cursor->p, check the remaining length
  84. * in cursor->left, and finally consume more string using
  85. * mp_cur_consume(cursor,len), to advance 'p' and subtract 'left'.
  86. * An additional field cursor->error is set to zero on initialization and can
  87. * be used to report errors. */
  88. #define MP_CUR_ERROR_NONE 0
  89. #define MP_CUR_ERROR_EOF 1 /* Not enough data to complete the opereation. */
  90. #define MP_CUR_ERROR_BADFMT 2 /* Bad data format */
  91. typedef struct mp_cur {
  92. const unsigned char *p;
  93. size_t left;
  94. int err;
  95. } mp_cur;
  96. static mp_cur *mp_cur_new(const unsigned char *s, size_t len) {
  97. mp_cur *cursor = malloc(sizeof(*cursor));
  98. cursor->p = s;
  99. cursor->left = len;
  100. cursor->err = MP_CUR_ERROR_NONE;
  101. return cursor;
  102. }
  103. static void mp_cur_free(mp_cur *cursor) {
  104. free(cursor);
  105. }
  106. #define mp_cur_consume(_c,_len) do { _c->p += _len; _c->left -= _len; } while(0)
  107. /* When there is not enough room we set an error in the cursor and return, this
  108. * is very common across the code so we have a macro to make the code look
  109. * a bit simpler. */
  110. #define mp_cur_need(_c,_len) do { \
  111. if (_c->left < _len) { \
  112. _c->err = MP_CUR_ERROR_EOF; \
  113. return; \
  114. } \
  115. } while(0)
  116. /* --------------------------- Low level MP encoding -------------------------- */
  117. static void mp_encode_bytes(mp_buf *buf, const unsigned char *s, size_t len) {
  118. unsigned char hdr[5];
  119. int hdrlen;
  120. if (len < 32) {
  121. hdr[0] = 0xa0 | (len&0xff); /* fix raw */
  122. hdrlen = 1;
  123. } else if (len <= 0xffff) {
  124. hdr[0] = 0xda;
  125. hdr[1] = (len&0xff00)>>8;
  126. hdr[2] = len&0xff;
  127. hdrlen = 3;
  128. } else {
  129. hdr[0] = 0xdb;
  130. hdr[1] = (len&0xff000000)>>24;
  131. hdr[2] = (len&0xff0000)>>16;
  132. hdr[3] = (len&0xff00)>>8;
  133. hdr[4] = len&0xff;
  134. hdrlen = 5;
  135. }
  136. mp_buf_append(buf,hdr,hdrlen);
  137. mp_buf_append(buf,s,len);
  138. }
  139. /* we assume IEEE 754 internal format for single and double precision floats. */
  140. static void mp_encode_double(mp_buf *buf, double d) {
  141. unsigned char b[9];
  142. float f = d;
  143. assert(sizeof(f) == 4 && sizeof(d) == 8);
  144. if (d == (double)f) {
  145. b[0] = 0xca; /* float IEEE 754 */
  146. memcpy(b+1,&f,4);
  147. memrevifle(b+1,4);
  148. mp_buf_append(buf,b,5);
  149. } else if (sizeof(d) == 8) {
  150. b[0] = 0xcb; /* double IEEE 754 */
  151. memcpy(b+1,&d,8);
  152. memrevifle(b+1,8);
  153. mp_buf_append(buf,b,9);
  154. }
  155. }
  156. static void mp_encode_int(mp_buf *buf, int64_t n) {
  157. unsigned char b[9];
  158. int enclen;
  159. if (n >= 0) {
  160. if (n <= 127) {
  161. b[0] = n & 0x7f; /* positive fixnum */
  162. enclen = 1;
  163. } else if (n <= 0xff) {
  164. b[0] = 0xcc; /* uint 8 */
  165. b[1] = n & 0xff;
  166. enclen = 2;
  167. } else if (n <= 0xffff) {
  168. b[0] = 0xcd; /* uint 16 */
  169. b[1] = (n & 0xff00) >> 8;
  170. b[2] = n & 0xff;
  171. enclen = 3;
  172. } else if (n <= 0xffffffffLL) {
  173. b[0] = 0xce; /* uint 32 */
  174. b[1] = (n & 0xff000000) >> 24;
  175. b[2] = (n & 0xff0000) >> 16;
  176. b[3] = (n & 0xff00) >> 8;
  177. b[4] = n & 0xff;
  178. enclen = 5;
  179. } else {
  180. b[0] = 0xcf; /* uint 64 */
  181. b[1] = (n & 0xff00000000000000LL) >> 56;
  182. b[2] = (n & 0xff000000000000LL) >> 48;
  183. b[3] = (n & 0xff0000000000LL) >> 40;
  184. b[4] = (n & 0xff00000000LL) >> 32;
  185. b[5] = (n & 0xff000000) >> 24;
  186. b[6] = (n & 0xff0000) >> 16;
  187. b[7] = (n & 0xff00) >> 8;
  188. b[8] = n & 0xff;
  189. enclen = 9;
  190. }
  191. } else {
  192. if (n >= -32) {
  193. b[0] = ((char)n); /* negative fixnum */
  194. enclen = 1;
  195. } else if (n >= -128) {
  196. b[0] = 0xd0; /* int 8 */
  197. b[1] = n & 0xff;
  198. enclen = 2;
  199. } else if (n >= -32768) {
  200. b[0] = 0xd1; /* int 16 */
  201. b[1] = (n & 0xff00) >> 8;
  202. b[2] = n & 0xff;
  203. enclen = 3;
  204. } else if (n >= -2147483648LL) {
  205. b[0] = 0xd2; /* int 32 */
  206. b[1] = (n & 0xff000000) >> 24;
  207. b[2] = (n & 0xff0000) >> 16;
  208. b[3] = (n & 0xff00) >> 8;
  209. b[4] = n & 0xff;
  210. enclen = 5;
  211. } else {
  212. b[0] = 0xd3; /* int 64 */
  213. b[1] = (n & 0xff00000000000000LL) >> 56;
  214. b[2] = (n & 0xff000000000000LL) >> 48;
  215. b[3] = (n & 0xff0000000000LL) >> 40;
  216. b[4] = (n & 0xff00000000LL) >> 32;
  217. b[5] = (n & 0xff000000) >> 24;
  218. b[6] = (n & 0xff0000) >> 16;
  219. b[7] = (n & 0xff00) >> 8;
  220. b[8] = n & 0xff;
  221. enclen = 9;
  222. }
  223. }
  224. mp_buf_append(buf,b,enclen);
  225. }
  226. static void mp_encode_array(mp_buf *buf, int64_t n) {
  227. unsigned char b[5];
  228. int enclen;
  229. if (n <= 15) {
  230. b[0] = 0x90 | (n & 0xf); /* fix array */
  231. enclen = 1;
  232. } else if (n <= 65535) {
  233. b[0] = 0xdc; /* array 16 */
  234. b[1] = (n & 0xff00) >> 8;
  235. b[2] = n & 0xff;
  236. enclen = 3;
  237. } else {
  238. b[0] = 0xdd; /* array 32 */
  239. b[1] = (n & 0xff000000) >> 24;
  240. b[2] = (n & 0xff0000) >> 16;
  241. b[3] = (n & 0xff00) >> 8;
  242. b[4] = n & 0xff;
  243. enclen = 5;
  244. }
  245. mp_buf_append(buf,b,enclen);
  246. }
  247. static void mp_encode_map(mp_buf *buf, int64_t n) {
  248. unsigned char b[5];
  249. int enclen;
  250. if (n <= 15) {
  251. b[0] = 0x80 | (n & 0xf); /* fix map */
  252. enclen = 1;
  253. } else if (n <= 65535) {
  254. b[0] = 0xde; /* map 16 */
  255. b[1] = (n & 0xff00) >> 8;
  256. b[2] = n & 0xff;
  257. enclen = 3;
  258. } else {
  259. b[0] = 0xdf; /* map 32 */
  260. b[1] = (n & 0xff000000) >> 24;
  261. b[2] = (n & 0xff0000) >> 16;
  262. b[3] = (n & 0xff00) >> 8;
  263. b[4] = n & 0xff;
  264. enclen = 5;
  265. }
  266. mp_buf_append(buf,b,enclen);
  267. }
  268. /* ----------------------------- Lua types encoding --------------------------- */
  269. static void mp_encode_lua_string(lua_State *L, mp_buf *buf) {
  270. size_t len;
  271. const char *s;
  272. s = lua_tolstring(L,-1,&len);
  273. mp_encode_bytes(buf,(const unsigned char*)s,len);
  274. }
  275. static void mp_encode_lua_bool(lua_State *L, mp_buf *buf) {
  276. unsigned char b = lua_toboolean(L,-1) ? 0xc3 : 0xc2;
  277. mp_buf_append(buf,&b,1);
  278. }
  279. static void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
  280. lua_Number n = lua_tonumber(L,-1);
  281. if (floor(n) != n) {
  282. mp_encode_double(buf,(double)n);
  283. } else {
  284. mp_encode_int(buf,(int64_t)n);
  285. }
  286. }
  287. static void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level);
  288. /* Convert a lua table into a message pack list. */
  289. static void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
  290. size_t len = lua_objlen(L,-1), j;
  291. mp_encode_array(buf,len);
  292. for (j = 1; j <= len; j++) {
  293. lua_pushnumber(L,j);
  294. lua_gettable(L,-2);
  295. mp_encode_lua_type(L,buf,level+1);
  296. }
  297. }
  298. /* Convert a lua table into a message pack key-value map. */
  299. static void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
  300. size_t len = 0;
  301. /* First step: count keys into table. No other way to do it with the
  302. * Lua API, we need to iterate a first time. Note that an alternative
  303. * would be to do a single run, and then hack the buffer to insert the
  304. * map opcodes for message pack. Too hachish for this lib. */
  305. lua_pushnil(L);
  306. while(lua_next(L,-2)) {
  307. lua_pop(L,1); /* remove value, keep key for next iteration. */
  308. len++;
  309. }
  310. /* Step two: actually encoding of the map. */
  311. mp_encode_map(buf,len);
  312. lua_pushnil(L);
  313. while(lua_next(L,-2)) {
  314. /* Stack: ... key value */
  315. lua_pushvalue(L,-2); /* Stack: ... key value key */
  316. mp_encode_lua_type(L,buf,level+1); /* encode key */
  317. mp_encode_lua_type(L,buf,level+1); /* encode val */
  318. }
  319. }
  320. /* Returns true if the Lua table on top of the stack is exclusively composed
  321. * of keys from numerical keys from 1 up to N, with N being the total number
  322. * of elements, without any hole in the middle. */
  323. static int table_is_an_array(lua_State *L) {
  324. long count = 0, max = 0, idx = 0;
  325. lua_Number n;
  326. lua_pushnil(L);
  327. while(lua_next(L,-2)) {
  328. /* Stack: ... key value */
  329. lua_pop(L,1); /* Stack: ... key */
  330. if (!lua_isnumber(L,-1)) goto not_array;
  331. n = lua_tonumber(L,-1);
  332. idx = n;
  333. if (idx != n || idx < 1) goto not_array;
  334. count++;
  335. max = idx;
  336. }
  337. /* We have the total number of elements in "count". Also we have
  338. * the max index encountered in "idx". We can't reach this code
  339. * if there are indexes <= 0. If you also note that there can not be
  340. * repeated keys into a table, you have that if idx==count you are sure
  341. * that there are all the keys form 1 to count (both included). */
  342. return idx == count;
  343. not_array:
  344. lua_pop(L,1);
  345. return 0;
  346. }
  347. /* If the length operator returns non-zero, that is, there is at least
  348. * an object at key '1', we serialize to message pack list. Otherwise
  349. * we use a map. */
  350. static void mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {
  351. if (table_is_an_array(L))
  352. mp_encode_lua_table_as_array(L,buf,level);
  353. else
  354. mp_encode_lua_table_as_map(L,buf,level);
  355. }
  356. static void mp_encode_lua_null(lua_State *L, mp_buf *buf) {
  357. unsigned char b[1];
  358. b[0] = 0xc0;
  359. mp_buf_append(buf,b,1);
  360. }
  361. static void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {
  362. int t = lua_type(L,-1);
  363. /* Limit the encoding of nested tables to a specfiied maximum depth, so that
  364. * we survive when called against circular references in tables. */
  365. if (t == LUA_TTABLE && level == LUACMSGPACK_MAX_NESTING) t = LUA_TNIL;
  366. switch(t) {
  367. case LUA_TSTRING: mp_encode_lua_string(L,buf); break;
  368. case LUA_TBOOLEAN: mp_encode_lua_bool(L,buf); break;
  369. case LUA_TNUMBER: mp_encode_lua_number(L,buf); break;
  370. case LUA_TTABLE: mp_encode_lua_table(L,buf,level); break;
  371. default: mp_encode_lua_null(L,buf); break;
  372. }
  373. lua_pop(L,1);
  374. }
  375. static int mp_pack(lua_State *L) {
  376. mp_buf *buf = mp_buf_new();
  377. mp_encode_lua_type(L,buf,0);
  378. lua_pushlstring(L,(char*)buf->b,buf->len);
  379. mp_buf_free(buf);
  380. return 1;
  381. }
  382. /* --------------------------------- Decoding --------------------------------- */
  383. void mp_decode_to_lua_type(lua_State *L, mp_cur *c);
  384. void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {
  385. int index = 1;
  386. lua_newtable(L);
  387. while(len--) {
  388. lua_pushnumber(L,index++);
  389. mp_decode_to_lua_type(L,c);
  390. if (c->err) return;
  391. lua_settable(L,-3);
  392. }
  393. }
  394. void mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {
  395. lua_newtable(L);
  396. while(len--) {
  397. mp_decode_to_lua_type(L,c); /* key */
  398. if (c->err) return;
  399. mp_decode_to_lua_type(L,c); /* value */
  400. if (c->err) return;
  401. lua_settable(L,-3);
  402. }
  403. }
  404. /* Decode a Message Pack raw object pointed by the string cursor 'c' to
  405. * a Lua type, that is left as the only result on the stack. */
  406. void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
  407. mp_cur_need(c,1);
  408. switch(c->p[0]) {
  409. case 0xcc: /* uint 8 */
  410. mp_cur_need(c,2);
  411. lua_pushnumber(L,c->p[1]);
  412. mp_cur_consume(c,2);
  413. break;
  414. case 0xd0: /* int 8 */
  415. mp_cur_need(c,2);
  416. lua_pushnumber(L,(char)c->p[1]);
  417. mp_cur_consume(c,2);
  418. break;
  419. case 0xcd: /* uint 16 */
  420. mp_cur_need(c,3);
  421. lua_pushnumber(L,
  422. (c->p[1] << 8) |
  423. c->p[2]);
  424. mp_cur_consume(c,3);
  425. break;
  426. case 0xd1: /* int 16 */
  427. mp_cur_need(c,3);
  428. lua_pushnumber(L,(int16_t)
  429. (c->p[1] << 8) |
  430. c->p[2]);
  431. mp_cur_consume(c,3);
  432. break;
  433. case 0xce: /* uint 32 */
  434. mp_cur_need(c,5);
  435. lua_pushnumber(L,
  436. ((uint32_t)c->p[1] << 24) |
  437. ((uint32_t)c->p[2] << 16) |
  438. ((uint32_t)c->p[3] << 8) |
  439. (uint32_t)c->p[4]);
  440. mp_cur_consume(c,5);
  441. break;
  442. case 0xd2: /* int 32 */
  443. mp_cur_need(c,5);
  444. lua_pushnumber(L,
  445. ((int32_t)c->p[1] << 24) |
  446. ((int32_t)c->p[2] << 16) |
  447. ((int32_t)c->p[3] << 8) |
  448. (int32_t)c->p[4]);
  449. mp_cur_consume(c,5);
  450. break;
  451. case 0xcf: /* uint 64 */
  452. mp_cur_need(c,9);
  453. lua_pushnumber(L,
  454. ((uint64_t)c->p[1] << 56) |
  455. ((uint64_t)c->p[2] << 48) |
  456. ((uint64_t)c->p[3] << 40) |
  457. ((uint64_t)c->p[4] << 32) |
  458. ((uint64_t)c->p[5] << 24) |
  459. ((uint64_t)c->p[6] << 16) |
  460. ((uint64_t)c->p[7] << 8) |
  461. (uint64_t)c->p[8]);
  462. mp_cur_consume(c,9);
  463. break;
  464. case 0xd3: /* int 64 */
  465. mp_cur_need(c,9);
  466. lua_pushnumber(L,
  467. ((int64_t)c->p[1] << 56) |
  468. ((int64_t)c->p[2] << 48) |
  469. ((int64_t)c->p[3] << 40) |
  470. ((int64_t)c->p[4] << 32) |
  471. ((int64_t)c->p[5] << 24) |
  472. ((int64_t)c->p[6] << 16) |
  473. ((int64_t)c->p[7] << 8) |
  474. (int64_t)c->p[8]);
  475. mp_cur_consume(c,9);
  476. break;
  477. case 0xc0: /* nil */
  478. lua_pushnil(L);
  479. mp_cur_consume(c,1);
  480. break;
  481. case 0xc3: /* true */
  482. lua_pushboolean(L,1);
  483. mp_cur_consume(c,1);
  484. break;
  485. case 0xc2: /* false */
  486. lua_pushboolean(L,0);
  487. mp_cur_consume(c,1);
  488. break;
  489. case 0xca: /* float */
  490. mp_cur_need(c,5);
  491. assert(sizeof(float) == 4);
  492. {
  493. float f;
  494. memcpy(&f,c->p+1,4);
  495. memrevifle(&f,4);
  496. lua_pushnumber(L,f);
  497. mp_cur_consume(c,5);
  498. }
  499. break;
  500. case 0xcb: /* double */
  501. mp_cur_need(c,9);
  502. assert(sizeof(double) == 8);
  503. {
  504. double d;
  505. memcpy(&d,c->p+1,8);
  506. memrevifle(&d,8);
  507. lua_pushnumber(L,d);
  508. mp_cur_consume(c,9);
  509. }
  510. break;
  511. case 0xda: /* raw 16 */
  512. mp_cur_need(c,3);
  513. {
  514. size_t l = (c->p[1] << 8) | c->p[2];
  515. mp_cur_need(c,3+l);
  516. lua_pushlstring(L,(char*)c->p+3,l);
  517. mp_cur_consume(c,3+l);
  518. }
  519. break;
  520. case 0xdb: /* raw 32 */
  521. mp_cur_need(c,5);
  522. {
  523. size_t l = (c->p[1] << 24) |
  524. (c->p[2] << 16) |
  525. (c->p[3] << 8) |
  526. c->p[4];
  527. mp_cur_need(c,5+l);
  528. lua_pushlstring(L,(char*)c->p+5,l);
  529. mp_cur_consume(c,5+l);
  530. }
  531. break;
  532. case 0xdc: /* array 16 */
  533. mp_cur_need(c,3);
  534. {
  535. size_t l = (c->p[1] << 8) | c->p[2];
  536. mp_cur_consume(c,3);
  537. mp_decode_to_lua_array(L,c,l);
  538. }
  539. break;
  540. case 0xdd: /* array 32 */
  541. mp_cur_need(c,5);
  542. {
  543. size_t l = (c->p[1] << 24) |
  544. (c->p[2] << 16) |
  545. (c->p[3] << 8) |
  546. c->p[4];
  547. mp_cur_consume(c,5);
  548. mp_decode_to_lua_array(L,c,l);
  549. }
  550. break;
  551. case 0xde: /* map 16 */
  552. mp_cur_need(c,3);
  553. {
  554. size_t l = (c->p[1] << 8) | c->p[2];
  555. mp_cur_consume(c,3);
  556. mp_decode_to_lua_hash(L,c,l);
  557. }
  558. break;
  559. case 0xdf: /* map 32 */
  560. mp_cur_need(c,5);
  561. {
  562. size_t l = (c->p[1] << 24) |
  563. (c->p[2] << 16) |
  564. (c->p[3] << 8) |
  565. c->p[4];
  566. mp_cur_consume(c,5);
  567. mp_decode_to_lua_hash(L,c,l);
  568. }
  569. break;
  570. default: /* types that can't be idenitified by first byte value. */
  571. if ((c->p[0] & 0x80) == 0) { /* positive fixnum */
  572. lua_pushnumber(L,c->p[0]);
  573. mp_cur_consume(c,1);
  574. } else if ((c->p[0] & 0xe0) == 0xe0) { /* negative fixnum */
  575. lua_pushnumber(L,(signed char)c->p[0]);
  576. mp_cur_consume(c,1);
  577. } else if ((c->p[0] & 0xe0) == 0xa0) { /* fix raw */
  578. size_t l = c->p[0] & 0x1f;
  579. mp_cur_need(c,1+l);
  580. lua_pushlstring(L,(char*)c->p+1,l);
  581. mp_cur_consume(c,1+l);
  582. } else if ((c->p[0] & 0xf0) == 0x90) { /* fix map */
  583. size_t l = c->p[0] & 0xf;
  584. mp_cur_consume(c,1);
  585. mp_decode_to_lua_array(L,c,l);
  586. } else if ((c->p[0] & 0xf0) == 0x80) { /* fix map */
  587. size_t l = c->p[0] & 0xf;
  588. mp_cur_consume(c,1);
  589. mp_decode_to_lua_hash(L,c,l);
  590. } else {
  591. c->err = MP_CUR_ERROR_BADFMT;
  592. }
  593. }
  594. }
  595. static int mp_unpack(lua_State *L) {
  596. size_t len;
  597. const unsigned char *s;
  598. mp_cur *c;
  599. if (!lua_isstring(L,-1)) {
  600. lua_pushstring(L,"MessagePack decoding needs a string as input.");
  601. lua_error(L);
  602. }
  603. s = (const unsigned char*) lua_tolstring(L,-1,&len);
  604. c = mp_cur_new(s,len);
  605. mp_decode_to_lua_type(L,c);
  606. if (c->err == MP_CUR_ERROR_EOF) {
  607. mp_cur_free(c);
  608. lua_pushstring(L,"Missing bytes in input.");
  609. lua_error(L);
  610. } else if (c->err == MP_CUR_ERROR_BADFMT) {
  611. mp_cur_free(c);
  612. lua_pushstring(L,"Bad data format in input.");
  613. lua_error(L);
  614. } else if (c->left != 0) {
  615. mp_cur_free(c);
  616. lua_pushstring(L,"Extra bytes in input.");
  617. lua_error(L);
  618. }
  619. mp_cur_free(c);
  620. return 1;
  621. }
  622. /* ---------------------------------------------------------------------------- */
  623. static const struct luaL_reg thislib[] = {
  624. {"pack", mp_pack},
  625. {"unpack", mp_unpack},
  626. {NULL, NULL}
  627. };
  628. LUALIB_API int luaopen_cmsgpack (lua_State *L) {
  629. luaL_register(L, "cmsgpack", thislib);
  630. lua_pushliteral(L, LUACMSGPACK_VERSION);
  631. lua_setfield(L, -2, "_VERSION");
  632. lua_pushliteral(L, LUACMSGPACK_COPYRIGHT);
  633. lua_setfield(L, -2, "_COPYRIGHT");
  634. lua_pushliteral(L, LUACMSGPACK_DESCRIPTION);
  635. lua_setfield(L, -2, "_DESCRIPTION");
  636. return 1;
  637. }
  638. /******************************************************************************
  639. * Copyright (C) 2012 Salvatore Sanfilippo. All rights reserved.
  640. *
  641. * Permission is hereby granted, free of charge, to any person obtaining
  642. * a copy of this software and associated documentation files (the
  643. * "Software"), to deal in the Software without restriction, including
  644. * without limitation the rights to use, copy, modify, merge, publish,
  645. * distribute, sublicense, and/or sell copies of the Software, and to
  646. * permit persons to whom the Software is furnished to do so, subject to
  647. * the following conditions:
  648. *
  649. * The above copyright notice and this permission notice shall be
  650. * included in all copies or substantial portions of the Software.
  651. *
  652. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  653. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  654. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  655. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  656. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  657. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  658. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  659. ******************************************************************************/