3 * Testy, Virtual(-izable) Buffer of guint8*'s
5 * "Testy" -- the buffer gets mad when an attempt to access data
6 * beyond the bounds of the buffer. An exception is thrown.
8 * "Virtual" -- the buffer can have its own data, can use a subset of
9 * the data of a backing tvbuff, or can be a composite of
14 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
16 * Code to convert IEEE floating point formats to native floating point
17 * derived from code Copyright (c) Ashok Narayanan, 2000
19 * Wireshark - Network traffic analyzer
20 * By Gerald Combs <gerald@wireshark.org>
21 * Copyright 1998 Gerald Combs
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 #include "wsutil/pint.h"
48 #include "tvbuff-int.h"
52 #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
53 #include "exceptions.h"
56 _tvb_get_bits64(tvbuff_t
*tvb
, guint bit_offset
, const gint total_no_of_bits
);
59 tvb_new(const struct tvb_ops
*ops
)
62 gsize size
= ops
->tvb_size
;
64 g_assert(size
>= sizeof(*tvb
));
66 tvb
= (tvbuff_t
*) g_slice_alloc(size
);
70 tvb
->initialized
= FALSE
;
73 tvb
->reported_length
= 0;
74 tvb
->real_data
= NULL
;
82 tvb_free_internal(tvbuff_t
*tvb
)
86 DISSECTOR_ASSERT(tvb
);
88 if (tvb
->ops
->tvb_free
)
89 tvb
->ops
->tvb_free(tvb
);
91 size
= tvb
->ops
->tvb_size
;
93 g_slice_free1(size
, tvb
);
96 /* XXX: just call tvb_free_chain();
97 * Not removed so that existing dissectors using tvb_free() need not be changed.
98 * I'd argue that existing calls to tvb_free() should have actually beeen
99 * calls to tvb_free_chain() although the calls were OK as long as no
100 * subsets, etc had been created on the tvb. */
102 tvb_free(tvbuff_t
*tvb
)
108 tvb_free_chain(tvbuff_t
*tvb
)
111 DISSECTOR_ASSERT(tvb
);
114 tvb_free_internal(tvb
);
120 tvb_new_chain(tvbuff_t
*parent
, tvbuff_t
*backing
)
122 tvbuff_t
*tvb
= tvb_new_proxy(backing
);
124 tvb_add_to_chain(parent
, tvb
);
129 tvb_add_to_chain(tvbuff_t
*parent
, tvbuff_t
*child
)
131 tvbuff_t
*tmp
= child
;
133 DISSECTOR_ASSERT(parent
);
134 DISSECTOR_ASSERT(child
);
140 tmp
->next
= parent
->next
;
146 * Check whether that offset goes more than one byte past the
149 * If not, return 0; otherwise, return exception
152 validate_offset(const tvbuff_t
*tvb
, const guint abs_offset
)
154 if (G_LIKELY(abs_offset
<= tvb
->length
))
156 else if (abs_offset
<= tvb
->reported_length
)
158 else if (tvb
->flags
& TVBUFF_FRAGMENT
)
159 return FragmentBoundsError
;
161 return ReportedBoundsError
;
165 compute_offset(const tvbuff_t
*tvb
, const gint offset
, guint
*offset_ptr
)
168 /* Positive offset - relative to the beginning of the packet. */
169 if ((guint
) offset
<= tvb
->length
) {
170 *offset_ptr
= offset
;
171 } else if ((guint
) offset
<= tvb
->reported_length
) {
173 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
174 return FragmentBoundsError
;
176 return ReportedBoundsError
;
180 /* Negative offset - relative to the end of the packet. */
181 if ((guint
) -offset
<= tvb
->length
) {
182 *offset_ptr
= tvb
->length
+ offset
;
183 } else if ((guint
) -offset
<= tvb
->reported_length
) {
185 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
186 return FragmentBoundsError
;
188 return ReportedBoundsError
;
196 compute_offset_and_remaining(const tvbuff_t
*tvb
, const gint offset
, guint
*offset_ptr
, guint
*rem_len
)
200 exception
= compute_offset(tvb
, offset
, offset_ptr
);
202 *rem_len
= tvb
->length
- *offset_ptr
;
207 /* Computes the absolute offset and length based on a possibly-negative offset
208 * and a length that is possible -1 (which means "to the end of the data").
209 * Returns integer indicating whether the offset is in bounds (0) or
210 * not (exception number). The integer ptrs are modified with the new offset and length.
211 * No exception is thrown.
213 * XXX - we return success (0), if the offset is positive and right
214 * after the end of the tvbuff (i.e., equal to the length). We do this
215 * so that a dissector constructing a subset tvbuff for the next protocol
216 * will get a zero-length tvbuff, not an exception, if there's no data
217 * left for the next protocol - we want the next protocol to be the one
218 * that gets an exception, so the error is reported as an error in that
219 * protocol rather than the containing protocol. */
221 check_offset_length_no_exception(const tvbuff_t
*tvb
,
222 const gint offset
, gint
const length_val
,
223 guint
*offset_ptr
, guint
*length_ptr
)
228 DISSECTOR_ASSERT(offset_ptr
);
229 DISSECTOR_ASSERT(length_ptr
);
231 /* Compute the offset */
232 exception
= compute_offset(tvb
, offset
, offset_ptr
);
236 if (length_val
< -1) {
237 /* XXX - ReportedBoundsError? */
241 /* Compute the length */
242 if (length_val
== -1)
243 *length_ptr
= tvb
->length
- *offset_ptr
;
245 *length_ptr
= length_val
;
248 * Compute the offset of the first byte past the length.
250 end_offset
= *offset_ptr
+ *length_ptr
;
253 * Check for an overflow
255 if (end_offset
< *offset_ptr
)
258 return validate_offset(tvb
, end_offset
);
261 /* Checks (+/-) offset and length and throws an exception if
262 * either is out of bounds. Sets integer ptrs to the new offset
265 check_offset_length(const tvbuff_t
*tvb
,
266 const gint offset
, gint
const length_val
,
267 guint
*offset_ptr
, guint
*length_ptr
)
271 exception
= check_offset_length_no_exception(tvb
, offset
, length_val
, offset_ptr
, length_ptr
);
277 tvb_check_offset_length(const tvbuff_t
*tvb
,
278 const gint offset
, gint
const length_val
,
279 guint
*offset_ptr
, guint
*length_ptr
)
281 check_offset_length(tvb
, offset
, length_val
, offset_ptr
, length_ptr
);
284 static const unsigned char left_aligned_bitmask
[] = {
296 tvb_new_octet_aligned(tvbuff_t
*tvb
, guint32 bit_offset
, gint32 no_of_bits
)
298 tvbuff_t
*sub_tvb
= NULL
;
301 guint8 left
, right
, remaining_bits
, *buf
;
304 byte_offset
= bit_offset
>> 3;
305 left
= bit_offset
% 8; /* for left-shifting */
306 right
= 8 - left
; /* for right-shifting */
308 if (no_of_bits
== -1) {
309 datalen
= tvb_length_remaining(tvb
, byte_offset
);
312 datalen
= no_of_bits
>> 3;
313 remaining_bits
= no_of_bits
% 8;
314 if (remaining_bits
) {
319 /* already aligned -> shortcut */
320 if ((left
== 0) && (remaining_bits
== 0)) {
321 return tvb_new_subset(tvb
, byte_offset
, datalen
, -1);
324 DISSECTOR_ASSERT(datalen
>0);
326 /* if at least one trailing byte is available, we must use the content
327 * of that byte for the last shift (i.e. tvb_get_ptr() must use datalen + 1
328 * if non extra byte is available, the last shifted byte requires
331 if (tvb_length_remaining(tvb
, byte_offset
) > datalen
) {
332 data
= tvb_get_ptr(tvb
, byte_offset
, datalen
+ 1);
334 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
335 buf
= (guint8
*)g_malloc(datalen
);
337 /* shift tvb data bit_offset bits to the left */
338 for (i
= 0; i
< datalen
; i
++)
339 buf
[i
] = (data
[i
] << left
) | (data
[i
+1] >> right
);
341 data
= tvb_get_ptr(tvb
, byte_offset
, datalen
);
343 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
344 buf
= (guint8
*)g_malloc(datalen
);
346 /* shift tvb data bit_offset bits to the left */
347 for (i
= 0; i
< (datalen
-1); i
++)
348 buf
[i
] = (data
[i
] << left
) | (data
[i
+1] >> right
);
349 buf
[datalen
-1] = data
[datalen
-1] << left
; /* set last octet */
351 buf
[datalen
-1] &= left_aligned_bitmask
[remaining_bits
];
353 sub_tvb
= tvb_new_child_real_data(tvb
, buf
, datalen
, datalen
);
354 tvb_set_free_cb(sub_tvb
, g_free
);
360 tvb_generic_clone_offset_len(tvbuff_t
*tvb
, guint offset
, guint len
)
362 tvbuff_t
*cloned_tvb
;
364 guint8
*data
= (guint8
*) g_malloc(len
);
366 tvb_memcpy(tvb
, data
, offset
, len
);
368 cloned_tvb
= tvb_new_real_data(data
, len
, len
);
369 tvb_set_free_cb(cloned_tvb
, g_free
);
375 tvb_clone_offset_len(tvbuff_t
*tvb
, guint offset
, guint len
)
377 if (tvb
->ops
->tvb_clone
) {
378 tvbuff_t
*cloned_tvb
;
380 cloned_tvb
= tvb
->ops
->tvb_clone(tvb
, offset
, len
);
385 return tvb_generic_clone_offset_len(tvb
, offset
, len
);
389 tvb_clone(tvbuff_t
*tvb
)
391 return tvb_clone_offset_len(tvb
, 0, tvb
->length
);
395 tvb_length(const tvbuff_t
*tvb
)
397 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
403 tvb_length_remaining(const tvbuff_t
*tvb
, const gint offset
)
405 guint abs_offset
, rem_length
;
408 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
410 exception
= compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &rem_length
);
418 tvb_ensure_length_remaining(const tvbuff_t
*tvb
, const gint offset
)
420 guint abs_offset
, rem_length
;
423 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
425 exception
= compute_offset_and_remaining(tvb
, offset
, &abs_offset
, &rem_length
);
429 if (rem_length
== 0) {
431 * This routine ensures there's at least one byte available.
432 * There aren't any bytes available, so throw the appropriate
435 if (abs_offset
>= tvb
->reported_length
) {
436 if (tvb
->flags
& TVBUFF_FRAGMENT
) {
437 THROW(FragmentBoundsError
);
439 THROW(ReportedBoundsError
);
450 /* Validates that 'length' bytes are available starting from
451 * offset (pos/neg). Does not throw an exception. */
453 tvb_bytes_exist(const tvbuff_t
*tvb
, const gint offset
, const gint length
)
455 guint abs_offset
, abs_length
;
458 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
460 exception
= check_offset_length_no_exception(tvb
, offset
, length
, &abs_offset
, &abs_length
);
467 /* Validates that 'length' bytes are available starting from
468 * offset (pos/neg). Throws an exception if they aren't. */
470 tvb_ensure_bytes_exist(const tvbuff_t
*tvb
, const gint offset
, const gint length
)
472 guint real_offset
, end_offset
;
474 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
477 * -1 doesn't mean "until end of buffer", as that's pointless
478 * for this routine. We must treat it as a Really Large Positive
479 * Number, so that we throw an exception; we throw
480 * ReportedBoundsError, as if it were past even the end of a
481 * reassembled packet, and past the end of even the data we
484 * We do the same with other negative lengths.
487 THROW(ReportedBoundsError
);
490 /* XXX: Below this point could be replaced with a call to
491 * check_offset_length with no functional change, however this is a
492 * *very* hot path and check_offset_length is not well-optimized for
493 * this case, so we eat some code duplication for a lot of speedup. */
496 /* Positive offset - relative to the beginning of the packet. */
497 if ((guint
) offset
<= tvb
->length
) {
498 real_offset
= offset
;
499 } else if ((guint
) offset
<= tvb
->reported_length
) {
501 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
502 THROW(FragmentBoundsError
);
504 THROW(ReportedBoundsError
);
508 /* Negative offset - relative to the end of the packet. */
509 if ((guint
) -offset
<= tvb
->length
) {
510 real_offset
= tvb
->length
+ offset
;
511 } else if ((guint
) -offset
<= tvb
->reported_length
) {
513 } else if (tvb
->flags
& TVBUFF_FRAGMENT
) {
514 THROW(FragmentBoundsError
);
516 THROW(ReportedBoundsError
);
521 * Compute the offset of the first byte past the length.
523 end_offset
= real_offset
+ length
;
526 * Check for an overflow
528 if (end_offset
< real_offset
)
531 if (G_LIKELY(end_offset
<= tvb
->length
))
533 else if (end_offset
<= tvb
->reported_length
)
535 else if (tvb
->flags
& TVBUFF_FRAGMENT
)
536 THROW(FragmentBoundsError
);
538 THROW(ReportedBoundsError
);
542 tvb_offset_exists(const tvbuff_t
*tvb
, const gint offset
)
547 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
549 exception
= compute_offset(tvb
, offset
, &abs_offset
);
553 /* compute_offset only throws an exception on >, not >= because of the
554 * comment above check_offset_length_no_exception, but here we want the
555 * opposite behaviour so we check ourselves... */
556 if (abs_offset
< tvb
->length
) {
565 tvb_reported_length(const tvbuff_t
*tvb
)
567 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
569 return tvb
->reported_length
;
573 tvb_reported_length_remaining(const tvbuff_t
*tvb
, const gint offset
)
578 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
580 exception
= compute_offset(tvb
, offset
, &abs_offset
);
584 if (tvb
->reported_length
>= abs_offset
)
585 return tvb
->reported_length
- abs_offset
;
590 /* Set the reported length of a tvbuff to a given value; used for protocols
591 * whose headers contain an explicit length and where the calling
592 * dissector's payload may include padding as well as the packet for
594 * Also adjusts the data length. */
596 tvb_set_reported_length(tvbuff_t
*tvb
, const guint reported_length
)
598 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
600 if (reported_length
> tvb
->reported_length
)
601 THROW(ReportedBoundsError
);
603 tvb
->reported_length
= reported_length
;
604 if (reported_length
< tvb
->length
)
605 tvb
->length
= reported_length
;
609 tvb_offset_from_real_beginning_counter(const tvbuff_t
*tvb
, const guint counter
)
611 if (tvb
->ops
->tvb_offset
)
612 return tvb
->ops
->tvb_offset(tvb
, counter
);
614 DISSECTOR_ASSERT_NOT_REACHED();
619 tvb_offset_from_real_beginning(const tvbuff_t
*tvb
)
621 return tvb_offset_from_real_beginning_counter(tvb
, 0);
625 ensure_contiguous_no_exception(tvbuff_t
*tvb
, const gint offset
, const gint length
, int *pexception
)
627 guint abs_offset
, abs_length
;
630 exception
= check_offset_length_no_exception(tvb
, offset
, length
, &abs_offset
, &abs_length
);
633 *pexception
= exception
;
638 * We know that all the data is present in the tvbuff, so
639 * no exceptions should be thrown.
642 return tvb
->real_data
+ abs_offset
;
644 if (tvb
->ops
->tvb_get_ptr
)
645 return tvb
->ops
->tvb_get_ptr(tvb
, abs_offset
, abs_length
);
647 DISSECTOR_ASSERT_NOT_REACHED();
652 ensure_contiguous(tvbuff_t
*tvb
, const gint offset
, const gint length
)
657 p
= ensure_contiguous_no_exception(tvb
, offset
, length
, &exception
);
659 DISSECTOR_ASSERT(exception
> 0);
666 fast_ensure_contiguous(tvbuff_t
*tvb
, const gint offset
, const guint length
)
671 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
672 /* We don't check for overflow in this fast path so we only handle simple types */
673 DISSECTOR_ASSERT(length
<= 8);
675 if (offset
< 0 || !tvb
->real_data
) {
676 return ensure_contiguous(tvb
, offset
, length
);
680 end_offset
= u_offset
+ length
;
682 if (end_offset
<= tvb
->length
) {
683 return tvb
->real_data
+ u_offset
;
686 if (end_offset
> tvb
->reported_length
) {
687 if (tvb
->flags
& TVBUFF_FRAGMENT
) {
688 THROW(FragmentBoundsError
);
690 THROW(ReportedBoundsError
);
700 guint8_pbrk(const guint8
* haystack
, size_t haystacklen
, const guint8
*needles
, guchar
*found_needle
)
702 gchar tmp
[256] = { 0 };
703 const guint8
*haystack_end
;
708 haystack_end
= haystack
+ haystacklen
;
709 while (haystack
< haystack_end
) {
710 if (tmp
[*haystack
]) {
712 *found_needle
= *haystack
;
723 /************** ACCESSORS **************/
726 tvb_memcpy(tvbuff_t
*tvb
, void *target
, const gint offset
, size_t length
)
728 guint abs_offset
, abs_length
;
730 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
733 * XXX - we should eliminate the "length = -1 means 'to the end
734 * of the tvbuff'" convention, and use other means to achieve
735 * that; this would let us eliminate a bunch of checks for
736 * negative lengths in cases where the protocol has a 32-bit
739 * Allowing -1 but throwing an assertion on other negative
740 * lengths is a bit more work with the length being a size_t;
741 * instead, we check for a length <= 2^31-1.
743 DISSECTOR_ASSERT(length
<= 0x7FFFFFFF);
744 check_offset_length(tvb
, offset
, (gint
) length
, &abs_offset
, &abs_length
);
746 if (tvb
->real_data
) {
747 return memcpy(target
, tvb
->real_data
+ abs_offset
, abs_length
);
750 if (tvb
->ops
->tvb_memcpy
)
751 return tvb
->ops
->tvb_memcpy(tvb
, target
, abs_offset
, abs_length
);
753 /* XXX, fallback to slower method */
755 DISSECTOR_ASSERT_NOT_REACHED();
761 * XXX - this doesn't treat a length of -1 as an error.
762 * If it did, this could replace some code that calls
763 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
766 * "composite_get_ptr()" depends on -1 not being
767 * an error; does anything else depend on this routine treating -1 as
768 * meaning "to the end of the buffer"?
770 * If scope is NULL, memory is allocated with g_malloc() and user must
771 * explicitely free it with g_free().
772 * If scope is not NULL, memory is allocated with the corresponding pool
776 tvb_memdup(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const gint offset
, size_t length
)
778 guint abs_offset
, abs_length
;
781 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
783 check_offset_length(tvb
, offset
, (gint
) length
, &abs_offset
, &abs_length
);
785 duped
= wmem_alloc(scope
, abs_length
);
786 return tvb_memcpy(tvb
, duped
, abs_offset
, abs_length
);
792 tvb_get_ptr(tvbuff_t
*tvb
, const gint offset
, const gint length
)
794 return ensure_contiguous(tvb
, offset
, length
);
797 /* ---------------- */
799 tvb_get_guint8(tvbuff_t
*tvb
, const gint offset
)
803 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(guint8
));
808 tvb_get_ntohs(tvbuff_t
*tvb
, const gint offset
)
812 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(guint16
));
817 tvb_get_ntoh24(tvbuff_t
*tvb
, const gint offset
)
821 ptr
= fast_ensure_contiguous(tvb
, offset
, 3);
826 tvb_get_ntohl(tvbuff_t
*tvb
, const gint offset
)
830 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(guint32
));
835 tvb_get_ntoh40(tvbuff_t
*tvb
, const gint offset
)
839 ptr
= fast_ensure_contiguous(tvb
, offset
, 5);
844 tvb_get_ntoh48(tvbuff_t
*tvb
, const gint offset
)
848 ptr
= fast_ensure_contiguous(tvb
, offset
, 6);
853 tvb_get_ntoh56(tvbuff_t
*tvb
, const gint offset
)
857 ptr
= fast_ensure_contiguous(tvb
, offset
, 7);
862 tvb_get_ntoh64(tvbuff_t
*tvb
, const gint offset
)
866 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(guint64
));
871 * Stuff for IEEE float handling on platforms that don't have IEEE
872 * format as the native floating-point format.
874 * For now, we treat only the VAX as such a platform.
876 * XXX - other non-IEEE boxes that can run UNIX include some Crays,
877 * and possibly other machines.
879 * It appears that the official Linux port to System/390 and
880 * zArchitecture uses IEEE format floating point (not a
883 * I don't know whether there are any other machines that
884 * could run Wireshark and that don't use IEEE format.
885 * As far as I know, all of the main commercial microprocessor
886 * families on which OSes that support Wireshark can run
887 * use IEEE format (x86, 68k, SPARC, MIPS, PA-RISC, Alpha,
898 #define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
899 #define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
900 #define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
902 #define IEEE_SP_SIGN_MASK 0x80000000
903 #define IEEE_SP_EXPONENT_MASK 0x7F800000
904 #define IEEE_SP_MANTISSA_MASK 0x007FFFFF
905 #define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
907 #define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
908 #define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
909 #define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
912 ieee_float_is_zero(const guint32 w
)
914 return ((w
& ~IEEE_SP_SIGN_MASK
) == 0);
918 get_ieee_float(const guint32 w
)
924 sign
= w
& IEEE_SP_SIGN_MASK
;
925 exponent
= w
& IEEE_SP_EXPONENT_MASK
;
926 mantissa
= w
& IEEE_SP_MANTISSA_MASK
;
928 if (ieee_float_is_zero(w
)) {
929 /* number is zero, unnormalized, or not-a-number */
934 * XXX - how to handle this?
936 if (IEEE_SP_INFINITY
== exponent
) {
938 * number is positive or negative infinity, or a special value
940 return (sign
? MINUS_INFINITY
: PLUS_INFINITY
);
944 exponent
= ((exponent
>> IEEE_SP_MANTISSA_WIDTH
) - IEEE_SP_BIAS
) -
945 IEEE_SP_MANTISSA_WIDTH
;
946 mantissa
|= IEEE_SP_IMPLIED_BIT
;
949 return -mantissa
* pow(2, exponent
);
951 return mantissa
* pow(2, exponent
);
956 * We assume that if you don't have IEEE floating-point, you have a
957 * compiler that understands 64-bit integral quantities.
959 #define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
960 #define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
961 #define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
963 #define IEEE_DP_SIGN_MASK 0x8000000000000000LL
964 #define IEEE_DP_EXPONENT_MASK 0x7FF0000000000000LL
965 #define IEEE_DP_MANTISSA_MASK 0x000FFFFFFFFFFFFFLL
966 #define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
968 #define IEEE_DP_IMPLIED_BIT (1LL << IEEE_DP_MANTISSA_WIDTH)
969 #define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
970 #define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
973 ieee_double_is_zero(const guint64 w
)
975 return ((w
& ~IEEE_SP_SIGN_MASK
) == 0);
979 get_ieee_double(const guint64 w
)
985 sign
= w
& IEEE_DP_SIGN_MASK
;
986 exponent
= w
& IEEE_DP_EXPONENT_MASK
;
987 mantissa
= w
& IEEE_DP_MANTISSA_MASK
;
989 if (ieee_double_is_zero(w
)) {
990 /* number is zero, unnormalized, or not-a-number */
995 * XXX - how to handle this?
997 if (IEEE_DP_INFINITY
== exponent
) {
999 * number is positive or negative infinity, or a special value
1001 return (sign
? MINUS_INFINITY
: PLUS_INFINITY
);
1005 exponent
= ((exponent
>> IEEE_DP_MANTISSA_WIDTH
) - IEEE_DP_BIAS
) -
1006 IEEE_DP_MANTISSA_WIDTH
;
1007 mantissa
|= IEEE_DP_IMPLIED_BIT
;
1010 return -mantissa
* pow(2, exponent
);
1012 return mantissa
* pow(2, exponent
);
1017 * Fetches an IEEE single-precision floating-point number, in
1018 * big-endian form, and returns a "float".
1020 * XXX - should this be "double", in case there are IEEE single-
1021 * precision numbers that won't fit in some platform's native
1025 tvb_get_ntohieee_float(tvbuff_t
*tvb
, const int offset
)
1028 return get_ieee_float(tvb_get_ntohl(tvb
, offset
));
1035 ieee_fp_union
.w
= tvb_get_ntohl(tvb
, offset
);
1036 return ieee_fp_union
.f
;
1041 * Fetches an IEEE double-precision floating-point number, in
1042 * big-endian form, and returns a "double".
1045 tvb_get_ntohieee_double(tvbuff_t
*tvb
, const int offset
)
1059 #ifdef WORDS_BIGENDIAN
1060 ieee_fp_union
.w
[0] = tvb_get_ntohl(tvb
, offset
);
1061 ieee_fp_union
.w
[1] = tvb_get_ntohl(tvb
, offset
+4);
1063 ieee_fp_union
.w
[0] = tvb_get_ntohl(tvb
, offset
+4);
1064 ieee_fp_union
.w
[1] = tvb_get_ntohl(tvb
, offset
);
1067 return get_ieee_double(ieee_fp_union
.dw
);
1069 return ieee_fp_union
.d
;
1074 tvb_get_letohs(tvbuff_t
*tvb
, const gint offset
)
1078 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(guint16
));
1079 return pletohs(ptr
);
1083 tvb_get_letoh24(tvbuff_t
*tvb
, const gint offset
)
1087 ptr
= fast_ensure_contiguous(tvb
, offset
, 3);
1088 return pletoh24(ptr
);
1092 tvb_get_letohl(tvbuff_t
*tvb
, const gint offset
)
1096 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(guint32
));
1097 return pletohl(ptr
);
1101 tvb_get_letoh40(tvbuff_t
*tvb
, const gint offset
)
1105 ptr
= fast_ensure_contiguous(tvb
, offset
, 5);
1106 return pletoh40(ptr
);
1110 tvb_get_letoh48(tvbuff_t
*tvb
, const gint offset
)
1114 ptr
= fast_ensure_contiguous(tvb
, offset
, 6);
1115 return pletoh48(ptr
);
1119 tvb_get_letoh56(tvbuff_t
*tvb
, const gint offset
)
1123 ptr
= fast_ensure_contiguous(tvb
, offset
, 7);
1124 return pletoh56(ptr
);
1128 tvb_get_letoh64(tvbuff_t
*tvb
, const gint offset
)
1132 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(guint64
));
1133 return pletoh64(ptr
);
1137 * Fetches an IEEE single-precision floating-point number, in
1138 * little-endian form, and returns a "float".
1140 * XXX - should this be "double", in case there are IEEE single-
1141 * precision numbers that won't fit in some platform's native
1145 tvb_get_letohieee_float(tvbuff_t
*tvb
, const int offset
)
1148 return get_ieee_float(tvb_get_letohl(tvb
, offset
));
1155 ieee_fp_union
.w
= tvb_get_letohl(tvb
, offset
);
1156 return ieee_fp_union
.f
;
1161 * Fetches an IEEE double-precision floating-point number, in
1162 * little-endian form, and returns a "double".
1165 tvb_get_letohieee_double(tvbuff_t
*tvb
, const int offset
)
1179 #ifdef WORDS_BIGENDIAN
1180 ieee_fp_union
.w
[0] = tvb_get_letohl(tvb
, offset
+4);
1181 ieee_fp_union
.w
[1] = tvb_get_letohl(tvb
, offset
);
1183 ieee_fp_union
.w
[0] = tvb_get_letohl(tvb
, offset
);
1184 ieee_fp_union
.w
[1] = tvb_get_letohl(tvb
, offset
+4);
1187 return get_ieee_double(ieee_fp_union
.dw
);
1189 return ieee_fp_union
.d
;
1193 /* Fetch an IPv4 address, in network byte order.
1194 * We do *not* convert them to host byte order; we leave them in
1195 * network byte order. */
1197 tvb_get_ipv4(tvbuff_t
*tvb
, const gint offset
)
1202 ptr
= fast_ensure_contiguous(tvb
, offset
, sizeof(guint32
));
1203 memcpy(&addr
, ptr
, sizeof addr
);
1207 /* Fetch an IPv6 address. */
1209 tvb_get_ipv6(tvbuff_t
*tvb
, const gint offset
, struct e_in6_addr
*addr
)
1213 ptr
= ensure_contiguous(tvb
, offset
, sizeof(*addr
));
1214 memcpy(addr
, ptr
, sizeof *addr
);
1219 tvb_get_ntohguid(tvbuff_t
*tvb
, const gint offset
, e_guid_t
*guid
)
1221 const guint8
*ptr
= ensure_contiguous(tvb
, offset
, GUID_LEN
);
1223 guid
->data1
= pntohl(ptr
+ 0);
1224 guid
->data2
= pntohs(ptr
+ 4);
1225 guid
->data3
= pntohs(ptr
+ 6);
1226 memcpy(guid
->data4
, ptr
+ 8, sizeof guid
->data4
);
1230 tvb_get_letohguid(tvbuff_t
*tvb
, const gint offset
, e_guid_t
*guid
)
1232 const guint8
*ptr
= ensure_contiguous(tvb
, offset
, GUID_LEN
);
1234 guid
->data1
= pletohl(ptr
+ 0);
1235 guid
->data2
= pletohs(ptr
+ 4);
1236 guid
->data3
= pletohs(ptr
+ 6);
1237 memcpy(guid
->data4
, ptr
+ 8, sizeof guid
->data4
);
1241 * NOTE: to support code written when proto_tree_add_item() took a
1242 * gboolean as its last argument, with FALSE meaning "big-endian"
1243 * and TRUE meaning "little-endian", we treat any non-zero value of
1244 * "representation" as meaning "little-endian".
1247 tvb_get_guid(tvbuff_t
*tvb
, const gint offset
, e_guid_t
*guid
, const guint representation
)
1249 if (representation
) {
1250 tvb_get_letohguid(tvb
, offset
, guid
);
1252 tvb_get_ntohguid(tvb
, offset
, guid
);
1256 static const guint8 inverse_bit_mask8
[] = {
1267 static const guint8 bit_mask8
[] = {
1279 /* Get 1 - 8 bits */
1281 tvb_get_bits8(tvbuff_t
*tvb
, guint bit_offset
, const gint no_of_bits
)
1283 return (guint8
)_tvb_get_bits64(tvb
, bit_offset
, no_of_bits
);
1286 /* Get 9 - 16 bits */
1288 tvb_get_bits16(tvbuff_t
*tvb
, guint bit_offset
, const gint no_of_bits
,const guint encoding _U_
)
1290 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1291 return (guint16
)_tvb_get_bits64(tvb
, bit_offset
, no_of_bits
);
1294 /* Get 1 - 32 bits */
1296 tvb_get_bits32(tvbuff_t
*tvb
, guint bit_offset
, const gint no_of_bits
, const guint encoding _U_
)
1298 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1299 return (guint32
)_tvb_get_bits64(tvb
, bit_offset
, no_of_bits
);
1302 /* Get 1 - 64 bits */
1304 tvb_get_bits64(tvbuff_t
*tvb
, guint bit_offset
, const gint no_of_bits
, const guint encoding _U_
)
1306 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1307 return _tvb_get_bits64(tvb
, bit_offset
, no_of_bits
);
1310 * This function will dissect a sequence of bits that does not need to be byte aligned; the bits
1311 * set will be shown in the tree as ..10 10.. and the integer value returned if return_value is set.
1312 * Offset should be given in bits from the start of the tvb.
1313 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
1316 _tvb_get_bits64(tvbuff_t
*tvb
, guint bit_offset
, const gint total_no_of_bits
)
1319 guint octet_offset
= bit_offset
>> 3;
1320 guint8 required_bits_in_first_octet
= 8 - (bit_offset
% 8);
1322 if(required_bits_in_first_octet
> total_no_of_bits
)
1324 /* the required bits don't extend to the end of the first octet */
1325 guint8 right_shift
= required_bits_in_first_octet
- total_no_of_bits
;
1326 value
= (tvb_get_guint8(tvb
, octet_offset
) >> right_shift
) & bit_mask8
[total_no_of_bits
% 8];
1330 guint8 remaining_bit_length
= total_no_of_bits
;
1332 /* get the bits up to the first octet boundary */
1334 required_bits_in_first_octet
%= 8;
1335 if(required_bits_in_first_octet
!= 0)
1337 value
= tvb_get_guint8(tvb
, octet_offset
) & bit_mask8
[required_bits_in_first_octet
];
1338 remaining_bit_length
-= required_bits_in_first_octet
;
1341 /* take the biggest words, shorts or octets that we can */
1342 while (remaining_bit_length
> 7)
1344 switch (remaining_bit_length
>> 4)
1347 /* 8 - 15 bits. (note that 0 - 7 would have dropped out of the while() loop) */
1349 value
+= tvb_get_guint8(tvb
, octet_offset
);
1350 remaining_bit_length
-= 8;
1357 value
+= tvb_get_ntohs(tvb
, octet_offset
);
1358 remaining_bit_length
-= 16;
1366 value
+= tvb_get_ntohl(tvb
, octet_offset
);
1367 remaining_bit_length
-= 32;
1372 /* 64 bits (or more???) */
1373 value
= tvb_get_ntoh64(tvb
, octet_offset
);
1374 remaining_bit_length
-= 64;
1379 /* get bits from any partial octet at the tail */
1380 if(remaining_bit_length
)
1382 value
<<= remaining_bit_length
;
1383 value
+= (tvb_get_guint8(tvb
, octet_offset
) >> (8 - remaining_bit_length
));
1388 /* Get 1 - 32 bits (should be deprecated as same as tvb_get_bits32??) */
1390 tvb_get_bits(tvbuff_t
*tvb
, const guint bit_offset
, const gint no_of_bits
, const guint encoding _U_
)
1392 /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
1393 return (guint32
)_tvb_get_bits64(tvb
, bit_offset
, no_of_bits
);
1397 tvb_find_guint8_generic(tvbuff_t
*tvb
, guint abs_offset
, guint limit
, guint8 needle
)
1400 const guint8
*result
;
1402 ptr
= tvb_get_ptr(tvb
, abs_offset
, limit
);
1404 result
= (const guint8
*) memchr(ptr
, needle
, limit
);
1408 return (gint
) ((result
- ptr
) + abs_offset
);
1411 /* Find first occurrence of needle in tvbuff, starting at offset. Searches
1412 * at most maxlength number of bytes; if maxlength is -1, searches to
1414 * Returns the offset of the found needle, or -1 if not found.
1415 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1416 * in that case, -1 will be returned if the boundary is reached before
1417 * finding needle. */
1419 tvb_find_guint8(tvbuff_t
*tvb
, const gint offset
, const gint maxlength
, const guint8 needle
)
1421 const guint8
*result
;
1426 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
1428 check_offset_length(tvb
, offset
, -1, &abs_offset
, &tvbufflen
);
1430 /* Only search to end of tvbuff, w/o throwing exception. */
1431 if (maxlength
== -1) {
1432 /* No maximum length specified; search to end of tvbuff. */
1435 else if (tvbufflen
< (guint
) maxlength
) {
1436 /* Maximum length goes past end of tvbuff; search to end
1441 /* Maximum length doesn't go past end of tvbuff; search
1446 /* If we have real data, perform our search now. */
1447 if (tvb
->real_data
) {
1448 result
= (const guint8
*)memchr(tvb
->real_data
+ abs_offset
, needle
, limit
);
1449 if (result
== NULL
) {
1453 return (gint
) (result
- tvb
->real_data
);
1457 if (tvb
->ops
->tvb_find_guint8
)
1458 return tvb
->ops
->tvb_find_guint8(tvb
, abs_offset
, limit
, needle
);
1460 return tvb_find_guint8_generic(tvb
, offset
, limit
, needle
);
1464 tvb_pbrk_guint8_generic(tvbuff_t
*tvb
, guint abs_offset
, guint limit
, const guint8
*needles
, guchar
*found_needle
)
1467 const guint8
*result
;
1469 ptr
= tvb_get_ptr(tvb
, abs_offset
, limit
);
1471 result
= guint8_pbrk(ptr
, limit
, needles
, found_needle
);
1475 return (gint
) ((result
- ptr
) + abs_offset
);
1478 /* Find first occurrence of any of the needles in tvbuff, starting at offset.
1479 * Searches at most maxlength number of bytes; if maxlength is -1, searches
1481 * Returns the offset of the found needle, or -1 if not found.
1482 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1483 * in that case, -1 will be returned if the boundary is reached before
1484 * finding needle. */
1486 tvb_pbrk_guint8(tvbuff_t
*tvb
, const gint offset
, const gint maxlength
, const guint8
*needles
, guchar
*found_needle
)
1488 const guint8
*result
;
1493 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
1495 check_offset_length(tvb
, offset
, -1, &abs_offset
, &tvbufflen
);
1497 /* Only search to end of tvbuff, w/o throwing exception. */
1498 if (maxlength
== -1) {
1499 /* No maximum length specified; search to end of tvbuff. */
1502 else if (tvbufflen
< (guint
) maxlength
) {
1503 /* Maximum length goes past end of tvbuff; search to end
1508 /* Maximum length doesn't go past end of tvbuff; search
1513 /* If we have real data, perform our search now. */
1514 if (tvb
->real_data
) {
1515 result
= guint8_pbrk(tvb
->real_data
+ abs_offset
, limit
, needles
, found_needle
);
1516 if (result
== NULL
) {
1520 return (gint
) (result
- tvb
->real_data
);
1524 if (tvb
->ops
->tvb_pbrk_guint8
)
1525 return tvb
->ops
->tvb_pbrk_guint8(tvb
, abs_offset
, limit
, needles
, found_needle
);
1527 return tvb_pbrk_guint8_generic(tvb
, abs_offset
, limit
, needles
, found_needle
);
1530 /* Find size of stringz (NUL-terminated string) by looking for terminating
1531 * NUL. The size of the string includes the terminating NUL.
1533 * If the NUL isn't found, it throws the appropriate exception.
1536 tvb_strsize(tvbuff_t
*tvb
, const gint offset
)
1538 guint abs_offset
, junk_length
;
1541 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
1543 check_offset_length(tvb
, offset
, 0, &abs_offset
, &junk_length
);
1544 nul_offset
= tvb_find_guint8(tvb
, abs_offset
, -1, 0);
1545 if (nul_offset
== -1) {
1547 * OK, we hit the end of the tvbuff, so we should throw
1550 * Did we hit the end of the captured data, or the end
1551 * of the actual data? If there's less captured data
1552 * than actual data, we presumably hit the end of the
1553 * captured data, otherwise we hit the end of the actual
1556 if (tvb
->length
< tvb
->reported_length
) {
1559 if (tvb
->flags
& TVBUFF_FRAGMENT
) {
1560 THROW(FragmentBoundsError
);
1562 THROW(ReportedBoundsError
);
1566 return (nul_offset
- abs_offset
) + 1;
1569 /* UTF-16/UCS-2 version of tvb_strsize */
1570 /* Returns number of bytes including the (two-bytes) null terminator */
1572 tvb_unicode_strsize(tvbuff_t
*tvb
, const gint offset
)
1577 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
1580 /* Endianness doesn't matter when looking for null */
1581 uchar
= tvb_get_ntohs(tvb
, offset
+ i
);
1583 } while(uchar
!= 0);
1588 /* Find length of string by looking for end of string ('\0'), up to
1589 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
1591 * Returns -1 if 'maxlength' reached before finding EOS. */
1593 tvb_strnlen(tvbuff_t
*tvb
, const gint offset
, const guint maxlength
)
1596 guint abs_offset
, junk_length
;
1598 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
1600 check_offset_length(tvb
, offset
, 0, &abs_offset
, &junk_length
);
1602 result_offset
= tvb_find_guint8(tvb
, abs_offset
, maxlength
, 0);
1604 if (result_offset
== -1) {
1608 return result_offset
- abs_offset
;
1613 * Implement strneql etc
1617 * Call strncmp after checking if enough chars left, returning 0 if
1618 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1621 tvb_strneql(tvbuff_t
*tvb
, const gint offset
, const gchar
*str
, const size_t size
)
1625 ptr
= ensure_contiguous_no_exception(tvb
, offset
, (gint
)size
, NULL
);
1628 int cmp
= strncmp((const char *)ptr
, str
, size
);
1631 * Return 0 if equal, -1 otherwise.
1633 return (cmp
== 0 ? 0 : -1);
1636 * Not enough characters in the tvbuff to match the
1644 * Call g_ascii_strncasecmp after checking if enough chars left, returning
1645 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1648 tvb_strncaseeql(tvbuff_t
*tvb
, const gint offset
, const gchar
*str
, const size_t size
)
1652 ptr
= ensure_contiguous_no_exception(tvb
, offset
, (gint
)size
, NULL
);
1655 int cmp
= g_ascii_strncasecmp((const char *)ptr
, str
, size
);
1658 * Return 0 if equal, -1 otherwise.
1660 return (cmp
== 0 ? 0 : -1);
1663 * Not enough characters in the tvbuff to match the
1671 * Call memcmp after checking if enough chars left, returning 0 if
1672 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1675 tvb_memeql(tvbuff_t
*tvb
, const gint offset
, const guint8
*str
, size_t size
)
1679 ptr
= ensure_contiguous_no_exception(tvb
, offset
, (gint
) size
, NULL
);
1682 int cmp
= memcmp(ptr
, str
, size
);
1685 * Return 0 if equal, -1 otherwise.
1687 return (cmp
== 0 ? 0 : -1);
1690 * Not enough characters in the tvbuff to match the
1697 /* Convert a string from Unicode to ASCII. At the moment we fake it by
1698 * replacing all non-ASCII characters with a '.' )-: The len parameter is
1699 * the number of guint16's to convert from Unicode.
1701 * If scope is set to NULL, returned buffer is allocated by g_malloc()
1702 * and must be g_free by the caller. Otherwise memory is automatically
1703 * freed when the scope lifetime is reached.
1705 /* XXX: This has been replaced by tvb_get_string() */
1707 tvb_get_faked_unicode(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, int offset
,
1708 const int len
, const gboolean little_endian
)
1714 /* Make sure we have enough data before allocating the buffer,
1715 so we don't blow up if the length is huge. */
1716 tvb_ensure_bytes_exist(tvb
, offset
, 2*len
);
1718 /* We know we won't throw an exception, so we don't have to worry
1719 about leaking this buffer. */
1720 buffer
= (char *)wmem_alloc(scope
, len
+ 1);
1722 for (i
= 0; i
< len
; i
++) {
1723 character
= little_endian
? tvb_get_letohs(tvb
, offset
)
1724 : tvb_get_ntohs(tvb
, offset
);
1725 buffer
[i
] = character
< 256 ? character
: '.';
1735 * Format the data in the tvb from offset for length ...
1738 tvb_format_text(tvbuff_t
*tvb
, const gint offset
, const gint size
)
1743 len
= (size
> 0) ? size
: 0;
1745 ptr
= ensure_contiguous(tvb
, offset
, size
);
1746 return format_text(ptr
, len
);
1750 * Format the data in the tvb from offset for length ...
1753 tvb_format_text_wsp(tvbuff_t
*tvb
, const gint offset
, const gint size
)
1758 len
= (size
> 0) ? size
: 0;
1760 ptr
= ensure_contiguous(tvb
, offset
, size
);
1761 return format_text_wsp(ptr
, len
);
1765 * Like "tvb_format_text()", but for null-padded strings; don't show
1766 * the null padding characters as "\000".
1769 tvb_format_stringzpad(tvbuff_t
*tvb
, const gint offset
, const gint size
)
1771 const guint8
*ptr
, *p
;
1775 len
= (size
> 0) ? size
: 0;
1777 ptr
= ensure_contiguous(tvb
, offset
, size
);
1778 for (p
= ptr
, stringlen
= 0; stringlen
< len
&& *p
!= '\0'; p
++, stringlen
++)
1780 return format_text(ptr
, stringlen
);
1784 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
1785 * the null padding characters as "\000".
1788 tvb_format_stringzpad_wsp(tvbuff_t
*tvb
, const gint offset
, const gint size
)
1790 const guint8
*ptr
, *p
;
1794 len
= (size
> 0) ? size
: 0;
1796 ptr
= ensure_contiguous(tvb
, offset
, size
);
1797 for (p
= ptr
, stringlen
= 0; stringlen
< len
&& *p
!= '\0'; p
++, stringlen
++)
1799 return format_text_wsp(ptr
, stringlen
);
1803 * Given a tvbuff, an offset, and a length, allocate a buffer big enough
1804 * to hold a non-null-terminated string of that length at that offset,
1805 * plus a trailing '\0', copy the string into it, and return a pointer
1807 * If scope is NULL, memory is allocated with g_malloc() and user must
1808 * explicitely free it with g_free().
1809 * If scope is not NULL, memory is allocated with the corresponding pool
1811 * Throws an exception if the tvbuff ends before the string does.
1814 tvb_get_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const gint offset
, const gint length
)
1818 tvb_ensure_bytes_exist(tvb
, offset
, length
); /* make sure length = -1 fails */
1819 strbuf
= (guint8
*)wmem_alloc(scope
, length
+ 1);
1820 tvb_memcpy(tvb
, strbuf
, offset
, length
);
1821 strbuf
[length
] = '\0';
1826 * Unicode (UTF-16) version of tvb_get_string()
1827 * XXX - this is UCS-2, not UTF-16, as it doesn't handle surrogate pairs
1829 * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
1831 * Specify length in bytes
1833 * If scope is NULL, memory is allocated with g_malloc() and user must
1834 * explicitely free it with g_free().
1835 * If scope is not NULL, memory is allocated with the corresponding pool
1838 * Returns an UTF-8 string
1841 tvb_get_unicode_string(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const gint offset
, gint length
, const guint encoding
)
1844 gint i
; /* Byte counter for tvbuff */
1845 wmem_strbuf_t
*strbuf
;
1847 tvb_ensure_bytes_exist(tvb
, offset
, length
);
1849 strbuf
= wmem_strbuf_new(scope
, NULL
);
1851 for(i
= 0; i
< length
; i
+= 2) {
1852 if (encoding
== ENC_BIG_ENDIAN
)
1853 uchar
= tvb_get_ntohs(tvb
, offset
+ i
);
1855 uchar
= tvb_get_letohs(tvb
, offset
+ i
);
1857 wmem_strbuf_append_unichar(strbuf
, uchar
);
1860 return (gchar
*)wmem_strbuf_get_str(strbuf
);
1864 * Given a tvbuff, an offset, a length, and an encoding, allocate a
1865 * buffer big enough to hold a non-null-terminated string of that length
1866 * at that offset, plus a trailing '\0', copy into the buffer the
1867 * string as converted from the appropriate encoding to UTF-8, and
1868 * return a pointer to the string.
1870 * Throws an exception if the tvbuff ends before the string does.
1872 * If scope is NULL, memory is allocated with g_malloc() and user must
1873 * explicitely free it with g_free().
1874 * If scope is not NULL, memory is allocated with the corresponding pool
1878 tvb_get_string_enc(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const gint offset
,
1879 const gint length
, const guint encoding
)
1884 switch (encoding
& ENC_CHARENCODING_MASK
) {
1889 * For now, we treat bogus values as meaning
1890 * "ASCII" rather than reporting an error,
1891 * for the benefit of old dissectors written
1892 * when the last argument to proto_tree_add_item()
1893 * was a gboolean for the byte order, not an
1894 * encoding value, and passed non-zero values
1895 * other than TRUE to mean "little-endian".
1897 * XXX - should map all octets with the 8th bit
1898 * not set to a "substitute" UTF-8 character.
1900 strbuf
= tvb_get_string(scope
, tvb
, offset
, length
);
1905 * XXX - should map all invalid UTF-8 sequences
1906 * to a "substitute" UTF-8 character.
1908 strbuf
= tvb_get_string(scope
, tvb
, offset
, length
);
1913 * XXX - needs to handle surrogate pairs and to map
1914 * invalid characters and sequences to a "substitute"
1917 strbuf
= tvb_get_unicode_string(scope
, tvb
, offset
, length
,
1918 encoding
& ENC_LITTLE_ENDIAN
);
1923 * XXX - needs to map values that are not valid UCS-2
1924 * characters (such as, I think, values used as the
1925 * components of a UTF-16 surrogate pair) to a
1926 * "substitute" UTF-8 character.
1928 strbuf
= tvb_get_unicode_string(scope
, tvb
, offset
, length
,
1929 encoding
& ENC_LITTLE_ENDIAN
);
1934 * XXX - do the copy and conversion in one pass.
1936 * XXX - multiple "dialects" of EBCDIC?
1938 tvb_ensure_bytes_exist(tvb
, offset
, length
); /* make sure length = -1 fails */
1939 strbuf
= (guint8
*)wmem_alloc(scope
, length
+ 1);
1941 ptr
= ensure_contiguous(tvb
, offset
, length
);
1942 memcpy(strbuf
, ptr
, length
);
1943 EBCDIC_to_ASCII(strbuf
, length
);
1945 strbuf
[length
] = '\0';
1952 * Given a tvbuff and an offset, with the offset assumed to refer to
1953 * a null-terminated string, find the length of that string (and throw
1954 * an exception if the tvbuff ends before we find the null), allocate
1955 * a buffer big enough to hold the string, copy the string into it,
1956 * and return a pointer to the string. Also return the length of the
1957 * string (including the terminating null) through a pointer.
1959 * If scope is NULL, memory is allocated with g_malloc() and user must
1960 * explicitely free it with g_free().
1961 * If scope is not NULL, memory is allocated with the corresponding pool
1965 tvb_get_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const gint offset
, gint
*lengthp
)
1970 size
= tvb_strsize(tvb
, offset
);
1971 strptr
= (guint8
*)wmem_alloc(scope
, size
);
1972 tvb_memcpy(tvb
, strptr
, offset
, size
);
1979 tvb_get_stringz_enc(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const gint offset
, gint
*lengthp
, const guint encoding
)
1984 switch (encoding
& ENC_CHARENCODING_MASK
) {
1989 * For now, we treat bogus values as meaning
1990 * "ASCII" rather than reporting an error,
1991 * for the benefit of old dissectors written
1992 * when the last argument to proto_tree_add_item()
1993 * was a gboolean for the byte order, not an
1994 * encoding value, and passed non-zero values
1995 * other than TRUE to mean "little-endian".
1997 * XXX - should map all octets with the 8th bit
1998 * not set to a "substitute" UTF-8 character.
2000 strptr
= tvb_get_stringz(scope
, tvb
, offset
, lengthp
);
2005 * XXX - should map all invalid UTF-8 sequences
2006 * to a "substitute" UTF-8 character.
2008 strptr
= tvb_get_stringz(scope
, tvb
, offset
, lengthp
);
2013 * XXX - needs to handle surrogate pairs and to map
2014 * invalid characters and sequences to a "substitute"
2017 strptr
= tvb_get_unicode_stringz(scope
, tvb
, offset
, lengthp
,
2018 encoding
& ENC_LITTLE_ENDIAN
);
2023 * XXX - needs to map values that are not valid UCS-2
2024 * characters (such as, I think, values used as the
2025 * components of a UTF-16 surrogate pair) to a
2026 * "substitute" UTF-8 character.
2028 strptr
= tvb_get_unicode_stringz(scope
, tvb
, offset
, lengthp
,
2029 encoding
& ENC_LITTLE_ENDIAN
);
2034 * XXX - do the copy and conversion in one pass.
2036 * XXX - multiple "dialects" of EBCDIC?
2038 size
= tvb_strsize(tvb
, offset
);
2039 strptr
= (guint8
*)wmem_alloc(scope
, size
);
2040 tvb_memcpy(tvb
, strptr
, offset
, size
);
2041 EBCDIC_to_ASCII(strptr
, size
);
2051 * Given a tvbuff and an offset, with the offset assumed to refer to
2052 * a null-terminated string, find the length of that string (and throw
2053 * an exception if the tvbuff ends before we find the null), ensure that
2054 * the TVB is flat, and return a pointer to the string (in the TVB).
2055 * Also return the length of the string (including the terminating null)
2056 * through a pointer.
2058 * As long as we aren't using composite TVBs, this saves the cycles used
2059 * (often unnecessariliy) in allocating a buffer and copying the string into
2060 * it. (If we do start using composite TVBs, we may want to replace this
2061 * function with the _ephemeral versoin.)
2064 tvb_get_const_stringz(tvbuff_t
*tvb
, const gint offset
, gint
*lengthp
)
2067 const guint8
*strptr
;
2069 size
= tvb_strsize(tvb
, offset
);
2070 strptr
= ensure_contiguous(tvb
, offset
, size
);
2077 * Unicode (UTF-16) version of tvb_get_stringz()
2079 * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
2081 * Returns an allocated UTF-8 string and updates lengthp pointer with length of string (in bytes)
2084 tvb_get_unicode_stringz(wmem_allocator_t
*scope
, tvbuff_t
*tvb
, const gint offset
, gint
*lengthp
, const guint encoding
)
2087 gint size
; /* Number of UTF-16 characters */
2088 gint i
; /* Byte counter for tvbuff */
2089 wmem_strbuf_t
*strbuf
;
2091 size
= tvb_unicode_strsize(tvb
, offset
);
2093 strbuf
= wmem_strbuf_new(scope
, NULL
);
2095 for(i
= 0; i
< size
; i
+= 2) {
2096 if (encoding
== ENC_BIG_ENDIAN
)
2097 uchar
= tvb_get_ntohs(tvb
, offset
+ i
);
2099 uchar
= tvb_get_letohs(tvb
, offset
+ i
);
2101 wmem_strbuf_append_unichar(strbuf
, uchar
);
2105 *lengthp
= i
; /* Number of *bytes* processed */
2107 return (gchar
*)wmem_strbuf_get_str(strbuf
);
2110 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2111 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2112 * Returns length of string (not including terminating NUL), or -1 if the string was
2113 * truncated in the buffer due to not having reached the terminating NUL.
2114 * In this way, it acts like g_snprintf().
2116 * bufsize MUST be greater than 0.
2118 * When processing a packet where the remaining number of bytes is less
2119 * than bufsize, an exception is not thrown if the end of the packet
2120 * is reached before the NUL is found. If no NUL is found before reaching
2121 * the end of the short packet, -1 is still returned, and the string
2122 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2123 * at the correct spot, terminating the string.
2125 * *bytes_copied will contain the number of bytes actually copied,
2126 * including the terminating-NUL.
2129 _tvb_get_nstringz(tvbuff_t
*tvb
, const gint offset
, const guint bufsize
, guint8
* buffer
, gint
*bytes_copied
)
2134 gboolean decreased_max
= FALSE
;
2136 /* Only read to end of tvbuff, w/o throwing exception. */
2137 check_offset_length(tvb
, offset
, -1, &abs_offset
, &len
);
2139 /* There must at least be room for the terminating NUL. */
2140 DISSECTOR_ASSERT(bufsize
!= 0);
2142 /* If there's no room for anything else, just return the NUL. */
2149 /* check_offset_length() won't throw an exception if we're
2150 * looking at the byte immediately after the end of the tvbuff. */
2152 THROW(ReportedBoundsError
);
2155 /* This should not happen because check_offset_length() would
2156 * have already thrown an exception if 'offset' were out-of-bounds.
2158 DISSECTOR_ASSERT(len
!= -1);
2161 * If we've been passed a negative number, bufsize will
2164 DISSECTOR_ASSERT(bufsize
<= G_MAXINT
);
2166 if ((guint
)len
< bufsize
) {
2168 decreased_max
= TRUE
;
2174 stringlen
= tvb_strnlen(tvb
, abs_offset
, limit
- 1);
2175 /* If NUL wasn't found, copy the data and return -1 */
2176 if (stringlen
== -1) {
2177 tvb_memcpy(tvb
, buffer
, abs_offset
, limit
);
2178 if (decreased_max
) {
2180 /* Add 1 for the extra NUL that we set at buffer[limit],
2181 * pretending that it was copied as part of the string. */
2182 *bytes_copied
= limit
+ 1;
2185 *bytes_copied
= limit
;
2190 /* Copy the string to buffer */
2191 tvb_memcpy(tvb
, buffer
, abs_offset
, stringlen
+ 1);
2192 *bytes_copied
= stringlen
+ 1;
2196 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2197 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2198 * Returns length of string (not including terminating NUL), or -1 if the string was
2199 * truncated in the buffer due to not having reached the terminating NUL.
2200 * In this way, it acts like g_snprintf().
2202 * When processing a packet where the remaining number of bytes is less
2203 * than bufsize, an exception is not thrown if the end of the packet
2204 * is reached before the NUL is found. If no NUL is found before reaching
2205 * the end of the short packet, -1 is still returned, and the string
2206 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2207 * at the correct spot, terminating the string.
2210 tvb_get_nstringz(tvbuff_t
*tvb
, const gint offset
, const guint bufsize
, guint8
* buffer
)
2214 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
2216 return _tvb_get_nstringz(tvb
, offset
, bufsize
, buffer
, &bytes_copied
);
2219 /* Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
2220 * have a terminating NUL. If the string was truncated when copied into buffer,
2221 * a NUL is placed at the end of buffer to terminate it.
2224 tvb_get_nstringz0(tvbuff_t
*tvb
, const gint offset
, const guint bufsize
, guint8
* buffer
)
2226 gint len
, bytes_copied
;
2228 DISSECTOR_ASSERT(tvb
&& tvb
->initialized
);
2230 len
= _tvb_get_nstringz(tvb
, offset
, bufsize
, buffer
, &bytes_copied
);
2233 buffer
[bufsize
- 1] = 0;
2234 return bytes_copied
- 1;
2242 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2243 * at that offset (which may be -1 for "all the way to the end of the
2244 * tvbuff"), find the end of the (putative) line that starts at the
2245 * specified offset in the tvbuff, going no further than the specified
2248 * Return the length of the line (not counting the line terminator at
2249 * the end), or, if we don't find a line terminator:
2251 * if "deseg" is true, return -1;
2253 * if "deseg" is false, return the amount of data remaining in
2256 * Set "*next_offset" to the offset of the character past the line
2257 * terminator, or past the end of the buffer if we don't find a line
2258 * terminator. (It's not set if we return -1.)
2261 tvb_find_line_end(tvbuff_t
*tvb
, const gint offset
, int len
, gint
*next_offset
, const gboolean desegment
)
2266 guchar found_needle
= 0;
2269 len
= tvb_length_remaining(tvb
, offset
);
2271 * XXX - what if "len" is still -1, meaning "offset is past the
2272 * end of the tvbuff"?
2274 eob_offset
= offset
+ len
;
2277 * Look either for a CR or an LF.
2279 eol_offset
= tvb_pbrk_guint8(tvb
, offset
, len
, "\r\n", &found_needle
);
2280 if (eol_offset
== -1) {
2282 * No CR or LF - line is presumably continued in next packet.
2286 * Tell our caller we saw no EOL, so they can
2287 * try to desegment and get the entire line
2293 * Pretend the line runs to the end of the tvbuff.
2295 linelen
= eob_offset
- offset
;
2297 *next_offset
= eob_offset
;
2301 * Find the number of bytes between the starting offset
2304 linelen
= eol_offset
- offset
;
2309 if (found_needle
== '\r') {
2311 * Yes - is it followed by an LF?
2313 if (eol_offset
+ 1 >= eob_offset
) {
2315 * Dunno - the next byte isn't in this
2320 * We'll return -1, although that
2321 * runs the risk that if the line
2322 * really *is* terminated with a CR,
2323 * we won't properly dissect this
2326 * It's probably more likely that
2327 * the line ends with CR-LF than
2328 * that it ends with CR by itself.
2334 * Well, we can at least look at the next
2337 if (tvb_get_guint8(tvb
, eol_offset
+ 1) == '\n') {
2339 * It's an LF; skip over the CR.
2347 * Return the offset of the character after the last
2348 * character in the line, skipping over the last character
2349 * in the line terminator.
2352 *next_offset
= eol_offset
+ 1;
2358 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2359 * at that offset (which may be -1 for "all the way to the end of the
2360 * tvbuff"), find the end of the (putative) line that starts at the
2361 * specified offset in the tvbuff, going no further than the specified
2364 * However, treat quoted strings inside the buffer specially - don't
2365 * treat newlines in quoted strings as line terminators.
2367 * Return the length of the line (not counting the line terminator at
2368 * the end), or the amount of data remaining in the buffer if we don't
2369 * find a line terminator.
2371 * Set "*next_offset" to the offset of the character past the line
2372 * terminator, or past the end of the buffer if we don't find a line
2376 tvb_find_line_end_unquoted(tvbuff_t
*tvb
, const gint offset
, int len
, gint
*next_offset
)
2378 gint cur_offset
, char_offset
;
2385 len
= tvb_length_remaining(tvb
, offset
);
2387 * XXX - what if "len" is still -1, meaning "offset is past the
2388 * end of the tvbuff"?
2390 eob_offset
= offset
+ len
;
2392 cur_offset
= offset
;
2396 * Is this part of the string quoted?
2400 * Yes - look only for the terminating quote.
2402 char_offset
= tvb_find_guint8(tvb
, cur_offset
, len
,
2406 * Look either for a CR, an LF, or a '"'.
2408 char_offset
= tvb_pbrk_guint8(tvb
, cur_offset
, len
, "\r\n\"", &c
);
2410 if (char_offset
== -1) {
2412 * Not found - line is presumably continued in
2414 * We pretend the line runs to the end of the tvbuff.
2416 linelen
= eob_offset
- offset
;
2418 *next_offset
= eob_offset
;
2424 * We're processing a quoted string.
2425 * We only looked for ", so we know it's a ";
2426 * as we're processing a quoted string, it's a
2436 * Un-quoted "; it begins a quoted
2442 * It's a CR or LF; we've found a line
2445 * Find the number of bytes between the
2446 * starting offset and the CR or LF.
2448 linelen
= char_offset
- offset
;
2455 * Yes; is it followed by an LF?
2457 if (char_offset
+ 1 < eob_offset
&&
2458 tvb_get_guint8(tvb
, char_offset
+ 1)
2461 * Yes; skip over the CR.
2468 * Return the offset of the character after
2469 * the last character in the line, skipping
2470 * over the last character in the line
2471 * terminator, and quit.
2474 *next_offset
= char_offset
+ 1;
2480 * Step past the character we found.
2482 cur_offset
= char_offset
+ 1;
2483 if (cur_offset
>= eob_offset
) {
2485 * The character we found was the last character
2486 * in the tvbuff - line is presumably continued in
2488 * We pretend the line runs to the end of the tvbuff.
2490 linelen
= eob_offset
- offset
;
2492 *next_offset
= eob_offset
;
2500 * Copied from the mgcp dissector. (This function should be moved to /epan )
2501 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
2502 * character following offset or offset + maxlength -1 whichever
2506 * tvb - The tvbuff in which we are skipping whitespace.
2507 * offset - The offset in tvb from which we begin trying to skip whitespace.
2508 * maxlength - The maximum distance from offset that we may try to skip
2511 * Returns: The position in tvb of the first non-whitespace
2512 * character following offset or offset + maxlength -1 whichever
2516 tvb_skip_wsp(tvbuff_t
*tvb
, const gint offset
, const gint maxlength
)
2518 gint counter
= offset
;
2522 /* Get the length remaining */
2523 tvb_len
= tvb_length(tvb
);
2524 end
= offset
+ maxlength
;
2530 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
2531 for (counter
= offset
;
2533 ((tempchar
= tvb_get_guint8(tvb
,counter
)) == ' ' ||
2534 tempchar
== '\t' || tempchar
== '\r' || tempchar
== '\n');
2541 tvb_skip_wsp_return(tvbuff_t
*tvb
, const gint offset
) {
2542 gint counter
= offset
;
2545 for(counter
= offset
; counter
> 0 &&
2546 ((tempchar
= tvb_get_guint8(tvb
,counter
)) == ' ' ||
2547 tempchar
== '\t' || tempchar
== '\n' || tempchar
== '\r'); counter
--);
2554 * Format a bunch of data from a tvbuff as bytes, returning a pointer
2555 * to the string with the formatted data, with "punct" as a byte
2559 tvb_bytes_to_str_punct(tvbuff_t
*tvb
, const gint offset
, const gint len
, const gchar punct
)
2561 return bytes_to_str_punct(ensure_contiguous(tvb
, offset
, len
), len
, punct
);
2566 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2567 * at that offset (which may be -1 for "all the way to the end of the
2568 * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
2569 * the low or high half byte, formating the digits according to an input digit set,
2570 * if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
2571 * A pointer to the packet scope allocated string will be returned.
2572 * Note a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
2574 static dgt_set_t Dgt1_9_bcd
= {
2576 /* 0 1 2 3 4 5 6 7 8 9 a b c d e */
2577 '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?'
2581 tvb_bcd_dig_to_wmem_packet_str(tvbuff_t
*tvb
, const gint offset
, const gint len
, dgt_set_t
*dgt
, gboolean skip_first
)
2587 gint t_offset
= offset
;
2593 length
= tvb_length(tvb
);
2594 if (length
< offset
) {
2598 length
= offset
+ len
;
2600 digit_str
= (char *)wmem_alloc(wmem_packet_scope(), (length
- offset
)*2+1);
2602 while (t_offset
< length
) {
2604 octet
= tvb_get_guint8(tvb
,t_offset
);
2606 digit_str
[i
] = dgt
->out
[octet
& 0x0f];
2612 * unpack second value in byte
2616 if (octet
== 0x0f) /* odd number bytes - hit filler */
2619 digit_str
[i
] = dgt
->out
[octet
& 0x0f];
2630 * Format a bunch of data from a tvbuff as bytes, returning a pointer
2631 * to the string with the formatted data.
2634 tvb_bytes_to_str(tvbuff_t
*tvb
, const gint offset
, const gint len
)
2636 return bytes_to_str(ensure_contiguous(tvb
, offset
, len
), len
);
2639 /* Find a needle tvbuff within a haystack tvbuff. */
2641 tvb_find_tvb(tvbuff_t
*haystack_tvb
, tvbuff_t
*needle_tvb
, const gint haystack_offset
)
2643 guint haystack_abs_offset
, haystack_abs_length
;
2644 const guint8
*haystack_data
;
2645 const guint8
*needle_data
;
2646 const guint needle_len
= needle_tvb
->length
;
2647 const guint8
*location
;
2649 DISSECTOR_ASSERT(haystack_tvb
&& haystack_tvb
->initialized
);
2651 if (haystack_tvb
->length
< 1 || needle_tvb
->length
< 1) {
2655 /* Get pointers to the tvbuffs' data. */
2656 haystack_data
= ensure_contiguous(haystack_tvb
, 0, -1);
2657 needle_data
= ensure_contiguous(needle_tvb
, 0, -1);
2659 check_offset_length(haystack_tvb
, haystack_offset
, -1,
2660 &haystack_abs_offset
, &haystack_abs_length
);
2662 location
= epan_memmem(haystack_data
+ haystack_abs_offset
, haystack_abs_length
,
2663 needle_data
, needle_len
);
2666 return (gint
) (location
- haystack_data
);
2674 * Uncompresses a zlib compressed packet inside a message of tvb at offset with
2675 * length comprlen. Returns an uncompressed tvbuffer if uncompression
2676 * succeeded or NULL if uncompression failed.
2678 #define TVB_Z_MIN_BUFSIZ 32768
2679 #define TVB_Z_MAX_BUFSIZ 1048576 * 10
2680 /* #define TVB_Z_DEBUG 1 */
2684 tvb_uncompress(tvbuff_t
*tvb
, const int offset
, int comprlen
)
2687 guint bytes_out
= 0;
2688 guint8
*compr
= NULL
;
2689 guint8
*uncompr
= NULL
;
2690 tvbuff_t
*uncompr_tvb
= NULL
;
2691 z_streamp strm
= NULL
;
2692 Bytef
*strmbuf
= NULL
;
2693 guint inits_done
= 0;
2694 gint wbits
= MAX_WBITS
;
2695 guint8
*next
= NULL
;
2696 guint bufsiz
= TVB_Z_MIN_BUFSIZ
;
2698 guint inflate_passes
= 0;
2699 guint bytes_in
= tvb_length_remaining(tvb
, offset
);
2706 compr
= (guint8
*)tvb_memdup(NULL
, tvb
, offset
, comprlen
);
2712 * Assume that the uncompressed data is at least twice as big as
2713 * the compressed size.
2715 bufsiz
= tvb_length_remaining(tvb
, offset
) * 2;
2716 bufsiz
= CLAMP(bufsiz
, TVB_Z_MIN_BUFSIZ
, TVB_Z_MAX_BUFSIZ
);
2719 printf("bufsiz: %u bytes\n", bufsiz
);
2724 strm
= g_new0(z_stream
, 1);
2725 strm
->next_in
= next
;
2726 strm
->avail_in
= comprlen
;
2728 strmbuf
= (Bytef
*)g_malloc0(bufsiz
);
2729 strm
->next_out
= strmbuf
;
2730 strm
->avail_out
= bufsiz
;
2732 err
= inflateInit2(strm
, wbits
);
2743 memset(strmbuf
, '\0', bufsiz
);
2744 strm
->next_out
= strmbuf
;
2745 strm
->avail_out
= bufsiz
;
2747 err
= inflate(strm
, Z_SYNC_FLUSH
);
2749 if (err
== Z_OK
|| err
== Z_STREAM_END
) {
2750 guint bytes_pass
= bufsiz
- strm
->avail_out
;
2756 if (uncompr
== NULL
) {
2758 * This is ugly workaround for bug #6480
2759 * (https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=6480)
2761 * g_memdup(..., 0) returns NULL (g_malloc(0) also)
2762 * when uncompr is NULL logic below doesn't create tvb
2763 * which is later interpreted as decompression failed.
2765 uncompr
= (guint8
*)((bytes_pass
|| err
!= Z_STREAM_END
) ?
2766 g_memdup(strmbuf
, bytes_pass
) :
2769 guint8
*new_data
= (guint8
*)g_malloc0(bytes_out
+ bytes_pass
);
2771 memcpy(new_data
, uncompr
, bytes_out
);
2772 memcpy(new_data
+ bytes_out
, strmbuf
, bytes_pass
);
2778 bytes_out
+= bytes_pass
;
2780 if (err
== Z_STREAM_END
) {
2786 } else if (err
== Z_BUF_ERROR
) {
2788 * It's possible that not enough frames were captured
2789 * to decompress this fully, so return what we've done
2796 if (uncompr
!= NULL
) {
2803 } else if (err
== Z_DATA_ERROR
&& inits_done
== 1
2804 && uncompr
== NULL
&& (*compr
== 0x1f) &&
2805 (*(compr
+ 1) == 0x8b)) {
2807 * inflate() is supposed to handle both gzip and deflate
2808 * streams automatically, but in reality it doesn't
2809 * seem to handle either (at least not within the
2810 * context of an HTTP response.) We have to try
2811 * several tweaks, depending on the type of data and
2812 * version of the library installed.
2816 * Gzip file format. Skip past the header, since the
2817 * fix to make it work (setting windowBits to 31)
2818 * doesn't work with all versions of the library.
2820 Bytef
*c
= compr
+ 2;
2823 if (*c
== Z_DEFLATED
) {
2835 /* Skip past the MTIME, XFL, and OS fields. */
2838 if (flags
& (1 << 2)) {
2839 /* An Extra field is present. */
2840 gint xsize
= (gint
)(*c
|
2846 if (flags
& (1 << 3)) {
2847 /* A null terminated filename */
2849 while ((c
- compr
) < comprlen
&& *c
!= '\0') {
2856 if (flags
& (1 << 4)) {
2857 /* A null terminated comment */
2859 while ((c
- compr
) < comprlen
&& *c
!= '\0') {
2869 strm
->next_in
= next
;
2870 if (c
- compr
> comprlen
) {
2877 comprlen
-= (int) (c
- compr
);
2880 inflateInit2(strm
, wbits
);
2882 } else if (err
== Z_DATA_ERROR
&& uncompr
== NULL
&&
2886 * Re-init the stream with a negative
2887 * MAX_WBITS. This is necessary due to
2888 * some servers (Apache) not sending
2889 * the deflate header with the
2890 * content-encoded response.
2896 strm
->next_in
= next
;
2897 strm
->avail_in
= comprlen
;
2900 memset(strmbuf
, '\0', bufsiz
);
2901 strm
->next_out
= strmbuf
;
2902 strm
->avail_out
= bufsiz
;
2904 err
= inflateInit2(strm
, wbits
);
2921 if (uncompr
== NULL
) {
2931 printf("inflate() total passes: %u\n", inflate_passes
);
2932 printf("bytes in: %u\nbytes out: %u\n\n", bytes_in
, bytes_out
);
2935 if (uncompr
!= NULL
) {
2936 uncompr_tvb
= tvb_new_real_data((guint8
*) uncompr
, bytes_out
, bytes_out
);
2937 tvb_set_free_cb(uncompr_tvb
, g_free
);
2944 tvb_uncompress(tvbuff_t
*tvb _U_
, const int offset _U_
, int comprlen _U_
)
2951 tvb_child_uncompress(tvbuff_t
*parent
, tvbuff_t
*tvb
, const int offset
, int comprlen
)
2953 tvbuff_t
*new_tvb
= tvb_uncompress(tvb
, offset
, comprlen
);
2955 tvb_set_child_real_data_tvbuff (parent
, new_tvb
);
2960 tvb_raw_offset(tvbuff_t
*tvb
)
2962 return ((tvb
->raw_offset
==-1)?(tvb
->raw_offset
= tvb_offset_from_real_beginning(tvb
)):tvb
->raw_offset
);
2966 tvb_set_fragment(tvbuff_t
*tvb
)
2968 tvb
->flags
|= TVBUFF_FRAGMENT
;
2972 tvb_get_ds_tvb(tvbuff_t
*tvb
)
2974 return(tvb
->ds_tvb
);
2978 * Editor modelines - http://www.wireshark.org/tools/modelines.html
2983 * indent-tabs-mode: t
2986 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2987 * :indentSize=8:tabSize=8:noTabs=false: