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 LUA_VERSION_NUM < 503 35 #if BITS_32 36 #define lua_pushunsigned(L, n) lua_pushnumber(L, n) 37 #else 38 #define lua_pushunsigned(L, n) lua_pushinteger(L, n) 39 #endif 40 #endif 41 42 /* ============================================================================= 43 * MessagePack implementation and bindings for Lua 5.1/5.2. 44 * Copyright(C) 2012 Salvatore Sanfilippo <[email protected]> 45 * 46 * http://github.com/antirez/lua-cmsgpack 47 * 48 * For MessagePack specification check the following web site: 49 * http://wiki.msgpack.org/display/MSGPACK/Format+specification 50 * 51 * See Copyright Notice at the end of this file. 52 * 53 * CHANGELOG: 54 * 19-Feb-2012 (ver 0.1.0): Initial release. 55 * 20-Feb-2012 (ver 0.2.0): Tables encoding improved. 56 * 20-Feb-2012 (ver 0.2.1): Minor bug fixing. 57 * 20-Feb-2012 (ver 0.3.0): Module renamed lua-cmsgpack (was lua-msgpack). 58 * 04-Apr-2014 (ver 0.3.1): Lua 5.2 support and minor bug fix. 59 * 07-Apr-2014 (ver 0.4.0): Multiple pack/unpack, lua allocator, efficiency. 60 * ========================================================================== */ 61 62 /* -------------------------- Endian conversion -------------------------------- 63 * We use it only for floats and doubles, all the other conversions performed 64 * in an endian independent fashion. So the only thing we need is a function 65 * that swaps a binary string if arch is little endian (and left it untouched 66 * otherwise). */ 67 68 /* Reverse memory bytes if arch is little endian. Given the conceptual 69 * simplicity of the Lua build system we prefer check for endianess at runtime. 70 * The performance difference should be acceptable. */ 71 static void memrevifle(void *ptr, size_t len) { 72 unsigned char *p = (unsigned char *)ptr, 73 *e = (unsigned char *)p+len-1, 74 aux; 75 int test = 1; 76 unsigned char *testp = (unsigned char*) &test; 77 78 if (testp[0] == 0) return; /* Big endian, nothing to do. */ 79 len /= 2; 80 while(len--) { 81 aux = *p; 82 *p = *e; 83 *e = aux; 84 p++; 85 e--; 86 } 87 } 88 89 /* ---------------------------- String buffer ---------------------------------- 90 * This is a simple implementation of string buffers. The only operation 91 * supported is creating empty buffers and appending bytes to it. 92 * The string buffer uses 2x preallocation on every realloc for O(N) append 93 * behavior. */ 94 95 typedef struct mp_buf { 96 lua_State *L; 97 unsigned char *b; 98 size_t len, free; 99 } mp_buf; 100 101 static void *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) { 102 void *(*local_realloc) (void *, void *, size_t osize, size_t nsize) = NULL; 103 void *ud; 104 105 local_realloc = lua_getallocf(L, &ud); 106 107 return local_realloc(ud, target, osize, nsize); 108 } 109 110 static mp_buf *mp_buf_new(lua_State *L) { 111 mp_buf *buf = NULL; 112 113 /* Old size = 0; new size = sizeof(*buf) */ 114 buf = (mp_buf*)mp_realloc(L, NULL, 0, sizeof(*buf)); 115 116 buf->L = L; 117 buf->b = NULL; 118 buf->len = buf->free = 0; 119 return buf; 120 } 121 122 static void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) { 123 if (buf->free < len) { 124 size_t newlen = buf->len+len; 125 126 buf->b = (unsigned char*)mp_realloc(buf->L, buf->b, buf->len, newlen*2); 127 buf->free = newlen; 128 } 129 memcpy(buf->b+buf->len,s,len); 130 buf->len += len; 131 buf->free -= len; 132 } 133 134 void mp_buf_free(mp_buf *buf) { 135 mp_realloc(buf->L, buf->b, buf->len, 0); /* realloc to 0 = free */ 136 mp_realloc(buf->L, buf, sizeof(*buf), 0); 137 } 138 139 /* ---------------------------- String cursor ---------------------------------- 140 * This simple data structure is used for parsing. Basically you create a cursor 141 * using a string pointer and a length, then it is possible to access the 142 * current string position with cursor->p, check the remaining length 143 * in cursor->left, and finally consume more string using 144 * mp_cur_consume(cursor,len), to advance 'p' and subtract 'left'. 145 * An additional field cursor->error is set to zero on initialization and can 146 * be used to report errors. */ 147 148 #define MP_CUR_ERROR_NONE 0 149 #define MP_CUR_ERROR_EOF 1 /* Not enough data to complete operation. */ 150 #define MP_CUR_ERROR_BADFMT 2 /* Bad data format */ 151 152 typedef struct mp_cur { 153 const unsigned char *p; 154 size_t left; 155 int err; 156 } mp_cur; 157 158 static void mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) { 159 cursor->p = s; 160 cursor->left = len; 161 cursor->err = MP_CUR_ERROR_NONE; 162 } 163 164 #define mp_cur_consume(_c,_len) do { _c->p += _len; _c->left -= _len; } while(0) 165 166 /* When there is not enough room we set an error in the cursor and return. This 167 * is very common across the code so we have a macro to make the code look 168 * a bit simpler. */ 169 #define mp_cur_need(_c,_len) do { \ 170 if (_c->left < _len) { \ 171 _c->err = MP_CUR_ERROR_EOF; \ 172 return; \ 173 } \ 174 } while(0) 175 176 /* ------------------------- Low level MP encoding -------------------------- */ 177 178 static void mp_encode_bytes(mp_buf *buf, const unsigned char *s, size_t len) { 179 unsigned char hdr[5]; 180 int hdrlen; 181 182 if (len < 32) { 183 hdr[0] = 0xa0 | (len&0xff); /* fix raw */ 184 hdrlen = 1; 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(buf,hdr,hdrlen); 199 mp_buf_append(buf,s,len); 200 } 201 202 /* we assume IEEE 754 internal format for single and double precision floats. */ 203 static void mp_encode_double(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(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(buf,b,9); 218 } 219 } 220 221 static void mp_encode_int(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] = ((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(buf,b,enclen); 291 } 292 293 static void mp_encode_array(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(buf,b,enclen); 314 } 315 316 static void mp_encode_map(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(buf,b,enclen); 337 } 338 339 /* --------------------------- Lua types encoding --------------------------- */ 340 341 static 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(buf,(const unsigned char*)s,len); 347 } 348 349 static 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(buf,&b,1); 352 } 353 354 /* Lua 5.3 has a built in 64-bit integer type */ 355 static 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(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 static 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(buf,(double)n); 374 } 375 } 376 377 static 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 static 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(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 static 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(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 static 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 static 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 static void mp_encode_lua_null(lua_State *L, mp_buf *buf) { 474 unsigned char b[1]; 475 (void)L; 476 477 b[0] = 0xc0; 478 mp_buf_append(buf,b,1); 479 } 480 481 static void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) { 482 int t = lua_type(L,-1); 483 484 /* Limit the encoding of nested tables to a specified maximum depth, so that 485 * we survive when called against circular references in tables. */ 486 if (t == LUA_TTABLE && level == LUACMSGPACK_MAX_NESTING) t = LUA_TNIL; 487 switch(t) { 488 case LUA_TSTRING: mp_encode_lua_string(L,buf); break; 489 case LUA_TBOOLEAN: mp_encode_lua_bool(L,buf); break; 490 case LUA_TNUMBER: 491 #if LUA_VERSION_NUM < 503 492 mp_encode_lua_number(L,buf); break; 493 #else 494 if (lua_isinteger(L, -1)) { 495 mp_encode_lua_integer(L, buf); 496 } else { 497 mp_encode_lua_number(L, buf); 498 } 499 break; 500 #endif 501 case LUA_TTABLE: mp_encode_lua_table(L,buf,level); break; 502 default: mp_encode_lua_null(L,buf); break; 503 } 504 lua_pop(L,1); 505 } 506 507 /* 508 * Packs all arguments as a stream for multiple upacking later. 509 * Returns error if no arguments provided. 510 */ 511 static int mp_pack(lua_State *L) { 512 int nargs = lua_gettop(L); 513 int i; 514 mp_buf *buf; 515 516 if (nargs == 0) 517 return luaL_argerror(L, 0, "MessagePack pack needs input."); 518 519 buf = mp_buf_new(L); 520 for(i = 1; i <= nargs; i++) { 521 /* Copy argument i to top of stack for _encode processing; 522 * the encode function pops it from the stack when complete. */ 523 lua_pushvalue(L, i); 524 525 mp_encode_lua_type(L,buf,0); 526 527 lua_pushlstring(L,(char*)buf->b,buf->len); 528 529 /* Reuse the buffer for the next operation by 530 * setting its free count to the total buffer size 531 * and the current position to zero. */ 532 buf->free += buf->len; 533 buf->len = 0; 534 } 535 mp_buf_free(buf); 536 537 /* Concatenate all nargs buffers together */ 538 lua_concat(L, nargs); 539 return 1; 540 } 541 542 /* ------------------------------- Decoding --------------------------------- */ 543 544 void mp_decode_to_lua_type(lua_State *L, mp_cur *c); 545 546 void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) { 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 lua_newtable(L); 560 while(len--) { 561 mp_decode_to_lua_type(L,c); /* key */ 562 if (c->err) return; 563 mp_decode_to_lua_type(L,c); /* value */ 564 if (c->err) return; 565 lua_settable(L,-3); 566 } 567 } 568 569 /* Decode a Message Pack raw object pointed by the string cursor 'c' to 570 * a Lua type, that is left as the only result on the stack. */ 571 void mp_decode_to_lua_type(lua_State *L, mp_cur *c) { 572 mp_cur_need(c,1); 573 574 /* If we return more than 18 elements, we must resize the stack to 575 * fit all our return values. But, there is no way to 576 * determine how many objects a msgpack will unpack to up front, so 577 * we request a +1 larger stack on each iteration (noop if stack is 578 * big enough, and when stack does require resize it doubles in size) */ 579 luaL_checkstack(L, 1, 580 "too many return values at once; " 581 "use unpack_one or unpack_limit instead."); 582 583 switch(c->p[0]) { 584 case 0xcc: /* uint 8 */ 585 mp_cur_need(c,2); 586 lua_pushunsigned(L,c->p[1]); 587 mp_cur_consume(c,2); 588 break; 589 case 0xd0: /* int 8 */ 590 mp_cur_need(c,2); 591 lua_pushinteger(L,(char)c->p[1]); 592 mp_cur_consume(c,2); 593 break; 594 case 0xcd: /* uint 16 */ 595 mp_cur_need(c,3); 596 lua_pushunsigned(L, 597 (c->p[1] << 8) | 598 c->p[2]); 599 mp_cur_consume(c,3); 600 break; 601 case 0xd1: /* int 16 */ 602 mp_cur_need(c,3); 603 lua_pushinteger(L,(int16_t) 604 (c->p[1] << 8) | 605 c->p[2]); 606 mp_cur_consume(c,3); 607 break; 608 case 0xce: /* uint 32 */ 609 mp_cur_need(c,5); 610 lua_pushunsigned(L, 611 ((uint32_t)c->p[1] << 24) | 612 ((uint32_t)c->p[2] << 16) | 613 ((uint32_t)c->p[3] << 8) | 614 (uint32_t)c->p[4]); 615 mp_cur_consume(c,5); 616 break; 617 case 0xd2: /* int 32 */ 618 mp_cur_need(c,5); 619 lua_pushinteger(L, 620 ((int32_t)c->p[1] << 24) | 621 ((int32_t)c->p[2] << 16) | 622 ((int32_t)c->p[3] << 8) | 623 (int32_t)c->p[4]); 624 mp_cur_consume(c,5); 625 break; 626 case 0xcf: /* uint 64 */ 627 mp_cur_need(c,9); 628 lua_pushunsigned(L, 629 ((uint64_t)c->p[1] << 56) | 630 ((uint64_t)c->p[2] << 48) | 631 ((uint64_t)c->p[3] << 40) | 632 ((uint64_t)c->p[4] << 32) | 633 ((uint64_t)c->p[5] << 24) | 634 ((uint64_t)c->p[6] << 16) | 635 ((uint64_t)c->p[7] << 8) | 636 (uint64_t)c->p[8]); 637 mp_cur_consume(c,9); 638 break; 639 case 0xd3: /* int 64 */ 640 mp_cur_need(c,9); 641 #if LUA_VERSION_NUM < 503 642 lua_pushnumber(L, 643 #else 644 lua_pushinteger(L, 645 #endif 646 ((int64_t)c->p[1] << 56) | 647 ((int64_t)c->p[2] << 48) | 648 ((int64_t)c->p[3] << 40) | 649 ((int64_t)c->p[4] << 32) | 650 ((int64_t)c->p[5] << 24) | 651 ((int64_t)c->p[6] << 16) | 652 ((int64_t)c->p[7] << 8) | 653 (int64_t)c->p[8]); 654 mp_cur_consume(c,9); 655 break; 656 case 0xc0: /* nil */ 657 lua_pushnil(L); 658 mp_cur_consume(c,1); 659 break; 660 case 0xc3: /* true */ 661 lua_pushboolean(L,1); 662 mp_cur_consume(c,1); 663 break; 664 case 0xc2: /* false */ 665 lua_pushboolean(L,0); 666 mp_cur_consume(c,1); 667 break; 668 case 0xca: /* float */ 669 mp_cur_need(c,5); 670 assert(sizeof(float) == 4); 671 { 672 float f; 673 memcpy(&f,c->p+1,4); 674 memrevifle(&f,4); 675 lua_pushnumber(L,f); 676 mp_cur_consume(c,5); 677 } 678 break; 679 case 0xcb: /* double */ 680 mp_cur_need(c,9); 681 assert(sizeof(double) == 8); 682 { 683 double d; 684 memcpy(&d,c->p+1,8); 685 memrevifle(&d,8); 686 lua_pushnumber(L,d); 687 mp_cur_consume(c,9); 688 } 689 break; 690 case 0xda: /* raw 16 */ 691 mp_cur_need(c,3); 692 { 693 size_t l = (c->p[1] << 8) | c->p[2]; 694 mp_cur_need(c,3+l); 695 lua_pushlstring(L,(char*)c->p+3,l); 696 mp_cur_consume(c,3+l); 697 } 698 break; 699 case 0xdb: /* raw 32 */ 700 mp_cur_need(c,5); 701 { 702 size_t l = (c->p[1] << 24) | 703 (c->p[2] << 16) | 704 (c->p[3] << 8) | 705 c->p[4]; 706 mp_cur_need(c,5+l); 707 lua_pushlstring(L,(char*)c->p+5,l); 708 mp_cur_consume(c,5+l); 709 } 710 break; 711 case 0xdc: /* array 16 */ 712 mp_cur_need(c,3); 713 { 714 size_t l = (c->p[1] << 8) | c->p[2]; 715 mp_cur_consume(c,3); 716 mp_decode_to_lua_array(L,c,l); 717 } 718 break; 719 case 0xdd: /* array 32 */ 720 mp_cur_need(c,5); 721 { 722 size_t l = (c->p[1] << 24) | 723 (c->p[2] << 16) | 724 (c->p[3] << 8) | 725 c->p[4]; 726 mp_cur_consume(c,5); 727 mp_decode_to_lua_array(L,c,l); 728 } 729 break; 730 case 0xde: /* map 16 */ 731 mp_cur_need(c,3); 732 { 733 size_t l = (c->p[1] << 8) | c->p[2]; 734 mp_cur_consume(c,3); 735 mp_decode_to_lua_hash(L,c,l); 736 } 737 break; 738 case 0xdf: /* map 32 */ 739 mp_cur_need(c,5); 740 { 741 size_t l = (c->p[1] << 24) | 742 (c->p[2] << 16) | 743 (c->p[3] << 8) | 744 c->p[4]; 745 mp_cur_consume(c,5); 746 mp_decode_to_lua_hash(L,c,l); 747 } 748 break; 749 default: /* types that can't be idenitified by first byte value. */ 750 if ((c->p[0] & 0x80) == 0) { /* positive fixnum */ 751 lua_pushunsigned(L,c->p[0]); 752 mp_cur_consume(c,1); 753 } else if ((c->p[0] & 0xe0) == 0xe0) { /* negative fixnum */ 754 lua_pushinteger(L,(signed char)c->p[0]); 755 mp_cur_consume(c,1); 756 } else if ((c->p[0] & 0xe0) == 0xa0) { /* fix raw */ 757 size_t l = c->p[0] & 0x1f; 758 mp_cur_need(c,1+l); 759 lua_pushlstring(L,(char*)c->p+1,l); 760 mp_cur_consume(c,1+l); 761 } else if ((c->p[0] & 0xf0) == 0x90) { /* fix map */ 762 size_t l = c->p[0] & 0xf; 763 mp_cur_consume(c,1); 764 mp_decode_to_lua_array(L,c,l); 765 } else if ((c->p[0] & 0xf0) == 0x80) { /* fix map */ 766 size_t l = c->p[0] & 0xf; 767 mp_cur_consume(c,1); 768 mp_decode_to_lua_hash(L,c,l); 769 } else { 770 c->err = MP_CUR_ERROR_BADFMT; 771 } 772 } 773 } 774 775 static int mp_unpack_full(lua_State *L, int limit, int offset) { 776 size_t len; 777 const char *s; 778 mp_cur c; 779 int cnt; /* Number of objects unpacked */ 780 int decode_all = (!limit && !offset); 781 782 s = luaL_checklstring(L,1,&len); /* if no match, exits */ 783 784 if (offset < 0 || limit < 0) /* requesting negative off or lim is invalid */ 785 return luaL_error(L, 786 "Invalid request to unpack with offset of %d and limit of %d.", 787 offset, len); 788 else if (offset > len) 789 return luaL_error(L, 790 "Start offset %d greater than input length %d.", offset, len); 791 792 if (decode_all) limit = INT_MAX; 793 794 mp_cur_init(&c,(const unsigned char *)s+offset,len-offset); 795 796 /* We loop over the decode because this could be a stream 797 * of multiple top-level values serialized together */ 798 for(cnt = 0; c.left > 0 && cnt < limit; cnt++) { 799 mp_decode_to_lua_type(L,&c); 800 801 if (c.err == MP_CUR_ERROR_EOF) { 802 return luaL_error(L,"Missing bytes in input."); 803 } else if (c.err == MP_CUR_ERROR_BADFMT) { 804 return luaL_error(L,"Bad data format in input."); 805 } 806 } 807 808 if (!decode_all) { 809 /* c->left is the remaining size of the input buffer. 810 * subtract the entire buffer size from the unprocessed size 811 * to get our next start offset */ 812 int offset = len - c.left; 813 /* Return offset -1 when we have have processed the entire buffer. */ 814 lua_pushinteger(L, c.left == 0 ? -1 : offset); 815 /* Results are returned with the arg elements still 816 * in place. Lua takes care of only returning 817 * elements above the args for us. 818 * In this case, we have one arg on the stack 819 * for this function, so we insert our first return 820 * value at position 2. */ 821 lua_insert(L, 2); 822 cnt += 1; /* increase return count by one to make room for offset */ 823 } 824 825 return cnt; 826 } 827 828 static int mp_unpack(lua_State *L) { 829 return mp_unpack_full(L, 0, 0); 830 } 831 832 static int mp_unpack_one(lua_State *L) { 833 int offset = luaL_optint(L, 2, 0); 834 /* Variable pop because offset may not exist */ 835 lua_pop(L, lua_gettop(L)-1); 836 return mp_unpack_full(L, 1, offset); 837 } 838 839 static int mp_unpack_limit(lua_State *L) { 840 int limit = luaL_checkint(L, 2); 841 int offset = luaL_optint(L, 3, 0); 842 /* Variable pop because offset may not exist */ 843 lua_pop(L, lua_gettop(L)-1); 844 845 return mp_unpack_full(L, limit, offset); 846 } 847 848 static int mp_safe(lua_State *L) { 849 int argc, err, total_results; 850 851 argc = lua_gettop(L); 852 853 /* This adds our function to the bottom of the stack 854 * (the "call this function" position) */ 855 lua_pushvalue(L, lua_upvalueindex(1)); 856 lua_insert(L, 1); 857 858 err = lua_pcall(L, argc, LUA_MULTRET, 0); 859 total_results = lua_gettop(L); 860 861 if (!err) { 862 return total_results; 863 } else { 864 lua_pushnil(L); 865 lua_insert(L,-2); 866 return 2; 867 } 868 } 869 870 /* -------------------------------------------------------------------------- */ 871 static const struct luaL_Reg cmds[] = { 872 {"pack", mp_pack}, 873 {"unpack", mp_unpack}, 874 {"unpack_one", mp_unpack_one}, 875 {"unpack_limit", mp_unpack_limit}, 876 {0} 877 }; 878 879 static int luaopen_create(lua_State *L) { 880 int i; 881 /* Manually construct our module table instead of 882 * relying on _register or _newlib */ 883 lua_newtable(L); 884 885 for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) { 886 lua_pushcfunction(L, cmds[i].func); 887 lua_setfield(L, -2, cmds[i].name); 888 } 889 890 /* Add metadata */ 891 lua_pushliteral(L, LUACMSGPACK_NAME); 892 lua_setfield(L, -2, "_NAME"); 893 lua_pushliteral(L, LUACMSGPACK_VERSION); 894 lua_setfield(L, -2, "_VERSION"); 895 lua_pushliteral(L, LUACMSGPACK_COPYRIGHT); 896 lua_setfield(L, -2, "_COPYRIGHT"); 897 lua_pushliteral(L, LUACMSGPACK_DESCRIPTION); 898 lua_setfield(L, -2, "_DESCRIPTION"); 899 return 1; 900 } 901 902 LUALIB_API int luaopen_cmsgpack(lua_State *L) { 903 luaopen_create(L); 904 905 #if LUA_VERSION_NUM < 502 906 /* Register name globally for 5.1 */ 907 lua_pushvalue(L, -1); 908 lua_setglobal(L, LUACMSGPACK_NAME); 909 #endif 910 911 return 1; 912 } 913 914 LUALIB_API int luaopen_cmsgpack_safe(lua_State *L) { 915 int i; 916 917 luaopen_cmsgpack(L); 918 919 /* Wrap all functions in the safe handler */ 920 for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) { 921 lua_getfield(L, -1, cmds[i].name); 922 lua_pushcclosure(L, mp_safe, 1); 923 lua_setfield(L, -2, cmds[i].name); 924 } 925 926 #if LUA_VERSION_NUM < 502 927 /* Register name globally for 5.1 */ 928 lua_pushvalue(L, -1); 929 lua_setglobal(L, LUACMSGPACK_SAFE_NAME); 930 #endif 931 932 return 1; 933 } 934 935 /****************************************************************************** 936 * Copyright (C) 2012 Salvatore Sanfilippo. All rights reserved. 937 * 938 * Permission is hereby granted, free of charge, to any person obtaining 939 * a copy of this software and associated documentation files (the 940 * "Software"), to deal in the Software without restriction, including 941 * without limitation the rights to use, copy, modify, merge, publish, 942 * distribute, sublicense, and/or sell copies of the Software, and to 943 * permit persons to whom the Software is furnished to do so, subject to 944 * the following conditions: 945 * 946 * The above copyright notice and this permission notice shall be 947 * included in all copies or substantial portions of the Software. 948 * 949 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 950 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 951 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 952 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 953 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 954 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 955 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 956 ******************************************************************************/ 957