Skip to content

Commit

Permalink
优化编码、解码的条件判断
Browse files Browse the repository at this point in the history
  • Loading branch information
CandyMi authored May 30, 2022
1 parent 0746db3 commit 8c6505a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions luaclib/src/lcrypt/hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8c6505a

Please sign in to comment.