From a8717c9d1c98a14182bb387fec834a5608f578bf Mon Sep 17 00:00:00 2001 From: Chanjun <15755396353@163.com> Date: Tue, 25 Apr 2017 14:36:45 +0800 Subject: [PATCH 1/4] fix a small error rt --- lua_cmsgpack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua_cmsgpack.c b/lua_cmsgpack.c index 90a388f..a442013 100644 --- a/lua_cmsgpack.c +++ b/lua_cmsgpack.c @@ -769,7 +769,7 @@ void mp_decode_to_lua_type(lua_State *L, mp_cur *c) { 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 */ + } else if ((c->p[0] & 0xf0) == 0x90) { /* fix array */ size_t l = c->p[0] & 0xf; mp_cur_consume(c,1); mp_decode_to_lua_array(L,c,l); From 61cc7e0d4de1aa01cab859097737c3a330562fb0 Mon Sep 17 00:00:00 2001 From: Chanjun <15755396353@163.com> Date: Thu, 27 Apr 2017 00:19:54 -0700 Subject: [PATCH 2/4] simple test rt --- test_c.lua | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 test_c.lua diff --git a/test_c.lua b/test_c.lua new file mode 100644 index 0000000..084652d --- /dev/null +++ b/test_c.lua @@ -0,0 +1,175 @@ +local cmsgpack = require("cmsgpack") + +local pack, upack = cmsgpack.pack, cmsgpack.unpack + +local pack_errs = pack_errs or 0 +local pack_set = pack_set or {} + +local total_tests = total_tests or 0 + +local test_data = { + ['number'] = { + {127}, + {128}, + {254}, + {255}, + {256}, + {65535}, + {4294967295}, + {4294967296}, + {9007199254740991}, + {9007199254740990}, + {0.0}, + {0.1}, + {0.010}, + {12.123}, + {12.00}, + {1.0 / 0.0}, + {-1.0 / 0.0}, + {0.0 / 0.0}, + }, + ['boolean'] = { + {true}, + {false}, + }, + ['nil'] = { + {nil}, + }, + ['table'] = { + {{}}, + {{1, 2, 3, 4, 'x', 'y','z'}}, + {{x = 1, y = 2, z = 3, {4, 5, 6}}}, + {{1, 2, 3, nil}}, + {{1, 2, nil, 3}}, + {{500026, 155, {__entity_id = 500026 , __gate_id = 2, __server_id = 5, __type = "Player"}}}, + {{500026, 93, {autoMatchGoal = 0, autoMatchPlayerNum = 0, autoMatchTeamNum = 0, teams = {}}}}, + }, + ['string'] = { + {""}, + {" "}, + {"1234567891234567891234567891234"}, + {"checkInteractionConditionWithQuestId"}, + } +} + +function __dump_table(t, level, leadSpace) + level = level or 3 + leadSpace = leadSpace or 1 + + local rlt = "" + for idx = 1, leadSpace do + rlt = rlt .. " " + end + + local blacnSpace = rlt + + local len = #t + for k, v in pairs(t) do + if type(k) == "number" then + rlt = rlt .. "[" .. k .. "]" + elseif type(k) == "string" then + rlt = rlt .. '["' .. k .. '"]' + end + + if type(v) == "table" then + if level > 0 then + rlt = rlt .. " = {\n" .. __dump_table(v, level - 1, leadSpace + 4) .. "\n" .. blacnSpace .. "}, " + else + rlt = rlt .. " = {" .. __dump_table(v, level - 1, 0) .. "}, " + end + elseif type(v) == "string" then + rlt = rlt .. ' = "' .. v .. '", ' + else + rlt = rlt .. ' = ' .. tostring(v) .. ', ' + end + + if level >= 0 then + rlt = rlt .. '\n' .. blacnSpace + end + + end + + return rlt +end + +function print_hex(buffer, num) + local str = '' + local cnt = 0 + local len = num or string.len(buffer) + + for idx = 1, len do + str = str .. string.format("%02X",buffer:sub(idx, idx):byte()) .. ' ' + cnt = cnt + 1 + end + print(str) +end + + +function dump(v) + if type(v) ~= 'table' then + return tostring(v) + end + + return __dump_table(v) +end + +function is_equal(dst, src) + + if type(dst) ~= type(src) then + return false + end + + if type(dst) ~= 'table' then + return dst == src + end + + if #dst ~= #src then + return false + end + + for k, v in pairs(dst) do + if type(v) ~= 'table' then + if src[k] ~= v then + return false + end + else + return is_equal(v, src[k]) + end + end + + return true +end + + +function test(v) + --print(upack(pack(v))) + print('pack ---------------------------- :\n', dump(v)) + local rlt = pack(v) + print_hex(rlt) + + print('unpack -------------------------- :\n') + local urlt = upack(rlt) + print(dump(urlt)) + + if not is_equal(urlt, v) or not is_equal(v, urlt) then + pack_errs = pack_errs + 1 + pack_set[#pack_set + 1] = {rlt, v} + end +end + +for k, v in pairs(test_data) do + print('Testing-------------------------->', k) + local td = test_data[k] + for _, value in pairs(td) do + test(value[1]) + total_tests = total_tests + 1 + end +end + +print('Total tests: ', total_tests) +print('Error tests: ', pack_errs) +print('Passed tests: ', total_tests - pack_errs) + +for k, v in pairs(pack_set) do + print('dst:\n', dump(pack_set[1]), '\nsrc:\n', dump(pack_set[2])) +end From 1403d6992d5b9f3132515672950af5bc4fece95e Mon Sep 17 00:00:00 2001 From: Ubuntu123 Date: Thu, 27 Apr 2017 22:54:24 -0700 Subject: [PATCH 3/4] add an unpack error type:extra types --- lua_cmsgpack.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua_cmsgpack.c b/lua_cmsgpack.c index a442013..772498e 100644 --- a/lua_cmsgpack.c +++ b/lua_cmsgpack.c @@ -144,6 +144,7 @@ void mp_buf_free(lua_State *L, mp_buf *buf) { #define MP_CUR_ERROR_NONE 0 #define MP_CUR_ERROR_EOF 1 /* Not enough data to complete operation. */ #define MP_CUR_ERROR_BADFMT 2 /* Bad data format */ +#define MP_CUR_ERROR_EXTRA 3 /* Extra bytes */ typedef struct mp_cur { const unsigned char *p; @@ -769,7 +770,7 @@ void mp_decode_to_lua_type(lua_State *L, mp_cur *c) { 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 array */ + } 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); @@ -813,6 +814,8 @@ int mp_unpack_full(lua_State *L, int limit, int offset) { 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."); + } else if (c.err == MP_CUR_ERROR_EXTRA || c.left > 0) { + return luaL_error(L,"Extra Bytes."); } } From ea5f4f5190ee4b0a882fba78aa31dd259e600ea8 Mon Sep 17 00:00:00 2001 From: Chanjun <15755396353@163.com> Date: Thu, 27 Apr 2017 22:55:49 -0700 Subject: [PATCH 4/4] tmp delete rt --- test_c.lua | 175 ----------------------------------------------------- 1 file changed, 175 deletions(-) delete mode 100644 test_c.lua diff --git a/test_c.lua b/test_c.lua deleted file mode 100644 index 084652d..0000000 --- a/test_c.lua +++ /dev/null @@ -1,175 +0,0 @@ -local cmsgpack = require("cmsgpack") - -local pack, upack = cmsgpack.pack, cmsgpack.unpack - -local pack_errs = pack_errs or 0 -local pack_set = pack_set or {} - -local total_tests = total_tests or 0 - -local test_data = { - ['number'] = { - {127}, - {128}, - {254}, - {255}, - {256}, - {65535}, - {4294967295}, - {4294967296}, - {9007199254740991}, - {9007199254740990}, - {0.0}, - {0.1}, - {0.010}, - {12.123}, - {12.00}, - {1.0 / 0.0}, - {-1.0 / 0.0}, - {0.0 / 0.0}, - }, - ['boolean'] = { - {true}, - {false}, - }, - ['nil'] = { - {nil}, - }, - ['table'] = { - {{}}, - {{1, 2, 3, 4, 'x', 'y','z'}}, - {{x = 1, y = 2, z = 3, {4, 5, 6}}}, - {{1, 2, 3, nil}}, - {{1, 2, nil, 3}}, - {{500026, 155, {__entity_id = 500026 , __gate_id = 2, __server_id = 5, __type = "Player"}}}, - {{500026, 93, {autoMatchGoal = 0, autoMatchPlayerNum = 0, autoMatchTeamNum = 0, teams = {}}}}, - }, - ['string'] = { - {""}, - {" "}, - {"1234567891234567891234567891234"}, - {"checkInteractionConditionWithQuestId"}, - } -} - -function __dump_table(t, level, leadSpace) - level = level or 3 - leadSpace = leadSpace or 1 - - local rlt = "" - for idx = 1, leadSpace do - rlt = rlt .. " " - end - - local blacnSpace = rlt - - local len = #t - for k, v in pairs(t) do - if type(k) == "number" then - rlt = rlt .. "[" .. k .. "]" - elseif type(k) == "string" then - rlt = rlt .. '["' .. k .. '"]' - end - - if type(v) == "table" then - if level > 0 then - rlt = rlt .. " = {\n" .. __dump_table(v, level - 1, leadSpace + 4) .. "\n" .. blacnSpace .. "}, " - else - rlt = rlt .. " = {" .. __dump_table(v, level - 1, 0) .. "}, " - end - elseif type(v) == "string" then - rlt = rlt .. ' = "' .. v .. '", ' - else - rlt = rlt .. ' = ' .. tostring(v) .. ', ' - end - - if level >= 0 then - rlt = rlt .. '\n' .. blacnSpace - end - - end - - return rlt -end - -function print_hex(buffer, num) - local str = '' - local cnt = 0 - local len = num or string.len(buffer) - - for idx = 1, len do - str = str .. string.format("%02X",buffer:sub(idx, idx):byte()) .. ' ' - cnt = cnt + 1 - end - print(str) -end - - -function dump(v) - if type(v) ~= 'table' then - return tostring(v) - end - - return __dump_table(v) -end - -function is_equal(dst, src) - - if type(dst) ~= type(src) then - return false - end - - if type(dst) ~= 'table' then - return dst == src - end - - if #dst ~= #src then - return false - end - - for k, v in pairs(dst) do - if type(v) ~= 'table' then - if src[k] ~= v then - return false - end - else - return is_equal(v, src[k]) - end - end - - return true -end - - -function test(v) - --print(upack(pack(v))) - print('pack ---------------------------- :\n', dump(v)) - local rlt = pack(v) - print_hex(rlt) - - print('unpack -------------------------- :\n') - local urlt = upack(rlt) - print(dump(urlt)) - - if not is_equal(urlt, v) or not is_equal(v, urlt) then - pack_errs = pack_errs + 1 - pack_set[#pack_set + 1] = {rlt, v} - end -end - -for k, v in pairs(test_data) do - print('Testing-------------------------->', k) - local td = test_data[k] - for _, value in pairs(td) do - test(value[1]) - total_tests = total_tests + 1 - end -end - -print('Total tests: ', total_tests) -print('Error tests: ', pack_errs) -print('Passed tests: ', total_tests - pack_errs) - -for k, v in pairs(pack_set) do - print('dst:\n', dump(pack_set[1]), '\nsrc:\n', dump(pack_set[2])) -end