lua_cmsgpack.c 29.5 KB
Newer Older
A
antirez 已提交
1 2 3 4 5 6 7 8 9
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include "lua.h"
#include "lauxlib.h"

M
Matt Stancliff 已提交
10 11 12
#define LUACMSGPACK_NAME        "cmsgpack"
#define LUACMSGPACK_SAFE_NAME   "cmsgpack_safe"
#define LUACMSGPACK_VERSION     "lua-cmsgpack 0.4.0"
A
antirez 已提交
13 14 15
#define LUACMSGPACK_COPYRIGHT   "Copyright (C) 2012, Salvatore Sanfilippo"
#define LUACMSGPACK_DESCRIPTION "MessagePack C implementation for Lua"

M
Matt Stancliff 已提交
16 17
/* Allows a preprocessor directive to override MAX_NESTING */
#ifndef LUACMSGPACK_MAX_NESTING
18
    #define LUACMSGPACK_MAX_NESTING  16 /* Max tables nesting. */
M
Matt Stancliff 已提交
19 20 21
#endif

/* 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))
M
Matt Stancliff 已提交
23 24 25 26

#define IS_INT64_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int64_t)
#define IS_INT_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int)

27 28 29 30 31 32 33
/* If size of pointer is equal to a 4 byte integer, we're on 32 bits. */
#if UINTPTR_MAX == UINT_MAX
    #define BITS_32 1
#else
    #define BITS_32 0
#endif

34 35 36 37
#if BITS_32
    #define lua_pushunsigned(L, n) lua_pushnumber(L, n)
#else
    #define lua_pushunsigned(L, n) lua_pushinteger(L, n)
M
Matt Stancliff 已提交
38 39 40 41
#endif

/* =============================================================================
 * MessagePack implementation and bindings for Lua 5.1/5.2.
A
antirez 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55
 * Copyright(C) 2012 Salvatore Sanfilippo <antirez@gmail.com>
 *
 * http://github.com/antirez/lua-cmsgpack
 *
 * For MessagePack specification check the following web site:
 * http://wiki.msgpack.org/display/MSGPACK/Format+specification
 *
 * See Copyright Notice at the end of this file.
 *
 * CHANGELOG:
 * 19-Feb-2012 (ver 0.1.0): Initial release.
 * 20-Feb-2012 (ver 0.2.0): Tables encoding improved.
 * 20-Feb-2012 (ver 0.2.1): Minor bug fixing.
 * 20-Feb-2012 (ver 0.3.0): Module renamed lua-cmsgpack (was lua-msgpack).
M
Matt Stancliff 已提交
56 57 58
 * 04-Apr-2014 (ver 0.3.1): Lua 5.2 support and minor bug fix.
 * 07-Apr-2014 (ver 0.4.0): Multiple pack/unpack, lua allocator, efficiency.
 * ========================================================================== */
A
antirez 已提交
59

M
Matt Stancliff 已提交
60 61
/* -------------------------- Endian conversion --------------------------------
 * We use it only for floats and doubles, all the other conversions performed
A
antirez 已提交
62
 * in an endian independent fashion. So the only thing we need is a function
M
Matt Stancliff 已提交
63
 * that swaps a binary string if arch is little endian (and left it untouched
A
antirez 已提交
64 65 66
 * otherwise). */

/* Reverse memory bytes if arch is little endian. Given the conceptual
M
Matt Stancliff 已提交
67
 * simplicity of the Lua build system we prefer check for endianess at runtime.
A
antirez 已提交
68 69
 * The performance difference should be acceptable. */
static void memrevifle(void *ptr, size_t len) {
M
Matt Stancliff 已提交
70 71 72
    unsigned char   *p = (unsigned char *)ptr,
                    *e = (unsigned char *)p+len-1,
                    aux;
A
antirez 已提交
73 74 75
    int test = 1;
    unsigned char *testp = (unsigned char*) &test;

76
    if (testp[0] == 0) return; /* Big endian, nothing to do. */
A
antirez 已提交
77 78 79 80 81 82 83 84 85 86
    len /= 2;
    while(len--) {
        aux = *p;
        *p = *e;
        *e = aux;
        p++;
        e--;
    }
}

M
Matt Stancliff 已提交
87
/* ---------------------------- String buffer ----------------------------------
88
 * This is a simple implementation of string buffers. The only operation
A
antirez 已提交
89 90 91 92 93
 * supported is creating empty buffers and appending bytes to it.
 * The string buffer uses 2x preallocation on every realloc for O(N) append
 * behavior.  */

typedef struct mp_buf {
M
Matt Stancliff 已提交
94
    lua_State *L;
A
antirez 已提交
95 96 97 98
    unsigned char *b;
    size_t len, free;
} mp_buf;

