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