fix: varint decoding

This commit is contained in:
Luca 2024-09-05 21:30:44 +02:00
parent 8e34d4d144
commit 8d283790b6
1 changed files with 14 additions and 8 deletions

View File

@ -10,27 +10,33 @@ local safeRead = function (conn, n)
end end
local readVarint = function (conn, first_byte) local readVarint = function (conn, first_byte)
local b, err local b, data, err
if first_byte == nil then if first_byte == nil then
b, err = safeRead(conn, 1) data, err = safeRead(conn, 1)
if err ~= nil then
return nil, err
end
b = string.byte(data)
else else
b = first_byte b = first_byte
end end
local n, s = 0, 0 local n, s = 0, 0
while err == nil and b & 0x80 == 0x80 do while b & 0x80 == 0x80 do
if s > 21 then if s > 21 then
return 0, "number too large" return nil, "number too large"
end end
n = n + ((b & 0x7F) << s) n = n + ((b & 0x7F) << s)
s = s + 7 s = s + 7
b, err = safeRead(conn, 1) data, err = safeRead(conn, 1)
end if err ~= nil then
return nil, err
end
if err ~= nil then b = string.byte(data)
return n, err
end end
return n + (b << s), nil return n + (b << s), nil