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