-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat.lua
More file actions
587 lines (480 loc) · 14.6 KB
/
format.lua
File metadata and controls
587 lines (480 loc) · 14.6 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
587
local format = { }
local Writer = { }
Writer.__index = Writer;
local spack = string.pack
local sunpack = string.unpack
local tunpack = table.unpack
function Writer:getposition()
return self.position
end
local function writeraw(writer, string)
writer.inner:write(string)
writer.position = writer.position + #string
end
local tmpByteStorage = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
local bytesPack = { "B", "BB", "BBB", "BBBB", "BBBBB",
"BBBBBB", "BBBBBBB", "BBBBBBBB",
"BBBBBBBBB", "BBBBBBBBBB" }
local function writevarint(writer, number)
assert(type(number) == "number", "number expected")
local count = 1
while number >= 0x80 or number < 0 do
local byte = (number | 0x80) & 0x00000000000000FF
number = number >> 7;
tmpByteStorage[count] = byte
count = count + 1
end
tmpByteStorage[count] = number
writer.inner:write(spack(bytesPack[count], tunpack(tmpByteStorage)))
writer.position = writer.position + count
end
--Writes the bytes contained in a raw string,
--to the output stream.
function Writer:raw(string)
self.inner:write(string)
self.position = self.position + #string
end
--Writes a length delimted stream of bytes
function Writer:stream(s)
assert(type(s) == "string", "string expected")
local length = #s
writevarint(self, length)
self.inner:write(s)
self.position = self.position + length
end
--Encodes an integer that is in the range [0 .. 0xff].
function Writer:uint8(byte)
self.inner:write(spack("B", byte))
self.position = self.position + 1
end
--Writes an integer that is in the range [0 .. 0xffff].
function Writer:uint16(number)
self.inner:write(spack("I2", number))
self.position = self.position + 2
end
--Writes an integer that is in the range [0 .. 0xffffffff].
function Writer:uint32(number)
self.inner:write(spack("I4", number) )
self.position = self.position + 4
end
--Writes an integer that is in the range [0 .. 0xffffffffffffffff].
function Writer:uint64(number)
self.inner:write(spack("I8", number))
self.position = self.position + 8
end
--Writes an integer that is in the range [-0x80 .. 0x7F]
function Writer:int8(byte)
self.inner:write(spack("b", byte))
self.position = self.position + 1
end
--Writes an integer that is in the range [-0x8000 .. 0x7fff].
function Writer:int16(number)
self.inner:write(spack("i2", number))
self.position = self.position + 2
end
--Writes integer that is in the range [-0x80000000 .. 0x7fffffff].
function Writer:int32(number)
self.inner:write(spack("i4", number))
self.position = self.position + 4
end
--Writes an integer that is in the range [-0x8000000000000000 .. 0x7fffffffffffffff].
function Writer:int64(number)
self.inner:write(spack("i8", number))
self.position = self.position + 8
end
--Writes a float point value with 32-bit precision in IEEE 754 format.
function Writer:float(number)
self.inner:write(spack("f", number))
self.position = self.position + 4
end
--Writes a float point value with 64-bit precision in IEEE 754 format.
function Writer:double(number)
self.inner:write(spack("d", number))
self.position = self.position + 8
end
--Writes a number in the variable integer encoding
--used by google protocol buffers.
Writer.varint = writevarint
--Writes a number in zigzag encoded format
--used for zigzag variable integer encoding used in
--google protocol buffers.
function Writer:varintzz(number)
-- This did not work for negative numbers...
-- local bits = number >> 63
--Workaround
--All bits set to 1
local bits = 0xFFFFFFFFFFFFFFFF
if(number >= 0) then
--All bits set to 0
bits = 0;
end
local zigzaged = (number << 1) ~ bits
writevarint(self, zigzaged)
end
--Writes a boolean value.
function Writer:bool(bool)
if bool then bool = 1 else bool = 0 end
self.inner:write(spack("B", bool))
self.position = self.position + 1
end
--Writes the first size bits in value
function Writer:bits(size, value)
local count = self.bit_count
local bits = self.bit_buffer
local nbytes = 0
bits = bits | (value << count)
count = count + size
while count >= 8 do
count = count - 8
value = value >> (size-count)
size = count
nbytes = nbytes + 1
tmpByteStorage[nbytes] = bits & 0xFF
bits = value
end
self.bit_count = count
self.bit_buffer = bits & ~(-1 << count)
if nbytes ~= 0 then
writeraw(self, spack(bytesPack[nbytes], tunpack(tmpByteStorage)))
end
end
--Writes an unsigned integer of (size) bits
function Writer:uint(size, value)
local max = (1 << size) - 1
assert(type(value) == "number", "number expected")
assert(math.type(value) == "integer", "has no integer representation")
assert((value >= 0 and value <= max) or size == 64, "unsigned overflow ")
self:bits(size, value)
end
--Writes a signed integer of (size) bits
function Writer:int(size, value)
assert(type(value) == "number", "number expected")
assert(math.type(value) == "integer", "has no integer representation")
local half = 1 << (size - 1)
if value < 0 then
assert(-half <= value or size == 64, "integer overflow")
local offset = (1 << size)
local nval = offset + value
self:bits(size, nval)
else
assert(half > value or size == 64, "integer overflow")
self:bits(size, value)
end
end
--Flushes any remaining bits in the bitbuffer to the
--output.
function Writer:flushbits()
local count = self.bit_count
if count > 0 then
writeraw(self, spack("B", self.bit_buffer & 0xFF))
self.bit_count = 0
self.bit_buffer = 0
end
end
local alignbytes =
{
spack("I1", 0), spack("I2", 0),
spack("I3", 0), spack("I4", 0),
spack("I5", 0), spack("I6", 0),
spack("I7", 0), spack("I8", 0)
}
--Alignes the output to the specified number of bytes.
function Writer:align(to)
local pos = self.position
local to_align = ((pos + (to - 1)) & ~(to - 1)) - pos
if to_align > 0 then
self:flushbits()
self:raw(alignbytes[to_align])
end
end
--Flushes the underlying stream and any bits not yet written.
function Writer:flush()
self:flushbits()
self.inner:flush()
end
local funcs =
{
b = { f = Writer.int8, args = 1 },
h = { f = Writer.int16, args = 1 },
i = { f = Writer.int32, args = 1 },
l = { f = Writer.int64, args = 1 },
B = { f = Writer.uint8, args = 1 },
H = { f = Writer.uint16, args = 1 },
I = { f = Writer.uint32, args = 1 },
L = { f = Writer.uint64, args = 1 },
f = { f = Writer.float, args = 1 },
d = { f = Writer.double, args = 1 },
s = { f = Writer.stream, args = 1 },
r = { f = Writer.raw, args = 1 },
v = { f = Writer.varintzz, args = 1 },
V = { f = Writer.varint, args = 1 },
p = { f = Writer.int, args = 2 },
P = { f = Writer.uint, args = 2 }
}
function Writer:writef(fmt, ...)
local idx = 1
for i=1, #fmt do
local char = string.sub(fmt, i, i)
local func = funcs[char]
assert(func)
func.f(self, select(idx, ...))
idx = idx + func.args
end
end
function format.writer(innerstream)
local writer = { inner = innerstream }
writer.position = 0
writer.bit_count = 0
writer.bit_buffer = 0
setmetatable(writer, Writer)
return writer
end
local Reader = { }
Reader.__index = Reader
function Reader:getposition()
return self.position
end
local function readraw(reader, count)
reader.position = reader.position + count
return reader.inner:read(count)
end
--Reads a string of length count from the input stream.
function Reader:raw(count)
self:discardbits()
return readraw(self, count)
end
--Reads a length prefixed stream of bytes from the input stream.
function Reader:stream()
local size = self:varint()
self.position = self.position + size
return self.inner:read(size)
end
--Reads a byte from the input stream.
function Reader:uint8()
self.position = self.position + 1
return sunpack("B", self.inner:read(1));
end
--Reads a number between [0 .. 0xffff] from the input stream.
function Reader:uint16()
self.position = self.position + 2
return sunpack("I2", self.inner:read(2))
end
--Reads a number between [0 .. 0xffffffff] from the input stream.
function Reader:uint32()
self.position = self.position + 4
return sunpack("I4", self.inner:read(4))
end
--Reads a number between [0 .. 0xffffffffffff] from the input stream.
function Reader:uint64()
self.position = self.position + 8
return sunpack("I8", self.inner:read(8))
end
--Reads a signed byte from the input stream.
function Reader:int8()
self.position = self.position + 1
return sunpack("b", self.inner:read(1));
end
--Reads a number between [-0x8000 .. 0x7fff] from the input stream.
function Reader:int16()
self.position = self.position + 2
return sunpack("i2", self.inner:read(2))
end
--Reads a number between [-0x80000000 .. 0x7fffffff] from the input stream.
function Reader:int32()
self.position = self.position + 4
return sunpack("i4", self.inner:read(4))
end
--Reads a number between [-0x8000000000000000 .. 0x7fffffffffffffff] from the input stream.
function Reader:int64()
self.position = self.position + 8
return sunpack("i8", self.inner:read(8))
end
--Reads a floting point number of precision 32-bit in
--IEEE 754 single precision format.
function Reader:float()
self.position = self.position + 4
return sunpack("f", self.inner:read(4))
end
--Reads a floting point number of precision 64-bit in
--IEEE 754 double precision format.
function Reader:double()
self.position = self.position + 8
return sunpack("d", self.inner:read(8))
end
--Reads a boolean value
function Reader:bool()
local value = self:byte()
if value == 1 then
value = true
else
value = false
end
return value
end
--Reads a variable integer encoded using the encoding
--employed by google protocol buffers.
function Reader:varint()
local number = 0;
local count = 0;
self:discardbits()
while true do
local byte = sunpack("B", readraw(self, 1))
number = number | ((byte & 0x7F) << (7 * count))
if byte < 0x80 then break end
count = count + 1;
if count == 10 then
--Something is wrong the data is corrupt.
error("stream is corrupt!");
end
end
return number;
end
--Reads a variable zigzag encoded integer encoded using the
-- zigzag encoding employed by google protocol buffers.
function Reader:varintzz()
local zigzaged = self:varint()
return (zigzaged >> 1) ~ (-(zigzaged & 1))
end
--Reads size bits from the stream.
function Reader:bits(size)
local count = self.bit_count
local bits = self.bit_buffer
local value = 0
local ready = 0
while count < size - ready do
value = value | (bits << ready)
ready = ready + count
bits = sunpack("B", readraw(self, 1))
count = 8
end
size = size - ready
self.bit_count = count - size
self.bit_buffer = bits >> size
return value | ((bits & ~(-1 << size)) << ready)
end
-- Reads an unsigned integer of size bits
function Reader:uint(size)
return self:bits(size)
end
-- Reads a signed integer of size bits
function Reader:int(size)
--We need to fix the value
local value = self:bits(size)
local sign = (value >> (size - 1))
if sign == 1 then
local max = (1 << size)
value = -(max - value)
end
return value
end
--Alignes the stream to the specified byte size
function Reader:align(to)
local pos = self.position
local aligned = pos + (to - 1) & ~(to - 1)
if extra ~= 0 then
local toremove = aligned - pos
self:raw(toremove)
end
end
--Discards any remaining bits in the bitbuffer.
function Reader:discardbits()
self.bit_count = 0
self.bit_buffer = 0
end
local funcs =
{
b = { f = Reader.int8, args = 0 },
h = { f = Reader.int16, args = 0 },
i = { f = Reader.int32, args = 0 },
l = { f = Reader.int64, args = 0 },
B = { f = Reader.uint8, args = 0 },
H = { f = Reader.uint16, args = 0 },
I = { f = Reader.uint32, args = 0 },
L = { f = Reader.uint64, args = 0 },
f = { f = Reader.float, args = 0 },
d = { f = Reader.double, args = 0 },
s = { f = Reader.stream, args = 0 },
r = { f = Reader.raw, args = 1 },
v = { f = Reader.varintzz, args = 0 },
V = { f = Reader.varint, args = 0 },
p = { f = Reader.int, args = 1 },
P = { f = Reader.uint, args = 1 }
}
local sub = string.sub
local readf_storage = { }
function Reader:readf(fmt, ...)
local idx = 1
for i=1, #fmt do
local char = sub(fmt, i, i)
local func = funcs[char]
assert(func)
readf_storage[i] = func.f(self, select(idx, ...))
idx = idx + func.args
end
return tunpack(readf_storage, 1, #fmt)
end
function format.reader(innerstream)
local reader = { inner = innerstream }
reader.position = 0
reader.bit_count = 0
reader.bit_buffer = 0
setmetatable(reader, Reader)
return reader
end
--Packs a number into a string using the varint format.
function format.packvarint(number)
local s = ""
while number >= 0x80 or number < 0 do
local byte = (number | 0x80) & 0x00000000000000FF
number = number >> 7;
s = s .. spack("B", byte)
end
s = s .. spack("B", number)
return s;
end
--Unpacks a number from a string with the varint format.
function format.unpackvarint(str)
local number = 0;
local count = 0;
while true do
local byte = string.byte(str, count + 1)
number = number | ((byte & 0x7F) << (7 * count))
if byte < 0x80 then break end
count = count + 1;
if count == 10 then
--Something is wrong the data is corrupt.
error("stream is corrupt!");
end
end
return number, count + 1
end
local MemoryOutStream = {}
MemoryOutStream.__index = MemoryOutStream
local insert = table.insert
function MemoryOutStream:write(string)
insert(self.buffer, string)
end
function MemoryOutStream:flush() end
function MemoryOutStream:close() end
local concat = table.concat
function MemoryOutStream:getdata()
return concat(self.buffer)
end
function format.outmemorystream()
return setmetatable( { buffer = { } }, MemoryOutStream )
end
local MemoryInStream = { }
MemoryInStream.__index = MemoryInStream
function MemoryInStream:read(count)
local pos = self.position
self.position = pos + count
return string.sub(self.buffer, pos, self.position - 1)
end
function MemoryInStream:close() end
function format.inmemorystream(buffer)
local stream = setmetatable({}, MemoryInStream)
stream.buffer = buffer
stream.position = 1
return stream
end
return format