Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / tvbuff.h
blob9930ddc4969b5c052447a25d132ee372be18f2e3
1 /** @file
3 * Testy, Virtual(-izable) Buffer of uint8_t*'s
5 * "Testy" -- the buffer gets mad when an attempt is made to access data
6 * beyond the bounds of the buffer. An exception is thrown.
8 * "Virtual" -- the buffer can have its own data, can use a subset of
9 * the data of a backing tvbuff, or can be a composite of
10 * other tvbuffs.
12 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
14 * Wireshark - Network traffic analyzer
15 * By Gerald Combs <gerald@wireshark.org>
16 * Copyright 1998 Gerald Combs
18 * SPDX-License-Identifier: GPL-2.0-or-later
21 #ifndef __TVBUFF_H__
22 #define __TVBUFF_H__
24 #include <ws_symbol_export.h>
25 #include <ws_attributes.h>
27 #include <epan/guid-utils.h>
29 #include <wsutil/inet_addr.h>
30 #include <wsutil/nstime.h>
31 #include "wsutil/ws_mempbrk.h"
33 #ifdef __cplusplus
34 extern "C" {
35 #endif /* __cplusplus */
37 /**
38 * "testy, virtual(-izable) buffer". They are testy in that they get mad when
39 * an attempt is made to access data beyond the bounds of their array. In that
40 * case, they throw an exception.
42 * They are virtualizable in that new tvbuff's can be made from other tvbuffs,
43 * while only the original tvbuff may have data. That is, the new tvbuff has
44 * virtual data.
47 struct tvbuff;
48 typedef struct tvbuff tvbuff_t;
50 /** @defgroup tvbuff Testy, Virtual(-izable) Buffers
52 * Dissector use and management
54 * Consider a collection of tvbs as being a chain or stack of tvbs.
56 * When dissecting a frame:
57 * The top-level dissector (packet.c) pushes the initial tvb (containing
58 * the complete frame) onto the stack (starts the chain) and then calls
59 * a sub-dissector which in turn calls the next sub-dissector and so on.
60 * Each sub-dissector may chain additional tvbs (see below) to the tvb
61 * handed to that dissector. After dissection is complete and control has
62 * returned to the top-level dissector, the chain of tvbs (stack) is free'd
63 * via a call to tvb_free_chain() (in epan_dissect_cleanup()).
65 * A dissector:
66 * - Can chain new tvbs (subset, real, composite) to the
67 * tvb handed to the dissector using tvb_new_subset_length_caplen(),
68 * tvb_new_subset_length(), tvb_new_subset_remaining(),
69 * tvb_new_child_real_data(), tvb_set_child_real_data_tvbuff(),
70 * tvb_composite_finalize(), and tvb_child_uncompress(). (Composite
71 * tvbs should reference only tvbs which are already part of the chain).
72 * - Must not save for later use (e.g., when dissecting another frame) a
73 * pointer to a tvb handed to the dissector; (A higher level function
74 * may very well free the chain thus leaving a dangling pointer).
75 * This (obviously) also applies to any tvbs chained to the tvb handed
76 * to the dissector.
77 * - Can create its own tvb chain (using tvb_new_real_data() which the
78 * dissector is free to manage as desired.
79 * @{
82 /** A "real" tvbuff contains a uint8_t* that points to real data.
83 * The data is allocated and contiguous.
85 * A "subset" tvbuff has a backing tvbuff. It is a "window" through
86 * which the program sees only a portion of the backing tvbuff.
88 * A "composite" tvbuff combines multiple tvbuffs sequentially to
89 * produce a larger byte array.
91 * tvbuff's of any type can be used as the backing-tvbuff of a
92 * "subset" tvbuff or as a member of a "composite" tvbuff.
93 * "composite" tvbuffs can have member-tvbuffs of different types.
95 * Once a tvbuff is create/initialized/finalized, the tvbuff is read-only.
96 * That is, it cannot point to any other data. A new tvbuff must be created if
97 * you want a tvbuff that points to other data.
99 * tvbuff's are normally chained together to allow efficient de-allocation of
100 * tvbuff's.
103 typedef void (*tvbuff_free_cb_t)(void*);
105 /** Extracts 'number of bits' starting at 'bit offset'.
106 * Returns a pointer to a newly initialized g_malloc'd REAL_DATA
107 * tvbuff with the bits octet aligned.
108 * Bits are counted from MSB (0) to LSB (7) within octets.
110 WS_DLL_PUBLIC tvbuff_t *tvb_new_octet_aligned(tvbuff_t *tvb,
111 uint32_t bit_offset, int32_t no_of_bits);
113 /** Extracts 'number of bits' starting at 'bit offset'.
114 * Bits are counted from LSB (0) to MSB (7) within octets.
116 WS_DLL_PUBLIC tvbuff_t *tvb_new_octet_right_aligned(tvbuff_t *tvb,
117 uint32_t bit_offset, int32_t no_of_bits);
119 WS_DLL_PUBLIC tvbuff_t *tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing);
121 WS_DLL_PUBLIC tvbuff_t *tvb_clone(tvbuff_t *tvb);
123 WS_DLL_PUBLIC tvbuff_t *tvb_clone_offset_len(tvbuff_t *tvb, unsigned offset,
124 unsigned len);
126 /** Free a tvbuff_t and all tvbuffs chained from it
127 * The tvbuff must be 'the 'head' (initial) tvb of a chain or
128 * must not be in a chain.
129 * If specified, a callback to free the tvbuff data will be invoked
130 * for each tvbuff free'd */
131 WS_DLL_PUBLIC void tvb_free(tvbuff_t *tvb);
133 /** Free the tvbuff_t and all tvbuffs chained from it.
134 * The tvbuff must be 'the 'head' (initial) tvb of a chain or
135 * must not be in a chain.
136 * If specified, a callback to free the tvbuff data will be invoked
137 * for each tvbuff free'd */
138 WS_DLL_PUBLIC void tvb_free_chain(tvbuff_t *tvb);
140 /** Set a callback function to call when a tvbuff is actually freed
141 * One argument is passed to that callback --- a void* that points
142 * to the real data. Obviously, this only applies to a
143 * "real" tvbuff. */
144 WS_DLL_PUBLIC void tvb_set_free_cb(tvbuff_t *tvb, const tvbuff_free_cb_t func);
146 /** Attach a "real" tvbuff to a parent tvbuff. This connection is used
147 * during a tvb_free_chain()... the "child" "real" tvbuff acts as if it
148 * is part of the chain-of-creation of the parent tvbuff, although it
149 * isn't. This is useful if you need to take the data from some tvbuff,
150 * run some operation on it, like decryption or decompression, and make
151 * a new tvbuff from it, yet want the new tvbuff to be part of the chain.
152 * The reality is that the new tvbuff *is* part of the "chain of creation",
153 * but in a way that these tvbuff routines are ignorant of. Use this
154 * function to make the tvbuff routines knowledgable of this fact. */
155 WS_DLL_PUBLIC void tvb_set_child_real_data_tvbuff(tvbuff_t *parent,
156 tvbuff_t *child);
158 WS_DLL_PUBLIC tvbuff_t *tvb_new_child_real_data(tvbuff_t *parent,
159 const uint8_t *data, const unsigned length, const int reported_length);
161 /** Create a tvbuff backed by existing data. Can throw ReportedBoundsError.
162 * Normally, a callback to free the data should be registered using
163 * tvb_set_free_cb(); when this tvbuff is freed, then your callback will be
164 * called, and at that time you can free your original data. */
165 WS_DLL_PUBLIC tvbuff_t *tvb_new_real_data(const uint8_t *data,
166 const unsigned length, const int reported_length);
168 /** Create a tvbuff that's a subset of another tvbuff, with the captured
169 * length explicitly given. You probably want tvb_new_subset_length() or
170 * tvb_new_subset_remaining() instead.
172 * @param backing The backing tvbuff onto which the new tvbuff is a view
173 * @param backing_offset If positive, is the offset from the beginning of
174 * the backing tvbuff at which the new tvbuff's data begins, and, if
175 * negative, is the offset from the end of the backing tvbuff at which
176 * the new tvbuff's data begins.
177 * @param backing_length The length of the data to include in the new
178 * tvbuff, starting with the byte at 'backing_offset'; if -1, it
179 * means "to the end of the backing tvbuff". It can be 0, although
180 * the usefulness of the buffer would be rather limited. The length
181 * actually included will be no more than the reported length.
182 * @param reported_length The reported length of the new tvbuff; if -1, it
183 * means "the reported length to the end of the backing tvbuff". It can
184 * be 0, although the usefulness of the buffer would be rather limited.
186 * @return A tvbuff that is a subset of the backing tvbuff beginning at
187 * backing_offset (which is offset 0 in the subset) and with the given
188 * reported_length, with captured length no more than backing_length.
190 * @note In most cases use tvb_new_subset_length() (or equivalently, pass -1
191 * as 'backing_length') or tvb_new_subset_remaining() instead. Use this when
192 * the backing tvbuff includes bytes at the end that must not be included in
193 * the subset regardless of the reported length, such as an FCS or padding.
194 * In such cases it may still be simpler to call tvb_new_subset_length()
195 * twice, once to remove the trailing bytes and once to select the chosen
196 * payload bytes.
198 * @warning Will throw BoundsError if 'backing_offset'/'length'
199 * is beyond the bounds of the backing tvbuff.
200 * Can throw ReportedBoundsError. */
201 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset_length_caplen(tvbuff_t *backing,
202 const int backing_offset, const int backing_length,
203 const int reported_length);
206 * Similar to tvb_new_subset_length_caplen() but with captured length calculated
207 * to fit within the existing captured length and the specified
208 * reported length.
209 * Can throw ReportedBoundsError. */
210 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset_length(tvbuff_t *backing,
211 const int backing_offset, const int reported_length);
213 /** Similar to tvb_new_subset_length_caplen() but with backing_length and reported_length set
214 * to -1. Can throw ReportedBoundsError. */
215 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset_remaining(tvbuff_t *backing,
216 const int backing_offset);
219 * Both tvb_composite_append and tvb_composite_prepend can throw
220 * BoundsError if member_offset/member_length goes beyond bounds of
221 * the 'member' tvbuff. */
223 /** Append to the list of tvbuffs that make up this composite tvbuff */
224 WS_DLL_PUBLIC void tvb_composite_append(tvbuff_t *tvb, tvbuff_t *member);
226 /** Prepend to the list of tvbuffs that make up this composite tvbuff */
227 extern void tvb_composite_prepend(tvbuff_t *tvb, tvbuff_t *member);
229 /** Create an empty composite tvbuff. */
230 WS_DLL_PUBLIC tvbuff_t *tvb_new_composite(void);
232 /** Mark a composite tvbuff as initialized. No further appends or prepends
233 * occur, data access can finally happen after this finalization. */
234 WS_DLL_PUBLIC void tvb_composite_finalize(tvbuff_t *tvb);
237 /* Get amount of captured data in the buffer (which is *NOT* necessarily the
238 * length of the packet). You probably want tvb_reported_length instead. */
239 WS_DLL_PUBLIC unsigned tvb_captured_length(const tvbuff_t *tvb);
241 /** Computes bytes to end of buffer, from offset (which can be negative,
242 * to indicate bytes from end of buffer). Function returns 0 if offset is
243 * either at the end of the buffer or out of bounds. No exception is thrown.
244 * You probably want tvb_reported_length_remaining instead. */
245 WS_DLL_PUBLIC int tvb_captured_length_remaining(const tvbuff_t *tvb, const int offset);
247 /** Same as above, but throws an exception if the offset is out of bounds. */
248 WS_DLL_PUBLIC unsigned tvb_ensure_captured_length_remaining(const tvbuff_t *tvb,
249 const int offset);
251 /* Checks (w/o throwing exception) that the bytes referred to by
252 * 'offset'/'length' actually exist in the buffer */
253 WS_DLL_PUBLIC bool tvb_bytes_exist(const tvbuff_t *tvb, const int offset,
254 const int length);
256 /** Checks that the bytes referred to by 'offset'/'length', where 'length'
257 * is a 64-bit unsigned integer, actually exist in the buffer, and throws
258 * an exception if they aren't. */
259 WS_DLL_PUBLIC void tvb_ensure_bytes_exist64(const tvbuff_t *tvb,
260 const int offset, const uint64_t length);
262 /** Checks that the bytes referred to by 'offset'/'length' actually exist
263 * in the buffer, and throws an exception if they aren't. */
264 WS_DLL_PUBLIC void tvb_ensure_bytes_exist(const tvbuff_t *tvb,
265 const int offset, const int length);
267 /* Checks (w/o throwing exception) that offset exists in buffer */
268 WS_DLL_PUBLIC bool tvb_offset_exists(const tvbuff_t *tvb,
269 const int offset);
271 /* Get reported length of buffer */
272 WS_DLL_PUBLIC unsigned tvb_reported_length(const tvbuff_t *tvb);
274 /** Computes bytes of reported packet data to end of buffer, from offset
275 * (which can be negative, to indicate bytes from end of buffer). Function
276 * returns 0 if offset is either at the end of the buffer or out of bounds.
277 * No exception is thrown. */
278 WS_DLL_PUBLIC int tvb_reported_length_remaining(const tvbuff_t *tvb,
279 const int offset);
281 /** Same as above, but throws an exception if the offset is out of bounds. */
282 WS_DLL_PUBLIC unsigned tvb_ensure_reported_length_remaining(const tvbuff_t *tvb,
283 const int offset);
285 /** Set the reported length of a tvbuff to a given value; used for protocols
286 whose headers contain an explicit length and where the calling
287 dissector's payload may include padding as well as the packet for
288 this protocol.
290 Also adjusts the available and contained length. */
291 WS_DLL_PUBLIC void tvb_set_reported_length(tvbuff_t *tvb, const unsigned);
293 /* Repair a tvbuff where the captured length is greater than the
294 * reported length; such a tvbuff makes no sense, as it's impossible
295 * to capture more data than is in the packet.
297 WS_DLL_PUBLIC void tvb_fix_reported_length(tvbuff_t *tvb);
299 WS_DLL_PUBLIC unsigned tvb_offset_from_real_beginning(const tvbuff_t *tvb);
301 /* Returns the offset from the first byte of real data. */
302 WS_DLL_PUBLIC int tvb_raw_offset(tvbuff_t *tvb);
304 /** Set the "this is a fragment" flag. This affects whether
305 * FragmentBoundsError is thrown instead of ContainedBoundsError
306 * or ReportedBoundsError. */
307 WS_DLL_PUBLIC void tvb_set_fragment(tvbuff_t *tvb);
309 WS_DLL_PUBLIC struct tvbuff *tvb_get_ds_tvb(tvbuff_t *tvb);
312 /************** START OF ACCESSORS ****************/
313 /* All accessors will throw an exception if appropriate */
315 WS_DLL_PUBLIC uint8_t tvb_get_uint8(tvbuff_t *tvb, const int offset);
316 WS_DEPRECATED_X("Use tvb_get_uint8 instead")
317 static inline uint8_t tvb_get_guint8(tvbuff_t *tvb, const int offset) { return tvb_get_uint8(tvb, offset); }
318 WS_DLL_PUBLIC int8_t tvb_get_int8(tvbuff_t *tvb, const int offset);
319 WS_DEPRECATED_X("Use tvb_get_int8 instead")
320 static inline int8_t tvb_get_gint8(tvbuff_t *tvb, const int offset) { return tvb_get_int8(tvb, offset); }
322 WS_DLL_PUBLIC uint16_t tvb_get_ntohs(tvbuff_t *tvb, const int offset);
323 WS_DLL_PUBLIC int16_t tvb_get_ntohis(tvbuff_t *tvb, const int offset);
324 WS_DLL_PUBLIC uint32_t tvb_get_ntoh24(tvbuff_t *tvb, const int offset);
325 WS_DLL_PUBLIC int32_t tvb_get_ntohi24(tvbuff_t *tvb, const int offset);
326 WS_DLL_PUBLIC uint32_t tvb_get_ntohl(tvbuff_t *tvb, const int offset);
327 WS_DLL_PUBLIC int32_t tvb_get_ntohil(tvbuff_t *tvb, const int offset);
328 WS_DLL_PUBLIC uint64_t tvb_get_ntoh40(tvbuff_t *tvb, const int offset);
329 WS_DLL_PUBLIC int64_t tvb_get_ntohi40(tvbuff_t *tvb, const int offset);
330 WS_DLL_PUBLIC uint64_t tvb_get_ntoh48(tvbuff_t *tvb, const int offset);
331 WS_DLL_PUBLIC int64_t tvb_get_ntohi48(tvbuff_t *tvb, const int offset);
332 WS_DLL_PUBLIC uint64_t tvb_get_ntoh56(tvbuff_t *tvb, const int offset);
333 WS_DLL_PUBLIC int64_t tvb_get_ntohi56(tvbuff_t *tvb, const int offset);
334 WS_DLL_PUBLIC uint64_t tvb_get_ntoh64(tvbuff_t *tvb, const int offset);
335 WS_DLL_PUBLIC int64_t tvb_get_ntohi64(tvbuff_t *tvb, const int offset);
336 WS_DLL_PUBLIC float tvb_get_ntohieee_float(tvbuff_t *tvb, const int offset);
337 WS_DLL_PUBLIC double tvb_get_ntohieee_double(tvbuff_t *tvb,
338 const int offset);
340 WS_DLL_PUBLIC uint16_t tvb_get_letohs(tvbuff_t *tvb, const int offset);
341 WS_DLL_PUBLIC int16_t tvb_get_letohis(tvbuff_t *tvb, const int offset);
342 WS_DLL_PUBLIC uint32_t tvb_get_letoh24(tvbuff_t *tvb, const int offset);
343 WS_DLL_PUBLIC int32_t tvb_get_letohi24(tvbuff_t *tvb, const int offset);
344 WS_DLL_PUBLIC uint32_t tvb_get_letohl(tvbuff_t *tvb, const int offset);
345 WS_DLL_PUBLIC int32_t tvb_get_letohil(tvbuff_t *tvb, const int offset);
346 WS_DLL_PUBLIC uint64_t tvb_get_letoh40(tvbuff_t *tvb, const int offset);
347 WS_DLL_PUBLIC int64_t tvb_get_letohi40(tvbuff_t *tvb, const int offset);
348 WS_DLL_PUBLIC uint64_t tvb_get_letoh48(tvbuff_t *tvb, const int offset);
349 WS_DLL_PUBLIC int64_t tvb_get_letohi48(tvbuff_t *tvb, const int offset);
350 WS_DLL_PUBLIC uint64_t tvb_get_letoh56(tvbuff_t *tvb, const int offset);
351 WS_DLL_PUBLIC int64_t tvb_get_letohi56(tvbuff_t *tvb, const int offset);
352 WS_DLL_PUBLIC uint64_t tvb_get_letoh64(tvbuff_t *tvb, const int offset);
353 WS_DLL_PUBLIC int64_t tvb_get_letohi64(tvbuff_t *tvb, const int offset);
354 WS_DLL_PUBLIC float tvb_get_letohieee_float(tvbuff_t *tvb, const int offset);
355 WS_DLL_PUBLIC double tvb_get_letohieee_double(tvbuff_t *tvb,
356 const int offset);
358 WS_DLL_PUBLIC uint16_t tvb_get_uint16(tvbuff_t *tvb, const int offset, const unsigned encoding);
359 WS_DEPRECATED_X("Use tvb_get_uint16 instead")
360 static inline uint16_t tvb_get_guint16(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_uint16(tvb, offset, encoding); }
361 WS_DLL_PUBLIC int16_t tvb_get_int16(tvbuff_t *tvb, const int offset, const unsigned encoding);
362 WS_DEPRECATED_X("Use tvb_get_int16 instead")
363 static inline int16_t tvb_get_gint16(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_int16(tvb, offset, encoding); }
364 WS_DLL_PUBLIC uint32_t tvb_get_uint24(tvbuff_t *tvb, const int offset, const unsigned encoding);
365 WS_DEPRECATED_X("Use tvb_get_uint24 instead")
366 static inline uint32_t tvb_get_guint24(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_uint24(tvb, offset, encoding); }
367 WS_DLL_PUBLIC int32_t tvb_get_int24(tvbuff_t *tvb, const int offset, const unsigned encoding);
368 WS_DEPRECATED_X("Use tvb_get_int24 instead")
369 static inline int32_t tvb_get_gint24(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_int24(tvb, offset, encoding); }
370 WS_DLL_PUBLIC uint32_t tvb_get_uint32(tvbuff_t *tvb, const int offset, const unsigned encoding);
371 WS_DEPRECATED_X("Use tvb_get_uint32 instead")
372 static inline uint32_t tvb_get_guint32(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_uint32(tvb, offset, encoding); }
373 WS_DLL_PUBLIC int32_t tvb_get_int32(tvbuff_t *tvb, const int offset, const unsigned encoding);
374 WS_DEPRECATED_X("Use tvb_get_int32 instead")
375 static inline int32_t tvb_get_gint32(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_int32(tvb, offset, encoding); }
376 WS_DLL_PUBLIC uint64_t tvb_get_uint40(tvbuff_t *tvb, const int offset, const unsigned encoding);
377 WS_DEPRECATED_X("Use tvb_get_uint40 instead")
378 static inline uint64_t tvb_get_guint40(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_uint40(tvb, offset, encoding); }
379 WS_DLL_PUBLIC int64_t tvb_get_int40(tvbuff_t *tvb, const int offset, const unsigned encoding);
380 WS_DEPRECATED_X("Use tvb_get_int40 instead")
381 static inline int64_t tvb_get_gint40(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_int40(tvb, offset, encoding); }
382 WS_DLL_PUBLIC uint64_t tvb_get_uint48(tvbuff_t *tvb, const int offset, const unsigned encoding);
383 WS_DEPRECATED_X("Use tvb_get_uint48 instead")
384 static inline uint64_t tvb_get_guint48(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_uint48(tvb, offset, encoding); }
385 WS_DLL_PUBLIC int64_t tvb_get_int48(tvbuff_t *tvb, const int offset, const unsigned encoding);
386 WS_DEPRECATED_X("Use tvb_get_int48 instead")
387 static inline int64_t tvb_get_gint48(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_int48(tvb, offset, encoding); }
388 WS_DLL_PUBLIC uint64_t tvb_get_uint56(tvbuff_t *tvb, const int offset, const unsigned encoding);
389 WS_DEPRECATED_X("Use tvb_get_uint56 instead")
390 static inline uint64_t tvb_get_guint56(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_uint56(tvb, offset, encoding); }
391 WS_DLL_PUBLIC int64_t tvb_get_int56(tvbuff_t *tvb, const int offset, const unsigned encoding);
392 WS_DEPRECATED_X("Use tvb_get_int56 instead")
393 static inline int64_t tvb_get_gint56(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_int56(tvb, offset, encoding); }
394 WS_DLL_PUBLIC uint64_t tvb_get_uint64(tvbuff_t *tvb, const int offset, const unsigned encoding);
395 WS_DLL_PUBLIC uint64_t tvb_get_uint64_with_length(tvbuff_t *tvb, const int offset, unsigned length, const unsigned encoding);
396 WS_DEPRECATED_X("Use tvb_get_uint64 instead")
397 static inline uint64_t tvb_get_guint64(tvbuff_t *tvb, const int offset, const unsigned encoding) {return tvb_get_uint64(tvb, offset, encoding); }
398 WS_DLL_PUBLIC int64_t tvb_get_int64(tvbuff_t *tvb, const int offset, const unsigned encoding);
399 WS_DEPRECATED_X("Use tvb_get_int64 instead")
400 static inline int64_t tvb_get_gint64(tvbuff_t *tvb, const int offset, const unsigned encoding) { return tvb_get_int64(tvb, offset, encoding); }
401 WS_DLL_PUBLIC float tvb_get_ieee_float(tvbuff_t *tvb, const int offset, const unsigned encoding);
402 WS_DLL_PUBLIC double tvb_get_ieee_double(tvbuff_t *tvb, const int offset, const unsigned encoding);
405 * Fetch 16-bit and 32-bit values in host byte order.
406 * Used for some pseudo-headers in pcap/pcapng files, in which the
407 * headers are, when capturing, in the byte order of the host, and
408 * are converted to the byte order of the host reading the file
409 * when reading a capture file.
411 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
412 #define tvb_get_h_uint16 tvb_get_letohs
413 #define tvb_get_h_uint32 tvb_get_letohl
414 #elif G_BYTE_ORDER == G_BIG_ENDIAN
415 #define tvb_get_h_uint16 tvb_get_ntohs
416 #define tvb_get_h_uint32 tvb_get_ntohl
417 #else
418 #error "Unsupported byte order"
419 #endif
422 /* Fetch a time value from an ASCII-style string in the tvb.
424 * @param[in] offset The beginning offset in the tvb (cannot be negative)
425 * @param[in] length The field's length in the tvb (or -1 for remaining)
426 * @param[in] encoding The ENC_* that defines the format (e.g., ENC_ISO_8601_DATE_TIME)
427 * @param[in,out] ns The pre-allocated nstime_t that will be set to the decoded value
428 * @param[out] endoff if not NULL, should point to a int that this
429 * routine will then set to be the offset to the character after
430 * the last character used in the conversion. This is useful because
431 * they may not consume the whole section.
433 * @return a pointer to the nstime_t passed-in, or NULL on failure; if no
434 * valid conversion could be performed, *endoff is set to 0, and the
435 * nstime_t* passed-in will be cleared.
437 * @note The conversion ignores leading spaces, and will succeed even if it does
438 * not consume the entire string. If you care about such things, always compare
439 * the *endoff to where you expect it to be (namely, offset+length).
441 * This routine will not throw an error unless the passed-in arguments are
442 * invalid (e.g., offset is beyond the tvb's length).
444 * @warning This only works for string encodings which encode ASCII characters in
445 * a single byte: ENC_ASCII, ENC_UTF_8, ENC_ISO_8859_*, etc. It does NOT work
446 * for purely multi-byte encodings such as ENC_UTF_16, ENC_UCS_*, etc.
448 WS_DLL_PUBLIC
449 nstime_t* tvb_get_string_time(tvbuff_t *tvb, const int offset, const int length,
450 const unsigned encoding, nstime_t* ns, int *endoff);
452 /* Similar to above, but returns a GByteArray based on the case-insensitive
453 * hex-char strings with optional separators, and with optional leading spaces.
454 * The separators allowed are based on the ENC_SEP_* passed in the encoding param.
456 * The passed-in bytes is set to the values, and its pointer is also the return
457 * value or NULL on error. The GByteArray bytes must be pre-constructed with
458 * g_byte_array_new().
460 WS_DLL_PUBLIC
461 GByteArray* tvb_get_string_bytes(tvbuff_t *tvb, const int offset, const int length,
462 const unsigned encoding, GByteArray* bytes, int *endoff);
465 * Fetch an IPv4 address, in network byte order.
466 * We do *not* convert it to host byte order; we leave it in
467 * network byte order, as that's what its callers expect. */
468 WS_DLL_PUBLIC uint32_t tvb_get_ipv4(tvbuff_t *tvb, const int offset);
470 /* Fetch an IPv6 address. */
471 WS_DLL_PUBLIC void tvb_get_ipv6(tvbuff_t *tvb, const int offset,
472 ws_in6_addr *addr);
475 * Fetches an IPv4 address from a tvbuff and
476 * masks out bits other than those covered by a prefix length
478 * @param tvb tvbuff to read an IPv4 address from
479 * @param offset offset in the tvbuff to read the IPv4 address from
480 * @param addr memory location where the IPv4 address read should be stored
481 * @param prefix_len the length of the prefix (in bits)
482 * @return the length (in bytes) of the address on success, or -1 on failure
484 extern int tvb_get_ipv4_addr_with_prefix_len(tvbuff_t *tvb, int offset,
485 ws_in4_addr *addr, uint32_t prefix_len);
488 * Fetches an IPv6 address from a tvbuff and
489 * masks out bits other than those covered by a prefix length
491 * @param tvb tvbuff to read an IPv6 address from
492 * @param offset offset in the tvbuff to read the IPv6 address from
493 * @param addr memory location where the IPv6 address read should be stored
494 * @param prefix_len the length of the prefix (in bits)
495 * @return the length (in bytes) of the address on success, or -1 on failure
497 extern int tvb_get_ipv6_addr_with_prefix_len(tvbuff_t *tvb, int offset,
498 ws_in6_addr *addr, uint32_t prefix_len);
500 /* Fetch a GUID. */
501 WS_DLL_PUBLIC void tvb_get_ntohguid(tvbuff_t *tvb, const int offset,
502 e_guid_t *guid);
504 WS_DLL_PUBLIC void tvb_get_letohguid(tvbuff_t *tvb, const int offset,
505 e_guid_t *guid);
507 WS_DLL_PUBLIC void tvb_get_guid(tvbuff_t *tvb, const int offset,
508 e_guid_t *guid, const unsigned encoding);
510 /* Fetches a byte array given a bit offset in a tvb */
511 WS_DLL_PUBLIC uint8_t* tvb_get_bits_array(wmem_allocator_t *scope, tvbuff_t *tvb,
512 const int offset, size_t length, size_t *data_length, const unsigned encoding);
514 /* Fetch a specified number of bits from bit offset in a tvb. All of these
515 * functions are equivalent, except for the type of the return value. Note
516 * that the parameter encoding (where supplied) indicates the *bit* ordering
517 * within each octet, but offsets 0-7 still indicate octet 0 in the buffer.
518 * Versions of Wireshark prior to 3.6 ignored the encoding parameter.
521 /* get 1 - 8 bits returned in a uint8_t */
522 WS_DLL_PUBLIC uint8_t tvb_get_bits8(tvbuff_t *tvb, unsigned bit_offset,
523 const int no_of_bits);
525 /* get 1 - 16 bits returned in a uint16_t */
526 WS_DLL_PUBLIC uint16_t tvb_get_bits16(tvbuff_t *tvb, unsigned bit_offset,
527 const int no_of_bits, const unsigned encoding);
529 /* get 1 - 32 bits returned in a uint32_t */
530 WS_DLL_PUBLIC uint32_t tvb_get_bits32(tvbuff_t *tvb, unsigned bit_offset,
531 const int no_of_bits, const unsigned encoding);
533 /* get 1 - 64 bits returned in a uint64_t */
534 WS_DLL_PUBLIC uint64_t tvb_get_bits64(tvbuff_t *tvb, unsigned bit_offset,
535 const int no_of_bits, const unsigned encoding);
538 * This function has EXACTLY the same behavior as
539 * tvb_get_bits32()
541 WS_DLL_PUBLIC uint32_t tvb_get_bits(tvbuff_t *tvb, const unsigned bit_offset,
542 const int no_of_bits, const unsigned encoding);
544 /** Returns target for convenience. Does not suffer from possible
545 * expense of tvb_get_ptr(), since this routine is smart enough
546 * to copy data in chunks if the request range actually exists in
547 * different "real" tvbuffs. This function assumes that the target
548 * memory is already allocated; it does not allocate or free the
549 * target memory. */
550 WS_DLL_PUBLIC void *tvb_memcpy(tvbuff_t *tvb, void *target, const int offset,
551 size_t length);
553 /** Given an allocator scope, a tvbuff, a byte offset, a byte length:
555 * allocate a buffer using the specified scope;
557 * copy the data from the tvbuff specified by the offset and length
558 * into that buffer, using tvb_memcpy();
560 * and return a pointer to the buffer.
562 * Throws an exception if the tvbuff ends before the data being copied does.
564 * If scope is set to NULL it is the user's responsibility to wmem_free()
565 * the memory allocated. Otherwise memory is automatically freed when the
566 * scope lifetime is reached.
568 WS_DLL_PUBLIC void *tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb,
569 const int offset, size_t length);
571 /** WARNING! This function is possibly expensive, temporarily allocating
572 * another copy of the packet data. Furthermore, it's dangerous because once
573 * this pointer is given to the user, there's no guarantee that the user will
574 * honor the 'length' and not overstep the boundaries of the buffer.
576 * If you're thinking of using tvb_get_ptr, STOP WHAT YOU ARE DOING
577 * IMMEDIATELY. Go take a break. Consider that tvb_get_ptr hands you
578 * a raw, unprotected pointer that you can easily use to create a
579 * security vulnerability or otherwise crash Wireshark. Then consider
580 * that you can probably find a function elsewhere in this file that
581 * does exactly what you want in a much more safe and robust manner.
583 * The returned pointer is data that is internal to the tvbuff, so do not
584 * attempt to free it. Don't modify the data, either, because another tvbuff
585 * that might be using this tvbuff may have already copied that portion of
586 * the data (sometimes tvbuff's need to make copies of data, but that's the
587 * internal implementation that you need not worry about). Assume that the
588 * uint8_t* points to read-only data that the tvbuff manages.
590 * Return a pointer into our buffer if the data asked for via 'offset'/'length'
591 * is contiguous (which might not be the case for a "composite" tvbuff). If the
592 * data is not contiguous, a tvb_memdup() is called for the entire buffer
593 * and the pointer to the newly-contiguous data is returned. This dynamically-
594 * allocated memory will be freed when the tvbuff is freed, after the
595 * tvbuff_free_cb_t() is called, if any. */
596 WS_DLL_PUBLIC const uint8_t *tvb_get_ptr(tvbuff_t *tvb, const int offset,
597 const int length);
599 /** Find first occurrence of needle in tvbuff, starting at offset. Searches
600 * at most maxlength number of bytes; if maxlength is -1, searches to
601 * end of tvbuff.
602 * Returns the offset of the found needle, or -1 if not found.
603 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
604 * in that case, -1 will be returned if the boundary is reached before
605 * finding needle. */
606 WS_DLL_PUBLIC int tvb_find_uint8(tvbuff_t *tvb, const int offset,
607 const int maxlength, const uint8_t needle);
609 WS_DEPRECATED_X("Use tvb_find_uint8 instead")
610 static inline int tvb_find_guint8(tvbuff_t* tvb, const int offset,
611 const int maxlength, const uint8_t needle) { return tvb_find_uint8(tvb, offset, maxlength, needle); }
613 /** Same as tvb_find_uint8() with 16bit needle. */
614 WS_DLL_PUBLIC int tvb_find_uint16(tvbuff_t *tvb, const int offset,
615 const int maxlength, const uint16_t needle);
617 WS_DEPRECATED_X("Use tvb_find_uint16 instead")
618 static inline int tvb_find_guint16(tvbuff_t* tvb, const int offset,
619 const int maxlength, const uint16_t needle) {
620 return tvb_find_uint16(tvb, offset, maxlength, needle);
622 /** Find first occurrence of any of the needles of the pre-compiled pattern in
623 * tvbuff, starting at offset. The passed in pattern must have been "compiled"
624 * before-hand, using ws_mempbrk_compile().
625 * Searches at most maxlength number of bytes. Returns the offset of the
626 * found needle, or -1 if not found and the found needle.
627 * Will not throw an exception, even if
628 * maxlength exceeds boundary of tvbuff; in that case, -1 will be returned if
629 * the boundary is reached before finding needle. */
630 WS_DLL_PUBLIC int tvb_ws_mempbrk_pattern_uint8(tvbuff_t *tvb, const int offset,
631 const int maxlength, const ws_mempbrk_pattern* pattern, unsigned char *found_needle);
633 WS_DEPRECATED_X("Use tvb_ws_mempbrk_pattern_uint8 instead")
634 static inline int tvb_ws_mempbrk_pattern_guint8(tvbuff_t* tvb, const int offset,
635 const int maxlength, const ws_mempbrk_pattern* pattern, unsigned char* found_needle) {
636 return tvb_ws_mempbrk_pattern_uint8(tvb, offset, maxlength, pattern, found_needle);
639 /** Find size of stringz (NUL-terminated string) by looking for terminating
640 * NUL. The size of the string includes the terminating NUL.
642 * If the NUL isn't found, it throws the appropriate exception.
644 WS_DLL_PUBLIC unsigned tvb_strsize(tvbuff_t *tvb, const int offset);
646 /** Find size of UCS-2 or UTF-16 stringz (NUL-terminated string) by
647 * looking for terminating 16-bit NUL. The size of the string includes
648 * the terminating NUL.
650 * If the NUL isn't found, it throws the appropriate exception.
652 WS_DLL_PUBLIC unsigned tvb_unicode_strsize(tvbuff_t *tvb, const int offset);
654 /** Find length of string by looking for end of zero terminated string, up to
655 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
656 * of tvbuff.
657 * Returns -1 if 'maxlength' reached before finding EOS. */
658 WS_DLL_PUBLIC int tvb_strnlen(tvbuff_t *tvb, const int offset,
659 const unsigned maxlength);
662 * Format the data in the tvb from offset for size.
664 WS_DLL_PUBLIC char *tvb_format_text(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset,
665 const int size);
668 * Like "tvb_format_text()", but for 'wsp'; don't show
669 * the characters as C-style escapes.
671 WS_DLL_PUBLIC char *tvb_format_text_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const int offset,
672 const int size);
675 * Like "tvb_format_text()", but for null-padded strings; don't show
676 * the null padding characters as "\000".
678 extern char *tvb_format_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset,
679 const int size);
682 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
683 * the null padding characters as "\000".
685 extern char *tvb_format_stringzpad_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const int offset,
686 const int size);
689 * Given an allocator scope, a tvbuff, a byte offset, a byte length, and
690 * a string encoding, with the specified offset and length referring to
691 * a string in the specified encoding:
693 * allocate a buffer using the specified scope;
695 * convert the string from the specified encoding to UTF-8, possibly
696 * mapping some characters or invalid octet sequences to the Unicode
697 * REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
698 * trailing '\0', into that buffer;
700 * and return a pointer to the buffer.
702 * Throws an exception if the tvbuff ends before the string does.
704 * If scope is set to NULL it is the user's responsibility to wmem_free()
705 * the memory allocated. Otherwise memory is automatically freed when the
706 * scope lifetime is reached.
708 WS_DLL_PUBLIC uint8_t *tvb_get_string_enc(wmem_allocator_t *scope,
709 tvbuff_t *tvb, const int offset, const int length, const unsigned encoding);
712 * Given an allocator scope, a tvbuff, a bit offset, and a length in
713 * 7-bit characters (not octets!), with the specified offset and
714 * length referring to a string in the 3GPP TS 23.038 7bits encoding,
715 * with code points packed into 7 bits:
717 * allocate a buffer using the specified scope;
719 * convert the string from the specified encoding to UTF-8, possibly
720 * mapping some characters or invalid octet sequences to the Unicode
721 * REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
722 * trailing '\0', into that buffer;
724 * and return a pointer to the buffer.
726 * Throws an exception if the tvbuff ends before the string does.
728 * If scope is set to NULL it is the user's responsibility to wmem_free()
729 * the memory allocated. Otherwise memory is automatically freed when the
730 * scope lifetime is reached.
732 WS_DLL_PUBLIC char *tvb_get_ts_23_038_7bits_string_packed(wmem_allocator_t *scope,
733 tvbuff_t *tvb, const int bit_offset, int no_of_chars);
736 * Given an allocator scope, a tvbuff, an offset, and a length in
737 * octets with the specified offset and length referring to a string
738 * in the 3GPP TS 23.038 7bits encoding, with one octet per code poiint
739 * (the 8th bit of each octet should be 0; if not, the octet is invalid):
741 * allocate a buffer using the specified scope;
743 * convert the string from the specified encoding to UTF-8, possibly
744 * mapping some characters or invalid octet sequences to the Unicode
745 * REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
746 * trailing '\0', into that buffer;
748 * and return a pointer to the buffer.
750 * Throws an exception if the tvbuff ends before the string does.
752 * If scope is set to NULL it is the user's responsibility to wmem_free()
753 * the memory allocated. Otherwise memory is automatically freed when the
754 * scope lifetime is reached.
756 WS_DLL_PUBLIC char *tvb_get_ts_23_038_7bits_string_unpacked(wmem_allocator_t *scope,
757 tvbuff_t *tvb, const int offset, int length);
760 * Given an allocator scope, a tvbuff, an offset, and a length in
761 * octets with the specified offset and length referring to a string
762 * in the ETSI TS 102 221 Annex A encodings; if not:
764 * allocate a buffer using the specified scope;
766 * convert the string from the specified encoding to UTF-8, possibly
767 * mapping some characters or invalid octet sequences to the Unicode
768 * REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
769 * trailing '\0', into that buffer;
771 * and return a pointer to the buffer.
773 * Throws an exception if the tvbuff ends before the string does.
775 * If scope is set to NULL it is the user's responsibility to wmem_free()
776 * the memory allocated. Otherwise memory is automatically freed when the
777 * scope lifetime is reached.
779 WS_DLL_PUBLIC char *tvb_get_etsi_ts_102_221_annex_a_string(wmem_allocator_t *scope,
780 tvbuff_t *tvb, const int offset, int length);
783 * Given an allocator scope, a tvbuff, an offset, and a length in
784 * 7-bit characters (not octets!), with the specified offset and
785 * length referring to a string in the ASCII 7bits encoding:
787 * allocate a buffer using the specified scope;
789 * convert the string from the specified encoding to UTF-8, possibly
790 * mapping some characters or invalid octet sequences to the Unicode
791 * REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
792 * trailing '\0', into that buffer;
794 * and return a pointer to the buffer.
796 * Throws an exception if the tvbuff ends before the string does.
798 * If scope is set to NULL it is the user's responsibility to wmem_free()
799 * the memory allocated. Otherwise memory is automatically freed when the
800 * scope lifetime is reached.
802 WS_DLL_PUBLIC char *tvb_get_ascii_7bits_string(wmem_allocator_t *scope,
803 tvbuff_t *tvb, const int bit_offset, int no_of_chars);
806 * Given an allocator scope, a tvbuff, a byte offset, a byte length, and
807 * a string encoding, with the specified offset and length referring to
808 * a null-padded string in the specified encoding:
810 * allocate a buffer using the specified scope;
812 * convert the string from the specified encoding to UTF-8, possibly
813 * mapping some characters or invalid octet sequences to the Unicode
814 * REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
815 * trailing '\0', into that buffer;
817 * and return a pointer to the buffer.
819 * Throws an exception if the tvbuff ends before the string does.
821 * If scope is set to NULL it is the user's responsibility to wmem_free()
822 * the memory allocated. Otherwise memory is automatically freed when the
823 * scope lifetime is reached.
825 WS_DLL_PUBLIC uint8_t *tvb_get_stringzpad(wmem_allocator_t *scope,
826 tvbuff_t *tvb, const int offset, const int length, const unsigned encoding);
829 * Given an allocator scope, a tvbuff, a byte offset, a pointer to a
830 * int, and a string encoding, with the specified offset referring to
831 * a null-terminated string in the specified encoding:
833 * find the length of that string (and throw an exception if the tvbuff
834 * ends before we find the null);
836 * allocate a buffer using the specified scope;
838 * convert the string from the specified encoding to UTF-8, possibly
839 * mapping some characters or invalid octet sequences to the Unicode
840 * REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
841 * trailing '\0', into that buffer;
843 * if the pointer to the int is non-null, set the int to which it
844 * points to the length of the string;
846 * and return a pointer to the buffer.
848 * Throws an exception if the tvbuff ends before the string does.
850 * If scope is set to NULL it is the user's responsibility to wmem_free()
851 * the memory allocated. Otherwise memory is automatically freed when the
852 * scope lifetime is reached.
854 WS_DLL_PUBLIC uint8_t *tvb_get_stringz_enc(wmem_allocator_t *scope,
855 tvbuff_t *tvb, const int offset, int *lengthp, const unsigned encoding);
858 * Given a tvbuff and an offset, with the offset assumed to refer to
859 * a null-terminated string, find the length of that string (and throw
860 * an exception if the tvbuff ends before we find the null), allocate
861 * a buffer big enough to hold the string, copy the string into it,
862 * and return a pointer to the string. Also return the length of the
863 * string (including the terminating null) through a pointer.
865 * This returns a constant (unmodifiable) string that does not need
866 * to be freed; instead, it will automatically be freed once the next
867 * packet is dissected.
869 * It is slightly more efficient than the other routines, but does *NOT*
870 * do any translation to UTF-8 - the string consists of the raw octets
871 * of the string, in whatever encoding they happen to be in, and, if
872 * the string is not valid in that encoding, with invalid octet sequences
873 * as they are in the packet.
875 * This function is deprecated because it does no validation of the string
876 * encoding. Do not use in new code. Prefer other APIs such as:
877 * tvb_get_stringz_enc()
878 * proto_tree_add_item_ret_string_and_length()
879 * tvb_strsize() and validate the pointed to memory region manually.
881 WS_DLL_PUBLIC
882 WS_DEPRECATED_X("Use APIs that return a valid UTF-8 string instead")
883 const uint8_t *tvb_get_const_stringz(tvbuff_t *tvb,
884 const int offset, int *lengthp);
886 /** Looks for a NUL byte in tvbuff and copies
887 * no more than bufsize number of bytes, including terminating NUL, to buffer.
888 * Returns number of bytes copied (not including terminating NUL).
890 * When processing a packet where the remaining number of bytes is less
891 * than bufsize, an exception is not thrown if the end of the packet
892 * is reached before the NUL is found. The byte buffer is guaranteed to
893 * have a terminating NUL.
895 WS_DLL_PUBLIC int tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const int offset,
896 const unsigned bufsize, uint8_t *buffer);
899 * Given a tvbuff, an offset into the tvbuff, a buffer, and a buffer size,
900 * extract as many raw bytes from the tvbuff, starting at the offset,
901 * as 1) are available in the tvbuff and 2) will fit in the buffer, leaving
902 * room for a terminating NUL.
904 WS_DLL_PUBLIC int tvb_get_raw_bytes_as_string(tvbuff_t *tvb, const int offset, char *buffer, size_t bufsize);
906 /** Iterates over the provided portion of the tvb checking that each byte
907 * is an ascii printable character.
908 * Returns true if all bytes are printable, false otherwise
910 WS_DLL_PUBLIC bool tvb_ascii_isprint(tvbuff_t *tvb, const int offset,
911 const int length);
913 /** Iterates over the provided portion of the tvb checking that it is
914 * valid UTF-8 consisting entirely of printable characters. (The characters
915 * must be complete; if the portion ends in a partial sequence that could
916 * begin a valid character, this returns false.) The length may be -1 for
917 * "all the way to the end of the tvbuff".
918 * Returns true if printable, false otherwise
920 * @see isprint_utf8_string()
922 WS_DLL_PUBLIC bool tvb_utf_8_isprint(tvbuff_t *tvb, const int offset,
923 const int length);
925 /** Iterates over the provided portion of the tvb checking that each byte
926 * is an ascii digit.
927 * Returns true if all bytes are digits, false otherwise
929 WS_DLL_PUBLIC bool tvb_ascii_isdigit(tvbuff_t *tvb, const int offset,
930 const int length);
933 * Given a tvbuff, an offset into the tvbuff, and a length that starts
934 * at that offset (which may be -1 for "all the way to the end of the
935 * tvbuff"), find the end of the (putative) line that starts at the
936 * specified offset in the tvbuff, going no further than the specified
937 * length.
939 * Return the length of the line (not counting the line terminator at
940 * the end), or, if we don't find a line terminator:
942 * if "deseg" is true, return -1;
944 * if "deseg" is false, return the amount of data remaining in
945 * the buffer.
947 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
948 * character past the line terminator, or past the end of the buffer if
949 * we don't find a line terminator. (It's not set if we return -1.)
951 WS_DLL_PUBLIC int tvb_find_line_end(tvbuff_t *tvb, const int offset, int len,
952 int *next_offset, const bool desegment);
955 * Given a tvbuff, an offset into the tvbuff, and a length that starts
956 * at that offset (which may be -1 for "all the way to the end of the
957 * tvbuff"), find the end of the (putative) line that starts at the
958 * specified offset in the tvbuff, going no further than the specified
959 * length.
961 * However, treat quoted strings inside the buffer specially - don't
962 * treat newlines in quoted strings as line terminators.
964 * Return the length of the line (not counting the line terminator at
965 * the end), or the amount of data remaining in the buffer if we don't
966 * find a line terminator.
968 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
969 * character past the line terminator, or past the end of the buffer if
970 * we don't find a line terminator.
972 WS_DLL_PUBLIC int tvb_find_line_end_unquoted(tvbuff_t *tvb, const int offset,
973 int len, int *next_offset);
976 * Copied from the mgcp dissector. (This function should be moved to /epan )
977 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
978 * character following offset or offset + maxlength -1 whichever
979 * is smaller.
981 * Parameters:
982 * tvb - The tvbuff in which we are skipping whitespace.
983 * offset - The offset in tvb from which we begin trying to skip whitespace.
984 * maxlength - The maximum distance from offset that we may try to skip
985 * whitespace.
987 * Returns: The position in tvb of the first non-whitespace
988 * character following offset or offset + maxlength -1 whichever
989 * is smaller.
992 WS_DLL_PUBLIC int tvb_skip_wsp(tvbuff_t *tvb, const int offset,
993 const int maxlength);
995 WS_DLL_PUBLIC int tvb_skip_wsp_return(tvbuff_t *tvb, const int offset);
997 int tvb_skip_uint8(tvbuff_t *tvb, int offset, const int maxlength, const uint8_t ch);
998 WS_DEPRECATED_X("Use tvb_skip_uint8 instead")
999 static inline int tvb_skip_guint8(tvbuff_t *tvb, int offset, const int maxlength, const uint8_t ch) {
1000 return tvb_skip_uint8(tvb, offset, maxlength, ch);
1004 * Given a tvbuff, an offset into the tvbuff, and a length that starts
1005 * at that offset (which may be -1 for "all the way to the end of the
1006 * tvbuff"), find the end of the token that starts at the
1007 * specified offset in the tvbuff, going no further than the specified
1008 * length.
1010 * Return the length of the token, or, if we don't find a terminator:
1012 * if "deseg" is true, return -1;
1014 * if "deseg" is false, return the amount of data remaining in
1015 * the buffer.
1017 * Set "*next_offset" to the offset of the character past the
1018 * terminator, or past the end of the buffer if we don't find a line
1019 * terminator. (It's not set if we return -1.)
1021 WS_DLL_PUBLIC int tvb_get_token_len(tvbuff_t *tvb, const int offset, int len, int *next_offset, const bool desegment);
1024 * Call strncmp after checking if enough chars left, returning 0 if
1025 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1027 WS_DLL_PUBLIC int tvb_strneql(tvbuff_t *tvb, const int offset,
1028 const char *str, const size_t size);
1031 * Call g_ascii_strncasecmp after checking if enough chars left, returning
1032 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1034 WS_DLL_PUBLIC int tvb_strncaseeql(tvbuff_t *tvb, const int offset,
1035 const char *str, const size_t size);
1038 * Call memcmp after checking if enough chars left, returning 0 if
1039 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1041 WS_DLL_PUBLIC int tvb_memeql(tvbuff_t *tvb, const int offset,
1042 const uint8_t *str, size_t size);
1045 * Format a bunch of data from a tvbuff as bytes, returning a pointer
1046 * to the string with the formatted data, with "punct" as a byte
1047 * separator.
1049 WS_DLL_PUBLIC char *tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset,
1050 const int len, const char punct);
1053 * Format a bunch of data from a tvbuff as bytes, returning a pointer
1054 * to the string with the formatted data.
1056 WS_DLL_PUBLIC char *tvb_bytes_to_str(wmem_allocator_t *allocator, tvbuff_t *tvb,
1057 const int offset, const int len);
1060 * Given a tvbuff, an offset into the tvbuff, and a length that starts
1061 * at that offset (which may be -1 for "all the way to the end of the
1062 * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
1063 * the low or high half byte, formatting the digits according to an input digit
1064 * set, if NUL a default digit set of 0-9 returning "?" for overdecadic digits
1065 * will be used. A pointer to the WMEM-allocated string will
1066 * be returned. Note a tvbuff content of 0xf is considered a 'filler' and will
1067 * end the conversion.
1069 typedef struct dgt_set_t
1071 const unsigned char out[16];
1073 dgt_set_t;
1075 WS_DLL_PUBLIC const char *tvb_bcd_dig_to_str(wmem_allocator_t *scope,
1076 tvbuff_t *tvb, const int offset, const int len, const dgt_set_t *dgt,
1077 bool skip_first);
1080 * Given a tvbuff, an offset into the tvbuff, and a length that starts
1081 * at that offset (which may be -1 for "all the way to the end of the
1082 * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
1083 * the low or high half byte, formatting the digits according to an input digit
1084 * set, if NUL a default digit set of 0-9 returning "?" for overdecadic digits
1085 * will be used. A pointer to the WMEM-allocated string will
1086 * be returned. Note a tvbuff content of 0xf is considered a 'filler' and will
1087 * end the conversion. Function uses big endian convetion: first digit is based
1088 * on high order nibble, second digit is based on low order nibble.
1090 WS_DLL_PUBLIC const char *tvb_bcd_dig_to_str_be(wmem_allocator_t *scope,
1091 tvbuff_t *tvb, const int offset, const int len, const dgt_set_t *dgt,
1092 bool skip_first);
1095 * Given a wmem scope, a tvbuff, an offset, a length, an input digit
1096 * set, and a boolean indicator, fetch BCD-encoded digits from a
1097 * tvbuff starting from either the low or high half byte of the
1098 * first byte depending on the boolean indicator (true means "start
1099 * with the high half byte, ignoring the low half byte", and false
1100 * means "start with the low half byte and proceed to the high half
1101 * byte), formating the digits into characters according to the
1102 * input digit set, and return a pointer to a UTF-8 string, allocated
1103 * using the wmem scope. A high-order nibble of 0xf is considered a
1104 * 'filler' and will end the conversion. If odd is set the high order
1105 * nibble in the last octet will be skipped. If bigendian is set then
1106 * high order nibble is taken as first digit of a byte and low order
1107 * nibble as second digit.
1109 WS_DLL_PUBLIC char *tvb_get_bcd_string(wmem_allocator_t *scope, tvbuff_t *tvb,
1110 const int offset, int len, const dgt_set_t *dgt,
1111 bool skip_first, bool odd, bool bigendian);
1113 /** Locate a sub-tvbuff within another tvbuff, starting at position
1114 * 'haystack_offset'. Returns the index of the beginning of 'needle' within
1115 * 'haystack', or -1 if 'needle' is not found. The index is relative
1116 * to the start of 'haystack', not 'haystack_offset'. */
1117 WS_DLL_PUBLIC int tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb,
1118 const int haystack_offset);
1120 /* From tvbuff_zlib.c */
1122 WS_DEPRECATED_X("Use tvb_uncompress_zlib instead")
1123 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress(tvbuff_t *tvb, const int offset,
1124 int comprlen);
1127 * Uncompresses a zlib compressed packet inside a tvbuff at offset with
1128 * length comprlen. Returns an uncompressed tvbuffer if uncompression
1129 * succeeded or NULL if uncompression failed.
1131 * The returned tvbuffer must be freed with `tvb_free` or added to the
1132 * chain of another tvbuffer to avoid a memory leak. Consider using
1133 * tvb_child_uncompress to simplify memory management.
1135 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress_zlib(tvbuff_t *tvb, const int offset,
1136 int comprlen);
1138 WS_DEPRECATED_X("Use tvb_child_uncompress_zlib instead")
1139 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress(tvbuff_t *parent, tvbuff_t *tvb,
1140 const int offset, int comprlen);
1143 * Uncompresses a zlib compressed packet inside a tvbuff at offset with
1144 * length comprlen. Returns an uncompressed tvbuffer attached to parent if
1145 * uncompression succeeded or NULL if uncompression failed.
1147 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress_zlib(tvbuff_t *parent, tvbuff_t *tvb,
1148 const int offset, int comprlen);
1150 /* From tvbuff_brotli.c */
1153 * Uncompresses a brotli compressed packet inside a tvbuff at offset with
1154 * length comprlen. Returns an uncompressed tvbuffer if uncompression
1155 * succeeded or NULL if uncompression failed.
1157 * The returned tvbuffer must be freed with `tvb_free` or added to the
1158 * chain of another tvbuffer to avoid a memory leak. Consider using
1159 * tvb_child_uncompress_brotli to simplify memory management.
1161 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress_brotli(tvbuff_t *tvb, const int offset,
1162 int comprlen);
1165 * Uncompresses a brotli compressed packet inside a tvbuff at offset with
1166 * length comprlen. Returns an uncompressed tvbuffer attached to parent if
1167 * uncompression succeeded or NULL if uncompression failed.
1169 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress_brotli(tvbuff_t *parent, tvbuff_t *tvb,
1170 const int offset, int comprlen);
1172 /* From tvbuff_snappy.c */
1175 * Uncompresses a snappy compressed packet inside a tvbuff at offset with
1176 * length comprlen. Returns an uncompressed tvbuffer if uncompression
1177 * succeeded or NULL if uncompression failed.
1179 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress_snappy(tvbuff_t *tvb, const int offset,
1180 int comprlen);
1183 * Uncompresses a snappy compressed packet inside a tvbuff at offset with
1184 * length comprlen. Returns an uncompressed tvbuffer attached to tvb if
1185 * uncompression succeeded or NULL if uncompression failed.
1187 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress_snappy(tvbuff_t *parent, tvbuff_t *tvb,
1188 const int offset, int comprlen);
1190 /* From tvbuff_lz77.c */
1193 * Uncompresses a Microsoft Plain LZ77 compressed payload inside a
1194 * tvbuff at offset with length comprlen. Returns an uncompressed
1195 * tvbuffer if uncompression succeeded or NULL if uncompression
1196 * failed.
1198 * The returned tvbuffer must be freed with `tvb_free` or added to the
1199 * chain of another tvbuffer to avoid a memory leak. Consider using
1200 * tvb_child_uncompress_lz77 to simplify memory management.
1202 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress_lz77(tvbuff_t *tvb,
1203 const int offset, int comprlen);
1206 * Uncompresses a Microsoft Plain LZ77 compressed payload inside a
1207 * tvbuff at offset with length comprlen. Returns an uncompressed
1208 * tvbuffer attached to parent if uncompression succeeded or NULL if
1209 * uncompression failed.
1211 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress_lz77(tvbuff_t *parent,
1212 tvbuff_t *tvb, const int offset, int comprlen);
1214 /* From tvbuff_lz77huff.c */
1217 * Uncompresses a Microsoft LZ77+Huffman compressed payload inside a
1218 * tvbuff at offset with length comprlen. Returns an uncompressed
1219 * tvbuffer if uncompression succeeded or NULL if uncompression
1220 * failed.
1222 * The returned tvbuffer must be freed with `tvb_free` or added to the
1223 * chain of another tvbuffer to avoid a memory leak. Consider using
1224 * tvb_child_uncompress_lz77huff to simplify memory management.
1226 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress_lz77huff(tvbuff_t *tvb,
1227 const int offset, int comprlen);
1230 * Uncompresses a Microsoft LZ77+Huffman compressed payload inside a
1231 * tvbuff at offset with length comprlen. Returns an uncompressed
1232 * tvbuffer attached to parent if uncompression succeeded or NULL if
1233 * uncompression failed.
1235 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress_lz77huff(tvbuff_t *parent,
1236 tvbuff_t *tvb, const int offset, int comprlen);
1238 /* From tvbuff_lznt1.c */
1241 * Uncompresses a Microsoft LZNT1 compressed payload inside
1242 * a tvbuff at offset with length comprlen. Returns an uncompressed
1243 * tvbuffer if uncompression succeeded or NULL if uncompression
1244 * failed.
1246 * The returned tvbuffer must be freed with `tvb_free` or added to the
1247 * chain of another tvbuffer to avoid a memory leak. Consider using
1248 * tvb_child_uncompress_lznt1 to simplify memory management.
1250 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress_lznt1(tvbuff_t *tvb,
1251 const int offset, int comprlen);
1254 * Uncompresses a Microsoft LZNT1 compressed payload inside
1255 * a tvbuff at offset with length comprlen. Returns an uncompressed
1256 * tvbuffer attached to parent if uncompression succeeded or NULL if
1257 * uncompression failed.
1259 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress_lznt1(tvbuff_t *parent,
1260 tvbuff_t *tvb, const int offset, int comprlen);
1263 * Uncompresses a ZSTD compressed payload inside a
1264 * tvbuff at offset with length comprlen. Returns an uncompressed
1265 * tvbuffer if uncompression succeeded or NULL if uncompression
1266 * failed.
1268 * The returned tvbuffer must be freed with `tvb_free` or added to the
1269 * chain of another tvbuffer to avoid a memory leak. Consider using
1270 * tvb_child_uncompress_zstd to simplify memory management.
1272 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress_zstd(tvbuff_t *tvb,
1273 const int offset, int comprlen);
1276 * Uncompresses a ZSTD compressed payload inside a
1277 * tvbuff at offset with length comprlen. Returns an uncompressed
1278 * tvbuffer attached to parent if uncompression succeeded or NULL
1279 * if uncompression failed.
1281 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress_zstd(tvbuff_t *parent,
1282 tvbuff_t *tvb, const int offset, int comprlen);
1284 /* From tvbuff_base64.c */
1286 /** Return a tvb that contains the binary representation of a base64
1287 * string as a child of the indicated tvb.
1289 * @param parent The parent tvbuff.
1290 * @param base64 The base64 encoded string which binary representation will be
1291 * returned in the child tvb.
1293 * @return A tvb with the binary representation of the base64 decoded string.
1295 extern tvbuff_t* base64_to_tvb(tvbuff_t *parent, const char *base64);
1298 /** Return a tvb that contains the binary representation of a base64
1299 * encoded string in the parent tvb as a child of the indicated tvb.
1301 * @param parent The parent tvbuff.
1302 * @param offset Start of the base64 string in the tvb
1303 * @param length Length of the base64 string in the tvb
1305 * @return A tvb with the binary representation of the base64 decoded string.
1307 extern tvbuff_t* base64_tvb_to_new_tvb(tvbuff_t* parent, int offset, int length);
1309 extern tvbuff_t* base64uri_tvb_to_new_tvb(tvbuff_t* parent, int offset, int length);
1311 /* From tvbuff_hpackhuff.c */
1313 WS_DLL_PUBLIC wmem_strbuf_t* tvb_get_hpack_huffman_strbuf(wmem_allocator_t *scope,
1314 tvbuff_t *tvb, const int offset, const int len);
1316 WS_DLL_PUBLIC tvbuff_t* tvb_child_uncompress_hpack_huff(tvbuff_t *parent,
1317 int offset, int length);
1320 * Extract a variable length integer from a tvbuff.
1321 * Each byte in a varint, except the last byte, has the most significant bit (msb)
1322 * set -- this indicates that there are further bytes to come. For example,
1323 * 1010 1100 0000 0010 is 300
1325 * @param tvb The tvbuff in which we are extracting integer.
1326 * @param offset The offset in tvb from which we begin trying to extract integer.
1327 * @param maxlen The maximum distance from offset that we may try to extract integer
1328 * @param value if parsing succeeds, parsed varint will store here.
1329 * @param encoding The ENC_* that defines the format (e.g., ENC_VARINT_PROTOBUF, ENC_VARINT_QUIC, ENC_VARINT_ZIGZAG, ENC_VARINT_SDNV)
1330 * @return the length of this varint in tvb. 0 means parsing failed.
1332 WS_DLL_PUBLIC unsigned tvb_get_varint(tvbuff_t *tvb, unsigned offset, unsigned maxlen, uint64_t *value, const unsigned encoding);
1334 /************** END OF ACCESSORS ****************/
1336 /** @} */
1338 #ifdef __cplusplus
1340 #endif /* __cplusplus */
1342 #endif /* __TVBUFF_H__ */
1345 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1347 * Local variables:
1348 * c-basic-offset: 4
1349 * tab-width: 8
1350 * indent-tabs-mode: nil
1351 * End:
1353 * vi: set shiftwidth=4 tabstop=8 expandtab:
1354 * :indentSize=4:tabSize=8:noTabs=true: