3 * Testy, Virtual(-izable) Buffer of uint8_t*'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 * SPDX-License-Identifier: GPL-2.0-or-later
32 #include "wsutil/pint.h"
33 #include "wsutil/sign_ext.h"
34 #include "wsutil/strtoi.h"
35 #include "wsutil/unicode-utils.h"
36 #include "wsutil/nstime.h"
37 #include "wsutil/time_util.h"
38 #include <wsutil/ws_assert.h>
40 #include "tvbuff-int.h"
44 #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
45 #include "exceptions.h"
50 _tvb_get_bits64(tvbuff_t
*tvb
, unsigned bit_offset
, const int total_no_of_bits
);
53 _tvb_get_bits64_le(tvbuff_t
*tvb
, unsigned bit_offset
, const int total_no_of_bits
);
56 _tvb_captured_length_remaining(const tvbuff_t
*tvb
, const int offset
);
58 static inline const uint8_t*
59 ensure_contiguous(tvbuff_t
*tvb
, const int offset
, const int length
);
61 static inline uint8_t *
62 tvb_get_raw_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, const int length
);
65 tvb_new(const struct tvb_ops
*ops
)
68 size_t size
= ops
->tvb_size
;
70 ws_assert(size
>= sizeof(*tvb
));
72 tvb
= (tvbuff_t
*) g_slice_alloc(size
);
76 tvb
->initialized
= false;
79 tvb
->reported_length
= 0;
80 tvb
->contained_length
= 0;
81 tvb
->real_data
= NULL
;
89 tvb_free_internal(tvbuff_t
*tvb
)
93 DISSECTOR_ASSERT(tvb
);
95 if (tvb
->ops
->tvb_free
)
96 tvb
->ops
->tvb_free(tvb
);
98 size
= tvb
->ops
->tvb_size
;
100 g_slice_free1(size
, tvb
);
103 /* XXX: just call tvb_free_chain();
104 * Not removed so that existing dissectors using tvb_free() need not be changed.
105 * I'd argue that existing calls to tvb_free() should have actually beeen
106 * calls to tvb_free_chain() although the calls were OK as long as no
107 * subsets, etc had been created on the tvb. */
109 tvb_free(tvbuff_t
*tvb
)
115 tvb_free_chain(tvbuff_t
*tvb
)
118 DISSECTOR_ASSERT(tvb
);
120 next_tvb
= tvb
->next
;
121 tvb_free_internal(tvb
);
127 tvb_new_chain(tvbuff_t
*parent
, tvbuff_t
*backing
)
129 tvbuff_t
*tvb
= tvb_new_proxy(backing
);
131 tvb_add_to_chain(parent
, tvb
);
136 tvb_add_to_chain(tvbuff_t
*parent
, tvbuff_t
*child
)
140 DISSECTOR_ASSERT(parent
);
141 DISSECTOR_ASSERT(child
);
147 tmp
->next
= parent
->next
;
153 * Check whether that offset goes more than one byte past the
156 * If not, return 0; otherwise, return exception
159 validate_offset(const tvbuff_t
*tvb
, const unsigned abs_offset
)
161 if (G_LIKELY(abs_offset
<= tvb
->length
)) {
167 * It's not OK, but why? Which boundaries is it
170 if (abs_offset
<= tvb
->contained_length
) {
172 * It's past the captured length, but not past
173 * the reported end of any parent tvbuffs from
174 * which this is constructed, or the reported
175 * end of this tvbuff, so it's out of bounds
176 * solely because we're past the end of the
183 * There's some actual packet boundary, not just the
184 * artificial boundary imposed by packet slicing, that
188 if (tvb
->flags
& TVBUFF_FRAGMENT
) {
190 * This tvbuff is the first fragment of a larger
191 * packet that hasn't been reassembled, so we
192 * assume that's the source of the problem - if
193 * we'd reassembled the packet, we wouldn't have
196 * That might not be true, but for at least
197 * some forms of reassembly, such as IP
198 * reassembly, you don't know how big the
199 * reassembled packet is unless you reassemble
200 * it, so, in those cases, we can't determine
201 * whether we would have gone past the end
202 * had we reassembled the packet.
204 return FragmentBoundsError
;
207 /* OK, we're not an unreassembled fragment (that we know of). */
208 if (abs_offset
<= tvb
->reported_length
) {
210 * We're within the bounds of what this tvbuff
211 * purportedly contains, based on some length
212 * value, but we're not within the bounds of
213 * something from which this tvbuff was
214 * extracted, so that length value ran past
215 * the end of some parent tvbuff.
217 return ContainedBoundsError
;
221 * OK, it looks as if we ran past the claimed length
224 return ReportedBoundsError
;
228 compute_offset(const tvbuff_t
*tvb
, const int offset
, unsigned *offset_ptr
)
231 /* Positive offset - relative to the beginning of the packet. */
232 if (G_LIKELY((unsigned) offset
<= tvb
->length
)) {
233 *offset_ptr
= offset
;
234 } else if ((unsigned) offset
<= tvb
->contained_length
) {
236 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
237 return FragmentBoundsError
;
238 } else if ((unsigned) offset
<= tvb
->reported_length
) {
239 return ContainedBoundsError
;
241 return ReportedBoundsError
;
245 /* Negative offset - relative to the end of the packet. */
246 if (G_LIKELY((unsigned) -offset
<= tvb
->length
)) {
247 *offset_ptr
= tvb
->length
+ offset
;
248 } else if ((unsigned) -offset
<= tvb
->contained_length
) {
250 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
251 return FragmentBoundsError
;
252 } else if ((unsigned) -offset
<= tvb
->reported_length
) {
253 return ContainedBoundsError
;
255 return ReportedBoundsError
;
263 compute_offset_and_remaining(const tvbuff_t
*tvb
, const int offset
, unsigned *offset_ptr
, unsigned *rem_len
)
267 exception
= compute_offset(tvb
, offset
, offset_ptr
);
269 *rem_len
= tvb
->length
- *offset_ptr
;
274 /* Computes the absolute offset and length based on a possibly-negative offset
275 * and a length that is possible -1 (which means "to the end of the data").
276 * Returns integer indicating whether the offset is in bounds (0) or
277 * not (exception number). The integer ptrs are modified with the new offset,
278 * captured (available) length, and contained length (amount that's present
279 * in the parent tvbuff based on its reported length).
280 * No exception is thrown; on success, we return 0, otherwise we return an
281 * exception for the caller to throw if appropriate.
283 * XXX - we return success (0), if the offset is positive and right
284 * after the end of the tvbuff (i.e., equal to the length). We do this
285 * so that a dissector constructing a subset tvbuff for the next protocol
286 * will get a zero-length tvbuff, not an exception, if there's no data
287 * left for the next protocol - we want the next protocol to be the one
288 * that gets an exception, so the error is reported as an error in that
289 * protocol rather than the containing protocol. */
291 check_offset_length_no_exception(const tvbuff_t
*tvb
,
292 const int offset
, int const length_val
,
293 unsigned *offset_ptr
, unsigned *length_ptr
)
298 DISSECTOR_ASSERT(offset_ptr
);
299 DISSECTOR_ASSERT(length_ptr
);
301 /* Compute the offset */
302 exception
= compute_offset(tvb
, offset
, offset_ptr
);
306 if (length_val
< -1) {
307 /* XXX - ReportedBoundsError? */
311 /* Compute the length */
312 if (length_val
== -1)
313 *length_ptr
= tvb
->length
- *offset_ptr
;
315 *length_ptr
= length_val
;
318 * Compute the offset of the first byte past the length.
320 end_offset
= *offset_ptr
+ *length_ptr
;
323 * Check for an overflow
325 if (end_offset
< *offset_ptr
)
328 return validate_offset(tvb
, end_offset
);
331 /* Checks (+/-) offset and length and throws an exception if
332 * either is out of bounds. Sets integer ptrs to the new offset
335 check_offset_length(const tvbuff_t
*tvb
,
336 const int offset
, int const length_val
,
337 unsigned *offset_ptr
, unsigned *length_ptr
)
341 exception
= check_offset_length_no_exception(tvb
, offset
, length_val
, offset_ptr
, length_ptr
);
347 tvb_check_offset_length(const tvbuff_t
*tvb
,
348 const int offset
, int const length_val
,
349 unsigned *offset_ptr
, unsigned *length_ptr
)
351 check_offset_length(tvb
, offset
, length_val
, offset_ptr
, length_ptr
);
354 static const unsigned char left_aligned_bitmask
[] = {
366 tvb_new_octet_aligned(tvbuff_t
*tvb
, uint32_t bit_offset
, int32_t no_of_bits
)
368 tvbuff_t
*sub_tvb
= NULL
;
369 uint32_t byte_offset
;
371 uint8_t left
, right
, remaining_bits
, *buf
;
374 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
376 byte_offset
= bit_offset
>> 3;
377 left
= bit_offset
% 8; /* for left-shifting */
378 right
= 8 - left
; /* for right-shifting */
380 if (no_of_bits
== -1) {
381 datalen
= _tvb_captured_length_remaining(tvb
, byte_offset
);
384 datalen
= no_of_bits
>> 3;
385 remaining_bits
= no_of_bits
% 8;
386 if (remaining_bits
) {
391 /* already aligned -> shortcut */
392 if ((left
== 0) && (remaining_bits
== 0)) {
393 return tvb_new_subset_length_caplen(tvb
, byte_offset
, datalen
, datalen
);
396 DISSECTOR_ASSERT(datalen
>0);
398 /* if at least one trailing byte is available, we must use the content
399 * of that byte for the last shift (i.e. tvb_get_ptr() must use datalen + 1
400 * if non extra byte is available, the last shifted byte requires
403 if (_tvb_captured_length_remaining(tvb
, byte_offset
) > datalen
) {
404 data
= ensure_contiguous(tvb
, byte_offset
, datalen
+ 1); /* tvb_get_ptr */
406 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
407 buf
= (uint8_t *)g_malloc(datalen
);
409 /* shift tvb data bit_offset bits to the left */
410 for (i
= 0; i
< datalen
; i
++)
411 buf
[i
] = (data
[i
] << left
) | (data
[i
+1] >> right
);
413 data
= ensure_contiguous(tvb
, byte_offset
, datalen
); /* tvb_get_ptr() */
415 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
416 buf
= (uint8_t *)g_malloc(datalen
);
418 /* shift tvb data bit_offset bits to the left */
419 for (i
= 0; i
< (datalen
-1); i
++)
420 buf
[i
] = (data
[i
] << left
) | (data
[i
+1] >> right
);
421 buf
[datalen
-1] = data
[datalen
-1] << left
; /* set last octet */
423 buf
[datalen
-1] &= left_aligned_bitmask
[remaining_bits
];
425 sub_tvb
= tvb_new_child_real_data(tvb
, buf
, datalen
, datalen
);
426 tvb_set_free_cb(sub_tvb
, g_free
);
432 tvb_new_octet_right_aligned(tvbuff_t
*tvb
, uint32_t bit_offset
, int32_t no_of_bits
)
434 tvbuff_t
*sub_tvb
= NULL
;
435 uint32_t byte_offset
;
436 int src_len
, dst_len
, i
;
437 uint8_t left
, right
, remaining_bits
, *buf
;
440 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
442 byte_offset
= bit_offset
/ 8;
443 /* right shift to put bits in place and discard least significant bits */
444 right
= bit_offset
% 8;
445 /* left shift to get most significant bits from next octet */
448 if (no_of_bits
== -1) {
449 dst_len
= _tvb_captured_length_remaining(tvb
, byte_offset
);
452 dst_len
= no_of_bits
/ 8;
453 remaining_bits
= no_of_bits
% 8;
454 if (remaining_bits
) {
459 /* already aligned -> shortcut */
460 if ((right
== 0) && (remaining_bits
== 0)) {
461 return tvb_new_subset_length_caplen(tvb
, byte_offset
, dst_len
, dst_len
);
464 DISSECTOR_ASSERT(dst_len
>0);
466 if (_tvb_captured_length_remaining(tvb
, byte_offset
) > dst_len
) {
467 /* last octet will get data from trailing octet */
468 src_len
= dst_len
+ 1;
470 /* last octet will be zero padded */
474 data
= ensure_contiguous(tvb
, byte_offset
, src_len
); /* tvb_get_ptr */
476 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
477 buf
= (uint8_t *)g_malloc(dst_len
);
479 for (i
= 0; i
< (dst_len
- 1); i
++)
480 buf
[i
] = (data
[i
] >> right
) | (data
[i
+1] << left
);
482 /* Special handling for last octet */
483 buf
[i
] = (data
[i
] >> right
);
484 /* Shift most significant bits from trailing octet if available */
485 if (src_len
> dst_len
)
486 buf
[i
] |= (data
[i
+1] << left
);
487 /* Preserve only remaining bits in last octet if not multiple of 8 */
489 buf
[i
] &= ((1 << remaining_bits
) - 1);
491 sub_tvb
= tvb_new_child_real_data(tvb
, buf
, dst_len
, dst_len
);
492 tvb_set_free_cb(sub_tvb
, g_free
);
498 tvb_generic_clone_offset_len(tvbuff_t
*tvb
, unsigned offset
, unsigned len
)
500 tvbuff_t
*cloned_tvb
;
503 DISSECTOR_ASSERT(tvb_bytes_exist(tvb
, offset
, len
));
505 data
= (uint8_t *) g_malloc(len
);
507 tvb_memcpy(tvb
, data
, offset
, len
);
509 cloned_tvb
= tvb_new_real_data(data
, len
, len
);
510 tvb_set_free_cb(cloned_tvb
, g_free
);
516 tvb_clone_offset_len(tvbuff_t
*tvb
, unsigned offset
, unsigned len
)
518 if (tvb
->ops
->tvb_clone
) {
519 tvbuff_t
*cloned_tvb
;
521 cloned_tvb
= tvb
->ops
->tvb_clone(tvb
, offset
, len
);
526 return tvb_generic_clone_offset_len(tvb
, offset
, len
);
530 tvb_clone(tvbuff_t
*tvb
)
532 return tvb_clone_offset_len(tvb
, 0, tvb
->length
);
536 tvb_captured_length(const tvbuff_t
*tvb
)
538 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
543 /* For tvbuff internal use */
545 _tvb_captured_length_remaining(const tvbuff_t
*tvb
, const int offset
)
547 unsigned abs_offset
= 0, rem_length
;
550 exception
= compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &rem_length
);
558 tvb_captured_length_remaining(const tvbuff_t
*tvb
, const int offset
)
560 unsigned abs_offset
= 0, rem_length
;
563 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
565 exception
= compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &rem_length
);
573 tvb_ensure_captured_length_remaining(const tvbuff_t
*tvb
, const int offset
)
575 unsigned abs_offset
= 0, rem_length
= 0;
578 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
580 exception
= compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &rem_length
);
584 if (rem_length
== 0) {
586 * This routine ensures there's at least one byte available.
587 * There aren't any bytes available, so throw the appropriate
590 if (abs_offset
< tvb
->contained_length
) {
592 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
593 THROW(FragmentBoundsError
);
594 } else if (abs_offset
< tvb
->reported_length
) {
595 THROW(ContainedBoundsError
);
597 THROW(ReportedBoundsError
);
603 /* Validates that 'length' bytes are available starting from
604 * offset (pos/neg). Does not throw an exception. */
606 tvb_bytes_exist(const tvbuff_t
*tvb
, const int offset
, const int length
)
608 unsigned abs_offset
= 0, abs_length
;
611 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
614 * Negative lengths are not possible and indicate a bug (e.g. arithmetic
615 * error or an overly large value from packet data).
620 exception
= check_offset_length_no_exception(tvb
, offset
, length
, &abs_offset
, &abs_length
);
627 /* Validates that 'length' bytes, where 'length' is a 64-bit unsigned
628 * integer, are available starting from offset (pos/neg). Throws an
629 * exception if they aren't. */
631 tvb_ensure_bytes_exist64(const tvbuff_t
*tvb
, const int offset
, const uint64_t length
)
634 * Make sure the value fits in a signed integer; if not, assume
635 * that means that it's too big.
637 if (length
> INT_MAX
) {
638 THROW(ReportedBoundsError
);
641 /* OK, now cast it and try it with tvb_ensure_bytes_exist(). */
642 tvb_ensure_bytes_exist(tvb
, offset
, (int)length
);
645 /* Validates that 'length' bytes are available starting from
646 * offset (pos/neg). Throws an exception if they aren't. */
648 tvb_ensure_bytes_exist(const tvbuff_t
*tvb
, const int offset
, const int length
)
650 unsigned real_offset
, end_offset
;
652 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
655 * -1 doesn't mean "until end of buffer", as that's pointless
656 * for this routine. We must treat it as a Really Large Positive
657 * Number, so that we throw an exception; we throw
658 * ReportedBoundsError, as if it were past even the end of a
659 * reassembled packet, and past the end of even the data we
662 * We do the same with other negative lengths.
665 THROW(ReportedBoundsError
);
668 /* XXX: Below this point could be replaced with a call to
669 * check_offset_length with no functional change, however this is a
670 * *very* hot path and check_offset_length is not well-optimized for
671 * this case, so we eat some code duplication for a lot of speedup. */
674 /* Positive offset - relative to the beginning of the packet. */
675 if (G_LIKELY((unsigned) offset
<= tvb
->length
)) {
676 real_offset
= offset
;
677 } else if ((unsigned) offset
<= tvb
->contained_length
) {
679 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
680 THROW(FragmentBoundsError
);
681 } else if ((unsigned) offset
<= tvb
->reported_length
) {
682 THROW(ContainedBoundsError
);
684 THROW(ReportedBoundsError
);
688 /* Negative offset - relative to the end of the packet. */
689 if (G_LIKELY((unsigned) -offset
<= tvb
->length
)) {
690 real_offset
= tvb
->length
+ offset
;
691 } else if ((unsigned) -offset
<= tvb
->contained_length
) {
693 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
694 THROW(FragmentBoundsError
);
695 } else if ((unsigned) -offset
<= tvb
->reported_length
) {
696 THROW(ContainedBoundsError
);
698 THROW(ReportedBoundsError
);
703 * Compute the offset of the first byte past the length.
705 end_offset
= real_offset
+ length
;
708 * Check for an overflow
710 if (end_offset
< real_offset
)
713 if (G_LIKELY(end_offset
<= tvb
->length
))
715 else if (end_offset
<= tvb
->contained_length
)
717 else if (tvb
->flags
& TVBUFF_FRAGMENT
)
718 THROW(FragmentBoundsError
);
719 else if (end_offset
<= tvb
->reported_length
)
720 THROW(ContainedBoundsError
);
722 THROW(ReportedBoundsError
);
726 tvb_offset_exists(const tvbuff_t
*tvb
, const int offset
)
728 unsigned abs_offset
= 0;
731 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
733 exception
= compute_offset(tvb
, offset
, &abs_offset
);
737 /* compute_offset only throws an exception on >, not >= because of the
738 * comment above check_offset_length_no_exception, but here we want the
739 * opposite behaviour so we check ourselves... */
740 return abs_offset
< tvb
->length
;
744 tvb_reported_length(const tvbuff_t
*tvb
)
746 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
748 return tvb
->reported_length
;
752 tvb_reported_length_remaining(const tvbuff_t
*tvb
, const int offset
)
754 unsigned abs_offset
= 0;
757 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
759 exception
= compute_offset(tvb
, offset
, &abs_offset
);
763 if (tvb
->reported_length
>= abs_offset
)
764 return tvb
->reported_length
- abs_offset
;
770 tvb_ensure_reported_length_remaining(const tvbuff_t
*tvb
, const int offset
)
772 unsigned abs_offset
= 0;
775 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
777 exception
= compute_offset(tvb
, offset
, &abs_offset
);
781 if (tvb
->reported_length
>= abs_offset
)
782 return tvb
->reported_length
- abs_offset
;
784 THROW(ReportedBoundsError
);
787 /* Set the reported length of a tvbuff to a given value; used for protocols
788 * whose headers contain an explicit length and where the calling
789 * dissector's payload may include padding as well as the packet for
791 * Also adjusts the available and contained length. */
793 tvb_set_reported_length(tvbuff_t
*tvb
, const unsigned reported_length
)
795 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
797 if (reported_length
> tvb
->reported_length
)
798 THROW(ReportedBoundsError
);
800 tvb
->reported_length
= reported_length
;
801 if (reported_length
< tvb
->length
)
802 tvb
->length
= reported_length
;
803 if (reported_length
< tvb
->contained_length
)
804 tvb
->contained_length
= reported_length
;
807 /* Repair a tvbuff where the captured length is greater than the
808 * reported length; such a tvbuff makes no sense, as it's impossible
809 * to capture more data than is in the packet.
812 tvb_fix_reported_length(tvbuff_t
*tvb
)
814 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
815 DISSECTOR_ASSERT(tvb
->reported_length
< tvb
->length
);
817 tvb
->reported_length
= tvb
->length
;
818 if (tvb
->contained_length
< tvb
->length
)
819 tvb
->contained_length
= tvb
->length
;
823 tvb_offset_from_real_beginning_counter(const tvbuff_t
*tvb
, const unsigned counter
)
825 if (tvb
->ops
->tvb_offset
)
826 return tvb
->ops
->tvb_offset(tvb
, counter
);
828 DISSECTOR_ASSERT_NOT_REACHED();
833 tvb_offset_from_real_beginning(const tvbuff_t
*tvb
)
835 return tvb_offset_from_real_beginning_counter(tvb
, 0);
838 static inline const uint8_t*
839 ensure_contiguous_no_exception(tvbuff_t
*tvb
, const int offset
, const int length
, int *pexception
)
841 unsigned abs_offset
= 0, abs_length
= 0;
844 exception
= check_offset_length_no_exception(tvb
, offset
, length
, &abs_offset
, &abs_length
);
847 *pexception
= exception
;
852 * Special case: if the caller (e.g. tvb_get_ptr) requested no data,
853 * then it is acceptable to have an empty tvb (!tvb->real_data).
860 * We know that all the data is present in the tvbuff, so
861 * no exceptions should be thrown.
864 return tvb
->real_data
+ abs_offset
;
866 if (tvb
->ops
->tvb_get_ptr
)
867 return tvb
->ops
->tvb_get_ptr(tvb
, abs_offset
, abs_length
);
869 DISSECTOR_ASSERT_NOT_REACHED();
873 static inline const uint8_t*
874 ensure_contiguous(tvbuff_t
*tvb
, const int offset
, const int length
)
879 p
= ensure_contiguous_no_exception(tvb
, offset
, length
, &exception
);
880 if (p
== NULL
&& length
!= 0) {
881 DISSECTOR_ASSERT(exception
> 0);
887 static inline const uint8_t*
888 fast_ensure_contiguous(tvbuff_t
*tvb
, const int offset
, const unsigned length
)
893 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
894 /* We don't check for overflow in this fast path so we only handle simple types */
895 DISSECTOR_ASSERT(length
<= 8);
897 if (offset
< 0 || !tvb
->real_data
) {
898 return ensure_contiguous(tvb
, offset
, length
);
902 end_offset
= u_offset
+ length
;
904 if (G_LIKELY(end_offset
<= tvb
->length
)) {
905 return tvb
->real_data
+ u_offset
;
906 } else if (end_offset
<= tvb
->contained_length
) {
908 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
909 THROW(FragmentBoundsError
);
910 } else if (end_offset
<= tvb
->reported_length
) {
911 THROW(ContainedBoundsError
);
913 THROW(ReportedBoundsError
);
921 /************** ACCESSORS **************/
924 tvb_memcpy(tvbuff_t
*tvb
, void *target
, const int offset
, size_t length
)
926 unsigned abs_offset
= 0, abs_length
= 0;
928 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
931 * XXX - we should eliminate the "length = -1 means 'to the end
932 * of the tvbuff'" convention, and use other means to achieve
933 * that; this would let us eliminate a bunch of checks for
934 * negative lengths in cases where the protocol has a 32-bit
937 * Allowing -1 but throwing an assertion on other negative
938 * lengths is a bit more work with the length being a size_t;
939 * instead, we check for a length <= 2^31-1.
941 DISSECTOR_ASSERT(length
<= 0x7FFFFFFF);
942 check_offset_length(tvb
, offset
, (int) length
, &abs_offset
, &abs_length
);
944 if (target
&& tvb
->real_data
) {
945 return memcpy(target
, tvb
->real_data
+ abs_offset
, abs_length
);
948 if (target
&& tvb
->ops
->tvb_memcpy
)
949 return tvb
->ops
->tvb_memcpy(tvb
, target
, abs_offset
, abs_length
);
952 * If the length is 0, there's nothing to do.
953 * (tvb->real_data could be null if it's allocated with
958 * XXX, fallback to slower method
960 DISSECTOR_ASSERT_NOT_REACHED();
967 * XXX - this doesn't treat a length of -1 as an error.
968 * If it did, this could replace some code that calls
969 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
972 * "composite_get_ptr()" depends on -1 not being
973 * an error; does anything else depend on this routine treating -1 as
974 * meaning "to the end of the buffer"?
976 * If scope is NULL, memory is allocated with g_malloc() and user must
977 * explicitly free it with g_free().
978 * If scope is not NULL, memory is allocated with the corresponding pool
982 tvb_memdup(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, size_t length
)
984 unsigned abs_offset
= 0, abs_length
= 0;
987 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
989 check_offset_length(tvb
, offset
, (int) length
, &abs_offset
, &abs_length
);
994 duped
= wmem_alloc(scope
, abs_length
);
995 return tvb_memcpy(tvb
, duped
, abs_offset
, abs_length
);
1001 tvb_get_ptr(tvbuff_t
*tvb
, const int offset
, const int length
)
1003 return ensure_contiguous(tvb
, offset
, length
);
1006 /* ---------------- */
1008 tvb_get_uint8(tvbuff_t
*tvb
, const int offset
)
1012 ptr
= fast_ensure_contiguous(tvb
, offset
, 1);
1017 tvb_get_int8(tvbuff_t
*tvb
, const int offset
)
1021 ptr
= fast_ensure_contiguous(tvb
, offset
, 1);
1026 tvb_get_ntohs(tvbuff_t
*tvb
, const int offset
)
1030 ptr
= fast_ensure_contiguous(tvb
, offset
, 2);
1031 return pntoh16(ptr
);
1035 tvb_get_ntohis(tvbuff_t
*tvb
, const int offset
)
1039 ptr
= fast_ensure_contiguous(tvb
, offset
, 2);
1040 return pntoh16(ptr
);
1044 tvb_get_ntoh24(tvbuff_t
*tvb
, const int offset
)
1048 ptr
= fast_ensure_contiguous(tvb
, offset
, 3);
1049 return pntoh24(ptr
);
1053 tvb_get_ntohi24(tvbuff_t
*tvb
, const int offset
)
1057 ret
= ws_sign_ext32(tvb_get_ntoh24(tvb
, offset
), 24);
1059 return (int32_t)ret
;
1063 tvb_get_ntohl(tvbuff_t
*tvb
, const int offset
)
1067 ptr
= fast_ensure_contiguous(tvb
, offset
, 4);
1068 return pntoh32(ptr
);
1072 tvb_get_ntohil(tvbuff_t
*tvb
, const int offset
)
1076 ptr
= fast_ensure_contiguous(tvb
, offset
, 4);
1077 return pntoh32(ptr
);
1081 tvb_get_ntoh40(tvbuff_t
*tvb
, const int offset
)
1085 ptr
= fast_ensure_contiguous(tvb
, offset
, 5);
1086 return pntoh40(ptr
);
1090 tvb_get_ntohi40(tvbuff_t
*tvb
, const int offset
)
1094 ret
= ws_sign_ext64(tvb_get_ntoh40(tvb
, offset
), 40);
1096 return (int64_t)ret
;
1100 tvb_get_ntoh48(tvbuff_t
*tvb
, const int offset
)
1104 ptr
= fast_ensure_contiguous(tvb
, offset
, 6);
1105 return pntoh48(ptr
);
1109 tvb_get_ntohi48(tvbuff_t
*tvb
, const int offset
)
1113 ret
= ws_sign_ext64(tvb_get_ntoh48(tvb
, offset
), 48);
1115 return (int64_t)ret
;
1119 tvb_get_ntoh56(tvbuff_t
*tvb
, const int offset
)
1123 ptr
= fast_ensure_contiguous(tvb
, offset
, 7);
1124 return pntoh56(ptr
);
1128 tvb_get_ntohi56(tvbuff_t
*tvb
, const int offset
)
1132 ret
= ws_sign_ext64(tvb_get_ntoh56(tvb
, offset
), 56);
1134 return (int64_t)ret
;
1138 tvb_get_ntoh64(tvbuff_t
*tvb
, const int offset
)
1142 ptr
= fast_ensure_contiguous(tvb
, offset
, 8);
1143 return pntoh64(ptr
);
1147 tvb_get_ntohi64(tvbuff_t
*tvb
, const int offset
)
1151 ptr
= fast_ensure_contiguous(tvb
, offset
, 8);
1152 return pntoh64(ptr
);
1156 tvb_get_uint16(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1157 if (encoding
& ENC_LITTLE_ENDIAN
) {
1158 return tvb_get_letohs(tvb
, offset
);
1160 return tvb_get_ntohs(tvb
, offset
);
1165 tvb_get_int16(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1166 if (encoding
& ENC_LITTLE_ENDIAN
) {
1167 return tvb_get_letohis(tvb
, offset
);
1169 return tvb_get_ntohis(tvb
, offset
);
1174 tvb_get_uint24(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1175 if (encoding
& ENC_LITTLE_ENDIAN
) {
1176 return tvb_get_letoh24(tvb
, offset
);
1178 return tvb_get_ntoh24(tvb
, offset
);
1183 tvb_get_int24(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1184 if (encoding
& ENC_LITTLE_ENDIAN
) {
1185 return tvb_get_letohi24(tvb
, offset
);
1187 return tvb_get_ntohi24(tvb
, offset
);
1192 tvb_get_uint32(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1193 if (encoding
& ENC_LITTLE_ENDIAN
) {
1194 return tvb_get_letohl(tvb
, offset
);
1196 return tvb_get_ntohl(tvb
, offset
);
1201 tvb_get_int32(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1202 if (encoding
& ENC_LITTLE_ENDIAN
) {
1203 return tvb_get_letohil(tvb
, offset
);
1205 return tvb_get_ntohil(tvb
, offset
);
1210 tvb_get_uint40(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1211 if (encoding
& ENC_LITTLE_ENDIAN
) {
1212 return tvb_get_letoh40(tvb
, offset
);
1214 return tvb_get_ntoh40(tvb
, offset
);
1219 tvb_get_int40(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1220 if (encoding
& ENC_LITTLE_ENDIAN
) {
1221 return tvb_get_letohi40(tvb
, offset
);
1223 return tvb_get_ntohi40(tvb
, offset
);
1228 tvb_get_uint48(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1229 if (encoding
& ENC_LITTLE_ENDIAN
) {
1230 return tvb_get_letoh48(tvb
, offset
);
1232 return tvb_get_ntoh48(tvb
, offset
);
1237 tvb_get_int48(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1238 if (encoding
& ENC_LITTLE_ENDIAN
) {
1239 return tvb_get_letohi48(tvb
, offset
);
1241 return tvb_get_ntohi48(tvb
, offset
);
1246 tvb_get_uint56(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1247 if (encoding
& ENC_LITTLE_ENDIAN
) {
1248 return tvb_get_letoh56(tvb
, offset
);
1250 return tvb_get_ntoh56(tvb
, offset
);
1255 tvb_get_int56(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1256 if (encoding
& ENC_LITTLE_ENDIAN
) {
1257 return tvb_get_letohi56(tvb
, offset
);
1259 return tvb_get_ntohi56(tvb
, offset
);
1264 tvb_get_uint64(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1265 if (encoding
& ENC_LITTLE_ENDIAN
) {
1266 return tvb_get_letoh64(tvb
, offset
);
1268 return tvb_get_ntoh64(tvb
, offset
);
1273 tvb_get_uint64_with_length(tvbuff_t
*tvb
, const int offset
, unsigned length
, const unsigned encoding
)
1280 value
= tvb_get_uint8(tvb
, offset
);
1284 value
= (encoding
& ENC_LITTLE_ENDIAN
) ? tvb_get_letohs(tvb
, offset
)
1285 : tvb_get_ntohs(tvb
, offset
);
1289 value
= (encoding
& ENC_LITTLE_ENDIAN
) ? tvb_get_letoh24(tvb
, offset
)
1290 : tvb_get_ntoh24(tvb
, offset
);
1294 value
= (encoding
& ENC_LITTLE_ENDIAN
) ? tvb_get_letohl(tvb
, offset
)
1295 : tvb_get_ntohl(tvb
, offset
);
1299 value
= (encoding
& ENC_LITTLE_ENDIAN
) ? tvb_get_letoh40(tvb
, offset
)
1300 : tvb_get_ntoh40(tvb
, offset
);
1304 value
= (encoding
& ENC_LITTLE_ENDIAN
) ? tvb_get_letoh48(tvb
, offset
)
1305 : tvb_get_ntoh48(tvb
, offset
);
1309 value
= (encoding
& ENC_LITTLE_ENDIAN
) ? tvb_get_letoh56(tvb
, offset
)
1310 : tvb_get_ntoh56(tvb
, offset
);
1314 value
= (encoding
& ENC_LITTLE_ENDIAN
) ? tvb_get_letoh64(tvb
, offset
)
1315 : tvb_get_ntoh64(tvb
, offset
);
1322 value
= (encoding
& ENC_LITTLE_ENDIAN
) ? tvb_get_letoh64(tvb
, offset
)
1323 : tvb_get_ntoh64(tvb
, offset
);
1331 tvb_get_int64(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1332 if (encoding
& ENC_LITTLE_ENDIAN
) {
1333 return tvb_get_letohi64(tvb
, offset
);
1335 return tvb_get_ntohi64(tvb
, offset
);
1340 tvb_get_ieee_float(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1341 if (encoding
& ENC_LITTLE_ENDIAN
) {
1342 return tvb_get_letohieee_float(tvb
, offset
);
1344 return tvb_get_ntohieee_float(tvb
, offset
);
1349 tvb_get_ieee_double(tvbuff_t
*tvb
, const int offset
, const unsigned encoding
) {
1350 if (encoding
& ENC_LITTLE_ENDIAN
) {
1351 return tvb_get_letohieee_double(tvb
, offset
);
1353 return tvb_get_ntohieee_double(tvb
, offset
);
1358 * Stuff for IEEE float handling on platforms that don't have IEEE
1359 * format as the native floating-point format.
1361 * For now, we treat only the VAX as such a platform.
1363 * XXX - other non-IEEE boxes that can run UN*X include some Crays,
1364 * and possibly other machines. However, I don't know whether there
1365 * are any other machines that could run Wireshark and that don't use
1366 * IEEE format. As far as I know, all of the main current and past
1367 * commercial microprocessor families on which OSes that support
1368 * Wireshark can run use IEEE format (x86, ARM, 68k, SPARC, MIPS,
1369 * PA-RISC, Alpha, IA-64, and so on), and it appears that the official
1370 * Linux port to System/390 and zArchitecture uses IEEE format floating-
1371 * point rather than IBM hex floating-point (not a huge surprise), so
1372 * I'm not sure that leaves any 32-bit or larger UN*X or Windows boxes,
1373 * other than VAXes, that don't use IEEE format. If you're not running
1374 * UN*X or Windows, the floating-point format is probably going to be
1375 * the least of your problems in a port.
1385 #define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
1386 #define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
1387 #define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
1389 #define IEEE_SP_SIGN_MASK 0x80000000
1390 #define IEEE_SP_EXPONENT_MASK 0x7F800000
1391 #define IEEE_SP_MANTISSA_MASK 0x007FFFFF
1392 #define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
1394 #define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
1395 #define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
1396 #define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
1399 ieee_float_is_zero(const uint32_t w
)
1401 return ((w
& ~IEEE_SP_SIGN_MASK
) == 0);
1405 get_ieee_float(const uint32_t w
)
1411 sign
= w
& IEEE_SP_SIGN_MASK
;
1412 exponent
= w
& IEEE_SP_EXPONENT_MASK
;
1413 mantissa
= w
& IEEE_SP_MANTISSA_MASK
;
1415 if (ieee_float_is_zero(w
)) {
1416 /* number is zero, unnormalized, or not-a-number */
1421 * XXX - how to handle this?
1423 if (IEEE_SP_INFINITY
== exponent
) {
1425 * number is positive or negative infinity, or a special value
1427 return (sign
? MINUS_INFINITY
: PLUS_INFINITY
);
1431 exponent
= ((exponent
>> IEEE_SP_MANTISSA_WIDTH
) - IEEE_SP_BIAS
) -
1432 IEEE_SP_MANTISSA_WIDTH
;
1433 mantissa
|= IEEE_SP_IMPLIED_BIT
;
1436 return -mantissa
* pow(2, exponent
);
1438 return mantissa
* pow(2, exponent
);
1443 * We assume that if you don't have IEEE floating-point, you have a
1444 * compiler that understands 64-bit integral quantities.
1446 #define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
1447 #define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
1448 #define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
1450 #define IEEE_DP_SIGN_MASK INT64_C(0x8000000000000000)
1451 #define IEEE_DP_EXPONENT_MASK INT64_C(0x7FF0000000000000)
1452 #define IEEE_DP_MANTISSA_MASK INT64_C(0x000FFFFFFFFFFFFF)
1453 #define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
1455 #define IEEE_DP_IMPLIED_BIT (INT64_C(1) << IEEE_DP_MANTISSA_WIDTH)
1456 #define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
1457 #define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
1460 ieee_double_is_zero(const uint64_t w
)
1462 return ((w
& ~IEEE_SP_SIGN_MASK
) == 0);
1466 get_ieee_double(const uint64_t w
)
1472 sign
= w
& IEEE_DP_SIGN_MASK
;
1473 exponent
= w
& IEEE_DP_EXPONENT_MASK
;
1474 mantissa
= w
& IEEE_DP_MANTISSA_MASK
;
1476 if (ieee_double_is_zero(w
)) {
1477 /* number is zero, unnormalized, or not-a-number */
1482 * XXX - how to handle this?
1484 if (IEEE_DP_INFINITY
== exponent
) {
1486 * number is positive or negative infinity, or a special value
1488 return (sign
? MINUS_INFINITY
: PLUS_INFINITY
);
1492 exponent
= ((exponent
>> IEEE_DP_MANTISSA_WIDTH
) - IEEE_DP_BIAS
) -
1493 IEEE_DP_MANTISSA_WIDTH
;
1494 mantissa
|= IEEE_DP_IMPLIED_BIT
;
1497 return -mantissa
* pow(2, exponent
);
1499 return mantissa
* pow(2, exponent
);
1504 * Fetches an IEEE single-precision floating-point number, in
1505 * big-endian form, and returns a "float".
1507 * XXX - should this be "double", in case there are IEEE single-
1508 * precision numbers that won't fit in some platform's native
1512 tvb_get_ntohieee_float(tvbuff_t
*tvb
, const int offset
)
1515 return get_ieee_float(tvb_get_ntohl(tvb
, offset
));
1522 ieee_fp_union
.w
= tvb_get_ntohl(tvb
, offset
);
1523 return ieee_fp_union
.f
;
1528 * Fetches an IEEE double-precision floating-point number, in
1529 * big-endian form, and returns a "double".
1532 tvb_get_ntohieee_double(tvbuff_t
*tvb
, const int offset
)
1546 #if G_BYTE_ORDER == G_BIG_ENDIAN
1547 ieee_fp_union
.w
[0] = tvb_get_ntohl(tvb
, offset
);
1548 ieee_fp_union
.w
[1] = tvb_get_ntohl(tvb
, offset
+4);
1550 ieee_fp_union
.w
[0] = tvb_get_ntohl(tvb
, offset
+4);
1551 ieee_fp_union
.w
[1] = tvb_get_ntohl(tvb
, offset
);
1554 return get_ieee_double(ieee_fp_union
.dw
);
1556 return ieee_fp_union
.d
;
1561 tvb_get_letohs(tvbuff_t
*tvb
, const int offset
)
1565 ptr
= fast_ensure_contiguous(tvb
, offset
, 2);
1566 return pletoh16(ptr
);
1570 tvb_get_letohis(tvbuff_t
*tvb
, const int offset
)
1574 ptr
= fast_ensure_contiguous(tvb
, offset
, 2);
1575 return pletoh16(ptr
);
1579 tvb_get_letoh24(tvbuff_t
*tvb
, const int offset
)
1583 ptr
= fast_ensure_contiguous(tvb
, offset
, 3);
1584 return pletoh24(ptr
);
1588 tvb_get_letohi24(tvbuff_t
*tvb
, const int offset
)
1592 ret
= ws_sign_ext32(tvb_get_letoh24(tvb
, offset
), 24);
1594 return (int32_t)ret
;
1598 tvb_get_letohl(tvbuff_t
*tvb
, const int offset
)
1602 ptr
= fast_ensure_contiguous(tvb
, offset
, 4);
1603 return pletoh32(ptr
);
1607 tvb_get_letohil(tvbuff_t
*tvb
, const int offset
)
1611 ptr
= fast_ensure_contiguous(tvb
, offset
, 4);
1612 return pletoh32(ptr
);
1616 tvb_get_letoh40(tvbuff_t
*tvb
, const int offset
)
1620 ptr
= fast_ensure_contiguous(tvb
, offset
, 5);
1621 return pletoh40(ptr
);
1625 tvb_get_letohi40(tvbuff_t
*tvb
, const int offset
)
1629 ret
= ws_sign_ext64(tvb_get_letoh40(tvb
, offset
), 40);
1631 return (int64_t)ret
;
1635 tvb_get_letoh48(tvbuff_t
*tvb
, const int offset
)
1639 ptr
= fast_ensure_contiguous(tvb
, offset
, 6);
1640 return pletoh48(ptr
);
1644 tvb_get_letohi48(tvbuff_t
*tvb
, const int offset
)
1648 ret
= ws_sign_ext64(tvb_get_letoh48(tvb
, offset
), 48);
1650 return (int64_t)ret
;
1654 tvb_get_letoh56(tvbuff_t
*tvb
, const int offset
)
1658 ptr
= fast_ensure_contiguous(tvb
, offset
, 7);
1659 return pletoh56(ptr
);
1663 tvb_get_letohi56(tvbuff_t
*tvb
, const int offset
)
1667 ret
= ws_sign_ext64(tvb_get_letoh56(tvb
, offset
), 56);
1669 return (int64_t)ret
;
1673 tvb_get_letoh64(tvbuff_t
*tvb
, const int offset
)
1677 ptr
= fast_ensure_contiguous(tvb
, offset
, 8);
1678 return pletoh64(ptr
);
1682 tvb_get_letohi64(tvbuff_t
*tvb
, const int offset
)
1686 ptr
= fast_ensure_contiguous(tvb
, offset
, 8);
1687 return pletoh64(ptr
);
1691 * Fetches an IEEE single-precision floating-point number, in
1692 * little-endian form, and returns a "float".
1694 * XXX - should this be "double", in case there are IEEE single-
1695 * precision numbers that won't fit in some platform's native
1699 tvb_get_letohieee_float(tvbuff_t
*tvb
, const int offset
)
1702 return get_ieee_float(tvb_get_letohl(tvb
, offset
));
1709 ieee_fp_union
.w
= tvb_get_letohl(tvb
, offset
);
1710 return ieee_fp_union
.f
;
1715 * Fetches an IEEE double-precision floating-point number, in
1716 * little-endian form, and returns a "double".
1719 tvb_get_letohieee_double(tvbuff_t
*tvb
, const int offset
)
1733 #if G_BYTE_ORDER == G_BIG_ENDIAN
1734 ieee_fp_union
.w
[0] = tvb_get_letohl(tvb
, offset
+4);
1735 ieee_fp_union
.w
[1] = tvb_get_letohl(tvb
, offset
);
1737 ieee_fp_union
.w
[0] = tvb_get_letohl(tvb
, offset
);
1738 ieee_fp_union
.w
[1] = tvb_get_letohl(tvb
, offset
+4);
1741 return get_ieee_double(ieee_fp_union
.dw
);
1743 return ieee_fp_union
.d
;
1747 /* This function is a slight misnomer. It accepts all encodings that are
1748 * ASCII "enough", which means encodings that are the same as US-ASCII
1749 * for textual representations of dates and hex bytes; i.e., the same
1750 * for the hex digits and Z (in practice, all alphanumerics), and the
1751 * four separators ':' '-' '.' and ' '
1752 * That means that any encoding that keeps the ISO/IEC 646 invariant
1753 * characters the same (including the T.61 8 bit encoding and multibyte
1754 * encodings like EUC-KR and GB18030) are OK, even if they replace characters
1755 * like '$' '#' and '\' with national variants, but not encodings like UTF-16
1756 * that include extra null bytes.
1757 * For our current purposes, the unpacked GSM 7-bit default alphabet (but not
1758 * all National Language Shift Tables) also satisfies this requirement, but
1759 * note that it does *not* keep all ISO/IEC 646 invariant characters the same.
1760 * If this internal function gets used for additional purposes than currently,
1761 * the set of encodings that it accepts could change.
1764 validate_single_byte_ascii_encoding(const unsigned encoding
)
1766 const unsigned enc
= encoding
& ~ENC_CHARENCODING_MASK
;
1772 case ENC_3GPP_TS_23_038_7BITS_PACKED
:
1773 case ENC_ASCII_7BITS
:
1775 case ENC_EBCDIC_CP037
:
1776 case ENC_EBCDIC_CP500
:
1777 case ENC_BCD_DIGITS_0_9
:
1778 case ENC_KEYPAD_ABC_TBCD
:
1779 case ENC_KEYPAD_BC_TBCD
:
1780 case ENC_ETSI_TS_102_221_ANNEX_A
:
1782 case ENC_DECT_STANDARD_4BITS_TBCD
:
1783 REPORT_DISSECTOR_BUG("Invalid string encoding type passed to tvb_get_string_XXX");
1788 /* make sure something valid was set */
1790 REPORT_DISSECTOR_BUG("No string encoding type passed to tvb_get_string_XXX");
1794 tvb_get_string_bytes(tvbuff_t
*tvb
, const int offset
, const int length
,
1795 const unsigned encoding
, GByteArray
*bytes
, int *endoff
)
1799 const char *end
= NULL
;
1800 GByteArray
*retval
= NULL
;
1802 validate_single_byte_ascii_encoding(encoding
);
1804 ptr
= (char*) tvb_get_raw_string(NULL
, tvb
, offset
, length
);
1807 if (endoff
) *endoff
= offset
;
1809 while (*begin
== ' ') begin
++;
1811 if (*begin
&& bytes
) {
1812 if (hex_str_to_bytes_encoding(begin
, bytes
, &end
, encoding
, false)) {
1813 if (bytes
->len
> 0) {
1814 if (endoff
) *endoff
= offset
+ (int)(end
- ptr
);
1820 wmem_free(NULL
, ptr
);
1826 parse_month_name(const char *name
, int *tm_mon
)
1828 static const char months
[][4] = { "Jan", "Feb", "Mar", "Apr", "May",
1829 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1830 for (int i
= 0; i
< 12; i
++) {
1831 if (memcmp(months
[i
], name
, 4) == 0) {
1840 * Is the character a WSP character, as per RFC 5234? (space or tab).
1842 #define IS_WSP(c) ((c) == ' ' || (c) == '\t')
1844 /* support hex-encoded time values? */
1846 tvb_get_string_time(tvbuff_t
*tvb
, const int offset
, const int length
,
1847 const unsigned encoding
, nstime_t
*ns
, int *endoff
)
1851 const char *end
= NULL
;
1855 validate_single_byte_ascii_encoding(encoding
);
1857 DISSECTOR_ASSERT(ns
);
1859 begin
= (char*) tvb_get_raw_string(NULL
, tvb
, offset
, length
);
1862 while (IS_WSP(*ptr
))
1866 if ((encoding
& ENC_ISO_8601_DATE_TIME
) == ENC_ISO_8601_DATE_TIME
) {
1867 if (!(end
= iso8601_to_nstime(ns
, ptr
, ISO8601_DATETIME
))) {
1872 } else if ((encoding
& ENC_ISO_8601_DATE_TIME_BASIC
) == ENC_ISO_8601_DATE_TIME_BASIC
) {
1873 if (!(end
= iso8601_to_nstime(ns
, ptr
, ISO8601_DATETIME_BASIC
))) {
1881 memset(&tm
, 0, sizeof(tm
));
1886 /* note: sscanf is known to be inconsistent across platforms with respect
1887 to whether a %n is counted as a return value or not, so we have to use
1889 if (encoding
& ENC_ISO_8601_DATE
) {
1891 if (sscanf(ptr
, "%d-%d-%d%n",
1897 end
= ptr
+ num_chars
;
1899 if (tm
.tm_year
> 1900) tm
.tm_year
-= 1900;
1904 else if (encoding
& ENC_ISO_8601_TIME
) {
1906 if (sscanf(ptr
, "%d:%d:%d%n",
1912 /* what should we do about day/month/year? */
1913 /* setting it to "now" for now */
1914 time_t time_now
= time(NULL
);
1915 struct tm
*tm_now
= gmtime(&time_now
);
1916 if (tm_now
!= NULL
) {
1917 tm
.tm_year
= tm_now
->tm_year
;
1918 tm
.tm_mon
= tm_now
->tm_mon
;
1919 tm
.tm_mday
= tm_now
->tm_mday
;
1921 /* The second before the Epoch */
1926 end
= ptr
+ num_chars
;
1931 else if (encoding
& ENC_IMF_DATE_TIME
) {
1933 * Match [dow,] day month year hh:mm[:ss] with
1934 * two-digit years (RFC 822) or four-digit
1935 * years (RFCs 1123, 2822, 5822). Skip
1936 * the day of week since it is locale
1937 * dependent and does not affect the resulting
1940 if (g_ascii_isalpha(ptr
[0]) && g_ascii_isalpha(ptr
[1]) && g_ascii_isalpha(ptr
[2]) && ptr
[3] == ',')
1941 ptr
+= 4; /* Skip day of week. */
1944 * Parse the day-of-month and month
1947 char month_name
[4] = { 0 };
1949 if (sscanf(ptr
, "%d %3s%n",
1957 if (!parse_month_name(month_name
, &tm
.tm_mon
)) {
1961 while (IS_WSP(*ptr
))
1965 * Scan the year. Treat 2-digit years
1966 * differently from 4-digit years.
1969 const char *yearendp
;
1971 if (!ws_strtou32(ptr
, &yearendp
, &year
)) {
1974 if (!IS_WSP(*yearendp
)) {
1975 /* Not followed by WSP. */
1978 if (yearendp
- ptr
< 2) {
1979 /* 1-digit year. Error. */
1982 if (yearendp
- ptr
== 2) {
1986 * Match RFC 2822/RFC 5322 behavior;
1987 * add 2000 to years from 0 to
1988 * 49 and 1900 to uears from 50
1996 } else if (yearendp
- ptr
== 3) {
2000 * Match RFC 2822/RFC 5322 behavior;
2001 * add 1900 to the year.
2005 tm
.tm_year
= year
- 1900;
2007 while (IS_WSP(*ptr
))
2010 /* Parse the time. */
2011 if (sscanf(ptr
, "%d:%d%n:%d%n",
2021 while (IS_WSP(*ptr
))
2025 * Parse the time zone.
2026 * Check for obs-zone values first.
2028 if (g_ascii_strncasecmp(ptr
, "UT", 2) == 0)
2032 else if (g_ascii_strncasecmp(ptr
, "GMT", 3) == 0)
2042 if (sscanf(ptr
, "%c%2d%2d%n",
2052 * If sign is '+', there's a positive
2055 * If sign is '-', there's a negative
2058 * Otherwise, that's an invalid UTC
2062 utc_offset
+= (off_hr
* 3600) + (off_min
* 60);
2063 else if (sign
== '-')
2064 utc_offset
-= (off_hr
* 3600) + (off_min
* 60);
2066 /* Sign must be + or - */
2073 ns
->secs
= mktime_utc(&tm
);
2074 if (ns
->secs
== (time_t)-1 && errno
!= 0) {
2077 ns
->secs
+= utc_offset
;
2085 *endoff
= (int)(offset
+ (end
- begin
));
2086 wmem_free(NULL
, begin
);
2090 wmem_free(NULL
, begin
);
2094 /* Fetch an IPv4 address, in network byte order.
2095 * We do *not* convert them to host byte order; we leave them in
2096 * network byte order. */
2098 tvb_get_ipv4(tvbuff_t
*tvb
, const int offset
)
2103 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(uint32_t));
2104 memcpy(&addr
, ptr
, sizeof addr
);
2108 /* Fetch an IPv6 address. */
2110 tvb_get_ipv6(tvbuff_t
*tvb
, const int offset
, ws_in6_addr
*addr
)
2114 ptr
= ensure_contiguous(tvb
, offset
, sizeof(*addr
));
2115 memcpy(addr
, ptr
, sizeof *addr
);
2119 * These routines return the length of the address in bytes on success
2120 * and -1 if the prefix length is too long.
2123 tvb_get_ipv4_addr_with_prefix_len(tvbuff_t
*tvb
, int offset
, ws_in4_addr
*addr
,
2124 uint32_t prefix_len
)
2128 if (prefix_len
> 32)
2131 addr_len
= (prefix_len
+ 7) / 8;
2133 tvb_memcpy(tvb
, addr
, offset
, addr_len
);
2135 ((uint8_t*)addr
)[addr_len
- 1] &= ((0xff00 >> (prefix_len
% 8)) & 0xff);
2140 * These routines return the length of the address in bytes on success
2141 * and -1 if the prefix length is too long.
2144 tvb_get_ipv6_addr_with_prefix_len(tvbuff_t
*tvb
, int offset
, ws_in6_addr
*addr
,
2145 uint32_t prefix_len
)
2149 if (prefix_len
> 128)
2152 addr_len
= (prefix_len
+ 7) / 8;
2153 memset(addr
->bytes
, 0, 16);
2154 tvb_memcpy(tvb
, addr
->bytes
, offset
, addr_len
);
2155 if (prefix_len
% 8) {
2156 addr
->bytes
[addr_len
- 1] &=
2157 ((0xff00 >> (prefix_len
% 8)) & 0xff);
2165 tvb_get_ntohguid(tvbuff_t
*tvb
, const int offset
, e_guid_t
*guid
)
2167 const uint8_t *ptr
= ensure_contiguous(tvb
, offset
, GUID_LEN
);
2169 guid
->data1
= pntoh32(ptr
+ 0);
2170 guid
->data2
= pntoh16(ptr
+ 4);
2171 guid
->data3
= pntoh16(ptr
+ 6);
2172 memcpy(guid
->data4
, ptr
+ 8, sizeof guid
->data4
);
2176 tvb_get_letohguid(tvbuff_t
*tvb
, const int offset
, e_guid_t
*guid
)
2178 const uint8_t *ptr
= ensure_contiguous(tvb
, offset
, GUID_LEN
);
2180 guid
->data1
= pletoh32(ptr
+ 0);
2181 guid
->data2
= pletoh16(ptr
+ 4);
2182 guid
->data3
= pletoh16(ptr
+ 6);
2183 memcpy(guid
->data4
, ptr
+ 8, sizeof guid
->data4
);
2187 * NOTE: to support code written when proto_tree_add_item() took a
2188 * bool as its last argument, with false meaning "big-endian"
2189 * and true meaning "little-endian", we treat any non-zero value of
2190 * "encoding" as meaning "little-endian".
2193 tvb_get_guid(tvbuff_t
*tvb
, const int offset
, e_guid_t
*guid
, const unsigned encoding
)
2196 tvb_get_letohguid(tvb
, offset
, guid
);
2198 tvb_get_ntohguid(tvb
, offset
, guid
);
2202 static const uint8_t bit_mask8
[] = {
2215 /* Get a variable amount of bits
2217 * Return a byte array with bit limited data.
2218 * When encoding is ENC_BIG_ENDIAN, the data is aligned to the left.
2219 * When encoding is ENC_LITTLE_ENDIAN, the data is aligned to the right.
2222 tvb_get_bits_array(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int bit_offset
,
2223 size_t no_of_bits
, size_t *data_length
, const unsigned encoding
)
2226 if (encoding
& ENC_LITTLE_ENDIAN
) {
2227 sub_tvb
= tvb_new_octet_right_aligned(tvb
, bit_offset
, (int32_t) no_of_bits
);
2229 sub_tvb
= tvb_new_octet_aligned(tvb
, bit_offset
, (int32_t) no_of_bits
);
2231 *data_length
= tvb_reported_length(sub_tvb
);
2232 return (uint8_t*)tvb_memdup(scope
, sub_tvb
, 0, *data_length
);
2235 /* Get 1 - 8 bits */
2237 tvb_get_bits8(tvbuff_t
*tvb
, unsigned bit_offset
, const int no_of_bits
)
2239 return (uint8_t)_tvb_get_bits64(tvb
, bit_offset
, no_of_bits
);
2242 /* Get 1 - 16 bits */
2244 tvb_get_bits16(tvbuff_t
*tvb
, unsigned bit_offset
, const int no_of_bits
, const unsigned encoding
)
2246 return (uint16_t)tvb_get_bits64(tvb
, bit_offset
, no_of_bits
, encoding
);
2249 /* Get 1 - 32 bits */
2251 tvb_get_bits32(tvbuff_t
*tvb
, unsigned bit_offset
, const int no_of_bits
, const unsigned encoding
)
2253 return (uint32_t)tvb_get_bits64(tvb
, bit_offset
, no_of_bits
, encoding
);
2256 /* Get 1 - 64 bits */
2258 tvb_get_bits64(tvbuff_t
*tvb
, unsigned bit_offset
, const int no_of_bits
, const unsigned encoding
)
2260 /* encoding determines bit numbering within octet array */
2261 if (encoding
& ENC_LITTLE_ENDIAN
) {
2262 return _tvb_get_bits64_le(tvb
, bit_offset
, no_of_bits
);
2264 return _tvb_get_bits64(tvb
, bit_offset
, no_of_bits
);
2269 * This function will dissect a sequence of bits that does not need to be byte aligned; the bits
2270 * set will be shown in the tree as ..10 10.. and the integer value returned if return_value is set.
2271 * Offset should be given in bits from the start of the tvb.
2272 * Bits within octet are numbered from MSB (0) to LSB (7). Bit at bit_offset is return value most significant bit.
2273 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
2276 _tvb_get_bits64(tvbuff_t
*tvb
, unsigned bit_offset
, const int total_no_of_bits
)
2279 unsigned octet_offset
= bit_offset
>> 3;
2280 uint8_t required_bits_in_first_octet
= 8 - (bit_offset
% 8);
2282 if(required_bits_in_first_octet
> total_no_of_bits
)
2284 /* the required bits don't extend to the end of the first octet */
2285 uint8_t right_shift
= required_bits_in_first_octet
- total_no_of_bits
;
2286 value
= (tvb_get_uint8(tvb
, octet_offset
) >> right_shift
) & bit_mask8
[total_no_of_bits
% 8];
2290 uint8_t remaining_bit_length
= total_no_of_bits
;
2292 /* get the bits up to the first octet boundary */
2294 required_bits_in_first_octet
%= 8;
2295 if(required_bits_in_first_octet
!= 0)
2297 value
= tvb_get_uint8(tvb
, octet_offset
) & bit_mask8
[required_bits_in_first_octet
];
2298 remaining_bit_length
-= required_bits_in_first_octet
;
2301 /* take the biggest words, shorts or octets that we can */
2302 while (remaining_bit_length
> 7)
2304 switch (remaining_bit_length
>> 4)
2307 /* 8 - 15 bits. (note that 0 - 7 would have dropped out of the while() loop) */
2309 value
+= tvb_get_uint8(tvb
, octet_offset
);
2310 remaining_bit_length
-= 8;
2317 value
+= tvb_get_ntohs(tvb
, octet_offset
);
2318 remaining_bit_length
-= 16;
2326 value
+= tvb_get_ntohl(tvb
, octet_offset
);
2327 remaining_bit_length
-= 32;
2332 /* 64 bits (or more???) */
2333 value
= tvb_get_ntoh64(tvb
, octet_offset
);
2334 remaining_bit_length
-= 64;
2339 /* get bits from any partial octet at the tail */
2340 if(remaining_bit_length
)
2342 value
<<= remaining_bit_length
;
2343 value
+= (tvb_get_uint8(tvb
, octet_offset
) >> (8 - remaining_bit_length
));
2350 * Offset should be given in bits from the start of the tvb.
2351 * Bits within octet are numbered from LSB (0) to MSB (7). Bit at bit_offset is return value least significant bit.
2352 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
2355 _tvb_get_bits64_le(tvbuff_t
*tvb
, unsigned bit_offset
, const int total_no_of_bits
)
2358 unsigned octet_offset
= bit_offset
/ 8;
2359 int remaining_bits
= total_no_of_bits
;
2362 if (remaining_bits
> 64)
2364 remaining_bits
= 64;
2369 /* not aligned, extract bits from first octet */
2370 shift
= 8 - (bit_offset
% 8);
2371 value
= tvb_get_uint8(tvb
, octet_offset
) >> (bit_offset
% 8);
2372 if (shift
> remaining_bits
)
2374 /* keep only the requested bits */
2375 value
&= (UINT64_C(1) << remaining_bits
) - 1;
2380 remaining_bits
-= shift
;
2385 while (remaining_bits
> 0)
2387 /* take the biggest words, shorts or octets that we can */
2388 if (remaining_bits
>= 32)
2390 value
|= ((uint64_t)tvb_get_letohl(tvb
, octet_offset
) << shift
);
2392 remaining_bits
-= 32;
2395 else if (remaining_bits
>= 16)
2397 value
|= ((uint64_t)tvb_get_letohs(tvb
, octet_offset
) << shift
);
2399 remaining_bits
-= 16;
2402 else if (remaining_bits
>= 8)
2404 value
|= ((uint64_t)tvb_get_uint8(tvb
, octet_offset
) << shift
);
2406 remaining_bits
-= 8;
2411 unsigned mask
= (1 << remaining_bits
) - 1;
2412 value
|= (((uint64_t)tvb_get_uint8(tvb
, octet_offset
) & mask
) << shift
);
2413 shift
+= remaining_bits
;
2421 /* Get 1 - 32 bits (should be deprecated as same as tvb_get_bits32??) */
2423 tvb_get_bits(tvbuff_t
*tvb
, const unsigned bit_offset
, const int no_of_bits
, const unsigned encoding
)
2425 return (uint32_t)tvb_get_bits64(tvb
, bit_offset
, no_of_bits
, encoding
);
2429 tvb_find_uint8_generic(tvbuff_t
*tvb
, unsigned abs_offset
, unsigned limit
, uint8_t needle
)
2432 const uint8_t *result
;
2434 ptr
= ensure_contiguous(tvb
, abs_offset
, limit
); /* tvb_get_ptr() */
2438 result
= (const uint8_t *) memchr(ptr
, needle
, limit
);
2442 return (int) ((result
- ptr
) + abs_offset
);
2445 /* Find first occurrence of needle in tvbuff, starting at offset. Searches
2446 * at most maxlength number of bytes; if maxlength is -1, searches to
2448 * Returns the offset of the found needle, or -1 if not found.
2449 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
2450 * in that case, -1 will be returned if the boundary is reached before
2451 * finding needle. */
2453 tvb_find_uint8(tvbuff_t
*tvb
, const int offset
, const int maxlength
, const uint8_t needle
)
2455 const uint8_t *result
;
2456 unsigned abs_offset
= 0;
2460 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
2462 exception
= compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &limit
);
2466 /* Only search to end of tvbuff, w/o throwing exception. */
2467 if (maxlength
>= 0 && limit
> (unsigned) maxlength
) {
2468 /* Maximum length doesn't go past end of tvbuff; search
2470 limit
= (unsigned) maxlength
;
2473 /* If we have real data, perform our search now. */
2474 if (tvb
->real_data
) {
2475 result
= (const uint8_t *)memchr(tvb
->real_data
+ abs_offset
, needle
, limit
);
2476 if (result
== NULL
) {
2480 return (int) (result
- tvb
->real_data
);
2484 if (tvb
->ops
->tvb_find_uint8
)
2485 return tvb
->ops
->tvb_find_uint8(tvb
, abs_offset
, limit
, needle
);
2487 return tvb_find_uint8_generic(tvb
, offset
, limit
, needle
);
2490 /* Same as tvb_find_uint8() with 16bit needle. */
2492 tvb_find_uint16(tvbuff_t
*tvb
, const int offset
, const int maxlength
,
2493 const uint16_t needle
)
2495 unsigned abs_offset
= 0;
2499 exception
= compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &limit
);
2503 /* Only search to end of tvbuff, w/o throwing exception. */
2504 if (maxlength
>= 0 && limit
> (unsigned) maxlength
) {
2505 /* Maximum length doesn't go past end of tvbuff; search
2507 limit
= (unsigned) maxlength
;
2510 const uint8_t needle1
= ((needle
& 0xFF00) >> 8);
2511 const uint8_t needle2
= ((needle
& 0x00FF) >> 0);
2512 unsigned searched_bytes
= 0;
2513 unsigned pos
= abs_offset
;
2517 tvb_find_uint8(tvb
, pos
, limit
- searched_bytes
, needle1
);
2520 if (offset1
== -1) {
2524 searched_bytes
= (unsigned)offset1
- abs_offset
+ 1;
2526 if (searched_bytes
>= limit
) {
2530 offset2
= tvb_find_uint8(tvb
, offset1
+ 1, 1, needle2
);
2532 searched_bytes
+= 1;
2534 if (offset2
!= -1) {
2535 if (searched_bytes
> limit
) {
2542 } while (searched_bytes
< limit
);
2548 tvb_ws_mempbrk_uint8_generic(tvbuff_t
*tvb
, unsigned abs_offset
, unsigned limit
, const ws_mempbrk_pattern
* pattern
, unsigned char *found_needle
)
2551 const uint8_t *result
;
2553 ptr
= ensure_contiguous(tvb
, abs_offset
, limit
); /* tvb_get_ptr */
2557 result
= ws_mempbrk_exec(ptr
, limit
, pattern
, found_needle
);
2561 return (int) ((result
- ptr
) + abs_offset
);
2565 /* Find first occurrence of any of the pattern chars in tvbuff, starting at offset.
2566 * Searches at most maxlength number of bytes; if maxlength is -1, searches
2568 * Returns the offset of the found needle, or -1 if not found.
2569 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
2570 * in that case, -1 will be returned if the boundary is reached before
2571 * finding needle. */
2573 tvb_ws_mempbrk_pattern_uint8(tvbuff_t
*tvb
, const int offset
, const int maxlength
,
2574 const ws_mempbrk_pattern
* pattern
, unsigned char *found_needle
)
2576 const uint8_t *result
;
2577 unsigned abs_offset
= 0;
2581 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
2583 exception
= compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &limit
);
2587 /* Only search to end of tvbuff, w/o throwing exception. */
2588 if (limit
> (unsigned) maxlength
) {
2589 /* Maximum length doesn't go past end of tvbuff; search
2594 /* If we have real data, perform our search now. */
2595 if (tvb
->real_data
) {
2596 result
= ws_mempbrk_exec(tvb
->real_data
+ abs_offset
, limit
, pattern
, found_needle
);
2597 if (result
== NULL
) {
2601 return (int) (result
- tvb
->real_data
);
2605 if (tvb
->ops
->tvb_ws_mempbrk_pattern_uint8
)
2606 return tvb
->ops
->tvb_ws_mempbrk_pattern_uint8(tvb
, abs_offset
, limit
, pattern
, found_needle
);
2608 return tvb_ws_mempbrk_uint8_generic(tvb
, abs_offset
, limit
, pattern
, found_needle
);
2611 /* Find size of stringz (NUL-terminated string) by looking for terminating
2612 * NUL. The size of the string includes the terminating NUL.
2614 * If the NUL isn't found, it throws the appropriate exception.
2617 tvb_strsize(tvbuff_t
*tvb
, const int offset
)
2619 unsigned abs_offset
= 0, junk_length
;
2622 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
2624 check_offset_length(tvb
, offset
, 0, &abs_offset
, &junk_length
);
2625 nul_offset
= tvb_find_uint8(tvb
, abs_offset
, -1, 0);
2626 if (nul_offset
== -1) {
2628 * OK, we hit the end of the tvbuff, so we should throw
2631 if (tvb
->length
< tvb
->contained_length
) {
2633 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
2634 THROW(FragmentBoundsError
);
2635 } else if (tvb
->length
< tvb
->reported_length
) {
2636 THROW(ContainedBoundsError
);
2638 THROW(ReportedBoundsError
);
2641 return (nul_offset
- abs_offset
) + 1;
2644 /* UTF-16/UCS-2 version of tvb_strsize */
2645 /* Returns number of bytes including the (two-bytes) null terminator */
2647 tvb_unicode_strsize(tvbuff_t
*tvb
, const int offset
)
2652 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
2655 /* Endianness doesn't matter when looking for null */
2656 uchar
= tvb_get_ntohs(tvb
, offset
+ i
);
2658 } while(uchar
!= 0);
2663 /* Find length of string by looking for end of string ('\0'), up to
2664 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
2666 * Returns -1 if 'maxlength' reached before finding EOS. */
2668 tvb_strnlen(tvbuff_t
*tvb
, const int offset
, const unsigned maxlength
)
2671 unsigned abs_offset
= 0, junk_length
;
2673 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
2675 check_offset_length(tvb
, offset
, 0, &abs_offset
, &junk_length
);
2677 result_offset
= tvb_find_uint8(tvb
, abs_offset
, maxlength
, 0);
2679 if (result_offset
== -1) {
2683 return result_offset
- abs_offset
;
2688 * Implement strneql etc
2692 * Call strncmp after checking if enough chars left, returning 0 if
2693 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
2696 tvb_strneql(tvbuff_t
*tvb
, const int offset
, const char *str
, const size_t size
)
2700 ptr
= ensure_contiguous_no_exception(tvb
, offset
, (int)size
, NULL
);
2703 int cmp
= strncmp((const char *)ptr
, str
, size
);
2706 * Return 0 if equal, -1 otherwise.
2708 return (cmp
== 0 ? 0 : -1);
2711 * Not enough characters in the tvbuff to match the
2719 * Call g_ascii_strncasecmp after checking if enough chars left, returning
2720 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
2723 tvb_strncaseeql(tvbuff_t
*tvb
, const int offset
, const char *str
, const size_t size
)
2727 ptr
= ensure_contiguous_no_exception(tvb
, offset
, (int)size
, NULL
);
2730 int cmp
= g_ascii_strncasecmp((const char *)ptr
, str
, size
);
2733 * Return 0 if equal, -1 otherwise.
2735 return (cmp
== 0 ? 0 : -1);
2738 * Not enough characters in the tvbuff to match the
2746 * Check that the tvbuff contains at least size bytes, starting at
2747 * offset, and that those bytes are equal to str. Return 0 for success
2748 * and -1 for error. This function does not throw an exception.
2751 tvb_memeql(tvbuff_t
*tvb
, const int offset
, const uint8_t *str
, size_t size
)
2755 ptr
= ensure_contiguous_no_exception(tvb
, offset
, (int) size
, NULL
);
2758 int cmp
= memcmp(ptr
, str
, size
);
2761 * Return 0 if equal, -1 otherwise.
2763 return (cmp
== 0 ? 0 : -1);
2766 * Not enough characters in the tvbuff to match the
2774 * Format the data in the tvb from offset for size.
2777 tvb_format_text(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, const int size
)
2782 len
= (size
> 0) ? size
: 0;
2784 ptr
= ensure_contiguous(tvb
, offset
, size
);
2785 return format_text(scope
, ptr
, len
);
2789 * Format the data in the tvb from offset for length ...
2792 tvb_format_text_wsp(wmem_allocator_t
* allocator
, tvbuff_t
*tvb
, const int offset
, const int size
)
2797 len
= (size
> 0) ? size
: 0;
2799 ptr
= ensure_contiguous(tvb
, offset
, size
);
2800 return format_text_wsp(allocator
, ptr
, len
);
2804 * Like "tvb_format_text()", but for null-padded strings; don't show
2805 * the null padding characters as "\000".
2808 tvb_format_stringzpad(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, const int size
)
2810 const uint8_t *ptr
, *p
;
2814 len
= (size
> 0) ? size
: 0;
2816 ptr
= ensure_contiguous(tvb
, offset
, size
);
2817 for (p
= ptr
, stringlen
= 0; stringlen
< len
&& *p
!= '\0'; p
++, stringlen
++)
2819 return format_text(scope
, ptr
, stringlen
);
2823 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
2824 * the null padding characters as "\000".
2827 tvb_format_stringzpad_wsp(wmem_allocator_t
* allocator
, tvbuff_t
*tvb
, const int offset
, const int size
)
2829 const uint8_t *ptr
, *p
;
2833 len
= (size
> 0) ? size
: 0;
2835 ptr
= ensure_contiguous(tvb
, offset
, size
);
2836 for (p
= ptr
, stringlen
= 0; stringlen
< len
&& *p
!= '\0'; p
++, stringlen
++)
2838 return format_text_wsp(allocator
, ptr
, stringlen
);
2842 * All string functions below take a scope as an argument.
2845 * If scope is NULL, memory is allocated with g_malloc() and user must
2846 * explicitly free it with g_free().
2847 * If scope is not NULL, memory is allocated with the corresponding pool
2850 * All functions throw an exception if the tvbuff ends before the string
2855 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
2856 * of bytes referred to by the tvbuff, offset, and length as an ASCII string,
2857 * with all bytes with the high-order bit set being invalid, and return a
2858 * pointer to a UTF-8 string, allocated using the wmem scope.
2860 * Octets with the highest bit set will be converted to the Unicode
2861 * REPLACEMENT CHARACTER.
2864 tvb_get_ascii_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
)
2868 ptr
= ensure_contiguous(tvb
, offset
, length
);
2869 return get_ascii_string(scope
, ptr
, length
);
2873 * Given a wmem scope, a tvbuff, an offset, a length, and a translation table,
2874 * treat the string of bytes referred to by the tvbuff, offset, and length
2875 * as a string encoded using one octet per character, with octets with the
2876 * high-order bit clear being mapped by the translation table to 2-byte
2877 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
2878 * CHARACTER) and octets with the high-order bit set being mapped to
2879 * REPLACEMENT CHARACTER, and return a pointer to a UTF-8 string,
2880 * allocated using the wmem scope.
2882 * Octets with the highest bit set will be converted to the Unicode
2883 * REPLACEMENT CHARACTER.
2886 tvb_get_iso_646_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
, const gunichar2 table
[0x80])
2890 ptr
= ensure_contiguous(tvb
, offset
, length
);
2891 return get_iso_646_string(scope
, ptr
, length
, table
);
2895 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
2896 * of bytes referred to by the tvbuff, the offset. and the length as a UTF-8
2897 * string, and return a pointer to a UTF-8 string, allocated using the wmem
2898 * scope, with all ill-formed sequences replaced with the Unicode REPLACEMENT
2899 * CHARACTER according to the recommended "best practices" given in the Unicode
2900 * Standard and specified by W3C/WHATWG.
2902 * Note that in conformance with the Unicode Standard, this treats three
2903 * byte sequences corresponding to UTF-16 surrogate halves (paired or unpaired)
2904 * and two byte overlong encodings of 7-bit ASCII characters as invalid and
2905 * substitutes REPLACEMENT CHARACTER for them. Explicit support for nonstandard
2906 * derivative encoding formats (e.g. CESU-8, Java Modified UTF-8, WTF-8) could
2910 tvb_get_utf_8_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, const int length
)
2914 ptr
= ensure_contiguous(tvb
, offset
, length
);
2915 return get_utf_8_string(scope
, ptr
, length
);
2919 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
2920 * of bytes referred to by the tvbuff, the offset, and the length as a
2921 * raw string, and return a pointer to that string, allocated using the
2922 * wmem scope. This means a null is appended at the end, but no replacement
2923 * checking is done otherwise, unlike tvb_get_utf_8_string().
2925 * Also, this one allows a length of -1 to mean get all, but does not
2926 * allow a negative offset.
2928 static inline uint8_t *
2929 tvb_get_raw_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, const int length
)
2932 int abs_length
= length
;
2934 DISSECTOR_ASSERT(offset
>= 0);
2935 DISSECTOR_ASSERT(abs_length
>= -1);
2938 abs_length
= tvb
->length
- offset
;
2940 tvb_ensure_bytes_exist(tvb
, offset
, abs_length
);
2941 strbuf
= (uint8_t *)wmem_alloc(scope
, abs_length
+ 1);
2942 tvb_memcpy(tvb
, strbuf
, offset
, abs_length
);
2943 strbuf
[abs_length
] = '\0';
2948 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
2949 * of bytes referred to by the tvbuff, the offset, and the length as an
2950 * ISO 8859/1 string, and return a pointer to a UTF-8 string, allocated
2951 * using the wmem scope.
2954 tvb_get_string_8859_1(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
)
2958 ptr
= ensure_contiguous(tvb
, offset
, length
);
2959 return get_8859_1_string(scope
, ptr
, length
);
2963 * Given a wmem scope, a tvbuff, an offset, a length, and a translation
2964 * table, treat the string of bytes referred to by the tvbuff, the offset,
2965 * and the length as a string encoded using one octet per character, with
2966 * octets with the high-order bit clear being ASCII and octets with the
2967 * high-order bit set being mapped by the translation table to 2-byte
2968 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
2969 * CHARACTER), and return a pointer to a UTF-8 string, allocated with the
2973 tvb_get_string_unichar2(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
, const gunichar2 table
[0x80])
2977 ptr
= ensure_contiguous(tvb
, offset
, length
);
2978 return get_unichar2_string(scope
, ptr
, length
, table
);
2982 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
2983 * giving the byte order, treat the string of bytes referred to by the
2984 * tvbuff, the offset, and the length as a UCS-2 encoded string in
2985 * the byte order in question, containing characters from the Basic
2986 * Multilingual Plane (plane 0) of Unicode, and return a pointer to a
2987 * UTF-8 string, allocated with the wmem scope.
2989 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN.
2991 * Specify length in bytes.
2993 * XXX - should map lead and trail surrogate values to REPLACEMENT
2994 * CHARACTERs (0xFFFD)?
2995 * XXX - if there are an odd number of bytes, should put a
2996 * REPLACEMENT CHARACTER at the end.
2999 tvb_get_ucs_2_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int length
, const unsigned encoding
)
3003 ptr
= ensure_contiguous(tvb
, offset
, length
);
3004 return get_ucs_2_string(scope
, ptr
, length
, encoding
);
3008 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3009 * giving the byte order, treat the string of bytes referred to by the
3010 * tvbuff, the offset, and the length as a UTF-16 encoded string in
3011 * the byte order in question, and return a pointer to a UTF-8 string,
3012 * allocated with the wmem scope.
3014 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN.
3016 * Specify length in bytes.
3018 * XXX - should map surrogate errors to REPLACEMENT CHARACTERs (0xFFFD).
3019 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3020 * XXX - if there are an odd number of bytes, should put a
3021 * REPLACEMENT CHARACTER at the end.
3024 tvb_get_utf_16_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int length
, const unsigned encoding
)
3028 ptr
= ensure_contiguous(tvb
, offset
, length
);
3029 return get_utf_16_string(scope
, ptr
, length
, encoding
);
3033 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3034 * giving the byte order, treat the string of bytes referred to by the
3035 * tvbuff, the offset, and the length as a UCS-4 encoded string in
3036 * the byte order in question, and return a pointer to a UTF-8 string,
3037 * allocated with the wmem scope.
3039 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
3041 * Specify length in bytes
3043 * XXX - should map lead and trail surrogate values to a "substitute"
3045 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3046 * XXX - if the number of bytes isn't a multiple of 4, should put a
3047 * REPLACEMENT CHARACTER at the end.
3050 tvb_get_ucs_4_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int length
, const unsigned encoding
)
3054 ptr
= ensure_contiguous(tvb
, offset
, length
);
3055 return get_ucs_4_string(scope
, ptr
, length
, encoding
);
3059 tvb_get_ts_23_038_7bits_string_packed(wmem_allocator_t
*scope
, tvbuff_t
*tvb
,
3060 const int bit_offset
, int no_of_chars
)
3062 int in_offset
= bit_offset
>> 3; /* Current pointer to the input buffer */
3063 int length
= ((no_of_chars
+ 1) * 7 + (bit_offset
& 0x07)) >> 3;
3066 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
3068 ptr
= ensure_contiguous(tvb
, in_offset
, length
);
3069 return get_ts_23_038_7bits_string_packed(scope
, ptr
, bit_offset
, no_of_chars
);
3073 tvb_get_ts_23_038_7bits_string_unpacked(wmem_allocator_t
*scope
, tvbuff_t
*tvb
,
3074 const int offset
, int length
)
3078 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
3080 ptr
= ensure_contiguous(tvb
, offset
, length
);
3081 return get_ts_23_038_7bits_string_unpacked(scope
, ptr
, length
);
3085 tvb_get_etsi_ts_102_221_annex_a_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
,
3086 const int offset
, int length
)
3090 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
3092 ptr
= ensure_contiguous(tvb
, offset
, length
);
3093 return get_etsi_ts_102_221_annex_a_string(scope
, ptr
, length
);
3097 tvb_get_ascii_7bits_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
,
3098 const int bit_offset
, int no_of_chars
)
3100 int in_offset
= bit_offset
>> 3; /* Current pointer to the input buffer */
3101 int length
= ((no_of_chars
+ 1) * 7 + (bit_offset
& 0x07)) >> 3;
3104 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
3106 ptr
= ensure_contiguous(tvb
, in_offset
, length
);
3107 return get_ascii_7bits_string(scope
, ptr
, bit_offset
, no_of_chars
);
3111 * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3112 * table, treat the string of bytes referred to by the tvbuff, the offset,
3113 * and the length as a string encoded using one octet per character, with
3114 * octets being mapped by the translation table to 2-byte Unicode Basic
3115 * Multilingual Plane characters (including REPLACEMENT CHARACTER), and
3116 * return a pointer to a UTF-8 string, allocated with the wmem scope.
3119 tvb_get_nonascii_unichar2_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
, const gunichar2 table
[256])
3123 ptr
= ensure_contiguous(tvb
, offset
, length
);
3124 return get_nonascii_unichar2_string(scope
, ptr
, length
, table
);
3128 * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3129 * referred to by the tvbuff, offset, and length as a GB18030 encoded string,
3130 * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3131 * converted having substituted REPLACEMENT CHARACTER according to the
3132 * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3133 * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3135 * As expected, this will also decode GBK and GB2312 strings.
3138 tvb_get_gb18030_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
)
3142 ptr
= ensure_contiguous(tvb
, offset
, length
);
3143 return get_gb18030_string(scope
, ptr
, length
);
3147 * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3148 * referred to by the tvbuff, offset, and length as a EUC-KR encoded string,
3149 * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3150 * converted having substituted REPLACEMENT CHARACTER according to the
3151 * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3152 * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3155 tvb_get_euc_kr_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
)
3159 ptr
= ensure_contiguous(tvb
, offset
, length
);
3160 return get_euc_kr_string(scope
, ptr
, length
);
3164 tvb_get_t61_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
)
3168 ptr
= ensure_contiguous(tvb
, offset
, length
);
3169 return get_t61_string(scope
, ptr
, length
);
3173 * Encoding tables for BCD strings.
3175 static const dgt_set_t Dgt0_9_bcd
= {
3177 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3178 '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?'
3182 static const dgt_set_t Dgt_keypad_abc_tbcd
= {
3184 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3185 '0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'
3189 static const dgt_set_t Dgt_ansi_tbcd
= {
3191 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3192 '0','1','2','3','4','5','6','7','8','9','?','B','C','*','#','?'
3196 static const dgt_set_t Dgt_dect_standard_4bits_tbcd
= {
3198 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3199 '0','1','2','3','4','5','6','7','8','9','?',' ','?','?','?','?'
3204 tvb_get_apn_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
,
3210 * This is a domain name.
3212 * 3GPP TS 23.003, section 19.4.2 "Fully Qualified Domain Names
3213 * (FQDNs)", subsection 19.4.2.1 "General", says:
3215 * The encoding of any identifier used as part of a Fully
3216 * Qualifed Domain Name (FQDN) shall follow the Name Syntax
3217 * defined in IETF RFC 2181 [18], IETF RFC 1035 [19] and
3218 * IETF RFC 1123 [20]. An FQDN consists of one or more
3219 * labels. Each label is coded as a one octet length field
3220 * followed by that number of octets coded as 8 bit ASCII
3223 * so this does not appear to use full-blown DNS compression -
3224 * the upper 2 bits of the length don't indicate that it's a
3225 * pointer or an extended label (RFC 2673).
3227 str
= wmem_strbuf_new_sized(scope
, length
+ 1);
3231 ptr
= ensure_contiguous(tvb
, offset
, length
);
3237 * Process this label.
3243 while (label_len
!= 0) {
3251 wmem_strbuf_append_c(str
, ch
);
3253 wmem_strbuf_append_unichar_repl(str
);
3262 wmem_strbuf_append_c(str
, '.');
3267 return (uint8_t *) wmem_strbuf_finalize(str
);
3271 tvb_get_dect_standard_8bits_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int length
)
3275 ptr
= ensure_contiguous(tvb
, offset
, length
);
3276 return get_dect_standard_8bits_string(scope
, ptr
, length
);
3280 * Given a tvbuff, an offset, a length, and an encoding, allocate a
3281 * buffer big enough to hold a non-null-terminated string of that length
3282 * at that offset, plus a trailing '\0', copy into the buffer the
3283 * string as converted from the appropriate encoding to UTF-8, and
3284 * return a pointer to the string.
3287 tvb_get_string_enc(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
,
3288 const int length
, const unsigned encoding
)
3291 bool odd
, skip_first
;
3293 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
3295 /* make sure length = -1 fails */
3297 THROW(ReportedBoundsError
);
3300 switch (encoding
& ENC_CHARENCODING_MASK
) {
3305 * For now, we treat bogus values as meaning
3306 * "ASCII" rather than reporting an error,
3307 * for the benefit of old dissectors written
3308 * when the last argument to proto_tree_add_item()
3309 * was a bool for the byte order, not an
3310 * encoding value, and passed non-zero values
3311 * other than true to mean "little-endian".
3313 strptr
= tvb_get_ascii_string(scope
, tvb
, offset
, length
);
3317 strptr
= tvb_get_utf_8_string(scope
, tvb
, offset
, length
);
3321 strptr
= tvb_get_utf_16_string(scope
, tvb
, offset
, length
,
3322 encoding
& (ENC_LITTLE_ENDIAN
|ENC_BOM
));
3326 strptr
= tvb_get_ucs_2_string(scope
, tvb
, offset
, length
,
3327 encoding
& (ENC_LITTLE_ENDIAN
|ENC_BOM
));
3331 strptr
= tvb_get_ucs_4_string(scope
, tvb
, offset
, length
,
3332 encoding
& (ENC_LITTLE_ENDIAN
|ENC_BOM
));
3335 case ENC_ISO_8859_1
:
3337 * ISO 8859-1 printable code point values are equal
3338 * to the equivalent Unicode code point value, so
3339 * no translation table is needed.
3341 strptr
= tvb_get_string_8859_1(scope
, tvb
, offset
, length
);
3344 case ENC_ISO_8859_2
:
3345 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_2
);
3348 case ENC_ISO_8859_3
:
3349 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_3
);
3352 case ENC_ISO_8859_4
:
3353 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_4
);
3356 case ENC_ISO_8859_5
:
3357 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_5
);
3360 case ENC_ISO_8859_6
:
3361 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_6
);
3364 case ENC_ISO_8859_7
:
3365 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_7
);
3368 case ENC_ISO_8859_8
:
3369 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_8
);
3372 case ENC_ISO_8859_9
:
3373 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_9
);
3376 case ENC_ISO_8859_10
:
3377 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_10
);
3380 case ENC_ISO_8859_11
:
3381 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_11
);
3384 case ENC_ISO_8859_13
:
3385 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_13
);
3388 case ENC_ISO_8859_14
:
3389 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_14
);
3392 case ENC_ISO_8859_15
:
3393 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_15
);
3396 case ENC_ISO_8859_16
:
3397 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_iso_8859_16
);
3400 case ENC_WINDOWS_1250
:
3401 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_cp1250
);
3404 case ENC_WINDOWS_1251
:
3405 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_cp1251
);
3408 case ENC_WINDOWS_1252
:
3409 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_cp1252
);
3413 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_mac_roman
);
3417 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_cp437
);
3421 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_cp855
);
3425 strptr
= tvb_get_string_unichar2(scope
, tvb
, offset
, length
, charset_table_cp866
);
3428 case ENC_ISO_646_BASIC
:
3429 strptr
= tvb_get_iso_646_string(scope
, tvb
, offset
, length
, charset_table_iso_646_basic
);
3432 case ENC_3GPP_TS_23_038_7BITS_PACKED
:
3434 int bit_offset
= offset
<< 3;
3435 int no_of_chars
= (length
<< 3) / 7;
3436 strptr
= tvb_get_ts_23_038_7bits_string_packed(scope
, tvb
, bit_offset
, no_of_chars
);
3440 case ENC_ASCII_7BITS
:
3442 int bit_offset
= offset
<< 3;
3443 int no_of_chars
= (length
<< 3) / 7;
3444 strptr
= tvb_get_ascii_7bits_string(scope
, tvb
, bit_offset
, no_of_chars
);
3450 * "Common" EBCDIC, covering all characters with the
3451 * same code point in all Roman-alphabet EBCDIC code
3454 strptr
= tvb_get_nonascii_unichar2_string(scope
, tvb
, offset
, length
, charset_table_ebcdic
);
3457 case ENC_EBCDIC_CP037
:
3459 * EBCDIC code page 037.
3461 strptr
= tvb_get_nonascii_unichar2_string(scope
, tvb
, offset
, length
, charset_table_ebcdic_cp037
);
3464 case ENC_EBCDIC_CP500
:
3466 * EBCDIC code page 500.
3468 strptr
= tvb_get_nonascii_unichar2_string(scope
, tvb
, offset
, length
, charset_table_ebcdic_cp500
);
3472 strptr
= tvb_get_t61_string(scope
, tvb
, offset
, length
);
3475 case ENC_BCD_DIGITS_0_9
:
3477 * Packed BCD, with digits 0-9.
3479 odd
= (encoding
& ENC_BCD_ODD_NUM_DIG
) >> 16;
3480 skip_first
= (encoding
& ENC_BCD_SKIP_FIRST
) >> 17;
3481 strptr
= tvb_get_bcd_string(scope
, tvb
, offset
, length
, &Dgt0_9_bcd
, skip_first
, odd
, !(encoding
& ENC_LITTLE_ENDIAN
));
3484 case ENC_KEYPAD_ABC_TBCD
:
3486 * Keypad-with-a/b/c "telephony BCD" - packed BCD, with
3487 * digits 0-9 and symbols *, #, a, b, and c.
3489 odd
= (encoding
& ENC_BCD_ODD_NUM_DIG
) >> 16;
3490 skip_first
= (encoding
& ENC_BCD_SKIP_FIRST
) >> 17;
3491 strptr
= tvb_get_bcd_string(scope
, tvb
, offset
, length
, &Dgt_keypad_abc_tbcd
, skip_first
, odd
, !(encoding
& ENC_LITTLE_ENDIAN
));
3494 case ENC_KEYPAD_BC_TBCD
:
3496 * Keypad-with-B/C "telephony BCD" - packed BCD, with
3497 * digits 0-9 and symbols B, C, *, and #.
3499 odd
= (encoding
& ENC_BCD_ODD_NUM_DIG
) >> 16;
3500 skip_first
= (encoding
& ENC_BCD_SKIP_FIRST
) >> 17;
3501 strptr
= tvb_get_bcd_string(scope
, tvb
, offset
, length
, &Dgt_ansi_tbcd
, skip_first
, odd
, !(encoding
& ENC_LITTLE_ENDIAN
));
3504 case ENC_3GPP_TS_23_038_7BITS_UNPACKED
:
3505 strptr
= tvb_get_ts_23_038_7bits_string_unpacked(scope
, tvb
, offset
, length
);
3508 case ENC_ETSI_TS_102_221_ANNEX_A
:
3509 strptr
= tvb_get_etsi_ts_102_221_annex_a_string(scope
, tvb
, offset
, length
);
3513 strptr
= tvb_get_gb18030_string(scope
, tvb
, offset
, length
);
3517 strptr
= tvb_get_euc_kr_string(scope
, tvb
, offset
, length
);
3521 strptr
= tvb_get_apn_string(scope
, tvb
, offset
, length
);
3524 case ENC_DECT_STANDARD_8BITS
:
3525 strptr
= tvb_get_dect_standard_8bits_string(scope
, tvb
, offset
, length
);
3528 case ENC_DECT_STANDARD_4BITS_TBCD
:
3530 * DECT standard 4bits "telephony BCD" - packed BCD, with
3531 * digits 0-9 and symbol SPACE for 0xb.
3533 odd
= (encoding
& ENC_BCD_ODD_NUM_DIG
) >> 16;
3534 skip_first
= (encoding
& ENC_BCD_SKIP_FIRST
) >> 17;
3535 strptr
= tvb_get_bcd_string(scope
, tvb
, offset
, length
, &Dgt_dect_standard_4bits_tbcd
, skip_first
, odd
, false);
3542 * This is like tvb_get_string_enc(), except that it handles null-padded
3545 * Currently, string values are stored as UTF-8 null-terminated strings,
3546 * so nothing needs to be done differently for null-padded strings; we
3547 * could save a little memory by not storing the null padding.
3549 * If we ever store string values differently, in a fashion that doesn't
3550 * involve null termination, that might change.
3553 tvb_get_stringzpad(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
,
3554 const int length
, const unsigned encoding
)
3556 return tvb_get_string_enc(scope
, tvb
, offset
, length
, encoding
);
3560 * These routines are like the above routines, except that they handle
3561 * null-terminated strings. They find the length of that string (and
3562 * throw an exception if the tvbuff ends before we find the null), and
3563 * also return through a pointer the length of the string, in bytes,
3564 * including the terminating null (the terminating null being 2 bytes
3565 * for UCS-2 and UTF-16, 4 bytes for UCS-4, and 1 byte for other
3569 tvb_get_ascii_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
)
3574 size
= tvb_strsize(tvb
, offset
);
3575 ptr
= ensure_contiguous(tvb
, offset
, size
);
3576 /* XXX, conversion between signed/unsigned integer */
3579 return get_ascii_string(scope
, ptr
, size
);
3583 tvb_get_iso_646_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
, const gunichar2 table
[0x80])
3588 size
= tvb_strsize(tvb
, offset
);
3589 ptr
= ensure_contiguous(tvb
, offset
, size
);
3590 /* XXX, conversion between signed/unsigned integer */
3593 return get_iso_646_string(scope
, ptr
, size
, table
);
3597 tvb_get_utf_8_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int *lengthp
)
3602 size
= tvb_strsize(tvb
, offset
);
3603 ptr
= ensure_contiguous(tvb
, offset
, size
);
3604 /* XXX, conversion between signed/unsigned integer */
3607 return get_utf_8_string(scope
, ptr
, size
);
3611 tvb_get_stringz_8859_1(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
)
3616 size
= tvb_strsize(tvb
, offset
);
3617 ptr
= ensure_contiguous(tvb
, offset
, size
);
3618 /* XXX, conversion between signed/unsigned integer */
3621 return get_8859_1_string(scope
, ptr
, size
);
3625 tvb_get_stringz_unichar2(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
, const gunichar2 table
[0x80])
3630 size
= tvb_strsize(tvb
, offset
);
3631 ptr
= ensure_contiguous(tvb
, offset
, size
);
3632 /* XXX, conversion between signed/unsigned integer */
3635 return get_unichar2_string(scope
, ptr
, size
, table
);
3639 * Given a tvbuff and an offset, with the offset assumed to refer to
3640 * a null-terminated string, find the length of that string (and throw
3641 * an exception if the tvbuff ends before we find the null), ensure that
3642 * the TVB is flat, and return a pointer to the string (in the TVB).
3643 * Also return the length of the string (including the terminating null)
3644 * through a pointer.
3646 * As long as we aren't using composite TVBs, this saves the cycles used
3647 * (often unnecessariliy) in allocating a buffer and copying the string into
3648 * it. (If we do start using composite TVBs, we may want to replace this
3649 * function with the _ephemeral version.)
3652 tvb_get_const_stringz(tvbuff_t
*tvb
, const int offset
, int *lengthp
)
3655 const uint8_t *strptr
;
3657 size
= tvb_strsize(tvb
, offset
);
3658 strptr
= ensure_contiguous(tvb
, offset
, size
);
3665 tvb_get_ucs_2_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int *lengthp
, const unsigned encoding
)
3667 int size
; /* Number of bytes in string */
3670 size
= tvb_unicode_strsize(tvb
, offset
);
3671 ptr
= ensure_contiguous(tvb
, offset
, size
);
3672 /* XXX, conversion between signed/unsigned integer */
3675 return get_ucs_2_string(scope
, ptr
, size
, encoding
);
3679 tvb_get_utf_16_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int *lengthp
, const unsigned encoding
)
3684 size
= tvb_unicode_strsize(tvb
, offset
);
3685 ptr
= ensure_contiguous(tvb
, offset
, size
);
3686 /* XXX, conversion between signed/unsigned integer */
3689 return get_utf_16_string(scope
, ptr
, size
, encoding
);
3693 tvb_get_ucs_4_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int *lengthp
, const unsigned encoding
)
3701 /* Endianness doesn't matter when looking for null */
3702 uchar
= tvb_get_ntohl(tvb
, offset
+ size
);
3704 } while(uchar
!= 0);
3706 ptr
= ensure_contiguous(tvb
, offset
, size
);
3707 /* XXX, conversion between signed/unsigned integer */
3710 return get_ucs_4_string(scope
, ptr
, size
, encoding
);
3714 tvb_get_nonascii_unichar2_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
, const gunichar2 table
[256])
3719 size
= tvb_strsize(tvb
, offset
);
3720 ptr
= ensure_contiguous(tvb
, offset
, size
);
3721 /* XXX, conversion between signed/unsigned integer */
3724 return get_nonascii_unichar2_string(scope
, ptr
, size
, table
);
3728 tvb_get_t61_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
)
3733 size
= tvb_strsize(tvb
, offset
);
3734 ptr
= ensure_contiguous(tvb
, offset
, size
);
3735 /* XXX, conversion between signed/unsigned integer */
3738 return get_t61_string(scope
, ptr
, size
);
3742 tvb_get_gb18030_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
)
3747 size
= tvb_strsize(tvb
, offset
);
3748 ptr
= ensure_contiguous(tvb
, offset
, size
);
3749 /* XXX, conversion between signed/unsigned integer */
3752 return get_gb18030_string(scope
, ptr
, size
);
3756 tvb_get_euc_kr_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
)
3761 size
= tvb_strsize(tvb
, offset
);
3762 ptr
= ensure_contiguous(tvb
, offset
, size
);
3763 /* XXX, conversion between signed/unsigned integer */
3766 return get_euc_kr_string(scope
, ptr
, size
);
3770 tvb_get_dect_standard_8bits_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
, int *lengthp
)
3775 size
= tvb_strsize(tvb
, offset
);
3776 ptr
= ensure_contiguous(tvb
, offset
, size
);
3777 /* XXX, conversion between signed/unsigned integer */
3780 return get_t61_string(scope
, ptr
, size
);
3784 tvb_get_stringz_enc(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int *lengthp
, const unsigned encoding
)
3788 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
3790 switch (encoding
& ENC_CHARENCODING_MASK
) {
3795 * For now, we treat bogus values as meaning
3796 * "ASCII" rather than reporting an error,
3797 * for the benefit of old dissectors written
3798 * when the last argument to proto_tree_add_item()
3799 * was a bool for the byte order, not an
3800 * encoding value, and passed non-zero values
3801 * other than true to mean "little-endian".
3803 strptr
= tvb_get_ascii_stringz(scope
, tvb
, offset
, lengthp
);
3808 * XXX - should map all invalid UTF-8 sequences
3809 * to a "substitute" UTF-8 character.
3810 * XXX - should map code points > 10FFFF to REPLACEMENT
3813 strptr
= tvb_get_utf_8_stringz(scope
, tvb
, offset
, lengthp
);
3817 strptr
= tvb_get_utf_16_stringz(scope
, tvb
, offset
, lengthp
,
3818 encoding
& (ENC_LITTLE_ENDIAN
|ENC_BOM
));
3822 strptr
= tvb_get_ucs_2_stringz(scope
, tvb
, offset
, lengthp
,
3823 encoding
& (ENC_LITTLE_ENDIAN
|ENC_BOM
));
3827 strptr
= tvb_get_ucs_4_stringz(scope
, tvb
, offset
, lengthp
,
3828 encoding
& (ENC_LITTLE_ENDIAN
|ENC_BOM
));
3831 case ENC_ISO_8859_1
:
3833 * ISO 8859-1 printable code point values are equal
3834 * to the equivalent Unicode code point value, so
3835 * no translation table is needed.
3837 strptr
= tvb_get_stringz_8859_1(scope
, tvb
, offset
, lengthp
);
3840 case ENC_ISO_8859_2
:
3841 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_2
);
3844 case ENC_ISO_8859_3
:
3845 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_3
);
3848 case ENC_ISO_8859_4
:
3849 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_4
);
3852 case ENC_ISO_8859_5
:
3853 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_5
);
3856 case ENC_ISO_8859_6
:
3857 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_6
);
3860 case ENC_ISO_8859_7
:
3861 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_7
);
3864 case ENC_ISO_8859_8
:
3865 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_8
);
3868 case ENC_ISO_8859_9
:
3869 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_9
);
3872 case ENC_ISO_8859_10
:
3873 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_10
);
3876 case ENC_ISO_8859_11
:
3877 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_11
);
3880 case ENC_ISO_8859_13
:
3881 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_13
);
3884 case ENC_ISO_8859_14
:
3885 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_14
);
3888 case ENC_ISO_8859_15
:
3889 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_15
);
3892 case ENC_ISO_8859_16
:
3893 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_iso_8859_16
);
3896 case ENC_WINDOWS_1250
:
3897 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_cp1250
);
3900 case ENC_WINDOWS_1251
:
3901 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_cp1251
);
3904 case ENC_WINDOWS_1252
:
3905 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_cp1252
);
3909 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_mac_roman
);
3913 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_cp437
);
3917 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_cp855
);
3921 strptr
= tvb_get_stringz_unichar2(scope
, tvb
, offset
, lengthp
, charset_table_cp866
);
3924 case ENC_ISO_646_BASIC
:
3925 strptr
= tvb_get_iso_646_stringz(scope
, tvb
, offset
, lengthp
, charset_table_iso_646_basic
);
3928 case ENC_3GPP_TS_23_038_7BITS_PACKED
:
3929 case ENC_3GPP_TS_23_038_7BITS_UNPACKED
:
3930 case ENC_ETSI_TS_102_221_ANNEX_A
:
3931 REPORT_DISSECTOR_BUG("TS 23.038 7bits has no null character and doesn't support null-terminated strings");
3934 case ENC_ASCII_7BITS
:
3935 REPORT_DISSECTOR_BUG("tvb_get_stringz_enc function with ENC_ASCII_7BITS not implemented yet");
3940 * "Common" EBCDIC, covering all characters with the
3941 * same code point in all Roman-alphabet EBCDIC code
3944 strptr
= tvb_get_nonascii_unichar2_stringz(scope
, tvb
, offset
, lengthp
, charset_table_ebcdic
);
3947 case ENC_EBCDIC_CP037
:
3949 * EBCDIC code page 037.
3951 strptr
= tvb_get_nonascii_unichar2_stringz(scope
, tvb
, offset
, lengthp
, charset_table_ebcdic_cp037
);
3954 case ENC_EBCDIC_CP500
:
3956 * EBCDIC code page 500.
3958 strptr
= tvb_get_nonascii_unichar2_stringz(scope
, tvb
, offset
, lengthp
, charset_table_ebcdic_cp500
);
3962 strptr
= tvb_get_t61_stringz(scope
, tvb
, offset
, lengthp
);
3966 strptr
= tvb_get_gb18030_stringz(scope
, tvb
, offset
, lengthp
);
3970 strptr
= tvb_get_euc_kr_stringz(scope
, tvb
, offset
, lengthp
);
3973 case ENC_DECT_STANDARD_8BITS
:
3974 strptr
= tvb_get_dect_standard_8bits_stringz(scope
, tvb
, offset
, lengthp
);
3981 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
3982 * no more than bufsize number of bytes, including terminating NUL, to buffer.
3983 * Returns length of string (not including terminating NUL), or -1 if the string was
3984 * truncated in the buffer due to not having reached the terminating NUL.
3985 * In this way, it acts like snprintf().
3987 * bufsize MUST be greater than 0.
3989 * When processing a packet where the remaining number of bytes is less
3990 * than bufsize, an exception is not thrown if the end of the packet
3991 * is reached before the NUL is found. If no NUL is found before reaching
3992 * the end of the short packet, -1 is still returned, and the string
3993 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
3994 * at the correct spot, terminating the string.
3996 * *bytes_copied will contain the number of bytes actually copied,
3997 * including the terminating-NUL.
4000 _tvb_get_raw_bytes_as_stringz(tvbuff_t
*tvb
, const int offset
, const unsigned bufsize
, uint8_t* buffer
, int *bytes_copied
)
4003 unsigned abs_offset
= 0;
4005 bool decreased_max
= false;
4007 /* Only read to end of tvbuff, w/o throwing exception. */
4008 check_offset_length(tvb
, offset
, -1, &abs_offset
, &len
);
4010 /* There must at least be room for the terminating NUL. */
4011 DISSECTOR_ASSERT(bufsize
!= 0);
4013 /* If there's no room for anything else, just return the NUL. */
4020 /* check_offset_length() won't throw an exception if we're
4021 * looking at the byte immediately after the end of the tvbuff. */
4023 THROW(ReportedBoundsError
);
4026 /* This should not happen because check_offset_length() would
4027 * have already thrown an exception if 'offset' were out-of-bounds.
4029 DISSECTOR_ASSERT(len
!= -1);
4032 * If we've been passed a negative number, bufsize will
4035 DISSECTOR_ASSERT(bufsize
<= INT_MAX
);
4037 if ((unsigned)len
< bufsize
) {
4039 decreased_max
= true;
4045 stringlen
= tvb_strnlen(tvb
, abs_offset
, limit
- 1);
4046 /* If NUL wasn't found, copy the data and return -1 */
4047 if (stringlen
== -1) {
4048 tvb_memcpy(tvb
, buffer
, abs_offset
, limit
);
4049 if (decreased_max
) {
4051 /* Add 1 for the extra NUL that we set at buffer[limit],
4052 * pretending that it was copied as part of the string. */
4053 *bytes_copied
= limit
+ 1;
4056 *bytes_copied
= limit
;
4061 /* Copy the string to buffer */
4062 tvb_memcpy(tvb
, buffer
, abs_offset
, stringlen
+ 1);
4063 *bytes_copied
= stringlen
+ 1;
4068 tvb_get_raw_bytes_as_stringz(tvbuff_t
*tvb
, const int offset
, const unsigned bufsize
, uint8_t* buffer
)
4070 int len
, bytes_copied
;
4072 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4074 len
= _tvb_get_raw_bytes_as_stringz(tvb
, offset
, bufsize
, buffer
, &bytes_copied
);
4077 buffer
[bufsize
- 1] = 0;
4078 return bytes_copied
- 1;
4086 * Given a tvbuff, an offset into the tvbuff, a buffer, and a buffer size,
4087 * extract as many raw bytes from the tvbuff, starting at the offset,
4088 * as 1) are available in the tvbuff and 2) will fit in the buffer, leaving
4089 * room for a terminating NUL.
4092 tvb_get_raw_bytes_as_string(tvbuff_t
*tvb
, const int offset
, char *buffer
, size_t bufsize
)
4096 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4098 /* There must be room for the string and the terminating NUL. */
4099 DISSECTOR_ASSERT(bufsize
> 0);
4101 DISSECTOR_ASSERT(bufsize
- 1 < INT_MAX
);
4103 len
= tvb_captured_length_remaining(tvb
, offset
);
4108 if (len
> (int)(bufsize
- 1))
4109 len
= (int)(bufsize
- 1);
4111 /* Copy the string to buffer */
4112 tvb_memcpy(tvb
, buffer
, offset
, len
);
4118 tvb_ascii_isprint(tvbuff_t
*tvb
, const int offset
, const int length
)
4120 const uint8_t* buf
= tvb_get_ptr(tvb
, offset
, length
);
4121 unsigned abs_offset
, abs_length
= length
;
4124 /* tvb_get_ptr has already checked for exceptions. */
4125 compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &abs_length
);
4127 for (unsigned i
= 0; i
< abs_length
; i
++, buf
++)
4128 if (!g_ascii_isprint(*buf
))
4135 tvb_utf_8_isprint(tvbuff_t
*tvb
, const int offset
, const int length
)
4137 const uint8_t* buf
= tvb_get_ptr(tvb
, offset
, length
);
4138 unsigned abs_offset
, abs_length
= length
;
4141 /* tvb_get_ptr has already checked for exceptions. */
4142 compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &abs_length
);
4145 return isprint_utf8_string(buf
, abs_length
);
4149 tvb_ascii_isdigit(tvbuff_t
*tvb
, const int offset
, const int length
)
4151 const uint8_t* buf
= tvb_get_ptr(tvb
, offset
, length
);
4152 unsigned abs_offset
, abs_length
= length
;
4155 /* tvb_get_ptr has already checked for exceptions. */
4156 compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &abs_length
);
4158 for (unsigned i
= 0; i
< abs_length
; i
++, buf
++)
4159 if (!g_ascii_isdigit(*buf
))
4165 static ws_mempbrk_pattern pbrk_crlf
;
4167 * Given a tvbuff, an offset into the tvbuff, and a length that starts
4168 * at that offset (which may be -1 for "all the way to the end of the
4169 * tvbuff"), find the end of the (putative) line that starts at the
4170 * specified offset in the tvbuff, going no further than the specified
4173 * Return the length of the line (not counting the line terminator at
4174 * the end), or, if we don't find a line terminator:
4176 * if "desegment" is true, return -1;
4178 * if "desegment" is false, return the amount of data remaining in
4181 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4182 * character past the line terminator, or past the end of the buffer if
4183 * we don't find a line terminator. (It's not set if we return -1.)
4186 tvb_find_line_end(tvbuff_t
*tvb
, const int offset
, int len
, int *next_offset
, const bool desegment
)
4191 unsigned char found_needle
= 0;
4192 static bool compiled
= false;
4194 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4197 len
= _tvb_captured_length_remaining(tvb
, offset
);
4198 /* if offset is past the end of the tvbuff, len is now 0 */
4201 eob_offset
= offset
+ len
;
4204 ws_mempbrk_compile(&pbrk_crlf
, "\r\n");
4209 * Look either for a CR or an LF.
4211 eol_offset
= tvb_ws_mempbrk_pattern_uint8(tvb
, offset
, len
, &pbrk_crlf
, &found_needle
);
4212 if (eol_offset
== -1) {
4214 * No CR or LF - line is presumably continued in next packet.
4218 * Tell our caller we saw no EOL, so they can
4219 * try to desegment and get the entire line
4225 * Pretend the line runs to the end of the tvbuff.
4227 linelen
= eob_offset
- offset
;
4229 *next_offset
= eob_offset
;
4233 * Find the number of bytes between the starting offset
4236 linelen
= eol_offset
- offset
;
4241 if (found_needle
== '\r') {
4243 * Yes - is it followed by an LF?
4245 if (eol_offset
+ 1 >= eob_offset
) {
4247 * Dunno - the next byte isn't in this
4252 * We'll return -1, although that
4253 * runs the risk that if the line
4254 * really *is* terminated with a CR,
4255 * we won't properly dissect this
4258 * It's probably more likely that
4259 * the line ends with CR-LF than
4260 * that it ends with CR by itself.
4266 * Well, we can at least look at the next
4269 if (tvb_get_uint8(tvb
, eol_offset
+ 1) == '\n') {
4271 * It's an LF; skip over the CR.
4279 * Return the offset of the character after the last
4280 * character in the line, skipping over the last character
4281 * in the line terminator.
4284 *next_offset
= eol_offset
+ 1;
4289 static ws_mempbrk_pattern pbrk_crlf_dquote
;
4291 * Given a tvbuff, an offset into the tvbuff, and a length that starts
4292 * at that offset (which may be -1 for "all the way to the end of the
4293 * tvbuff"), find the end of the (putative) line that starts at the
4294 * specified offset in the tvbuff, going no further than the specified
4297 * However, treat quoted strings inside the buffer specially - don't
4298 * treat newlines in quoted strings as line terminators.
4300 * Return the length of the line (not counting the line terminator at
4301 * the end), or the amount of data remaining in the buffer if we don't
4302 * find a line terminator.
4304 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4305 * character past the line terminator, or past the end of the buffer if
4306 * we don't find a line terminator.
4309 tvb_find_line_end_unquoted(tvbuff_t
*tvb
, const int offset
, int len
, int *next_offset
)
4311 int cur_offset
, char_offset
;
4313 unsigned char c
= 0;
4316 static bool compiled
= false;
4318 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4321 len
= _tvb_captured_length_remaining(tvb
, offset
);
4324 ws_mempbrk_compile(&pbrk_crlf_dquote
, "\r\n\"");
4329 * XXX - what if "len" is still -1, meaning "offset is past the
4330 * end of the tvbuff"?
4332 eob_offset
= offset
+ len
;
4334 cur_offset
= offset
;
4338 * Is this part of the string quoted?
4342 * Yes - look only for the terminating quote.
4344 char_offset
= tvb_find_uint8(tvb
, cur_offset
, len
,
4348 * Look either for a CR, an LF, or a '"'.
4350 char_offset
= tvb_ws_mempbrk_pattern_uint8(tvb
, cur_offset
, len
, &pbrk_crlf_dquote
, &c
);
4352 if (char_offset
== -1) {
4354 * Not found - line is presumably continued in
4356 * We pretend the line runs to the end of the tvbuff.
4358 linelen
= eob_offset
- offset
;
4360 *next_offset
= eob_offset
;
4366 * We're processing a quoted string.
4367 * We only looked for ", so we know it's a ";
4368 * as we're processing a quoted string, it's a
4378 * Un-quoted "; it begins a quoted
4384 * It's a CR or LF; we've found a line
4387 * Find the number of bytes between the
4388 * starting offset and the CR or LF.
4390 linelen
= char_offset
- offset
;
4397 * Yes; is it followed by an LF?
4399 if (char_offset
+ 1 < eob_offset
&&
4400 tvb_get_uint8(tvb
, char_offset
+ 1)
4403 * Yes; skip over the CR.
4410 * Return the offset of the character after
4411 * the last character in the line, skipping
4412 * over the last character in the line
4413 * terminator, and quit.
4416 *next_offset
= char_offset
+ 1;
4422 * Step past the character we found.
4424 cur_offset
= char_offset
+ 1;
4425 if (cur_offset
>= eob_offset
) {
4427 * The character we found was the last character
4428 * in the tvbuff - line is presumably continued in
4430 * We pretend the line runs to the end of the tvbuff.
4432 linelen
= eob_offset
- offset
;
4434 *next_offset
= eob_offset
;
4442 * Copied from the mgcp dissector. (This function should be moved to /epan )
4443 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
4444 * character following offset or offset + maxlength -1 whichever
4448 * tvb - The tvbuff in which we are skipping whitespace.
4449 * offset - The offset in tvb from which we begin trying to skip whitespace.
4450 * maxlength - The maximum distance from offset that we may try to skip
4453 * Returns: The position in tvb of the first non-whitespace
4454 * character following offset or offset + maxlength -1 whichever
4458 tvb_skip_wsp(tvbuff_t
*tvb
, const int offset
, const int maxlength
)
4464 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4466 /* Get the length remaining */
4467 /*tvb_len = tvb_captured_length(tvb);*/
4468 tvb_len
= tvb
->length
;
4470 end
= offset
+ maxlength
;
4476 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
4477 for (counter
= offset
;
4479 ((tempchar
= tvb_get_uint8(tvb
,counter
)) == ' ' ||
4480 tempchar
== '\t' || tempchar
== '\r' || tempchar
== '\n');
4487 tvb_skip_wsp_return(tvbuff_t
*tvb
, const int offset
)
4492 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4494 for (counter
= offset
; counter
> 0 &&
4495 ((tempchar
= tvb_get_uint8(tvb
,counter
)) == ' ' ||
4496 tempchar
== '\t' || tempchar
== '\n' || tempchar
== '\r'); counter
--);
4503 tvb_skip_uint8(tvbuff_t
*tvb
, int offset
, const int maxlength
, const uint8_t ch
)
4507 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4509 /* Get the length remaining */
4510 /*tvb_len = tvb_captured_length(tvb);*/
4511 tvb_len
= tvb
->length
;
4513 end
= offset
+ maxlength
;
4517 while (offset
< end
) {
4518 uint8_t tempch
= tvb_get_uint8(tvb
, offset
);
4528 static ws_mempbrk_pattern pbrk_whitespace
;
4530 int tvb_get_token_len(tvbuff_t
*tvb
, const int offset
, int len
, int *next_offset
, const bool desegment
)
4535 unsigned char found_needle
= 0;
4536 static bool compiled
= false;
4538 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4541 len
= _tvb_captured_length_remaining(tvb
, offset
);
4542 /* if offset is past the end of the tvbuff, len is now 0 */
4545 eob_offset
= offset
+ len
;
4548 ws_mempbrk_compile(&pbrk_whitespace
, " \r\n");
4553 * Look either for a space, CR, or LF.
4555 eot_offset
= tvb_ws_mempbrk_pattern_uint8(tvb
, offset
, len
, &pbrk_whitespace
, &found_needle
);
4556 if (eot_offset
== -1) {
4558 * No space, CR or LF - token is presumably continued in next packet.
4562 * Tell our caller we saw no whitespace, so they can
4563 * try to desegment and get the entire line
4570 * Pretend the token runs to the end of the tvbuff.
4572 tokenlen
= eob_offset
- offset
;
4574 *next_offset
= eob_offset
;
4579 * Find the number of bytes between the starting offset
4580 * and the space, CR or LF.
4582 tokenlen
= eot_offset
- offset
;
4585 * Return the offset of the character after the last
4586 * character in the line, skipping over the last character
4587 * in the line terminator.
4590 *next_offset
= eot_offset
+ 1;
4596 * Format a bunch of data from a tvbuff as bytes, returning a pointer
4597 * to the string with the formatted data, with "punct" as a byte
4601 tvb_bytes_to_str_punct(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, const int len
, const char punct
)
4603 DISSECTOR_ASSERT(len
> 0);
4604 return bytes_to_str_punct(scope
, ensure_contiguous(tvb
, offset
, len
), len
, punct
);
4608 * Given a wmem scope, a tvbuff, an offset, a length, an input digit
4609 * set, and a boolean indicator, fetch BCD-encoded digits from a
4610 * tvbuff starting from either the low or high half byte of the
4611 * first byte depending on the boolean indicator (true means "start
4612 * with the high half byte, ignoring the low half byte", and false
4613 * means "start with the low half byte and proceed to the high half
4614 * byte), formating the digits into characters according to the
4615 * input digit set, and return a pointer to a UTF-8 string, allocated
4616 * using the wmem scope. A nibble of 0xf is considered a 'filler'
4617 * and will end the conversion. Similarly if odd is set the last
4618 * high nibble will be omitted. (Note that if both skip_first and
4619 * odd are true, then both the first and last semi-octet are skipped,
4620 * i.e. an even number of nibbles are considered.)
4623 tvb_get_bcd_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, int len
, const dgt_set_t
*dgt
, bool skip_first
, bool odd
, bool bigendian
)
4628 uint8_t octet
, nibble
;
4630 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
4634 * Run to the end of the captured data.
4636 * XXX - captured, or total?
4638 /*length = tvb_captured_length(tvb);*/
4646 ptr
= ensure_contiguous(tvb
, offset
, len
);
4649 * XXX - map illegal digits (digits that map to 0) to REPLACEMENT
4650 * CHARACTER, and have all the tables in epan/tvbuff.c use 0 rather
4653 digit_str
= (char *)wmem_alloc(scope
, len
*2 + 1);
4659 nibble
= (octet
>> 4) & 0x0f;
4661 nibble
= octet
& 0x0f;
4663 if (nibble
== 0x0f) {
4669 digit_str
[i
] = dgt
->out
[nibble
];
4675 * unpack second value in byte
4678 nibble
= octet
& 0x0f;
4680 nibble
= octet
>> 4;
4683 if (nibble
== 0x0f) {
4685 * This is the stop digit or a filler digit. Ignore
4690 if ((len
== 1) && (odd
== true )){
4691 /* Last octet, skip last high nibble in case of odd number of digits */
4694 digit_str
[i
] = dgt
->out
[nibble
];
4700 digit_str
[i
] = '\0';
4704 /* XXXX Fix me - needs odd indicator added */
4706 tvb_bcd_dig_to_str(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, const int len
, const dgt_set_t
*dgt
, bool skip_first
)
4711 return tvb_get_bcd_string(scope
, tvb
, offset
, len
, dgt
, skip_first
, false, false);
4715 tvb_bcd_dig_to_str_be(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const int offset
, const int len
, const dgt_set_t
*dgt
, bool skip_first
)
4720 return tvb_get_bcd_string(scope
, tvb
, offset
, len
, dgt
, skip_first
, false, true);
4724 * Format a bunch of data from a tvbuff as bytes, returning a pointer
4725 * to the string with the formatted data.
4727 char *tvb_bytes_to_str(wmem_allocator_t
*allocator
, tvbuff_t
*tvb
,
4728 const int offset
, const int len
)
4730 DISSECTOR_ASSERT(len
> 0);
4731 return bytes_to_str(allocator
, ensure_contiguous(tvb
, offset
, len
), len
);
4734 /* Find a needle tvbuff within a haystack tvbuff. */
4736 tvb_find_tvb(tvbuff_t
*haystack_tvb
, tvbuff_t
*needle_tvb
, const int haystack_offset
)
4738 unsigned haystack_abs_offset
= 0, haystack_abs_length
= 0;
4739 const uint8_t *haystack_data
;
4740 const uint8_t *needle_data
;
4741 const unsigned needle_len
= needle_tvb
->length
;
4742 const uint8_t *location
;
4744 DISSECTOR_ASSERT(haystack_tvb
&& haystack_tvb
->initialized
);
4746 if (haystack_tvb
->length
< 1 || needle_tvb
->length
< 1) {
4750 /* Get pointers to the tvbuffs' data. */
4751 haystack_data
= ensure_contiguous(haystack_tvb
, 0, -1);
4752 needle_data
= ensure_contiguous(needle_tvb
, 0, -1);
4754 check_offset_length(haystack_tvb
, haystack_offset
, -1,
4755 &haystack_abs_offset
, &haystack_abs_length
);
4757 location
= ws_memmem(haystack_data
+ haystack_abs_offset
, haystack_abs_length
,
4758 needle_data
, needle_len
);
4761 return (int) (location
- haystack_data
);
4768 tvb_raw_offset(tvbuff_t
*tvb
)
4770 return ((tvb
->raw_offset
==-1) ? (tvb
->raw_offset
= tvb_offset_from_real_beginning(tvb
)) : tvb
->raw_offset
);
4774 tvb_set_fragment(tvbuff_t
*tvb
)
4776 tvb
->flags
|= TVBUFF_FRAGMENT
;
4780 tvb_get_ds_tvb(tvbuff_t
*tvb
)
4782 return(tvb
->ds_tvb
);
4786 tvb_get_varint(tvbuff_t
*tvb
, unsigned offset
, unsigned maxlen
, uint64_t *value
, const unsigned encoding
)
4790 switch (encoding
& ENC_VARINT_MASK
) {
4791 case ENC_VARINT_PROTOBUF
:
4794 uint64_t b
; /* current byte */
4796 for (i
= 0; ((i
< FT_VARINT_MAX_LEN
) && (i
< maxlen
)); ++i
) {
4797 b
= tvb_get_uint8(tvb
, offset
++);
4798 *value
|= ((b
& 0x7F) << (i
* 7)); /* add lower 7 bits to val */
4801 /* end successfully becauseof last byte's msb(most significant bit) is zero */
4808 case ENC_VARINT_ZIGZAG
:
4811 uint64_t b
; /* current byte */
4813 for (i
= 0; ((i
< FT_VARINT_MAX_LEN
) && (i
< maxlen
)); ++i
) {
4814 b
= tvb_get_uint8(tvb
, offset
++);
4815 *value
|= ((b
& 0x7F) << (i
* 7)); /* add lower 7 bits to val */
4818 /* end successfully becauseof last byte's msb(most significant bit) is zero */
4819 *value
= (*value
>> 1) ^ ((*value
& 1) ? -1 : 0);
4826 case ENC_VARINT_SDNV
:
4828 /* Decodes similar to protobuf but in MSByte order */
4830 uint64_t b
; /* current byte */
4832 for (i
= 0; ((i
< FT_VARINT_MAX_LEN
) && (i
< maxlen
)); ++i
) {
4833 b
= tvb_get_uint8(tvb
, offset
++);
4834 if ((i
== 9) && (*value
>= UINT64_C(1)<<(64-7))) {
4835 // guaranteed overflow, not valid SDNV
4839 *value
|= (b
& 0x7F); /* add lower 7 bits to val */
4842 /* end successfully because of last byte's msb(most significant bit) is zero */
4849 case ENC_VARINT_QUIC
:
4851 /* calculate variable length */
4852 *value
= tvb_get_uint8(tvb
, offset
);
4853 switch((*value
) >> 6) {
4854 case 0: /* 0b00 => 1 byte length (6 bits Usable) */
4857 case 1: /* 0b01 => 2 bytes length (14 bits Usable) */
4858 *value
= tvb_get_ntohs(tvb
, offset
) & 0x3FFF;
4860 case 2: /* 0b10 => 4 bytes length (30 bits Usable) */
4861 *value
= tvb_get_ntohl(tvb
, offset
) & 0x3FFFFFFF;
4863 case 3: /* 0b11 => 8 bytes length (62 bits Usable) */
4864 *value
= tvb_get_ntoh64(tvb
, offset
) & UINT64_C(0x3FFFFFFFFFFFFFFF);
4866 default: /* No Possible */
4867 ws_assert_not_reached();
4874 DISSECTOR_ASSERT_NOT_REACHED();
4877 return 0; /* 10 bytes scanned, but no bytes' msb is zero */
4881 * Editor modelines - https://www.wireshark.org/tools/modelines.html
4886 * indent-tabs-mode: t
4889 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
4890 * :indentSize=8:tabSize=8:noTabs=false: