Repository navigation
Expand file tree
/
Copy pathstruct.lua
More file actions
586 lines (509 loc) · 15.8 KB
/
Copy pathstruct.lua
File metadata and controls
586 lines (509 loc) · 15.8 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
--[[
* Copyright (c) 2015-2026 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.
]]
local unpack = table.unpack or _G.unpack
local floor, huge, log = math.floor, math.huge, math.log
local byte, char, sub, rep = string.byte, string.char, string.sub, string.rep
local find, match, reverse = string.find, string.match, string.reverse
local concat = table.concat
local struct = {}
struct._VERSION = '1.0.0'
-- math.ldexp and math.frexp are deprecated since Lua 5.3 and missing from
-- builds without LUA_COMPAT_MATHLIB, so provide replacements for those
local ldexp = math.ldexp or function(m, e)
-- scale in two steps, 2 ^ e on its own could overflow or underflow
local half = floor(e / 2)
return m * 2 ^ half * 2 ^ (e - half)
end
local frexp = math.frexp or function(x)
-- x is always finite and greater than zero in here
local e = floor(log(x) / log(2)) + 1
local m = ldexp(x, -e)
-- math.log may be off by one around powers of two
while m >= 1 do
m = m / 2
e = e + 1
end
while m < 0.5 do
m = m * 2
e = e - 1
end
return m, e
end
-- size in bytes of each option, for 'i', 'I' and 'c' it is only the default
local sizes = {
b = 1, B = 1, h = 2, H = 2, i = 4, I = 4, l = 8, L = 8, T = 8,
f = 4, d = 8, x = 1, c = 1
}
local numeric = {
b = true, B = true, h = true, H = true, i = true, I = true,
l = true, L = true, T = true, f = true, d = true
}
local signed = {b = true, h = true, i = true, l = true}
-- level is the one error() would get if it was called in place of badarg()
local function badarg(level, fname, narg, msg)
error(string.format("bad argument #%d to '%s' (%s)", narg, fname, msg), level + 1)
end
local function typename(args, nargs, argi)
return argi > nargs and 'no value' or type(args[argi])
end
local function getnum(format, i, default)
local digits = match(format, '^%d+', i)
if digits then
return tonumber(digits), i + #digits
end
return default, i
end
local function ispow2(n)
while n > 1 and n % 2 == 0 do
n = n / 2
end
return n == 1
end
-- reads the option at position i of the format, returns the option,
-- its size and the position of the option that follows
local function nextoption(fname, format, i)
local opt = sub(format, i, i)
local size = sizes[opt]
i = i + 1
if opt == 'i' or opt == 'I' then
size, i = getnum(format, i, size)
if size < 1 or size > 8 then
badarg(3, fname, 1, string.format('integral size %s is out of limits [1,8]', tostring(size)))
end
elseif opt == 'c' then
size, i = getnum(format, i, size)
elseif opt == '!' then
size, i = getnum(format, i, 8)
if not ispow2(size) then
badarg(3, fname, 1, string.format('alignment %s is not a power of 2', tostring(size)))
end
elseif not size and opt ~= 's' and opt ~= '<' and opt ~= '>' and opt ~= ' ' then
badarg(3, fname, 1, string.format("invalid format option '%s'", opt))
end
return opt, size, i
end
-- number of bytes needed to align an item of the given size at the offset
local function padding(fname, offset, size, align)
if size > align then
size = align
end
if size ~= 1 and size ~= 2 and size ~= 4 and size ~= 8 then
badarg(3, fname, 1, 'format asks for alignment not power of 2')
end
return (size - offset % size) % size
end
-- The value is split into 32 bit halves before it gets cut into bytes. That
-- keeps every step exact both for doubles and for the 64 bit integers of
-- Lua 5.3+ (where '/' would otherwise round anything above 2 ^ 53).
local function packint(val, size, little)
if size == 1 then
return char(val % 256)
end
local low = val % 4294967296
local high = (val - low) / 4294967296
local b1 = low % 256
low = (low - b1) / 256
local b2 = low % 256
low = (low - b2) / 256
local b3 = low % 256
local b4 = (low - b3) / 256
local str
if size <= 4 then
str = char(b1, b2, b3, b4)
else
high = high % 4294967296
local b5 = high % 256
high = (high - b5) / 256
local b6 = high % 256
high = (high - b6) / 256
local b7 = high % 256
local b8 = (high - b7) / 256
str = char(b1, b2, b3, b4, b5, b6, b7, b8)
end
if size ~= 4 and size ~= 8 then
str = sub(str, 1, size)
end
return little and str or reverse(str)
end
local function unpackint(stream, pos, size, little, issigned)
if size == 1 then
local val = byte(stream, pos)
if issigned and val >= 128 then
val = val - 256
end
return val
elseif size == 2 then
local b1, b2 = byte(stream, pos, pos + 1)
local val = little and b2 * 256 + b1 or b1 * 256 + b2
if issigned and val >= 32768 then
val = val - 65536
end
return val
elseif size == 4 then
local b1, b2, b3, b4 = byte(stream, pos, pos + 3)
if little then
b1, b2, b3, b4 = b4, b3, b2, b1
end
local val = ((b1 * 256 + b2) * 256 + b3) * 256 + b4
if issigned and val >= 2147483648 then
val = val - 4294967296
end
return val
end
-- walk from the most significant byte down
local first, last, step = pos, pos + size - 1, 1
if little then
first, last, step = last, first, -1
end
-- Negative numbers are read as their complement, so they stay small and
-- exact in a double instead of being rounded somewhere around 2 ^ 64.
local negative = issigned and byte(stream, first) >= 128
local high, low = 0, 0
local count = size - 4
for i = first, last, step do
local b = byte(stream, i)
if negative then
b = 255 - b
end
if count > 0 then
high = high * 256 + b
else
low = low * 256 + b
end
count = count - 1
end
-- high and low are added just once, this way a value which does not fit
-- into a double is rounded a single time
if negative then
return -(high * 4294967296 + (low + 1))
end
local val = high * 4294967296 + low
if val < 0 then
-- Lua 5.3+: an unsigned value above the integer range wrapped around
val = high * 4294967296.0 + low
end
return val
end
-- splits a number into the sign, exponent and mantissa of an IEEE 754 value
-- with the given amount of exponent (ebits) and mantissa (mbits) bits
local function splitfloat(val, ebits, mbits)
local sign, exponent, mantissa = 0, 0, 0
local emax = 2 ^ ebits - 1
local bias = (emax - 1) / 2
-- turns an integer of Lua 5.3+ into a float, unlike '+ 0.0' keeps the sign of -0.0
val = val * 1.0
if val < 0 or (val == 0 and 1 / val < 0) then
sign = 1
val = -val
end
if val ~= val then
exponent, mantissa = emax, 2 ^ (mbits - 1)
elseif val == huge then
exponent = emax
elseif val ~= 0 then
local m, e = frexp(val)
exponent = e + bias - 1
if exponent > 0 then
mantissa = (m * 2 - 1) * 2 ^ mbits
else
-- subnormal
mantissa = ldexp(val, bias - 1 + mbits)
exponent = 0
end
-- round to nearest, ties to even, exactly like a cast in C does
local rounded = floor(mantissa)
local rest = mantissa - rounded
if rest > 0.5 or (rest == 0.5 and rounded % 2 == 1) then
rounded = rounded + 1
end
mantissa = rounded
if mantissa == 2 ^ mbits then
-- rounding carried over into the exponent
mantissa = 0
exponent = exponent + 1
end
if exponent >= emax then
-- too large, becomes infinity
exponent, mantissa = emax, 0
end
end
return sign, exponent, mantissa
end
local function joinfloat(sign, exponent, mantissa, ebits, mbits)
local emax = 2 ^ ebits - 1
local bias = (emax - 1) / 2
if exponent == emax then
if mantissa ~= 0 then
return 0 / 0
end
return sign * huge
elseif exponent == 0 then
-- zero or subnormal
return sign * ldexp(mantissa, 1 - bias - mbits)
end
return sign * ldexp(mantissa + 2 ^ mbits, exponent - bias - mbits)
end
local function packfloat(val, little)
local sign, exponent, mantissa = splitfloat(val, 8, 23)
local b1 = mantissa % 256
mantissa = (mantissa - b1) / 256
local b2 = mantissa % 256
local b3 = (mantissa - b2) / 256 + exponent % 2 * 128
local b4 = sign * 128 + (exponent - exponent % 2) / 2
if little then
return char(b1, b2, b3, b4)
end
return char(b4, b3, b2, b1)
end
local function packdouble(val, little)
local sign, exponent, mantissa = splitfloat(val, 11, 52)
local low = mantissa % 4294967296
local high = (mantissa - low) / 4294967296
local b1 = low % 256
low = (low - b1) / 256
local b2 = low % 256
low = (low - b2) / 256
local b3 = low % 256
local b4 = (low - b3) / 256
local b5 = high % 256
high = (high - b5) / 256
local b6 = high % 256
local b7 = (high - b6) / 256 + exponent % 16 * 16
local b8 = sign * 128 + (exponent - exponent % 16) / 16
if little then
return char(b1, b2, b3, b4, b5, b6, b7, b8)
end
return char(b8, b7, b6, b5, b4, b3, b2, b1)
end
local function unpackfloat(stream, pos, little)
local b1, b2, b3, b4 = byte(stream, pos, pos + 3)
if not little then
b1, b2, b3, b4 = b4, b3, b2, b1
end
local sign = b4 >= 128 and -1 or 1
local exponent = b4 % 128 * 2 + floor(b3 / 128)
local mantissa = (b3 % 128 * 256 + b2) * 256 + b1
return joinfloat(sign, exponent, mantissa, 8, 23)
end
local function unpackdouble(stream, pos, little)
local b1, b2, b3, b4, b5, b6, b7, b8 = byte(stream, pos, pos + 7)
if not little then
b1, b2, b3, b4, b5, b6, b7, b8 = b8, b7, b6, b5, b4, b3, b2, b1
end
local sign = b8 >= 128 and -1 or 1
local exponent = b8 % 128 * 16 + floor(b7 / 16)
local mantissa = (b7 % 16 * 256 + b6) * 256 + b5
mantissa = (((mantissa * 256 + b4) * 256 + b3) * 256 + b2) * 256 + b1
return joinfloat(sign, exponent, mantissa, 11, 52)
end
function struct.pack(format, ...)
if type(format) ~= 'string' then
badarg(2, 'pack', 1, 'string expected, got ' .. type(format))
end
local args, nargs = {...}, select('#', ...)
local argi = 0
local stream, n, len = {}, 0, 0
local little, align = true, 1
local i = 1
while i <= #format do
local opt, size
opt, size, i = nextoption('pack', format, i)
if numeric[opt] then
argi = argi + 1
local val = tonumber(args[argi])
if not val then
badarg(2, 'pack', argi + 1, 'number expected, got ' .. typename(args, nargs, argi))
end
if align > 1 then
local pad = padding('pack', len, size, align)
if pad > 0 then
n = n + 1
stream[n] = rep('\0', pad)
len = len + pad
end
end
n = n + 1
if opt == 'f' then
stream[n] = packfloat(val, little)
elseif opt == 'd' then
stream[n] = packdouble(val, little)
else
if val ~= val or val == huge or val == -huge then
badarg(2, 'pack', argi + 1, 'number has no integer representation')
end
stream[n] = packint(floor(val), size, little)
end
len = len + size
elseif opt == 's' or opt == 'c' then
argi = argi + 1
local val = args[argi]
if type(val) == 'number' then
val = tostring(val)
elseif type(val) ~= 'string' then
badarg(2, 'pack', argi + 1, 'string expected, got ' .. typename(args, nargs, argi))
end
if opt == 's' then
n = n + 1
stream[n] = val
size = #val + 1
val = '\0'
else
if size == 0 then
size = #val
end
if #val < size then
val = val .. rep(' ', size - #val)
elseif #val > size then
val = sub(val, 1, size)
end
end
n = n + 1
stream[n] = val
len = len + size
elseif opt == 'x' then
n = n + 1
stream[n] = '\0'
len = len + 1
elseif opt == '<' then
little = true
elseif opt == '>' then
little = false
elseif opt == '!' then
align = size
end
end
return concat(stream)
end
function struct.unpack(format, stream, pos)
if type(format) ~= 'string' then
badarg(2, 'unpack', 1, 'string expected, got ' .. type(format))
end
if type(stream) ~= 'string' then
badarg(2, 'unpack', 2, 'string expected, got ' .. type(stream))
end
local last = #stream
if pos == nil then
pos = 1
else
local start = tonumber(pos)
if not start then
badarg(2, 'unpack', 3, 'number expected, got ' .. type(pos))
end
pos = floor(start)
if pos ~= pos or pos < -last or pos > last + 1 or pos == 0 then
badarg(2, 'unpack', 3, 'initial position out of string')
end
if pos < 0 then
pos = last + pos + 1
end
end
local vars, n = {}, 0
local little, align = true, 1
local i = 1
while i <= #format do
local opt, size
opt, size, i = nextoption('unpack', format, i)
if numeric[opt] then
if align > 1 then
pos = pos + padding('unpack', pos - 1, size, align)
end
if size > last - pos + 1 then
badarg(2, 'unpack', 2, 'data string too short')
end
n = n + 1
if opt == 'f' then
vars[n] = unpackfloat(stream, pos, little)
elseif opt == 'd' then
vars[n] = unpackdouble(stream, pos, little)
else
vars[n] = unpackint(stream, pos, size, little, signed[opt])
end
pos = pos + size
elseif opt == 'c' then
if size == 0 then
size = tonumber(vars[n])
if not size then
error("format 'c0' needs a previous size", 2)
end
if size < 0 or size ~= floor(size) then
error("invalid size for format 'c0'", 2)
end
-- not redundant: turns a float like 5.0 into an integer on Lua 5.3+
size = floor(size)
n = n - 1
end
if size > last - pos + 1 then
badarg(2, 'unpack', 2, 'data string too short')
end
n = n + 1
vars[n] = sub(stream, pos, pos + size - 1)
pos = pos + size
elseif opt == 's' then
local zero = find(stream, '\0', pos, true)
if not zero then
error('unfinished string in data', 2)
end
n = n + 1
vars[n] = sub(stream, pos, zero - 1)
pos = zero + 1
elseif opt == 'x' then
if pos > last then
badarg(2, 'unpack', 2, 'data string too short')
end
pos = pos + 1
elseif opt == '<' then
little = true
elseif opt == '>' then
little = false
elseif opt == '!' then
align = size
end
end
n = n + 1
vars[n] = pos
return unpack(vars, 1, n)
end
function struct.size(format)
if type(format) ~= 'string' then
badarg(2, 'size', 1, 'string expected, got ' .. type(format))
end
local total, align = 0, 1
local i = 1
while i <= #format do
local opt, size
opt, size, i = nextoption('size', format, i)
if opt == 's' then
badarg(2, 'size', 1, "option 's' has no fixed size")
elseif opt == 'c' and size == 0 then
badarg(2, 'size', 1, "option 'c0' has no fixed size")
elseif opt == '!' then
align = size
elseif size then
if align > 1 and numeric[opt] then
total = total + padding('size', total, size, align)
end
total = total + size
end
end
return total
end
return struct