1 /******************************************************************************
2 * Copyright (C) 2010-2012 Lua.org, PUC-Rio. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 ******************************************************************************/
24 ** {======================================================
25 ** Library for packing/unpacking structures.
26 ** $Id: struct.c,v 1.4 2012/07/04 18:54:29 roberto Exp $
27 ** See Copyright Notice above.
29 ** Small changes were made by Hadriel Kaplan - those changes
30 ** are in the Public Domain.
32 ** Some changes are based on a patch to struct.h from
33 ** Flemming Madsen, from here:
34 ** http://lua-users.org/lists/lua-l/2009-10/msg00572.html
35 ** In particular, these changes from him:
36 ** -Can handle 'long long' integers (i8 / I8); though they're converted to doubles
37 ** -Can insert/specify padding anywhere in a struct. ('X' eg. when a string is following a union)
38 ** -Can report current offset in both pack and unpack ('=')
39 ** -Can mask out return values when you only want to calculate sizes or unmarshal pascal-style strings. '(' & ')'
42 ** -Added support for Int64/UIn64 being packed/unpacked, using 'e'/'E'
43 ** -Made it follow Wireshark's conventions so we could get API docs
44 ** =======================================================
51 ** x[num] - pad num bytes, default 1
52 ** X[num] - pad to num align, default MAXALIGN
54 ** Following are system-dependent sizes:
55 ** b/B - signed/unsigned byte
56 ** h/H - signed/unsigned short
57 ** i/I - signed/unsigned int
58 ** l/L - signed/unsigned long
63 ** Following are system-independent sizes:
64 ** in/In - signed/unsigned integer of size `n' bytes
65 Note: unpack of i/I is done to a Lua_number, typically a double,
66 so unpacking a 64-bit field (i8/I8) will lose precision.
67 Use e/E to unpack into a Wireshark Int64/UInt64 object/userdata instead.
68 ** e/E - signed/unsigned eight-byte Integer (64bits, long long), to/from Int64/UInt64 object
69 ** cn - sequence of `n' chars (from/to a string); when packing, n==0 means
70 the whole string; when unpacking, n==0 means use the previous
71 read number as the string length
72 ** s - zero-terminated string
74 ** '(' ')' - stop assigning items. ')' start assigning (padding when packing)
75 ** '=' - return current position / offset
91 /* WSLUA_MODULE Struct Binary encode/decode support */
93 /* TODO: figure out a way for wslua Module's to have (possibly long) description text */
95 /* The following line is here so that make-reg.pl does the right thing. This 'Struct' class
96 isn't really a class, so it doesn't have the checkStruct/pushStruct/etc. functions
97 the following macro would generate; but it does need to be registered and such.
98 WSLUA_CLASS_DEFINE_BASE(Struct,NOP,NOP,0);
101 /* basic integer type - yes this is system-specific size - it's meant to be */
102 #if !defined(STRUCT_INT)
103 #define STRUCT_INT long
106 typedef STRUCT_INT Inttype;
108 /* corresponding unsigned version */
109 typedef unsigned STRUCT_INT Uinttype;
111 /* maximum size (in bytes) for integral types */
112 #define MAXINTSIZE 32
114 /* is 'x' a power of 2? */
115 #define isp2(x) ((x) > 0 && ((x) & ((x) - 1)) == 0)
117 /* dummy structure to get padding/alignment requirements */
124 #define PADDING (sizeof(struct cD) - sizeof(gdouble))
125 #define MAXALIGN (PADDING > sizeof(int) ? PADDING : sizeof(int))
132 /* trick to determine native endianess of system */
136 } const native = {1};
139 typedef struct Header {
145 /* For options that take a number argument, gets the number */
146 static int getnum (const gchar **fmt, int df) {
147 if (!isdigit(**fmt)) /* no number? */
148 return df; /* return default value */
152 a = a*10 + *((*fmt)++) - '0';
153 } while (isdigit(**fmt));
159 #define defaultoptions(h) ((h)->endian = native.endian, (h)->align = 1, (h)->noassign = FALSE)
162 /* gets size (number of bytes) for a given type */
163 static size_t optsize (lua_State *L, gchar opt, const gchar **fmt) {
165 case 'B': case 'b': return sizeof(gchar);
166 case 'H': case 'h': return sizeof(gshort);
167 case 'L': case 'l': return sizeof(glong);
168 case 'E': case 'e': return sizeof(gint64);
169 case 'T': return sizeof(size_t);
170 case 'f': return sizeof(gfloat);
171 case 'd': return sizeof(gdouble);
172 case 'x': return getnum(fmt, 1);
173 case 'X': return getnum(fmt, MAXALIGN);
174 case 'c': return getnum(fmt, 1);
175 case 'i': case 'I': {
176 int sz = getnum(fmt, sizeof(int));
178 luaL_error(L, "integral size %d is larger than limit of %d",
186 return 0; /* these cases do not have a size */
188 const gchar *msg = lua_pushfstring(L, "invalid format option [%c]", opt);
189 return luaL_argerror(L, 1, msg);
196 ** return number of bytes needed to align an element of size 'size'
197 ** at current position 'len'
199 static int gettoalign (size_t len, Header *h, int opt, size_t size) {
200 if (size == 0 || opt == 'c' || opt == 's') return 0;
201 if (size > (size_t)h->align)
202 size = h->align; /* respect max. alignment */
203 return (int)((size - (len & (size - 1))) & (size - 1));
208 ** options to control endianess and alignment settings
210 static void controloptions (lua_State *L, int opt, const gchar **fmt,
213 case ' ': return; /* ignore white spaces */
214 case '>': h->endian = BIG; return;
215 case '<': h->endian = LITTLE; return;
216 case '(': h->noassign = TRUE; return;
217 case ')': h->noassign = FALSE; return;
219 int a = getnum(fmt, MAXALIGN);
221 luaL_error(L, "alignment %d is not a power of 2", a);
226 const char *msg = lua_pushfstring(L, "invalid format option '%c'", opt);
227 luaL_argerror(L, 1, msg);
232 /* Encodes a Lua number as an integer of given size and endiannes into a string struct */
233 static void putinteger (lua_State *L, luaL_Buffer *b, int arg, int endian,
235 lua_Number n = luaL_checknumber(L, arg);
236 /* this one's not system dependent size - it's a long long */
238 gchar buff[MAXINTSIZE];
240 value = (guint64)(gint64)n;
243 if (endian == LITTLE) {
245 for (i = 0; i < size; i++) {
246 buff[i] = (value & 0xff);
252 for (i = size - 1; i >= 0; i--) {
253 buff[i] = (value & 0xff);
257 luaL_addlstring(b, buff, size);
260 /* corrects endiannes - usually done by other functions themselves, but is
261 * used for float/doubles, since on some platforms they're endian'ed as well
263 static void correctbytes (gchar *b, int size, int endian) {
264 if (endian != native.endian) {
275 WSLUA_CONSTRUCTOR Struct_pack (lua_State *L) {
276 /* Returns a string containing the values arg1, arg2, etc. packed/encoded according to the format string. */
277 #define WSLUA_ARG_Struct_unpack_FORMAT 1 /* The format string */
278 #define WSLUA_ARG_Struct_unpack_STRUCT 2 /* One or more Lua value(s) to encode, based on the given format. */
280 const char *fmt = luaL_checkstring(L, WSLUA_ARG_Struct_unpack_FORMAT);
285 size_t totalsize = 0;
287 lua_pushnil(L); /* mark to separate arguments from string buffer */
288 luaL_buffinit(L, &b);
289 while (*fmt != '\0') {
291 size_t size = optsize(L, opt, &fmt);
292 int toalign = gettoalign(totalsize, &h, opt, size);
293 totalsize += toalign;
294 while (toalign-- > 0) luaL_addchar(&b, '\0');
295 if (opt == 'X') size = 0; /* 'X' is about alignment, not size */
296 if (h.noassign && size) opt = 'x'; /* for pack, "(i4)" is the same as "x4" */
298 case 'b': case 'B': case 'h': case 'H':
299 case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
300 putinteger(L, &b, arg++, h.endian, (int)size);
304 Int64_pack(L, &b, arg++, h.endian == LITTLE);
308 UInt64_pack(L, &b, arg++, h.endian == LITTLE);
311 case 'x': case 'X': {
314 luaL_addchar(&b, '\0');
318 gfloat f = (gfloat)luaL_checknumber(L, arg++);
319 correctbytes((gchar *)&f, (int)size, h.endian);
320 luaL_addlstring(&b, (gchar *)&f, size);
324 gdouble d = luaL_checknumber(L, arg++);
325 correctbytes((gchar *)&d, (int)size, h.endian);
326 luaL_addlstring(&b, (gchar *)&d, size);
329 case 'c': case 's': {
331 const gchar *s = luaL_checklstring(L, arg++, &l);
332 if (size == 0) size = l;
333 luaL_argcheck(L, l >= (size_t)size, arg, "string too short");
334 luaL_addlstring(&b, s, size);
336 luaL_addchar(&b, '\0'); /* add zero at the end */
342 if (poscnt < (int)(sizeof(posBuf)/sizeof(posBuf[0])))
343 posBuf[poscnt++] = (int)totalsize + 1;
346 default: controloptions(L, opt, &fmt, &h);
351 for (arg = 0; arg < poscnt; arg++)
352 lua_pushinteger(L, posBuf[arg]);
356 /* Decodes an integer from a string struct into a Lua number, based on
357 * given endianess and size. If the integer type is signed, this makes
358 * the Lua number be +/- correctly as well.
360 static lua_Number getinteger (const gchar *buff, int endian,
361 int issigned, int size) {
365 for (i = 0; i < size; i++) {
367 l |= (Uinttype)(guchar)buff[i];
371 for (i = size - 1; i >= 0; i--) {
373 l |= (Uinttype)(guchar)buff[i];
377 return (lua_Number)l;
378 else { /* signed format */
379 Uinttype mask = (Uinttype)(~((Uinttype)0)) << (size*8 - 1);
380 if (l & mask) /* negative value? */
381 l |= mask; /* signal extension */
382 return (lua_Number)(Inttype)l;
386 #define b_pushnumber(n) { if (!h.noassign) lua_pushnumber(L, (lua_Number)(n)); }
388 WSLUA_CONSTRUCTOR Struct_unpack (lua_State *L) {
389 /* Unpacks/decodes multiple Lua values from a given struct-like binary Lua string.
390 The number of returned values depends on the format given, plus an addtional value of the position where it stopped reading is returned. */
391 #define WSLUA_ARG_Struct_unpack_FORMAT 1 /* The format string */
392 #define WSLUA_ARG_Struct_unpack_STRUCT 2 /* The binary Lua string to unpack */
393 #define WSLUA_OPTARG_Struct_unpack_BEGIN 3 /* The position to begin reading from (default=1) */
395 const char *fmt = luaL_checkstring(L, WSLUA_ARG_Struct_unpack_FORMAT);
397 const char *data = luaL_checklstring(L, WSLUA_ARG_Struct_unpack_STRUCT, &ld);
398 size_t pos = luaL_optinteger(L, WSLUA_OPTARG_Struct_unpack_BEGIN, 1) - 1;
403 size_t size = optsize(L, opt, &fmt);
404 pos += gettoalign(pos, &h, opt, size);
405 luaL_argcheck(L, pos+size <= ld, 2, "data string too short");
407 if (opt == 'X') size = 0;
408 if (h.noassign && size > 0) {
409 /* if we're not assigning, and the opt type has a size, then loop again */
410 /* this will not be the case for controloptions, 'c0', 's', and '=' */
415 luaL_checkstack(L, 1, "too many results");
417 case 'b': case 'B': case 'h': case 'H':
418 case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
419 int issigned = islower(opt);
420 lua_Number res = getinteger(data+pos, h.endian, issigned, (int)size);
421 lua_pushnumber(L, res);
425 Int64_unpack(L, data+pos, h.endian == LITTLE);
429 UInt64_unpack(L, data+pos, h.endian == LITTLE);
432 case 'x': case 'X': {
437 memcpy(&f, data+pos, size);
438 correctbytes((gchar *)&f, sizeof(f), h.endian);
439 lua_pushnumber(L, f);
444 memcpy(&d, data+pos, size);
445 correctbytes((gchar *)&d, sizeof(d), h.endian);
446 lua_pushnumber(L, d);
451 if (!lua_isnumber(L, -1))
452 luaL_error(L, "format `c0' needs a previous size");
453 size = (guint32)lua_tonumber(L, -1);
455 luaL_argcheck(L, pos+size <= ld, 2, "data string too short");
458 lua_pushlstring(L, data+pos, size);
462 const gchar *e = (const char *)memchr(data+pos, '\0', ld - pos);
464 luaL_error(L, "unfinished string in data");
465 size = (e - (data+pos)) + 1;
467 lua_pushlstring(L, data+pos, size - 1);
471 lua_pushinteger(L, pos + 1);
474 default: controloptions(L, opt, &fmt, &h);
478 lua_pushinteger(L, pos + 1);
479 return lua_gettop(L) - 2;
483 WSLUA_CONSTRUCTOR Struct_size (lua_State *L) {
484 /* Returns the length of the binary string struct that would be consumed/handled by the given format string. */
486 const gchar *fmt = luaL_checkstring(L, 1);
491 size_t size = optsize(L, opt, &fmt);
492 pos += gettoalign(pos, &h, opt, size);
494 luaL_argerror(L, 1, "option 's' has no fixed size");
495 else if (opt == 'c' && size == 0)
496 luaL_argerror(L, 1, "option 'c0' has no fixed size");
498 controloptions(L, opt, &fmt, &h);
501 lua_pushinteger(L, pos);
505 /* }====================================================== */
507 /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
508 static int Struct__gc(lua_State* L _U_) {
512 WSLUA_METHODS Struct_methods[] = {
513 {"pack", Struct_pack},
514 {"unpack", Struct_unpack},
515 {"size", Struct_size},
519 WSLUA_META Struct_meta[] = {
523 LUALIB_API int Struct_register(lua_State* L) {
524 WSLUA_REGISTER_CLASS(Struct);