M
Matt Stancliff 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
static void *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) {
    void *(*local_realloc) (void *, void *, size_t osize, size_t nsize) = NULL;
    void *ud;

    local_realloc = lua_getallocf(L, &ud);

    return local_realloc(ud, target, osize, nsize);
}

static mp_buf *mp_buf_new(lua_State *L) {
    mp_buf *buf = NULL;

    /* Old size = 0; new size = sizeof(*buf) */
112
    buf = (mp_buf*)mp_realloc(L, NULL, 0, sizeof(*buf));
M
Matt Stancliff 已提交
113 114

    buf->L = L;
A
antirez 已提交
115 116 117 118 119
    buf->b = NULL;
    buf->len = buf->free = 0;
    return buf;
}

M
Matt Stancliff 已提交
120
static void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) {
A
antirez 已提交
121 122 123
    if (buf->free < len) {
        size_t newlen = buf->len+len;

M
Matt Stancliff 已提交
124
        buf->b = (unsigned char*)mp_realloc(buf->L, buf->b, buf->len, newlen*2);
A
antirez 已提交
125 126 127 128 129 130 131 132
        buf->free = newlen;
    }
    memcpy(buf->b+buf->len,s,len);
    buf->len += len;
    buf->free -= len;
}

void mp_buf_free(mp_buf *buf) {
M
Matt Stancliff 已提交
133 134
    mp_realloc(buf->L, buf->b, buf->len, 0); /* realloc to 0 = free */
    mp_realloc(buf->L, buf, sizeof(*buf), 0);
A
antirez 已提交
135 136
}

M
Matt Stancliff 已提交
137
/* ---------------------------- String cursor ----------------------------------
A
antirez 已提交
138 139 140 141 142 143 144 145 146
 * This simple data structure is used for parsing. Basically you create a cursor
 * using a string pointer and a length, then it is possible to access the
 * current string position with cursor->p, check the remaining length
 * in cursor->left, and finally consume more string using
 * mp_cur_consume(cursor,len), to advance 'p' and subtract 'left'.
 * An additional field cursor->error is set to zero on initialization and can
 * be used to report errors. */

#define MP_CUR_ERROR_NONE   0
147
#define MP_CUR_ERROR_EOF    1   /* Not enough data to complete operation. */
A
antirez 已提交
148 149 150 151 152 153 154 155
#define MP_CUR_ERROR_BADFMT 2   /* Bad data format */

typedef struct mp_cur {
    const unsigned char *p;
    size_t left;
    int err;
} mp_cur;

M
Matt Stancliff 已提交
156
static void mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) {
A
antirez 已提交
157 158 159 160 161 162 163
    cursor->p = s;
    cursor->left = len;
    cursor->err = MP_CUR_ERROR_NONE;
}

#define mp_cur_consume(_c,_len) do { _c->p += _len; _c->left -= _len; } while(0)

164
/* When there is not enough room we set an error in the cursor and return. This
A
antirez 已提交
165 166 167 168 169 170 171 172 173
 * is very common across the code so we have a macro to make the code look
 * a bit simpler. */
#define mp_cur_need(_c,_len) do { \
    if (_c->left < _len) { \
        _c->err = MP_CUR_ERROR_EOF; \
        return; \
    } \
} while(0)

M
Matt Stancliff 已提交
174
/* ------------------------- Low level MP encoding -------------------------- */
A
antirez 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

static void mp_encode_bytes(mp_buf *buf, const unsigned char *s, size_t len) {
    unsigned char hdr[5];
    int hdrlen;

    if (len < 32) {
        hdr[0] = 0xa0 | (len&0xff); /* fix raw */
        hdrlen = 1;
    } else if (len <= 0xffff) {
        hdr[0] = 0xda;
        hdr[1] = (len&0xff00)>>8;
        hdr[2] = len&0xff;
        hdrlen = 3;
    } else {
        hdr[0] = 0xdb;
190 191 192 193
        hdr[1] = (unsigned char)((len&0xff000000)>>24);
        hdr[2] = (unsigned char)((len & 0xff0000) >> 16);
        hdr[3] = (unsigned char)((len & 0xff00) >> 8);
        hdr[4] = (unsigned char)(len & 0xff);
A
antirez 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        hdrlen = 5;
    }
    mp_buf_append(buf,hdr,hdrlen);
    mp_buf_append(buf,s,len);
}

/* we assume IEEE 754 internal format for single and double precision floats. */
static void mp_encode_double(mp_buf *buf, double d) {
    unsigned char b[9];
    float f = d;

    assert(sizeof(f) == 4 && sizeof(d) == 8);
    if (d == (double)f) {
        b[0] = 0xca;    /* float IEEE 754 */
        memcpy(b+1,&f,4);
        memrevifle(b+1,4);
        mp_buf_append(buf,b,5);
    } else if (sizeof(d) == 8) {
        b[0] = 0xcb;    /* double IEEE 754 */
        memcpy(b+1,&d,8);
        memrevifle(b+1,8);
        mp_buf_append(buf,b,9);
    }
}

