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