| File: | builds/wireshark/wireshark/epan/wslua/wslua_tvb.c |
| Warning: | line 1092, column 5 2nd function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* | |||
| 2 | * wslua_tvb.c | |||
| 3 | * | |||
| 4 | * Wireshark's interface to the Lua Programming Language | |||
| 5 | * | |||
| 6 | * (c) 2006, Luis E. Garcia Ontanon <[email protected]> | |||
| 7 | * (c) 2008, Balint Reczey <[email protected]> | |||
| 8 | * (c) 2009, Stig Bjorlykke <[email protected]> | |||
| 9 | * (c) 2014, Hadriel Kaplan <[email protected]> | |||
| 10 | * | |||
| 11 | * Wireshark - Network traffic analyzer | |||
| 12 | * By Gerald Combs <[email protected]> | |||
| 13 | * Copyright 1998 Gerald Combs | |||
| 14 | * | |||
| 15 | * SPDX-License-Identifier: GPL-2.0-or-later | |||
| 16 | */ | |||
| 17 | ||||
| 18 | #include "config.h" | |||
| 19 | ||||
| 20 | #include "wslua.h" | |||
| 21 | #include <epan/wmem_scopes.h> | |||
| 22 | ||||
| 23 | ||||
| 24 | /* WSLUA_MODULE Tvb Functions For Handling Packet Data */ | |||
| 25 | ||||
| 26 | ||||
| 27 | /* | |||
| 28 | * Tvb & TvbRange | |||
| 29 | * | |||
| 30 | * a Tvb represents a tvbuff_t in Lua. | |||
| 31 | * a TvbRange represents a range in a tvb (tvb,offset,length) its main purpose is to do bounds checking, | |||
| 32 | * It helps, too, simplifying argument passing to Tree. | |||
| 33 | * | |||
| 34 | * Normally in Wireshark explicit bounds checking is unnecessary as the tvbuff | |||
| 35 | * functions throw appropriate exceptions depending on why data wasn't present. | |||
| 36 | * In Lua, the interaction between the exception handling in epan and Lua's error | |||
| 37 | * handling, both of which use setjmp/longjmp, requires careful programming to | |||
| 38 | * avoid longjmp'ing down to the stack to a location that has already exited, | |||
| 39 | * particularly when Lua dissector calls are nested. TvbRange reduces the amount | |||
| 40 | * of possible exceptions (and TRY/CATCH to deal with them) by doing preemptive | |||
| 41 | * bounds checking - at a cost of making it impossible to support truncated | |||
| 42 | * captures where captured length < reported length (#15655). | |||
| 43 | * | |||
| 44 | * These lua objects refer to structures in wireshark that are freed independently from Lua's garbage collector. | |||
| 45 | * To avoid using pointers from Lua to Wireshark structures that are already freed, we maintain a list of the | |||
| 46 | * pointers each with a marker that tracks its expiry. | |||
| 47 | * | |||
| 48 | * All pointers are marked as expired when the dissection of the current frame is finished or when the garbage | |||
| 49 | * collector tries to free the object referring to the pointer, whichever comes first. | |||
| 50 | * | |||
| 51 | * All allocated memory chunks used for tracking the pointers' state are freed after marking the pointer as expired | |||
| 52 | * by the garbage collector or by the end of the dissection of the current frame, whichever comes second. | |||
| 53 | * | |||
| 54 | * We check the expiry state of the pointer before each access. | |||
| 55 | * | |||
| 56 | */ | |||
| 57 | ||||
| 58 | WSLUA_CLASS_DEFINE(Tvb,FAIL_ON_NULL_OR_EXPIRED("Tvb"))Tvb toTvb(lua_State* L, int idx) { Tvb* v = (Tvb*)lua_touserdata (L, idx); if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)" , idx, "Tvb", lua_typename(L, lua_type(L, idx))); return v ? * v : ((void*)0); } Tvb checkTvb(lua_State* L, int idx) { Tvb* p ; luaL_checktype(L,idx,7); p = (Tvb*)luaL_checkudata(L, idx, "Tvb" ); if (!*p) { luaL_argerror(L,idx,"null " "Tvb"); } else if ( (*p)->expired) { luaL_argerror(L,idx,"expired " "Tvb"); }; return p ? *p : ((void*)0); } Tvb* pushTvb(lua_State* L, Tvb v) { Tvb* p; luaL_checkstack(L,2,"Unable to grow stack\n"); p = (Tvb*)lua_newuserdatauv(L,sizeof(Tvb),1); *p = v; (lua_getfield (L, (-1000000 - 1000), ("Tvb"))); lua_setmetatable(L, -2); return p; }_Bool isTvb(lua_State* L,int i) { void *p; if(!lua_isuserdata (L,i)) return 0; p = lua_touserdata(L, i); lua_getfield(L, (- 1000000 - 1000), "Tvb"); if (p == ((void*)0) || !lua_getmetatable (L, i) || !lua_rawequal(L, -1, -2)) p=((void*)0); lua_settop( L, -(2)-1); return p ? 1 : 0; } Tvb shiftTvb(lua_State* L,int i) { Tvb* p; if(!lua_isuserdata(L,i)) return ((void*)0); p = (Tvb*)lua_touserdata(L, i); lua_getfield(L, (-1000000 - 1000 ), "Tvb"); if (p == ((void*)0) || !lua_getmetatable(L, i) || ! lua_rawequal(L, -1, -2)) p=((void*)0); lua_settop(L, -(2)-1); if (p) { (lua_rotate(L, (i), -1), lua_settop(L, -(1)-1)); return *p; } else return ((void*)0);} typedef int dummyTvb; | |||
| 59 | /* A <<lua_class_Tvb,`Tvb`>> represents the packet's buffer. It is passed as an argument to listeners and dissectors, | |||
| 60 | and can be used to extract information (via <<lua_class_TvbRange,`TvbRange`>>) from the packet's data. | |||
| 61 | ||||
| 62 | To create a <<lua_class_TvbRange,`TvbRange`>> the <<lua_class_Tvb,`Tvb`>> must be called with offset and length as optional arguments; | |||
| 63 | the offset defaults to 0 and the length to `tvb:captured_len()`. | |||
| 64 | ||||
| 65 | [WARNING] | |||
| 66 | ==== | |||
| 67 | Tvbs are usable only by the current listener or dissector call and are destroyed | |||
| 68 | as soon as the listener or dissector returns, so references to them are unusable once the function | |||
| 69 | has returned. | |||
| 70 | ==== | |||
| 71 | */ | |||
| 72 | ||||
| 73 | static GPtrArray* outstanding_Tvb; | |||
| 74 | static GPtrArray* outstanding_TvbRange; | |||
| 75 | ||||
| 76 | /* this is used to push Tvbs that were created brand new by wslua code */ | |||
| 77 | int push_wsluaTvb(lua_State* L, Tvb t) { | |||
| 78 | g_ptr_array_add(outstanding_Tvb,t); | |||
| 79 | pushTvb(L,t); | |||
| 80 | return 1; | |||
| 81 | } | |||
| 82 | ||||
| 83 | #define PUSH_TVBRANGE(L,t){g_ptr_array_add(outstanding_TvbRange,t);pushTvbRange(L,t);} {g_ptr_array_add(outstanding_TvbRange,t);pushTvbRange(L,t);} | |||
| 84 | ||||
| 85 | ||||
| 86 | static void free_Tvb(Tvb tvb) { | |||
| 87 | if (!tvb) return; | |||
| 88 | ||||
| 89 | if (!tvb->expired) { | |||
| 90 | tvb->expired = true1; | |||
| 91 | } else { | |||
| 92 | if (tvb->need_free) | |||
| 93 | tvb_free(tvb->ws_tvb); | |||
| 94 | g_free(tvb); | |||
| 95 | } | |||
| 96 | } | |||
| 97 | ||||
| 98 | void clear_outstanding_Tvb(void) { | |||
| 99 | while (outstanding_Tvb->len) { | |||
| 100 | Tvb tvb = (Tvb)g_ptr_array_remove_index_fast(outstanding_Tvb,0); | |||
| 101 | free_Tvb(tvb); | |||
| 102 | } | |||
| 103 | } | |||
| 104 | ||||
| 105 | /* this is used to push Tvbs that just point to pre-existing C-code Tvbs */ | |||
| 106 | Tvb* push_Tvb(lua_State* L, tvbuff_t* ws_tvb) { | |||
| 107 | Tvb tvb = (Tvb)g_malloc(sizeof(struct _wslua_tvb)); | |||
| 108 | tvb->ws_tvb = ws_tvb; | |||
| 109 | tvb->expired = false0; | |||
| 110 | tvb->need_free = false0; | |||
| 111 | g_ptr_array_add(outstanding_Tvb,tvb); | |||
| 112 | return pushTvb(L,tvb); | |||
| 113 | } | |||
| 114 | ||||
| 115 | ||||
| 116 | /* WSLUA_ATTRIBUTE Tvb_captured_length RO The captured length of the Tvb | |||
| 117 | (amount saved in the capture process). Mirrors `tvb:captured_len()`. */ | |||
| 118 | WSLUA_ATTRIBUTE_GET(Tvb,captured_length, {static int Tvb_get_captured_length (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_captured_length(obj ->ws_tvb));} return 1; } typedef void __dummyTvb_get_captured_length | |||
| 119 | lua_pushinteger(L, tvb_captured_length(obj->ws_tvb));static int Tvb_get_captured_length (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_captured_length(obj ->ws_tvb));} return 1; } typedef void __dummyTvb_get_captured_length | |||
| 120 | })static int Tvb_get_captured_length (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_captured_length(obj ->ws_tvb));} return 1; } typedef void __dummyTvb_get_captured_length; | |||
| 121 | ||||
| 122 | /* WSLUA_ATTRIBUTE Tvb_reported_length RO The reported length of the Tvb | |||
| 123 | (length on the network). Mirrors `tvb:reported_len()`. */ | |||
| 124 | WSLUA_ATTRIBUTE_GET(Tvb,reported_length, {static int Tvb_get_reported_length (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_reported_length(obj ->ws_tvb));} return 1; } typedef void __dummyTvb_get_reported_length | |||
| 125 | lua_pushinteger(L, tvb_reported_length(obj->ws_tvb));static int Tvb_get_reported_length (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_reported_length(obj ->ws_tvb));} return 1; } typedef void __dummyTvb_get_reported_length | |||
| 126 | })static int Tvb_get_reported_length (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_reported_length(obj ->ws_tvb));} return 1; } typedef void __dummyTvb_get_reported_length; | |||
| 127 | ||||
| 128 | /* WSLUA_ATTRIBUTE Tvb_raw_offset RO The raw offset of this (sub) Tvb in its | |||
| 129 | source Tvb. Mirrors `tvb:offset()`. */ | |||
| 130 | WSLUA_ATTRIBUTE_GET(Tvb,raw_offset, {static int Tvb_get_raw_offset (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_raw_offset(obj->ws_tvb)); } return 1; } typedef void __dummyTvb_get_raw_offset | |||
| 131 | lua_pushinteger(L, tvb_raw_offset(obj->ws_tvb));static int Tvb_get_raw_offset (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_raw_offset(obj->ws_tvb)); } return 1; } typedef void __dummyTvb_get_raw_offset | |||
| 132 | })static int Tvb_get_raw_offset (lua_State* L) { Tvb obj = checkTvb (L,1); { lua_pushinteger(L, tvb_raw_offset(obj->ws_tvb)); } return 1; } typedef void __dummyTvb_get_raw_offset; | |||
| 133 | ||||
| 134 | WSLUA_METAMETHODstatic int Tvb__tostring(lua_State* L) { | |||
| 135 | /* | |||
| 136 | Convert the bytes of a <<lua_class_Tvb,`Tvb`>> into a string of | |||
| 137 | the form `Tvb: captured=<C> reported=<R> bytes=<hex>`. | |||
| 138 | The hex preview is automatically truncated by `tvb_bytes_to_str` | |||
| 139 | to a small number of bytes (currently 36) so the rendering stays | |||
| 140 | useful in the debugger Variables view and in `print()` for large | |||
| 141 | packets. | |||
| 142 | */ | |||
| 143 | Tvb tvb = checkTvb(L,1); | |||
| 144 | int captured = tvb_captured_length(tvb->ws_tvb); | |||
| 145 | int reported = tvb_reported_length(tvb->ws_tvb); | |||
| 146 | char* str = tvb_bytes_to_str(NULL((void*)0), tvb->ws_tvb, 0, captured); | |||
| 147 | ||||
| 148 | lua_pushfstring(L, "Tvb: captured=%d reported=%d bytes=%s", | |||
| 149 | captured, reported, str ? str : ""); | |||
| 150 | ||||
| 151 | wmem_free(NULL((void*)0), str); | |||
| 152 | ||||
| 153 | WSLUA_RETURN(1)return (1); /* The string. */ | |||
| 154 | } | |||
| 155 | ||||
| 156 | /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */ | |||
| 157 | static int Tvb__gc(lua_State* L) { | |||
| 158 | Tvb tvb = toTvb(L,1); | |||
| 159 | ||||
| 160 | free_Tvb(tvb); | |||
| 161 | ||||
| 162 | return 0; | |||
| 163 | ||||
| 164 | } | |||
| 165 | ||||
| 166 | WSLUA_METHODstatic int Tvb_reported_len(lua_State* L) { | |||
| 167 | /* Obtain the reported length (length on the network) of a <<lua_class_Tvb,`Tvb`>>. */ | |||
| 168 | Tvb tvb = checkTvb(L,1); | |||
| 169 | ||||
| 170 | lua_pushinteger(L,tvb_reported_length(tvb->ws_tvb)); | |||
| 171 | WSLUA_RETURN(1)return (1); /* The reported length of the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 172 | } | |||
| 173 | ||||
| 174 | WSLUA_METHODstatic int Tvb_captured_len(lua_State* L) { | |||
| 175 | /* Obtain the captured length (amount saved in the capture process) of a <<lua_class_Tvb,`Tvb`>>. */ | |||
| 176 | Tvb tvb = checkTvb(L,1); | |||
| 177 | ||||
| 178 | lua_pushinteger(L,tvb_captured_length(tvb->ws_tvb)); | |||
| 179 | WSLUA_RETURN(1)return (1); /* The captured length of the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 180 | } | |||
| 181 | ||||
| 182 | WSLUA_METHODstatic int Tvb_len(lua_State* L) { | |||
| 183 | /* Obtain the captured length (amount saved in the capture process) of a <<lua_class_Tvb,`Tvb`>>. | |||
| 184 | Same as captured_len; kept only for backwards compatibility */ | |||
| 185 | Tvb tvb = checkTvb(L,1); | |||
| 186 | ||||
| 187 | lua_pushinteger(L,tvb_captured_length(tvb->ws_tvb)); | |||
| 188 | WSLUA_RETURN(1)return (1); /* The captured length of the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 189 | } | |||
| 190 | ||||
| 191 | WSLUA_METHODstatic int Tvb_reported_length_remaining(lua_State* L) { | |||
| 192 | /* Obtain the reported (not captured) length of packet data to end of a <<lua_class_Tvb,`Tvb`>> or 0 if the | |||
| 193 | offset is beyond the end of the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 194 | #define WSLUA_OPTARG_Tvb_reported_length_remaining_OFFSET2 2 /* The offset (in octets) from the beginning of the <<lua_class_Tvb,`Tvb`>>. Defaults to 0. */ | |||
| 195 | Tvb tvb = checkTvb(L,1); | |||
| 196 | int offset = (int) luaL_optinteger(L, WSLUA_OPTARG_Tvb_reported_length_remaining_OFFSET2, 0); | |||
| 197 | ||||
| 198 | lua_pushinteger(L,tvb_reported_length_remaining(tvb->ws_tvb, offset)); | |||
| 199 | WSLUA_RETURN(1)return (1); /* The remaining reported length of the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 200 | } | |||
| 201 | ||||
| 202 | WSLUA_METHODstatic int Tvb_bytes(lua_State* L) { | |||
| 203 | /* Obtain a <<lua_class_ByteArray,`ByteArray`>> from a <<lua_class_Tvb,`Tvb`>>. */ | |||
| 204 | #define WSLUA_OPTARG_Tvb_bytes_OFFSET2 2 /* The offset (in octets) from the beginning of the <<lua_class_Tvb,`Tvb`>>. Defaults to 0. */ | |||
| 205 | #define WSLUA_OPTARG_Tvb_bytes_LENGTH3 3 /* The length (in octets) of the range. Defaults to until the end of the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 206 | Tvb tvb = checkTvb(L,1); | |||
| 207 | GByteArray* ba; | |||
| 208 | #if LUA_VERSION_NUM504 >= 503 | |||
| 209 | int offset = (int)luaL_optinteger(L, WSLUA_OPTARG_Tvb_bytes_OFFSET2, 0); | |||
| 210 | int len = (int)luaL_optinteger(L, WSLUA_OPTARG_Tvb_bytes_LENGTH3, -1); | |||
| 211 | #else | |||
| 212 | int offset = luaL_optint(L, WSLUA_OPTARG_Tvb_bytes_OFFSET2, 0); | |||
| 213 | int len = luaL_optint(L,WSLUA_OPTARG_Tvb_bytes_LENGTH3,-1); | |||
| 214 | #endif | |||
| 215 | if (tvb->expired) { | |||
| 216 | luaL_error(L,"expired tvb"); | |||
| 217 | return 0; | |||
| 218 | } | |||
| 219 | ||||
| 220 | /* XXX - Why is *any* negative value allowed here to mean "to the | |||
| 221 | * end of the Tvb" instead of just -1 as elsewhere? | |||
| 222 | */ | |||
| 223 | if (len < 0) { | |||
| 224 | len = tvb_captured_length_remaining(tvb->ws_tvb,offset); | |||
| 225 | if (len < 0) { | |||
| 226 | luaL_error(L,"out of bounds"); | |||
| 227 | return 0; | |||
| 228 | } | |||
| 229 | } else if ( (unsigned)(len + offset) > tvb_captured_length(tvb->ws_tvb)) { | |||
| 230 | luaL_error(L,"Range is out of bounds"); | |||
| 231 | return 0; | |||
| 232 | } | |||
| 233 | ||||
| 234 | ba = g_byte_array_new(); | |||
| 235 | g_byte_array_append(ba, tvb_get_ptr(tvb->ws_tvb, offset, len), len); | |||
| 236 | pushByteArray(L,ba); | |||
| 237 | ||||
| 238 | WSLUA_RETURN(1)return (1); /* The <<lua_class_ByteArray,`ByteArray`>> object or nil. */ | |||
| 239 | } | |||
| 240 | ||||
| 241 | WSLUA_METHODstatic int Tvb_offset(lua_State* L) { | |||
| 242 | /* Returns the raw offset (from the beginning of the source <<lua_class_Tvb,`Tvb`>>) of a sub <<lua_class_Tvb,`Tvb`>>. */ | |||
| 243 | Tvb tvb = checkTvb(L,1); | |||
| 244 | ||||
| 245 | lua_pushinteger(L,tvb_raw_offset(tvb->ws_tvb)); | |||
| 246 | WSLUA_RETURN(1)return (1); /* The raw offset of the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 247 | } | |||
| 248 | ||||
| 249 | ||||
| 250 | #if USED_FOR_DOC_PURPOSES | |||
| 251 | WSLUA_METAMETHODstatic int Tvb__call(lua_State* L) { | |||
| 252 | /* Equivalent to tvb:range(...) */ | |||
| 253 | return 0; | |||
| 254 | } | |||
| 255 | #endif | |||
| 256 | ||||
| 257 | ||||
| 258 | WSLUA_METHODstatic int Tvb_range(lua_State* L) { | |||
| 259 | /* Creates a <<lua_class_TvbRange,`TvbRange`>> from this <<lua_class_Tvb,`Tvb`>>. */ | |||
| 260 | #define WSLUA_OPTARG_Tvb_range_OFFSET2 2 /* The offset (in octets) from the beginning of the <<lua_class_Tvb,`Tvb`>>. Defaults to 0. */ | |||
| 261 | #define WSLUA_OPTARG_Tvb_range_LENGTH3 3 /* The length (in octets) of the range. Defaults to -1, which specifies the remaining bytes in the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 262 | ||||
| 263 | Tvb tvb = checkTvb(L,1); | |||
| 264 | int offset = (int) luaL_optinteger(L,WSLUA_OPTARG_Tvb_range_OFFSET2,0); | |||
| 265 | int len = (int) luaL_optinteger(L,WSLUA_OPTARG_Tvb_range_LENGTH3,-1); | |||
| 266 | ||||
| 267 | if (push_TvbRange(L,tvb->ws_tvb,offset,len)) { | |||
| 268 | WSLUA_RETURN(1)return (1); /* The TvbRange */ | |||
| 269 | } | |||
| 270 | ||||
| 271 | return 0; | |||
| 272 | } | |||
| 273 | ||||
| 274 | WSLUA_METHODstatic int Tvb_raw(lua_State* L) { | |||
| 275 | /* Obtain a Lua string of the binary bytes in a <<lua_class_Tvb,`Tvb`>>. */ | |||
| 276 | #define WSLUA_OPTARG_Tvb_raw_OFFSET2 2 /* The position of the first byte. Default is 0, or the first byte. */ | |||
| 277 | #define WSLUA_OPTARG_Tvb_raw_LENGTH3 3 /* The length of the segment to get. Default is -1, or the remaining bytes in the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 278 | Tvb tvb = checkTvb(L,1); | |||
| 279 | int offset = (int) luaL_optinteger(L,WSLUA_OPTARG_Tvb_raw_OFFSET2,0); | |||
| 280 | int len = (int) luaL_optinteger(L,WSLUA_OPTARG_Tvb_raw_LENGTH3,-1); | |||
| 281 | ||||
| 282 | if (!tvb) return 0; | |||
| 283 | if (tvb->expired) { | |||
| 284 | luaL_error(L,"expired tvb"); | |||
| 285 | return 0; | |||
| 286 | } | |||
| 287 | ||||
| 288 | if ((unsigned)offset > tvb_captured_length(tvb->ws_tvb)) { | |||
| 289 | WSLUA_OPTARG_ERROR(Tvb_raw,OFFSET,"offset beyond end of Tvb"){ luaL_argerror(L,2, "Tvb_raw" ": " "offset beyond end of Tvb" ); }; | |||
| 290 | return 0; | |||
| 291 | } | |||
| 292 | ||||
| 293 | if (len == -1) { | |||
| 294 | len = tvb_captured_length_remaining(tvb->ws_tvb,offset); | |||
| 295 | if (len < 0) { | |||
| 296 | luaL_error(L,"out of bounds"); | |||
| 297 | return false0; | |||
| 298 | } | |||
| 299 | } else if ( (unsigned)(len + offset) > tvb_captured_length(tvb->ws_tvb)) { | |||
| 300 | luaL_error(L,"Range is out of bounds"); | |||
| 301 | return false0; | |||
| 302 | } | |||
| 303 | ||||
| 304 | lua_pushlstring(L, (const char*)tvb_get_ptr(tvb->ws_tvb, offset, len), len); | |||
| 305 | ||||
| 306 | WSLUA_RETURN(1)return (1); /* A Lua string of the binary bytes in the <<lua_class_Tvb,`Tvb`>>. */ | |||
| 307 | } | |||
| 308 | ||||
| 309 | WSLUA_METAMETHODstatic int Tvb__eq(lua_State* L) { | |||
| 310 | /* Checks whether contents of two <<lua_class_Tvb,`Tvb`>>s are equal. */ | |||
| 311 | Tvb tvb_l = checkTvb(L,1); | |||
| 312 | Tvb tvb_r = checkTvb(L,2); | |||
| 313 | ||||
| 314 | int len_l = tvb_captured_length(tvb_l->ws_tvb); | |||
| 315 | int len_r = tvb_captured_length(tvb_r->ws_tvb); | |||
| 316 | ||||
| 317 | /* it is not an error if their ds_tvb are different... they're just not equal */ | |||
| 318 | if (len_l == len_r) | |||
| 319 | { | |||
| 320 | const uint8_t* lp = tvb_get_ptr(tvb_l->ws_tvb, 0, len_l); | |||
| 321 | const uint8_t* rp = tvb_get_ptr(tvb_r->ws_tvb, 0, len_r); | |||
| 322 | ||||
| 323 | int ret = memcmp(lp, rp, len_l) == 0 ? 1 : 0; | |||
| 324 | ||||
| 325 | lua_pushboolean(L,ret); | |||
| 326 | } else { | |||
| 327 | lua_pushboolean(L,0); | |||
| 328 | } | |||
| 329 | ||||
| 330 | return 1; | |||
| 331 | } | |||
| 332 | ||||
| 333 | WSLUA_METHODSstatic const luaL_Reg Tvb_methods[] = { | |||
| 334 | WSLUA_CLASS_FNREG(Tvb,bytes){ "bytes", Tvb_bytes }, | |||
| 335 | WSLUA_CLASS_FNREG(Tvb,range){ "range", Tvb_range }, | |||
| 336 | WSLUA_CLASS_FNREG(Tvb,offset){ "offset", Tvb_offset }, | |||
| 337 | WSLUA_CLASS_FNREG(Tvb,reported_len){ "reported_len", Tvb_reported_len }, | |||
| 338 | WSLUA_CLASS_FNREG(Tvb,reported_length_remaining){ "reported_length_remaining", Tvb_reported_length_remaining }, | |||
| 339 | WSLUA_CLASS_FNREG(Tvb,captured_len){ "captured_len", Tvb_captured_len }, | |||
| 340 | WSLUA_CLASS_FNREG(Tvb,len){ "len", Tvb_len }, | |||
| 341 | WSLUA_CLASS_FNREG(Tvb,raw){ "raw", Tvb_raw }, | |||
| 342 | { NULL((void*)0), NULL((void*)0) } | |||
| 343 | }; | |||
| 344 | ||||
| 345 | WSLUA_METAstatic const luaL_Reg Tvb_meta[] = { | |||
| 346 | WSLUA_CLASS_MTREG(Tvb,eq){ "__" "eq", Tvb__eq }, | |||
| 347 | WSLUA_CLASS_MTREG(Tvb,tostring){ "__" "tostring", Tvb__tostring }, | |||
| 348 | {"__call", Tvb_range}, | |||
| 349 | { NULL((void*)0), NULL((void*)0) } | |||
| 350 | }; | |||
| 351 | ||||
| 352 | /* Read-only attributes. Registered so the Lua debugger can expand a Tvb | |||
| 353 | * in the Variables view without having to invoke methods. The attribute | |||
| 354 | * names intentionally differ from the corresponding method names to | |||
| 355 | * avoid the attribute/method collision check performed by | |||
| 356 | * wslua_push_attributes. */ | |||
| 357 | WSLUA_ATTRIBUTESstatic const wslua_attribute_table Tvb_attributes[] = { | |||
| 358 | WSLUA_ATTRIBUTE_ROREG(Tvb,captured_length){ "captured_length", Tvb_get_captured_length, ((void*)0) }, | |||
| 359 | WSLUA_ATTRIBUTE_ROREG(Tvb,reported_length){ "reported_length", Tvb_get_reported_length, ((void*)0) }, | |||
| 360 | WSLUA_ATTRIBUTE_ROREG(Tvb,raw_offset){ "raw_offset", Tvb_get_raw_offset, ((void*)0) }, | |||
| 361 | { NULL((void*)0), NULL((void*)0), NULL((void*)0) } | |||
| 362 | }; | |||
| 363 | ||||
| 364 | int Tvb_register(lua_State* L) { | |||
| 365 | WSLUA_REGISTER_CLASS_WITH_ATTRS(Tvb){ const wslua_class Tvb_class = { .name = "Tvb", .class_methods = Tvb_methods, .class_meta = Tvb_meta, .instance_methods = Tvb_methods , .instance_meta = Tvb_meta, .attrs = Tvb_attributes }; wslua_register_class (L, &Tvb_class); (lua_getfield(L, (-1000000 - 1000), ("Tvb" ))); lua_pushcclosure(L, (Tvb__gc), 0); lua_setfield(L, -2, "__gc" ); lua_settop(L, -(1)-1); }; | |||
| 366 | if (outstanding_Tvb != NULL((void*)0)) { | |||
| 367 | g_ptr_array_unref(outstanding_Tvb); | |||
| 368 | } | |||
| 369 | outstanding_Tvb = g_ptr_array_new(); | |||
| 370 | return 0; | |||
| 371 | } | |||
| 372 | ||||
| 373 | ||||
| 374 | ||||
| 375 | ||||
| 376 | WSLUA_CLASS_DEFINE(TvbRange,FAIL_ON_NULL("TvbRange"))TvbRange toTvbRange(lua_State* L, int idx) { TvbRange* v = (TvbRange *)lua_touserdata (L, idx); if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)" , idx, "TvbRange", lua_typename(L, lua_type(L, idx))); return v ? *v : ((void*)0); } TvbRange checkTvbRange(lua_State* L, int idx) { TvbRange* p; luaL_checktype(L,idx,7); p = (TvbRange*) luaL_checkudata(L, idx, "TvbRange"); if (! *p) luaL_argerror( L,idx,"null " "TvbRange"); return p ? *p : ((void*)0); } TvbRange * pushTvbRange(lua_State* L, TvbRange v) { TvbRange* p; luaL_checkstack (L,2,"Unable to grow stack\n"); p = (TvbRange*)lua_newuserdatauv (L,sizeof(TvbRange),1); *p = v; (lua_getfield(L, (-1000000 - 1000 ), ("TvbRange"))); lua_setmetatable(L, -2); return p; }_Bool isTvbRange (lua_State* L,int i) { void *p; if(!lua_isuserdata(L,i)) return 0; p = lua_touserdata(L, i); lua_getfield(L, (-1000000 - 1000 ), "TvbRange"); if (p == ((void*)0) || !lua_getmetatable(L, i ) || !lua_rawequal(L, -1, -2)) p=((void*)0); lua_settop(L, -( 2)-1); return p ? 1 : 0; } TvbRange shiftTvbRange(lua_State* L ,int i) { TvbRange* p; if(!lua_isuserdata(L,i)) return ((void *)0); p = (TvbRange*)lua_touserdata(L, i); lua_getfield(L, (- 1000000 - 1000), "TvbRange"); if (p == ((void*)0) || !lua_getmetatable (L, i) || !lua_rawequal(L, -1, -2)) p=((void*)0); lua_settop( L, -(2)-1); if (p) { (lua_rotate(L, (i), -1), lua_settop(L, - (1)-1)); return *p; } else return ((void*)0);} typedef int dummyTvbRange; | |||
| 377 | /* | |||
| 378 | A <<lua_class_TvbRange,`TvbRange`>> represents a usable range of a <<lua_class_Tvb,`Tvb`>> and is used to extract data from the <<lua_class_Tvb,`Tvb`>> that generated it. | |||
| 379 | ||||
| 380 | <<lua_class_TvbRange,`TvbRange`>>s are created by calling a <<lua_class_Tvb,`Tvb`>> (e.g. 'tvb(offset,length)'). | |||
| 381 | A length of -1, which is the default, means to use the bytes up to the end of the <<lua_class_Tvb,`Tvb`>>. | |||
| 382 | If the <<lua_class_TvbRange,`TvbRange`>> span is outside the <<lua_class_Tvb,`Tvb`>>'s range the creation will cause a runtime error. | |||
| 383 | */ | |||
| 384 | ||||
| 385 | static void free_TvbRange(TvbRange tvbr) { | |||
| 386 | if (!(tvbr && tvbr->tvb)) return; | |||
| 387 | ||||
| 388 | if (!tvbr->tvb->expired) { | |||
| 389 | tvbr->tvb->expired = true1; | |||
| 390 | } else { | |||
| 391 | free_Tvb(tvbr->tvb); | |||
| 392 | g_free(tvbr); | |||
| 393 | } | |||
| 394 | } | |||
| 395 | ||||
| 396 | void clear_outstanding_TvbRange(void) { | |||
| 397 | while (outstanding_TvbRange->len) { | |||
| 398 | TvbRange tvbr = (TvbRange)g_ptr_array_remove_index_fast(outstanding_TvbRange,0); | |||
| 399 | free_TvbRange(tvbr); | |||
| 400 | } | |||
| 401 | } | |||
| 402 | ||||
| 403 | ||||
| 404 | bool_Bool push_TvbRange(lua_State* L, tvbuff_t* ws_tvb, int offset, int len) { | |||
| 405 | TvbRange tvbr; | |||
| 406 | ||||
| 407 | if (!ws_tvb) { | |||
| 408 | luaL_error(L,"expired tvb"); | |||
| 409 | return false0; | |||
| 410 | } | |||
| 411 | ||||
| 412 | /* XXX - What if offset is negative? In epan/tvbuff.h functions, a negative | |||
| 413 | * offset means "the offset from the end of the backing tvbuff at which | |||
| 414 | * the new tvbuff's data begins", but that's not done consistently in this | |||
| 415 | * code when taking ranges and passing an offset. And maybe we don't want | |||
| 416 | * to support negative offsets at all in the future (#20103). | |||
| 417 | */ | |||
| 418 | if (len == -1) { | |||
| 419 | len = tvb_captured_length_remaining(ws_tvb,offset); | |||
| 420 | if (len < 0) { | |||
| 421 | luaL_error(L,"out of bounds"); | |||
| 422 | return false0; | |||
| 423 | } | |||
| 424 | } else if (len < -1) { | |||
| 425 | luaL_error(L, "negative length in tvb range"); | |||
| 426 | return false0; | |||
| 427 | } else if ( (unsigned)(len + offset) > tvb_captured_length(ws_tvb)) { | |||
| 428 | luaL_error(L,"Range is out of bounds"); | |||
| 429 | return false0; | |||
| 430 | } | |||
| 431 | ||||
| 432 | tvbr = (TvbRange)g_malloc(sizeof(struct _wslua_tvbrange)); | |||
| 433 | tvbr->tvb = (Tvb)g_malloc(sizeof(struct _wslua_tvb)); | |||
| 434 | tvbr->tvb->ws_tvb = ws_tvb; | |||
| 435 | tvbr->tvb->expired = false0; | |||
| 436 | tvbr->tvb->need_free = false0; | |||
| 437 | tvbr->offset = offset; | |||
| 438 | tvbr->len = len; | |||
| 439 | ||||
| 440 | PUSH_TVBRANGE(L,tvbr){g_ptr_array_add(outstanding_TvbRange,tvbr);pushTvbRange(L,tvbr );}; | |||
| 441 | ||||
| 442 | return true1; | |||
| 443 | } | |||
| 444 | ||||
| 445 | ||||
| 446 | WSLUA_METHODstatic int TvbRange_tvb(lua_State *L) { | |||
| 447 | /* Creates a new <<lua_class_Tvb,`Tvb`>> from a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 448 | ||||
| 449 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 450 | Tvb tvb; | |||
| 451 | ||||
| 452 | if (! (tvbr && tvbr->tvb)) return 0; | |||
| 453 | if (tvbr->tvb->expired) { | |||
| 454 | luaL_error(L,"expired tvb"); | |||
| 455 | return 0; | |||
| 456 | } | |||
| 457 | ||||
| 458 | if (tvb_bytes_exist(tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len)) { | |||
| 459 | tvb = (Tvb)g_malloc(sizeof(struct _wslua_tvb)); | |||
| 460 | tvb->expired = false0; | |||
| 461 | tvb->need_free = false0; | |||
| 462 | tvb->ws_tvb = tvb_new_subset_length(tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len); | |||
| 463 | return push_wsluaTvb(L, tvb); | |||
| 464 | } else { | |||
| 465 | luaL_error(L,"Out Of Bounds"); | |||
| 466 | return 0; | |||
| 467 | } | |||
| 468 | } | |||
| 469 | ||||
| 470 | ||||
| 471 | /* | |||
| 472 | * get a Blefuscuoan unsigned integer from a tvb | |||
| 473 | */ | |||
| 474 | WSLUA_METHODstatic int TvbRange_uint(lua_State* L) { | |||
| 475 | /* Get a Big Endian (network order) unsigned integer from a <<lua_class_TvbRange,`TvbRange`>>. | |||
| 476 | The range must be 1-4 octets long. */ | |||
| 477 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 478 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 479 | if (tvbr->tvb->expired) { | |||
| 480 | luaL_error(L,"expired tvb"); | |||
| 481 | return 0; | |||
| 482 | } | |||
| 483 | ||||
| 484 | switch (tvbr->len) { | |||
| 485 | case 1: | |||
| 486 | lua_pushinteger(L,tvb_get_uint8(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 487 | return 1; | |||
| 488 | case 2: | |||
| 489 | lua_pushinteger(L,tvb_get_ntohs(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 490 | return 1; | |||
| 491 | case 3: | |||
| 492 | lua_pushinteger(L,tvb_get_ntoh24(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 493 | return 1; | |||
| 494 | case 4: | |||
| 495 | lua_pushinteger(L,tvb_get_ntohl(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 496 | WSLUA_RETURN(1)return (1); /* The unsigned integer value. */ | |||
| 497 | default: | |||
| 498 | luaL_error(L,"TvbRange:uint() does not handle %d byte integers",tvbr->len); | |||
| 499 | return 0; | |||
| 500 | } | |||
| 501 | } | |||
| 502 | ||||
| 503 | /* | |||
| 504 | * get a Lilliputian unsigned integer from a tvb | |||
| 505 | */ | |||
| 506 | WSLUA_METHODstatic int TvbRange_le_uint(lua_State* L) { | |||
| 507 | /* Get a Little Endian unsigned integer from a <<lua_class_TvbRange,`TvbRange`>>. | |||
| 508 | The range must be 1-4 octets long. */ | |||
| 509 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 510 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 511 | if (tvbr->tvb->expired) { | |||
| 512 | luaL_error(L,"expired tvb"); | |||
| 513 | return 0; | |||
| 514 | } | |||
| 515 | ||||
| 516 | switch (tvbr->len) { | |||
| 517 | case 1: | |||
| 518 | /* XXX unsigned anyway */ | |||
| 519 | lua_pushinteger(L,(lua_Integer)(unsigned)tvb_get_uint8(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 520 | return 1; | |||
| 521 | case 2: | |||
| 522 | lua_pushinteger(L,tvb_get_letohs(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 523 | return 1; | |||
| 524 | case 3: | |||
| 525 | lua_pushinteger(L,tvb_get_letoh24(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 526 | return 1; | |||
| 527 | case 4: | |||
| 528 | lua_pushinteger(L,tvb_get_letohl(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 529 | WSLUA_RETURN(1)return (1); /* The unsigned integer value */ | |||
| 530 | default: | |||
| 531 | luaL_error(L,"TvbRange:le_uint() does not handle %d byte integers",tvbr->len); | |||
| 532 | return 0; | |||
| 533 | } | |||
| 534 | } | |||
| 535 | ||||
| 536 | /* | |||
| 537 | * get a Blefuscuoan unsigned 64 bit integer from a tvb | |||
| 538 | */ | |||
| 539 | WSLUA_METHODstatic int TvbRange_uint64(lua_State* L) { | |||
| 540 | /* Get a Big Endian (network order) unsigned 64 bit integer from a <<lua_class_TvbRange,`TvbRange`>>, as a <<lua_class_UInt64,`UInt64`>> object. | |||
| 541 | The range must be 1-8 octets long. */ | |||
| 542 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 543 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 544 | if (tvbr->tvb->expired) { | |||
| 545 | luaL_error(L,"expired tvb"); | |||
| 546 | return 0; | |||
| 547 | } | |||
| 548 | ||||
| 549 | switch (tvbr->len) { | |||
| 550 | case 1: | |||
| 551 | pushUInt64(L,tvb_get_uint8(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 552 | return 1; | |||
| 553 | case 2: | |||
| 554 | pushUInt64(L,tvb_get_ntohs(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 555 | return 1; | |||
| 556 | case 3: | |||
| 557 | pushUInt64(L,tvb_get_ntoh24(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 558 | return 1; | |||
| 559 | case 4: | |||
| 560 | pushUInt64(L,tvb_get_ntohl(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 561 | return 1; | |||
| 562 | case 5: | |||
| 563 | pushUInt64(L,tvb_get_ntoh40(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 564 | return 1; | |||
| 565 | case 6: | |||
| 566 | pushUInt64(L,tvb_get_ntoh48(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 567 | return 1; | |||
| 568 | case 7: | |||
| 569 | pushUInt64(L,tvb_get_ntoh56(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 570 | return 1; | |||
| 571 | case 8: | |||
| 572 | pushUInt64(L,tvb_get_ntoh64(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 573 | WSLUA_RETURN(1)return (1); /* The <<lua_class_UInt64,`UInt64`>> object. */ | |||
| 574 | default: | |||
| 575 | luaL_error(L,"TvbRange:uint64() does not handle %d byte integers",tvbr->len); | |||
| 576 | return 0; | |||
| 577 | } | |||
| 578 | } | |||
| 579 | ||||
| 580 | /* | |||
| 581 | * get a Lilliputian unsigned 64 bit integer from a tvb | |||
| 582 | */ | |||
| 583 | WSLUA_METHODstatic int TvbRange_le_uint64(lua_State* L) { | |||
| 584 | /* Get a Little Endian unsigned 64 bit integer from a <<lua_class_TvbRange,`TvbRange`>>, as a <<lua_class_UInt64,`UInt64`>> object. | |||
| 585 | The range must be 1-8 octets long. */ | |||
| 586 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 587 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 588 | if (tvbr->tvb->expired) { | |||
| 589 | luaL_error(L,"expired tvb"); | |||
| 590 | return 0; | |||
| 591 | } | |||
| 592 | ||||
| 593 | switch (tvbr->len) { | |||
| 594 | case 1: | |||
| 595 | pushUInt64(L,tvb_get_uint8(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 596 | return 1; | |||
| 597 | case 2: | |||
| 598 | pushUInt64(L,tvb_get_letohs(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 599 | return 1; | |||
| 600 | case 3: | |||
| 601 | pushUInt64(L,tvb_get_letoh24(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 602 | return 1; | |||
| 603 | case 4: | |||
| 604 | pushUInt64(L,tvb_get_letohl(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 605 | return 1; | |||
| 606 | case 5: | |||
| 607 | pushUInt64(L,tvb_get_letoh40(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 608 | return 1; | |||
| 609 | case 6: | |||
| 610 | pushUInt64(L,tvb_get_letoh48(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 611 | return 1; | |||
| 612 | case 7: | |||
| 613 | pushUInt64(L,tvb_get_letoh56(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 614 | return 1; | |||
| 615 | case 8: | |||
| 616 | pushUInt64(L,tvb_get_letoh64(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 617 | WSLUA_RETURN(1)return (1); /* The <<lua_class_UInt64,`UInt64`>> object. */ | |||
| 618 | default: | |||
| 619 | luaL_error(L,"TvbRange:le_uint64() does not handle %d byte integers",tvbr->len); | |||
| 620 | return 0; | |||
| 621 | } | |||
| 622 | } | |||
| 623 | ||||
| 624 | /* | |||
| 625 | * get a Blefuscuoan signed integer from a tvb | |||
| 626 | */ | |||
| 627 | WSLUA_METHODstatic int TvbRange_int(lua_State* L) { | |||
| 628 | /* Get a Big Endian (network order) signed integer from a <<lua_class_TvbRange,`TvbRange`>>. | |||
| 629 | The range must be 1-4 octets long. */ | |||
| 630 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 631 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 632 | if (tvbr->tvb->expired) { | |||
| 633 | luaL_error(L,"expired tvb"); | |||
| 634 | return 0; | |||
| 635 | } | |||
| 636 | ||||
| 637 | switch (tvbr->len) { | |||
| 638 | case 1: | |||
| 639 | lua_pushinteger(L,tvb_get_int8(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 640 | return 1; | |||
| 641 | case 2: | |||
| 642 | lua_pushinteger(L,tvb_get_ntohis(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 643 | return 1; | |||
| 644 | case 3: | |||
| 645 | lua_pushinteger(L,tvb_get_ntohi24(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 646 | return 1; | |||
| 647 | case 4: | |||
| 648 | lua_pushinteger(L,tvb_get_ntohil(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 649 | WSLUA_RETURN(1)return (1); /* The signed integer value. */ | |||
| 650 | /* | |||
| 651 | * XXX: | |||
| 652 | * lua uses double so we have 52 bits to play with | |||
| 653 | * we are missing 5 and 6 byte integers within lua's range | |||
| 654 | * and 64 bit integers are not supported (there's a lib for | |||
| 655 | * lua that does). | |||
| 656 | */ | |||
| 657 | default: | |||
| 658 | luaL_error(L,"TvbRange:int() does not handle %d byte integers",tvbr->len); | |||
| 659 | return 0; | |||
| 660 | } | |||
| 661 | } | |||
| 662 | ||||
| 663 | /* | |||
| 664 | * get a Lilliputian signed integer from a tvb | |||
| 665 | */ | |||
| 666 | WSLUA_METHODstatic int TvbRange_le_int(lua_State* L) { | |||
| 667 | /* Get a Little Endian signed integer from a <<lua_class_TvbRange,`TvbRange`>>. | |||
| 668 | The range must be 1-4 octets long. */ | |||
| 669 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 670 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 671 | if (tvbr->tvb->expired) { | |||
| 672 | luaL_error(L,"expired tvb"); | |||
| 673 | return 0; | |||
| 674 | } | |||
| 675 | ||||
| 676 | switch (tvbr->len) { | |||
| 677 | case 1: | |||
| 678 | lua_pushinteger(L,tvb_get_int8(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 679 | return 1; | |||
| 680 | case 2: | |||
| 681 | lua_pushinteger(L,tvb_get_letohis(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 682 | return 1; | |||
| 683 | case 3: | |||
| 684 | lua_pushinteger(L,tvb_get_letohi24(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 685 | return 1; | |||
| 686 | case 4: | |||
| 687 | lua_pushinteger(L,tvb_get_letohil(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 688 | WSLUA_RETURN(1)return (1); /* The signed integer value. */ | |||
| 689 | default: | |||
| 690 | luaL_error(L,"TvbRange:le_int() does not handle %d byte integers",tvbr->len); | |||
| 691 | return 0; | |||
| 692 | } | |||
| 693 | } | |||
| 694 | ||||
| 695 | /* | |||
| 696 | * get a Blefuscuoan signed 64 bit integer from a tvb | |||
| 697 | */ | |||
| 698 | WSLUA_METHODstatic int TvbRange_int64(lua_State* L) { | |||
| 699 | /* Get a Big Endian (network order) signed 64 bit integer from a <<lua_class_TvbRange,`TvbRange`>>, as an <<lua_class_Int64,`Int64`>> object. | |||
| 700 | The range must be 1-8 octets long. */ | |||
| 701 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 702 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 703 | if (tvbr->tvb->expired) { | |||
| 704 | luaL_error(L,"expired tvb"); | |||
| 705 | return 0; | |||
| 706 | } | |||
| 707 | ||||
| 708 | switch (tvbr->len) { | |||
| 709 | case 1: | |||
| 710 | pushInt64(L,tvb_get_int8(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 711 | return 1; | |||
| 712 | case 2: | |||
| 713 | pushInt64(L,tvb_get_ntohis(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 714 | return 1; | |||
| 715 | case 3: | |||
| 716 | pushInt64(L,tvb_get_ntohi24(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 717 | return 1; | |||
| 718 | case 4: | |||
| 719 | pushInt64(L,tvb_get_ntohil(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 720 | return 1; | |||
| 721 | case 5: | |||
| 722 | pushInt64(L,tvb_get_ntohi40(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 723 | return 1; | |||
| 724 | case 6: | |||
| 725 | pushInt64(L,tvb_get_ntohi48(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 726 | return 1; | |||
| 727 | case 7: | |||
| 728 | pushInt64(L,tvb_get_ntohi56(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 729 | return 1; | |||
| 730 | case 8: | |||
| 731 | pushInt64(L,tvb_get_ntohi64(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 732 | WSLUA_RETURN(1)return (1); /* The <<lua_class_Int64,`Int64`>> object. */ | |||
| 733 | default: | |||
| 734 | luaL_error(L,"TvbRange:int64() does not handle %d byte integers",tvbr->len); | |||
| 735 | return 0; | |||
| 736 | } | |||
| 737 | } | |||
| 738 | ||||
| 739 | /* | |||
| 740 | * get a Lilliputian signed 64 bit integer from a tvb | |||
| 741 | */ | |||
| 742 | WSLUA_METHODstatic int TvbRange_le_int64(lua_State* L) { | |||
| 743 | /* Get a Little Endian signed 64 bit integer from a <<lua_class_TvbRange,`TvbRange`>>, as an <<lua_class_Int64,`Int64`>> object. | |||
| 744 | The range must be 1-8 octets long. */ | |||
| 745 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 746 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 747 | if (tvbr->tvb->expired) { | |||
| 748 | luaL_error(L,"expired tvb"); | |||
| 749 | return 0; | |||
| 750 | } | |||
| 751 | ||||
| 752 | switch (tvbr->len) { | |||
| 753 | case 1: | |||
| 754 | pushInt64(L,tvb_get_int8(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 755 | return 1; | |||
| 756 | case 2: | |||
| 757 | pushInt64(L,tvb_get_letohis(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 758 | return 1; | |||
| 759 | case 3: | |||
| 760 | pushInt64(L,tvb_get_letohi24(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 761 | return 1; | |||
| 762 | case 4: | |||
| 763 | pushInt64(L,tvb_get_letohil(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 764 | return 1; | |||
| 765 | case 5: | |||
| 766 | pushInt64(L,tvb_get_letohi40(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 767 | return 1; | |||
| 768 | case 6: | |||
| 769 | pushInt64(L,tvb_get_letohi48(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 770 | return 1; | |||
| 771 | case 7: | |||
| 772 | pushInt64(L,tvb_get_letohi56(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 773 | return 1; | |||
| 774 | case 8: | |||
| 775 | pushInt64(L,tvb_get_letohi64(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 776 | WSLUA_RETURN(1)return (1); /* The <<lua_class_Int64,`Int64`>> object. */ | |||
| 777 | default: | |||
| 778 | luaL_error(L,"TvbRange:le_int64() does not handle %d byte integers",tvbr->len); | |||
| 779 | return 0; | |||
| 780 | } | |||
| 781 | } | |||
| 782 | ||||
| 783 | /* | |||
| 784 | * get a Blefuscuoan float | |||
| 785 | */ | |||
| 786 | WSLUA_METHODstatic int TvbRange_float(lua_State* L) { | |||
| 787 | /* Get a Big Endian (network order) floating point number from a <<lua_class_TvbRange,`TvbRange`>>. | |||
| 788 | The range must be 4 or 8 octets long. */ | |||
| 789 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 790 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 791 | if (tvbr->tvb->expired) { | |||
| 792 | luaL_error(L,"expired tvb"); | |||
| 793 | return 0; | |||
| 794 | } | |||
| 795 | ||||
| 796 | switch (tvbr->len) { | |||
| 797 | case 4: | |||
| 798 | lua_pushnumber(L,(double)tvb_get_ntohieee_float(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 799 | return 1; | |||
| 800 | case 8: | |||
| 801 | lua_pushnumber(L,tvb_get_ntohieee_double(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 802 | WSLUA_RETURN(1)return (1); /* The floating point value. */ | |||
| 803 | default: | |||
| 804 | luaL_error(L,"TvbRange:float() does not handle %d byte floating numbers",tvbr->len); | |||
| 805 | return 0; | |||
| 806 | } | |||
| 807 | } | |||
| 808 | ||||
| 809 | /* | |||
| 810 | * get a Lilliputian float | |||
| 811 | */ | |||
| 812 | WSLUA_METHODstatic int TvbRange_le_float(lua_State* L) { | |||
| 813 | /* Get a Little Endian floating point number from a <<lua_class_TvbRange,`TvbRange`>>. | |||
| 814 | The range must be 4 or 8 octets long. */ | |||
| 815 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 816 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 817 | ||||
| 818 | switch (tvbr->len) { | |||
| 819 | case 4: | |||
| 820 | lua_pushnumber(L,tvb_get_letohieee_float(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 821 | return 1; | |||
| 822 | case 8: | |||
| 823 | lua_pushnumber(L,tvb_get_letohieee_double(tvbr->tvb->ws_tvb,tvbr->offset)); | |||
| 824 | WSLUA_RETURN(1)return (1); /* The floating point value. */ | |||
| 825 | default: | |||
| 826 | luaL_error(L,"TvbRange:le_float() does not handle %d byte floating numbers",tvbr->len); | |||
| 827 | return 0; | |||
| 828 | } | |||
| 829 | } | |||
| 830 | ||||
| 831 | WSLUA_METHODstatic int TvbRange_ipv4(lua_State* L) { | |||
| 832 | /* Get an IPv4 Address from a <<lua_class_TvbRange,`TvbRange`>>, as an <<lua_class_Address,`Address`>> object. */ | |||
| 833 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 834 | Address addr; | |||
| 835 | ||||
| 836 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 837 | if (tvbr->tvb->expired) { | |||
| 838 | luaL_error(L,"expired tvb"); | |||
| 839 | return 0; | |||
| 840 | } | |||
| 841 | ||||
| 842 | if (tvbr->len != 4) { | |||
| 843 | WSLUA_ERROR(TvbRange_ipv4,"The range must be 4 octets long"){ luaL_error(L, "%s%s", "TvbRange_ipv4" ": ", "The range must be 4 octets long" ); }; | |||
| 844 | return 0; | |||
| 845 | } | |||
| 846 | ||||
| 847 | addr = g_new(address,1)((address *) g_malloc_n ((1), sizeof (address))); | |||
| 848 | alloc_address_tvb(NULL((void*)0),addr,AT_IPv4,sizeof(uint32_t),tvbr->tvb->ws_tvb,tvbr->offset); | |||
| 849 | pushAddress(L,addr); | |||
| 850 | ||||
| 851 | WSLUA_RETURN(1)return (1); /* The IPv4 <<lua_class_Address,`Address`>> object. */ | |||
| 852 | } | |||
| 853 | ||||
| 854 | WSLUA_METHODstatic int TvbRange_le_ipv4(lua_State* L) { | |||
| 855 | /* Get an Little Endian IPv4 Address from a <<lua_class_TvbRange,`TvbRange`>>, as an <<lua_class_Address,`Address`>> object. */ | |||
| 856 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 857 | Address addr; | |||
| 858 | uint32_t ip_addr; | |||
| 859 | ||||
| 860 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 861 | if (tvbr->tvb->expired) { | |||
| 862 | luaL_error(L,"expired tvb"); | |||
| 863 | return 0; | |||
| 864 | } | |||
| 865 | ||||
| 866 | if (tvbr->len != 4) { | |||
| 867 | WSLUA_ERROR(TvbRange_ipv4,"The range must be 4 octets long"){ luaL_error(L, "%s%s", "TvbRange_ipv4" ": ", "The range must be 4 octets long" ); }; | |||
| 868 | return 0; | |||
| 869 | } | |||
| 870 | ||||
| 871 | addr = g_new(address,1)((address *) g_malloc_n ((1), sizeof (address))); | |||
| 872 | ip_addr = GUINT32_SWAP_LE_BE(tvb_get_ipv4(tvbr->tvb->ws_tvb,tvbr->offset))(((guint32) ( (((guint32) (tvb_get_ipv4(tvbr->tvb->ws_tvb ,tvbr->offset)) & (guint32) 0x000000ffU) << 24) | (((guint32) (tvb_get_ipv4(tvbr->tvb->ws_tvb,tvbr->offset )) & (guint32) 0x0000ff00U) << 8) | (((guint32) (tvb_get_ipv4 (tvbr->tvb->ws_tvb,tvbr->offset)) & (guint32) 0x00ff0000U ) >> 8) | (((guint32) (tvb_get_ipv4(tvbr->tvb->ws_tvb ,tvbr->offset)) & (guint32) 0xff000000U) >> 24)) )); | |||
| 873 | alloc_address_wmem(NULL((void*)0), addr, AT_IPv4, sizeof(ip_addr), &ip_addr); | |||
| 874 | pushAddress(L,addr); | |||
| 875 | ||||
| 876 | WSLUA_RETURN(1)return (1); /* The IPv4 <<lua_class_Address,`Address`>> object. */ | |||
| 877 | } | |||
| 878 | ||||
| 879 | WSLUA_METHODstatic int TvbRange_ipv6(lua_State* L) { | |||
| 880 | /* Get an IPv6 Address from a <<lua_class_TvbRange,`TvbRange`>>, as an <<lua_class_Address,`Address`>> object. */ | |||
| 881 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 882 | Address addr; | |||
| 883 | ||||
| 884 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 885 | if (tvbr->tvb->expired) { | |||
| 886 | luaL_error(L,"expired tvb"); | |||
| 887 | return 0; | |||
| 888 | } | |||
| 889 | ||||
| 890 | if (tvbr->len != 16) { | |||
| 891 | WSLUA_ERROR(TvbRange_ipv6,"The range must be 16 octets long"){ luaL_error(L, "%s%s", "TvbRange_ipv6" ": ", "The range must be 16 octets long" ); }; | |||
| 892 | return 0; | |||
| 893 | } | |||
| 894 | ||||
| 895 | addr = g_new(address,1)((address *) g_malloc_n ((1), sizeof (address))); | |||
| 896 | alloc_address_tvb(NULL((void*)0),addr,AT_IPv6,16,tvbr->tvb->ws_tvb,tvbr->offset); | |||
| 897 | pushAddress(L,addr); | |||
| 898 | ||||
| 899 | WSLUA_RETURN(1)return (1); /* The IPv6 <<lua_class_Address,`Address`>> object. */ | |||
| 900 | } | |||
| 901 | ||||
| 902 | WSLUA_METHODstatic int TvbRange_ether(lua_State* L) { | |||
| 903 | /* Get an Ethernet Address from a <<lua_class_TvbRange,`TvbRange`>>, as an <<lua_class_Address,`Address`>> object. */ | |||
| 904 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 905 | Address addr; | |||
| 906 | ||||
| 907 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 908 | if (tvbr->tvb->expired) { | |||
| 909 | luaL_error(L,"expired tvb"); | |||
| 910 | return 0; | |||
| 911 | } | |||
| 912 | ||||
| 913 | if (tvbr->len != 6) { | |||
| 914 | WSLUA_ERROR(TvbRange_ether,"The range must be 6 bytes long"){ luaL_error(L, "%s%s", "TvbRange_ether" ": ", "The range must be 6 bytes long" ); }; | |||
| 915 | return 0; | |||
| 916 | } | |||
| 917 | ||||
| 918 | addr = g_new(address,1)((address *) g_malloc_n ((1), sizeof (address))); | |||
| 919 | alloc_address_tvb(NULL((void*)0),addr,AT_ETHER,6,tvbr->tvb->ws_tvb,tvbr->offset); | |||
| 920 | pushAddress(L,addr); | |||
| 921 | ||||
| 922 | WSLUA_RETURN(1)return (1); /* The Ethernet <<lua_class_Address,`Address`>> object. */ | |||
| 923 | } | |||
| 924 | ||||
| 925 | WSLUA_METHODstatic int TvbRange_nstime(lua_State* L) { | |||
| 926 | /* Obtain a time_t structure from a <<lua_class_TvbRange,`TvbRange`>>, as an <<lua_class_NSTime,`NSTime`>> object. */ | |||
| 927 | #define WSLUA_OPTARG_TvbRange_nstime_ENCODING2 2 /* An optional ENC_* encoding value to use */ | |||
| 928 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 929 | NSTime nstime; | |||
| 930 | const unsigned encoding = (unsigned) luaL_optinteger(L, WSLUA_OPTARG_TvbRange_nstime_ENCODING2, 0); | |||
| 931 | ||||
| 932 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 933 | if (tvbr->tvb->expired) { | |||
| 934 | luaL_error(L,"expired tvb"); | |||
| 935 | return 0; | |||
| 936 | } | |||
| 937 | ||||
| 938 | if (encoding & ~ENC_STR_TIME_MASK0x001F0000) { | |||
| 939 | WSLUA_OPTARG_ERROR(TvbRange_nstime, ENCODING, "invalid encoding value"){ luaL_argerror(L,2, "TvbRange_nstime" ": " "invalid encoding value" ); }; | |||
| 940 | return 0; | |||
| 941 | } | |||
| 942 | ||||
| 943 | nstime = g_new(nstime_t,1)((nstime_t *) g_malloc_n ((1), sizeof (nstime_t))); | |||
| 944 | ||||
| 945 | if (encoding == 0) { | |||
| 946 | if (tvbr->len == 4) { | |||
| 947 | nstime->secs = tvb_get_ntohl(tvbr->tvb->ws_tvb, tvbr->offset); | |||
| 948 | nstime->nsecs = 0; | |||
| 949 | } else if (tvbr->len == 8) { | |||
| 950 | nstime->secs = tvb_get_ntohl(tvbr->tvb->ws_tvb, tvbr->offset); | |||
| 951 | nstime->nsecs = tvb_get_ntohl(tvbr->tvb->ws_tvb, tvbr->offset + 4); | |||
| 952 | } else { | |||
| 953 | g_free(nstime); | |||
| 954 | WSLUA_ERROR(TvbRange_nstime,"The range must be 4 or 8 bytes long"){ luaL_error(L, "%s%s", "TvbRange_nstime" ": ", "The range must be 4 or 8 bytes long" ); }; | |||
| 955 | return 0; | |||
| 956 | } | |||
| 957 | pushNSTime(L, nstime); | |||
| 958 | lua_pushinteger(L, tvbr->len); | |||
| 959 | } | |||
| 960 | else { | |||
| 961 | unsigned endoff = 0; | |||
| 962 | nstime_t *retval = tvb_get_string_time(tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len, | |||
| 963 | encoding, nstime, &endoff); | |||
| 964 | if (!retval || endoff == 0) { | |||
| 965 | g_free(nstime); | |||
| 966 | /* push nil nstime and offset */ | |||
| 967 | lua_pushnil(L); | |||
| 968 | lua_pushnil(L); | |||
| 969 | } | |||
| 970 | else { | |||
| 971 | pushNSTime(L, nstime); | |||
| 972 | lua_pushinteger(L, endoff); | |||
| 973 | } | |||
| 974 | } | |||
| 975 | ||||
| 976 | WSLUA_RETURN(2)return (2); /* The <<lua_class_NSTime,`NSTime`>> object and number of bytes used, or nil on failure. */ | |||
| 977 | } | |||
| 978 | ||||
| 979 | WSLUA_METHODstatic int TvbRange_le_nstime(lua_State* L) { | |||
| 980 | /* Obtain a nstime from a <<lua_class_TvbRange,`TvbRange`>>, as an <<lua_class_NSTime,`NSTime`>> object. */ | |||
| 981 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 982 | NSTime nstime; | |||
| 983 | ||||
| 984 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 985 | if (tvbr->tvb->expired) { | |||
| 986 | luaL_error(L,"expired tvb"); | |||
| 987 | return 0; | |||
| 988 | } | |||
| 989 | ||||
| 990 | nstime = g_new(nstime_t,1)((nstime_t *) g_malloc_n ((1), sizeof (nstime_t))); | |||
| 991 | ||||
| 992 | if (tvbr->len == 4) { | |||
| 993 | nstime->secs = tvb_get_letohl(tvbr->tvb->ws_tvb, tvbr->offset); | |||
| 994 | nstime->nsecs = 0; | |||
| 995 | } else if (tvbr->len == 8) { | |||
| 996 | nstime->secs = tvb_get_letohl(tvbr->tvb->ws_tvb, tvbr->offset); | |||
| 997 | nstime->nsecs = tvb_get_letohl(tvbr->tvb->ws_tvb, tvbr->offset + 4); | |||
| 998 | } else { | |||
| 999 | g_free(nstime); | |||
| 1000 | WSLUA_ERROR(TvbRange_nstime,"The range must be 4 or 8 bytes long"){ luaL_error(L, "%s%s", "TvbRange_nstime" ": ", "The range must be 4 or 8 bytes long" ); }; | |||
| 1001 | return 0; | |||
| 1002 | } | |||
| 1003 | ||||
| 1004 | pushNSTime(L, nstime); | |||
| 1005 | ||||
| 1006 | WSLUA_RETURN(1)return (1); /* The <<lua_class_NSTime,`NSTime`>> object. */ | |||
| 1007 | } | |||
| 1008 | ||||
| 1009 | WSLUA_METHODstatic int TvbRange_string(lua_State* L) { | |||
| 1010 | /* Obtain a string from a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1011 | #define WSLUA_OPTARG_TvbRange_string_ENCODING2 2 /* The encoding to use. Defaults to ENC_ASCII. */ | |||
| 1012 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1013 | unsigned encoding = (unsigned)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_string_ENCODING2, ENC_ASCII0x00000000|ENC_NA0x00000000); | |||
| 1014 | char * str; | |||
| 1015 | ||||
| 1016 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 1017 | if (tvbr->tvb->expired) { | |||
| 1018 | luaL_error(L,"expired tvb"); | |||
| 1019 | return 0; | |||
| 1020 | } | |||
| 1021 | ||||
| 1022 | str = (char*)tvb_get_string_enc(NULL((void*)0),tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len,encoding); | |||
| 1023 | lua_pushlstring(L, str, strlen(str)); | |||
| 1024 | wmem_free(NULL((void*)0), str); | |||
| 1025 | ||||
| 1026 | WSLUA_RETURN(1)return (1); /* A string containing all bytes in the <<lua_class_TvbRange,`TvbRange`>> including all zeroes (e.g., "a\000bc\000"). */ | |||
| 1027 | } | |||
| 1028 | ||||
| 1029 | static int TvbRange_ustring_any(lua_State* L, bool_Bool little_endian) { | |||
| 1030 | /* Obtain a UTF-16 encoded string from a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1031 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1032 | char * str; | |||
| 1033 | ||||
| 1034 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 1035 | if (tvbr->tvb->expired) { | |||
| 1036 | luaL_error(L,"expired tvb"); | |||
| 1037 | return 0; | |||
| 1038 | } | |||
| 1039 | ||||
| 1040 | str = (char*)tvb_get_string_enc(NULL((void*)0),tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len,(little_endian ? ENC_UTF_160x00000004|ENC_LITTLE_ENDIAN0x80000000 : ENC_UTF_160x00000004|ENC_BIG_ENDIAN0x00000000)); | |||
| 1041 | lua_pushlstring(L, str, strlen(str)); | |||
| 1042 | wmem_free(NULL((void*)0), str); | |||
| 1043 | ||||
| 1044 | return 1; /* The string */ | |||
| 1045 | } | |||
| 1046 | ||||
| 1047 | WSLUA_METHODstatic int TvbRange_ustring(lua_State* L) { | |||
| 1048 | /* Obtain a Big Endian (network order) UTF-16 encoded string from a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1049 | WSLUA_RETURN(TvbRange_ustring_any(L, false))return (TvbRange_ustring_any(L, 0)); /* A string containing all bytes in the <<lua_class_TvbRange,`TvbRange`>> including all zeroes (e.g., "a\000bc\000"). */ | |||
| 1050 | } | |||
| 1051 | ||||
| 1052 | WSLUA_METHODstatic int TvbRange_le_ustring(lua_State* L) { | |||
| 1053 | /* Obtain a Little Endian UTF-16 encoded string from a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1054 | WSLUA_RETURN(TvbRange_ustring_any(L, true))return (TvbRange_ustring_any(L, 1)); /* A string containing all bytes in the <<lua_class_TvbRange,`TvbRange`>> including all zeroes (e.g., "a\000bc\000"). */ | |||
| 1055 | } | |||
| 1056 | ||||
| 1057 | WSLUA_METHODstatic int TvbRange_stringz(lua_State* L) { | |||
| 1058 | /* Obtain a zero terminated string from a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1059 | #define WSLUA_OPTARG_TvbRange_stringz_ENCODING2 2 /* The encoding to use. Defaults to ENC_ASCII. */ | |||
| 1060 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1061 | unsigned encoding = (unsigned)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_stringz_ENCODING2, ENC_ASCII0x00000000|ENC_NA0x00000000); | |||
| 1062 | char *str = NULL((void*)0); | |||
| 1063 | unsigned length; | |||
| ||||
| 1064 | const char* error = NULL((void*)0); | |||
| 1065 | ||||
| 1066 | if ( !(tvbr
| |||
| 1067 | if (tvbr->tvb->expired) { | |||
| 1068 | luaL_error(L,"expired tvb"); | |||
| 1069 | return 0; | |||
| 1070 | } | |||
| 1071 | ||||
| 1072 | /* XXX - This leaks outside the length of the TvbRange to scan the entire | |||
| 1073 | * underlying tvbuffer. It has always done that, but that does seem odd. */ | |||
| 1074 | TRY{ except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(& except_sn, &except_ch, catch_spec, 1); if (_setjmp (except_ch .except_jmp)) *(&exc) = &except_ch.except_obj; else * (&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { | |||
| 1075 | str = (char*)tvb_get_stringz_enc(NULL((void*)0),tvbr->tvb->ws_tvb,tvbr->offset,&length,encoding); | |||
| 1076 | } CATCH(DissectorError)if (except_state == 0 && exc != 0 && exc-> except_id.except_code == (6) && (except_state |= 1)) { | |||
| 1077 | /* Presumably an unsupported encoding */ | |||
| 1078 | error = lua_pushstring(L, GET_MESSAGE((exc)->except_message)); | |||
| 1079 | } CATCH_BOUNDS_ERRORSif (except_state == 0 && exc != 0 && (exc-> except_id.except_code == (1) || exc->except_id.except_code == (4) || exc->except_id.except_code == (3) || exc->except_id .except_code == (2) || exc->except_id.except_code == (7)) && (except_state|=1)) { | |||
| 1080 | error = lua_pushstring(L, "Out of bounds"); | |||
| 1081 | } ENDTRYif(!(except_state&1) && exc != 0) except_rethrow( exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; | |||
| 1082 | ||||
| 1083 | if (error) { | |||
| 1084 | /* By converting the exceptions into Lua errors, we also add | |||
| 1085 | * the Lua traceback. */ | |||
| 1086 | WSLUA_ERROR(TvbRange_stringz, lua_tostring(L, 1)){ luaL_error(L, "%s%s", "TvbRange_stringz" ": ", lua_tolstring (L, (1), ((void*)0))); }; | |||
| 1087 | return 0; | |||
| 1088 | } | |||
| 1089 | ||||
| 1090 | lua_pushstring(L, str); | |||
| 1091 | wmem_free(NULL((void*)0), str); | |||
| 1092 | lua_pushinteger(L, length); | |||
| ||||
| 1093 | ||||
| 1094 | WSLUA_RETURN(2)return (2); /* The string containing all bytes in the <<lua_class_TvbRange,`TvbRange`>> up to the first terminating zero, and the length of that string. */ | |||
| 1095 | } | |||
| 1096 | ||||
| 1097 | WSLUA_METHODstatic int TvbRange_strsize(lua_State* L) { | |||
| 1098 | /* | |||
| 1099 | Find the size of a zero terminated string from a <<lua_class_TvbRange,`TvbRange`>>. | |||
| 1100 | The size of the string includes the terminating zero. */ | |||
| 1101 | #define WSLUA_OPTARG_TvbRange_strsize_ENCODING2 2 /* The encoding to use. Defaults to ENC_ASCII. */ | |||
| 1102 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1103 | unsigned encoding = (unsigned)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_strsize_ENCODING2, ENC_ASCII0x00000000|ENC_NA0x00000000); | |||
| 1104 | const char* error = NULL((void*)0); | |||
| 1105 | ||||
| 1106 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 1107 | if (tvbr->tvb->expired) { | |||
| 1108 | luaL_error(L,"expired tvb"); | |||
| 1109 | return 0; | |||
| 1110 | } | |||
| 1111 | ||||
| 1112 | /* XXX - This leaks outside the length of the TvbRange to scan the entire | |||
| 1113 | * underlying tvbuffer. It has always done that, but that does seem odd. */ | |||
| 1114 | TRY{ except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(& except_sn, &except_ch, catch_spec, 1); if (_setjmp (except_ch .except_jmp)) *(&exc) = &except_ch.except_obj; else * (&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { | |||
| 1115 | lua_pushinteger(L, tvb_strsize_enc(tvbr->tvb->ws_tvb, tvbr->offset, encoding)); | |||
| 1116 | } CATCH(DissectorError)if (except_state == 0 && exc != 0 && exc-> except_id.except_code == (6) && (except_state |= 1)) { | |||
| 1117 | /* Presumably an unsupported encoding */ | |||
| 1118 | error = lua_pushstring(L, GET_MESSAGE((exc)->except_message)); | |||
| 1119 | } CATCH_BOUNDS_ERRORSif (except_state == 0 && exc != 0 && (exc-> except_id.except_code == (1) || exc->except_id.except_code == (4) || exc->except_id.except_code == (3) || exc->except_id .except_code == (2) || exc->except_id.except_code == (7)) && (except_state|=1)) { | |||
| 1120 | error = lua_pushstring(L, "Out of bounds"); | |||
| 1121 | } ENDTRYif(!(except_state&1) && exc != 0) except_rethrow( exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; | |||
| 1122 | ||||
| 1123 | if (error) { | |||
| 1124 | /* By converting the exceptions into Lua errors, we also add | |||
| 1125 | * the Lua traceback. */ | |||
| 1126 | WSLUA_ERROR(TvbRange_strsize, lua_tostring(L, 1)){ luaL_error(L, "%s%s", "TvbRange_strsize" ": ", lua_tolstring (L, (1), ((void*)0))); }; | |||
| 1127 | return 0; | |||
| 1128 | } | |||
| 1129 | ||||
| 1130 | WSLUA_RETURN(1)return (1); /* Length of the zero terminated string. */ | |||
| 1131 | } | |||
| 1132 | ||||
| 1133 | ||||
| 1134 | static int TvbRange_ustringz_any(lua_State* L, bool_Bool little_endian) { | |||
| 1135 | /* Obtain a zero terminated string from a TvbRange */ | |||
| 1136 | unsigned count; | |||
| 1137 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1138 | int offset; | |||
| 1139 | gunichar2 uchar; | |||
| 1140 | char *str; | |||
| 1141 | ||||
| 1142 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 1143 | if (tvbr->tvb->expired) { | |||
| 1144 | luaL_error(L,"expired tvb"); | |||
| 1145 | return 0; | |||
| 1146 | } | |||
| 1147 | ||||
| 1148 | offset = tvbr->offset; | |||
| 1149 | do { | |||
| 1150 | if (!tvb_bytes_exist (tvbr->tvb->ws_tvb, offset, 2)) { | |||
| 1151 | luaL_error(L,"out of bounds"); | |||
| 1152 | return 0; | |||
| 1153 | } | |||
| 1154 | /* Endianness doesn't matter when looking for null */ | |||
| 1155 | uchar = tvb_get_ntohs (tvbr->tvb->ws_tvb, offset); | |||
| 1156 | offset += 2; | |||
| 1157 | } while (uchar != 0); | |||
| 1158 | ||||
| 1159 | str = (char*)tvb_get_stringz_enc(NULL((void*)0),tvbr->tvb->ws_tvb,tvbr->offset,&count, | |||
| 1160 | (little_endian ? ENC_UTF_160x00000004|ENC_LITTLE_ENDIAN0x80000000 : ENC_UTF_160x00000004|ENC_BIG_ENDIAN0x00000000)); | |||
| 1161 | lua_pushstring(L, str); | |||
| 1162 | lua_pushinteger(L,count); | |||
| 1163 | wmem_free(NULL((void*)0), str); | |||
| 1164 | ||||
| 1165 | return 2; /* The zero terminated string, the length found in tvbr */ | |||
| 1166 | } | |||
| 1167 | ||||
| 1168 | WSLUA_METHODstatic int TvbRange_ustringz(lua_State* L) { | |||
| 1169 | /* Obtain a Big Endian (network order) UTF-16 encoded zero terminated string from a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1170 | WSLUA_RETURN(TvbRange_ustringz_any(L, false))return (TvbRange_ustringz_any(L, 0)); /* Two return values: the zero terminated string, and the length. */ | |||
| 1171 | } | |||
| 1172 | ||||
| 1173 | WSLUA_METHODstatic int TvbRange_le_ustringz(lua_State* L) { | |||
| 1174 | /* Obtain a Little Endian UTF-16 encoded zero terminated string from a TvbRange */ | |||
| 1175 | WSLUA_RETURN(TvbRange_ustringz_any(L, true))return (TvbRange_ustringz_any(L, 1)); /* Two return values: the zero terminated string, and the length. */ | |||
| 1176 | } | |||
| 1177 | ||||
| 1178 | WSLUA_METHODstatic int TvbRange_bytes(lua_State* L) { | |||
| 1179 | /* Obtain a <<lua_class_ByteArray,`ByteArray`>> from a <<lua_class_TvbRange,`TvbRange`>>. | |||
| 1180 | ||||
| 1181 | Starting in 1.11.4, this function also takes an optional `encoding` argument, | |||
| 1182 | which can be set to `ENC_STR_HEX` to decode a hex-string from the <<lua_class_TvbRange,`TvbRange`>> | |||
| 1183 | into the returned <<lua_class_ByteArray,`ByteArray`>>. The `encoding` can be bitwise-or'ed with one | |||
| 1184 | or more separator encodings, such as `ENC_SEP_COLON`, to allow separators | |||
| 1185 | to occur between each pair of hex characters. | |||
| 1186 | ||||
| 1187 | The return value also now returns the number of bytes used as a second return value. | |||
| 1188 | ||||
| 1189 | On failure or error, nil is returned for both return values. | |||
| 1190 | ||||
| 1191 | [NOTE] | |||
| 1192 | ==== | |||
| 1193 | The encoding type of the hex string should also be set, for example | |||
| 1194 | `ENC_ASCII` or `ENC_UTF_8`, along with `ENC_STR_HEX`. | |||
| 1195 | ==== | |||
| 1196 | */ | |||
| 1197 | #define WSLUA_OPTARG_TvbRange_bytes_ENCODING2 2 /* An optional ENC_* encoding value to use */ | |||
| 1198 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1199 | GByteArray* ba; | |||
| 1200 | uint8_t* raw; | |||
| 1201 | const unsigned encoding = (unsigned)luaL_optinteger(L, WSLUA_OPTARG_TvbRange_bytes_ENCODING2, 0); | |||
| 1202 | ||||
| 1203 | ||||
| 1204 | if ( !(tvbr && tvbr->tvb)) return 0; | |||
| 1205 | if (tvbr->tvb->expired) { | |||
| 1206 | luaL_error(L,"expired tvb"); | |||
| 1207 | return 0; | |||
| 1208 | } | |||
| 1209 | ||||
| 1210 | if (encoding == 0) { | |||
| 1211 | ba = g_byte_array_new(); | |||
| 1212 | raw = (uint8_t *)tvb_memdup(NULL((void*)0),tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len); | |||
| 1213 | g_byte_array_append(ba,raw,tvbr->len); | |||
| 1214 | wmem_free(NULL((void*)0), raw); | |||
| 1215 | pushByteArray(L,ba); | |||
| 1216 | lua_pushinteger(L, tvbr->len); | |||
| 1217 | } | |||
| 1218 | else if ((encoding & ENC_STR_HEX0x02000000) == 0) { | |||
| 1219 | WSLUA_OPTARG_ERROR(TvbRange_nstime, ENCODING, "invalid encoding value"){ luaL_argerror(L,2, "TvbRange_nstime" ": " "invalid encoding value" ); }; | |||
| 1220 | } | |||
| 1221 | else { | |||
| 1222 | unsigned endoff = 0; | |||
| 1223 | GByteArray* retval; | |||
| 1224 | ||||
| 1225 | ba = g_byte_array_new(); | |||
| 1226 | retval = tvb_get_string_bytes(tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len, | |||
| 1227 | encoding, ba, &endoff); | |||
| 1228 | if (!retval || endoff == 0) { | |||
| 1229 | g_byte_array_free(ba, true1); | |||
| 1230 | /* push nil nstime and offset */ | |||
| 1231 | lua_pushnil(L); | |||
| 1232 | lua_pushnil(L); | |||
| 1233 | } | |||
| 1234 | else { | |||
| 1235 | pushByteArray(L,ba); | |||
| 1236 | lua_pushinteger(L, endoff); | |||
| 1237 | } | |||
| 1238 | } | |||
| 1239 | ||||
| 1240 | WSLUA_RETURN(2)return (2); /* The <<lua_class_ByteArray,`ByteArray`>> object or nil, and number of bytes consumed or nil. */ | |||
| 1241 | } | |||
| 1242 | ||||
| 1243 | WSLUA_METHODstatic int TvbRange_bitfield(lua_State* L) { | |||
| 1244 | /* Get a bitfield from a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1245 | #define WSLUA_OPTARG_TvbRange_bitfield_POSITION2 2 /* The bit offset (link:https://en.wikipedia.org/wiki/Bit_numbering#MSB_0_bit_numbering[MSB 0 bit numbering]) from the beginning of the <<lua_class_TvbRange,`TvbRange`>>. Defaults to 0. */ | |||
| 1246 | #define WSLUA_OPTARG_TvbRange_bitfield_LENGTH3 3 /* The length in bits of the field. Defaults to 1. */ | |||
| 1247 | ||||
| 1248 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1249 | unsigned pos = (unsigned)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_bitfield_POSITION2,0); | |||
| 1250 | unsigned len = (unsigned)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_bitfield_LENGTH3,1); | |||
| 1251 | ||||
| 1252 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1253 | if (tvbr->tvb->expired) { | |||
| 1254 | luaL_error(L,"expired tvb"); | |||
| 1255 | return 0; | |||
| 1256 | } | |||
| 1257 | ||||
| 1258 | if ((pos+len) > (tvbr->len<<3)) { | |||
| 1259 | luaL_error(L, "Requested bitfield out of range"); | |||
| 1260 | return 0; | |||
| 1261 | } | |||
| 1262 | ||||
| 1263 | if (len <= 32) { | |||
| 1264 | /* XXX - If LUA_INTEGER_SIZE is 4 (on Lua 5.3/5.4 it's usually 8), then | |||
| 1265 | * for len == 32 an unsigned won't necessarily fit in a lua_Integer. | |||
| 1266 | * Should we use a UInt64 then? | |||
| 1267 | */ | |||
| 1268 | WRAP_NON_LUA_EXCEPTIONS({ volatile _Bool has_error = 0; { except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(&except_sn, &except_ch, catch_spec , 1); if (_setjmp (except_ch.except_jmp)) *(&exc) = & except_ch.except_obj; else *(&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { lua_pushinteger(L,tvb_get_bits32 (tvbr->tvb->ws_tvb,tvbr->offset*8 + pos, len, 0)); } if (except_state == 0 && exc != 0 && (exc-> except_id.except_code == (1) || exc->except_id.except_code == (4) || exc->except_id.except_code == (7)) && ( except_state|=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree ->tree, ((exc)->except_id.except_code), ((exc)->except_message )); } if (except_state == 0 && exc != 0 && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); lua_pushfstring(L, "%s: %s", __func__, ((exc)->except_message ) ? ((exc)->except_message) : "Malformed packet"); has_error = 1; } if(!(except_state&1) && exc != 0) except_rethrow (exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; if (has_error) { lua_error(L); } } | |||
| 1269 | lua_pushinteger(L,tvb_get_bits32(tvbr->tvb->ws_tvb,tvbr->offset*8 + pos, len, false));{ volatile _Bool has_error = 0; { except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(&except_sn, &except_ch, catch_spec , 1); if (_setjmp (except_ch.except_jmp)) *(&exc) = & except_ch.except_obj; else *(&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { lua_pushinteger(L,tvb_get_bits32 (tvbr->tvb->ws_tvb,tvbr->offset*8 + pos, len, 0)); } if (except_state == 0 && exc != 0 && (exc-> except_id.except_code == (1) || exc->except_id.except_code == (4) || exc->except_id.except_code == (7)) && ( except_state|=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree ->tree, ((exc)->except_id.except_code), ((exc)->except_message )); } if (except_state == 0 && exc != 0 && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); lua_pushfstring(L, "%s: %s", __func__, ((exc)->except_message ) ? ((exc)->except_message) : "Malformed packet"); has_error = 1; } if(!(except_state&1) && exc != 0) except_rethrow (exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; if (has_error) { lua_error(L); } } | |||
| 1270 | ){ volatile _Bool has_error = 0; { except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(&except_sn, &except_ch, catch_spec , 1); if (_setjmp (except_ch.except_jmp)) *(&exc) = & except_ch.except_obj; else *(&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { lua_pushinteger(L,tvb_get_bits32 (tvbr->tvb->ws_tvb,tvbr->offset*8 + pos, len, 0)); } if (except_state == 0 && exc != 0 && (exc-> except_id.except_code == (1) || exc->except_id.except_code == (4) || exc->except_id.except_code == (7)) && ( except_state|=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree ->tree, ((exc)->except_id.except_code), ((exc)->except_message )); } if (except_state == 0 && exc != 0 && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); lua_pushfstring(L, "%s: %s", __func__, ((exc)->except_message ) ? ((exc)->except_message) : "Malformed packet"); has_error = 1; } if(!(except_state&1) && exc != 0) except_rethrow (exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; if (has_error) { lua_error(L); } } | |||
| 1271 | } else if (len <= 64) { | |||
| 1272 | WRAP_NON_LUA_EXCEPTIONS({ volatile _Bool has_error = 0; { except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(&except_sn, &except_ch, catch_spec , 1); if (_setjmp (except_ch.except_jmp)) *(&exc) = & except_ch.except_obj; else *(&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { pushUInt64(L,tvb_get_bits64(tvbr ->tvb->ws_tvb,tvbr->offset*8 + pos, len, 0)); } if ( except_state == 0 && exc != 0 && (exc->except_id .except_code == (1) || exc->except_id.except_code == (4) || exc->except_id.except_code == (7)) && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); } if (except_state == 0 && exc != 0 && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); lua_pushfstring(L, "%s: %s", __func__, ((exc)->except_message ) ? ((exc)->except_message) : "Malformed packet"); has_error = 1; } if(!(except_state&1) && exc != 0) except_rethrow (exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; if (has_error) { lua_error(L); } } | |||
| 1273 | pushUInt64(L,tvb_get_bits64(tvbr->tvb->ws_tvb,tvbr->offset*8 + pos, len, false));{ volatile _Bool has_error = 0; { except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(&except_sn, &except_ch, catch_spec , 1); if (_setjmp (except_ch.except_jmp)) *(&exc) = & except_ch.except_obj; else *(&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { pushUInt64(L,tvb_get_bits64(tvbr ->tvb->ws_tvb,tvbr->offset*8 + pos, len, 0)); } if ( except_state == 0 && exc != 0 && (exc->except_id .except_code == (1) || exc->except_id.except_code == (4) || exc->except_id.except_code == (7)) && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); } if (except_state == 0 && exc != 0 && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); lua_pushfstring(L, "%s: %s", __func__, ((exc)->except_message ) ? ((exc)->except_message) : "Malformed packet"); has_error = 1; } if(!(except_state&1) && exc != 0) except_rethrow (exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; if (has_error) { lua_error(L); } } | |||
| 1274 | ){ volatile _Bool has_error = 0; { except_t *volatile exc; volatile int except_state = 0; static const except_id_t catch_spec[] = { { 1, 0 } }; { struct except_stacknode except_sn; struct except_catch except_ch; except_setup_try(&except_sn, &except_ch, catch_spec , 1); if (_setjmp (except_ch.except_jmp)) *(&exc) = & except_ch.except_obj; else *(&exc) = 0; if(except_state & 1) except_state |= 2; except_state &= ~1; if (except_state == 0 && exc == 0) { pushUInt64(L,tvb_get_bits64(tvbr ->tvb->ws_tvb,tvbr->offset*8 + pos, len, 0)); } if ( except_state == 0 && exc != 0 && (exc->except_id .except_code == (1) || exc->except_id.except_code == (4) || exc->except_id.except_code == (7)) && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); } if (except_state == 0 && exc != 0 && (except_state |=1)) { show_exception(lua_tvb, lua_pinfo, lua_tree->tree, ((exc)->except_id.except_code), ((exc)->except_message )); lua_pushfstring(L, "%s: %s", __func__, ((exc)->except_message ) ? ((exc)->except_message) : "Malformed packet"); has_error = 1; } if(!(except_state&1) && exc != 0) except_rethrow (exc); except_free(except_ch.except_obj.except_dyndata); except_pop (); };}; if (has_error) { lua_error(L); } } | |||
| 1275 | } else { | |||
| 1276 | luaL_error(L,"TvbRange:bitfield() does not handle %d bits",len); | |||
| 1277 | return 0; | |||
| 1278 | } | |||
| 1279 | ||||
| 1280 | WSLUA_RETURN(1)return (1); /* The bitfield value */ | |||
| 1281 | } | |||
| 1282 | ||||
| 1283 | WSLUA_METHODstatic int TvbRange_range(lua_State* L) { | |||
| 1284 | /* Creates a sub-<<lua_class_TvbRange,`TvbRange`>> from this <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1285 | #define WSLUA_OPTARG_TvbRange_range_OFFSET2 2 /* The offset (in octets) from the beginning of the <<lua_class_TvbRange,`TvbRange`>>. Defaults to 0. */ | |||
| 1286 | #define WSLUA_OPTARG_TvbRange_range_LENGTH3 3 /* The length (in octets) of the range. Defaults to until the end of the <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1287 | ||||
| 1288 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1289 | int offset = (int)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_range_OFFSET2,0); | |||
| 1290 | int len = (int)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_range_LENGTH3,-1); | |||
| 1291 | ||||
| 1292 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1293 | if (tvbr->tvb->expired) { | |||
| 1294 | luaL_error(L,"expired tvb"); | |||
| 1295 | return 0; | |||
| 1296 | } | |||
| 1297 | ||||
| 1298 | if (offset < 0) { | |||
| 1299 | WSLUA_OPTARG_ERROR(TvbRange_range,OFFSET,"offset before start of TvbRange"){ luaL_argerror(L,2, "TvbRange_range" ": " "offset before start of TvbRange" ); }; | |||
| 1300 | return 0; | |||
| 1301 | } | |||
| 1302 | if ((unsigned)offset > tvbr->len) { | |||
| 1303 | WSLUA_OPTARG_ERROR(TvbRange_range,OFFSET,"offset beyond end of TvbRange"){ luaL_argerror(L,2, "TvbRange_range" ": " "offset beyond end of TvbRange" ); }; | |||
| 1304 | return 0; | |||
| 1305 | } | |||
| 1306 | ||||
| 1307 | if (len == -1) { | |||
| 1308 | len = tvbr->len - offset; | |||
| 1309 | } | |||
| 1310 | if (len < 0) { | |||
| 1311 | luaL_error(L,"out of bounds"); | |||
| 1312 | return 0; | |||
| 1313 | } else if ( (unsigned)(len + offset) > tvbr->len) { | |||
| 1314 | luaL_error(L,"Range is out of bounds"); | |||
| 1315 | return 0; | |||
| 1316 | } | |||
| 1317 | ||||
| 1318 | if (push_TvbRange(L,tvbr->tvb->ws_tvb,tvbr->offset+offset,len)) { | |||
| 1319 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1320 | } | |||
| 1321 | ||||
| 1322 | return 0; | |||
| 1323 | } | |||
| 1324 | ||||
| 1325 | WSLUA_METHODstatic int TvbRange_uncompress_zlib(lua_State* L) { | |||
| 1326 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing zlib compressed data, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. | |||
| 1327 | @since 4.3.0 | |||
| 1328 | */ | |||
| 1329 | #define WSLUA_ARG_TvbRange_uncompress_zlib_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1330 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1331 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) | |||
| 1332 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_uncompress_zlib_NAME,"Uncompressed")(luaL_optlstring(L, (2), ("Uncompressed"), ((void*)0))); | |||
| 1333 | tvbuff_t *uncompr_tvb; | |||
| 1334 | #endif | |||
| 1335 | ||||
| 1336 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1337 | ||||
| 1338 | if (tvbr->tvb->expired) { | |||
| 1339 | luaL_error(L,"expired tvb"); | |||
| 1340 | return 0; | |||
| 1341 | } | |||
| 1342 | ||||
| 1343 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) | |||
| 1344 | uncompr_tvb = tvb_child_uncompress_zlib(tvbr->tvb->ws_tvb, tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1345 | if (uncompr_tvb) { | |||
| 1346 | add_new_data_source (lua_pinfo, uncompr_tvb, name); | |||
| 1347 | if (push_TvbRange(L,uncompr_tvb,0,tvb_captured_length(uncompr_tvb))) { | |||
| 1348 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1349 | } | |||
| 1350 | } | |||
| 1351 | #else | |||
| 1352 | luaL_error(L,"Missing support for ZLIB"); | |||
| 1353 | #endif | |||
| 1354 | ||||
| 1355 | return 0; | |||
| 1356 | } | |||
| 1357 | ||||
| 1358 | WSLUA_METHODstatic int TvbRange_uncompress(lua_State* L) { | |||
| 1359 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing zlib compressed data, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. Deprecated; use tvbrange:uncompress_zlib() instead. */ | |||
| 1360 | #define WSLUA_ARG_TvbRange_uncompress_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1361 | return TvbRange_uncompress_zlib(L); | |||
| 1362 | } | |||
| 1363 | ||||
| 1364 | /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */ | |||
| 1365 | static int TvbRange__gc(lua_State* L) { | |||
| 1366 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1367 | ||||
| 1368 | free_TvbRange(tvbr); | |||
| 1369 | ||||
| 1370 | return 0; | |||
| 1371 | ||||
| 1372 | } | |||
| 1373 | ||||
| 1374 | WSLUA_METHODstatic int TvbRange_uncompress_brotli(lua_State* L) { | |||
| 1375 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing Brotli compressed data, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. | |||
| 1376 | @since 4.3.0 | |||
| 1377 | */ | |||
| 1378 | #define WSLUA_ARG_TvbRange_uncompress_brotli_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1379 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1380 | #ifdef HAVE_BROTLI1 | |||
| 1381 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_uncompress_brotli_NAME,"Uncompressed")(luaL_optlstring(L, (2), ("Uncompressed"), ((void*)0))); | |||
| 1382 | tvbuff_t *uncompr_tvb; | |||
| 1383 | #endif | |||
| 1384 | ||||
| 1385 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1386 | ||||
| 1387 | if (tvbr->tvb->expired) { | |||
| 1388 | luaL_error(L,"expired tvb"); | |||
| 1389 | return 0; | |||
| 1390 | } | |||
| 1391 | ||||
| 1392 | #ifdef HAVE_BROTLI1 | |||
| 1393 | uncompr_tvb = tvb_child_uncompress_brotli(tvbr->tvb->ws_tvb, tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1394 | if (uncompr_tvb) { | |||
| 1395 | add_new_data_source (lua_pinfo, uncompr_tvb, name); | |||
| 1396 | if (push_TvbRange(L,uncompr_tvb,0,tvb_captured_length(uncompr_tvb))) { | |||
| 1397 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1398 | } | |||
| 1399 | } | |||
| 1400 | #else | |||
| 1401 | luaL_error(L,"Missing support for Brotli"); | |||
| 1402 | #endif | |||
| 1403 | ||||
| 1404 | return 0; | |||
| 1405 | } | |||
| 1406 | ||||
| 1407 | WSLUA_METHODstatic int TvbRange_uncompress_hpack_huff(lua_State* L) { | |||
| 1408 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing data compressed using the Huffman encoding in HTTP/2 HPACK and HTTP/3 QPACK, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. | |||
| 1409 | @since 4.3.0 | |||
| 1410 | */ | |||
| 1411 | #define WSLUA_ARG_TvbRange_uncompress_hpack_huff_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1412 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1413 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_uncompress_hpack_huff_NAME,"Uncompressed")(luaL_optlstring(L, (2), ("Uncompressed"), ((void*)0))); | |||
| 1414 | tvbuff_t *uncompr_tvb; | |||
| 1415 | ||||
| 1416 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1417 | ||||
| 1418 | if (tvbr->tvb->expired) { | |||
| 1419 | luaL_error(L,"expired tvb"); | |||
| 1420 | return 0; | |||
| 1421 | } | |||
| 1422 | ||||
| 1423 | uncompr_tvb = tvb_child_uncompress_hpack_huff(tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1424 | if (uncompr_tvb) { | |||
| 1425 | add_new_data_source (lua_pinfo, uncompr_tvb, name); | |||
| 1426 | if (push_TvbRange(L,uncompr_tvb,0,tvb_captured_length(uncompr_tvb))) { | |||
| 1427 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1428 | } | |||
| 1429 | } | |||
| 1430 | ||||
| 1431 | return 0; | |||
| 1432 | } | |||
| 1433 | ||||
| 1434 | WSLUA_METHODstatic int TvbRange_uncompress_lz77(lua_State* L) { | |||
| 1435 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing Microsoft Plain LZ77 compressed data, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. | |||
| 1436 | @since 4.3.0 | |||
| 1437 | */ | |||
| 1438 | #define WSLUA_ARG_TvbRange_uncompress_lz77_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1439 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1440 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_uncompress_lz77_NAME,"Uncompressed")(luaL_optlstring(L, (2), ("Uncompressed"), ((void*)0))); | |||
| 1441 | tvbuff_t *uncompr_tvb; | |||
| 1442 | ||||
| 1443 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1444 | ||||
| 1445 | if (tvbr->tvb->expired) { | |||
| 1446 | luaL_error(L,"expired tvb"); | |||
| 1447 | return 0; | |||
| 1448 | } | |||
| 1449 | ||||
| 1450 | uncompr_tvb = tvb_child_uncompress_lz77(tvbr->tvb->ws_tvb, tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1451 | if (uncompr_tvb) { | |||
| 1452 | add_new_data_source (lua_pinfo, uncompr_tvb, name); | |||
| 1453 | if (push_TvbRange(L,uncompr_tvb,0,tvb_captured_length(uncompr_tvb))) { | |||
| 1454 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1455 | } | |||
| 1456 | } | |||
| 1457 | ||||
| 1458 | return 0; | |||
| 1459 | } | |||
| 1460 | ||||
| 1461 | WSLUA_METHODstatic int TvbRange_uncompress_lz77huff(lua_State* L) { | |||
| 1462 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing Microsoft LZ77+Huffman compressed data, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. | |||
| 1463 | @since 4.3.0 | |||
| 1464 | */ | |||
| 1465 | #define WSLUA_ARG_TvbRange_uncompress_lz77huff_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1466 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1467 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_uncompress_lz77huff_NAME,"Uncompressed")(luaL_optlstring(L, (2), ("Uncompressed"), ((void*)0))); | |||
| 1468 | tvbuff_t *uncompr_tvb; | |||
| 1469 | ||||
| 1470 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1471 | ||||
| 1472 | if (tvbr->tvb->expired) { | |||
| 1473 | luaL_error(L,"expired tvb"); | |||
| 1474 | return 0; | |||
| 1475 | } | |||
| 1476 | ||||
| 1477 | uncompr_tvb = tvb_child_uncompress_lz77huff(tvbr->tvb->ws_tvb, tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1478 | if (uncompr_tvb) { | |||
| 1479 | add_new_data_source (lua_pinfo, uncompr_tvb, name); | |||
| 1480 | if (push_TvbRange(L,uncompr_tvb,0,tvb_captured_length(uncompr_tvb))) { | |||
| 1481 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1482 | } | |||
| 1483 | } | |||
| 1484 | ||||
| 1485 | return 0; | |||
| 1486 | } | |||
| 1487 | ||||
| 1488 | WSLUA_METHODstatic int TvbRange_uncompress_lznt1(lua_State* L) { | |||
| 1489 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing Microsoft LZNT1 compressed data, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. | |||
| 1490 | @since 4.3.0 | |||
| 1491 | */ | |||
| 1492 | #define WSLUA_ARG_TvbRange_uncompress_lznt1_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1493 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1494 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_uncompress_lznt1_NAME,"Uncompressed")(luaL_optlstring(L, (2), ("Uncompressed"), ((void*)0))); | |||
| 1495 | tvbuff_t *uncompr_tvb; | |||
| 1496 | ||||
| 1497 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1498 | ||||
| 1499 | if (tvbr->tvb->expired) { | |||
| 1500 | luaL_error(L,"expired tvb"); | |||
| 1501 | return 0; | |||
| 1502 | } | |||
| 1503 | ||||
| 1504 | uncompr_tvb = tvb_child_uncompress_lznt1(tvbr->tvb->ws_tvb, tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1505 | if (uncompr_tvb) { | |||
| 1506 | add_new_data_source (lua_pinfo, uncompr_tvb, name); | |||
| 1507 | if (push_TvbRange(L,uncompr_tvb,0,tvb_captured_length(uncompr_tvb))) { | |||
| 1508 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1509 | } | |||
| 1510 | } | |||
| 1511 | ||||
| 1512 | return 0; | |||
| 1513 | } | |||
| 1514 | ||||
| 1515 | WSLUA_METHODstatic int TvbRange_uncompress_snappy(lua_State* L) { | |||
| 1516 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing Snappy compressed data, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. | |||
| 1517 | @since 4.3.0 | |||
| 1518 | */ | |||
| 1519 | #define WSLUA_ARG_TvbRange_uncompress_snappy_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1520 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1521 | #ifdef HAVE_SNAPPY1 | |||
| 1522 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_uncompress_snappy_NAME,"Uncompressed")(luaL_optlstring(L, (2), ("Uncompressed"), ((void*)0))); | |||
| 1523 | tvbuff_t *uncompr_tvb; | |||
| 1524 | #endif | |||
| 1525 | ||||
| 1526 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1527 | ||||
| 1528 | if (tvbr->tvb->expired) { | |||
| 1529 | luaL_error(L,"expired tvb"); | |||
| 1530 | return 0; | |||
| 1531 | } | |||
| 1532 | ||||
| 1533 | #ifdef HAVE_SNAPPY1 | |||
| 1534 | uncompr_tvb = tvb_child_uncompress_snappy(tvbr->tvb->ws_tvb, tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1535 | if (uncompr_tvb) { | |||
| 1536 | add_new_data_source (lua_pinfo, uncompr_tvb, name); | |||
| 1537 | if (push_TvbRange(L,uncompr_tvb,0,tvb_captured_length(uncompr_tvb))) { | |||
| 1538 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1539 | } | |||
| 1540 | } | |||
| 1541 | #else | |||
| 1542 | luaL_error(L,"Missing support for Snappy"); | |||
| 1543 | #endif | |||
| 1544 | ||||
| 1545 | return 0; | |||
| 1546 | } | |||
| 1547 | ||||
| 1548 | WSLUA_METHODstatic int TvbRange_uncompress_zstd(lua_State* L) { | |||
| 1549 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing Zstandard compressed data, decompresses the data and returns a new <<lua_class_TvbRange,`TvbRange`>> containing the uncompressed data. | |||
| 1550 | @since 4.3.0 | |||
| 1551 | */ | |||
| 1552 | #define WSLUA_ARG_TvbRange_uncompress_zstd_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1553 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1554 | #ifdef HAVE_ZSTD1 | |||
| 1555 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_uncompress_zstd_NAME,"Uncompressed")(luaL_optlstring(L, (2), ("Uncompressed"), ((void*)0))); | |||
| 1556 | tvbuff_t *uncompr_tvb; | |||
| 1557 | #endif | |||
| 1558 | ||||
| 1559 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1560 | ||||
| 1561 | if (tvbr->tvb->expired) { | |||
| 1562 | luaL_error(L,"expired tvb"); | |||
| 1563 | return 0; | |||
| 1564 | } | |||
| 1565 | ||||
| 1566 | #ifdef HAVE_ZSTD1 | |||
| 1567 | uncompr_tvb = tvb_child_uncompress_zstd(tvbr->tvb->ws_tvb, tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1568 | if (uncompr_tvb) { | |||
| 1569 | add_new_data_source (lua_pinfo, uncompr_tvb, name); | |||
| 1570 | if (push_TvbRange(L,uncompr_tvb,0,tvb_captured_length(uncompr_tvb))) { | |||
| 1571 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1572 | } | |||
| 1573 | } | |||
| 1574 | #else | |||
| 1575 | luaL_error(L,"Missing support for ZStandard"); | |||
| 1576 | #endif | |||
| 1577 | ||||
| 1578 | return 0; | |||
| 1579 | } | |||
| 1580 | ||||
| 1581 | WSLUA_METHODstatic int TvbRange_decode_base64(lua_State* L) { | |||
| 1582 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing Base64 encoded data, return a new <<lua_class_TvbRange,`TvbRange`>> containing the decoded data. | |||
| 1583 | @since 4.3.0 | |||
| 1584 | */ | |||
| 1585 | #define WSLUA_ARG_TvbRange_decode_base64_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1586 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1587 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_decode_base64_NAME,"Decoded")(luaL_optlstring(L, (2), ("Decoded"), ((void*)0))); | |||
| 1588 | tvbuff_t *decoded_tvb; | |||
| 1589 | ||||
| 1590 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1591 | ||||
| 1592 | if (tvbr->tvb->expired) { | |||
| 1593 | luaL_error(L,"expired tvb"); | |||
| 1594 | return 0; | |||
| 1595 | } | |||
| 1596 | ||||
| 1597 | decoded_tvb = base64_tvb_to_new_tvb(tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1598 | if (decoded_tvb) { | |||
| 1599 | add_new_data_source (lua_pinfo, decoded_tvb, name); | |||
| 1600 | if (push_TvbRange(L,decoded_tvb,0,tvb_captured_length(decoded_tvb))) { | |||
| 1601 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1602 | } | |||
| 1603 | } | |||
| 1604 | ||||
| 1605 | return 0; | |||
| 1606 | } | |||
| 1607 | ||||
| 1608 | WSLUA_METHODstatic int TvbRange_decode_base64url(lua_State* L) { | |||
| 1609 | /* Given a <<lua_class_TvbRange,`TvbRange`>> containing base64url encoded data, return a new <<lua_class_TvbRange,`TvbRange`>> containing the decoded data. | |||
| 1610 | @since 4.3.0 | |||
| 1611 | */ | |||
| 1612 | #define WSLUA_ARG_TvbRange_decode_base64url_NAME2 2 /* The name to be given to the new data-source. */ | |||
| 1613 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1614 | const char* name = luaL_optstring(L,WSLUA_ARG_TvbRange_decode_base64url_NAME,"Decoded")(luaL_optlstring(L, (2), ("Decoded"), ((void*)0))); | |||
| 1615 | tvbuff_t *decoded_tvb; | |||
| 1616 | ||||
| 1617 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1618 | ||||
| 1619 | if (tvbr->tvb->expired) { | |||
| 1620 | luaL_error(L,"expired tvb"); | |||
| 1621 | return 0; | |||
| 1622 | } | |||
| 1623 | ||||
| 1624 | decoded_tvb = base64uri_tvb_to_new_tvb(tvbr->tvb->ws_tvb, tvbr->offset, tvbr->len); | |||
| 1625 | if (decoded_tvb) { | |||
| 1626 | add_new_data_source (lua_pinfo, decoded_tvb, name); | |||
| 1627 | if (push_TvbRange(L,decoded_tvb,0,tvb_captured_length(decoded_tvb))) { | |||
| 1628 | WSLUA_RETURN(1)return (1); /* The <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1629 | } | |||
| 1630 | } | |||
| 1631 | ||||
| 1632 | return 0; | |||
| 1633 | } | |||
| 1634 | ||||
| 1635 | WSLUA_METHODstatic int TvbRange_len(lua_State* L) { | |||
| 1636 | /* Obtain the length of a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1637 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1638 | ||||
| 1639 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1640 | if (tvbr->tvb->expired) { | |||
| 1641 | luaL_error(L,"expired tvb"); | |||
| 1642 | return 0; | |||
| 1643 | } | |||
| 1644 | lua_pushinteger(L,(lua_Integer)tvbr->len); | |||
| 1645 | return 1; | |||
| 1646 | } | |||
| 1647 | ||||
| 1648 | WSLUA_METHODstatic int TvbRange_offset(lua_State* L) { | |||
| 1649 | /* Obtain the offset in a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1650 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1651 | ||||
| 1652 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1653 | if (tvbr->tvb->expired) { | |||
| 1654 | luaL_error(L,"expired tvb"); | |||
| 1655 | return 0; | |||
| 1656 | } | |||
| 1657 | lua_pushinteger(L,(lua_Integer)tvbr->offset); | |||
| 1658 | return 1; | |||
| 1659 | } | |||
| 1660 | ||||
| 1661 | WSLUA_METHODstatic int TvbRange_raw(lua_State* L) { | |||
| 1662 | /* Obtain a Lua string of the binary bytes in a <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1663 | #define WSLUA_OPTARG_TvbRange_raw_OFFSET2 2 /* The position of the first byte within the range. Default is 0, or first byte. */ | |||
| 1664 | #define WSLUA_OPTARG_TvbRange_raw_LENGTH3 3 /* The length of the segment to get. Default is -1, or the remaining bytes in the range. */ | |||
| 1665 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1666 | int offset = (int)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_raw_OFFSET2,0); | |||
| 1667 | int len = (int)luaL_optinteger(L,WSLUA_OPTARG_TvbRange_raw_LENGTH3,-1); | |||
| 1668 | ||||
| 1669 | if (!tvbr || !tvbr->tvb) return 0; | |||
| 1670 | if (tvbr->tvb->expired) { | |||
| 1671 | luaL_error(L,"expired tvb"); | |||
| 1672 | return 0; | |||
| 1673 | } | |||
| 1674 | ||||
| 1675 | if (offset < 0) { | |||
| 1676 | WSLUA_OPTARG_ERROR(TvbRange_raw,OFFSET,"offset before start of TvbRange"){ luaL_argerror(L,2, "TvbRange_raw" ": " "offset before start of TvbRange" ); }; | |||
| 1677 | return 0; | |||
| 1678 | } | |||
| 1679 | if ((unsigned)offset > tvbr->len) { | |||
| 1680 | WSLUA_OPTARG_ERROR(TvbRange_raw,OFFSET,"offset beyond end of TvbRange"){ luaL_argerror(L,2, "TvbRange_raw" ": " "offset beyond end of TvbRange" ); }; | |||
| 1681 | return 0; | |||
| 1682 | } | |||
| 1683 | ||||
| 1684 | if (len == -1) { | |||
| 1685 | len = tvbr->len - offset; | |||
| 1686 | } | |||
| 1687 | if (len < 0) { | |||
| 1688 | luaL_error(L,"out of bounds"); | |||
| 1689 | return false0; | |||
| 1690 | } else if ( (unsigned)(len + offset) > tvbr->len) { | |||
| 1691 | luaL_error(L,"Range is out of bounds"); | |||
| 1692 | return false0; | |||
| 1693 | } | |||
| 1694 | ||||
| 1695 | lua_pushlstring(L, (const char*)tvb_get_ptr(tvbr->tvb->ws_tvb, tvbr->offset+offset, len), len); | |||
| 1696 | ||||
| 1697 | WSLUA_RETURN(1)return (1); /* A Lua string of the binary bytes in the <<lua_class_TvbRange,`TvbRange`>>. */ | |||
| 1698 | } | |||
| 1699 | ||||
| 1700 | /* WSLUA_ATTRIBUTE TvbRange_length RO The length (in bytes) of the range. | |||
| 1701 | Mirrors `range:len()`. */ | |||
| 1702 | WSLUA_ATTRIBUTE_GET(TvbRange,length, {static int TvbRange_get_length (lua_State* L) { TvbRange obj = checkTvbRange (L,1); { lua_pushinteger(L, (lua_Integer)obj-> len);} return 1; } typedef void __dummyTvbRange_get_length | |||
| 1703 | lua_pushinteger(L, (lua_Integer)obj->len);static int TvbRange_get_length (lua_State* L) { TvbRange obj = checkTvbRange (L,1); { lua_pushinteger(L, (lua_Integer)obj-> len);} return 1; } typedef void __dummyTvbRange_get_length | |||
| 1704 | })static int TvbRange_get_length (lua_State* L) { TvbRange obj = checkTvbRange (L,1); { lua_pushinteger(L, (lua_Integer)obj-> len);} return 1; } typedef void __dummyTvbRange_get_length; | |||
| 1705 | ||||
| 1706 | /* WSLUA_ATTRIBUTE TvbRange_position RO The offset within the parent Tvb at | |||
| 1707 | which the range starts. Mirrors `range:offset()`. */ | |||
| 1708 | WSLUA_ATTRIBUTE_GET(TvbRange,position, {static int TvbRange_get_position (lua_State* L) { TvbRange obj = checkTvbRange (L,1); { lua_pushinteger(L, (lua_Integer)obj ->offset);} return 1; } typedef void __dummyTvbRange_get_position | |||
| 1709 | lua_pushinteger(L, (lua_Integer)obj->offset);static int TvbRange_get_position (lua_State* L) { TvbRange obj = checkTvbRange (L,1); { lua_pushinteger(L, (lua_Integer)obj ->offset);} return 1; } typedef void __dummyTvbRange_get_position | |||
| 1710 | })static int TvbRange_get_position (lua_State* L) { TvbRange obj = checkTvbRange (L,1); { lua_pushinteger(L, (lua_Integer)obj ->offset);} return 1; } typedef void __dummyTvbRange_get_position; | |||
| 1711 | ||||
| 1712 | WSLUA_METAMETHODstatic int TvbRange__eq(lua_State* L) { | |||
| 1713 | /* Checks whether the contents of two <<lua_class_TvbRange,`TvbRange`>>s are equal. */ | |||
| 1714 | TvbRange tvb_l = checkTvbRange(L,1); | |||
| 1715 | TvbRange tvb_r = checkTvbRange(L,2); | |||
| 1716 | ||||
| 1717 | /* it is not an error if their ds_tvb are different... they're just not equal */ | |||
| 1718 | if (tvb_l->len == tvb_r->len && | |||
| 1719 | tvb_l->len <= tvb_captured_length_remaining(tvb_l->tvb->ws_tvb, tvb_l->offset) && | |||
| 1720 | tvb_r->len <= tvb_captured_length_remaining(tvb_r->tvb->ws_tvb, tvb_r->offset)) | |||
| 1721 | { | |||
| 1722 | const char* lp = (const char*)tvb_get_ptr(tvb_l->tvb->ws_tvb, tvb_l->offset, tvb_l->len); | |||
| 1723 | const char* rp = (const char*)tvb_get_ptr(tvb_r->tvb->ws_tvb, tvb_r->offset, tvb_r->len); | |||
| 1724 | unsigned i = 0; | |||
| 1725 | ||||
| 1726 | for (; i < tvb_r->len; ++i) { | |||
| 1727 | if (lp[i] != rp[i]) { | |||
| 1728 | lua_pushboolean(L,0); | |||
| 1729 | return 1; | |||
| 1730 | } | |||
| 1731 | } | |||
| 1732 | lua_pushboolean(L,1); | |||
| 1733 | } else { | |||
| 1734 | lua_pushboolean(L,0); | |||
| 1735 | } | |||
| 1736 | ||||
| 1737 | return 1; | |||
| 1738 | } | |||
| 1739 | ||||
| 1740 | WSLUA_METAMETHODstatic int TvbRange__tostring(lua_State* L) { | |||
| 1741 | /* | |||
| 1742 | Converts the <<lua_class_TvbRange,`TvbRange`>> into a string of | |||
| 1743 | the form `TvbRange: offset=<O> length=<L> bytes=<hex>`. The hex | |||
| 1744 | preview is truncated by `tvb_bytes_to_str` to a small number of | |||
| 1745 | bytes so the rendering stays useful in the debugger Variables | |||
| 1746 | view and in `print()` for large ranges. Empty ranges render as | |||
| 1747 | `TvbRange: offset=<O> length=0 (empty)`. | |||
| 1748 | */ | |||
| 1749 | TvbRange tvbr = checkTvbRange(L,1); | |||
| 1750 | ||||
| 1751 | if (!(tvbr && tvbr->tvb)) return 0; | |||
| 1752 | if (tvbr->tvb->expired) { | |||
| 1753 | luaL_error(L,"expired tvb"); | |||
| 1754 | return 0; | |||
| 1755 | } | |||
| 1756 | ||||
| 1757 | if (tvbr->len == 0) { | |||
| 1758 | lua_pushfstring(L, "TvbRange: offset=%d length=0 (empty)", | |||
| 1759 | tvbr->offset); | |||
| 1760 | } else { | |||
| 1761 | char *str = tvb_bytes_to_str(NULL((void*)0), tvbr->tvb->ws_tvb, | |||
| 1762 | tvbr->offset, tvbr->len); | |||
| 1763 | lua_pushfstring(L, "TvbRange: offset=%d length=%d bytes=%s", | |||
| 1764 | tvbr->offset, tvbr->len, str ? str : ""); | |||
| 1765 | wmem_free(NULL((void*)0), str); | |||
| 1766 | } | |||
| 1767 | ||||
| 1768 | WSLUA_RETURN(1)return (1); /* A short label including the offset, length, | |||
| 1769 | and a truncated hex preview. */ | |||
| 1770 | } | |||
| 1771 | ||||
| 1772 | WSLUA_METHODSstatic const luaL_Reg TvbRange_methods[] = { | |||
| 1773 | WSLUA_CLASS_FNREG(TvbRange,uint){ "uint", TvbRange_uint }, | |||
| 1774 | WSLUA_CLASS_FNREG(TvbRange,le_uint){ "le_uint", TvbRange_le_uint }, | |||
| 1775 | WSLUA_CLASS_FNREG(TvbRange,int){ "int", TvbRange_int }, | |||
| 1776 | WSLUA_CLASS_FNREG(TvbRange,le_int){ "le_int", TvbRange_le_int }, | |||
| 1777 | WSLUA_CLASS_FNREG(TvbRange,uint64){ "uint64", TvbRange_uint64 }, | |||
| 1778 | WSLUA_CLASS_FNREG(TvbRange,le_uint64){ "le_uint64", TvbRange_le_uint64 }, | |||
| 1779 | WSLUA_CLASS_FNREG(TvbRange,int64){ "int64", TvbRange_int64 }, | |||
| 1780 | WSLUA_CLASS_FNREG(TvbRange,le_int64){ "le_int64", TvbRange_le_int64 }, | |||
| 1781 | WSLUA_CLASS_FNREG(TvbRange,float){ "float", TvbRange_float }, | |||
| 1782 | WSLUA_CLASS_FNREG(TvbRange,le_float){ "le_float", TvbRange_le_float }, | |||
| 1783 | WSLUA_CLASS_FNREG(TvbRange,ether){ "ether", TvbRange_ether }, | |||
| 1784 | WSLUA_CLASS_FNREG(TvbRange,ipv4){ "ipv4", TvbRange_ipv4 }, | |||
| 1785 | WSLUA_CLASS_FNREG(TvbRange,le_ipv4){ "le_ipv4", TvbRange_le_ipv4 }, | |||
| 1786 | WSLUA_CLASS_FNREG(TvbRange,ipv6){ "ipv6", TvbRange_ipv6 }, | |||
| 1787 | WSLUA_CLASS_FNREG(TvbRange,nstime){ "nstime", TvbRange_nstime }, | |||
| 1788 | WSLUA_CLASS_FNREG(TvbRange,le_nstime){ "le_nstime", TvbRange_le_nstime }, | |||
| 1789 | WSLUA_CLASS_FNREG(TvbRange,string){ "string", TvbRange_string }, | |||
| 1790 | WSLUA_CLASS_FNREG(TvbRange,stringz){ "stringz", TvbRange_stringz }, | |||
| 1791 | WSLUA_CLASS_FNREG(TvbRange,strsize){ "strsize", TvbRange_strsize }, | |||
| 1792 | WSLUA_CLASS_FNREG(TvbRange,bytes){ "bytes", TvbRange_bytes }, | |||
| 1793 | WSLUA_CLASS_FNREG(TvbRange,bitfield){ "bitfield", TvbRange_bitfield }, | |||
| 1794 | WSLUA_CLASS_FNREG(TvbRange,range){ "range", TvbRange_range }, | |||
| 1795 | WSLUA_CLASS_FNREG(TvbRange,len){ "len", TvbRange_len }, | |||
| 1796 | WSLUA_CLASS_FNREG(TvbRange,offset){ "offset", TvbRange_offset }, | |||
| 1797 | WSLUA_CLASS_FNREG(TvbRange,tvb){ "tvb", TvbRange_tvb }, | |||
| 1798 | WSLUA_CLASS_FNREG(TvbRange,le_ustring){ "le_ustring", TvbRange_le_ustring }, | |||
| 1799 | WSLUA_CLASS_FNREG(TvbRange,ustring){ "ustring", TvbRange_ustring }, | |||
| 1800 | WSLUA_CLASS_FNREG(TvbRange,le_ustringz){ "le_ustringz", TvbRange_le_ustringz }, | |||
| 1801 | WSLUA_CLASS_FNREG(TvbRange,ustringz){ "ustringz", TvbRange_ustringz }, | |||
| 1802 | WSLUA_CLASS_FNREG(TvbRange,uncompress){ "uncompress", TvbRange_uncompress }, | |||
| 1803 | WSLUA_CLASS_FNREG(TvbRange,uncompress_zlib){ "uncompress_zlib", TvbRange_uncompress_zlib }, | |||
| 1804 | WSLUA_CLASS_FNREG(TvbRange,uncompress_brotli){ "uncompress_brotli", TvbRange_uncompress_brotli }, | |||
| 1805 | WSLUA_CLASS_FNREG(TvbRange,uncompress_hpack_huff){ "uncompress_hpack_huff", TvbRange_uncompress_hpack_huff }, | |||
| 1806 | WSLUA_CLASS_FNREG(TvbRange,uncompress_lz77){ "uncompress_lz77", TvbRange_uncompress_lz77 }, | |||
| 1807 | WSLUA_CLASS_FNREG(TvbRange,uncompress_lz77huff){ "uncompress_lz77huff", TvbRange_uncompress_lz77huff }, | |||
| 1808 | WSLUA_CLASS_FNREG(TvbRange,uncompress_lznt1){ "uncompress_lznt1", TvbRange_uncompress_lznt1 }, | |||
| 1809 | WSLUA_CLASS_FNREG(TvbRange,uncompress_snappy){ "uncompress_snappy", TvbRange_uncompress_snappy }, | |||
| 1810 | WSLUA_CLASS_FNREG(TvbRange,uncompress_zstd){ "uncompress_zstd", TvbRange_uncompress_zstd }, | |||
| 1811 | WSLUA_CLASS_FNREG(TvbRange,decode_base64){ "decode_base64", TvbRange_decode_base64 }, | |||
| 1812 | WSLUA_CLASS_FNREG(TvbRange,decode_base64url){ "decode_base64url", TvbRange_decode_base64url }, | |||
| 1813 | WSLUA_CLASS_FNREG(TvbRange,raw){ "raw", TvbRange_raw }, | |||
| 1814 | { NULL((void*)0), NULL((void*)0) } | |||
| 1815 | }; | |||
| 1816 | ||||
| 1817 | WSLUA_METAstatic const luaL_Reg TvbRange_meta[] = { | |||
| 1818 | WSLUA_CLASS_MTREG(TvbRange,tostring){ "__" "tostring", TvbRange__tostring }, | |||
| 1819 | WSLUA_CLASS_MTREG(wslua,concat){ "__" "concat", wslua__concat }, | |||
| 1820 | WSLUA_CLASS_MTREG(TvbRange,eq){ "__" "eq", TvbRange__eq }, | |||
| 1821 | {"__call", TvbRange_range}, | |||
| 1822 | { NULL((void*)0), NULL((void*)0) } | |||
| 1823 | }; | |||
| 1824 | ||||
| 1825 | /* Read-only attributes for debugger introspection; see the comment on | |||
| 1826 | * Tvb_attributes for the naming rationale. */ | |||
| 1827 | WSLUA_ATTRIBUTESstatic const wslua_attribute_table TvbRange_attributes[] = { | |||
| 1828 | WSLUA_ATTRIBUTE_ROREG(TvbRange,length){ "length", TvbRange_get_length, ((void*)0) }, | |||
| 1829 | WSLUA_ATTRIBUTE_ROREG(TvbRange,position){ "position", TvbRange_get_position, ((void*)0) }, | |||
| 1830 | { NULL((void*)0), NULL((void*)0), NULL((void*)0) } | |||
| 1831 | }; | |||
| 1832 | ||||
| 1833 | int TvbRange_register(lua_State* L) { | |||
| 1834 | if (outstanding_TvbRange != NULL((void*)0)) { | |||
| 1835 | g_ptr_array_unref(outstanding_TvbRange); | |||
| 1836 | } | |||
| 1837 | outstanding_TvbRange = g_ptr_array_new(); | |||
| 1838 | WSLUA_REGISTER_CLASS_WITH_ATTRS(TvbRange){ const wslua_class TvbRange_class = { .name = "TvbRange", .class_methods = TvbRange_methods, .class_meta = TvbRange_meta, .instance_methods = TvbRange_methods, .instance_meta = TvbRange_meta, .attrs = TvbRange_attributes }; wslua_register_class(L, &TvbRange_class ); (lua_getfield(L, (-1000000 - 1000), ("TvbRange"))); lua_pushcclosure (L, (TvbRange__gc), 0); lua_setfield(L, -2, "__gc"); lua_settop (L, -(1)-1); }; | |||
| 1839 | return 0; | |||
| 1840 | } | |||
| 1841 | ||||
| 1842 | /* | |||
| 1843 | * Editor modelines - https://www.wireshark.org/tools/modelines.html | |||
| 1844 | * | |||
| 1845 | * Local variables: | |||
| 1846 | * c-basic-offset: 4 | |||
| 1847 | * tab-width: 8 | |||
| 1848 | * indent-tabs-mode: nil | |||
| 1849 | * End: | |||
| 1850 | * | |||
| 1851 | * vi: set shiftwidth=4 tabstop=8 expandtab: | |||
| 1852 | * :indentSize=4:tabSize=8:noTabs=true: | |||
| 1853 | */ |