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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50 #include "tvbuff-int.h"
54 #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
57 ensure_contiguous_no_exception(tvbuff_t *tvb, const gint offset, const gint length,
61 ensure_contiguous(tvbuff_t *tvb, const gint offset, const gint length);
63 #if GLIB_CHECK_VERSION(2,10,0)
65 /* We dole out tvbuff's from this memchunk. */
66 static GMemChunk *tvbuff_mem_chunk = NULL;
72 #if GLIB_CHECK_VERSION(2,10,0)
74 if (!tvbuff_mem_chunk)
75 tvbuff_mem_chunk = g_mem_chunk_create(tvbuff_t, 20, G_ALLOC_AND_FREE);
82 #if GLIB_CHECK_VERSION(2,10,0)
85 g_mem_chunk_destroy(tvbuff_mem_chunk);
87 tvbuff_mem_chunk = NULL;
92 tvb_init(tvbuff_t *tvb, const tvbuff_type type)
94 tvb_backing_t *backing;
95 tvb_comp_t *composite;
98 tvb->initialized = FALSE;
101 tvb->reported_length = 0;
103 tvb->real_data = NULL;
104 tvb->raw_offset = -1;
109 case TVBUFF_REAL_DATA:
114 backing = &tvb->tvbuffs.subset;
120 case TVBUFF_COMPOSITE:
121 composite = &tvb->tvbuffs.composite;
122 composite->tvbs = NULL;
123 composite->start_offsets = NULL;
124 composite->end_offsets = NULL;
128 DISSECTOR_ASSERT_NOT_REACHED();
135 tvb_new(const tvbuff_type type)
139 #if GLIB_CHECK_VERSION(2,10,0)
140 tvb = g_slice_new(tvbuff_t);
142 tvb = g_chunk_new(tvbuff_t, tvbuff_mem_chunk);
151 tvb_new_with_subset(const guint subset_tvb_offset, const guint subset_tvb_length)
153 tvbuff_t *tvb = tvb_new(TVBUFF_SUBSET);
154 tvb->tvbuffs.subset.offset = subset_tvb_offset;
155 tvb->tvbuffs.subset.length = subset_tvb_length;
161 tvb_free(tvbuff_t* tvb)
163 tvbuff_t *member_tvb;
164 tvb_comp_t *composite;
169 if (tvb->usage_count == 0) {
171 case TVBUFF_REAL_DATA:
174 * XXX - do this with a union?
176 tvb->free_cb((gpointer)tvb->real_data);
181 /* This will be NULL if tvb_new_subset() fails because
182 * reported_length < -1 */
183 if (tvb->tvbuffs.subset.tvb) {
184 tvb_decrement_usage_count(tvb->tvbuffs.subset.tvb, 1);
188 case TVBUFF_COMPOSITE:
189 composite = &tvb->tvbuffs.composite;
190 for (slist = composite->tvbs; slist != NULL ; slist = slist->next) {
191 member_tvb = slist->data;
192 tvb_decrement_usage_count(member_tvb, 1);
195 g_slist_free(composite->tvbs);
197 g_free(composite->start_offsets);
198 g_free(composite->end_offsets);
199 if (tvb->real_data) {
201 * XXX - do this with a union?
203 g_free((gpointer)tvb->real_data);
210 g_slist_free(tvb->used_in);
213 #if GLIB_CHECK_VERSION(2,10,0)
214 g_slice_free(tvbuff_t, tvb);
216 g_chunk_free(tvb, tvbuff_mem_chunk);
222 tvb_increment_usage_count(tvbuff_t* tvb, const guint count)
224 tvb->usage_count += count;
226 return tvb->usage_count;
230 tvb_decrement_usage_count(tvbuff_t* tvb, const guint count)
232 if (tvb->usage_count <= count) {
233 tvb->usage_count = 1;
238 tvb->usage_count -= count;
239 return tvb->usage_count;
245 tvb_free_chain(tvbuff_t* tvb)
249 /* Recursively call tvb_free_chain() */
250 for (slist = tvb->used_in; slist != NULL ; slist = slist->next) {
251 tvb_free_chain( (tvbuff_t*)slist->data );
254 /* Stop the recursion */
261 tvb_set_free_cb(tvbuff_t* tvb, const tvbuff_free_cb_t func)
263 DISSECTOR_ASSERT(tvb);
264 DISSECTOR_ASSERT(tvb->type == TVBUFF_REAL_DATA);
269 add_to_used_in_list(tvbuff_t *tvb, tvbuff_t *used_in)
271 tvb->used_in = g_slist_prepend(tvb->used_in, used_in);
272 tvb_increment_usage_count(tvb, 1);
276 tvb_set_child_real_data_tvbuff(tvbuff_t* parent, tvbuff_t* child)
278 DISSECTOR_ASSERT(parent && child);
279 DISSECTOR_ASSERT(parent->initialized);
280 DISSECTOR_ASSERT(child->initialized);
281 DISSECTOR_ASSERT(child->type == TVBUFF_REAL_DATA);
282 add_to_used_in_list(parent, child);
286 tvb_set_real_data_no_exceptions(tvbuff_t* tvb, const guint8* data, const guint length, const gint reported_length)
288 tvb->real_data = data;
289 tvb->length = length;
290 tvb->reported_length = reported_length;
291 tvb->initialized = TRUE;
295 tvb_set_real_data(tvbuff_t* tvb, const guint8* data, const guint length, const gint reported_length)
297 DISSECTOR_ASSERT(tvb);
298 DISSECTOR_ASSERT(tvb->type == TVBUFF_REAL_DATA);
299 DISSECTOR_ASSERT(!tvb->initialized);
301 THROW_ON(reported_length < -1, ReportedBoundsError);
303 tvb_set_real_data_no_exceptions(tvb, data, length, reported_length);
307 tvb_new_real_data(const guint8* data, const guint length, const gint reported_length)
311 THROW_ON(reported_length < -1, ReportedBoundsError);
313 tvb = tvb_new(TVBUFF_REAL_DATA);
315 tvb_set_real_data_no_exceptions(tvb, data, length, reported_length);
318 * This is the top-level real tvbuff for this data source,
319 * so its data source tvbuff is itself.
327 tvb_new_child_real_data(tvbuff_t *parent, const guint8* data, const guint length, const gint reported_length)
329 tvbuff_t *tvb = tvb_new_real_data(data, length, reported_length);
331 tvb_set_child_real_data_tvbuff (parent, tvb);
337 /* Computes the absolute offset and length based on a possibly-negative offset
338 * and a length that is possible -1 (which means "to the end of the data").
339 * Returns TRUE/FALSE indicating whether the offset is in bounds or
340 * not. The integer ptrs are modified with the new offset and length.
341 * No exception is thrown.
343 * XXX - we return TRUE, not FALSE, if the offset is positive and right
344 * after the end of the tvbuff (i.e., equal to the length). We do this
345 * so that a dissector constructing a subset tvbuff for the next protocol
346 * will get a zero-length tvbuff, not an exception, if there's no data
347 * left for the next protocol - we want the next protocol to be the one
348 * that gets an exception, so the error is reported as an error in that
349 * protocol rather than the containing protocol. */
351 compute_offset_length(const guint tvb_length_val, const guint tvb_reported_length_val, const gint offset, const gint length_val,
352 guint *offset_ptr, guint *length_ptr, int *exception)
354 DISSECTOR_ASSERT(offset_ptr);
355 DISSECTOR_ASSERT(length_ptr);
357 /* Compute the offset */
359 /* Positive offset - relative to the beginning of the packet. */
360 if ((guint) offset > tvb_reported_length_val) {
362 *exception = ReportedBoundsError;
366 else if ((guint) offset > tvb_length_val) {
368 *exception = BoundsError;
373 *offset_ptr = offset;
377 /* Negative offset - relative to the end of the packet. */
378 if ((guint) -offset > tvb_reported_length_val) {
380 *exception = ReportedBoundsError;
384 else if ((guint) -offset > tvb_length_val) {
386 *exception = BoundsError;
391 *offset_ptr = tvb_length_val + offset;
395 /* Compute the length */
396 if (length_val < -1) {
398 /* XXX - ReportedBoundsError? */
399 *exception = BoundsError;
403 else if (length_val == -1) {
404 *length_ptr = tvb_length_val - *offset_ptr;
407 *length_ptr = length_val;
415 check_offset_length_no_exception(const guint tvb_length_val, const guint tvb_reported_length_val, const gint offset, gint const length_val,
416 guint *offset_ptr, guint *length_ptr, int *exception)
420 if (!compute_offset_length(tvb_length_val, tvb_reported_length_val, offset, length_val, offset_ptr, length_ptr, exception)) {
425 * Compute the offset of the first byte past the length.
427 end_offset = *offset_ptr + *length_ptr;
430 * Check for an overflow, and clamp "end_offset" at the maximum
431 * if we got an overflow - that should force us to indicate that
432 * we're past the end of the tvbuff.
434 if (end_offset < *offset_ptr)
435 end_offset = UINT_MAX;
438 * Check whether that offset goes more than one byte past the
441 * If not, return TRUE; otherwise, return FALSE and, if "exception"
442 * is non-null, return the appropriate exception through it.
444 if (end_offset <= tvb_length_val) {
447 else if (end_offset <= tvb_reported_length_val) {
449 *exception = BoundsError;
454 *exception = ReportedBoundsError;
461 /* Checks (+/-) offset and length and throws an exception if
462 * either is out of bounds. Sets integer ptrs to the new offset
465 check_offset_length(const guint tvb_length_val, const guint tvb_reported_length_val, const gint offset, gint const length_val,
466 guint *offset_ptr, guint *length_ptr)
470 if (!check_offset_length_no_exception(tvb_length_val, tvb_reported_length_val, offset, length_val, offset_ptr, length_ptr, &exception)) {
471 DISSECTOR_ASSERT(exception > 0);
477 tvb_set_subset_no_exceptions(tvbuff_t *tvb, tvbuff_t *backing, const gint reported_length)
479 tvb->tvbuffs.subset.tvb = backing;
480 tvb->length = tvb->tvbuffs.subset.length;
482 if (reported_length == -1) {
483 tvb->reported_length = backing->reported_length - tvb->tvbuffs.subset.offset;
486 tvb->reported_length = reported_length;
488 tvb->initialized = TRUE;
489 add_to_used_in_list(backing, tvb);
491 /* Optimization. If the backing buffer has a pointer to contiguous, real data,
492 * then we can point directly to our starting offset in that buffer */
493 if (backing->real_data != NULL) {
494 tvb->real_data = backing->real_data + tvb->tvbuffs.subset.offset;
499 tvb_set_subset(tvbuff_t *tvb, tvbuff_t *backing,
500 const gint backing_offset, const gint backing_length, const gint reported_length)
502 DISSECTOR_ASSERT(tvb);
503 DISSECTOR_ASSERT(tvb->type == TVBUFF_SUBSET);
504 DISSECTOR_ASSERT(!tvb->initialized);
506 THROW_ON(reported_length < -1, ReportedBoundsError);
508 check_offset_length(backing->length, backing->reported_length, backing_offset, backing_length,
509 &tvb->tvbuffs.subset.offset,
510 &tvb->tvbuffs.subset.length);
512 tvb_set_subset_no_exceptions(tvb, backing, reported_length);
516 tvb_new_subset(tvbuff_t *backing, const gint backing_offset, const gint backing_length, const gint reported_length)
519 guint subset_tvb_offset;
520 guint subset_tvb_length;
522 DISSECTOR_ASSERT(backing && backing->initialized);
524 THROW_ON(reported_length < -1, ReportedBoundsError);
526 check_offset_length(backing->length, backing->reported_length, backing_offset, backing_length,
530 tvb = tvb_new_with_subset(subset_tvb_offset, subset_tvb_length);
532 tvb_set_subset_no_exceptions(tvb, backing, reported_length);
535 * The top-level data source of this tvbuff is the top-level
536 * data source of its parent.
538 tvb->ds_tvb = backing->ds_tvb;
544 tvb_new_subset_remaining(tvbuff_t *backing, const gint backing_offset)
547 guint subset_tvb_offset;
548 guint subset_tvb_length;
550 check_offset_length(backing->length, backing->reported_length, backing_offset, -1 /* backing_length */,
554 tvb = tvb_new_with_subset(subset_tvb_offset, subset_tvb_length);
556 tvb_set_subset_no_exceptions(tvb, backing, -1 /* reported_length */);
559 * The top-level data source of this tvbuff is the top-level
560 * data source of its parent.
562 tvb->ds_tvb = backing->ds_tvb;
568 tvb_composite_append(tvbuff_t* tvb, tvbuff_t* member)
570 tvb_comp_t *composite;
572 DISSECTOR_ASSERT(tvb && !tvb->initialized);
573 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
574 composite = &tvb->tvbuffs.composite;
575 composite->tvbs = g_slist_append( composite->tvbs, member );
576 add_to_used_in_list(tvb, member);
580 tvb_composite_prepend(tvbuff_t* tvb, tvbuff_t* member)
582 tvb_comp_t *composite;
584 DISSECTOR_ASSERT(tvb && !tvb->initialized);
585 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
586 composite = &tvb->tvbuffs.composite;
587 composite->tvbs = g_slist_prepend( composite->tvbs, member );
588 add_to_used_in_list(tvb, member);
592 tvb_new_composite(void)
594 return tvb_new(TVBUFF_COMPOSITE);
598 tvb_composite_finalize(tvbuff_t* tvb)
602 tvbuff_t *member_tvb;
603 tvb_comp_t *composite;
606 DISSECTOR_ASSERT(tvb && !tvb->initialized);
607 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
608 DISSECTOR_ASSERT(tvb->length == 0);
609 DISSECTOR_ASSERT(tvb->reported_length == 0);
611 composite = &tvb->tvbuffs.composite;
612 num_members = g_slist_length(composite->tvbs);
614 composite->start_offsets = g_new(guint, num_members);
615 composite->end_offsets = g_new(guint, num_members);
617 for (slist = composite->tvbs; slist != NULL; slist = slist->next) {
618 DISSECTOR_ASSERT((guint) i < num_members);
619 member_tvb = slist->data;
620 composite->start_offsets[i] = tvb->length;
621 tvb->length += member_tvb->length;
622 tvb->reported_length += member_tvb->reported_length;
623 composite->end_offsets[i] = tvb->length - 1;
627 tvb->initialized = TRUE;
633 tvb_length(const tvbuff_t* tvb)
635 DISSECTOR_ASSERT(tvb && tvb->initialized);
641 tvb_length_remaining(const tvbuff_t *tvb, const gint offset)
643 guint abs_offset, abs_length;
645 DISSECTOR_ASSERT(tvb && tvb->initialized);
647 if (compute_offset_length(tvb->length, tvb->reported_length, offset, -1, &abs_offset, &abs_length, NULL)) {
656 tvb_ensure_length_remaining(const tvbuff_t *tvb, const gint offset)
658 guint abs_offset, abs_length;
661 DISSECTOR_ASSERT(tvb && tvb->initialized);
663 if (!compute_offset_length(tvb->length, tvb->reported_length, offset, -1, &abs_offset, &abs_length, &exception)) {
666 if (abs_length == 0) {
668 * This routine ensures there's at least one byte available.
669 * There aren't any bytes available, so throw the appropriate
672 if (abs_offset >= tvb->reported_length)
673 THROW(ReportedBoundsError);
683 /* Validates that 'length' bytes are available starting from
684 * offset (pos/neg). Does not throw an exception. */
686 tvb_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
688 guint abs_offset, abs_length;
690 DISSECTOR_ASSERT(tvb && tvb->initialized);
692 if (!compute_offset_length(tvb->length, tvb->reported_length, offset, length, &abs_offset, &abs_length, NULL))
695 if (abs_offset + abs_length <= tvb->length) {
703 /* Validates that 'length' bytes are available starting from
704 * offset (pos/neg). Throws an exception if they aren't. */
706 tvb_ensure_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
708 guint abs_offset, abs_length;
710 DISSECTOR_ASSERT(tvb && tvb->initialized);
713 * -1 doesn't mean "until end of buffer", as that's pointless
714 * for this routine. We must treat it as a Really Large Positive
715 * Number, so that we throw an exception; we throw
716 * ReportedBoundsError, as if it were past even the end of a
717 * reassembled packet, and past the end of even the data we
720 * We do the same with other negative lengths.
723 THROW(ReportedBoundsError);
725 check_offset_length(tvb->length, tvb->reported_length, offset, length, &abs_offset, &abs_length);
729 tvb_offset_exists(const tvbuff_t *tvb, const gint offset)
731 guint abs_offset, abs_length;
733 DISSECTOR_ASSERT(tvb && tvb->initialized);
734 if (!compute_offset_length(tvb->length, tvb->reported_length, offset, -1, &abs_offset, &abs_length, NULL))
737 if (abs_offset < tvb->length) {
746 tvb_reported_length(const tvbuff_t* tvb)
748 DISSECTOR_ASSERT(tvb && tvb->initialized);
750 return tvb->reported_length;
754 tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset)
756 guint abs_offset, abs_length;
758 DISSECTOR_ASSERT(tvb && tvb->initialized);
760 if (compute_offset_length(tvb->length, tvb->reported_length, offset, -1, &abs_offset, &abs_length, NULL)) {
761 if (tvb->reported_length >= abs_offset)
762 return tvb->reported_length - abs_offset;
771 /* Set the reported length of a tvbuff to a given value; used for protocols
772 whose headers contain an explicit length and where the calling
773 dissector's payload may include padding as well as the packet for
776 Also adjusts the data length. */
778 tvb_set_reported_length(tvbuff_t* tvb, const guint reported_length)
780 DISSECTOR_ASSERT(tvb && tvb->initialized);
782 if (reported_length > tvb->reported_length)
783 THROW(ReportedBoundsError);
785 tvb->reported_length = reported_length;
786 if (reported_length < tvb->length)
787 tvb->length = reported_length;
793 first_real_data_ptr(tvbuff_t *tvb)
798 case TVBUFF_REAL_DATA:
799 return tvb->real_data;
801 member = tvb->tvbuffs.subset.tvb;
802 return first_real_data_ptr(member);
803 case TVBUFF_COMPOSITE:
804 member = tvb->tvbuffs.composite.tvbs->data;
805 return first_real_data_ptr(member);
808 DISSECTOR_ASSERT_NOT_REACHED();
814 offset_from_real_beginning(const tvbuff_t *tvb, const guint counter)
819 case TVBUFF_REAL_DATA:
822 member = tvb->tvbuffs.subset.tvb;
823 return offset_from_real_beginning(member, counter + tvb->tvbuffs.subset.offset);
824 case TVBUFF_COMPOSITE:
825 member = tvb->tvbuffs.composite.tvbs->data;
826 return offset_from_real_beginning(member, counter);
829 DISSECTOR_ASSERT_NOT_REACHED();
834 tvb_offset_from_real_beginning(const tvbuff_t *tvb)
836 return offset_from_real_beginning(tvb, 0);
840 composite_ensure_contiguous_no_exception(tvbuff_t *tvb, const guint abs_offset,
841 const guint abs_length)
843 guint i, num_members;
844 tvb_comp_t *composite;
845 tvbuff_t *member_tvb = NULL;
846 guint member_offset, member_length;
849 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
851 /* Maybe the range specified by offset/length
852 * is contiguous inside one of the member tvbuffs */
853 composite = &tvb->tvbuffs.composite;
854 num_members = g_slist_length(composite->tvbs);
856 for (i = 0; i < num_members; i++) {
857 if (abs_offset <= composite->end_offsets[i]) {
858 slist = g_slist_nth(composite->tvbs, i);
859 member_tvb = slist->data;
863 DISSECTOR_ASSERT(member_tvb);
865 if (check_offset_length_no_exception(member_tvb->length, member_tvb->reported_length, abs_offset - composite->start_offsets[i],
866 abs_length, &member_offset, &member_length, NULL)) {
869 * The range is, in fact, contiguous within member_tvb.
871 DISSECTOR_ASSERT(!tvb->real_data);
872 return ensure_contiguous_no_exception(member_tvb, member_offset, member_length, NULL);
875 tvb->real_data = tvb_memdup(tvb, 0, -1);
876 return tvb->real_data + abs_offset;
879 DISSECTOR_ASSERT_NOT_REACHED();
883 ensure_contiguous_no_exception(tvbuff_t *tvb, const gint offset, const gint length,
886 guint abs_offset, abs_length;
888 if (!check_offset_length_no_exception(tvb->length, tvb->reported_length, offset, length,
889 &abs_offset, &abs_length, exception)) {
894 * We know that all the data is present in the tvbuff, so
895 * no exceptions should be thrown.
897 if (tvb->real_data) {
898 return tvb->real_data + abs_offset;
902 case TVBUFF_REAL_DATA:
903 DISSECTOR_ASSERT_NOT_REACHED();
905 return ensure_contiguous_no_exception(tvb->tvbuffs.subset.tvb,
906 abs_offset - tvb->tvbuffs.subset.offset,
908 case TVBUFF_COMPOSITE:
909 return composite_ensure_contiguous_no_exception(tvb, abs_offset, abs_length);
913 DISSECTOR_ASSERT_NOT_REACHED();
918 ensure_contiguous(tvbuff_t *tvb, const gint offset, const gint length)
923 p = ensure_contiguous_no_exception(tvb, offset, length, &exception);
925 DISSECTOR_ASSERT(exception > 0);
932 fast_ensure_contiguous(tvbuff_t *tvb, const gint offset, const guint length)
937 DISSECTOR_ASSERT(tvb && tvb->initialized);
938 /* We don't check for overflow in this fast path so we only handle simple types */
939 DISSECTOR_ASSERT(length <= 8);
941 if (offset < 0 || !tvb->real_data) {
942 return ensure_contiguous(tvb, offset, length);
946 end_offset = u_offset + length;
948 if (end_offset <= tvb->length) {
949 return tvb->real_data + u_offset;
952 if (end_offset > tvb->reported_length) {
953 THROW(ReportedBoundsError);
961 guint8_pbrk(const guint8* haystack, size_t haystacklen, const guint8 *needles, guchar *found_needle)
963 gchar tmp[256] = { 0 };
964 const guint8 *haystack_end;
969 haystack_end = haystack + haystacklen;
970 while (haystack < haystack_end) {
971 if (tmp[*haystack]) {
973 *found_needle = *haystack;
984 /************** ACCESSORS **************/
987 composite_memcpy(tvbuff_t *tvb, guint8* target, guint abs_offset, size_t abs_length)
989 guint i, num_members;
990 tvb_comp_t *composite;
991 tvbuff_t *member_tvb = NULL;
992 guint member_offset, member_length;
996 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
998 /* Maybe the range specified by offset/length
999 * is contiguous inside one of the member tvbuffs */
1000 composite = &tvb->tvbuffs.composite;
1001 num_members = g_slist_length(composite->tvbs);
1003 for (i = 0; i < num_members; i++) {
1004 if (abs_offset <= composite->end_offsets[i]) {
1005 slist = g_slist_nth(composite->tvbs, i);
1006 member_tvb = slist->data;
1010 DISSECTOR_ASSERT(member_tvb);
1012 if (check_offset_length_no_exception(member_tvb->length, member_tvb->reported_length, abs_offset - composite->start_offsets[i],
1013 (gint) abs_length, &member_offset, &member_length, NULL)) {
1015 DISSECTOR_ASSERT(!tvb->real_data);
1016 return tvb_memcpy(member_tvb, target, member_offset, member_length);
1019 /* The requested data is non-contiguous inside
1020 * the member tvb. We have to memcpy() the part that's in the member tvb,
1021 * then iterate across the other member tvb's, copying their portions
1022 * until we have copied all data.
1024 retval = compute_offset_length(member_tvb->length, member_tvb->reported_length, abs_offset - composite->start_offsets[i], -1,
1025 &member_offset, &member_length, NULL);
1026 DISSECTOR_ASSERT(retval);
1028 tvb_memcpy(member_tvb, target, member_offset, member_length);
1029 abs_offset += member_length;
1030 abs_length -= member_length;
1033 if (abs_length > 0) {
1034 composite_memcpy(tvb, target + member_length, abs_offset, abs_length);
1040 DISSECTOR_ASSERT_NOT_REACHED();
1044 tvb_memcpy(tvbuff_t *tvb, void* target, const gint offset, size_t length)
1046 guint abs_offset, abs_length;
1048 DISSECTOR_ASSERT(tvb && tvb->initialized);
1051 * XXX - we should eliminate the "length = -1 means 'to the end
1052 * of the tvbuff'" convention, and use other means to achieve
1053 * that; this would let us eliminate a bunch of checks for
1054 * negative lengths in cases where the protocol has a 32-bit
1057 * Allowing -1 but throwing an assertion on other negative
1058 * lengths is a bit more work with the length being a size_t;
1059 * instead, we check for a length <= 2^31-1.
1061 DISSECTOR_ASSERT(length <= 0x7FFFFFFF);
1062 check_offset_length(tvb->length, tvb->reported_length, offset, (gint) length, &abs_offset, &abs_length);
1064 if (tvb->real_data) {
1065 return memcpy(target, tvb->real_data + abs_offset, abs_length);
1069 case TVBUFF_REAL_DATA:
1070 DISSECTOR_ASSERT_NOT_REACHED();
1073 return tvb_memcpy(tvb->tvbuffs.subset.tvb, target,
1074 abs_offset - tvb->tvbuffs.subset.offset,
1077 case TVBUFF_COMPOSITE:
1078 return composite_memcpy(tvb, target, offset, length);
1081 DISSECTOR_ASSERT_NOT_REACHED();
1087 * XXX - this doesn't treat a length of -1 as an error.
1088 * If it did, this could replace some code that calls
1089 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
1092 * "composite_ensure_contiguous_no_exception()" depends on -1 not being
1093 * an error; does anything else depend on this routine treating -1 as
1094 * meaning "to the end of the buffer"?
1097 tvb_memdup(tvbuff_t *tvb, const gint offset, size_t length)
1099 guint abs_offset, abs_length;
1102 DISSECTOR_ASSERT(tvb && tvb->initialized);
1104 check_offset_length(tvb->length, tvb->reported_length, offset, (gint) length, &abs_offset, &abs_length);
1106 duped = g_malloc(abs_length);
1107 return tvb_memcpy(tvb, duped, abs_offset, abs_length);
1111 * XXX - this doesn't treat a length of -1 as an error.
1112 * If it did, this could replace some code that calls
1113 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
1116 * "composite_ensure_contiguous_no_exception()" depends on -1 not being
1117 * an error; does anything else depend on this routine treating -1 as
1118 * meaning "to the end of the buffer"?
1120 * This function allocates memory from a buffer with packet lifetime.
1121 * You do not have to free this buffer, it will be automatically freed
1122 * when wireshark starts decoding the next packet.
1123 * Do not use this function if you want the allocated memory to be persistent
1124 * after the current packet has been dissected.
1127 ep_tvb_memdup(tvbuff_t *tvb, const gint offset, size_t length)
1129 guint abs_offset, abs_length;
1132 DISSECTOR_ASSERT(tvb && tvb->initialized);
1134 check_offset_length(tvb->length, tvb->reported_length, offset, (gint) length, &abs_offset, &abs_length);
1136 duped = ep_alloc(abs_length);
1137 return tvb_memcpy(tvb, duped, abs_offset, abs_length);
1143 tvb_get_ptr(tvbuff_t *tvb, const gint offset, const gint length)
1145 return ensure_contiguous(tvb, offset, length);
1148 /* ---------------- */
1150 tvb_get_guint8(tvbuff_t *tvb, const gint offset)
1154 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint8));
1159 tvb_get_ntohs(tvbuff_t *tvb, const gint offset)
1163 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
1168 tvb_get_ntoh24(tvbuff_t *tvb, const gint offset)
1172 ptr = fast_ensure_contiguous(tvb, offset, 3);
1173 return pntoh24(ptr);
1177 tvb_get_ntohl(tvbuff_t *tvb, const gint offset)
1181 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1186 tvb_get_ntoh40(tvbuff_t *tvb, const gint offset)
1190 ptr = fast_ensure_contiguous(tvb, offset, 5);
1191 return pntoh40(ptr);
1195 tvb_get_ntoh48(tvbuff_t *tvb, const gint offset)
1199 ptr = fast_ensure_contiguous(tvb, offset, 6);
1200 return pntoh48(ptr);
1204 tvb_get_ntoh56(tvbuff_t *tvb, const gint offset)
1208 ptr = fast_ensure_contiguous(tvb, offset, 7);
1209 return pntoh56(ptr);
1213 tvb_get_ntoh64(tvbuff_t *tvb, const gint offset)
1217 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
1218 return pntoh64(ptr);
1222 * Stuff for IEEE float handling on platforms that don't have IEEE
1223 * format as the native floating-point format.
1225 * For now, we treat only the VAX as such a platform.
1227 * XXX - other non-IEEE boxes that can run UNIX include some Crays,
1228 * and possibly other machines.
1230 * It appears that the official Linux port to System/390 and
1231 * zArchitecture uses IEEE format floating point (not a
1234 * I don't know whether there are any other machines that
1235 * could run Wireshark and that don't use IEEE format.
1236 * As far as I know, all of the main commercial microprocessor
1237 * families on which OSes that support Wireshark can run
1238 * use IEEE format (x86, 68k, SPARC, MIPS, PA-RISC, Alpha,
1239 * IA-64, and so on).
1249 #define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
1250 #define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
1251 #define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
1253 #define IEEE_SP_SIGN_MASK 0x80000000
1254 #define IEEE_SP_EXPONENT_MASK 0x7F800000
1255 #define IEEE_SP_MANTISSA_MASK 0x007FFFFF
1256 #define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
1258 #define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
1259 #define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
1260 #define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
1263 ieee_float_is_zero(const guint32 w)
1265 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1269 get_ieee_float(const guint32 w)
1275 sign = w & IEEE_SP_SIGN_MASK;
1276 exponent = w & IEEE_SP_EXPONENT_MASK;
1277 mantissa = w & IEEE_SP_MANTISSA_MASK;
1279 if (ieee_float_is_zero(w)) {
1280 /* number is zero, unnormalized, or not-a-number */
1285 * XXX - how to handle this?
1287 if (IEEE_SP_INFINITY == exponent) {
1289 * number is positive or negative infinity, or a special value
1291 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1295 exponent = ((exponent >> IEEE_SP_MANTISSA_WIDTH) - IEEE_SP_BIAS) -
1296 IEEE_SP_MANTISSA_WIDTH;
1297 mantissa |= IEEE_SP_IMPLIED_BIT;
1300 return -mantissa * pow(2, exponent);
1302 return mantissa * pow(2, exponent);
1307 * We assume that if you don't have IEEE floating-point, you have a
1308 * compiler that understands 64-bit integral quantities.
1310 #define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
1311 #define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
1312 #define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
1314 #define IEEE_DP_SIGN_MASK 0x8000000000000000LL
1315 #define IEEE_DP_EXPONENT_MASK 0x7FF0000000000000LL
1316 #define IEEE_DP_MANTISSA_MASK 0x000FFFFFFFFFFFFFLL
1317 #define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
1319 #define IEEE_DP_IMPLIED_BIT (1LL << IEEE_DP_MANTISSA_WIDTH)
1320 #define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
1321 #define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
1324 ieee_double_is_zero(const guint64 w)
1326 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1330 get_ieee_double(const guint64 w)
1336 sign = w & IEEE_DP_SIGN_MASK;
1337 exponent = w & IEEE_DP_EXPONENT_MASK;
1338 mantissa = w & IEEE_DP_MANTISSA_MASK;
1340 if (ieee_double_is_zero(w)) {
1341 /* number is zero, unnormalized, or not-a-number */
1346 * XXX - how to handle this?
1348 if (IEEE_DP_INFINITY == exponent) {
1350 * number is positive or negative infinity, or a special value
1352 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1356 exponent = ((exponent >> IEEE_DP_MANTISSA_WIDTH) - IEEE_DP_BIAS) -
1357 IEEE_DP_MANTISSA_WIDTH;
1358 mantissa |= IEEE_DP_IMPLIED_BIT;
1361 return -mantissa * pow(2, exponent);
1363 return mantissa * pow(2, exponent);
1368 * Fetches an IEEE single-precision floating-point number, in
1369 * big-endian form, and returns a "float".
1371 * XXX - should this be "double", in case there are IEEE single-
1372 * precision numbers that won't fit in some platform's native
1376 tvb_get_ntohieee_float(tvbuff_t *tvb, const int offset)
1379 return get_ieee_float(tvb_get_ntohl(tvb, offset));
1386 ieee_fp_union.w = tvb_get_ntohl(tvb, offset);
1387 return ieee_fp_union.f;
1392 * Fetches an IEEE double-precision floating-point number, in
1393 * big-endian form, and returns a "double".
1396 tvb_get_ntohieee_double(tvbuff_t *tvb, const int offset)
1410 #ifdef WORDS_BIGENDIAN
1411 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset);
1412 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset+4);
1414 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset+4);
1415 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset);
1418 return get_ieee_double(ieee_fp_union.dw);
1420 return ieee_fp_union.d;
1425 tvb_get_letohs(tvbuff_t *tvb, const gint offset)
1429 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
1430 return pletohs(ptr);
1434 tvb_get_letoh24(tvbuff_t *tvb, const gint offset)
1438 ptr = fast_ensure_contiguous(tvb, offset, 3);
1439 return pletoh24(ptr);
1443 tvb_get_letohl(tvbuff_t *tvb, const gint offset)
1447 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1448 return pletohl(ptr);
1452 tvb_get_letoh40(tvbuff_t *tvb, const gint offset)
1456 ptr = fast_ensure_contiguous(tvb, offset, 5);
1457 return pletoh40(ptr);
1461 tvb_get_letoh48(tvbuff_t *tvb, const gint offset)
1465 ptr = fast_ensure_contiguous(tvb, offset, 6);
1466 return pletoh48(ptr);
1470 tvb_get_letoh56(tvbuff_t *tvb, const gint offset)
1474 ptr = fast_ensure_contiguous(tvb, offset, 7);
1475 return pletoh56(ptr);
1479 tvb_get_letoh64(tvbuff_t *tvb, const gint offset)
1483 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
1484 return pletoh64(ptr);
1488 * Fetches an IEEE single-precision floating-point number, in
1489 * little-endian form, and returns a "float".
1491 * XXX - should this be "double", in case there are IEEE single-
1492 * precision numbers that won't fit in some platform's native
1496 tvb_get_letohieee_float(tvbuff_t *tvb, const int offset)
1499 return get_ieee_float(tvb_get_letohl(tvb, offset));
1506 ieee_fp_union.w = tvb_get_letohl(tvb, offset);
1507 return ieee_fp_union.f;
1512 * Fetches an IEEE double-precision floating-point number, in
1513 * little-endian form, and returns a "double".
1516 tvb_get_letohieee_double(tvbuff_t *tvb, const int offset)
1530 #ifdef WORDS_BIGENDIAN
1531 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset+4);
1532 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset);
1534 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset);
1535 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset+4);
1538 return get_ieee_double(ieee_fp_union.dw);
1540 return ieee_fp_union.d;
1544 /* Fetch an IPv4 address, in network byte order.
1545 * We do *not* convert them to host byte order; we leave them in
1546 * network byte order. */
1548 tvb_get_ipv4(tvbuff_t *tvb, const gint offset)
1553 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1554 memcpy(&addr, ptr, sizeof addr);
1558 /* Fetch an IPv6 address. */
1560 tvb_get_ipv6(tvbuff_t *tvb, const gint offset, struct e_in6_addr *addr)
1564 ptr = ensure_contiguous(tvb, offset, sizeof(*addr));
1565 memcpy(addr, ptr, sizeof *addr);
1570 tvb_get_ntohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid)
1572 ensure_contiguous(tvb, offset, sizeof(*guid));
1573 guid->data1 = tvb_get_ntohl(tvb, offset);
1574 guid->data2 = tvb_get_ntohs(tvb, offset + 4);
1575 guid->data3 = tvb_get_ntohs(tvb, offset + 6);
1576 tvb_memcpy(tvb, guid->data4, offset + 8, sizeof guid->data4);
1580 tvb_get_letohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid)
1582 ensure_contiguous(tvb, offset, sizeof(*guid));
1583 guid->data1 = tvb_get_letohl(tvb, offset);
1584 guid->data2 = tvb_get_letohs(tvb, offset + 4);
1585 guid->data3 = tvb_get_letohs(tvb, offset + 6);
1586 tvb_memcpy(tvb, guid->data4, offset + 8, sizeof guid->data4);
1590 * NOTE: to support code written when proto_tree_add_item() took a
1591 * gboolean as its last argument, with FALSE meaning "big-endian"
1592 * and TRUE meaning "little-endian", we treat any non-zero value of
1593 * "representation" as meaning "little-endian".
1596 tvb_get_guid(tvbuff_t *tvb, const gint offset, e_guid_t *guid, const guint representation)
1598 if (representation) {
1599 tvb_get_letohguid(tvb, offset, guid);
1601 tvb_get_ntohguid(tvb, offset, guid);
1605 static const guint8 bit_mask8[] = {
1616 /* Bit offset mask for number of bits = 8 - 16 */
1617 static const guint16 bit_mask16[] = {
1628 /* Get 1 - 8 bits */
1630 tvb_get_bits8(tvbuff_t *tvb, gint bit_offset, const gint no_of_bits)
1637 DISSECTOR_ASSERT_NOT_REACHED();
1639 /* Byte align offset */
1640 offset = bit_offset>>3;
1642 /* Find out which mask to use for the most significant octet
1643 * by convering bit_offset into the offset into the first
1646 bit_offset = bit_offset & 0x7;
1647 tot_no_bits = bit_offset+no_of_bits;
1649 /* Read one octet, mask off bit_offset bits and left shift out the unused bits */
1650 value = tvb_get_guint8(tvb,offset) & bit_mask8[bit_offset];
1651 value = value >> (8-tot_no_bits);
1653 /* Read two octets, mask off bit_offset bits and left shift out the unused bits */
1654 value = tvb_get_ntohs(tvb,offset) & bit_mask16[bit_offset];
1655 value = value >> (16 - tot_no_bits);
1658 return (guint8)value;
1662 tvb_get_bits_buf(tvbuff_t *tvb, gint bit_offset, gint no_of_bits, guint8 *buf, gboolean lsb0)
1664 guint8 bit_mask, bit_shift;
1665 /* Byte align offset */
1666 gint offset = bit_offset >> 3;
1667 bit_offset = bit_offset & 0x7;
1669 bit_mask = (lsb0) ? 0xff : bit_mask8[bit_offset];
1670 bit_shift = (lsb0) ? bit_offset : (8 - bit_offset);
1672 if (G_LIKELY(bit_offset != 0)) {
1673 guint16 value = (guint16) tvb_get_guint8(tvb, offset);
1675 while (no_of_bits >= 8) {
1677 value = ((value & bit_mask) << 8) | tvb_get_guint8(tvb, offset);
1680 *buf++ = (guint8) (GUINT16_SWAP_LE_BE(value) >> bit_shift);
1682 *buf++ = (guint8) (value >> bit_shift);
1686 /* something left? */
1687 if (no_of_bits > 0) {
1688 guint8 tot_no_bits = bit_offset+no_of_bits;
1690 /* Overlaps with next byte? Get next byte */
1691 if (tot_no_bits > 8) {
1693 value = ((value & bit_mask) << 8) | tvb_get_guint8(tvb, offset);
1697 if (tot_no_bits > 8)
1698 value = (GUINT16_SWAP_LE_BE(value) >> bit_offset) & (bit_mask8[8-no_of_bits]);
1700 value = (value >> bit_offset) & (bit_mask8[8-no_of_bits]);
1702 /* value = (value & ((1 << tot_no_bits)-1)) >> bit_offset; */
1705 if (tot_no_bits > 8)
1706 value = value >> (16 - tot_no_bits);
1708 value = (value & bit_mask) >> (8-tot_no_bits);
1710 *buf = (guint8) value;
1714 /* fast code path for bit_offset == 0 */
1715 while (no_of_bits >= 8) {
1716 *buf++ = tvb_get_guint8(tvb, offset);
1721 /* something left? */
1722 if (no_of_bits > 0) {
1724 *buf = tvb_get_guint8(tvb, offset) & bit_mask8[8-no_of_bits]; /* read: ((1 << no_of_bits)-1) */
1726 *buf = tvb_get_guint8(tvb, offset) >> (8-no_of_bits);
1732 ep_tvb_get_bits(tvbuff_t *tvb, gint bit_offset, gint no_of_bits, gboolean lsb0)
1737 /* XXX, no_of_bits == -1 -> to end of tvb? */
1739 if (no_of_bits < 0 || bit_offset < 0) {
1740 DISSECTOR_ASSERT_NOT_REACHED();
1743 no_of_bytes = (no_of_bits >> 3) + ((no_of_bits & 0x7) != 0); /* ceil(no_of_bits / 8.0) */
1744 buf = ep_alloc(no_of_bytes);
1745 tvb_get_bits_buf(tvb, bit_offset, no_of_bits, buf, lsb0);
1749 /* Get 9 - 16 bits */
1750 /* Bit offset mask for number of bits = 16 - 32 */
1751 static const guint32 bit_mask32[] = {
1763 tvb_get_bits16(tvbuff_t *tvb, gint bit_offset, const gint no_of_bits,const gboolean little_endian)
1767 guint16 tempval = 0;
1770 if ((no_of_bits<=8)||(no_of_bits>16)) {
1771 /* If bits <= 8 use tvb_get_bits8 */
1772 DISSECTOR_ASSERT_NOT_REACHED();
1775 DISSECTOR_ASSERT_NOT_REACHED();
1776 /* This part is not implemented yet */
1779 /* Byte align offset */
1780 offset = bit_offset>>3;
1782 /* Find out which mask to use for the most significant octet
1783 * by convering bit_offset into the offset into the first
1786 bit_offset = bit_offset & 0x7;
1787 tot_no_bits = bit_offset+no_of_bits;
1788 /* Read two octets and mask off bit_offset bits */
1789 value = tvb_get_ntohs(tvb,offset) & bit_mask16[bit_offset];
1790 if(tot_no_bits < 16){
1791 /* Left shift out the unused bits */
1792 value = value >> (16 - tot_no_bits);
1793 }else if(tot_no_bits > 16){
1794 /* Spans three octets, read next octet and shift as needed */
1795 value = value << (tot_no_bits - 16);
1796 tempval = tvb_get_guint8(tvb,offset+2);
1797 tempval = tempval >> (24-tot_no_bits);
1798 value = value | tempval;
1804 /* Bit offset mask for number of bits = 32 - 64 */
1805 static const guint64 bit_mask64[] = {
1806 G_GINT64_CONSTANT(0xffffffffffffffffU),
1807 G_GINT64_CONSTANT(0x7fffffffffffffffU),
1808 G_GINT64_CONSTANT(0x3fffffffffffffffU),
1809 G_GINT64_CONSTANT(0x1fffffffffffffffU),
1810 G_GINT64_CONSTANT(0x0fffffffffffffffU),
1811 G_GINT64_CONSTANT(0x07ffffffffffffffU),
1812 G_GINT64_CONSTANT(0x03ffffffffffffffU),
1813 G_GINT64_CONSTANT(0x01ffffffffffffffU)
1817 tvb_get_bits32(tvbuff_t *tvb, gint bit_offset, const gint no_of_bits, const gboolean little_endian)
1821 guint32 tempval = 0;
1823 guint8 tot_no_octets = 0;
1827 if ((no_of_bits<=16)||(no_of_bits>32)) {
1828 /* If bits <= 16 use tvb_get_bits8 or tvb_get_bits16 */
1829 DISSECTOR_ASSERT_NOT_REACHED();
1832 DISSECTOR_ASSERT_NOT_REACHED();
1833 /* This part is not implemented yet */
1836 /* Byte align offset */
1837 offset = bit_offset>>3;
1839 bit_offset = bit_offset & 0x7;
1840 tot_no_bits = bit_offset+no_of_bits;
1841 tot_no_octets = tot_no_bits / 8;
1842 if (tot_no_bits % 8)
1844 shift = no_of_bits - (8 - bit_offset);
1846 value = tvb_get_guint8(tvb, offset) & bit_mask8[bit_offset];
1847 value = value << shift;
1849 for (i = 1; i < tot_no_octets; i++)
1852 tempval = tvb_get_guint8(tvb, offset+i);
1855 tempval = tempval << shift;
1859 tempval = tempval >> (- shift);
1861 value = value | tempval;
1868 tvb_get_bits64(tvbuff_t *tvb, gint bit_offset, const gint no_of_bits, const gboolean little_endian)
1872 guint64 tempval = 0;
1875 if ((no_of_bits<=32)||(no_of_bits>64)) {
1876 /* If bits <= 32 use tvb_get_bits8, tvb_get_bits16 or tvb_get_bits32 */
1877 DISSECTOR_ASSERT_NOT_REACHED();
1880 DISSECTOR_ASSERT_NOT_REACHED();
1881 /* This part is not implemented yet */
1884 /* Byte align offset */
1885 offset = bit_offset>>3;
1887 /* Find out which mask to use for the most significant octet
1888 * by convering bit_offset into the offset into the first
1891 bit_offset = bit_offset & 0x7;
1892 tot_no_bits = bit_offset+no_of_bits;
1893 /* Read eight octets and mask off bit_offset bits */
1894 value = tvb_get_ntoh64(tvb,offset) & bit_mask64[bit_offset];
1895 if (tot_no_bits < 64){
1896 /* Left shift out the unused bits */
1897 value = value >> (64 - tot_no_bits);
1898 }else if (tot_no_bits > 64){
1899 /* Spans nine octets, read next octet and shift as needed */
1900 value = value << (tot_no_bits - 64);
1901 tempval = tvb_get_guint8(tvb,offset+8);
1902 tempval = tempval >> (72-tot_no_bits);
1903 value = value | tempval;
1910 tvb_get_bits(tvbuff_t *tvb, const gint bit_offset, const gint no_of_bits, const gboolean little_endian)
1912 /* This function can handle only up to 32 requested bits */
1913 if (no_of_bits > 32)
1914 DISSECTOR_ASSERT_NOT_REACHED();
1916 if (no_of_bits == 0)
1919 /* Number of requested bits is in range [17, 32] */
1920 if (no_of_bits > 16)
1921 return tvb_get_bits32(tvb, bit_offset, no_of_bits, little_endian);
1923 /* Number of requested bits is in range [9, 16] */
1925 return tvb_get_bits16(tvb, bit_offset, no_of_bits, little_endian);
1927 /* Number of requested bits is in range [1, 8] */
1928 return tvb_get_bits8(tvb, bit_offset, no_of_bits);
1931 /* Find first occurence of needle in tvbuff, starting at offset. Searches
1932 * at most maxlength number of bytes; if maxlength is -1, searches to
1934 * Returns the offset of the found needle, or -1 if not found.
1935 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1936 * in that case, -1 will be returned if the boundary is reached before
1937 * finding needle. */
1939 tvb_find_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 needle)
1941 const guint8 *result;
1942 guint abs_offset, junk_length;
1946 DISSECTOR_ASSERT(tvb && tvb->initialized);
1948 check_offset_length(tvb->length, tvb->reported_length, offset, 0, &abs_offset, &junk_length);
1950 /* Only search to end of tvbuff, w/o throwing exception. */
1951 tvbufflen = tvb_length_remaining(tvb, abs_offset);
1952 if (maxlength == -1) {
1953 /* No maximum length specified; search to end of tvbuff. */
1956 else if (tvbufflen < (guint) maxlength) {
1957 /* Maximum length goes past end of tvbuff; search to end
1962 /* Maximum length doesn't go past end of tvbuff; search
1967 /* If we have real data, perform our search now. */
1968 if (tvb->real_data) {
1969 result = memchr(tvb->real_data + abs_offset, needle, limit);
1970 if (result == NULL) {
1974 return (gint) (result - tvb->real_data);
1979 case TVBUFF_REAL_DATA:
1980 DISSECTOR_ASSERT_NOT_REACHED();
1983 return tvb_find_guint8(tvb->tvbuffs.subset.tvb,
1984 abs_offset - tvb->tvbuffs.subset.offset,
1987 case TVBUFF_COMPOSITE:
1988 DISSECTOR_ASSERT_NOT_REACHED();
1989 /* XXX - return composite_find_guint8(tvb, offset, limit, needle); */
1992 DISSECTOR_ASSERT_NOT_REACHED();
1996 /* Find first occurence of any of the needles in tvbuff, starting at offset.
1997 * Searches at most maxlength number of bytes; if maxlength is -1, searches
1999 * Returns the offset of the found needle, or -1 if not found.
2000 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
2001 * in that case, -1 will be returned if the boundary is reached before
2002 * finding needle. */
2004 tvb_pbrk_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 *needles, guchar *found_needle)
2006 const guint8 *result;
2007 guint abs_offset, junk_length;
2011 DISSECTOR_ASSERT(tvb && tvb->initialized);
2013 check_offset_length(tvb->length, tvb->reported_length, offset, 0, &abs_offset, &junk_length);
2015 /* Only search to end of tvbuff, w/o throwing exception. */
2016 tvbufflen = tvb_length_remaining(tvb, abs_offset);
2017 if (maxlength == -1) {
2018 /* No maximum length specified; search to end of tvbuff. */
2021 else if (tvbufflen < (guint) maxlength) {
2022 /* Maximum length goes past end of tvbuff; search to end
2027 /* Maximum length doesn't go past end of tvbuff; search
2032 /* If we have real data, perform our search now. */
2033 if (tvb->real_data) {
2034 result = guint8_pbrk(tvb->real_data + abs_offset, limit, needles, found_needle);
2035 if (result == NULL) {
2039 return (gint) (result - tvb->real_data);
2044 case TVBUFF_REAL_DATA:
2045 DISSECTOR_ASSERT_NOT_REACHED();
2048 return tvb_pbrk_guint8(tvb->tvbuffs.subset.tvb,
2049 abs_offset - tvb->tvbuffs.subset.offset,
2050 limit, needles, found_needle);
2052 case TVBUFF_COMPOSITE:
2053 DISSECTOR_ASSERT_NOT_REACHED();
2054 /* XXX - return composite_pbrk_guint8(tvb, offset, limit, needle); */
2057 DISSECTOR_ASSERT_NOT_REACHED();
2061 /* Find size of stringz (NUL-terminated string) by looking for terminating
2062 * NUL. The size of the string includes the terminating NUL.
2064 * If the NUL isn't found, it throws the appropriate exception.
2067 tvb_strsize(tvbuff_t *tvb, const gint offset)
2069 guint abs_offset, junk_length;
2072 DISSECTOR_ASSERT(tvb && tvb->initialized);
2074 check_offset_length(tvb->length, tvb->reported_length, offset, 0, &abs_offset, &junk_length);
2075 nul_offset = tvb_find_guint8(tvb, abs_offset, -1, 0);
2076 if (nul_offset == -1) {
2078 * OK, we hit the end of the tvbuff, so we should throw
2081 * Did we hit the end of the captured data, or the end
2082 * of the actual data? If there's less captured data
2083 * than actual data, we presumably hit the end of the
2084 * captured data, otherwise we hit the end of the actual
2087 if (tvb_length(tvb) < tvb_reported_length(tvb)) {
2090 THROW(ReportedBoundsError);
2093 return (nul_offset - abs_offset) + 1;
2096 /* Unicode (UTF-16) version of tvb_strsize */
2097 /* Returns number of *UTF-16 characters* (not bytes) excluding the null terminator */
2099 tvb_unicode_strsize(tvbuff_t *tvb, const gint offset)
2104 DISSECTOR_ASSERT(tvb && tvb->initialized);
2107 /* Endianness doesn't matter when looking for null */
2108 uchar = tvb_get_ntohs(tvb, offset + i);
2110 } while(uchar != 0);
2112 return i; /* Number of *UTF-16* characters */
2115 /* Find length of string by looking for end of string ('\0'), up to
2116 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
2118 * Returns -1 if 'maxlength' reached before finding EOS. */
2120 tvb_strnlen(tvbuff_t *tvb, const gint offset, const guint maxlength)
2123 guint abs_offset, junk_length;
2125 DISSECTOR_ASSERT(tvb && tvb->initialized);
2127 check_offset_length(tvb->length, tvb->reported_length, offset, 0, &abs_offset, &junk_length);
2129 result_offset = tvb_find_guint8(tvb, abs_offset, maxlength, 0);
2131 if (result_offset == -1) {
2135 return result_offset - abs_offset;
2140 * Implement strneql etc
2144 * Call strncmp after checking if enough chars left, returning 0 if
2145 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
2148 tvb_strneql(tvbuff_t *tvb, const gint offset, const gchar *str, const size_t size)
2152 ptr = ensure_contiguous_no_exception(tvb, offset, (gint)size, NULL);
2155 int cmp = strncmp((const char *)ptr, str, size);
2158 * Return 0 if equal, -1 otherwise.
2160 return (cmp == 0 ? 0 : -1);
2163 * Not enough characters in the tvbuff to match the
2171 * Call g_ascii_strncasecmp after checking if enough chars left, returning
2172 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
2175 tvb_strncaseeql(tvbuff_t *tvb, const gint offset, const gchar *str, const size_t size)
2179 ptr = ensure_contiguous_no_exception(tvb, offset, (gint)size, NULL);
2182 int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
2185 * Return 0 if equal, -1 otherwise.
2187 return (cmp == 0 ? 0 : -1);
2190 * Not enough characters in the tvbuff to match the
2198 * Call memcmp after checking if enough chars left, returning 0 if
2199 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
2202 tvb_memeql(tvbuff_t *tvb, const gint offset, const guint8 *str, size_t size)
2206 ptr = ensure_contiguous_no_exception(tvb, offset, (gint) size, NULL);
2209 int cmp = memcmp(ptr, str, size);
2212 * Return 0 if equal, -1 otherwise.
2214 return (cmp == 0 ? 0 : -1);
2217 * Not enough characters in the tvbuff to match the
2224 /* Convert a string from Unicode to ASCII. At the moment we fake it by
2225 * replacing all non-ASCII characters with a '.' )-: The caller must
2226 * free the result returned. The len parameter is the number of guint16's
2227 * to convert from Unicode. */
2228 /* XXX - this function has been superceded by tvb_get_unicode_string() */
2230 tvb_fake_unicode(tvbuff_t *tvb, int offset, const int len, const gboolean little_endian)
2236 /* Make sure we have enough data before allocating the buffer,
2237 so we don't blow up if the length is huge. */
2238 tvb_ensure_bytes_exist(tvb, offset, 2*len);
2240 /* We know we won't throw an exception, so we don't have to worry
2241 about leaking this buffer. */
2242 buffer = g_malloc(len + 1);
2244 for (i = 0; i < len; i++) {
2245 character = little_endian ? tvb_get_letohs(tvb, offset)
2246 : tvb_get_ntohs(tvb, offset);
2247 buffer[i] = character < 256 ? character : '.';
2256 /* Convert a string from Unicode to ASCII. At the moment we fake it by
2257 * replacing all non-ASCII characters with a '.' )-: The len parameter is
2258 * the number of guint16's to convert from Unicode.
2260 * This function allocates memory from a buffer with packet lifetime.
2261 * You do not have to free this buffer, it will be automatically freed
2262 * when wireshark starts decoding the next packet.
2265 tvb_get_ephemeral_faked_unicode(tvbuff_t *tvb, int offset, const int len, const gboolean little_endian)
2271 /* Make sure we have enough data before allocating the buffer,
2272 so we don't blow up if the length is huge. */
2273 tvb_ensure_bytes_exist(tvb, offset, 2*len);
2275 /* We know we won't throw an exception, so we don't have to worry
2276 about leaking this buffer. */
2277 buffer = ep_alloc(len + 1);
2279 for (i = 0; i < len; i++) {
2280 character = little_endian ? tvb_get_letohs(tvb, offset)
2281 : tvb_get_ntohs(tvb, offset);
2282 buffer[i] = character < 256 ? character : '.';
2292 * Format the data in the tvb from offset for length ...
2296 tvb_format_text(tvbuff_t *tvb, const gint offset, const gint size)
2301 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
2302 len = tvb_length_remaining(tvb, offset);
2303 ptr = ensure_contiguous(tvb, offset, len);
2306 return format_text(ptr, len);
2310 * Format the data in the tvb from offset for length ...
2314 tvb_format_text_wsp(tvbuff_t *tvb, const gint offset, const gint size)
2319 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
2321 len = tvb_length_remaining(tvb, offset);
2322 ptr = ensure_contiguous(tvb, offset, len);
2326 return format_text_wsp(ptr, len);
2331 * Like "tvb_format_text()", but for null-padded strings; don't show
2332 * the null padding characters as "\000".
2335 tvb_format_stringzpad(tvbuff_t *tvb, const gint offset, const gint size)
2337 const guint8 *ptr, *p;
2341 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
2343 len = tvb_length_remaining(tvb, offset);
2344 ptr = ensure_contiguous(tvb, offset, len);
2348 for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
2350 return format_text(ptr, stringlen);
2355 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
2356 * the null padding characters as "\000".
2359 tvb_format_stringzpad_wsp(tvbuff_t *tvb, const gint offset, const gint size)
2361 const guint8 *ptr, *p;
2365 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
2367 len = tvb_length_remaining(tvb, offset);
2368 ptr = ensure_contiguous(tvb, offset, len);
2372 for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
2374 return format_text_wsp(ptr, stringlen);
2379 * Given a tvbuff, an offset, and a length, allocate a buffer big enough
2380 * to hold a non-null-terminated string of that length at that offset,
2381 * plus a trailing '\0', copy the string into it, and return a pointer
2384 * Throws an exception if the tvbuff ends before the string does.
2387 tvb_get_string(tvbuff_t *tvb, const gint offset, const gint length)
2390 guint8 *strbuf = NULL;
2392 tvb_ensure_bytes_exist(tvb, offset, length);
2394 ptr = ensure_contiguous(tvb, offset, length);
2395 strbuf = g_malloc(length + 1);
2397 memcpy(strbuf, ptr, length);
2399 strbuf[length] = '\0';
2404 * Unicode (UTF-16) version of tvb_get_string()
2406 * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
2408 * Specify length in bytes
2410 * Returns an UTF-8 string that must be freed by the caller
2413 tvb_get_unicode_string(tvbuff_t *tvb, const gint offset, gint length, const guint encoding)
2415 gchar *tmpbuf = NULL;
2417 gint i; /* Byte counter for tvbuff */
2419 GString *strbuf = NULL;
2421 strbuf = g_string_new(NULL);
2423 for(i = 0; i < length; i += 2) {
2425 if(encoding == ENC_BIG_ENDIAN)
2426 uchar = tvb_get_ntohs(tvb, offset + i);
2428 uchar = tvb_get_letohs(tvb, offset + i);
2430 /* Calculate how much space is needed to store UTF-16 character
2432 tmpbuf_len = g_unichar_to_utf8(uchar, NULL);
2434 tmpbuf = g_malloc(tmpbuf_len + 1); /* + 1 to make room for null
2437 g_unichar_to_utf8(uchar, tmpbuf);
2439 /* NULL terminate the tmpbuf so g_string_append knows where
2441 tmpbuf[tmpbuf_len] = '\0';
2443 g_string_append(strbuf, tmpbuf);
2448 return g_string_free(strbuf, FALSE);
2452 * Given a tvbuff, an offset, a length, and an encoding, allocate a
2453 * buffer big enough to hold a non-null-terminated string of that length
2454 * at that offset, plus a trailing '\0', copy the string into it, and
2455 * return a pointer to the string; if the encoding is EBCDIC, map
2456 * the string from EBCDIC to ASCII.
2458 * Throws an exception if the tvbuff ends before the string does.
2460 * This function allocates memory from a buffer with packet lifetime.
2461 * You do not have to free this buffer, it will be automatically freed
2462 * when wireshark starts decoding the next packet.
2463 * Do not use this function if you want the allocated memory to be persistent
2464 * after the current packet has been dissected.
2467 tvb_get_ephemeral_string_enc(tvbuff_t *tvb, const gint offset,
2468 const gint length, const gint encoding)
2471 guint8 *strbuf = NULL;
2473 tvb_ensure_bytes_exist(tvb, offset, length);
2475 ptr = ensure_contiguous(tvb, offset, length);
2476 strbuf = ep_alloc(length + 1);
2478 memcpy(strbuf, ptr, length);
2480 if ((encoding & ENC_CHARENCODING_MASK) == ENC_EBCDIC)
2481 EBCDIC_to_ASCII(strbuf, length);
2482 strbuf[length] = '\0';
2487 tvb_get_ephemeral_string(tvbuff_t *tvb, const gint offset,
2490 return tvb_get_ephemeral_string_enc(tvb, offset, length, ENC_UTF_8|ENC_NA);
2494 * Unicode (UTF-16) version of tvb_get_ephemeral_string()
2496 * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
2498 * Specify length in bytes
2500 * Returns an ep_ allocated UTF-8 string
2503 tvb_get_ephemeral_unicode_string(tvbuff_t *tvb, const gint offset, gint length, const guint encoding)
2505 gchar *tmpbuf = NULL;
2507 gint i; /* Byte counter for tvbuff */
2509 emem_strbuf_t *strbuf = NULL;
2511 strbuf = ep_strbuf_new(NULL);
2513 for(i = 0; i < length; i += 2) {
2515 if(encoding == ENC_BIG_ENDIAN)
2516 uchar = tvb_get_ntohs(tvb, offset + i);
2518 uchar = tvb_get_letohs(tvb, offset + i);
2520 /* Calculate how much space is needed to store UTF-16 character
2522 tmpbuf_len = g_unichar_to_utf8(uchar, NULL);
2524 tmpbuf = g_malloc(tmpbuf_len + 1); /* + 1 to make room for null
2527 g_unichar_to_utf8(uchar, tmpbuf);
2529 /* NULL terminate the tmpbuf so ep_strbuf_append knows where
2531 tmpbuf[tmpbuf_len] = '\0';
2533 ep_strbuf_append(strbuf, tmpbuf);
2542 * Given a tvbuff, an offset, and a length, allocate a buffer big enough
2543 * to hold a non-null-terminated string of that length at that offset,
2544 * plus a trailing '\0', copy the string into it, and return a pointer
2547 * Throws an exception if the tvbuff ends before the string does.
2549 * This function allocates memory from a buffer with capture session lifetime.
2550 * You do not have to free this buffer, it will be automatically freed
2551 * when wireshark starts or opens a new capture.
2554 tvb_get_seasonal_string(tvbuff_t *tvb, const gint offset, const gint length)
2557 guint8 *strbuf = NULL;
2559 tvb_ensure_bytes_exist(tvb, offset, length);
2561 ptr = ensure_contiguous(tvb, offset, length);
2562 strbuf = se_alloc(length + 1);
2564 memcpy(strbuf, ptr, length);
2566 strbuf[length] = '\0';
2571 * Given a tvbuff, an offset, and an encoding, with the offset assumed
2572 * to refer to a null-terminated string, find the length of that string
2573 * (and throw an exception if the tvbuff ends before we find the null),
2574 * allocate a buffer big enough to hold the string, copy the string into
2575 * it, and return a pointer to the string; if the encoding is EBCDIC, map
2576 * the string from EBCDIC to ASCII. Also return the length of the
2577 * string (including the terminating null) through a pointer.
2580 tvb_get_stringz_enc(tvbuff_t *tvb, const gint offset, gint *lengthp, gint encoding)
2585 size = tvb_strsize(tvb, offset);
2586 strptr = g_malloc(size);
2587 tvb_memcpy(tvb, strptr, offset, size);
2588 if ((encoding & ENC_CHARENCODING_MASK) == ENC_EBCDIC)
2589 EBCDIC_to_ASCII(strptr, size);
2596 tvb_get_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
2598 return tvb_get_stringz_enc(tvb, offset, lengthp, ENC_UTF_8|ENC_NA);
2602 * Given a tvbuff and an offset, with the offset assumed to refer to
2603 * a null-terminated string, find the length of that string (and throw
2604 * an exception if the tvbuff ends before we find the null), ensure that
2605 * the TVB is flat, and return a pointer to the string (in the TVB).
2606 * Also return the length of the string (including the terminating null)
2607 * through a pointer.
2609 * As long as we aren't using composite TVBs, this saves the cycles used
2610 * (often unnecessariliy) in allocating a buffer and copying the string into
2611 * it. (If we do start using composite TVBs, we may want to replace this
2612 * function with the _ephemeral versoin.)
2615 tvb_get_const_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
2618 const guint8 *strptr;
2620 size = tvb_strsize(tvb, offset);
2621 strptr = ensure_contiguous(tvb, offset, size);
2628 * Given a tvbuff and an offset, with the offset assumed to refer to
2629 * a null-terminated string, find the length of that string (and throw
2630 * an exception if the tvbuff ends before we find the null), allocate
2631 * a buffer big enough to hold the string, copy the string into it,
2632 * and return a pointer to the string. Also return the length of the
2633 * string (including the terminating null) through a pointer.
2635 * This function allocates memory from a buffer with packet lifetime.
2636 * You do not have to free this buffer, it will be automatically freed
2637 * when wireshark starts decoding the next packet.
2638 * Do not use this function if you want the allocated memory to be persistent
2639 * after the current packet has been dissected.
2642 tvb_get_ephemeral_stringz_enc(tvbuff_t *tvb, const gint offset, gint *lengthp, gint encoding)
2647 size = tvb_strsize(tvb, offset);
2648 strptr = ep_alloc(size);
2649 tvb_memcpy(tvb, strptr, offset, size);
2650 if ((encoding & ENC_CHARENCODING_MASK) == ENC_EBCDIC)
2651 EBCDIC_to_ASCII(strptr, size);
2658 tvb_get_ephemeral_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
2660 return tvb_get_ephemeral_stringz_enc(tvb, offset, lengthp, ENC_UTF_8|ENC_NA);
2664 * Unicode (UTF-16) version of tvb_get_ephemeral_stringz()
2666 * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
2668 * Returns an ep_ allocated UTF-8 string and updates lengthp pointer with length of string (in bytes)
2671 tvb_get_ephemeral_unicode_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
2673 gchar *tmpbuf = NULL;
2675 gint size; /* Number of UTF-16 characters */
2676 gint i; /* Byte counter for tvbuff */
2678 emem_strbuf_t *strbuf = NULL;
2680 strbuf = ep_strbuf_new(NULL);
2682 size = tvb_unicode_strsize(tvb, offset);
2684 for(i = 0; i < size; i += 2) {
2686 if(encoding == ENC_BIG_ENDIAN)
2687 uchar = tvb_get_ntohs(tvb, offset + i);
2689 uchar = tvb_get_letohs(tvb, offset + i);
2691 /* Calculate how much space is needed to store UTF-16 character
2693 tmpbuf_len = g_unichar_to_utf8(uchar, NULL);
2695 tmpbuf = g_malloc(tmpbuf_len + 1); /* + 1 to make room for null
2698 g_unichar_to_utf8(uchar, tmpbuf);
2700 /* NULL terminate the tmpbuf so ep_strbuf_append knows where
2702 tmpbuf[tmpbuf_len] = '\0';
2704 ep_strbuf_append(strbuf, tmpbuf);
2710 *lengthp = i; /* Number of *bytes* processed */
2716 * Given a tvbuff and an offset, with the offset assumed to refer to
2717 * a null-terminated string, find the length of that string (and throw
2718 * an exception if the tvbuff ends before we find the null), allocate
2719 * a buffer big enough to hold the string, copy the string into it,
2720 * and return a pointer to the string. Also return the length of the
2721 * string (including the terminating null) through a pointer.
2723 * This function allocates memory from a buffer with capture session lifetime.
2724 * You do not have to free this buffer, it will be automatically freed
2725 * when wireshark starts or opens a new capture.
2728 tvb_get_seasonal_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
2733 size = tvb_strsize(tvb, offset);
2734 strptr = se_alloc(size);
2735 tvb_memcpy(tvb, strptr, offset, size);
2741 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2742 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2743 * Returns length of string (not including terminating NUL), or -1 if the string was
2744 * truncated in the buffer due to not having reached the terminating NUL.
2745 * In this way, it acts like g_snprintf().
2747 * bufsize MUST be greater than 0.
2749 * When processing a packet where the remaining number of bytes is less
2750 * than bufsize, an exception is not thrown if the end of the packet
2751 * is reached before the NUL is found. If no NUL is found before reaching
2752 * the end of the short packet, -1 is still returned, and the string
2753 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2754 * at the correct spot, terminating the string.
2756 * *bytes_copied will contain the number of bytes actually copied,
2757 * including the terminating-NUL.
2760 _tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer,
2764 guint abs_offset, junk_length;
2766 gboolean decreased_max = FALSE;
2768 check_offset_length(tvb->length, tvb->reported_length, offset, 0, &abs_offset, &junk_length);
2770 /* There must at least be room for the terminating NUL. */
2771 DISSECTOR_ASSERT(bufsize != 0);
2773 /* If there's no room for anything else, just return the NUL. */
2780 /* Only read to end of tvbuff, w/o throwing exception. */
2781 len = tvb_length_remaining(tvb, abs_offset);
2783 /* check_offset_length() won't throw an exception if we're
2784 * looking at the byte immediately after the end of the tvbuff. */
2786 THROW(ReportedBoundsError);
2789 /* This should not happen because check_offset_length() would
2790 * have already thrown an exception if 'offset' were out-of-bounds.
2792 DISSECTOR_ASSERT(len != -1);
2795 * If we've been passed a negative number, bufsize will
2798 DISSECTOR_ASSERT(bufsize <= G_MAXINT);
2800 if ((guint)len < bufsize) {
2802 decreased_max = TRUE;
2808 stringlen = tvb_strnlen(tvb, abs_offset, limit - 1);
2809 /* If NUL wasn't found, copy the data and return -1 */
2810 if (stringlen == -1) {
2811 tvb_memcpy(tvb, buffer, abs_offset, limit);
2812 if (decreased_max) {
2814 /* Add 1 for the extra NUL that we set at buffer[limit],
2815 * pretending that it was copied as part of the string. */
2816 *bytes_copied = limit + 1;
2819 *bytes_copied = limit;
2824 /* Copy the string to buffer */
2825 tvb_memcpy(tvb, buffer, abs_offset, stringlen + 1);
2826 *bytes_copied = stringlen + 1;
2830 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2831 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2832 * Returns length of string (not including terminating NUL), or -1 if the string was
2833 * truncated in the buffer due to not having reached the terminating NUL.
2834 * In this way, it acts like g_snprintf().
2836 * When processing a packet where the remaining number of bytes is less
2837 * than bufsize, an exception is not thrown if the end of the packet
2838 * is reached before the NUL is found. If no NUL is found before reaching
2839 * the end of the short packet, -1 is still returned, and the string
2840 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2841 * at the correct spot, terminating the string.
2844 tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer)
2848 DISSECTOR_ASSERT(tvb && tvb->initialized);
2850 return _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
2853 /* Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
2854 * have a terminating NUL. If the string was truncated when copied into buffer,
2855 * a NUL is placed at the end of buffer to terminate it.
2858 tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer)
2860 gint len, bytes_copied;
2862 DISSECTOR_ASSERT(tvb && tvb->initialized);
2864 len = _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
2867 buffer[bufsize - 1] = 0;
2868 return bytes_copied - 1;
2876 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2877 * at that offset (which may be -1 for "all the way to the end of the
2878 * tvbuff"), find the end of the (putative) line that starts at the
2879 * specified offset in the tvbuff, going no further than the specified
2882 * Return the length of the line (not counting the line terminator at
2883 * the end), or, if we don't find a line terminator:
2885 * if "deseg" is true, return -1;
2887 * if "deseg" is false, return the amount of data remaining in
2890 * Set "*next_offset" to the offset of the character past the line
2891 * terminator, or past the end of the buffer if we don't find a line
2892 * terminator. (It's not set if we return -1.)
2895 tvb_find_line_end(tvbuff_t *tvb, const gint offset, int len, gint *next_offset,
2896 const gboolean desegment)
2901 guchar found_needle = 0;
2904 len = tvb_length_remaining(tvb, offset);
2906 * XXX - what if "len" is still -1, meaning "offset is past the
2907 * end of the tvbuff"?
2909 eob_offset = offset + len;
2912 * Look either for a CR or an LF.
2914 eol_offset = tvb_pbrk_guint8(tvb, offset, len, (const guint8 *)"\r\n", &found_needle);
2915 if (eol_offset == -1) {
2917 * No CR or LF - line is presumably continued in next packet.
2921 * Tell our caller we saw no EOL, so they can
2922 * try to desegment and get the entire line
2928 * Pretend the line runs to the end of the tvbuff.
2930 linelen = eob_offset - offset;
2932 *next_offset = eob_offset;
2936 * Find the number of bytes between the starting offset
2939 linelen = eol_offset - offset;
2944 if (found_needle == '\r') {
2946 * Yes - is it followed by an LF?
2948 if (eol_offset + 1 >= eob_offset) {
2950 * Dunno - the next byte isn't in this
2955 * We'll return -1, although that
2956 * runs the risk that if the line
2957 * really *is* terminated with a CR,
2958 * we won't properly dissect this
2961 * It's probably more likely that
2962 * the line ends with CR-LF than
2963 * that it ends with CR by itself.
2969 * Well, we can at least look at the next
2972 if (tvb_get_guint8(tvb, eol_offset + 1) == '\n') {
2974 * It's an LF; skip over the CR.
2982 * Return the offset of the character after the last
2983 * character in the line, skipping over the last character
2984 * in the line terminator.
2987 *next_offset = eol_offset + 1;
2993 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2994 * at that offset (which may be -1 for "all the way to the end of the
2995 * tvbuff"), find the end of the (putative) line that starts at the
2996 * specified offset in the tvbuff, going no further than the specified
2999 * However, treat quoted strings inside the buffer specially - don't
3000 * treat newlines in quoted strings as line terminators.
3002 * Return the length of the line (not counting the line terminator at
3003 * the end), or the amount of data remaining in the buffer if we don't
3004 * find a line terminator.
3006 * Set "*next_offset" to the offset of the character past the line
3007 * terminator, or past the end of the buffer if we don't find a line
3011 tvb_find_line_end_unquoted(tvbuff_t *tvb, const gint offset, int len,
3014 gint cur_offset, char_offset;
3021 len = tvb_length_remaining(tvb, offset);
3023 * XXX - what if "len" is still -1, meaning "offset is past the
3024 * end of the tvbuff"?
3026 eob_offset = offset + len;
3028 cur_offset = offset;
3032 * Is this part of the string quoted?
3036 * Yes - look only for the terminating quote.
3038 char_offset = tvb_find_guint8(tvb, cur_offset, len,
3042 * Look either for a CR, an LF, or a '"'.
3044 char_offset = tvb_pbrk_guint8(tvb, cur_offset, len,
3045 (const guint8 *)"\r\n\"", &c);
3047 if (char_offset == -1) {
3049 * Not found - line is presumably continued in
3051 * We pretend the line runs to the end of the tvbuff.
3053 linelen = eob_offset - offset;
3055 *next_offset = eob_offset;
3061 * We're processing a quoted string.
3062 * We only looked for ", so we know it's a ";
3063 * as we're processing a quoted string, it's a
3073 * Un-quoted "; it begins a quoted
3079 * It's a CR or LF; we've found a line
3082 * Find the number of bytes between the
3083 * starting offset and the CR or LF.
3085 linelen = char_offset - offset;
3092 * Yes; is it followed by an LF?
3094 if (char_offset + 1 < eob_offset &&
3095 tvb_get_guint8(tvb, char_offset + 1)
3098 * Yes; skip over the CR.
3105 * Return the offset of the character after
3106 * the last character in the line, skipping
3107 * over the last character in the line
3108 * terminator, and quit.
3111 *next_offset = char_offset + 1;
3117 * Step past the character we found.
3119 cur_offset = char_offset + 1;
3120 if (cur_offset >= eob_offset) {
3122 * The character we found was the last character
3123 * in the tvbuff - line is presumably continued in
3125 * We pretend the line runs to the end of the tvbuff.
3127 linelen = eob_offset - offset;
3129 *next_offset = eob_offset;
3137 * Copied from the mgcp dissector. (This function should be moved to /epan )
3138 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
3139 * character following offset or offset + maxlength -1 whichever
3143 * tvb - The tvbuff in which we are skipping whitespace.
3144 * offset - The offset in tvb from which we begin trying to skip whitespace.
3145 * maxlength - The maximum distance from offset that we may try to skip
3148 * Returns: The position in tvb of the first non-whitespace
3149 * character following offset or offset + maxlength -1 whichever
3152 gint tvb_skip_wsp(tvbuff_t* tvb, const gint offset, const gint maxlength)
3154 gint counter = offset;
3158 /* Get the length remaining */
3159 tvb_len = tvb_length(tvb);
3160 end = offset + maxlength;
3166 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
3167 for (counter = offset;
3169 ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
3170 tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
3176 gint tvb_skip_wsp_return(tvbuff_t* tvb, const gint offset){
3177 gint counter = offset;
3182 for(counter = offset; counter > end &&
3183 ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
3184 tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
3191 * Format a bunch of data from a tvbuff as bytes, returning a pointer
3192 * to the string with the formatted data, with "punct" as a byte
3196 tvb_bytes_to_str_punct(tvbuff_t *tvb, const gint offset, const gint len, const gchar punct)
3198 return bytes_to_str_punct(ensure_contiguous(tvb, offset, len), len, punct);
3203 * Given a tvbuff, an offset into the tvbuff, and a length that starts
3204 * at that offset (which may be -1 for "all the way to the end of the
3205 * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
3206 * the low or high half byte, formating the digits according to an input digit set,
3207 * if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
3208 * A pointer to the EP allocated string will be returned.
3209 * Note a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
3211 static dgt_set_t Dgt1_9_bcd = {
3213 /* 0 1 2 3 4 5 6 7 8 9 a b c d e */
3214 '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?'
3218 tvb_bcd_dig_to_ep_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first)
3224 gint t_offset = offset;
3230 length = tvb_length(tvb);
3231 if (length < offset){
3235 length = offset + len;
3237 digit_str = ep_alloc((length - offset)*2+1);
3239 while ( t_offset < length ){
3241 octet = tvb_get_guint8(tvb,t_offset);
3243 digit_str[i] = dgt->out[octet & 0x0f];
3249 * unpack second value in byte
3253 if (octet == 0x0f) /* odd number bytes - hit filler */
3256 digit_str[i] = dgt->out[octet & 0x0f];
3267 * Format a bunch of data from a tvbuff as bytes, returning a pointer
3268 * to the string with the formatted data.
3271 tvb_bytes_to_str(tvbuff_t *tvb, const gint offset, const gint len)
3273 return bytes_to_str(ensure_contiguous(tvb, offset, len), len);
3276 /* Find a needle tvbuff within a haystack tvbuff. */
3278 tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const gint haystack_offset)
3280 guint haystack_abs_offset, haystack_abs_length;
3281 const guint8 *haystack_data;
3282 const guint8 *needle_data;
3283 const guint needle_len = needle_tvb->length;
3284 const guint8 *location;
3286 DISSECTOR_ASSERT(haystack_tvb && haystack_tvb->initialized);
3288 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
3292 /* Get pointers to the tvbuffs' data. */
3293 haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
3294 needle_data = ensure_contiguous(needle_tvb, 0, -1);
3296 check_offset_length(haystack_tvb->length, haystack_tvb->reported_length, haystack_offset, -1,
3297 &haystack_abs_offset, &haystack_abs_length);
3299 location = epan_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
3300 needle_data, needle_len);
3303 return (gint) (location - haystack_data);
3311 * Uncompresses a zlib compressed packet inside a message of tvb at offset with
3312 * length comprlen. Returns an uncompressed tvbuffer if uncompression
3313 * succeeded or NULL if uncompression failed.
3315 #define TVB_Z_MIN_BUFSIZ 32768
3316 #define TVB_Z_MAX_BUFSIZ 1048576 * 10
3317 /* #define TVB_Z_DEBUG 1 */
3321 tvb_uncompress(tvbuff_t *tvb, const int offset, int comprlen)
3324 guint bytes_out = 0;
3325 guint8 *compr = NULL;
3326 guint8 *uncompr = NULL;
3327 tvbuff_t *uncompr_tvb = NULL;
3328 z_streamp strm = NULL;
3329 Bytef *strmbuf = NULL;
3330 guint inits_done = 0;
3331 gint wbits = MAX_WBITS;
3332 guint8 *next = NULL;
3333 guint bufsiz = TVB_Z_MIN_BUFSIZ;
3335 guint inflate_passes = 0;
3336 guint bytes_in = tvb_length_remaining(tvb, offset);
3343 compr = tvb_memdup(tvb, offset, comprlen);
3349 * Assume that the uncompressed data is at least twice as big as
3350 * the compressed size.
3352 bufsiz = tvb_length_remaining(tvb, offset) * 2;
3353 bufsiz = CLAMP(bufsiz, TVB_Z_MIN_BUFSIZ, TVB_Z_MAX_BUFSIZ);
3356 printf("bufsiz: %u bytes\n", bufsiz);
3361 strm = g_new0(z_stream, 1);
3362 strm->next_in = next;
3363 strm->avail_in = comprlen;
3365 strmbuf = g_malloc0(bufsiz);
3366 strm->next_out = strmbuf;
3367 strm->avail_out = bufsiz;
3369 err = inflateInit2(strm, wbits);
3380 memset(strmbuf, '\0', bufsiz);
3381 strm->next_out = strmbuf;
3382 strm->avail_out = bufsiz;
3384 err = inflate(strm, Z_SYNC_FLUSH);
3386 if (err == Z_OK || err == Z_STREAM_END) {
3387 guint bytes_pass = bufsiz - strm->avail_out;
3393 if (uncompr == NULL) {
3394 uncompr = g_memdup(strmbuf, bytes_pass);
3396 guint8 *new_data = g_malloc0(bytes_out + bytes_pass);
3398 g_memmove(new_data, uncompr, bytes_out);
3399 g_memmove((new_data + bytes_out), strmbuf,
3406 bytes_out += bytes_pass;
3408 if ( err == Z_STREAM_END) {
3414 } else if (err == Z_BUF_ERROR) {
3416 * It's possible that not enough frames were captured
3417 * to decompress this fully, so return what we've done
3424 if (uncompr != NULL) {
3431 } else if (err == Z_DATA_ERROR && inits_done == 1
3432 && uncompr == NULL && (*compr == 0x1f) &&
3433 (*(compr + 1) == 0x8b)) {
3435 * inflate() is supposed to handle both gzip and deflate
3436 * streams automatically, but in reality it doesn't
3437 * seem to handle either (at least not within the
3438 * context of an HTTP response.) We have to try
3439 * several tweaks, depending on the type of data and
3440 * version of the library installed.
3444 * Gzip file format. Skip past the header, since the
3445 * fix to make it work (setting windowBits to 31)
3446 * doesn't work with all versions of the library.
3448 Bytef *c = compr + 2;
3451 if (*c == Z_DEFLATED) {
3463 /* Skip past the MTIME, XFL, and OS fields. */
3466 if (flags & (1 << 2)) {
3467 /* An Extra field is present. */
3468 gint xsize = (gint)(*c |
3474 if (flags & (1 << 3)) {
3475 /* A null terminated filename */
3477 while ((c - compr) < comprlen && *c != '\0') {
3484 if (flags & (1 << 4)) {
3485 /* A null terminated comment */
3487 while ((c - compr) < comprlen && *c != '\0') {
3497 strm->next_in = next;
3498 if (c - compr > comprlen) {
3505 comprlen -= (int) (c - compr);
3508 err = inflateInit2(strm, wbits);
3510 } else if (err == Z_DATA_ERROR && uncompr == NULL &&
3514 * Re-init the stream with a negative
3515 * MAX_WBITS. This is necessary due to
3516 * some servers (Apache) not sending
3517 * the deflate header with the
3518 * content-encoded response.
3524 strm->next_in = next;
3525 strm->avail_in = comprlen;
3528 memset(strmbuf, '\0', bufsiz);
3529 strm->next_out = strmbuf;
3530 strm->avail_out = bufsiz;
3532 err = inflateInit2(strm, wbits);
3549 if (uncompr == NULL) {
3559 printf("inflate() total passes: %u\n", inflate_passes);
3560 printf("bytes in: %u\nbytes out: %u\n\n", bytes_in, bytes_out);
3563 if (uncompr != NULL) {
3564 uncompr_tvb = tvb_new_real_data((guint8*) uncompr, bytes_out,
3566 tvb_set_free_cb(uncompr_tvb, g_free);
3573 tvb_uncompress(tvbuff_t *tvb _U_, const int offset _U_, int comprlen _U_)
3580 tvb_child_uncompress(tvbuff_t *parent, tvbuff_t *tvb, const int offset, int comprlen)
3582 tvbuff_t *new_tvb = tvb_uncompress(tvb, offset, comprlen);
3584 tvb_set_child_real_data_tvbuff (parent, new_tvb);
3589 tvb_raw_offset(tvbuff_t *tvb)
3591 return ((tvb->raw_offset==-1)?(tvb->raw_offset = tvb_offset_from_real_beginning(tvb)):tvb->raw_offset);
3595 tvb_get_ds_tvb(tvbuff_t *tvb)
3597 return(tvb->ds_tvb);