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
12 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
14 * Code to convert IEEE floating point formats to native floating point
15 * derived from code Copyright (c) Ashok Narayanan, 2000
17 * Wireshark - Network traffic analyzer
18 * By Gerald Combs <gerald@wireshark.org>
19 * Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
42 #include "wsutil/pint.h"
43 #include "wsutil/sign_ext.h"
44 #include "wsutil/unicode-utils.h"
45 #include "wsutil/nstime.h"
46 #include "wsutil/time_util.h"
47 #include "wsutil/ws_mempbrk.h"
49 #include "tvbuff-int.h"
53 #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
54 #include "exceptions.h"
57 * Just make sure we include the prototype for strptime as well
58 * (needed for glibc 2.2) but make sure we do this only if not
62 /*#ifndef HAVE_STRPTIME*/
64 #include "wsutil/strptime.h"
69 _tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint total_no_of_bits);
72 _tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset);
74 static inline const guint8*
75 ensure_contiguous(tvbuff_t *tvb, const gint offset, const gint length);
77 static inline guint8 *
78 tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length);
81 tvb_new(const struct tvb_ops *ops)
84 gsize size = ops->tvb_size;
86 g_assert(size >= sizeof(*tvb));
88 tvb = (tvbuff_t *) g_slice_alloc(size);
92 tvb->initialized = FALSE;
95 tvb->reported_length = 0;
96 tvb->real_data = NULL;
104 tvb_free_internal(tvbuff_t *tvb)
108 DISSECTOR_ASSERT(tvb);
110 if (tvb->ops->tvb_free)
111 tvb->ops->tvb_free(tvb);
113 size = tvb->ops->tvb_size;
115 g_slice_free1(size, tvb);
118 /* XXX: just call tvb_free_chain();
119 * Not removed so that existing dissectors using tvb_free() need not be changed.
120 * I'd argue that existing calls to tvb_free() should have actually beeen
121 * calls to tvb_free_chain() although the calls were OK as long as no
122 * subsets, etc had been created on the tvb. */
124 tvb_free(tvbuff_t *tvb)
130 tvb_free_chain(tvbuff_t *tvb)
133 DISSECTOR_ASSERT(tvb);
135 next_tvb = tvb->next;
136 tvb_free_internal(tvb);
142 tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing)
144 tvbuff_t *tvb = tvb_new_proxy(backing);
146 tvb_add_to_chain(parent, tvb);
151 tvb_add_to_chain(tvbuff_t *parent, tvbuff_t *child)
153 tvbuff_t *tmp = child;
155 DISSECTOR_ASSERT(parent);
156 DISSECTOR_ASSERT(child);
162 tmp->next = parent->next;
168 * Check whether that offset goes more than one byte past the
171 * If not, return 0; otherwise, return exception
174 validate_offset(const tvbuff_t *tvb, const guint abs_offset)
176 if (G_LIKELY(abs_offset <= tvb->length))
178 else if (abs_offset <= tvb->reported_length)
180 else if (tvb->flags & TVBUFF_FRAGMENT)
181 return FragmentBoundsError;
183 return ReportedBoundsError;
187 compute_offset(const tvbuff_t *tvb, const gint offset, guint *offset_ptr)
190 /* Positive offset - relative to the beginning of the packet. */
191 if ((guint) offset <= tvb->length) {
192 *offset_ptr = offset;
193 } else if ((guint) offset <= tvb->reported_length) {
195 } else if (tvb->flags & TVBUFF_FRAGMENT) {
196 return FragmentBoundsError;
198 return ReportedBoundsError;
202 /* Negative offset - relative to the end of the packet. */
203 if ((guint) -offset <= tvb->length) {
204 *offset_ptr = tvb->length + offset;
205 } else if ((guint) -offset <= tvb->reported_length) {
207 } else if (tvb->flags & TVBUFF_FRAGMENT) {
208 return FragmentBoundsError;
210 return ReportedBoundsError;
218 compute_offset_and_remaining(const tvbuff_t *tvb, const gint offset, guint *offset_ptr, guint *rem_len)
222 exception = compute_offset(tvb, offset, offset_ptr);
224 *rem_len = tvb->length - *offset_ptr;
229 /* Computes the absolute offset and length based on a possibly-negative offset
230 * and a length that is possible -1 (which means "to the end of the data").
231 * Returns integer indicating whether the offset is in bounds (0) or
232 * not (exception number). The integer ptrs are modified with the new offset and length.
233 * No exception is thrown.
235 * XXX - we return success (0), if the offset is positive and right
236 * after the end of the tvbuff (i.e., equal to the length). We do this
237 * so that a dissector constructing a subset tvbuff for the next protocol
238 * will get a zero-length tvbuff, not an exception, if there's no data
239 * left for the next protocol - we want the next protocol to be the one
240 * that gets an exception, so the error is reported as an error in that
241 * protocol rather than the containing protocol. */
243 check_offset_length_no_exception(const tvbuff_t *tvb,
244 const gint offset, gint const length_val,
245 guint *offset_ptr, guint *length_ptr)
250 DISSECTOR_ASSERT(offset_ptr);
251 DISSECTOR_ASSERT(length_ptr);
253 /* Compute the offset */
254 exception = compute_offset(tvb, offset, offset_ptr);
258 if (length_val < -1) {
259 /* XXX - ReportedBoundsError? */
263 /* Compute the length */
264 if (length_val == -1)
265 *length_ptr = tvb->length - *offset_ptr;
267 *length_ptr = length_val;
270 * Compute the offset of the first byte past the length.
272 end_offset = *offset_ptr + *length_ptr;
275 * Check for an overflow
277 if (end_offset < *offset_ptr)
280 return validate_offset(tvb, end_offset);
283 /* Checks (+/-) offset and length and throws an exception if
284 * either is out of bounds. Sets integer ptrs to the new offset
287 check_offset_length(const tvbuff_t *tvb,
288 const gint offset, gint const length_val,
289 guint *offset_ptr, guint *length_ptr)
293 exception = check_offset_length_no_exception(tvb, offset, length_val, offset_ptr, length_ptr);
299 tvb_check_offset_length(const tvbuff_t *tvb,
300 const gint offset, gint const length_val,
301 guint *offset_ptr, guint *length_ptr)
303 check_offset_length(tvb, offset, length_val, offset_ptr, length_ptr);
306 static const unsigned char left_aligned_bitmask[] = {
318 tvb_new_octet_aligned(tvbuff_t *tvb, guint32 bit_offset, gint32 no_of_bits)
320 tvbuff_t *sub_tvb = NULL;
323 guint8 left, right, remaining_bits, *buf;
326 DISSECTOR_ASSERT(tvb && tvb->initialized);
328 byte_offset = bit_offset >> 3;
329 left = bit_offset % 8; /* for left-shifting */
330 right = 8 - left; /* for right-shifting */
332 if (no_of_bits == -1) {
333 datalen = _tvb_captured_length_remaining(tvb, byte_offset);
336 datalen = no_of_bits >> 3;
337 remaining_bits = no_of_bits % 8;
338 if (remaining_bits) {
343 /* already aligned -> shortcut */
344 if ((left == 0) && (remaining_bits == 0)) {
345 return tvb_new_subset(tvb, byte_offset, datalen, -1);
348 DISSECTOR_ASSERT(datalen>0);
350 /* if at least one trailing byte is available, we must use the content
351 * of that byte for the last shift (i.e. tvb_get_ptr() must use datalen + 1
352 * if non extra byte is available, the last shifted byte requires
355 if (_tvb_captured_length_remaining(tvb, byte_offset) > datalen) {
356 data = ensure_contiguous(tvb, byte_offset, datalen + 1); /* tvb_get_ptr */
358 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
359 buf = (guint8 *)g_malloc(datalen);
361 /* shift tvb data bit_offset bits to the left */
362 for (i = 0; i < datalen; i++)
363 buf[i] = (data[i] << left) | (data[i+1] >> right);
365 data = ensure_contiguous(tvb, byte_offset, datalen); /* tvb_get_ptr() */
367 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
368 buf = (guint8 *)g_malloc(datalen);
370 /* shift tvb data bit_offset bits to the left */
371 for (i = 0; i < (datalen-1); i++)
372 buf[i] = (data[i] << left) | (data[i+1] >> right);
373 buf[datalen-1] = data[datalen-1] << left; /* set last octet */
375 buf[datalen-1] &= left_aligned_bitmask[remaining_bits];
377 sub_tvb = tvb_new_child_real_data(tvb, buf, datalen, datalen);
378 tvb_set_free_cb(sub_tvb, g_free);
384 tvb_generic_clone_offset_len(tvbuff_t *tvb, guint offset, guint len)
386 tvbuff_t *cloned_tvb;
389 DISSECTOR_ASSERT(tvb_bytes_exist(tvb, offset, len));
391 data = (guint8 *) g_malloc(len);
393 tvb_memcpy(tvb, data, offset, len);
395 cloned_tvb = tvb_new_real_data(data, len, len);
396 tvb_set_free_cb(cloned_tvb, g_free);
402 tvb_clone_offset_len(tvbuff_t *tvb, guint offset, guint len)
404 if (tvb->ops->tvb_clone) {
405 tvbuff_t *cloned_tvb;
407 cloned_tvb = tvb->ops->tvb_clone(tvb, offset, len);
412 return tvb_generic_clone_offset_len(tvb, offset, len);
416 tvb_clone(tvbuff_t *tvb)
418 return tvb_clone_offset_len(tvb, 0, tvb->length);
422 tvb_captured_length(const tvbuff_t *tvb)
424 DISSECTOR_ASSERT(tvb && tvb->initialized);
429 /* For tvbuff internal use */
431 _tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset)
433 guint abs_offset, rem_length;
436 exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
444 tvb_captured_length_remaining(const tvbuff_t *tvb, const gint offset)
446 guint abs_offset, rem_length;
449 DISSECTOR_ASSERT(tvb && tvb->initialized);
451 exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
459 tvb_ensure_captured_length_remaining(const tvbuff_t *tvb, const gint offset)
461 guint abs_offset, rem_length;
464 DISSECTOR_ASSERT(tvb && tvb->initialized);
466 exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
470 if (rem_length == 0) {
472 * This routine ensures there's at least one byte available.
473 * There aren't any bytes available, so throw the appropriate
476 if (abs_offset >= tvb->reported_length) {
477 if (tvb->flags & TVBUFF_FRAGMENT) {
478 THROW(FragmentBoundsError);
480 THROW(ReportedBoundsError);
491 /* Validates that 'length' bytes are available starting from
492 * offset (pos/neg). Does not throw an exception. */
494 tvb_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
496 guint abs_offset, abs_length;
499 DISSECTOR_ASSERT(tvb && tvb->initialized);
501 exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
508 /* Validates that 'length' bytes, where 'length' is a 64-bit unsigned
509 * integer, are available starting from offset (pos/neg). Throws an
510 * exception if they aren't. */
512 tvb_ensure_bytes_exist64(const tvbuff_t *tvb, const gint offset, const guint64 length)
515 * Make sure the value fits in a signed integer; if not, assume
516 * that means that it's too big.
518 if (length > G_MAXINT) {
519 THROW(ReportedBoundsError);
522 /* OK, now cast it and try it with tvb_ensure_bytes_exist(). */
523 tvb_ensure_bytes_exist(tvb, offset, (gint)length);
526 /* Validates that 'length' bytes are available starting from
527 * offset (pos/neg). Throws an exception if they aren't. */
529 tvb_ensure_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
531 guint real_offset, end_offset;
533 DISSECTOR_ASSERT(tvb && tvb->initialized);
536 * -1 doesn't mean "until end of buffer", as that's pointless
537 * for this routine. We must treat it as a Really Large Positive
538 * Number, so that we throw an exception; we throw
539 * ReportedBoundsError, as if it were past even the end of a
540 * reassembled packet, and past the end of even the data we
543 * We do the same with other negative lengths.
546 THROW(ReportedBoundsError);
549 /* XXX: Below this point could be replaced with a call to
550 * check_offset_length with no functional change, however this is a
551 * *very* hot path and check_offset_length is not well-optimized for
552 * this case, so we eat some code duplication for a lot of speedup. */
555 /* Positive offset - relative to the beginning of the packet. */
556 if ((guint) offset <= tvb->length) {
557 real_offset = offset;
558 } else if ((guint) offset <= tvb->reported_length) {
560 } else if (tvb->flags & TVBUFF_FRAGMENT) {
561 THROW(FragmentBoundsError);
563 THROW(ReportedBoundsError);
567 /* Negative offset - relative to the end of the packet. */
568 if ((guint) -offset <= tvb->length) {
569 real_offset = tvb->length + offset;
570 } else if ((guint) -offset <= tvb->reported_length) {
572 } else if (tvb->flags & TVBUFF_FRAGMENT) {
573 THROW(FragmentBoundsError);
575 THROW(ReportedBoundsError);
580 * Compute the offset of the first byte past the length.
582 end_offset = real_offset + length;
585 * Check for an overflow
587 if (end_offset < real_offset)
590 if (G_LIKELY(end_offset <= tvb->length))
592 else if (end_offset <= tvb->reported_length)
594 else if (tvb->flags & TVBUFF_FRAGMENT)
595 THROW(FragmentBoundsError);
597 THROW(ReportedBoundsError);
601 tvb_offset_exists(const tvbuff_t *tvb, const gint offset)
606 DISSECTOR_ASSERT(tvb && tvb->initialized);
608 exception = compute_offset(tvb, offset, &abs_offset);
612 /* compute_offset only throws an exception on >, not >= because of the
613 * comment above check_offset_length_no_exception, but here we want the
614 * opposite behaviour so we check ourselves... */
615 if (abs_offset < tvb->length) {
624 tvb_reported_length(const tvbuff_t *tvb)
626 DISSECTOR_ASSERT(tvb && tvb->initialized);
628 return tvb->reported_length;
632 tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset)
637 DISSECTOR_ASSERT(tvb && tvb->initialized);
639 exception = compute_offset(tvb, offset, &abs_offset);
643 if (tvb->reported_length >= abs_offset)
644 return tvb->reported_length - abs_offset;
649 /* Set the reported length of a tvbuff to a given value; used for protocols
650 * whose headers contain an explicit length and where the calling
651 * dissector's payload may include padding as well as the packet for
653 * Also adjusts the data length. */
655 tvb_set_reported_length(tvbuff_t *tvb, const guint reported_length)
657 DISSECTOR_ASSERT(tvb && tvb->initialized);
659 if (reported_length > tvb->reported_length)
660 THROW(ReportedBoundsError);
662 tvb->reported_length = reported_length;
663 if (reported_length < tvb->length)
664 tvb->length = reported_length;
668 tvb_offset_from_real_beginning_counter(const tvbuff_t *tvb, const guint counter)
670 if (tvb->ops->tvb_offset)
671 return tvb->ops->tvb_offset(tvb, counter);
673 DISSECTOR_ASSERT_NOT_REACHED();
678 tvb_offset_from_real_beginning(const tvbuff_t *tvb)
680 return tvb_offset_from_real_beginning_counter(tvb, 0);
683 static inline const guint8*
684 ensure_contiguous_no_exception(tvbuff_t *tvb, const gint offset, const gint length, int *pexception)
686 guint abs_offset, abs_length;
689 exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
692 *pexception = exception;
697 * We know that all the data is present in the tvbuff, so
698 * no exceptions should be thrown.
701 return tvb->real_data + abs_offset;
703 if (tvb->ops->tvb_get_ptr)
704 return tvb->ops->tvb_get_ptr(tvb, abs_offset, abs_length);
706 DISSECTOR_ASSERT_NOT_REACHED();
710 static inline const guint8*
711 ensure_contiguous(tvbuff_t *tvb, const gint offset, const gint length)
716 p = ensure_contiguous_no_exception(tvb, offset, length, &exception);
718 DISSECTOR_ASSERT(exception > 0);
724 static inline const guint8*
725 fast_ensure_contiguous(tvbuff_t *tvb, const gint offset, const guint length)
730 DISSECTOR_ASSERT(tvb && tvb->initialized);
731 /* We don't check for overflow in this fast path so we only handle simple types */
732 DISSECTOR_ASSERT(length <= 8);
734 if (offset < 0 || !tvb->real_data) {
735 return ensure_contiguous(tvb, offset, length);
739 end_offset = u_offset + length;
741 if (end_offset <= tvb->length) {
742 return tvb->real_data + u_offset;
745 if (end_offset > tvb->reported_length) {
746 if (tvb->flags & TVBUFF_FRAGMENT) {
747 THROW(FragmentBoundsError);
749 THROW(ReportedBoundsError);
758 static inline const guint8*
759 guint8_pbrk(const guint8* haystack, size_t haystacklen, const guint8 *needles, guchar *found_needle)
761 const guint8 *result = ws_mempbrk(haystack, haystacklen, needles);
763 if (result && found_needle)
764 *found_needle = *result;
771 /************** ACCESSORS **************/
774 tvb_memcpy(tvbuff_t *tvb, void *target, const gint offset, size_t length)
776 guint abs_offset, abs_length;
778 DISSECTOR_ASSERT(tvb && tvb->initialized);
781 * XXX - we should eliminate the "length = -1 means 'to the end
782 * of the tvbuff'" convention, and use other means to achieve
783 * that; this would let us eliminate a bunch of checks for
784 * negative lengths in cases where the protocol has a 32-bit
787 * Allowing -1 but throwing an assertion on other negative
788 * lengths is a bit more work with the length being a size_t;
789 * instead, we check for a length <= 2^31-1.
791 DISSECTOR_ASSERT(length <= 0x7FFFFFFF);
792 check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);
794 if (tvb->real_data) {
795 return memcpy(target, tvb->real_data + abs_offset, abs_length);
798 if (tvb->ops->tvb_memcpy)
799 return tvb->ops->tvb_memcpy(tvb, target, abs_offset, abs_length);
802 * If the length is 0, there's nothing to do.
803 * (tvb->real_data could be null if it's allocated with
808 * XXX, fallback to slower method
810 DISSECTOR_ASSERT_NOT_REACHED();
817 * XXX - this doesn't treat a length of -1 as an error.
818 * If it did, this could replace some code that calls
819 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
822 * "composite_get_ptr()" depends on -1 not being
823 * an error; does anything else depend on this routine treating -1 as
824 * meaning "to the end of the buffer"?
826 * If scope is NULL, memory is allocated with g_malloc() and user must
827 * explicitly free it with g_free().
828 * If scope is not NULL, memory is allocated with the corresponding pool
832 tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, size_t length)
834 guint abs_offset, abs_length;
837 DISSECTOR_ASSERT(tvb && tvb->initialized);
839 check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);
841 duped = wmem_alloc(scope, abs_length);
842 return tvb_memcpy(tvb, duped, abs_offset, abs_length);
848 tvb_get_ptr(tvbuff_t *tvb, const gint offset, const gint length)
850 return ensure_contiguous(tvb, offset, length);
853 /* ---------------- */
855 tvb_get_guint8(tvbuff_t *tvb, const gint offset)
859 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint8));
864 tvb_get_ntohs(tvbuff_t *tvb, const gint offset)
868 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
873 tvb_get_ntoh24(tvbuff_t *tvb, const gint offset)
877 ptr = fast_ensure_contiguous(tvb, offset, 3);
882 tvb_get_ntohl(tvbuff_t *tvb, const gint offset)
886 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
891 tvb_get_ntoh40(tvbuff_t *tvb, const gint offset)
895 ptr = fast_ensure_contiguous(tvb, offset, 5);
900 tvb_get_ntohi40(tvbuff_t *tvb, const gint offset)
904 ret = ws_sign_ext64(tvb_get_ntoh40(tvb, offset), 40);
910 tvb_get_ntoh48(tvbuff_t *tvb, const gint offset)
914 ptr = fast_ensure_contiguous(tvb, offset, 6);
919 tvb_get_ntohi48(tvbuff_t *tvb, const gint offset)
923 ret = ws_sign_ext64(tvb_get_ntoh48(tvb, offset), 48);
929 tvb_get_ntoh56(tvbuff_t *tvb, const gint offset)
933 ptr = fast_ensure_contiguous(tvb, offset, 7);
938 tvb_get_ntohi56(tvbuff_t *tvb, const gint offset)
942 ret = ws_sign_ext64(tvb_get_ntoh56(tvb, offset), 56);
948 tvb_get_ntoh64(tvbuff_t *tvb, const gint offset)
952 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
957 tvb_get_guint16(tvbuff_t *tvb, const gint offset, const guint encoding) {
958 if (encoding & ENC_LITTLE_ENDIAN) {
959 return tvb_get_letohs(tvb, offset);
961 return tvb_get_ntohs(tvb, offset);
966 tvb_get_guint24(tvbuff_t *tvb, const gint offset, const guint encoding) {
967 if (encoding & ENC_LITTLE_ENDIAN) {
968 return tvb_get_letoh24(tvb, offset);
970 return tvb_get_ntoh24(tvb, offset);
975 tvb_get_guint32(tvbuff_t *tvb, const gint offset, const guint encoding) {
976 if (encoding & ENC_LITTLE_ENDIAN) {
977 return tvb_get_letohl(tvb, offset);
979 return tvb_get_ntohl(tvb, offset);
984 tvb_get_guint40(tvbuff_t *tvb, const gint offset, const guint encoding) {
985 if (encoding & ENC_LITTLE_ENDIAN) {
986 return tvb_get_letoh40(tvb, offset);
988 return tvb_get_ntoh40(tvb, offset);
993 tvb_get_gint40(tvbuff_t *tvb, const gint offset, const guint encoding) {
994 if (encoding & ENC_LITTLE_ENDIAN) {
995 return tvb_get_letohi40(tvb, offset);
997 return tvb_get_ntohi40(tvb, offset);
1002 tvb_get_guint48(tvbuff_t *tvb, const gint offset, const guint encoding) {
1003 if (encoding & ENC_LITTLE_ENDIAN) {
1004 return tvb_get_letoh48(tvb, offset);
1006 return tvb_get_ntoh48(tvb, offset);
1011 tvb_get_gint48(tvbuff_t *tvb, const gint offset, const guint encoding) {
1012 if (encoding & ENC_LITTLE_ENDIAN) {
1013 return tvb_get_letohi48(tvb, offset);
1015 return tvb_get_ntohi48(tvb, offset);
1020 tvb_get_guint56(tvbuff_t *tvb, const gint offset, const guint encoding) {
1021 if (encoding & ENC_LITTLE_ENDIAN) {
1022 return tvb_get_letoh56(tvb, offset);
1024 return tvb_get_ntoh56(tvb, offset);
1029 tvb_get_gint56(tvbuff_t *tvb, const gint offset, const guint encoding) {
1030 if (encoding & ENC_LITTLE_ENDIAN) {
1031 return tvb_get_letohi56(tvb, offset);
1033 return tvb_get_ntohi56(tvb, offset);
1038 tvb_get_guint64(tvbuff_t *tvb, const gint offset, const guint encoding) {
1039 if (encoding & ENC_LITTLE_ENDIAN) {
1040 return tvb_get_letoh64(tvb, offset);
1042 return tvb_get_ntoh64(tvb, offset);
1047 tvb_get_ieee_float(tvbuff_t *tvb, const gint offset, const guint encoding) {
1048 if (encoding & ENC_LITTLE_ENDIAN) {
1049 return tvb_get_letohieee_float(tvb, offset);
1051 return tvb_get_ntohieee_float(tvb, offset);
1056 tvb_get_ieee_double(tvbuff_t *tvb, const gint offset, const guint encoding) {
1057 if (encoding & ENC_LITTLE_ENDIAN) {
1058 return tvb_get_letohieee_double(tvb, offset);
1060 return tvb_get_ntohieee_double(tvb, offset);
1065 * Stuff for IEEE float handling on platforms that don't have IEEE
1066 * format as the native floating-point format.
1068 * For now, we treat only the VAX as such a platform.
1070 * XXX - other non-IEEE boxes that can run UNIX include some Crays,
1071 * and possibly other machines.
1073 * It appears that the official Linux port to System/390 and
1074 * zArchitecture uses IEEE format floating point (not a
1077 * I don't know whether there are any other machines that
1078 * could run Wireshark and that don't use IEEE format.
1079 * As far as I know, all of the main commercial microprocessor
1080 * families on which OSes that support Wireshark can run
1081 * use IEEE format (x86, 68k, SPARC, MIPS, PA-RISC, Alpha,
1082 * IA-64, and so on).
1092 #define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
1093 #define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
1094 #define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
1096 #define IEEE_SP_SIGN_MASK 0x80000000
1097 #define IEEE_SP_EXPONENT_MASK 0x7F800000
1098 #define IEEE_SP_MANTISSA_MASK 0x007FFFFF
1099 #define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
1101 #define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
1102 #define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
1103 #define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
1106 ieee_float_is_zero(const guint32 w)
1108 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1112 get_ieee_float(const guint32 w)
1118 sign = w & IEEE_SP_SIGN_MASK;
1119 exponent = w & IEEE_SP_EXPONENT_MASK;
1120 mantissa = w & IEEE_SP_MANTISSA_MASK;
1122 if (ieee_float_is_zero(w)) {
1123 /* number is zero, unnormalized, or not-a-number */
1128 * XXX - how to handle this?
1130 if (IEEE_SP_INFINITY == exponent) {
1132 * number is positive or negative infinity, or a special value
1134 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1138 exponent = ((exponent >> IEEE_SP_MANTISSA_WIDTH) - IEEE_SP_BIAS) -
1139 IEEE_SP_MANTISSA_WIDTH;
1140 mantissa |= IEEE_SP_IMPLIED_BIT;
1143 return -mantissa * pow(2, exponent);
1145 return mantissa * pow(2, exponent);
1150 * We assume that if you don't have IEEE floating-point, you have a
1151 * compiler that understands 64-bit integral quantities.
1153 #define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
1154 #define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
1155 #define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
1157 #define IEEE_DP_SIGN_MASK G_GINT64_CONSTANT(0x8000000000000000)
1158 #define IEEE_DP_EXPONENT_MASK G_GINT64_CONSTANT(0x7FF0000000000000)
1159 #define IEEE_DP_MANTISSA_MASK G_GINT64_CONSTANT(0x000FFFFFFFFFFFFF)
1160 #define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
1162 #define IEEE_DP_IMPLIED_BIT (G_GINT64_CONSTANT(1) << IEEE_DP_MANTISSA_WIDTH)
1163 #define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
1164 #define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
1167 ieee_double_is_zero(const guint64 w)
1169 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1173 get_ieee_double(const guint64 w)
1179 sign = w & IEEE_DP_SIGN_MASK;
1180 exponent = w & IEEE_DP_EXPONENT_MASK;
1181 mantissa = w & IEEE_DP_MANTISSA_MASK;
1183 if (ieee_double_is_zero(w)) {
1184 /* number is zero, unnormalized, or not-a-number */
1189 * XXX - how to handle this?
1191 if (IEEE_DP_INFINITY == exponent) {
1193 * number is positive or negative infinity, or a special value
1195 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1199 exponent = ((exponent >> IEEE_DP_MANTISSA_WIDTH) - IEEE_DP_BIAS) -
1200 IEEE_DP_MANTISSA_WIDTH;
1201 mantissa |= IEEE_DP_IMPLIED_BIT;
1204 return -mantissa * pow(2, exponent);
1206 return mantissa * pow(2, exponent);
1211 * Fetches an IEEE single-precision floating-point number, in
1212 * big-endian form, and returns a "float".
1214 * XXX - should this be "double", in case there are IEEE single-
1215 * precision numbers that won't fit in some platform's native
1219 tvb_get_ntohieee_float(tvbuff_t *tvb, const int offset)
1222 return get_ieee_float(tvb_get_ntohl(tvb, offset));
1229 ieee_fp_union.w = tvb_get_ntohl(tvb, offset);
1230 return ieee_fp_union.f;
1235 * Fetches an IEEE double-precision floating-point number, in
1236 * big-endian form, and returns a "double".
1239 tvb_get_ntohieee_double(tvbuff_t *tvb, const int offset)
1253 #ifdef WORDS_BIGENDIAN
1254 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset);
1255 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset+4);
1257 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset+4);
1258 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset);
1261 return get_ieee_double(ieee_fp_union.dw);
1263 return ieee_fp_union.d;
1268 tvb_get_letohs(tvbuff_t *tvb, const gint offset)
1272 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
1273 return pletoh16(ptr);
1277 tvb_get_letoh24(tvbuff_t *tvb, const gint offset)
1281 ptr = fast_ensure_contiguous(tvb, offset, 3);
1282 return pletoh24(ptr);
1286 tvb_get_letohl(tvbuff_t *tvb, const gint offset)
1290 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1291 return pletoh32(ptr);
1295 tvb_get_letoh40(tvbuff_t *tvb, const gint offset)
1299 ptr = fast_ensure_contiguous(tvb, offset, 5);
1300 return pletoh40(ptr);
1304 tvb_get_letohi40(tvbuff_t *tvb, const gint offset)
1308 ret = ws_sign_ext64(tvb_get_letoh40(tvb, offset), 40);
1314 tvb_get_letoh48(tvbuff_t *tvb, const gint offset)
1318 ptr = fast_ensure_contiguous(tvb, offset, 6);
1319 return pletoh48(ptr);
1323 tvb_get_letohi48(tvbuff_t *tvb, const gint offset)
1327 ret = ws_sign_ext64(tvb_get_letoh48(tvb, offset), 48);
1333 tvb_get_letoh56(tvbuff_t *tvb, const gint offset)
1337 ptr = fast_ensure_contiguous(tvb, offset, 7);
1338 return pletoh56(ptr);
1342 tvb_get_letohi56(tvbuff_t *tvb, const gint offset)
1346 ret = ws_sign_ext64(tvb_get_letoh56(tvb, offset), 56);
1352 tvb_get_letoh64(tvbuff_t *tvb, const gint offset)
1356 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
1357 return pletoh64(ptr);
1361 * Fetches an IEEE single-precision floating-point number, in
1362 * little-endian form, and returns a "float".
1364 * XXX - should this be "double", in case there are IEEE single-
1365 * precision numbers that won't fit in some platform's native
1369 tvb_get_letohieee_float(tvbuff_t *tvb, const int offset)
1372 return get_ieee_float(tvb_get_letohl(tvb, offset));
1379 ieee_fp_union.w = tvb_get_letohl(tvb, offset);
1380 return ieee_fp_union.f;
1385 * Fetches an IEEE double-precision floating-point number, in
1386 * little-endian form, and returns a "double".
1389 tvb_get_letohieee_double(tvbuff_t *tvb, const int offset)
1403 #ifdef WORDS_BIGENDIAN
1404 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset+4);
1405 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset);
1407 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset);
1408 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset+4);
1411 return get_ieee_double(ieee_fp_union.dw);
1413 return ieee_fp_union.d;
1418 validate_single_byte_ascii_encoding(const guint encoding)
1420 const guint enc = encoding & ~ENC_STR_MASK;
1426 case ENC_3GPP_TS_23_038_7BITS:
1428 REPORT_DISSECTOR_BUG("Invalid string encoding type passed to tvb_get_string_XXX");
1433 /* make sure something valid was set */
1435 REPORT_DISSECTOR_BUG("No string encoding type passed to tvb_get_string_XXX");
1439 tvb_get_string_bytes(tvbuff_t *tvb, const gint offset, const gint length,
1440 const guint encoding, GByteArray *bytes, gint *endoff)
1442 const gchar *ptr = (gchar*) tvb_get_raw_string(wmem_packet_scope(), tvb, offset, length);
1443 const gchar *begin = ptr;
1444 const gchar *end = NULL;
1445 GByteArray *retval = NULL;
1449 validate_single_byte_ascii_encoding(encoding);
1451 if (endoff) *endoff = 0;
1453 while (*begin == ' ') begin++;
1455 if (*begin && bytes) {
1456 if (hex_str_to_bytes_encoding(begin, bytes, &end, encoding, FALSE)) {
1457 if (bytes->len > 0) {
1458 if (endoff) *endoff = offset + (gint)(end - ptr);
1468 /* support hex-encoded time values? */
1470 tvb_get_string_time(tvbuff_t *tvb, const gint offset, const gint length,
1471 const guint encoding, nstime_t *ns, gint *endoff)
1473 const gchar *begin = (gchar*) tvb_get_raw_string(wmem_packet_scope(), tvb, offset, length);
1474 const gchar *ptr = begin;
1475 const gchar *end = NULL;
1477 nstime_t* retval = NULL;
1482 gboolean matched = FALSE;
1486 validate_single_byte_ascii_encoding(encoding);
1488 DISSECTOR_ASSERT(ns);
1490 memset(&tm, 0, sizeof(tm));
1495 while (*ptr == ' ') ptr++;
1498 /* note: sscanf is known to be inconsistent across platforms with respect
1499 to whether a %n is counted as a return value or not, so we have to use
1501 if ((encoding & ENC_ISO_8601_DATE_TIME) == ENC_ISO_8601_DATE_TIME) {
1502 /* TODO: using sscanf this many times is probably slow; might want
1503 to parse it by hand in the future */
1504 /* 2014-04-07T05:41:56+00:00 */
1505 if (sscanf(ptr, "%d-%d-%d%*c%d:%d:%d%c%d:%d%n",
1519 /* no seconds is ok */
1520 else if (sscanf(ptr, "%d-%d-%d%*c%d:%d%c%d:%d%n",
1533 /* 2007-04-05T14:30:56Z */
1534 else if (sscanf(ptr, "%d-%d-%d%*c%d:%d:%dZ%n",
1547 /* 2007-04-05T14:30Z no seconds is ok */
1548 else if (sscanf(ptr, "%d-%d-%d%*c%d:%dZ%n",
1563 end = ptr + num_chars;
1565 if (tm.tm_year > 1900) tm.tm_year -= 1900;
1566 if (sign == '-') off_hr = -off_hr;
1569 else if (encoding & ENC_ISO_8601_DATE) {
1571 if (sscanf(ptr, "%d-%d-%d%n",
1578 end = ptr + num_chars;
1580 if (tm.tm_year > 1900) tm.tm_year -= 1900;
1583 else if (encoding & ENC_ISO_8601_TIME) {
1585 if (sscanf(ptr, "%d:%d:%d%n",
1591 /* what should we do about day/month/year? */
1592 /* setting it to "now" for now */
1593 time_t time_now = time(NULL);
1594 struct tm *tm_now = gmtime(&time_now);
1595 tm.tm_year = tm_now->tm_year;
1596 tm.tm_mon = tm_now->tm_mon;
1597 tm.tm_mday = tm_now->tm_mday;
1598 end = ptr + num_chars;
1603 else if (encoding & ENC_RFC_822 || encoding & ENC_RFC_1123) {
1604 if (encoding & ENC_RFC_822) {
1605 /* this will unfortunately match ENC_RFC_1123 style
1606 strings too, partially - probably need to do this the long way */
1607 end = strptime(ptr, "%a, %d %b %y %H:%M:%S", &tm);
1608 if (!end) end = strptime(ptr, "%a, %d %b %y %H:%M", &tm);
1609 if (!end) end = strptime(ptr, "%d %b %y %H:%M:%S", &tm);
1610 if (!end) end = strptime(ptr, "%d %b %y %H:%M", &tm);
1612 else if (encoding & ENC_RFC_1123) {
1613 end = strptime(ptr, "%a, %d %b %Y %H:%M:%S", &tm);
1614 if (!end) end = strptime(ptr, "%a, %d %b %Y %H:%M", &tm);
1615 if (!end) end = strptime(ptr, "%d %b %Y %H:%M:%S", &tm);
1616 if (!end) end = strptime(ptr, "%d %b %Y %H:%M", &tm);
1620 if (*end == ' ') end++;
1621 if (g_ascii_strncasecmp(end, "UT", 2) == 0)
1625 else if (g_ascii_strncasecmp(end, "GMT", 3) == 0)
1629 else if (sscanf(end, "%c%2d%2d%n",
1637 if (sign == '-') off_hr = -off_hr;
1643 ns->secs = mktime_utc (&tm);
1645 ns->secs += (off_hr * 3600) + (off_min * 60);
1646 else if (off_hr < 0)
1647 ns->secs -= ((-off_hr) * 3600) + (off_min * 60);
1650 *endoff = (gint)(offset + (end - begin));
1656 /* Fetch an IPv4 address, in network byte order.
1657 * We do *not* convert them to host byte order; we leave them in
1658 * network byte order. */
1660 tvb_get_ipv4(tvbuff_t *tvb, const gint offset)
1665 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1666 memcpy(&addr, ptr, sizeof addr);
1670 /* Fetch an IPv6 address. */
1672 tvb_get_ipv6(tvbuff_t *tvb, const gint offset, struct e_in6_addr *addr)
1676 ptr = ensure_contiguous(tvb, offset, sizeof(*addr));
1677 memcpy(addr, ptr, sizeof *addr);
1682 tvb_get_ntohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid)
1684 const guint8 *ptr = ensure_contiguous(tvb, offset, GUID_LEN);
1686 guid->data1 = pntoh32(ptr + 0);
1687 guid->data2 = pntoh16(ptr + 4);
1688 guid->data3 = pntoh16(ptr + 6);
1689 memcpy(guid->data4, ptr + 8, sizeof guid->data4);
1693 tvb_get_letohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid)
1695 const guint8 *ptr = ensure_contiguous(tvb, offset, GUID_LEN);
1697 guid->data1 = pletoh32(ptr + 0);
1698 guid->data2 = pletoh16(ptr + 4);
1699 guid->data3 = pletoh16(ptr + 6);
1700 memcpy(guid->data4, ptr + 8, sizeof guid->data4);
1704 * NOTE: to support code written when proto_tree_add_item() took a
1705 * gboolean as its last argument, with FALSE meaning "big-endian"
1706 * and TRUE meaning "little-endian", we treat any non-zero value of
1707 * "encoding" as meaning "little-endian".
1710 tvb_get_guid(tvbuff_t *tvb, const gint offset, e_guid_t *guid, const guint encoding)
1713 tvb_get_letohguid(tvb, offset, guid);
1715 tvb_get_ntohguid(tvb, offset, guid);
1719 static const guint8 bit_mask8[] = {
1731 /* Get 1 - 8 bits */
1733 tvb_get_bits8(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits)
1735 return (guint8)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
1738 /* Get 9 - 16 bits */
1740 tvb_get_bits16(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits,const guint encoding _U_)
1742 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1743 return (guint16)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
1746 /* Get 1 - 32 bits */
1748 tvb_get_bits32(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding _U_)
1750 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1751 return (guint32)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
1754 /* Get 1 - 64 bits */
1756 tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding _U_)
1758 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1759 return _tvb_get_bits64(tvb, bit_offset, no_of_bits);
1762 * This function will dissect a sequence of bits that does not need to be byte aligned; the bits
1763 * set will be shown in the tree as ..10 10.. and the integer value returned if return_value is set.
1764 * Offset should be given in bits from the start of the tvb.
1765 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
1768 _tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint total_no_of_bits)
1771 guint octet_offset = bit_offset >> 3;
1772 guint8 required_bits_in_first_octet = 8 - (bit_offset % 8);
1774 if(required_bits_in_first_octet > total_no_of_bits)
1776 /* the required bits don't extend to the end of the first octet */
1777 guint8 right_shift = required_bits_in_first_octet - total_no_of_bits;
1778 value = (tvb_get_guint8(tvb, octet_offset) >> right_shift) & bit_mask8[total_no_of_bits % 8];
1782 guint8 remaining_bit_length = total_no_of_bits;
1784 /* get the bits up to the first octet boundary */
1786 required_bits_in_first_octet %= 8;
1787 if(required_bits_in_first_octet != 0)
1789 value = tvb_get_guint8(tvb, octet_offset) & bit_mask8[required_bits_in_first_octet];
1790 remaining_bit_length -= required_bits_in_first_octet;
1793 /* take the biggest words, shorts or octets that we can */
1794 while (remaining_bit_length > 7)
1796 switch (remaining_bit_length >> 4)
1799 /* 8 - 15 bits. (note that 0 - 7 would have dropped out of the while() loop) */
1801 value += tvb_get_guint8(tvb, octet_offset);
1802 remaining_bit_length -= 8;
1809 value += tvb_get_ntohs(tvb, octet_offset);
1810 remaining_bit_length -= 16;
1818 value += tvb_get_ntohl(tvb, octet_offset);
1819 remaining_bit_length -= 32;
1824 /* 64 bits (or more???) */
1825 value = tvb_get_ntoh64(tvb, octet_offset);
1826 remaining_bit_length -= 64;
1831 /* get bits from any partial octet at the tail */
1832 if(remaining_bit_length)
1834 value <<= remaining_bit_length;
1835 value += (tvb_get_guint8(tvb, octet_offset) >> (8 - remaining_bit_length));
1840 /* Get 1 - 32 bits (should be deprecated as same as tvb_get_bits32??) */
1842 tvb_get_bits(tvbuff_t *tvb, const guint bit_offset, const gint no_of_bits, const guint encoding _U_)
1844 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1845 return (guint32)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
1849 tvb_find_guint8_generic(tvbuff_t *tvb, guint abs_offset, guint limit, guint8 needle)
1852 const guint8 *result;
1854 ptr = ensure_contiguous(tvb, abs_offset, limit); /* tvb_get_ptr() */
1856 result = (const guint8 *) memchr(ptr, needle, limit);
1860 return (gint) ((result - ptr) + abs_offset);
1863 /* Find first occurrence of needle in tvbuff, starting at offset. Searches
1864 * at most maxlength number of bytes; if maxlength is -1, searches to
1866 * Returns the offset of the found needle, or -1 if not found.
1867 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1868 * in that case, -1 will be returned if the boundary is reached before
1869 * finding needle. */
1871 tvb_find_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 needle)
1873 const guint8 *result;
1878 DISSECTOR_ASSERT(tvb && tvb->initialized);
1880 exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &limit);
1884 /* Only search to end of tvbuff, w/o throwing exception. */
1885 if (limit > (guint) maxlength) {
1886 /* Maximum length doesn't go past end of tvbuff; search
1891 /* If we have real data, perform our search now. */
1892 if (tvb->real_data) {
1893 result = (const guint8 *)memchr(tvb->real_data + abs_offset, needle, limit);
1894 if (result == NULL) {
1898 return (gint) (result - tvb->real_data);
1902 if (tvb->ops->tvb_find_guint8)
1903 return tvb->ops->tvb_find_guint8(tvb, abs_offset, limit, needle);
1905 return tvb_find_guint8_generic(tvb, offset, limit, needle);
1909 tvb_pbrk_guint8_generic(tvbuff_t *tvb, guint abs_offset, guint limit, const guint8 *needles, guchar *found_needle)
1912 const guint8 *result;
1914 ptr = ensure_contiguous(tvb, abs_offset, limit); /* tvb_get_ptr */
1916 result = guint8_pbrk(ptr, limit, needles, found_needle);
1920 return (gint) ((result - ptr) + abs_offset);
1923 /* Find first occurrence of any of the needles in tvbuff, starting at offset.
1924 * Searches at most maxlength number of bytes; if maxlength is -1, searches
1926 * Returns the offset of the found needle, or -1 if not found.
1927 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1928 * in that case, -1 will be returned if the boundary is reached before
1929 * finding needle. */
1931 tvb_pbrk_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 *needles, guchar *found_needle)
1933 const guint8 *result;
1938 DISSECTOR_ASSERT(tvb && tvb->initialized);
1940 exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &limit);
1944 /* Only search to end of tvbuff, w/o throwing exception. */
1945 if (limit > (guint) maxlength) {
1946 /* Maximum length doesn't go past end of tvbuff; search
1951 /* If we have real data, perform our search now. */
1952 if (tvb->real_data) {
1953 result = guint8_pbrk(tvb->real_data + abs_offset, limit, needles, found_needle);
1954 if (result == NULL) {
1958 return (gint) (result - tvb->real_data);
1962 if (tvb->ops->tvb_pbrk_guint8)
1963 return tvb->ops->tvb_pbrk_guint8(tvb, abs_offset, limit, needles, found_needle);
1965 return tvb_pbrk_guint8_generic(tvb, abs_offset, limit, needles, found_needle);
1968 /* Find size of stringz (NUL-terminated string) by looking for terminating
1969 * NUL. The size of the string includes the terminating NUL.
1971 * If the NUL isn't found, it throws the appropriate exception.
1974 tvb_strsize(tvbuff_t *tvb, const gint offset)
1976 guint abs_offset, junk_length;
1979 DISSECTOR_ASSERT(tvb && tvb->initialized);
1981 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1982 nul_offset = tvb_find_guint8(tvb, abs_offset, -1, 0);
1983 if (nul_offset == -1) {
1985 * OK, we hit the end of the tvbuff, so we should throw
1988 * Did we hit the end of the captured data, or the end
1989 * of the actual data? If there's less captured data
1990 * than actual data, we presumably hit the end of the
1991 * captured data, otherwise we hit the end of the actual
1994 if (tvb->length < tvb->reported_length) {
1997 if (tvb->flags & TVBUFF_FRAGMENT) {
1998 THROW(FragmentBoundsError);
2000 THROW(ReportedBoundsError);
2004 return (nul_offset - abs_offset) + 1;
2007 /* UTF-16/UCS-2 version of tvb_strsize */
2008 /* Returns number of bytes including the (two-bytes) null terminator */
2010 tvb_unicode_strsize(tvbuff_t *tvb, const gint offset)
2015 DISSECTOR_ASSERT(tvb && tvb->initialized);
2018 /* Endianness doesn't matter when looking for null */
2019 uchar = tvb_get_ntohs(tvb, offset + i);
2021 } while(uchar != 0);
2026 /* Find length of string by looking for end of string ('\0'), up to
2027 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
2029 * Returns -1 if 'maxlength' reached before finding EOS. */
2031 tvb_strnlen(tvbuff_t *tvb, const gint offset, const guint maxlength)
2034 guint abs_offset, junk_length;
2036 DISSECTOR_ASSERT(tvb && tvb->initialized);
2038 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
2040 result_offset = tvb_find_guint8(tvb, abs_offset, maxlength, 0);
2042 if (result_offset == -1) {
2046 return result_offset - abs_offset;
2051 * Implement strneql etc
2055 * Call strncmp after checking if enough chars left, returning 0 if
2056 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
2059 tvb_strneql(tvbuff_t *tvb, const gint offset, const gchar *str, const size_t size)
2063 ptr = ensure_contiguous_no_exception(tvb, offset, (gint)size, NULL);
2066 int cmp = strncmp((const char *)ptr, str, size);
2069 * Return 0 if equal, -1 otherwise.
2071 return (cmp == 0 ? 0 : -1);
2074 * Not enough characters in the tvbuff to match the
2082 * Call g_ascii_strncasecmp after checking if enough chars left, returning
2083 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
2086 tvb_strncaseeql(tvbuff_t *tvb, const gint offset, const gchar *str, const size_t size)
2090 ptr = ensure_contiguous_no_exception(tvb, offset, (gint)size, NULL);
2093 int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
2096 * Return 0 if equal, -1 otherwise.
2098 return (cmp == 0 ? 0 : -1);
2101 * Not enough characters in the tvbuff to match the
2109 * Call memcmp after checking if enough chars left, returning 0 if
2110 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
2113 tvb_memeql(tvbuff_t *tvb, const gint offset, const guint8 *str, size_t size)
2117 ptr = ensure_contiguous_no_exception(tvb, offset, (gint) size, NULL);
2120 int cmp = memcmp(ptr, str, size);
2123 * Return 0 if equal, -1 otherwise.
2125 return (cmp == 0 ? 0 : -1);
2128 * Not enough characters in the tvbuff to match the
2136 * Format the data in the tvb from offset for length ...
2139 tvb_format_text(tvbuff_t *tvb, const gint offset, const gint size)
2144 len = (size > 0) ? size : 0;
2146 ptr = ensure_contiguous(tvb, offset, size);
2147 return format_text(ptr, len);
2151 * Format the data in the tvb from offset for length ...
2154 tvb_format_text_wsp(tvbuff_t *tvb, const gint offset, const gint size)
2159 len = (size > 0) ? size : 0;
2161 ptr = ensure_contiguous(tvb, offset, size);
2162 return format_text_wsp(ptr, len);
2166 * Like "tvb_format_text()", but for null-padded strings; don't show
2167 * the null padding characters as "\000".
2170 tvb_format_stringzpad(tvbuff_t *tvb, const gint offset, const gint size)
2172 const guint8 *ptr, *p;
2176 len = (size > 0) ? size : 0;
2178 ptr = ensure_contiguous(tvb, offset, size);
2179 for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
2181 return format_text(ptr, stringlen);
2185 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
2186 * the null padding characters as "\000".
2189 tvb_format_stringzpad_wsp(tvbuff_t *tvb, const gint offset, const gint size)
2191 const guint8 *ptr, *p;
2195 len = (size > 0) ? size : 0;
2197 ptr = ensure_contiguous(tvb, offset, size);
2198 for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
2200 return format_text_wsp(ptr, stringlen);
2203 /* Unicode REPLACEMENT CHARACTER */
2204 #define UNREPL 0x00FFFD
2207 * All string functions below take a scope as an argument.
2210 * If scope is NULL, memory is allocated with g_malloc() and user must
2211 * explicitly free it with g_free().
2212 * If scope is not NULL, memory is allocated with the corresponding pool
2215 * All functions throw an exception if the tvbuff ends before the string
2220 * Given a wmem scope, tvbuff, an offset, and a length, treat the string
2221 * of bytes referred to by the tvbuff, offset, and length as an ASCII string,
2222 * with all bytes with the high-order bit set being invalid, and return a
2223 * pointer to a UTF-8 string, allocated using the wmem scope.
2225 * Octets with the highest bit set will be converted to the Unicode
2226 * REPLACEMENT CHARACTER.
2229 tvb_get_ascii_string(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint length)
2233 ptr = ensure_contiguous(tvb, offset, length);
2234 return get_ascii_string(scope, ptr, length);
2238 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
2239 * of bytes referred to by the tvbuff, the offset. and the length as a UTF-8
2240 * string, and return a pointer to that string, allocated using the wmem scope.
2242 * XXX - should map invalid UTF-8 sequences to UNREPL.
2245 tvb_get_utf_8_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length)
2249 tvb_ensure_bytes_exist(tvb, offset, length); /* make sure length = -1 fails */
2250 strbuf = (guint8 *)wmem_alloc(scope, length + 1);
2251 tvb_memcpy(tvb, strbuf, offset, length);
2252 strbuf[length] = '\0';
2257 * Given a wmem scope, tvbuff, an offset, and a length, treat the string
2258 * of bytes referred to by the tvbuff, the offset, and the length as a
2259 * raw string, and return a pointer to that string, allocated using the
2260 * wmem scope. This means a null is appended at the end, but no replacement
2261 * checking is done otherwise. Currently tvb_get_utf_8_string() does not
2262 * replace either, but it might in the future.
2264 * Also, this one allows a length of -1 to mean get all, but does not
2265 * allow a negative offset.
2267 static inline guint8 *
2268 tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint length)
2271 gint abs_length = length;
2273 DISSECTOR_ASSERT(offset >= 0);
2274 DISSECTOR_ASSERT(abs_length >= -1);
2277 abs_length = tvb->length - offset;
2279 tvb_ensure_bytes_exist(tvb, offset, abs_length);
2280 strbuf = (guint8 *)wmem_alloc(scope, abs_length + 1);
2281 tvb_memcpy(tvb, strbuf, offset, abs_length);
2282 strbuf[abs_length] = '\0';
2287 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
2288 * of bytes referred to by the tvbuff, the offset, and the length as an
2289 * ISO 8859/1 string, and return a pointer to a UTF-8 string, allocated
2290 * using the wmem scope.
2293 tvb_get_string_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint length)
2297 ptr = ensure_contiguous(tvb, offset, length);
2298 return get_8859_1_string(scope, ptr, length);
2302 * Given a wmem scope, a tvbuff, an offset, and a length, and a translation
2303 * table, treat the string of bytes referred to by the tvbuff, the offset,
2304 * and the length as a string encoded using one octet per character, with
2305 * octets with the high-order bit clear being ASCII and octets with the
2306 * high-order bit set being mapped by the translation table to 2-byte
2307 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
2308 * CHARACTER), and return a pointer to a UTF-8 string, allocated with the
2312 tvb_get_string_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint length, const gunichar2 table[0x80])
2316 ptr = ensure_contiguous(tvb, offset, length);
2317 return get_unichar2_string(scope, ptr, length, table);
2321 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
2322 * giving the byte order, treat the string of bytes referred to by the
2323 * tvbuff, the offset, and the length as a UCS-2 encoded string in
2324 * the byte order in question, containing characters from the Basic
2325 * Multilingual Plane (plane 0) of Unicode, and return a pointer to a
2326 * UTF-8 string, allocated with the wmem scope.
2328 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN.
2330 * Specify length in bytes.
2332 * XXX - should map lead and trail surrogate values to REPLACEMENT
2333 * CHARACTERs (0xFFFD)?
2334 * XXX - if there are an odd number of bytes, should put a
2335 * REPLACEMENT CHARACTER at the end.
2338 tvb_get_ucs_2_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint length, const guint encoding)
2342 ptr = ensure_contiguous(tvb, offset, length);
2343 return get_ucs_2_string(scope, ptr, length, encoding);
2347 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
2348 * giving the byte order, treat the string of bytes referred to by the
2349 * tvbuff, the offset, and the length as a UTF-16 encoded string in
2350 * the byte order in question, and return a pointer to a UTF-8 string,
2351 * allocated with the wmem scope.
2353 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN.
2355 * Specify length in bytes.
2357 * XXX - should map surrogate errors to REPLACEMENT CHARACTERs (0xFFFD).
2358 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
2359 * XXX - if there are an odd number of bytes, should put a
2360 * REPLACEMENT CHARACTER at the end.
2363 tvb_get_utf_16_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint length, const guint encoding)
2367 ptr = ensure_contiguous(tvb, offset, length);
2368 return get_utf_16_string(scope, ptr, length, encoding);
2372 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
2373 * giving the byte order, treat the string of bytes referred to by the
2374 * tvbuff, the offset, and the length as a UCS-4 encoded string in
2375 * the byte order in question, and return a pointer to a UTF-8 string,
2376 * allocated with the wmem scope.
2378 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
2380 * Specify length in bytes
2382 * XXX - should map lead and trail surrogate values to a "substitute"
2384 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
2385 * XXX - if the number of bytes isn't a multiple of 4, should put a
2386 * REPLACEMENT CHARACTER at the end.
2389 tvb_get_ucs_4_string(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint length, const guint encoding)
2393 ptr = ensure_contiguous(tvb, offset, length);
2394 return get_ucs_4_string(scope, ptr, length, encoding);
2398 tvb_get_ts_23_038_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb,
2399 const gint bit_offset, gint no_of_chars)
2401 gint in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
2402 gint length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
2405 DISSECTOR_ASSERT(tvb && tvb->initialized);
2407 ptr = ensure_contiguous(tvb, in_offset, length);
2408 return get_ts_23_038_7bits_string(scope, ptr, bit_offset, no_of_chars);
2412 tvb_get_ascii_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb,
2413 const gint bit_offset, gint no_of_chars)
2415 gint in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
2416 gint length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
2419 DISSECTOR_ASSERT(tvb && tvb->initialized);
2421 ptr = ensure_contiguous(tvb, in_offset, length);
2422 return get_ascii_7bits_string(scope, ptr, bit_offset, no_of_chars);
2426 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
2427 * of bytes referred to by the tvbuff, offset, and length as a string encoded
2428 * in EBCDIC using one octet per character, and return a pointer to a
2429 * UTF-8 string, allocated using the wmem scope.
2432 tvb_get_ebcdic_string(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint length)
2436 ptr = ensure_contiguous(tvb, offset, length);
2437 return get_ebcdic_string(scope, ptr, length);
2441 * Given a tvbuff, an offset, a length, and an encoding, allocate a
2442 * buffer big enough to hold a non-null-terminated string of that length
2443 * at that offset, plus a trailing '\0', copy into the buffer the
2444 * string as converted from the appropriate encoding to UTF-8, and
2445 * return a pointer to the string.
2448 tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset,
2449 const gint length, const guint encoding)
2453 DISSECTOR_ASSERT(tvb && tvb->initialized);
2455 /* make sure length = -1 fails */
2457 THROW(ReportedBoundsError);
2460 switch (encoding & ENC_CHARENCODING_MASK) {
2465 * For now, we treat bogus values as meaning
2466 * "ASCII" rather than reporting an error,
2467 * for the benefit of old dissectors written
2468 * when the last argument to proto_tree_add_item()
2469 * was a gboolean for the byte order, not an
2470 * encoding value, and passed non-zero values
2471 * other than TRUE to mean "little-endian".
2473 strptr = tvb_get_ascii_string(scope, tvb, offset, length);
2478 * XXX - should map lead and trail surrogate value code
2479 * points to a "substitute" UTF-8 character?
2480 * XXX - should map code points > 10FFFF to REPLACEMENT
2483 strptr = tvb_get_utf_8_string(scope, tvb, offset, length);
2487 strptr = tvb_get_utf_16_string(scope, tvb, offset, length,
2488 encoding & ENC_LITTLE_ENDIAN);
2492 strptr = tvb_get_ucs_2_string(scope, tvb, offset, length,
2493 encoding & ENC_LITTLE_ENDIAN);
2497 strptr = tvb_get_ucs_4_string(scope, tvb, offset, length,
2498 encoding & ENC_LITTLE_ENDIAN);
2501 case ENC_ISO_8859_1:
2503 * ISO 8859-1 printable code point values are equal
2504 * to the equivalent Unicode code point value, so
2505 * no translation table is needed.
2507 strptr = tvb_get_string_8859_1(scope, tvb, offset, length);
2510 case ENC_ISO_8859_2:
2511 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_2);
2514 case ENC_ISO_8859_3:
2515 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_3);
2518 case ENC_ISO_8859_4:
2519 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_4);
2522 case ENC_ISO_8859_5:
2523 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_5);
2526 case ENC_ISO_8859_6:
2527 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_6);
2530 case ENC_ISO_8859_7:
2531 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_7);
2534 case ENC_ISO_8859_8:
2535 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_8);
2538 case ENC_ISO_8859_9:
2539 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_9);
2542 case ENC_ISO_8859_10:
2543 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_10);
2546 case ENC_ISO_8859_11:
2547 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_11);
2550 case ENC_ISO_8859_13:
2551 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_13);
2554 case ENC_ISO_8859_14:
2555 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_14);
2558 case ENC_ISO_8859_15:
2559 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_15);
2562 case ENC_ISO_8859_16:
2563 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_16);
2566 case ENC_WINDOWS_1250:
2567 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1250);
2571 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_mac_roman);
2575 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp437);
2578 case ENC_3GPP_TS_23_038_7BITS:
2580 gint bit_offset = offset << 3;
2581 gint no_of_chars = (length << 3) / 7;
2582 strptr = tvb_get_ts_23_038_7bits_string(scope, tvb, bit_offset, no_of_chars);
2586 case ENC_ASCII_7BITS:
2588 gint bit_offset = offset << 3;
2589 gint no_of_chars = (length << 3) / 7;
2590 strptr = tvb_get_ascii_7bits_string(scope, tvb, bit_offset, no_of_chars);
2596 * XXX - multiple "dialects" of EBCDIC?
2598 strptr = tvb_get_ebcdic_string(scope, tvb, offset, length);
2605 * This is like tvb_get_string_enc(), except that it handles null-padded
2608 * Currently, string values are stored as UTF-8 null-terminated strings,
2609 * so nothing needs to be done differently for null-padded strings; we
2610 * could save a little memory by not storing the null padding.
2612 * If we ever store string values differently, in a fashion that doesn't
2613 * involve null termination, that might change.
2616 tvb_get_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset,
2617 const gint length, const guint encoding)
2619 return tvb_get_string_enc(scope, tvb, offset, length, encoding);
2623 * These routines are like the above routines, except that they handle
2624 * null-terminated strings. They find the length of that string (and
2625 * throw an exception if the tvbuff ends before we find the null), and
2626 * also return through a pointer the length of the string, in bytes,
2627 * including the terminating null (the terminating null being 2 bytes
2628 * for UCS-2 and UTF-16, 4 bytes for UCS-4, and 1 byte for other
2632 tvb_get_ascii_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint *lengthp)
2637 size = tvb_strsize(tvb, offset);
2638 ptr = ensure_contiguous(tvb, offset, size);
2639 /* XXX, conversion between signed/unsigned integer */
2642 return get_ascii_string(scope, ptr, size);
2646 tvb_get_utf_8_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp)
2651 size = tvb_strsize(tvb, offset);
2652 strptr = (guint8 *)wmem_alloc(scope, size);
2653 tvb_memcpy(tvb, strptr, offset, size);
2660 tvb_get_stringz_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint *lengthp)
2665 size = tvb_strsize(tvb, offset);
2666 ptr = ensure_contiguous(tvb, offset, size);
2667 /* XXX, conversion between signed/unsigned integer */
2670 return get_8859_1_string(scope, ptr, size);
2674 tvb_get_stringz_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint *lengthp, const gunichar2 table[0x80])
2679 size = tvb_strsize(tvb, offset);
2680 ptr = ensure_contiguous(tvb, offset, size);
2681 /* XXX, conversion between signed/unsigned integer */
2684 return get_unichar2_string(scope, ptr, size, table);
2688 * Given a tvbuff and an offset, with the offset assumed to refer to
2689 * a null-terminated string, find the length of that string (and throw
2690 * an exception if the tvbuff ends before we find the null), ensure that
2691 * the TVB is flat, and return a pointer to the string (in the TVB).
2692 * Also return the length of the string (including the terminating null)
2693 * through a pointer.
2695 * As long as we aren't using composite TVBs, this saves the cycles used
2696 * (often unnecessariliy) in allocating a buffer and copying the string into
2697 * it. (If we do start using composite TVBs, we may want to replace this
2698 * function with the _ephemeral version.)
2701 tvb_get_const_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
2704 const guint8 *strptr;
2706 size = tvb_strsize(tvb, offset);
2707 strptr = ensure_contiguous(tvb, offset, size);
2714 tvb_get_ucs_2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
2716 gint size; /* Number of bytes in string */
2719 size = tvb_unicode_strsize(tvb, offset);
2720 ptr = ensure_contiguous(tvb, offset, size);
2721 /* XXX, conversion between signed/unsigned integer */
2724 return get_ucs_2_string(scope, ptr, size, encoding);
2728 tvb_get_utf_16_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
2733 size = tvb_unicode_strsize(tvb, offset);
2734 ptr = ensure_contiguous(tvb, offset, size);
2735 /* XXX, conversion between signed/unsigned integer */
2738 return get_utf_16_string(scope, ptr, size, encoding);
2742 tvb_get_ucs_4_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
2750 /* Endianness doesn't matter when looking for null */
2751 uchar = tvb_get_ntohl(tvb, offset + size);
2753 } while(uchar != 0);
2755 ptr = ensure_contiguous(tvb, offset, size);
2756 /* XXX, conversion between signed/unsigned integer */
2759 return get_ucs_4_string(scope, ptr, size, encoding);
2763 tvb_get_ebcdic_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, gint offset, gint *lengthp)
2768 size = tvb_strsize(tvb, offset);
2769 ptr = ensure_contiguous(tvb, offset, size);
2770 /* XXX, conversion between signed/unsigned integer */
2773 return get_ebcdic_string(scope, ptr, size);
2777 tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
2781 DISSECTOR_ASSERT(tvb && tvb->initialized);
2783 switch (encoding & ENC_CHARENCODING_MASK) {
2788 * For now, we treat bogus values as meaning
2789 * "ASCII" rather than reporting an error,
2790 * for the benefit of old dissectors written
2791 * when the last argument to proto_tree_add_item()
2792 * was a gboolean for the byte order, not an
2793 * encoding value, and passed non-zero values
2794 * other than TRUE to mean "little-endian".
2796 strptr = tvb_get_ascii_stringz(scope, tvb, offset, lengthp);
2801 * XXX - should map all invalid UTF-8 sequences
2802 * to a "substitute" UTF-8 character.
2803 * XXX - should map code points > 10FFFF to REPLACEMENT
2806 strptr = tvb_get_utf_8_stringz(scope, tvb, offset, lengthp);
2810 strptr = tvb_get_utf_16_stringz(scope, tvb, offset, lengthp,
2811 encoding & ENC_LITTLE_ENDIAN);
2815 strptr = tvb_get_ucs_2_stringz(scope, tvb, offset, lengthp,
2816 encoding & ENC_LITTLE_ENDIAN);
2820 strptr = tvb_get_ucs_4_stringz(scope, tvb, offset, lengthp,
2821 encoding & ENC_LITTLE_ENDIAN);
2824 case ENC_ISO_8859_1:
2826 * ISO 8859-1 printable code point values are equal
2827 * to the equivalent Unicode code point value, so
2828 * no translation table is needed.
2830 strptr = tvb_get_stringz_8859_1(scope, tvb, offset, lengthp);
2833 case ENC_ISO_8859_2:
2834 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_2);
2837 case ENC_ISO_8859_3:
2838 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_3);
2841 case ENC_ISO_8859_4:
2842 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_4);
2845 case ENC_ISO_8859_5:
2846 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_5);
2849 case ENC_ISO_8859_6:
2850 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_6);
2853 case ENC_ISO_8859_7:
2854 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_7);
2857 case ENC_ISO_8859_8:
2858 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_8);
2861 case ENC_ISO_8859_9:
2862 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_9);
2865 case ENC_ISO_8859_10:
2866 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_10);
2869 case ENC_ISO_8859_11:
2870 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_11);
2873 case ENC_ISO_8859_13:
2874 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_13);
2877 case ENC_ISO_8859_14:
2878 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_14);
2881 case ENC_ISO_8859_15:
2882 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_15);
2885 case ENC_ISO_8859_16:
2886 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_16);
2889 case ENC_WINDOWS_1250:
2890 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1250);
2894 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_mac_roman);
2898 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp437);
2901 case ENC_3GPP_TS_23_038_7BITS:
2902 REPORT_DISSECTOR_BUG("TS 23.038 7bits has no null character and doesn't support null-terminated strings");
2905 case ENC_ASCII_7BITS:
2906 REPORT_DISSECTOR_BUG("tvb_get_stringz_enc function with ENC_ASCII_7BITS not implemented yet");
2911 * XXX - multiple "dialects" of EBCDIC?
2913 strptr = tvb_get_ebcdic_stringz(scope, tvb, offset, lengthp);
2920 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2921 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2922 * Returns length of string (not including terminating NUL), or -1 if the string was
2923 * truncated in the buffer due to not having reached the terminating NUL.
2924 * In this way, it acts like g_snprintf().
2926 * bufsize MUST be greater than 0.
2928 * When processing a packet where the remaining number of bytes is less
2929 * than bufsize, an exception is not thrown if the end of the packet
2930 * is reached before the NUL is found. If no NUL is found before reaching
2931 * the end of the short packet, -1 is still returned, and the string
2932 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2933 * at the correct spot, terminating the string.
2935 * *bytes_copied will contain the number of bytes actually copied,
2936 * including the terminating-NUL.
2939 _tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer, gint *bytes_copied)
2944 gboolean decreased_max = FALSE;
2946 /* Only read to end of tvbuff, w/o throwing exception. */
2947 check_offset_length(tvb, offset, -1, &abs_offset, &len);
2949 /* There must at least be room for the terminating NUL. */
2950 DISSECTOR_ASSERT(bufsize != 0);
2952 /* If there's no room for anything else, just return the NUL. */
2959 /* check_offset_length() won't throw an exception if we're
2960 * looking at the byte immediately after the end of the tvbuff. */
2962 THROW(ReportedBoundsError);
2965 /* This should not happen because check_offset_length() would
2966 * have already thrown an exception if 'offset' were out-of-bounds.
2968 DISSECTOR_ASSERT(len != -1);
2971 * If we've been passed a negative number, bufsize will
2974 DISSECTOR_ASSERT(bufsize <= G_MAXINT);
2976 if ((guint)len < bufsize) {
2978 decreased_max = TRUE;
2984 stringlen = tvb_strnlen(tvb, abs_offset, limit - 1);
2985 /* If NUL wasn't found, copy the data and return -1 */
2986 if (stringlen == -1) {
2987 tvb_memcpy(tvb, buffer, abs_offset, limit);
2988 if (decreased_max) {
2990 /* Add 1 for the extra NUL that we set at buffer[limit],
2991 * pretending that it was copied as part of the string. */
2992 *bytes_copied = limit + 1;
2995 *bytes_copied = limit;
3000 /* Copy the string to buffer */
3001 tvb_memcpy(tvb, buffer, abs_offset, stringlen + 1);
3002 *bytes_copied = stringlen + 1;
3006 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
3007 * no more than bufsize number of bytes, including terminating NUL, to buffer.
3008 * Returns length of string (not including terminating NUL), or -1 if the string was
3009 * truncated in the buffer due to not having reached the terminating NUL.
3010 * In this way, it acts like g_snprintf().
3012 * When processing a packet where the remaining number of bytes is less
3013 * than bufsize, an exception is not thrown if the end of the packet
3014 * is reached before the NUL is found. If no NUL is found before reaching
3015 * the end of the short packet, -1 is still returned, and the string
3016 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
3017 * at the correct spot, terminating the string.
3020 tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8 *buffer)
3024 DISSECTOR_ASSERT(tvb && tvb->initialized);
3026 return _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
3029 /* Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
3030 * have a terminating NUL. If the string was truncated when copied into buffer,
3031 * a NUL is placed at the end of buffer to terminate it.
3034 tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer)
3036 gint len, bytes_copied;
3038 DISSECTOR_ASSERT(tvb && tvb->initialized);
3040 len = _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
3043 buffer[bufsize - 1] = 0;
3044 return bytes_copied - 1;
3052 * Given a tvbuff, an offset into the tvbuff, and a length that starts
3053 * at that offset (which may be -1 for "all the way to the end of the
3054 * tvbuff"), find the end of the (putative) line that starts at the
3055 * specified offset in the tvbuff, going no further than the specified
3058 * Return the length of the line (not counting the line terminator at
3059 * the end), or, if we don't find a line terminator:
3061 * if "deseg" is true, return -1;
3063 * if "deseg" is false, return the amount of data remaining in
3066 * Set "*next_offset" to the offset of the character past the line
3067 * terminator, or past the end of the buffer if we don't find a line
3068 * terminator. (It's not set if we return -1.)
3071 tvb_find_line_end(tvbuff_t *tvb, const gint offset, int len, gint *next_offset, const gboolean desegment)
3074 static const char __declspec(align(16)) crlf[] = "\r\n" ;
3076 static const char crlf[] __attribute__((aligned(16))) = "\r\n" ;
3082 guchar found_needle = 0;
3084 DISSECTOR_ASSERT(tvb && tvb->initialized);
3087 len = _tvb_captured_length_remaining(tvb, offset);
3089 * XXX - what if "len" is still -1, meaning "offset is past the
3090 * end of the tvbuff"?
3092 eob_offset = offset + len;
3095 * Look either for a CR or an LF.
3097 eol_offset = tvb_pbrk_guint8(tvb, offset, len, crlf, &found_needle);
3098 if (eol_offset == -1) {
3100 * No CR or LF - line is presumably continued in next packet.
3104 * Tell our caller we saw no EOL, so they can
3105 * try to desegment and get the entire line
3111 * Pretend the line runs to the end of the tvbuff.
3113 linelen = eob_offset - offset;
3115 *next_offset = eob_offset;
3119 * Find the number of bytes between the starting offset
3122 linelen = eol_offset - offset;
3127 if (found_needle == '\r') {
3129 * Yes - is it followed by an LF?
3131 if (eol_offset + 1 >= eob_offset) {
3133 * Dunno - the next byte isn't in this
3138 * We'll return -1, although that
3139 * runs the risk that if the line
3140 * really *is* terminated with a CR,
3141 * we won't properly dissect this
3144 * It's probably more likely that
3145 * the line ends with CR-LF than
3146 * that it ends with CR by itself.
3152 * Well, we can at least look at the next
3155 if (tvb_get_guint8(tvb, eol_offset + 1) == '\n') {
3157 * It's an LF; skip over the CR.
3165 * Return the offset of the character after the last
3166 * character in the line, skipping over the last character
3167 * in the line terminator.
3170 *next_offset = eol_offset + 1;
3176 * Given a tvbuff, an offset into the tvbuff, and a length that starts
3177 * at that offset (which may be -1 for "all the way to the end of the
3178 * tvbuff"), find the end of the (putative) line that starts at the
3179 * specified offset in the tvbuff, going no further than the specified
3182 * However, treat quoted strings inside the buffer specially - don't
3183 * treat newlines in quoted strings as line terminators.
3185 * Return the length of the line (not counting the line terminator at
3186 * the end), or the amount of data remaining in the buffer if we don't
3187 * find a line terminator.
3189 * Set "*next_offset" to the offset of the character past the line
3190 * terminator, or past the end of the buffer if we don't find a line
3194 tvb_find_line_end_unquoted(tvbuff_t *tvb, const gint offset, int len, gint *next_offset)
3196 gint cur_offset, char_offset;
3202 DISSECTOR_ASSERT(tvb && tvb->initialized);
3205 len = _tvb_captured_length_remaining(tvb, offset);
3207 * XXX - what if "len" is still -1, meaning "offset is past the
3208 * end of the tvbuff"?
3210 eob_offset = offset + len;
3212 cur_offset = offset;
3216 * Is this part of the string quoted?
3220 * Yes - look only for the terminating quote.
3222 char_offset = tvb_find_guint8(tvb, cur_offset, len,
3226 * Look either for a CR, an LF, or a '"'.
3228 char_offset = tvb_pbrk_guint8(tvb, cur_offset, len, "\r\n\"", &c);
3230 if (char_offset == -1) {
3232 * Not found - line is presumably continued in
3234 * We pretend the line runs to the end of the tvbuff.
3236 linelen = eob_offset - offset;
3238 *next_offset = eob_offset;
3244 * We're processing a quoted string.
3245 * We only looked for ", so we know it's a ";
3246 * as we're processing a quoted string, it's a
3256 * Un-quoted "; it begins a quoted
3262 * It's a CR or LF; we've found a line
3265 * Find the number of bytes between the
3266 * starting offset and the CR or LF.
3268 linelen = char_offset - offset;
3275 * Yes; is it followed by an LF?
3277 if (char_offset + 1 < eob_offset &&
3278 tvb_get_guint8(tvb, char_offset + 1)
3281 * Yes; skip over the CR.
3288 * Return the offset of the character after
3289 * the last character in the line, skipping
3290 * over the last character in the line
3291 * terminator, and quit.
3294 *next_offset = char_offset + 1;
3300 * Step past the character we found.
3302 cur_offset = char_offset + 1;
3303 if (cur_offset >= eob_offset) {
3305 * The character we found was the last character
3306 * in the tvbuff - line is presumably continued in
3308 * We pretend the line runs to the end of the tvbuff.
3310 linelen = eob_offset - offset;
3312 *next_offset = eob_offset;
3320 * Copied from the mgcp dissector. (This function should be moved to /epan )
3321 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
3322 * character following offset or offset + maxlength -1 whichever
3326 * tvb - The tvbuff in which we are skipping whitespace.
3327 * offset - The offset in tvb from which we begin trying to skip whitespace.
3328 * maxlength - The maximum distance from offset that we may try to skip
3331 * Returns: The position in tvb of the first non-whitespace
3332 * character following offset or offset + maxlength -1 whichever
3336 tvb_skip_wsp(tvbuff_t *tvb, const gint offset, const gint maxlength)
3338 gint counter = offset;
3342 DISSECTOR_ASSERT(tvb && tvb->initialized);
3344 /* Get the length remaining */
3345 /*tvb_len = tvb_captured_length(tvb);*/
3346 tvb_len = tvb->length;
3348 end = offset + maxlength;
3354 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
3355 for (counter = offset;
3357 ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
3358 tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
3365 tvb_skip_wsp_return(tvbuff_t *tvb, const gint offset) {
3366 gint counter = offset;
3369 for(counter = offset; counter > 0 &&
3370 ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
3371 tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
3377 tvb_skip_guint8(tvbuff_t *tvb, int offset, const int maxlength, const guint8 ch)
3381 DISSECTOR_ASSERT(tvb && tvb->initialized);
3383 /* Get the length remaining */
3384 /*tvb_len = tvb_captured_length(tvb);*/
3385 tvb_len = tvb->length;
3387 end = offset + maxlength;
3391 while (offset < end) {
3392 guint8 tempch = tvb_get_guint8(tvb, offset);
3403 * Format a bunch of data from a tvbuff as bytes, returning a pointer
3404 * to the string with the formatted data, with "punct" as a byte
3408 tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, const gint offset, const gint len, const gchar punct)
3410 return (gchar*)bytestring_to_str(scope, ensure_contiguous(tvb, offset, len), len, punct);
3415 * Given a tvbuff, an offset into the tvbuff, and a length that starts
3416 * at that offset (which may be -1 for "all the way to the end of the
3417 * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
3418 * the low or high half byte, formating the digits according to an input digit set,
3419 * if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
3420 * A pointer to the packet scope allocated string will be returned.
3421 * Note a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
3423 static dgt_set_t Dgt1_9_bcd = {
3425 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f*/
3426 '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?'
3430 tvb_bcd_dig_to_wmem_packet_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first)
3436 gint t_offset = offset;
3438 DISSECTOR_ASSERT(tvb && tvb->initialized);
3444 /*length = tvb_captured_length(tvb);*/
3445 length = tvb->length;
3446 if (length < offset) {
3450 length = offset + len;
3452 digit_str = (char *)wmem_alloc(wmem_packet_scope(), (length - offset)*2+1);
3454 while (t_offset < length) {
3456 octet = tvb_get_guint8(tvb,t_offset);
3458 digit_str[i] = dgt->out[octet & 0x0f];
3464 * unpack second value in byte
3468 if (octet == 0x0f) /* odd number bytes - hit filler */
3471 digit_str[i] = dgt->out[octet & 0x0f];
3482 * Format a bunch of data from a tvbuff as bytes, returning a pointer
3483 * to the string with the formatted data.
3486 tvb_bytes_to_ep_str(tvbuff_t *tvb, const gint offset, const gint len)
3488 return bytes_to_ep_str(ensure_contiguous(tvb, offset, len), len);
3492 * Same as tvb_bytes_to_ep_str but with wmem
3494 gchar *tvb_bytes_to_wmem_str(wmem_allocator_t *allocator, tvbuff_t *tvb,
3495 const gint offset, const gint len)
3497 return bytes_to_str(allocator, ensure_contiguous(tvb, offset, len), len);
3500 /* Find a needle tvbuff within a haystack tvbuff. */
3502 tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const gint haystack_offset)
3504 guint haystack_abs_offset, haystack_abs_length;
3505 const guint8 *haystack_data;
3506 const guint8 *needle_data;
3507 const guint needle_len = needle_tvb->length;
3508 const guint8 *location;
3510 DISSECTOR_ASSERT(haystack_tvb && haystack_tvb->initialized);
3512 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
3516 /* Get pointers to the tvbuffs' data. */
3517 haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
3518 needle_data = ensure_contiguous(needle_tvb, 0, -1);
3520 check_offset_length(haystack_tvb, haystack_offset, -1,
3521 &haystack_abs_offset, &haystack_abs_length);
3523 location = epan_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
3524 needle_data, needle_len);
3527 return (gint) (location - haystack_data);
3534 tvb_raw_offset(tvbuff_t *tvb)
3536 return ((tvb->raw_offset==-1) ? (tvb->raw_offset = tvb_offset_from_real_beginning(tvb)) : tvb->raw_offset);
3540 tvb_set_fragment(tvbuff_t *tvb)
3542 tvb->flags |= TVBUFF_FRAGMENT;
3546 tvb_get_ds_tvb(tvbuff_t *tvb)
3548 return(tvb->ds_tvb);
3552 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3557 * indent-tabs-mode: t
3560 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
3561 * :indentSize=8:tabSize=8:noTabs=false: