-
Notifications
You must be signed in to change notification settings - Fork 1
/
leb128.lua
196 lines (155 loc) · 3.63 KB
/
leb128.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
local ffi = require("ffi")
local bit = require("bit")
local leb128 = {}
-- https://en.wikipedia.org/wiki/LEB128
---@param p ffi.cdata*
---@return integer
---@return integer
function leb128.udec(p)
p = ffi.cast("const unsigned char *", p) ---@type integer[]
local result = 0ull
local shift = 0
local byte = 0
local i = 0
while true do
byte = ffi.cast("uint64_t", p[i]) ---@cast byte integer
i = i + 1
result = bit.bor(result, bit.lshift(bit.band(byte, 0x7f), shift))
if bit.band(byte, 0x80) == 0 then
break
end
shift = shift + 7
end
return i, result
end
---@param p ffi.cdata*
---@return integer
---@return integer
function leb128.sdec(p)
p = ffi.cast("const unsigned char *", p) ---@type integer[]
local result = 0ll
local shift = 0
local byte = 0
local i = 0
while true do
byte = ffi.cast("uint64_t", p[i]) ---@cast byte integer
i = i + 1
result = bit.bor(result, bit.lshift(bit.band(byte, 0x7f), shift))
shift = shift + 7
if bit.band(byte, 0x80) == 0 then
break
end
end
if shift < 64 and bit.band(byte, 0x40) ~= 0 then
result = bit.bor(result, bit.lshift(bit.bnot(0ull), shift))
end
return i, ffi.cast("int64_t", result) ---@type integer, integer
end
---@param p ffi.cdata*
---@param value integer
function leb128.uenc(p, value)
p = ffi.cast("unsigned char *", p) ---@type integer[]
value = value + 0ull
local i = 0
while true do
local byte = bit.band(value, 0x7f)
value = bit.rshift(value, 7)
if value ~= 0 then
byte = bit.bor(byte, 0x80)
end
p[i] = byte
i = i + 1
if value == 0 then
break
end
end
return i
end
---@param p ffi.cdata*
---@param value integer
function leb128.senc(p, value)
p = ffi.cast("unsigned char *", p) ---@type integer[]
value = value + 0ll
local i = 0
local more = true
while more do
local byte = bit.band(value, 0x7f)
value = bit.arshift(value, 7)
if value == 0ll and bit.band(byte, 0x40) == 0ll or value == -1ll and bit.band(byte, 0x40) ~= 0ll then
more = false
else
byte = bit.bor(byte, 0x80)
end
p[i] = byte
i = i + 1
end
return i
end
-- tests
local p = ffi.new("uint8_t[?]", 16)
do -- from wikipedia
local n = 624485
local s = string.char(0xE5, 0x8E, 0x26) -- LSB to MSB
local size = leb128.uenc(p, n)
assert(size == #s)
assert(ffi.string(p, size) == s)
local size, result = leb128.udec(p)
assert(size == 3)
assert(result == n)
end
do -- from wikipedia
local n = -123456
local s = string.char(0xC0, 0xBB, 0x78) -- LSB to MSB
local size = leb128.senc(p, n)
assert(size == 3)
assert(ffi.string(p, size) == s)
local size, result = leb128.sdec(p)
assert(size == 3)
assert(result == n, result)
end
do
local n = 0ull
assert(tostring(n) == "0ULL")
local size = leb128.uenc(p, n)
assert(size == 1)
local size, result = leb128.udec(p)
assert(size == 1)
assert(result == n)
end
do
local n = -1ull
assert(tostring(n) == "18446744073709551615ULL")
local size = leb128.uenc(p, n)
assert(size == 10)
local size, result = leb128.udec(p)
assert(size == 10)
assert(result == n)
end
do
local n = bit.ror(0ll, 1)
assert(tostring(n) == "0LL")
local size = leb128.uenc(p, n)
assert(size == 1)
local size, result = leb128.udec(p)
assert(size == 1)
assert(result == n)
end
do
local n = bit.ror(1ll, 1)
assert(tostring(n) == "-9223372036854775808LL")
local size = leb128.uenc(p, n)
assert(size == 10)
local size, result = leb128.udec(p)
assert(size == 10)
assert(result == n)
end
do
local n = bit.bnot(bit.ror(1ll, 1))
assert(tostring(n) == "9223372036854775807LL")
local size = leb128.uenc(p, n)
assert(size == 9)
local size, result = leb128.udec(p)
assert(size == 9)
assert(result == n)
end
return leb128