static void mp_encode_int(mp_buf *buf, int64_t n) {
    unsigned char b[9];
    int enclen;

    if (n >= 0) {
        if (n <= 127) {
            b[0] = n & 0x7f;    /* positive fixnum */
            enclen = 1;
        } else if (n <= 0xff) {
            b[0] = 0xcc;        /* uint 8 */
            b[1] = n & 0xff;
            enclen = 2;
        } else if (n <= 0xffff) {
            b[0] = 0xcd;        /* uint 16 */
            b[1] = (n & 0xff00) >> 8;
            b[2] = n & 0xff;
            enclen = 3;
        } else if (n <= 0xffffffffLL) {
            b[0] = 0xce;        /* uint 32 */
            b[1] = (n & 0xff000000) >> 24;
            b[2] = (n & 0xff0000) >> 16;
            b[3] = (n & 0xff00) >> 8;
            b[4] = n & 0xff;
            enclen = 5;
        } else {
            b[0] = 0xcf;        /* uint 64 */
            b[1] = (n & 0xff00000000000000LL) >> 56;
            b[2] = (n & 0xff000000000000LL) >> 48;
            b[3] = (n & 0xff0000000000LL) >> 40;
            b[4] = (n & 0xff00000000LL) >> 32;
            b[5] = (n & 0xff000000) >> 24;
            b[6] = (n & 0xff0000) >> 16;
            b[7] = (n & 0xff00) >> 8;
            b[8] = n & 0xff;
            enclen = 9;
        }
    } else {
        if (n >= -32) {
257
            b[0] = ((signed char)n);   /* negative fixnum */
A
antirez 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
            enclen = 1;
        } else if (n >= -128) {
            b[0] = 0xd0;        /* int 8 */
            b[1] = n & 0xff;
            enclen = 2;
        } else if (n >= -32768) {
            b[0] = 0xd1;        /* int 16 */
            b[1] = (n & 0xff00) >> 8;
            b[2] = n & 0xff;
            enclen = 3;
        } else if (n >= -2147483648LL) {
            b[0] = 0xd2;        /* int 32 */
            b[1] = (n & 0xff000000) >> 24;
            b[2] = (n & 0xff0000) >> 16;
            b[3] = (n & 0xff00) >> 8;
            b[4] = n & 0xff;
            enclen = 5;
        } else {
            b[0] = 0xd3;        /* int 64 */
            b[1] = (n & 0xff00000000000000LL) >> 56;
            b[2] = (n & 0xff000000000000LL) >> 48;
            b[3] = (n & 0xff0000000000LL) >> 40;
            b[4] = (n & 0xff00000000LL) >> 32;
            b[5] = (n & 0xff000000) >> 24;
            b[6] = (n & 0xff0000) >> 16;
            b[7] = (n & 0xff00) >> 8;
            b[8] = n & 0xff;
            enclen = 9;
        }
    }
    mp_buf_append(buf,b,enclen);
}

static void mp_encode_array(mp_buf *buf, int64_t n) {
    unsigned char b[5];
    int enclen;

    if (n <= 15) {
        b[0] = 0x90 | (n & 0xf);    /* fix array */
        enclen = 1;
    } else if (n <= 65535) {
        b[0] = 0xdc;                /* array 16 */
        b[1] = (n & 0xff00) >> 8;
        b[2] = n & 0xff;
        enclen = 3;
    } else {
        b[0] = 0xdd;                /* array 32 */
        b[1] = (n & 0xff000000) >> 24;
        b[2] = (n & 0xff0000) >> 16;
        b[3] = (n & 0xff00) >> 8;
        b[4] = n & 0xff;
        enclen = 5;
    }
    mp_buf_append(buf,b,enclen);
}

static void mp_encode_map(mp_buf *buf, int64_t n) {
    unsigned char b[5];
    int enclen;

    if (n <= 15) {
        b[0] = 0x80 | (n & 0xf);    /* fix map */
        enclen = 1;
    } else if (n <= 65535) {
        b[0] = 0xde;                /* map 16 */
        b[1] = (n & 0xff00) >> 8;
        b[2] = n & 0xff;
        enclen = 3;
    } else {
        b[0] = 0xdf;                /* map 32 */
        b[1] = (n & 0xff000000) >> 24;
        b[2] = (n & 0xff0000) >> 16;
        b[3] = (n & 0xff00) >> 8;
        b[4] = n & 0xff;
        enclen = 5;
    }
    mp_buf_append(buf,b,enclen);
}

M
Matt Stancliff 已提交
337
/* --------------------------- Lua types encoding --------------------------- */
A
antirez 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351

static void mp_encode_lua_string(lua_State *L, mp_buf *buf) {
    size_t len;
    const char *s;

    s = lua_tolstring(L,-1,&len);
    mp_encode_bytes(buf,(const unsigned char*)s,len);
}

