-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteStream.lua
379 lines (328 loc) · 8.84 KB
/
ByteStream.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
local _G = _G
local pairs = pairs
local ipairs = ipairs
local remove = table.remove
local max = math.max
local type = type
local select = select
local next = next
local GetTime = GetTime
local char=string.char
local byte=string.byte
local Core=AVR
AVRByteStream={Embed = Core.Embed}
local T=AVRByteStream
--T.LibBase64=LibStub("LibBase64-1.0")
function T:New(store)
if self ~= T then return end
local s={}
store=store or {}
T:Embed(s)
-- s.maxBufferSize=384 -- preferably multiple of 3 for base64 encoding
s.maxBufferSize=1024
s.store=store
if not s.store.data then s.store.data={} end
if not s.store.buffer then s.store.buffer={} end
s.data=s.store.data
s.buffer=s.store.buffer
s.readDataPosition=1
s.readBufferPosition=1
s.readBufer=nil
return s
end
function T:IsEOS()
while self.readBuffer==nil or self.readBufferPosition>#self.readBuffer do
if self.readDataPosition>#self.data then return true end
-- self.readBuffer=T.LibBase64.Decode(self.data[self.readDataPosition])
self.readBuffer=self.data[self.readDataPosition]
self.readDataPosition=self.readDataPosition+1
self.readBufferPosition=1
end
return false
end
function T:ReadByte()
-- IsEOS also fills buffer if needed
if self:IsEOS() then return -1 end
local c=byte(self.readBuffer,self.readBufferPosition)
self.readBufferPosition=self.readBufferPosition+1
return c;
end
function T:FlushBuffer()
-- self.data[#self.data+1]=T.LibBase64.Encode(table.concat(self.buffer),4096)
self.data[#self.data+1]=table.concat(self.buffer)
self.store.buffer={}
self.buffer=self.store.buffer
end
-- Writes one byte. Value should be integer in [0,255]
function T:WriteByte(value)
self.buffer[#self.buffer+1]=char(value)
--self.buffer[#self.buffer+1]=format("%x",bit.band(value,255)) -- debugging
if #self.buffer>=self.maxBufferSize then
self:FlushBuffer()
end
end
-- Writes the value in specified amount of bytes. Most significant bits are written first.
-- If signed is true, sign bit is written before anything else.
function T:WriteBytes(value,size,signed)
while size > 0 do
if signed then
signed=false
local sign=0
if value<0 then
value=-value
sign=128
end
-- self:WriteByte(bit.band(bit.rshift(value,(size-1)*8),127)+sign)
self:WriteByte(bit.band(value/math.pow(256,size-1),127)+sign)
else
-- self:WriteByte(bit.band(bit.rshift(value,(size-1)*8),255))
self:WriteByte(bit.band(value/math.pow(256,size-1),255))
end
size=size-1
end
end
function T:ReadBytes(size,signed)
local value=0
local sign=1
while size > 0 do
local b=self:ReadByte()
if signed then
signed=false
if b>=128 then
value=b-128
sign=-1
else
value=b
end
else
value=value*256+b
end
size=size-1
end
return sign*value
end
-- Writes the value in an amount of bytes suitable for the value. The amount of consecutive
-- 1 bits in high end of first byte tells how many more bytes follow. After the first 0
-- rest of the bits in the first byte are the most significant bits of the actual value.
-- If signed is true then a sign bit is written before everything else, including the length
-- marker bits. The size parameter can be used to set a minimum number of bytes to be written.
function T:WriteVariableBytes(value,size,signed)
size=size or 1
local signBits=signed and 1 or 0
local signBit=0
if signed and value<0 then
value=-value
signBit=128
end
local extras=0
while math.pow(2,(size-1+extras)*8+7-extras-signBits) <= value do
extras=extras+1
if(extras==7-signBits) then
extras=8-signBits
break
end
end
local fullBytes=size-1+extras
local header=signBit
for i=0,extras-1,1 do
header=bit.bor(header,bit.lshift(1,7-i-signBits))
end
if extras<8 then
-- header=bit.bor(header,bit.rshift(value,fullBytes*8))
header=bit.bor(header,bit.band(value/math.pow(256,fullBytes),255))
end
self:WriteByte(bit.band(header,255))
if fullBytes>0 then self:WriteBytes(value,fullBytes) end
return 1+fullBytes
end
function T:ReadVariableBytes(size,signed)
size=size or 1
signBits=signed and 1 or 0
sign=1
local value=0
local b=self:ReadByte()
if signed and b>=128 then
sign=-1
end
local extras=0
while bit.band(bit.rshift(b,7-signBits-extras),1)==1 do
extras=extras+1
if extras==7-signBits then
extras=8-signBits
break
end
end
if extras<7-signBits then
value=bit.band(b,math.pow(2,7-extras-signBits)-1)*math.pow(256,size-1+extras)
end
value=sign*(value+self:ReadBytes(size-1+extras))
return value
end
local m,e
function T:WriteDouble(value)
-- ieee 754 binary64
-- 66665555 55555544 44444444 33333333 33222222 22221111 111111
-- 32109876 54321098 76543210 98765432 10987654 32109876 54321098 76543210
-- seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
if value==0.0 then
self:WriteBytes(0,8)
return
end
signBit=0
if value<0 then
signBit=128 -- shifted left to appropriate position
value=-value
end
m,e=math.frexp(value)
m=m*2-1 -- m in [0.5,1.0), multiply by 2 will get it to [1.0,2.0) giving the implicit first bit in mantissa, -1 to get rid of that
e=e-1+1023 -- adjust for the *2 on previous line and 1023 is the exponent zero offset
-- sign and 7 bits of exponent
self:WriteByte(bit.bor(signBit,bit.band(bit.rshift(e,4),127)))
-- first 4 bits of mantissa
m=m*16
-- write last 4 bits of exponent and first 4 of mantissa
self:WriteByte(bit.bor(bit.band(bit.lshift(e,4),255),bit.band(m,15)))
-- get rid of written bits and shift for next 8
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
-- repeat for rest of mantissa
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
end
function T:ReadDouble()
local b=self:ReadByte()
local sign=1
if b>=128 then
sign=-1
b=b-128
end
local exponent=b*16
b=self:ReadByte()
exponent=exponent+bit.band(bit.rshift(b,4),15)-1023
local mantissa=bit.band(b,15)/16
b=self:ReadByte()
mantissa=mantissa+b/16/256
b=self:ReadByte()
mantissa=mantissa+b/16/65536
b=self:ReadByte()
mantissa=mantissa+b/16/65536/256
b=self:ReadByte()
mantissa=mantissa+b/16/65536/65536
b=self:ReadByte()
mantissa=mantissa+b/16/65536/65536/256
b=self:ReadByte()
mantissa=mantissa+b/16/65536/65536/65536
if mantissa==0.0 and exponent==-1023 then return 0.0
else return (mantissa+1.0)*math.pow(2,exponent)*sign end
end
function T:WriteFloat(value)
-- ieee 754 binary32
-- 33222222 22221111 111111
-- 10987654 32109876 54321098 76543210
-- seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
if value==0.0 then
self:WriteBytes(0,4)
return
end
signBit=0
if value<0 then
signBit=128 -- shifted left to appropriate position
value=-value
end
m,e=math.frexp(value)
m=m*2-1
e=e-1+127
e=min(max(0,e),255)
-- sign and 7 bits of exponent
self:WriteByte(bit.bor(signBit,bit.band(bit.rshift(e,1),127)))
-- first 7 bits of mantissa
m=m*128
-- write last bit of exponent and first 7 of mantissa
self:WriteByte(bit.bor(bit.band(bit.lshift(e,7),255),bit.band(m,127)))
-- get rid of written bits and shift for next 8
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
end
function T:ReadFloat()
local b=self:ReadByte()
local sign=1
if b>=128 then
sign=-1
b=b-128
end
local exponent=b*2
b=self:ReadByte()
exponent=exponent+bit.band(bit.rshift(b,7),1)-127
local mantissa=bit.band(b,127)/128
b=self:ReadByte()
mantissa=mantissa+b/128/256
b=self:ReadByte()
mantissa=mantissa+b/128/65536
if mantissa==0.0 and exponent==-127 then return 0.0
else return (mantissa+1.0)*math.pow(2,exponent)*sign end
end
function T:WriteHalf(value)
-- ieee 754 binary16
-- 111111
-- 54321098 76543210
-- seeeeemm mmmmmmmm
if value==0.0 then
self:WriteBytes(0,2)
return
end
signBit=0
if value<0 then
signBit=128 -- shifted left to appropriate position
value=-value
end
m,e=math.frexp(value)
m=m*2-1
e=e-1+15
e=min(max(0,e),31)
m=m*4
-- sign, 5 bits of exponent, 2 bits of mantissa
self:WriteByte(bit.bor(signBit,bit.band(e,31)*4,bit.band(m,3)))
-- get rid of written bits and shift for next 8
m=(m-math.floor(m))*256
self:WriteByte(bit.band(m,255))
end
function T:ReadHalf()
local b=self:ReadByte()
local sign=1
if b>=128 then
sign=-1
b=b-128
end
local exponent=bit.rshift(b,2)-15
local mantissa=bit.band(b,3)/4
b=self:ReadByte()
mantissa=mantissa+b/4/256
if mantissa==0.0 and exponent==-15 then return 0.0
else return (mantissa+1.0)*math.pow(2,exponent)*sign end
end
function T:WriteString(value)
self:WriteVariableBytes(#value)
for i=1,#value,1 do
self:WriteByte(string.byte(value,i))
end
end
function T:ReadString()
local length=self:ReadVariableBytes()
local chars={}
local b
for i=1,length do
b=self:ReadByte()
chars:insert(string.char(b))
end
return chars:concat()
end