This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BlobWriter.lua
524 lines (521 loc) · 14.4 KB
/
BlobWriter.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
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
local LICENSE = [[
Copyright (c) 2017-2021 megagrump
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 ffi = require('ffi')
local band, bnot, shr, shl = bit.band, bit.bnot, bit.rshift, bit.lshift
local _byteOrder, _parseByteOrder, _Union
local _tags, _getTag, _taggedWriters, _packMap, _arrayTypeMap
local _hasTypename, _hasSerializer
local BlobWriter
do
local _class_0
local _base_0 = {
write = function(self, value)
return self:_writeTagged(value)
end,
number = function(self, value)
self._union.f64 = value
return self:u32(self._union.u32[0]):u32(self._union.u32[1])
end,
bool = function(self, value)
return self:u8(value and 1 or 0)
end,
string = function(self, value)
local length = #value
return self:vu32(length):raw(value, length)
end,
u8 = function(self, value)
if self._length + 1 > self._size then
self:_grow(1)
end
self._data[self._length] = value
self._length = self._length + 1
return self
end,
s8 = function(self, value)
self._union.s8[0] = value
return self:u8(self._union.u8[0])
end,
u16 = function(self, value)
local len = self._length
if len + 2 > self._size then
self:_grow(2)
end
self._data[len], self._data[len + 1] = self._orderBytes(band(value, 2 ^ 8 - 1), shr(value, 8))
self._length = self._length + 2
return self
end,
s16 = function(self, value)
self._union.s16[0] = value
return self:u16(self._union.u16[0])
end,
u32 = function(self, value)
local len = self._length
if len + 4 > self._size then
self:_grow(4)
end
local w1, w2 = self._orderBytes(band(value, 2 ^ 16 - 1), shr(value, 16))
local b1, b2 = self._orderBytes(band(w1, 2 ^ 8 - 1), shr(w1, 8))
local b3, b4 = self._orderBytes(band(w2, 2 ^ 8 - 1), shr(w2, 8))
self._data[len], self._data[len + 1], self._data[len + 2], self._data[len + 3] = b1, b2, b3, b4
self._length = self._length + 4
return self
end,
s32 = function(self, value)
self._union.s32[0] = value
return self:u32(self._union.u32[0])
end,
vu32 = function(self, value)
if not (value < 2 ^ 32) then
error("Exceeded u32 value limits")
end
for i = 7, 28, 7 do
local mask, shift = 2 ^ i - 1, i - 7
if value < 2 ^ i then
return self:u8(shr(band(value, mask), shift))
end
self:u8(shr(band(value, mask), shift) + 0x80)
end
return self:u8(shr(band(value, 0xf0000000), 28))
end,
vs32 = function(self, value)
if not (value < 2 ^ 31 and value >= -2 ^ 31) then
error("Exceeded s32 value limits")
end
local signBit
signBit, value = value < 0 and 1 or 0, math.abs(value)
if value < 2 ^ 6 then
return self:u8(shl(band(value, 0x3f), 1) + signBit)
end
self:u8(shl(band(value, 0x3f), 1) + signBit + 0x80)
for i = 13, 27, 7 do
local mask, shift = 2 ^ i - 1, i - 7
if value < 2 ^ i then
return self:u8(shr(band(value, mask), shift))
end
self:u8(shr(band(value, mask), shift) + 0x80)
end
return self:u8(shr(band(value, 0xf8000000), 27))
end,
u64 = function(self, value)
self._union.u64 = value
local a, b = self._orderBytes(self._union.u32[0], self._union.u32[1])
return self:u32(a):u32(b)
end,
s64 = function(self, value)
self._union.s64 = value
local a, b = self._orderBytes(self._union.u32[0], self._union.u32[1])
return self:u32(a):u32(b)
end,
f32 = function(self, value)
self._union.f32[0] = value
return self:u32(self._union.u32[0])
end,
f64 = function(self, value)
return self:number(value)
end,
raw = function(self, value, length)
length = length or (type(value) == 'string' and #value or ffi.sizeof(value))
local makeRoom = (self._size - self._length) - length
if makeRoom < 0 then
self:_grow(math.abs(makeRoom))
end
ffi.copy(ffi.cast('uint8_t*', self._data + self._length), value, length)
self._length = self._length + length
return self
end,
cstring = function(self, value)
return self:raw(value):u8(0)
end,
cdata = function(self, value, typename, length)
if not typename and pcall(_hasTypename, value) then
typename = value.__typename
end
if not (typename) then
error("Can't write cdata without a type name")
end
self:string(typename)
if pcall(_hasSerializer, value) then
value:__serialize(self)
else
length = length or ffi.sizeof(value)
self:vu32(length)
self:raw(value, length)
end
return self
end,
table = function(self, value)
return self:_writeTable(value, { })
end,
array = function(self, valueType, values, writeLength)
if writeLength == nil then
writeLength = true
end
local writer = _arrayTypeMap[valueType]
if not (writer) then
error("Invalid array type <" .. tostring(valueType) .. ">")
end
if writeLength then
self:vu32(#values)
end
for _index_0 = 1, #values do
local v = values[_index_0]
writer(self, v)
end
return self
end,
pack = function(self, format, ...)
local data, index, len = {
...
}, 1, nil
local limit = select('#', ...)
local _writeRaw
_writeRaw = function()
local l = tonumber(table.concat(len))
if not (l) then
error("Invalid string length specification: " .. tostring(table.concat(len)))
end
if not (l < 2 ^ 32) then
error("Maximum string length exceeded")
end
self:raw(data[index], l)
index, len = index + 1, nil
end
for i = 1, #format do
local c = format:sub(i, i)
if len then
if tonumber(c) then
table.insert(len, c)
else
if not (index <= limit) then
error("Number of arguments to pack does not match format specifiers")
end
_writeRaw()
end
end
if not (len) then
local writer = _packMap[c]
if not (writer) then
error("Invalid data type specifier: " .. tostring(c))
end
if c == 'c' then
len = { }
else
if not (index <= limit) then
error("Number of arguments to pack does not match format specifiers")
end
if writer(self, data[index]) then
index = index + 1
end
end
end
end
if len then
_writeRaw()
end
return self
end,
clear = function(self, size)
self._length = 0
if size then
self._data = nil
self:resize(size)
end
return self
end,
tostring = function(self)
return ffi.string(self._data, self._length)
end,
length = function(self)
return self._length
end,
size = function(self)
return self._size
end,
vu32size = function(self, value)
if not (value < 2 ^ 32) then
error("Exceeded u32 value limits")
end
if value < 2 ^ 7 then
return 1
end
if value < 2 ^ 14 then
return 2
end
if value < 2 ^ 21 then
return 3
end
if value < 2 ^ 28 then
return 4
end
return 5
end,
vs32size = function(self, value)
if not (value < 2 ^ 31 and value >= -2 ^ 31) then
error("Exceeded s32 value limits")
end
value = math.abs(value) + 1
if value < 2 ^ 7 then
return 1
end
if value < 2 ^ 14 then
return 2
end
if value < 2 ^ 21 then
return 3
end
if value < 2 ^ 28 then
return 4
end
return 5
end,
setByteOrder = function(self, byteOrder)
self._orderBytes = _byteOrder[_parseByteOrder(byteOrder)]
return self
end,
resize = function(self, newSize)
local data
self._length = math.min(newSize, self._length)
if newSize > 0 then
data = ffi.new('uint8_t[?]', newSize)
if self._data then
ffi.copy(data, self._data, self._length)
end
end
self._data, self._size = data, newSize
return self
end,
_grow = function(self, minimum)
if minimum == nil then
minimum = 0
end
local newSize = math.max(self._size + minimum, math.floor(math.max(1, self._size * 1.5) + .5))
return self:resize(newSize)
end,
_writeTable = function(self, t, stack)
if stack == nil then
stack = { }
end
if stack[t] then
error("Cycle detected; can't serialize table")
end
stack[t] = true
for key, value in pairs(t) do
self:_writeTaggedPair(key, value, stack)
end
stack[t] = nil
return self:u8(_tags.stop)
end,
_writeTaggedPair = function(self, key, value, stack)
if type(value) == 'function' then
return self
end
self:_writeTagged(key, stack)
return self:_writeTagged(value, stack)
end,
_writeTagged = function(self, value, stack)
local tag = _getTag(value)
if not (tag) then
error("Can't write values of type '" .. tostring(type(value)) .. "'")
end
self:u8(tag)
return _taggedWriters[tag](self, value, stack)
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, sizeOrByteOrder, size)
self._union = _Union()
self._length, self._size = 0, 0
local byteOrder = type(sizeOrByteOrder) == 'string' and sizeOrByteOrder or nil
size = type(sizeOrByteOrder) == 'number' and sizeOrByteOrder or size
self:setByteOrder(byteOrder)
return self:resize(size or 1024)
end,
__base = _base_0,
__name = "BlobWriter"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
BlobWriter = _class_0
end
_byteOrder = {
le = function(v1, v2)
return v1, v2
end,
be = function(v1, v2)
return v2, v1
end
}
_tags = {
stop = 0,
number = 1,
string = 2,
table = 3,
[true] = 4,
[false] = 5,
zero = 6,
vs32 = 7,
vu32 = 8,
vs64 = 9,
vu64 = 10,
cdata = 11
}
do
_taggedWriters = {
BlobWriter.number,
BlobWriter.string,
BlobWriter._writeTable,
function(self)
return self
end,
function(self)
return self
end,
function(self)
return self
end,
BlobWriter.vs32,
BlobWriter.vu32,
function(self, val)
self._union.s64 = val
return self:vs32(self._union.s32[0]):vs32(self._union.s32[1])
end,
function(self, val)
self._union.u64 = val
return self:vu32(self._union.u32[0]):vu32(self._union.u32[1])
end,
function(self, val)
return self:cdata(val)
end
}
_arrayTypeMap = {
s8 = BlobWriter.s8,
u8 = BlobWriter.u8,
s16 = BlobWriter.s16,
u16 = BlobWriter.u16,
s32 = BlobWriter.s32,
u32 = BlobWriter.u32,
s64 = BlobWriter.s64,
u64 = BlobWriter.u64,
vs32 = BlobWriter.vs32,
vu32 = BlobWriter.vu32,
f32 = BlobWriter.f32,
f64 = BlobWriter.f64,
number = BlobWriter.number,
string = BlobWriter.string,
cstring = BlobWriter.cstring,
bool = BlobWriter.bool,
table = BlobWriter.table,
cdata = BlobWriter.cdata
}
_packMap = {
b = BlobWriter.s8,
B = BlobWriter.u8,
h = BlobWriter.s16,
H = BlobWriter.u16,
l = BlobWriter.s32,
L = BlobWriter.u32,
v = BlobWriter.vs32,
V = BlobWriter.vu32,
q = BlobWriter.s64,
Q = BlobWriter.u64,
f = BlobWriter.f32,
d = BlobWriter.number,
n = BlobWriter.number,
c = BlobWriter.raw,
s = BlobWriter.string,
z = BlobWriter.cstring,
t = BlobWriter.table,
y = BlobWriter.bool,
C = BlobWriter.cdata,
['<'] = function(self)
return nil, self:setByteOrder('<')
end,
['>'] = function(self)
return nil, self:setByteOrder('>')
end,
['='] = function(self)
return nil, self:setByteOrder('=')
end
}
end
_parseByteOrder = function(endian)
local _exp_0 = endian
if nil == _exp_0 or '=' == _exp_0 or 'host' == _exp_0 then
endian = ffi.abi('le') and 'le' or 'be'
elseif '<' == _exp_0 or 'le' == _exp_0 then
endian = 'le'
elseif '>' == _exp_0 or 'be' == _exp_0 then
endian = 'be'
else
error("Invalid byteOrder identifier: " .. tostring(endian))
end
return endian
end
_getTag = function(value)
local t = type(value)
local _exp_0 = t
if 'boolean' == _exp_0 then
return _tags[value]
elseif 'number' == _exp_0 then
if math.floor(value) ~= value then
return _tags.number
end
if value == 0 then
return _tags.zero
end
if value > 0 then
if value < 2 ^ 32 then
return _tags.vu32
end
return _tags.vu64
end
if value >= -2 ^ 31 then
return _tags.vs32
end
return _tags.vs64
end
return _tags[t]
end
_hasTypename = function(value)
return value.__typename ~= nil
end
_hasSerializer = function(value)
return value.__serialize ~= nil
end
_Union = ffi.typeof([[ union {
int8_t s8[8];
uint8_t u8[8];
int16_t s16[4];
uint16_t u16[4];
int32_t s32[2];
uint32_t u32[2];
float f32[2];
int64_t s64;
uint64_t u64;
double f64;
}
]])
return BlobWriter