static void mp_encode_lua_bool(lua_State *L, mp_buf *buf) {
    unsigned char b = lua_toboolean(L,-1) ? 0xc3 : 0xc2;
    mp_buf_append(buf,&b,1);
}

M
Matt Stancliff 已提交
352 353
/* Lua 5.3 has a built in 64-bit integer type */
static void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
354 355 356
#if (LUA_VERSION_NUM < 503) && BITS_32
    lua_Number i = lua_tonumber(L,-1);
#else
M
Matt Stancliff 已提交
357
    lua_Integer i = lua_tointeger(L,-1);
358
#endif
M
Matt Stancliff 已提交
359 360 361 362 363 364
    mp_encode_int(buf, (int64_t)i);
}

/* Lua 5.2 and lower only has 64-bit doubles, so we need to
 * detect if the double may be representable as an int
 * for Lua < 5.3 */
A
antirez 已提交
365 366 367
static void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
    lua_Number n = lua_tonumber(L,-1);

M
Matt Stancliff 已提交
368 369
    if (IS_INT64_EQUIVALENT(n)) {
        mp_encode_lua_integer(L, buf);
A
antirez 已提交
370
    } else {
M
Matt Stancliff 已提交
371
        mp_encode_double(buf,(double)n);
A
antirez 已提交
372 373 374 375 376 377 378
    }
}

static void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level);

/* Convert a lua table into a message pack list. */
static void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
M
Matt Stancliff 已提交
379
#if LUA_VERSION_NUM < 502
A
antirez 已提交
380
    size_t len = lua_objlen(L,-1), j;
M
Matt Stancliff 已提交
381 382 383
#else
    size_t len = lua_rawlen(L,-1), j;
#endif
A
antirez 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

    mp_encode_array(buf,len);
    for (j = 1; j <= len; j++) {
        lua_pushnumber(L,j);
        lua_gettable(L,-2);
        mp_encode_lua_type(L,buf,level+1);
    }
}

/* Convert a lua table into a message pack key-value map. */
static void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
    size_t len = 0;

    /* First step: count keys into table. No other way to do it with the
     * Lua API, we need to iterate a first time. Note that an alternative
     * would be to do a single run, and then hack the buffer to insert the
400
     * map opcodes for message pack. Too hackish for this lib. */
A
antirez 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    lua_pushnil(L);
    while(lua_next(L,-2)) {
        lua_pop(L,1); /* remove value, keep key for next iteration. */
        len++;
    }

    /* Step two: actually encoding of the map. */
    mp_encode_map(buf,len);
    lua_pushnil(L);
    while(lua_next(L,-2)) {
        /* Stack: ... key value */
        lua_pushvalue(L,-2); /* Stack: ... key value key */
        mp_encode_lua_type(L,buf,level+1); /* encode key */
        mp_encode_lua_type(L,buf,level+1); /* encode val */
    }
}

/* Returns true if the Lua table on top of the stack is exclusively composed
 * of keys from numerical keys from 1 up to N, with N being the total number
 * of elements, without any hole in the middle. */
static int table_is_an_array(lua_State *L) {
M
Matt Stancliff 已提交
422 423
    int count = 0, max = 0;
#if LUA_VERSION_NUM < 503
A
antirez 已提交
424
    lua_Number n;
M
Matt Stancliff 已提交
425 426 427 428 429 430 431 432
#else
    lua_Integer n;
#endif

    /* Stack top on function entry */
    int stacktop;

    stacktop = lua_gettop(L);
A
antirez 已提交
433 434 435 436 437

    lua_pushnil(L);
    while(lua_next(L,-2)) {
        /* Stack: ... key value */
        lua_pop(L,1); /* Stack: ... key */
M
Matt Stancliff 已提交
438 439
        /* The <= 0 check is valid here because we're comparing indexes. */
#if LUA_VERSION_NUM < 503
440 441
        if ((LUA_TNUMBER != lua_type(L,-1)) || (n = lua_tonumber(L, -1)) <= 0 ||
            !IS_INT_EQUIVALENT(n))
M
Matt Stancliff 已提交
442
#else
443
        if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0)
M
Matt Stancliff 已提交
444
#endif
445
        {
M
Matt Stancliff 已提交
446 447 448 449
            lua_settop(L, stacktop);
            return 0;
        }
        max = (n > max ? n : max);
A
antirez 已提交
450 451 452
        count++;
    }
    /* We have the total number of elements in "count". Also we have
M
Matt Stancliff 已提交
453
     * the max index encountered in "max". We can't reach this code
A
antirez 已提交
454
     * if there are indexes <= 0. If you also note that there can not be
M
Matt Stancliff 已提交
455
     * repeated keys into a table, you have that if max==count you are sure
A
antirez 已提交
456
     * that there are all the keys form 1 to count (both included). */
M
Matt Stancliff 已提交
457 458
    lua_settop(L, stacktop);
    return max == count;
A
antirez 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472
}

/* If the length operator returns non-zero, that is, there is at least
 * an object at key '1', we serialize to message pack list. Otherwise
 * we use a map. */
static void mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {
    if (table_is_an_array(L))
        mp_encode_lua_table_as_array(L,buf,level);
    else
        mp_encode_lua_table_as_map(L,buf,level);
}

static void mp_encode_lua_null(lua_State *L, mp_buf *buf) {
    unsigned char b[1];
M
Matt Stancliff 已提交
473
    (void)L;
A
antirez 已提交
474 475 476 477 478 479 480 481

    b[0] = 0xc0;
    mp_buf_append(buf,b,1);
}

static void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {
    int t = lua_type(L,-1);

482
    /* Limit the encoding of nested tables to a specified maximum depth, so that
A
antirez 已提交
483 484 485 486 487
     * we survive when called against circular references in tables. */
    if (t == LUA_TTABLE && level == LUACMSGPACK_MAX_NESTING) t = LUA_TNIL;
    switch(t) {
    case LUA_TSTRING: mp_encode_lua_string(L,buf); break;
    case LUA_TBOOLEAN: mp_encode_lua_bool(L,buf); break;
M
Matt Stancliff 已提交
488 489 490 491 492 493 494 495 496 497 498
    case LUA_TNUMBER:
    #if LUA_VERSION_NUM < 503
        mp_encode_lua_number(L,buf); break;
    #else
        if (lua_isinteger(L, -1)) {
            mp_encode_lua_integer(L, buf);
        } else {
            mp_encode_lua_number(L, buf);
        }
        break;
    #endif
A
antirez 已提交
499 500 501 502 503 504
    case LUA_TTABLE: mp_encode_lua_table(L,buf,level); break;
    default: mp_encode_lua_null(L,buf); break;
    }
    lua_pop(L,1);
}

M
Matt Stancliff 已提交
505 506 507 508
/*
 * Packs all arguments as a stream for multiple upacking later.
 * Returns error if no arguments provided.
 */
A
antirez 已提交
509
static int mp_pack(lua_State *L) {
M
Matt Stancliff 已提交
510 511 512 513 514 515 516 517 518 519 520 521
    int nargs = lua_gettop(L);
    int i;
    mp_buf *buf;

    if (nargs == 0)
        return luaL_argerror(L, 0, "MessagePack pack needs input.");

    buf = mp_buf_new(L);
    for(i = 1; i <= nargs; i++) {
        /* Copy argument i to top of stack for _encode processing;
         * the encode function pops it from the stack when complete. */
        lua_pushvalue(L, i);
A
antirez 已提交
522

M
Matt Stancliff 已提交
523
        mp_encode_lua_type(L,buf,0);
A
antirez 已提交
524

M
Matt Stancliff 已提交
525 526 527 528 529 530 531 532
        lua_pushlstring(L,(char*)buf->b,buf->len);

        /* Reuse the buffer for the next operation by
         * setting its free count to the total buffer size
         * and the current position to zero. */
        buf->free += buf->len;
        buf->len = 0;
    }
A
antirez 已提交
533
    mp_buf_free(buf);
M
Matt Stancliff 已提交
534 535 536

    /* Concatenate all nargs buffers together */
    lua_concat(L, nargs);
A
antirez 已提交
537 538 539
    return 1;
}

M
Matt Stancliff 已提交
540
/* ------------------------------- Decoding --------------------------------- */
A
antirez 已提交
541 542 543 544

void mp_decode_to_lua_type(lua_State *L, mp_cur *c);

void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {
545
    assert(len <= UINT_MAX);
A
antirez 已提交
546 547 548 549 550 551 552 553 554 555 556 557
    int index = 1;

    lua_newtable(L);
    while(len--) {
        lua_pushnumber(L,index++);
        mp_decode_to_lua_type(L,c);
        if (c->err) return;
        lua_settable(L,-3);
    }
}

void mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {
558
    assert(len <= UINT_MAX);
A
antirez 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572
    lua_newtable(L);
    while(len--) {
        mp_decode_to_lua_type(L,c); /* key */
        if (c->err) return;
        mp_decode_to_lua_type(L,c); /* value */
        if (c->err) return;
        lua_settable(L,-3);
    }
}

/* Decode a Message Pack raw object pointed by the string cursor 'c' to
 * a Lua type, that is left as the only result on the stack. */
void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
    mp_cur_need(c,1);
M
Matt Stancliff 已提交
573 574 575 576 577 578 579 580 581 582

    /* If we return more than 18 elements, we must resize the stack to
     * fit all our return values.  But, there is no way to
     * determine how many objects a msgpack will unpack to up front, so
     * we request a +1 larger stack on each iteration (noop if stack is
     * big enough, and when stack does require resize it doubles in size) */
    luaL_checkstack(L, 1,
        "too many return values at once; "
        "use unpack_one or unpack_limit instead.");

