tvb: implement endianness-paramterized getters
[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  * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
13  *
14  * Wireshark - Network traffic analyzer
15  * By Gerald Combs <gerald@wireshark.org>
16  * Copyright 1998 Gerald Combs
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31  */
32
33 #ifndef __TVBUFF_H__
34 #define __TVBUFF_H__
35
36 #include <glib.h>
37 #include <epan/guid-utils.h>
38 #include <epan/wmem/wmem.h>
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif /* __cplusplus */
43
44 /**
45  * "testy, virtual(-izable) buffer".  They are testy in that they get mad when
46  * an attempt is made to access data beyond the bounds of their array. In that
47  * case, they throw an exception.
48  *
49  * They are virtualizable in that new tvbuff's can be made from other tvbuffs,
50  * while only the original tvbuff may have data. That is, the new tvbuff has
51  * virtual data.
52  */
53
54 struct tvbuff;
55 typedef struct tvbuff tvbuff_t;
56
57 struct e_in6_addr; /* ipv6-utils.h */
58 struct nstime_t;   /* nstime.h */
59
60 /** @defgroup tvbuff Testy, Virtual(-izable) Buffers
61  *
62  * Dissector use and management
63  *
64  *  Consider a collection of tvbs as being a chain or stack of tvbs.
65  *
66  *  When dissecting a frame:
67  *   The top-level dissector (packet.c) pushes the initial tvb (containing
68  *   the complete frame) onto the stack (starts the chain) and then calls
69  *   a sub-dissector which in turn calls the next sub-dissector and so on.
70  *   Each sub-dissector may chain additional tvbs (see below) to the tvb
71  *   handed to that dissector. After dissection is complete and control has
72  *   returned to the top-level dissector, the chain of tvbs (stack) is free'd
73  *   via a call to tvb_free_chain() (in epan_dissect_cleanup()).
74  *
75  * A dissector:
76  *  - Can chain new tvbs (subset, real, composite) to the
77  *    tvb handed to the dissector using tvb_new_subset(),
78  *    tvb_new_subset_length(), tvb_new_subset_remaining(),
79  *    tvb_new_child_real_data(), tvb_set_child_real_data_tvbuff(),
80  *    tvb_composite_finalize(), and tvb_child_uncompress(). (Composite
81  *    tvbs should reference only tvbs which are already part of the chain).
82  *  - Must not save for later use (e.g., when dissecting another frame) a
83  *    pointer to a tvb handed to the dissector; (A higher level function
84  *    may very well free the chain thus leaving a dangling pointer).
85  *    This (obviously) also applies to any tvbs chained to the tvb handed
86  *    to the dissector.
87  *  - Can create its own tvb chain (using tvb_new_real_data() which the
88  *
89  *    dissector is free to manage as desired.
90  * @{
91  */
92
93 /** TVBUFF_REAL_DATA contains a guint8* that points to real data.
94  * The data is allocated and contiguous.
95  *
96  * TVBUFF_SUBSET has a backing tvbuff. The TVBUFF_SUBSET is a "window"
97  * through which the program sees only a portion of the backing tvbuff.
98  *
99  * TVBUFF_COMPOSITE combines multiple tvbuffs sequentially to produce
100  * a larger byte array.
101  *
102  * tvbuff's of any type can be used as the backing-tvbuff of a
103  * TVBUFF_SUBSET or as the member of a TVBUFF_COMPOSITE.
104  * TVBUFF_COMPOSITEs can have member-tvbuffs of different types.
105  *
106  * Once a tvbuff is create/initialized/finalized, the tvbuff is read-only.
107  * That is, it cannot point to any other data. A new tvbuff must be created if
108  * you want a tvbuff that points to other data.
109  *
110  * tvbuff's are normally chained together to allow efficient de-allocation of
111  * tvbuff's.
112  */
113
114 typedef void (*tvbuff_free_cb_t)(void*);
115
116 /** Extracts 'number of bits' starting at 'bit offset'.
117  * Returns a pointer to a newly initialized g_malloc'd REAL_DATA
118  * tvbuff with the bits octet aligned.
119  */
120 WS_DLL_PUBLIC tvbuff_t *tvb_new_octet_aligned(tvbuff_t *tvb,
121     guint32 bit_offset, gint32 no_of_bits);
122
123 WS_DLL_PUBLIC tvbuff_t *tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing);
124
125 WS_DLL_PUBLIC tvbuff_t *tvb_clone(tvbuff_t *tvb);
126
127 WS_DLL_PUBLIC tvbuff_t *tvb_clone_offset_len(tvbuff_t *tvb, guint offset,
128     guint len);
129
130 /** Free a tvbuff_t and all tvbuffs chained from it
131  * The tvbuff must be 'the 'head' (initial) tvb of a chain or
132  * must not be in a chain.
133  * If specified, a callback to free the tvbuff data will be invoked
134  * for each tvbuff free'd */
135 WS_DLL_PUBLIC void tvb_free(tvbuff_t *tvb);
136
137 /** Free the tvbuff_t and all tvbuffs chained from it.
138  * The tvbuff must be 'the 'head' (initial) tvb of a chain or
139  * must not be in a chain.
140  * If specified, a callback to free the tvbuff data will be invoked
141  * for each tvbuff free'd */
142 WS_DLL_PUBLIC void tvb_free_chain(tvbuff_t *tvb);
143
144 /** Set a callback function to call when a tvbuff is actually freed
145  * One argument is passed to that callback --- a void* that points
146  * to the real data. Obviously, this only applies to a
147  * TVBUFF_REAL_DATA tvbuff. */
148 WS_DLL_PUBLIC void tvb_set_free_cb(tvbuff_t *tvb, const tvbuff_free_cb_t func);
149
150 /** Attach a TVBUFF_REAL_DATA tvbuff to a parent tvbuff. This connection
151  * is used during a tvb_free_chain()... the "child" TVBUFF_REAL_DATA acts
152  * as if it is part of the chain-of-creation of the parent tvbuff, although it
153  * isn't. This is useful if you need to take the data from some tvbuff,
154  * run some operation on it, like decryption or decompression, and make a new
155  * tvbuff from it, yet want the new tvbuff to be part of the chain. The reality
156  * is that the new tvbuff *is* part of the "chain of creation", but in a way
157  * that these tvbuff routines are ignorant of. Use this function to make
158  * the tvbuff routines knowledgable of this fact. */
159 WS_DLL_PUBLIC void tvb_set_child_real_data_tvbuff(tvbuff_t *parent,
160     tvbuff_t *child);
161
162 WS_DLL_PUBLIC tvbuff_t *tvb_new_child_real_data(tvbuff_t *parent,
163     const guint8 *data, const guint length, const gint reported_length);
164
165 /** Create a tvbuff backed by existing data. Can throw ReportedBoundsError.
166  * Normally, a callback to free the data should be registered using
167  * tvb_set_free_cb(); when this tvbuff is freed, then your callback will be
168  * called, and at that time you can free your original data. */
169 WS_DLL_PUBLIC tvbuff_t *tvb_new_real_data(const guint8 *data,
170     const guint length, const gint reported_length);
171
172 /** Create a tvbuff that's a subset of another tvbuff.
173  *
174  * 'backing_offset', if positive, is the offset from the beginning of
175  * the backing tvbuff at which the new tvbuff's data begins, and, if
176  * negative, is the offset from the end of the backing tvbuff at which
177  * the new tvbuff's data begins.
178  *
179  * 'backing_length' is the length of the data to include in the new
180  * tvbuff, starting with the byte at 'backing_offset"; if -1, it
181  * means "to the end of the backing tvbuff".  It can be 0, although
182  * the usefulness of the buffer would be rather limited.
183  *
184  * Will throw BoundsError if 'backing_offset'/'length'
185  * is beyond the bounds of the backing tvbuff.
186  * Can throw ReportedBoundsError. */
187 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset(tvbuff_t *backing,
188     const gint backing_offset, const gint backing_length,
189     const gint reported_length);
190
191 /**
192  * Similar to tvb_new_subset() but with captured length calculated
193  * to fit within the existing captured length and the specified
194  * backing length (which is used as the reported length).
195  * Can throw ReportedBoundsError. */
196 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset_length(tvbuff_t *backing,
197     const gint backing_offset, const gint backing_length);
198
199 /** Similar to tvb_new_subset() but with backing_length and reported_length set
200  * to -1.  Can throw ReportedBoundsError. */
201 WS_DLL_PUBLIC tvbuff_t *tvb_new_subset_remaining(tvbuff_t *backing,
202     const gint backing_offset);
203
204 /*
205 * Both tvb_composite_append and tvb_composite_prepend can throw
206  * BoundsError if member_offset/member_length goes beyond bounds of
207  * the 'member' tvbuff. */
208
209 /** Append to the list of tvbuffs that make up this composite tvbuff */
210 WS_DLL_PUBLIC void tvb_composite_append(tvbuff_t *tvb, tvbuff_t *member);
211
212 /** Prepend to the list of tvbuffs that make up this composite tvbuff */
213 extern void tvb_composite_prepend(tvbuff_t *tvb, tvbuff_t *member);
214
215 /** Create an empty composite tvbuff. */
216 WS_DLL_PUBLIC tvbuff_t *tvb_new_composite(void);
217
218 /** Mark a composite tvbuff as initialized. No further appends or prepends
219  * occur, data access can finally happen after this finalization. */
220 WS_DLL_PUBLIC void tvb_composite_finalize(tvbuff_t *tvb);
221
222
223 /* Get amount of captured data in the buffer (which is *NOT* necessarily the
224  * length of the packet). You probably want tvb_reported_length instead. */
225 WS_DLL_PUBLIC guint tvb_captured_length(const tvbuff_t *tvb);
226
227 /* DEPRECATED, do not use in new code, call tvb_captured_length directly! */
228 #define tvb_length tvb_captured_length
229
230 /** Computes bytes to end of buffer, from offset (which can be negative,
231  * to indicate bytes from end of buffer). Function returns 0 if offset is
232  * either at the end of the buffer or out of bounds. No exception is thrown.
233  * You probably want tvb_reported_length_remaining instead. */
234 WS_DLL_PUBLIC gint tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset);
235
236 /* DEPRECATED, do not use in new code, call tvb_captured_length_remaining directly! */
237 #define tvb_length_remaining tvb_captured_length_remaining
238
239 /** Same as above, but throws an exception if the offset is out of bounds. */
240 WS_DLL_PUBLIC guint tvb_ensure_captured_length_remaining(const tvbuff_t *tvb,
241     const gint offset);
242
243 /* DEPRECATED, do not use in new code, call tvb_ensure_captured_length_remaining directly! */
244 #define tvb_ensure_length_remaining tvb_ensure_captured_length_remaining
245
246 /* Checks (w/o throwing exception) that the bytes referred to by
247  * 'offset'/'length' actually exist in the buffer */
248 WS_DLL_PUBLIC gboolean tvb_bytes_exist(const tvbuff_t *tvb, const gint offset,
249     const gint length);
250
251 /** Checks that the bytes referred to by 'offset'/'length', where 'length'
252  * is a 64-bit unsigned integer, actually exist in the buffer, and throws
253  * an exception if they aren't. */
254 WS_DLL_PUBLIC void tvb_ensure_bytes_exist64(const tvbuff_t *tvb,
255     const gint offset, const guint64 length);
256
257 /** Checks that the bytes referred to by 'offset'/'length' actually exist
258  * in the buffer, and throws an exception if they aren't. */
259 WS_DLL_PUBLIC void tvb_ensure_bytes_exist(const tvbuff_t *tvb,
260     const gint offset, const gint length);
261
262 /* Checks (w/o throwing exception) that offset exists in buffer */
263 WS_DLL_PUBLIC gboolean tvb_offset_exists(const tvbuff_t *tvb,
264     const gint offset);
265
266 /* Get reported length of buffer */
267 WS_DLL_PUBLIC guint tvb_reported_length(const tvbuff_t *tvb);
268
269 /** Computes bytes of reported packet data to end of buffer, from offset
270  * (which can be negative, to indicate bytes from end of buffer). Function
271  * returns 0 if offset is either at the end of the buffer or out of bounds.
272  * No exception is thrown. */
273 WS_DLL_PUBLIC gint tvb_reported_length_remaining(const tvbuff_t *tvb,
274     const gint offset);
275
276 /** Set the reported length of a tvbuff to a given value; used for protocols
277    whose headers contain an explicit length and where the calling
278    dissector's payload may include padding as well as the packet for
279    this protocol.
280
281    Also adjusts the data length. */
282 WS_DLL_PUBLIC void tvb_set_reported_length(tvbuff_t *tvb, const guint);
283
284 WS_DLL_PUBLIC guint tvb_offset_from_real_beginning(const tvbuff_t *tvb);
285
286 /* Returns the offset from the first byte of real data. */
287 WS_DLL_PUBLIC gint tvb_raw_offset(tvbuff_t *tvb);
288
289 /** Set the "this is a fragment" flag. */
290 WS_DLL_PUBLIC void tvb_set_fragment(tvbuff_t *tvb);
291
292 WS_DLL_PUBLIC struct tvbuff *tvb_get_ds_tvb(tvbuff_t *tvb);
293
294
295 /************** START OF ACCESSORS ****************/
296 /* All accessors will throw an exception if appropriate */
297
298 WS_DLL_PUBLIC guint8 tvb_get_guint8(tvbuff_t *tvb, const gint offset);
299
300 WS_DLL_PUBLIC guint16 tvb_get_ntohs(tvbuff_t *tvb, const gint offset);
301 WS_DLL_PUBLIC guint32 tvb_get_ntoh24(tvbuff_t *tvb, const gint offset);
302 WS_DLL_PUBLIC guint32 tvb_get_ntohl(tvbuff_t *tvb, const gint offset);
303 WS_DLL_PUBLIC guint64 tvb_get_ntoh40(tvbuff_t *tvb, const gint offset);
304 WS_DLL_PUBLIC gint64 tvb_get_ntohi40(tvbuff_t *tvb, const gint offset);
305 WS_DLL_PUBLIC guint64 tvb_get_ntoh48(tvbuff_t *tvb, const gint offset);
306 WS_DLL_PUBLIC gint64 tvb_get_ntohi48(tvbuff_t *tvb, const gint offset);
307 WS_DLL_PUBLIC guint64 tvb_get_ntoh56(tvbuff_t *tvb, const gint offset);
308 WS_DLL_PUBLIC gint64 tvb_get_ntohi56(tvbuff_t *tvb, const gint offset);
309 WS_DLL_PUBLIC guint64 tvb_get_ntoh64(tvbuff_t *tvb, const gint offset);
310 WS_DLL_PUBLIC gfloat tvb_get_ntohieee_float(tvbuff_t *tvb, const gint offset);
311 WS_DLL_PUBLIC gdouble tvb_get_ntohieee_double(tvbuff_t *tvb,
312     const gint offset);
313
314 WS_DLL_PUBLIC guint16 tvb_get_letohs(tvbuff_t *tvb, const gint offset);
315 WS_DLL_PUBLIC guint32 tvb_get_letoh24(tvbuff_t *tvb, const gint offset);
316 WS_DLL_PUBLIC guint32 tvb_get_letohl(tvbuff_t *tvb, const gint offset);
317 WS_DLL_PUBLIC guint64 tvb_get_letoh40(tvbuff_t *tvb, const gint offset);
318 WS_DLL_PUBLIC gint64 tvb_get_letohi40(tvbuff_t *tvb, const gint offset);
319 WS_DLL_PUBLIC guint64 tvb_get_letoh48(tvbuff_t *tvb, const gint offset);
320 WS_DLL_PUBLIC gint64 tvb_get_letohi48(tvbuff_t *tvb, const gint offset);
321 WS_DLL_PUBLIC guint64 tvb_get_letoh56(tvbuff_t *tvb, const gint offset);
322 WS_DLL_PUBLIC gint64 tvb_get_letohi56(tvbuff_t *tvb, const gint offset);
323 WS_DLL_PUBLIC guint64 tvb_get_letoh64(tvbuff_t *tvb, const gint offset);
324 WS_DLL_PUBLIC gfloat tvb_get_letohieee_float(tvbuff_t *tvb, const gint offset);
325 WS_DLL_PUBLIC gdouble tvb_get_letohieee_double(tvbuff_t *tvb,
326     const gint offset);
327
328 WS_DLL_PUBLIC guint16 tvb_get_guint16(tvbuff_t *tvb, const gint offset, const guint encoding);
329 WS_DLL_PUBLIC guint32 tvb_get_guint24(tvbuff_t *tvb, const gint offset, const guint encoding);
330 WS_DLL_PUBLIC guint32 tvb_get_guint32(tvbuff_t *tvb, const gint offset, const guint encoding);
331 WS_DLL_PUBLIC guint64 tvb_get_guint40(tvbuff_t *tvb, const gint offset, const guint encoding);
332 WS_DLL_PUBLIC gint64 tvb_get_gint40(tvbuff_t *tvb, const gint offset, const guint encoding);
333 WS_DLL_PUBLIC guint64 tvb_get_guint48(tvbuff_t *tvb, const gint offset, const guint encoding);
334 WS_DLL_PUBLIC gint64 tvb_get_gint48(tvbuff_t *tvb, const gint offset, const guint encoding);
335 WS_DLL_PUBLIC guint64 tvb_get_guint56(tvbuff_t *tvb, const gint offset, const guint encoding);
336 WS_DLL_PUBLIC gint64 tvb_get_gint56(tvbuff_t *tvb, const gint offset, const guint encoding);
337 WS_DLL_PUBLIC guint64 tvb_get_guint64(tvbuff_t *tvb, const gint offset, const guint encoding);
338 WS_DLL_PUBLIC gfloat tvb_get_ieee_float(tvbuff_t *tvb, const gint offset, const guint encoding);
339 WS_DLL_PUBLIC gdouble tvb_get_ieee_double(tvbuff_t *tvb, const gint offset, const guint encoding);
340
341 /*
342  * Fetch 16-bit and 32-bit values in host byte order.
343  * Used for some pseudo-headers in pcap/pcap-ng files, in which the
344  * headers are, when capturing, in the byte order of the host, and
345  * are converted to the byte order of the host reading the file
346  * when reading a capture file.
347  */
348 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
349 #define tvb_get_h_guint16   tvb_get_letohs
350 #define tvb_get_h_guint32   tvb_get_letohl
351 #elif G_BYTE_ORDER == G_BIG_ENDIAN
352 #define tvb_get_h_guint16   tvb_get_ntohs
353 #define tvb_get_h_guint32   tvb_get_ntohl
354 #else
355 #error "Unsupported byte order"
356 #endif
357
358
359 /* Fetch a time value from an ASCII-style string in the tvb.
360  *
361  * @param[in] offset The beginning offset in the tvb (cannot be negative)
362  * @param[in] length The field's length in the tvb (or -1 for remaining)
363  * @param[in] encoding The ENC_* that defines the format (e.g., ENC_ISO_8601_DATE_TIME)
364  * @param[in,out] ns The pre-allocated nstime_t that will be set to the decoded value
365  * @param[out] endoff if not NULL, should point to a gint that this
366  *     routine will then set to be the offset to the character after
367  *     the last character used in the conversion. This is useful because
368  *     they may not consume the whole section.
369  *
370  * @return a pointer to the nstime_t passed-in, or NULL on failure; if no
371  *    valid conversion could be performed, *endoff is set to 0, and errno will be
372  *    EDOM or ERANGE, and the nstime_t* passed-in will be cleared.
373  *
374  * @note The conversion ignores leading spaces, and will succeed even if it does
375  *    not consume the entire string. If you care about such things, always compare
376  *    the *endoff to where you expect it to be (namely, offset+length).
377  *
378  * This routine will not throw an error unless the passed-in arguments are
379  * invalid (e.g., offset is beyond the tvb's length).
380  *
381  * @warning This only works for string encodings which encode ASCII characters in
382  * a single byte: ENC_ASCII, ENC_UTF_8, ENC_ISO_8859_*, etc. It does NOT work
383  * for purely multi-byte encodings such as ENC_UTF_16, ENC_UCS_*, etc.
384  */
385 WS_DLL_PUBLIC
386 struct nstime_t* tvb_get_string_time(tvbuff_t *tvb, const gint offset, const gint length,
387                               const guint encoding, struct nstime_t* ns, gint *endoff);
388
389 /* Similar to above, but returns a GByteArray based on the case-insensitive
390  * hex-char strings with optional separators, and with optional leading spaces.
391  * The separators allowed are based on the ENC_SEP_* passed in the encoding param.
392  *
393  * The passed-in bytes is set to the values, and its pointer is also the return
394  * value or NULL on error. The GByteArray bytes must be pre-constructed with
395  * g_byte_array_new().
396  */
397 WS_DLL_PUBLIC
398 GByteArray* tvb_get_string_bytes(tvbuff_t *tvb, const gint offset, const gint length,
399                                  const guint encoding, GByteArray* bytes, gint *endoff);
400
401 /**
402  * Fetch an IPv4 address, in network byte order.
403  * We do *not* convert it to host byte order; we leave it in
404  * network byte order, as that's what its callers expect. */
405 WS_DLL_PUBLIC guint32 tvb_get_ipv4(tvbuff_t *tvb, const gint offset);
406
407 /* Fetch an IPv6 address. */
408 WS_DLL_PUBLIC void tvb_get_ipv6(tvbuff_t *tvb, const gint offset,
409     struct e_in6_addr *addr);
410
411 /* Fetch a GUID. */
412 WS_DLL_PUBLIC void tvb_get_ntohguid(tvbuff_t *tvb, const gint offset,
413     e_guid_t *guid);
414 WS_DLL_PUBLIC void tvb_get_letohguid(tvbuff_t *tvb, const gint offset,
415     e_guid_t *guid);
416 WS_DLL_PUBLIC void tvb_get_guid(tvbuff_t *tvb, const gint offset,
417     e_guid_t *guid, const guint encoding);
418
419 /* Fetch a specified number of bits from bit offset in a tvb.  All of these
420  * functions are equivalent, except for the type of the return value.  Note
421  * that the parameter encoding (where supplied) is meaningless and ignored */
422
423 /* get 1 - 8 bits returned in a guint8 */
424 WS_DLL_PUBLIC guint8 tvb_get_bits8(tvbuff_t *tvb, guint bit_offset,
425     const gint no_of_bits);
426 /* get 1 - 16 bits returned in a guint16 */
427 WS_DLL_PUBLIC guint16 tvb_get_bits16(tvbuff_t *tvb, guint bit_offset,
428     const gint no_of_bits, const guint encoding);
429 /* get 1 - 32 bits returned in a guint32 */
430 WS_DLL_PUBLIC guint32 tvb_get_bits32(tvbuff_t *tvb, guint bit_offset,
431     const gint no_of_bits, const guint encoding);
432 /* get 1 - 64 bits returned in a guint64 */
433 WS_DLL_PUBLIC guint64 tvb_get_bits64(tvbuff_t *tvb, guint bit_offset,
434     const gint no_of_bits, const guint encoding);
435
436 /**
437  *  This function has EXACTLY the same behavior as
438  *  tvb_get_bits32()
439  */
440 WS_DLL_PUBLIC guint32 tvb_get_bits(tvbuff_t *tvb, const guint bit_offset,
441     const gint no_of_bits, const guint encoding);
442
443 /** Returns target for convenience. Does not suffer from possible
444  * expense of tvb_get_ptr(), since this routine is smart enough
445  * to copy data in chunks if the request range actually exists in
446  * different TVBUFF_REAL_DATA tvbuffs. This function assumes that the
447  * target memory is already allocated; it does not allocate or free the
448  * target memory. */
449 WS_DLL_PUBLIC void *tvb_memcpy(tvbuff_t *tvb, void *target, const gint offset,
450     size_t length);
451
452 /** Given an allocator scope, a tvbuff, a byte offset, a byte length:
453  *
454  *    allocate a buffer using the specified scope;
455  *
456  *    copy the data from the tvbuff specified by the offset and length
457  *    into that buffer, using tvb_memcpy();
458  *
459  *    and return a pointer to the buffer.
460  *
461  * Throws an exception if the tvbuff ends before the data being copied does.
462  *
463  * If scope is set to NULL it is the user's responsibility to wmem_free()
464  * the memory allocated. Otherwise memory is automatically freed when the
465  * scope lifetime is reached.
466  */
467 WS_DLL_PUBLIC void *tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb,
468     const gint offset, size_t length);
469
470 /** WARNING! This function is possibly expensive, temporarily allocating
471  * another copy of the packet data. Furthermore, it's dangerous because once
472  * this pointer is given to the user, there's no guarantee that the user will
473  * honor the 'length' and not overstep the boundaries of the buffer.
474  *
475  * If you're thinking of using tvb_get_ptr, STOP WHAT YOU ARE DOING
476  * IMMEDIATELY. Go take a break. Consider that tvb_get_ptr hands you
477  * a raw, unprotected pointer that you can easily use to create a
478  * security vulnerability or otherwise crash Wireshark. Then consider
479  * that you can probably find a function elsewhere in this file that
480  * does exactly what you want in a much more safe and robust manner.
481  *
482  * The returned pointer is data that is internal to the tvbuff, so do not
483  * attempt to free it. Don't modify the data, either, because another tvbuff
484  * that might be using this tvbuff may have already copied that portion of
485  * the data (sometimes tvbuff's need to make copies of data, but that's the
486  * internal implementation that you need not worry about). Assume that the
487  * guint8* points to read-only data that the tvbuff manages.
488  *
489  * Return a pointer into our buffer if the data asked for via 'offset'/'length'
490  * is contiguous (which might not be the case for TVBUFF_COMPOSITE). If the
491  * data is not contiguous, a tvb_memdup() is called for the entire buffer
492  * and the pointer to the newly-contiguous data is returned. This dynamically-
493  * allocated memory will be freed when the tvbuff is freed, after the
494  * tvbuff_free_cb_t() is called, if any. */
495 WS_DLL_PUBLIC const guint8 *tvb_get_ptr(tvbuff_t *tvb, const gint offset,
496     const gint length);
497
498 /** Find first occurrence of needle in tvbuff, starting at offset. Searches
499  * at most maxlength number of bytes; if maxlength is -1, searches to
500  * end of tvbuff.
501  * Returns the offset of the found needle, or -1 if not found.
502  * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
503  * in that case, -1 will be returned if the boundary is reached before
504  * finding needle. */
505 WS_DLL_PUBLIC gint tvb_find_guint8(tvbuff_t *tvb, const gint offset,
506     const gint maxlength, const guint8 needle);
507
508 /** Find first occurrence of any of the needles in tvbuff, starting at offset.
509  * Searches at most maxlength number of bytes. Returns the offset of the
510  * found needle, or -1 if not found and the found needle.
511  * Will not throw an exception, even if
512  * maxlength exceeds boundary of tvbuff; in that case, -1 will be returned if
513  * the boundary is reached before finding needle. */
514 WS_DLL_PUBLIC gint tvb_pbrk_guint8(tvbuff_t *tvb, const gint offset,
515     const gint maxlength, const guint8 *needles, guchar *found_needle);
516
517 /** Find size of stringz (NUL-terminated string) by looking for terminating
518  * NUL.  The size of the string includes the terminating NUL.
519  *
520  * If the NUL isn't found, it throws the appropriate exception.
521  */
522 WS_DLL_PUBLIC guint tvb_strsize(tvbuff_t *tvb, const gint offset);
523
524 /** Find size of UCS-2 or UTF-16 stringz (NUL-terminated string) by
525  * looking for terminating 16-bit NUL.  The size of the string includes
526  * the terminating NUL.
527  *
528  * If the NUL isn't found, it throws the appropriate exception.
529  */
530 WS_DLL_PUBLIC guint tvb_unicode_strsize(tvbuff_t *tvb, const gint offset);
531
532 /** Find length of string by looking for end of zero terminated string, up to
533  * 'maxlength' characters'; if 'maxlength' is -1, searches to end
534  * of tvbuff.
535  * Returns -1 if 'maxlength' reached before finding EOS. */
536 WS_DLL_PUBLIC gint tvb_strnlen(tvbuff_t *tvb, const gint offset,
537     const guint maxlength);
538
539 /**
540  * Format the data in the tvb from offset for size ...
541  */
542 WS_DLL_PUBLIC gchar *tvb_format_text(tvbuff_t *tvb, const gint offset,
543     const gint size);
544
545 /**
546  * Like "tvb_format_text()", but for 'wsp'; don't show
547  * the characters as C-style escapes.
548  */
549 WS_DLL_PUBLIC gchar *tvb_format_text_wsp(tvbuff_t *tvb, const gint offset,
550     const gint size);
551
552 /**
553  * Like "tvb_format_text()", but for null-padded strings; don't show
554  * the null padding characters as "\000".
555  */
556 extern gchar *tvb_format_stringzpad(tvbuff_t *tvb, const gint offset,
557     const gint size);
558
559 /**
560  * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
561  * the null padding characters as "\000".
562  */
563 extern gchar *tvb_format_stringzpad_wsp(tvbuff_t *tvb, const gint offset,
564     const gint size);
565
566 /**
567  * Given an allocator scope, a tvbuff, a byte offset, a byte length, and
568  * a string encoding, with the specified offset and length referring to
569  * a string in the specified encoding:
570  *
571  *    allocate a buffer using the specified scope;
572  *
573  *    convert the string from the specified encoding to UTF-8, possibly
574  *    mapping some characters or invalid octet sequences to the Unicode
575  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
576  *    trailing '\0', into that buffer;
577  *
578  *    and return a pointer to the buffer.
579  *
580  * Throws an exception if the tvbuff ends before the string does.
581  *
582  * If scope is set to NULL it is the user's responsibility to wmem_free()
583  * the memory allocated. Otherwise memory is automatically freed when the
584  * scope lifetime is reached.
585  */
586 WS_DLL_PUBLIC guint8 *tvb_get_string_enc(wmem_allocator_t *scope,
587     tvbuff_t *tvb, const gint offset, const gint length, const guint encoding);
588
589 /*
590  * DEPRECATED, do not use in new code, call tvb_get_string_enc directly with
591  * the appropriate extension!  Do not assume that ENC_ASCII will work
592  * with arbitrary string encodings; it will map all bytes with the 8th
593  * bit set to the Unicode REPLACEMENT CHARACTER, so it won't show non-ASCII
594  * characters as anything other than an ugly blob.
595  */
596 #define tvb_get_string(SCOPE, TVB, OFFSET, LENGTH) \
597     tvb_get_string_enc(SCOPE, TVB, OFFSET, LENGTH, ENC_ASCII)
598
599 /**
600  * Given an allocator scope, a tvbuff, a bit offset, and a length in
601  * 7-bit characters (not octets!), with the specified offset and
602  * length referring to a string in the 3GPP TS 23.038 7bits encoding:
603  *
604  *    allocate a buffer using the specified scope;
605  *
606  *    convert the string from the specified encoding to UTF-8, possibly
607  *    mapping some characters or invalid octet sequences to the Unicode
608  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
609  *    trailing '\0', into that buffer;
610  *
611  *    and return a pointer to the buffer.
612  *
613  * Throws an exception if the tvbuff ends before the string does.
614  *
615  * If scope is set to NULL it is the user's responsibility to wmem_free()
616  * the memory allocated. Otherwise memory is automatically freed when the
617  * scope lifetime is reached.
618  */
619 WS_DLL_PUBLIC gchar *tvb_get_ts_23_038_7bits_string(wmem_allocator_t *scope,
620     tvbuff_t *tvb, const gint bit_offset, gint no_of_chars);
621
622 /**
623  * Given an allocator scope, a tvbuff, a bit offset, and a length in
624  * 7-bit characters (not octets!), with the specified offset and
625  * length referring to a string in the ASCII 7bits encoding:
626  *
627  *    allocate a buffer using the specified scope;
628  *
629  *    convert the string from the specified encoding to UTF-8, possibly
630  *    mapping some characters or invalid octet sequences to the Unicode
631  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
632  *    trailing '\0', into that buffer;
633  *
634  *    and return a pointer to the buffer.
635  *
636  * Throws an exception if the tvbuff ends before the string does.
637  *
638  * If scope is set to NULL it is the user's responsibility to wmem_free()
639  * the memory allocated. Otherwise memory is automatically freed when the
640  * scope lifetime is reached.
641  */
642 WS_DLL_PUBLIC gchar *tvb_get_ascii_7bits_string(wmem_allocator_t *scope,
643     tvbuff_t *tvb, const gint bit_offset, gint no_of_chars);
644
645 /**
646  * Given an allocator scope, a tvbuff, a byte offset, a byte length, and
647  * a string encoding, with the specified offset and length referring to
648  * a null-padded string in the specified encoding:
649  *
650  *    allocate a buffer using the specified scope;
651  *
652  *    convert the string from the specified encoding to UTF-8, possibly
653  *    mapping some characters or invalid octet sequences to the Unicode
654  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
655  *    trailing '\0', into that buffer;
656  *
657  *    and return a pointer to the buffer.
658  *
659  * Throws an exception if the tvbuff ends before the string does.
660  *
661  * If scope is set to NULL it is the user's responsibility to wmem_free()
662  * the memory allocated. Otherwise memory is automatically freed when the
663  * scope lifetime is reached.
664  */
665 WS_DLL_PUBLIC guint8 *tvb_get_stringzpad(wmem_allocator_t *scope,
666     tvbuff_t *tvb, const gint offset, const gint length, const guint encoding);
667
668 /**
669  * Given an allocator scope, a tvbuff, a byte offset, a pointer to a
670  * gint, and a string encoding, with the specified offset referring to
671  * a null-terminated string in the specified encoding:
672  *
673  *    find the length of that string (and throw an exception if the tvbuff
674  *    ends before we find the null);
675  *
676  *    allocate a buffer using the specified scope;
677  *
678  *    convert the string from the specified encoding to UTF-8, possibly
679  *    mapping some characters or invalid octet sequences to the Unicode
680  *    REPLACEMENT CHARACTER, and put the resulting UTF-8 string, plus a
681  *    trailing '\0', into that buffer;
682  *
683  *    if the pointer to the gint is non-null, set the gint to which it
684  *    points to the length of the string;
685  *
686  *    and return a pointer to the buffer.
687  *
688  * Throws an exception if the tvbuff ends before the string does.
689  *
690  * If scope is set to NULL it is the user's responsibility to wmem_free()
691  * the memory allocated. Otherwise memory is automatically freed when the
692  * scope lifetime is reached.
693  */
694 WS_DLL_PUBLIC guint8 *tvb_get_stringz_enc(wmem_allocator_t *scope,
695     tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding);
696
697 /*
698  * DEPRECATED, do not use in new code, call tvb_get_string_enc directly with
699  * the appropriate extension!  Do not assume that ENC_ASCII will work
700  * with arbitrary string encodings; it will map all bytes with the 8th
701  * bit set to the Unicode REPLACEMENT CHARACTER, so it won't show non-ASCII
702  * characters as anything other than an ugly blob.
703  */
704 #define tvb_get_stringz(SCOPE, TVB, OFFSET, LENGTHP) \
705     tvb_get_stringz_enc(SCOPE, TVB, OFFSET, LENGTHP, ENC_ASCII)
706
707 /**
708  * Given a tvbuff and an offset, with the offset assumed to refer to
709  * a null-terminated string, find the length of that string (and throw
710  * an exception if the tvbuff ends before we find the null), allocate
711  * a buffer big enough to hold the string, copy the string into it,
712  * and return a pointer to the string.  Also return the length of the
713  * string (including the terminating null) through a pointer.
714  *
715  * This returns a constant (unmodifiable) string that does not need
716  * to be freed; instead, it will automatically be freed once the next
717  * packet is dissected.
718  *
719  * It is slightly more efficient than the other routines, but does *NOT*
720  * do any translation to UTF-8 - the string consists of the raw octets
721  * of the string, in whatever encoding they happen to be in, and, if
722  * the string is not valid in that encoding, with invalid octet sequences
723  * as they are in the packet.
724  */
725 WS_DLL_PUBLIC const guint8 *tvb_get_const_stringz(tvbuff_t *tvb,
726     const gint offset, gint *lengthp);
727
728 /** Looks for a stringz (NUL-terminated string) in tvbuff and copies
729  * no more than bufsize number of bytes, including terminating NUL, to buffer.
730  * Returns length of string (not including terminating NUL), or -1 if the
731  * string was truncated in the buffer due to not having reached the terminating
732  * NUL.  In this way, it acts like g_snprintf().
733  *
734  * When processing a packet where the remaining number of bytes is less
735  * than bufsize, an exception is not thrown if the end of the packet
736  * is reached before the NUL is found. If no NUL is found before reaching
737  * the end of the short packet, -1 is still returned, and the string
738  * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
739  * at the correct spot, terminating the string.
740  */
741 WS_DLL_PUBLIC gint tvb_get_nstringz(tvbuff_t *tvb, const gint offset,
742     const guint bufsize, guint8 *buffer);
743
744 /** Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
745  * have a terminating NUL. If the string was truncated when copied into buffer,
746  * a NUL is placed at the end of buffer to terminate it.
747  *
748  * bufsize MUST be greater than 0.
749  */
750 WS_DLL_PUBLIC gint tvb_get_nstringz0(tvbuff_t *tvb, const gint offset,
751     const guint bufsize, guint8 *buffer);
752
753 /**
754  * Given a tvbuff, an offset into the tvbuff, and a length that starts
755  * at that offset (which may be -1 for "all the way to the end of the
756  * tvbuff"), find the end of the (putative) line that starts at the
757  * specified offset in the tvbuff, going no further than the specified
758  * length.
759  *
760  * Return the length of the line (not counting the line terminator at
761  * the end), or, if we don't find a line terminator:
762  *
763  *  if "deseg" is true, return -1;
764  *
765  *  if "deseg" is false, return the amount of data remaining in
766  *  the buffer.
767  *
768  * Set "*next_offset" to the offset of the character past the line
769  * terminator, or past the end of the buffer if we don't find a line
770  * terminator.  (It's not set if we return -1.)
771  */
772 WS_DLL_PUBLIC gint tvb_find_line_end(tvbuff_t *tvb, const gint offset, int len,
773     gint *next_offset, const gboolean desegment);
774
775 /**
776  * Given a tvbuff, an offset into the tvbuff, and a length that starts
777  * at that offset (which may be -1 for "all the way to the end of the
778  * tvbuff"), find the end of the (putative) line that starts at the
779  * specified offset in the tvbuff, going no further than the specified
780  * length.
781  *
782  * However, treat quoted strings inside the buffer specially - don't
783  * treat newlines in quoted strings as line terminators.
784  *
785  * Return the length of the line (not counting the line terminator at
786  * the end), or the amount of data remaining in the buffer if we don't
787  * find a line terminator.
788  *
789  * Set "*next_offset" to the offset of the character past the line
790  * terminator, or past the end of the buffer if we don't find a line
791  * terminator.
792  */
793 WS_DLL_PUBLIC gint tvb_find_line_end_unquoted(tvbuff_t *tvb, const gint offset,
794     int len, gint *next_offset);
795
796 /**
797  * Copied from the mgcp dissector. (This function should be moved to /epan )
798  * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
799  *                character following offset or offset + maxlength -1 whichever
800  *                is smaller.
801  *
802  * Parameters:
803  * tvb - The tvbuff in which we are skipping whitespace.
804  * offset - The offset in tvb from which we begin trying to skip whitespace.
805  * maxlength - The maximum distance from offset that we may try to skip
806  * whitespace.
807  *
808  * Returns: The position in tvb of the first non-whitespace
809  *          character following offset or offset + maxlength -1 whichever
810  *          is smaller.
811  */
812
813 WS_DLL_PUBLIC gint tvb_skip_wsp(tvbuff_t *tvb, const gint offset,
814     const gint maxlength);
815
816 WS_DLL_PUBLIC gint tvb_skip_wsp_return(tvbuff_t *tvb, const gint offset);
817
818 int tvb_skip_guint8(tvbuff_t *tvb, int offset, const int maxlength, const guint8 ch);
819
820 /**
821  * Call strncmp after checking if enough chars left, returning 0 if
822  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
823  */
824 WS_DLL_PUBLIC gint tvb_strneql(tvbuff_t *tvb, const gint offset,
825     const gchar *str, const size_t size);
826
827 /**
828  * Call g_ascii_strncasecmp after checking if enough chars left, returning
829  * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
830  */
831 WS_DLL_PUBLIC gint tvb_strncaseeql(tvbuff_t *tvb, const gint offset,
832     const gchar *str, const size_t size);
833
834 /**
835  * Call memcmp after checking if enough chars left, returning 0 if
836  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
837  */
838 WS_DLL_PUBLIC gint tvb_memeql(tvbuff_t *tvb, const gint offset,
839     const guint8 *str, size_t size);
840
841 /**
842  * Format a bunch of data from a tvbuff as bytes, returning a pointer
843  * to the string with the formatted data, with "punct" as a byte
844  * separator.
845  */
846 WS_DLL_PUBLIC gchar *tvb_bytes_to_ep_str_punct(tvbuff_t *tvb, const gint offset,
847     const gint len, const gchar punct);
848
849 /**
850  * Format a bunch of data from a tvbuff as bytes, returning a pointer
851  * to the string with the formatted data.
852  */
853 WS_DLL_PUBLIC gchar *tvb_bytes_to_ep_str(tvbuff_t *tvb, const gint offset,
854     const gint len);
855
856 /**
857  * Given a tvbuff, an offset into the tvbuff, and a length that starts
858  * at that offset (which may be -1 for "all the way to the end of the
859  * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
860  * the low or high half byte, formatting the digits according to an input digit
861  * set, if NUL a default digit set of 0-9 returning "?" for overdecadic digits
862  * will be used.  A pointer to the packet-scope (WMEM-allocated) string will
863  * be returned. Note a tvbuff content of 0xf is considered a 'filler' and will
864  * end the conversion.
865  */
866 typedef struct dgt_set_t
867 {
868     const unsigned char out[16];
869 }
870 dgt_set_t;
871
872 WS_DLL_PUBLIC const gchar *tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb,
873     const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first);
874
875 /** Locate a sub-tvbuff within another tvbuff, starting at position
876  * 'haystack_offset'. Returns the index of the beginning of 'needle' within
877  * 'haystack', or -1 if 'needle' is not found. The index is relative
878  * to the start of 'haystack', not 'haystack_offset'. */
879 WS_DLL_PUBLIC gint tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb,
880     const gint haystack_offset);
881
882 /* From tvbuff_zlib.c */
883
884 /**
885  * Uncompresses a zlib compressed packet inside a tvbuff at offset with
886  * length comprlen.  Returns an uncompressed tvbuffer if uncompression
887  * succeeded or NULL if uncompression failed.
888  */
889 WS_DLL_PUBLIC tvbuff_t *tvb_uncompress(tvbuff_t *tvb, const int offset,
890     int comprlen);
891
892 /**
893  * Uncompresses a zlib compressed packet inside a tvbuff at offset with
894  * length comprlen.  Returns an uncompressed tvbuffer attached to tvb if
895  * uncompression succeeded or NULL if uncompression failed.
896  */
897 WS_DLL_PUBLIC tvbuff_t *tvb_child_uncompress(tvbuff_t *parent, tvbuff_t *tvb,
898     const int offset, int comprlen);
899
900 /* From tvbuff_base64.c */
901
902 /** Return a tvb that contains the binary representation of a base64
903  *  string
904  */
905 extern tvbuff_t* base64_to_tvb(tvbuff_t *parent, const char *base64);
906
907 /************** END OF ACCESSORS ****************/
908
909 /** @} */
910
911 #ifdef __cplusplus
912 }
913 #endif /* __cplusplus */
914
915 #endif /* __TVBUFF_H__ */
916
917 /*
918  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
919  *
920  * Local variables:
921  * c-basic-offset: 4
922  * tab-width: 8
923  * indent-tabs-mode: nil
924  * End:
925  *
926  * vi: set shiftwidth=4 tabstop=8 expandtab:
927  * :indentSize=4:tabSize=8:noTabs=true:
928  */