Witness: add pidl output
[wireshark-wip.git] / epan / tvbuff.c
blobc886e8568eb3950d3b9de45590a28b8568188be9
1 /* tvbuff.c
3 * Testy, Virtual(-izable) Buffer of guint8*'s
5 * "Testy" -- the buffer gets mad when an attempt to access data
6 * beyond the bounds of the buffer. An exception is thrown.
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.
12 * $Id$
14 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
16 * Code to convert IEEE floating point formats to native floating point
17 * derived from code Copyright (c) Ashok Narayanan, 2000
19 * Wireshark - Network traffic analyzer
20 * By Gerald Combs <gerald@wireshark.org>
21 * Copyright 1998 Gerald Combs
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38 #include "config.h"
40 #include <string.h>
42 #ifdef HAVE_LIBZ
43 #include <zlib.h>
44 #endif
46 #include "wsutil/pint.h"
47 #include "tvbuff.h"
48 #include "tvbuff-int.h"
49 #include "strutil.h"
50 #include "to_str.h"
51 #include "charsets.h"
52 #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
53 #include "exceptions.h"
55 static guint64
56 _tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint total_no_of_bits);
58 tvbuff_t *
59 tvb_new(const struct tvb_ops *ops)
61 tvbuff_t *tvb;
62 gsize size = ops->tvb_size;
64 g_assert(size >= sizeof(*tvb));
66 tvb = (tvbuff_t *) g_slice_alloc(size);
68 tvb->next = NULL;
69 tvb->ops = ops;
70 tvb->initialized = FALSE;
71 tvb->flags = 0;
72 tvb->length = 0;
73 tvb->reported_length = 0;
74 tvb->real_data = NULL;
75 tvb->raw_offset = -1;
76 tvb->ds_tvb = NULL;
78 return tvb;
81 static void
82 tvb_free_internal(tvbuff_t *tvb)
84 gsize size;
86 DISSECTOR_ASSERT(tvb);
88 if (tvb->ops->tvb_free)
89 tvb->ops->tvb_free(tvb);
91 size = tvb->ops->tvb_size;
93 g_slice_free1(size, tvb);
96 /* XXX: just call tvb_free_chain();
97 * Not removed so that existing dissectors using tvb_free() need not be changed.
98 * I'd argue that existing calls to tvb_free() should have actually beeen
99 * calls to tvb_free_chain() although the calls were OK as long as no
100 * subsets, etc had been created on the tvb. */
101 void
102 tvb_free(tvbuff_t *tvb)
104 tvb_free_chain(tvb);
107 void
108 tvb_free_chain(tvbuff_t *tvb)
110 tvbuff_t *next_tvb;
111 DISSECTOR_ASSERT(tvb);
112 while (tvb) {
113 next_tvb=tvb->next;
114 tvb_free_internal(tvb);
115 tvb = next_tvb;
119 tvbuff_t *
120 tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing)
122 tvbuff_t *tvb = tvb_new_proxy(backing);
124 tvb_add_to_chain(parent, tvb);
125 return tvb;
128 void
129 tvb_add_to_chain(tvbuff_t *parent, tvbuff_t *child)
131 tvbuff_t *tmp = child;
133 DISSECTOR_ASSERT(parent);
134 DISSECTOR_ASSERT(child);
136 while (child) {
137 tmp = child;
138 child = child->next;
140 tmp->next = parent->next;
141 parent->next = tmp;
146 * Check whether that offset goes more than one byte past the
147 * end of the buffer.
149 * If not, return 0; otherwise, return exception
151 static inline int
152 validate_offset(const tvbuff_t *tvb, const guint abs_offset)
154 if (G_LIKELY(abs_offset <= tvb->length))
155 return 0;
156 else if (abs_offset <= tvb->reported_length)
157 return BoundsError;
158 else if (tvb->flags & TVBUFF_FRAGMENT)
159 return FragmentBoundsError;
160 else
161 return ReportedBoundsError;
164 static int
165 compute_offset(const tvbuff_t *tvb, const gint offset, guint *offset_ptr)
167 if (offset >= 0) {
168 /* Positive offset - relative to the beginning of the packet. */
169 if ((guint) offset <= tvb->length) {
170 *offset_ptr = offset;
171 } else if ((guint) offset <= tvb->reported_length) {
172 return BoundsError;
173 } else if (tvb->flags & TVBUFF_FRAGMENT) {
174 return FragmentBoundsError;
175 } else {
176 return ReportedBoundsError;
179 else {
180 /* Negative offset - relative to the end of the packet. */
181 if ((guint) -offset <= tvb->length) {
182 *offset_ptr = tvb->length + offset;
183 } else if ((guint) -offset <= tvb->reported_length) {
184 return BoundsError;
185 } else if (tvb->flags & TVBUFF_FRAGMENT) {
186 return FragmentBoundsError;
187 } else {
188 return ReportedBoundsError;
192 return 0;
195 static int
196 compute_offset_and_remaining(const tvbuff_t *tvb, const gint offset, guint *offset_ptr, guint *rem_len)
198 int exception;
200 exception = compute_offset(tvb, offset, offset_ptr);
201 if (!exception)
202 *rem_len = tvb->length - *offset_ptr;
204 return exception;
207 /* Computes the absolute offset and length based on a possibly-negative offset
208 * and a length that is possible -1 (which means "to the end of the data").
209 * Returns integer indicating whether the offset is in bounds (0) or
210 * not (exception number). The integer ptrs are modified with the new offset and length.
211 * No exception is thrown.
213 * XXX - we return success (0), if the offset is positive and right
214 * after the end of the tvbuff (i.e., equal to the length). We do this
215 * so that a dissector constructing a subset tvbuff for the next protocol
216 * will get a zero-length tvbuff, not an exception, if there's no data
217 * left for the next protocol - we want the next protocol to be the one
218 * that gets an exception, so the error is reported as an error in that
219 * protocol rather than the containing protocol. */
220 static int
221 check_offset_length_no_exception(const tvbuff_t *tvb,
222 const gint offset, gint const length_val,
223 guint *offset_ptr, guint *length_ptr)
225 guint end_offset;
226 int exception;
228 DISSECTOR_ASSERT(offset_ptr);
229 DISSECTOR_ASSERT(length_ptr);
231 /* Compute the offset */
232 exception = compute_offset(tvb, offset, offset_ptr);
233 if (exception)
234 return exception;
236 if (length_val < -1) {
237 /* XXX - ReportedBoundsError? */
238 return BoundsError;
241 /* Compute the length */
242 if (length_val == -1)
243 *length_ptr = tvb->length - *offset_ptr;
244 else
245 *length_ptr = length_val;
248 * Compute the offset of the first byte past the length.
250 end_offset = *offset_ptr + *length_ptr;
253 * Check for an overflow
255 if (end_offset < *offset_ptr)
256 return BoundsError;
258 return validate_offset(tvb, end_offset);
261 /* Checks (+/-) offset and length and throws an exception if
262 * either is out of bounds. Sets integer ptrs to the new offset
263 * and length. */
264 static void
265 check_offset_length(const tvbuff_t *tvb,
266 const gint offset, gint const length_val,
267 guint *offset_ptr, guint *length_ptr)
269 int exception;
271 exception = check_offset_length_no_exception(tvb, offset, length_val, offset_ptr, length_ptr);
272 if (exception)
273 THROW(exception);
276 void
277 tvb_check_offset_length(const tvbuff_t *tvb,
278 const gint offset, gint const length_val,
279 guint *offset_ptr, guint *length_ptr)
281 check_offset_length(tvb, offset, length_val, offset_ptr, length_ptr);
284 static const unsigned char left_aligned_bitmask[] = {
285 0xff,
286 0x80,
287 0xc0,
288 0xe0,
289 0xf0,
290 0xf8,
291 0xfc,
292 0xfe
295 tvbuff_t *
296 tvb_new_octet_aligned(tvbuff_t *tvb, guint32 bit_offset, gint32 no_of_bits)
298 tvbuff_t *sub_tvb = NULL;
299 guint32 byte_offset;
300 gint32 datalen, i;
301 guint8 left, right, remaining_bits, *buf;
302 const guint8 *data;
304 byte_offset = bit_offset >> 3;
305 left = bit_offset % 8; /* for left-shifting */
306 right = 8 - left; /* for right-shifting */
308 if (no_of_bits == -1) {
309 datalen = tvb_length_remaining(tvb, byte_offset);
310 remaining_bits = 0;
311 } else {
312 datalen = no_of_bits >> 3;
313 remaining_bits = no_of_bits % 8;
314 if (remaining_bits) {
315 datalen++;
319 /* already aligned -> shortcut */
320 if ((left == 0) && (remaining_bits == 0)) {
321 return tvb_new_subset(tvb, byte_offset, datalen, -1);
324 DISSECTOR_ASSERT(datalen>0);
326 /* if at least one trailing byte is available, we must use the content
327 * of that byte for the last shift (i.e. tvb_get_ptr() must use datalen + 1
328 * if non extra byte is available, the last shifted byte requires
329 * special treatment
331 if (tvb_length_remaining(tvb, byte_offset) > datalen) {
332 data = tvb_get_ptr(tvb, byte_offset, datalen + 1);
334 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
335 buf = (guint8 *)g_malloc(datalen);
337 /* shift tvb data bit_offset bits to the left */
338 for (i = 0; i < datalen; i++)
339 buf[i] = (data[i] << left) | (data[i+1] >> right);
340 } else {
341 data = tvb_get_ptr(tvb, byte_offset, datalen);
343 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
344 buf = (guint8 *)g_malloc(datalen);
346 /* shift tvb data bit_offset bits to the left */
347 for (i = 0; i < (datalen-1); i++)
348 buf[i] = (data[i] << left) | (data[i+1] >> right);
349 buf[datalen-1] = data[datalen-1] << left; /* set last octet */
351 buf[datalen-1] &= left_aligned_bitmask[remaining_bits];
353 sub_tvb = tvb_new_child_real_data(tvb, buf, datalen, datalen);
354 tvb_set_free_cb(sub_tvb, g_free);
356 return sub_tvb;
359 static tvbuff_t *
360 tvb_generic_clone_offset_len(tvbuff_t *tvb, guint offset, guint len)
362 tvbuff_t *cloned_tvb;
364 guint8 *data = (guint8 *) g_malloc(len);
366 tvb_memcpy(tvb, data, offset, len);
368 cloned_tvb = tvb_new_real_data(data, len, len);
369 tvb_set_free_cb(cloned_tvb, g_free);
371 return cloned_tvb;
374 tvbuff_t *
375 tvb_clone_offset_len(tvbuff_t *tvb, guint offset, guint len)
377 if (tvb->ops->tvb_clone) {
378 tvbuff_t *cloned_tvb;
380 cloned_tvb = tvb->ops->tvb_clone(tvb, offset, len);
381 if (cloned_tvb)
382 return cloned_tvb;
385 return tvb_generic_clone_offset_len(tvb, offset, len);
388 tvbuff_t *
389 tvb_clone(tvbuff_t *tvb)
391 return tvb_clone_offset_len(tvb, 0, tvb->length);
394 guint
395 tvb_length(const tvbuff_t *tvb)
397 DISSECTOR_ASSERT(tvb && tvb->initialized);
399 return tvb->length;
402 gint
403 tvb_length_remaining(const tvbuff_t *tvb, const gint offset)
405 guint abs_offset, rem_length;
406 int exception;
408 DISSECTOR_ASSERT(tvb && tvb->initialized);
410 exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
411 if (exception)
412 return 0;
414 return rem_length;
417 guint
418 tvb_ensure_length_remaining(const tvbuff_t *tvb, const gint offset)
420 guint abs_offset, rem_length;
421 int exception;
423 DISSECTOR_ASSERT(tvb && tvb->initialized);
425 exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
426 if (exception)
427 THROW(exception);
429 if (rem_length == 0) {
431 * This routine ensures there's at least one byte available.
432 * There aren't any bytes available, so throw the appropriate
433 * exception.
435 if (abs_offset >= tvb->reported_length) {
436 if (tvb->flags & TVBUFF_FRAGMENT) {
437 THROW(FragmentBoundsError);
438 } else {
439 THROW(ReportedBoundsError);
441 } else
442 THROW(BoundsError);
444 return rem_length;
450 /* Validates that 'length' bytes are available starting from
451 * offset (pos/neg). Does not throw an exception. */
452 gboolean
453 tvb_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
455 guint abs_offset, abs_length;
456 int exception;
458 DISSECTOR_ASSERT(tvb && tvb->initialized);
460 exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
461 if (exception)
462 return FALSE;
464 return TRUE;
467 /* Validates that 'length' bytes are available starting from
468 * offset (pos/neg). Throws an exception if they aren't. */
469 void
470 tvb_ensure_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
472 guint real_offset, end_offset;
474 DISSECTOR_ASSERT(tvb && tvb->initialized);
477 * -1 doesn't mean "until end of buffer", as that's pointless
478 * for this routine. We must treat it as a Really Large Positive
479 * Number, so that we throw an exception; we throw
480 * ReportedBoundsError, as if it were past even the end of a
481 * reassembled packet, and past the end of even the data we
482 * didn't capture.
484 * We do the same with other negative lengths.
486 if (length < 0) {
487 THROW(ReportedBoundsError);
490 /* XXX: Below this point could be replaced with a call to
491 * check_offset_length with no functional change, however this is a
492 * *very* hot path and check_offset_length is not well-optimized for
493 * this case, so we eat some code duplication for a lot of speedup. */
495 if (offset >= 0) {
496 /* Positive offset - relative to the beginning of the packet. */
497 if ((guint) offset <= tvb->length) {
498 real_offset = offset;
499 } else if ((guint) offset <= tvb->reported_length) {
500 THROW(BoundsError);
501 } else if (tvb->flags & TVBUFF_FRAGMENT) {
502 THROW(FragmentBoundsError);
503 } else {
504 THROW(ReportedBoundsError);
507 else {
508 /* Negative offset - relative to the end of the packet. */
509 if ((guint) -offset <= tvb->length) {
510 real_offset = tvb->length + offset;
511 } else if ((guint) -offset <= tvb->reported_length) {
512 THROW(BoundsError);
513 } else if (tvb->flags & TVBUFF_FRAGMENT) {
514 THROW(FragmentBoundsError);
515 } else {
516 THROW(ReportedBoundsError);
521 * Compute the offset of the first byte past the length.
523 end_offset = real_offset + length;
526 * Check for an overflow
528 if (end_offset < real_offset)
529 THROW(BoundsError);
531 if (G_LIKELY(end_offset <= tvb->length))
532 return;
533 else if (end_offset <= tvb->reported_length)
534 THROW(BoundsError);
535 else if (tvb->flags & TVBUFF_FRAGMENT)
536 THROW(FragmentBoundsError);
537 else
538 THROW(ReportedBoundsError);
541 gboolean
542 tvb_offset_exists(const tvbuff_t *tvb, const gint offset)
544 guint abs_offset;
545 int exception;
547 DISSECTOR_ASSERT(tvb && tvb->initialized);
549 exception = compute_offset(tvb, offset, &abs_offset);
550 if (exception)
551 return FALSE;
553 /* compute_offset only throws an exception on >, not >= because of the
554 * comment above check_offset_length_no_exception, but here we want the
555 * opposite behaviour so we check ourselves... */
556 if (abs_offset < tvb->length) {
557 return TRUE;
559 else {
560 return FALSE;
564 guint
565 tvb_reported_length(const tvbuff_t *tvb)
567 DISSECTOR_ASSERT(tvb && tvb->initialized);
569 return tvb->reported_length;
572 gint
573 tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset)
575 guint abs_offset;
576 int exception;
578 DISSECTOR_ASSERT(tvb && tvb->initialized);
580 exception = compute_offset(tvb, offset, &abs_offset);
581 if (exception)
582 return 0;
584 if (tvb->reported_length >= abs_offset)
585 return tvb->reported_length - abs_offset;
586 else
587 return 0;
590 /* Set the reported length of a tvbuff to a given value; used for protocols
591 * whose headers contain an explicit length and where the calling
592 * dissector's payload may include padding as well as the packet for
593 * this protocol.
594 * Also adjusts the data length. */
595 void
596 tvb_set_reported_length(tvbuff_t *tvb, const guint reported_length)
598 DISSECTOR_ASSERT(tvb && tvb->initialized);
600 if (reported_length > tvb->reported_length)
601 THROW(ReportedBoundsError);
603 tvb->reported_length = reported_length;
604 if (reported_length < tvb->length)
605 tvb->length = reported_length;
608 guint
609 tvb_offset_from_real_beginning_counter(const tvbuff_t *tvb, const guint counter)
611 if (tvb->ops->tvb_offset)
612 return tvb->ops->tvb_offset(tvb, counter);
614 DISSECTOR_ASSERT_NOT_REACHED();
615 return 0;
618 guint
619 tvb_offset_from_real_beginning(const tvbuff_t *tvb)
621 return tvb_offset_from_real_beginning_counter(tvb, 0);
624 static const guint8*
625 ensure_contiguous_no_exception(tvbuff_t *tvb, const gint offset, const gint length, int *pexception)
627 guint abs_offset, abs_length;
628 int exception;
630 exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
631 if (exception) {
632 if (pexception)
633 *pexception = exception;
634 return NULL;
638 * We know that all the data is present in the tvbuff, so
639 * no exceptions should be thrown.
641 if (tvb->real_data)
642 return tvb->real_data + abs_offset;
644 if (tvb->ops->tvb_get_ptr)
645 return tvb->ops->tvb_get_ptr(tvb, abs_offset, abs_length);
647 DISSECTOR_ASSERT_NOT_REACHED();
648 return NULL;
651 static const guint8*
652 ensure_contiguous(tvbuff_t *tvb, const gint offset, const gint length)
654 int exception = 0;
655 const guint8 *p;
657 p = ensure_contiguous_no_exception(tvb, offset, length, &exception);
658 if (p == NULL) {
659 DISSECTOR_ASSERT(exception > 0);
660 THROW(exception);
662 return p;
665 static const guint8*
666 fast_ensure_contiguous(tvbuff_t *tvb, const gint offset, const guint length)
668 guint end_offset;
669 guint u_offset;
671 DISSECTOR_ASSERT(tvb && tvb->initialized);
672 /* We don't check for overflow in this fast path so we only handle simple types */
673 DISSECTOR_ASSERT(length <= 8);
675 if (offset < 0 || !tvb->real_data) {
676 return ensure_contiguous(tvb, offset, length);
679 u_offset = offset;
680 end_offset = u_offset + length;
682 if (end_offset <= tvb->length) {
683 return tvb->real_data + u_offset;
686 if (end_offset > tvb->reported_length) {
687 if (tvb->flags & TVBUFF_FRAGMENT) {
688 THROW(FragmentBoundsError);
689 } else {
690 THROW(ReportedBoundsError);
692 /* not reached */
694 THROW(BoundsError);
695 /* not reached */
696 return NULL;
699 static const guint8*
700 guint8_pbrk(const guint8* haystack, size_t haystacklen, const guint8 *needles, guchar *found_needle)
702 gchar tmp[256] = { 0 };
703 const guint8 *haystack_end;
705 while (*needles)
706 tmp[*needles++] = 1;
708 haystack_end = haystack + haystacklen;
709 while (haystack < haystack_end) {
710 if (tmp[*haystack]) {
711 if (found_needle)
712 *found_needle = *haystack;
713 return haystack;
715 haystack++;
718 return NULL;
723 /************** ACCESSORS **************/
725 void *
726 tvb_memcpy(tvbuff_t *tvb, void *target, const gint offset, size_t length)
728 guint abs_offset, abs_length;
730 DISSECTOR_ASSERT(tvb && tvb->initialized);
733 * XXX - we should eliminate the "length = -1 means 'to the end
734 * of the tvbuff'" convention, and use other means to achieve
735 * that; this would let us eliminate a bunch of checks for
736 * negative lengths in cases where the protocol has a 32-bit
737 * length field.
739 * Allowing -1 but throwing an assertion on other negative
740 * lengths is a bit more work with the length being a size_t;
741 * instead, we check for a length <= 2^31-1.
743 DISSECTOR_ASSERT(length <= 0x7FFFFFFF);
744 check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);
746 if (tvb->real_data) {
747 return memcpy(target, tvb->real_data + abs_offset, abs_length);
750 if (tvb->ops->tvb_memcpy)
751 return tvb->ops->tvb_memcpy(tvb, target, abs_offset, abs_length);
753 /* XXX, fallback to slower method */
755 DISSECTOR_ASSERT_NOT_REACHED();
756 return NULL;
761 * XXX - this doesn't treat a length of -1 as an error.
762 * If it did, this could replace some code that calls
763 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
764 * data to it.
766 * "composite_get_ptr()" depends on -1 not being
767 * an error; does anything else depend on this routine treating -1 as
768 * meaning "to the end of the buffer"?
770 * If scope is NULL, memory is allocated with g_malloc() and user must
771 * explicitely free it with g_free().
772 * If scope is not NULL, memory is allocated with the corresponding pool
773 * lifetime.
775 void *
776 tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, size_t length)
778 guint abs_offset, abs_length;
779 void *duped;
781 DISSECTOR_ASSERT(tvb && tvb->initialized);
783 check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);
785 duped = wmem_alloc(scope, abs_length);
786 return tvb_memcpy(tvb, duped, abs_offset, abs_length);
791 const guint8*
792 tvb_get_ptr(tvbuff_t *tvb, const gint offset, const gint length)
794 return ensure_contiguous(tvb, offset, length);
797 /* ---------------- */
798 guint8
799 tvb_get_guint8(tvbuff_t *tvb, const gint offset)
801 const guint8 *ptr;
803 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint8));
804 return *ptr;
807 guint16
808 tvb_get_ntohs(tvbuff_t *tvb, const gint offset)
810 const guint8 *ptr;
812 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
813 return pntohs(ptr);
816 guint32
817 tvb_get_ntoh24(tvbuff_t *tvb, const gint offset)
819 const guint8 *ptr;
821 ptr = fast_ensure_contiguous(tvb, offset, 3);
822 return pntoh24(ptr);
825 guint32
826 tvb_get_ntohl(tvbuff_t *tvb, const gint offset)
828 const guint8 *ptr;
830 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
831 return pntohl(ptr);
834 guint64
835 tvb_get_ntoh40(tvbuff_t *tvb, const gint offset)
837 const guint8 *ptr;
839 ptr = fast_ensure_contiguous(tvb, offset, 5);
840 return pntoh40(ptr);
843 guint64
844 tvb_get_ntoh48(tvbuff_t *tvb, const gint offset)
846 const guint8 *ptr;
848 ptr = fast_ensure_contiguous(tvb, offset, 6);
849 return pntoh48(ptr);
852 guint64
853 tvb_get_ntoh56(tvbuff_t *tvb, const gint offset)
855 const guint8 *ptr;
857 ptr = fast_ensure_contiguous(tvb, offset, 7);
858 return pntoh56(ptr);
861 guint64
862 tvb_get_ntoh64(tvbuff_t *tvb, const gint offset)
864 const guint8 *ptr;
866 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
867 return pntoh64(ptr);
871 * Stuff for IEEE float handling on platforms that don't have IEEE
872 * format as the native floating-point format.
874 * For now, we treat only the VAX as such a platform.
876 * XXX - other non-IEEE boxes that can run UNIX include some Crays,
877 * and possibly other machines.
879 * It appears that the official Linux port to System/390 and
880 * zArchitecture uses IEEE format floating point (not a
881 * huge surprise).
883 * I don't know whether there are any other machines that
884 * could run Wireshark and that don't use IEEE format.
885 * As far as I know, all of the main commercial microprocessor
886 * families on which OSes that support Wireshark can run
887 * use IEEE format (x86, 68k, SPARC, MIPS, PA-RISC, Alpha,
888 * IA-64, and so on).
891 #if defined(vax)
893 #include <math.h>
896 * Single-precision.
898 #define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
899 #define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
900 #define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
902 #define IEEE_SP_SIGN_MASK 0x80000000
903 #define IEEE_SP_EXPONENT_MASK 0x7F800000
904 #define IEEE_SP_MANTISSA_MASK 0x007FFFFF
905 #define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
907 #define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
908 #define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
909 #define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
911 static int
912 ieee_float_is_zero(const guint32 w)
914 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
917 static gfloat
918 get_ieee_float(const guint32 w)
920 long sign;
921 long exponent;
922 long mantissa;
924 sign = w & IEEE_SP_SIGN_MASK;
925 exponent = w & IEEE_SP_EXPONENT_MASK;
926 mantissa = w & IEEE_SP_MANTISSA_MASK;
928 if (ieee_float_is_zero(w)) {
929 /* number is zero, unnormalized, or not-a-number */
930 return 0.0;
932 #if 0
934 * XXX - how to handle this?
936 if (IEEE_SP_INFINITY == exponent) {
938 * number is positive or negative infinity, or a special value
940 return (sign? MINUS_INFINITY: PLUS_INFINITY);
942 #endif
944 exponent = ((exponent >> IEEE_SP_MANTISSA_WIDTH) - IEEE_SP_BIAS) -
945 IEEE_SP_MANTISSA_WIDTH;
946 mantissa |= IEEE_SP_IMPLIED_BIT;
948 if (sign)
949 return -mantissa * pow(2, exponent);
950 else
951 return mantissa * pow(2, exponent);
955 * Double-precision.
956 * We assume that if you don't have IEEE floating-point, you have a
957 * compiler that understands 64-bit integral quantities.
959 #define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
960 #define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
961 #define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
963 #define IEEE_DP_SIGN_MASK 0x8000000000000000LL
964 #define IEEE_DP_EXPONENT_MASK 0x7FF0000000000000LL
965 #define IEEE_DP_MANTISSA_MASK 0x000FFFFFFFFFFFFFLL
966 #define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
968 #define IEEE_DP_IMPLIED_BIT (1LL << IEEE_DP_MANTISSA_WIDTH)
969 #define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
970 #define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
972 static int
973 ieee_double_is_zero(const guint64 w)
975 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
978 static gdouble
979 get_ieee_double(const guint64 w)
981 gint64 sign;
982 gint64 exponent;
983 gint64 mantissa;
985 sign = w & IEEE_DP_SIGN_MASK;
986 exponent = w & IEEE_DP_EXPONENT_MASK;
987 mantissa = w & IEEE_DP_MANTISSA_MASK;
989 if (ieee_double_is_zero(w)) {
990 /* number is zero, unnormalized, or not-a-number */
991 return 0.0;
993 #if 0
995 * XXX - how to handle this?
997 if (IEEE_DP_INFINITY == exponent) {
999 * number is positive or negative infinity, or a special value
1001 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1003 #endif
1005 exponent = ((exponent >> IEEE_DP_MANTISSA_WIDTH) - IEEE_DP_BIAS) -
1006 IEEE_DP_MANTISSA_WIDTH;
1007 mantissa |= IEEE_DP_IMPLIED_BIT;
1009 if (sign)
1010 return -mantissa * pow(2, exponent);
1011 else
1012 return mantissa * pow(2, exponent);
1014 #endif
1017 * Fetches an IEEE single-precision floating-point number, in
1018 * big-endian form, and returns a "float".
1020 * XXX - should this be "double", in case there are IEEE single-
1021 * precision numbers that won't fit in some platform's native
1022 * "float" format?
1024 gfloat
1025 tvb_get_ntohieee_float(tvbuff_t *tvb, const int offset)
1027 #if defined(vax)
1028 return get_ieee_float(tvb_get_ntohl(tvb, offset));
1029 #else
1030 union {
1031 gfloat f;
1032 guint32 w;
1033 } ieee_fp_union;
1035 ieee_fp_union.w = tvb_get_ntohl(tvb, offset);
1036 return ieee_fp_union.f;
1037 #endif
1041 * Fetches an IEEE double-precision floating-point number, in
1042 * big-endian form, and returns a "double".
1044 gdouble
1045 tvb_get_ntohieee_double(tvbuff_t *tvb, const int offset)
1047 #if defined(vax)
1048 union {
1049 guint32 w[2];
1050 guint64 dw;
1051 } ieee_fp_union;
1052 #else
1053 union {
1054 gdouble d;
1055 guint32 w[2];
1056 } ieee_fp_union;
1057 #endif
1059 #ifdef WORDS_BIGENDIAN
1060 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset);
1061 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset+4);
1062 #else
1063 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset+4);
1064 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset);
1065 #endif
1066 #if defined(vax)
1067 return get_ieee_double(ieee_fp_union.dw);
1068 #else
1069 return ieee_fp_union.d;
1070 #endif
1073 guint16
1074 tvb_get_letohs(tvbuff_t *tvb, const gint offset)
1076 const guint8 *ptr;
1078 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
1079 return pletohs(ptr);
1082 guint32
1083 tvb_get_letoh24(tvbuff_t *tvb, const gint offset)
1085 const guint8 *ptr;
1087 ptr = fast_ensure_contiguous(tvb, offset, 3);
1088 return pletoh24(ptr);
1091 guint32
1092 tvb_get_letohl(tvbuff_t *tvb, const gint offset)
1094 const guint8 *ptr;
1096 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1097 return pletohl(ptr);
1100 guint64
1101 tvb_get_letoh40(tvbuff_t *tvb, const gint offset)
1103 const guint8 *ptr;
1105 ptr = fast_ensure_contiguous(tvb, offset, 5);
1106 return pletoh40(ptr);
1109 guint64
1110 tvb_get_letoh48(tvbuff_t *tvb, const gint offset)
1112 const guint8 *ptr;
1114 ptr = fast_ensure_contiguous(tvb, offset, 6);
1115 return pletoh48(ptr);
1118 guint64
1119 tvb_get_letoh56(tvbuff_t *tvb, const gint offset)
1121 const guint8 *ptr;
1123 ptr = fast_ensure_contiguous(tvb, offset, 7);
1124 return pletoh56(ptr);
1127 guint64
1128 tvb_get_letoh64(tvbuff_t *tvb, const gint offset)
1130 const guint8 *ptr;
1132 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
1133 return pletoh64(ptr);
1137 * Fetches an IEEE single-precision floating-point number, in
1138 * little-endian form, and returns a "float".
1140 * XXX - should this be "double", in case there are IEEE single-
1141 * precision numbers that won't fit in some platform's native
1142 * "float" format?
1144 gfloat
1145 tvb_get_letohieee_float(tvbuff_t *tvb, const int offset)
1147 #if defined(vax)
1148 return get_ieee_float(tvb_get_letohl(tvb, offset));
1149 #else
1150 union {
1151 gfloat f;
1152 guint32 w;
1153 } ieee_fp_union;
1155 ieee_fp_union.w = tvb_get_letohl(tvb, offset);
1156 return ieee_fp_union.f;
1157 #endif
1161 * Fetches an IEEE double-precision floating-point number, in
1162 * little-endian form, and returns a "double".
1164 gdouble
1165 tvb_get_letohieee_double(tvbuff_t *tvb, const int offset)
1167 #if defined(vax)
1168 union {
1169 guint32 w[2];
1170 guint64 dw;
1171 } ieee_fp_union;
1172 #else
1173 union {
1174 gdouble d;
1175 guint32 w[2];
1176 } ieee_fp_union;
1177 #endif
1179 #ifdef WORDS_BIGENDIAN
1180 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset+4);
1181 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset);
1182 #else
1183 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset);
1184 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset+4);
1185 #endif
1186 #if defined(vax)
1187 return get_ieee_double(ieee_fp_union.dw);
1188 #else
1189 return ieee_fp_union.d;
1190 #endif
1193 /* Fetch an IPv4 address, in network byte order.
1194 * We do *not* convert them to host byte order; we leave them in
1195 * network byte order. */
1196 guint32
1197 tvb_get_ipv4(tvbuff_t *tvb, const gint offset)
1199 const guint8 *ptr;
1200 guint32 addr;
1202 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1203 memcpy(&addr, ptr, sizeof addr);
1204 return addr;
1207 /* Fetch an IPv6 address. */
1208 void
1209 tvb_get_ipv6(tvbuff_t *tvb, const gint offset, struct e_in6_addr *addr)
1211 const guint8 *ptr;
1213 ptr = ensure_contiguous(tvb, offset, sizeof(*addr));
1214 memcpy(addr, ptr, sizeof *addr);
1217 /* Fetch a GUID. */
1218 void
1219 tvb_get_ntohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid)
1221 const guint8 *ptr = ensure_contiguous(tvb, offset, GUID_LEN);
1223 guid->data1 = pntohl(ptr + 0);
1224 guid->data2 = pntohs(ptr + 4);
1225 guid->data3 = pntohs(ptr + 6);
1226 memcpy(guid->data4, ptr + 8, sizeof guid->data4);
1229 void
1230 tvb_get_letohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid)
1232 const guint8 *ptr = ensure_contiguous(tvb, offset, GUID_LEN);
1234 guid->data1 = pletohl(ptr + 0);
1235 guid->data2 = pletohs(ptr + 4);
1236 guid->data3 = pletohs(ptr + 6);
1237 memcpy(guid->data4, ptr + 8, sizeof guid->data4);
1241 * NOTE: to support code written when proto_tree_add_item() took a
1242 * gboolean as its last argument, with FALSE meaning "big-endian"
1243 * and TRUE meaning "little-endian", we treat any non-zero value of
1244 * "representation" as meaning "little-endian".
1246 void
1247 tvb_get_guid(tvbuff_t *tvb, const gint offset, e_guid_t *guid, const guint representation)
1249 if (representation) {
1250 tvb_get_letohguid(tvb, offset, guid);
1251 } else {
1252 tvb_get_ntohguid(tvb, offset, guid);
1256 static const guint8 inverse_bit_mask8[] = {
1257 0xff,
1258 0x7f,
1259 0x3f,
1260 0x1f,
1261 0x0f,
1262 0x07,
1263 0x03,
1264 0x01
1267 static const guint8 bit_mask8[] = {
1268 0x00,
1269 0x01,
1270 0x03,
1271 0x07,
1272 0x0f,
1273 0x1f,
1274 0x3f,
1275 0x7f,
1276 0xff
1279 /* Get 1 - 8 bits */
1280 guint8
1281 tvb_get_bits8(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits)
1283 return (guint8)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
1286 /* Get 9 - 16 bits */
1287 guint16
1288 tvb_get_bits16(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits,const guint encoding _U_)
1290 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1291 return (guint16)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
1294 /* Get 1 - 32 bits */
1295 guint32
1296 tvb_get_bits32(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding _U_)
1298 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1299 return (guint32)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
1302 /* Get 1 - 64 bits */
1303 guint64
1304 tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding _U_)
1306 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1307 return _tvb_get_bits64(tvb, bit_offset, no_of_bits);
1310 * This function will dissect a sequence of bits that does not need to be byte aligned; the bits
1311 * set will be shown in the tree as ..10 10.. and the integer value returned if return_value is set.
1312 * Offset should be given in bits from the start of the tvb.
1313 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
1315 static guint64
1316 _tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint total_no_of_bits)
1318 guint64 value;
1319 guint octet_offset = bit_offset >> 3;
1320 guint8 required_bits_in_first_octet = 8 - (bit_offset % 8);
1322 if(required_bits_in_first_octet > total_no_of_bits)
1324 /* the required bits don't extend to the end of the first octet */
1325 guint8 right_shift = required_bits_in_first_octet - total_no_of_bits;
1326 value = (tvb_get_guint8(tvb, octet_offset) >> right_shift) & bit_mask8[total_no_of_bits % 8];
1328 else
1330 guint8 remaining_bit_length = total_no_of_bits;
1332 /* get the bits up to the first octet boundary */
1333 value = 0;
1334 required_bits_in_first_octet %= 8;
1335 if(required_bits_in_first_octet != 0)
1337 value = tvb_get_guint8(tvb, octet_offset) & bit_mask8[required_bits_in_first_octet];
1338 remaining_bit_length -= required_bits_in_first_octet;
1339 octet_offset ++;
1341 /* take the biggest words, shorts or octets that we can */
1342 while (remaining_bit_length > 7)
1344 switch (remaining_bit_length >> 4)
1346 case 0:
1347 /* 8 - 15 bits. (note that 0 - 7 would have dropped out of the while() loop) */
1348 value <<= 8;
1349 value += tvb_get_guint8(tvb, octet_offset);
1350 remaining_bit_length -= 8;
1351 octet_offset ++;
1352 break;
1354 case 1:
1355 /* 16 - 31 bits */
1356 value <<= 16;
1357 value += tvb_get_ntohs(tvb, octet_offset);
1358 remaining_bit_length -= 16;
1359 octet_offset += 2;
1360 break;
1362 case 2:
1363 case 3:
1364 /* 32 - 63 bits */
1365 value <<= 32;
1366 value += tvb_get_ntohl(tvb, octet_offset);
1367 remaining_bit_length -= 32;
1368 octet_offset += 4;
1369 break;
1371 default:
1372 /* 64 bits (or more???) */
1373 value = tvb_get_ntoh64(tvb, octet_offset);
1374 remaining_bit_length -= 64;
1375 octet_offset += 8;
1376 break;
1379 /* get bits from any partial octet at the tail */
1380 if(remaining_bit_length)
1382 value <<= remaining_bit_length;
1383 value += (tvb_get_guint8(tvb, octet_offset) >> (8 - remaining_bit_length));
1386 return value;
1388 /* Get 1 - 32 bits (should be deprecated as same as tvb_get_bits32??) */
1389 guint32
1390 tvb_get_bits(tvbuff_t *tvb, const guint bit_offset, const gint no_of_bits, const guint encoding _U_)
1392 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1393 return (guint32)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
1396 static gint
1397 tvb_find_guint8_generic(tvbuff_t *tvb, guint abs_offset, guint limit, guint8 needle)
1399 const guint8 *ptr;
1400 const guint8 *result;
1402 ptr = tvb_get_ptr(tvb, abs_offset, limit);
1404 result = (const guint8 *) memchr(ptr, needle, limit);
1405 if (!result)
1406 return -1;
1408 return (gint) ((result - ptr) + abs_offset);
1411 /* Find first occurrence of needle in tvbuff, starting at offset. Searches
1412 * at most maxlength number of bytes; if maxlength is -1, searches to
1413 * end of tvbuff.
1414 * Returns the offset of the found needle, or -1 if not found.
1415 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1416 * in that case, -1 will be returned if the boundary is reached before
1417 * finding needle. */
1418 gint
1419 tvb_find_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 needle)
1421 const guint8 *result;
1422 guint abs_offset;
1423 guint tvbufflen;
1424 guint limit;
1426 DISSECTOR_ASSERT(tvb && tvb->initialized);
1428 check_offset_length(tvb, offset, -1, &abs_offset, &tvbufflen);
1430 /* Only search to end of tvbuff, w/o throwing exception. */
1431 if (maxlength == -1) {
1432 /* No maximum length specified; search to end of tvbuff. */
1433 limit = tvbufflen;
1435 else if (tvbufflen < (guint) maxlength) {
1436 /* Maximum length goes past end of tvbuff; search to end
1437 of tvbuff. */
1438 limit = tvbufflen;
1440 else {
1441 /* Maximum length doesn't go past end of tvbuff; search
1442 to that value. */
1443 limit = maxlength;
1446 /* If we have real data, perform our search now. */
1447 if (tvb->real_data) {
1448 result = (const guint8 *)memchr(tvb->real_data + abs_offset, needle, limit);
1449 if (result == NULL) {
1450 return -1;
1452 else {
1453 return (gint) (result - tvb->real_data);
1457 if (tvb->ops->tvb_find_guint8)
1458 return tvb->ops->tvb_find_guint8(tvb, abs_offset, limit, needle);
1460 return tvb_find_guint8_generic(tvb, offset, limit, needle);
1463 static gint
1464 tvb_pbrk_guint8_generic(tvbuff_t *tvb, guint abs_offset, guint limit, const guint8 *needles, guchar *found_needle)
1466 const guint8 *ptr;
1467 const guint8 *result;
1469 ptr = tvb_get_ptr(tvb, abs_offset, limit);
1471 result = guint8_pbrk(ptr, limit, needles, found_needle);
1472 if (!result)
1473 return -1;
1475 return (gint) ((result - ptr) + abs_offset);
1478 /* Find first occurrence of any of the needles in tvbuff, starting at offset.
1479 * Searches at most maxlength number of bytes; if maxlength is -1, searches
1480 * to end of tvbuff.
1481 * Returns the offset of the found needle, or -1 if not found.
1482 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1483 * in that case, -1 will be returned if the boundary is reached before
1484 * finding needle. */
1485 gint
1486 tvb_pbrk_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 *needles, guchar *found_needle)
1488 const guint8 *result;
1489 guint abs_offset;
1490 guint tvbufflen;
1491 guint limit;
1493 DISSECTOR_ASSERT(tvb && tvb->initialized);
1495 check_offset_length(tvb, offset, -1, &abs_offset, &tvbufflen);
1497 /* Only search to end of tvbuff, w/o throwing exception. */
1498 if (maxlength == -1) {
1499 /* No maximum length specified; search to end of tvbuff. */
1500 limit = tvbufflen;
1502 else if (tvbufflen < (guint) maxlength) {
1503 /* Maximum length goes past end of tvbuff; search to end
1504 of tvbuff. */
1505 limit = tvbufflen;
1507 else {
1508 /* Maximum length doesn't go past end of tvbuff; search
1509 to that value. */
1510 limit = maxlength;
1513 /* If we have real data, perform our search now. */
1514 if (tvb->real_data) {
1515 result = guint8_pbrk(tvb->real_data + abs_offset, limit, needles, found_needle);
1516 if (result == NULL) {
1517 return -1;
1519 else {
1520 return (gint) (result - tvb->real_data);
1524 if (tvb->ops->tvb_pbrk_guint8)
1525 return tvb->ops->tvb_pbrk_guint8(tvb, abs_offset, limit, needles, found_needle);
1527 return tvb_pbrk_guint8_generic(tvb, abs_offset, limit, needles, found_needle);
1530 /* Find size of stringz (NUL-terminated string) by looking for terminating
1531 * NUL. The size of the string includes the terminating NUL.
1533 * If the NUL isn't found, it throws the appropriate exception.
1535 guint
1536 tvb_strsize(tvbuff_t *tvb, const gint offset)
1538 guint abs_offset, junk_length;
1539 gint nul_offset;
1541 DISSECTOR_ASSERT(tvb && tvb->initialized);
1543 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1544 nul_offset = tvb_find_guint8(tvb, abs_offset, -1, 0);
1545 if (nul_offset == -1) {
1547 * OK, we hit the end of the tvbuff, so we should throw
1548 * an exception.
1550 * Did we hit the end of the captured data, or the end
1551 * of the actual data? If there's less captured data
1552 * than actual data, we presumably hit the end of the
1553 * captured data, otherwise we hit the end of the actual
1554 * data.
1556 if (tvb->length < tvb->reported_length) {
1557 THROW(BoundsError);
1558 } else {
1559 if (tvb->flags & TVBUFF_FRAGMENT) {
1560 THROW(FragmentBoundsError);
1561 } else {
1562 THROW(ReportedBoundsError);
1566 return (nul_offset - abs_offset) + 1;
1569 /* UTF-16/UCS-2 version of tvb_strsize */
1570 /* Returns number of bytes including the (two-bytes) null terminator */
1571 guint
1572 tvb_unicode_strsize(tvbuff_t *tvb, const gint offset)
1574 guint i = 0;
1575 gunichar2 uchar;
1577 DISSECTOR_ASSERT(tvb && tvb->initialized);
1579 do {
1580 /* Endianness doesn't matter when looking for null */
1581 uchar = tvb_get_ntohs(tvb, offset + i);
1582 i += 2;
1583 } while(uchar != 0);
1585 return i;
1588 /* Find length of string by looking for end of string ('\0'), up to
1589 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
1590 * of tvbuff.
1591 * Returns -1 if 'maxlength' reached before finding EOS. */
1592 gint
1593 tvb_strnlen(tvbuff_t *tvb, const gint offset, const guint maxlength)
1595 gint result_offset;
1596 guint abs_offset, junk_length;
1598 DISSECTOR_ASSERT(tvb && tvb->initialized);
1600 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1602 result_offset = tvb_find_guint8(tvb, abs_offset, maxlength, 0);
1604 if (result_offset == -1) {
1605 return -1;
1607 else {
1608 return result_offset - abs_offset;
1613 * Implement strneql etc
1617 * Call strncmp after checking if enough chars left, returning 0 if
1618 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1620 gint
1621 tvb_strneql(tvbuff_t *tvb, const gint offset, const gchar *str, const size_t size)
1623 const guint8 *ptr;
1625 ptr = ensure_contiguous_no_exception(tvb, offset, (gint)size, NULL);
1627 if (ptr) {
1628 int cmp = strncmp((const char *)ptr, str, size);
1631 * Return 0 if equal, -1 otherwise.
1633 return (cmp == 0 ? 0 : -1);
1634 } else {
1636 * Not enough characters in the tvbuff to match the
1637 * string.
1639 return -1;
1644 * Call g_ascii_strncasecmp after checking if enough chars left, returning
1645 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1647 gint
1648 tvb_strncaseeql(tvbuff_t *tvb, const gint offset, const gchar *str, const size_t size)
1650 const guint8 *ptr;
1652 ptr = ensure_contiguous_no_exception(tvb, offset, (gint)size, NULL);
1654 if (ptr) {
1655 int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
1658 * Return 0 if equal, -1 otherwise.
1660 return (cmp == 0 ? 0 : -1);
1661 } else {
1663 * Not enough characters in the tvbuff to match the
1664 * string.
1666 return -1;
1671 * Call memcmp after checking if enough chars left, returning 0 if
1672 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1674 gint
1675 tvb_memeql(tvbuff_t *tvb, const gint offset, const guint8 *str, size_t size)
1677 const guint8 *ptr;
1679 ptr = ensure_contiguous_no_exception(tvb, offset, (gint) size, NULL);
1681 if (ptr) {
1682 int cmp = memcmp(ptr, str, size);
1685 * Return 0 if equal, -1 otherwise.
1687 return (cmp == 0 ? 0 : -1);
1688 } else {
1690 * Not enough characters in the tvbuff to match the
1691 * string.
1693 return -1;
1697 /* Convert a string from Unicode to ASCII. At the moment we fake it by
1698 * replacing all non-ASCII characters with a '.' )-: The len parameter is
1699 * the number of guint16's to convert from Unicode.
1701 * If scope is set to NULL, returned buffer is allocated by g_malloc()
1702 * and must be g_free by the caller. Otherwise memory is automatically
1703 * freed when the scope lifetime is reached.
1705 /* XXX: This has been replaced by tvb_get_string() */
1706 char *
1707 tvb_get_faked_unicode(wmem_allocator_t *scope, tvbuff_t *tvb, int offset,
1708 const int len, const gboolean little_endian)
1710 char *buffer;
1711 int i;
1712 guint16 character;
1714 /* Make sure we have enough data before allocating the buffer,
1715 so we don't blow up if the length is huge. */
1716 tvb_ensure_bytes_exist(tvb, offset, 2*len);
1718 /* We know we won't throw an exception, so we don't have to worry
1719 about leaking this buffer. */
1720 buffer = (char *)wmem_alloc(scope, len + 1);
1722 for (i = 0; i < len; i++) {
1723 character = little_endian ? tvb_get_letohs(tvb, offset)
1724 : tvb_get_ntohs(tvb, offset);
1725 buffer[i] = character < 256 ? character : '.';
1726 offset += 2;
1729 buffer[len] = 0;
1731 return buffer;
1735 * Format the data in the tvb from offset for length ...
1737 gchar *
1738 tvb_format_text(tvbuff_t *tvb, const gint offset, const gint size)
1740 const guint8 *ptr;
1741 gint len;
1743 len = (size > 0) ? size : 0;
1745 ptr = ensure_contiguous(tvb, offset, size);
1746 return format_text(ptr, len);
1750 * Format the data in the tvb from offset for length ...
1752 gchar *
1753 tvb_format_text_wsp(tvbuff_t *tvb, const gint offset, const gint size)
1755 const guint8 *ptr;
1756 gint len;
1758 len = (size > 0) ? size : 0;
1760 ptr = ensure_contiguous(tvb, offset, size);
1761 return format_text_wsp(ptr, len);
1765 * Like "tvb_format_text()", but for null-padded strings; don't show
1766 * the null padding characters as "\000".
1768 gchar *
1769 tvb_format_stringzpad(tvbuff_t *tvb, const gint offset, const gint size)
1771 const guint8 *ptr, *p;
1772 gint len;
1773 gint stringlen;
1775 len = (size > 0) ? size : 0;
1777 ptr = ensure_contiguous(tvb, offset, size);
1778 for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
1780 return format_text(ptr, stringlen);
1784 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
1785 * the null padding characters as "\000".
1787 gchar *
1788 tvb_format_stringzpad_wsp(tvbuff_t *tvb, const gint offset, const gint size)
1790 const guint8 *ptr, *p;
1791 gint len;
1792 gint stringlen;
1794 len = (size > 0) ? size : 0;
1796 ptr = ensure_contiguous(tvb, offset, size);
1797 for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
1799 return format_text_wsp(ptr, stringlen);
1803 * Given a tvbuff, an offset, and a length, allocate a buffer big enough
1804 * to hold a non-null-terminated string of that length at that offset,
1805 * plus a trailing '\0', copy the string into it, and return a pointer
1806 * to the string.
1807 * If scope is NULL, memory is allocated with g_malloc() and user must
1808 * explicitely free it with g_free().
1809 * If scope is not NULL, memory is allocated with the corresponding pool
1810 * lifetime.
1811 * Throws an exception if the tvbuff ends before the string does.
1813 guint8 *
1814 tvb_get_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length)
1816 guint8 *strbuf;
1818 tvb_ensure_bytes_exist(tvb, offset, length); /* make sure length = -1 fails */
1819 strbuf = (guint8 *)wmem_alloc(scope, length + 1);
1820 tvb_memcpy(tvb, strbuf, offset, length);
1821 strbuf[length] = '\0';
1822 return strbuf;
1826 * Unicode (UTF-16) version of tvb_get_string()
1827 * XXX - this is UCS-2, not UTF-16, as it doesn't handle surrogate pairs
1829 * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
1831 * Specify length in bytes
1833 * If scope is NULL, memory is allocated with g_malloc() and user must
1834 * explicitely free it with g_free().
1835 * If scope is not NULL, memory is allocated with the corresponding pool
1836 * lifetime.
1838 * Returns an UTF-8 string
1840 gchar *
1841 tvb_get_unicode_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint length, const guint encoding)
1843 gunichar2 uchar;
1844 gint i; /* Byte counter for tvbuff */
1845 wmem_strbuf_t *strbuf;
1847 tvb_ensure_bytes_exist(tvb, offset, length);
1849 strbuf = wmem_strbuf_new(scope, NULL);
1851 for(i = 0; i < length; i += 2) {
1852 if (encoding == ENC_BIG_ENDIAN)
1853 uchar = tvb_get_ntohs(tvb, offset + i);
1854 else
1855 uchar = tvb_get_letohs(tvb, offset + i);
1857 wmem_strbuf_append_unichar(strbuf, uchar);
1860 return (gchar*)wmem_strbuf_get_str(strbuf);
1864 * Given a tvbuff, an offset, a length, and an encoding, allocate a
1865 * buffer big enough to hold a non-null-terminated string of that length
1866 * at that offset, plus a trailing '\0', copy into the buffer the
1867 * string as converted from the appropriate encoding to UTF-8, and
1868 * return a pointer to the string.
1870 * Throws an exception if the tvbuff ends before the string does.
1872 * If scope is NULL, memory is allocated with g_malloc() and user must
1873 * explicitely free it with g_free().
1874 * If scope is not NULL, memory is allocated with the corresponding pool
1875 * lifetime.
1877 guint8 *
1878 tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset,
1879 const gint length, const guint encoding)
1881 const guint8 *ptr;
1882 guint8 *strbuf;
1884 switch (encoding & ENC_CHARENCODING_MASK) {
1886 case ENC_ASCII:
1887 default:
1889 * For now, we treat bogus values as meaning
1890 * "ASCII" rather than reporting an error,
1891 * for the benefit of old dissectors written
1892 * when the last argument to proto_tree_add_item()
1893 * was a gboolean for the byte order, not an
1894 * encoding value, and passed non-zero values
1895 * other than TRUE to mean "little-endian".
1897 * XXX - should map all octets with the 8th bit
1898 * not set to a "substitute" UTF-8 character.
1900 strbuf = tvb_get_string(scope, tvb, offset, length);
1901 break;
1903 case ENC_UTF_8:
1905 * XXX - should map all invalid UTF-8 sequences
1906 * to a "substitute" UTF-8 character.
1908 strbuf = tvb_get_string(scope, tvb, offset, length);
1909 break;
1911 case ENC_UTF_16:
1913 * XXX - needs to handle surrogate pairs and to map
1914 * invalid characters and sequences to a "substitute"
1915 * UTF-8 character.
1917 strbuf = tvb_get_unicode_string(scope, tvb, offset, length,
1918 encoding & ENC_LITTLE_ENDIAN);
1919 break;
1921 case ENC_UCS_2:
1923 * XXX - needs to map values that are not valid UCS-2
1924 * characters (such as, I think, values used as the
1925 * components of a UTF-16 surrogate pair) to a
1926 * "substitute" UTF-8 character.
1928 strbuf = tvb_get_unicode_string(scope, tvb, offset, length,
1929 encoding & ENC_LITTLE_ENDIAN);
1930 break;
1932 case ENC_EBCDIC:
1934 * XXX - do the copy and conversion in one pass.
1936 * XXX - multiple "dialects" of EBCDIC?
1938 tvb_ensure_bytes_exist(tvb, offset, length); /* make sure length = -1 fails */
1939 strbuf = (guint8 *)wmem_alloc(scope, length + 1);
1940 if (length != 0) {
1941 ptr = ensure_contiguous(tvb, offset, length);
1942 memcpy(strbuf, ptr, length);
1943 EBCDIC_to_ASCII(strbuf, length);
1945 strbuf[length] = '\0';
1946 break;
1948 return strbuf;
1952 * Given a tvbuff and an offset, with the offset assumed to refer to
1953 * a null-terminated string, find the length of that string (and throw
1954 * an exception if the tvbuff ends before we find the null), allocate
1955 * a buffer big enough to hold the string, copy the string into it,
1956 * and return a pointer to the string. Also return the length of the
1957 * string (including the terminating null) through a pointer.
1959 * If scope is NULL, memory is allocated with g_malloc() and user must
1960 * explicitely free it with g_free().
1961 * If scope is not NULL, memory is allocated with the corresponding pool
1962 * lifetime.
1964 guint8 *
1965 tvb_get_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp)
1967 guint size;
1968 guint8 *strptr;
1970 size = tvb_strsize(tvb, offset);
1971 strptr = (guint8 *)wmem_alloc(scope, size);
1972 tvb_memcpy(tvb, strptr, offset, size);
1973 if (lengthp)
1974 *lengthp = size;
1975 return strptr;
1978 guint8 *
1979 tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
1981 guint size;
1982 guint8 *strptr;
1984 switch (encoding & ENC_CHARENCODING_MASK) {
1986 case ENC_ASCII:
1987 default:
1989 * For now, we treat bogus values as meaning
1990 * "ASCII" rather than reporting an error,
1991 * for the benefit of old dissectors written
1992 * when the last argument to proto_tree_add_item()
1993 * was a gboolean for the byte order, not an
1994 * encoding value, and passed non-zero values
1995 * other than TRUE to mean "little-endian".
1997 * XXX - should map all octets with the 8th bit
1998 * not set to a "substitute" UTF-8 character.
2000 strptr = tvb_get_stringz(scope, tvb, offset, lengthp);
2001 break;
2003 case ENC_UTF_8:
2005 * XXX - should map all invalid UTF-8 sequences
2006 * to a "substitute" UTF-8 character.
2008 strptr = tvb_get_stringz(scope, tvb, offset, lengthp);
2009 break;
2011 case ENC_UTF_16:
2013 * XXX - needs to handle surrogate pairs and to map
2014 * invalid characters and sequences to a "substitute"
2015 * UTF-8 character.
2017 strptr = tvb_get_unicode_stringz(scope, tvb, offset, lengthp,
2018 encoding & ENC_LITTLE_ENDIAN);
2019 break;
2021 case ENC_UCS_2:
2023 * XXX - needs to map values that are not valid UCS-2
2024 * characters (such as, I think, values used as the
2025 * components of a UTF-16 surrogate pair) to a
2026 * "substitute" UTF-8 character.
2028 strptr = tvb_get_unicode_stringz(scope, tvb, offset, lengthp,
2029 encoding & ENC_LITTLE_ENDIAN);
2030 break;
2032 case ENC_EBCDIC:
2034 * XXX - do the copy and conversion in one pass.
2036 * XXX - multiple "dialects" of EBCDIC?
2038 size = tvb_strsize(tvb, offset);
2039 strptr = (guint8 *)wmem_alloc(scope, size);
2040 tvb_memcpy(tvb, strptr, offset, size);
2041 EBCDIC_to_ASCII(strptr, size);
2042 if (lengthp)
2043 *lengthp = size;
2044 break;
2047 return strptr;
2051 * Given a tvbuff and an offset, with the offset assumed to refer to
2052 * a null-terminated string, find the length of that string (and throw
2053 * an exception if the tvbuff ends before we find the null), ensure that
2054 * the TVB is flat, and return a pointer to the string (in the TVB).
2055 * Also return the length of the string (including the terminating null)
2056 * through a pointer.
2058 * As long as we aren't using composite TVBs, this saves the cycles used
2059 * (often unnecessariliy) in allocating a buffer and copying the string into
2060 * it. (If we do start using composite TVBs, we may want to replace this
2061 * function with the _ephemeral versoin.)
2063 const guint8 *
2064 tvb_get_const_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
2066 guint size;
2067 const guint8 *strptr;
2069 size = tvb_strsize(tvb, offset);
2070 strptr = ensure_contiguous(tvb, offset, size);
2071 if (lengthp)
2072 *lengthp = size;
2073 return strptr;
2077 * Unicode (UTF-16) version of tvb_get_stringz()
2079 * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
2081 * Returns an allocated UTF-8 string and updates lengthp pointer with length of string (in bytes)
2083 gchar *
2084 tvb_get_unicode_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
2086 gunichar2 uchar;
2087 gint size; /* Number of UTF-16 characters */
2088 gint i; /* Byte counter for tvbuff */
2089 wmem_strbuf_t *strbuf;
2091 size = tvb_unicode_strsize(tvb, offset);
2093 strbuf = wmem_strbuf_new(scope, NULL);
2095 for(i = 0; i < size; i += 2) {
2096 if (encoding == ENC_BIG_ENDIAN)
2097 uchar = tvb_get_ntohs(tvb, offset + i);
2098 else
2099 uchar = tvb_get_letohs(tvb, offset + i);
2101 wmem_strbuf_append_unichar(strbuf, uchar);
2104 if (lengthp)
2105 *lengthp = i; /* Number of *bytes* processed */
2107 return (gchar*)wmem_strbuf_get_str(strbuf);
2110 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2111 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2112 * Returns length of string (not including terminating NUL), or -1 if the string was
2113 * truncated in the buffer due to not having reached the terminating NUL.
2114 * In this way, it acts like g_snprintf().
2116 * bufsize MUST be greater than 0.
2118 * When processing a packet where the remaining number of bytes is less
2119 * than bufsize, an exception is not thrown if the end of the packet
2120 * is reached before the NUL is found. If no NUL is found before reaching
2121 * the end of the short packet, -1 is still returned, and the string
2122 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2123 * at the correct spot, terminating the string.
2125 * *bytes_copied will contain the number of bytes actually copied,
2126 * including the terminating-NUL.
2128 static gint
2129 _tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer, gint *bytes_copied)
2131 gint stringlen;
2132 guint abs_offset;
2133 gint limit, len;
2134 gboolean decreased_max = FALSE;
2136 /* Only read to end of tvbuff, w/o throwing exception. */
2137 check_offset_length(tvb, offset, -1, &abs_offset, &len);
2139 /* There must at least be room for the terminating NUL. */
2140 DISSECTOR_ASSERT(bufsize != 0);
2142 /* If there's no room for anything else, just return the NUL. */
2143 if (bufsize == 1) {
2144 buffer[0] = 0;
2145 *bytes_copied = 1;
2146 return 0;
2149 /* check_offset_length() won't throw an exception if we're
2150 * looking at the byte immediately after the end of the tvbuff. */
2151 if (len == 0) {
2152 THROW(ReportedBoundsError);
2155 /* This should not happen because check_offset_length() would
2156 * have already thrown an exception if 'offset' were out-of-bounds.
2158 DISSECTOR_ASSERT(len != -1);
2161 * If we've been passed a negative number, bufsize will
2162 * be huge.
2164 DISSECTOR_ASSERT(bufsize <= G_MAXINT);
2166 if ((guint)len < bufsize) {
2167 limit = len;
2168 decreased_max = TRUE;
2170 else {
2171 limit = bufsize;
2174 stringlen = tvb_strnlen(tvb, abs_offset, limit - 1);
2175 /* If NUL wasn't found, copy the data and return -1 */
2176 if (stringlen == -1) {
2177 tvb_memcpy(tvb, buffer, abs_offset, limit);
2178 if (decreased_max) {
2179 buffer[limit] = 0;
2180 /* Add 1 for the extra NUL that we set at buffer[limit],
2181 * pretending that it was copied as part of the string. */
2182 *bytes_copied = limit + 1;
2184 else {
2185 *bytes_copied = limit;
2187 return -1;
2190 /* Copy the string to buffer */
2191 tvb_memcpy(tvb, buffer, abs_offset, stringlen + 1);
2192 *bytes_copied = stringlen + 1;
2193 return stringlen;
2196 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2197 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2198 * Returns length of string (not including terminating NUL), or -1 if the string was
2199 * truncated in the buffer due to not having reached the terminating NUL.
2200 * In this way, it acts like g_snprintf().
2202 * When processing a packet where the remaining number of bytes is less
2203 * than bufsize, an exception is not thrown if the end of the packet
2204 * is reached before the NUL is found. If no NUL is found before reaching
2205 * the end of the short packet, -1 is still returned, and the string
2206 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2207 * at the correct spot, terminating the string.
2209 gint
2210 tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer)
2212 gint bytes_copied;
2214 DISSECTOR_ASSERT(tvb && tvb->initialized);
2216 return _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
2219 /* Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
2220 * have a terminating NUL. If the string was truncated when copied into buffer,
2221 * a NUL is placed at the end of buffer to terminate it.
2223 gint
2224 tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer)
2226 gint len, bytes_copied;
2228 DISSECTOR_ASSERT(tvb && tvb->initialized);
2230 len = _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
2232 if (len == -1) {
2233 buffer[bufsize - 1] = 0;
2234 return bytes_copied - 1;
2236 else {
2237 return len;
2242 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2243 * at that offset (which may be -1 for "all the way to the end of the
2244 * tvbuff"), find the end of the (putative) line that starts at the
2245 * specified offset in the tvbuff, going no further than the specified
2246 * length.
2248 * Return the length of the line (not counting the line terminator at
2249 * the end), or, if we don't find a line terminator:
2251 * if "deseg" is true, return -1;
2253 * if "deseg" is false, return the amount of data remaining in
2254 * the buffer.
2256 * Set "*next_offset" to the offset of the character past the line
2257 * terminator, or past the end of the buffer if we don't find a line
2258 * terminator. (It's not set if we return -1.)
2260 gint
2261 tvb_find_line_end(tvbuff_t *tvb, const gint offset, int len, gint *next_offset, const gboolean desegment)
2263 gint eob_offset;
2264 gint eol_offset;
2265 int linelen;
2266 guchar found_needle = 0;
2268 if (len == -1)
2269 len = tvb_length_remaining(tvb, offset);
2271 * XXX - what if "len" is still -1, meaning "offset is past the
2272 * end of the tvbuff"?
2274 eob_offset = offset + len;
2277 * Look either for a CR or an LF.
2279 eol_offset = tvb_pbrk_guint8(tvb, offset, len, "\r\n", &found_needle);
2280 if (eol_offset == -1) {
2282 * No CR or LF - line is presumably continued in next packet.
2284 if (desegment) {
2286 * Tell our caller we saw no EOL, so they can
2287 * try to desegment and get the entire line
2288 * into one tvbuff.
2290 return -1;
2291 } else {
2293 * Pretend the line runs to the end of the tvbuff.
2295 linelen = eob_offset - offset;
2296 if (next_offset)
2297 *next_offset = eob_offset;
2299 } else {
2301 * Find the number of bytes between the starting offset
2302 * and the CR or LF.
2304 linelen = eol_offset - offset;
2307 * Is it a CR?
2309 if (found_needle == '\r') {
2311 * Yes - is it followed by an LF?
2313 if (eol_offset + 1 >= eob_offset) {
2315 * Dunno - the next byte isn't in this
2316 * tvbuff.
2318 if (desegment) {
2320 * We'll return -1, although that
2321 * runs the risk that if the line
2322 * really *is* terminated with a CR,
2323 * we won't properly dissect this
2324 * tvbuff.
2326 * It's probably more likely that
2327 * the line ends with CR-LF than
2328 * that it ends with CR by itself.
2330 return -1;
2332 } else {
2334 * Well, we can at least look at the next
2335 * byte.
2337 if (tvb_get_guint8(tvb, eol_offset + 1) == '\n') {
2339 * It's an LF; skip over the CR.
2341 eol_offset++;
2347 * Return the offset of the character after the last
2348 * character in the line, skipping over the last character
2349 * in the line terminator.
2351 if (next_offset)
2352 *next_offset = eol_offset + 1;
2354 return linelen;
2358 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2359 * at that offset (which may be -1 for "all the way to the end of the
2360 * tvbuff"), find the end of the (putative) line that starts at the
2361 * specified offset in the tvbuff, going no further than the specified
2362 * length.
2364 * However, treat quoted strings inside the buffer specially - don't
2365 * treat newlines in quoted strings as line terminators.
2367 * Return the length of the line (not counting the line terminator at
2368 * the end), or the amount of data remaining in the buffer if we don't
2369 * find a line terminator.
2371 * Set "*next_offset" to the offset of the character past the line
2372 * terminator, or past the end of the buffer if we don't find a line
2373 * terminator.
2375 gint
2376 tvb_find_line_end_unquoted(tvbuff_t *tvb, const gint offset, int len, gint *next_offset)
2378 gint cur_offset, char_offset;
2379 gboolean is_quoted;
2380 guchar c = 0;
2381 gint eob_offset;
2382 int linelen;
2384 if (len == -1)
2385 len = tvb_length_remaining(tvb, offset);
2387 * XXX - what if "len" is still -1, meaning "offset is past the
2388 * end of the tvbuff"?
2390 eob_offset = offset + len;
2392 cur_offset = offset;
2393 is_quoted = FALSE;
2394 for (;;) {
2396 * Is this part of the string quoted?
2398 if (is_quoted) {
2400 * Yes - look only for the terminating quote.
2402 char_offset = tvb_find_guint8(tvb, cur_offset, len,
2403 '"');
2404 } else {
2406 * Look either for a CR, an LF, or a '"'.
2408 char_offset = tvb_pbrk_guint8(tvb, cur_offset, len, "\r\n\"", &c);
2410 if (char_offset == -1) {
2412 * Not found - line is presumably continued in
2413 * next packet.
2414 * We pretend the line runs to the end of the tvbuff.
2416 linelen = eob_offset - offset;
2417 if (next_offset)
2418 *next_offset = eob_offset;
2419 break;
2422 if (is_quoted) {
2424 * We're processing a quoted string.
2425 * We only looked for ", so we know it's a ";
2426 * as we're processing a quoted string, it's a
2427 * closing quote.
2429 is_quoted = FALSE;
2430 } else {
2432 * OK, what is it?
2434 if (c == '"') {
2436 * Un-quoted "; it begins a quoted
2437 * string.
2439 is_quoted = TRUE;
2440 } else {
2442 * It's a CR or LF; we've found a line
2443 * terminator.
2445 * Find the number of bytes between the
2446 * starting offset and the CR or LF.
2448 linelen = char_offset - offset;
2451 * Is it a CR?
2453 if (c == '\r') {
2455 * Yes; is it followed by an LF?
2457 if (char_offset + 1 < eob_offset &&
2458 tvb_get_guint8(tvb, char_offset + 1)
2459 == '\n') {
2461 * Yes; skip over the CR.
2463 char_offset++;
2468 * Return the offset of the character after
2469 * the last character in the line, skipping
2470 * over the last character in the line
2471 * terminator, and quit.
2473 if (next_offset)
2474 *next_offset = char_offset + 1;
2475 break;
2480 * Step past the character we found.
2482 cur_offset = char_offset + 1;
2483 if (cur_offset >= eob_offset) {
2485 * The character we found was the last character
2486 * in the tvbuff - line is presumably continued in
2487 * next packet.
2488 * We pretend the line runs to the end of the tvbuff.
2490 linelen = eob_offset - offset;
2491 if (next_offset)
2492 *next_offset = eob_offset;
2493 break;
2496 return linelen;
2500 * Copied from the mgcp dissector. (This function should be moved to /epan )
2501 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
2502 * character following offset or offset + maxlength -1 whichever
2503 * is smaller.
2505 * Parameters:
2506 * tvb - The tvbuff in which we are skipping whitespace.
2507 * offset - The offset in tvb from which we begin trying to skip whitespace.
2508 * maxlength - The maximum distance from offset that we may try to skip
2509 * whitespace.
2511 * Returns: The position in tvb of the first non-whitespace
2512 * character following offset or offset + maxlength -1 whichever
2513 * is smaller.
2515 gint
2516 tvb_skip_wsp(tvbuff_t *tvb, const gint offset, const gint maxlength)
2518 gint counter = offset;
2519 gint end, tvb_len;
2520 guint8 tempchar;
2522 /* Get the length remaining */
2523 tvb_len = tvb_length(tvb);
2524 end = offset + maxlength;
2525 if (end >= tvb_len)
2527 end = tvb_len;
2530 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
2531 for (counter = offset;
2532 counter < end &&
2533 ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
2534 tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
2535 counter++);
2537 return (counter);
2540 gint
2541 tvb_skip_wsp_return(tvbuff_t *tvb, const gint offset) {
2542 gint counter = offset;
2543 guint8 tempchar;
2545 for(counter = offset; counter > 0 &&
2546 ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
2547 tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
2548 counter++;
2549 return (counter);
2554 * Format a bunch of data from a tvbuff as bytes, returning a pointer
2555 * to the string with the formatted data, with "punct" as a byte
2556 * separator.
2558 gchar *
2559 tvb_bytes_to_str_punct(tvbuff_t *tvb, const gint offset, const gint len, const gchar punct)
2561 return bytes_to_str_punct(ensure_contiguous(tvb, offset, len), len, punct);
2566 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2567 * at that offset (which may be -1 for "all the way to the end of the
2568 * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
2569 * the low or high half byte, formating the digits according to an input digit set,
2570 * if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
2571 * A pointer to the packet scope allocated string will be returned.
2572 * Note a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
2574 static dgt_set_t Dgt1_9_bcd = {
2576 /* 0 1 2 3 4 5 6 7 8 9 a b c d e */
2577 '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?'
2580 const gchar *
2581 tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first)
2583 int length;
2584 guint8 octet;
2585 int i = 0;
2586 char *digit_str;
2587 gint t_offset = offset;
2589 if (!dgt)
2590 dgt = &Dgt1_9_bcd;
2592 if (len == -1) {
2593 length = tvb_length(tvb);
2594 if (length < offset) {
2595 return "";
2597 } else {
2598 length = offset + len;
2600 digit_str = (char *)wmem_alloc(wmem_packet_scope(), (length - offset)*2+1);
2602 while (t_offset < length) {
2604 octet = tvb_get_guint8(tvb,t_offset);
2605 if (!skip_first) {
2606 digit_str[i] = dgt->out[octet & 0x0f];
2607 i++;
2609 skip_first = FALSE;
2612 * unpack second value in byte
2614 octet = octet >> 4;
2616 if (octet == 0x0f) /* odd number bytes - hit filler */
2617 break;
2619 digit_str[i] = dgt->out[octet & 0x0f];
2620 i++;
2621 t_offset++;
2624 digit_str[i]= '\0';
2625 return digit_str;
2630 * Format a bunch of data from a tvbuff as bytes, returning a pointer
2631 * to the string with the formatted data.
2633 gchar *
2634 tvb_bytes_to_str(tvbuff_t *tvb, const gint offset, const gint len)
2636 return bytes_to_str(ensure_contiguous(tvb, offset, len), len);
2639 /* Find a needle tvbuff within a haystack tvbuff. */
2640 gint
2641 tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const gint haystack_offset)
2643 guint haystack_abs_offset, haystack_abs_length;
2644 const guint8 *haystack_data;
2645 const guint8 *needle_data;
2646 const guint needle_len = needle_tvb->length;
2647 const guint8 *location;
2649 DISSECTOR_ASSERT(haystack_tvb && haystack_tvb->initialized);
2651 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
2652 return -1;
2655 /* Get pointers to the tvbuffs' data. */
2656 haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
2657 needle_data = ensure_contiguous(needle_tvb, 0, -1);
2659 check_offset_length(haystack_tvb, haystack_offset, -1,
2660 &haystack_abs_offset, &haystack_abs_length);
2662 location = epan_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
2663 needle_data, needle_len);
2665 if (location) {
2666 return (gint) (location - haystack_data);
2669 return -1;
2672 #ifdef HAVE_LIBZ
2674 * Uncompresses a zlib compressed packet inside a message of tvb at offset with
2675 * length comprlen. Returns an uncompressed tvbuffer if uncompression
2676 * succeeded or NULL if uncompression failed.
2678 #define TVB_Z_MIN_BUFSIZ 32768
2679 #define TVB_Z_MAX_BUFSIZ 1048576 * 10
2680 /* #define TVB_Z_DEBUG 1 */
2681 #undef TVB_Z_DEBUG
2683 tvbuff_t *
2684 tvb_uncompress(tvbuff_t *tvb, const int offset, int comprlen)
2686 gint err = Z_OK;
2687 guint bytes_out = 0;
2688 guint8 *compr = NULL;
2689 guint8 *uncompr = NULL;
2690 tvbuff_t *uncompr_tvb = NULL;
2691 z_streamp strm = NULL;
2692 Bytef *strmbuf = NULL;
2693 guint inits_done = 0;
2694 gint wbits = MAX_WBITS;
2695 guint8 *next = NULL;
2696 guint bufsiz = TVB_Z_MIN_BUFSIZ;
2697 #ifdef TVB_Z_DEBUG
2698 guint inflate_passes = 0;
2699 guint bytes_in = tvb_length_remaining(tvb, offset);
2700 #endif
2702 if (tvb == NULL) {
2703 return NULL;
2706 compr = (guint8 *)tvb_memdup(NULL, tvb, offset, comprlen);
2708 if (!compr)
2709 return NULL;
2712 * Assume that the uncompressed data is at least twice as big as
2713 * the compressed size.
2715 bufsiz = tvb_length_remaining(tvb, offset) * 2;
2716 bufsiz = CLAMP(bufsiz, TVB_Z_MIN_BUFSIZ, TVB_Z_MAX_BUFSIZ);
2718 #ifdef TVB_Z_DEBUG
2719 printf("bufsiz: %u bytes\n", bufsiz);
2720 #endif
2722 next = compr;
2724 strm = g_new0(z_stream, 1);
2725 strm->next_in = next;
2726 strm->avail_in = comprlen;
2728 strmbuf = (Bytef *)g_malloc0(bufsiz);
2729 strm->next_out = strmbuf;
2730 strm->avail_out = bufsiz;
2732 err = inflateInit2(strm, wbits);
2733 inits_done = 1;
2734 if (err != Z_OK) {
2735 inflateEnd(strm);
2736 g_free(strm);
2737 g_free(compr);
2738 g_free(strmbuf);
2739 return NULL;
2742 while (1) {
2743 memset(strmbuf, '\0', bufsiz);
2744 strm->next_out = strmbuf;
2745 strm->avail_out = bufsiz;
2747 err = inflate(strm, Z_SYNC_FLUSH);
2749 if (err == Z_OK || err == Z_STREAM_END) {
2750 guint bytes_pass = bufsiz - strm->avail_out;
2752 #ifdef TVB_Z_DEBUG
2753 ++inflate_passes;
2754 #endif
2756 if (uncompr == NULL) {
2758 * This is ugly workaround for bug #6480
2759 * (https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=6480)
2761 * g_memdup(..., 0) returns NULL (g_malloc(0) also)
2762 * when uncompr is NULL logic below doesn't create tvb
2763 * which is later interpreted as decompression failed.
2765 uncompr = (guint8 *)((bytes_pass || err != Z_STREAM_END) ?
2766 g_memdup(strmbuf, bytes_pass) :
2767 g_strdup(""));
2768 } else {
2769 guint8 *new_data = (guint8 *)g_malloc0(bytes_out + bytes_pass);
2771 memcpy(new_data, uncompr, bytes_out);
2772 memcpy(new_data + bytes_out, strmbuf, bytes_pass);
2774 g_free(uncompr);
2775 uncompr = new_data;
2778 bytes_out += bytes_pass;
2780 if (err == Z_STREAM_END) {
2781 inflateEnd(strm);
2782 g_free(strm);
2783 g_free(strmbuf);
2784 break;
2786 } else if (err == Z_BUF_ERROR) {
2788 * It's possible that not enough frames were captured
2789 * to decompress this fully, so return what we've done
2790 * so far, if any.
2792 inflateEnd(strm);
2793 g_free(strm);
2794 g_free(strmbuf);
2796 if (uncompr != NULL) {
2797 break;
2798 } else {
2799 g_free(compr);
2800 return NULL;
2803 } else if (err == Z_DATA_ERROR && inits_done == 1
2804 && uncompr == NULL && (*compr == 0x1f) &&
2805 (*(compr + 1) == 0x8b)) {
2807 * inflate() is supposed to handle both gzip and deflate
2808 * streams automatically, but in reality it doesn't
2809 * seem to handle either (at least not within the
2810 * context of an HTTP response.) We have to try
2811 * several tweaks, depending on the type of data and
2812 * version of the library installed.
2816 * Gzip file format. Skip past the header, since the
2817 * fix to make it work (setting windowBits to 31)
2818 * doesn't work with all versions of the library.
2820 Bytef *c = compr + 2;
2821 Bytef flags = 0;
2823 if (*c == Z_DEFLATED) {
2824 c++;
2825 } else {
2826 inflateEnd(strm);
2827 g_free(strm);
2828 g_free(compr);
2829 g_free(strmbuf);
2830 return NULL;
2833 flags = *c;
2835 /* Skip past the MTIME, XFL, and OS fields. */
2836 c += 7;
2838 if (flags & (1 << 2)) {
2839 /* An Extra field is present. */
2840 gint xsize = (gint)(*c |
2841 (*(c + 1) << 8));
2843 c += xsize;
2846 if (flags & (1 << 3)) {
2847 /* A null terminated filename */
2849 while ((c - compr) < comprlen && *c != '\0') {
2850 c++;
2853 c++;
2856 if (flags & (1 << 4)) {
2857 /* A null terminated comment */
2859 while ((c - compr) < comprlen && *c != '\0') {
2860 c++;
2863 c++;
2867 inflateReset(strm);
2868 next = c;
2869 strm->next_in = next;
2870 if (c - compr > comprlen) {
2871 inflateEnd(strm);
2872 g_free(strm);
2873 g_free(compr);
2874 g_free(strmbuf);
2875 return NULL;
2877 comprlen -= (int) (c - compr);
2879 inflateEnd(strm);
2880 inflateInit2(strm, wbits);
2881 inits_done++;
2882 } else if (err == Z_DATA_ERROR && uncompr == NULL &&
2883 inits_done <= 3) {
2886 * Re-init the stream with a negative
2887 * MAX_WBITS. This is necessary due to
2888 * some servers (Apache) not sending
2889 * the deflate header with the
2890 * content-encoded response.
2892 wbits = -MAX_WBITS;
2894 inflateReset(strm);
2896 strm->next_in = next;
2897 strm->avail_in = comprlen;
2899 inflateEnd(strm);
2900 memset(strmbuf, '\0', bufsiz);
2901 strm->next_out = strmbuf;
2902 strm->avail_out = bufsiz;
2904 err = inflateInit2(strm, wbits);
2906 inits_done++;
2908 if (err != Z_OK) {
2909 g_free(strm);
2910 g_free(strmbuf);
2911 g_free(compr);
2912 g_free(uncompr);
2914 return NULL;
2916 } else {
2917 inflateEnd(strm);
2918 g_free(strm);
2919 g_free(strmbuf);
2921 if (uncompr == NULL) {
2922 g_free(compr);
2923 return NULL;
2926 break;
2930 #ifdef TVB_Z_DEBUG
2931 printf("inflate() total passes: %u\n", inflate_passes);
2932 printf("bytes in: %u\nbytes out: %u\n\n", bytes_in, bytes_out);
2933 #endif
2935 if (uncompr != NULL) {
2936 uncompr_tvb = tvb_new_real_data((guint8*) uncompr, bytes_out, bytes_out);
2937 tvb_set_free_cb(uncompr_tvb, g_free);
2939 g_free(compr);
2940 return uncompr_tvb;
2942 #else
2943 tvbuff_t *
2944 tvb_uncompress(tvbuff_t *tvb _U_, const int offset _U_, int comprlen _U_)
2946 return NULL;
2948 #endif
2950 tvbuff_t *
2951 tvb_child_uncompress(tvbuff_t *parent, tvbuff_t *tvb, const int offset, int comprlen)
2953 tvbuff_t *new_tvb = tvb_uncompress(tvb, offset, comprlen);
2954 if (new_tvb)
2955 tvb_set_child_real_data_tvbuff (parent, new_tvb);
2956 return new_tvb;
2959 gint
2960 tvb_raw_offset(tvbuff_t *tvb)
2962 return ((tvb->raw_offset==-1)?(tvb->raw_offset = tvb_offset_from_real_beginning(tvb)):tvb->raw_offset);
2965 void
2966 tvb_set_fragment(tvbuff_t *tvb)
2968 tvb->flags |= TVBUFF_FRAGMENT;
2971 struct tvbuff *
2972 tvb_get_ds_tvb(tvbuff_t *tvb)
2974 return(tvb->ds_tvb);
2978 * Editor modelines - http://www.wireshark.org/tools/modelines.html
2980 * Local variables:
2981 * c-basic-offset: 8
2982 * tab-width: 8
2983 * indent-tabs-mode: t
2984 * End:
2986 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2987 * :indentSize=8:tabSize=8:noTabs=false: