aboutsummaryrefslogtreecommitdiffstats
path: root/epan/wslua/wslua_struct.c
diff options
context:
space:
mode:
authorHadriel Kaplan <hadrielk@yahoo.com>2014-02-18 10:03:04 -0500
committerAlexis La Goutte <alexis.lagoutte@gmail.com>2014-02-25 21:06:21 +0000
commit860747e1e78aefdaf31e77408ad590b9d759d1aa (patch)
treebcdb8aebc725be4f21e807d3345c0194a74ba28f /epan/wslua/wslua_struct.c
parent907a8259862401af3fdc8ad1201e13c6abf677db (diff)
Adds some Lua helper functions: some commonly used functions, and to help troubleshooting Lua scripts
There are some common things people need to do, such as convert to/from hex or get the raw binary string in a ByteArray/Tvb/TvbRange. These have been added, as well as some tests for them in the testsuites. Also, functions have been added to allow a script to get all the available tap types and filter fields, since they are not exactly what one can see in the Wireshark gui. Change-Id: I92e5e4eae713bb90d79b0c024eaa4e55b99cc96b Reviewed-on: https://code.wireshark.org/review/249 Reviewed-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
Diffstat (limited to 'epan/wslua/wslua_struct.c')
-rw-r--r--epan/wslua/wslua_struct.c64
1 files changed, 54 insertions, 10 deletions
diff --git a/epan/wslua/wslua_struct.c b/epan/wslua/wslua_struct.c
index 3f26d8755d..3ef76458e6 100644
--- a/epan/wslua/wslua_struct.c
+++ b/epan/wslua/wslua_struct.c
@@ -274,10 +274,10 @@ static void correctbytes (gchar *b, int size, int endian) {
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. */
+#define WSLUA_ARG_Struct_pack_FORMAT 1 /* The format string */
+#define WSLUA_ARG_Struct_pack_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);
+ const char *fmt = luaL_checkstring(L, WSLUA_ARG_Struct_pack_FORMAT);
Header h;
int poscnt = 0;
int posBuf[10];
@@ -350,7 +350,7 @@ WSLUA_CONSTRUCTOR Struct_pack (lua_State *L) {
luaL_pushresult(&b);
for (arg = 0; arg < poscnt; arg++)
lua_pushinteger(L, posBuf[arg]);
- return poscnt + 1;
+ WSLUA_RETURN(poscnt + 1); /* The packed binary Lua string, plus any positions due to '=' being used in format. */
}
/* Decodes an integer from a string struct into a Lua number, based on
@@ -476,14 +476,15 @@ WSLUA_CONSTRUCTOR Struct_unpack (lua_State *L) {
pos += size;
}
lua_pushinteger(L, pos + 1);
- return lua_gettop(L) - 2;
+ WSLUA_RETURN(lua_gettop(L) - 2); /* One or more values based on format, plus the posoition it stopped unpacking. */
}
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. */
+#define WSLUA_ARG_Struct_size_FORMAT 1 /* The format string */
Header h;
- const gchar *fmt = luaL_checkstring(L, 1);
+ const gchar *fmt = luaL_checkstring(L, WSLUA_ARG_Struct_size_FORMAT);
size_t pos = 0;
defaultoptions(&h);
while (*fmt) {
@@ -499,7 +500,48 @@ WSLUA_CONSTRUCTOR Struct_size (lua_State *L) {
pos += size;
}
lua_pushinteger(L, pos);
- return 1;
+ WSLUA_RETURN(1); /* The size number */
+}
+
+WSLUA_CONSTRUCTOR Struct_tohex (lua_State *L) {
+ /* Converts the passed-in binary string to a hex-ascii string. */
+#define WSLUA_ARG_Struct_tohex_BYTESTRING 1 /* A Lua string consisting of binary bytes */
+#define WSLUA_OPTARG_Struct_tohex_LOWERCASE 2 /* True to use lower-case hex characters (default=false). */
+#define WSLUA_OPTARG_Struct_tohex_SEPARATOR 3 /* A string separator to insert between hex bytes (default=nil). */
+ const gchar* s = NULL;
+ size_t len = 0;
+ gboolean lowercase = FALSE;
+ const gchar* sep = NULL;
+
+ s = luaL_checklstring(L,WSLUA_ARG_Struct_tohex_BYTESTRING,&len);
+
+ if (!s)
+ WSLUA_ARG_ERROR(Struct_tohex,BYTESTRING,"must be a Lua string");
+
+ lowercase = wslua_optbool(L,WSLUA_OPTARG_Struct_tohex_LOWERCASE,FALSE);
+ sep = luaL_optstring(L,WSLUA_OPTARG_Struct_tohex_SEPARATOR,NULL);
+
+ wslua_bin2hex(L, s, (guint)len, lowercase, sep);
+ WSLUA_RETURN(1); /* The Lua hex-ascii string */
+}
+
+WSLUA_CONSTRUCTOR Struct_fromhex (lua_State *L) {
+ /* Converts the passed-in hex-ascii string to a binary string. */
+#define WSLUA_ARG_Struct_fromhex_HEXBYTES 1 /* A string consisting of hexadecimal bytes like "00 B1 A2" or "1a2b3c4d" */
+#define WSLUA_OPTARG_Struct_fromhex_SEPARATOR 2 /* A string separator between hex bytes/words (default=" "). */
+ const gchar* s = NULL;
+ size_t len = 0;
+ const gchar* sep = NULL;
+
+ s = luaL_checklstring(L,WSLUA_ARG_Struct_fromhex_HEXBYTES,&len);
+
+ if (!s)
+ WSLUA_ARG_ERROR(Struct_tohex,BYTESTRING,"must be a Lua string");
+
+ sep = luaL_optstring(L,WSLUA_OPTARG_Struct_fromhex_SEPARATOR,NULL);
+
+ wslua_hex2bin(L, s, (guint)len, sep);
+ WSLUA_RETURN(1); /* The Lua binary string */
}
/* }====================================================== */
@@ -510,9 +552,11 @@ static int Struct__gc(lua_State* L _U_) {
}
WSLUA_METHODS Struct_methods[] = {
- {"pack", Struct_pack},
- {"unpack", Struct_unpack},
- {"size", Struct_size},
+ WSLUA_CLASS_FNREG(Struct,pack),
+ WSLUA_CLASS_FNREG(Struct,unpack),
+ WSLUA_CLASS_FNREG(Struct,size),
+ WSLUA_CLASS_FNREG(Struct,tohex),
+ WSLUA_CLASS_FNREG(Struct,fromhex),
{ NULL, NULL }
};