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
|
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
/*
Description of the 'format' field.
- (...) are comments.
- whitespace is ignored.
- numbers are numeric arguments, noted as "*" below. unless otherwise stated,
these are the number of repetitions of the next command. "4 i" == "iiii"
- native types:
"f" float
"F" double
"n" lua_Integer
"N" lua_Number
"h"/"H" char (uppercase = unsigned)
"w"/"W" short
"i"/"I" int
"l"/"L" long
"a"/"A" int1_t
"b"/"B" int2_t
"c"/"C" int4_t
"d"/"D" int8_t
- other data: (not aligned)
"x" padding (1-byte; ignore when unpacking, fill with zeroes when packing)
"*s" string of given length (when packing, arg is optional)
"z"/"0z" zero-terminated string
"*z" zero-terminated string of given length, but with padded zeroes
(arg is the exact number of bytes that will be read/written)
(when packing, if arg < length, write only arg bytes)
"St" (where t is some native integer type, e.g. "SC")
the integer, and a string of that length
- special flags:
">" big endian
"<" little endian
"=" native endian
"*!" alignment (when without n, use native) (currently can't be turned off)
(n is the maximum alignment. for example, 4! is Linux-like alignment where
even 8-byte doubles are 4-byte aligned)
-----------------
API:
string.pack([outptr, [stopptr|maxlen]], fmt, {data}|data...) -> outptr ? none : outstr
string.unpack(instr|inptr, [stopptr|maxlen], fmt, [bool rettable], [offset from 0]) ->
rettable ? {data},newpos : data...,newpos
the rest was moved to SDL itself:
rw:read(num) (num=nil means read all) -> outstr
rw:write(str, [num]) (num=nil means whole str) -> true (or nil,error,nwrote)
rw:seek(["end"|"set"|"cur"], [offset]) -> none
SDL.RWFromNewMem(len) -> rw
SDL.RWFromMem(ptr, len) -> rw
rw:baseptr() -> ptr
rw:hereptr() -> ptr
rw:disown()
SDL.IMG_Load_RW(rw, freesrc)
SDL.LoadWAV_RW(rw, freesrc)
*/
// how many bytes is 'double' is aligned to
struct dummyalign { char c; double d; };
#define MAXALIGN (sizeof(struct dummyalign) - sizeof(double))
// FIXME: in the original struct.c from ~roberto, this is used instead:
// #define NEW_MAXALIGN (MAXALIGN > sizeof(int) ? MAXALIGN : sizeof(int))
// why? is the current approach invalid? IMHO, if MAXALIGN < sizeof(int),
// the platform probably doesn't align at all, and MAXALIGN is correct.
struct PackState {
int swap;
int align; // max alignment for the packed data
int usestring;
char token;
int num_arg;
const char *p_format;
lua_State *L;
};
static void doswap(char *a, size_t n) {
int i, j;
for (i=0, j=n-1, n=n/2; n--; i++, j--) {
char t = a[i]; a[i] = a[j]; a[j] = t;
}
}
static int doalign(int offset, int align, int maxalign) {
if(align > maxalign) align = maxalign;
return (align - (offset % align)) % align;
}
static void get_next_token(struct PackState *st) {
char input;
st->usestring = 0;
if(st->token == 's' || st->token == 'z') st->token = 0, st->num_arg = 0;
if(st->num_arg && st->token) {
st->num_arg--;
return;
}
while(isspace(*st->p_format) || *st->p_format == '(') {
if(*st->p_format == '(') {
while(*st->p_format != ')' && *st->p_format != '\0') st->p_format++;
if(*st->p_format == '\0') luaL_error(st->L, "comment not terminated");
}
st->p_format++;
}
if(*st->p_format == '\0') {
if(st->num_arg) luaL_error(st->L, "trailing number at end of format");
st->token = -1; return; // end of format string
}
if(*st->p_format >= '0' && *st->p_format <= '9') {
st->token = 0;
st->num_arg = *st->p_format - '0';
st->p_format++;
while(*st->p_format >= '0' && *st->p_format <= '9') {
st->num_arg = 10*st->num_arg + *st->p_format-'0';
st->p_format++;
}
get_next_token(st); return;
}
input = *st->p_format;
st->p_format++;
if(strchr("S=<>", input) && st->num_arg != 0)
luaL_error(st->L, "unexpected number before '%c'", input);
if(input == '!') { // used alignment
st->align = (st->num_arg ? st->num_arg : MAXALIGN);
st->num_arg = 0;
get_next_token(st); return;
}
if(input == '=') { // native endian, don't swap
st->swap = 0;
get_next_token(st); return;
}
if(input == '<') { // little-endian packed
int arch_is_le = 1;
st->swap = !(*(char *)&arch_is_le);
get_next_token(st); return;
}
if(input == '>') { // big-endian packed
int arch_is_le = 1;
st->swap = (*(char *)&arch_is_le);
get_next_token(st); return;
}
if(input == 'S') { // string with prepended packed length
get_next_token(st);
if(!strchr("hHwWiIlLaAbBcCdD", st->token))
luaL_error(st->L, "expected integer type after 'S'");
if(st->num_arg)
luaL_error(st->L, "unexpected number after 'S'");
st->usestring = 1;
return;
}
if(strchr("zxsfFnNhHwWiIlLaAbBcCdD", input)) { // supported chars
if(st->num_arg != 0 && input != 's' && input != 'z')
st->num_arg--;
st->token = input;
return;
}
luaL_error(st->L, "unexpected character '%c'", input);
}
#define UNPACK(OP,T) \
case OP: { \
T val; int size = sizeof(val); \
if(st.align && !st.usestring) \
pos += doalign(pos, size, st.align); \
if(maxlen != -1 && pos+size > maxlen) \
luaL_error(L, "unexpected end of input at '%c'", st.token); \
memcpy(&val, data+pos, size); \
pos += size; \
if(st.swap) doswap((char *)&val, size); \
if(st.usestring) { \
if(maxlen != -1 && pos+val > maxlen) \
luaL_error(L, "unexpected end of input in 'S' string"); \
lua_pushlstring(L, data+pos, val); \
pos += val; \
} \
else \
lua_pushnumber(L, (lua_Number)val); \
break; \
}
static int str_unpack(lua_State *L) {
const char *data = NULL;
int offset = 0, pos = 0, fmtarg = 2, intable = 0;
size_t maxlen = (size_t)-1;
struct PackState st;
memset(&st, 0, sizeof st);
st.L = L;
if(lua_type(L, 1) == LUA_TLIGHTUSERDATA)
data = lua_touserdata(L, 1);
else
data = luaL_checklstring(L, 1, &maxlen);
if(lua_type(L, 1) == LUA_TLIGHTUSERDATA &&
lua_type(L, 2) == LUA_TLIGHTUSERDATA) {
const char *stop = lua_touserdata(L, 2);
if(stop < data) luaL_error(L, "stop pointer is sooner than start pointer");
maxlen = stop - data; fmtarg++;
}
else if(lua_isnumber(L, 2)) {
maxlen = luaL_checkinteger(L, 2); fmtarg++;
}
st.p_format = luaL_checkstring(L, fmtarg);
if(lua_isboolean(L, fmtarg+1) && lua_toboolean(L, fmtarg+1)) {
intable = 1; fmtarg++;
}
if(lua_isnumber(L, fmtarg+1)) {
offset = luaL_checkinteger(L, fmtarg+1);
if(maxlen != -1 && offset > maxlen)
luaL_error(L, "unexpected end of input at offset");
// offset isn't just an increment to pos because alignment is computed
// against pos. this way, pos is always 0 at the beginning.
data += offset;
if(maxlen != -1) maxlen -= offset;
}
lua_settop(L, 0);
if(intable)
lua_newtable(L);
while(1) {
get_next_token(&st);
if(st.token == -1) return lua_pushinteger(L, pos+offset), lua_gettop(L);
if(st.token != 'x')
luaL_checkstack(L, 1, "too many results");
switch(st.token) {
case 'x':
if(maxlen != -1 && pos+1 > maxlen)
luaL_error(L, "unexpected end of input at '%c'", st.token);
pos++;
break;
case 'z':
if(st.num_arg == 0) {
while((maxlen == -1 || pos+st.num_arg < maxlen) &&
data[pos+st.num_arg] != 0)
st.num_arg++;
st.num_arg++; // the "\0" itself
}
// no break
case 's':
if(maxlen != -1 && pos+st.num_arg > maxlen)
luaL_error(L, "unexpected end of input at '%c'", st.token);
if(st.token == 'z')
lua_pushstring(L, data+pos);
else
lua_pushlstring(L, data+pos, st.num_arg);
pos += st.num_arg;
break;
UNPACK('f', float)
UNPACK('F', double)
UNPACK('n', lua_Integer)
UNPACK('N', lua_Number)
UNPACK('h', char)
UNPACK('H', unsigned char)
UNPACK('w', short)
UNPACK('W', unsigned short)
UNPACK('i', int)
UNPACK('I', unsigned int)
UNPACK('l', long)
UNPACK('L', unsigned long)
UNPACK('a', int8_t)
UNPACK('A', uint8_t)
UNPACK('b', int16_t)
UNPACK('B', uint16_t)
UNPACK('c', int32_t)
UNPACK('C', uint32_t)
UNPACK('d', int64_t)
UNPACK('D', uint64_t)
}
if(st.token != 'x' && intable) {
lua_rawseti(L, 1, intable);
intable++;
}
}
}
#define OUTPUT(str, size) do { \
if(out) { \
if(maxlen != -1 && pos+size > maxlen) \
luaL_error(L, "not enough space in output at '%c'", st.token); \
memcpy(out+pos, str, size); \
} \
else \
luaL_addlstring(&b, (const char *)str, size); \
pos += size; \
} while(0)
#define PACK(OP,T) \
case OP: { \
T val; int size = sizeof(val); const char *usedstr = NULL; \
if(intable) lua_rawgeti(L, intable, arg); \
if(st.usestring) { \
usedstr = luaL_checklstring(L, intable ? -1 : arg, &len); \
val = len; \
} \
else val = (T)luaL_checknumber(L, intable ? -1 : arg); \
if(intable) lua_pop(L, 1); \
if(st.align && !st.usestring) { \
int myalign = doalign(pos, size, st.align); \
while(myalign--) OUTPUT("\0", 1); \
} \
if(st.swap) doswap((char *)&val, size); \
OUTPUT(&val, size); \
if(st.usestring) OUTPUT(usedstr, len); \
break; \
}
static int str_pack(lua_State *L) {
luaL_Buffer b;
char *out = NULL; int pos = 0, maxlen = -1;
int intable = 0, arg = 1;
size_t len;
struct PackState st;
memset(&st, 0, sizeof st);
st.L = L;
if(lua_type(L, arg) == LUA_TLIGHTUSERDATA) { // outptr
out = lua_touserdata(L, arg); arg++;
}
if(lua_type(L, arg) == LUA_TLIGHTUSERDATA) { // stopptr
maxlen = (char *)lua_touserdata(L, arg) - out; arg++;
}
else if(lua_type(L, arg) == LUA_TNUMBER) {
maxlen = luaL_checkinteger(L, arg); arg++;
}
if(!out) {
lua_pushnil(L); // mark to separate arguments from the string buffer
luaL_buffinit(L, &b);
}
st.p_format = luaL_checkstring(L, arg);
if(lua_type(L, arg+1) == LUA_TTABLE) {
intable = arg+1;
arg = 0;
}
while(1) {
get_next_token(&st);
if(st.token == -1) break;
if(st.token != 'x') arg++;
switch(st.token) {
case 'x':
OUTPUT("\0", 1);
break;
case 's': case 'z': {
const char *tmp;
if(intable) lua_rawgeti(L, intable, arg);
tmp = luaL_checklstring(L, intable ? -1 : arg, &len);
if(intable) lua_pop(L, 1);
if(st.num_arg != 0 && st.token == 's' && st.num_arg != len)
luaL_error(L, "incorrect string length for 's'");
if(st.num_arg != 0 && st.num_arg < len)
len = st.num_arg;
OUTPUT(tmp, len);
if(st.token == 'z' && st.num_arg == 0)
st.num_arg = len + 1; // add one "\0"
while(st.num_arg > len) {
OUTPUT("\0", 1);
len++;
}
break;
}
PACK('f', float)
PACK('F', double)
PACK('n', lua_Integer)
PACK('N', lua_Number)
PACK('h', char)
PACK('H', unsigned char)
PACK('w', short)
PACK('W', unsigned short)
PACK('i', int)
PACK('I', unsigned int)
PACK('l', long)
PACK('L', unsigned long)
PACK('a', int8_t)
PACK('A', uint8_t)
PACK('b', int16_t)
PACK('B', uint16_t)
PACK('c', int32_t)
PACK('C', uint32_t)
PACK('d', int64_t)
PACK('D', uint64_t)
}
}
if(out) return 0;
luaL_pushresult(&b);
return 1;
}
int luaopen_buf(lua_State *L) {
const luaL_Reg reg_buf[] = {
{ "pack", str_pack },
{ "unpack", str_unpack },
{ NULL, NULL }
};
lua_settop(L, 0);
luaL_register(L, LUA_STRLIBNAME, reg_buf);
return 1;
}
|