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