Bug Summary

File:epan/tvbuff.c
Warning:line 589, column 10
Potential leak of memory pointed to by 'buf'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name tvbuff.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-21/lib/clang/21 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/epan -isystem /builds/wireshark/wireshark/build/epan -isystem /usr/include/mit-krb5 -isystem /usr/include/libxml2 -isystem /usr/include/lua5.4 -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D epan_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu11 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /builds/wireshark/wireshark/sbout/2025-12-31-100335-3583-1 -x c /builds/wireshark/wireshark/epan/tvbuff.c
1/* tvbuff.c
2 *
3 * Testy, Virtual(-izable) Buffer of uint8_t*'s
4 *
5 * "Testy" -- the buffer gets mad when an attempt to access data
6 * beyond the bounds of the buffer. An exception is thrown.
7 *
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
10 * other tvbuffs.
11 *
12 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
13 *
14 * Code to convert IEEE floating point formats to native floating point
15 * derived from code Copyright (c) Ashok Narayanan, 2000
16 *
17 * Wireshark - Network traffic analyzer
18 * By Gerald Combs <gerald@wireshark.org>
19 * Copyright 1998 Gerald Combs
20 *
21 * SPDX-License-Identifier: GPL-2.0-or-later
22 */
23
24#include "config.h"
25
26#include <string.h>
27#include <stdio.h>
28#include <errno(*__errno_location ()).h>
29
30#include <glib.h>
31
32#include "wsutil/pint.h"
33#include "wsutil/sign_ext.h"
34#include "wsutil/strtoi.h"
35#include "wsutil/unicode-utils.h"
36#include "wsutil/nstime.h"
37#include "wsutil/time_util.h"
38#include <wsutil/ws_assert.h>
39#include "tvbuff.h"
40#include "tvbuff-int.h"
41#include "strutil.h"
42#include "to_str.h"
43#include "charsets.h"
44#include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
45#include "exceptions.h"
46
47#include <time.h>
48
49static uint64_t
50_tvb_get_bits64(tvbuff_t *tvb, unsigned bit_offset, const int total_no_of_bits);
51
52static uint64_t
53_tvb_get_bits64_le(tvbuff_t *tvb, unsigned bit_offset, const int total_no_of_bits);
54
55static inline unsigned
56_tvb_captured_length_remaining(const tvbuff_t *tvb, const unsigned offset);
57
58static inline const uint8_t*
59ensure_contiguous(tvbuff_t *tvb, const int offset, const int length);
60
61static inline const uint8_t*
62ensure_contiguous_unsigned(tvbuff_t *tvb, const unsigned offset, const unsigned length);
63
64static inline uint8_t *
65tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length);
66
67tvbuff_t *
68tvb_new(const struct tvb_ops *ops)
69{
70 tvbuff_t *tvb;
71 size_t size = ops->tvb_size;
72
73 ws_assert(size >= sizeof(*tvb))do { if ((1) && !(size >= sizeof(*tvb))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/tvbuff.c", 73, __func__, "assertion failed: %s"
, "size >= sizeof(*tvb)"); } while (0)
;
74
75 tvb = (tvbuff_t *) g_slice_alloc(size);
76
77 tvb->next = NULL((void*)0);
78 tvb->ops = ops;
79 tvb->initialized = false0;
80 tvb->flags = 0;
81 tvb->length = 0;
82 tvb->reported_length = 0;
83 tvb->contained_length = 0;
84 tvb->real_data = NULL((void*)0);
85 tvb->raw_offset = 0;
86 tvb->ds_tvb = NULL((void*)0);
87
88 return tvb;
89}
90
91static void
92tvb_free_internal(tvbuff_t *tvb)
93{
94 size_t size;
95
96 DISSECTOR_ASSERT(tvb)((void) ((tvb) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 96, "tvb"))))
;
97
98 if (tvb->ops->tvb_free)
99 tvb->ops->tvb_free(tvb);
100
101 size = tvb->ops->tvb_size;
102
103 g_slice_free1(size, tvb);
104}
105
106/* XXX: just call tvb_free_chain();
107 * Not removed so that existing dissectors using tvb_free() need not be changed.
108 * I'd argue that existing calls to tvb_free() should have actually been
109 * calls to tvb_free_chain() although the calls were OK as long as no
110 * subsets, etc had been created on the tvb. */
111void
112tvb_free(tvbuff_t *tvb)
113{
114 tvb_free_chain(tvb);
115}
116
117void
118tvb_free_chain(tvbuff_t *tvb)
119{
120 tvbuff_t *next_tvb;
121 DISSECTOR_ASSERT(tvb)((void) ((tvb) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 121, "tvb"))))
;
122 while (tvb) {
123 next_tvb = tvb->next;
124 tvb_free_internal(tvb);
125 tvb = next_tvb;
126 }
127}
128
129tvbuff_t *
130tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing)
131{
132 tvbuff_t *tvb = tvb_new_proxy(backing);
133
134 tvb_add_to_chain(parent, tvb);
135 return tvb;
136}
137
138void
139tvb_add_to_chain(tvbuff_t *parent, tvbuff_t *child)
140{
141 tvbuff_t *tmp;
142
143 DISSECTOR_ASSERT(parent)((void) ((parent) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 143, "parent"))))
;
144 DISSECTOR_ASSERT(child)((void) ((child) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 144, "child"))))
;
145
146 while (child) {
147 tmp = child;
148 child = child->next;
149
150 tmp->next = parent->next;
151 parent->next = tmp;
152 }
153}
154
155/*
156 * Check whether that offset goes more than one byte past the
157 * end of the buffer.
158 *
159 * If not, return 0; otherwise, return exception
160 */
161static inline int
162validate_offset(const tvbuff_t *tvb, const unsigned abs_offset)
163{
164 if (G_LIKELY(abs_offset <= tvb->length)(abs_offset <= tvb->length)) {
165 /* It's OK. */
166 return 0;
167 }
168
169 /*
170 * It's not OK, but why? Which boundaries is it
171 * past?
172 */
173 if (abs_offset <= tvb->contained_length) {
174 /*
175 * It's past the captured length, but not past
176 * the reported end of any parent tvbuffs from
177 * which this is constructed, or the reported
178 * end of this tvbuff, so it's out of bounds
179 * solely because we're past the end of the
180 * captured data.
181 */
182 return BoundsError1;
183 }
184
185 /*
186 * There's some actual packet boundary, not just the
187 * artificial boundary imposed by packet slicing, that
188 * we're past.
189 */
190
191 if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
192 /*
193 * This tvbuff is the first fragment of a larger
194 * packet that hasn't been reassembled, so we
195 * assume that's the source of the problem - if
196 * we'd reassembled the packet, we wouldn't have
197 * gone past the end.
198 *
199 * That might not be true, but for at least
200 * some forms of reassembly, such as IP
201 * reassembly, you don't know how big the
202 * reassembled packet is unless you reassemble
203 * it, so, in those cases, we can't determine
204 * whether we would have gone past the end
205 * had we reassembled the packet.
206 */
207 return FragmentBoundsError4;
208 }
209
210 /* OK, we're not an unreassembled fragment (that we know of). */
211 if (abs_offset <= tvb->reported_length) {
212 /*
213 * We're within the bounds of what this tvbuff
214 * purportedly contains, based on some length
215 * value, but we're not within the bounds of
216 * something from which this tvbuff was
217 * extracted, so that length value ran past
218 * the end of some parent tvbuff.
219 */
220 return ContainedBoundsError2;
221 }
222
223 /*
224 * OK, it looks as if we ran past the claimed length
225 * of data.
226 */
227 return ReportedBoundsError3;
228}
229
230static inline int
231validate_offset_and_remaining(const tvbuff_t *tvb, const unsigned offset, unsigned *rem_len)
232{
233 int exception;
234
235 exception = validate_offset(tvb, offset);
236 if (!exception)
237 *rem_len = tvb->length - offset;
238
239 return exception;
240}
241
242/* Returns integer indicating whether the given offset and the end offset
243 * calculated from that offset and the given length are in bounds (0) or
244 * not (exception number).
245 * No exception is thrown; on success, we return 0, otherwise we return an
246 * exception for the caller to throw if appropriate.
247 *
248 * N.B. - we return success (0), if the offset is positive and right
249 * after the end of the tvbuff (i.e., equal to the length). We do this
250 * so that a dissector constructing a subset tvbuff for the next protocol
251 * will get a zero-length tvbuff, not an exception, if there's no data
252 * left for the next protocol - we want the next protocol to be the one
253 * that gets an exception, so the error is reported as an error in that
254 * protocol rather than the containing protocol. */
255static inline int
256validate_offset_length_no_exception(const tvbuff_t *tvb,
257 const unsigned offset, const unsigned length)
258{
259 unsigned end_offset;
260 int exception;
261
262 /* Compute the offset */
263 /* Since offset is unsigned, the only effect of validation is to throw
264 * a possibly different exception if offset is outside the captured
265 * bytes. E.g., offset could be outside the captured bytes but in the
266 * reported length, but end_offset outside the reported length.
267 * XXX - Which *is* the proper exception? Compare fast_ensure_contiguous
268 * which only throws the exception related to the end offset.
269 */
270 exception = validate_offset(tvb, offset);
271 if (exception)
272 return exception;
273
274 /*
275 * Compute the offset of the first byte past the length,
276 * checking for an overflow.
277 */
278 if (ckd_add(&end_offset, offset, length)__builtin_add_overflow((offset), (length), (&end_offset)))
279 return BoundsError1;
280
281 return validate_offset(tvb, end_offset);
282}
283
284/* Checks offset and length and throws an exception if
285 * either is out of bounds. Sets integer ptrs to the new length. */
286static inline void
287validate_offset_length(const tvbuff_t *tvb,
288 const unsigned offset, const unsigned length)
289{
290 int exception;
291
292 exception = validate_offset_length_no_exception(tvb, offset, length);
293 if (exception)
294 THROW(exception)except_throw(1, (exception), ((void*)0));
295}
296
297/* Internal function so that other translation units can use
298 * validate_offset_length. */
299void
300tvb_validate_offset_length(const tvbuff_t *tvb,
301 const unsigned offset, const unsigned length)
302{
303 validate_offset_length(tvb, offset, length);
304}
305
306/* Internal function so that other translation units can use
307 * validate_offset_and_remaining. This throws the exception
308 * from validate_offset_and_remaining. */
309void
310tvb_validate_offset_and_remaining(const tvbuff_t *tvb,
311 const unsigned offset, unsigned *rem_len)
312{
313 int exception;
314
315 exception = validate_offset_and_remaining(tvb, offset, rem_len);
316 if (exception)
317 THROW(exception)except_throw(1, (exception), ((void*)0));
318}
319
320/*
321 * The same as validate_offset except this accepts negative offsets, meaning
322 * relative to the end of (captured) length. (That it's captured, not reported,
323 * length is one reason to deprecate signed offsets, #20103.)
324 */
325static inline int
326compute_offset(const tvbuff_t *tvb, const int offset, unsigned *offset_ptr)
327{
328 if (offset >= 0) {
329 /* Positive offset - relative to the beginning of the packet. */
330 if (G_LIKELY((unsigned) offset <= tvb->length)((unsigned) offset <= tvb->length)) {
331 *offset_ptr = offset;
332 } else if ((unsigned) offset <= tvb->contained_length) {
333 return BoundsError1;
334 } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
335 return FragmentBoundsError4;
336 } else if ((unsigned) offset <= tvb->reported_length) {
337 return ContainedBoundsError2;
338 } else {
339 return ReportedBoundsError3;
340 }
341 }
342 else {
343 /* Negative offset - relative to the end of the packet. */
344 /* Prevent UB on 2's complement platforms. All tested compilers
345 * (gcc, clang, MSVC) compile this to a single instruction on
346 * x86, ARM, RISC-V, S390x, SPARC, etc. at -O1 and higher
347 * according to godbolt.org. */
348 unsigned abs_offset = ((unsigned)-(offset + 1)) + 1;
349 if (G_LIKELY(abs_offset <= tvb->length)(abs_offset <= tvb->length)) {
350 *offset_ptr = tvb->length - abs_offset;
351 } else if (abs_offset <= tvb->contained_length) {
352 return BoundsError1;
353 } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
354 return FragmentBoundsError4;
355 } else if (abs_offset <= tvb->reported_length) {
356 return ContainedBoundsError2;
357 } else {
358 return ReportedBoundsError3;
359 }
360 }
361
362 return 0;
363}
364
365static inline int
366compute_offset_and_remaining(const tvbuff_t *tvb, const int offset, unsigned *offset_ptr, unsigned *rem_len)
367{
368 int exception;
369
370 exception = compute_offset(tvb, offset, offset_ptr);
371 if (!exception)
372 *rem_len = tvb->length - *offset_ptr;
373
374 return exception;
375}
376
377/* Computes the absolute offset and length based on a possibly-negative offset
378 * and a length that is possible -1 (which means "to the end of the data").
379 * Returns integer indicating whether the offset is in bounds (0) or
380 * not (exception number). The integer ptrs are modified with the new offset,
381 * captured (available) length, and contained length (amount that's present
382 * in the parent tvbuff based on its reported length).
383 * No exception is thrown; on success, we return 0, otherwise we return an
384 * exception for the caller to throw if appropriate.
385 *
386 * XXX - we return success (0), if the offset is positive and right
387 * after the end of the tvbuff (i.e., equal to the length). We do this
388 * so that a dissector constructing a subset tvbuff for the next protocol
389 * will get a zero-length tvbuff, not an exception, if there's no data
390 * left for the next protocol - we want the next protocol to be the one
391 * that gets an exception, so the error is reported as an error in that
392 * protocol rather than the containing protocol. */
393static inline int
394check_offset_length_no_exception(const tvbuff_t *tvb,
395 const int offset, int const length_val,
396 unsigned *offset_ptr, unsigned *length_ptr)
397{
398 unsigned end_offset;
399 int exception;
400
401 DISSECTOR_ASSERT(offset_ptr)((void) ((offset_ptr) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 401, "offset_ptr"
))))
;
402 DISSECTOR_ASSERT(length_ptr)((void) ((length_ptr) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 402, "length_ptr"
))))
;
403
404 /* Compute the offset */
405 exception = compute_offset(tvb, offset, offset_ptr);
406 if (exception)
407 return exception;
408
409 if (length_val < -1) {
410 /* XXX - ReportedBoundsError? */
411 return BoundsError1;
412 }
413
414 /* Compute the length */
415 if (length_val == -1)
416 *length_ptr = tvb->length - *offset_ptr;
417 else
418 *length_ptr = length_val;
419
420 /*
421 * Compute the offset of the first byte past the length.
422 */
423 end_offset = *offset_ptr + *length_ptr;
424
425 /*
426 * Check for an overflow
427 */
428 if (end_offset < *offset_ptr)
429 return BoundsError1;
430
431 return validate_offset(tvb, end_offset);
432}
433
434/* Checks (+/-) offset and length and throws an exception if
435 * either is out of bounds. Sets integer ptrs to the new offset
436 * and length. */
437static inline void
438check_offset_length(const tvbuff_t *tvb,
439 const int offset, int const length_val,
440 unsigned *offset_ptr, unsigned *length_ptr)
441{
442 int exception;
443
444 exception = check_offset_length_no_exception(tvb, offset, length_val, offset_ptr, length_ptr);
445 if (exception)
446 THROW(exception)except_throw(1, (exception), ((void*)0));
447}
448
449/* Internal function so that other translation units can use
450 * check_offset_length. */
451void
452tvb_check_offset_length(const tvbuff_t *tvb,
453 const int offset, int const length_val,
454 unsigned *offset_ptr, unsigned *length_ptr)
455{
456 check_offset_length(tvb, offset, length_val, offset_ptr, length_ptr);
457}
458
459static const unsigned char left_aligned_bitmask[] = {
460 0xff,
461 0x80,
462 0xc0,
463 0xe0,
464 0xf0,
465 0xf8,
466 0xfc,
467 0xfe
468};
469
470/* tvb_new_octet_aligned used to support -1 no_of_bits as meaning "to the
471 * end of the buffer." Nothing every used it. It could be supported with
472 * a _remaining() function if necessary. Note that the previous implementation
473 * didn't properly keep the extra reported length if the reported length
474 * was greater than the captured length.
475 */
476
477tvbuff_t *
478tvb_new_octet_aligned(tvbuff_t *tvb, uint32_t bit_offset, uint32_t no_of_bits)
479{
480 tvbuff_t *sub_tvb = NULL((void*)0);
481 uint32_t byte_offset;
482 uint32_t datalen, i;
483 uint8_t left, right, remaining_bits, *buf;
484 const uint8_t *data;
485
486 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 486, "tvb && tvb->initialized"
))))
;
487
488 byte_offset = bit_offset >> 3;
489 left = bit_offset % 8; /* for left-shifting */
490 right = 8 - left; /* for right-shifting */
491
492 datalen = no_of_bits >> 3;
493 remaining_bits = no_of_bits % 8;
494 if (remaining_bits) {
495 datalen++;
496 }
497
498 /* already aligned -> shortcut */
499 if (((left == 0) && (remaining_bits == 0)) || datalen == 0) {
500 return tvb_new_subset_length_caplen(tvb, byte_offset, datalen, datalen);
501 }
502
503 /* If at least one trailing byte is available, we must use the content
504 * of that byte for the last shift (i.e. tvb_get_ptr() must use datalen + 1).
505 * If no extra byte is available, the last shifted byte requires
506 * special treatment.
507 */
508 if (_tvb_captured_length_remaining(tvb, byte_offset) > datalen) {
509 data = ensure_contiguous(tvb, byte_offset, datalen + 1); /* tvb_get_ptr */
510
511 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
512 buf = (uint8_t *)g_malloc(datalen);
513
514 /* shift tvb data bit_offset bits to the left */
515 for (i = 0; i < datalen; i++)
516 buf[i] = (data[i] << left) | (data[i+1] >> right);
517 } else {
518 data = ensure_contiguous(tvb, byte_offset, datalen); /* tvb_get_ptr() */
519
520 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
521 buf = (uint8_t *)g_malloc(datalen);
522
523 /* shift tvb data bit_offset bits to the left */
524 for (i = 0; i < (datalen-1); i++)
525 buf[i] = (data[i] << left) | (data[i+1] >> right);
526 buf[datalen-1] = data[datalen-1] << left; /* set last octet */
527 }
528 buf[datalen-1] &= left_aligned_bitmask[remaining_bits];
529
530 sub_tvb = tvb_new_child_real_data(tvb, buf, datalen, datalen);
531 tvb_set_free_cb(sub_tvb, g_free);
532
533 return sub_tvb;
534}
535
536tvbuff_t *
537tvb_new_octet_right_aligned(tvbuff_t *tvb, uint32_t bit_offset, uint32_t no_of_bits)
538{
539 tvbuff_t *sub_tvb = NULL((void*)0);
540 uint32_t byte_offset;
541 unsigned src_len, dst_len, i;
542 uint8_t left, right, remaining_bits, *buf;
543 const uint8_t *data;
544
545 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 545, "tvb && tvb->initialized"
))))
;
4
Assuming 'tvb' is non-null
5
Assuming field 'initialized' is true
6
'?' condition is true
546
547 byte_offset = bit_offset / 8;
548 /* right shift to put bits in place and discard least significant bits */
549 right = bit_offset % 8;
550 /* left shift to get most significant bits from next octet */
551 left = 8 - right;
552
553 dst_len = no_of_bits / 8;
554 remaining_bits = no_of_bits % 8;
555 if (remaining_bits) {
7
Assuming 'remaining_bits' is not equal to 0
8
Taking true branch
556 dst_len++;
557 }
558
559 /* already aligned -> shortcut */
560 if (((right == 0) && (remaining_bits
9.1
'remaining_bits' is not equal to 0
== 0)) || dst_len == 0) {
9
Assuming 'right' is equal to 0
10
Assuming 'dst_len' is not equal to 0
11
Taking false branch
561 return tvb_new_subset_length_caplen(tvb, byte_offset, dst_len, dst_len);
562 }
563
564 if (_tvb_captured_length_remaining(tvb, byte_offset) > dst_len) {
12
Taking false branch
565 /* last octet will get data from trailing octet */
566 src_len = dst_len + 1;
567 } else {
568 /* last octet will be zero padded */
569 src_len = dst_len;
570 }
571
572 data = ensure_contiguous(tvb, byte_offset, src_len); /* tvb_get_ptr */
573
574 /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
575 buf = (uint8_t *)g_malloc(dst_len);
13
Memory is allocated
576
577 for (i = 0; i < (dst_len - 1); i++)
14
Assuming the condition is false
15
Loop condition is false. Execution continues on line 581
578 buf[i] = (data[i] >> right) | (data[i+1] << left);
579
580 /* Special handling for last octet */
581 buf[i] = (data[i] >> right);
582 /* Shift most significant bits from trailing octet if available */
583 if (src_len
15.1
'src_len' is <= 'dst_len'
> dst_len)
16
Taking false branch
584 buf[i] |= (data[i+1] << left);
585 /* Preserve only remaining bits in last octet if not multiple of 8 */
586 if (remaining_bits
16.1
'remaining_bits' is not equal to 0
)
17
Taking true branch
587 buf[i] &= ((1 << remaining_bits) - 1);
18
Assuming right operand of bit shift is less than 32
588
589 sub_tvb = tvb_new_child_real_data(tvb, buf, dst_len, dst_len);
19
Potential leak of memory pointed to by 'buf'
590 tvb_set_free_cb(sub_tvb, g_free);
591
592 return sub_tvb;
593}
594
595static tvbuff_t *
596tvb_generic_clone_offset_len(tvbuff_t *tvb, unsigned offset, unsigned len)
597{
598 tvbuff_t *cloned_tvb;
599 uint8_t *data;
600
601 DISSECTOR_ASSERT(tvb_bytes_exist(tvb, offset, len))((void) ((tvb_bytes_exist(tvb, offset, len)) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 601, "tvb_bytes_exist(tvb, offset, len)"
))))
;
602
603 data = (uint8_t *) g_malloc(len);
604
605 tvb_memcpy(tvb, data, offset, len);
606
607 cloned_tvb = tvb_new_real_data(data, len, len);
608 tvb_set_free_cb(cloned_tvb, g_free);
609
610 return cloned_tvb;
611}
612
613tvbuff_t *
614tvb_clone_offset_len(tvbuff_t *tvb, unsigned offset, unsigned len)
615{
616 if (tvb->ops->tvb_clone) {
617 tvbuff_t *cloned_tvb;
618
619 cloned_tvb = tvb->ops->tvb_clone(tvb, offset, len);
620 if (cloned_tvb)
621 return cloned_tvb;
622 }
623
624 return tvb_generic_clone_offset_len(tvb, offset, len);
625}
626
627tvbuff_t *
628tvb_clone(tvbuff_t *tvb)
629{
630 return tvb_clone_offset_len(tvb, 0, tvb->length);
631}
632
633inline unsigned
634tvb_captured_length(const tvbuff_t *tvb)
635{
636 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 636, "tvb && tvb->initialized"
))))
;
637
638 return tvb->length;
639}
640
641/* For tvbuff internal use */
642static inline unsigned
643_tvb_captured_length_remaining(const tvbuff_t *tvb, const unsigned offset)
644{
645 unsigned rem_length;
646 int exception;
647
648 exception = validate_offset_and_remaining(tvb, offset, &rem_length);
649 if (exception)
650 return 0;
651
652 return rem_length;
653}
654
655unsigned
656tvb_captured_length_remaining(const tvbuff_t *tvb, const unsigned offset)
657{
658 unsigned rem_length;
659 int exception;
660
661 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 661, "tvb && tvb->initialized"
))))
;
662
663 exception = validate_offset_and_remaining(tvb, offset, &rem_length);
664 if (exception)
665 return 0;
666
667 return rem_length;
668}
669
670unsigned
671tvb_ensure_captured_length_remaining(const tvbuff_t *tvb, const unsigned offset)
672{
673 unsigned rem_length = 0;
674 int exception;
675
676 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 676, "tvb && tvb->initialized"
))))
;
677
678 exception = validate_offset(tvb, offset);
679 if (exception)
680 THROW(exception)except_throw(1, (exception), ((void*)0));
681
682 rem_length = tvb->length - offset;
683
684 if (rem_length == 0) {
685 /*
686 * This routine ensures there's at least one byte available.
687 * There aren't any bytes available, so throw the appropriate
688 * exception.
689 */
690 if (offset < tvb->contained_length) {
691 THROW(BoundsError)except_throw(1, (1), ((void*)0));
692 } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
693 THROW(FragmentBoundsError)except_throw(1, (4), ((void*)0));
694 } else if (offset < tvb->reported_length) {
695 THROW(ContainedBoundsError)except_throw(1, (2), ((void*)0));
696 } else {
697 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
698 }
699 }
700 return rem_length;
701}
702
703/* Validates that 'length' bytes are available starting from
704 * offset. Does not throw an exception. */
705bool_Bool
706tvb_bytes_exist(const tvbuff_t *tvb, const unsigned offset, const int length)
707{
708 unsigned end_offset;
709
710 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 710, "tvb && tvb->initialized"
))))
;
711
712 /*
713 * Negative lengths are not possible and indicate a bug (e.g. arithmetic
714 * error or an overly large value from packet data).
715 */
716 if (length < 0)
717 return false0;
718
719 /*
720 * Compute the offset of the first byte past the length.
721 * Make sure it doesn't overflow.
722 */
723 if (ckd_add(&end_offset, offset, length)__builtin_add_overflow((offset), (length), (&end_offset)))
724 return false0;
725
726 /*
727 * Check that bytes exist up to right before that offset. (As length is
728 * positive and there was no overflow we don't need to check offset.)
729 */
730 if (end_offset > tvb->length)
731 return false0;
732
733 return true1;
734}
735
736/* Validates that 'length' bytes, where 'length' is a 64-bit unsigned
737 * integer, are available starting from offset (pos/neg). Throws an
738 * exception if they aren't. */
739void
740tvb_ensure_bytes_exist64(const tvbuff_t *tvb, const unsigned offset, const uint64_t length)
741{
742 /*
743 * Make sure the value fits in a signed integer; if not, assume
744 * that means that it's too big.
745 */
746 if (length > INT_MAX2147483647) {
747 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
748 }
749
750 /* OK, now cast it and try it with tvb_ensure_bytes_exist(). */
751 tvb_ensure_bytes_exist(tvb, offset, (int)length);
752}
753
754/* Validates that 'length' bytes are available starting from
755 * offset (pos/neg). Throws an exception if they aren't. */
756void
757tvb_ensure_bytes_exist(const tvbuff_t *tvb, const unsigned offset, const int length)
758{
759 unsigned end_offset;
760 int exception;
761
762 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 762, "tvb && tvb->initialized"
))))
;
763
764 /*
765 * -1 doesn't mean "until end of buffer", as that's pointless
766 * for this routine. We must treat it as a Really Large Positive
767 * Number, so that we throw an exception; we throw
768 * ReportedBoundsError, as if it were past even the end of a
769 * reassembled packet, and past the end of even the data we
770 * didn't capture.
771 *
772 * We do the same with other negative lengths.
773 */
774 if (length < 0) {
775 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
776 }
777
778 exception = validate_offset(tvb, offset);
779 if (exception)
780 THROW(exception)except_throw(1, (exception), ((void*)0));
781
782 /*
783 * Compute the offset of the first byte past the length.
784 */
785 end_offset = offset + length;
786
787 /*
788 * Check for an overflow
789 */
790 if (end_offset < offset)
791 THROW(BoundsError)except_throw(1, (1), ((void*)0));
792
793 if (G_LIKELY(end_offset <= tvb->length)(end_offset <= tvb->length))
794 return;
795 else if (end_offset <= tvb->contained_length)
796 THROW(BoundsError)except_throw(1, (1), ((void*)0));
797 else if (tvb->flags & TVBUFF_FRAGMENT0x00000001)
798 THROW(FragmentBoundsError)except_throw(1, (4), ((void*)0));
799 else if (end_offset <= tvb->reported_length)
800 THROW(ContainedBoundsError)except_throw(1, (2), ((void*)0));
801 else
802 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
803}
804
805bool_Bool
806tvb_offset_exists(const tvbuff_t *tvb, const unsigned offset)
807{
808 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 808, "tvb && tvb->initialized"
))))
;
809
810 /* We don't care why the offset doesn't exist, and unlike some
811 * other functions we don't accept an offset one past the end,
812 * so we check ourselves... */
813 return offset < tvb->length;
814}
815
816unsigned
817tvb_reported_length(const tvbuff_t *tvb)
818{
819 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 819, "tvb && tvb->initialized"
))))
;
820
821 return tvb->reported_length;
822}
823
824int
825tvb_reported_length_remaining(const tvbuff_t *tvb, const unsigned offset)
826{
827 int exception;
828
829 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 829, "tvb && tvb->initialized"
))))
;
830
831 exception = validate_offset(tvb, offset);
832 if (exception)
833 return 0;
834
835 if (tvb->reported_length >= offset)
836 return tvb->reported_length - offset;
837 else
838 return 0;
839}
840
841unsigned
842tvb_ensure_reported_length_remaining(const tvbuff_t *tvb, const unsigned offset)
843{
844 int exception;
845
846 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 846, "tvb && tvb->initialized"
))))
;
847
848 exception = validate_offset(tvb, offset);
849 if (exception)
850 THROW(exception)except_throw(1, (exception), ((void*)0));
851
852 if (tvb->reported_length >= offset)
853 return tvb->reported_length - offset;
854 else
855 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
856}
857
858/* Set the reported length of a tvbuff to a given value; used for protocols
859 * whose headers contain an explicit length and where the calling
860 * dissector's payload may include padding as well as the packet for
861 * this protocol.
862 * Also adjusts the available and contained length. */
863void
864tvb_set_reported_length(tvbuff_t *tvb, const unsigned reported_length)
865{
866 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 866, "tvb && tvb->initialized"
))))
;
867
868 if (reported_length > tvb->reported_length)
869 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
870
871 tvb->reported_length = reported_length;
872 if (reported_length < tvb->length)
873 tvb->length = reported_length;
874 if (reported_length < tvb->contained_length)
875 tvb->contained_length = reported_length;
876}
877
878/* Repair a tvbuff where the captured length is greater than the
879 * reported length; such a tvbuff makes no sense, as it's impossible
880 * to capture more data than is in the packet.
881 */
882void
883tvb_fix_reported_length(tvbuff_t *tvb)
884{
885 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 885, "tvb && tvb->initialized"
))))
;
886 DISSECTOR_ASSERT(tvb->reported_length < tvb->length)((void) ((tvb->reported_length < tvb->length) ? (void
)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 886, "tvb->reported_length < tvb->length"
))))
;
887
888 tvb->reported_length = tvb->length;
889 if (tvb->contained_length < tvb->length)
890 tvb->contained_length = tvb->length;
891}
892
893unsigned
894tvb_offset_from_real_beginning_counter(const tvbuff_t *tvb, const unsigned counter)
895{
896 if (tvb->ops->tvb_offset)
897 return tvb->ops->tvb_offset(tvb, counter);
898
899 DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\""
, "epan/tvbuff.c", 899))
;
900 return 0;
901}
902
903unsigned
904tvb_offset_from_real_beginning(const tvbuff_t *tvb)
905{
906 return tvb_offset_from_real_beginning_counter(tvb, 0);
907}
908
909static inline const uint8_t*
910ensure_contiguous_unsigned_no_exception(tvbuff_t *tvb, const unsigned offset, const unsigned length, int *pexception)
911{
912 int exception;
913
914 exception = validate_offset_length_no_exception(tvb, offset, length);
915 if (exception) {
916 if (pexception)
917 *pexception = exception;
918 return NULL((void*)0);
919 }
920
921 /*
922 * Special case: if the caller (e.g. tvb_get_ptr) requested no data,
923 * then it is acceptable to have an empty tvb (!tvb->real_data).
924 */
925 if (length == 0) {
926 return NULL((void*)0);
927 }
928
929 /*
930 * We know that all the data is present in the tvbuff, so
931 * no exceptions should be thrown.
932 */
933 if (tvb->real_data)
934 return tvb->real_data + offset;
935
936 if (tvb->ops->tvb_get_ptr)
937 return tvb->ops->tvb_get_ptr(tvb, offset, length);
938
939 DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\""
, "epan/tvbuff.c", 939))
;
940 return NULL((void*)0);
941}
942
943static inline const uint8_t*
944ensure_contiguous_unsigned(tvbuff_t *tvb, const unsigned offset, const unsigned length)
945{
946 int exception = 0;
947 const uint8_t *p;
948
949 p = ensure_contiguous_unsigned_no_exception(tvb, offset, length, &exception);
950 if (p == NULL((void*)0) && length != 0) {
951 DISSECTOR_ASSERT(exception > 0)((void) ((exception > 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 951, "exception > 0"
))))
;
952 THROW(exception)except_throw(1, (exception), ((void*)0));
953 }
954 return p;
955}
956
957static inline const uint8_t*
958ensure_contiguous_no_exception(tvbuff_t *tvb, const int offset, const int length, int *pexception)
959{
960 unsigned abs_offset = 0, abs_length = 0;
961 int exception;
962
963 exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
964 if (exception) {
965 if (pexception)
966 *pexception = exception;
967 return NULL((void*)0);
968 }
969
970 /*
971 * Special case: if the caller (e.g. tvb_get_ptr) requested no data,
972 * then it is acceptable to have an empty tvb (!tvb->real_data).
973 */
974 if (length == 0) {
975 return NULL((void*)0);
976 }
977
978 /*
979 * We know that all the data is present in the tvbuff, so
980 * no exceptions should be thrown.
981 */
982 if (tvb->real_data)
983 return tvb->real_data + abs_offset;
984
985 if (tvb->ops->tvb_get_ptr)
986 return tvb->ops->tvb_get_ptr(tvb, abs_offset, abs_length);
987
988 DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\""
, "epan/tvbuff.c", 988))
;
989 return NULL((void*)0);
990}
991
992static inline const uint8_t*
993ensure_contiguous(tvbuff_t *tvb, const int offset, const int length)
994{
995 int exception = 0;
996 const uint8_t *p;
997
998 p = ensure_contiguous_no_exception(tvb, offset, length, &exception);
999 if (p == NULL((void*)0) && length != 0) {
1000 DISSECTOR_ASSERT(exception > 0)((void) ((exception > 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 1000, "exception > 0"
))))
;
1001 THROW(exception)except_throw(1, (exception), ((void*)0));
1002 }
1003 return p;
1004}
1005
1006static inline const uint8_t*
1007fast_ensure_contiguous(tvbuff_t *tvb, const unsigned offset, const unsigned length)
1008{
1009 unsigned end_offset;
1010
1011 /* Since offset is unsigned, we have to check for overflow. */
1012 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 1012, "tvb && tvb->initialized"
))))
;
1013
1014 if (!tvb->real_data) {
1015 return ensure_contiguous_unsigned(tvb, offset, length);
1016 }
1017
1018 /* XXX - Is this really faster now (other than the slight difference
1019 * in behavior from only throwing the exception related to the end
1020 * offset?) */
1021 if (ckd_add(&end_offset, offset, length)__builtin_add_overflow((offset), (length), (&end_offset)))
1022 THROW(BoundsError)except_throw(1, (1), ((void*)0));
1023
1024 if (G_LIKELY(end_offset <= tvb->length)(end_offset <= tvb->length)) {
1025 return tvb->real_data + offset;
1026 } else if (end_offset <= tvb->contained_length) {
1027 THROW(BoundsError)except_throw(1, (1), ((void*)0));
1028 } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
1029 THROW(FragmentBoundsError)except_throw(1, (4), ((void*)0));
1030 } else if (end_offset <= tvb->reported_length) {
1031 THROW(ContainedBoundsError)except_throw(1, (2), ((void*)0));
1032 } else {
1033 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
1034 }
1035 /* not reached */
1036 return NULL((void*)0);
1037}
1038
1039
1040
1041/************** ACCESSORS **************/
1042
1043void *
1044tvb_memcpy(tvbuff_t *tvb, void *target, const unsigned offset, size_t length)
1045{
1046 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 1046, "tvb && tvb->initialized"
))))
;
1047
1048 /*
1049 * XXX - The length is a size_t, but the tvb length and tvb_ops
1050 * only supports an unsigned.
1051 */
1052 DISSECTOR_ASSERT(length <= UINT_MAX)((void) ((length <= (2147483647 *2U +1U)) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 1052, "length <= (2147483647 *2U +1U)"
))))
;
1053 validate_offset_length(tvb, offset, (unsigned)length);
1054
1055 if (target && tvb->real_data) {
1056 return memcpy(target, tvb->real_data + offset, length);
1057 }
1058
1059 if (target && tvb->ops->tvb_memcpy)
1060 return tvb->ops->tvb_memcpy(tvb, target, offset, (unsigned)length);
1061
1062 /*
1063 * If the length is 0, there's nothing to do.
1064 * (tvb->real_data could be null if it's allocated with
1065 * a size of length.)
1066 */
1067 if (length != 0) {
1068 /*
1069 * XXX, fallback to slower method
1070 */
1071 DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\""
, "epan/tvbuff.c", 1071))
;
1072 }
1073 return NULL((void*)0);
1074}
1075
1076
1077/*
1078 * XXX - This could replace some code that calls "tvb_ensure_bytes_exist()"
1079 * and then allocates a buffer and copies data to it.
1080 *
1081 * If scope is NULL, memory is allocated with g_malloc() and user must
1082 * explicitly free it with g_free().
1083 * If scope is not NULL, memory is allocated with the corresponding pool
1084 * lifetime.
1085 */
1086void *
1087tvb_memdup(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, size_t length)
1088{
1089 void *duped;
1090
1091 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 1091, "tvb && tvb->initialized"
))))
;
1092
1093 /*
1094 * XXX - The length is a size_t, but the tvb length and tvb_ops
1095 * only supports an unsigned.
1096 */
1097 DISSECTOR_ASSERT(length <= UINT_MAX)((void) ((length <= (2147483647 *2U +1U)) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 1097, "length <= (2147483647 *2U +1U)"
))))
;
1098 validate_offset_length(tvb, offset, (unsigned)length);
1099
1100 if (length == 0)
1101 return NULL((void*)0);
1102
1103 duped = wmem_alloc(scope, length);
1104 return tvb_memcpy(tvb, duped, offset, length);
1105}
1106
1107#if 0
1108/* XXX - Is a _remaining variant of this necessary? The user would still need
1109 * to get the length from tvb_captured_length_remaining() to productively use
1110 * the (not necessarily null terminated) byte array. But see uses of
1111 * tvb_get_ptr(...,...,-1) in the repo, which is similar. */
1112void *
1113tvb_memdup_remaining(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset)
1114{
1115 void *duped;
1116 unsigned length;
1117
1118 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 1118, "tvb && tvb->initialized"
))))
;
1119
1120 validate_offset_and_remaining(tvb, offset, &length);
1121
1122 if (length == 0)
1123 return NULL((void*)0);
1124
1125 duped = wmem_alloc(scope, length);
1126 return tvb_memcpy(tvb, duped, offset, length);
1127}
1128#endif
1129
1130const uint8_t*
1131tvb_get_ptr(tvbuff_t *tvb, const int offset, const int length)
1132{
1133 return ensure_contiguous(tvb, offset, length);
1134}
1135
1136/* ---------------- */
1137uint8_t
1138tvb_get_uint8(tvbuff_t *tvb, const unsigned offset)
1139{
1140 const uint8_t *ptr;
1141
1142 ptr = fast_ensure_contiguous(tvb, offset, 1);
1143 return *ptr;
1144}
1145
1146int8_t
1147tvb_get_int8(tvbuff_t *tvb, const unsigned offset)
1148{
1149 const uint8_t *ptr;
1150
1151 ptr = fast_ensure_contiguous(tvb, offset, 1);
1152 return *ptr;
1153}
1154
1155uint16_t
1156tvb_get_ntohs(tvbuff_t *tvb, const unsigned offset)
1157{
1158 const uint8_t *ptr;
1159
1160 ptr = fast_ensure_contiguous(tvb, offset, 2);
1161 return pntohu16(ptr);
1162}
1163
1164int16_t
1165tvb_get_ntohis(tvbuff_t *tvb, const unsigned offset)
1166{
1167 const uint8_t *ptr;
1168
1169 ptr = fast_ensure_contiguous(tvb, offset, 2);
1170 return pntohu16(ptr);
1171}
1172
1173uint32_t
1174tvb_get_ntoh24(tvbuff_t *tvb, const unsigned offset)
1175{
1176 const uint8_t *ptr;
1177
1178 ptr = fast_ensure_contiguous(tvb, offset, 3);
1179 return pntohu24(ptr);
1180}
1181
1182int32_t
1183tvb_get_ntohi24(tvbuff_t *tvb, const unsigned offset)
1184{
1185 uint32_t ret;
1186
1187 ret = ws_sign_ext32(tvb_get_ntoh24(tvb, offset), 24);
1188
1189 return (int32_t)ret;
1190}
1191
1192uint32_t
1193tvb_get_ntohl(tvbuff_t *tvb, const unsigned offset)
1194{
1195 const uint8_t *ptr;
1196
1197 ptr = fast_ensure_contiguous(tvb, offset, 4);
1198 return pntohu32(ptr);
1199}
1200
1201int32_t
1202tvb_get_ntohil(tvbuff_t *tvb, const unsigned offset)
1203{
1204 const uint8_t *ptr;
1205
1206 ptr = fast_ensure_contiguous(tvb, offset, 4);
1207 return pntohu32(ptr);
1208}
1209
1210uint64_t
1211tvb_get_ntoh40(tvbuff_t *tvb, const unsigned offset)
1212{
1213 const uint8_t *ptr;
1214
1215 ptr = fast_ensure_contiguous(tvb, offset, 5);
1216 return pntohu40(ptr);
1217}
1218
1219int64_t
1220tvb_get_ntohi40(tvbuff_t *tvb, const unsigned offset)
1221{
1222 uint64_t ret;
1223
1224 ret = ws_sign_ext64(tvb_get_ntoh40(tvb, offset), 40);
1225
1226 return (int64_t)ret;
1227}
1228
1229uint64_t
1230tvb_get_ntoh48(tvbuff_t *tvb, const unsigned offset)
1231{
1232 const uint8_t *ptr;
1233
1234 ptr = fast_ensure_contiguous(tvb, offset, 6);
1235 return pntohu48(ptr);
1236}
1237
1238int64_t
1239tvb_get_ntohi48(tvbuff_t *tvb, const unsigned offset)
1240{
1241 uint64_t ret;
1242
1243 ret = ws_sign_ext64(tvb_get_ntoh48(tvb, offset), 48);
1244
1245 return (int64_t)ret;
1246}
1247
1248uint64_t
1249tvb_get_ntoh56(tvbuff_t *tvb, const unsigned offset)
1250{
1251 const uint8_t *ptr;
1252
1253 ptr = fast_ensure_contiguous(tvb, offset, 7);
1254 return pntohu56(ptr);
1255}
1256
1257int64_t
1258tvb_get_ntohi56(tvbuff_t *tvb, const unsigned offset)
1259{
1260 uint64_t ret;
1261
1262 ret = ws_sign_ext64(tvb_get_ntoh56(tvb, offset), 56);
1263
1264 return (int64_t)ret;
1265}
1266
1267uint64_t
1268tvb_get_ntoh64(tvbuff_t *tvb, const unsigned offset)
1269{
1270 const uint8_t *ptr;
1271
1272 ptr = fast_ensure_contiguous(tvb, offset, 8);
1273 return pntohu64(ptr);
1274}
1275
1276int64_t
1277tvb_get_ntohi64(tvbuff_t *tvb, const unsigned offset)
1278{
1279 const uint8_t *ptr;
1280
1281 ptr = fast_ensure_contiguous(tvb, offset, 8);
1282 return pntohu64(ptr);
1283}
1284
1285uint16_t
1286tvb_get_uint16(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1287 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1288 return tvb_get_letohs(tvb, offset);
1289 } else {
1290 return tvb_get_ntohs(tvb, offset);
1291 }
1292}
1293
1294int16_t
1295tvb_get_int16(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1296 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1297 return tvb_get_letohis(tvb, offset);
1298 } else {
1299 return tvb_get_ntohis(tvb, offset);
1300 }
1301}
1302
1303uint32_t
1304tvb_get_uint24(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1305 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1306 return tvb_get_letoh24(tvb, offset);
1307 } else {
1308 return tvb_get_ntoh24(tvb, offset);
1309 }
1310}
1311
1312int32_t
1313tvb_get_int24(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1314 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1315 return tvb_get_letohi24(tvb, offset);
1316 } else {
1317 return tvb_get_ntohi24(tvb, offset);
1318 }
1319}
1320
1321uint32_t
1322tvb_get_uint32(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1323 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1324 return tvb_get_letohl(tvb, offset);
1325 } else {
1326 return tvb_get_ntohl(tvb, offset);
1327 }
1328}
1329
1330int32_t
1331tvb_get_int32(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1332 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1333 return tvb_get_letohil(tvb, offset);
1334 } else {
1335 return tvb_get_ntohil(tvb, offset);
1336 }
1337}
1338
1339uint64_t
1340tvb_get_uint40(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1341 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1342 return tvb_get_letoh40(tvb, offset);
1343 } else {
1344 return tvb_get_ntoh40(tvb, offset);
1345 }
1346}
1347
1348int64_t
1349tvb_get_int40(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1350 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1351 return tvb_get_letohi40(tvb, offset);
1352 } else {
1353 return tvb_get_ntohi40(tvb, offset);
1354 }
1355}
1356
1357uint64_t
1358tvb_get_uint48(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1359 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1360 return tvb_get_letoh48(tvb, offset);
1361 } else {
1362 return tvb_get_ntoh48(tvb, offset);
1363 }
1364}
1365
1366int64_t
1367tvb_get_int48(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1368 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1369 return tvb_get_letohi48(tvb, offset);
1370 } else {
1371 return tvb_get_ntohi48(tvb, offset);
1372 }
1373}
1374
1375uint64_t
1376tvb_get_uint56(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1377 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1378 return tvb_get_letoh56(tvb, offset);
1379 } else {
1380 return tvb_get_ntoh56(tvb, offset);
1381 }
1382}
1383
1384int64_t
1385tvb_get_int56(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1386 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1387 return tvb_get_letohi56(tvb, offset);
1388 } else {
1389 return tvb_get_ntohi56(tvb, offset);
1390 }
1391}
1392
1393uint64_t
1394tvb_get_uint64(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1395 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1396 return tvb_get_letoh64(tvb, offset);
1397 } else {
1398 return tvb_get_ntoh64(tvb, offset);
1399 }
1400}
1401
1402uint64_t
1403tvb_get_uint64_with_length(tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
1404{
1405 uint64_t value;
1406
1407 switch (length) {
1408
1409 case 1:
1410 value = tvb_get_uint8(tvb, offset);
1411 break;
1412
1413 case 2:
1414 value = (encoding & ENC_LITTLE_ENDIAN0x80000000) ? tvb_get_letohs(tvb, offset)
1415 : tvb_get_ntohs(tvb, offset);
1416 break;
1417
1418 case 3:
1419 value = (encoding & ENC_LITTLE_ENDIAN0x80000000) ? tvb_get_letoh24(tvb, offset)
1420 : tvb_get_ntoh24(tvb, offset);
1421 break;
1422
1423 case 4:
1424 value = (encoding & ENC_LITTLE_ENDIAN0x80000000) ? tvb_get_letohl(tvb, offset)
1425 : tvb_get_ntohl(tvb, offset);
1426 break;
1427
1428 case 5:
1429 value = (encoding & ENC_LITTLE_ENDIAN0x80000000) ? tvb_get_letoh40(tvb, offset)
1430 : tvb_get_ntoh40(tvb, offset);
1431 break;
1432
1433 case 6:
1434 value = (encoding & ENC_LITTLE_ENDIAN0x80000000) ? tvb_get_letoh48(tvb, offset)
1435 : tvb_get_ntoh48(tvb, offset);
1436 break;
1437
1438 case 7:
1439 value = (encoding & ENC_LITTLE_ENDIAN0x80000000) ? tvb_get_letoh56(tvb, offset)
1440 : tvb_get_ntoh56(tvb, offset);
1441 break;
1442
1443 case 8:
1444 value = (encoding & ENC_LITTLE_ENDIAN0x80000000) ? tvb_get_letoh64(tvb, offset)
1445 : tvb_get_ntoh64(tvb, offset);
1446 break;
1447
1448 default:
1449 if (length < 1) {
1450 value = 0;
1451 } else {
1452 value = (encoding & ENC_LITTLE_ENDIAN0x80000000) ? tvb_get_letoh64(tvb, offset)
1453 : tvb_get_ntoh64(tvb, offset);
1454 }
1455 break;
1456 }
1457 return value;
1458}
1459
1460int64_t
1461tvb_get_int64(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1462 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1463 return tvb_get_letohi64(tvb, offset);
1464 } else {
1465 return tvb_get_ntohi64(tvb, offset);
1466 }
1467}
1468
1469float
1470tvb_get_ieee_float(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1471 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1472 return tvb_get_letohieee_float(tvb, offset);
1473 } else {
1474 return tvb_get_ntohieee_float(tvb, offset);
1475 }
1476}
1477
1478double
1479tvb_get_ieee_double(tvbuff_t *tvb, const unsigned offset, const unsigned encoding) {
1480 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1481 return tvb_get_letohieee_double(tvb, offset);
1482 } else {
1483 return tvb_get_ntohieee_double(tvb, offset);
1484 }
1485}
1486
1487/*
1488 * Stuff for IEEE float handling on platforms that don't have IEEE
1489 * format as the native floating-point format.
1490 *
1491 * For now, we treat only the VAX as such a platform.
1492 *
1493 * XXX - other non-IEEE boxes that can run UN*X include some Crays,
1494 * and possibly other machines. However, I don't know whether there
1495 * are any other machines that could run Wireshark and that don't use
1496 * IEEE format. As far as I know, all of the main current and past
1497 * commercial microprocessor families on which OSes that support
1498 * Wireshark can run use IEEE format (x86, ARM, 68k, SPARC, MIPS,
1499 * PA-RISC, Alpha, IA-64, and so on), and it appears that the official
1500 * Linux port to System/390 and zArchitecture uses IEEE format floating-
1501 * point rather than IBM hex floating-point (not a huge surprise), so
1502 * I'm not sure that leaves any 32-bit or larger UN*X or Windows boxes,
1503 * other than VAXes, that don't use IEEE format. If you're not running
1504 * UN*X or Windows, the floating-point format is probably going to be
1505 * the least of your problems in a port.
1506 */
1507
1508#if defined(vax)
1509
1510#include <math.h>
1511
1512/*
1513 * Single-precision.
1514 */
1515#define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
1516#define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
1517#define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
1518
1519#define IEEE_SP_SIGN_MASK 0x80000000
1520#define IEEE_SP_EXPONENT_MASK 0x7F800000
1521#define IEEE_SP_MANTISSA_MASK 0x007FFFFF
1522#define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
1523
1524#define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
1525#define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
1526#define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
1527
1528static int
1529ieee_float_is_zero(const uint32_t w)
1530{
1531 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1532}
1533
1534static float
1535get_ieee_float(const uint32_t w)
1536{
1537 long sign;
1538 long exponent;
1539 long mantissa;
1540
1541 sign = w & IEEE_SP_SIGN_MASK;
1542 exponent = w & IEEE_SP_EXPONENT_MASK;
1543 mantissa = w & IEEE_SP_MANTISSA_MASK;
1544
1545 if (ieee_float_is_zero(w)) {
1546 /* number is zero, unnormalized, or not-a-number */
1547 return 0.0;
1548 }
1549#if 0
1550 /*
1551 * XXX - how to handle this?
1552 */
1553 if (IEEE_SP_INFINITY == exponent) {
1554 /*
1555 * number is positive or negative infinity, or a special value
1556 */
1557 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1558 }
1559#endif
1560
1561 exponent = ((exponent >> IEEE_SP_MANTISSA_WIDTH) - IEEE_SP_BIAS) -
1562 IEEE_SP_MANTISSA_WIDTH;
1563 mantissa |= IEEE_SP_IMPLIED_BIT;
1564
1565 if (sign)
1566 return -mantissa * pow(2, exponent);
1567 else
1568 return mantissa * pow(2, exponent);
1569}
1570
1571/*
1572 * Double-precision.
1573 * We assume that if you don't have IEEE floating-point, you have a
1574 * compiler that understands 64-bit integral quantities.
1575 */
1576#define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
1577#define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
1578#define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
1579
1580#define IEEE_DP_SIGN_MASK INT64_C(0x8000000000000000)0x8000000000000000L
1581#define IEEE_DP_EXPONENT_MASK INT64_C(0x7FF0000000000000)0x7FF0000000000000L
1582#define IEEE_DP_MANTISSA_MASK INT64_C(0x000FFFFFFFFFFFFF)0x000FFFFFFFFFFFFFL
1583#define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
1584
1585#define IEEE_DP_IMPLIED_BIT (INT64_C(1)1L << IEEE_DP_MANTISSA_WIDTH)
1586#define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
1587#define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
1588
1589static int
1590ieee_double_is_zero(const uint64_t w)
1591{
1592 return ((w & ~IEEE_SP_SIGN_MASK) == 0);
1593}
1594
1595static double
1596get_ieee_double(const uint64_t w)
1597{
1598 int64_t sign;
1599 int64_t exponent;
1600 int64_t mantissa;
1601
1602 sign = w & IEEE_DP_SIGN_MASK;
1603 exponent = w & IEEE_DP_EXPONENT_MASK;
1604 mantissa = w & IEEE_DP_MANTISSA_MASK;
1605
1606 if (ieee_double_is_zero(w)) {
1607 /* number is zero, unnormalized, or not-a-number */
1608 return 0.0;
1609 }
1610#if 0
1611 /*
1612 * XXX - how to handle this?
1613 */
1614 if (IEEE_DP_INFINITY == exponent) {
1615 /*
1616 * number is positive or negative infinity, or a special value
1617 */
1618 return (sign? MINUS_INFINITY: PLUS_INFINITY);
1619 }
1620#endif
1621
1622 exponent = ((exponent >> IEEE_DP_MANTISSA_WIDTH) - IEEE_DP_BIAS) -
1623 IEEE_DP_MANTISSA_WIDTH;
1624 mantissa |= IEEE_DP_IMPLIED_BIT;
1625
1626 if (sign)
1627 return -mantissa * pow(2, exponent);
1628 else
1629 return mantissa * pow(2, exponent);
1630}
1631#endif
1632
1633/*
1634 * Fetches an IEEE single-precision floating-point number, in
1635 * big-endian form, and returns a "float".
1636 *
1637 * XXX - should this be "double", in case there are IEEE single-
1638 * precision numbers that won't fit in some platform's native
1639 * "float" format?
1640 */
1641float
1642tvb_get_ntohieee_float(tvbuff_t *tvb, const unsigned offset)
1643{
1644#if defined(vax)
1645 return get_ieee_float(tvb_get_ntohl(tvb, offset));
1646#else
1647 union {
1648 float f;
1649 uint32_t w;
1650 } ieee_fp_union;
1651
1652 ieee_fp_union.w = tvb_get_ntohl(tvb, offset);
1653 return ieee_fp_union.f;
1654#endif
1655}
1656
1657/*
1658 * Fetches an IEEE double-precision floating-point number, in
1659 * big-endian form, and returns a "double".
1660 */
1661double
1662tvb_get_ntohieee_double(tvbuff_t *tvb, const unsigned offset)
1663{
1664#if defined(vax)
1665 union {
1666 uint32_t w[2];
1667 uint64_t dw;
1668 } ieee_fp_union;
1669#else
1670 union {
1671 double d;
1672 uint32_t w[2];
1673 } ieee_fp_union;
1674#endif
1675
1676#if G_BYTE_ORDER1234 == G_BIG_ENDIAN4321
1677 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset);
1678 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset+4);
1679#else
1680 ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset+4);
1681 ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset);
1682#endif
1683#if defined(vax)
1684 return get_ieee_double(ieee_fp_union.dw);
1685#else
1686 return ieee_fp_union.d;
1687#endif
1688}
1689
1690uint16_t
1691tvb_get_letohs(tvbuff_t *tvb, const unsigned offset)
1692{
1693 const uint8_t *ptr;
1694
1695 ptr = fast_ensure_contiguous(tvb, offset, 2);
1696 return pletohu16(ptr);
1697}
1698
1699int16_t
1700tvb_get_letohis(tvbuff_t *tvb, const unsigned offset)
1701{
1702 const uint8_t *ptr;
1703
1704 ptr = fast_ensure_contiguous(tvb, offset, 2);
1705 return pletohu16(ptr);
1706}
1707
1708uint32_t
1709tvb_get_letoh24(tvbuff_t *tvb, const unsigned offset)
1710{
1711 const uint8_t *ptr;
1712
1713 ptr = fast_ensure_contiguous(tvb, offset, 3);
1714 return pletohu24(ptr);
1715}
1716
1717int32_t
1718tvb_get_letohi24(tvbuff_t *tvb, const unsigned offset)
1719{
1720 uint32_t ret;
1721
1722 ret = ws_sign_ext32(tvb_get_letoh24(tvb, offset), 24);
1723
1724 return (int32_t)ret;
1725}
1726
1727uint32_t
1728tvb_get_letohl(tvbuff_t *tvb, const unsigned offset)
1729{
1730 const uint8_t *ptr;
1731
1732 ptr = fast_ensure_contiguous(tvb, offset, 4);
1733 return pletohu32(ptr);
1734}
1735
1736int32_t
1737tvb_get_letohil(tvbuff_t *tvb, const unsigned offset)
1738{
1739 const uint8_t *ptr;
1740
1741 ptr = fast_ensure_contiguous(tvb, offset, 4);
1742 return pletohu32(ptr);
1743}
1744
1745uint64_t
1746tvb_get_letoh40(tvbuff_t *tvb, const unsigned offset)
1747{
1748 const uint8_t *ptr;
1749
1750 ptr = fast_ensure_contiguous(tvb, offset, 5);
1751 return pletohu40(ptr);
1752}
1753
1754int64_t
1755tvb_get_letohi40(tvbuff_t *tvb, const unsigned offset)
1756{
1757 uint64_t ret;
1758
1759 ret = ws_sign_ext64(tvb_get_letoh40(tvb, offset), 40);
1760
1761 return (int64_t)ret;
1762}
1763
1764uint64_t
1765tvb_get_letoh48(tvbuff_t *tvb, const unsigned offset)
1766{
1767 const uint8_t *ptr;
1768
1769 ptr = fast_ensure_contiguous(tvb, offset, 6);
1770 return pletohu48(ptr);
1771}
1772
1773int64_t
1774tvb_get_letohi48(tvbuff_t *tvb, const unsigned offset)
1775{
1776 uint64_t ret;
1777
1778 ret = ws_sign_ext64(tvb_get_letoh48(tvb, offset), 48);
1779
1780 return (int64_t)ret;
1781}
1782
1783uint64_t
1784tvb_get_letoh56(tvbuff_t *tvb, const unsigned offset)
1785{
1786 const uint8_t *ptr;
1787
1788 ptr = fast_ensure_contiguous(tvb, offset, 7);
1789 return pletohu56(ptr);
1790}
1791
1792int64_t
1793tvb_get_letohi56(tvbuff_t *tvb, const unsigned offset)
1794{
1795 uint64_t ret;
1796
1797 ret = ws_sign_ext64(tvb_get_letoh56(tvb, offset), 56);
1798
1799 return (int64_t)ret;
1800}
1801
1802uint64_t
1803tvb_get_letoh64(tvbuff_t *tvb, const unsigned offset)
1804{
1805 const uint8_t *ptr;
1806
1807 ptr = fast_ensure_contiguous(tvb, offset, 8);
1808 return pletohu64(ptr);
1809}
1810
1811int64_t
1812tvb_get_letohi64(tvbuff_t *tvb, const unsigned offset)
1813{
1814 const uint8_t *ptr;
1815
1816 ptr = fast_ensure_contiguous(tvb, offset, 8);
1817 return pletohu64(ptr);
1818}
1819
1820/*
1821 * Fetches an IEEE single-precision floating-point number, in
1822 * little-endian form, and returns a "float".
1823 *
1824 * XXX - should this be "double", in case there are IEEE single-
1825 * precision numbers that won't fit in some platform's native
1826 * "float" format?
1827 */
1828float
1829tvb_get_letohieee_float(tvbuff_t *tvb, const unsigned offset)
1830{
1831#if defined(vax)
1832 return get_ieee_float(tvb_get_letohl(tvb, offset));
1833#else
1834 union {
1835 float f;
1836 uint32_t w;
1837 } ieee_fp_union;
1838
1839 ieee_fp_union.w = tvb_get_letohl(tvb, offset);
1840 return ieee_fp_union.f;
1841#endif
1842}
1843
1844/*
1845 * Fetches an IEEE double-precision floating-point number, in
1846 * little-endian form, and returns a "double".
1847 */
1848double
1849tvb_get_letohieee_double(tvbuff_t *tvb, const unsigned offset)
1850{
1851#if defined(vax)
1852 union {
1853 uint32_t w[2];
1854 uint64_t dw;
1855 } ieee_fp_union;
1856#else
1857 union {
1858 double d;
1859 uint32_t w[2];
1860 } ieee_fp_union;
1861#endif
1862
1863#if G_BYTE_ORDER1234 == G_BIG_ENDIAN4321
1864 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset+4);
1865 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset);
1866#else
1867 ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset);
1868 ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset+4);
1869#endif
1870#if defined(vax)
1871 return get_ieee_double(ieee_fp_union.dw);
1872#else
1873 return ieee_fp_union.d;
1874#endif
1875}
1876
1877/* This function is a slight misnomer. It accepts all encodings that are
1878 * ASCII "enough", which means encodings that are the same as US-ASCII
1879 * for textual representations of dates and hex bytes; i.e., the same
1880 * for the hex digits and Z (in practice, all alphanumerics), and the
1881 * four separators ':' '-' '.' and ' '
1882 * That means that any encoding that keeps the ISO/IEC 646 invariant
1883 * characters the same (including the T.61 8 bit encoding and multibyte
1884 * encodings like EUC-KR and GB18030) are OK, even if they replace characters
1885 * like '$' '#' and '\' with national variants, but not encodings like UTF-16
1886 * that include extra null bytes.
1887 * For our current purposes, the unpacked GSM 7-bit default alphabet (but not
1888 * all National Language Shift Tables) also satisfies this requirement, but
1889 * note that it does *not* keep all ISO/IEC 646 invariant characters the same.
1890 * If this internal function gets used for additional purposes than currently,
1891 * the set of encodings that it accepts could change.
1892 * */
1893static inline void
1894validate_single_byte_ascii_encoding(const unsigned encoding)
1895{
1896 const unsigned enc = encoding & ~ENC_CHARENCODING_MASK0x0000FFFE;
1897
1898 switch (enc) {
1899 case ENC_UTF_160x00000004:
1900 case ENC_UCS_20x00000006:
1901 case ENC_UCS_40x00000008:
1902 case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
1903 case ENC_ASCII_7BITS0x00000034:
1904 case ENC_EBCDIC0x0000002E:
1905 case ENC_EBCDIC_CP0370x00000038:
1906 case ENC_EBCDIC_CP5000x00000060:
1907 case ENC_BCD_DIGITS_0_90x00000044:
1908 case ENC_KEYPAD_ABC_TBCD0x00000046:
1909 case ENC_KEYPAD_BC_TBCD0x00000048:
1910 case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
1911 case ENC_APN_STR0x00000054:
1912 case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
1913 REPORT_DISSECTOR_BUG("Invalid string encoding type passed to tvb_get_string_XXX")proto_report_dissector_bug("Invalid string encoding type passed to tvb_get_string_XXX"
)
;
1914 break;
1915 default:
1916 break;
1917 }
1918 /* make sure something valid was set */
1919 if (enc == 0)
1920 REPORT_DISSECTOR_BUG("No string encoding type passed to tvb_get_string_XXX")proto_report_dissector_bug("No string encoding type passed to tvb_get_string_XXX"
)
;
1921}
1922
1923GByteArray*
1924tvb_get_string_bytes(tvbuff_t *tvb, const unsigned offset, const unsigned length,
1925 const unsigned encoding, GByteArray *bytes, unsigned *endoff)
1926{
1927 char *ptr;
1928 const char *begin;
1929 const char *end = NULL((void*)0);
1930 GByteArray *retval = NULL((void*)0);
1931
1932 validate_single_byte_ascii_encoding(encoding);
1933
1934 ptr = (char*) tvb_get_raw_string(NULL((void*)0), tvb, offset, length);
1935 begin = ptr;
1936
1937 if (endoff) *endoff = offset;
1938
1939 while (*begin == ' ') begin++;
1940
1941 if (*begin && bytes) {
1942 if (hex_str_to_bytes_encoding(begin, bytes, &end, encoding, false0)) {
1943 if (bytes->len > 0) {
1944 if (endoff) *endoff = offset + (unsigned)(end - ptr);
1945 retval = bytes;
1946 }
1947 }
1948 }
1949
1950 wmem_free(NULL((void*)0), ptr);
1951
1952 return retval;
1953}
1954
1955static bool_Bool
1956parse_month_name(const char *name, int *tm_mon)
1957{
1958 static const char months[][4] = { "Jan", "Feb", "Mar", "Apr", "May",
1959 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
1960 for (int i = 0; i < 12; i++) {
1961 if (memcmp(months[i], name, 4) == 0) {
1962 *tm_mon = i;
1963 return true1;
1964 }
1965 }
1966 return false0;
1967}
1968
1969/*
1970 * Is the character a WSP character, as per RFC 5234? (space or tab).
1971 */
1972#define IS_WSP(c)((c) == ' ' || (c) == '\t') ((c) == ' ' || (c) == '\t')
1973
1974/* support hex-encoded time values? */
1975nstime_t*
1976tvb_get_string_time(tvbuff_t *tvb, const unsigned offset, const unsigned length,
1977 const unsigned encoding, nstime_t *ns, unsigned *endoff)
1978{
1979 char *begin;
1980 const char *ptr;
1981 const char *end = NULL((void*)0);
1982 int num_chars = 0;
1983 int utc_offset = 0;
1984
1985 validate_single_byte_ascii_encoding(encoding);
1986
1987 DISSECTOR_ASSERT(ns)((void) ((ns) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 1987, "ns"))))
;
1988
1989 begin = (char*) tvb_get_raw_string(NULL((void*)0), tvb, offset, length);
1990 ptr = begin;
1991
1992 while (IS_WSP(*ptr)((*ptr) == ' ' || (*ptr) == '\t'))
1993 ptr++;
1994
1995 if (*ptr) {
1996 if ((encoding & ENC_ISO_8601_DATE_TIME0x00030000) == ENC_ISO_8601_DATE_TIME0x00030000) {
1997 if (!(end = iso8601_to_nstime(ns, ptr, ISO8601_DATETIME))) {
1998
1999
2000 goto fail;
2001 }
2002 } else if ((encoding & ENC_ISO_8601_DATE_TIME_BASIC0x00100000) == ENC_ISO_8601_DATE_TIME_BASIC0x00100000) {
2003 if (!(end = iso8601_to_nstime(ns, ptr, ISO8601_DATETIME_BASIC))) {
2004
2005
2006 goto fail;
2007 }
2008 } else {
2009 struct tm tm;
2010
2011 memset(&tm, 0, sizeof(tm));
2012 tm.tm_isdst = -1;
2013 ns->secs = 0;
2014 ns->nsecs = 0;
2015
2016 /* note: sscanf is known to be inconsistent across platforms with respect
2017 to whether a %n is counted as a return value or not, so we have to use
2018 '>=' a lot */
2019 if (encoding & ENC_ISO_8601_DATE0x00010000) {
2020 /* 2014-04-07 */
2021 if (sscanf(ptr, "%d-%d-%d%n",
2022 &tm.tm_year,
2023 &tm.tm_mon,
2024 &tm.tm_mday,
2025 &num_chars) >= 3)
2026 {
2027 end = ptr + num_chars;
2028 tm.tm_mon--;
2029 if (tm.tm_year > 1900) tm.tm_year -= 1900;
2030 } else {
2031 goto fail;
2032 }
2033 }
2034 else if (encoding & ENC_ISO_8601_TIME0x00020000) {
2035 /* 2014-04-07 */
2036 if (sscanf(ptr, "%d:%d:%d%n",
2037 &tm.tm_hour,
2038 &tm.tm_min,
2039 &tm.tm_sec,
2040 &num_chars) >= 2)
2041 {
2042 /* what should we do about day/month/year? */
2043 /* setting it to "now" for now */
2044 time_t time_now = time(NULL((void*)0));
2045 struct tm *tm_now = gmtime(&time_now);
2046 if (tm_now != NULL((void*)0)) {
2047 tm.tm_year = tm_now->tm_year;
2048 tm.tm_mon = tm_now->tm_mon;
2049 tm.tm_mday = tm_now->tm_mday;
2050 } else {
2051 /* The second before the Epoch */
2052 tm.tm_year = 69;
2053 tm.tm_mon = 12;
2054 tm.tm_mday = 31;
2055 }
2056 end = ptr + num_chars;
2057 } else {
2058 goto fail;
2059 }
2060 }
2061 else if (encoding & ENC_IMF_DATE_TIME0x00040000) {
2062 /*
2063 * Match [dow,] day month year hh:mm[:ss] with
2064 * two-digit years (RFC 822) or four-digit
2065 * years (RFCs 1123, 2822, 5822). Skip
2066 * the day of week since it is locale
2067 * dependent and does not affect the resulting
2068 * date anyway.
2069 */
2070 if (g_ascii_isalpha(ptr[0])((g_ascii_table[(guchar) (ptr[0])] & G_ASCII_ALPHA) != 0) && g_ascii_isalpha(ptr[1])((g_ascii_table[(guchar) (ptr[1])] & G_ASCII_ALPHA) != 0) && g_ascii_isalpha(ptr[2])((g_ascii_table[(guchar) (ptr[2])] & G_ASCII_ALPHA) != 0) && ptr[3] == ',')
2071 ptr += 4; /* Skip day of week. */
2072
2073 /*
2074 * Parse the day-of-month and month
2075 * name.
2076 */
2077 char month_name[4] = { 0 };
2078
2079 if (sscanf(ptr, "%d %3s%n",
2080 &tm.tm_mday,
2081 month_name,
2082 &num_chars) < 2)
2083 {
2084 /* Not matched. */
2085 goto fail;
2086 }
2087 if (!parse_month_name(month_name, &tm.tm_mon)) {
2088 goto fail;
2089 }
2090 ptr += num_chars;
2091 while (IS_WSP(*ptr)((*ptr) == ' ' || (*ptr) == '\t'))
2092 ptr++;
2093
2094 /*
2095 * Scan the year. Treat 2-digit years
2096 * differently from 4-digit years.
2097 */
2098 uint32_t year;
2099 const char *yearendp;
2100
2101 if (!ws_strtou32(ptr, &yearendp, &year)) {
2102 goto fail;
2103 }
2104 if (!IS_WSP(*yearendp)((*yearendp) == ' ' || (*yearendp) == '\t')) {
2105 /* Not followed by WSP. */
2106 goto fail;
2107 }
2108 if (yearendp - ptr < 2) {
2109 /* 1-digit year. Error. */
2110 goto fail;
2111 }
2112 if (yearendp - ptr == 2) {
2113 /*
2114 * 2-digit year.
2115 *
2116 * Match RFC 2822/RFC 5322 behavior;
2117 * add 2000 to years from 0 to
2118 * 49 and 1900 to uears from 50
2119 * to 99.
2120 */
2121 if (year <= 49) {
2122 year += 2000;
2123 } else {
2124 year += 1900;
2125 }
2126 } else if (yearendp - ptr == 3) {
2127 /*
2128 * 3-digit year.
2129 *
2130 * Match RFC 2822/RFC 5322 behavior;
2131 * add 1900 to the year.
2132 */
2133 year += 1900;
2134 }
2135 tm.tm_year = year - 1900;
2136 ptr = yearendp;
2137 while (IS_WSP(*ptr)((*ptr) == ' ' || (*ptr) == '\t'))
2138 ptr++;
2139
2140 /* Parse the time. */
2141 if (sscanf(ptr, "%d:%d%n:%d%n",
2142 &tm.tm_hour,
2143 &tm.tm_min,
2144 &num_chars,
2145 &tm.tm_sec,
2146 &num_chars) < 2)
2147 {
2148 goto fail;
2149 }
2150 ptr += num_chars;
2151 while (IS_WSP(*ptr)((*ptr) == ' ' || (*ptr) == '\t'))
2152 ptr++;
2153
2154 /*
2155 * Parse the time zone.
2156 * Check for obs-zone values first.
2157 */
2158 if (g_ascii_strncasecmp(ptr, "UT", 2) == 0)
2159 {
2160 ptr += 2;
2161 }
2162 else if (g_ascii_strncasecmp(ptr, "GMT", 3) == 0)
2163 {
2164 ptr += 3;
2165 }
2166 else
2167 {
2168 char sign;
2169 int off_hr;
2170 int off_min;
2171
2172 if (sscanf(ptr, "%c%2d%2d%n",
2173 &sign,
2174 &off_hr,
2175 &off_min,
2176 &num_chars) < 3)
2177 {
2178 goto fail;
2179 }
2180
2181 /*
2182 * If sign is '+', there's a positive
2183 * UTC offset.
2184 *
2185 * If sign is '-', there's a negative
2186 * UTC offset.
2187 *
2188 * Otherwise, that's an invalid UTC
2189 * offset string.
2190 */
2191 if (sign == '+')
2192 utc_offset += (off_hr * 3600) + (off_min * 60);
2193 else if (sign == '-')
2194 utc_offset -= (off_hr * 3600) + (off_min * 60);
2195 else {
2196 /* Sign must be + or - */
2197 goto fail;
2198 }
2199 ptr += num_chars;
2200 }
2201 end = ptr;
2202 }
2203 ns->secs = mktime_utc(&tm);
2204 if (ns->secs == (time_t)-1 && errno(*__errno_location ()) != 0) {
2205 goto fail;
2206 }
2207 ns->secs += utc_offset;
2208 }
2209 } else {
2210 /* Empty string */
2211 goto fail;
2212 }
2213
2214 if (endoff)
2215 *endoff = (unsigned)(offset + (end - begin));
2216 wmem_free(NULL((void*)0), begin);
2217 return ns;
2218
2219fail:
2220 wmem_free(NULL((void*)0), begin);
2221 return NULL((void*)0);
2222}
2223
2224/* Fetch an IPv4 address, in network byte order.
2225 * We do *not* convert them to host byte order; we leave them in
2226 * network byte order. */
2227uint32_t
2228tvb_get_ipv4(tvbuff_t *tvb, const unsigned offset)
2229{
2230 const uint8_t *ptr;
2231 uint32_t addr;
2232
2233 ptr = fast_ensure_contiguous(tvb, offset, sizeof(uint32_t));
2234 memcpy(&addr, ptr, sizeof addr);
2235 return addr;
2236}
2237
2238/* Fetch an IPv6 address. */
2239void
2240tvb_get_ipv6(tvbuff_t *tvb, const unsigned offset, ws_in6_addr *addr)
2241{
2242 const uint8_t *ptr;
2243
2244 ptr = ensure_contiguous_unsigned(tvb, offset, sizeof(*addr));
2245 memcpy(addr, ptr, sizeof *addr);
2246}
2247
2248/*
2249 * These routines return the length of the address in bytes on success
2250 * and -1 if the prefix length is too long.
2251 */
2252int
2253tvb_get_ipv4_addr_with_prefix_len(tvbuff_t *tvb, const unsigned offset, ws_in4_addr *addr,
2254 uint32_t prefix_len)
2255{
2256 uint8_t addr_len;
2257
2258 if (prefix_len > 32)
2259 return -1;
2260
2261 addr_len = (prefix_len + 7) / 8;
2262 *addr = 0;
2263 tvb_memcpy(tvb, addr, offset, addr_len);
2264 if (prefix_len % 8)
2265 ((uint8_t*)addr)[addr_len - 1] &= ((0xff00 >> (prefix_len % 8)) & 0xff);
2266 return addr_len;
2267}
2268
2269/*
2270 * These routines return the length of the address in bytes on success
2271 * and -1 if the prefix length is too long.
2272 */
2273int
2274tvb_get_ipv6_addr_with_prefix_len(tvbuff_t *tvb, const unsigned offset, ws_in6_addr *addr,
2275 uint32_t prefix_len)
2276{
2277 uint32_t addr_len;
2278
2279 if (prefix_len > 128)
2280 return -1;
2281
2282 addr_len = (prefix_len + 7) / 8;
2283 memset(addr->bytes, 0, 16);
2284 tvb_memcpy(tvb, addr->bytes, offset, addr_len);
2285 if (prefix_len % 8) {
2286 addr->bytes[addr_len - 1] &=
2287 ((0xff00 >> (prefix_len % 8)) & 0xff);
2288 }
2289
2290 return addr_len;
2291}
2292
2293/* Fetch a GUID. */
2294void
2295tvb_get_ntohguid(tvbuff_t *tvb, const unsigned offset, e_guid_t *guid)
2296{
2297 const uint8_t *ptr = ensure_contiguous_unsigned(tvb, offset, GUID_LEN16);
2298
2299 guid->data1 = pntohu32(ptr + 0);
2300 guid->data2 = pntohu16(ptr + 4);
2301 guid->data3 = pntohu16(ptr + 6);
2302 memcpy(guid->data4, ptr + 8, sizeof guid->data4);
2303}
2304
2305void
2306tvb_get_letohguid(tvbuff_t *tvb, const unsigned offset, e_guid_t *guid)
2307{
2308 const uint8_t *ptr = ensure_contiguous_unsigned(tvb, offset, GUID_LEN16);
2309
2310 guid->data1 = pletohu32(ptr + 0);
2311 guid->data2 = pletohu16(ptr + 4);
2312 guid->data3 = pletohu16(ptr + 6);
2313 memcpy(guid->data4, ptr + 8, sizeof guid->data4);
2314}
2315
2316void
2317tvb_get_guid(tvbuff_t *tvb, const unsigned offset, e_guid_t *guid, const unsigned encoding)
2318{
2319 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
2320 tvb_get_letohguid(tvb, offset, guid);
2321 } else {
2322 tvb_get_ntohguid(tvb, offset, guid);
2323 }
2324}
2325
2326static const uint8_t bit_mask8[] = {
2327 0x00,
2328 0x01,
2329 0x03,
2330 0x07,
2331 0x0f,
2332 0x1f,
2333 0x3f,
2334 0x7f,
2335 0xff
2336};
2337
2338
2339/* Get a variable amount of bits
2340 *
2341 * Return a byte array with bit limited data.
2342 * When encoding is ENC_BIG_ENDIAN, the data is aligned to the left.
2343 * When encoding is ENC_LITTLE_ENDIAN, the data is aligned to the right.
2344 */
2345uint8_t *
2346tvb_get_bits_array(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned bit_offset,
2347 size_t no_of_bits, size_t *data_length, const unsigned encoding)
2348{
2349 tvbuff_t *sub_tvb;
2350 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1
Assuming the condition is true
2
Taking true branch
2351 sub_tvb = tvb_new_octet_right_aligned(tvb, bit_offset, (int32_t) no_of_bits);
3
Calling 'tvb_new_octet_right_aligned'
2352 } else {
2353 sub_tvb = tvb_new_octet_aligned(tvb, bit_offset, (int32_t) no_of_bits);
2354 }
2355 *data_length = tvb_reported_length(sub_tvb);
2356 return (uint8_t*)tvb_memdup(scope, sub_tvb, 0, *data_length);
2357}
2358
2359/* Get 1 - 8 bits */
2360uint8_t
2361tvb_get_bits8(tvbuff_t *tvb, unsigned bit_offset, const int no_of_bits)
2362{
2363 DISSECTOR_ASSERT_HINT(no_of_bits <= 8, "Too many bits requested for 8-bit return type")((void) ((no_of_bits <= 8) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\" (%s)", "epan/tvbuff.c", 2363
, "no_of_bits <= 8", "Too many bits requested for 8-bit return type"
))))
;
2364 return (uint8_t)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
2365}
2366
2367/* Get 1 - 16 bits */
2368uint16_t
2369tvb_get_bits16(tvbuff_t *tvb, unsigned bit_offset, const int no_of_bits, const unsigned encoding)
2370{
2371 DISSECTOR_ASSERT_HINT(no_of_bits <= 16, "Too many bits requested for 16-bit return type")((void) ((no_of_bits <= 16) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\" (%s)", "epan/tvbuff.c", 2371
, "no_of_bits <= 16", "Too many bits requested for 16-bit return type"
))))
;
2372 return (uint16_t)tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
2373}
2374
2375/* Get 1 - 32 bits */
2376uint32_t
2377tvb_get_bits32(tvbuff_t *tvb, unsigned bit_offset, const int no_of_bits, const unsigned encoding)
2378{
2379 DISSECTOR_ASSERT_HINT(no_of_bits <= 32, "Too many bits requested for 32-bit return type")((void) ((no_of_bits <= 32) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\" (%s)", "epan/tvbuff.c", 2379
, "no_of_bits <= 32", "Too many bits requested for 32-bit return type"
))))
;
2380 return (uint32_t)tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
2381}
2382
2383/* Get 1 - 64 bits */
2384uint64_t
2385tvb_get_bits64(tvbuff_t *tvb, unsigned bit_offset, const int no_of_bits, const unsigned encoding)
2386{
2387 DISSECTOR_ASSERT_HINT(no_of_bits <= 64, "Too many bits requested for 64-bit return type")((void) ((no_of_bits <= 64) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\" (%s)", "epan/tvbuff.c", 2387
, "no_of_bits <= 64", "Too many bits requested for 64-bit return type"
))))
;
2388
2389 /* encoding determines bit numbering within octet array */
2390 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
2391 return _tvb_get_bits64_le(tvb, bit_offset, no_of_bits);
2392 } else {
2393 return _tvb_get_bits64(tvb, bit_offset, no_of_bits);
2394 }
2395}
2396
2397/*
2398 * This function will dissect a sequence of bits that does not need to be byte aligned; the bits
2399 * set will be shown in the tree as ..10 10.. and the integer value returned if return_value is set.
2400 * Offset should be given in bits from the start of the tvb.
2401 * Bits within octet are numbered from MSB (0) to LSB (7). Bit at bit_offset is return value most significant bit.
2402 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
2403 */
2404static uint64_t
2405_tvb_get_bits64(tvbuff_t *tvb, unsigned bit_offset, const int total_no_of_bits)
2406{
2407 uint64_t value;
2408 unsigned octet_offset = bit_offset >> 3;
2409 uint8_t required_bits_in_first_octet = 8 - (bit_offset % 8);
2410
2411 if(required_bits_in_first_octet > total_no_of_bits)
2412 {
2413 /* the required bits don't extend to the end of the first octet */
2414 uint8_t right_shift = required_bits_in_first_octet - total_no_of_bits;
2415 value = (tvb_get_uint8(tvb, octet_offset) >> right_shift) & bit_mask8[total_no_of_bits % 8];
2416 }
2417 else
2418 {
2419 uint8_t remaining_bit_length = total_no_of_bits;
2420
2421 /* get the bits up to the first octet boundary */
2422 value = 0;
2423 required_bits_in_first_octet %= 8;
2424 if(required_bits_in_first_octet != 0)
2425 {
2426 value = tvb_get_uint8(tvb, octet_offset) & bit_mask8[required_bits_in_first_octet];
2427 remaining_bit_length -= required_bits_in_first_octet;
2428 octet_offset ++;
2429 }
2430 /* take the biggest words, shorts or octets that we can */
2431 while (remaining_bit_length > 7)
2432 {
2433 switch (remaining_bit_length >> 4)
2434 {
2435 case 0:
2436 /* 8 - 15 bits. (note that 0 - 7 would have dropped out of the while() loop) */
2437 value <<= 8;
2438 value += tvb_get_uint8(tvb, octet_offset);
2439 remaining_bit_length -= 8;
2440 octet_offset ++;
2441 break;
2442
2443 case 1:
2444 /* 16 - 31 bits */
2445 value <<= 16;
2446 value += tvb_get_ntohs(tvb, octet_offset);
2447 remaining_bit_length -= 16;
2448 octet_offset += 2;
2449 break;
2450
2451 case 2:
2452 case 3:
2453 /* 32 - 63 bits */
2454 value <<= 32;
2455 value += tvb_get_ntohl(tvb, octet_offset);
2456 remaining_bit_length -= 32;
2457 octet_offset += 4;
2458 break;
2459
2460 default:
2461 /* 64 bits (or more???) */
2462 value = tvb_get_ntoh64(tvb, octet_offset);
2463 remaining_bit_length -= 64;
2464 octet_offset += 8;
2465 break;
2466 }
2467 }
2468 /* get bits from any partial octet at the tail */
2469 if(remaining_bit_length)
2470 {
2471 value <<= remaining_bit_length;
2472 value += (tvb_get_uint8(tvb, octet_offset) >> (8 - remaining_bit_length));
2473 }
2474 }
2475 return value;
2476}
2477
2478/*
2479 * Offset should be given in bits from the start of the tvb.
2480 * Bits within octet are numbered from LSB (0) to MSB (7). Bit at bit_offset is return value least significant bit.
2481 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
2482 */
2483static uint64_t
2484_tvb_get_bits64_le(tvbuff_t *tvb, unsigned bit_offset, const int total_no_of_bits)
2485{
2486 uint64_t value = 0;
2487 unsigned octet_offset = bit_offset / 8;
2488 int remaining_bits = total_no_of_bits;
2489 int shift = 0;
2490
2491 if (remaining_bits > 64)
2492 {
2493 remaining_bits = 64;
2494 }
2495
2496 if (bit_offset % 8)
2497 {
2498 /* not aligned, extract bits from first octet */
2499 shift = 8 - (bit_offset % 8);
2500 value = tvb_get_uint8(tvb, octet_offset) >> (bit_offset % 8);
2501 if (shift > remaining_bits)
2502 {
2503 /* keep only the requested bits */
2504 value &= (UINT64_C(1)1UL << remaining_bits) - 1;
2505 remaining_bits = 0;
2506 }
2507 else
2508 {
2509 remaining_bits -= shift;
2510 }
2511 octet_offset++;
2512 }
2513
2514 while (remaining_bits > 0)
2515 {
2516 /* take the biggest words, shorts or octets that we can */
2517 if (remaining_bits >= 32)
2518 {
2519 value |= ((uint64_t)tvb_get_letohl(tvb, octet_offset) << shift);
2520 shift += 32;
2521 remaining_bits -= 32;
2522 octet_offset += 4;
2523 }
2524 else if (remaining_bits >= 16)
2525 {
2526 value |= ((uint64_t)tvb_get_letohs(tvb, octet_offset) << shift);
2527 shift += 16;
2528 remaining_bits -= 16;
2529 octet_offset += 2;
2530 }
2531 else if (remaining_bits >= 8)
2532 {
2533 value |= ((uint64_t)tvb_get_uint8(tvb, octet_offset) << shift);
2534 shift += 8;
2535 remaining_bits -= 8;
2536 octet_offset += 1;
2537 }
2538 else
2539 {
2540 unsigned mask = (1 << remaining_bits) - 1;
2541 value |= (((uint64_t)tvb_get_uint8(tvb, octet_offset) & mask) << shift);
2542 shift += remaining_bits;
2543 remaining_bits = 0;
2544 octet_offset += 1;
2545 }
2546 }
2547 return value;
2548}
2549
2550/* Get 1 - 32 bits (should be deprecated as same as tvb_get_bits32??) */
2551uint32_t
2552tvb_get_bits(tvbuff_t *tvb, const unsigned bit_offset, const int no_of_bits, const unsigned encoding)
2553{
2554 return (uint32_t)tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
2555}
2556
2557static bool_Bool
2558tvb_find_uint8_generic(tvbuff_t *tvb, unsigned abs_offset, unsigned limit, uint8_t needle, unsigned *end_offset)
2559{
2560 const uint8_t *ptr;
2561 const uint8_t *result;
2562
2563 if (end_offset) {
2564 *end_offset = abs_offset + limit;
2565 }
2566
2567 ptr = ensure_contiguous_unsigned(tvb, abs_offset, limit); /* tvb_get_ptr() */
2568 if (!ptr)
2569 return false0;
2570
2571 result = (const uint8_t *) memchr(ptr, needle, limit);
2572 if (!result)
2573 return false0;
2574
2575 if (end_offset) {
2576 *end_offset = (unsigned)((result - ptr) + abs_offset);
2577 }
2578 return true1;
2579}
2580
2581/* Find first occurrence of needle in tvbuff, starting at offset. Searches
2582 * at most maxlength number of bytes; if maxlength is -1, searches to
2583 * end of tvbuff.
2584 * Returns the offset of the found needle, or -1 if not found.
2585 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
2586 * in that case, -1 will be returned if the boundary is reached before
2587 * finding needle. */
2588int
2589tvb_find_uint8(tvbuff_t *tvb, const unsigned offset, const int maxlength, const uint8_t needle)
2590{
2591 const uint8_t *result;
2592 unsigned limit = 0, end_offset;
2593 int exception;
2594
2595 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2595, "tvb && tvb->initialized"
))))
;
2596
2597 exception = validate_offset_and_remaining(tvb, offset, &limit);
2598 if (exception)
2599 THROW(exception)except_throw(1, (exception), ((void*)0));
2600
2601 /* Only search to end of tvbuff, w/o throwing exception. */
2602 if (maxlength >= 0 && limit > (unsigned) maxlength) {
2603 /* Maximum length doesn't go past end of tvbuff; search
2604 to that value. */
2605 limit = (unsigned) maxlength;
2606 }
2607
2608 /* If we have real data, perform our search now. */
2609 if (tvb->real_data) {
2610 result = (const uint8_t *)memchr(tvb->real_data + offset, needle, limit);
2611 if (result == NULL((void*)0)) {
2612 return -1;
2613 }
2614 else {
2615 return (int) (result - tvb->real_data);
2616 }
2617 }
2618
2619 if (tvb->ops->tvb_find_uint8) {
2620 if (!tvb->ops->tvb_find_uint8(tvb, offset, limit, needle, &end_offset)) {
2621 return -1;
2622 }
2623 } else if (!tvb_find_uint8_generic(tvb, offset, limit, needle, &end_offset)) {
2624 return -1;
2625 }
2626 return (int)end_offset;
2627}
2628
2629static bool_Bool
2630_tvb_find_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, const uint8_t needle, unsigned *end_offset)
2631{
2632 const uint8_t *result;
2633
2634 /* If we have real data, perform our search now. */
2635 if (tvb->real_data) {
2636 result = (const uint8_t *)memchr(tvb->real_data + offset, needle, limit);
2637 if (result == NULL((void*)0)) {
2638 if (end_offset) {
2639 *end_offset = offset + limit;
2640 }
2641 return false0;
2642 }
2643 else {
2644 if (end_offset) {
2645 *end_offset = (unsigned)(result - tvb->real_data);
2646 }
2647 return true1;
2648 }
2649 }
2650
2651 if (tvb->ops->tvb_find_uint8)
2652 return tvb->ops->tvb_find_uint8(tvb, offset, limit, needle, end_offset);
2653
2654 return tvb_find_uint8_generic(tvb, offset, limit, needle, end_offset);
2655}
2656
2657bool_Bool
2658tvb_find_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, const uint8_t needle, unsigned *end_offset)
2659{
2660 unsigned limit = 0;
2661 int exception;
2662
2663 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2663, "tvb && tvb->initialized"
))))
;
2664
2665 exception = validate_offset_and_remaining(tvb, offset, &limit);
2666 if (exception)
2667 THROW(exception)except_throw(1, (exception), ((void*)0));
2668
2669 /* Only search to end of tvbuff, w/o throwing exception. */
2670 if (limit > maxlength) {
2671 /* Maximum length doesn't go past end of tvbuff; search
2672 to that value. */
2673 limit = maxlength;
2674 }
2675
2676 return _tvb_find_uint8_length(tvb, offset, limit, needle, end_offset);
2677}
2678
2679bool_Bool
2680tvb_find_uint8_remaining(tvbuff_t *tvb, const unsigned offset, const uint8_t needle, unsigned *end_offset)
2681{
2682 unsigned limit = 0;
2683 int exception;
2684
2685 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2685, "tvb && tvb->initialized"
))))
;
2686
2687 exception = validate_offset_and_remaining(tvb, offset, &limit);
2688 if (exception)
2689 THROW(exception)except_throw(1, (exception), ((void*)0));
2690
2691 return _tvb_find_uint8_length(tvb, offset, limit, needle, end_offset);
2692}
2693
2694static bool_Bool
2695_tvb_find_uint16_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, const uint16_t needle, unsigned *end_offset)
2696{
2697 const uint8_t needle1 = ((needle & 0xFF00) >> 8);
2698 const uint8_t needle2 = ((needle & 0x00FF) >> 0);
2699 unsigned searched_bytes = 0;
2700 unsigned pos = offset;
2701
2702 if (end_offset) {
2703 *end_offset = offset + limit;
2704 }
2705
2706 do {
2707 if (!_tvb_find_uint8_length(tvb, pos, limit - searched_bytes, needle1, &pos)) {
2708 return false0;
2709 }
2710
2711 /* Bytes searched so far (not counting the second byte) */
2712 searched_bytes = pos - offset + 1;
2713
2714 /* Test vs. equality to account for the second byte */
2715 if (searched_bytes >= limit) {
2716 return false0;
2717 }
2718
2719 if (_tvb_find_uint8_length(tvb, pos + 1, 1, needle2, NULL((void*)0))) {
2720 if (end_offset) {
2721 *end_offset = pos;
2722 }
2723 return true1;
2724 }
2725
2726 pos += 1;
2727 searched_bytes += 1;
2728 } while (searched_bytes < limit);
2729
2730 return false0;
2731}
2732
2733/* Same as tvb_find_uint8() with 16bit needle. */
2734int
2735tvb_find_uint16(tvbuff_t *tvb, const int offset, const int maxlength,
2736 const uint16_t needle)
2737{
2738 unsigned abs_offset = 0;
2739 unsigned limit = 0;
2740 unsigned end_offset;
2741 int exception;
2742
2743 exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &limit);
2744 if (exception)
2745 THROW(exception)except_throw(1, (exception), ((void*)0));
2746
2747 /* Only search to end of tvbuff, w/o throwing exception. */
2748 if (maxlength >= 0 && limit > (unsigned) maxlength) {
2749 /* Maximum length doesn't go past end of tvbuff; search
2750 to that value. */
2751 limit = (unsigned) maxlength;
2752 }
2753
2754 if (!_tvb_find_uint16_length(tvb, abs_offset, limit, needle, &end_offset)) {
2755 return -1;
2756 }
2757
2758 return end_offset;
2759}
2760
2761bool_Bool
2762tvb_find_uint16_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, const uint16_t needle, unsigned *end_offset)
2763{
2764 unsigned limit = 0;
2765 int exception;
2766
2767 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2767, "tvb && tvb->initialized"
))))
;
2768
2769 exception = validate_offset_and_remaining(tvb, offset, &limit);
2770 if (exception)
2771 THROW(exception)except_throw(1, (exception), ((void*)0));
2772
2773 /* Only search to end of tvbuff, w/o throwing exception. */
2774 if (limit > maxlength) {
2775 /* Maximum length doesn't go past end of tvbuff; search
2776 to that value. */
2777 limit = maxlength;
2778 }
2779
2780 return _tvb_find_uint16_length(tvb, offset, limit, needle, end_offset);
2781}
2782
2783bool_Bool
2784tvb_find_uint16_remaining(tvbuff_t *tvb, const unsigned offset, const uint16_t needle, unsigned *end_offset)
2785{
2786 unsigned limit = 0;
2787 int exception;
2788
2789 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2789, "tvb && tvb->initialized"
))))
;
2790
2791 exception = validate_offset_and_remaining(tvb, offset, &limit);
2792 if (exception)
2793 THROW(exception)except_throw(1, (exception), ((void*)0));
2794
2795 return _tvb_find_uint16_length(tvb, offset, limit, needle, end_offset);
2796}
2797
2798static inline bool_Bool
2799tvb_ws_mempbrk_uint8_generic(tvbuff_t *tvb, unsigned abs_offset, unsigned limit, const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
2800{
2801 const uint8_t *ptr;
2802 const uint8_t *result;
2803
2804 if (found_offset) {
2805 *found_offset = abs_offset + limit;
2806 }
2807
2808 ptr = ensure_contiguous_unsigned(tvb, abs_offset, limit); /* tvb_get_ptr */
2809 if (!ptr)
2810 return false0;
2811
2812 result = ws_mempbrk_exec(ptr, limit, pattern, found_needle);
2813 if (!result)
2814 return false0;
2815
2816 if (found_offset) {
2817 *found_offset = (unsigned)((result - ptr) + abs_offset);
2818 }
2819 return true1;
2820}
2821
2822
2823/* Find first occurrence of any of the pattern chars in tvbuff, starting at offset.
2824 * Searches at most maxlength number of bytes; if maxlength is -1, searches
2825 * to end of tvbuff.
2826 * Returns the offset of the found needle, or -1 if not found.
2827 * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
2828 * in that case, -1 will be returned if the boundary is reached before
2829 * finding needle. */
2830int
2831tvb_ws_mempbrk_pattern_uint8(tvbuff_t *tvb, const unsigned offset, const int maxlength,
2832 const ws_mempbrk_pattern* pattern, unsigned char *found_needle)
2833{
2834 const uint8_t *result;
2835 unsigned limit = 0, found_offset;
2836 int exception;
2837
2838 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2838, "tvb && tvb->initialized"
))))
;
2839
2840 exception = validate_offset_and_remaining(tvb, offset, &limit);
2841 if (exception)
2842 THROW(exception)except_throw(1, (exception), ((void*)0));
2843
2844 /* Only search to end of tvbuff, w/o throwing exception. */
2845 if (limit > (unsigned) maxlength) {
2846 /* Maximum length doesn't go past end of tvbuff; search
2847 to that value. */
2848 limit = maxlength;
2849 }
2850
2851 /* If we have real data, perform our search now. */
2852 if (tvb->real_data) {
2853 result = ws_mempbrk_exec(tvb->real_data + offset, limit, pattern, found_needle);
2854 if (result == NULL((void*)0)) {
2855 return -1;
2856 }
2857 else {
2858 return (int) (result - tvb->real_data);
2859 }
2860 }
2861
2862 if (tvb->ops->tvb_ws_mempbrk_pattern_uint8) {
2863 if (!tvb->ops->tvb_ws_mempbrk_pattern_uint8(tvb, offset, limit, pattern, &found_offset, found_needle)) {
2864 return -1;
2865 }
2866 } else if (!tvb_ws_mempbrk_uint8_generic(tvb, offset, limit, pattern, &found_offset, found_needle)) {
2867 return -1;
2868 }
2869 return (int)found_offset;
2870}
2871
2872static bool_Bool
2873_tvb_ws_mempbrk_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit,
2874 const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
2875{
2876 const uint8_t *result;
2877
2878 /* If we have real data, perform our search now. */
2879 if (tvb->real_data) {
2880 result = ws_mempbrk_exec(tvb->real_data + offset, limit, pattern, found_needle);
2881 if (result == NULL((void*)0)) {
2882 if (found_offset) {
2883 *found_offset = offset + limit;
2884 }
2885 return false0;
2886 }
2887 else {
2888 if (found_offset) {
2889 *found_offset = (unsigned)(result - tvb->real_data);
2890 }
2891 return true1;
2892 }
2893 }
2894
2895 if (tvb->ops->tvb_ws_mempbrk_pattern_uint8)
2896 return tvb->ops->tvb_ws_mempbrk_pattern_uint8(tvb, offset, limit, pattern, found_offset, found_needle);
2897
2898 return tvb_ws_mempbrk_uint8_generic(tvb, offset, limit, pattern, found_offset, found_needle);
2899}
2900
2901bool_Bool
2902tvb_ws_mempbrk_uint8_remaining(tvbuff_t *tvb, const unsigned offset,
2903 const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
2904{
2905 unsigned limit = 0;
2906 int exception;
2907
2908 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2908, "tvb && tvb->initialized"
))))
;
2909
2910 exception = validate_offset_and_remaining(tvb, offset, &limit);
2911 if (exception)
2912 THROW(exception)except_throw(1, (exception), ((void*)0));
2913
2914 return _tvb_ws_mempbrk_uint8_length(tvb, offset, limit, pattern, found_offset, found_needle);
2915}
2916
2917bool_Bool
2918tvb_ws_mempbrk_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength,
2919 const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
2920{
2921 unsigned limit = 0;
2922 int exception;
2923
2924 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2924, "tvb && tvb->initialized"
))))
;
2925
2926 exception = validate_offset_and_remaining(tvb, offset, &limit);
2927 if (exception)
2928 THROW(exception)except_throw(1, (exception), ((void*)0));
2929
2930 /* Only search to end of tvbuff, w/o throwing exception. */
2931 if (limit > maxlength) {
2932 /* Maximum length doesn't go past end of tvbuff; search
2933 to that value. */
2934 limit = maxlength;
2935 }
2936
2937 return _tvb_ws_mempbrk_uint8_length(tvb, offset, limit, pattern, found_offset, found_needle);
2938}
2939
2940/* Find size of stringz (NUL-terminated string) by looking for terminating
2941 * NUL. The size of the string includes the terminating NUL.
2942 *
2943 * If the NUL isn't found, it throws the appropriate exception.
2944 */
2945unsigned
2946tvb_strsize(tvbuff_t *tvb, const unsigned offset)
2947{
2948 unsigned nul_offset;
2949
2950 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2950, "tvb && tvb->initialized"
))))
;
2951
2952 validate_offset(tvb, offset);
2953 if (!tvb_find_uint8_remaining(tvb, offset, 0, &nul_offset)) {
2954 /*
2955 * OK, we hit the end of the tvbuff, so we should throw
2956 * an exception.
2957 */
2958 if (tvb->length < tvb->contained_length) {
2959 THROW(BoundsError)except_throw(1, (1), ((void*)0));
2960 } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
2961 THROW(FragmentBoundsError)except_throw(1, (4), ((void*)0));
2962 } else if (tvb->length < tvb->reported_length) {
2963 THROW(ContainedBoundsError)except_throw(1, (2), ((void*)0));
2964 } else {
2965 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
2966 }
2967 }
2968 return (nul_offset - offset) + 1;
2969}
2970
2971/* UTF-16/UCS-2 version of tvb_strsize */
2972/* Returns number of bytes including the (two-bytes) null terminator */
2973unsigned
2974tvb_unicode_strsize(tvbuff_t *tvb, const unsigned offset)
2975{
2976 unsigned cur_offset = offset;
2977 gunichar2 uchar;
2978
2979 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2979, "tvb && tvb->initialized"
))))
;
2980
2981 /* Note: don't use tvb_find_uint16 because it must be aligned */
2982 do {
2983 /* Endianness doesn't matter when looking for null */
2984 uchar = tvb_get_ntohs(tvb, cur_offset);
2985 /* Make sure we don't overflow */
2986 if (ckd_add(&cur_offset, cur_offset, 2)__builtin_add_overflow((cur_offset), (2), (&cur_offset))) {
2987 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
2988 }
2989 } while(uchar != 0);
2990
2991 return cur_offset - offset;
2992}
2993
2994/* Find length of string by looking for end of string ('\0'), up to
2995 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
2996 * of tvbuff.
2997 * Returns -1 if 'maxlength' reached before finding EOS. */
2998int
2999tvb_strnlen(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
3000{
3001 unsigned result_offset;
3002
3003 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3003, "tvb && tvb->initialized"
))))
;
3004
3005 /* TODO - this needs a variant that returns a bool
3006 * and sets a unsigned offset to the value if true. */
3007 if (!tvb_find_uint8_length(tvb, offset, maxlength, 0, &result_offset)) {
3008 return -1;
3009 }
3010 else {
3011 return (int)(result_offset - offset);
3012 }
3013}
3014
3015/*
3016 * Implement strneql etc
3017 */
3018
3019/*
3020 * Call strncmp after checking if enough chars left, returning 0 if
3021 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
3022 */
3023int
3024tvb_strneql(tvbuff_t *tvb, const unsigned offset, const char *str, const size_t size)
3025{
3026 const uint8_t *ptr;
3027
3028 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3029
3030 if (ptr) {
3031 int cmp = strncmp((const char *)ptr, str, size);
3032
3033 /*
3034 * Return 0 if equal, -1 otherwise.
3035 */
3036 return (cmp == 0 ? 0 : -1);
3037 } else {
3038 /*
3039 * Not enough characters in the tvbuff to match the
3040 * string.
3041 */
3042 return -1;
3043 }
3044}
3045
3046/*
3047 * Call g_ascii_strncasecmp after checking if enough chars left, returning
3048 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
3049 */
3050int
3051tvb_strncaseeql(tvbuff_t *tvb, const unsigned offset, const char *str, const size_t size)
3052{
3053 const uint8_t *ptr;
3054
3055 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3056
3057 if (ptr) {
3058 int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
3059
3060 /*
3061 * Return 0 if equal, -1 otherwise.
3062 */
3063 return (cmp == 0 ? 0 : -1);
3064 } else {
3065 /*
3066 * Not enough characters in the tvbuff to match the
3067 * string.
3068 */
3069 return -1;
3070 }
3071}
3072
3073/*
3074 * Check that the tvbuff contains at least size bytes, starting at
3075 * offset, and that those bytes are equal to str. Return 0 for success
3076 * and -1 for error. This function does not throw an exception.
3077 */
3078int
3079tvb_memeql(tvbuff_t *tvb, const unsigned offset, const uint8_t *str, size_t size)
3080{
3081 const uint8_t *ptr;
3082
3083 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3084
3085 if (ptr) {
3086 int cmp = memcmp(ptr, str, size);
3087
3088 /*
3089 * Return 0 if equal, -1 otherwise.
3090 */
3091 return (cmp == 0 ? 0 : -1);
3092 } else {
3093 /*
3094 * Not enough characters in the tvbuff to match the
3095 * string.
3096 */
3097 return -1;
3098 }
3099}
3100
3101/**
3102 * Format the data in the tvb from offset for size.
3103 */
3104char *
3105tvb_format_text(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3106{
3107 const uint8_t *ptr;
3108
3109 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3110 return format_text(scope, (const char*)ptr, size);
3111}
3112
3113/*
3114 * Format the data in the tvb from offset for length ...
3115 */
3116char *
3117tvb_format_text_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3118{
3119 const uint8_t *ptr;
3120
3121 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3122 return format_text_wsp(allocator, (const char*)ptr, size);
3123}
3124
3125/**
3126 * Like "tvb_format_text()", but for null-padded strings; don't show
3127 * the null padding characters as "\000".
3128 */
3129char *
3130tvb_format_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3131{
3132 const uint8_t *ptr, *p;
3133 unsigned stringlen;
3134
3135 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3136 for (p = ptr, stringlen = 0; stringlen < size && *p != '\0'; p++, stringlen++)
3137 ;
3138 return format_text(scope, (const char*)ptr, stringlen);
3139}
3140
3141/*
3142 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
3143 * the null padding characters as "\000".
3144 */
3145char *
3146tvb_format_stringzpad_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3147{
3148 const uint8_t *ptr, *p;
3149 unsigned stringlen;
3150
3151 ptr = ensure_contiguous(tvb, offset, size);
3152 for (p = ptr, stringlen = 0; stringlen < size && *p != '\0'; p++, stringlen++)
3153 ;
3154 return format_text_wsp(allocator, (const char*)ptr, stringlen);
3155}
3156
3157/*
3158 * All string functions below take a scope as an argument.
3159 *
3160 *
3161 * If scope is NULL, memory is allocated with g_malloc() and user must
3162 * explicitly free it with g_free().
3163 * If scope is not NULL, memory is allocated with the corresponding pool
3164 * lifetime.
3165 *
3166 * All functions throw an exception if the tvbuff ends before the string
3167 * does.
3168 */
3169
3170/*
3171 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3172 * of bytes referred to by the tvbuff, offset, and length as an ASCII string,
3173 * with all bytes with the high-order bit set being invalid, and return a
3174 * pointer to a UTF-8 string, allocated using the wmem scope.
3175 *
3176 * Octets with the highest bit set will be converted to the Unicode
3177 * REPLACEMENT CHARACTER.
3178 */
3179static uint8_t *
3180tvb_get_ascii_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3181{
3182 const uint8_t *ptr;
3183
3184 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3185 return get_ascii_string(scope, ptr, length);
3186}
3187
3188/*
3189 * Given a wmem scope, a tvbuff, an offset, a length, and a translation table,
3190 * treat the string of bytes referred to by the tvbuff, offset, and length
3191 * as a string encoded using one octet per character, with octets with the
3192 * high-order bit clear being mapped by the translation table to 2-byte
3193 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3194 * CHARACTER) and octets with the high-order bit set being mapped to
3195 * REPLACEMENT CHARACTER, and return a pointer to a UTF-8 string,
3196 * allocated using the wmem scope.
3197 *
3198 * Octets with the highest bit set will be converted to the Unicode
3199 * REPLACEMENT CHARACTER.
3200 */
3201static uint8_t *
3202tvb_get_iso_646_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3203{
3204 const uint8_t *ptr;
3205
3206 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3207 return get_iso_646_string(scope, ptr, length, table);
3208}
3209
3210/*
3211 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3212 * of bytes referred to by the tvbuff, the offset. and the length as a UTF-8
3213 * string, and return a pointer to a UTF-8 string, allocated using the wmem
3214 * scope, with all ill-formed sequences replaced with the Unicode REPLACEMENT
3215 * CHARACTER according to the recommended "best practices" given in the Unicode
3216 * Standard and specified by W3C/WHATWG.
3217 *
3218 * Note that in conformance with the Unicode Standard, this treats three
3219 * byte sequences corresponding to UTF-16 surrogate halves (paired or unpaired)
3220 * and two byte overlong encodings of 7-bit ASCII characters as invalid and
3221 * substitutes REPLACEMENT CHARACTER for them. Explicit support for nonstandard
3222 * derivative encoding formats (e.g. CESU-8, Java Modified UTF-8, WTF-8) could
3223 * be added later.
3224 */
3225static uint8_t *
3226tvb_get_utf_8_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3227{
3228 const uint8_t *ptr;
3229
3230 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3231 return get_utf_8_string(scope, ptr, length);
3232}
3233
3234/*
3235 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3236 * of bytes referred to by the tvbuff, the offset, and the length as a
3237 * raw string, and return a pointer to that string, allocated using the
3238 * wmem scope. This means a null is appended at the end, but no replacement
3239 * checking is done otherwise, unlike tvb_get_utf_8_string().
3240 */
3241static inline uint8_t *
3242tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3243{
3244 uint8_t *strbuf;
3245
3246 tvb_ensure_bytes_exist(tvb, offset, length);
3247 strbuf = (uint8_t *)wmem_alloc(scope, length + 1);
3248 tvb_memcpy(tvb, strbuf, offset, length);
3249 strbuf[length] = '\0';
3250 return strbuf;
3251}
3252
3253/*
3254 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3255 * of bytes referred to by the tvbuff, the offset, and the length as an
3256 * ISO 8859/1 string, and return a pointer to a UTF-8 string, allocated
3257 * using the wmem scope.
3258 */
3259static uint8_t *
3260tvb_get_string_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3261{
3262 const uint8_t *ptr;
3263
3264 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3265 return get_8859_1_string(scope, ptr, length);
3266}
3267
3268/*
3269 * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3270 * table, treat the string of bytes referred to by the tvbuff, the offset,
3271 * and the length as a string encoded using one octet per character, with
3272 * octets with the high-order bit clear being ASCII and octets with the
3273 * high-order bit set being mapped by the translation table to 2-byte
3274 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3275 * CHARACTER), and return a pointer to a UTF-8 string, allocated with the
3276 * wmem scope.
3277 */
3278static uint8_t *
3279tvb_get_string_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3280{
3281 const uint8_t *ptr;
3282
3283 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3284 return get_unichar2_string(scope, ptr, length, table);
3285}
3286
3287/*
3288 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3289 * giving the byte order, treat the string of bytes referred to by the
3290 * tvbuff, the offset, and the length as a UCS-2 encoded string in
3291 * the byte order in question, containing characters from the Basic
3292 * Multilingual Plane (plane 0) of Unicode, and return a pointer to a
3293 * UTF-8 string, allocated with the wmem scope.
3294 *
3295 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3296 * optionally with ENC_BOM.
3297 *
3298 * Specify length in bytes.
3299 *
3300 * XXX - should map lead and trail surrogate values to REPLACEMENT
3301 * CHARACTERs (0xFFFD)?
3302 * XXX - if there are an odd number of bytes, should put a
3303 * REPLACEMENT CHARACTER at the end.
3304 */
3305static uint8_t *
3306tvb_get_ucs_2_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3307{
3308 const uint8_t *ptr;
3309
3310 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3311 return get_ucs_2_string(scope, ptr, length, encoding);
3312}
3313
3314/*
3315 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3316 * giving the byte order, treat the string of bytes referred to by the
3317 * tvbuff, the offset, and the length as a UTF-16 encoded string in
3318 * the byte order in question, and return a pointer to a UTF-8 string,
3319 * allocated with the wmem scope.
3320 *
3321 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3322 * optionally with ENC_BOM.
3323 *
3324 * Specify length in bytes.
3325 *
3326 * XXX - should map surrogate errors to REPLACEMENT CHARACTERs (0xFFFD).
3327 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3328 * XXX - if there are an odd number of bytes, should put a
3329 * REPLACEMENT CHARACTER at the end.
3330 */
3331static uint8_t *
3332tvb_get_utf_16_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3333{
3334 const uint8_t *ptr;
3335
3336 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3337 return get_utf_16_string(scope, ptr, length, encoding);
3338}
3339
3340/*
3341 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3342 * giving the byte order, treat the string of bytes referred to by the
3343 * tvbuff, the offset, and the length as a UCS-4 encoded string in
3344 * the byte order in question, and return a pointer to a UTF-8 string,
3345 * allocated with the wmem scope.
3346 *
3347 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3348 * optionally with ENC_BOM.
3349 *
3350 * Specify length in bytes
3351 *
3352 * XXX - should map lead and trail surrogate values to a "substitute"
3353 * UTF-8 character?
3354 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3355 * XXX - if the number of bytes isn't a multiple of 4, should put a
3356 * REPLACEMENT CHARACTER at the end.
3357 */
3358static char *
3359tvb_get_ucs_4_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3360{
3361 const uint8_t *ptr;
3362
3363 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3364 return (char*)get_ucs_4_string(scope, ptr, length, encoding);
3365}
3366
3367char *
3368tvb_get_ts_23_038_7bits_string_packed(wmem_allocator_t *scope, tvbuff_t *tvb,
3369 const unsigned bit_offset, unsigned no_of_chars)
3370{
3371 unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3372 unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3373 const uint8_t *ptr;
3374
3375 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3375, "tvb && tvb->initialized"
))))
;
3376
3377 ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3378 return (char*)get_ts_23_038_7bits_string_packed(scope, ptr, bit_offset, no_of_chars);
3379}
3380
3381char *
3382tvb_get_ts_23_038_7bits_string_unpacked(wmem_allocator_t *scope, tvbuff_t *tvb,
3383 const unsigned offset, unsigned length)
3384{
3385 const uint8_t *ptr;
3386
3387 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3387, "tvb && tvb->initialized"
))))
;
3388
3389 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3390 return (char*)get_ts_23_038_7bits_string_unpacked(scope, ptr, length);
3391}
3392
3393char *
3394tvb_get_etsi_ts_102_221_annex_a_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3395 const unsigned offset, unsigned length)
3396{
3397 const uint8_t *ptr;
3398
3399 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3399, "tvb && tvb->initialized"
))))
;
3400
3401 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3402 return (char*)get_etsi_ts_102_221_annex_a_string(scope, ptr, length);
3403}
3404
3405char *
3406tvb_get_ascii_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3407 const unsigned bit_offset, unsigned no_of_chars)
3408{
3409 unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3410 unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3411 const uint8_t *ptr;
3412
3413 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3413, "tvb && tvb->initialized"
))))
;
3414
3415 ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3416 return (char*)get_ascii_7bits_string(scope, ptr, bit_offset, no_of_chars);
3417}
3418
3419/*
3420 * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3421 * table, treat the string of bytes referred to by the tvbuff, the offset,
3422 * and the length as a string encoded using one octet per character, with
3423 * octets being mapped by the translation table to 2-byte Unicode Basic
3424 * Multilingual Plane characters (including REPLACEMENT CHARACTER), and
3425 * return a pointer to a UTF-8 string, allocated with the wmem scope.
3426 */
3427static uint8_t *
3428tvb_get_nonascii_unichar2_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[256])
3429{
3430 const uint8_t *ptr;
3431
3432 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3433 return get_nonascii_unichar2_string(scope, ptr, length, table);
3434}
3435
3436/*
3437 * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3438 * referred to by the tvbuff, offset, and length as a GB18030 encoded string,
3439 * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3440 * converted having substituted REPLACEMENT CHARACTER according to the
3441 * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3442 * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3443 *
3444 * As expected, this will also decode GBK and GB2312 strings.
3445 */
3446static uint8_t *
3447tvb_get_gb18030_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3448{
3449 const uint8_t *ptr;
3450
3451 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3452 return get_gb18030_string(scope, ptr, length);
3453}
3454
3455/*
3456 * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3457 * referred to by the tvbuff, offset, and length as a EUC-KR encoded string,
3458 * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3459 * converted having substituted REPLACEMENT CHARACTER according to the
3460 * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3461 * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3462 */
3463static uint8_t *
3464tvb_get_euc_kr_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3465{
3466 const uint8_t *ptr;
3467
3468 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3469 return get_euc_kr_string(scope, ptr, length);
3470}
3471
3472static uint8_t *
3473tvb_get_t61_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3474{
3475 const uint8_t *ptr;
3476
3477 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3478 return get_t61_string(scope, ptr, length);
3479}
3480
3481/*
3482 * Encoding tables for BCD strings.
3483 */
3484static const dgt_set_t Dgt0_9_bcd = {
3485 {
3486 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3487 '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?'
3488 }
3489};
3490
3491static const dgt_set_t Dgt_keypad_abc_tbcd = {
3492 {
3493 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3494 '0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'
3495 }
3496};
3497
3498static const dgt_set_t Dgt_ansi_tbcd = {
3499 {
3500 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3501 '0','1','2','3','4','5','6','7','8','9','?','B','C','*','#','?'
3502 }
3503};
3504
3505static const dgt_set_t Dgt_dect_standard_4bits_tbcd = {
3506 {
3507 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3508 '0','1','2','3','4','5','6','7','8','9','?',' ','?','?','?','?'
3509 }
3510};
3511
3512static uint8_t *
3513tvb_get_apn_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3514 unsigned length)
3515{
3516 wmem_strbuf_t *str;
3517
3518 /*
3519 * This is a domain name.
3520 *
3521 * 3GPP TS 23.003, section 19.4.2 "Fully Qualified Domain Names
3522 * (FQDNs)", subsection 19.4.2.1 "General", says:
3523 *
3524 * The encoding of any identifier used as part of a Fully
3525 * Qualified Domain Name (FQDN) shall follow the Name Syntax
3526 * defined in IETF RFC 2181 [18], IETF RFC 1035 [19] and
3527 * IETF RFC 1123 [20]. An FQDN consists of one or more
3528 * labels. Each label is coded as a one octet length field
3529 * followed by that number of octets coded as 8 bit ASCII
3530 * characters.
3531 *
3532 * so this does not appear to use full-blown DNS compression -
3533 * the upper 2 bits of the length don't indicate that it's a
3534 * pointer or an extended label (RFC 2673).
3535 */
3536 str = wmem_strbuf_new_sized(scope, length + 1);
3537 if (length > 0) {
3538 const uint8_t *ptr;
3539
3540 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3541
3542 for (;;) {
3543 unsigned label_len;
3544
3545 /*
3546 * Process this label.
3547 */
3548 label_len = *ptr;
3549 ptr++;
3550 length--;
3551
3552 while (label_len != 0) {
3553 uint8_t ch;
3554
3555 if (length == 0)
3556 goto end;
3557
3558 ch = *ptr;
3559 if (ch < 0x80)
3560 wmem_strbuf_append_c(str, ch);
3561 else
3562 wmem_strbuf_append_unichar_repl(str)wmem_strbuf_append_unichar(str, 0x00FFFD);
3563 ptr++;
3564 label_len--;
3565 length--;
3566 }
3567
3568 if (length == 0)
3569 goto end;
3570
3571 wmem_strbuf_append_c(str, '.');
3572 }
3573 }
3574
3575end:
3576 return (uint8_t *) wmem_strbuf_finalize(str);
3577}
3578
3579static uint8_t *
3580tvb_get_dect_standard_8bits_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3581{
3582 const uint8_t *ptr;
3583
3584 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3585 return get_dect_standard_8bits_string(scope, ptr, length);
3586}
3587
3588/*
3589 * Given a tvbuff, an offset, a length, and an encoding, allocate a
3590 * buffer big enough to hold a non-null-terminated string of that length
3591 * at that offset, plus a trailing '\0', copy into the buffer the
3592 * string as converted from the appropriate encoding to UTF-8, and
3593 * return a pointer to the string.
3594 */
3595uint8_t *
3596tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3597 const unsigned length, const unsigned encoding)
3598{
3599 uint8_t *strptr;
3600 bool_Bool odd, skip_first;
3601
3602 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3602, "tvb && tvb->initialized"
))))
;
3603
3604 switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
3605
3606 case ENC_ASCII0x00000000:
3607 default:
3608 /*
3609 * For now, we treat bogus values as meaning
3610 * "ASCII" rather than reporting an error,
3611 * for the benefit of old dissectors written
3612 * when the last argument to proto_tree_add_item()
3613 * was a bool for the byte order, not an
3614 * encoding value, and passed non-zero values
3615 * other than true to mean "little-endian".
3616 */
3617 strptr = tvb_get_ascii_string(scope, tvb, offset, length);
3618 break;
3619
3620 case ENC_UTF_80x00000002:
3621 strptr = tvb_get_utf_8_string(scope, tvb, offset, length);
3622 break;
3623
3624 case ENC_UTF_160x00000004:
3625 strptr = tvb_get_utf_16_string(scope, tvb, offset, length,
3626 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3627 break;
3628
3629 case ENC_UCS_20x00000006:
3630 strptr = tvb_get_ucs_2_string(scope, tvb, offset, length,
3631 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3632 break;
3633
3634 case ENC_UCS_40x00000008:
3635 strptr = (uint8_t*)tvb_get_ucs_4_string(scope, tvb, offset, length,
3636 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3637 break;
3638
3639 case ENC_ISO_8859_10x0000000A:
3640 /*
3641 * ISO 8859-1 printable code point values are equal
3642 * to the equivalent Unicode code point value, so
3643 * no translation table is needed.
3644 */
3645 strptr = tvb_get_string_8859_1(scope, tvb, offset, length);
3646 break;
3647
3648 case ENC_ISO_8859_20x0000000C:
3649 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_2);
3650 break;
3651
3652 case ENC_ISO_8859_30x0000000E:
3653 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_3);
3654 break;
3655
3656 case ENC_ISO_8859_40x00000010:
3657 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_4);
3658 break;
3659
3660 case ENC_ISO_8859_50x00000012:
3661 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_5);
3662 break;
3663
3664 case ENC_ISO_8859_60x00000014:
3665 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_6);
3666 break;
3667
3668 case ENC_ISO_8859_70x00000016:
3669 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_7);
3670 break;
3671
3672 case ENC_ISO_8859_80x00000018:
3673 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_8);
3674 break;
3675
3676 case ENC_ISO_8859_90x0000001A:
3677 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_9);
3678 break;
3679
3680 case ENC_ISO_8859_100x0000001C:
3681 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_10);
3682 break;
3683
3684 case ENC_ISO_8859_110x0000001E:
3685 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_11);
3686 break;
3687
3688 case ENC_ISO_8859_130x00000022:
3689 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_13);
3690 break;
3691
3692 case ENC_ISO_8859_140x00000024:
3693 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_14);
3694 break;
3695
3696 case ENC_ISO_8859_150x00000026:
3697 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_15);
3698 break;
3699
3700 case ENC_ISO_8859_160x00000028:
3701 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_16);
3702 break;
3703
3704 case ENC_WINDOWS_12500x0000002A:
3705 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1250);
3706 break;
3707
3708 case ENC_WINDOWS_12510x0000003C:
3709 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1251);
3710 break;
3711
3712 case ENC_WINDOWS_12520x0000003A:
3713 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1252);
3714 break;
3715
3716 case ENC_MAC_ROMAN0x00000030:
3717 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_mac_roman);
3718 break;
3719
3720 case ENC_CP4370x00000032:
3721 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp437);
3722 break;
3723
3724 case ENC_CP8550x0000003E:
3725 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp855);
3726 break;
3727
3728 case ENC_CP8660x00000040:
3729 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp866);
3730 break;
3731
3732 case ENC_ISO_646_BASIC0x00000042:
3733 strptr = tvb_get_iso_646_string(scope, tvb, offset, length, charset_table_iso_646_basic);
3734 break;
3735
3736 case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
3737 {
3738 unsigned bit_offset = offset << 3;
3739 unsigned no_of_chars = (length << 3) / 7;
3740 strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_packed(scope, tvb, bit_offset, no_of_chars);
3741 }
3742 break;
3743
3744 case ENC_ASCII_7BITS0x00000034:
3745 {
3746 unsigned bit_offset = offset << 3;
3747 unsigned no_of_chars = (length << 3) / 7;
3748 strptr = (uint8_t*)tvb_get_ascii_7bits_string(scope, tvb, bit_offset, no_of_chars);
3749 }
3750 break;
3751
3752 case ENC_EBCDIC0x0000002E:
3753 /*
3754 * "Common" EBCDIC, covering all characters with the
3755 * same code point in all Roman-alphabet EBCDIC code
3756 * pages.
3757 */
3758 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic);
3759 break;
3760
3761 case ENC_EBCDIC_CP0370x00000038:
3762 /*
3763 * EBCDIC code page 037.
3764 */
3765 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp037);
3766 break;
3767
3768 case ENC_EBCDIC_CP5000x00000060:
3769 /*
3770 * EBCDIC code page 500.
3771 */
3772 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp500);
3773 break;
3774
3775 case ENC_T610x00000036:
3776 strptr = tvb_get_t61_string(scope, tvb, offset, length);
3777 break;
3778
3779 case ENC_BCD_DIGITS_0_90x00000044:
3780 /*
3781 * Packed BCD, with digits 0-9.
3782 */
3783 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3784 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3785 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt0_9_bcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3786 break;
3787
3788 case ENC_KEYPAD_ABC_TBCD0x00000046:
3789 /*
3790 * Keypad-with-a/b/c "telephony BCD" - packed BCD, with
3791 * digits 0-9 and symbols *, #, a, b, and c.
3792 */
3793 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3794 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3795 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_keypad_abc_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3796 break;
3797
3798 case ENC_KEYPAD_BC_TBCD0x00000048:
3799 /*
3800 * Keypad-with-B/C "telephony BCD" - packed BCD, with
3801 * digits 0-9 and symbols B, C, *, and #.
3802 */
3803 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3804 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3805 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_ansi_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3806 break;
3807
3808 case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
3809 strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_unpacked(scope, tvb, offset, length);
3810 break;
3811
3812 case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
3813 strptr = (uint8_t*)tvb_get_etsi_ts_102_221_annex_a_string(scope, tvb, offset, length);
3814 break;
3815
3816 case ENC_GB180300x00000050:
3817 strptr = tvb_get_gb18030_string(scope, tvb, offset, length);
3818 break;
3819
3820 case ENC_EUC_KR0x00000052:
3821 strptr = tvb_get_euc_kr_string(scope, tvb, offset, length);
3822 break;
3823
3824 case ENC_APN_STR0x00000054:
3825 strptr = tvb_get_apn_string(scope, tvb, offset, length);
3826 break;
3827
3828 case ENC_DECT_STANDARD_8BITS0x00000056:
3829 strptr = tvb_get_dect_standard_8bits_string(scope, tvb, offset, length);
3830 break;
3831
3832 case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
3833 /*
3834 * DECT standard 4bits "telephony BCD" - packed BCD, with
3835 * digits 0-9 and symbol SPACE for 0xb.
3836 */
3837 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3838 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3839 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_dect_standard_4bits_tbcd, skip_first, odd, false0);
3840 break;
3841 }
3842 return strptr;
3843}
3844
3845/*
3846 * This is like tvb_get_string_enc(), except that it handles null-padded
3847 * strings.
3848 *
3849 * Currently, string values are stored as UTF-8 null-terminated strings,
3850 * so nothing needs to be done differently for null-padded strings; we
3851 * could save a little memory by not storing the null padding.
3852 *
3853 * If we ever store string values differently, in a fashion that doesn't
3854 * involve null termination, that might change.
3855 */
3856uint8_t *
3857tvb_get_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3858 const unsigned length, const unsigned encoding)
3859{
3860 return tvb_get_string_enc(scope, tvb, offset, length, encoding);
3861}
3862
3863/*
3864 * These routines are like the above routines, except that they handle
3865 * null-terminated strings. They find the length of that string (and
3866 * throw an exception if the tvbuff ends before we find the null), and
3867 * also return through a pointer the length of the string, in bytes,
3868 * including the terminating null (the terminating null being 2 bytes
3869 * for UCS-2 and UTF-16, 4 bytes for UCS-4, and 1 byte for other
3870 * encodings).
3871 */
3872static uint8_t *
3873tvb_get_ascii_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3874{
3875 unsigned size;
3876 const uint8_t *ptr;
3877
3878 size = tvb_strsize(tvb, offset);
3879 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3880 if (lengthp)
3881 *lengthp = size;
3882 return get_ascii_string(scope, ptr, size);
3883}
3884
3885static uint8_t *
3886tvb_get_iso_646_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
3887{
3888 unsigned size;
3889 const uint8_t *ptr;
3890
3891 size = tvb_strsize(tvb, offset);
3892 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3893 if (lengthp)
3894 *lengthp = size;
3895 return get_iso_646_string(scope, ptr, size, table);
3896}
3897
3898static uint8_t *
3899tvb_get_utf_8_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3900{
3901 unsigned size;
3902 const uint8_t *ptr;
3903
3904 size = tvb_strsize(tvb, offset);
3905 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3906 if (lengthp)
3907 *lengthp = size;
3908 return get_utf_8_string(scope, ptr, size);
3909}
3910
3911static uint8_t *
3912tvb_get_stringz_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3913{
3914 unsigned size;
3915 const uint8_t *ptr;
3916
3917 size = tvb_strsize(tvb, offset);
3918 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3919 if (lengthp)
3920 *lengthp = size;
3921 return get_8859_1_string(scope, ptr, size);
3922}
3923
3924static uint8_t *
3925tvb_get_stringz_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
3926{
3927 unsigned size;
3928 const uint8_t *ptr;
3929
3930 size = tvb_strsize(tvb, offset);
3931 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3932 if (lengthp)
3933 *lengthp = size;
3934 return get_unichar2_string(scope, ptr, size, table);
3935}
3936
3937/*
3938 * Given a tvbuff and an offset, with the offset assumed to refer to
3939 * a null-terminated string, find the length of that string (and throw
3940 * an exception if the tvbuff ends before we find the null), ensure that
3941 * the TVB is flat, and return a pointer to the string (in the TVB).
3942 * Also return the length of the string (including the terminating null)
3943 * through a pointer.
3944 *
3945 * As long as we aren't using composite TVBs, this saves the cycles used
3946 * (often unnecessarily) in allocating a buffer and copying the string into
3947 * it. OTOH, the string returned isn't valid UTF-8, so it shouldn't be
3948 * added to the tree, the columns, etc., just used with various other
3949 * functions that operate on strings that don't have a tvb_ equivalent.
3950 * That's hard to enforce, which is why this is deprecated.
3951 */
3952const uint8_t *
3953tvb_get_const_stringz(tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3954{
3955 unsigned size;
3956 const uint8_t *strptr;
3957
3958 size = tvb_strsize(tvb, offset);
3959 strptr = ensure_contiguous_unsigned(tvb, offset, size);
3960 if (lengthp)
3961 *lengthp = size;
3962 return strptr;
3963}
3964
3965static char *
3966tvb_get_ucs_2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3967{
3968 unsigned size; /* Number of bytes in string */
3969 const uint8_t *ptr;
3970
3971 size = tvb_unicode_strsize(tvb, offset);
3972 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3973 if (lengthp)
3974 *lengthp = size;
3975 return (char*)get_ucs_2_string(scope, ptr, size, encoding);
3976}
3977
3978static char *
3979tvb_get_utf_16_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3980{
3981 unsigned size;
3982 const uint8_t *ptr;
3983
3984 size = tvb_unicode_strsize(tvb, offset);
3985 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3986 if (lengthp)
3987 *lengthp = size;
3988 return (char*)get_utf_16_string(scope, ptr, size, encoding);
3989}
3990
3991static char *
3992tvb_get_ucs_4_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3993{
3994 unsigned end_offset, size;
3995 gunichar uchar;
3996 const uint8_t *ptr;
3997
3998 end_offset = offset;
3999 do {
4000 /* Endianness doesn't matter when looking for null */
4001 uchar = tvb_get_ntohl(tvb, end_offset);
4002 /* Make sure we don't overflow */
4003 if (ckd_add(&end_offset, end_offset, 4)__builtin_add_overflow((end_offset), (4), (&end_offset))) {
4004 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
4005 }
4006 } while(uchar != 0);
4007 size = end_offset - offset;
4008
4009 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4010 if (lengthp)
4011 *lengthp = size;
4012 return (char*)get_ucs_4_string(scope, ptr, size, encoding);
4013}
4014
4015static uint8_t *
4016tvb_get_nonascii_unichar2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[256])
4017{
4018 unsigned size;
4019 const uint8_t *ptr;
4020
4021 size = tvb_strsize(tvb, offset);
4022 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4023 if (lengthp)
4024 *lengthp = size;
4025 return get_nonascii_unichar2_string(scope, ptr, size, table);
4026}
4027
4028static uint8_t *
4029tvb_get_t61_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4030{
4031 unsigned size;
4032 const uint8_t *ptr;
4033
4034 size = tvb_strsize(tvb, offset);
4035 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4036 if (lengthp)
4037 *lengthp = size;
4038 return get_t61_string(scope, ptr, size);
4039}
4040
4041static uint8_t *
4042tvb_get_gb18030_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4043{
4044 unsigned size;
4045 const uint8_t *ptr;
4046
4047 size = tvb_strsize(tvb, offset);
4048 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4049 if (lengthp)
4050 *lengthp = size;
4051 return get_gb18030_string(scope, ptr, size);
4052}
4053
4054static uint8_t *
4055tvb_get_euc_kr_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4056{
4057 unsigned size;
4058 const uint8_t *ptr;
4059
4060 size = tvb_strsize(tvb, offset);
4061 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4062 if (lengthp)
4063 *lengthp = size;
4064 return get_euc_kr_string(scope, ptr, size);
4065}
4066
4067static uint8_t *
4068tvb_get_dect_standard_8bits_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4069{
4070 unsigned size;
4071 const uint8_t *ptr;
4072
4073 size = tvb_strsize(tvb, offset);
4074 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4075 if (lengthp)
4076 *lengthp = size;
4077 return get_dect_standard_8bits_string(scope, ptr, size);
4078}
4079
4080uint8_t *
4081tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4082{
4083 uint8_t *strptr;
4084
4085 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4085, "tvb && tvb->initialized"
))))
;
4086
4087 switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
4088
4089 case ENC_ASCII0x00000000:
4090 default:
4091 /*
4092 * For now, we treat bogus values as meaning
4093 * "ASCII" rather than reporting an error,
4094 * for the benefit of old dissectors written
4095 * when the last argument to proto_tree_add_item()
4096 * was a bool for the byte order, not an
4097 * encoding value, and passed non-zero values
4098 * other than true to mean "little-endian".
4099 */
4100 strptr = tvb_get_ascii_stringz(scope, tvb, offset, lengthp);
4101 break;
4102
4103 case ENC_UTF_80x00000002:
4104 strptr = tvb_get_utf_8_stringz(scope, tvb, offset, lengthp);
4105 break;
4106
4107 case ENC_UTF_160x00000004:
4108 strptr = (uint8_t*)tvb_get_utf_16_stringz(scope, tvb, offset, lengthp,
4109 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4110 break;
4111
4112 case ENC_UCS_20x00000006:
4113 strptr = (uint8_t*)tvb_get_ucs_2_stringz(scope, tvb, offset, lengthp,
4114 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4115 break;
4116
4117 case ENC_UCS_40x00000008:
4118 strptr = (uint8_t*)tvb_get_ucs_4_stringz(scope, tvb, offset, lengthp,
4119 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4120 break;
4121
4122 case ENC_ISO_8859_10x0000000A:
4123 /*
4124 * ISO 8859-1 printable code point values are equal
4125 * to the equivalent Unicode code point value, so
4126 * no translation table is needed.
4127 */
4128 strptr = tvb_get_stringz_8859_1(scope, tvb, offset, lengthp);
4129 break;
4130
4131 case ENC_ISO_8859_20x0000000C:
4132 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_2);
4133 break;
4134
4135 case ENC_ISO_8859_30x0000000E:
4136 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_3);
4137 break;
4138
4139 case ENC_ISO_8859_40x00000010:
4140 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_4);
4141 break;
4142
4143 case ENC_ISO_8859_50x00000012:
4144 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_5);
4145 break;
4146
4147 case ENC_ISO_8859_60x00000014:
4148 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_6);
4149 break;
4150
4151 case ENC_ISO_8859_70x00000016:
4152 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_7);
4153 break;
4154
4155 case ENC_ISO_8859_80x00000018:
4156 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_8);
4157 break;
4158
4159 case ENC_ISO_8859_90x0000001A:
4160 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_9);
4161 break;
4162
4163 case ENC_ISO_8859_100x0000001C:
4164 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_10);
4165 break;
4166
4167 case ENC_ISO_8859_110x0000001E:
4168 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_11);
4169 break;
4170
4171 case ENC_ISO_8859_130x00000022:
4172 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_13);
4173 break;
4174
4175 case ENC_ISO_8859_140x00000024:
4176 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_14);
4177 break;
4178
4179 case ENC_ISO_8859_150x00000026:
4180 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_15);
4181 break;
4182
4183 case ENC_ISO_8859_160x00000028:
4184 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_16);
4185 break;
4186
4187 case ENC_WINDOWS_12500x0000002A:
4188 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1250);
4189 break;
4190
4191 case ENC_WINDOWS_12510x0000003C:
4192 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1251);
4193 break;
4194
4195 case ENC_WINDOWS_12520x0000003A:
4196 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1252);
4197 break;
4198
4199 case ENC_MAC_ROMAN0x00000030:
4200 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_mac_roman);
4201 break;
4202
4203 case ENC_CP4370x00000032:
4204 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp437);
4205 break;
4206
4207 case ENC_CP8550x0000003E:
4208 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp855);
4209 break;
4210
4211 case ENC_CP8660x00000040:
4212 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp866);
4213 break;
4214
4215 case ENC_ISO_646_BASIC0x00000042:
4216 strptr = tvb_get_iso_646_stringz(scope, tvb, offset, lengthp, charset_table_iso_646_basic);
4217 break;
4218
4219 case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
4220 case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
4221 case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
4222 REPORT_DISSECTOR_BUG("TS 23.038 7bits has no null character and doesn't support null-terminated strings")proto_report_dissector_bug("TS 23.038 7bits has no null character and doesn't support null-terminated strings"
)
;
4223 break;
4224
4225 case ENC_ASCII_7BITS0x00000034:
4226 REPORT_DISSECTOR_BUG("tvb_get_stringz_enc function with ENC_ASCII_7BITS not implemented yet")proto_report_dissector_bug("tvb_get_stringz_enc function with ENC_ASCII_7BITS not implemented yet"
)
;
4227 break;
4228
4229 case ENC_EBCDIC0x0000002E:
4230 /*
4231 * "Common" EBCDIC, covering all characters with the
4232 * same code point in all Roman-alphabet EBCDIC code
4233 * pages.
4234 */
4235 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic);
4236 break;
4237
4238 case ENC_EBCDIC_CP0370x00000038:
4239 /*
4240 * EBCDIC code page 037.
4241 */
4242 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp037);
4243 break;
4244
4245 case ENC_EBCDIC_CP5000x00000060:
4246 /*
4247 * EBCDIC code page 500.
4248 */
4249 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp500);
4250 break;
4251
4252 case ENC_T610x00000036:
4253 strptr = tvb_get_t61_stringz(scope, tvb, offset, lengthp);
4254 break;
4255
4256 case ENC_GB180300x00000050:
4257 strptr = tvb_get_gb18030_stringz(scope, tvb, offset, lengthp);
4258 break;
4259
4260 case ENC_EUC_KR0x00000052:
4261 strptr = tvb_get_euc_kr_stringz(scope, tvb, offset, lengthp);
4262 break;
4263
4264 case ENC_DECT_STANDARD_8BITS0x00000056:
4265 strptr = tvb_get_dect_standard_8bits_stringz(scope, tvb, offset, lengthp);
4266 break;
4267 }
4268
4269 return strptr;
4270}
4271
4272/* Looks for a stringz (NUL-terminated string) in tvbuff and copies
4273 * no more than bufsize number of bytes, including terminating NUL, to buffer.
4274 * Returns length of string (not including terminating NUL).
4275 * In this way, it acts like snprintf().
4276 *
4277 * bufsize MUST be greater than 0.
4278 *
4279 * This function does not otherwise throw an exception for running out of room
4280 * in the buffer or running out of remaining bytes in the tvbuffer. It will
4281 * copy as many bytes to the buffer as possible (the lesser of bufsize - 1
4282 * and the number of remaining captured bytes) and then NUL terminate the
4283 * string.
4284 *
4285 * *bytes_copied will contain the number of bytes actually copied,
4286 * including the terminating-NUL if present in the frame, but not
4287 * if it was supplied by the function instead of copied from packet data.
4288 * [Not currently used, but could be used to determine how much to advance
4289 * the offset.]
4290 */
4291static unsigned
4292_tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const unsigned offset, const unsigned bufsize, uint8_t* buffer, unsigned *bytes_copied)
4293{
4294 int stringlen;
4295 unsigned limit;
4296 unsigned len = 0;
4297
4298 /* Only read to end of tvbuff, w/o throwing exception. */
4299 validate_offset_and_remaining(tvb, offset, &len);
4300
4301 /* There must at least be room for the terminating NUL. */
4302 DISSECTOR_ASSERT(bufsize != 0)((void) ((bufsize != 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4302, "bufsize != 0"
))))
;
4303
4304 /* If there's no room for anything else, just return the NUL. */
4305 if (bufsize == 1) {
4306 buffer[0] = 0;
4307 if (len && tvb_get_uint8(tvb, offset) == 0) {
4308 *bytes_copied = 1;
4309 } else {
4310 *bytes_copied = 0;
4311 }
4312 return 0;
4313 }
4314
4315 /* check_offset_length() won't throw an exception if we're
4316 * looking at the byte immediately after the end of the tvbuff. */
4317 if (len == 0) {
4318 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
4319 }
4320
4321 if (len < bufsize) {
4322 limit = len;
4323 }
4324 else {
4325 limit = bufsize - 1;
4326 }
4327
4328 stringlen = tvb_strnlen(tvb, offset, limit);
4329 /* If NUL wasn't found, copy the data up to the limit and terminate */
4330 if (stringlen == -1) {
4331 tvb_memcpy(tvb, buffer, offset, limit);
4332 buffer[limit] = 0;
4333 *bytes_copied = limit;
4334 return limit;
4335 }
4336
4337 /* Copy the string to buffer */
4338 tvb_memcpy(tvb, buffer, offset, stringlen + 1);
4339 *bytes_copied = stringlen + 1;
4340 return (unsigned)stringlen;
4341}
4342
4343unsigned
4344tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const unsigned offset, const unsigned bufsize, uint8_t* buffer)
4345{
4346 unsigned bytes_copied;
4347
4348 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4348, "tvb && tvb->initialized"
))))
;
4349
4350 return _tvb_get_raw_bytes_as_stringz(tvb, offset, bufsize, buffer, &bytes_copied);
4351}
4352
4353/*
4354 * Given a tvbuff, an offset into the tvbuff, a buffer, and a buffer size,
4355 * extract as many raw bytes from the tvbuff, starting at the offset,
4356 * as 1) are available in the tvbuff and 2) will fit in the buffer, leaving
4357 * room for a terminating NUL.
4358 */
4359unsigned
4360tvb_get_raw_bytes_as_string(tvbuff_t *tvb, const unsigned offset, char *buffer, size_t bufsize)
4361{
4362 unsigned len = 0;
4363
4364 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4364, "tvb && tvb->initialized"
))))
;
4365
4366 /* There must be room for the string and the terminating NUL. */
4367 DISSECTOR_ASSERT(bufsize > 0)((void) ((bufsize > 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4367, "bufsize > 0"
))))
;
4368
4369 /* bufsize is size_t, but tvbuffers only have up to unsigned bytes */
4370 DISSECTOR_ASSERT(bufsize - 1 < UINT_MAX)((void) ((bufsize - 1 < (2147483647 *2U +1U)) ? (void)0 : (
proto_report_dissector_bug("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c"
, 4370, "bufsize - 1 < (2147483647 *2U +1U)"))))
;
4371
4372 len = _tvb_captured_length_remaining(tvb, offset);
4373 if (len == 0) {
4374 buffer[0] = '\0';
4375 return 0;
4376 }
4377 if (len > (bufsize - 1))
4378 len = (unsigned)(bufsize - 1);
4379
4380 /* Copy the string to buffer */
4381 tvb_memcpy(tvb, buffer, offset, len);
4382 buffer[len] = '\0';
4383 return len;
4384}
4385
4386bool_Bool
4387tvb_ascii_isprint(tvbuff_t *tvb, const int offset, const int length)
4388{
4389 const uint8_t* buf = tvb_get_ptr(tvb, offset, length);
4390 unsigned abs_offset, abs_length = length;
4391
4392 if (length == -1) {
4393 /* tvb_get_ptr has already checked for exceptions. */
4394 compute_offset_and_remaining(tvb, offset, &abs_offset, &abs_length);
4395 }
4396 for (unsigned i = 0; i < abs_length; i++, buf++)
4397 if (!g_ascii_isprint(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_PRINT) != 0))
4398 return false0;
4399
4400 return true1;
4401}
4402
4403bool_Bool
4404tvb_utf_8_isprint(tvbuff_t *tvb, const int offset, const int length)
4405{
4406 const uint8_t* buf = tvb_get_ptr(tvb, offset, length);
4407 unsigned abs_offset, abs_length = length;
4408
4409 if (length == -1) {
4410 /* tvb_get_ptr has already checked for exceptions. */
4411 compute_offset_and_remaining(tvb, offset, &abs_offset, &abs_length);
4412 }
4413
4414 return isprint_utf8_string((const char*)buf, abs_length);
4415}
4416
4417bool_Bool
4418tvb_ascii_isdigit(tvbuff_t *tvb, const int offset, const int length)
4419{
4420 const uint8_t* buf = tvb_get_ptr(tvb, offset, length);
4421 unsigned abs_offset, abs_length = length;
4422
4423 if (length == -1) {
4424 /* tvb_get_ptr has already checked for exceptions. */
4425 compute_offset_and_remaining(tvb, offset, &abs_offset, &abs_length);
4426 }
4427 for (unsigned i = 0; i < abs_length; i++, buf++)
4428 if (!g_ascii_isdigit(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_DIGIT) != 0))
4429 return false0;
4430
4431 return true1;
4432}
4433
4434static ws_mempbrk_pattern pbrk_crlf;
4435/*
4436 * Given a tvbuff, an offset into the tvbuff, and a length that starts
4437 * at that offset (which may be -1 for "all the way to the end of the
4438 * tvbuff"), find the end of the (putative) line that starts at the
4439 * specified offset in the tvbuff, going no further than the specified
4440 * length.
4441 *
4442 * Return the length of the line (not counting the line terminator at
4443 * the end), or, if we don't find a line terminator:
4444 *
4445 * if "desegment" is true, return -1;
4446 *
4447 * if "desegment" is false, return the amount of data remaining in
4448 * the buffer.
4449 *
4450 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4451 * character past the line terminator, or past the end of the buffer if
4452 * we don't find a line terminator. (It's not set if we return -1.)
4453 */
4454int
4455tvb_find_line_end(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset, const bool_Bool desegment)
4456{
4457 int eob_offset;
4458 int eol_offset;
4459 int linelen;
4460 unsigned char found_needle = 0;
4461 static bool_Bool compiled = false0;
4462
4463 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4463, "tvb && tvb->initialized"
))))
;
4464
4465 if (len == -1) {
4466 len = _tvb_captured_length_remaining(tvb, offset);
4467 /* if offset is past the end of the tvbuff, len is now 0 */
4468 }
4469
4470 eob_offset = offset + len;
4471
4472 if (!compiled) {
4473 ws_mempbrk_compile(&pbrk_crlf, "\r\n");
4474 compiled = true1;
4475 }
4476
4477 /*
4478 * Look either for a CR or an LF.
4479 */
4480 eol_offset = tvb_ws_mempbrk_pattern_uint8(tvb, offset, len, &pbrk_crlf, &found_needle);
4481 if (eol_offset == -1) {
4482 /*
4483 * No CR or LF - line is presumably continued in next packet.
4484 */
4485 if (desegment) {
4486 /*
4487 * Tell our caller we saw no EOL, so they can
4488 * try to desegment and get the entire line
4489 * into one tvbuff.
4490 */
4491 return -1;
4492 } else {
4493 /*
4494 * Pretend the line runs to the end of the tvbuff.
4495 */
4496 linelen = eob_offset - offset;
4497 if (next_offset)
4498 *next_offset = eob_offset;
4499 }
4500 } else {
4501 /*
4502 * Find the number of bytes between the starting offset
4503 * and the CR or LF.
4504 */
4505 linelen = eol_offset - offset;
4506
4507 /*
4508 * Is it a CR?
4509 */
4510 if (found_needle == '\r') {
4511 /*
4512 * Yes - is it followed by an LF?
4513 */
4514 if (eol_offset + 1 >= eob_offset) {
4515 /*
4516 * Dunno - the next byte isn't in this
4517 * tvbuff.
4518 */
4519 if (desegment) {
4520 /*
4521 * We'll return -1, although that
4522 * runs the risk that if the line
4523 * really *is* terminated with a CR,
4524 * we won't properly dissect this
4525 * tvbuff.
4526 *
4527 * It's probably more likely that
4528 * the line ends with CR-LF than
4529 * that it ends with CR by itself.
4530 */
4531 return -1;
4532 }
4533 } else {
4534 /*
4535 * Well, we can at least look at the next
4536 * byte.
4537 */
4538 if (tvb_get_uint8(tvb, eol_offset + 1) == '\n') {
4539 /*
4540 * It's an LF; skip over the CR.
4541 */
4542 eol_offset++;
4543 }
4544 }
4545 }
4546
4547 /*
4548 * Return the offset of the character after the last
4549 * character in the line, skipping over the last character
4550 * in the line terminator.
4551 */
4552 if (next_offset)
4553 *next_offset = eol_offset + 1;
4554 }
4555 return linelen;
4556}
4557
4558static ws_mempbrk_pattern pbrk_crlf_dquote;
4559/*
4560 * Given a tvbuff, an offset into the tvbuff, and a length that starts
4561 * at that offset (which may be -1 for "all the way to the end of the
4562 * tvbuff"), find the end of the (putative) line that starts at the
4563 * specified offset in the tvbuff, going no further than the specified
4564 * length.
4565 *
4566 * However, treat quoted strings inside the buffer specially - don't
4567 * treat newlines in quoted strings as line terminators.
4568 *
4569 * Return the length of the line (not counting the line terminator at
4570 * the end), or the amount of data remaining in the buffer if we don't
4571 * find a line terminator.
4572 *
4573 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4574 * character past the line terminator, or past the end of the buffer if
4575 * we don't find a line terminator.
4576 */
4577int
4578tvb_find_line_end_unquoted(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset)
4579{
4580 int cur_offset, char_offset;
4581 bool_Bool is_quoted;
4582 unsigned char c = 0;
4583 int eob_offset;
4584 int linelen;
4585 static bool_Bool compiled = false0;
4586
4587 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4587, "tvb && tvb->initialized"
))))
;
4588
4589 if (len == -1)
4590 len = _tvb_captured_length_remaining(tvb, offset);
4591
4592 if (!compiled) {
4593 ws_mempbrk_compile(&pbrk_crlf_dquote, "\r\n\"");
4594 compiled = true1;
4595 }
4596
4597 eob_offset = offset + len;
4598
4599 cur_offset = offset;
4600 is_quoted = false0;
4601 for (;;) {
4602 /*
4603 * Is this part of the string quoted?
4604 */
4605 if (is_quoted) {
4606 /*
4607 * Yes - look only for the terminating quote.
4608 */
4609 char_offset = tvb_find_uint8(tvb, cur_offset, len,
4610 '"');
4611 } else {
4612 /*
4613 * Look either for a CR, an LF, or a '"'.
4614 */
4615 char_offset = tvb_ws_mempbrk_pattern_uint8(tvb, cur_offset, len, &pbrk_crlf_dquote, &c);
4616 }
4617 if (char_offset == -1) {
4618 /*
4619 * Not found - line is presumably continued in
4620 * next packet.
4621 * We pretend the line runs to the end of the tvbuff.
4622 */
4623 linelen = eob_offset - offset;
4624 if (next_offset)
4625 *next_offset = eob_offset;
4626 break;
4627 }
4628
4629 if (is_quoted) {
4630 /*
4631 * We're processing a quoted string.
4632 * We only looked for ", so we know it's a ";
4633 * as we're processing a quoted string, it's a
4634 * closing quote.
4635 */
4636 is_quoted = false0;
4637 } else {
4638 /*
4639 * OK, what is it?
4640 */
4641 if (c == '"') {
4642 /*
4643 * Un-quoted "; it begins a quoted
4644 * string.
4645 */
4646 is_quoted = true1;
4647 } else {
4648 /*
4649 * It's a CR or LF; we've found a line
4650 * terminator.
4651 *
4652 * Find the number of bytes between the
4653 * starting offset and the CR or LF.
4654 */
4655 linelen = char_offset - offset;
4656
4657 /*
4658 * Is it a CR?
4659 */
4660 if (c == '\r') {
4661 /*
4662 * Yes; is it followed by an LF?
4663 */
4664 if (char_offset + 1 < eob_offset &&
4665 tvb_get_uint8(tvb, char_offset + 1)
4666 == '\n') {
4667 /*
4668 * Yes; skip over the CR.
4669 */
4670 char_offset++;
4671 }
4672 }
4673
4674 /*
4675 * Return the offset of the character after
4676 * the last character in the line, skipping
4677 * over the last character in the line
4678 * terminator, and quit.
4679 */
4680 if (next_offset)
4681 *next_offset = char_offset + 1;
4682 break;
4683 }
4684 }
4685
4686 /*
4687 * Step past the character we found.
4688 */
4689 cur_offset = char_offset + 1;
4690 if (cur_offset >= eob_offset) {
4691 /*
4692 * The character we found was the last character
4693 * in the tvbuff - line is presumably continued in
4694 * next packet.
4695 * We pretend the line runs to the end of the tvbuff.
4696 */
4697 linelen = eob_offset - offset;
4698 if (next_offset)
4699 *next_offset = eob_offset;
4700 break;
4701 }
4702 }
4703 return linelen;
4704}
4705
4706/*
4707 * Copied from the mgcp dissector. (This function should be moved to /epan )
4708 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
4709 * character following offset or offset + maxlength -1 whichever
4710 * is smaller.
4711 *
4712 * Parameters:
4713 * tvb - The tvbuff in which we are skipping whitespace.
4714 * offset - The offset in tvb from which we begin trying to skip whitespace.
4715 * maxlength - The maximum distance from offset that we may try to skip
4716 * whitespace.
4717 *
4718 * Returns: The position in tvb of the first non-whitespace
4719 * character following offset or offset + maxlength -1 whichever
4720 * is smaller.
4721 */
4722unsigned
4723tvb_skip_wsp(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
4724{
4725 unsigned counter;
4726 unsigned end, tvb_len;
4727 uint8_t tempchar;
4728
4729 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4729, "tvb && tvb->initialized"
))))
;
4730
4731 /* Get the length remaining */
4732 /*tvb_len = tvb_captured_length(tvb);*/
4733 tvb_len = tvb->length;
4734
4735 if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
4736 end = tvb_len;
4737 }
4738
4739 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
4740 /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
4741 * slightly faster but also tests for vertical tab and form feed. */
4742 for (counter = offset;
4743 counter < end &&
4744 ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
4745 tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
4746 counter++);
4747
4748 return counter;
4749}
4750
4751unsigned
4752tvb_skip_wsp_return(tvbuff_t *tvb, const unsigned offset)
4753{
4754 unsigned counter;
4755 uint8_t tempchar;
4756
4757 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4757, "tvb && tvb->initialized"
))))
;
4758
4759 /* XXX - DISSECTOR_ASSERT(offset > 0) and then subtract 1 from offset?
4760 * The way this is used the caller almost always wants to subtract one
4761 * from the offset of a non WSP separator, and they might forget to do
4762 * so and then this function return the offset past the separator. */
4763
4764 /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
4765 * slightly faster but also tests for vertical tab and form feed. */
4766 for (counter = offset; counter > 0 &&
4767 ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
4768 tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
4769 counter++;
4770
4771 return counter;
4772}
4773
4774unsigned
4775tvb_skip_uint8(tvbuff_t *tvb, unsigned offset, const unsigned maxlength, const uint8_t ch)
4776{
4777 unsigned end, tvb_len;
4778
4779 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4779, "tvb && tvb->initialized"
))))
;
4780
4781 /* Get the length remaining */
4782 /*tvb_len = tvb_captured_length(tvb);*/
4783 tvb_len = tvb->length;
4784
4785 if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
4786 end = tvb_len;
4787 }
4788
4789 while (offset < end) {
4790 uint8_t tempch = tvb_get_uint8(tvb, offset);
4791
4792 if (tempch != ch)
4793 break;
4794 offset++;
4795 }
4796
4797 return offset;
4798}
4799
4800static ws_mempbrk_pattern pbrk_whitespace;
4801
4802int tvb_get_token_len(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset, const bool_Bool desegment)
4803{
4804 int eob_offset;
4805 int eot_offset;
4806 int tokenlen;
4807 unsigned char found_needle = 0;
4808 static bool_Bool compiled = false0;
4809
4810 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4810, "tvb && tvb->initialized"
))))
;
4811
4812 if (len == -1) {
4813 len = _tvb_captured_length_remaining(tvb, offset);
4814 /* if offset is past the end of the tvbuff, len is now 0 */
4815 }
4816
4817 eob_offset = offset + len;
4818
4819 if (!compiled) {
4820 ws_mempbrk_compile(&pbrk_whitespace, " \r\n");
4821 compiled = true1;
4822 }
4823
4824 /*
4825 * Look either for a space, CR, or LF.
4826 */
4827 eot_offset = tvb_ws_mempbrk_pattern_uint8(tvb, offset, len, &pbrk_whitespace, &found_needle);
4828 if (eot_offset == -1) {
4829 /*
4830 * No space, CR or LF - token is presumably continued in next packet.
4831 */
4832 if (desegment) {
4833 /*
4834 * Tell our caller we saw no whitespace, so they can
4835 * try to desegment and get the entire line
4836 * into one tvbuff.
4837 */
4838 return -1;
4839 }
4840 else {
4841 /*
4842 * Pretend the token runs to the end of the tvbuff.
4843 */
4844 tokenlen = eob_offset - offset;
4845 if (next_offset)
4846 *next_offset = eob_offset;
4847 }
4848 }
4849 else {
4850 /*
4851 * Find the number of bytes between the starting offset
4852 * and the space, CR or LF.
4853 */
4854 tokenlen = eot_offset - offset;
4855
4856 /*
4857 * Return the offset of the character after the last
4858 * character in the line, skipping over the last character
4859 * in the line terminator.
4860 */
4861 if (next_offset)
4862 *next_offset = eot_offset + 1;
4863 }
4864 return tokenlen;
4865}
4866
4867/*
4868 * Format a bunch of data from a tvbuff as bytes, returning a pointer
4869 * to the string with the formatted data, with "punct" as a byte
4870 * separator.
4871 */
4872char *
4873tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned len, const char punct)
4874{
4875 return bytes_to_str_punct(scope, ensure_contiguous_unsigned(tvb, offset, len), len, punct)bytes_to_str_punct_maxlen(scope, ensure_contiguous_unsigned(tvb
, offset, len), len, punct, 24)
;
4876}
4877
4878/*
4879 * Given a wmem scope, a tvbuff, an offset, a length, an input digit
4880 * set, and a boolean indicator, fetch BCD-encoded digits from a
4881 * tvbuff starting from either the low or high half byte of the
4882 * first byte depending on the boolean indicator (true means "start
4883 * with the high half byte, ignoring the low half byte", and false
4884 * means "start with the low half byte and proceed to the high half
4885 * byte), formating the digits into characters according to the
4886 * input digit set, and return a pointer to a UTF-8 string, allocated
4887 * using the wmem scope. A nibble of 0xf is considered a 'filler'
4888 * and will end the conversion. Similarly if odd is set the last
4889 * high nibble will be omitted. (Note that if both skip_first and
4890 * odd are true, then both the first and last semi-octet are skipped,
4891 * i.e. an even number of nibbles are considered.)
4892 */
4893char *
4894tvb_get_bcd_string(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, int len, const dgt_set_t *dgt, bool_Bool skip_first, bool_Bool odd, bool_Bool bigendian)
4895{
4896 const uint8_t *ptr;
4897 int i = 0;
4898 char *digit_str;
4899 uint8_t octet, nibble;
4900
4901 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4901, "tvb && tvb->initialized"
))))
;
4902
4903 if (len == -1) {
4904 /*
4905 * Run to the end of the captured data.
4906 *
4907 * XXX - captured, or total?
4908 */
4909 /*length = tvb_captured_length(tvb);*/
4910 len = tvb->length;
4911 if (len < offset) {
4912 return (char *)"";
4913 }
4914 len -= offset;
4915 }
4916
4917 ptr = ensure_contiguous(tvb, offset, len);
4918
4919 /*
4920 * XXX - map illegal digits (digits that map to 0) to REPLACEMENT
4921 * CHARACTER, and have all the tables in epan/tvbuff.c use 0 rather
4922 * than '?'?
4923 */
4924 digit_str = (char *)wmem_alloc(scope, len*2 + 1);
4925
4926 while (len > 0) {
4927 octet = *ptr;
4928 if (!skip_first) {
4929 if (bigendian) {
4930 nibble = (octet >> 4) & 0x0f;
4931 } else {
4932 nibble = octet & 0x0f;
4933 }
4934 if (nibble == 0x0f) {
4935 /*
4936 * Stop digit.
4937 */
4938 break;
4939 }
4940 digit_str[i] = dgt->out[nibble];
4941 i++;
4942 }
4943 skip_first = false0;
4944
4945 /*
4946 * unpack second value in byte
4947 */
4948 if (bigendian) {
4949 nibble = octet & 0x0f;
4950 } else {
4951 nibble = octet >> 4;
4952 }
4953
4954 if (nibble == 0x0f) {
4955 /*
4956 * This is the stop digit or a filler digit. Ignore
4957 * it.
4958 */
4959 break;
4960 }
4961 if ((len == 1) && (odd == true1 )){
4962 /* Last octet, skip last high nibble in case of odd number of digits */
4963 break;
4964 }
4965 digit_str[i] = dgt->out[nibble];
4966 i++;
4967
4968 ptr++;
4969 len--;
4970 }
4971 digit_str[i] = '\0';
4972 return digit_str;
4973}
4974
4975/* XXXX Fix me - needs odd indicator added */
4976const char *
4977tvb_bcd_dig_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, const int len, const dgt_set_t *dgt, bool_Bool skip_first)
4978{
4979 if (!dgt)
4980 dgt = &Dgt0_9_bcd;
4981
4982 return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, false0);
4983}
4984
4985const char *
4986tvb_bcd_dig_to_str_be(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, const int len, const dgt_set_t *dgt, bool_Bool skip_first)
4987{
4988 if (!dgt)
4989 dgt = &Dgt0_9_bcd;
4990
4991 return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, true1);
4992}
4993
4994/*
4995 * Format a bunch of data from a tvbuff as bytes, returning a pointer
4996 * to the string with the formatted data.
4997 */
4998char *tvb_bytes_to_str(wmem_allocator_t *allocator, tvbuff_t *tvb,
4999 const int offset, const int len)
5000{
5001 DISSECTOR_ASSERT(len >= 0)((void) ((len >= 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5001, "len >= 0"
))))
;
5002 return bytes_to_str(allocator, ensure_contiguous(tvb, offset, len), len)bytes_to_str_maxlen(allocator, ensure_contiguous(tvb, offset,
len), len, 36)
;
5003}
5004
5005/* Find a needle tvbuff within a haystack tvbuff. */
5006int
5007tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const int haystack_offset)
5008{
5009 unsigned haystack_abs_offset = 0, haystack_abs_length = 0;
5010 const uint8_t *haystack_data;
5011 const uint8_t *needle_data;
5012 const unsigned needle_len = needle_tvb->length;
5013 const uint8_t *location;
5014
5015 DISSECTOR_ASSERT(haystack_tvb && haystack_tvb->initialized)((void) ((haystack_tvb && haystack_tvb->initialized
) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 5015, "haystack_tvb && haystack_tvb->initialized"
))))
;
5016
5017 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
5018 return -1;
5019 }
5020
5021 /* Get pointers to the tvbuffs' data. */
5022 haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
5023 needle_data = ensure_contiguous(needle_tvb, 0, -1);
5024
5025 check_offset_length(haystack_tvb, haystack_offset, -1,
5026 &haystack_abs_offset, &haystack_abs_length);
5027
5028 location = ws_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
5029 needle_data, needle_len);
5030
5031 if (location) {
5032 return (int) (location - haystack_data);
5033 }
5034
5035 return -1;
5036}
5037
5038unsigned
5039tvb_raw_offset(tvbuff_t *tvb)
5040{
5041 if (!(tvb->flags & TVBUFF_RAW_OFFSET0x00000002)) {
5042 tvb->raw_offset = tvb_offset_from_real_beginning(tvb);
5043 tvb->flags |= TVBUFF_RAW_OFFSET0x00000002;
5044 }
5045 return tvb->raw_offset;
5046}
5047
5048void
5049tvb_set_fragment(tvbuff_t *tvb)
5050{
5051 tvb->flags |= TVBUFF_FRAGMENT0x00000001;
5052}
5053
5054struct tvbuff *
5055tvb_get_ds_tvb(tvbuff_t *tvb)
5056{
5057 return(tvb->ds_tvb);
5058}
5059
5060unsigned
5061tvb_get_varint(tvbuff_t *tvb, unsigned offset, unsigned maxlen, uint64_t *value, const unsigned encoding)
5062{
5063 *value = 0;
5064
5065 switch (encoding & ENC_VARINT_MASK(0x00000002|0x00000004|0x00000008|0x00000010)) {
5066 case ENC_VARINT_PROTOBUF0x00000002:
5067 {
5068 unsigned i;
5069 uint64_t b; /* current byte */
5070
5071 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5072 b = tvb_get_uint8(tvb, offset++);
5073 *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5074
5075 if (b < 0x80) {
5076 /* end successfully because of last byte's msb(most significant bit) is zero */
5077 return i + 1;
5078 }
5079 }
5080 break;
5081 }
5082
5083 case ENC_VARINT_ZIGZAG0x00000008:
5084 {
5085 unsigned i;
5086 uint64_t b; /* current byte */
5087
5088 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5089 b = tvb_get_uint8(tvb, offset++);
5090 *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5091
5092 if (b < 0x80) {
5093 /* end successfully because of last byte's msb(most significant bit) is zero */
5094 *value = (*value >> 1) ^ ((*value & 1) ? -1 : 0);
5095 return i + 1;
5096 }
5097 }
5098 break;
5099 }
5100
5101 case ENC_VARINT_SDNV0x00000010:
5102 {
5103 /* Decodes similar to protobuf but in MSByte order */
5104 unsigned i;
5105 uint64_t b; /* current byte */
5106
5107 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5108 b = tvb_get_uint8(tvb, offset++);
5109 if ((i == 9) && (*value >= UINT64_C(1)1UL<<(64-7))) {
5110 // guaranteed overflow, not valid SDNV
5111 return 0;
5112 }
5113 *value <<= 7;
5114 *value |= (b & 0x7F); /* add lower 7 bits to val */
5115
5116 if (b < 0x80) {
5117 /* end successfully because of last byte's msb(most significant bit) is zero */
5118 return i + 1;
5119 }
5120 }
5121 break;
5122 }
5123
5124 case ENC_VARINT_QUIC0x00000004:
5125 {
5126 /* calculate variable length */
5127 *value = tvb_get_uint8(tvb, offset);
5128 switch((*value) >> 6) {
5129 case 0: /* 0b00 => 1 byte length (6 bits Usable) */
5130 (*value) &= 0x3F;
5131 return 1;
5132 case 1: /* 0b01 => 2 bytes length (14 bits Usable) */
5133 *value = tvb_get_ntohs(tvb, offset) & 0x3FFF;
5134 return 2;
5135 case 2: /* 0b10 => 4 bytes length (30 bits Usable) */
5136 *value = tvb_get_ntohl(tvb, offset) & 0x3FFFFFFF;
5137 return 4;
5138 case 3: /* 0b11 => 8 bytes length (62 bits Usable) */
5139 *value = tvb_get_ntoh64(tvb, offset) & UINT64_C(0x3FFFFFFFFFFFFFFF)0x3FFFFFFFFFFFFFFFUL;
5140 return 8;
5141 default: /* No Possible */
5142 ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/tvbuff.c", 5142,
__func__, "assertion \"not reached\" failed")
;
5143 break;
5144 }
5145 break;
5146 }
5147
5148 default:
5149 DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\""
, "epan/tvbuff.c", 5149))
;
5150 }
5151
5152 return 0; /* 10 bytes scanned, but no bytes' msb is zero */
5153}
5154
5155/*
5156 * Editor modelines - https://www.wireshark.org/tools/modelines.html
5157 *
5158 * Local variables:
5159 * c-basic-offset: 8
5160 * tab-width: 8
5161 * indent-tabs-mode: t
5162 * End:
5163 *
5164 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
5165 * :indentSize=8:tabSize=8:noTabs=false:
5166 */