From 8c6505a7fb334880e14793ec3b21b57cfc4cb180 Mon Sep 17 00:00:00 2001 From: CandyMi <869646063@qq.com> Date: Mon, 30 May 2022 10:59:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BC=96=E7=A0=81=E3=80=81?= =?UTF-8?q?=E8=A7=A3=E7=A0=81=E7=9A=84=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- luaclib/src/lcrypt/hex.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/luaclib/src/lcrypt/hex.c b/luaclib/src/lcrypt/hex.c index d8e9aba..db4f4df 100644 --- a/luaclib/src/lcrypt/hex.c +++ b/luaclib/src/lcrypt/hex.c @@ -52,15 +52,17 @@ int lfromhex(lua_State *L) { size_t idx = 0; int8_t hi; int8_t lo; while (idx < tsize){ + /* 跳过空格 */ while(isspace((uint8_t)text[idx])) idx++; if (idx >= tsize) break; + if (idx + 1 == tsize) + return luaL_error(L, "Invalid hexdecode ending."); /* 解码计算 */ - hi = text[idx++]; hi = deindex[hi]; - lo = text[idx++]; lo = deindex[lo]; - if (lo == -1 || hi == -1) - return luaL_error(L, "Invalid hexdecode char pos in %d and %d", idx - 2, idx - 1); + hi = deindex[text[idx++]]; lo = deindex[text[idx++]]; + if (hi == -1 || lo == -1) + return luaL_error(L, "Invalid hexdecode char pos between %d and %d", idx - 2, idx - 1); /* 还原数据 */ luaL_addchar(&B, hi << 4 | lo); } @@ -88,11 +90,13 @@ int ltohex(lua_State *L) { luaL_buffinit(L, &B); uint8_t code; - for (size_t i = 0; i < tsize; i++) { - code = text[i]; + size_t i = 0; + while (i < tsize) + { + code = text[i++]; luaL_addchar(&B, etable[code >> 4]); luaL_addchar(&B, etable[code & 0xF]); - if (n == 3) + if (n == 3 && i < tsize) /* 编码结尾不添加空格 */ luaL_addchar(&B, ' '); } luaL_pushresult(&B);