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