A
antirez 已提交
583 584 585
    switch(c->p[0]) {
    case 0xcc:  /* uint 8 */
        mp_cur_need(c,2);
M
Matt Stancliff 已提交
586
        lua_pushunsigned(L,c->p[1]);
A
antirez 已提交
587 588 589 590
        mp_cur_consume(c,2);
        break;
    case 0xd0:  /* int 8 */
        mp_cur_need(c,2);
591
        lua_pushinteger(L,(signed char)c->p[1]);
A
antirez 已提交
592 593 594 595
        mp_cur_consume(c,2);
        break;
    case 0xcd:  /* uint 16 */
        mp_cur_need(c,3);
M
Matt Stancliff 已提交
596
        lua_pushunsigned(L,
A
antirez 已提交
597 598 599 600 601 602
            (c->p[1] << 8) |
             c->p[2]);
        mp_cur_consume(c,3);
        break;
    case 0xd1:  /* int 16 */
        mp_cur_need(c,3);
M
Matt Stancliff 已提交
603
        lua_pushinteger(L,(int16_t)
A
antirez 已提交
604 605 606 607 608 609
            (c->p[1] << 8) |
             c->p[2]);
        mp_cur_consume(c,3);
        break;
    case 0xce:  /* uint 32 */
        mp_cur_need(c,5);
M
Matt Stancliff 已提交
610
        lua_pushunsigned(L,
A
antirez 已提交
611 612 613 614 615 616 617 618
            ((uint32_t)c->p[1] << 24) |
            ((uint32_t)c->p[2] << 16) |
            ((uint32_t)c->p[3] << 8) |
             (uint32_t)c->p[4]);
        mp_cur_consume(c,5);
        break;
    case 0xd2:  /* int 32 */
        mp_cur_need(c,5);
M
Matt Stancliff 已提交
619
        lua_pushinteger(L,
A
antirez 已提交
620 621 622 623 624 625 626 627
            ((int32_t)c->p[1] << 24) |
            ((int32_t)c->p[2] << 16) |
            ((int32_t)c->p[3] << 8) |
             (int32_t)c->p[4]);
        mp_cur_consume(c,5);
        break;
    case 0xcf:  /* uint 64 */
        mp_cur_need(c,9);
M
Matt Stancliff 已提交
628
        lua_pushunsigned(L,
A
antirez 已提交
629 630 631 632 633 634 635 636 637 638 639 640
            ((uint64_t)c->p[1] << 56) |
            ((uint64_t)c->p[2] << 48) |
            ((uint64_t)c->p[3] << 40) |
            ((uint64_t)c->p[4] << 32) |
            ((uint64_t)c->p[5] << 24) |
            ((uint64_t)c->p[6] << 16) |
            ((uint64_t)c->p[7] << 8) |
             (uint64_t)c->p[8]);
        mp_cur_consume(c,9);
        break;
    case 0xd3:  /* int 64 */
        mp_cur_need(c,9);
641
#if LUA_VERSION_NUM < 503
A
antirez 已提交
642
        lua_pushnumber(L,
643
#else
M
Matt Stancliff 已提交
644
        lua_pushinteger(L,
645
#endif
A
antirez 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
            ((int64_t)c->p[1] << 56) |
            ((int64_t)c->p[2] << 48) |
            ((int64_t)c->p[3] << 40) |
            ((int64_t)c->p[4] << 32) |
            ((int64_t)c->p[5] << 24) |
            ((int64_t)c->p[6] << 16) |
            ((int64_t)c->p[7] << 8) |
             (int64_t)c->p[8]);
        mp_cur_consume(c,9);
        break;
    case 0xc0:  /* nil */
        lua_pushnil(L);
        mp_cur_consume(c,1);
        break;
    case 0xc3:  /* true */
        lua_pushboolean(L,1);
        mp_cur_consume(c,1);
        break;
    case 0xc2:  /* false */
        lua_pushboolean(L,0);
        mp_cur_consume(c,1);
        break;
    case 0xca:  /* float */
        mp_cur_need(c,5);
        assert(sizeof(float) == 4);
        {
            float f;
            memcpy(&f,c->p+1,4);
            memrevifle(&f,4);
            lua_pushnumber(L,f);
            mp_cur_consume(c,5);
        }
        break;
    case 0xcb:  /* double */
        mp_cur_need(c,9);
        assert(sizeof(double) == 8);
        {
            double d;
            memcpy(&d,c->p+1,8);
            memrevifle(&d,8);
            lua_pushnumber(L,d);
            mp_cur_consume(c,9);
        }
        break;
    case 0xda:  /* raw 16 */
        mp_cur_need(c,3);
        {
            size_t l = (c->p[1] << 8) | c->p[2];
            mp_cur_need(c,3+l);
            lua_pushlstring(L,(char*)c->p+3,l);
            mp_cur_consume(c,3+l);
        }
        break;
    case 0xdb:  /* raw 32 */
        mp_cur_need(c,5);
        {
702 703 704 705 706 707 708 709
            size_t l = ((size_t)c->p[1] << 24) |
                       ((size_t)c->p[2] << 16) |
                       ((size_t)c->p[3] << 8) |
                       (size_t)c->p[4];
            mp_cur_consume(c,5);
            mp_cur_need(c,l);
            lua_pushlstring(L,(char*)c->p,l);
            mp_cur_consume(c,l);
A
antirez 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722
        }
        break;
    case 0xdc:  /* array 16 */
        mp_cur_need(c,3);
        {
            size_t l = (c->p[1] << 8) | c->p[2];
            mp_cur_consume(c,3);
            mp_decode_to_lua_array(L,c,l);
        }
        break;
    case 0xdd:  /* array 32 */
        mp_cur_need(c,5);
        {
723 724 725 726
            size_t l = ((size_t)c->p[1] << 24) |
                       ((size_t)c->p[2] << 16) |
                       ((size_t)c->p[3] << 8) |
                       (size_t)c->p[4];
A
antirez 已提交
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
            mp_cur_consume(c,5);
            mp_decode_to_lua_array(L,c,l);
        }
        break;
    case 0xde:  /* map 16 */
        mp_cur_need(c,3);
        {
            size_t l = (c->p[1] << 8) | c->p[2];
            mp_cur_consume(c,3);
            mp_decode_to_lua_hash(L,c,l);
        }
        break;
    case 0xdf:  /* map 32 */
        mp_cur_need(c,5);
        {
742 743 744 745
            size_t l = ((size_t)c->p[1] << 24) |
                       ((size_t)c->p[2] << 16) |
                       ((size_t)c->p[3] << 8) |
                       (size_t)c->p[4];
A
antirez 已提交
746 747 748 749 750 751
            mp_cur_consume(c,5);
            mp_decode_to_lua_hash(L,c,l);
        }
        break;
    default:    /* types that can't be idenitified by first byte value. */
        if ((c->p[0] & 0x80) == 0) {   /* positive fixnum */
M
Matt Stancliff 已提交
752
            lua_pushunsigned(L,c->p[0]);
A
antirez 已提交
753 754
            mp_cur_consume(c,1);
        } else if ((c->p[0] & 0xe0) == 0xe0) {  /* negative fixnum */
M
Matt Stancliff 已提交
755
            lua_pushinteger(L,(signed char)c->p[0]);
A
antirez 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
            mp_cur_consume(c,1);
        } else if ((c->p[0] & 0xe0) == 0xa0) {  /* fix raw */
            size_t l = c->p[0] & 0x1f;
            mp_cur_need(c,1+l);
            lua_pushlstring(L,(char*)c->p+1,l);
            mp_cur_consume(c,1+l);
        } else if ((c->p[0] & 0xf0) == 0x90) {  /* fix map */
            size_t l = c->p[0] & 0xf;
            mp_cur_consume(c,1);
            mp_decode_to_lua_array(L,c,l);
        } else if ((c->p[0] & 0xf0) == 0x80) {  /* fix map */
            size_t l = c->p[0] & 0xf;
            mp_cur_consume(c,1);
            mp_decode_to_lua_hash(L,c,l);
        } else {
            c->err = MP_CUR_ERROR_BADFMT;
        }
    }
}

