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