-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathByteArray.lua
More file actions
executable file
·413 lines (354 loc) · 10.3 KB
/
ByteArray.lua
File metadata and controls
executable file
·413 lines (354 loc) · 10.3 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
--[[
For quick-cocos2d-x
Serialzation bytes stream like ActionScript flash.utils.ByteArray.
It depends on lpack
@see http://underpop.free.fr/l/lua/lpack/
@see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html
@author zrong(zengrong.net)
Creation: 2013-11-14
]]
local ByteArray = class("ByteArray")
ByteArray.ENDIAN_LITTLE = "ENDIAN_LITTLE"
ByteArray.ENDIAN_BIG = "ENDIAN_BIG"
ByteArray.radix = {[10]="%03u",[8]="%03o",[16]="%02X"}
require("pack")
--- Return a string to display.
-- If self is ByteArray, read string from self.
-- Else, treat self as byte string.
-- @param __radix radix of display, value is 8, 10 or 16, default is 10.
-- @param __separator default is " ".
-- @return string, number
function ByteArray.toString(self, __radix, __separator)
__radix = __radix or 16
__radix = ByteArray.radix[__radix] or "%02X"
__separator = __separator or " "
local __fmt = __radix..__separator
local __format = function(__s)
return string.format(__fmt, string.byte(__s))
end
if type(self) == "string" then
return string.gsub(self, "(.)", __format)
end
local __bytes = {}
for i=1,#self._buf do
__bytes[i] = __format(self._buf[i])
end
return table.concat(__bytes) ,#__bytes
end
function ByteArray:ctor(__endian)
self._endian = __endian
self._buf = {}
self._pos = 1
end
function ByteArray:getLen()
return #self._buf
end
function ByteArray:getAvailable()
return #self._buf - self._pos + 1
end
function ByteArray:getPos()
return self._pos
end
function ByteArray:setPos(__pos)
self._pos = __pos
return self
end
function ByteArray:getEndian()
return self._endian
end
function ByteArray:setEndian(__endian)
self._endian = __endian
end
--- Get all byte array as a lua string.
-- Do not update position.
function ByteArray:getBytes(__offset, __length)
__offset = __offset or 1
__length = __length or #self._buf
--printf("getBytes,offset:%u, length:%u", __offset, __length)
return table.concat(self._buf, "", __offset, __length)
end
--- Get pack style string by lpack.
-- The result use ByteArray.getBytes to get is unavailable for lua socket.
-- E.g. the #self:_buf is 18, but #ByteArray.getBytes is 63.
-- I think the cause is the table.concat treat every item in ByteArray._buf as a general string, not a char.
-- So, I use lpack repackage the ByteArray._buf, theretofore, I must convert them to a byte number.
function ByteArray:getPack(__offset, __length)
__offset = __offset or 1
__length = __length or #self._buf
local __t = {}
for i=__offset,__length do
__t[#__t+1] = string.byte(self._buf[i])
end
local __fmt = self:_getLC("b"..#__t)
--print("fmt:", __fmt)
local __s = string.pack(__fmt, unpack(__t))
return __s
end
function ByteArray:rawPack(__fmt, ...)
local __s = string.pack(self:_getLC(__fmt), ...)
self:writeBuf(__s)
return self
end
--- rawUnPack perform like lpack.unpack, but it is only supported FORMAT parameter.
-- Because ByteArray include a position itself, so we haven't to save another.
function ByteArray:rawUnPack(__fmt)
-- read all of bytes.
local __s = self:getBytes(self._pos)
local __next, __val = string.unpack(__s, __fmt)
-- update position of the ByteArray
self._pos = self._pos + __next
return __val, __next
end
function ByteArray:readBool()
-- When char > 256, the readByte method will show an error.
-- So, we have to use readChar
return self:readChar() ~= 0
end
function ByteArray:writeBool(__bool)
if __bool then
self:writeByte(1)
else
self:writeByte(0)
end
return self
end
function ByteArray:readDouble()
local __, __v = string.unpack(self:readBuf(8), self:_getLC("d"))
return __v
end
function ByteArray:writeDouble(__double)
local __s = string.pack( self:_getLC("d"), __double)
self:writeBuf(__s)
return self
end
function ByteArray:readFloat()
local __, __v = string.unpack(self:readBuf(4), self:_getLC("f"))
return __v
end
function ByteArray:writeFloat(__float)
local __s = string.pack( self:_getLC("f"), __float)
self:writeBuf(__s)
return self
end
function ByteArray:readInt()
local __, __v = string.unpack(self:readBuf(4), self:_getLC("i"))
return __v
end
function ByteArray:writeInt(__int)
local __s = string.pack( self:_getLC("i"), __int)
self:writeBuf(__s)
return self
end
function ByteArray:readUInt()
local __, __v = string.unpack(self:readBuf(4), self:_getLC("I"))
return __v
end
function ByteArray:writeUInt(__uint)
local __s = string.pack(self:_getLC("I"), __uint)
self:writeBuf(__s)
return self
end
function ByteArray:readShort()
local __, __v = string.unpack(self:readBuf(2), self:_getLC("h"))
return __v
end
function ByteArray:writeShort(__short)
local __s = string.pack( self:_getLC("h"), __short)
self:writeBuf(__s)
return self
end
function ByteArray:readUShort()
local __, __v = string.unpack(self:readBuf(2), self:_getLC("H"))
return __v
end
function ByteArray:writeUShort(__ushort)
local __s = string.pack(self:_getLC("H"), __ushort)
self:writeBuf(__s)
return self
end
function ByteArray:readLong()
local __, __v = string.unpack(self:readBuf(8), self:_getLC("l"))
return __v
end
function ByteArray:writeLong(__long)
local __s = string.pack( self:_getLC("l"), __long)
self:writeBuf(__s)
return self
end
function ByteArray:readULong()
local __, __v = string.unpack(self:readBuf(4), self:_getLC("L"))
return __v
end
function ByteArray:writeULong(__ulong)
local __s = string.pack( self:_getLC("L"), __ulong)
self:writeBuf(__s)
return self
end
function ByteArray:readUByte()
local __, __val = string.unpack(self:readRawByte(), "b")
return __val
end
function ByteArray:writeUByte(__ubyte)
local __s = string.pack("b", __ubyte)
self:writeBuf(__s)
return self
end
function ByteArray:readLuaNumber(__number)
local __, __v = string.unpack(self:readBuf(8), self:_getLC("n"))
return __v
end
function ByteArray:writeLuaNumber(__number)
local __s = string.pack(self:_getLC("n"), __number)
self:writeBuf(__s)
return self
end
function ByteArray:readStringBytes(__len)
assert(__len, "Need a length of the string!")
if __len == 0 then return "" end
self:_checkAvailable()
local __, __v = string.unpack(self:readBuf(__len), self:_getLC("A"..__len))
return __v
end
function ByteArray:writeStringBytes(__string)
local __s = string.pack(self:_getLC("A"), __string)
self:writeBuf(__s)
return self
end
--- DO NOT perform this method, it's inefficient.
-- Alternatively use readStringBytes.
function ByteArray:readString(__len)
assert(__len, "Need a length of the string!")
if __len == 0 then return "" end
self:_checkAvailable()
local __bytes = ""
for i=self._pos, #self._buf do
local __byte = string.byte(self._buf[i])
__bytes = __bytes .. string.char(__byte)
end
return __bytes
end
function ByteArray:writeString(__string)
self:writeBuf(__string)
return self
end
function ByteArray:readStringUInt()
self:_checkAvailable()
local __len = self:readUInt()
return self:readStringBytes(__len)
end
function ByteArray:writeStringUInt(__string)
local __s = string.pack(self:_getLC("a"), __string)
self:writeBuf(__s)
return self
end
function ByteArray:readStringUShort()
self:_checkAvailable()
local __len = self:readUShort()
return self:readStringBytes(__len)
end
function ByteArray:writeStringUShort(__string)
local __s = string.pack(self:_getLC("P"), __string)
self:writeBuf(__s)
return self
end
--- Read some bytes from buf
-- @return a bit string
function ByteArray:readBytes(__bytes, __offset, __length)
assert(iskindof(__bytes, "ByteArray"), "Need a ByteArray instance!")
local __selfLen = #self._buf
local __availableLen = __selfLen - self._pos
__offset = __offset or 1
if __offset > __selfLen then __offset = 1 end
__length = __length or 0
if __length == 0 or __length > __availableLen then __length = __availableLen end
__bytes:setPos(__offset)
for i=__offset,__offset+__length do
__bytes:writeRawByte(self:readRawByte())
end
end
--- Write some bytes into buf
function ByteArray:writeBytes(__bytes, __offset, __length)
assert(iskindof(__bytes, "ByteArray"), "Need a ByteArray instance!")
local __bytesLen = __bytes:getLen()
if __bytesLen == 0 then return end
__offset = __offset or 1
if __offset > __bytesLen then __offset = 1 end
local __availableLen = __bytesLen - __offset
__length = __length or __availableLen
if __length == 0 or __length > __availableLen then __length = __availableLen end
local __oldPos = __bytes:getPos()
__bytes:setPos(__offset)
for i=__offset,__offset+__length do
self:writeRawByte(__bytes:readRawByte())
end
__bytes:setPos(__oldPos)
return self
end
--- Actionscript3 readByte == lpack readChar
-- A signed char
function ByteArray:readChar()
local __, __val = string.unpack( self:readRawByte(), "c")
return __val
end
function ByteArray:writeChar(__char)
self:writeRawByte(string.pack("c", __char))
return self
end
--- Use the lua string library to get a byte
-- A unsigned char
function ByteArray:readByte()
return string.byte(self:readRawByte())
end
--- Use the lua string library to write a byte.
-- The byte is a number between 0 and 255, otherwise, the lua will get an error.
function ByteArray:writeByte(__byte)
self:writeRawByte(string.char(__byte))
return self
end
function ByteArray:readRawByte()
self:_checkAvailable()
local __byte = self._buf[self._pos]
self._pos = self._pos + 1
return __byte
end
function ByteArray:writeRawByte(__rawByte)
if self._pos > #self._buf+1 then
for i=#self._buf+1,self._pos-1 do
self._buf[i] = string.char(0)
end
end
self._buf[self._pos] = __rawByte
self._pos = self._pos + 1
return self
end
--- Read a byte array as string from current position, then update the position.
function ByteArray:readBuf(__len)
--printf("readBuf,len:%u, pos:%u", __len, self._pos)
local __ba = self:getBytes(self._pos, self._pos + __len - 1)
self._pos = self._pos + __len
return __ba
end
--- Write a encoded char array into buf
function ByteArray:writeBuf(__s)
for i=1,#__s do
self:writeRawByte(__s:sub(i))
end
return self
end
----------------------------------------
-- private
----------------------------------------
function ByteArray:_checkAvailable()
assert(#self._buf >= self._pos, string.format("End of file was encountered. pos: %d, len: %d.", self._pos, #self._buf))
end
--- Get Letter Code
function ByteArray:_getLC(__fmt)
__fmt = __fmt or ""
if self._endian == ByteArray.ENDIAN_LITTLE then
return "<"..__fmt
elseif self._endian == ByteArray.ENDIAN_BIG then
return ">"..__fmt
end
return "="..__fmt
end
return ByteArray