M
Matt Stancliff 已提交
776
static int mp_unpack_full(lua_State *L, int limit, int offset) {
A
antirez 已提交
777
    size_t len;
M
Matt Stancliff 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791
    const char *s;
    mp_cur c;
    int cnt; /* Number of objects unpacked */
    int decode_all = (!limit && !offset);

    s = luaL_checklstring(L,1,&len); /* if no match, exits */

    if (offset < 0 || limit < 0) /* requesting negative off or lim is invalid */
        return luaL_error(L,
            "Invalid request to unpack with offset of %d and limit of %d.",
            offset, len);
    else if (offset > len)
        return luaL_error(L,
            "Start offset %d greater than input length %d.", offset, len);
A
antirez 已提交
792

M
Matt Stancliff 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806
    if (decode_all) limit = INT_MAX;

    mp_cur_init(&c,(const unsigned char *)s+offset,len-offset);

    /* We loop over the decode because this could be a stream
     * of multiple top-level values serialized together */
    for(cnt = 0; c.left > 0 && cnt < limit; cnt++) {
        mp_decode_to_lua_type(L,&c);

        if (c.err == MP_CUR_ERROR_EOF) {
            return luaL_error(L,"Missing bytes in input.");
        } else if (c.err == MP_CUR_ERROR_BADFMT) {
            return luaL_error(L,"Bad data format in input.");
        }
A
antirez 已提交
807 808
    }

M
Matt Stancliff 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    if (!decode_all) {
        /* c->left is the remaining size of the input buffer.
         * subtract the entire buffer size from the unprocessed size
         * to get our next start offset */
        int offset = len - c.left;
        /* Return offset -1 when we have have processed the entire buffer. */
        lua_pushinteger(L, c.left == 0 ? -1 : offset);
        /* Results are returned with the arg elements still
         * in place. Lua takes care of only returning
         * elements above the args for us.
         * In this case, we have one arg on the stack
         * for this function, so we insert our first return
         * value at position 2. */
        lua_insert(L, 2);
        cnt += 1; /* increase return count by one to make room for offset */
A
antirez 已提交
824
    }
M
Matt Stancliff 已提交
825 826

    return cnt;
A
antirez 已提交
827 828
}

