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.
52 #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
55 ensure_contiguous_no_exception(tvbuff_t *tvb, gint offset, gint length,
59 ensure_contiguous(tvbuff_t *tvb, gint offset, gint length);
61 /* We dole out tvbuff's from this memchunk. */
62 GMemChunk *tvbuff_mem_chunk = NULL;
67 if (!tvbuff_mem_chunk)
68 tvbuff_mem_chunk = g_mem_chunk_create(tvbuff_t, 20, G_ALLOC_AND_FREE);
75 g_mem_chunk_destroy(tvbuff_mem_chunk);
77 tvbuff_mem_chunk = NULL;
84 tvb_init(tvbuff_t *tvb, tvbuff_type type)
86 tvb_backing_t *backing;
87 tvb_comp_t *composite;
90 tvb->initialized = FALSE;
93 tvb->reported_length = 0;
95 tvb->real_data = NULL;
101 case TVBUFF_REAL_DATA:
106 backing = &tvb->tvbuffs.subset;
112 case TVBUFF_COMPOSITE:
113 composite = &tvb->tvbuffs.composite;
114 composite->tvbs = NULL;
115 composite->start_offsets = NULL;
116 composite->end_offsets = NULL;
123 tvb_new(tvbuff_type type)
127 tvb = g_chunk_new(tvbuff_t, tvbuff_mem_chunk);
135 tvb_free(tvbuff_t* tvb)
137 tvbuff_t *member_tvb;
138 tvb_comp_t *composite;
143 if (tvb->usage_count == 0) {
145 case TVBUFF_REAL_DATA:
148 * XXX - do this with a union?
150 tvb->free_cb((gpointer)tvb->real_data);
155 /* This will be NULL if tvb_new_subset() fails because
156 * reported_length < -1 */
157 if (tvb->tvbuffs.subset.tvb) {
158 tvb_decrement_usage_count(tvb->tvbuffs.subset.tvb, 1);
162 case TVBUFF_COMPOSITE:
163 composite = &tvb->tvbuffs.composite;
164 for (slist = composite->tvbs; slist != NULL ; slist = slist->next) {
165 member_tvb = slist->data;
166 tvb_decrement_usage_count(member_tvb, 1);
169 g_slist_free(composite->tvbs);
171 if (composite->start_offsets)
172 g_free(composite->start_offsets);
173 if (composite->end_offsets)
174 g_free(composite->end_offsets);
175 if (tvb->real_data) {
177 * XXX - do this with a union?
179 g_free((gpointer)tvb->real_data);
186 g_slist_free(tvb->used_in);
189 g_chunk_free(tvb, tvbuff_mem_chunk);
194 tvb_increment_usage_count(tvbuff_t* tvb, guint count)
196 tvb->usage_count += count;
198 return tvb->usage_count;
202 tvb_decrement_usage_count(tvbuff_t* tvb, guint count)
204 if (tvb->usage_count <= count) {
205 tvb->usage_count = 1;
210 tvb->usage_count -= count;
211 return tvb->usage_count;
217 tvb_free_chain(tvbuff_t* tvb)
221 /* Recursively call tvb_free_chain() */
222 for (slist = tvb->used_in; slist != NULL ; slist = slist->next) {
223 tvb_free_chain( (tvbuff_t*)slist->data );
226 /* Stop the recursion */
233 tvb_set_free_cb(tvbuff_t* tvb, tvbuff_free_cb_t func)
235 DISSECTOR_ASSERT(tvb);
236 DISSECTOR_ASSERT(tvb->type == TVBUFF_REAL_DATA);
241 add_to_used_in_list(tvbuff_t *tvb, tvbuff_t *used_in)
243 tvb->used_in = g_slist_prepend(tvb->used_in, used_in);
244 tvb_increment_usage_count(tvb, 1);
248 tvb_set_child_real_data_tvbuff(tvbuff_t* parent, tvbuff_t* child)
250 DISSECTOR_ASSERT(parent && child);
251 DISSECTOR_ASSERT(parent->initialized);
252 DISSECTOR_ASSERT(child->initialized);
253 DISSECTOR_ASSERT(child->type == TVBUFF_REAL_DATA);
254 add_to_used_in_list(parent, child);
258 tvb_set_real_data(tvbuff_t* tvb, const guint8* data, guint length, gint reported_length)
260 DISSECTOR_ASSERT(tvb);
261 DISSECTOR_ASSERT(tvb->type == TVBUFF_REAL_DATA);
262 DISSECTOR_ASSERT(!tvb->initialized);
264 if (reported_length < -1) {
265 THROW(ReportedBoundsError);
268 tvb->real_data = data;
269 tvb->length = length;
270 tvb->reported_length = reported_length;
271 tvb->initialized = TRUE;
275 tvb_new_real_data(const guint8* data, guint length, gint reported_length)
277 static tvbuff_t *last_tvb=NULL;
280 tvb = tvb_new(TVBUFF_REAL_DATA);
285 /* remember this tvb in case we throw an exception and
286 * lose the pointer to it.
290 tvb_set_real_data(tvb, data, length, reported_length);
293 * This is the top-level real tvbuff for this data source,
294 * so its data source tvbuff is itself.
298 /* ok no exception so we dont need to remember it any longer */
304 /* Computes the absolute offset and length based on a possibly-negative offset
305 * and a length that is possible -1 (which means "to the end of the data").
306 * Returns TRUE/FALSE indicating whether the offset is in bounds or
307 * not. The integer ptrs are modified with the new offset and length.
308 * No exception is thrown.
310 * XXX - we return TRUE, not FALSE, if the offset is positive and right
311 * after the end of the tvbuff (i.e., equal to the length). We do this
312 * so that a dissector constructing a subset tvbuff for the next protocol
313 * will get a zero-length tvbuff, not an exception, if there's no data
314 * left for the next protocol - we want the next protocol to be the one
315 * that gets an exception, so the error is reported as an error in that
316 * protocol rather than the containing protocol. */
318 compute_offset_length(tvbuff_t *tvb, gint offset, gint length,
319 guint *offset_ptr, guint *length_ptr, int *exception)
321 DISSECTOR_ASSERT(offset_ptr);
322 DISSECTOR_ASSERT(length_ptr);
324 /* Compute the offset */
326 /* Positive offset - relative to the beginning of the packet. */
327 if ((guint) offset > tvb->reported_length) {
329 *exception = ReportedBoundsError;
333 else if ((guint) offset > tvb->length) {
335 *exception = BoundsError;
340 *offset_ptr = offset;
344 /* Negative offset - relative to the end of the packet. */
345 if ((guint) -offset > tvb->reported_length) {
347 *exception = ReportedBoundsError;
351 else if ((guint) -offset > tvb->length) {
353 *exception = BoundsError;
358 *offset_ptr = tvb->length + offset;
362 /* Compute the length */
365 /* XXX - ReportedBoundsError? */
366 *exception = BoundsError;
370 else if (length == -1) {
371 *length_ptr = tvb->length - *offset_ptr;
374 *length_ptr = length;
382 check_offset_length_no_exception(tvbuff_t *tvb, gint offset, gint length,
383 guint *offset_ptr, guint *length_ptr, int *exception)
387 DISSECTOR_ASSERT(tvb && tvb->initialized);
389 if (!compute_offset_length(tvb, offset, length, offset_ptr, length_ptr, exception)) {
394 * Compute the offset of the first byte past the length.
396 end_offset = *offset_ptr + *length_ptr;
399 * Check for an overflow, and clamp "end_offset" at the maximum
400 * if we got an overflow - that should force us to indicate that
401 * we're past the end of the tvbuff.
403 if (end_offset < *offset_ptr)
404 end_offset = UINT_MAX;
407 * Check whether that offset goes more than one byte past the
410 * If not, return TRUE; otherwise, return FALSE and, if "exception"
411 * is non-null, return the appropriate exception through it.
413 if (end_offset <= tvb->length) {
416 else if (end_offset <= tvb->reported_length) {
418 *exception = BoundsError;
424 *exception = ReportedBoundsError;
429 DISSECTOR_ASSERT_NOT_REACHED();
432 /* Checks (+/-) offset and length and throws an exception if
433 * either is out of bounds. Sets integer ptrs to the new offset
436 check_offset_length(tvbuff_t *tvb, gint offset, gint length,
437 guint *offset_ptr, guint *length_ptr)
441 if (!check_offset_length_no_exception(tvb, offset, length, offset_ptr, length_ptr, &exception)) {
442 DISSECTOR_ASSERT(exception > 0);
450 tvb_set_subset(tvbuff_t *tvb, tvbuff_t *backing,
451 gint backing_offset, gint backing_length, gint reported_length)
453 DISSECTOR_ASSERT(tvb);
454 DISSECTOR_ASSERT(tvb->type == TVBUFF_SUBSET);
455 DISSECTOR_ASSERT(!tvb->initialized);
457 if (reported_length < -1) {
458 THROW(ReportedBoundsError);
461 check_offset_length(backing, backing_offset, backing_length,
462 &tvb->tvbuffs.subset.offset,
463 &tvb->tvbuffs.subset.length);
465 tvb->tvbuffs.subset.tvb = backing;
466 tvb->length = tvb->tvbuffs.subset.length;
468 if (reported_length == -1) {
469 tvb->reported_length = backing->reported_length - tvb->tvbuffs.subset.offset;
472 tvb->reported_length = reported_length;
474 tvb->initialized = TRUE;
475 add_to_used_in_list(backing, tvb);
477 /* Optimization. If the backing buffer has a pointer to contiguous, real data,
478 * then we can point directly to our starting offset in that buffer */
479 if (backing->real_data != NULL) {
480 tvb->real_data = backing->real_data + tvb->tvbuffs.subset.offset;
486 tvb_new_subset(tvbuff_t *backing, gint backing_offset, gint backing_length, gint reported_length)
488 static tvbuff_t *last_tvb=NULL;
491 tvb = tvb_new(TVBUFF_SUBSET);
496 /* remember this tvb in case we throw an exception and
497 * lose the pointer to it.
501 tvb_set_subset(tvb, backing, backing_offset, backing_length, reported_length);
504 * The top-level data source of this tvbuff is the top-level
505 * data source of its parent.
507 tvb->ds_tvb = backing->ds_tvb;
509 /* ok no exception so we dont need to remember it any longer */
516 tvb_composite_append(tvbuff_t* tvb, tvbuff_t* member)
518 tvb_comp_t *composite;
520 DISSECTOR_ASSERT(tvb && !tvb->initialized);
521 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
522 composite = &tvb->tvbuffs.composite;
523 composite->tvbs = g_slist_append( composite->tvbs, member );
524 add_to_used_in_list(tvb, member);
528 tvb_composite_prepend(tvbuff_t* tvb, tvbuff_t* member)
530 tvb_comp_t *composite;
532 DISSECTOR_ASSERT(tvb && !tvb->initialized);
533 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
534 composite = &tvb->tvbuffs.composite;
535 composite->tvbs = g_slist_prepend( composite->tvbs, member );
536 add_to_used_in_list(tvb, member);
540 tvb_new_composite(void)
542 return tvb_new(TVBUFF_COMPOSITE);
546 tvb_composite_finalize(tvbuff_t* tvb)
550 tvbuff_t *member_tvb;
551 tvb_comp_t *composite;
554 DISSECTOR_ASSERT(tvb && !tvb->initialized);
555 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
556 DISSECTOR_ASSERT(tvb->length == 0);
558 composite = &tvb->tvbuffs.composite;
559 num_members = g_slist_length(composite->tvbs);
561 composite->start_offsets = g_new(guint, num_members);
562 composite->end_offsets = g_new(guint, num_members);
564 for (slist = composite->tvbs; slist != NULL; slist = slist->next) {
565 DISSECTOR_ASSERT((guint) i < num_members);
566 member_tvb = slist->data;
567 composite->start_offsets[i] = tvb->length;
568 tvb->length += member_tvb->length;
569 composite->end_offsets[i] = tvb->length - 1;
573 tvb->initialized = TRUE;
579 tvb_length(tvbuff_t* tvb)
581 DISSECTOR_ASSERT(tvb && tvb->initialized);
587 tvb_length_remaining(tvbuff_t *tvb, gint offset)
589 guint abs_offset, abs_length;
591 DISSECTOR_ASSERT(tvb && tvb->initialized);
593 if (compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL)) {
602 tvb_ensure_length_remaining(tvbuff_t *tvb, gint offset)
604 guint abs_offset, abs_length;
607 DISSECTOR_ASSERT(tvb && tvb->initialized);
609 if (!compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, &exception)) {
612 if (abs_length == 0) {
614 * This routine ensures there's at least one byte available.
615 * There aren't any bytes available, so throw the appropriate
618 if (abs_offset >= tvb->reported_length)
619 THROW(ReportedBoundsError);
629 /* Validates that 'length' bytes are available starting from
630 * offset (pos/neg). Does not throw an exception. */
632 tvb_bytes_exist(tvbuff_t *tvb, gint offset, gint length)
634 guint abs_offset, abs_length;
636 DISSECTOR_ASSERT(tvb && tvb->initialized);
638 if (!compute_offset_length(tvb, offset, length, &abs_offset, &abs_length, NULL))
641 if (abs_offset + abs_length <= tvb->length) {
649 /* Validates that 'length' bytes are available starting from
650 * offset (pos/neg). Throws an exception if they aren't. */
652 tvb_ensure_bytes_exist(tvbuff_t *tvb, gint offset, gint length)
654 guint abs_offset, abs_length;
656 DISSECTOR_ASSERT(tvb && tvb->initialized);
659 * -1 doesn't mean "until end of buffer", as that's pointless
660 * for this routine. We must treat it as a Really Large Positive
661 * Number, so that we throw an exception; we throw
662 * ReportedBoundsError, as if it were past even the end of a
663 * reassembled packet, and past the end of even the data we
666 * We do the same with other negative lengths.
669 THROW(ReportedBoundsError);
671 check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
675 tvb_offset_exists(tvbuff_t *tvb, gint offset)
677 guint abs_offset, abs_length;
679 DISSECTOR_ASSERT(tvb && tvb->initialized);
680 if (!compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL))
683 if (abs_offset < tvb->length) {
692 tvb_reported_length(tvbuff_t* tvb)
694 DISSECTOR_ASSERT(tvb && tvb->initialized);
696 return tvb->reported_length;
700 tvb_reported_length_remaining(tvbuff_t *tvb, gint offset)
702 guint abs_offset, abs_length;
704 DISSECTOR_ASSERT(tvb && tvb->initialized);
706 if (compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL)) {
707 if (tvb->reported_length >= abs_offset)
708 return tvb->reported_length - abs_offset;
717 /* Set the reported length of a tvbuff to a given value; used for protocols
718 whose headers contain an explicit length and where the calling
719 dissector's payload may include padding as well as the packet for
722 Also adjusts the data length. */
724 tvb_set_reported_length(tvbuff_t* tvb, guint reported_length)
726 DISSECTOR_ASSERT(tvb && tvb->initialized);
728 if (reported_length > tvb->reported_length)
729 THROW(ReportedBoundsError);
731 tvb->reported_length = reported_length;
732 if (reported_length < tvb->length)
733 tvb->length = reported_length;
739 first_real_data_ptr(tvbuff_t *tvb)
744 case TVBUFF_REAL_DATA:
745 return tvb->real_data;
747 member = tvb->tvbuffs.subset.tvb;
748 return first_real_data_ptr(member);
749 case TVBUFF_COMPOSITE:
750 member = tvb->tvbuffs.composite.tvbs->data;
751 return first_real_data_ptr(member);
754 DISSECTOR_ASSERT_NOT_REACHED();
760 offset_from_real_beginning(tvbuff_t *tvb, int counter)
765 case TVBUFF_REAL_DATA:
768 member = tvb->tvbuffs.subset.tvb;
769 return offset_from_real_beginning(member, counter + tvb->tvbuffs.subset.offset);
770 case TVBUFF_COMPOSITE:
771 member = tvb->tvbuffs.composite.tvbs->data;
772 return offset_from_real_beginning(member, counter);
775 DISSECTOR_ASSERT_NOT_REACHED();
780 composite_ensure_contiguous_no_exception(tvbuff_t *tvb, guint abs_offset,
783 guint i, num_members;
784 tvb_comp_t *composite;
785 tvbuff_t *member_tvb = NULL;
786 guint member_offset, member_length;
789 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
791 /* Maybe the range specified by offset/length
792 * is contiguous inside one of the member tvbuffs */
793 composite = &tvb->tvbuffs.composite;
794 num_members = g_slist_length(composite->tvbs);
796 for (i = 0; i < num_members; i++) {
797 if (abs_offset <= composite->end_offsets[i]) {
798 slist = g_slist_nth(composite->tvbs, i);
799 member_tvb = slist->data;
803 DISSECTOR_ASSERT(member_tvb);
805 if (check_offset_length_no_exception(member_tvb, abs_offset - composite->start_offsets[i],
806 abs_length, &member_offset, &member_length, NULL)) {
809 * The range is, in fact, contiguous within member_tvb.
811 DISSECTOR_ASSERT(!tvb->real_data);
812 return ensure_contiguous_no_exception(member_tvb, member_offset, member_length, NULL);
815 tvb->real_data = tvb_memdup(tvb, 0, -1);
816 return tvb->real_data + abs_offset;
819 DISSECTOR_ASSERT_NOT_REACHED();
824 ensure_contiguous_no_exception(tvbuff_t *tvb, gint offset, gint length,
827 guint abs_offset, abs_length;
829 if (!check_offset_length_no_exception(tvb, offset, length,
830 &abs_offset, &abs_length, exception)) {
835 * We know that all the data is present in the tvbuff, so
836 * no exceptions should be thrown.
838 if (tvb->real_data) {
839 return tvb->real_data + abs_offset;
843 case TVBUFF_REAL_DATA:
844 DISSECTOR_ASSERT_NOT_REACHED();
846 return ensure_contiguous_no_exception(tvb->tvbuffs.subset.tvb,
847 abs_offset - tvb->tvbuffs.subset.offset,
849 case TVBUFF_COMPOSITE:
850 return composite_ensure_contiguous_no_exception(tvb, abs_offset, abs_length);
854 DISSECTOR_ASSERT_NOT_REACHED();
858 /* ----------------------------- */
860 fast_ensure_contiguous(tvbuff_t *tvb, gint offset, guint length)
865 DISSECTOR_ASSERT(tvb && tvb->initialized);
866 if (offset < 0 || !tvb->real_data) {
867 return ensure_contiguous(tvb, offset, length);
871 end_offset = u_offset + length;
873 /* don't need to check for overflow because length <= 8 */
875 if (end_offset <= tvb->length) {
876 return tvb->real_data + u_offset;
879 if (end_offset > tvb->reported_length) {
880 THROW(ReportedBoundsError);
889 ensure_contiguous(tvbuff_t *tvb, gint offset, gint length)
894 p = ensure_contiguous_no_exception(tvb, offset, length, &exception);
896 DISSECTOR_ASSERT(exception > 0);
903 guint8_find(const guint8* haystack, size_t haystacklen, guint8 needle)
908 for (b = haystack, i = 0; (guint) i < haystacklen; i++, b++) {
918 guint8_pbrk(const guint8* haystack, size_t haystacklen, const guint8 *needles)
923 const guint8 *needlep;
925 for (b = haystack, i = 0; (guint) i < haystacklen; i++, b++) {
928 while ((needle = *needlep) != '\0') {
940 /************** ACCESSORS **************/
943 composite_memcpy(tvbuff_t *tvb, guint8* target, guint abs_offset, guint abs_length)
945 guint i, num_members;
946 tvb_comp_t *composite;
947 tvbuff_t *member_tvb = NULL;
948 guint member_offset, member_length;
952 DISSECTOR_ASSERT(tvb->type == TVBUFF_COMPOSITE);
954 /* Maybe the range specified by offset/length
955 * is contiguous inside one of the member tvbuffs */
956 composite = &tvb->tvbuffs.composite;
957 num_members = g_slist_length(composite->tvbs);
959 for (i = 0; i < num_members; i++) {
960 if (abs_offset <= composite->end_offsets[i]) {
961 slist = g_slist_nth(composite->tvbs, i);
962 member_tvb = slist->data;
966 DISSECTOR_ASSERT(member_tvb);
968 if (check_offset_length_no_exception(member_tvb, abs_offset - composite->start_offsets[i],
969 abs_length, &member_offset, &member_length, NULL)) {
971 DISSECTOR_ASSERT(!tvb->real_data);
972 return tvb_memcpy(member_tvb, target, member_offset, member_length);
975 /* The requested data is non-contiguous inside
976 * the member tvb. We have to memcpy() the part that's in the member tvb,
977 * then iterate across the other member tvb's, copying their portions
978 * until we have copied all data.
980 retval = compute_offset_length(member_tvb, abs_offset - composite->start_offsets[i], -1,
981 &member_offset, &member_length, NULL);
982 DISSECTOR_ASSERT(retval);
984 tvb_memcpy(member_tvb, target, member_offset, member_length);
985 abs_offset += member_length;
986 abs_length -= member_length;
989 if (abs_length > 0) {
990 composite_memcpy(tvb, target + member_length, abs_offset, abs_length);
996 DISSECTOR_ASSERT_NOT_REACHED();
1001 tvb_memcpy(tvbuff_t *tvb, void* target, gint offset, gint length)
1003 guint abs_offset, abs_length;
1005 DISSECTOR_ASSERT(length >= -1);
1006 check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
1008 if (tvb->real_data) {
1009 return memcpy(target, tvb->real_data + abs_offset, abs_length);
1013 case TVBUFF_REAL_DATA:
1014 DISSECTOR_ASSERT_NOT_REACHED();
1017 return tvb_memcpy(tvb->tvbuffs.subset.tvb, target,
1018 abs_offset - tvb->tvbuffs.subset.offset,
1021 case TVBUFF_COMPOSITE:
1022 return composite_memcpy(tvb, target, offset, length);
1025 DISSECTOR_ASSERT_NOT_REACHED();
1031 * XXX - this doesn't treat a length of -1 as an error.
1032 * If it did, this could replace some code that calls
1033 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
1036 * "composite_ensure_contiguous_no_exception()" depends on -1 not being
1037 * an error; does anything else depend on this routine treating -1 as
1038 * meaning "to the end of the buffer"?
1041 tvb_memdup(tvbuff_t *tvb, gint offset, gint length)
1043 guint abs_offset, abs_length;
1046 check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
1048 duped = g_malloc(abs_length);
1049 return tvb_memcpy(tvb, duped, abs_offset, abs_length);
1053 * XXX - this doesn't treat a length of -1 as an error.
1054 * If it did, this could replace some code that calls
1055 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
1058 * "composite_ensure_contiguous_no_exception()" depends on -1 not being
1059 * an error; does anything else depend on this routine treating -1 as
1060 * meaning "to the end of the buffer"?
1062 * This function allocates memory from a buffer with packet lifetime.
1063 * You do not have to free this buffer, it will be automatically freed
1064 * when wireshark starts decoding the next packet.
1065 * Do not use this function if you want the allocated memory to be persistent
1066 * after the current packet has been dissected.
1069 ep_tvb_memdup(tvbuff_t *tvb, gint offset, gint length)
1071 guint abs_offset, abs_length;
1074 check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
1076 duped = ep_alloc(abs_length);
1077 return tvb_memcpy(tvb, duped, abs_offset, abs_length);
1083 tvb_get_ptr(tvbuff_t *tvb, gint offset, gint length)
1085 return ensure_contiguous(tvb, offset, length);
1088 /* ---------------- */
1090 tvb_get_guint8(tvbuff_t *tvb, gint offset)
1094 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint8));
1099 tvb_get_ntohs(tvbuff_t *tvb, gint offset)
1103 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
1108 tvb_get_ntoh24(tvbuff_t *tvb, gint offset)
1112 ptr = fast_ensure_contiguous(tvb, offset, 3);
1113 return pntoh24(ptr);
1117 tvb_get_ntohl(tvbuff_t *tvb, gint offset)
1121 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1126 tvb_get_ntoh64(tvbuff_t *tvb, gint offset)
1130 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
1131 return pntoh64(ptr);
1135 * Stuff for IEEE float handling on platforms that don't have IEEE
1136 * format as the native floating-point format.
1138 * For now, we treat only the VAX as such a platform.
1140 * XXX - other non-IEEE boxes that can run UNIX include some Crays,
1141 * and possibly other machines.
1143 * It appears that the official Linux port to System/390 and
1144 * zArchitecture uses IEEE format floating point (not a
1147 * I don't know whether there are any other machines that
1148 * could run Wireshark and that don't use IEEE format.
1149 * As far as I know, all of the main commercial microprocessor
1150 * families on which OSes that support Wireshark can run
1151 * use IEEE format (x86, 68k, SPARC, MIPS, PA-RISC, Alpha,
1152 * IA-64, and so on).
1162 #define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
1163 #define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
1164 #define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
1166 #define IEEE_SP_SIGN_MASK 0x80000000
1167 #define IEEE_SP_EXPONENT_MASK 0x7F800000
1168 #define IEEE_SP_MANTISSA_MASK 0x007FFFFF
1169 #define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
1171 #define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
1172 #define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
1173 #define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
1176 ieee_float_is_zero(guint32 w)
1178 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1182 get_ieee_float(guint32 w)
1188 sign = w & IEEE_SP_SIGN_MASK;
1189 exponent = w & IEEE_SP_EXPONENT_MASK;
1190 mantissa = w & IEEE_SP_MANTISSA_MASK;
1192 if (ieee_float_is_zero(w)) {
1193 /* number is zero, unnormalized, or not-a-number */
1198 * XXX - how to handle this?
1200 if (IEEE_SP_INFINITY == exponent) {
1202 * number is positive or negative infinity, or a special value
1204 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1208 exponent = ((exponent >> IEEE_SP_MANTISSA_WIDTH) - IEEE_SP_BIAS) -
1209 IEEE_SP_MANTISSA_WIDTH;
1210 mantissa |= IEEE_SP_IMPLIED_BIT;
1213 return -mantissa * pow(2, exponent);
1215 return mantissa * pow(2, exponent);
1220 * We assume that if you don't have IEEE floating-point, you have a
1221 * compiler that understands 64-bit integral quantities.
1223 #define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
1224 #define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
1225 #define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
1227 #define IEEE_DP_SIGN_MASK 0x8000000000000000LL
1228 #define IEEE_DP_EXPONENT_MASK 0x7FF0000000000000LL
1229 #define IEEE_DP_MANTISSA_MASK 0x000FFFFFFFFFFFFFLL
1230 #define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
1232 #define IEEE_DP_IMPLIED_BIT (1LL << IEEE_DP_MANTISSA_WIDTH)
1233 #define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
1234 #define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
1237 ieee_double_is_zero(guint64 w)
1239 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1243 get_ieee_double(guint64 w)
1249 sign = w & IEEE_DP_SIGN_MASK;
1250 exponent = w & IEEE_DP_EXPONENT_MASK;
1251 mantissa = w & IEEE_DP_MANTISSA_MASK;
1253 if (ieee_double_is_zero(w)) {
1254 /* number is zero, unnormalized, or not-a-number */
1259 * XXX - how to handle this?
1261 if (IEEE_DP_INFINITY == exponent) {
1263 * number is positive or negative infinity, or a special value
1265 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1269 exponent = ((exponent >> IEEE_DP_MANTISSA_WIDTH) - IEEE_DP_BIAS) -
1270 IEEE_DP_MANTISSA_WIDTH;
1271 mantissa |= IEEE_DP_IMPLIED_BIT;
1274 return -mantissa * pow(2, exponent);
1276 return mantissa * pow(2, exponent);
1281 * Fetches an IEEE single-precision floating-point number, in
1282 * big-endian form, and returns a "float".
1284 * XXX - should this be "double", in case there are IEEE single-
1285 * precision numbers that won't fit in some platform's native
1289 tvb_get_ntohieee_float(tvbuff_t *tvb, int offset)
1292 return get_ieee_float(tvb_get_ntohl(tvb, offset));
1299 ieee_fp_union.w = tvb_get_ntohl(tvb, offset);
1300 return ieee_fp_union.f;
1305 * Fetches an IEEE double-precision floating-point number, in
1306 * big-endian form, and returns a "double".
1309 tvb_get_ntohieee_double(tvbuff_t *tvb, int offset)
1323 #ifdef WORDS_BIGENDIAN
1324 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset);
1325 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset+4);
1327 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset+4);
1328 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset);
1331 return get_ieee_double(ieee_fp_union.dw);
1333 return ieee_fp_union.d;
1338 tvb_get_letohs(tvbuff_t *tvb, gint offset)
1342 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
1343 return pletohs(ptr);
1347 tvb_get_letoh24(tvbuff_t *tvb, gint offset)
1351 ptr = fast_ensure_contiguous(tvb, offset, 3);
1352 return pletoh24(ptr);
1356 tvb_get_letohl(tvbuff_t *tvb, gint offset)
1360 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1361 return pletohl(ptr);
1365 tvb_get_letoh64(tvbuff_t *tvb, gint offset)
1369 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
1370 return pletoh64(ptr);
1374 * Fetches an IEEE single-precision floating-point number, in
1375 * little-endian form, and returns a "float".
1377 * XXX - should this be "double", in case there are IEEE single-
1378 * precision numbers that won't fit in some platform's native
1382 tvb_get_letohieee_float(tvbuff_t *tvb, int offset)
1385 return get_ieee_float(tvb_get_letohl(tvb, offset));
1392 ieee_fp_union.w = tvb_get_letohl(tvb, offset);
1393 return ieee_fp_union.f;
1398 * Fetches an IEEE double-precision floating-point number, in
1399 * little-endian form, and returns a "double".
1402 tvb_get_letohieee_double(tvbuff_t *tvb, int offset)
1416 #ifdef WORDS_BIGENDIAN
1417 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset+4);
1418 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset);
1420 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset);
1421 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset+4);
1424 return get_ieee_double(ieee_fp_union.dw);
1426 return ieee_fp_union.d;
1430 /* Fetch an IPv4 address, in network byte order.
1431 * We do *not* convert them to host byte order; we leave them in
1432 * network byte order. */
1434 tvb_get_ipv4(tvbuff_t *tvb, gint offset)
1439 ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
1440 memcpy(&addr, ptr, sizeof addr);
1444 /* Fetch an IPv6 address. */
1446 tvb_get_ipv6(tvbuff_t *tvb, gint offset, struct e_in6_addr *addr)
1450 ptr = ensure_contiguous(tvb, offset, sizeof(*addr));
1451 memcpy(addr, ptr, sizeof *addr);
1456 tvb_get_ntohguid(tvbuff_t *tvb, gint offset, e_guid_t *guid)
1458 ensure_contiguous(tvb, offset, sizeof(*guid));
1459 guid->data1 = tvb_get_ntohl(tvb, offset);
1460 guid->data2 = tvb_get_ntohs(tvb, offset + 4);
1461 guid->data3 = tvb_get_ntohs(tvb, offset + 6);
1462 tvb_memcpy(tvb, guid->data4, offset + 8, sizeof guid->data4);
1466 tvb_get_letohguid(tvbuff_t *tvb, gint offset, e_guid_t *guid)
1468 ensure_contiguous(tvb, offset, sizeof(*guid));
1469 guid->data1 = tvb_get_letohl(tvb, offset);
1470 guid->data2 = tvb_get_letohs(tvb, offset + 4);
1471 guid->data3 = tvb_get_letohs(tvb, offset + 6);
1472 tvb_memcpy(tvb, guid->data4, offset + 8, sizeof guid->data4);
1476 tvb_get_guid(tvbuff_t *tvb, gint offset, e_guid_t *guid, gboolean little_endian)
1478 if (little_endian) {
1479 tvb_get_letohguid(tvb, offset, guid);
1481 tvb_get_ntohguid(tvb, offset, guid);
1485 static const guint8 bit_mask8[] = {
1496 /* Bit offset mask for number of bits = 8 - 16 */
1497 static const guint16 bit_mask16[] = {
1507 /* Get 1 - 8 bits */
1509 tvb_get_bits8(tvbuff_t *tvb, gint bit_offset, gint no_of_bits)
1516 DISSECTOR_ASSERT_NOT_REACHED();
1518 /* Byte align offset */
1519 offset = bit_offset>>3;
1521 /* Find out which mask to use for the most significant octet
1522 * by convering bit_offset into the offset into the first
1525 bit_offset = bit_offset & 0x7;
1526 tot_no_bits = bit_offset+no_of_bits;
1528 /* Read one octet, mask off bit_offset bits and left shift out the unused bits */
1529 value = tvb_get_guint8(tvb,offset) & bit_mask8[bit_offset];
1530 value = value >> (8-tot_no_bits);
1532 /* Read two octets, mask off bit_offset bits and left shift out the unused bits */
1533 value = tvb_get_ntohs(tvb,offset) & bit_mask16[bit_offset];
1534 value = value >> (16 - tot_no_bits);
1537 return (guint8)value;
1543 /* Get 9 - 16 bits */
1544 /* Bit offset mask for number of bits = 9 - 32 */
1545 static const guint32 bit_mask32[] = {
1556 tvb_get_bits16(tvbuff_t *tvb, gint bit_offset, gint no_of_bits,gboolean little_endian)
1560 guint16 tempval = 0;
1563 if ((no_of_bits<8)||(no_of_bits>16)) {
1564 /* If bits < 8 use tvb_get_bits8 */
1565 DISSECTOR_ASSERT_NOT_REACHED();
1568 DISSECTOR_ASSERT_NOT_REACHED();
1569 /* This part is not implemented yet */
1572 /* Byte align offset */
1573 offset = bit_offset>>3;
1575 /* Find out which mask to use for the most significant octet
1576 * by convering bit_offset into the offset into the first
1579 bit_offset = bit_offset & 0x7;
1580 tot_no_bits = bit_offset+no_of_bits;
1581 /* Read two octets and mask off bit_offset bits */
1582 value = tvb_get_ntohs(tvb,offset) & bit_mask16[bit_offset];
1583 if(tot_no_bits < 16){
1584 /* Left shift out the unused bits */
1585 value = value >> (16 - tot_no_bits);
1586 }else if(tot_no_bits > 16){
1587 /* Spans three octets, read next octet and shift as needed */
1588 value = value << (tot_no_bits - 16);
1589 tempval = tvb_get_guint8(tvb,offset+2);
1590 tempval = tempval >> (24-tot_no_bits);
1591 value = value | tempval;
1598 /* Bit offset mask for number of bits = 32 - 64 */
1599 static const guint64 bit_mask64[] = {
1600 G_GINT64_CONSTANT(0xffffffffffffffffU),
1601 G_GINT64_CONSTANT(0x7fffffffffffffffU),
1602 G_GINT64_CONSTANT(0x3fffffffffffffffU),
1603 G_GINT64_CONSTANT(0x1fffffffffffffffU),
1604 G_GINT64_CONSTANT(0x0fffffffffffffffU),
1605 G_GINT64_CONSTANT(0x07ffffffffffffffU),
1606 G_GINT64_CONSTANT(0x03ffffffffffffffU),
1607 G_GINT64_CONSTANT(0x01ffffffffffffffU)
1611 tvb_get_bits32(tvbuff_t *tvb, gint bit_offset, gint no_of_bits, gboolean little_endian)
1615 guint32 tempval = 0;
1618 if ((no_of_bits<17)||(no_of_bits>32)) {
1619 /* If bits < 17 use tvb_get_bits8 or tvb_get_bits_ntohs */
1620 DISSECTOR_ASSERT_NOT_REACHED();
1623 DISSECTOR_ASSERT_NOT_REACHED();
1624 /* This part is not implemented yet */
1627 /* Byte align offset */
1628 offset = bit_offset>>3;
1630 /* Find out which mask to use for the most significant octet
1631 * by convering bit_offset into the offset into the first
1634 bit_offset = bit_offset & 0x7;
1635 tot_no_bits = bit_offset+no_of_bits;
1636 /* Read four octets and mask off bit_offset bits */
1637 value = tvb_get_ntohl(tvb,offset) & bit_mask32[bit_offset];
1638 if(tot_no_bits < 32){
1639 /* Left shift out the unused bits */
1640 value = value >> (32 - tot_no_bits);
1641 }else if(tot_no_bits > 32){
1642 /* Spans five octets, read next octet and shift as needed */
1643 value = value << (tot_no_bits - 32);
1644 tempval = tvb_get_guint8(tvb,offset+4);
1645 tempval = tempval >> (40-tot_no_bits);
1646 value = value | tempval;
1653 tvb_get_bits64(tvbuff_t *tvb, gint bit_offset, gint no_of_bits, gboolean little_endian)
1658 guint64 tempval = 0;
1661 if ((no_of_bits<32)||(no_of_bits>64)) {
1662 /* If bits < 17 use tvb_get_bits8 or tvb_get_bits_ntohs */
1663 DISSECTOR_ASSERT_NOT_REACHED();
1666 DISSECTOR_ASSERT_NOT_REACHED();
1667 /* This part is not implemented yet */
1670 /* Byte align offset */
1671 offset = bit_offset>>3;
1673 /* Find out which mask to use for the most significant octet
1674 * by convering bit_offset into the offset into the first
1677 bit_offset = bit_offset & 0x7;
1678 tot_no_bits = bit_offset+no_of_bits;
1679 /* Read eight octets and mask off bit_offset bits */
1680 value = tvb_get_ntoh64(tvb,offset) & bit_mask64[bit_offset];
1681 if (tot_no_bits < 64){
1682 /* Left shift out the unused bits */
1683 value = value >> (64 - tot_no_bits);
1684 }else if (tot_no_bits > 64){
1685 /* Spans nine octets, read next octet and shift as needed */
1686 value = value << (tot_no_bits - 64);
1687 tempval = tvb_get_guint8(tvb,offset+8);
1688 tempval = tempval >> (72-tot_no_bits);
1689 value = value | tempval;
1696 /* Find first occurence of needle in tvbuff, starting at offset. Searches
1697 * at most maxlength number of bytes; if maxlength is -1, searches to
1699 * Returns the offset of the found needle, or -1 if not found.
1700 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1701 * in that case, -1 will be returned if the boundary is reached before
1702 * finding needle. */
1704 tvb_find_guint8(tvbuff_t *tvb, gint offset, gint maxlength, guint8 needle)
1706 const guint8 *result;
1707 guint abs_offset, junk_length;
1711 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1713 /* Only search to end of tvbuff, w/o throwing exception. */
1714 tvbufflen = tvb_length_remaining(tvb, abs_offset);
1715 if (maxlength == -1) {
1716 /* No maximum length specified; search to end of tvbuff. */
1719 else if (tvbufflen < (guint) maxlength) {
1720 /* Maximum length goes past end of tvbuff; search to end
1725 /* Maximum length doesn't go past end of tvbuff; search
1730 /* If we have real data, perform our search now. */
1731 if (tvb->real_data) {
1732 result = guint8_find(tvb->real_data + abs_offset, limit, needle);
1733 if (result == NULL) {
1737 return result - tvb->real_data;
1742 case TVBUFF_REAL_DATA:
1743 DISSECTOR_ASSERT_NOT_REACHED();
1746 return tvb_find_guint8(tvb->tvbuffs.subset.tvb,
1747 abs_offset - tvb->tvbuffs.subset.offset,
1750 case TVBUFF_COMPOSITE:
1751 DISSECTOR_ASSERT_NOT_REACHED();
1752 /* XXX - return composite_find_guint8(tvb, offset, limit, needle); */
1755 DISSECTOR_ASSERT_NOT_REACHED();
1759 /* Find first occurence of any of the needles in tvbuff, starting at offset.
1760 * Searches at most maxlength number of bytes; if maxlength is -1, searches
1762 * Returns the offset of the found needle, or -1 if not found.
1763 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1764 * in that case, -1 will be returned if the boundary is reached before
1765 * finding needle. */
1767 tvb_pbrk_guint8(tvbuff_t *tvb, gint offset, gint maxlength, const guint8 *needles)
1769 const guint8 *result;
1770 guint abs_offset, junk_length;
1774 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1776 /* Only search to end of tvbuff, w/o throwing exception. */
1777 tvbufflen = tvb_length_remaining(tvb, abs_offset);
1778 if (maxlength == -1) {
1779 /* No maximum length specified; search to end of tvbuff. */
1782 else if (tvbufflen < (guint) maxlength) {
1783 /* Maximum length goes past end of tvbuff; search to end
1788 /* Maximum length doesn't go past end of tvbuff; search
1793 /* If we have real data, perform our search now. */
1794 if (tvb->real_data) {
1795 result = guint8_pbrk(tvb->real_data + abs_offset, limit, needles);
1796 if (result == NULL) {
1800 return result - tvb->real_data;
1805 case TVBUFF_REAL_DATA:
1806 DISSECTOR_ASSERT_NOT_REACHED();
1809 return tvb_pbrk_guint8(tvb->tvbuffs.subset.tvb,
1810 abs_offset - tvb->tvbuffs.subset.offset,
1813 case TVBUFF_COMPOSITE:
1814 DISSECTOR_ASSERT_NOT_REACHED();
1815 /* XXX - return composite_pbrk_guint8(tvb, offset, limit, needle); */
1818 DISSECTOR_ASSERT_NOT_REACHED();
1822 /* Find size of stringz (NUL-terminated string) by looking for terminating
1823 * NUL. The size of the string includes the terminating NUL.
1825 * If the NUL isn't found, it throws the appropriate exception.
1828 tvb_strsize(tvbuff_t *tvb, gint offset)
1830 guint abs_offset, junk_length;
1833 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1834 nul_offset = tvb_find_guint8(tvb, abs_offset, -1, 0);
1835 if (nul_offset == -1) {
1837 * OK, we hit the end of the tvbuff, so we should throw
1840 * Did we hit the end of the captured data, or the end
1841 * of the actual data? If there's less captured data
1842 * than actual data, we presumably hit the end of the
1843 * captured data, otherwise we hit the end of the actual
1846 if (tvb_length(tvb) < tvb_reported_length(tvb)) {
1849 THROW(ReportedBoundsError);
1852 return (nul_offset - abs_offset) + 1;
1855 /* Find length of string by looking for end of string ('\0'), up to
1856 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
1858 * Returns -1 if 'maxlength' reached before finding EOS. */
1860 tvb_strnlen(tvbuff_t *tvb, gint offset, guint maxlength)
1863 guint abs_offset, junk_length;
1865 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1867 result_offset = tvb_find_guint8(tvb, abs_offset, maxlength, 0);
1869 if (result_offset == -1) {
1873 return result_offset - abs_offset;
1878 * Implement strneql etc
1882 * Call strncmp after checking if enough chars left, returning 0 if
1883 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1886 tvb_strneql(tvbuff_t *tvb, gint offset, const gchar *str, gint size)
1890 ptr = ensure_contiguous_no_exception(tvb, offset, size, NULL);
1893 int cmp = strncmp((const char *)ptr, str, size);
1896 * Return 0 if equal, -1 otherwise.
1898 return (cmp == 0 ? 0 : -1);
1901 * Not enough characters in the tvbuff to match the
1909 * Call g_ascii_strncasecmp after checking if enough chars left, returning
1910 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1913 tvb_strncaseeql(tvbuff_t *tvb, gint offset, const gchar *str, gint size)
1917 ptr = ensure_contiguous_no_exception(tvb, offset, size, NULL);
1920 int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
1923 * Return 0 if equal, -1 otherwise.
1925 return (cmp == 0 ? 0 : -1);
1928 * Not enough characters in the tvbuff to match the
1936 * Call memcmp after checking if enough chars left, returning 0 if
1937 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1940 tvb_memeql(tvbuff_t *tvb, gint offset, const guint8 *str, gint size)
1944 ptr = ensure_contiguous_no_exception(tvb, offset, size, NULL);
1947 int cmp = memcmp(ptr, str, size);
1950 * Return 0 if equal, -1 otherwise.
1952 return (cmp == 0 ? 0 : -1);
1955 * Not enough characters in the tvbuff to match the
1962 /* Convert a string from Unicode to ASCII. At the moment we fake it by
1963 * replacing all non-ASCII characters with a '.' )-: The caller must
1964 * free the result returned. The len parameter is the number of guint16's
1965 * to convert from Unicode. */
1967 tvb_fake_unicode(tvbuff_t *tvb, int offset, int len, gboolean little_endian)
1973 /* Make sure we have enough data before allocating the buffer,
1974 so we don't blow up if the length is huge. */
1975 tvb_ensure_bytes_exist(tvb, offset, 2*len);
1977 /* We know we won't throw an exception, so we don't have to worry
1978 about leaking this buffer. */
1979 buffer = g_malloc(len + 1);
1981 for (i = 0; i < len; i++) {
1982 character = little_endian ? tvb_get_letohs(tvb, offset)
1983 : tvb_get_ntohs(tvb, offset);
1984 buffer[i] = character < 256 ? character : '.';
1993 /* Convert a string from Unicode to ASCII. At the moment we fake it by
1994 * replacing all non-ASCII characters with a '.' )-: The len parameter is
1995 * the number of guint16's to convert from Unicode.
1997 * This function allocates memory from a buffer with packet lifetime.
1998 * You do not have to free this buffer, it will be automatically freed
1999 * when wireshark starts decoding the next packet.
2002 tvb_get_ephemeral_faked_unicode(tvbuff_t *tvb, int offset, int len, gboolean little_endian)
2008 /* Make sure we have enough data before allocating the buffer,
2009 so we don't blow up if the length is huge. */
2010 tvb_ensure_bytes_exist(tvb, offset, 2*len);
2012 /* We know we won't throw an exception, so we don't have to worry
2013 about leaking this buffer. */
2014 buffer = ep_alloc(len + 1);
2016 for (i = 0; i < len; i++) {
2017 character = little_endian ? tvb_get_letohs(tvb, offset)
2018 : tvb_get_ntohs(tvb, offset);
2019 buffer[i] = character < 256 ? character : '.';
2029 * Format the data in the tvb from offset for length ...
2033 tvb_format_text(tvbuff_t *tvb, gint offset, gint size)
2038 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
2040 len = tvb_length_remaining(tvb, offset);
2041 ptr = ensure_contiguous(tvb, offset, len);
2045 return format_text(ptr, len);
2050 * Format the data in the tvb from offset for length ...
2054 tvb_format_text_wsp(tvbuff_t *tvb, gint offset, gint size)
2059 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
2061 len = tvb_length_remaining(tvb, offset);
2062 ptr = ensure_contiguous(tvb, offset, len);
2066 return format_text_wsp(ptr, len);
2071 * Like "tvb_format_text()", but for null-padded strings; don't show
2072 * the null padding characters as "\000".
2075 tvb_format_stringzpad(tvbuff_t *tvb, gint offset, gint size)
2077 const guint8 *ptr, *p;
2081 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
2083 len = tvb_length_remaining(tvb, offset);
2084 ptr = ensure_contiguous(tvb, offset, len);
2088 for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
2090 return format_text(ptr, stringlen);
2095 * Given a tvbuff, an offset, and a length, allocate a buffer big enough
2096 * to hold a non-null-terminated string of that length at that offset,
2097 * plus a trailing '\0', copy the string into it, and return a pointer
2100 * Throws an exception if the tvbuff ends before the string does.
2103 tvb_get_string(tvbuff_t *tvb, gint offset, gint length)
2106 guint8 *strbuf = NULL;
2108 tvb_ensure_bytes_exist(tvb, offset, length);
2110 ptr = ensure_contiguous(tvb, offset, length);
2111 strbuf = g_malloc(length + 1);
2113 memcpy(strbuf, ptr, length);
2115 strbuf[length] = '\0';
2119 * Given a tvbuff, an offset, and a length, allocate a buffer big enough
2120 * to hold a non-null-terminated string of that length at that offset,
2121 * plus a trailing '\0', copy the string into it, and return a pointer
2124 * Throws an exception if the tvbuff ends before the string does.
2126 * This function allocates memory from a buffer with packet lifetime.
2127 * You do not have to free this buffer, it will be automatically freed
2128 * when wireshark starts decoding the next packet.
2129 * Do not use this function if you want the allocated memory to be persistent
2130 * after the current packet has been dissected.
2133 tvb_get_ephemeral_string(tvbuff_t *tvb, gint offset, gint length)
2136 guint8 *strbuf = NULL;
2138 tvb_ensure_bytes_exist(tvb, offset, length);
2140 ptr = ensure_contiguous(tvb, offset, length);
2141 strbuf = ep_alloc(length + 1);
2143 memcpy(strbuf, ptr, length);
2145 strbuf[length] = '\0';
2151 * Given a tvbuff and an offset, with the offset assumed to refer to
2152 * a null-terminated string, find the length of that string (and throw
2153 * an exception if the tvbuff ends before we find the null), allocate
2154 * a buffer big enough to hold the string, copy the string into it,
2155 * and return a pointer to the string. Also return the length of the
2156 * string (including the terminating null) through a pointer.
2159 tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp)
2164 size = tvb_strsize(tvb, offset);
2165 strptr = g_malloc(size);
2166 tvb_memcpy(tvb, strptr, offset, size);
2171 * Given a tvbuff and an offset, with the offset assumed to refer to
2172 * a null-terminated string, find the length of that string (and throw
2173 * an exception if the tvbuff ends before we find the null), allocate
2174 * a buffer big enough to hold the string, copy the string into it,
2175 * and return a pointer to the string. Also return the length of the
2176 * string (including the terminating null) through a pointer.
2178 * This function allocates memory from a buffer with packet lifetime.
2179 * You do not have to free this buffer, it will be automatically freed
2180 * when wireshark starts decoding the next packet.
2181 * Do not use this function if you want the allocated memory to be persistent
2182 * after the current packet has been dissected.
2185 tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp)
2190 size = tvb_strsize(tvb, offset);
2191 strptr = ep_alloc(size);
2192 tvb_memcpy(tvb, strptr, offset, size);
2197 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2198 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2199 * Returns length of string (not including terminating NUL), or -1 if the string was
2200 * truncated in the buffer due to not having reached the terminating NUL.
2201 * In this way, it acts like g_snprintf().
2203 * bufsize MUST be greater than 0.
2205 * When processing a packet where the remaining number of bytes is less
2206 * than bufsize, an exception is not thrown if the end of the packet
2207 * is reached before the NUL is found. If no NUL is found before reaching
2208 * the end of the short packet, -1 is still returned, and the string
2209 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2210 * at the correct spot, terminating the string.
2212 * *bytes_copied will contain the number of bytes actually copied,
2213 * including the terminating-NUL.
2216 _tvb_get_nstringz(tvbuff_t *tvb, gint offset, guint bufsize, guint8* buffer,
2220 guint abs_offset, junk_length;
2222 gboolean decreased_max = FALSE;
2224 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
2226 /* There must at least be room for the terminating NUL. */
2227 DISSECTOR_ASSERT(bufsize != 0);
2229 /* If there's no room for anything else, just return the NUL. */
2236 /* Only read to end of tvbuff, w/o throwing exception. */
2237 len = tvb_length_remaining(tvb, abs_offset);
2239 /* check_offset_length() won't throw an exception if we're
2240 * looking at the byte immediately after the end of the tvbuff. */
2242 THROW(ReportedBoundsError);
2245 /* This should not happen because check_offset_length() would
2246 * have already thrown an exception if 'offset' were out-of-bounds.
2248 DISSECTOR_ASSERT(len != -1);
2251 * If we've been passed a negative number, bufsize will
2254 DISSECTOR_ASSERT(bufsize <= G_MAXINT);
2256 if ((guint)len < bufsize) {
2258 decreased_max = TRUE;
2264 stringlen = tvb_strnlen(tvb, abs_offset, limit - 1);
2265 /* If NUL wasn't found, copy the data and return -1 */
2266 if (stringlen == -1) {
2267 tvb_memcpy(tvb, buffer, abs_offset, limit);
2268 if (decreased_max) {
2270 /* Add 1 for the extra NUL that we set at buffer[limit],
2271 * pretending that it was copied as part of the string. */
2272 *bytes_copied = limit + 1;
2275 *bytes_copied = limit;
2280 /* Copy the string to buffer */
2281 tvb_memcpy(tvb, buffer, abs_offset, stringlen + 1);
2282 *bytes_copied = stringlen + 1;
2286 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
2287 * no more than bufsize number of bytes, including terminating NUL, to buffer.
2288 * Returns length of string (not including terminating NUL), or -1 if the string was
2289 * truncated in the buffer due to not having reached the terminating NUL.
2290 * In this way, it acts like g_snprintf().
2292 * When processing a packet where the remaining number of bytes is less
2293 * than bufsize, an exception is not thrown if the end of the packet
2294 * is reached before the NUL is found. If no NUL is found before reaching
2295 * the end of the short packet, -1 is still returned, and the string
2296 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
2297 * at the correct spot, terminating the string.
2300 tvb_get_nstringz(tvbuff_t *tvb, gint offset, guint bufsize, guint8* buffer)
2304 return _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
2307 /* Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
2308 * have a terminating NUL. If the string was truncated when copied into buffer,
2309 * a NUL is placed at the end of buffer to terminate it.
2312 tvb_get_nstringz0(tvbuff_t *tvb, gint offset, guint bufsize, guint8* buffer)
2314 gint len, bytes_copied;
2316 len = _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
2319 buffer[bufsize - 1] = 0;
2320 return bytes_copied - 1;
2328 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2329 * at that offset (which may be -1 for "all the way to the end of the
2330 * tvbuff"), find the end of the (putative) line that starts at the
2331 * specified offset in the tvbuff, going no further than the specified
2334 * Return the length of the line (not counting the line terminator at
2335 * the end), or, if we don't find a line terminator:
2337 * if "deseg" is true, return -1;
2339 * if "deseg" is false, return the amount of data remaining in
2342 * Set "*next_offset" to the offset of the character past the line
2343 * terminator, or past the end of the buffer if we don't find a line
2344 * terminator. (It's not set if we return -1.)
2347 tvb_find_line_end(tvbuff_t *tvb, gint offset, int len, gint *next_offset,
2355 len = tvb_length_remaining(tvb, offset);
2357 * XXX - what if "len" is still -1, meaning "offset is past the
2358 * end of the tvbuff"?
2360 eob_offset = offset + len;
2363 * Look either for a CR or an LF.
2365 eol_offset = tvb_pbrk_guint8(tvb, offset, len, (const guint8 *)"\r\n");
2366 if (eol_offset == -1) {
2368 * No CR or LF - line is presumably continued in next packet.
2372 * Tell our caller we saw no EOL, so they can
2373 * try to desegment and get the entire line
2379 * Pretend the line runs to the end of the tvbuff.
2381 linelen = eob_offset - offset;
2382 *next_offset = eob_offset;
2386 * Find the number of bytes between the starting offset
2389 linelen = eol_offset - offset;
2394 if (tvb_get_guint8(tvb, eol_offset) == '\r') {
2396 * Yes - is it followed by an LF?
2398 if (eol_offset + 1 >= eob_offset) {
2400 * Dunno - the next byte isn't in this
2405 * We'll return -1, although that
2406 * runs the risk that if the line
2407 * really *is* terminated with a CR,
2408 * we won't properly dissect this
2411 * It's probably more likely that
2412 * the line ends with CR-LF than
2413 * that it ends with CR by itself.
2419 * Well, we can at least look at the next
2422 if (tvb_get_guint8(tvb, eol_offset + 1) == '\n') {
2424 * It's an LF; skip over the CR.
2432 * Return the offset of the character after the last
2433 * character in the line, skipping over the last character
2434 * in the line terminator.
2436 *next_offset = eol_offset + 1;
2442 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2443 * at that offset (which may be -1 for "all the way to the end of the
2444 * tvbuff"), find the end of the (putative) line that starts at the
2445 * specified offset in the tvbuff, going no further than the specified
2448 * However, treat quoted strings inside the buffer specially - don't
2449 * treat newlines in quoted strings as line terminators.
2451 * Return the length of the line (not counting the line terminator at
2452 * the end), or the amount of data remaining in the buffer if we don't
2453 * find a line terminator.
2455 * Set "*next_offset" to the offset of the character past the line
2456 * terminator, or past the end of the buffer if we don't find a line
2460 tvb_find_line_end_unquoted(tvbuff_t *tvb, gint offset, int len,
2463 gint cur_offset, char_offset;
2470 len = tvb_length_remaining(tvb, offset);
2472 * XXX - what if "len" is still -1, meaning "offset is past the
2473 * end of the tvbuff"?
2475 eob_offset = offset + len;
2477 cur_offset = offset;
2481 * Is this part of the string quoted?
2485 * Yes - look only for the terminating quote.
2487 char_offset = tvb_find_guint8(tvb, cur_offset, len,
2491 * Look either for a CR, an LF, or a '"'.
2493 char_offset = tvb_pbrk_guint8(tvb, cur_offset, len,
2494 (const guint8 *)"\r\n\"");
2496 if (char_offset == -1) {
2498 * Not found - line is presumably continued in
2500 * We pretend the line runs to the end of the tvbuff.
2502 linelen = eob_offset - offset;
2503 *next_offset = eob_offset;
2509 * We're processing a quoted string.
2510 * We only looked for ", so we know it's a ";
2511 * as we're processing a quoted string, it's a
2519 c = tvb_get_guint8(tvb, char_offset);
2522 * Un-quoted "; it begins a quoted
2528 * It's a CR or LF; we've found a line
2531 * Find the number of bytes between the
2532 * starting offset and the CR or LF.
2534 linelen = char_offset - offset;
2541 * Yes; is it followed by an LF?
2543 if (char_offset + 1 < eob_offset &&
2544 tvb_get_guint8(tvb, char_offset + 1)
2547 * Yes; skip over the CR.
2554 * Return the offset of the character after
2555 * the last character in the line, skipping
2556 * over the last character in the line
2557 * terminator, and quit.
2559 *next_offset = char_offset + 1;
2565 * Step past the character we found.
2567 cur_offset = char_offset + 1;
2568 if (cur_offset >= eob_offset) {
2570 * The character we found was the last character
2571 * in the tvbuff - line is presumably continued in
2573 * We pretend the line runs to the end of the tvbuff.
2575 linelen = eob_offset - offset;
2576 *next_offset = eob_offset;
2584 * Copied from the mgcp dissector. (This function should be moved to /epan )
2585 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
2586 * character following offset or offset + maxlength -1 whichever
2590 * tvb - The tvbuff in which we are skipping whitespace.
2591 * offset - The offset in tvb from which we begin trying to skip whitespace.
2592 * maxlength - The maximum distance from offset that we may try to skip
2595 * Returns: The position in tvb of the first non-whitespace
2596 * character following offset or offset + maxlength -1 whichever
2599 gint tvb_skip_wsp(tvbuff_t* tvb, gint offset, gint maxlength)
2601 gint counter = offset;
2602 gint end = offset + maxlength,tvb_len;
2605 /* Get the length remaining */
2606 tvb_len = tvb_length(tvb);
2607 end = offset + maxlength;
2613 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
2614 for (counter = offset;
2616 ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
2617 tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
2623 gint tvb_skip_wsp_return(tvbuff_t* tvb, gint offset){
2624 gint counter = offset;
2629 for(counter = offset; counter > end &&
2630 ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
2631 tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
2638 * Format a bunch of data from a tvbuff as bytes, returning a pointer
2639 * to the string with the formatted data, with "punct" as a byte
2643 tvb_bytes_to_str_punct(tvbuff_t *tvb, gint offset, gint len, gchar punct)
2645 return bytes_to_str_punct(tvb_get_ptr(tvb, offset, len), len, punct);
2649 * Format a bunch of data from a tvbuff as bytes, returning a pointer
2650 * to the string with the formatted data.
2653 tvb_bytes_to_str(tvbuff_t *tvb, gint offset, gint len)
2655 return bytes_to_str(tvb_get_ptr(tvb, offset, len), len);
2658 /* Find a needle tvbuff within a haystack tvbuff. */
2660 tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, gint haystack_offset)
2662 guint haystack_abs_offset, haystack_abs_length;
2663 const guint8 *haystack_data;
2664 const guint8 *needle_data;
2665 const guint needle_len = needle_tvb->length;
2666 const guint8 *location;
2668 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
2672 /* Get pointers to the tvbuffs' data. */
2673 haystack_data = tvb_get_ptr(haystack_tvb, 0, -1);
2674 needle_data = tvb_get_ptr(needle_tvb, 0, -1);
2676 check_offset_length(haystack_tvb, haystack_offset, -1,
2677 &haystack_abs_offset, &haystack_abs_length);
2679 location = epan_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
2680 needle_data, needle_len);
2683 return location - haystack_data;
2694 * Uncompresses a zlib compressed packet inside a message of tvb at offset with
2695 * length comprlen. Returns an uncompressed tvbuffer if uncompression
2696 * succeeded or NULL if uncompression failed.
2698 #define TVB_Z_MIN_BUFSIZ 32768
2699 #define TVB_Z_MAX_BUFSIZ 1048576 * 10
2700 /* #define TVB_Z_DEBUG 1 */
2704 tvb_uncompress(tvbuff_t *tvb, int offset, int comprlen)
2709 guint bytes_out = 0;
2710 guint8 *compr = NULL;
2711 guint8 *uncompr = NULL;
2712 tvbuff_t *uncompr_tvb = NULL;
2713 z_streamp strm = NULL;
2714 Bytef *strmbuf = NULL;
2715 guint inits_done = 0;
2716 gint wbits = MAX_WBITS;
2717 guint8 *next = NULL;
2718 guint bufsiz = TVB_Z_MIN_BUFSIZ;
2720 guint inflate_passes = 0;
2721 guint bytes_in = tvb_length_remaining(tvb, offset);
2728 strm = g_malloc0(sizeof(z_stream));
2734 compr = tvb_memdup(tvb, offset, comprlen);
2742 * Assume that the uncompressed data is at least twice as big as
2743 * the compressed size.
2745 bufsiz = tvb_length_remaining(tvb, offset) * 2;
2747 if (bufsiz < TVB_Z_MIN_BUFSIZ) {
2748 bufsiz = TVB_Z_MIN_BUFSIZ;
2749 } else if (bufsiz > TVB_Z_MAX_BUFSIZ) {
2750 bufsiz = TVB_Z_MIN_BUFSIZ;
2754 printf("bufsiz: %u bytes\n", bufsiz);
2759 strm->next_in = next;
2760 strm->avail_in = comprlen;
2763 strmbuf = g_malloc0(bufsiz);
2765 if(strmbuf == NULL) {
2771 strm->next_out = strmbuf;
2772 strm->avail_out = bufsiz;
2774 err = inflateInit2(strm, wbits);
2785 memset(strmbuf, '\0', bufsiz);
2786 strm->next_out = strmbuf;
2787 strm->avail_out = bufsiz;
2789 err = inflate(strm, Z_SYNC_FLUSH);
2791 if (err == Z_OK || err == Z_STREAM_END) {
2792 guint bytes_pass = bufsiz - strm->avail_out;
2798 if (uncompr == NULL) {
2799 uncompr = g_memdup(strmbuf, bytes_pass);
2801 guint8 *new_data = g_malloc0(bytes_out +
2804 if (new_data == NULL) {
2810 if (uncompr != NULL) {
2817 g_memmove(new_data, uncompr, bytes_out);
2818 g_memmove((new_data + bytes_out), strmbuf,
2825 bytes_out += bytes_pass;
2827 if ( err == Z_STREAM_END) {
2833 } else if (err == Z_BUF_ERROR) {
2835 * It's possible that not enough frames were captured
2836 * to decompress this fully, so return what we've done
2843 if (uncompr != NULL) {
2850 } else if (err == Z_DATA_ERROR && inits_done == 1
2851 && uncompr == NULL && (*compr == 0x1f) &&
2852 (*(compr + 1) == 0x8b)) {
2854 * inflate() is supposed to handle both gzip and deflate
2855 * streams automatically, but in reality it doesn't
2856 * seem to handle either (at least not within the
2857 * context of an HTTP response.) We have to try
2858 * several tweaks, depending on the type of data and
2859 * version of the library installed.
2863 * Gzip file format. Skip past the header, since the
2864 * fix to make it work (setting windowBits to 31)
2865 * doesn't work with all versions of the library.
2867 Bytef *c = compr + 2;
2870 if (*c == Z_DEFLATED) {
2882 /* Skip past the MTIME, XFL, and OS fields. */
2885 if (flags & (1 << 2)) {
2886 /* An Extra field is present. */
2887 gint xsize = (gint)(*c |
2893 if (flags & (1 << 3)) {
2894 /* A null terminated filename */
2896 while ((c - compr) < comprlen && *c != '\0') {
2903 if (flags & (1 << 4)) {
2904 /* A null terminated comment */
2906 while ((c - compr) < comprlen && *c != '\0') {
2916 strm->next_in = next;
2917 if (c - compr > comprlen) {
2924 comprlen -= (c - compr);
2927 err = inflateInit2(strm, wbits);
2929 } else if (err == Z_DATA_ERROR && uncompr == NULL &&
2933 * Re-init the stream with a negative
2934 * MAX_WBITS. This is necessary due to
2935 * some servers (Apache) not sending
2936 * the deflate header with the
2937 * content-encoded response.
2943 strm->next_in = next;
2944 strm->avail_in = comprlen;
2947 memset(strmbuf, '\0', bufsiz);
2948 strm->next_out = strmbuf;
2949 strm->avail_out = bufsiz;
2951 err = inflateInit2(strm, wbits);
2969 if (uncompr == NULL) {
2978 printf("inflate() total passes: %u\n", inflate_passes);
2979 printf("bytes in: %u\nbytes out: %u\n\n", bytes_in, bytes_out);
2982 if (uncompr != NULL) {
2983 uncompr_tvb = tvb_new_real_data((guint8*) uncompr, bytes_out,
2985 tvb_set_free_cb(uncompr_tvb, g_free);
2992 tvb_uncompress(tvbuff_t *tvb _U_, int offset _U_, int comprlen _U_)