int memcached_tuple_set(struct memcached_connection *con, const char *kpos, uint32_t klen, uint64_t expire, const char *vpos, uint32_t vlen, uint64_t cas, uint32_t flags) { (void )con; uint64_t time = fiber_time64(); uint32_t len = mp_sizeof_array(6) + mp_sizeof_str (klen) + mp_sizeof_uint (expire) + mp_sizeof_uint (time) + mp_sizeof_str (vlen) + mp_sizeof_uint (cas) + mp_sizeof_uint (flags); char *begin = (char *)box_txn_alloc(len); if (begin == NULL) { memcached_error_ENOMEM(len, "tuple"); return -1; } char *end = mp_encode_array(begin, 6); end = mp_encode_str (end, kpos, klen); end = mp_encode_uint (end, expire); end = mp_encode_uint (end, time); end = mp_encode_str (end, vpos, vlen); end = mp_encode_uint (end, cas); end = mp_encode_uint (end, flags); assert(end <= begin + len); return box_replace(con->cfg->space_id, begin, end, NULL); }
int args(box_function_ctx_t *ctx, const char *args, const char *args_end) { uint32_t arg_count = mp_decode_array(&args); if (arg_count < 1) { return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", "invalid argument count"); } if (mp_typeof(*args) != MP_UINT) { return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", "first tuple field must be uint"); } uint32_t num = mp_decode_uint(&args); char tuple_buf[512]; char *d = tuple_buf; d = mp_encode_array(d, 2); d = mp_encode_uint(d, num); d = mp_encode_str(d, "hello", strlen("hello")); assert(d <= tuple_buf + sizeof(tuple_buf)); box_tuple_format_t *fmt = box_tuple_format_default(); box_tuple_t *tuple = box_tuple_new(fmt, tuple_buf, d); if (tuple == NULL) return -1; return box_return_tuple(ctx, tuple); }
ssize_t tnt_auth(struct tnt_stream *s, const char *user, int ulen, const char *pass, int plen) { struct tnt_iheader hdr; struct iovec v[6]; int v_sz = 5; char *data = NULL, *body_start = NULL; int guest = !user || (ulen == 5 && !strncmp(user, "guest", 5)); if (guest) { user = "******"; ulen = 5; } encode_header(&hdr, TNT_OP_AUTH, s->reqid++); v[1].iov_base = (void *)hdr.header; v[1].iov_len = hdr.end - hdr.header; char body[64]; data = body; body_start = data; data = mp_encode_map(data, 2); data = mp_encode_uint(data, TNT_USERNAME); data = mp_encode_strl(data, ulen); v[2].iov_base = body_start; v[2].iov_len = data - body_start; v[3].iov_base = (void *)user; v[3].iov_len = ulen; body_start = data; data = mp_encode_uint(data, TNT_TUPLE); if (!guest) { data = mp_encode_array(data, 2); data = mp_encode_str(data, "chap-sha1", strlen("chap-sha1")); data = mp_encode_strl(data, TNT_SCRAMBLE_SIZE); char salt[64], scramble[TNT_SCRAMBLE_SIZE]; base64_decode(TNT_SNET_CAST(s)->greeting + TNT_VERSION_SIZE, TNT_SALT_SIZE, salt, 64); tnt_scramble_prepare(scramble, salt, pass, plen); v[5].iov_base = scramble; v[5].iov_len = TNT_SCRAMBLE_SIZE; v_sz++; } else { data = mp_encode_array(data, 0); } v[4].iov_base = body_start; v[4].iov_len = data - body_start; size_t package_len = 0; for (int i = 1; i < v_sz; ++i) { package_len += v[i].iov_len; } char len_prefix[9]; char *len_end = mp_encode_luint32(len_prefix, package_len); v[0].iov_base = len_prefix; v[0].iov_len = len_end - len_prefix; return s->writev(s, v, v_sz); }
static ssize_t tnt_update_op(struct tnt_stream *ops, char op, uint32_t fieldno, const char *opdata, size_t opdata_len) { struct iovec v[2]; size_t v_sz = 2; char body[64], *data; data = body; data = mp_encode_array(data, tnt_update_op_len(op)); data = mp_encode_str(data, &op, 1); data = mp_encode_uint(data, fieldno); v[0].iov_base = body; v[0].iov_len = data - body; v[1].iov_base = (void *)opdata; v[1].iov_len = opdata_len; return ops->writev(ops, v, v_sz); }
ssize_t tnt_update_splice(struct tnt_stream *ops, uint32_t fieldno, uint32_t position, uint32_t offset, const char *buffer, size_t buffer_len) { size_t buf_size = mp_sizeof_uint(position) + mp_sizeof_uint(offset) + mp_sizeof_str(buffer_len); char *buf = tnt_mem_alloc(buf_size), *data = NULL; if (!buf) return -1; data = buf; data = mp_encode_uint(data, position); data = mp_encode_uint(data, offset); data = mp_encode_str(data, buffer, buffer_len); ssize_t retval = tnt_update_op(ops, ':', fieldno, buf, buf_size); tnt_mem_free(buf); return retval; }
static int test_overflow() { plan(4); header(); const char *chk; char *d; d = data; chk = data; d = mp_encode_array(d, 1); d = mp_encode_array(d, UINT32_MAX); is(mp_check(&chk, d), 1, "mp_check array overflow") d = data; chk = data; d = mp_encode_array(d, 1); d = mp_encode_map(d, UINT32_MAX); is(mp_check(&chk, d), 1, "mp_check map overflow") d = data; chk = data; d = mp_encode_array(d, 2); d = mp_encode_str(d, "", 0); d = mp_encode_strl(d, UINT32_MAX); is(mp_check(&chk, d), 1, "mp_check str overflow") d = data; chk = data; d = mp_encode_array(d, 2); d = mp_encode_bin(d, "", 0); d = mp_encode_binl(d, UINT32_MAX); is(mp_check(&chk, d), 1, "mp_check bin overflow") footer(); return check_plan(); }
int memcached_tuple_get(struct memcached_connection *con, const char *key, uint32_t key_len, box_tuple_t **tuple) { /* Create key for getting previous tuple from space */ uint32_t len = mp_sizeof_array(1) + mp_sizeof_str (key_len); char *begin = (char *)box_txn_alloc(len); if (begin == NULL) { memcached_error_ENOMEM(len, "key"); return -1; } char *end = NULL; end = mp_encode_array(begin, 1); end = mp_encode_str (end, key, key_len); assert(end <= begin + len); /* Get tuple from space */ if (box_index_get(con->cfg->space_id, 0, begin, end, tuple) == -1) { return -1; } return 0; }
int test_mp_print() { plan(10); header(); char msgpack[128]; char *d = msgpack; d = mp_encode_array(d, 6); d = mp_encode_int(d, -5); d = mp_encode_uint(d, 42); d = mp_encode_str(d, "kill bill", 9); d = mp_encode_map(d, 6); d = mp_encode_str(d, "bool true", 9); d = mp_encode_bool(d, true); d = mp_encode_str(d, "bool false", 10); d = mp_encode_bool(d, false); d = mp_encode_str(d, "null", 4); d = mp_encode_nil(d); d = mp_encode_str(d, "float", 5); d = mp_encode_float(d, 3.14); d = mp_encode_str(d, "double", 6); d = mp_encode_double(d, 3.14); d = mp_encode_uint(d, 100); d = mp_encode_uint(d, 500); *d++ = 0xd4; /* let's pack smallest fixed ext */ *d++ = 0; *d++ = 0; char bin[] = "\x12test\x34\b\t\n\"bla\\-bla\"\f\r"; d = mp_encode_bin(d, bin, sizeof(bin)); assert(d <= msgpack + sizeof(msgpack)); const char *expected = "[-5, 42, \"kill bill\", " "{\"bool true\": true, \"bool false\": false, \"null\": null, " "\"float\": 3.14, \"double\": 3.14, 100: 500}, undefined, " "\"\\u0012test4\\b\\t\\n\\\"bla\\\\-bla\\\"\\f\\r\\u0000\"]"; int esize = strlen(expected); char result[256]; int fsize = mp_snprint(result, sizeof(result), msgpack); ok(fsize == esize, "mp_snprint return value"); ok(strcmp(result, expected) == 0, "mp_snprint result"); fsize = mp_snprint(NULL, 0, msgpack); ok(fsize == esize, "mp_snprint limit = 0"); fsize = mp_snprint(result, 1, msgpack); ok(fsize == esize && result[0] == '\0', "mp_snprint limit = 1"); fsize = mp_snprint(result, 2, msgpack); ok(fsize == esize && result[1] == '\0', "mp_snprint limit = 2"); fsize = mp_snprint(result, esize, msgpack); ok(fsize == esize && result[esize - 1] == '\0', "mp_snprint limit = expected"); fsize = mp_snprint(result, esize + 1, msgpack); ok(fsize == esize && result[esize] == '\0', "mp_snprint limit = expected + 1"); FILE *tmpf = tmpfile(); if (tmpf != NULL) { int fsize = mp_fprint(tmpf, msgpack); ok(fsize == esize, "mp_fprint return value"); (void) rewind(tmpf); int rsize = fread(result, 1, sizeof(result), tmpf); ok(rsize == esize && memcmp(result, expected, esize) == 0, "mp_fprint result"); fclose(tmpf); } /* stdin is read-only */ int rc = mp_fprint(stdin, msgpack); is(rc, -1, "mp_fprint I/O error"); footer(); return check_plan(); }
static int test_format(void) { plan(282); header(); const size_t buf_size = 1024; char buf[buf_size]; size_t sz; const char *fmt; const char *p, *c, *e; uint32_t len = 0; fmt = "%d %u %i %ld %lu %li %lld %llu %lli" "%hd %hu %hi %hhd %hhu %hhi"; sz = mp_format(buf, buf_size, fmt, 1, 2, 3, (long)4, (long)5, (long)6, (long long)7, (long long)8, (long long)9, (short)10, (short)11, (short)12, (char)13, (char)14, (char)15); p = buf; for (unsigned i = 0; i < 15; i++) { ok(mp_typeof(*p) == MP_UINT, "Test type on step %d", i); ok(mp_decode_uint(&p) == i + 1, "Test value on step %d", i); } sz = mp_format(buf, buf_size, fmt, -1, -2, -3, (long)-4, (long)-5, (long)-6, (long long)-7, (long long)-8, (long long)-9, (short)-10, (unsigned short)-11, (short)-12, (signed char)-13, (unsigned char)-14, (signed char)-15); p = buf; for (int i = 0; i < 15; i++) { uint64_t expects[5] = { UINT_MAX - 1, ULONG_MAX - 4, ULLONG_MAX - 7, USHRT_MAX - 10, UCHAR_MAX - 13 }; if (i % 3 == 1) { ok(mp_typeof(*p) == MP_UINT, "Test type on step %d", i); ok(mp_decode_uint(&p) == expects[i / 3], "Test value on step %d", i); } else { ok(mp_typeof(*p) == MP_INT, "Test type on step %d", i); ok(mp_decode_int(&p) == - i - 1, "Test value on step %d", i); } } char data1[32]; char *data1_end = data1; data1_end = mp_encode_array(data1_end, 2); data1_end = mp_encode_str(data1_end, "ABC", 3); data1_end = mp_encode_uint(data1_end, 11); size_t data1_len = data1_end - data1; assert(data1_len <= sizeof(data1)); char data2[32]; char *data2_end = data2; data2_end = mp_encode_int(data2_end, -1234567890); data2_end = mp_encode_str(data2_end, "DEFGHIJKLMN", 11); data2_end = mp_encode_uint(data2_end, 321); size_t data2_len = data2_end - data2; assert(data2_len <= sizeof(data2)); fmt = "%d NIL [%d %b %b] this is test" "[%d %%%% [[ %d {%s %f %% %.*s %lf %.*s NIL}" "%p %d %.*p ]] %d%d%d]"; #define TEST_PARAMS 0, 1, true, false, -1, 2, \ "flt", 0.1, 6, "double#ignored", 0.2, 0, "ignore", \ data1, 3, data2_len, data2, 4, 5, 6 sz = mp_format(buf, buf_size, fmt, TEST_PARAMS); p = buf; e = buf + sz; c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_UINT, "type"); ok(mp_decode_uint(&p) == 0, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_NIL, "type"); mp_decode_nil(&p); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_ARRAY, "type"); ok(mp_decode_array(&p) == 3, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_UINT, "type"); ok(mp_decode_uint(&p) == 1, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_BOOL, "type"); ok(mp_decode_bool(&p) == true, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_BOOL, "type"); ok(mp_decode_bool(&p) == false, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_ARRAY, "type"); ok(mp_decode_array(&p) == 5, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_INT, "type"); ok(mp_decode_int(&p) == -1, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_ARRAY, "type"); ok(mp_decode_array(&p) == 1, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_ARRAY, "type"); ok(mp_decode_array(&p) == 5, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_UINT, "type"); ok(mp_decode_uint(&p) == 2, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_MAP, "type"); ok(mp_decode_map(&p) == 3, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_STR, "type"); c = mp_decode_str(&p, &len); ok(len == 3, "decode"); ok(memcmp(c, "flt", 3) == 0, "compare"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_FLOAT, "type"); ok(fequal(mp_decode_float(&p), 0.1), "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_STR, "type"); c = mp_decode_str(&p, &len); ok(len == 6, "decode"); ok(memcmp(c, "double", 6) == 0, "compare"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_DOUBLE, "type"); ok(dequal(mp_decode_double(&p), 0.2), "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_STR, "type"); c = mp_decode_str(&p, &len); ok(len == 0, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_NIL, "type"); mp_decode_nil(&p); c = p; ok(mp_check(&c, e) == 0, "check"); ok(((size_t)(c - p) == data1_len) && memcmp(p, data1, data1_len) == 0, "compare"); p = c; c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_UINT, "type"); ok(mp_decode_uint(&p) == 3, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_INT, "type"); ok(mp_decode_int(&p) == -1234567890, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_STR, "type"); c = mp_decode_str(&p, &len); ok(len == 11, "decode"); ok(memcmp(c, "DEFGHIJKLMN", 11) == 0, "compare"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_UINT, "type"); ok(mp_decode_uint(&p) == 321, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_UINT, "type"); ok(mp_decode_uint(&p) == 4, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_UINT, "type"); ok(mp_decode_uint(&p) == 5, "decode"); c = p; ok(mp_check(&c, e) == 0, "check"); ok(mp_typeof(*p) == MP_UINT, "type"); ok(mp_decode_uint(&p) == 6, "decode"); ok(p == e, "nothing more"); ok(sz < 70, "no magic detected"); for (size_t lim = 0; lim <= 70; lim++) { memset(buf, 0, buf_size); size_t test_sz = mp_format(buf, lim, fmt, TEST_PARAMS); ok(test_sz == sz, "return value on step %d", (int)lim); bool all_zero = true; for(size_t z = lim; z < buf_size; z++) all_zero = all_zero && (buf[z] == 0); ok(all_zero, "buffer overflow on step %d", (int)lim); } #undef TEST_PARAMS footer(); return check_plan(); }
void _mpack_item(SV *res, SV *o) { size_t len, res_len, new_len; char *s, *res_s; res_s = SvPVbyte(res, res_len); unsigned i; if (!SvOK(o)) { new_len = res_len + mp_sizeof_nil(); res_s = SvGROW(res, new_len); SvCUR_set(res, new_len); mp_encode_nil(res_s + res_len); return; } if (SvROK(o)) { o = SvRV(o); if (SvOBJECT(o)) { SvGETMAGIC(o); HV *stash = SvSTASH(o); GV *mtd = gv_fetchmethod_autoload(stash, "msgpack", 0); if (!mtd) croak("Object has no method 'msgpack'"); dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs (sv_bless (sv_2mortal (newRV_inc(o)), stash)); PUTBACK; call_sv((SV *)GvCV(mtd), G_SCALAR); SPAGAIN; SV *pkt = POPs; if (!SvOK(pkt)) croak("O->msgpack returned undef"); s = SvPV(pkt, len); new_len = res_len + len; res_s = SvGROW(res, new_len); SvCUR_set(res, new_len); memcpy(res_s + res_len, s, len); PUTBACK; FREETMPS; LEAVE; return; } switch(SvTYPE(o)) { case SVt_PVAV: { AV *a = (AV *)o; len = av_len(a) + 1; new_len = res_len + mp_sizeof_array(len); res_s = SvGROW(res, new_len); SvCUR_set(res, new_len); mp_encode_array(res_s + res_len, len); for (i = 0; i < len; i++) { SV **item = av_fetch(a, i, 0); if (!item) _mpack_item(res, 0); else _mpack_item(res, *item); } break; } case SVt_PVHV: { HV *h = (HV *)o; len = hv_iterinit(h); new_len = res_len + mp_sizeof_map(len); res_s = SvGROW(res, new_len); SvCUR_set(res, new_len); mp_encode_map(res_s + res_len, len); for (;;) { HE * iter = hv_iternext(h); if (!iter) break; SV *k = hv_iterkeysv(iter); SV *v = HeVAL(iter); _mpack_item(res, k); _mpack_item(res, v); } break; } default: croak("Can't serialize reference"); } return; } switch(SvTYPE(o)) { case SVt_PV: case SVt_PVIV: case SVt_PVNV: case SVt_PVMG: case SVt_REGEXP: if (!looks_like_number(o)) { s = SvPV(o, len); new_len = res_len + mp_sizeof_str(len); res_s = SvGROW(res, new_len); SvCUR_set(res, new_len); mp_encode_str(res_s + res_len, s, len); break; } case SVt_NV: { NV v = SvNV(o); IV iv = (IV)v; if (v != iv) { new_len = res_len + mp_sizeof_double(v); res_s = SvGROW(res, new_len); SvCUR_set(res, new_len); mp_encode_double(res_s + res_len, v); break; } } case SVt_IV: { IV v = SvIV(o); if (v >= 0) { new_len = res_len + mp_sizeof_uint(v); res_s = SvGROW(res, new_len); SvCUR_set(res, new_len); mp_encode_uint(res_s + res_len, v); } else { new_len = res_len + mp_sizeof_int(v); res_s = SvGROW(res, new_len); SvCUR_set(res, new_len); mp_encode_int(res_s + res_len, v); } break; } default: croak("Internal msgpack error %d", SvTYPE(o)); } }