Adds support for Lua struct library so one can pack/unpack binary structures

This is based on Roberto Ierusalimschy's struct library, along with additional
options based on Flemming Madsen's patch to the lua-users mailing list, and
some changes I made to support 64-bit integer packing/unpacking.  Details
are in the top comments for wslua_struct.c.  This also includes a test script.

Change-Id: Ifcd0116ba013d5c760927721c8d6e9f28965534b
Reviewed-on: https://code.wireshark.org/review/98
Reviewed-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
Tested-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
This commit is contained in:
Hadriel Kaplan 2014-02-02 22:49:30 -05:00 committed by Alexis La Goutte
parent 7a503703ac
commit c4f1777a97
13 changed files with 915 additions and 11 deletions

View File

@ -310,6 +310,8 @@ set(WSLUA_MODULES
${CMAKE_SOURCE_DIR}/epan/wslua/wslua_tree.c
${CMAKE_SOURCE_DIR}/epan/wslua/wslua_tvb.c
${CMAKE_SOURCE_DIR}/epan/wslua/wslua_util.c
${CMAKE_SOURCE_DIR}/epan/wslua/wslua_int64.c
${CMAKE_SOURCE_DIR}/epan/wslua/wslua_struct.c
)
ADD_CUSTOM_COMMAND(

View File

@ -251,6 +251,7 @@ WSLUA_MODULES = \
../epan/wslua/wslua_pinfo.c \
../epan/wslua/wslua_proto.c \
../epan/wslua/wslua_int64.c \
../epan/wslua/wslua_struct.c \
../epan/wslua/wslua_tree.c \
../epan/wslua/wslua_tvb.c \
../epan/wslua/wslua_util.c

View File

@ -348,6 +348,7 @@ WSLua Reference Manual
<!ENTITY WsLuaTvb SYSTEM "wsluarm_src/wslua_tvb.xml">
<!ENTITY WsLuaUtility SYSTEM "wsluarm_src/wslua_util.xml">
<!ENTITY WsLuaInt64 SYSTEM "wsluarm_src/wslua_int64.xml">
<!ENTITY WsLuaStruct SYSTEM "wsluarm_src/wslua_struct.xml">
]>

View File

@ -178,4 +178,5 @@ end
&WsLuaTvb;
&WsLuaUtility;
&WsLuaInt64;
&WsLuaStruct;
</chapter>

View File

@ -34,6 +34,7 @@ set(WSLUA_MODULES
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_gui.c
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_util.c
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_field.c
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_struct.c
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_dumper.c
)

View File

@ -40,6 +40,7 @@ wslua_modules = \
$(srcdir)/wslua_gui.c \
$(srcdir)/wslua_util.c \
$(srcdir)/wslua_field.c \
$(srcdir)/wslua_struct.c \
$(srcdir)/wslua_dumper.c
libwslua_la_SOURCES = \
@ -129,7 +130,8 @@ checkapi:
wslua_int64.c \
wslua_pinfo.c \
wslua_proto.c \
wslua_listener.c \
wslua_struct.c \
wslua_tree.c \
wslua_tree.c \
wslua_tvb.c \
wslua_util.c

View File

@ -25,6 +25,7 @@ MODULES = \
wslua_gui.c \
wslua_util.c \
wslua_field.c \
wslua_struct.c \
wslua_dumper.c
OBJECTS= \
@ -39,6 +40,7 @@ OBJECTS= \
wslua_proto.obj \
wslua_listener.obj \
wslua_int64.obj \
wslua_struct.obj \
wslua_tree.obj \
wslua_tvb.obj \
wslua_util.obj

View File

@ -61,7 +61,7 @@ end
function typeof(obj)
local mt = getmetatable(obj)
return mt and mt.__typeof or type(obj)
return mt and mt.__typeof or obj.__typeof or type(obj)
end
function file_exists(name)

View File

