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