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