@ -256,6 +256,7 @@ typedef tvbparse_action_t* Shortcut;
typedef struct _wslua_main* WireShark;
typedef struct _wslua_dir* Dir;
typedef struct _wslua_private_table* PrivateTable;
typedef gchar* Struct;
/*
* toXxx(L,idx) gets a Xxx from an index (Lua Error if fails)
@ -450,6 +451,11 @@ extern void lua_prime_all_fields(proto_tree* tree);
extern int Proto_commit(lua_State* L);
extern void Int64_pack(lua_State* L, luaL_Buffer *b, gint idx, gboolean asLittleEndian);
extern int Int64_unpack(lua_State* L, const gchar *buff, gboolean asLittleEndian);
extern void UInt64_pack(lua_State* L, luaL_Buffer *b, gint idx, gboolean asLittleEndian);
extern int UInt64_unpack(lua_State* L, const gchar *buff, gboolean asLittleEndian);
extern Tvb* push_Tvb(lua_State* L, tvbuff_t* tvb);
extern gboolean push_TvbRange(lua_State* L, tvbuff_t* tvb, int offset, int len);
extern void clear_outstanding_Tvb(void);

View File

@ -52,7 +52,7 @@ WSLUA_CLASS_DEFINE_BASE(Int64,NOP,NOP,0);
Lua uses one single number representation which can be chosen at compile time and since
it is often set to IEEE 754 double precision floating point, we cannot store a 64 bit integer
with full precision.
For details, see: http://lua-users.org/wiki/FloatingPoint
For details, see: http://wiki.wireshark.org/LuaAPI/Int64
*/
/* these declarations are here because some funcs in Int64 need to know about UInt64 */
@ -222,9 +222,10 @@ WSLUA_METHOD Int64_tonumber(lua_State* L) {
}
WSLUA_CONSTRUCTOR Int64_fromhex(lua_State* L) { /* Creates an Int64 object from the given hex string */
#define WSLUA_ARG_Int64_fromhex_HEX 1 /* The hex-ascii Lua string */
guint64 result = 0;
size_t len = 0;
const gchar *s = luaL_checklstring(L,1,&len);
const gchar *s = luaL_checklstring(L,WSLUA_ARG_Int64_fromhex_HEX,&len);
if (s && len > 0) {
sscanf(s, "%" G_GINT64_MODIFIER "x", &result);
@ -252,11 +253,11 @@ WSLUA_METHOD Int64_higher(lua_State* L) {
/* Returns a Lua number of the higher 32-bits of the Int64 value. (negative Int64 will return a negative Lua number) */
gint64 num = getInt64(L,1);
gint64 b = num;
lua_Number n;
lua_Number n = 0;
if (b < 0) b = -b; /* masking/shifting negative int64 isn't working on some platforms */
b &= G_GUINT64_CONSTANT(0x7FFFFFFF00000000);
b >>= 32;
n = (lua_Number)(guint32)(b & 0x00000000FFFFFFFFF);
n = (lua_Number)(guint32)(b & G_GUINT64_CONSTANT(0x00000000FFFFFFFFF));
if (num < 0) n = -n;
lua_pushnumber(L,n);
WSLUA_RETURN(1); /* The Lua number */
@ -515,7 +516,8 @@ LUALIB_API int Int64_register(lua_State* L) {
WSLUA_CLASS_DEFINE_BASE(UInt64,NOP,NOP,0);
/* UInt64 represents a 64 bit unsigned integer, similar to Int64. */
/* UInt64 represents a 64 bit unsigned integer, similar to Int64.
For details, see: http://wiki.wireshark.org/LuaAPI/Int64 */
/* A checkUInt64 but that also auto-converts numbers, strings, and Int64 to a guint64 */
static guint64 getUInt64(lua_State *L, int i)
@ -687,9 +689,10 @@ WSLUA_METAMETHOD UInt64__tostring(lua_State* L) {
}
WSLUA_CONSTRUCTOR UInt64_fromhex(lua_State* L) { /* Creates a UInt64 object from the given hex string */
#define WSLUA_ARG_UInt64_fromhex_HEX 1 /* The hex-ascii Lua string */
guint64 result = 0;
size_t len = 0;
const gchar *s = luaL_checklstring(L,1,&len);
const gchar *s = luaL_checklstring(L,WSLUA_ARG_UInt64_fromhex_HEX,&len);
if (s && len > 0) {
sscanf(s, "%" G_GINT64_MODIFIER "x", &result);
@ -700,9 +703,9 @@ WSLUA_CONSTRUCTOR UInt64_fromhex(lua_State* L) { /* Creates a UInt64 object from
WSLUA_METHOD UInt64_tohex(lua_State* L) {
/* Returns a hex string of the UInt64 value. */
#define WSLUA_OPTARG_Int64_new_NUMBYTES 2 /* The number of hex-chars/nibbles to generate, negative means uppercase (default=16) */
#define WSLUA_OPTARG_UInt64_new_NUMBYTES 2 /* The number of hex-chars/nibbles to generate, negative means uppercase (default=16) */
guint64 b = getUInt64(L,1);
gint n = luaL_optint(L, WSLUA_OPTARG_Int64_new_NUMBYTES, 16);
gint n = luaL_optint(L, WSLUA_OPTARG_UInt64_new_NUMBYTES, 16);
const gchar *hexdigits = "0123456789abcdef";
gchar buf[16];
gint i;
@ -717,7 +720,7 @@ WSLUA_METHOD UInt64_higher(lua_State* L) {
/* Returns a Lua number of the higher 32-bits of the UInt64 value. */
guint64 num = getUInt64(L,1);
guint64 b = num;
lua_Number n;
lua_Number n = 0;
b &= G_GUINT64_CONSTANT(0xFFFFFFFF00000000);
b >>= 32;
n = (lua_Number)(guint32)(b & G_GUINT64_CONSTANT(0x00000000FFFFFFFFF));

529
epan/wslua/wslua_struct.c Normal file
View File

@ -0,0 +1,529 @@
/*
** {======================================================
** Library for packing/unpacking structures.
** $Id: struct.c,v 1.4 2012/07/04 18:54:29 roberto Exp $
** See Copyright Notice at the end of this file
**
** Small changes were made by Hadriel Kaplan - those changes
** are in the Public Domain.
**
** Some changes are based on a patch to struct.h from
** Flemming Madsen, from here:
** http://lua-users.org/lists/lua-l/2009-10/msg00572.html
** In particular, these changes from him:
** -Can handle 'long long' integers (i8 / I8); though they're converted to doubles
** -Can insert/specify padding anywhere in a struct. ('X' eg. when a string is following a union)
** -Can report current offset in both pack and unpack ('=')
** -Can mask out return values when you only want to calculate sizes or unmarshal pascal-style strings. '(' & ')'
**
** Changes I made:
** -Added support for Int64/UIn64 being packed/unpacked, using 'e'/'E'
** -Made it follow Wireshark's conventions so we could get API docs
** =======================================================
*/
/*
** Valid formats:
** > - big endian
** < - little endian
** ![num] - alignment
** x[num] - pad num bytes, default 1
** X[num] - pad to num align, default MAXALIGN
**
** Following are system-dependent sizes:
** b/B - signed/unsigned byte
** h/H - signed/unsigned short
** i/I - signed/unsigned int
** l/L - signed/unsigned long
** f - float
** d - double
** T - size_t
**
** Following are system-independent sizes:
** in/In - signed/unsigned integer of size `n' bytes
Note: unpack of i/I is done to a Lua_number, typically a double,
so unpacking a 64-bit field (i8/I8) will lose precision.
Use e/E to unpack into a Wireshark Int64/UInt64 object/userdata instead.
** e/E - signed/unsigned eight-byte Integer (64bits, long long), to/from Int64/UInt64 object
** cn - sequence of `n' chars (from/to a string); when packing, n==0 means
the whole string; when unpacking, n==0 means use the previous
read number as the string length
** s - zero-terminated string
** ' ' - ignored
** '(' ')' - stop assigning items. ')' start assigning (padding when packing)
** '=' - return current position / offset
*/
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
#include "wslua.h"
/* WSLUA_MODULE Struct Binary encode/decode support */
/* TODO: figure out a way for wslua Module's to have (possibly long) description text */
/* The following line is here so that make-reg.pl does the right thing. This 'Struct' class
isn't really a class, so it doesn't have the checkStruct/pushStruct/etc. functions
the following macro would generate; but it does need to be registered and such.
WSLUA_CLASS_DEFINE_BASE(Struct,NOP,NOP,0);
*/
/* basic integer type - yes this is system-specific size - it's meant to be */
#if !defined(STRUCT_INT)
#define STRUCT_INT long
#endif
typedef STRUCT_INT Inttype;
/* corresponding unsigned version */
typedef unsigned STRUCT_INT Uinttype;
/* maximum size (in bytes) for integral types */
#define MAXINTSIZE 32
/* is 'x' a power of 2? */
#define isp2(x) ((x) > 0 && ((x) & ((x) - 1)) == 0)
/* dummy structure to get padding/alignment requirements */
struct cD {
gchar c;
gdouble d;
};
#define PADDING (sizeof(struct cD) - sizeof(gdouble))
#define MAXALIGN (PADDING > sizeof(int) ? PADDING : sizeof(int))
/* endian options */
#define BIG 0
#define LITTLE 1
/* trick to determine native endianess of system */
static union {
int dummy;
gchar endian;
} const native = {1};
/* settings info */
typedef struct Header {
int endian;
int align;
gboolean noassign;
} Header;
/* For options that take a number argument, gets the number */
static int getnum (const gchar **fmt, int df) {
if (!isdigit(**fmt)) /* no number? */
return df; /* return default value */
else {
int a = 0;
do {
a = a*10 + *((*fmt)++) - '0';
} while (isdigit(**fmt));
return a;
}
}
#define defaultoptions(h) ((h)->endian = native.endian, (h)->align = 1, (h)->noassign = FALSE)
/* gets size (number of bytes) for a given type */
static size_t optsize (lua_State *L, gchar opt, const gchar **fmt) {
switch (opt) {
case 'B': case 'b': return sizeof(gchar);
case 'H': case 'h': return sizeof(gshort);
case 'L': case 'l': return sizeof(glong);
case 'E': case 'e': return sizeof(gint64);
case 'T': return sizeof(size_t);
case 'f': return sizeof(gfloat);
case 'd': return sizeof(gdouble);
case 'x': return getnum(fmt, 1);
case 'X': return getnum(fmt, MAXALIGN);
case 'c': return getnum(fmt, 1);
case 'i': case 'I': {
int sz = getnum(fmt, sizeof(int));
if (sz > MAXINTSIZE)
luaL_error(L, "integral size %d is larger than limit of %d",
sz, MAXINTSIZE);
return sz;
}
case 's': case ' ':
case '<': case '>':
case '(': case ')':
case '!': case '=':
return 0; /* these cases do not have a size */
default: {
const gchar *msg = lua_pushfstring(L, "invalid format option [%c]", opt);
return luaL_argerror(L, 1, msg);
}
}
}
/*
** return number of bytes needed to align an element of size 'size'
** at current position 'len'
*/
static int gettoalign (size_t len, Header *h, int opt, size_t size) {
if (size == 0 || opt == 'c' || opt == 's') return 0;
if (size > (size_t)h->align)
size = h->align; /* respect max. alignment */
return (int)((size - (len & (size - 1))) & (size - 1));
}
/*
** options to control endianess and alignment settings
*/
static void controloptions (lua_State *L, int opt, const gchar **fmt,
Header *h) {
switch (opt) {
case ' ': return; /* ignore white spaces */
case '>': h->endian = BIG; return;
case '<': h->endian = LITTLE; return;
case '(': h->noassign = TRUE; return;
case ')': h->noassign = FALSE; return;
case '!': {
int a = getnum(fmt, MAXALIGN);
if (!isp2(a))
luaL_error(L, "alignment %d is not a power of 2", a);
h->align = a;
return;
}
default: {
const char *msg = lua_pushfstring(L, "invalid format option '%c'", opt);
luaL_argerror(L, 1, msg);
}
}
}
/* Encodes a Lua number as an integer of given size and endiannes into a string struct */
static void putinteger (lua_State *L, luaL_Buffer *b, int arg, int endian,
int size) {
lua_Number n = luaL_checknumber(L, arg);
/* this one's not system dependent size - it's a long long */
gint64 value;
gchar buff[MAXINTSIZE];
if (n < 0)
value = (guint64)(gint64)n;
else
value = (guint64)n;
if (endian == LITTLE) {
int i;
for (i = 0; i < size; i++) {
buff[i] = (value & 0xff);
value >>= 8;
}
}
else {
int i;
for (i = size - 1; i >= 0; i--) {
buff[i] = (value & 0xff);
value >>= 8;
}
}
luaL_addlstring(b, buff, size);
}
/* corrects endiannes - usually done by other functions themselves, but is
* used for float/doubles, since on some platforms they're endian'ed as well
*/
static void correctbytes (gchar *b, int size, int endian) {
if (endian != native.endian) {
int i = 0;
while (i < --size) {
gchar temp = b[i];
b[i++] = b[size];
b[size] = temp;
}
}
}
WSLUA_CONSTRUCTOR Struct_pack (lua_State *L) {
/* Returns a string containing the values arg1, arg2, etc. packed/encoded according to the format string. */
#define WSLUA_ARG_Struct_unpack_FORMAT 1 /* The format string */
#define WSLUA_ARG_Struct_unpack_STRUCT 2 /* One or more Lua value(s) to encode, based on the given format. */
luaL_Buffer b;
const char *fmt = luaL_checkstring(L, WSLUA_ARG_Struct_unpack_FORMAT);
Header h;
int poscnt = 0;
int posBuf[10];
int arg = 2;
size_t totalsize = 0;
defaultoptions(&h);
lua_pushnil(L); /* mark to separate arguments from string buffer */
luaL_buffinit(L, &b);
while (*fmt != '\0') {
int opt = *fmt++;
size_t size = optsize(L, opt, &fmt);
int toalign = gettoalign(totalsize, &h, opt, size);
totalsize += toalign;
while (toalign-- > 0) luaL_addchar(&b, '\0');
if (opt == 'X') size = 0; /* 'X' is about alignment, not size */
if (h.noassign && size) opt = 'x'; /* for pack, "(i4)" is the same as "x4" */
switch (opt) {
case 'b': case 'B': case 'h': case 'H':
case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
putinteger(L, &b, arg++, h.endian, (int)size);
break;
}
case 'e': {
Int64_pack(L, &b, arg++, h.endian == LITTLE);
break;
}
case 'E': {
UInt64_pack(L, &b, arg++, h.endian == LITTLE);
break;
}
case 'x': case 'X': {
size_t len = size;
while (len-- > 0)
luaL_addchar(&b, '\0');
break;
}
case 'f': {
gfloat f = (gfloat)luaL_checknumber(L, arg++);
correctbytes((gchar *)&f, (int)size, h.endian);
luaL_addlstring(&b, (gchar *)&f, size);
break;
}
case 'd': {
gdouble d = luaL_checknumber(L, arg++);
correctbytes((gchar *)&d, (int)size, h.endian);
luaL_addlstring(&b, (gchar *)&d, size);
break;
}
case 'c': case 's': {
size_t l;
const gchar *s = luaL_checklstring(L, arg++, &l);
if (size == 0) size = l;
luaL_argcheck(L, l >= (size_t)size, arg, "string too short");
luaL_addlstring(&b, s, size);
if (opt == 's') {
luaL_addchar(&b, '\0'); /* add zero at the end */
size++;
}
break;
}
case '=': {
if (poscnt < (int)(sizeof(posBuf)/sizeof(posBuf[0])))
posBuf[poscnt++] = (int)totalsize + 1;
break;
}
default: controloptions(L, opt, &fmt, &h);
}
totalsize += size;
}
luaL_pushresult(&b);
for (arg = 0; arg < poscnt; arg++)
lua_pushinteger(L, posBuf[arg]);
return poscnt + 1;
}
/* Decodes an integer from a string struct into a Lua number, based on
* given endianess and size. If the integer type is signed, this makes
* the Lua number be +/- correctly as well.
*/
static lua_Number getinteger (const gchar *buff, int endian,
int issigned, int size) {
Uinttype l = 0;
int i;
if (endian == BIG) {
for (i = 0; i < size; i++) {
l <<= 8;
l |= (Uinttype)(guchar)buff[i];
}
}
else {
for (i = size - 1; i >= 0; i--) {
l <<= 8;
l |= (Uinttype)(guchar)buff[i];
}
}
if (!issigned)
return (lua_Number)l;
else { /* signed format */
Uinttype mask = (Uinttype)(~((Uinttype)0)) << (size*8 - 1);
if (l & mask) /* negative value? */
l |= mask; /* signal extension */
return (lua_Number)(Inttype)l;
}
}
#define b_pushnumber(n) { if (!h.noassign) lua_pushnumber(L, (lua_Number)(n)); }
WSLUA_CONSTRUCTOR Struct_unpack (lua_State *L) {
/* Unpacks/decodes multiple Lua values from a given struct-like binary Lua string.
The number of returned values depends on the format given, plus an addtional value of the position where it stopped reading is returned. */
#define WSLUA_ARG_Struct_unpack_FORMAT 1 /* The format string */
#define WSLUA_ARG_Struct_unpack_STRUCT 2 /* The binary Lua string to unpack */
#define WSLUA_OPTARG_Struct_unpack_BEGIN 3 /* The position to begin reading from (default=1) */
Header h;
const char *fmt = luaL_checkstring(L, WSLUA_ARG_Struct_unpack_FORMAT);
size_t ld;
const char *data = luaL_checklstring(L, WSLUA_ARG_Struct_unpack_STRUCT, &ld);
size_t pos = luaL_optinteger(L, WSLUA_OPTARG_Struct_unpack_BEGIN, 1) - 1;
defaultoptions(&h);
lua_settop(L, 2);
while (*fmt) {
int opt = *fmt++;
size_t size = optsize(L, opt, &fmt);
pos += gettoalign(pos, &h, opt, size);
luaL_argcheck(L, pos+size <= ld, 2, "data string too short");
if (opt == 'X') size = 0;
if (h.noassign && size > 0) {
/* if we're not assigning, and the opt type has a size, then loop again */
/* this will not be the case for controloptions, 'c0', 's', and '=' */
pos += size;
continue;
}
luaL_checkstack(L, 1, "too many results");
switch (opt) {
case 'b': case 'B': case 'h': case 'H':
case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
int issigned = islower(opt);
lua_Number res = getinteger(data+pos, h.endian, issigned, (int)size);
lua_pushnumber(L, res);
break;
}
case 'e': {
Int64_unpack(L, data+pos, h.endian == LITTLE);
break;
}
case 'E': {
UInt64_unpack(L, data+pos, h.endian == LITTLE);
break;
}
case 'x': case 'X': {
break;
}
case 'f': {
gfloat f;
memcpy(&f, data+pos, size);
correctbytes((gchar *)&f, sizeof(f), h.endian);
lua_pushnumber(L, f);
break;
}
case 'd': {
gdouble d;
memcpy(&d, data+pos, size);
correctbytes((gchar *)&d, sizeof(d), h.endian);
lua_pushnumber(L, d);
break;
}
case 'c': {
if (size == 0) {
if (!lua_isnumber(L, -1))
luaL_error(L, "format `c0' needs a previous size");
size = lua_tonumber(L, -1);
lua_pop(L, 1);
luaL_argcheck(L, pos+size <= ld, 2, "data string too short");
}
if (!h.noassign)
lua_pushlstring(L, data+pos, size);
break;
}
case 's': {
const gchar *e = (const char *)memchr(data+pos, '\0', ld - pos);
if (e == NULL)
luaL_error(L, "unfinished string in data");
size = (e - (data+pos)) + 1;
if (!h.noassign)
lua_pushlstring(L, data+pos, size - 1);
break;
}
case '=': {
lua_pushinteger(L, pos + 1);
break;
}
default: controloptions(L, opt, &fmt, &h);
}
pos += size;
}
lua_pushinteger(L, pos + 1);
return lua_gettop(L) - 2;
}
WSLUA_CONSTRUCTOR Struct_size (lua_State *L) {
/* Returns the length of the binary string struct that would be consumed/handled by the given format string. */
Header h;
const gchar *fmt = luaL_checkstring(L, 1);
size_t pos = 0;
defaultoptions(&h);
while (*fmt) {
int opt = *fmt++;
size_t size = optsize(L, opt, &fmt);
pos += gettoalign(pos, &h, opt, size);
if (opt == 's')
luaL_argerror(L, 1, "option 's' has no fixed size");
else if (opt == 'c' && size == 0)
luaL_argerror(L, 1, "option 'c0' has no fixed size");
if (!isalnum(opt))
controloptions(L, opt, &fmt, &h);
pos += size;
}
lua_pushinteger(L, pos);
return 1;
}
/* }====================================================== */
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int Struct__gc(lua_State* L _U_) {
return 0;
}
static const luaL_Reg Struct_methods[] = {
{"pack", Struct_pack},
{"unpack", Struct_unpack},
{"size", Struct_size},
{NULL, NULL}
};
static const luaL_Reg Struct_meta[] = {
{ NULL, NULL }
};
LUALIB_API int Struct_register(lua_State* L) {
WSLUA_REGISTER_CLASS(Struct);
return 0;
}
/******************************************************************************
* Copyright (C) 2010-2012 Lua.org, PUC-Rio. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/

339
test/lua/struct.lua Normal file
View File

@ -0,0 +1,339 @@
-- This is a test script for tshark/wireshark.
-- This script runs inside tshark/wireshark, so to run it do:
-- wireshark -X lua_script:<path_to_testdir>/lua/struct.lua
-- tshark -r bogus.cap -X lua_script:<path_to_testdir>/lua/struct.lua
-- Tests Int64/UInt64 functions
local function testing(...)
print("---- Testing "..tostring(...).." ----")
end
local function test(name, ...)
io.stdout:write("test "..name.."...")
if (...) == true then
io.stdout:write("passed\n")
else
io.stdout:write("failed!\n")
error(name.." test failed!")
end
end
--
-- auxiliar function to print an hexadecimal `dump' of a given string
-- (not used by the test)
--
local function bp (s)
s = string.gsub(s, "(.)", function(c)
return string.format("\\%02x", string.byte(c))
end)
print(s)
end
-----------------------------
print("Lua version: ".._VERSION)
testing("Struct library")
local lib = Struct
test("global",_G.Struct == lib)
for name, val in pairs(lib) do
print("\t"..name.." = "..type(val))
end
test("class1",type(lib) == 'table')
test("class2",type(lib.pack) == 'function')
test("class3",type(lib.unpack) == 'function')
test("class4",type(lib.size) == 'function')
local val1 = "\42\00\00\00\00\00\00\01\00\00\00\02\00\00\00\03\00\00\00\04"
local fmt1_le = "<!4biii4i4"
local fmt1_be = ">!4biii4i4"
local fmt1_64le = "<!4ieE"
local fmt1_64be = ">!4ieE"
local fmt2_be = ">!4bi(ii4)i"
testing("basic size")
test("basic_size1", lib.size(fmt1_le) == string.len(val1))
test("basic_size2", lib.size(fmt1_le) == Struct.size(fmt1_be))
test("basic_size3", lib.size(fmt1_le) == Struct.size(fmt1_64le))
test("basic_size4", lib.size(fmt2_be) == Struct.size(fmt1_64le))
testing("basic unpack")
local ret1, ret2, ret3, ret4, ret5, pos = lib.unpack(fmt1_le, val1)
test("basic_unpack1", ret1 == 42 and ret2 == 0x01000000 and ret3 == 0x02000000 and ret4 == 0x03000000 and ret5 == 0x04000000)
test("basic_unpack_position1", pos == string.len(val1) + 1)
ret1, ret2, ret3, ret4, ret5, pos = lib.unpack(fmt1_be, val1)
test("basic_unpack2", ret1 == 42 and ret2 == 1 and ret3 == 2 and ret4 == 3 and ret5 == 4)
test("basic_unpack_position2", pos == string.len(val1) + 1)
ret1, ret2, ret3, pos = lib.unpack(fmt1_64le, val1)
test("basic_unpack3", ret1 == 42 and ret2 == Int64.new( 0x01000000, 0x02000000) and ret3 == UInt64.new( 0x03000000, 0x04000000))
print(typeof(ret2),typeof(ret3))
test("basic_unpack3b", typeof(ret2) == "Int64" and typeof(ret3) == "UInt64")
test("basic_unpack_position3", pos == string.len(val1) + 1)
ret1, ret2, ret3, pos = lib.unpack(fmt1_64be, val1)
test("basic_unpack4", ret1 == 0x2A000000 and ret2 == Int64.new( 2, 1) and ret3 == UInt64.new( 4, 3))
test("basic_unpack4b", typeof(ret2) == "Int64" and typeof(ret3) == "UInt64")
test("basic_unpack_position4", pos == string.len(val1) + 1)
ret1, ret2, ret3, pos = lib.unpack(fmt2_be, val1)
test("basic_unpack5", ret1 == 42 and ret2 == 1 and ret3 == 4)
test("basic_unpack_position5", pos == string.len(val1) + 1)
testing("basic pack")
local pval1 = lib.pack(fmt1_le, lib.unpack(fmt1_le, val1))
test("basic_pack1", pval1 == val1)
test("basic_pack2", val1 == lib.pack(fmt1_be, lib.unpack(fmt1_be, val1)))
test("basic_pack3", val1 == lib.pack(fmt1_64le, lib.unpack(fmt1_64le, val1)))
test("basic_pack4", val1 == lib.pack(fmt1_64be, lib.unpack(fmt1_64be, val1)))
test("basic_pack5", lib.pack(fmt2_be, lib.unpack(fmt1_be, val1)) == lib.pack(">!4biiii", 42, 1, 0, 0, 2))
----------------------------------
-- following comes from:
-- http://www.inf.puc-rio.br/~roberto/struct/teststruct
-- unfortunately many of his tests assumed a local machine word
-- size of 4 bytes for long and such, so I had to muck with this
-- to make it handle 64-bit compiles.
-- $Id: teststruct.lua,v 1.2 2008/04/18 20:06:01 roberto Exp $
-- some pack/unpack commands are host-size dependent, so we need to pad
local l_pad, ln_pad = "",""
if lib.size("l") == 8 then
-- the machine running this script uses a long of 8 bytes
l_pad = "\00\00\00\00"
ln_pad = "\255\255\255\255"
end
local a,b,c,d,e,f,x
testing("pack")
test("pack_I",#Struct.pack("I", 67324752) == 4)
test("pack_b1",lib.pack('b', 10) == string.char(10))
test("pack_b2",lib.pack('bbb', 10, 20, 30) == string.char(10, 20, 30))
test("pack_h1",lib.pack('<h', 10) == string.char(10, 0))
test("pack_h2",lib.pack('>h', 10) == string.char(0, 10))
test("pack_h3",lib.pack('<h', -10) == string.char(256-10, 256-1))
test("pack_l1",lib.pack('<l', 10) == string.char(10, 0, 0, 0)..l_pad)
test("pack_l2",lib.pack('>l', 10) == l_pad..string.char(0, 0, 0, 10))
test("pack_l3",lib.pack('<l', -10) == string.char(256-10, 256-1, 256-1, 256-1)..ln_pad)
testing("unpack")
test("unpack_h1",lib.unpack('<h', string.char(10, 0)) == 10)
test("unpack_h2",lib.unpack('>h', string.char(0, 10)) == 10)
test("unpack_h3",lib.unpack('<h', string.char(256-10, 256-1)) == -10)
test("unpack_l1",lib.unpack('<l', string.char(10, 0, 0, 1)..l_pad) == 10 + 2^(3*8))
test("unpack_l2",lib.unpack('>l', l_pad..string.char(0, 1, 0, 10)) == 10 + 2^(2*8))
test("unpack_l3",lib.unpack('<l', string.char(256-10, 256-1, 256-1, 256-1)..ln_pad) == -10)
-- limits
lims = {{'B', 255}, {'b', 127}, {'b', -128},
{'I1', 255}, {'i1', 127}, {'i1', -128},
{'H', 2^16 - 1}, {'h', 2^15 - 1}, {'h', -2^15},
{'I2', 2^16 - 1}, {'i2', 2^15 - 1}, {'i2', -2^15},
{'L', 2^32 - 1}, {'l', 2^31 - 1}, {'l', -2^31},
{'I4', 2^32 - 1}, {'i4', 2^31 - 1}, {'i4', -2^31},
}
for _, a in pairs{'', '>', '<'} do
local i = 1
for _, l in pairs(lims) do
local fmt = a .. l[1]
test("limit"..i.."("..l[1]..")", lib.unpack(fmt, lib.pack(fmt, l[2])) == l[2])
i = i + 1
end
end
testing("fixed-sized ints")
-- tests for fixed-sized ints
local num = 1
for _, i in pairs{1,2,4} do
x = lib.pack('<i'..i, -3)
test("pack_fixedlen"..num, string.len(x) == i)
test("pack_fixed"..num, x == string.char(256-3) .. string.rep(string.char(256-1), i-1))
test("unpack_fixed"..num, lib.unpack('<i'..i, x) == -3)
num = num + 1
end
testing("alignment")
-- alignment
d = lib.pack("d", 5.1)
ali = {[1] = string.char(1)..d,
[2] = string.char(1, 0)..d,
[4] = string.char(1, 0, 0, 0)..d,
[8] = string.char(1, 0, 0, 0, 0, 0, 0, 0)..d,
}
num = 1
for a,r in pairs(ali) do
test("pack_align"..num, lib.pack("!"..a.."bd", 1, 5.1) == r)
local x,y = lib.unpack("!"..a.."bd", r)
test("unpack_align"..num, x == 1 and y == 5.1)
num = num + 1
end
testing("string")
-- strings
test("string_pack1",lib.pack("c", "alo alo") == "a")
test("string_pack2",lib.pack("c4", "alo alo") == "alo ")
test("string_pack3",lib.pack("c5", "alo alo") == "alo a")
test("string_pack4",lib.pack("!4b>c7", 1, "alo alo") == "\1alo alo")
test("string_pack5",lib.pack("!2<s", "alo alo") == "alo alo\0")
test("string_pack6",lib.pack(" c0 ", "alo alo") == "alo alo")
num = 1
for _, f in pairs{"B", "l", "i2", "f", "d"} do
for _, s in pairs{"", "a", "alo", string.rep("x", 200)} do
local x = lib.pack(f.."c0", #s, s)
test("string_unpack"..num, lib.unpack(f.."c0", x) == s)
num = num + 1
end
end
testing("indeces")
-- indices
x = lib.pack("!>iiiii", 1, 2, 3, 4, 5)
local i = 1
local k = 1
num = 1
while i < #x do
local v, j = lib.unpack("!>i", x, i)
test("index_unpack"..num, j == i + 4 and v == k)
i = j; k = k + 1
num = num + 1
end
testing("absolute")
-- alignments are relative to 'absolute' positions
x = lib.pack("!8 xd", 12)
test("absolute_unpack1",lib.unpack("!8d", x, 3) == 12)
test("absolute_pack1",lib.pack("<lhbxxH", -2, 10, -10, 250) ==
string.char(254, 255, 255, 255) ..ln_pad.. string.char(10, 0, 246, 0, 0, 250, 0))
a,b,c,d = lib.unpack("<lhbxxH",
string.char(254, 255, 255, 255) ..ln_pad.. string.char(10, 0, 246, 0, 0, 250, 0))
test("absolute_unpack2",a == -2 and b == 10 and c == -10 and d == 250)
test("absolute_pack2",lib.pack(">lBxxH", -20, 10, 250) ==
ln_pad..string.char(255, 255, 255, 236, 10, 0, 0, 0, 250))
testing("position")
a, b, c, d = lib.unpack(">lBxxH",
ln_pad..string.char(255, 255, 255, 236, 10, 0, 0, 0, 250))
-- the 'd' return val is position in string, so will depend on size of long 'l'
local vald = 10 + string.len(l_pad)
test("position_unpack1",a == -20 and b == 10 and c == 250 and d == vald)
a,b,c,d,e = lib.unpack(">fdfH",
'000'..lib.pack(">fdfH", 3.5, -24e-5, 200.5, 30000),
4)
test("position_unpack2",a == 3.5 and b == -24e-5 and c == 200.5 and d == 30000 and e == 22)
a,b,c,d,e = lib.unpack("<fdxxfH",
'000'..lib.pack("<fdxxfH", -13.5, 24e5, 200.5, 300),
4)
test("position_unpack3",a == -13.5 and b == 24e5 and c == 200.5 and d == 300 and e == 24)
x = lib.pack(">I2fi4I2", 10, 20, -30, 40001)
test("position_pack1",string.len(x) == 2+4+4+2)
test("position_unpack4",lib.unpack(">f", x, 3) == 20)
a,b,c,d = lib.unpack(">i2fi4I2", x)
test("position_unpack5",a == 10 and b == 20 and c == -30 and d == 40001)
testing("string length")
local s = "hello hello"
x = lib.pack(" b c0 ", string.len(s), s)
test("stringlen_unpack1",lib.unpack("bc0", x) == s)
x = lib.pack("Lc0", string.len(s), s)
test("stringlen_unpack2",lib.unpack(" L c0 ", x) == s)
x = lib.pack("cc3b", s, s, 0)
test("stringlen_pack1",x == "hhel\0")
test("stringlen_unpack3",lib.unpack("xxxxb", x) == 0)
testing("padding")
test("padding_pack1",lib.pack("<!l", 3) == string.char(3, 0, 0, 0)..l_pad)
test("padding_pack2",lib.pack("<!xl", 3) == l_pad..string.char(0, 0, 0, 0, 3, 0, 0, 0)..l_pad)
test("padding_pack3",lib.pack("<!xxl", 3) == l_pad..string.char(0, 0, 0, 0, 3, 0, 0, 0)..l_pad)
test("padding_pack4",lib.pack("<!xxxl", 3) == l_pad..string.char(0, 0, 0, 0, 3, 0, 0, 0)..l_pad)
test("padding_unpack1",lib.unpack("<!l", string.char(3, 0, 0, 0)..l_pad) == 3)
test("padding_unpack2",lib.unpack("<!xl", l_pad..string.char(0, 0, 0, 0, 3, 0, 0, 0)..l_pad) == 3)
test("padding_unpack3",lib.unpack("<!xxl", l_pad..string.char(0, 0, 0, 0, 3, 0, 0, 0)..l_pad) == 3)
test("padding_unpack4",lib.unpack("<!xxxl", l_pad..string.char(0, 0, 0, 0, 3, 0, 0, 0)..l_pad) == 3)
testing("format")
test("format_pack1",lib.pack("<!2 b l h", 2, 3, 5) == string.char(2, 0, 3, 0)..l_pad..string.char(0, 0, 5, 0))
a,b,c = lib.unpack("<!2blh", string.char(2, 0, 3, 0)..l_pad..string.char(0, 0, 5, 0))
test("format_pack2",a == 2 and b == 3 and c == 5)
test("format_pack3",lib.pack("<!8blh", 2, 3, 5) == string.char(2, 0, 0, 0)..l_pad..string.char(3, 0, 0, 0)..l_pad..string.char(5, 0))
a,b,c = lib.unpack("<!8blh", string.char(2, 0, 0, 0)..l_pad..string.char(3, 0, 0, 0)..l_pad..string.char(5, 0))
test("format_pack4",a == 2 and b == 3 and c == 5)
test("format_pack5",lib.pack(">sh", "aloi", 3) == "aloi\0\0\3")
test("format_pack6",lib.pack(">!sh", "aloi", 3) == "aloi\0\0\0\3")
x = "aloi\0\0\0\0\3\2\0\0"
a, b, c = lib.unpack("<!si4", x)
test("format_unpack1",a == "aloi" and b == 2*256+3 and c == string.len(x)+1)
x = lib.pack("!4sss", "hi", "hello", "bye")
a,b,c = lib.unpack("sss", x)
test("format_unpack2",a == "hi" and b == "hello" and c == "bye")
a, i = lib.unpack("s", x, 1)
test("format_unpack3",a == "hi")
a, i = lib.unpack("s", x, i)
test("format_unpack4",a == "hello")
a, i = lib.unpack("s", x, i)
test("format_unpack5",a == "bye")
-- test for weird conditions
testing("weird conditions")
test("weird_pack1",lib.pack(">>>h <!!!<h", 10, 10) == string.char(0, 10, 10, 0))
test("weird_pack2",not pcall(lib.pack, "!3l", 10))
test("weird_pack3",not pcall(lib.pack, "3", 10))
test("weird_pack4",not pcall(lib.pack, "i33", 10))
test("weird_pack5",not pcall(lib.pack, "I33", 10))
test("weird_pack6",lib.pack("") == "")
test("weird_pack7",lib.pack(" ") == "")
test("weird_pack8",lib.pack(">>><<<!!") == "")
test("weird_unpack1",not pcall(lib.unpack, "c0", "alo"))
test("weird_unpack2",not pcall(lib.unpack, "s", "alo"))
test("weird_unpack3",lib.unpack("s", "alo\0") == "alo")
test("weird_pack9",not pcall(lib.pack, "c4", "alo"))
test("weird_pack10",pcall(lib.pack, "c3", "alo"))
test("weird_unpack4",not pcall(lib.unpack, "c4", "alo"))
test("weird_unpack5",pcall(lib.unpack, "c3", "alo"))
test("weird_unpack6",not pcall(lib.unpack, "bc0", "\4alo"))
test("weird_unpack7",pcall(lib.unpack, "bc0", "\3alo"))
test("weird_unpack8",not pcall(lib.unpack, "b", "alo", 4))
test("weird_unpack9",lib.unpack("b", "alo\3", 4) == 3)
print("\n-----------------------------\n")
print("All tests passed!\n\n")

View File

@ -152,6 +152,22 @@ unittests_step_lua_args_test() {
test_step_ok
}
unittests_step_lua_struct_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/struct.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
cat testout.txt
test_step_failed "didn't find pass marker"
fi
}
unittests_step_oids_test() {
DUT=$SOURCE_DIR/epan/oids_test
ARGS=
@ -188,6 +204,7 @@ unittests_suite() {
test_step_add "lua dissector" unittests_step_lua_dissector_test
test_step_add "lua int64" unittests_step_lua_int64_test
test_step_add "lua script arguments" unittests_step_lua_args_test
test_step_add "lua struct" unittests_step_lua_struct_test
test_step_add "oids_test" unittests_step_oids_test
test_step_add "reassemble_test" unittests_step_reassemble_test
test_step_add "tvbtest" unittests_step_tvbtest