]>
git.saurik.com Git - redis.git/blob - deps/lua/src/lua_cmsgpack.c
10 #define LUACMSGPACK_VERSION "lua-cmsgpack 0.3.0"
11 #define LUACMSGPACK_COPYRIGHT "Copyright (C) 2012, Salvatore Sanfilippo"
12 #define LUACMSGPACK_DESCRIPTION "MessagePack C implementation for Lua"
14 #define LUACMSGPACK_MAX_NESTING 16 /* Max tables nesting. */
16 /* ==============================================================================
17 * MessagePack implementation and bindings for Lua 5.1.
18 * Copyright(C) 2012 Salvatore Sanfilippo <antirez@gmail.com>
20 * http://github.com/antirez/lua-cmsgpack
22 * For MessagePack specification check the following web site:
23 * http://wiki.msgpack.org/display/MSGPACK/Format+specification
25 * See Copyright Notice at the end of this file.
28 * 19-Feb-2012 (ver 0.1.0): Initial release.
29 * 20-Feb-2012 (ver 0.2.0): Tables encoding improved.
30 * 20-Feb-2012 (ver 0.2.1): Minor bug fixing.
31 * 20-Feb-2012 (ver 0.3.0): Module renamed lua-cmsgpack (was lua-msgpack).
32 * ============================================================================ */
34 /* --------------------------- Endian conversion --------------------------------
35 * We use it only for floats and doubles, all the other conversions are performed
36 * in an endian independent fashion. So the only thing we need is a function
37 * that swaps a binary string if the arch is little endian (and left it untouched
40 /* Reverse memory bytes if arch is little endian. Given the conceptual
41 * simplicity of the Lua build system we prefer to check for endianess at runtime.
42 * The performance difference should be acceptable. */
43 static void memrevifle(void *ptr
, size_t len
) {
44 unsigned char *p
= ptr
, *e
= p
+len
-1, aux
;
46 unsigned char *testp
= (unsigned char*) &test
;
48 if (testp
[0] == 0) return; /* Big endian, nothign to do. */
59 /* ----------------------------- String buffer ----------------------------------
60 * This is a simple implementation of string buffers. The only opereation
61 * supported is creating empty buffers and appending bytes to it.
62 * The string buffer uses 2x preallocation on every realloc for O(N) append
65 typedef struct mp_buf
{
70 static mp_buf
*mp_buf_new(void) {
71 mp_buf
*buf
= malloc(sizeof(*buf
));
74 buf
->len
= buf
->free
= 0;
78 void mp_buf_append(mp_buf
*buf
, const unsigned char *s
, size_t len
) {
79 if (buf
->free
< len
) {
80 size_t newlen
= buf
->len
+len
;
82 buf
->b
= realloc(buf
->b
,newlen
*2);
85 memcpy(buf
->b
+buf
->len
,s
,len
);
90 void mp_buf_free(mp_buf
*buf
) {
95 /* ------------------------------ String cursor ----------------------------------
96 * This simple data structure is used for parsing. Basically you create a cursor
97 * using a string pointer and a length, then it is possible to access the
98 * current string position with cursor->p, check the remaining length
99 * in cursor->left, and finally consume more string using
100 * mp_cur_consume(cursor,len), to advance 'p' and subtract 'left'.
101 * An additional field cursor->error is set to zero on initialization and can
102 * be used to report errors. */
104 #define MP_CUR_ERROR_NONE 0
105 #define MP_CUR_ERROR_EOF 1 /* Not enough data to complete the opereation. */
106 #define MP_CUR_ERROR_BADFMT 2 /* Bad data format */
108 typedef struct mp_cur
{
109 const unsigned char *p
;
114 static mp_cur
*mp_cur_new(const unsigned char *s
, size_t len
) {
115 mp_cur
*cursor
= malloc(sizeof(*cursor
));
119 cursor
->err
= MP_CUR_ERROR_NONE
;
123 static void mp_cur_free(mp_cur
*cursor
) {
127 #define mp_cur_consume(_c,_len) do { _c->p += _len; _c->left -= _len; } while(0)
129 /* When there is not enough room we set an error in the cursor and return, this
130 * is very common across the code so we have a macro to make the code look
132 #define mp_cur_need(_c,_len) do { \
133 if (_c->left < _len) { \
134 _c->err = MP_CUR_ERROR_EOF; \
139 /* --------------------------- Low level MP encoding -------------------------- */
141 static void mp_encode_bytes(mp_buf
*buf
, const unsigned char *s
, size_t len
) {
142 unsigned char hdr
[5];
146 hdr
[0] = 0xa0 | (len
&0xff); /* fix raw */
148 } else if (len
<= 0xffff) {
150 hdr
[1] = (len
&0xff00)>>8;
155 hdr
[1] = (len
&0xff000000)>>24;
156 hdr
[2] = (len
&0xff0000)>>16;
157 hdr
[3] = (len
&0xff00)>>8;
161 mp_buf_append(buf
,hdr
,hdrlen
);
162 mp_buf_append(buf
,s
,len
);
165 /* we assume IEEE 754 internal format for single and double precision floats. */
166 static void mp_encode_double(mp_buf
*buf
, double d
) {
170 assert(sizeof(f
) == 4 && sizeof(d
) == 8);
171 if (d
== (double)f
) {
172 b
[0] = 0xca; /* float IEEE 754 */
175 mp_buf_append(buf
,b
,5);
176 } else if (sizeof(d
) == 8) {
177 b
[0] = 0xcb; /* double IEEE 754 */
180 mp_buf_append(buf
,b
,9);
184 static void mp_encode_int(mp_buf
*buf
, int64_t n
) {
190 b
[0] = n
& 0x7f; /* positive fixnum */
192 } else if (n
<= 0xff) {
193 b
[0] = 0xcc; /* uint 8 */
196 } else if (n
<= 0xffff) {
197 b
[0] = 0xcd; /* uint 16 */
198 b
[1] = (n
& 0xff00) >> 8;
201 } else if (n
<= 0xffffffffLL
) {
202 b
[0] = 0xce; /* uint 32 */
203 b
[1] = (n
& 0xff000000) >> 24;
204 b
[2] = (n
& 0xff0000) >> 16;
205 b
[3] = (n
& 0xff00) >> 8;
209 b
[0] = 0xcf; /* uint 64 */
210 b
[1] = (n
& 0xff00000000000000LL
) >> 56;
211 b
[2] = (n
& 0xff000000000000LL
) >> 48;
212 b
[3] = (n
& 0xff0000000000LL
) >> 40;
213 b
[4] = (n
& 0xff00000000LL
) >> 32;
214 b
[5] = (n
& 0xff000000) >> 24;
215 b
[6] = (n
& 0xff0000) >> 16;
216 b
[7] = (n
& 0xff00) >> 8;
222 b
[0] = ((char)n
); /* negative fixnum */
224 } else if (n
>= -128) {
225 b
[0] = 0xd0; /* int 8 */
228 } else if (n
>= -32768) {
229 b
[0] = 0xd1; /* int 16 */
230 b
[1] = (n
& 0xff00) >> 8;
233 } else if (n
>= -2147483648LL) {
234 b
[0] = 0xd2; /* int 32 */
235 b
[1] = (n
& 0xff000000) >> 24;
236 b
[2] = (n
& 0xff0000) >> 16;
237 b
[3] = (n
& 0xff00) >> 8;
241 b
[0] = 0xd3; /* int 64 */
242 b
[1] = (n
& 0xff00000000000000LL
) >> 56;
243 b
[2] = (n
& 0xff000000000000LL
) >> 48;
244 b
[3] = (n
& 0xff0000000000LL
) >> 40;
245 b
[4] = (n
& 0xff00000000LL
) >> 32;
246 b
[5] = (n
& 0xff000000) >> 24;
247 b
[6] = (n
& 0xff0000) >> 16;
248 b
[7] = (n
& 0xff00) >> 8;
253 mp_buf_append(buf
,b
,enclen
);
256 static void mp_encode_array(mp_buf
*buf
, int64_t n
) {
261 b
[0] = 0x90 | (n
& 0xf); /* fix array */
263 } else if (n
<= 65535) {
264 b
[0] = 0xdc; /* array 16 */
265 b
[1] = (n
& 0xff00) >> 8;
269 b
[0] = 0xdd; /* array 32 */
270 b
[1] = (n
& 0xff000000) >> 24;
271 b
[2] = (n
& 0xff0000) >> 16;
272 b
[3] = (n
& 0xff00) >> 8;
276 mp_buf_append(buf
,b
,enclen
);
279 static void mp_encode_map(mp_buf
*buf
, int64_t n
) {
284 b
[0] = 0x80 | (n
& 0xf); /* fix map */
286 } else if (n
<= 65535) {
287 b
[0] = 0xde; /* map 16 */
288 b
[1] = (n
& 0xff00) >> 8;
292 b
[0] = 0xdf; /* map 32 */
293 b
[1] = (n
& 0xff000000) >> 24;
294 b
[2] = (n
& 0xff0000) >> 16;
295 b
[3] = (n
& 0xff00) >> 8;
299 mp_buf_append(buf
,b
,enclen
);
302 /* ----------------------------- Lua types encoding --------------------------- */
304 static void mp_encode_lua_string(lua_State
*L
, mp_buf
*buf
) {
308 s
= lua_tolstring(L
,-1,&len
);
309 mp_encode_bytes(buf
,(const unsigned char*)s
,len
);
312 static void mp_encode_lua_bool(lua_State
*L
, mp_buf
*buf
) {
313 unsigned char b
= lua_toboolean(L
,-1) ? 0xc3 : 0xc2;
314 mp_buf_append(buf
,&b
,1);
317 static void mp_encode_lua_number(lua_State
*L
, mp_buf
*buf
) {
318 lua_Number n
= lua_tonumber(L
,-1);
321 mp_encode_double(buf
,(double)n
);
323 mp_encode_int(buf
,(int64_t)n
);
327 static void mp_encode_lua_type(lua_State
*L
, mp_buf
*buf
, int level
);
329 /* Convert a lua table into a message pack list. */
330 static void mp_encode_lua_table_as_array(lua_State
*L
, mp_buf
*buf
, int level
) {
331 size_t len
= lua_objlen(L
,-1), j
;
333 mp_encode_array(buf
,len
);
334 for (j
= 1; j
<= len
; j
++) {
337 mp_encode_lua_type(L
,buf
,level
+1);
341 /* Convert a lua table into a message pack key-value map. */
342 static void mp_encode_lua_table_as_map(lua_State
*L
, mp_buf
*buf
, int level
) {
345 /* First step: count keys into table. No other way to do it with the
346 * Lua API, we need to iterate a first time. Note that an alternative
347 * would be to do a single run, and then hack the buffer to insert the
348 * map opcodes for message pack. Too hachish for this lib. */
350 while(lua_next(L
,-2)) {
351 lua_pop(L
,1); /* remove value, keep key for next iteration. */
355 /* Step two: actually encoding of the map. */
356 mp_encode_map(buf
,len
);
358 while(lua_next(L
,-2)) {
359 /* Stack: ... key value */
360 lua_pushvalue(L
,-2); /* Stack: ... key value key */
361 mp_encode_lua_type(L
,buf
,level
+1); /* encode key */
362 mp_encode_lua_type(L
,buf
,level
+1); /* encode val */
366 /* Returns true if the Lua table on top of the stack is exclusively composed
367 * of keys from numerical keys from 1 up to N, with N being the total number
368 * of elements, without any hole in the middle. */
369 static int table_is_an_array(lua_State
*L
) {
370 long count
= 0, max
= 0, idx
= 0;
374 while(lua_next(L
,-2)) {
375 /* Stack: ... key value */
376 lua_pop(L
,1); /* Stack: ... key */
377 if (!lua_isnumber(L
,-1)) goto not_array
;
378 n
= lua_tonumber(L
,-1);
380 if (idx
!= n
|| idx
< 1) goto not_array
;
384 /* We have the total number of elements in "count". Also we have
385 * the max index encountered in "idx". We can't reach this code
386 * if there are indexes <= 0. If you also note that there can not be
387 * repeated keys into a table, you have that if idx==count you are sure
388 * that there are all the keys form 1 to count (both included). */
396 /* If the length operator returns non-zero, that is, there is at least
397 * an object at key '1', we serialize to message pack list. Otherwise
399 static void mp_encode_lua_table(lua_State
*L
, mp_buf
*buf
, int level
) {
400 if (table_is_an_array(L
))
401 mp_encode_lua_table_as_array(L
,buf
,level
);
403 mp_encode_lua_table_as_map(L
,buf
,level
);
406 static void mp_encode_lua_null(lua_State
*L
, mp_buf
*buf
) {
410 mp_buf_append(buf
,b
,1);
413 static void mp_encode_lua_type(lua_State
*L
, mp_buf
*buf
, int level
) {
414 int t
= lua_type(L
,-1);
416 /* Limit the encoding of nested tables to a specfiied maximum depth, so that
417 * we survive when called against circular references in tables. */
418 if (t
== LUA_TTABLE
&& level
== LUACMSGPACK_MAX_NESTING
) t
= LUA_TNIL
;
420 case LUA_TSTRING
: mp_encode_lua_string(L
,buf
); break;
421 case LUA_TBOOLEAN
: mp_encode_lua_bool(L
,buf
); break;
422 case LUA_TNUMBER
: mp_encode_lua_number(L
,buf
); break;
423 case LUA_TTABLE
: mp_encode_lua_table(L
,buf
,level
); break;
424 default: mp_encode_lua_null(L
,buf
); break;
429 static int mp_pack(lua_State
*L
) {
430 mp_buf
*buf
= mp_buf_new();
432 mp_encode_lua_type(L
,buf
,0);
433 lua_pushlstring(L
,(char*)buf
->b
,buf
->len
);
438 /* --------------------------------- Decoding --------------------------------- */
440 void mp_decode_to_lua_type(lua_State
*L
, mp_cur
*c
);
442 void mp_decode_to_lua_array(lua_State
*L
, mp_cur
*c
, size_t len
) {
447 lua_pushnumber(L
,index
++);
448 mp_decode_to_lua_type(L
,c
);
454 void mp_decode_to_lua_hash(lua_State
*L
, mp_cur
*c
, size_t len
) {
457 mp_decode_to_lua_type(L
,c
); /* key */
459 mp_decode_to_lua_type(L
,c
); /* value */
465 /* Decode a Message Pack raw object pointed by the string cursor 'c' to
466 * a Lua type, that is left as the only result on the stack. */
467 void mp_decode_to_lua_type(lua_State
*L
, mp_cur
*c
) {
470 case 0xcc: /* uint 8 */
472 lua_pushnumber(L
,c
->p
[1]);
475 case 0xd0: /* int 8 */
477 lua_pushnumber(L
,(char)c
->p
[1]);
480 case 0xcd: /* uint 16 */
487 case 0xd1: /* int 16 */
489 lua_pushnumber(L
,(int16_t)
494 case 0xce: /* uint 32 */
497 ((uint32_t)c
->p
[1] << 24) |
498 ((uint32_t)c
->p
[2] << 16) |
499 ((uint32_t)c
->p
[3] << 8) |
503 case 0xd2: /* int 32 */
506 ((int32_t)c
->p
[1] << 24) |
507 ((int32_t)c
->p
[2] << 16) |
508 ((int32_t)c
->p
[3] << 8) |
512 case 0xcf: /* uint 64 */
515 ((uint64_t)c
->p
[1] << 56) |
516 ((uint64_t)c
->p
[2] << 48) |
517 ((uint64_t)c
->p
[3] << 40) |
518 ((uint64_t)c
->p
[4] << 32) |
519 ((uint64_t)c
->p
[5] << 24) |
520 ((uint64_t)c
->p
[6] << 16) |
521 ((uint64_t)c
->p
[7] << 8) |
525 case 0xd3: /* int 64 */
528 ((int64_t)c
->p
[1] << 56) |
529 ((int64_t)c
->p
[2] << 48) |
530 ((int64_t)c
->p
[3] << 40) |
531 ((int64_t)c
->p
[4] << 32) |
532 ((int64_t)c
->p
[5] << 24) |
533 ((int64_t)c
->p
[6] << 16) |
534 ((int64_t)c
->p
[7] << 8) |
542 case 0xc3: /* true */
543 lua_pushboolean(L
,1);
546 case 0xc2: /* false */
547 lua_pushboolean(L
,0);
550 case 0xca: /* float */
552 assert(sizeof(float) == 4);
561 case 0xcb: /* double */
563 assert(sizeof(double) == 8);
572 case 0xda: /* raw 16 */
575 size_t l
= (c
->p
[1] << 8) | c
->p
[2];
577 lua_pushlstring(L
,(char*)c
->p
+3,l
);
578 mp_cur_consume(c
,3+l
);
581 case 0xdb: /* raw 32 */
584 size_t l
= (c
->p
[1] << 24) |
589 lua_pushlstring(L
,(char*)c
->p
+5,l
);
590 mp_cur_consume(c
,5+l
);
593 case 0xdc: /* array 16 */
596 size_t l
= (c
->p
[1] << 8) | c
->p
[2];
598 mp_decode_to_lua_array(L
,c
,l
);
601 case 0xdd: /* array 32 */
604 size_t l
= (c
->p
[1] << 24) |
609 mp_decode_to_lua_array(L
,c
,l
);
612 case 0xde: /* map 16 */
615 size_t l
= (c
->p
[1] << 8) | c
->p
[2];
617 mp_decode_to_lua_hash(L
,c
,l
);
620 case 0xdf: /* map 32 */
623 size_t l
= (c
->p
[1] << 24) |
628 mp_decode_to_lua_hash(L
,c
,l
);
631 default: /* types that can't be idenitified by first byte value. */
632 if ((c
->p
[0] & 0x80) == 0) { /* positive fixnum */
633 lua_pushnumber(L
,c
->p
[0]);
635 } else if ((c
->p
[0] & 0xe0) == 0xe0) { /* negative fixnum */
636 lua_pushnumber(L
,(signed char)c
->p
[0]);
638 } else if ((c
->p
[0] & 0xe0) == 0xa0) { /* fix raw */
639 size_t l
= c
->p
[0] & 0x1f;
641 lua_pushlstring(L
,(char*)c
->p
+1,l
);
642 mp_cur_consume(c
,1+l
);
643 } else if ((c
->p
[0] & 0xf0) == 0x90) { /* fix map */
644 size_t l
= c
->p
[0] & 0xf;
646 mp_decode_to_lua_array(L
,c
,l
);
647 } else if ((c
->p
[0] & 0xf0) == 0x80) { /* fix map */
648 size_t l
= c
->p
[0] & 0xf;
650 mp_decode_to_lua_hash(L
,c
,l
);
652 c
->err
= MP_CUR_ERROR_BADFMT
;
657 static int mp_unpack(lua_State
*L
) {
659 const unsigned char *s
;
662 if (!lua_isstring(L
,-1)) {
663 lua_pushstring(L
,"MessagePack decoding needs a string as input.");
667 s
= (const unsigned char*) lua_tolstring(L
,-1,&len
);
668 c
= mp_cur_new(s
,len
);
669 mp_decode_to_lua_type(L
,c
);
671 if (c
->err
== MP_CUR_ERROR_EOF
) {
673 lua_pushstring(L
,"Missing bytes in input.");
675 } else if (c
->err
== MP_CUR_ERROR_BADFMT
) {
677 lua_pushstring(L
,"Bad data format in input.");
679 } else if (c
->left
!= 0) {
681 lua_pushstring(L
,"Extra bytes in input.");
688 /* ---------------------------------------------------------------------------- */
690 static const struct luaL_reg thislib
[] = {
692 {"unpack", mp_unpack
},
696 LUALIB_API
int luaopen_cmsgpack (lua_State
*L
) {
697 luaL_register(L
, "cmsgpack", thislib
);
699 lua_pushliteral(L
, LUACMSGPACK_VERSION
);
700 lua_setfield(L
, -2, "_VERSION");
701 lua_pushliteral(L
, LUACMSGPACK_COPYRIGHT
);
702 lua_setfield(L
, -2, "_COPYRIGHT");
703 lua_pushliteral(L
, LUACMSGPACK_DESCRIPTION
);
704 lua_setfield(L
, -2, "_DESCRIPTION");
708 /******************************************************************************
709 * Copyright (C) 2012 Salvatore Sanfilippo. All rights reserved.
711 * Permission is hereby granted, free of charge, to any person obtaining
712 * a copy of this software and associated documentation files (the
713 * "Software"), to deal in the Software without restriction, including
714 * without limitation the rights to use, copy, modify, merge, publish,
715 * distribute, sublicense, and/or sell copies of the Software, and to
716 * permit persons to whom the Software is furnished to do so, subject to
717 * the following conditions:
719 * The above copyright notice and this permission notice shall be
720 * included in all copies or substantial portions of the Software.
722 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
723 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
724 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
725 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
726 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
727 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
728 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
729 ******************************************************************************/