TODO SMB2 NegotiateContext....
[metze/wireshark/wip.git] / epan / tvbuff.h
1 /* tvbuff.h
2  *
3  * Testy, Virtual(-izable) Buffer of guint8*'s
4  *
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.
7  *
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.
11  *
12  * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
13  *
14  * Wireshark - Network traffic analyzer
15  * By Gerald Combs <gerald@wireshark.org>
16  * Copyright 1998 Gerald Combs
17  *
18  * SPDX-License-Identifier: GPL-2.0-or-later
19  */
20
21 #ifndef __TVBUFF_H__
22 #define __TVBUFF_H__
23
24 #include <glib.h>
25 #include <epan/guid-utils.h>
26 #include <epan/wmem/wmem.h>
27 #include <epan/ipv6.h>
28
29 #include <wsutil/nstime.h>
30 #include "wsutil/ws_mempbrk.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35
36 /**
37  * "testy, virtual(-izable) buffer".  They are testy in that they get mad when
38  * an attempt is made to access data beyond the bounds of their array. In that
39  * case, they throw an exception.
40  *
41  * They are virtualizable in that new tvbuff's can be made from other tvbuffs,
42  * while only the original tvbuff may have data. That is, the new tvbuff has
43  * virtual data.
44  */
45
46 struct tvbuff;
47 typedef struct tvbuff tvbuff_t;
48
49 /** @defgroup tvbuff Testy, Virtual(-izable) Buffers
50  *
51  * Dissector use and management
52  *
53  *  Consider a collection of tvbs as being a chain or stack of tvbs.
54  *
55  *  When dissecting a frame:
56  *   The top-level dissector (packet.c) pushes the initial tvb (containing
57  *   the complete frame) onto the stack (starts the chain) and then calls
58  *   a sub-dissector which in turn calls the next sub-dissector and so on.
59  *   Each sub-dissector may chain additional tvbs (see below) to the tvb
60  *   handed to that dissector. After dissection is complete and control has
61  *   returned to the top-level dissector, the chain of tvbs (stack) is free'd
62  *   via a call to tvb_free_chain() (in epan_dissect_cleanup()).
63  *
64  * A dissector:
65  *  - Can chain new tvbs (subset, real, composite) to the
66  *    tvb handed to the dissector using tvb_new_subset_length_caplen(),
67  *    tvb_new_subset_length(), tvb_new_subset_remaining(),
68  *    tvb_new_child_real_data(), tvb_set_child_real_data_tvbuff(),
69  *    tvb_composite_finalize(), and tvb_child_uncompress(). (Composite
70  *    tvbs should reference only tvbs which are already part of the chain).
71  *  - Must not save for later use (e.g., when dissecting another frame) a
72  *    pointer to a tvb handed to the dissector; (A higher level function
73  *    may very well free the chain thus leaving a dangling pointer).
74  *    This (obviously) also applies to any tvbs chained to the tvb handed
75  *    to the dissector.
76  *  - Can create its own tvb chain (using tvb_new_real_data() which the
77  *
78  *    dissector is free to manage as desired.
79  * @{
80  */
81
82 /** A "real" tvbuff contains a guint8* that points to real data.
83  * The data is allocated and contiguous.
84  *
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.
87  *
88  * A "composite" tvbuff combines multiple tvbuffs sequentially to
89  * produce a larger byte array.
90  *
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.
94  *
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.
98  *
99  * tvbuff's are normally chained together to allow efficient de-allocation of
100  * tvbuff's.
101  */
102
103 typedef void (*tvbuff_free_cb_t)(void*);
104
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  */
109 WS_DLL_PUBLIC tvbuff_t *tvb_new_octet_aligned(tvbuff_t *tvb,
110     guint32 bit_offset, gint32 no_of_bits);
111
112 WS_DLL_PUBLIC tvbuff_t *tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing);
113
114 WS_DLL_PUBLIC tvbuff_t *tvb_clone(tvbuff_t *tvb);
115
116 WS_DLL_PUBLIC tvbuff_t *tvb_clone_offset_len(tvbuff_t *tvb, guint offset,
117     guint len);
118
119 /** Free a tvbuff_t and all tvbuffs chained from it
120  * The tvbuff must be 'the 'head' (initial) tvb of a chain or
121  * must not be in a chain.
122  * If specified, a callback to free the tvbuff data will be invoked
123  * for each tvbuff free'd */
124 WS_DLL_PUBLIC void tvb_free(tvbuff_t *tvb);
125
126 /** Free the 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_chain(tvbuff_t *tvb);
132
133 /** Set a callback function to call when a tvbuff is actually freed
134  * One argument is passed to that callback --- a void* that points
135  * to the real data. Obviously, this only applies to a
136  * "real" tvbuff. */
137 WS_DLL_PUBLIC void tvb_set_free_cb(tvbuff_t *tvb, const tvbuff_free_cb_t func);
138
139 /** Attach a "real" tvbuff to a parent tvbuff. This connection is used
140  * during a tvb_free_chain()... the "child" "real" tvbuff acts as if it
141  * is part of the chain-of-creation of the parent tvbuff, although it
142  * isn't. This is useful if you need to take the data from some tvbuff,
143  * run some operation on it, like decryption or decompression, and make
144  * a new tvbuff from it, yet want the new tvbuff to be part of the chain.
145  * The reality is that the new tvbuff *is* part of the "chain of creation",
146  * but in a way that these tvbuff routines are ignorant of. Use this
147  * function to make the tvbuff routines knowledgable of this fact. */
148 WS_DLL_PUBLIC void tvb_set_child_real_data_tvbuff(tvbuff_t *parent,
149     tvbuff_t *child);
150
151 WS_DLL_PUBLIC tvbuff_t *tvb_new_child_real_data(tvbuff_t *parent,
152     const guint8 *data, const guint length, const gint reported_length);
153
154 /** Create a tvbuff backed by existing data. Can throw ReportedBoundsError.
155  * Normally, a callback to free the data should be registered using
156  * tvb_set_free_cb(); when this tvbuff is freed, then your callback will be
157  * called, and at that time you can free your original data. */
158 WS_DLL_PUBLIC tvbuff_t *tvb_new_real_data(const guint8 *data,
159     const guint length, const gint reported_length);
160
161 /** Create a tvbuff that's a subset of another tvbuff.
162  *
163  * 'backing_offset', if positive, is the offset from the beginning of
164  * the backing tvbuff at which the new tvbuff's data begins, and, if
165  * negative, is the offset from the end of the backing tvbuff at which
166  * the new tvbuff's data begins.
167  *
168  * 'backing_length' is the length of the data to include in the new
169  * tvbuff, starting with the byte at 'backing_offset"; if -1, it
170  * means "to the end of the backing tvbuff".  It can be 0, although
171  * the usefulness of the buffer would be rather limited.
172  *
173  * Will throw BoundsError if 'backing_offset'/'length'
174  * is beyond the bounds of the backing tvbuff.
175  * Can throw ReportedBoundsError. */
176 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset_length_caplen(tvbuff_t *backing,
177     const gint backing_offset, const gint backing_length,
178     const gint reported_length);
179
180 /**
181  * Similar to tvb_new_subset_length_caplen() but with captured length calculated
182  * to fit within the existing captured length and the specified
183  * reported length.
184  * Can throw ReportedBoundsError. */
185 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset_length(tvbuff_t *backing,
186     const gint backing_offset, const gint reported_length);
187
188 /** Similar to tvb_new_subset_length_caplen() but with backing_length and reported_length set
189  * to -1.  Can throw ReportedBoundsError. */
190 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset_remaining(tvbuff_t *backing,
191     const gint backing_offset);
192
193 /*
194 * Both tvb_composite_append and tvb_composite_prepend can throw
195  * BoundsError if member_offset/member_length goes beyond bounds of
196  * the 'member' tvbuff. */
197
198 /** Append to the list of tvbuffs that make up this composite tvbuff */
199 WS_DLL_PUBLIC void tvb_composite_append(tvbuff_t *tvb, tvbuff_t *member);
200
201 /** Prepend to the list of tvbuffs that make up this composite tvbuff */
202 extern void tvb_composite_prepend(tvbuff_t *tvb, tvbuff_t *member);
203
204 /** Create an empty composite tvbuff. */
205 WS_DLL_PUBLIC tvbuff_t *tvb_new_composite(void);
206
207 /** Mark a composite tvbuff as initialized. No further appends or prepends
208  * occur, data access can finally happen after this finalization. */
209 WS_DLL_PUBLIC void tvb_composite_finalize(tvbuff_t *tvb);
210
211
212 /* Get amount of captured data in the buffer (which is *NOT* necessarily the
213  * length of the packet). You probably want tvb_reported_length instead. */
214 WS_DLL_PUBLIC guint tvb_captured_length(const tvbuff_t *tvb);
215
216 /** Computes bytes to end of buffer, from offset (which can be negative,
217  * to indicate bytes from end of buffer). Function returns 0 if offset is
218  * either at the end of the buffer or out of bounds. No exception is thrown.
219  * You probably want tvb_reported_length_remaining instead. */
220 WS_DLL_PUBLIC gint tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset);
221
222 /** Same as above, but throws an exception if the offset is out of bounds. */
223 WS_DLL_PUBLIC guint tvb_ensure_captured_length_remaining(const tvbuff_t *tvb,
224     const gint offset);
225
226 /* Checks (w/o throwing exception) that the bytes referred to by
227  * 'offset'/'length' actually exist in the buffer */
228 WS_DLL_PUBLIC gboolean tvb_bytes_exist(const tvbuff_t *tvb, const gint offset,
229     const gint length);
230
231 /** Checks that the bytes referred to by 'offset'/'length', where 'length'
232  * is a 64-bit unsigned integer, actually exist in the buffer, and throws
233  * an exception if they aren't. */
234 WS_DLL_PUBLIC void tvb_ensure_bytes_exist64(const tvbuff_t *tvb,
235     const gint offset, const guint64 length);
236
237 /** Checks that the bytes referred to by 'offset'/'length' actually exist
238  * in the buffer, and throws an exception if they aren't. */
239 WS_DLL_PUBLIC void tvb_ensure_bytes_exist(const tvbuff_t *tvb,
240     const gint offset, const gint length);
241
242 /* Checks (w/o throwing exception) that offset exists in buffer */
243 WS_DLL_PUBLIC gboolean tvb_offset_exists(const tvbuff_t *tvb,
244     const gint offset);
245
246 /* Get reported length of buffer */
247 WS_DLL_PUBLIC guint tvb_reported_length(const tvbuff_t *tvb);
248
249 /** Computes bytes of reported packet data to end of buffer, from offset
250  * (which can be negative, to indicate bytes from end of buffer). Function
251  * returns 0 if offset is either at the end of the buffer or out of bounds.
252  * No exception is thrown. */
253 WS_DLL_PUBLIC gint tvb_reported_length_remaining(const tvbuff_t *tvb,
254     const gint offset);
255
256 /** Set the reported length of a tvbuff to a given value; used for protocols
257    whose headers contain an explicit length and where the calling
258    dissector's payload may include padding as well as the packet for
259    this protocol.
260
261    Also adjusts the data length. */
262 WS_DLL_PUBLIC void tvb_set_reported_length(tvbuff_t *tvb, const guint);
263
264 WS_DLL_PUBLIC guint tvb_offset_from_real_beginning(const tvbuff_t *tvb);
265
266 /* Returns the offset from the first byte of real data. */
267 WS_DLL_PUBLIC gint tvb_raw_offset(tvbuff_t *tvb);
268
269 /** Set the "this is a fragment" flag. */
270 WS_DLL_PUBLIC void tvb_set_fragment(tvbuff_t *tvb);
271
272 WS_DLL_PUBLIC struct tvbuff *tvb_get_ds_tvb(tvbuff_t *tvb);
273
274
275 /************** START OF ACCESSORS ****************/
276 /* All accessors will throw an exception if appropriate */
277
278 WS_DLL_PUBLIC guint8 tvb_get_guint8(tvbuff_t *tvb, const gint offset);
279 WS_DLL_PUBLIC gint8 tvb_get_gint8(tvbuff_t *tvb, const gint offset);
280
281 WS_DLL_PUBLIC guint16 tvb_get_ntohs(tvbuff_t *tvb, const gint offset);
282 WS_DLL_PUBLIC gint16 tvb_get_ntohis(tvbuff_t *tvb, const gint offset);
283 WS_DLL_PUBLIC guint32 tvb_get_ntoh24(tvbuff_t *tvb, const gint offset);
284 WS_DLL_PUBLIC gint32 tvb_get_ntohi24(tvbuff_t *tvb, const gint offset);
285 WS_DLL_PUBLIC guint32 tvb_get_ntohl(tvbuff_t *tvb, const gint offset);
286 WS_DLL_PUBLIC gint32 tvb_get_ntohil(tvbuff_t *tvb, const gint offset);
287 WS_DLL_PUBLIC guint64 tvb_get_ntoh40(tvbuff_t *tvb, const gint offset);
288 WS_DLL_PUBLIC gint64 tvb_get_ntohi40(tvbuff_t *tvb, const gint offset);
289 WS_DLL_PUBLIC guint64 tvb_get_ntoh48(tvbuff_t *tvb, const gint offset);
290 WS_DLL_PUBLIC gint64 tvb_get_ntohi48(tvbuff_t *tvb, const gint offset);
291 WS_DLL_PUBLIC guint64 tvb_get_ntoh56(tvbuff_t *tvb, const gint offset);
292 WS_DLL_PUBLIC gint64 tvb_get_ntohi56(tvbuff_t *tvb, const gint offset);
293 WS_DLL_PUBLIC guint64 tvb_get_ntoh64(tvbuff_t *tvb, const gint offset);
294 WS_DLL_PUBLIC gint64 tvb_get_ntohi64(tvbuff_t *tvb, const gint offset);
295 WS_DLL_PUBLIC gfloat tvb_get_ntohieee_float(tvbuff_t *tvb, const gint offset);
296 WS_DLL_PUBLIC gdouble tvb_get_ntohieee_double(tvbuff_t *tvb,
297     const gint offset);
298
299 WS_DLL_PUBLIC guint16 tvb_get_letohs(tvbuff_t *tvb, const gint offset);
300 WS_DLL_PUBLIC gint16 tvb_get_letohis(tvbuff_t *tvb, const gint offset);
301 WS_DLL_PUBLIC guint32 tvb_get_letoh24(tvbuff_t *tvb, const gint offset);
302 WS_DLL_PUBLIC gint32 tvb_get_letohi24(tvbuff_t *tvb, const gint offset);
303 WS_DLL_PUBLIC guint32 tvb_get_letohl(tvbuff_t *tvb, const gint offset);
304 WS_DLL_PUBLIC gint32 tvb_get_letohil(tvbuff_t *tvb, const gint offset);
305 WS_DLL_PUBLIC guint64 tvb_get_letoh40(tvbuff_t *tvb, const gint offset);
306 WS_DLL_PUBLIC gint64 tvb_get_letohi40(tvbuff_t *tvb, const gint offset);
307 WS_DLL_PUBLIC guint64 tvb_get_letoh48(tvbuff_t *tvb, const gint offset);
308 WS_DLL_PUBLIC gint64 tvb_get_letohi48(tvbuff_t *tvb, const gint offset);
309 WS_DLL_PUBLIC guint64 tvb_get_letoh56(tvbuff_t *tvb, const gint offset);
310 WS_DLL_PUBLIC gint64 tvb_get_letohi56(tvbuff_t *tvb, const gint offset);
311 WS_DLL_PUBLIC guint64 tvb_get_letoh64(tvbuff_t *tvb, const gint offset);
312 WS_DLL_PUBLIC gint64 tvb_get_letohi64(tvbuff_t *tvb, const gint offset);
313 WS_DLL_PUBLIC gfloat tvb_get_letohieee_float(tvbuff_t *tvb, const gint offset);
314 WS_DLL_PUBLIC gdouble tvb_get_letohieee_double(tvbuff_t *tvb,
315     const gint offset);
316
317 WS_DLL_PUBLIC guint16 tvb_get_guint16(tvbuff_t *tvb, const gint offset, const guint encoding);
318 WS_DLL_PUBLIC gint16 tvb_get_gint16(tvbuff_t *tvb, const gint offset, const guint encoding);
319 WS_DLL_PUBLIC guint32 tvb_get_guint24(tvbuff_t *tvb, const gint offset, const guint encoding);
320 WS_DLL_PUBLIC gint32 tvb_get_gint24(tvbuff_t *tvb, const gint offset, const guint encoding);
321 WS_DLL_PUBLIC guint32 tvb_get_guint32(tvbuff_t *tvb, const gint offset, const guint encoding);
322 WS_DLL_PUBLIC gint32 tvb_get_gint32(tvbuff_t *tvb, const gint offset, const guint encoding);
323 WS_DLL_PUBLIC guint64 tvb_get_guint40(tvbuff_t *tvb, const gint offset, const guint encoding);
324 WS_DLL_PUBLIC gint64 tvb_get_gint40(tvbuff_t *tvb, const gint offset, const guint encoding);
325 WS_DLL_PUBLIC guint64 tvb_get_guint48(tvbuff_t *tvb, const gint offset, const guint encoding);
326 WS_DLL_PUBLIC gint64 tvb_get_gint48(tvbuff_t *tvb, const gint offset, const guint encoding);
327 WS_DLL_PUBLIC guint64 tvb_get_guint56(tvbuff_t *tvb, const gint offset, const guint encoding);
328 WS_DLL_PUBLIC gint64 tvb_get_gint56(tvbuff_t *tvb, const gint offset, const guint encoding);
329 WS_DLL_PUBLIC guint64 tvb_get_guint64(tvbuff_t *tvb, const gint offset, const guint encoding);
330 WS_DLL_PUBLIC gint64 tvb_get_gint64(tvbuff_t *tvb, const gint offset, const guint encoding);
331 WS_DLL_PUBLIC gfloat tvb_get_ieee_float(tvbuff_t *tvb, const gint offset, const guint encoding);
332 WS_DLL_PUBLIC gdouble tvb_get_ieee_double(tvbuff_t *tvb, const gint offset, const guint encoding);
333
334 /*
335  * Fetch 16-bit and 32-bit values in host byte order.
336  * Used for some pseudo-headers in pcap/pcapng files, in which the
337  * headers are, when capturing, in the byte order of the host, and
338  * are converted to the byte order of the host reading the file
339  * when reading a capture file.
340  */
341 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
342 #define tvb_get_h_guint16   tvb_get_letohs
343 #define tvb_get_h_guint32   tvb_get_letohl
344 #elif G_BYTE_ORDER == G_BIG_ENDIAN
345 #define tvb_get_h_guint16   tvb_get_ntohs
346 #define tvb_get_h_guint32   tvb_get_ntohl
347 #else
348 #error "Unsupported byte order"
349 #endif
350
351
352 /* Fetch a time value from an ASCII-style string in the tvb.
353  *
354  * @param[in] offset The beginning offset in the tvb (cannot be negative)
355  * @param[in] length The field's length in the tvb (or -1 for remaining)
356  * @param[in] encoding The ENC_* that defines the format (e.g., ENC_ISO_8601_DATE_TIME)
357  * @param[in,out] ns The pre-allocated nstime_t that will be set to the decoded value
358  * @param[out] endoff if not NULL, should point to a gint that this
359  *     routine will then set to be the offset to the character after
360  *     the last character used in the conversion. This is useful because
361  *     they may not consume the whole section.
362  *
363  * @return a pointer to the nstime_t passed-in, or NULL on failure; if no
364  *    valid conversion could be performed, *endoff is set to 0, and errno will be
365  *    EDOM or ERANGE, and the nstime_t* passed-in will be cleared.
366  *
367  * @note The conversion ignores leading spaces, and will succeed even if it does
368  *    not consume the entire string. If you care about such things, always compare
369  *    the *endoff to where you expect it to be (namely, offset+length).
370  *
371  * This routine will not throw an error unless the passed-in arguments are
372  * invalid (e.g., offset is beyond the tvb's length).
373  *
374  * @warning This only works for string encodings which encode ASCII characters in
375  * a single byte: ENC_ASCII, ENC_UTF_8, ENC_ISO_8859_*, etc. It does NOT work
376  * for purely multi-byte encodings such as ENC_UTF_16, ENC_UCS_*, etc.
377  */
378 WS_DLL_PUBLIC
379 nstime_t* tvb_get_string_time(tvbuff_t *tvb, const gint offset, const gint length,
380                               const guint encoding, nstime_t* ns, gint *endoff);
381
382 /* Similar to above, but returns a GByteArray based on the case-insensitive
383  * hex-char strings with optional separators, and with optional leading spaces.
384  * The separators allowed are based on the ENC_SEP_* passed in the encoding param.
385  *
386  * The passed-in bytes is set to the values, and its pointer is also the return
387  * value or NULL on error. The GByteArray bytes must be pre-constructed with
388  * g_byte_array_new().
389  */
390 WS_DLL_PUBLIC
391 GByteArray* tvb_get_string_bytes(tvbuff_t *tvb, const gint offset, const gint length,
392                                  const guint encoding, GByteArray* bytes, gint *endoff);
393
394 /**
395  * Fetch an IPv4 address, in network byte order.
396  * We do *not* convert it to host byte order; we leave it in
397  * network byte order, as that's what its callers expect. */
398 WS_DLL_PUBLIC guint32 tvb_get_ipv4(tvbuff_t *tvb, const gint offset);
399
400 /* Fetch an IPv6 address. */
401 WS_DLL_PUBLIC void tvb_get_ipv6(tvbuff_t *tvb, const gint offset,
402     ws_in6_addr *addr);
403
404 /* Fetch a GUID. */
405 WS_DLL_PUBLIC void tvb_get_ntohguid(tvbuff_t *tvb, const gint offset,
406     e_guid_t *guid);
407 WS_DLL_PUBLIC void tvb_get_letohguid(tvbuff_t *tvb, const gint offset,
408     e_guid_t *guid);
409 WS_DLL_PUBLIC void tvb_get_guid(tvbuff_t *tvb, const gint offset,
410     e_guid_t *guid, const guint encoding);
411
412 /* Fetch a specified number of bits from bit offset in a tvb.  All of these
413  * functions are equivalent, except for the type of the return value.  Note
414  * that the parameter encoding (where supplied) is meaningless and ignored */
415
416 /* get 1 - 8 bits returned in a guint8 */
417 WS_DLL_PUBLIC guint8 tvb_get_bits8(tvbuff_t *tvb, guint bit_offset,
418     const gint no_of_bits);
419 /* get 1 - 16 bits returned in a guint16 */
420 WS_DLL_PUBLIC guint16 tvb_get_bits16(tvbuff_t *tvb, guint bit_offset,
421     const gint no_of_bits, const guint encoding);
422 /* get 1 - 32 bits returned in a guint32 */
423 WS_DLL_PUBLIC guint32 tvb_get_bits32(tvbuff_t *tvb, guint bit_offset,
424     const gint no_of_bits, const guint encoding);
425 /* get 1 - 64 bits returned in a guint64 */
426 WS_DLL_PUBLIC guint64 tvb_get_bits64(tvbuff_t *tvb, guint bit_offset,
427     const gint no_of_bits, const guint encoding);
428
429 /**
430  *  This function has EXACTLY the same behavior as
431  *  tvb_get_bits32()
432  */
433 WS_DLL_PUBLIC guint32 tvb_get_bits(tvbuff_t *tvb, const guint bit_offset,
434     const gint no_of_bits, const guint encoding);
435
436 /** Returns target for convenience. Does not suffer from possible
437  * expense of tvb_get_ptr(), since this routine is smart enough
438  * to copy data in chunks if the request range actually exists in
439  * different "real" tvbuffs. This function assumes that the target
440  * memory is already allocated; it does not allocate or free the
441  * target memory. */
442 WS_DLL_PUBLIC void *tvb_memcpy(tvbuff_t *tvb, void *target, const gint offset,
443     size_t length);
444
445 /** Given an allocator scope, a tvbuff, a byte offset, a byte length:
446  *
447  *    allocate a buffer using the specified scope;
448  *
449  *    copy the data from the tvbuff specified by the offset and length
450  *    into that buffer, using tvb_memcpy();
451  *
452  *    and return a pointer to the buffer.
453  *
454  * Throws an exception if the tvbuff ends before the data being copied does.
455  *
456  * If scope is set to NULL it is the user's responsibility to wmem_free()
457  * the memory allocated. Otherwise memory is automatically freed when the
458  * scope lifetime is reached.
459  */
460 WS_DLL_PUBLIC void *tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb,
461     const gint offset, size_t length);
462
463 /** WARNING! This function is possibly expensive, temporarily allocating
464  * another copy of the packet data. Furthermore, it's dangerous because once
465  * this pointer is given to the user, there's no guarantee that the user will
466  * honor the 'length' and not overstep the boundaries of the buffer.
467  *
468  * If you're thinking of using tvb_get_ptr, STOP WHAT YOU ARE DOING
469  * IMMEDIATELY. Go take a break. Consider that tvb_get_ptr hands you
470  * a raw, unprotected pointer that you can easily use to create a
471  * security vulnerability or otherwise crash Wireshark. Then consider
472  * that you can probably find a function elsewhere in this file that
473  * does exactly what you want in a much more safe and robust manner.
474  *
475  * The returned pointer is data that is internal to the tvbuff, so do not
476  * attempt to free it. Don't modify the data, either, because another tvbuff
477  * that might be using this tvbuff may have already copied that portion of
478  * the data (sometimes tvbuff's need to make copies of data, but that's the
479  * internal implementation that you need not worry about). Assume that the
480  * guint8* points to read-only data that the tvbuff manages.
481  *
482  * Return a pointer into our buffer if the data asked for via 'offset'/'length'
483  * is contiguous (which might not be the case for a "composite" tvbuff). If the
484  * data is not contiguous, a tvb_memdup() is called for the entire buffer
485  * and the pointer to the newly-contiguous data is returned. This dynamically-
486  * allocated memory will be freed when the tvbuff is freed, after the
487  * tvbuff_free_cb_t() is called, if any. */
488 WS_DLL_PUBLIC const guint8 *tvb_get_ptr(tvbuff_t *tvb, const gint offset,
489     const gint length);
490
491 /** Find first occurrence of needle in tvbuff, starting at offset. Searches
492  * at most maxlength number of bytes; if maxlength is -1, searches to
493  * end of tvbuff.
494  * Returns the offset of the found needle, or -1 if not found.
495  * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
496  * in that case, -1 will be returned if the boundary is reached before
497  * finding needle. */
498 WS_DLL_PUBLIC gint tvb_find_guint8(tvbuff_t *tvb, const gint offset,
499     const gint maxlength, const guint8 needle);
500
501 /** Same as tvb_find_guint8() with 16bit needle. */
502 WS_DLL_PUBLIC gint tvb_find_guint16(tvbuff_t *tvb, const gint offset,
503     const gint maxlength, const guint16 needle);
504
505 /** Find first occurrence of any of the needles of the pre-compiled pattern in
506  * tvbuff, starting at offset. The passed in pattern must have been "compiled"
507  * before-hand, using ws_mempbrk_compile().
508  * Searches at most maxlength number of bytes. Returns the offset of the
509  * found needle, or -1 if not found and the found needle.
510  * Will not throw an exception, even if
511  * maxlength exceeds boundary of tvbuff; in that case, -1 will be returned if
512  * the boundary is reached before finding needle. */
513 WS_DLL_PUBLIC gint tvb_ws_mempbrk_pattern_guint8(tvbuff_t *tvb, const gint offset,
514     const gint maxlength, const ws_mempbrk_pattern* pattern, guchar *found_needle);
515
516
517 /** Find size of stringz (NUL-terminated string) by looking for terminating
518  * NUL.  The size of the string includes the terminating NUL.
519  *
520  * If the NUL isn't found, it throws the appropriate exception.
521  */
522 WS_DLL_PUBLIC guint tvb_strsize(tvbuff_t *tvb, const gint offset);
523
524 /** Find size of UCS-2 or UTF-16 stringz (NUL-terminated string) by
525  * looking for terminating 16-bit NUL.  The size of the string includes
526  * the terminating NUL.
527  *
528  * If the NUL isn't found, it throws the appropriate exception.
529  */
530 WS_DLL_PUBLIC guint tvb_unicode_strsize(tvbuff_t *tvb, const gint offset);
531
532 /** Find length of string by looking for end of zero terminated string, up to
533  * 'maxlength' characters'; if 'maxlength' is -1, searches to end
534  * of tvbuff.
535  * Returns -1 if 'maxlength' reached before finding EOS. */
536 WS_DLL_PUBLIC gint tvb_strnlen(tvbuff_t *tvb, const gint offset,
537     const guint maxlength);
538
539 /**
540  * Format the data in the tvb from offset for size.  Returned string is
541  * wmem packet_scoped so call must be in that scope.
542  */
543 WS_DLL_PUBLIC gchar *tvb_format_text(tvbuff_t *tvb, const gint offset,
544     const gint size);
545
546 /**
547  * Like "tvb_format_text()", but for 'wsp'; don't show
548  * the characters as C-style escapes.
549  */
550 WS_DLL_PUBLIC gchar *tvb_format_text_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const gint offset,
551     const gint size);
552
553 /**
554  * Like "tvb_format_text()", but for null-padded strings; don't show
555  * the null padding characters as "\000".  Returned string is wmem packet_scoped
556  * so call must be in that scope.
557  */
558 extern gchar *tvb_format_stringzpad(tvbuff_t *tvb, const gint offset,
559     const gint size);
560
561 /**
562  * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
563  * the null padding characters as "\000".
564  */
565 extern gchar *tvb_format_stringzpad_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const gint offset,
566     const gint size);
567
568 /**
569  * Given an allocator scope, a tvbuff, a byte offset, a byte length, and
570  * a string encoding, with the specified offset and length referring to
571  * a string in the specified encoding:
572  *
573  *    allocate a buffer using the specified scope;
574  *
575  *    convert the string from the specified encoding to UTF-8, possibly
576  *    mapping some characters or invalid octet sequences to the Unicode
577  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
578  *    trailing '\0', into that buffer;
579  *
580  *    and return a pointer to the buffer.
581  *
582  * Throws an exception if the tvbuff ends before the string does.
583  *
584  * If scope is set to NULL it is the user's responsibility to wmem_free()
585  * the memory allocated. Otherwise memory is automatically freed when the
586  * scope lifetime is reached.
587  */
588 WS_DLL_PUBLIC guint8 *tvb_get_string_enc(wmem_allocator_t *scope,
589     tvbuff_t *tvb, const gint offset, const gint length, const guint encoding);
590
591 /**
592  * Given an allocator scope, a tvbuff, a bit offset, and a length in
593  * 7-bit characters (not octets!), with the specified offset and
594  * length referring to a string in the 3GPP TS 23.038 7bits encoding:
595  *
596  *    allocate a buffer using the specified scope;
597  *
598  *    convert the string from the specified encoding to UTF-8, possibly
599  *    mapping some characters or invalid octet sequences to the Unicode
600  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
601  *    trailing '\0', into that buffer;
602  *
603  *    and return a pointer to the buffer.
604  *
605  * Throws an exception if the tvbuff ends before the string does.
606  *
607  * If scope is set to NULL it is the user's responsibility to wmem_free()
608  * the memory allocated. Otherwise memory is automatically freed when the
609  * scope lifetime is reached.
610  */
611 WS_DLL_PUBLIC gchar *tvb_get_ts_23_038_7bits_string(wmem_allocator_t *scope,
612     tvbuff_t *tvb, const gint bit_offset, gint no_of_chars);
613
614 /**
615  * Given an allocator scope, a tvbuff, a bit offset, and a length in
616  * 7-bit characters (not octets!), with the specified offset and
617  * length referring to a string in the ASCII 7bits encoding:
618  *
619  *    allocate a buffer using the specified scope;
620  *
621  *    convert the string from the specified encoding to UTF-8, possibly
622  *    mapping some characters or invalid octet sequences to the Unicode
623  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
624  *    trailing '\0', into that buffer;
625  *
626  *    and return a pointer to the buffer.
627  *
628  * Throws an exception if the tvbuff ends before the string does.
629  *
630  * If scope is set to NULL it is the user's responsibility to wmem_free()
631  * the memory allocated. Otherwise memory is automatically freed when the
632  * scope lifetime is reached.
633  */
634 WS_DLL_PUBLIC gchar *tvb_get_ascii_7bits_string(wmem_allocator_t *scope,
635     tvbuff_t *tvb, const gint bit_offset, gint no_of_chars);
636
637 /**
638  * Given an allocator scope, a tvbuff, a byte offset, a byte length, and
639  * a string encoding, with the specified offset and length referring to
640  * a null-padded string in the specified encoding:
641  *
642  *    allocate a buffer using the specified scope;
643  *
644  *    convert the string from the specified encoding to UTF-8, possibly
645  *    mapping some characters or invalid octet sequences to the Unicode
646  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
647  *    trailing '\0', into that buffer;
648  *
649  *    and return a pointer to the buffer.
650  *
651  * Throws an exception if the tvbuff ends before the string does.
652  *
653  * If scope is set to NULL it is the user's responsibility to wmem_free()
654  * the memory allocated. Otherwise memory is automatically freed when the
655  * scope lifetime is reached.
656  */
657 WS_DLL_PUBLIC guint8 *tvb_get_stringzpad(wmem_allocator_t *scope,
658     tvbuff_t *tvb, const gint offset, const gint length, const guint encoding);
659
660 /**
661  * Given an allocator scope, a tvbuff, a byte offset, a pointer to a
662  * gint, and a string encoding, with the specified offset referring to
663  * a null-terminated string in the specified encoding:
664  *
665  *    find the length of that string (and throw an exception if the tvbuff
666  *    ends before we find the null);
667  *
668  *    allocate a buffer using the specified scope;
669  *
670  *    convert the string from the specified encoding to UTF-8, possibly
671  *    mapping some characters or invalid octet sequences to the Unicode
672  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
673  *    trailing '\0', into that buffer;
674  *
675  *    if the pointer to the gint is non-null, set the gint to which it
676  *    points to the length of the string;
677  *
678  *    and return a pointer to the buffer.
679  *
680  * Throws an exception if the tvbuff ends before the string does.
681  *
682  * If scope is set to NULL it is the user's responsibility to wmem_free()
683  * the memory allocated. Otherwise memory is automatically freed when the
684  * scope lifetime is reached.
685  */
686 WS_DLL_PUBLIC guint8 *tvb_get_stringz_enc(wmem_allocator_t *scope,
687     tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding);
688
689 /**
690  * Given a tvbuff and an offset, with the offset assumed to refer to
691  * a null-terminated string, find the length of that string (and throw
692  * an exception if the tvbuff ends before we find the null), allocate
693  * a buffer big enough to hold the string, copy the string into it,
694  * and return a pointer to the string.  Also return the length of the
695  * string (including the terminating null) through a pointer.
696  *
697  * This returns a constant (unmodifiable) string that does not need
698  * to be freed; instead, it will automatically be freed once the next
699  * packet is dissected.
700  *
701  * It is slightly more efficient than the other routines, but does *NOT*
702  * do any translation to UTF-8 - the string consists of the raw octets
703  * of the string, in whatever encoding they happen to be in, and, if
704  * the string is not valid in that encoding, with invalid octet sequences
705  * as they are in the packet.
706  */
707 WS_DLL_PUBLIC const guint8 *tvb_get_const_stringz(tvbuff_t *tvb,
708     const gint offset, gint *lengthp);
709
710 /** Looks for a stringz (NUL-terminated string) in tvbuff and copies
711  * no more than bufsize number of bytes, including terminating NUL, to buffer.
712  * Returns length of string (not including terminating NUL), or -1 if the
713  * string was truncated in the buffer due to not having reached the terminating
714  * NUL.  In this way, it acts like g_snprintf().
715  *
716  * When processing a packet where the remaining number of bytes is less
717  * than bufsize, an exception is not thrown if the end of the packet
718  * is reached before the NUL is found. If no NUL is found before reaching
719  * the end of the short packet, -1 is still returned, and the string
720  * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
721  * at the correct spot, terminating the string.
722  */
723 WS_DLL_PUBLIC gint tvb_get_nstringz(tvbuff_t *tvb, const gint offset,
724     const guint bufsize, guint8 *buffer);
725
726 /** Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
727  * have a terminating NUL. If the string was truncated when copied into buffer,
728  * a NUL is placed at the end of buffer to terminate it.
729  *
730  * bufsize MUST be greater than 0.
731  */
732 WS_DLL_PUBLIC gint tvb_get_nstringz0(tvbuff_t *tvb, const gint offset,
733     const guint bufsize, guint8 *buffer);
734
735 /**
736  * Given a tvbuff, an offset into the tvbuff, and a length that starts
737  * at that offset (which may be -1 for "all the way to the end of the
738  * tvbuff"), find the end of the (putative) line that starts at the
739  * specified offset in the tvbuff, going no further than the specified
740  * length.
741  *
742  * Return the length of the line (not counting the line terminator at
743  * the end), or, if we don't find a line terminator:
744  *
745  *  if "deseg" is true, return -1;
746  *
747  *  if "deseg" is false, return the amount of data remaining in
748  *  the buffer.
749  *
750  * Set "*next_offset" to the offset of the character past the line
751  * terminator, or past the end of the buffer if we don't find a line
752  * terminator.  (It's not set if we return -1.)
753  */
754 WS_DLL_PUBLIC gint tvb_find_line_end(tvbuff_t *tvb, const gint offset, int len,
755     gint *next_offset, const gboolean desegment);
756
757 /**
758  * Given a tvbuff, an offset into the tvbuff, and a length that starts
759  * at that offset (which may be -1 for "all the way to the end of the
760  * tvbuff"), find the end of the (putative) line that starts at the
761  * specified offset in the tvbuff, going no further than the specified
762  * length.
763  *
764  * However, treat quoted strings inside the buffer specially - don't
765  * treat newlines in quoted strings as line terminators.
766  *
767  * Return the length of the line (not counting the line terminator at
768  * the end), or the amount of data remaining in the buffer if we don't
769  * find a line terminator.
770  *
771  * Set "*next_offset" to the offset of the character past the line
772  * terminator, or past the end of the buffer if we don't find a line
773  * terminator.
774  */
775 WS_DLL_PUBLIC gint tvb_find_line_end_unquoted(tvbuff_t *tvb, const gint offset,
776     int len, gint *next_offset);
777
778 /**
779  * Copied from the mgcp dissector. (This function should be moved to /epan )
780  * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
781  *                character following offset or offset + maxlength -1 whichever
782  *                is smaller.
783  *
784  * Parameters:
785  * tvb - The tvbuff in which we are skipping whitespace.
786  * offset - The offset in tvb from which we begin trying to skip whitespace.
787  * maxlength - The maximum distance from offset that we may try to skip
788  * whitespace.
789  *
790  * Returns: The position in tvb of the first non-whitespace
791  *          character following offset or offset + maxlength -1 whichever
792  *          is smaller.
793  */
794
795 WS_DLL_PUBLIC gint tvb_skip_wsp(tvbuff_t *tvb, const gint offset,
796     const gint maxlength);
797
798 WS_DLL_PUBLIC gint tvb_skip_wsp_return(tvbuff_t *tvb, const gint offset);
799
800 int tvb_skip_guint8(tvbuff_t *tvb, int offset, const int maxlength, const guint8 ch);
801
802 /**
803  * Call strncmp after checking if enough chars left, returning 0 if
804  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
805  */
806 WS_DLL_PUBLIC gint tvb_strneql(tvbuff_t *tvb, const gint offset,
807     const gchar *str, const size_t size);
808
809 /**
810  * Call g_ascii_strncasecmp after checking if enough chars left, returning
811  * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
812  */
813 WS_DLL_PUBLIC gint tvb_strncaseeql(tvbuff_t *tvb, const gint offset,
814     const gchar *str, const size_t size);
815
816 /**
817  * Call memcmp after checking if enough chars left, returning 0 if
818  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
819  */
820 WS_DLL_PUBLIC gint tvb_memeql(tvbuff_t *tvb, const gint offset,
821     const guint8 *str, size_t size);
822
823 /**
824  * Format a bunch of data from a tvbuff as bytes, returning a pointer
825  * to the string with the formatted data, with "punct" as a byte
826  * separator.
827  */
828 WS_DLL_PUBLIC gchar *tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset,
829     const gint len, const gchar punct);
830
831 /**
832  * Format a bunch of data from a tvbuff as bytes, returning a pointer
833  * to the string with the formatted data.
834  */
835 WS_DLL_PUBLIC gchar *tvb_bytes_to_str(wmem_allocator_t *allocator, tvbuff_t *tvb,
836     const gint offset, const gint len);
837
838 /**
839  * Given a tvbuff, an offset into the tvbuff, and a length that starts
840  * at that offset (which may be -1 for "all the way to the end of the
841  * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
842  * the low or high half byte, formatting the digits according to an input digit
843  * set, if NUL a default digit set of 0-9 returning "?" for overdecadic digits
844  * will be used.  A pointer to the packet-scope (WMEM-allocated) string will
845  * be returned. Note a tvbuff content of 0xf is considered a 'filler' and will
846  * end the conversion.
847  */
848 typedef struct dgt_set_t
849 {
850     const unsigned char out[16];
851 }
852 dgt_set_t;
853
854 WS_DLL_PUBLIC const gchar *tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb,
855     const gint offset, const gint len, const dgt_set_t *dgt,
856     gboolean skip_first);
857
858 /** Locate a sub-tvbuff within another tvbuff, starting at position
859  * 'haystack_offset'. Returns the index of the beginning of 'needle' within
860  * 'haystack', or -1 if 'needle' is not found. The index is relative
861  * to the start of 'haystack', not 'haystack_offset'. */
862 WS_DLL_PUBLIC gint tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb,
863     const gint haystack_offset);
864
865 /* From tvbuff_zlib.c */
866
867 /**
868  * Uncompresses a zlib compressed packet inside a tvbuff at offset with
869  * length comprlen.  Returns an uncompressed tvbuffer if uncompression
870  * succeeded or NULL if uncompression failed.
871  */
872 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress(tvbuff_t *tvb, const int offset,
873     int comprlen);
874
875 /**
876  * Uncompresses a zlib compressed packet inside a tvbuff at offset with
877  * length comprlen.  Returns an uncompressed tvbuffer attached to tvb if
878  * uncompression succeeded or NULL if uncompression failed.
879  */
880 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress(tvbuff_t *parent, tvbuff_t *tvb,
881     const int offset, int comprlen);
882
883 /* From tvbuff_base64.c */
884
885 /** Return a tvb that contains the binary representation of a base64
886  *  string
887  */
888 extern tvbuff_t* base64_to_tvb(tvbuff_t *parent, const char *base64);
889
890 /**
891  * Extract a variable length integer from a tvbuff.
892  * Each byte in a varint, except the last byte, has the most significant bit (msb)
893  * set -- this indicates that there are further bytes to come. For example,
894  *   1010 1100 0000 0010 is 300
895  *
896  * @param tvb The tvbuff in which we are extracting integer.
897  * @param offset The offset in tvb from which we begin trying to extract integer.
898  * @param maxlen The maximum distance from offset that we may try to extract integer
899  * @param value  if parsing succeeds, parsed varint will store here.
900  * @param encoding The ENC_* that defines the format (e.g., ENC_VARINT_PROTOBUF, ENC_VARINT_QUIC)
901  * @return   the length of this varint in tvb. 0 means parsing failed.
902  */
903 WS_DLL_PUBLIC guint tvb_get_varint(tvbuff_t *tvb, guint offset, guint maxlen, guint64 *value, const guint encoding);
904
905 /************** END OF ACCESSORS ****************/
906
907 /** @} */
908
909 #ifdef __cplusplus
910 }
911 #endif /* __cplusplus */
912
913 #endif /* __TVBUFF_H__ */
914
915 /*
916  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
917  *
918  * Local variables:
919  * c-basic-offset: 4
920  * tab-width: 8
921  * indent-tabs-mode: nil
922  * End:
923  *
924  * vi: set shiftwidth=4 tabstop=8 expandtab:
925  * :indentSize=4:tabSize=8:noTabs=true:
926  */