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