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