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 * Ethereal - Network traffic analyzer
20 * By Gerald Combs <gerald@ethereal.com>
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.
49 #include "ipv6-utils.h"
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);
136 tvb_free(tvbuff_t* tvb)
138 tvbuff_t *member_tvb;
139 tvb_comp_t *composite;
144 if (tvb->usage_count == 0) {
146 case TVBUFF_REAL_DATA:
149 * XXX - do this with a union?
151 tvb->free_cb((gpointer)tvb->real_data);
156 /* This will be NULL if tvb_new_subset() fails because
157 * reported_length < -1 */
158 if (tvb->tvbuffs.subset.tvb) {
159 tvb_decrement_usage_count(tvb->tvbuffs.subset.tvb, 1);
163 case TVBUFF_COMPOSITE:
164 composite = &tvb->tvbuffs.composite;
165 for (slist = composite->tvbs; slist != NULL ; slist = slist->next) {
166 member_tvb = slist->data;
167 tvb_decrement_usage_count(member_tvb, 1);
170 g_slist_free(composite->tvbs);
172 if (composite->start_offsets)
173 g_free(composite->start_offsets);
174 if (composite->end_offsets)
175 g_free(composite->end_offsets);
176 if (tvb->real_data) {
178 * XXX - do this with a union?
180 g_free((gpointer)tvb->real_data);
187 g_slist_free(tvb->used_in);
190 g_chunk_free(tvb, tvbuff_mem_chunk);
195 tvb_increment_usage_count(tvbuff_t* tvb, guint count)
197 tvb->usage_count += count;
199 return tvb->usage_count;
203 tvb_decrement_usage_count(tvbuff_t* tvb, guint count)
205 if (tvb->usage_count <= count) {
206 tvb->usage_count = 1;
211 tvb->usage_count -= count;
212 return tvb->usage_count;
218 tvb_free_chain(tvbuff_t* tvb)
222 /* Recursively call tvb_free_chain() */
223 for (slist = tvb->used_in; slist != NULL ; slist = slist->next) {
224 tvb_free_chain( (tvbuff_t*)slist->data );
227 /* Stop the recursion */
234 tvb_set_free_cb(tvbuff_t* tvb, tvbuff_free_cb_t func)
236 g_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 g_assert(parent->initialized);
251 g_assert(child->initialized);
252 g_assert(child->type == TVBUFF_REAL_DATA);
253 add_to_used_in_list(parent, child);
257 tvb_set_real_data(tvbuff_t* tvb, const guint8* data, guint length, gint reported_length)
259 g_assert(tvb->type == TVBUFF_REAL_DATA);
260 g_assert(!tvb->initialized);
262 if (reported_length < -1) {
263 THROW(ReportedBoundsError);
266 tvb->real_data = data;
267 tvb->length = length;
268 tvb->reported_length = reported_length;
269 tvb->initialized = TRUE;
273 tvb_new_real_data(const guint8* data, guint length, gint reported_length)
275 static tvbuff_t *last_tvb=NULL;
278 tvb = tvb_new(TVBUFF_REAL_DATA);
283 /* remember this tvb in case we throw an exception and
284 * lose the pointer to it.
288 tvb_set_real_data(tvb, data, length, reported_length);
291 * This is the top-level real tvbuff for this data source,
292 * so its data source tvbuff is itself.
296 /* ok no exception so we dont need to remember it any longer */
302 /* Computes the absolute offset and length based on a possibly-negative offset
303 * and a length that is possible -1 (which means "to the end of the data").
304 * Returns TRUE/FALSE indicating whether the offset is in bounds or
305 * not. The integer ptrs are modified with the new offset and length.
306 * No exception is thrown.
308 * XXX - we return TRUE, not FALSE, if the offset is positive and right
309 * after the end of the tvbuff (i.e., equal to the length). We do this
310 * so that a dissector constructing a subset tvbuff for the next protocol
311 * will get a zero-length tvbuff, not an exception, if there's no data
312 * left for the next protocol - we want the next protocol to be the one
313 * that gets an exception, so the error is reported as an error in that
314 * protocol rather than the containing protocol. */
316 compute_offset_length(tvbuff_t *tvb, gint offset, gint length,
317 guint *offset_ptr, guint *length_ptr, int *exception)
319 g_assert(offset_ptr);
320 g_assert(length_ptr);
322 /* Compute the offset */
324 /* Positive offset - relative to the beginning of the packet. */
325 if ((guint) offset > tvb->reported_length) {
327 *exception = ReportedBoundsError;
331 else if ((guint) offset > tvb->length) {
333 *exception = BoundsError;
338 *offset_ptr = offset;
342 /* Negative offset - relative to the end of the packet. */
343 if ((guint) -offset > tvb->reported_length) {
345 *exception = ReportedBoundsError;
349 else if ((guint) -offset > tvb->length) {
351 *exception = BoundsError;
356 *offset_ptr = tvb->length + offset;
360 /* Compute the length */
363 /* XXX - ReportedBoundsError? */
364 *exception = BoundsError;
368 else if (length == -1) {
369 *length_ptr = tvb->length - *offset_ptr;
372 *length_ptr = length;
380 check_offset_length_no_exception(tvbuff_t *tvb, gint offset, gint length,
381 guint *offset_ptr, guint *length_ptr, int *exception)
385 g_assert(tvb->initialized);
387 if (!compute_offset_length(tvb, offset, length, offset_ptr, length_ptr, exception)) {
392 * Compute the offset of the first byte past the length.
394 end_offset = *offset_ptr + *length_ptr;
397 * Check for an overflow, and clamp "end_offset" at the maximum
398 * if we got an overflow - that should force us to indicate that
399 * we're past the end of the tvbuff.
401 if (end_offset < *offset_ptr)
402 end_offset = UINT_MAX;
405 * Check whether that offset goes more than one byte past the
408 * If not, return TRUE; otherwise, return FALSE and, if "exception"
409 * is non-null, return the appropriate exception through it.
411 if (end_offset <= tvb->length) {
414 else if (end_offset <= tvb->reported_length) {
416 *exception = BoundsError;
422 *exception = ReportedBoundsError;
427 g_assert_not_reached();
430 /* Checks (+/-) offset and length and throws an exception if
431 * either is out of bounds. Sets integer ptrs to the new offset
434 check_offset_length(tvbuff_t *tvb, gint offset, gint length,
435 guint *offset_ptr, guint *length_ptr)
439 if (!check_offset_length_no_exception(tvb, offset, length, offset_ptr, length_ptr, &exception)) {
440 g_assert(exception > 0);
448 tvb_set_subset(tvbuff_t *tvb, tvbuff_t *backing,
449 gint backing_offset, gint backing_length, gint reported_length)
451 g_assert(tvb->type == TVBUFF_SUBSET);
452 g_assert(!tvb->initialized);
454 if (reported_length < -1) {
455 THROW(ReportedBoundsError);
458 check_offset_length(backing, backing_offset, backing_length,
459 &tvb->tvbuffs.subset.offset,
460 &tvb->tvbuffs.subset.length);
462 tvb->tvbuffs.subset.tvb = backing;
463 tvb->length = tvb->tvbuffs.subset.length;
465 if (reported_length == -1) {
466 tvb->reported_length = backing->reported_length - tvb->tvbuffs.subset.offset;
469 tvb->reported_length = reported_length;
471 tvb->initialized = TRUE;
472 add_to_used_in_list(backing, tvb);
474 /* Optimization. If the backing buffer has a pointer to contiguous, real data,
475 * then we can point directly to our starting offset in that buffer */
476 if (backing->real_data != NULL) {
477 tvb->real_data = backing->real_data + tvb->tvbuffs.subset.offset;
483 tvb_new_subset(tvbuff_t *backing, gint backing_offset, gint backing_length, gint reported_length)
485 static tvbuff_t *last_tvb=NULL;
488 tvb = tvb_new(TVBUFF_SUBSET);
493 /* remember this tvb in case we throw an exception and
494 * lose the pointer to it.
498 tvb_set_subset(tvb, backing, backing_offset, backing_length, reported_length);
501 * The top-level data source of this tvbuff is the top-level
502 * data source of its parent.
504 tvb->ds_tvb = backing->ds_tvb;
506 /* ok no exception so we dont need to remember it any longer */
513 tvb_composite_append(tvbuff_t* tvb, tvbuff_t* member)
515 tvb_comp_t *composite;
517 g_assert(!tvb->initialized);
518 composite = &tvb->tvbuffs.composite;
519 composite->tvbs = g_slist_append( composite->tvbs, member );
520 add_to_used_in_list(member, tvb);
524 tvb_composite_prepend(tvbuff_t* tvb, tvbuff_t* member)
526 tvb_comp_t *composite;
528 g_assert(!tvb->initialized);
529 composite = &tvb->tvbuffs.composite;
530 composite->tvbs = g_slist_prepend( composite->tvbs, member );
531 add_to_used_in_list(member, tvb);
535 tvb_new_composite(void)
537 return tvb_new(TVBUFF_COMPOSITE);
541 tvb_composite_finalize(tvbuff_t* tvb)
545 tvbuff_t *member_tvb;
546 tvb_comp_t *composite;
549 g_assert(!tvb->initialized);
550 g_assert(tvb->length == 0);
552 composite = &tvb->tvbuffs.composite;
553 num_members = g_slist_length(composite->tvbs);
555 composite->start_offsets = g_new(guint, num_members);
556 composite->end_offsets = g_new(guint, num_members);
558 for (slist = composite->tvbs; slist != NULL; slist = slist->next) {
559 g_assert((guint) i < num_members);
560 member_tvb = slist->data;
561 composite->start_offsets[i] = tvb->length;
562 tvb->length += member_tvb->length;
563 composite->end_offsets[i] = tvb->length - 1;
567 tvb->initialized = TRUE;
573 tvb_length(tvbuff_t* tvb)
575 g_assert(tvb->initialized);
581 tvb_length_remaining(tvbuff_t *tvb, gint offset)
583 guint abs_offset, abs_length;
585 g_assert(tvb->initialized);
587 if (compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL)) {
596 tvb_ensure_length_remaining(tvbuff_t *tvb, gint offset)
598 guint abs_offset, abs_length;
601 g_assert(tvb->initialized);
603 if (!compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, &exception)) {
606 if (abs_length == 0) {
608 * This routine ensures there's at least one byte available.
609 * There aren't any bytes available, so throw the appropriate
612 if (abs_offset >= tvb->reported_length)
613 THROW(ReportedBoundsError);
623 /* Validates that 'length' bytes are available starting from
624 * offset (pos/neg). Does not throw an exception. */
626 tvb_bytes_exist(tvbuff_t *tvb, gint offset, gint length)
628 guint abs_offset, abs_length;
630 g_assert(tvb->initialized);
632 if (!compute_offset_length(tvb, offset, length, &abs_offset, &abs_length, NULL))
635 if (abs_offset + abs_length <= tvb->length) {
643 /* Validates that 'length' bytes are available starting from
644 * offset (pos/neg). Throws an exception if they aren't. */
646 tvb_ensure_bytes_exist(tvbuff_t *tvb, gint offset, gint length)
648 guint abs_offset, abs_length;
650 g_assert(tvb->initialized);
653 * -1 doesn't mean "until end of buffer", as that's pointless
654 * for this routine. We must treat it as a Really Large Positive
655 * Number, so that we throw an exception; we throw
656 * ReportedBoundsError, as if it were past even the end of a
657 * reassembled packet, and past the end of even the data we
660 * We do the same with other negative lengths.
663 THROW(ReportedBoundsError);
665 check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
669 tvb_offset_exists(tvbuff_t *tvb, gint offset)
671 guint abs_offset, abs_length;
673 g_assert(tvb->initialized);
674 if (!compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL))
677 if (abs_offset < tvb->length) {
686 tvb_reported_length(tvbuff_t* tvb)
688 g_assert(tvb->initialized);
690 return tvb->reported_length;
694 tvb_reported_length_remaining(tvbuff_t *tvb, gint offset)
696 guint abs_offset, abs_length;
698 g_assert(tvb->initialized);
700 if (compute_offset_length(tvb, offset, -1, &abs_offset, &abs_length, NULL)) {
701 if (tvb->reported_length >= abs_offset)
702 return tvb->reported_length - abs_offset;
711 /* Set the reported length of a tvbuff to a given value; used for protocols
712 whose headers contain an explicit length and where the calling
713 dissector's payload may include padding as well as the packet for
716 Also adjusts the data length. */
718 tvb_set_reported_length(tvbuff_t* tvb, guint reported_length)
720 g_assert(tvb->initialized);
722 if (reported_length > tvb->reported_length)
723 THROW(ReportedBoundsError);
725 tvb->reported_length = reported_length;
726 if (reported_length < tvb->length)
727 tvb->length = reported_length;
732 first_real_data_ptr(tvbuff_t *tvb)
737 case TVBUFF_REAL_DATA:
738 return tvb->real_data;
740 member = tvb->tvbuffs.subset.tvb;
741 return first_real_data_ptr(member);
742 case TVBUFF_COMPOSITE:
743 member = tvb->tvbuffs.composite.tvbs->data;
744 return first_real_data_ptr(member);
747 g_assert_not_reached();
752 offset_from_real_beginning(tvbuff_t *tvb, int counter)
757 case TVBUFF_REAL_DATA:
760 member = tvb->tvbuffs.subset.tvb;
761 return offset_from_real_beginning(member, counter + tvb->tvbuffs.subset.offset);
762 case TVBUFF_COMPOSITE:
763 member = tvb->tvbuffs.composite.tvbs->data;
764 return offset_from_real_beginning(member, counter);
767 g_assert_not_reached();
772 composite_ensure_contiguous_no_exception(tvbuff_t *tvb, guint abs_offset,
775 guint i, num_members;
776 tvb_comp_t *composite;
777 tvbuff_t *member_tvb = NULL;
778 guint member_offset, member_length;
781 g_assert(tvb->type == TVBUFF_COMPOSITE);
783 /* Maybe the range specified by offset/length
784 * is contiguous inside one of the member tvbuffs */
785 composite = &tvb->tvbuffs.composite;
786 num_members = g_slist_length(composite->tvbs);
788 for (i = 0; i < num_members; i++) {
789 if (abs_offset <= composite->end_offsets[i]) {
790 slist = g_slist_nth(composite->tvbs, i);
791 member_tvb = slist->data;
795 g_assert(member_tvb);
797 if (check_offset_length_no_exception(member_tvb, abs_offset - composite->start_offsets[i],
798 abs_length, &member_offset, &member_length, NULL)) {
801 * The range is, in fact, contiguous within member_tvb.
803 g_assert(!tvb->real_data);
804 return ensure_contiguous_no_exception(member_tvb, member_offset, member_length, NULL);
807 tvb->real_data = tvb_memdup(tvb, 0, -1);
808 return tvb->real_data + abs_offset;
811 g_assert_not_reached();
816 ensure_contiguous_no_exception(tvbuff_t *tvb, gint offset, gint length,
819 guint abs_offset, abs_length;
821 if (!check_offset_length_no_exception(tvb, offset, length,
822 &abs_offset, &abs_length, exception)) {
827 * We know that all the data is present in the tvbuff, so
828 * no exceptions should be thrown.
830 if (tvb->real_data) {
831 return tvb->real_data + abs_offset;
835 case TVBUFF_REAL_DATA:
836 g_assert_not_reached();
838 return ensure_contiguous_no_exception(tvb->tvbuffs.subset.tvb,
839 abs_offset - tvb->tvbuffs.subset.offset,
841 case TVBUFF_COMPOSITE:
842 return composite_ensure_contiguous_no_exception(tvb, abs_offset, abs_length);
846 g_assert_not_reached();
851 ensure_contiguous(tvbuff_t *tvb, gint offset, gint length)
856 p = ensure_contiguous_no_exception(tvb, offset, length, &exception);
858 g_assert(exception > 0);
865 guint8_find(const guint8* haystack, size_t haystacklen, guint8 needle)
870 for (b = haystack, i = 0; (guint) i < haystacklen; i++, b++) {
880 guint8_pbrk(const guint8* haystack, size_t haystacklen, guint8 *needles)
884 guint8 item, *needlep, needle;
886 for (b = haystack, i = 0; (guint) i < haystacklen; i++, b++) {
889 while ((needle = *needlep) != '\0') {
901 /************** ACCESSORS **************/
904 composite_memcpy(tvbuff_t *tvb, guint8* target, guint abs_offset, guint abs_length)
906 guint i, num_members;
907 tvb_comp_t *composite;
908 tvbuff_t *member_tvb = NULL;
909 guint member_offset, member_length;
913 g_assert(tvb->type == TVBUFF_COMPOSITE);
915 /* Maybe the range specified by offset/length
916 * is contiguous inside one of the member tvbuffs */
917 composite = &tvb->tvbuffs.composite;
918 num_members = g_slist_length(composite->tvbs);
920 for (i = 0; i < num_members; i++) {
921 if (abs_offset <= composite->end_offsets[i]) {
922 slist = g_slist_nth(composite->tvbs, i);
923 member_tvb = slist->data;
927 g_assert(member_tvb);
929 if (check_offset_length_no_exception(member_tvb, abs_offset - composite->start_offsets[i],
930 abs_length, &member_offset, &member_length, NULL)) {
932 g_assert(!tvb->real_data);
933 return tvb_memcpy(member_tvb, target, member_offset, member_length);
936 /* The requested data is non-contiguous inside
937 * the member tvb. We have to memcpy() the part that's in the member tvb,
938 * then iterate across the other member tvb's, copying their portions
939 * until we have copied all data.
941 retval = compute_offset_length(member_tvb, abs_offset - composite->start_offsets[i], -1,
942 &member_offset, &member_length, NULL);
945 tvb_memcpy(member_tvb, target, member_offset, member_length);
946 abs_offset += member_length;
947 abs_length -= member_length;
950 if (abs_length > 0) {
951 composite_memcpy(tvb, target + member_length, abs_offset, abs_length);
957 g_assert_not_reached();
962 tvb_memcpy(tvbuff_t *tvb, guint8* target, gint offset, gint length)
964 guint abs_offset, abs_length;
966 g_assert(length >= -1);
967 check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
969 if (tvb->real_data) {
970 return (guint8*) memcpy(target, tvb->real_data + abs_offset, abs_length);
974 case TVBUFF_REAL_DATA:
975 g_assert_not_reached();
978 return tvb_memcpy(tvb->tvbuffs.subset.tvb, target,
979 abs_offset - tvb->tvbuffs.subset.offset,
982 case TVBUFF_COMPOSITE:
983 return composite_memcpy(tvb, target, offset, length);
986 g_assert_not_reached();
992 * XXX - this doesn't treat a length of -1 as an error.
993 * If it did, this could replace some code that calls
994 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
997 * "composite_ensure_contiguous_no_exception()" depends on -1 not being
998 * an error; does anything else depend on this routine treating -1 as
999 * meaning "to the end of the buffer"?
1002 tvb_memdup(tvbuff_t *tvb, gint offset, gint length)
1004 guint abs_offset, abs_length;
1007 check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
1009 duped = g_malloc(abs_length);
1010 return tvb_memcpy(tvb, duped, abs_offset, abs_length);
1014 * XXX - this doesn't treat a length of -1 as an error.
1015 * If it did, this could replace some code that calls
1016 * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
1019 * "composite_ensure_contiguous_no_exception()" depends on -1 not being
1020 * an error; does anything else depend on this routine treating -1 as
1021 * meaning "to the end of the buffer"?
1023 * This function allocates memory from a buffer with packet lifetime.
1024 * You do not have to free this buffer, it will be automatically freed
1025 * when ethereal starts decoding the next packet.
1026 * Do not use this function if you want the allocated memory to be persistent
1027 * after the current packet has been dissected.
1030 ep_tvb_memdup(tvbuff_t *tvb, gint offset, gint length)
1032 guint abs_offset, abs_length;
1035 check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
1037 duped = ep_alloc(abs_length);
1038 return tvb_memcpy(tvb, duped, abs_offset, abs_length);
1044 tvb_get_ptr(tvbuff_t *tvb, gint offset, gint length)
1046 return ensure_contiguous(tvb, offset, length);
1050 tvb_get_guint8(tvbuff_t *tvb, gint offset)
1054 ptr = ensure_contiguous(tvb, offset, sizeof(guint8));
1059 tvb_get_ntohs(tvbuff_t *tvb, gint offset)
1063 ptr = ensure_contiguous(tvb, offset, sizeof(guint16));
1068 tvb_get_ntoh24(tvbuff_t *tvb, gint offset)
1072 ptr = ensure_contiguous(tvb, offset, 3);
1073 return pntoh24(ptr);
1077 tvb_get_ntohl(tvbuff_t *tvb, gint offset)
1081 ptr = ensure_contiguous(tvb, offset, sizeof(guint32));
1086 tvb_get_ntoh64(tvbuff_t *tvb, gint offset)
1090 ptr = ensure_contiguous(tvb, offset, sizeof(guint64));
1091 return pntoh64(ptr);
1095 * Stuff for IEEE float handling on platforms that don't have IEEE
1096 * format as the native floating-point format.
1098 * For now, we treat only the VAX as such a platform.
1100 * XXX - other non-IEEE boxes that can run UNIX include some Crays,
1101 * and possibly other machines.
1103 * It appears that the official Linux port to System/390 and
1104 * zArchitecture uses IEEE format floating point (not a
1107 * I don't know whether there are any other machines that
1108 * could run Ethereal and that don't use IEEE format.
1109 * As far as I know, all of the main commercial microprocessor
1110 * families on which OSes that support Ethereal can run
1111 * use IEEE format (x86, 68k, SPARC, MIPS, PA-RISC, Alpha,
1112 * IA-64, and so on).
1122 #define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
1123 #define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
1124 #define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
1126 #define IEEE_SP_SIGN_MASK 0x80000000
1127 #define IEEE_SP_EXPONENT_MASK 0x7F800000
1128 #define IEEE_SP_MANTISSA_MASK 0x007FFFFF
1129 #define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
1131 #define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
1132 #define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
1133 #define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
1136 ieee_float_is_zero(guint32 w)
1138 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1142 get_ieee_float(guint32 w)
1148 sign = w & IEEE_SP_SIGN_MASK;
1149 exponent = w & IEEE_SP_EXPONENT_MASK;
1150 mantissa = w & IEEE_SP_MANTISSA_MASK;
1152 if (ieee_float_is_zero(w)) {
1153 /* number is zero, unnormalized, or not-a-number */
1158 * XXX - how to handle this?
1160 if (IEEE_SP_INFINITY == exponent) {
1162 * number is positive or negative infinity, or a special value
1164 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1168 exponent = ((exponent >> IEEE_SP_MANTISSA_WIDTH) - IEEE_SP_BIAS) -
1169 IEEE_SP_MANTISSA_WIDTH;
1170 mantissa |= IEEE_SP_IMPLIED_BIT;
1173 return -mantissa * pow(2, exponent);
1175 return mantissa * pow(2, exponent);
1180 * We assume that if you don't have IEEE floating-point, you have a
1181 * compiler that understands 64-bit integral quantities.
1183 #define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
1184 #define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
1185 #define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
1187 #define IEEE_DP_SIGN_MASK 0x8000000000000000LL
1188 #define IEEE_DP_EXPONENT_MASK 0x7FF0000000000000LL
1189 #define IEEE_DP_MANTISSA_MASK 0x000FFFFFFFFFFFFFLL
1190 #define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
1192 #define IEEE_DP_IMPLIED_BIT (1LL << IEEE_DP_MANTISSA_WIDTH)
1193 #define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
1194 #define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
1197 ieee_double_is_zero(guint64 w)
1199 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1203 get_ieee_double(guint64 w)
1209 sign = w & IEEE_DP_SIGN_MASK;
1210 exponent = w & IEEE_DP_EXPONENT_MASK;
1211 mantissa = w & IEEE_DP_MANTISSA_MASK;
1213 if (ieee_double_is_zero(w)) {
1214 /* number is zero, unnormalized, or not-a-number */
1219 * XXX - how to handle this?
1221 if (IEEE_DP_INFINITY == exponent) {
1223 * number is positive or negative infinity, or a special value
1225 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1229 exponent = ((exponent >> IEEE_DP_MANTISSA_WIDTH) - IEEE_DP_BIAS) -
1230 IEEE_DP_MANTISSA_WIDTH;
1231 mantissa |= IEEE_DP_IMPLIED_BIT;
1234 return -mantissa * pow(2, exponent);
1236 return mantissa * pow(2, exponent);
1241 * Fetches an IEEE single-precision floating-point number, in
1242 * big-endian form, and returns a "float".
1244 * XXX - should this be "double", in case there are IEEE single-
1245 * precision numbers that won't fit in some platform's native
1249 tvb_get_ntohieee_float(tvbuff_t *tvb, int offset)
1252 return get_ieee_float(tvb_get_ntohl(tvb, offset));
1259 ieee_fp_union.w = tvb_get_ntohl(tvb, offset);
1260 return ieee_fp_union.f;
1265 * Fetches an IEEE double-precision floating-point number, in
1266 * big-endian form, and returns a "double".
1269 tvb_get_ntohieee_double(tvbuff_t *tvb, int offset)
1283 #ifdef WORDS_BIGENDIAN
1284 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset);
1285 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset+4);
1287 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset+4);
1288 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset);
1291 return get_ieee_double(ieee_fp_union.dw);
1293 return ieee_fp_union.d;
1298 tvb_get_letohs(tvbuff_t *tvb, gint offset)
1302 ptr = ensure_contiguous(tvb, offset, sizeof(guint16));
1303 return pletohs(ptr);
1307 tvb_get_letoh24(tvbuff_t *tvb, gint offset)
1311 ptr = ensure_contiguous(tvb, offset, 3);
1312 return pletoh24(ptr);
1316 tvb_get_letohl(tvbuff_t *tvb, gint offset)
1320 ptr = ensure_contiguous(tvb, offset, sizeof(guint32));
1321 return pletohl(ptr);
1325 tvb_get_letoh64(tvbuff_t *tvb, gint offset)
1329 ptr = ensure_contiguous(tvb, offset, sizeof(guint64));
1330 return pletoh64(ptr);
1334 * Fetches an IEEE single-precision floating-point number, in
1335 * little-endian form, and returns a "float".
1337 * XXX - should this be "double", in case there are IEEE single-
1338 * precision numbers that won't fit in some platform's native
1342 tvb_get_letohieee_float(tvbuff_t *tvb, int offset)
1345 return get_ieee_float(tvb_get_letohl(tvb, offset));
1352 ieee_fp_union.w = tvb_get_letohl(tvb, offset);
1353 return ieee_fp_union.f;
1358 * Fetches an IEEE double-precision floating-point number, in
1359 * little-endian form, and returns a "double".
1362 tvb_get_letohieee_double(tvbuff_t *tvb, int offset)
1376 #ifdef WORDS_BIGENDIAN
1377 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset+4);
1378 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset);
1380 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset);
1381 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset+4);
1384 return get_ieee_double(ieee_fp_union.dw);
1386 return ieee_fp_union.d;
1390 /* Fetch an IPv4 address, in network byte order.
1391 * We do *not* convert them to host byte order; we leave them in
1392 * network byte order. */
1394 tvb_get_ipv4(tvbuff_t *tvb, gint offset)
1399 ptr = ensure_contiguous(tvb, offset, sizeof(guint32));
1400 memcpy(&addr, ptr, sizeof addr);
1404 /* Fetch an IPv6 address. */
1406 tvb_get_ipv6(tvbuff_t *tvb, gint offset, struct e_in6_addr *addr)
1410 ptr = ensure_contiguous(tvb, offset, sizeof(*addr));
1411 memcpy(addr, ptr, sizeof *addr);
1414 /* Find first occurence of needle in tvbuff, starting at offset. Searches
1415 * at most maxlength number of bytes; if maxlength is -1, searches to
1417 * Returns the offset of the found needle, or -1 if not found.
1418 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1419 * in that case, -1 will be returned if the boundary is reached before
1420 * finding needle. */
1422 tvb_find_guint8(tvbuff_t *tvb, gint offset, gint maxlength, guint8 needle)
1424 const guint8 *result;
1425 guint abs_offset, junk_length;
1429 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1431 /* Only search to end of tvbuff, w/o throwing exception. */
1432 tvbufflen = tvb_length_remaining(tvb, abs_offset);
1433 if (maxlength == -1) {
1434 /* No maximum length specified; search to end of tvbuff. */
1437 else if (tvbufflen < (guint) maxlength) {
1438 /* Maximum length goes past end of tvbuff; search to end
1443 /* Maximum length doesn't go past end of tvbuff; search
1448 /* If we have real data, perform our search now. */
1449 if (tvb->real_data) {
1450 result = guint8_find(tvb->real_data + abs_offset, limit, needle);
1451 if (result == NULL) {
1455 return result - tvb->real_data;
1460 case TVBUFF_REAL_DATA:
1461 g_assert_not_reached();
1464 return tvb_find_guint8(tvb->tvbuffs.subset.tvb,
1465 abs_offset - tvb->tvbuffs.subset.offset,
1468 case TVBUFF_COMPOSITE:
1469 g_assert_not_reached();
1470 /* XXX - return composite_find_guint8(tvb, offset, limit, needle); */
1473 g_assert_not_reached();
1477 /* Find first occurence of any of the needles in tvbuff, starting at offset.
1478 * Searches at most maxlength number of bytes; if maxlength is -1, searches
1480 * Returns the offset of the found needle, or -1 if not found.
1481 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
1482 * in that case, -1 will be returned if the boundary is reached before
1483 * finding needle. */
1485 tvb_pbrk_guint8(tvbuff_t *tvb, gint offset, gint maxlength, guint8 *needles)
1487 const guint8 *result;
1488 guint abs_offset, junk_length;
1492 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1494 /* Only search to end of tvbuff, w/o throwing exception. */
1495 tvbufflen = tvb_length_remaining(tvb, abs_offset);
1496 if (maxlength == -1) {
1497 /* No maximum length specified; search to end of tvbuff. */
1500 else if (tvbufflen < (guint) maxlength) {
1501 /* Maximum length goes past end of tvbuff; search to end
1506 /* Maximum length doesn't go past end of tvbuff; search
1511 /* If we have real data, perform our search now. */
1512 if (tvb->real_data) {
1513 result = guint8_pbrk(tvb->real_data + abs_offset, limit, needles);
1514 if (result == NULL) {
1518 return result - tvb->real_data;
1523 case TVBUFF_REAL_DATA:
1524 g_assert_not_reached();
1527 return tvb_pbrk_guint8(tvb->tvbuffs.subset.tvb,
1528 abs_offset - tvb->tvbuffs.subset.offset,
1531 case TVBUFF_COMPOSITE:
1532 g_assert_not_reached();
1533 /* XXX - return composite_pbrk_guint8(tvb, offset, limit, needle); */
1536 g_assert_not_reached();
1540 /* Find size of stringz (NUL-terminated string) by looking for terminating
1541 * NUL. The size of the string includes the terminating NUL.
1543 * If the NUL isn't found, it throws the appropriate exception.
1546 tvb_strsize(tvbuff_t *tvb, gint offset)
1548 guint abs_offset, junk_length;
1551 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1552 nul_offset = tvb_find_guint8(tvb, abs_offset, -1, 0);
1553 if (nul_offset == -1) {
1555 * OK, we hit the end of the tvbuff, so we should throw
1558 * Did we hit the end of the captured data, or the end
1559 * of the actual data? If there's less captured data
1560 * than actual data, we presumably hit the end of the
1561 * captured data, otherwise we hit the end of the actual
1564 if (tvb_length(tvb) < tvb_reported_length(tvb)) {
1567 THROW(ReportedBoundsError);
1570 return (nul_offset - abs_offset) + 1;
1573 /* Find length of string by looking for end of string ('\0'), up to
1574 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
1576 * Returns -1 if 'maxlength' reached before finding EOS. */
1578 tvb_strnlen(tvbuff_t *tvb, gint offset, guint maxlength)
1581 guint abs_offset, junk_length;
1583 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1585 result_offset = tvb_find_guint8(tvb, abs_offset, maxlength, 0);
1587 if (result_offset == -1) {
1591 return result_offset - abs_offset;
1596 * Implement strneql etc
1600 * Call strncmp after checking if enough chars left, returning 0 if
1601 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1604 tvb_strneql(tvbuff_t *tvb, gint offset, const gchar *str, gint size)
1608 ptr = ensure_contiguous_no_exception(tvb, offset, size, NULL);
1611 int cmp = strncmp((const char *)ptr, str, size);
1614 * Return 0 if equal, -1 otherwise.
1616 return (cmp == 0 ? 0 : -1);
1619 * Not enough characters in the tvbuff to match the
1627 * Call strncasecmp after checking if enough chars left, returning 0 if
1628 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1631 tvb_strncaseeql(tvbuff_t *tvb, gint offset, const gchar *str, gint size)
1635 ptr = ensure_contiguous_no_exception(tvb, offset, size, NULL);
1638 int cmp = strncasecmp((const char *)ptr, str, size);
1641 * Return 0 if equal, -1 otherwise.
1643 return (cmp == 0 ? 0 : -1);
1646 * Not enough characters in the tvbuff to match the
1654 * Call memcmp after checking if enough chars left, returning 0 if
1655 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
1658 tvb_memeql(tvbuff_t *tvb, gint offset, const guint8 *str, gint size)
1662 ptr = ensure_contiguous_no_exception(tvb, offset, size, NULL);
1665 int cmp = memcmp(ptr, str, size);
1668 * Return 0 if equal, -1 otherwise.
1670 return (cmp == 0 ? 0 : -1);
1673 * Not enough characters in the tvbuff to match the
1680 /* Convert a string from Unicode to ASCII. At the moment we fake it by
1681 * replacing all non-ASCII characters with a '.' )-: The caller must
1682 * free the result returned. The len parameter is the number of guint16's
1683 * to convert from Unicode. */
1685 tvb_fake_unicode(tvbuff_t *tvb, int offset, int len, gboolean little_endian)
1691 /* Make sure we have enough data before allocating the buffer,
1692 so we don't blow up if the length is huge. */
1693 tvb_ensure_bytes_exist(tvb, offset, 2*len);
1695 /* We know we won't throw an exception, so we don't have to worry
1696 about leaking this buffer. */
1697 buffer = g_malloc(len + 1);
1699 for (i = 0; i < len; i++) {
1700 character = little_endian ? tvb_get_letohs(tvb, offset)
1701 : tvb_get_ntohs(tvb, offset);
1702 buffer[i] = character < 256 ? character : '.';
1711 /* Convert a string from Unicode to ASCII. At the moment we fake it by
1712 * replacing all non-ASCII characters with a '.' )-: The len parameter is
1713 * the number of guint16's to convert from Unicode.
1715 * This function allocates memory from a buffer with packet lifetime.
1716 * You do not have to free this buffer, it will be automatically freed
1717 * when ethereal starts decoding the next packet.
1720 tvb_get_ephemeral_faked_unicode(tvbuff_t *tvb, int offset, int len, gboolean little_endian)
1726 /* Make sure we have enough data before allocating the buffer,
1727 so we don't blow up if the length is huge. */
1728 tvb_ensure_bytes_exist(tvb, offset, 2*len);
1730 /* We know we won't throw an exception, so we don't have to worry
1731 about leaking this buffer. */
1732 buffer = ep_alloc(len + 1);
1734 for (i = 0; i < len; i++) {
1735 character = little_endian ? tvb_get_letohs(tvb, offset)
1736 : tvb_get_ntohs(tvb, offset);
1737 buffer[i] = character < 256 ? character : '.';
1747 * Format the data in the tvb from offset for length ...
1751 tvb_format_text(tvbuff_t *tvb, gint offset, gint size)
1756 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
1758 len = tvb_length_remaining(tvb, offset);
1759 ptr = ensure_contiguous(tvb, offset, len);
1763 return format_text(ptr, len);
1768 * Like "tvb_format_text()", but for null-padded strings; don't show
1769 * the null padding characters as "\000".
1772 tvb_format_stringzpad(tvbuff_t *tvb, gint offset, gint size)
1774 const guint8 *ptr, *p;
1778 if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
1780 len = tvb_length_remaining(tvb, offset);
1781 ptr = ensure_contiguous(tvb, offset, len);
1785 for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
1787 return format_text(ptr, stringlen);
1792 * Given a tvbuff, an offset, and a length, allocate a buffer big enough
1793 * to hold a non-null-terminated string of that length at that offset,
1794 * plus a trailing '\0', copy the string into it, and return a pointer
1797 * Throws an exception if the tvbuff ends before the string does.
1800 tvb_get_string(tvbuff_t *tvb, gint offset, gint length)
1803 guint8 *strbuf = NULL;
1805 tvb_ensure_bytes_exist(tvb, offset, length);
1807 ptr = ensure_contiguous(tvb, offset, length);
1808 strbuf = g_malloc(length + 1);
1810 memcpy(strbuf, ptr, length);
1812 strbuf[length] = '\0';
1816 * Given a tvbuff, an offset, and a length, allocate a buffer big enough
1817 * to hold a non-null-terminated string of that length at that offset,
1818 * plus a trailing '\0', copy the string into it, and return a pointer
1821 * Throws an exception if the tvbuff ends before the string does.
1823 * This function allocates memory from a buffer with packet lifetime.
1824 * You do not have to free this buffer, it will be automatically freed
1825 * when ethereal starts decoding the next packet.
1826 * Do not use this function if you want the allocated memory to be persistent
1827 * after the current packet has been dissected.
1830 tvb_get_ephemeral_string(tvbuff_t *tvb, gint offset, gint length)
1833 guint8 *strbuf = NULL;
1835 tvb_ensure_bytes_exist(tvb, offset, length);
1837 ptr = ensure_contiguous(tvb, offset, length);
1838 strbuf = ep_alloc(length + 1);
1840 memcpy(strbuf, ptr, length);
1842 strbuf[length] = '\0';
1848 * Given a tvbuff and an offset, with the offset assumed to refer to
1849 * a null-terminated string, find the length of that string (and throw
1850 * an exception if the tvbuff ends before we find the null), allocate
1851 * a buffer big enough to hold the string, copy the string into it,
1852 * and return a pointer to the string. Also return the length of the
1853 * string (including the terminating null) through a pointer.
1856 tvb_get_stringz(tvbuff_t *tvb, gint offset, gint *lengthp)
1861 size = tvb_strsize(tvb, offset);
1862 strptr = g_malloc(size);
1863 tvb_memcpy(tvb, strptr, offset, size);
1868 * Given a tvbuff and an offset, with the offset assumed to refer to
1869 * a null-terminated string, find the length of that string (and throw
1870 * an exception if the tvbuff ends before we find the null), allocate
1871 * a buffer big enough to hold the string, copy the string into it,
1872 * and return a pointer to the string. Also return the length of the
1873 * string (including the terminating null) through a pointer.
1875 * This function allocates memory from a buffer with packet lifetime.
1876 * You do not have to free this buffer, it will be automatically freed
1877 * when ethereal starts decoding the next packet.
1878 * Do not use this function if you want the allocated memory to be persistent
1879 * after the current packet has been dissected.
1882 tvb_get_ephemeral_stringz(tvbuff_t *tvb, gint offset, gint *lengthp)
1887 size = tvb_strsize(tvb, offset);
1888 strptr = ep_alloc(size);
1889 tvb_memcpy(tvb, strptr, offset, size);
1894 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
1895 * no more than bufsize number of bytes, including terminating NUL, to buffer.
1896 * Returns length of string (not including terminating NUL), or -1 if the string was
1897 * truncated in the buffer due to not having reached the terminating NUL.
1898 * In this way, it acts like g_snprintf().
1900 * bufsize MUST be greater than 0.
1902 * When processing a packet where the remaining number of bytes is less
1903 * than bufsize, an exception is not thrown if the end of the packet
1904 * is reached before the NUL is found. If no NUL is found before reaching
1905 * the end of the short packet, -1 is still returned, and the string
1906 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
1907 * at the correct spot, terminating the string.
1909 * *bytes_copied will contain the number of bytes actually copied,
1910 * including the terminating-NUL.
1913 _tvb_get_nstringz(tvbuff_t *tvb, gint offset, guint bufsize, guint8* buffer,
1917 guint abs_offset, junk_length;
1919 gboolean decreased_max = FALSE;
1921 check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
1923 /* There must at least be room for the terminating NUL. */
1924 g_assert(bufsize != 0);
1926 /* If there's no room for anything else, just return the NUL. */
1933 /* Only read to end of tvbuff, w/o throwing exception. */
1934 len = tvb_length_remaining(tvb, abs_offset);
1936 /* check_offset_length() won't throw an exception if we're
1937 * looking at the byte immediately after the end of the tvbuff. */
1939 THROW(ReportedBoundsError);
1942 /* This should not happen because check_offset_length() would
1943 * have already thrown an exception if 'offset' were out-of-bounds.
1945 g_assert(len != -1);
1948 * If we've been passed a negative number, bufsize will
1951 g_assert(bufsize <= G_MAXINT);
1953 if ((guint)len < bufsize) {
1955 decreased_max = TRUE;
1961 stringlen = tvb_strnlen(tvb, abs_offset, limit - 1);
1962 /* If NUL wasn't found, copy the data and return -1 */
1963 if (stringlen == -1) {
1964 tvb_memcpy(tvb, buffer, abs_offset, limit);
1965 if (decreased_max) {
1967 /* Add 1 for the extra NUL that we set at buffer[limit],
1968 * pretending that it was copied as part of the string. */
1969 *bytes_copied = limit + 1;
1972 *bytes_copied = limit;
1977 /* Copy the string to buffer */
1978 tvb_memcpy(tvb, buffer, abs_offset, stringlen + 1);
1979 *bytes_copied = stringlen + 1;
1983 /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
1984 * no more than bufsize number of bytes, including terminating NUL, to buffer.
1985 * Returns length of string (not including terminating NUL), or -1 if the string was
1986 * truncated in the buffer due to not having reached the terminating NUL.
1987 * In this way, it acts like g_snprintf().
1989 * When processing a packet where the remaining number of bytes is less
1990 * than bufsize, an exception is not thrown if the end of the packet
1991 * is reached before the NUL is found. If no NUL is found before reaching
1992 * the end of the short packet, -1 is still returned, and the string
1993 * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
1994 * at the correct spot, terminating the string.
1997 tvb_get_nstringz(tvbuff_t *tvb, gint offset, guint bufsize, guint8* buffer)
2001 return _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
2004 /* Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
2005 * have a terminating NUL. If the string was truncated when copied into buffer,
2006 * a NUL is placed at the end of buffer to terminate it.
2009 tvb_get_nstringz0(tvbuff_t *tvb, gint offset, guint bufsize, guint8* buffer)
2011 gint len, bytes_copied;
2013 len = _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
2016 buffer[bufsize - 1] = 0;
2017 return bytes_copied - 1;
2025 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2026 * at that offset (which may be -1 for "all the way to the end of the
2027 * tvbuff"), find the end of the (putative) line that starts at the
2028 * specified offset in the tvbuff, going no further than the specified
2031 * Return the length of the line (not counting the line terminator at
2032 * the end), or, if we don't find a line terminator:
2034 * if "deseg" is true, return -1;
2036 * if "deseg" is false, return the amount of data remaining in
2039 * Set "*next_offset" to the offset of the character past the line
2040 * terminator, or past the end of the buffer if we don't find a line
2041 * terminator. (It's not set if we return -1.)
2044 tvb_find_line_end(tvbuff_t *tvb, gint offset, int len, gint *next_offset,
2052 len = tvb_length_remaining(tvb, offset);
2054 * XXX - what if "len" is still -1, meaning "offset is past the
2055 * end of the tvbuff"?
2057 eob_offset = offset + len;
2060 * Look either for a CR or an LF.
2062 eol_offset = tvb_pbrk_guint8(tvb, offset, len, (guint8 *)"\r\n");
2063 if (eol_offset == -1) {
2065 * No CR or LF - line is presumably continued in next packet.
2069 * Tell our caller we saw no EOL, so they can
2070 * try to desegment and get the entire line
2076 * Pretend the line runs to the end of the tvbuff.
2078 linelen = eob_offset - offset;
2079 *next_offset = eob_offset;
2083 * Find the number of bytes between the starting offset
2086 linelen = eol_offset - offset;
2091 if (tvb_get_guint8(tvb, eol_offset) == '\r') {
2093 * Yes - is it followed by an LF?
2095 if (eol_offset + 1 >= eob_offset) {
2097 * Dunno - the next byte isn't in this
2102 * We'll return -1, although that
2103 * runs the risk that if the line
2104 * really *is* terminated with a CR,
2105 * we won't properly dissect this
2108 * It's probably more likely that
2109 * the line ends with CR-LF than
2110 * that it ends with CR by itself.
2116 * Well, we can at least look at the next
2119 if (tvb_get_guint8(tvb, eol_offset + 1) == '\n') {
2121 * It's an LF; skip over the CR.
2129 * Return the offset of the character after the last
2130 * character in the line, skipping over the last character
2131 * in the line terminator.
2133 *next_offset = eol_offset + 1;
2139 * Given a tvbuff, an offset into the tvbuff, and a length that starts
2140 * at that offset (which may be -1 for "all the way to the end of the
2141 * tvbuff"), find the end of the (putative) line that starts at the
2142 * specified offset in the tvbuff, going no further than the specified
2145 * However, treat quoted strings inside the buffer specially - don't
2146 * treat newlines in quoted strings as line terminators.
2148 * Return the length of the line (not counting the line terminator at
2149 * the end), or the amount of data remaining in the buffer if we don't
2150 * find a line terminator.
2152 * Set "*next_offset" to the offset of the character past the line
2153 * terminator, or past the end of the buffer if we don't find a line
2157 tvb_find_line_end_unquoted(tvbuff_t *tvb, gint offset, int len,
2160 gint cur_offset, char_offset;
2167 len = tvb_length_remaining(tvb, offset);
2169 * XXX - what if "len" is still -1, meaning "offset is past the
2170 * end of the tvbuff"?
2172 eob_offset = offset + len;
2174 cur_offset = offset;
2178 * Is this part of the string quoted?
2182 * Yes - look only for the terminating quote.
2184 char_offset = tvb_find_guint8(tvb, cur_offset, len,
2188 * Look either for a CR, an LF, or a '"'.
2190 char_offset = tvb_pbrk_guint8(tvb, cur_offset, len,
2191 (guint8 *)"\r\n\"");
2193 if (char_offset == -1) {
2195 * Not found - line is presumably continued in
2197 * We pretend the line runs to the end of the tvbuff.
2199 linelen = eob_offset - offset;
2200 *next_offset = eob_offset;
2206 * We're processing a quoted string.
2207 * We only looked for ", so we know it's a ";
2208 * as we're processing a quoted string, it's a
2216 c = tvb_get_guint8(tvb, char_offset);
2219 * Un-quoted "; it begins a quoted
2225 * It's a CR or LF; we've found a line
2228 * Find the number of bytes between the
2229 * starting offset and the CR or LF.
2231 linelen = char_offset - offset;
2238 * Yes; is it followed by an LF?
2240 if (char_offset + 1 < eob_offset &&
2241 tvb_get_guint8(tvb, char_offset + 1)
2244 * Yes; skip over the CR.
2251 * Return the offset of the character after
2252 * the last character in the line, skipping
2253 * over the last character in the line
2254 * terminator, and quit.
2256 *next_offset = char_offset + 1;
2262 * Step past the character we found.
2264 cur_offset = char_offset + 1;
2265 if (cur_offset >= eob_offset) {
2267 * The character we found was the last character
2268 * in the tvbuff - line is presumably continued in
2270 * We pretend the line runs to the end of the tvbuff.
2272 linelen = eob_offset - offset;
2273 *next_offset = eob_offset;
2281 * Format a bunch of data from a tvbuff as bytes, returning a pointer
2282 * to the string with the formatted data, with "punct" as a byte
2286 tvb_bytes_to_str_punct(tvbuff_t *tvb, gint offset, gint len, gchar punct)
2288 return bytes_to_str_punct(tvb_get_ptr(tvb, offset, len), len, punct);
2292 * Format a bunch of data from a tvbuff as bytes, returning a pointer
2293 * to the string with the formatted data.
2296 tvb_bytes_to_str(tvbuff_t *tvb, gint offset, gint len)
2298 return bytes_to_str(tvb_get_ptr(tvb, offset, len), len);
2301 /* Find a needle tvbuff within a haystack tvbuff. */
2303 tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, gint haystack_offset)
2305 guint haystack_abs_offset, haystack_abs_length;
2306 const guint8 *haystack_data;
2307 const guint8 *needle_data;
2308 const guint needle_len = needle_tvb->length;
2309 const guint8 *location;
2311 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
2315 /* Get pointers to the tvbuffs' data. */
2316 haystack_data = tvb_get_ptr(haystack_tvb, 0, -1);
2317 needle_data = tvb_get_ptr(needle_tvb, 0, -1);
2319 check_offset_length(haystack_tvb, haystack_offset, -1,
2320 &haystack_abs_offset, &haystack_abs_length);
2322 location = epan_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
2323 needle_data, needle_len);
2326 return location - haystack_data;
2337 * Uncompresses a zlib compressed packet inside a message of tvb at offset with
2338 * length comprlen. Returns an uncompressed tvbuffer if uncompression
2339 * succeeded or NULL if uncompression failed.
2341 #define TVB_Z_MIN_BUFSIZ 32768
2342 #define TVB_Z_MAX_BUFSIZ 1048576 * 10
2343 /* #define TVB_Z_DEBUG 1 */
2347 tvb_uncompress(tvbuff_t *tvb, int offset, int comprlen)
2352 guint bytes_out = 0;
2353 guint8 *compr = NULL;
2354 guint8 *uncompr = NULL;
2355 tvbuff_t *uncompr_tvb = NULL;
2356 z_streamp strm = NULL;
2357 Bytef *strmbuf = NULL;
2358 guint inits_done = 0;
2359 gint wbits = MAX_WBITS;
2360 guint8 *next = NULL;
2361 guint bufsiz = TVB_Z_MIN_BUFSIZ;
2363 guint inflate_passes = 0;
2364 guint bytes_in = tvb_length_remaining(tvb, offset);
2371 strm = g_malloc0(sizeof(z_stream));
2377 compr = tvb_memdup(tvb, offset, comprlen);
2385 * Assume that the uncompressed data is at least twice as big as
2386 * the compressed size.
2388 bufsiz = tvb_length_remaining(tvb, offset) * 2;
2390 if (bufsiz < TVB_Z_MIN_BUFSIZ) {
2391 bufsiz = TVB_Z_MIN_BUFSIZ;
2392 } else if (bufsiz > TVB_Z_MAX_BUFSIZ) {
2393 bufsiz = TVB_Z_MIN_BUFSIZ;
2397 printf("bufsiz: %u bytes\n", bufsiz);
2402 strm->next_in = next;
2403 strm->avail_in = comprlen;
2406 strmbuf = g_malloc0(bufsiz);
2408 if(strmbuf == NULL) {
2414 strm->next_out = strmbuf;
2415 strm->avail_out = bufsiz;
2417 err = inflateInit2(strm, wbits);
2427 memset(strmbuf, '\0', bufsiz);
2428 strm->next_out = strmbuf;
2429 strm->avail_out = bufsiz;
2431 err = inflate(strm, Z_SYNC_FLUSH);
2433 if (err == Z_OK || err == Z_STREAM_END) {
2434 guint bytes_pass = bufsiz - strm->avail_out;
2440 if (uncompr == NULL) {
2441 uncompr = g_memdup(strmbuf, bytes_pass);
2443 guint8 *new_data = g_malloc0(bytes_out +
2446 if (new_data == NULL) {
2451 if (uncompr != NULL) {
2458 g_memmove(new_data, uncompr, bytes_out);
2459 g_memmove((new_data + bytes_out), strmbuf,
2466 bytes_out += bytes_pass;
2468 if ( err == Z_STREAM_END) {
2474 } else if (err == Z_BUF_ERROR) {
2476 * It's possible that not enough frames were captured
2477 * to decompress this fully, so return what we've done
2484 if (uncompr != NULL) {
2491 } else if (err == Z_DATA_ERROR && inits_done == 1
2492 && uncompr == NULL && (*compr == 0x1f) &&
2493 (*(compr + 1) == 0x8b)) {
2495 * inflate() is supposed to handle both gzip and deflate
2496 * streams automatically, but in reality it doesn't
2497 * seem to handle either (at least not within the
2498 * context of an HTTP response.) We have to try
2499 * several tweaks, depending on the type of data and
2500 * version of the library installed.
2504 * Gzip file format. Skip past the header, since the
2505 * fix to make it work (setting windowBits to 31)
2506 * doesn't work with all versions of the library.
2508 Bytef *c = compr + 2;
2511 if (*c == Z_DEFLATED) {
2522 /* Skip past the MTIME, XFL, and OS fields. */
2525 if (flags & (1 << 2)) {
2526 /* An Extra field is present. */
2527 gint xsize = (gint)(*c |
2533 if (flags & (1 << 3)) {
2534 /* A null terminated filename */
2536 while (*c != '\0') {
2543 if (flags & (1 << 4)) {
2544 /* A null terminated comment */
2546 while (*c != '\0') {
2556 strm->next_in = next;
2557 if (c - compr > comprlen) {
2563 comprlen -= (c - compr);
2565 err = inflateInit2(strm, wbits);
2567 } else if (err == Z_DATA_ERROR && uncompr == NULL &&
2571 * Re-init the stream with a negative
2572 * MAX_WBITS. This is necessary due to
2573 * some servers (Apache) not sending
2574 * the deflate header with the
2575 * content-encoded response.
2581 strm->next_in = next;
2582 strm->avail_in = comprlen;
2584 memset(strmbuf, '\0', bufsiz);
2585 strm->next_out = strmbuf;
2586 strm->avail_out = bufsiz;
2588 err = inflateInit2(strm, wbits);
2605 if (uncompr == NULL) {
2614 printf("inflate() total passes: %u\n", inflate_passes);
2615 printf("bytes in: %u\nbytes out: %u\n\n", bytes_in, bytes_out);
2618 if (uncompr != NULL) {
2619 uncompr_tvb = tvb_new_real_data((guint8*) uncompr, bytes_out,
2621 tvb_set_free_cb(uncompr_tvb, g_free);
2628 tvb_uncompress(tvbuff_t *tvb _U_, int offset _U_, int comprlen _U_)