WSLUA_METHOD ByteArray_append(lua_State* L) { /* Append a ByteArray to this ByteArray */ #define WSLUA_ARG_ByteArray_append_APPENDED 2 /* Array to be appended */ ByteArray ba = checkByteArray(L,1); ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray_append_APPENDED); g_byte_array_append(ba,ba2->data,ba2->len); return 0; }
WSLUA_METHOD ByteArray_append(lua_State* L) { /* Append a ByteArray to this ByteArray */ #define WSLUA_ARG_ByteArray_append_APPENDED 2 /* Array to be appended */ ByteArray ba = checkByteArray(L,1); ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray_append_APPENDED); if (! (ba && ba2) ) WSLUA_ERROR(ByteArray_append,"Both arguments must be ByteArrays"); g_byte_array_append(ba,ba2->data,ba2->len); return 0; }
/* * ByteArray_tvb(name) */ WSLUA_CONSTRUCTOR ByteArray_tvb (lua_State *L) { /* Creates a new Tvb from a bytearray (it gets added to the current frame too) */ #define WSLUA_ARG_ByteArray_tvb_NAME 2 /* The name to be given to the new data-source. */ ByteArray ba = checkByteArray(L,1); const gchar* name = luaL_optstring(L,WSLUA_ARG_ByteArray_tvb_NAME,"Unnamed") ; guint8* data; Tvb tvb; if (!ba) return 0; if (!lua_tvb) { luaL_error(L,"Tvbs can only be created and used in dissectors"); return 0; } data = (guint8 *)g_memdup(ba->data, ba->len); tvb = (Tvb)g_malloc(sizeof(struct _wslua_tvb)); tvb->ws_tvb = tvb_new_real_data(data, ba->len,ba->len); tvb->expired = FALSE; tvb->need_free = TRUE; tvb_set_free_cb(tvb->ws_tvb, g_free); add_new_data_source(lua_pinfo, tvb->ws_tvb, name); PUSH_TVB(L,tvb); WSLUA_RETURN(1); /* The created Tvb. */ }
WSLUA_METHOD ByteArray_set_index(lua_State* L) { /* Sets the value of an index of a ByteArray. */ #define WSLUA_ARG_ByteArray_set_index_INDEX 2 /* The position of the byte to be set */ #define WSLUA_ARG_ByteArray_set_index_VALUE 3 /* The char value to set [0-255] */ ByteArray ba = checkByteArray(L,1); int idx = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_INDEX); int v = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_VALUE); if (!ba) return 0; if (idx == 0 && ! g_str_equal(luaL_optstring(L,2,""),"0") ) { luaL_argerror(L,2,"bad index"); return 0; } if (idx < 0 || (guint)idx >= ba->len) { luaL_argerror(L,2,"index out of range"); return 0; } if (v < 0 || v > 255) { luaL_argerror(L,3,"Byte out of range"); return 0; } ba->data[idx] = (guint8)v; return 0; }
WSLUA_METAMETHOD ByteArray__concat(lua_State* L) { /* Concatenate two ByteArrays */ #define WSLUA_ARG_ByteArray__cat_FIRST 1 /* First array */ #define WSLUA_ARG_ByteArray__cat_SECOND 2 /* Second array */ ByteArray ba1 = checkByteArray(L,WSLUA_ARG_ByteArray__cat_FIRST); ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray__cat_SECOND); ByteArray ba; ba = g_byte_array_new(); g_byte_array_append(ba,ba1->data,ba1->len); g_byte_array_append(ba,ba2->data,ba2->len); pushByteArray(L,ba); WSLUA_RETURN(1); /* The new composite ByteArray. */ }
WSLUA_METAMETHOD ByteArray__concat(lua_State* L) { /* Concatenate two ByteArrays */ #define WSLUA_ARG_ByteArray__cat_FIRST 1 /* First array */ #define WSLUA_ARG_ByteArray__cat_SECOND 2 /* Second array */ ByteArray ba = checkByteArray(L,WSLUA_ARG_ByteArray__cat_FIRST); ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray__cat_SECOND); if (! (ba && ba2) ) WSLUA_ERROR(ByteArray__cat,"Both arguments must be ByteArrays"); g_byte_array_append(ba,ba2->data,ba2->len); pushByteArray(L,ba); WSLUA_RETURN(1); /* The new composite ByteArray. */ }
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */ static int ByteArray__gc(lua_State* L) { ByteArray ba = checkByteArray(L,1); if (!ba) return 0; g_byte_array_free(ba,TRUE); return 0; }
WSLUA_METHOD ByteArray_len(lua_State* L) { /* Obtain the length of a ByteArray */ ByteArray ba = checkByteArray(L,1); lua_pushnumber(L,(lua_Number)ba->len); WSLUA_RETURN(1); /* The length of the ByteArray. */ }
WSLUA_METAMETHOD ByteArray__tostring(lua_State* L) { /* Obtain a Lua string containing the bytes in a ByteArray so that it can be used in display filters (e.g. "01FE456789AB") */ ByteArray ba = checkByteArray(L,1); if (!ba) return 0; wslua_bin2hex(L, ba->data, ba->len, FALSE, NULL); WSLUA_RETURN(1); /* A hex-ascii string representation of the ByteArray. */ }
WSLUA_METHOD ByteArray_tohex(lua_State* L) { /* Obtain a Lua string of the bytes in a ByteArray as hex-ascii, with given separator */ #define WSLUA_OPTARG_ByteArray_tohex_LOWERCASE 2 /* True to use lower-case hex characters (default=false). */ #define WSLUA_OPTARG_ByteArray_tohex_SEPARATOR 3 /* A string separator to insert between hex bytes (default=nil). */ ByteArray ba = checkByteArray(L,1); gboolean lowercase = FALSE; const gchar* sep = NULL; if (!ba) return 0; lowercase = wslua_optbool(L,WSLUA_OPTARG_ByteArray_tohex_LOWERCASE,FALSE); sep = luaL_optstring(L,WSLUA_OPTARG_ByteArray_tohex_SEPARATOR,NULL); wslua_bin2hex(L, ba->data, ba->len, lowercase, sep); WSLUA_RETURN(1); /* A hex-ascii string representation of the ByteArray. */ }
WSLUA_METHOD ByteArray_base64_decode(lua_State* L) { /* Obtain a base64 decoded ByteArray */ ByteArray ba = checkByteArray(L,1); ByteArray ba2; gchar *data; size_t len; ba2 = g_byte_array_new(); data = (gchar*)g_malloc (ba->len + 1); memcpy(data, ba->data, ba->len); data[ba->len] = '\0'; len = ws_base64_decode_inplace(data); g_byte_array_append(ba2,data,(int)len); g_free(data); pushByteArray(L,ba2); WSLUA_RETURN(1); /* The created ByteArray. */ }
WSLUA_METHOD ByteArray_get_index(lua_State* L) { /* Get the value of a byte in a ByteArray */ #define WSLUA_ARG_ByteArray_get_index_INDEX 2 /* The position of the byte to get */ ByteArray ba = checkByteArray(L,1); int idx = luaL_checkint(L,WSLUA_ARG_ByteArray_get_index_INDEX); if (idx == 0 && ! g_str_equal(luaL_optstring(L,2,""),"0") ) { luaL_argerror(L,2,"bad index"); return 0; } if (idx < 0 || (guint)idx >= ba->len) { luaL_argerror(L,2,"index out of range"); return 0; } lua_pushnumber(L,ba->data[idx]); WSLUA_RETURN(1); /* The value [0-255] of the byte. */ }
WSLUA_METHOD ByteArray_set_size(lua_State* L) { /* Sets the size of a ByteArray, either truncating it or filling it with zeros. */ #define WSLUA_ARG_ByteArray_set_size_SIZE 2 /* New size of the array*/ ByteArray ba = checkByteArray(L,1); int siz = luaL_checkint(L,WSLUA_ARG_ByteArray_set_size_SIZE); guint8* padding; if (!ba) return 0; if (siz < 0) WSLUA_ERROR(ByteArray_set_size,"ByteArray size must be non-negative"); if (ba->len >= (guint)siz) { /* truncate */ g_byte_array_set_size(ba,siz); } else { /* fill */ padding = (guint8 *)g_malloc0(sizeof(guint8)*(siz - ba->len)); g_byte_array_append(ba,padding,siz - ba->len); g_free(padding); } return 0; }
WSLUA_METHOD ByteArray_subset(lua_State* L) { /* Obtain a segment of a ByteArray, as a new ByteArray. */ #define WSLUA_ARG_ByteArray_set_index_OFFSET 2 /* The position of the first byte (0=first) */ #define WSLUA_ARG_ByteArray_set_index_LENGTH 3 /* The length of the segment */ ByteArray ba = checkByteArray(L,1); int offset = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_OFFSET); int len = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_LENGTH); ByteArray sub; if ((offset + len) > (int)ba->len || offset < 0 || len < 1) { luaL_error(L,"Out Of Bounds"); return 0; } sub = g_byte_array_new(); g_byte_array_append(sub,ba->data + offset,len); pushByteArray(L,sub); WSLUA_RETURN(1); /* A ByteArray contaning the requested segment. */ }
WSLUA_METHOD ByteArray_raw(lua_State* L) { /* Obtain a Lua string of the binary bytes in a ByteArray. */ #define WSLUA_OPTARG_ByteArray_raw_OFFSET 2 /* The position of the first byte (default=0/first). */ #define WSLUA_OPTARG_ByteArray_raw_LENGTH 3 /* The length of the segment to get (default=all). */ ByteArray ba = checkByteArray(L,1); guint offset = (guint) luaL_optint(L,WSLUA_OPTARG_ByteArray_raw_OFFSET,0); int len; if (!ba) return 0; if (offset > ba->len) { WSLUA_OPTARG_ERROR(ByteArray_raw,OFFSET,"offset beyond end of byte array"); return 0; } len = luaL_optint(L,WSLUA_OPTARG_ByteArray_raw_LENGTH, ba->len - offset); if ((len < 0) || ((guint)len > (ba->len - offset))) len = ba->len - offset; lua_pushlstring(L, &(ba->data[offset]), len); WSLUA_RETURN(1); /* A Lua string of the binary bytes in the ByteArray. */ }
static int ByteArray_tostring(lua_State* L) { /* Obtain a string containing the bytes in a ByteArray so that it can be used in display filters (e.g. "01:23:45:67:89:AB") */ static const gchar* byte_to_str[] = { "00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F", "10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F", "20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F", "30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F", "40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F", "50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F", "60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F", "70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F", "80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F", "90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F", "A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF", "B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF", "C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF", "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF", "E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF", "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF" }; ByteArray ba = checkByteArray(L,1); int i; GString* s; if (!ba) return 0; s = g_string_new(""); for (i = 0; i < (int)ba->len; i++) { g_string_append(s,byte_to_str[(ba->data)[i]]); } lua_pushstring(L,s->str); g_string_free(s,TRUE); WSLUA_RETURN(1); /* A string contaning a representaion of the ByteArray. */ }