forked from AngeloYazar/lua-lpack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack.lua
305 lines (276 loc) · 7.19 KB
/
pack.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
--[[/*
* lpack.c
* a Lua library for packing and unpacking binary data
* Luiz Henrique de Figueiredo <[email protected]>
* 29 Jun 2007 19:27:20
* This code is hereby placed in the public domain.
* with contributions from Ignacio Castaño <[email protected]> and
* Roberto Ierusalimschy <[email protected]>.
*/
-- Conversion from C to lua by Angelo Yazar, 2013.
]]
local ffi = require "ffi"
local bit = require "bit"
local C = ffi.C
local tonumber = tonumber
local string = string
local assert = assert
ffi.cdef [[
int isdigit( int ch );
]]
local OP_ZSTRING = 'z' --/* zero-terminated string */
local OP_BSTRING = 'p' --/* string preceded by length byte */
local OP_WSTRING = 'P' --/* string preceded by length word */
local OP_SSTRING = 'a' --/* string preceded by length size_t */
local OP_STRING = 'A' --/* string */
local OP_FLOAT = 'f' --/* float */
local OP_DOUBLE = 'd' --/* double */
local OP_NUMBER = 'n' --/* Lua number */
local OP_CHAR = 'c' --/* char */
local OP_BYTE = 'b' --/* byte = unsigned char */
local OP_SHORT = 'h' --/* short */
local OP_USHORT = 'H' --/* unsigned short */
local OP_INT = 'i' --/* int */
local OP_UINT = 'I' --/* unsigned int */
local OP_LONG = 'l' --/* long */
local OP_ULONG = 'L' --/* unsigned long */
local OP_LITTLEENDIAN = '<' --/* little endian */
local OP_BIGENDIAN = '>' --/* big endian */
local OP_NATIVE = '=' --/* native endian */
local OP_NONE = function() end
function badcode(c)
assert( false, "bad character code: '" .. tostring(c) .. "'" )
end
local function isLittleEndian()
local x = ffi.new("short[1]", 0x1001)
local e = tonumber( ( ffi.new("char[1]", x[0]) )[0] )
if e == 1 then
return true
end
return false
end
function doendian(c)
local e = isLittleEndian()
if c == OP_LITTLEENDIAN then
return not e
elseif c == OP_BIGENDIAN then
return e
elseif c == OP_NATIVE then
return false
end
return false
end
function doswap(swap, a, T)
if T == "byte" or T == "char" then
return a
end
if swap then
-- if T == "double" or T == "float" then
-- this part makes me unhappy --
a = ffi.new(T.."[1]",a)
local m = ffi.sizeof(T)
local str = ffi.string( a, m )
str = str:reverse()
ffi.copy(a, str, m)
return tonumber( a[0] )
--else
-- return bit.bswap( a )
--end
end
return a
end
function isdigit(c)
return C.isdigit( string.byte(c) ) == 1
end
function l_unpack(s,f,init)
local len = #s
local i = (init or 1)
local n = 1
local N = 0
local cur = OP_NONE
local swap = false
--lua_pushnil(L);
local values = {}
local function push( value )
values[n] = value
n = n + 1
end
local function done()
return i, unpack(values)
end
local endianOp = function(c)
swap = doendian(c)
-- N = 0 -- I don't think this is needed
end
local stringOp = function(c)
if i + N - 1 > len then
return done
end
push( s:sub(i,i+N - 1) )
i = i + N
N = 0
end
local zstringOp = function(c)
local l = 0
if i >= len then
return done
end
local substr = s:sub(i)
l = substr:find('\0')
push( substr:sub(0, l) )
i = i + l
end
function unpackNumber(T)
return function()
local m = ffi.sizeof(T)
if i + m - 1 > len then return done end
local a = ffi.new(T.."[1]")
ffi.copy(a, s:sub(i,i+m), m)
push( doswap(swap, tonumber(a[0]), T) )
i = i + m
end
end
function unpackString(T)
return function()
local m = ffi.sizeof(T)
if i + m > len then return done end
local l = ffi.new(T.."[1]")
ffi.copy(l, s:sub(i), m)
l = doswap(swap, tonumber(l[0]), T)
if i + m + l - 1 > len then return done end
i = i + m
push( s:sub(i,i+l-1) )
i = i + l
end
end
local unpack_ops = {
[OP_LITTLEENDIAN] = endianOp,
[OP_BIGENDIAN] = endianOp,
[OP_NATIVE] = endianOp,
[OP_ZSTRING] = zstringOp,
[OP_STRING] = stringOp,
[OP_BSTRING] = unpackString("unsigned char"),
[OP_WSTRING] = unpackString("unsigned short"),
[OP_SSTRING] = unpackString("size_t"),
[OP_NUMBER] = unpackNumber("double"),
[OP_DOUBLE] = unpackNumber("double"),
[OP_FLOAT] = unpackNumber("float"),
[OP_CHAR] = unpackNumber("char"),
[OP_BYTE] = unpackNumber("unsigned char"),
[OP_SHORT] = unpackNumber("short"),
[OP_USHORT] = unpackNumber("unsigned short"),
[OP_INT] = unpackNumber("int"),
[OP_UINT] = unpackNumber("unsigned int"),
[OP_LONG] = unpackNumber("long"),
[OP_ULONG] = unpackNumber("unsigned long"),
[OP_NONE] = OP_NONE,
[' '] = OP_NONE,
[','] = OP_NONE,
}
for c in (f..'\0'):gmatch'.' do
if not isdigit( c ) then
if cur == OP_STRING then
if N == 0 then push("") else
if stringOp(cur) == done then
return done()
end
end
else
if N == 0 then N = 1 end
for k=1,N do
if unpack_ops[cur] then
if unpack_ops[cur](cur) == done then
return done()
end
else
badcode(cur)
end
end
end
cur = c
N = 0
else N = 10*N+tonumber(c) end
end
return done()
end
function l_pack(f,...)
local args = {f,...}
local i = 1
local N = 0
local swap = false
local b = ""
local cur = OP_NONE
local pop = function()
i = i + 1
return args[i]
end
local endianOp = function(c)
swap = doendian(c)
-- N = 0 -- I don't think this is needed
end
local stringOp = function(c)
b = b .. pop()
if c == OP_ZSTRING then
b = b .. '\0'
end
end
function packNumber(T)
return function()
local a = pop()
a = doswap(swap, a, T)
a = ffi.new(T.."[1]",a)
b = b .. ffi.string( a, ffi.sizeof(T) )
end
end
function packString(T)
return function()
local a = pop()
local l = #a
ll = doswap(swap, l, T)
ll = ffi.new(T.."[1]",ll)
b = b .. ffi.string( ll, ffi.sizeof(T) )
b = b .. a
end
end
local pack_ops = {
[OP_LITTLEENDIAN] = endianOp,
[OP_BIGENDIAN] = endianOp,
[OP_NATIVE] = endianOp,
[OP_ZSTRING] = stringOp,
[OP_STRING] = stringOp,
[OP_BSTRING] = packString("unsigned char"),
[OP_WSTRING] = packString("unsigned short"),
[OP_SSTRING] = packString("size_t"),
[OP_NUMBER] = packNumber("double"),
[OP_DOUBLE] = packNumber("double"),
[OP_FLOAT] = packNumber("float"),
[OP_CHAR] = packNumber("char"),
[OP_BYTE] = packNumber("unsigned char"),
[OP_SHORT] = packNumber("short"),
[OP_USHORT] = packNumber("unsigned short"),
[OP_INT] = packNumber("int"),
[OP_UINT] = packNumber("unsigned int"),
[OP_LONG] = packNumber("long"),
[OP_ULONG] = packNumber("unsigned long"),
[OP_NONE] = OP_NONE,
[' '] = OP_NONE,
[','] = OP_NONE,
}
for c in (f..'\0'):gmatch'.' do
if not isdigit( c ) then
if N == 0 then N = 1 end
for k=1,N do
if pack_ops[cur] then
pack_ops[cur](cur)
else
badcode(cur)
end
end
cur = c
N = 0
else N = 10*N+tonumber(c) end
end
return b
end
string.pack = l_pack
string.unpack = l_unpack