-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.lua
365 lines (318 loc) · 12.6 KB
/
struct.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
--[[
* Copyright (c) 2015-2020 Iryont <https://github.com/iryont/lua-struct>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
]]
-- Note: I've modified this file to be more in line with my needs;
-- reducing the amount of garbage created, not creating strings but writing
-- into a table that is assumed to hold single byte values. This makes the API
-- incompatible with the original, otherwise I would fork & improve it & submit
-- a pull request. There were some quite large performance issues with the code
-- due to aiming to be generic, which I don't really need.
-- TODO: replace :find with something more efficient
local unpack = table.unpack or _G.unpack
local struct = {}
local function append_string(stream, str)
local start = #stream
for i = 1, #str do
stream[start + i] = string.char(str:byte(i))
end
end
local function append_table(stream, tab)
local start = #stream
for i = 1, #tab do
stream[start + i] = tab[i]
end
end
local function append_table_reversed(stream, tab)
local start = #stream + 1
local len = #tab
for i = 0, #tab - 1 do
stream[start + i] = tab[len - i]
end
end
function struct.packIE(stream, val)
local offset = #stream
local a = val % 0x100
local rest = (val - a) / 0x100
local b = rest % 0x100
rest = (rest - b) / 0x100
local c = rest % 0x100
rest = (rest - c) / 0x100
local d = rest
stream[offset+1], stream[offset+2], stream[offset+3], stream[offset+4] = string.char(a,b,c,d)
end
function struct.packdE(stream, val)
local sign = 0
if val < 0 then
sign = 1
val = -val
end
local mantissa, exponent = math.frexp(val)
if val == 0 then
mantissa = 0
exponent = 0
else
mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, 53)
exponent = exponent + 1022
end
val = mantissa
for i = 1, 6 do
table.insert(stream, string.char(math.floor(val) % (0x100)))
val = math.floor(val / (0x100))
end
table.insert(stream, string.char(math.floor(exponent * 16 + val) % (0x100)))
val = math.floor((exponent * 16 + val) / (0x100))
table.insert(stream, string.char(math.floor(sign * 128 + val) % (0x100)))
end
---format is expected to be a string, which may contain the following format codes:
---* "b" a signed char.
---* "B" an unsigned char.
---* "h" a signed short (2 bytes).
---* "H" an unsigned short (2 bytes).
---* "i" a signed int (4 bytes).
---* "I" an unsigned int (4 bytes).
---* "l" a signed long (8 bytes).
---* "L" an unsigned long (8 bytes).
---* "f" a float (4 bytes).
---* "d" a double (8 bytes).
---* "s" a zero-terminated string.
---* "cn" a sequence of exactly n chars corresponding to a single Lua string (if n <= 0 then for packing - the string length is taken, unpacking - the number value of the previous unpacked value which is not returned).
---@param format string
---@param stream table
---@param ... unknown
---@return nil
function struct.pack(format, stream, ...)
local endianness = true
local var_index = 1
for i = 1, format:len() do
local opt = format:sub(i, i)
if opt == '<' then
endianness = true
elseif opt == '>' then
endianness = false
elseif opt:find('[bBhHiIlL]') then
local n = opt:find('[hH]') and 2 or opt:find('[iI]') and 4 or opt:find('[lL]') and 8 or 1
local val = tonumber(select(var_index, ...)) or 0
var_index = var_index + 1
local bytes = {}
for j = 1, n do
table.insert(bytes, string.char(val % (0x100)))
val = math.floor(val / (0x100))
end
if not endianness then
append_table_reversed(stream, bytes)
else
append_table(stream, bytes)
end
elseif opt:find('[fd]') then
local val = tonumber(select(var_index, ...)) or 0
var_index = var_index + 1
local sign = 0
if val < 0 then
sign = 1
val = -val
end
local mantissa, exponent = math.frexp(val)
if val == 0 then
mantissa = 0
exponent = 0
else
mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, (opt == 'd') and 53 or 24)
exponent = exponent + ((opt == 'd') and 1022 or 126)
end
local bytes = {}
if opt == 'd' then
val = mantissa
for i = 1, 6 do
table.insert(bytes, string.char(math.floor(val) % (0x100)))
val = math.floor(val / (0x100))
end
else
table.insert(bytes, string.char(math.floor(mantissa) % (0x100)))
val = math.floor(mantissa / (0x100))
table.insert(bytes, string.char(math.floor(val) % (0x100)))
val = math.floor(val / (0x100))
end
table.insert(bytes, string.char(math.floor(exponent * ((opt == 'd') and 16 or 128) + val) % (0x100)))
val = math.floor((exponent * ((opt == 'd') and 16 or 128) + val) / (0x100))
table.insert(bytes, string.char(math.floor(sign * 128 + val) % (0x100)))
val = math.floor((sign * 128 + val) / (0x100))
if not endianness then
append_table_reversed(stream, bytes)
else
append_table(stream, bytes)
end
elseif opt == 's' then
local val = tostring(select(var_index, ...))
var_index = var_index + 1
append_string(stream, val)
table.insert(stream, string.char(0))
elseif opt == 'c' then
local n = format:sub(i + 1):match('%d+')
local len = tonumber(n)
local str = tostring(select(var_index, ...))
var_index = var_index + 1
if len <= 0 then
len = str:len()
end
if len - str:len() > 0 then
str = str .. string.rep(' ', len - str:len())
end
append_string(stream, str:sub(1, len))
i = i + n:len()
end
end
end
function struct.unpackI(stream, pos)
local val = string.byte(stream, pos) + string.byte(stream, pos + 1) * 0x100 +
string.byte(stream, pos + 2) * 0x10000 + string.byte(stream, pos + 3) * 0x1000000
return val
end
function struct.unpackd(stream, pos)
local iterator = pos
local n = 8
local x = stream:sub(iterator, iterator + n - 1)
iterator = iterator + n
local sign = 1
local mantissa = string.byte(x, 7) % 16
for j = n - 2, 1, -1 do
mantissa = mantissa * (0x100) + string.byte(x, j)
end
if string.byte(x, n) > 127 then
sign = -1
end
local exponent = (string.byte(x, n) % 128) * 16 +
math.floor(string.byte(x, n - 1) / 16)
local val = 0
if exponent ~= 0 then
mantissa = (math.ldexp(mantissa, -52) + 1) * sign
val = math.ldexp(mantissa, exponent - 1023)
end
return val
end
function struct.unpack(format, stream, pos)
local vars = {}
local iterator = pos or 1
local endianness = true
for i = 1, format:len() do
local opt = format:sub(i, i)
if opt == '<' then
endianness = true
elseif opt == '>' then
endianness = false
-- defining some fast paths for the most common cases (I and d) of my serializer
elseif opt == 'I' and endianness then
local val = string.byte(stream, iterator) + string.byte(stream, iterator + 1) * 0x100 +
string.byte(stream, iterator + 2) * 0x10000 + string.byte(stream, iterator + 3) * 0x1000000
if #format == 1 then
return val
end
table.insert(vars, val)
elseif opt == "d" then
local n = 8
local x = stream:sub(iterator, iterator + n - 1)
iterator = iterator + n
if not endianness then
x = string.reverse(x)
end
local sign = 1
local mantissa = string.byte(x, 7) % 16
for j = n - 2, 1, -1 do
mantissa = mantissa * (0x100) + string.byte(x, j)
end
if string.byte(x, n) > 127 then
sign = -1
end
local exponent = (string.byte(x, n) % 128) * 16 +
math.floor(string.byte(x, n - 1) / 16)
local val = 0
if exponent ~= 0 then
mantissa = (math.ldexp(mantissa, -52) + 1) * sign
val = math.ldexp(mantissa, exponent - 1023)
end
if #format == 1 then
return val
end
table.insert(vars, val)
elseif opt:find('[bBhHiIlL]') then
local n = opt:find('[hH]') and 2 or opt:find('[iI]') and 4 or opt:find('[lL]') and 8 or 1
local signed = opt:lower() == opt
local val = 0
for j = 1, n do
local byte = string.byte(stream, iterator)
if endianness then
val = val + byte * (2 ^ ((j - 1) * 8))
else
val = val + byte * (2 ^ ((n - j) * 8))
end
iterator = iterator + 1
end
if signed and val >= 2 ^ (n * 8 - 1) then
val = val - 2 ^ (n * 8)
end
table.insert(vars, math.floor(val))
elseif opt:find('[fd]') then
local n = (opt == 'd') and 8 or 4
local x = stream:sub(iterator, iterator + n - 1)
iterator = iterator + n
if not endianness then
x = string.reverse(x)
end
local sign = 1
local mantissa = string.byte(x, (opt == 'd') and 7 or 3) % ((opt == 'd') and 16 or 128)
for i = n - 2, 1, -1 do
mantissa = mantissa * (0x100) + string.byte(x, i)
end
if string.byte(x, n) > 127 then
sign = -1
end
local exponent = (string.byte(x, n) % 128) * ((opt == 'd') and 16 or 2) +
math.floor(string.byte(x, n - 1) / ((opt == 'd') and 16 or 128))
if exponent == 0 then
table.insert(vars, 0.0)
else
mantissa = (math.ldexp(mantissa, (opt == 'd') and -52 or -23) + 1) * sign
table.insert(vars, math.ldexp(mantissa, exponent - ((opt == 'd') and 1023 or 127)))
end
elseif opt == 's' then
local bytes = {}
for j = iterator, stream:len() do
if stream:sub(j, j) == string.char(0) or stream:sub(j) == '' then
break
end
table.insert(bytes, stream:sub(j, j))
end
local str = table.concat(bytes)
iterator = iterator + str:len() + 1
table.insert(vars, str)
elseif opt == 'c' then
local n = format:sub(i + 1):match('%d+')
local len = tonumber(n)
if len <= 0 then
len = table.remove(vars)
end
table.insert(vars, stream:sub(iterator, iterator + len - 1))
iterator = iterator + len
i = i + n:len()
end
end
return unpack(vars)
end
return struct