M
Matt Stancliff 已提交
829 830 831 832 833
static int mp_unpack(lua_State *L) {
    return mp_unpack_full(L, 0, 0);
}

static int mp_unpack_one(lua_State *L) {
834
    int offset = luaL_optinteger(L, 2, 0);
M
Matt Stancliff 已提交
835 836 837 838 839 840
    /* Variable pop because offset may not exist */
    lua_pop(L, lua_gettop(L)-1);
    return mp_unpack_full(L, 1, offset);
}

static int mp_unpack_limit(lua_State *L) {
841 842
    int limit = luaL_checkinteger(L, 2);
    int offset = luaL_optinteger(L, 3, 0);
M
Matt Stancliff 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
    /* Variable pop because offset may not exist */
    lua_pop(L, lua_gettop(L)-1);

    return mp_unpack_full(L, limit, offset);
}

static int mp_safe(lua_State *L) {
    int argc, err, total_results;

    argc = lua_gettop(L);

    /* This adds our function to the bottom of the stack
     * (the "call this function" position) */
    lua_pushvalue(L, lua_upvalueindex(1));
    lua_insert(L, 1);

    err = lua_pcall(L, argc, LUA_MULTRET, 0);
    total_results = lua_gettop(L);

    if (!err) {
        return total_results;
    } else {
        lua_pushnil(L);
        lua_insert(L,-2);
        return 2;
    }
}
A
antirez 已提交
870

M
Matt Stancliff 已提交
871 872
/* -------------------------------------------------------------------------- */
static const struct luaL_Reg cmds[] = {
A
antirez 已提交
873 874
    {"pack", mp_pack},
    {"unpack", mp_unpack},
M
Matt Stancliff 已提交
875 876 877
    {"unpack_one", mp_unpack_one},
    {"unpack_limit", mp_unpack_limit},
    {0}
A
antirez 已提交
878 879
};

M
Matt Stancliff 已提交
880 881 882 883 884 885 886 887 888 889
static int luaopen_create(lua_State *L) {
    int i;
    /* Manually construct our module table instead of
     * relying on _register or _newlib */
    lua_newtable(L);

    for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {
        lua_pushcfunction(L, cmds[i].func);
        lua_setfield(L, -2, cmds[i].name);
    }
A
antirez 已提交
890

M
Matt Stancliff 已提交
891 892 893
    /* Add metadata */
    lua_pushliteral(L, LUACMSGPACK_NAME);
    lua_setfield(L, -2, "_NAME");
A
antirez 已提交
894 895 896 897 898
    lua_pushliteral(L, LUACMSGPACK_VERSION);
    lua_setfield(L, -2, "_VERSION");
    lua_pushliteral(L, LUACMSGPACK_COPYRIGHT);
    lua_setfield(L, -2, "_COPYRIGHT");
    lua_pushliteral(L, LUACMSGPACK_DESCRIPTION);
M
Matt Stancliff 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
    lua_setfield(L, -2, "_DESCRIPTION");
    return 1;
}

LUALIB_API int luaopen_cmsgpack(lua_State *L) {
    luaopen_create(L);

#if LUA_VERSION_NUM < 502
    /* Register name globally for 5.1 */
    lua_pushvalue(L, -1);
    lua_setglobal(L, LUACMSGPACK_NAME);
#endif

    return 1;
}

LUALIB_API int luaopen_cmsgpack_safe(lua_State *L) {
    int i;

    luaopen_cmsgpack(L);

    /* Wrap all functions in the safe handler */
    for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {
        lua_getfield(L, -1, cmds[i].name);
        lua_pushcclosure(L, mp_safe, 1);
        lua_setfield(L, -2, cmds[i].name);
    }

#if LUA_VERSION_NUM < 502
    /* Register name globally for 5.1 */
    lua_pushvalue(L, -1);
    lua_setglobal(L, LUACMSGPACK_SAFE_NAME);
#endif

A
antirez 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
    return 1;
}

/******************************************************************************
* Copyright (C) 2012 Salvatore Sanfilippo.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/