Stop using "tvb_get_ntohll()" and "%llX" in the BOOTP dissector, as the
[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: tvbuff.h,v 1.16 2001/10/29 21:56:48 guy Exp $
13  *
14  * Copyright (c) 2000 by Gilbert Ramirez <gram@xiexie.org>
15  *
16  * Ethereal - Network traffic analyzer
17  * By Gerald Combs <gerald@ethereal.com>
18  * Copyright 1998 Gerald Combs
19  *
20  * 
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License
23  * as published by the Free Software Foundation; either version 2
24  * of the License, or (at your option) any later version.
25  * 
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  * 
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
34  */
35
36 #ifndef __TVBUFF_H__
37 #define __TVBUFF_H__
38
39 #include <glib.h>
40 #include "exceptions.h"
41
42 typedef struct tvbuff tvbuff_t;
43
44 typedef void (*tvbuff_free_cb_t)(void*);
45
46 /* The different types of tvbuff's */
47 typedef enum {
48         TVBUFF_REAL_DATA,
49         TVBUFF_SUBSET,
50         TVBUFF_COMPOSITE
51 } tvbuff_type;
52
53 /* TVBUFF_REAL_DATA contains a guint8* that points to real data.
54  * The data is allocated and contiguous.
55  *
56  * TVBUFF_SUBSET has a backing tvbuff. The TVBUFF_SUBSET is a "window"
57  * through which the program sees only a portion of the backing tvbuff.
58  *
59  * TVBUFF_COMPOSITE combines multiple tvbuffs sequentually to produce
60  * a larger byte array.
61  *
62  * tvbuff's of any type can be used as the backing-tvbuff of a
63  * TVBUFF_SUBSET or as the member of a TVBUFF_COMPOSITE.
64  * TVBUFF_COMPOSITEs can have member-tvbuffs of different types.
65  *
66  * Once a tvbuff is create/initialized/finalized, the tvbuff is read-only.
67  * That is, it cannot point to any other data. A new tvbuff must be created if
68  * you want a tvbuff that points to other data.
69  */
70
71
72 /* "class" initialization. Called once during execution of program
73  * so that tvbuff.c can initialize its data. */
74 void tvbuff_init(void);
75
76 /* "class" cleanup. Called once during execution of program
77  * so that tvbuff.c can clean up its data. */
78 void tvbuff_cleanup(void);
79
80
81 /* Returns a pointer to a newly initialized tvbuff. Note that
82  * tvbuff's of types TVBUFF_SUBSET and TVBUFF_COMPOSITE
83  * require further initialization via the appropriate functions */
84 tvbuff_t* tvb_new(tvbuff_type);
85
86 /* Marks a tvbuff for freeing. The guint8* data of a TVBUFF_REAL_DATA
87  * is *never* freed by the tvbuff routines. The tvbuff itself is actually freed
88  * once its usage count drops to 0.
89  *
90  * Usage counts increment for any time the tvbuff is
91  * used as a member of another tvbuff, i.e., as the backing buffer for
92  * a TVBUFF_SUBSET or as a member of a TVBUFF_COMPOSITE.
93  *
94  * Although you may call tvb_free(), the tvbuff may still be in use
95  * by other tvbuff's (TVBUFF_SUBSET or TVBUFF_COMPOSITE), so it is not
96  * safe, unless you know otherwise, to free your guint8* data. If you
97  * cannot be sure that your TVBUFF_REAL_DATA is not in use by another
98  * tvbuff, register a callback with tvb_set_free_cb(); when your tvbuff
99  * is _really_ freed, then your callback will be called, and at that time
100  * you can free your original data.
101  *
102  * The caller can artificially increment/decrement the usage count
103  * with tvbuff_increment_usage_count()/tvbuff_decrement_usage_count().
104  */
105 void tvb_free(tvbuff_t*);
106
107 /* Free the tvbuff_t and all tvbuff's created from it. */
108 void tvb_free_chain(tvbuff_t*);
109
110 /* Both return the new usage count, after the increment or decrement */
111 guint tvb_increment_usage_count(tvbuff_t*, guint count);
112
113 /* If a decrement causes the usage count to drop to 0, a the tvbuff
114  * is immediately freed. Be sure you know exactly what you're doing
115  * if you decide to use this function, as another tvbuff could
116  * still have a pointer to the just-freed tvbuff, causing corrupted data
117  * or a segfault in the future */
118 guint tvb_decrement_usage_count(tvbuff_t*, guint count);
119
120 /* Set a callback function to call when a tvbuff is actually freed
121  * (once the usage count drops to 0). One argument is passed to
122  * that callback --- a void* that points to the real data.
123  * Obviously, this only applies to a TVBUFF_REAL_DATA tvbuff. */
124 void tvb_set_free_cb(tvbuff_t*, tvbuff_free_cb_t);
125
126
127 /* Attach a TVBUFF_REAL_DATA tvbuff to a parent tvbuff. This connection
128  * is used during a tvb_free_chain()... the "child" TVBUFF_REAL_DATA acts
129  * as if is part of the chain-of-creation of the parent tvbuff, although it
130  * isn't. This is useful if you need to take the data from some tvbuff,
131  * run some operation on it, like decryption or decompression, and make a new
132  * tvbuff from it, yet want the new tvbuff to be part of the chain. The reality
133  * is that the new tvbuff *is* part of the "chain of creation", but in a way
134  * that these tvbuff routines is ignorant of. Use this function to make
135  * the tvbuff routines knowledgable of this fact. */
136 void tvb_set_child_real_data_tvbuff(tvbuff_t* parent, tvbuff_t* child);
137
138 /* Sets parameters for TVBUFF_REAL_DATA. Can throw ReportedBoundsError. */
139 void tvb_set_real_data(tvbuff_t*, const guint8* data, guint length, gint reported_length);
140
141 /* Combination of tvb_new() and tvb_set_real_data(). Can throw ReportedBoundsError. */
142 tvbuff_t* tvb_new_real_data(const guint8* data, guint length, gint reported_length, const gchar *name);
143
144
145 /* Define the subset of the backing buffer to use.
146  *
147  * 'backing_offset' can be negative, to indicate bytes from
148  * the end of the backing buffer.
149  *
150  * 'backing_length' can be 0, although the usefulness of the buffer would
151  * be rather limited.
152  *
153  * 'backing_length' of -1 means "to the end of the backing buffer"
154  *
155  * Will throw BoundsError if 'backing_offset'/'length'
156  * is beyond the bounds of the backing tvbuff.
157  * Can throw ReportedBoundsError. */
158 void tvb_set_subset(tvbuff_t* tvb, tvbuff_t* backing,
159                 gint backing_offset, gint backing_length, gint reported_length);
160
161 /* Combination of tvb_new() and tvb_set_subset()
162  * Can throw ReportedBoundsError. */
163 tvbuff_t* tvb_new_subset(tvbuff_t* backing,
164                 gint backing_offset, gint backing_length, gint reported_length);
165
166
167 /* Both tvb_composite_append and tvb_composite_prepend can throw
168  * BoundsError if member_offset/member_length goes beyond bounds of
169  * the 'member' tvbuff. */
170
171 /* Append to the list of tvbuffs that make up this composite tvbuff */
172 void tvb_composite_append(tvbuff_t* tvb, tvbuff_t* member);
173
174 /* Prepend to the list of tvbuffs that make up this composite tvbuff */
175 void tvb_composite_prepend(tvbuff_t* tvb, tvbuff_t* member);
176
177 /* Helper function that calls tvb_new(TVBUFF_COMPOSITE).
178  * Provided only to maintain symmetry with other constructors */
179 tvbuff_t* tvb_new_composite(void);
180
181 /* Mark a composite tvbuff as initialized. No further appends or prepends
182  * occur, data access can finally happen after this finalization. */
183 void tvb_composite_finalize(tvbuff_t* tvb);
184
185
186 /* Get total length of buffer */
187 guint tvb_length(tvbuff_t*);
188
189 /* Computes bytes to end of buffer, from offset (which can be negative,
190  * to indicate bytes from end of buffer). Function returns -1 to
191  * indicate that offset is out of bounds. No exception is thrown. */
192 gint tvb_length_remaining(tvbuff_t*, gint offset);
193
194 /* Checks (w/o throwing exception) that the bytes referred to by 'offset'/'length'
195  * actualy exist in the buffer */
196 gboolean tvb_bytes_exist(tvbuff_t*, gint offset, gint length);
197
198 /* Checks (w/o throwing exception) that offset exists in buffer */
199 gboolean tvb_offset_exists(tvbuff_t*, gint offset);
200
201 /* Get reported length of buffer */
202 guint tvb_reported_length(tvbuff_t*);
203
204 /* Computes bytes of reported packet data to end of buffer, from offset
205  * (which can be negative, to indicate bytes from end of buffer). Function
206  * returns -1 to indicate that offset is out of bounds. No exception is
207  * thrown. */
208 gint tvb_reported_length_remaining(tvbuff_t *tvb, gint offset);
209
210 /* Set the reported length of a tvbuff to a given value; used for protocols
211    whose headers contain an explicit length and where the calling
212    dissector's payload may include padding as well as the packet for
213    this protocol.
214
215    Also adjusts the data length. */
216 void tvb_set_reported_length(tvbuff_t*, guint);
217
218 /* Returns the offset from the first byte of real data. This is
219  * the same value as 'offset' in tvb_compat() */
220 gint tvb_raw_offset(tvbuff_t*);
221
222 /************** START OF ACCESSORS ****************/
223 /* All accessors will throw BoundsError or ReportedBoundsError if appropriate */
224
225 guint8  tvb_get_guint8(tvbuff_t*, gint offset);
226
227 guint16 tvb_get_ntohs(tvbuff_t*, gint offset);
228 guint32 tvb_get_ntoh24(tvbuff_t*, gint offset);
229 guint32 tvb_get_ntohl(tvbuff_t*, gint offset);
230
231 guint16 tvb_get_letohs(tvbuff_t*, gint offset);
232 guint32 tvb_get_letoh24(tvbuff_t*, gint offset);
233 guint32 tvb_get_letohl(tvbuff_t*, gint offset);
234
235 /* Returns target for convenience. Does not suffer from possible
236  * expense of tvb_get_ptr(), since this routine is smart enough
237  * to copy data in chunks if the request range actually exists in
238  * different TVBUFF_REAL_DATA tvbuffs. This function assumes that the
239  * target memory is already allocated; it does not allocate or free the
240  * target memory. */
241 guint8* tvb_memcpy(tvbuff_t*, guint8* target, gint offset, gint length);
242
243 /* It is the user's responsibility to g_free() the memory allocated by
244  * tvb_memdup(). Calls tvb_memcpy() */
245 guint8* tvb_memdup(tvbuff_t*, gint offset, gint length);
246
247 /* WARNING! This function is possibly expensive, temporarily allocating
248  * another copy of the packet data. Furthermore, it's dangerous because once
249  * this pointer is given to the user, there's no guarantee that the user will
250  * honor the 'length' and not overstep the boundaries of the buffer.
251  *
252  * The returned pointer is data that is internal to the tvbuff, so do not
253  * attempt to free it. Don't modify the data, either, because another tvbuff
254  * that might be using this tvbuff may have already copied that portion of
255  * the data (sometimes tvbuff's need to make copies of data, but that's the
256  * internal implementation that you need not worry about). Assume that the
257  * guint8* points to read-only data that the tvbuff manages.
258  *
259  * Return a pointer into our buffer if the data asked for via 'offset'/'length'
260  * is contiguous (which might not be the case for TVBUFF_COMPOSITE). If the
261  * data is not contiguous, a tvb_memdup() is called for the entire buffer
262  * and the pointer to the newly-contiguous data is returned. This dynamically-
263  * allocated memory will be freed when the tvbuff is freed, after the
264  * tvbuff_free_cb_t() is called, if any. */
265 const guint8* tvb_get_ptr(tvbuff_t*, gint offset, gint length);
266
267 /* Find first occurence of any of the needles in tvbuff, starting at offset.
268  * Searches at most maxlength number of bytes; if maxlength is -1, searches
269  * to end of tvbuff.
270  * Returns the offset of the found needle, or -1 if not found.
271  * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
272  * in that case, -1 will be returned if the boundary is reached before
273  * finding needle. */
274 gint tvb_find_guint8(tvbuff_t*, gint offset, gint maxlength, guint8 needle);
275
276 /* Find first occurence of any of the needles in tvbuff, starting at offset.
277  * Searches at most maxlength number of bytes. Returns the offset of the
278  * found needle, or -1 if not found. Will not throw an exception, even if
279  * maxlength exceeds boundary of tvbuff; in that case, -1 will be returned if
280  * the boundary is reached before finding needle. */
281 gint tvb_pbrk_guint8(tvbuff_t *, gint offset, gint maxlength, guint8 *needles);
282
283 /* Find size of stringz (NUL-terminated string) by looking for terminating
284  * NUL.  The size of the string includes the terminating NUL.
285  *
286  * If the NUL isn't found, it throws the appropriate exception.
287  */
288 guint tvb_strsize(tvbuff_t *tvb, gint offset);
289
290 /* Find length of string by looking for end of string ('\0'), up to
291  * 'maxlength' characters'; if 'maxlength' is -1, searches to end
292  * of tvbuff.
293  * Returns -1 if 'maxlength' reached before finding EOS. */
294 gint tvb_strnlen(tvbuff_t*, gint offset, guint maxlength);
295
296 /*
297  * Format the data in the tvb from offset for size ...
298  */
299 guint8 * tvb_format_text(tvbuff_t *tvb, gint offset, gint size);
300
301 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
302  * no more than maxlength number of bytes, including terminating NUL, to buffer.
303  * Returns length of string (not including terminating NUL), or -1 if the string was
304  * truncated in the buffer due to not having reached the terminating NUL.
305  * In this way, it acts like snprintf().
306  */
307 gint tvb_get_nstringz(tvbuff_t *tvb, gint offset, guint maxlength, guint8* buffer);
308
309 /* Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
310  * have a terminating NUL. If the string was truncated when copied into buffer,
311  * a NUL is placed at the end of buffer to terminate it.
312  */
313 gint tvb_get_nstringz0(tvbuff_t *tvb, gint offset, guint maxlength, guint8* buffer);
314
315 /*
316  * Given a tvbuff, an offset into the tvbuff, and a length that starts
317  * at that offset (which may be -1 for "all the way to the end of the
318  * tvbuff"), find the end of the (putative) line that starts at the
319  * specified offset in the tvbuff, going no further than the specified
320  * length.
321  *
322  * Return the offset right past the end of the line as the return value,
323  * and return the offset of the EOL character(s) in "*eol".
324  */
325 gint tvb_find_line_end(tvbuff_t *tvb, gint offset, int len, gint *eol);
326
327 /*
328  * Given a tvbuff, an offset into the tvbuff, and a length that starts
329  * at that offset (which may be -1 for "all the way to the end of the
330  * tvbuff"), find the end of the (putative) line that starts at the
331  * specified offset in the tvbuff, going no further than the specified
332  * length.
333  *
334  * However, treat quoted strings inside the buffer specially - don't
335  * treat newlines in quoted strings as line terminators.
336  *
337  * Return the length of the line (not counting the line terminator at
338  * the end), or the amount of data remaining in the buffer if we don't
339  * find a line terminator.
340  *
341  * Set "*next_offset" to the offset of the character past the line
342  * terminator, or past the end of the buffer if we don't find a line
343  * terminator.
344  */
345 gint tvb_find_line_end_unquoted(tvbuff_t *tvb, gint offset, int len,
346     gint *next_offset);
347
348 /*
349  * Call strncmp after checking if enough chars left, returning 0 if
350  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
351  */
352 gint tvb_strneql(tvbuff_t *tvb, gint offset, const guint8 *str, gint size);
353
354 /*
355  * Call strncasecmp after checking if enough chars left, returning 0 if
356  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
357  */
358 gint tvb_strncaseeql(tvbuff_t *tvb, gint offset, const guint8 *str, gint size);
359
360 /*
361  * Call memcmp after checking if enough chars left, returning 0 if
362  * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
363  */
364 gint tvb_memeql(tvbuff_t *tvb, gint offset, const guint8 *str, gint size);
365
366 /*
367  * Format a bunch of data from a tvbuff as bytes, returning a pointer
368  * to the string with the formatted data.
369  */
370 gchar *tvb_bytes_to_str(tvbuff_t *tvb, gint offset, gint len);
371
372 gchar *tvb_get_name(tvbuff_t *tvb);
373
374 /************** END OF ACCESSORS ****************/
375
376 /* Sets pd and offset so that tvbuff's can be used with code
377  * that only understands pd/offset and not tvbuffs.
378  * This is the "compatibility" function */
379 void tvb_compat(tvbuff_t*, const guint8 **pd, int *offset);
380
381 #define tvb_create_from_top(offset) \
382         tvb_new_subset(pi.compat_top_tvb, (offset), \
383                                 pi.captured_len - (offset), pi.len - (offset))
384
385 #endif /* __TVBUFF_H__ */