xref: /redis-3.2.3/deps/lua/src/lua_cmsgpack.c (revision a117dfa8)
1 #include <math.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <assert.h>
6 
7 #include "lua.h"
8 #include "lauxlib.h"
9 
10 #define LUACMSGPACK_NAME        "cmsgpack"
11 #define LUACMSGPACK_SAFE_NAME   "cmsgpack_safe"
12 #define LUACMSGPACK_VERSION     "lua-cmsgpack 0.4.0"
13 #define LUACMSGPACK_COPYRIGHT   "Copyright (C) 2012, Salvatore Sanfilippo"
14 #define LUACMSGPACK_DESCRIPTION "MessagePack C implementation for Lua"
15 
16 /* Allows a preprocessor directive to override MAX_NESTING */
17 #ifndef LUACMSGPACK_MAX_NESTING
18     #define LUACMSGPACK_MAX_NESTING  16 /* Max tables nesting. */
19 #endif
20 
21 /* Check if float or double can be an integer without loss of precision */
22 #define IS_INT_TYPE_EQUIVALENT(x, T) (!isinf(x) && (T)(x) == (x))
23 
24 #define IS_INT64_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int64_t)
25 #define IS_INT_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int)
26 
27 /* If size of pointer is equal to a 4 byte integer, we're on 32 bits. */
28 #if UINTPTR_MAX == UINT_MAX
29     #define BITS_32 1
30 #else
31     #define BITS_32 0
32 #endif
33 
34 #if BITS_32
35     #define lua_pushunsigned(L, n) lua_pushnumber(L, n)
36 #else
37     #define lua_pushunsigned(L, n) lua_pushinteger(L, n)
38 #endif
39 
40 /* =============================================================================
41  * MessagePack implementation and bindings for Lua 5.1/5.2.
42  * Copyright(C) 2012 Salvatore Sanfilippo <[email protected]>
43  *
44  * http://github.com/antirez/lua-cmsgpack
45  *
46  * For MessagePack specification check the following web site:
47  * http://wiki.msgpack.org/display/MSGPACK/Format+specification
48  *
49  * See Copyright Notice at the end of this file.
50  *
51  * CHANGELOG:
52  * 19-Feb-2012 (ver 0.1.0): Initial release.
53  * 20-Feb-2012 (ver 0.2.0): Tables encoding improved.
54  * 20-Feb-2012 (ver 0.2.1): Minor bug fixing.
55  * 20-Feb-2012 (ver 0.3.0): Module renamed lua-cmsgpack (was lua-msgpack).
56  * 04-Apr-2014 (ver 0.3.1): Lua 5.2 support and minor bug fix.
57  * 07-Apr-2014 (ver 0.4.0): Multiple pack/unpack, lua allocator, efficiency.
58  * ========================================================================== */
59 
60 /* -------------------------- Endian conversion --------------------------------
61  * We use it only for floats and doubles, all the other conversions performed
62  * in an endian independent fashion. So the only thing we need is a function
63  * that swaps a binary string if arch is little endian (and left it untouched
64  * otherwise). */
65 
66 /* Reverse memory bytes if arch is little endian. Given the conceptual
67  * simplicity of the Lua build system we prefer check for endianess at runtime.
68  * The performance difference should be acceptable. */
69 void memrevifle(void *ptr, size_t len) {
70     unsigned char   *p = (unsigned char *)ptr,
71                     *e = (unsigned char *)p+len-1,
72                     aux;
73     int test = 1;
74     unsigned char *testp = (unsigned char*) &test;
75 
76     if (testp[0] == 0) return; /* Big endian, nothing to do. */
77     len /= 2;
78     while(len--) {
79         aux = *p;
80         *p = *e;
81         *e = aux;
82         p++;
83         e--;
84     }
85 }
86 
87 /* ---------------------------- String buffer ----------------------------------
88  * This is a simple implementation of string buffers. The only operation
89  * supported is creating empty buffers and appending bytes to it.
90  * The string buffer uses 2x preallocation on every realloc for O(N) append
91  * behavior.  */
92 
93 typedef struct mp_buf {
94     unsigned char *b;
95     size_t len, free;
96 } mp_buf;
97 
98 void *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) {
99     void *(*local_realloc) (void *, void *, size_t osize, size_t nsize) = NULL;
100     void *ud;
101 
102     local_realloc = lua_getallocf(L, &ud);
103 
104     return local_realloc(ud, target, osize, nsize);
105 }
106 
107 mp_buf *mp_buf_new(lua_State *L) {
108     mp_buf *buf = NULL;
109 
110     /* Old size = 0; new size = sizeof(*buf) */
111     buf = (mp_buf*)mp_realloc(L, NULL, 0, sizeof(*buf));
112 
113     buf->b = NULL;
114     buf->len = buf->free = 0;
115     return buf;
116 }
117 
118 void mp_buf_append(lua_State *L, mp_buf *buf, const unsigned char *s, size_t len) {
119     if (buf->free < len) {
120         size_t newsize = (buf->len+len)*2;
121 
122         buf->b = (unsigned char*)mp_realloc(L, buf->b, buf->len + buf->free, newsize);
123         buf->free = newsize - buf->len;
124     }
125     memcpy(buf->b+buf->len,s,len);
126     buf->len += len;
127     buf->free -= len;
128 }
129 
130 void mp_buf_free(lua_State *L, mp_buf *buf) {
131     mp_realloc(L, buf->b, buf->len + buf->free, 0); /* realloc to 0 = free */
132     mp_realloc(L, buf, sizeof(*buf), 0);
133 }
134 
135 /* ---------------------------- String cursor ----------------------------------
136  * This simple data structure is used for parsing. Basically you create a cursor
137  * using a string pointer and a length, then it is possible to access the
138  * current string position with cursor->p, check the remaining length
139  * in cursor->left, and finally consume more string using
140  * mp_cur_consume(cursor,len), to advance 'p' and subtract 'left'.
141  * An additional field cursor->error is set to zero on initialization and can
142  * be used to report errors. */
143 
144 #define MP_CUR_ERROR_NONE   0
145 #define MP_CUR_ERROR_EOF    1   /* Not enough data to complete operation. */
146 #define MP_CUR_ERROR_BADFMT 2   /* Bad data format */
147 
148 typedef struct mp_cur {
149     const unsigned char *p;
150     size_t left;
151     int err;
152 } mp_cur;
153 
154 void mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) {
155     cursor->p = s;
156     cursor->left = len;
157     cursor->err = MP_CUR_ERROR_NONE;
158 }
159 
160 #define mp_cur_consume(_c,_len) do { _c->p += _len; _c->left -= _len; } while(0)
161 
162 /* When there is not enough room we set an error in the cursor and return. This
163  * is very common across the code so we have a macro to make the code look
164  * a bit simpler. */
165 #define mp_cur_need(_c,_len) do { \
166     if (_c->left < _len) { \
167         _c->err = MP_CUR_ERROR_EOF; \
168         return; \
169     } \
170 } while(0)
171 
172 /* ------------------------- Low level MP encoding -------------------------- */
173 
174 void mp_encode_bytes(lua_State *L, mp_buf *buf, const unsigned char *s, size_t len) {
175     unsigned char hdr[5];
176     int hdrlen;
177 
178     if (len < 32) {
179         hdr[0] = 0xa0 | (len&0xff); /* fix raw */
180         hdrlen = 1;
181     } else if (len <= 0xff) {
182         hdr[0] = 0xd9;
183         hdr[1] = len;
184         hdrlen = 2;
185     } else if (len <= 0xffff) {
186         hdr[0] = 0xda;
187         hdr[1] = (len&0xff00)>>8;
188         hdr[2] = len&0xff;
189         hdrlen = 3;
190     } else {
191         hdr[0] = 0xdb;
192         hdr[1] = (len&0xff000000)>>24;
193         hdr[2] = (len&0xff0000)>>16;
194         hdr[3] = (len&0xff00)>>8;
195         hdr[4] = len&0xff;
196         hdrlen = 5;
197     }
198     mp_buf_append(L,buf,hdr,hdrlen);
199     mp_buf_append(L,buf,s,len);
200 }
201 
202 /* we assume IEEE 754 internal format for single and double precision floats. */
203 void mp_encode_double(lua_State *L, mp_buf *buf, double d) {
204     unsigned char b[9];
205     float f = d;
206 
207     assert(sizeof(f) == 4 && sizeof(d) == 8);
208     if (d == (double)f) {
209         b[0] = 0xca;    /* float IEEE 754 */
210         memcpy(b+1,&f,4);
211         memrevifle(b+1,4);
212         mp_buf_append(L,buf,b,5);
213     } else if (sizeof(d) == 8) {
214         b[0] = 0xcb;    /* double IEEE 754 */
215         memcpy(b+1,&d,8);
216         memrevifle(b+1,8);
217         mp_buf_append(L,buf,b,9);
218     }
219 }
220 
221 void mp_encode_int(lua_State *L, mp_buf *buf, int64_t n) {
222     unsigned char b[9];
223     int enclen;
224 
225     if (n >= 0) {
226         if (n <= 127) {
227             b[0] = n & 0x7f;    /* positive fixnum */
228             enclen = 1;
229         } else if (n <= 0xff) {
230             b[0] = 0xcc;        /* uint 8 */
231             b[1] = n & 0xff;
232             enclen = 2;
233         } else if (n <= 0xffff) {
234             b[0] = 0xcd;        /* uint 16 */
235             b[1] = (n & 0xff00) >> 8;
236             b[2] = n & 0xff;
237             enclen = 3;
238         } else if (n <= 0xffffffffLL) {
239             b[0] = 0xce;        /* uint 32 */
240             b[1] = (n & 0xff000000) >> 24;
241             b[2] = (n & 0xff0000) >> 16;
242             b[3] = (n & 0xff00) >> 8;
243             b[4] = n & 0xff;
244             enclen = 5;
245         } else {
246             b[0] = 0xcf;        /* uint 64 */
247             b[1] = (n & 0xff00000000000000LL) >> 56;
248             b[2] = (n & 0xff000000000000LL) >> 48;
249             b[3] = (n & 0xff0000000000LL) >> 40;
250             b[4] = (n & 0xff00000000LL) >> 32;
251             b[5] = (n & 0xff000000) >> 24;
252             b[6] = (n & 0xff0000) >> 16;
253             b[7] = (n & 0xff00) >> 8;
254             b[8] = n & 0xff;
255             enclen = 9;
256         }
257     } else {
258         if (n >= -32) {
259             b[0] = ((signed char)n);   /* negative fixnum */
260             enclen = 1;
261         } else if (n >= -128) {
262             b[0] = 0xd0;        /* int 8 */
263             b[1] = n & 0xff;
264             enclen = 2;
265         } else if (n >= -32768) {
266             b[0] = 0xd1;        /* int 16 */
267             b[1] = (n & 0xff00) >> 8;
268             b[2] = n & 0xff;
269             enclen = 3;
270         } else if (n >= -2147483648LL) {
271             b[0] = 0xd2;        /* int 32 */
272             b[1] = (n & 0xff000000) >> 24;
273             b[2] = (n & 0xff0000) >> 16;
274             b[3] = (n & 0xff00) >> 8;
275             b[4] = n & 0xff;
276             enclen = 5;
277         } else {
278             b[0] = 0xd3;        /* int 64 */
279             b[1] = (n & 0xff00000000000000LL) >> 56;
280             b[2] = (n & 0xff000000000000LL) >> 48;
281             b[3] = (n & 0xff0000000000LL) >> 40;
282             b[4] = (n & 0xff00000000LL) >> 32;
283             b[5] = (n & 0xff000000) >> 24;
284             b[6] = (n & 0xff0000) >> 16;
285             b[7] = (n & 0xff00) >> 8;
286             b[8] = n & 0xff;
287             enclen = 9;
288         }
289     }
290     mp_buf_append(L,buf,b,enclen);
291 }
292 
293 void mp_encode_array(lua_State *L, mp_buf *buf, int64_t n) {
294     unsigned char b[5];
295     int enclen;
296 
297     if (n <= 15) {
298         b[0] = 0x90 | (n & 0xf);    /* fix array */
299         enclen = 1;
300     } else if (n <= 65535) {
301         b[0] = 0xdc;                /* array 16 */
302         b[1] = (n & 0xff00) >> 8;
303         b[2] = n & 0xff;
304         enclen = 3;
305     } else {
306         b[0] = 0xdd;                /* array 32 */
307         b[1] = (n & 0xff000000) >> 24;
308         b[2] = (n & 0xff0000) >> 16;
309         b[3] = (n & 0xff00) >> 8;
310         b[4] = n & 0xff;
311         enclen = 5;
312     }
313     mp_buf_append(L,buf,b,enclen);
314 }
315 
316 void mp_encode_map(lua_State *L, mp_buf *buf, int64_t n) {
317     unsigned char b[5];
318     int enclen;
319 
320     if (n <= 15) {
321         b[0] = 0x80 | (n & 0xf);    /* fix map */
322         enclen = 1;
323     } else if (n <= 65535) {
324         b[0] = 0xde;                /* map 16 */
325         b[1] = (n & 0xff00) >> 8;
326         b[2] = n & 0xff;
327         enclen = 3;
328     } else {
329         b[0] = 0xdf;                /* map 32 */
330         b[1] = (n & 0xff000000) >> 24;
331         b[2] = (n & 0xff0000) >> 16;
332         b[3] = (n & 0xff00) >> 8;
333         b[4] = n & 0xff;
334         enclen = 5;
335     }
336     mp_buf_append(L,buf,b,enclen);
337 }
338 
339 /* --------------------------- Lua types encoding --------------------------- */
340 
341 void mp_encode_lua_string(lua_State *L, mp_buf *buf) {
342     size_t len;
343     const char *s;
344 
345     s = lua_tolstring(L,-1,&len);
346     mp_encode_bytes(L,buf,(const unsigned char*)s,len);
347 }
348 
349 void mp_encode_lua_bool(lua_State *L, mp_buf *buf) {
350     unsigned char b = lua_toboolean(L,-1) ? 0xc3 : 0xc2;
351     mp_buf_append(L,buf,&b,1);
352 }
353 
354 /* Lua 5.3 has a built in 64-bit integer type */
355 void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
356 #if (LUA_VERSION_NUM < 503) && BITS_32
357     lua_Number i = lua_tonumber(L,-1);
358 #else
359     lua_Integer i = lua_tointeger(L,-1);
360 #endif
361     mp_encode_int(L, buf, (int64_t)i);
362 }
363 
364 /* Lua 5.2 and lower only has 64-bit doubles, so we need to
365  * detect if the double may be representable as an int
366  * for Lua < 5.3 */
367 void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
368     lua_Number n = lua_tonumber(L,-1);
369 
370     if (IS_INT64_EQUIVALENT(n)) {
371         mp_encode_lua_integer(L, buf);
372     } else {
373         mp_encode_double(L,buf,(double)n);
374     }
375 }
376 
377 void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level);
378 
379 /* Convert a lua table into a message pack list. */
380 void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
381 #if LUA_VERSION_NUM < 502
382     size_t len = lua_objlen(L,-1), j;
383 #else
384     size_t len = lua_rawlen(L,-1), j;
385 #endif
386 
387     mp_encode_array(L,buf,len);
388     for (j = 1; j <= len; j++) {
389         lua_pushnumber(L,j);
390         lua_gettable(L,-2);
391         mp_encode_lua_type(L,buf,level+1);
392     }
393 }
394 
395 /* Convert a lua table into a message pack key-value map. */
396 void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
397     size_t len = 0;
398 
399     /* First step: count keys into table. No other way to do it with the
400      * Lua API, we need to iterate a first time. Note that an alternative
401      * would be to do a single run, and then hack the buffer to insert the
402      * map opcodes for message pack. Too hackish for this lib. */
403     lua_pushnil(L);
404     while(lua_next(L,-2)) {
405         lua_pop(L,1); /* remove value, keep key for next iteration. */
406         len++;
407     }
408 
409     /* Step two: actually encoding of the map. */
410     mp_encode_map(L,buf,len);
411     lua_pushnil(L);
412     while(lua_next(L,-2)) {
413         /* Stack: ... key value */
414         lua_pushvalue(L,-2); /* Stack: ... key value key */
415         mp_encode_lua_type(L,buf,level+1); /* encode key */
416         mp_encode_lua_type(L,buf,level+1); /* encode val */
417     }
418 }
419 
420 /* Returns true if the Lua table on top of the stack is exclusively composed
421  * of keys from numerical keys from 1 up to N, with N being the total number
422  * of elements, without any hole in the middle. */
423 int table_is_an_array(lua_State *L) {
424     int count = 0, max = 0;
425 #if LUA_VERSION_NUM < 503
426     lua_Number n;
427 #else
428     lua_Integer n;
429 #endif
430 
431     /* Stack top on function entry */
432     int stacktop;
433 
434     stacktop = lua_gettop(L);
435 
436     lua_pushnil(L);
437     while(lua_next(L,-2)) {
438         /* Stack: ... key value */
439         lua_pop(L,1); /* Stack: ... key */
440         /* The <= 0 check is valid here because we're comparing indexes. */
441 #if LUA_VERSION_NUM < 503
442         if ((LUA_TNUMBER != lua_type(L,-1)) || (n = lua_tonumber(L, -1)) <= 0 ||
443             !IS_INT_EQUIVALENT(n))
444 #else
445         if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0)
446 #endif
447         {
448             lua_settop(L, stacktop);
449             return 0;
450         }
451         max = (n > max ? n : max);
452         count++;
453     }
454     /* We have the total number of elements in "count". Also we have
455      * the max index encountered in "max". We can't reach this code
456      * if there are indexes <= 0. If you also note that there can not be
457      * repeated keys into a table, you have that if max==count you are sure
458      * that there are all the keys form 1 to count (both included). */
459     lua_settop(L, stacktop);
460     return max == count;
461 }
462 
463 /* If the length operator returns non-zero, that is, there is at least
464  * an object at key '1', we serialize to message pack list. Otherwise
465  * we use a map. */
466 void mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {
467     if (table_is_an_array(L))
468         mp_encode_lua_table_as_array(L,buf,level);
469     else
470         mp_encode_lua_table_as_map(L,buf,level);
471 }
472 
473 void mp_encode_lua_null(lua_State *L, mp_buf *buf) {
474     unsigned char b[1];
475 
476     b[0] = 0xc0;
477     mp_buf_append(L,buf,b,1);
478 }
479 
480 void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {
481     int t = lua_type(L,-1);
482 
483     /* Limit the encoding of nested tables to a specified maximum depth, so that
484      * we survive when called against circular references in tables. */
485     if (t == LUA_TTABLE && level == LUACMSGPACK_MAX_NESTING) t = LUA_TNIL;
486     switch(t) {
487     case LUA_TSTRING: mp_encode_lua_string(L,buf); break;
488     case LUA_TBOOLEAN: mp_encode_lua_bool(L,buf); break;
489     case LUA_TNUMBER:
490     #if LUA_VERSION_NUM < 503
491         mp_encode_lua_number(L,buf); break;
492     #else
493         if (lua_isinteger(L, -1)) {
494             mp_encode_lua_integer(L, buf);
495         } else {
496             mp_encode_lua_number(L, buf);
497         }
498         break;
499     #endif
500     case LUA_TTABLE: mp_encode_lua_table(L,buf,level); break;
501     default: mp_encode_lua_null(L,buf); break;
502     }
503     lua_pop(L,1);
504 }
505 
506 /*
507  * Packs all arguments as a stream for multiple upacking later.
508  * Returns error if no arguments provided.
509  */
510 int mp_pack(lua_State *L) {
511     int nargs = lua_gettop(L);
512     int i;
513     mp_buf *buf;
514 
515     if (nargs == 0)
516         return luaL_argerror(L, 0, "MessagePack pack needs input.");
517 
518     buf = mp_buf_new(L);
519     for(i = 1; i <= nargs; i++) {
520         /* Copy argument i to top of stack for _encode processing;
521          * the encode function pops it from the stack when complete. */
522         lua_pushvalue(L, i);
523 
524         mp_encode_lua_type(L,buf,0);
525 
526         lua_pushlstring(L,(char*)buf->b,buf->len);
527 
528         /* Reuse the buffer for the next operation by
529          * setting its free count to the total buffer size
530          * and the current position to zero. */
531         buf->free += buf->len;
532         buf->len = 0;
533     }
534     mp_buf_free(L, buf);
535 
536     /* Concatenate all nargs buffers together */
537     lua_concat(L, nargs);
538     return 1;
539 }
540 
541 /* ------------------------------- Decoding --------------------------------- */
542 
543 void mp_decode_to_lua_type(lua_State *L, mp_cur *c);
544 
545 void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {
546     assert(len <= UINT_MAX);
547     int index = 1;
548 
549     lua_newtable(L);
550     while(len--) {
551         lua_pushnumber(L,index++);
552         mp_decode_to_lua_type(L,c);
553         if (c->err) return;
554         lua_settable(L,-3);
555     }
556 }
557 
558 void mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {
559     assert(len <= UINT_MAX);
560     lua_newtable(L);
561     while(len--) {
562         mp_decode_to_lua_type(L,c); /* key */
563         if (c->err) return;
564         mp_decode_to_lua_type(L,c); /* value */
565         if (c->err) return;
566         lua_settable(L,-3);
567     }
568 }
569 
570 /* Decode a Message Pack raw object pointed by the string cursor 'c' to
571  * a Lua type, that is left as the only result on the stack. */
572 void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
573     mp_cur_need(c,1);
574 
575     /* If we return more than 18 elements, we must resize the stack to
576      * fit all our return values.  But, there is no way to
577      * determine how many objects a msgpack will unpack to up front, so
578      * we request a +1 larger stack on each iteration (noop if stack is
579      * big enough, and when stack does require resize it doubles in size) */
580     luaL_checkstack(L, 1,
581         "too many return values at once; "
582         "use unpack_one or unpack_limit instead.");
583 
584     switch(c->p[0]) {
585     case 0xcc:  /* uint 8 */
586         mp_cur_need(c,2);
587         lua_pushunsigned(L,c->p[1]);
588         mp_cur_consume(c,2);
589         break;
590     case 0xd0:  /* int 8 */
591         mp_cur_need(c,2);
592         lua_pushinteger(L,(signed char)c->p[1]);
593         mp_cur_consume(c,2);
594         break;
595     case 0xcd:  /* uint 16 */
596         mp_cur_need(c,3);
597         lua_pushunsigned(L,
598             (c->p[1] << 8) |
599              c->p[2]);
600         mp_cur_consume(c,3);
601         break;
602     case 0xd1:  /* int 16 */
603         mp_cur_need(c,3);
604         lua_pushinteger(L,(int16_t)
605             (c->p[1] << 8) |
606              c->p[2]);
607         mp_cur_consume(c,3);
608         break;
609     case 0xce:  /* uint 32 */
610         mp_cur_need(c,5);
611         lua_pushunsigned(L,
612             ((uint32_t)c->p[1] << 24) |
613             ((uint32_t)c->p[2] << 16) |
614             ((uint32_t)c->p[3] << 8) |
615              (uint32_t)c->p[4]);
616         mp_cur_consume(c,5);
617         break;
618     case 0xd2:  /* int 32 */
619         mp_cur_need(c,5);
620         lua_pushinteger(L,
621             ((int32_t)c->p[1] << 24) |
622             ((int32_t)c->p[2] << 16) |
623             ((int32_t)c->p[3] << 8) |
624              (int32_t)c->p[4]);
625         mp_cur_consume(c,5);
626         break;
627     case 0xcf:  /* uint 64 */
628         mp_cur_need(c,9);
629         lua_pushunsigned(L,
630             ((uint64_t)c->p[1] << 56) |
631             ((uint64_t)c->p[2] << 48) |
632             ((uint64_t)c->p[3] << 40) |
633             ((uint64_t)c->p[4] << 32) |
634             ((uint64_t)c->p[5] << 24) |
635             ((uint64_t)c->p[6] << 16) |
636             ((uint64_t)c->p[7] << 8) |
637              (uint64_t)c->p[8]);
638         mp_cur_consume(c,9);
639         break;
640     case 0xd3:  /* int 64 */
641         mp_cur_need(c,9);
642 #if LUA_VERSION_NUM < 503
643         lua_pushnumber(L,
644 #else
645         lua_pushinteger(L,
646 #endif
647             ((int64_t)c->p[1] << 56) |
648             ((int64_t)c->p[2] << 48) |
649             ((int64_t)c->p[3] << 40) |
650             ((int64_t)c->p[4] << 32) |
651             ((int64_t)c->p[5] << 24) |
652             ((int64_t)c->p[6] << 16) |
653             ((int64_t)c->p[7] << 8) |
654              (int64_t)c->p[8]);
655         mp_cur_consume(c,9);
656         break;
657     case 0xc0:  /* nil */
658         lua_pushnil(L);
659         mp_cur_consume(c,1);
660         break;
661     case 0xc3:  /* true */
662         lua_pushboolean(L,1);
663         mp_cur_consume(c,1);
664         break;
665     case 0xc2:  /* false */
666         lua_pushboolean(L,0);
667         mp_cur_consume(c,1);
668         break;
669     case 0xca:  /* float */
670         mp_cur_need(c,5);
671         assert(sizeof(float) == 4);
672         {
673             float f;
674             memcpy(&f,c->p+1,4);
675             memrevifle(&f,4);
676             lua_pushnumber(L,f);
677             mp_cur_consume(c,5);
678         }
679         break;
680     case 0xcb:  /* double */
681         mp_cur_need(c,9);
682         assert(sizeof(double) == 8);
683         {
684             double d;
685             memcpy(&d,c->p+1,8);
686             memrevifle(&d,8);
687             lua_pushnumber(L,d);
688             mp_cur_consume(c,9);
689         }
690         break;
691     case 0xd9:  /* raw 8 */
692         mp_cur_need(c,2);
693         {
694             size_t l = c->p[1];
695             mp_cur_need(c,2+l);
696             lua_pushlstring(L,(char*)c->p+2,l);
697             mp_cur_consume(c,2+l);
698         }
699         break;
700     case 0xda:  /* raw 16 */
701         mp_cur_need(c,3);
702         {
703             size_t l = (c->p[1] << 8) | c->p[2];
704             mp_cur_need(c,3+l);
705             lua_pushlstring(L,(char*)c->p+3,l);
706             mp_cur_consume(c,3+l);
707         }
708         break;
709     case 0xdb:  /* raw 32 */
710         mp_cur_need(c,5);
711         {
712             size_t l = ((size_t)c->p[1] << 24) |
713                        ((size_t)c->p[2] << 16) |
714                        ((size_t)c->p[3] << 8) |
715                        (size_t)c->p[4];
716             mp_cur_consume(c,5);
717             mp_cur_need(c,l);
718             lua_pushlstring(L,(char*)c->p,l);
719             mp_cur_consume(c,l);
720         }
721         break;
722     case 0xdc:  /* array 16 */
723         mp_cur_need(c,3);
724         {
725             size_t l = (c->p[1] << 8) | c->p[2];
726             mp_cur_consume(c,3);
727             mp_decode_to_lua_array(L,c,l);
728         }
729         break;
730     case 0xdd:  /* array 32 */
731         mp_cur_need(c,5);
732         {
733             size_t l = ((size_t)c->p[1] << 24) |
734                        ((size_t)c->p[2] << 16) |
735                        ((size_t)c->p[3] << 8) |
736                        (size_t)c->p[4];
737             mp_cur_consume(c,5);
738             mp_decode_to_lua_array(L,c,l);
739         }
740         break;
741     case 0xde:  /* map 16 */
742         mp_cur_need(c,3);
743         {
744             size_t l = (c->p[1] << 8) | c->p[2];
745             mp_cur_consume(c,3);
746             mp_decode_to_lua_hash(L,c,l);
747         }
748         break;
749     case 0xdf:  /* map 32 */
750         mp_cur_need(c,5);
751         {
752             size_t l = ((size_t)c->p[1] << 24) |
753                        ((size_t)c->p[2] << 16) |
754                        ((size_t)c->p[3] << 8) |
755                        (size_t)c->p[4];
756             mp_cur_consume(c,5);
757             mp_decode_to_lua_hash(L,c,l);
758         }
759         break;
760     default:    /* types that can't be idenitified by first byte value. */
761         if ((c->p[0] & 0x80) == 0) {   /* positive fixnum */
762             lua_pushunsigned(L,c->p[0]);
763             mp_cur_consume(c,1);
764         } else if ((c->p[0] & 0xe0) == 0xe0) {  /* negative fixnum */
765             lua_pushinteger(L,(signed char)c->p[0]);
766             mp_cur_consume(c,1);
767         } else if ((c->p[0] & 0xe0) == 0xa0) {  /* fix raw */
768             size_t l = c->p[0] & 0x1f;
769             mp_cur_need(c,1+l);
770             lua_pushlstring(L,(char*)c->p+1,l);
771             mp_cur_consume(c,1+l);
772         } else if ((c->p[0] & 0xf0) == 0x90) {  /* fix map */
773             size_t l = c->p[0] & 0xf;
774             mp_cur_consume(c,1);
775             mp_decode_to_lua_array(L,c,l);
776         } else if ((c->p[0] & 0xf0) == 0x80) {  /* fix map */
777             size_t l = c->p[0] & 0xf;
778             mp_cur_consume(c,1);
779             mp_decode_to_lua_hash(L,c,l);
780         } else {
781             c->err = MP_CUR_ERROR_BADFMT;
782         }
783     }
784 }
785 
786 int mp_unpack_full(lua_State *L, int limit, int offset) {
787     size_t len;
788     const char *s;
789     mp_cur c;
790     int cnt; /* Number of objects unpacked */
791     int decode_all = (!limit && !offset);
792 
793     s = luaL_checklstring(L,1,&len); /* if no match, exits */
794 
795     if (offset < 0 || limit < 0) /* requesting negative off or lim is invalid */
796         return luaL_error(L,
797             "Invalid request to unpack with offset of %d and limit of %d.",
798             offset, len);
799     else if (offset > len)
800         return luaL_error(L,
801             "Start offset %d greater than input length %d.", offset, len);
802 
803     if (decode_all) limit = INT_MAX;
804 
805     mp_cur_init(&c,(const unsigned char *)s+offset,len-offset);
806 
807     /* We loop over the decode because this could be a stream
808      * of multiple top-level values serialized together */
809     for(cnt = 0; c.left > 0 && cnt < limit; cnt++) {
810         mp_decode_to_lua_type(L,&c);
811 
812         if (c.err == MP_CUR_ERROR_EOF) {
813             return luaL_error(L,"Missing bytes in input.");
814         } else if (c.err == MP_CUR_ERROR_BADFMT) {
815             return luaL_error(L,"Bad data format in input.");
816         }
817     }
818 
819     if (!decode_all) {
820         /* c->left is the remaining size of the input buffer.
821          * subtract the entire buffer size from the unprocessed size
822          * to get our next start offset */
823         int offset = len - c.left;
824         /* Return offset -1 when we have have processed the entire buffer. */
825         lua_pushinteger(L, c.left == 0 ? -1 : offset);
826         /* Results are returned with the arg elements still
827          * in place. Lua takes care of only returning
828          * elements above the args for us.
829          * In this case, we have one arg on the stack
830          * for this function, so we insert our first return
831          * value at position 2. */
832         lua_insert(L, 2);
833         cnt += 1; /* increase return count by one to make room for offset */
834     }
835 
836     return cnt;
837 }
838 
839 int mp_unpack(lua_State *L) {
840     return mp_unpack_full(L, 0, 0);
841 }
842 
843 int mp_unpack_one(lua_State *L) {
844     int offset = luaL_optinteger(L, 2, 0);
845     /* Variable pop because offset may not exist */
846     lua_pop(L, lua_gettop(L)-1);
847     return mp_unpack_full(L, 1, offset);
848 }
849 
850 int mp_unpack_limit(lua_State *L) {
851     int limit = luaL_checkinteger(L, 2);
852     int offset = luaL_optinteger(L, 3, 0);
853     /* Variable pop because offset may not exist */
854     lua_pop(L, lua_gettop(L)-1);
855 
856     return mp_unpack_full(L, limit, offset);
857 }
858 
859 int mp_safe(lua_State *L) {
860     int argc, err, total_results;
861 
862     argc = lua_gettop(L);
863 
864     /* This adds our function to the bottom of the stack
865      * (the "call this function" position) */
866     lua_pushvalue(L, lua_upvalueindex(1));
867     lua_insert(L, 1);
868 
869     err = lua_pcall(L, argc, LUA_MULTRET, 0);
870     total_results = lua_gettop(L);
871 
872     if (!err) {
873         return total_results;
874     } else {
875         lua_pushnil(L);
876         lua_insert(L,-2);
877         return 2;
878     }
879 }
880 
881 /* -------------------------------------------------------------------------- */
882 const struct luaL_Reg cmds[] = {
883     {"pack", mp_pack},
884     {"unpack", mp_unpack},
885     {"unpack_one", mp_unpack_one},
886     {"unpack_limit", mp_unpack_limit},
887     {0}
888 };
889 
890 int luaopen_create(lua_State *L) {
891     int i;
892     /* Manually construct our module table instead of
893      * relying on _register or _newlib */
894     lua_newtable(L);
895 
896     for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {
897         lua_pushcfunction(L, cmds[i].func);
898         lua_setfield(L, -2, cmds[i].name);
899     }
900 
901     /* Add metadata */
902     lua_pushliteral(L, LUACMSGPACK_NAME);
903     lua_setfield(L, -2, "_NAME");
904     lua_pushliteral(L, LUACMSGPACK_VERSION);
905     lua_setfield(L, -2, "_VERSION");
906     lua_pushliteral(L, LUACMSGPACK_COPYRIGHT);
907     lua_setfield(L, -2, "_COPYRIGHT");
908     lua_pushliteral(L, LUACMSGPACK_DESCRIPTION);
909     lua_setfield(L, -2, "_DESCRIPTION");
910     return 1;
911 }
912 
913 LUALIB_API int luaopen_cmsgpack(lua_State *L) {
914     luaopen_create(L);
915 
916 #if LUA_VERSION_NUM < 502
917     /* Register name globally for 5.1 */
918     lua_pushvalue(L, -1);
919     lua_setglobal(L, LUACMSGPACK_NAME);
920 #endif
921 
922     return 1;
923 }
924 
925 LUALIB_API int luaopen_cmsgpack_safe(lua_State *L) {
926     int i;
927 
928     luaopen_cmsgpack(L);
929 
930     /* Wrap all functions in the safe handler */
931     for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {
932         lua_getfield(L, -1, cmds[i].name);
933         lua_pushcclosure(L, mp_safe, 1);
934         lua_setfield(L, -2, cmds[i].name);
935     }
936 
937 #if LUA_VERSION_NUM < 502
938     /* Register name globally for 5.1 */
939     lua_pushvalue(L, -1);
940     lua_setglobal(L, LUACMSGPACK_SAFE_NAME);
941 #endif
942 
943     return 1;
944 }
945 
946 /******************************************************************************
947 * Copyright (C) 2012 Salvatore Sanfilippo.  All rights reserved.
948 *
949 * Permission is hereby granted, free of charge, to any person obtaining
950 * a copy of this software and associated documentation files (the
951 * "Software"), to deal in the Software without restriction, including
952 * without limitation the rights to use, copy, modify, merge, publish,
953 * distribute, sublicense, and/or sell copies of the Software, and to
954 * permit persons to whom the Software is furnished to do so, subject to
955 * the following conditions:
956 *
957 * The above copyright notice and this permission notice shall be
958 * included in all copies or substantial portions of the Software.
959 *
960 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
961 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
962 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
963 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
964 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
965 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
966 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
967 ******************************************************************************/
968