initialize tree variables
[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  * $Id$
13  *
14  * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
15  *
16  * Wireshark - Network traffic analyzer
17  * By Gerald Combs <gerald@wireshark.org>
18  * Copyright 1998 Gerald Combs
19  *
20  * This program is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU General Public License
22  * as published by the Free Software Foundation; either version 2
23  * of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33  */
34
35 #ifndef __TVBUFF_H__
36 #define __TVBUFF_H__
37
38 #include <glib.h>
39 #include <epan/ipv6-utils.h>
40 #include <epan/guid-utils.h>
41 #include <epan/wmem/wmem.h>
42 #include "exceptions.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif /* __cplusplus */
47
48 /** @file
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(),
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 sequentually 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 tvbuff's.
112  *
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, guint32 bit_offset, gint32 no_of_bits);
122
123 WS_DLL_PUBLIC tvbuff_t *tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing);
124
125 WS_DLL_PUBLIC tvbuff_t *tvb_clone(tvbuff_t *tvb);
126
127 WS_DLL_PUBLIC tvbuff_t *tvb_clone_offset_len(tvbuff_t *tvb, guint offset, guint len);
128
129 /** Free a tvbuff_t and all tvbuffs chained from it
130  * The tvbuff must be 'the 'head' (initial) tvb of a chain or
131  * must not be in a chain.
132  * If specified, a callback to free the tvbuff data will be invoked
133  * for each tvbuff free'd */
134 WS_DLL_PUBLIC void tvb_free(tvbuff_t*);
135
136 /** Free the tvbuff_t and all tvbuffs chained from it.
137  * The tvbuff must be 'the 'head' (initial) tvb of a chain or
138  * must not be in a chain.
139  * If specified, a callback to free the tvbuff data will be invoked
140  * for each tvbuff free'd */
141 WS_DLL_PUBLIC void tvb_free_chain(tvbuff_t*);
142
143 /** Set a callback function to call when a tvbuff is actually freed
144  * One argument is passed to that callback --- a void* that points
145  * to the real data. Obviously, this only applies to a
146  * TVBUFF_REAL_DATA tvbuff. */
147 WS_DLL_PUBLIC void tvb_set_free_cb(tvbuff_t*, const tvbuff_free_cb_t);
148
149 /** Attach a TVBUFF_REAL_DATA tvbuff to a parent tvbuff. This connection
150  * is used during a tvb_free_chain()... the "child" TVBUFF_REAL_DATA acts
151  * as if is part of the chain-of-creation of the parent tvbuff, although it
152  * isn't. This is useful if you need to take the data from some tvbuff,
153  * run some operation on it, like decryption or decompression, and make a new
154  * tvbuff from it, yet want the new tvbuff to be part of the chain. The reality
155  * is that the new tvbuff *is* part of the "chain of creation", but in a way
156  * that these tvbuff routines are ignorant of. Use this function to make
157  * the tvbuff routines knowledgable of this fact. */
158 WS_DLL_PUBLIC void tvb_set_child_real_data_tvbuff(tvbuff_t* parent, tvbuff_t* child);
159
160 WS_DLL_PUBLIC tvbuff_t* tvb_new_child_real_data(tvbuff_t* parent, const guint8* data, const guint length,
161     const gint reported_length);
162
163 /** Create a tvbuff backed by existing data. Can throw ReportedBoundsError.
164  * Normally, a callback to free the data should be registered using tvb_set_free_cb();
165  * when this tvbuff is freed, then your callback will be called, and at that time
166  * you can free your original data. */
167 WS_DLL_PUBLIC tvbuff_t* tvb_new_real_data(const guint8* data, const guint length,
168     const gint reported_length);
169
170 /** Create a tvbuff that's a subset of another tvbuff.
171  *
172  * 'backing_offset', if positive, is the offset from the beginning of
173  * the backing tvbuff at which the new tvbuff's data begins, and, if
174  * negative, is the offset from the end of the backing tvbuff at which
175  * the new tvbuff's data begins.
176  *
177  * 'backing_length' is 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.
181  *
182  * Will throw BoundsError if 'backing_offset'/'length'
183  * is beyond the bounds of the backing tvbuff.
184  * Can throw ReportedBoundsError. */
185 WS_DLL_PUBLIC tvbuff_t* tvb_new_subset(tvbuff_t* backing,
186                 const gint backing_offset, const gint backing_length, const gint reported_length);
187
188 /**
189  * Similar to tvb_new_subset() but with captured length calculated
190  * to fit within the existing captured length and the specified
191  * backing length (which is used as the reported length).
192  * Can throw ReportedBoundsError. */
193 WS_DLL_PUBLIC tvbuff_t* tvb_new_subset_length(tvbuff_t *backing,
194                 const gint backing_offset, const gint backing_length);
195
196 /** Similar to tvb_new_subset() but with backing_length and reported_length set to -1.
197  * Can throw ReportedBoundsError. */
198 WS_DLL_PUBLIC tvbuff_t* tvb_new_subset_remaining(tvbuff_t* backing,
199                 const gint backing_offset);
200
201 /*
202 * Both tvb_composite_append and tvb_composite_prepend can throw
203  * BoundsError if member_offset/member_length goes beyond bounds of
204  * the 'member' tvbuff. */
205
206 /** Append to the list of tvbuffs that make up this composite tvbuff */
207 WS_DLL_PUBLIC void tvb_composite_append(tvbuff_t* tvb, tvbuff_t* member);
208
209 /** Prepend to the list of tvbuffs that make up this composite tvbuff */
210 extern void tvb_composite_prepend(tvbuff_t* tvb, tvbuff_t* member);
211
212 /** Create an empty composite tvbuff. */
213 WS_DLL_PUBLIC tvbuff_t* tvb_new_composite(void);
214
215 /** Mark a composite tvbuff as initialized. No further appends or prepends
216  * occur, data access can finally happen after this finalization. */
217 WS_DLL_PUBLIC void tvb_composite_finalize(tvbuff_t* tvb);
218
219
220 /* Get total length of buffer */
221 WS_DLL_PUBLIC guint tvb_length(const tvbuff_t*);
222
223 /** Computes bytes to end of buffer, from offset (which can be negative,
224  * to indicate bytes from end of buffer). Function returns 0 if offset is out
225  * of bounds. No exception is thrown. */
226 WS_DLL_PUBLIC gint tvb_length_remaining(const tvbuff_t*, const gint offset);
227
228 /** Same as above, but throws an exception if the offset is out of bounds. */
229 WS_DLL_PUBLIC guint tvb_ensure_length_remaining(const tvbuff_t*, const gint offset);
230
231 /* Checks (w/o throwing exception) that the bytes referred to by
232  * 'offset'/'length' actually exist in the buffer */
233 WS_DLL_PUBLIC gboolean tvb_bytes_exist(const tvbuff_t*, const gint offset, const gint length);
234
235 /** Checks that the bytes referred to by 'offset'/'length' actually exist
236  * in the buffer, and throws an exception if they aren't. */
237 WS_DLL_PUBLIC void tvb_ensure_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length);
238
239 /* Checks (w/o throwing exception) that offset exists in buffer */
240 WS_DLL_PUBLIC gboolean tvb_offset_exists(const tvbuff_t*, const gint offset);
241
242 /* Get reported length of buffer */
243 WS_DLL_PUBLIC guint tvb_reported_length(const tvbuff_t*);
244
245 /** Computes bytes of reported packet data to end of buffer, from offset
246  * (which can be negative, to indicate bytes from end of buffer). Function
247  * returns 0 if offset is out of bounds. No exception is thrown. */
248 WS_DLL_PUBLIC gint tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset);
249
250 /** Set the reported length of a tvbuff to a given value; used for protocols
251    whose headers contain an explicit length and where the calling
252    dissector's payload may include padding as well as the packet for
253    this protocol.
254
255    Also adjusts the data length. */
256 WS_DLL_PUBLIC void tvb_set_reported_length(tvbuff_t*, const guint);
257
258 WS_DLL_PUBLIC guint tvb_offset_from_real_beginning(const tvbuff_t *tvb);
259
260 /* Returns the offset from the first byte of real data. */
261 WS_DLL_PUBLIC gint tvb_raw_offset(tvbuff_t *tvb);
262
263 /** Set the "this is a fragment" flag. */
264 WS_DLL_PUBLIC void tvb_set_fragment(tvbuff_t *tvb);
265
266 WS_DLL_PUBLIC struct tvbuff *tvb_get_ds_tvb(tvbuff_t *tvb);
267
268
269 /************** START OF ACCESSORS ****************/
270 /* All accessors will throw an exception if appropriate */
271
272 WS_DLL_PUBLIC guint8  tvb_get_guint8(tvbuff_t*, const gint offset);
273
274 WS_DLL_PUBLIC guint16 tvb_get_ntohs(tvbuff_t*, const gint offset);
275 WS_DLL_PUBLIC guint32 tvb_get_ntoh24(tvbuff_t*, const gint offset);
276 WS_DLL_PUBLIC guint32 tvb_get_ntohl(tvbuff_t*, const gint offset);
277 WS_DLL_PUBLIC guint64 tvb_get_ntoh40(tvbuff_t*, const gint offset);
278 WS_DLL_PUBLIC guint64 tvb_get_ntoh48(tvbuff_t*, const gint offset);
279 WS_DLL_PUBLIC guint64 tvb_get_ntoh56(tvbuff_t*, const gint offset);
280 WS_DLL_PUBLIC guint64 tvb_get_ntoh64(tvbuff_t*, const gint offset);
281 WS_DLL_PUBLIC gfloat tvb_get_ntohieee_float(tvbuff_t*, const gint offset);
282 WS_DLL_PUBLIC gdouble tvb_get_ntohieee_double(tvbuff_t*, const gint offset);
283
284 WS_DLL_PUBLIC guint16 tvb_get_letohs(tvbuff_t*, const gint offset);
285 WS_DLL_PUBLIC guint32 tvb_get_letoh24(tvbuff_t*, const gint offset);
286 WS_DLL_PUBLIC guint32 tvb_get_letohl(tvbuff_t*, const gint offset);
287 WS_DLL_PUBLIC guint64 tvb_get_letoh40(tvbuff_t*, const gint offset);
288 WS_DLL_PUBLIC guint64 tvb_get_letoh48(tvbuff_t*, const gint offset);
289 WS_DLL_PUBLIC guint64 tvb_get_letoh56(tvbuff_t*, const gint offset);
290 WS_DLL_PUBLIC guint64 tvb_get_letoh64(tvbuff_t*, const gint offset);
291 WS_DLL_PUBLIC gfloat tvb_get_letohieee_float(tvbuff_t*, const gint offset);
292 WS_DLL_PUBLIC gdouble tvb_get_letohieee_double(tvbuff_t*, const gint offset);
293
294 /**
295  * Fetch an IPv4 address, in network byte order.
296  * We do *not* convert it to host byte order; we leave it in
297  * network byte order, as that's what its callers expect. */
298 WS_DLL_PUBLIC guint32 tvb_get_ipv4(tvbuff_t*, const gint offset);
299
300 /* Fetch an IPv6 address. */
301 WS_DLL_PUBLIC void tvb_get_ipv6(tvbuff_t*, const gint offset, struct e_in6_addr *addr);
302
303 /* Fetch a GUID. */
304 WS_DLL_PUBLIC void tvb_get_ntohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid);
305 WS_DLL_PUBLIC void tvb_get_letohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid);
306 WS_DLL_PUBLIC void tvb_get_guid(tvbuff_t *tvb, const gint offset, e_guid_t *guid, const guint representation);
307
308 /* Fetch a specified number of bits from bit offset in a tvb.
309    All of these functions are equivalent, except for the type of the retun value.
310    Note that the parameter encoding (where supplied) is meaningless and ignored */
311
312 /* get 1 - 8 bits returned in a guint8 */
313 WS_DLL_PUBLIC guint8 tvb_get_bits8(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits);
314 /* get 1 - 16 bits returned in a guint16 */
315 WS_DLL_PUBLIC guint16 tvb_get_bits16(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
316 /* get 1 - 32 bits returned in a guint32 */
317 WS_DLL_PUBLIC guint32 tvb_get_bits32(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
318 /* get 1 - 64 bits returned in a guint64 */
319 WS_DLL_PUBLIC guint64 tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding);
320
321 /**
322  *  This function has EXACTLY the same behaviour as
323  *  tvb_get_bits32()
324  */
325 WS_DLL_PUBLIC guint32 tvb_get_bits(tvbuff_t *tvb, const guint bit_offset, const gint no_of_bits, const guint encoding);
326
327 WS_DLL_PUBLIC
328 void tvb_get_bits_buf(tvbuff_t *tvb, guint bit_offset, gint no_of_bits, guint8 *buf, gboolean lsb0);
329 WS_DLL_PUBLIC
330 guint8 *wmem_packet_tvb_get_bits(tvbuff_t *tvb, guint bit_offset, gint no_of_bits, gboolean lsb0);
331
332 /** Returns target for convenience. Does not suffer from possible
333  * expense of tvb_get_ptr(), since this routine is smart enough
334  * to copy data in chunks if the request range actually exists in
335  * different TVBUFF_REAL_DATA tvbuffs. This function assumes that the
336  * target memory is already allocated; it does not allocate or free the
337  * target memory. */
338 WS_DLL_PUBLIC void* tvb_memcpy(tvbuff_t*, void* target, const gint offset, size_t length);
339
340 /** If scope is set to NULL it is the user's responsibility to g_free()
341  * the memory allocated by tvb_memdup(). Otherwise memory is
342  * automatically freed when the scope lifetime is reached.
343  * Calls tvb_memcpy() */
344 WS_DLL_PUBLIC void* tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, size_t length);
345
346 /** WARNING! This function is possibly expensive, temporarily allocating
347  * another copy of the packet data. Furthermore, it's dangerous because once
348  * this pointer is given to the user, there's no guarantee that the user will
349  * honor the 'length' and not overstep the boundaries of the buffer.
350  *
351  * If you're thinking of using tvb_get_ptr, STOP WHAT YOU ARE DOING
352  * IMMEDIATELY. Go take a break. Consider that tvb_get_ptr hands you
353  * a raw, unprotected pointer that you can easily use to create a
354  * security vulnerability or otherwise crash Wireshark. Then consider
355  * that you can probably find a function elsewhere in this file that
356  * does exactly what you want in a much more safe and robust manner.
357  *
358  * The returned pointer is data that is internal to the tvbuff, so do not
359  * attempt to free it. Don't modify the data, either, because another tvbuff
360  * that might be using this tvbuff may have already copied that portion of
361  * the data (sometimes tvbuff's need to make copies of data, but that's the
362  * internal implementation that you need not worry about). Assume that the
363  * guint8* points to read-only data that the tvbuff manages.
364  *
365  * Return a pointer into our buffer if the data asked for via 'offset'/'length'
366  * is contiguous (which might not be the case for TVBUFF_COMPOSITE). If the
367  * data is not contiguous, a tvb_memdup() is called for the entire buffer
368  * and the pointer to the newly-contiguous data is returned. This dynamically-
369  * allocated memory will be freed when the tvbuff is freed, after the
370  * tvbuff_free_cb_t() is called, if any. */
371 WS_DLL_PUBLIC const guint8* tvb_get_ptr(tvbuff_t*, const gint offset, const gint length);
372
373 /** Find first occurrence of needle in tvbuff, starting at offset. Searches
374  * at most maxlength number of bytes; if maxlength is -1, searches to
375  * end of tvbuff.
376  * Returns the offset of the found needle, or -1 if not found.
377  * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
378  * in that case, -1 will be returned if the boundary is reached before
379  * finding needle. */
380 WS_DLL_PUBLIC gint tvb_find_guint8(tvbuff_t*, const gint offset, const gint maxlength,
381     const guint8 needle);
382
383 /** Find first occurrence of any of the needles in tvbuff, starting at offset.
384  * Searches at most maxlength number of bytes. Returns the offset of the
385  * found needle, or -1 if not found and the found needle.
386  * Will not throw an exception, even if
387  * maxlength exceeds boundary of tvbuff; in that case, -1 will be returned if
388  * the boundary is reached before finding needle. */
389 WS_DLL_PUBLIC gint tvb_pbrk_guint8(tvbuff_t *, const gint offset, const gint maxlength,
390     const guint8 *needles, guchar *found_needle);
391
392 /** Find size of stringz (NUL-terminated string) by looking for terminating
393  * NUL.  The size of the string includes the terminating NUL.
394  *
395  * If the NUL isn't found, it throws the appropriate exception.
396  */
397 WS_DLL_PUBLIC guint tvb_strsize(tvbuff_t *tvb, const gint offset);
398
399 /** Find size of UCS-2 or UTF-16 stringz (NUL-terminated string) by
400  * looking for terminating 16-bit NUL.  The size of the string includes
401  * the terminating NUL.
402  *
403  * If the NUL isn't found, it throws the appropriate exception.
404  */
405 WS_DLL_PUBLIC guint tvb_unicode_strsize(tvbuff_t *tvb, const gint offset);
406
407 /** Find length of string by looking for end of zero terminated string, up to
408  * 'maxlength' characters'; if 'maxlength' is -1, searches to end
409  * of tvbuff.
410  * Returns -1 if 'maxlength' reached before finding EOS. */
411 WS_DLL_PUBLIC gint tvb_strnlen(tvbuff_t*, const gint offset, const guint maxlength);
412
413 /** Convert a string from Unicode to ASCII.  At the moment we fake it by
414  * assuming all characters are ASCII  )-:  The len parameter is the number
415  * of guint16's to convert from Unicode.
416  *
417  * XXX - These functions have been superceded by tvb_get_unicode_string()
418  *
419  * If scope is set to NULL, returned buffer is allocated by g_malloc()
420  * and must be g_free by the caller. Otherwise memory is automatically
421  * freed when the scope lifetime is reached.
422  */
423 WS_DLL_PUBLIC char *tvb_get_faked_unicode(wmem_allocator_t *scope, tvbuff_t *tvb,
424                               int offset, const int len, const gboolean little_endian);
425
426 /**
427  * Format the data in the tvb from offset for size ...
428  */
429 WS_DLL_PUBLIC gchar * tvb_format_text(tvbuff_t *tvb, const gint offset, const gint size);
430
431 /**
432  * Like "tvb_format_text()", but for 'wsp'; don't show
433  * the characters as C-style escapes.
434  */
435 WS_DLL_PUBLIC gchar * tvb_format_text_wsp(tvbuff_t *tvb, const gint offset, const gint size);
436
437 /**
438  * Like "tvb_format_text()", but for null-padded strings; don't show
439  * the null padding characters as "\000".
440  */
441 extern gchar *tvb_format_stringzpad(tvbuff_t *tvb, const gint offset, const gint size);
442
443 /**
444  * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
445  * the null padding characters as "\000".
446  */
447 extern gchar *tvb_format_stringzpad_wsp(tvbuff_t *tvb, const gint offset, const gint size);
448
449
450 /**
451  * Given a tvbuff, an offset, and a length, allocate a buffer big enough
452  * to hold a non-null-terminated string of that length at that offset,
453  * plus a trailing zero, copy the string into it, and return a pointer
454  * to the string.
455  *
456  * Throws an exception if the tvbuff ends before the string does.
457  *
458  * tvb_get_string() returns a string allocated.
459  *
460  * tvb_get_unicode_string() Unicode (UTF-16) version of above.
461  *
462  * tvb_get_string_enc() takes a string encoding as well, and converts to UTF-8
463  *                   from the encoding (only UTF-8 and EBCDIC supported).
464  *
465  * If scope is set to NULL it is the user's responsibility to g_free()
466  * the memory allocated by tvb_memdup(). Otherwise memory is
467  * automatically freed when the scope lifetime is reached.
468  */
469 WS_DLL_PUBLIC guint8 *tvb_get_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length);
470 WS_DLL_PUBLIC gchar  *tvb_get_unicode_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint length,
471     const guint encoding);
472 WS_DLL_PUBLIC guint8 *tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset,
473     const gint length, const guint encoding);
474
475
476 /**
477  * Given a tvbuff and an offset, with the offset assumed to refer to
478  * a null-terminated string, find the length of that string (and throw
479  * an exception if the tvbuff ends before we find the null), allocate
480  * a buffer big enough to hold the string, copy the string into it,
481  * and return a pointer to the string.  Also return the length of the
482  * string (including the terminating null) through a pointer.
483  *
484  * tvb_get_stringz() returns a string
485  *
486  * tvb_get_stringz_enc() takes a string encoding as well, and converts to
487  *                   UTF-8 from the encoding (only UTF-8 and EBCDIC supported)
488  *
489  * tvb_get_const_stringz() returns a constant (unmodifiable) string that does
490  *                   not need to be freed, instead it will automatically be
491  *                   freed once the next packet is dissected.  It is slightly
492  *                   more efficient than the other routines.
493  *
494  * tvb_get_unicode_stringz() Unicode (UTF-16) version of above
495  *
496  * If scope is set to NULL it is the user's responsibility to g_free()
497  * the memory allocated by tvb_memdup(). Otherwise memory is
498  * automatically freed when the scope lifetime is reached.
499  */
500 WS_DLL_PUBLIC guint8 *tvb_get_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp);
501 WS_DLL_PUBLIC guint8 *tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp,
502     const guint encoding);
503 WS_DLL_PUBLIC const guint8 *tvb_get_const_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp);
504 WS_DLL_PUBLIC gchar  *tvb_get_unicode_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset,
505     gint *lengthp, const guint encoding);
506
507 /** Looks for a stringz (NUL-terminated string) in tvbuff and copies
508  * no more than bufsize number of bytes, including terminating NUL, to buffer.
509  * Returns length of string (not including terminating NUL), or -1 if the string was
510  * truncated in the buffer due to not having reached the terminating NUL.
511  * In this way, it acts like g_snprintf().
512  *
513  * When processing a packet where the remaining number of bytes is less
514  * than bufsize, an exception is not thrown if the end of the packet
515  * is reached before the NUL is found. If no NUL is found before reaching
516  * the end of the short packet, -1 is still returned, and the string
517  * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
518  * at the correct spot, terminating the string.
519  */
520 WS_DLL_PUBLIC gint tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize,
521     guint8* buffer);
522
523 /** Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
524  * have a terminating NUL. If the string was truncated when copied into buffer,
525  * a NUL is placed at the end of buffer to terminate it.
526  *
527  * bufsize MUST be greater than 0.
528  */
529 WS_DLL_PUBLIC gint tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize,
530     guint8* buffer);
531
532 /**
533  * Given a tvbuff, an offset into the tvbuff, and a length that starts
534  * at that offset (which may be -1 for "all the way to the end of the
535  * tvbuff"), find the end of the (putative) line that starts at the
536  * specified offset in the tvbuff, going no further than the specified
537  * length.
538  *
539  * Return the length of the line (not counting the line terminator at
540  * the end), or, if we don't find a line terminator:
541  *
542  *      if "deseg" is true, return -1;
543  *
544  *      if "deseg" is false, return the amount of data remaining in
545  *      the buffer.
546  *
547  * Set "*next_offset" to the offset of the character past the line
548  * terminator, or past the end of the buffer if we don't find a line
549  * terminator.  (It's not set if we return -1.)
550  */
551 WS_DLL_PUBLIC gint tvb_find_line_end(tvbuff_t *tvb, const gint offset, int len,
552     gint *next_offset, const gboolean desegment);
553
554 /**
555  * Given a tvbuff, an offset into the tvbuff, and a length that starts
556  * at that offset (which may be -1 for "all the way to the end of the
557  * tvbuff"), find the end of the (putative) line that starts at the
558  * specified offset in the tvbuff, going no further than the specified
559  * length.
560  *
561  * However, treat quoted strings inside the buffer specially - don't
562  * treat newlines in quoted strings as line terminators.
563  *
564  * Return the length of the line (not counting the line terminator at
565  * the end), or the amount of data remaining in the buffer if we don't
566  * find a line terminator.
567  *
568  * Set "*next_offset" to the offset of the character past the line
569  * terminator, or past the end of the buffer if we don't find a line
570  * terminator.
571  */
572 WS_DLL_PUBLIC gint tvb_find_line_end_unquoted(tvbuff_t *tvb, const gint offset, int len,
573     gint *next_offset);
574
575 /**
576  * Copied from the mgcp dissector. (This function should be moved to /epan )
577  * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
578  *                character following offset or offset + maxlength -1 whichever
579  *                is smaller.
580  *
581  * Parameters:
582  * tvb - The tvbuff in which we are skipping whitespace.
583  * offset - The offset in tvb from which we begin trying to skip whitespace.
584  * maxlength - The maximum distance from offset that we may try to skip
585  * whitespace.
586  *
587  * Returns: The position in tvb of the first non-whitespace
588  *          character following offset or offset + maxlength -1 whichever
589  *          is smaller.
590  */
591
592 WS_DLL_PUBLIC gint tvb_skip_wsp(tvbuff_t* tvb, const gint offset, const gint maxlength);
593
594 WS_DLL_PUBLIC gint tvb_skip_wsp_return(tvbuff_t* tvb, const gint offset);
595
596 /**
597  * Call strncmp after checking if enough chars left, returning 0 if
598  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
599  */
600 WS_DLL_PUBLIC gint tvb_strneql(tvbuff_t *tvb, const gint offset, const gchar *str,
601     const size_t size);
602
603 /**
604  * Call g_ascii_strncasecmp after checking if enough chars left, returning
605  * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
606  */
607 WS_DLL_PUBLIC gint tvb_strncaseeql(tvbuff_t *tvb, const gint offset, const gchar *str,
608     const size_t size);
609
610 /**
611  * Call memcmp after checking if enough chars left, returning 0 if
612  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
613  */
614 WS_DLL_PUBLIC gint tvb_memeql(tvbuff_t *tvb, const gint offset, const guint8 *str,
615     size_t size);
616
617 /**
618  * Format a bunch of data from a tvbuff as bytes, returning a pointer
619  * to the string with the formatted data, with "punct" as a byte
620  * separator.
621  */
622 WS_DLL_PUBLIC gchar *tvb_bytes_to_str_punct(tvbuff_t *tvb, const gint offset, const gint len,
623     const gchar punct);
624
625 /**
626  * Format a bunch of data from a tvbuff as bytes, returning a pointer
627  * to the string with the formatted data.
628  */
629 WS_DLL_PUBLIC gchar *tvb_bytes_to_str(tvbuff_t *tvb, const gint offset, const gint len);
630
631 /**
632  * Given a tvbuff, an offset into the tvbuff, and a length that starts
633  * at that offset (which may be -1 for "all the way to the end of the
634  * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
635  * the low or high half byte, formating the digits according to an input digit set,
636  * if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
637  * A pointer to the EP allocated string will be returned.
638  * Note a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
639  */
640 typedef struct dgt_set_t
641 {
642         const unsigned char out[15];
643 }
644 dgt_set_t;
645
646 WS_DLL_PUBLIC const gchar *tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first);
647
648 /** Locate a sub-tvbuff within another tvbuff, starting at position
649  * 'haystack_offset'. Returns the index of the beginning of 'needle' within
650  * 'haystack', or -1 if 'needle' is not found. The index is relative
651  * to the start of 'haystack', not 'haystack_offset'. */
652 WS_DLL_PUBLIC gint tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb,
653         const gint haystack_offset);
654
655 /**
656  * Uncompresses a zlib compressed packet inside a tvbuff at offset with
657  * length comprlen.  Returns an uncompressed tvbuffer if uncompression
658  * succeeded or NULL if uncompression failed.
659  */
660 WS_DLL_PUBLIC tvbuff_t* tvb_uncompress(tvbuff_t *tvb, const int offset,  int comprlen);
661
662 /**
663  * Uncompresses a zlib compressed packet inside a tvbuff at offset with
664  * length comprlen.  Returns an uncompressed tvbuffer attached to tvb if uncompression
665  * succeeded or NULL if uncompression failed.
666  */
667 extern tvbuff_t* tvb_child_uncompress(tvbuff_t *parent, tvbuff_t *tvb, const int offset, int comprlen);
668
669 /************** END OF ACCESSORS ****************/
670
671 /** @} */
672
673 #ifdef __cplusplus
674 }
675 #endif /* __cplusplus */
676
677 #endif /* __TVBUFF_H__ */