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