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