Bug Summary

File:builds/wireshark/wireshark/epan/wslua/wslua_tvb.c
Warning:line 1096, column 5
2nd function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

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