Bug Summary

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