Bug Summary

File:epan/tvbuff.c
Warning:line 530, 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-05-100330-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 int total_no_of_bits);
51
52static uint64_t
53_tvb_get_bits64_le(tvbuff_t *tvb, unsigned bit_offset, const int total_no_of_bits);
54
55static inline unsigned
56_tvb_captured_length_remaining(const tvbuff_t *tvb, const unsigned offset);
57
58static inline const uint8_t*
59ensure_contiguous(tvbuff_t *tvb, const int offset, const int length);
60
61static inline const uint8_t*
62ensure_contiguous_unsigned(tvbuff_t *tvb, const unsigned offset, const unsigned length);
63
64static inline uint8_t *
65tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length);
66
67tvbuff_t *
68tvb_new(const struct tvb_ops *ops)
69{
70 tvbuff_t *tvb;
71 size_t size = ops->tvb_size;
72
73 ws_assert(size >= sizeof(*tvb))do { if ((1) && !(size >= sizeof(*tvb))) ws_log_fatal_full
("", LOG_LEVEL_ERROR, "epan/tvbuff.c", 73, __func__, "assertion failed: %s"
, "size >= sizeof(*tvb)"); } while (0)
;
74
75 tvb = (tvbuff_t *) g_slice_alloc(size);
76
77 tvb->next = NULL((void*)0);
78 tvb->ops = ops;
79 tvb->initialized = false0;
80 tvb->flags = 0;
81 tvb->length = 0;
82 tvb->reported_length = 0;
83 tvb->contained_length = 0;
84 tvb->real_data = NULL((void*)0);
85 tvb->raw_offset = 0;
86 tvb->ds_tvb = NULL((void*)0);
87
88 return tvb;
89}
90
91static void
92tvb_free_internal(tvbuff_t *tvb)
93{
94 size_t size;
95
96 DISSECTOR_ASSERT(tvb)((void) ((tvb) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 96, "tvb"))))
;
97
98 if (tvb->ops->tvb_free)
99 tvb->ops->tvb_free(tvb);
100
101 size = tvb->ops->tvb_size;
102
103 g_slice_free1(size, tvb);
104}
105
106/* XXX: just call tvb_free_chain();
107 * Not removed so that existing dissectors using tvb_free() need not be changed.
108 * I'd argue that existing calls to tvb_free() should have actually been
109 * calls to tvb_free_chain() although the calls were OK as long as no
110 * subsets, etc had been created on the tvb. */
111void
112tvb_free(tvbuff_t *tvb)
113{
114 tvb_free_chain(tvb);
115}
116
117void
118tvb_free_chain(tvbuff_t *tvb)
119{
120 tvbuff_t *next_tvb;
121 DISSECTOR_ASSERT(tvb)((void) ((tvb) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 121, "tvb"))))
;
122 while (tvb) {
123 next_tvb = tvb->next;
124 tvb_free_internal(tvb);
125 tvb = next_tvb;
126 }
127}
128
129tvbuff_t *
130tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing)
131{
132 tvbuff_t *tvb = tvb_new_proxy(backing);
133
134 tvb_add_to_chain(parent, tvb);
135 return tvb;
136}
137
138void
139tvb_add_to_chain(tvbuff_t *parent, tvbuff_t *child)
140{
141 tvbuff_t *tmp;
142
143 DISSECTOR_ASSERT(parent)((void) ((parent) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 143, "parent"))))
;
144 DISSECTOR_ASSERT(child)((void) ((child) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 144, "child"))))
;
145
146 while (child) {
147 tmp = child;
148 child = child->next;
149
150 tmp->next = parent->next;
151 parent->next = tmp;
152 }
153}
154
155/*
156 * Check whether that offset goes more than one byte past the
157 * end of the buffer.
158 *
159 * If not, return 0; otherwise, return exception
160 */
161static inline int
162validate_offset(const tvbuff_t *tvb, const unsigned abs_offset)
163{
164 if (G_LIKELY(abs_offset <= tvb->length)(abs_offset <= tvb->length)) {
165 /* It's OK. */
166 return 0;
167 }
168
169 /*
170 * It's not OK, but why? Which boundaries is it
171 * past?
172 */
173 if (abs_offset <= tvb->contained_length) {
174 /*
175 * It's past the captured length, but not past
176 * the reported end of any parent tvbuffs from
177 * which this is constructed, or the reported
178 * end of this tvbuff, so it's out of bounds
179 * solely because we're past the end of the
180 * captured data.
181 */
182 return BoundsError1;
183 }
184
185 /*
186 * There's some actual packet boundary, not just the
187 * artificial boundary imposed by packet slicing, that
188 * we're past.
189 */
190
191 if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
192 /*
193 * This tvbuff is the first fragment of a larger
194 * packet that hasn't been reassembled, so we
195 * assume that's the source of the problem - if
196 * we'd reassembled the packet, we wouldn't have
197 * gone past the end.
198 *
199 * That might not be true, but for at least
200 * some forms of reassembly, such as IP
201 * reassembly, you don't know how big the
202 * reassembled packet is unless you reassemble
203 * it, so, in those cases, we can't determine
204 * whether we would have gone past the end
205 * had we reassembled the packet.
206 */
207 return FragmentBoundsError4;
208 }
209
210 /* OK, we're not an unreassembled fragment (that we know of). */
211 if (abs_offset <= tvb->reported_length) {
212 /*
213 * We're within the bounds of what this tvbuff
214 * purportedly contains, based on some length
215 * value, but we're not within the bounds of
216 * something from which this tvbuff was
217 * extracted, so that length value ran past
218 * the end of some parent tvbuff.
219 */
220 return ContainedBoundsError2;
221 }
222
223 /*
224 * OK, it looks as if we ran past the claimed length
225 * of data.
226 */
227 return ReportedBoundsError3;
228}
229
230static inline int
231validate_offset_and_remaining(const tvbuff_t *tvb, const unsigned offset, unsigned *rem_len)
232{
233 int exception;
234
235 exception = validate_offset(tvb, offset);
236 if (!exception)
237 *rem_len = tvb->length - offset;
238
239 return exception;
240}
241
242/* Returns integer indicating whether the given offset and the end offset
243 * calculated from that offset and the given length are in bounds (0) or
244 * not (exception number).
245 * No exception is thrown; on success, we return 0, otherwise we return an
246 * exception for the caller to throw if appropriate.
247 *
248 * N.B. - we return success (0), if the offset is positive and right
249 * after the end of the tvbuff (i.e., equal to the length). We do this
250 * so that a dissector constructing a subset tvbuff for the next protocol
251 * will get a zero-length tvbuff, not an exception, if there's no data
252 * left for the next protocol - we want the next protocol to be the one
253 * that gets an exception, so the error is reported as an error in that
254 * protocol rather than the containing protocol. */
255static inline int
256validate_offset_length_no_exception(const tvbuff_t *tvb,
257 const unsigned offset, const unsigned length)
258{
259 unsigned end_offset;
260 int exception;
261
262 /* Compute the offset */
263 /* Since offset is unsigned, the only effect of validation is to throw
264 * a possibly different exception if offset is outside the captured
265 * bytes. E.g., offset could be outside the captured bytes but in the
266 * reported length, but end_offset outside the reported length.
267 * XXX - Which *is* the proper exception? Compare fast_ensure_contiguous
268 * which only throws the exception related to the end offset.
269 */
270 exception = validate_offset(tvb, offset);
271 if (exception)
272 return exception;
273
274 /*
275 * Compute the offset of the first byte past the length,
276 * checking for an overflow.
277 */
278 if (ckd_add(&end_offset, offset, length)__builtin_add_overflow((offset), (length), (&end_offset)))
279 return BoundsError1;
280
281 return validate_offset(tvb, end_offset);
282}
283
284/* Checks offset and length and throws an exception if
285 * either is out of bounds. Sets integer ptrs to the new length. */
286static inline void
287validate_offset_length(const tvbuff_t *tvb,
288 const unsigned offset, const unsigned length)
289{
290 int exception;
291
292 exception = validate_offset_length_no_exception(tvb, offset, length);
293 if (exception)
294 THROW(exception)except_throw(1, (exception), ((void*)0));
295}
296
297/* Internal function so that other translation units can use
298 * validate_offset_length. */
299void
300tvb_validate_offset_length(const tvbuff_t *tvb,
301 const unsigned offset, const unsigned length)
302{
303 validate_offset_length(tvb, offset, length);
304}
305
306/* Internal function so that other translation units can use
307 * validate_offset_and_remaining. This throws the exception
308 * from validate_offset_and_remaining. */
309void
310tvb_validate_offset_and_remaining(const tvbuff_t *tvb,
311 const unsigned offset, unsigned *rem_len)
312{
313 int exception;
314
315 exception = validate_offset_and_remaining(tvb, offset, rem_len);
316 if (exception)
317 THROW(exception)except_throw(1, (exception), ((void*)0));
318}
319
320/*
321 * The same as validate_offset except this accepts negative offsets, meaning
322 * relative to the end of (captured) length. (That it's captured, not reported,
323 * length is one reason to deprecate signed offsets, #20103.)
324 */
325static inline int
326compute_offset(const tvbuff_t *tvb, const int offset, unsigned *offset_ptr)
327{
328 if (offset >= 0) {
329 /* Positive offset - relative to the beginning of the packet. */
330 if (G_LIKELY((unsigned) offset <= tvb->length)((unsigned) offset <= tvb->length)) {
331 *offset_ptr = offset;
332 } else if ((unsigned) offset <= tvb->contained_length) {
333 return BoundsError1;
334 } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
335 return FragmentBoundsError4;
336 } else if ((unsigned) offset <= tvb->reported_length) {
337 return ContainedBoundsError2;
338 } else {
339 return ReportedBoundsError3;
340 }
341 }
342 else {
343 /* Negative offset - relative to the end of the packet. */
344 /* Prevent UB on 2's complement platforms. All tested compilers
345 * (gcc, clang, MSVC) compile this to a single instruction on
346 * x86, ARM, RISC-V, S390x, SPARC, etc. at -O1 and higher
347 * according to godbolt.org. */
348 unsigned abs_offset = ((unsigned)-(offset + 1)) + 1;
349 if (G_LIKELY(abs_offset <= tvb->length)(abs_offset <= tvb->length)) {
350 *offset_ptr = tvb->length - abs_offset;
351 } else if (abs_offset <= tvb->contained_length) {
352 return BoundsError1;
353 } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
354 return FragmentBoundsError4;
355 } else if (abs_offset <= tvb->reported_length) {
356 return ContainedBoundsError2;
357 } else {
358 return ReportedBoundsError3;
359 }
360 }
361
362 return 0;
363}
364
365static inline int
366compute_offset_and_remaining(const tvbuff_t *tvb, const int offset, unsigned *offset_ptr, unsigned *rem_len)
367{
368 int exception;
369
370 exception = compute_offset(tvb, offset, offset_ptr);
371 if (!exception)
372 *rem_len = tvb->length - *offset_ptr;
373
374 return exception;
375}
376
377/* Computes the absolute offset and length based on a possibly-negative offset
378 * and a length that is possible -1 (which means "to the end of the data").
379 * Returns integer indicating whether the offset is in bounds (0) or
380 * not (exception number). The integer ptrs are modified with the new offset,
381 * captured (available) length, and contained length (amount that's present
382 * in the parent tvbuff based on its reported length).
383 * No exception is thrown; on success, we return 0, otherwise we return an
384 * exception for the caller to throw if appropriate.
385 *
386 * XXX - we return success (0), if the offset is positive and right
387 * after the end of the tvbuff (i.e., equal to the length). We do this
388 * so that a dissector constructing a subset tvbuff for the next protocol
389 * will get a zero-length tvbuff, not an exception, if there's no data
390 * left for the next protocol - we want the next protocol to be the one
391 * that gets an exception, so the error is reported as an error in that
392 * protocol rather than the containing protocol. */
393static inline int
394check_offset_length_no_exception(const tvbuff_t *tvb,
395 const int offset, int const length_val,
396 unsigned *offset_ptr, unsigned *length_ptr)
397{
398 unsigned end_offset;
399 int exception;
400
401 DISSECTOR_ASSERT(offset_ptr)((void) ((offset_ptr) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 401, "offset_ptr"
))))
;
402 DISSECTOR_ASSERT(length_ptr)((void) ((length_ptr) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 402, "length_ptr"
))))
;
403
404 /* Compute the offset */
405 exception = compute_offset(tvb, offset, offset_ptr);
406 if (exception)
407 return exception;
408
409 if (length_val < -1) {
410 /* XXX - ReportedBoundsError? */
411 return BoundsError1;
412 }
413
414 /* Compute the length */
415 if (length_val == -1)
416 *length_ptr = tvb->length - *offset_ptr;
417 else
418 *length_ptr = length_val;
419
420 /*
421 * Compute the offset of the first byte past the length.
422 */
423 end_offset = *offset_ptr + *length_ptr;
424
425 /*
426 * Check for an overflow
427 */
428 if (end_offset < *offset_ptr)
429 return BoundsError1;
430
431 return validate_offset(tvb, end_offset);
432}
433
434/* Checks (+/-) offset and length and throws an exception if
435 * either is out of bounds. Sets integer ptrs to the new offset
436 * and length. */
437static inline void
438check_offset_length(const tvbuff_t *tvb,
439 const int offset, int const length_val,
440 unsigned *offset_ptr, unsigned *length_ptr)
441{
442 int exception;
443
444 exception = check_offset_length_no_exception(tvb, offset, length_val, offset_ptr, length_ptr);
445 if (exception)
446 THROW(exception)except_throw(1, (exception), ((void*)0));
447}
448
449/* Internal function so that other translation units can use
450 * check_offset_length. */
451void
452tvb_check_offset_length(const tvbuff_t *tvb,
453 const int offset, int const length_val,
454 unsigned *offset_ptr, unsigned *length_ptr)
455{
456 check_offset_length(tvb, offset, length_val, offset_ptr, length_ptr);
457}
458
459static const unsigned char left_aligned_bitmask[] = {
460 0xff,
461 0x80,
462 0xc0,
463 0xe0,
464 0xf0,
465 0xf8,
466 0xfc,
467 0xfe
468};
469
470/* tvb_new_octet_aligned used to support -1 no_of_bits as meaning "to the
471 * end of the buffer." Nothing every used it. It could be supported with
472 * a _remaining() function if necessary. Note that the previous implementation
473 * didn't properly keep the extra reported length if the reported length
474 * was greater than the captured length.
475 */
476
477tvbuff_t *
478tvb_new_octet_aligned(tvbuff_t *tvb, uint32_t bit_offset, uint32_t no_of_bits)
479{
480 tvbuff_t *sub_tvb = NULL((void*)0);
481 uint32_t byte_offset;
482 uint32_t datalen, i;
483 uint8_t left, right, remaining_bits, *buf;
484 const uint8_t *data;
485
486 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 486, "tvb && tvb->initialized"
))))
;
4
Assuming 'tvb' is non-null
5
Assuming field 'initialized' is true
6
'?' condition is true
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) {
7
Assuming 'remaining_bits' is not equal to 0
8
Taking true branch
495 datalen++;
496 }
497
498 /* already aligned -> shortcut */
499 if (((left == 0) && (remaining_bits
9.1
'remaining_bits' is not equal to 0
== 0)) || datalen == 0) {
9
Assuming 'left' is equal to 0
10
Assuming 'datalen' is not equal to 0
11
Taking false branch
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) {
12
Assuming the condition is true
13
Taking true branch
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);
14
Memory is allocated
513
514 /* shift tvb data bit_offset bits to the left */
515 for (i = 0; i
14.1
'i' is < 'datalen'
< datalen
; i++)
15
Loop condition is true. Entering loop body
16
Assuming 'i' is >= 'datalen'
17
Loop condition is false. Execution continues on line 528
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);
18
Potential leak of memory pointed to by 'buf'
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"
))))
;
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) {
556 dst_len++;
557 }
558
559 /* already aligned -> shortcut */
560 if (((right == 0) && (remaining_bits == 0)) || dst_len == 0) {
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) {
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);
576
577 for (i = 0; i < (dst_len - 1); i++)
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 > dst_len)
584 buf[i] |= (data[i+1] << left);
585 /* Preserve only remaining bits in last octet if not multiple of 8 */
586 if (remaining_bits)
587 buf[i] &= ((1 << remaining_bits) - 1);
588
589 sub_tvb = tvb_new_child_real_data(tvb, buf, dst_len, dst_len);
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 false
2
Taking false branch
2355 sub_tvb = tvb_new_octet_right_aligned(tvb, bit_offset, (int32_t) no_of_bits);
2356 } else {
2357 sub_tvb = tvb_new_octet_aligned(tvb, bit_offset, (int32_t) no_of_bits);
3
Calling 'tvb_new_octet_aligned'
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 int 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 int 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 int 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 int 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 int 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 int total_no_of_bits)
2489{
2490 uint64_t value = 0;
2491 unsigned octet_offset = bit_offset / 8;
2492 int remaining_bits = total_no_of_bits;
2493 int 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 int 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/* Find length of string by looking for end of string ('\0'), up to
2999 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
3000 * of tvbuff.
3001 * Returns -1 if 'maxlength' reached before finding EOS. */
3002int
3003tvb_strnlen(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
3004{
3005 unsigned result_offset;
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
3009 /* TODO - this needs a variant that returns a bool
3010 * and sets a unsigned offset to the value if true. */
3011 if (!tvb_find_uint8_length(tvb, offset, maxlength, 0, &result_offset)) {
3012 return -1;
3013 }
3014 else {
3015 return (int)(result_offset - offset);
3016 }
3017}
3018
3019/*
3020 * Implement strneql etc
3021 */
3022
3023/*
3024 * Call strncmp after checking if enough chars left, returning 0 if
3025 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
3026 */
3027int
3028tvb_strneql(tvbuff_t *tvb, const unsigned offset, const char *str, const size_t size)
3029{
3030 const uint8_t *ptr;
3031
3032 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3033
3034 if (ptr) {
3035 int cmp = strncmp((const char *)ptr, str, size);
3036
3037 /*
3038 * Return 0 if equal, -1 otherwise.
3039 */
3040 return (cmp == 0 ? 0 : -1);
3041 } else {
3042 /*
3043 * Not enough characters in the tvbuff to match the
3044 * string.
3045 */
3046 return -1;
3047 }
3048}
3049
3050/*
3051 * Call g_ascii_strncasecmp after checking if enough chars left, returning
3052 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
3053 */
3054int
3055tvb_strncaseeql(tvbuff_t *tvb, const unsigned offset, const char *str, const size_t size)
3056{
3057 const uint8_t *ptr;
3058
3059 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3060
3061 if (ptr) {
3062 int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
3063
3064 /*
3065 * Return 0 if equal, -1 otherwise.
3066 */
3067 return (cmp == 0 ? 0 : -1);
3068 } else {
3069 /*
3070 * Not enough characters in the tvbuff to match the
3071 * string.
3072 */
3073 return -1;
3074 }
3075}
3076
3077/*
3078 * Check that the tvbuff contains at least size bytes, starting at
3079 * offset, and that those bytes are equal to str. Return 0 for success
3080 * and -1 for error. This function does not throw an exception.
3081 */
3082int
3083tvb_memeql(tvbuff_t *tvb, const unsigned offset, const uint8_t *str, size_t size)
3084{
3085 const uint8_t *ptr;
3086
3087 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3088
3089 if (ptr) {
3090 int cmp = memcmp(ptr, str, size);
3091
3092 /*
3093 * Return 0 if equal, -1 otherwise.
3094 */
3095 return (cmp == 0 ? 0 : -1);
3096 } else {
3097 /*
3098 * Not enough characters in the tvbuff to match the
3099 * string.
3100 */
3101 return -1;
3102 }
3103}
3104
3105/**
3106 * Format the data in the tvb from offset for size.
3107 */
3108char *
3109tvb_format_text(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3110{
3111 const uint8_t *ptr;
3112
3113 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3114 return format_text(scope, (const char*)ptr, size);
3115}
3116
3117/*
3118 * Format the data in the tvb from offset for length ...
3119 */
3120char *
3121tvb_format_text_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3122{
3123 const uint8_t *ptr;
3124
3125 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3126 return format_text_wsp(allocator, (const char*)ptr, size);
3127}
3128
3129/**
3130 * Like "tvb_format_text()", but for null-padded strings; don't show
3131 * the null padding characters as "\000".
3132 */
3133char *
3134tvb_format_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3135{
3136 const uint8_t *ptr, *p;
3137 unsigned stringlen;
3138
3139 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3140 for (p = ptr, stringlen = 0; stringlen < size && *p != '\0'; p++, stringlen++)
3141 ;
3142 return format_text(scope, (const char*)ptr, stringlen);
3143}
3144
3145/*
3146 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
3147 * the null padding characters as "\000".
3148 */
3149char *
3150tvb_format_stringzpad_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3151{
3152 const uint8_t *ptr, *p;
3153 unsigned stringlen;
3154
3155 ptr = ensure_contiguous(tvb, offset, size);
3156 for (p = ptr, stringlen = 0; stringlen < size && *p != '\0'; p++, stringlen++)
3157 ;
3158 return format_text_wsp(allocator, (const char*)ptr, stringlen);
3159}
3160
3161/*
3162 * All string functions below take a scope as an argument.
3163 *
3164 *
3165 * If scope is NULL, memory is allocated with g_malloc() and user must
3166 * explicitly free it with g_free().
3167 * If scope is not NULL, memory is allocated with the corresponding pool
3168 * lifetime.
3169 *
3170 * All functions throw an exception if the tvbuff ends before the string
3171 * does.
3172 */
3173
3174/*
3175 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3176 * of bytes referred to by the tvbuff, offset, and length as an ASCII string,
3177 * with all bytes with the high-order bit set being invalid, and return a
3178 * pointer to a UTF-8 string, allocated using the wmem scope.
3179 *
3180 * Octets with the highest bit set will be converted to the Unicode
3181 * REPLACEMENT CHARACTER.
3182 */
3183static uint8_t *
3184tvb_get_ascii_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3185{
3186 const uint8_t *ptr;
3187
3188 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3189 return get_ascii_string(scope, ptr, length);
3190}
3191
3192/*
3193 * Given a wmem scope, a tvbuff, an offset, a length, and a translation table,
3194 * treat the string of bytes referred to by the tvbuff, offset, and length
3195 * as a string encoded using one octet per character, with octets with the
3196 * high-order bit clear being mapped by the translation table to 2-byte
3197 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3198 * CHARACTER) and octets with the high-order bit set being mapped to
3199 * REPLACEMENT CHARACTER, and return a pointer to a UTF-8 string,
3200 * 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_iso_646_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3207{
3208 const uint8_t *ptr;
3209
3210 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3211 return get_iso_646_string(scope, ptr, length, table);
3212}
3213
3214/*
3215 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3216 * of bytes referred to by the tvbuff, the offset. and the length as a UTF-8
3217 * string, and return a pointer to a UTF-8 string, allocated using the wmem
3218 * scope, with all ill-formed sequences replaced with the Unicode REPLACEMENT
3219 * CHARACTER according to the recommended "best practices" given in the Unicode
3220 * Standard and specified by W3C/WHATWG.
3221 *
3222 * Note that in conformance with the Unicode Standard, this treats three
3223 * byte sequences corresponding to UTF-16 surrogate halves (paired or unpaired)
3224 * and two byte overlong encodings of 7-bit ASCII characters as invalid and
3225 * substitutes REPLACEMENT CHARACTER for them. Explicit support for nonstandard
3226 * derivative encoding formats (e.g. CESU-8, Java Modified UTF-8, WTF-8) could
3227 * be added later.
3228 */
3229static uint8_t *
3230tvb_get_utf_8_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3231{
3232 const uint8_t *ptr;
3233
3234 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3235 return get_utf_8_string(scope, ptr, length);
3236}
3237
3238/*
3239 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3240 * of bytes referred to by the tvbuff, the offset, and the length as a
3241 * raw string, and return a pointer to that string, allocated using the
3242 * wmem scope. This means a null is appended at the end, but no replacement
3243 * checking is done otherwise, unlike tvb_get_utf_8_string().
3244 */
3245static inline uint8_t *
3246tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3247{
3248 uint8_t *strbuf;
3249
3250 tvb_ensure_bytes_exist(tvb, offset, length);
3251 strbuf = (uint8_t *)wmem_alloc(scope, length + 1);
3252 tvb_memcpy(tvb, strbuf, offset, length);
3253 strbuf[length] = '\0';
3254 return strbuf;
3255}
3256
3257/*
3258 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3259 * of bytes referred to by the tvbuff, the offset, and the length as an
3260 * ISO 8859/1 string, and return a pointer to a UTF-8 string, allocated
3261 * using the wmem scope.
3262 */
3263static uint8_t *
3264tvb_get_string_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3265{
3266 const uint8_t *ptr;
3267
3268 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3269 return get_8859_1_string(scope, ptr, length);
3270}
3271
3272/*
3273 * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3274 * table, treat the string of bytes referred to by the tvbuff, the offset,
3275 * and the length as a string encoded using one octet per character, with
3276 * octets with the high-order bit clear being ASCII and octets with the
3277 * high-order bit set being mapped by the translation table to 2-byte
3278 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3279 * CHARACTER), and return a pointer to a UTF-8 string, allocated with the
3280 * wmem scope.
3281 */
3282static uint8_t *
3283tvb_get_string_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3284{
3285 const uint8_t *ptr;
3286
3287 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3288 return get_unichar2_string(scope, ptr, length, table);
3289}
3290
3291/*
3292 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3293 * giving the byte order, treat the string of bytes referred to by the
3294 * tvbuff, the offset, and the length as a UCS-2 encoded string in
3295 * the byte order in question, containing characters from the Basic
3296 * Multilingual Plane (plane 0) of Unicode, and return a pointer to a
3297 * UTF-8 string, allocated with the wmem scope.
3298 *
3299 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3300 * optionally with ENC_BOM.
3301 *
3302 * Specify length in bytes.
3303 *
3304 * XXX - should map lead and trail surrogate values to REPLACEMENT
3305 * CHARACTERs (0xFFFD)?
3306 * XXX - if there are an odd number of bytes, should put a
3307 * REPLACEMENT CHARACTER at the end.
3308 */
3309static uint8_t *
3310tvb_get_ucs_2_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3311{
3312 const uint8_t *ptr;
3313
3314 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3315 return get_ucs_2_string(scope, ptr, length, encoding);
3316}
3317
3318/*
3319 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3320 * giving the byte order, treat the string of bytes referred to by the
3321 * tvbuff, the offset, and the length as a UTF-16 encoded string in
3322 * the byte order in question, and return a pointer to a UTF-8 string,
3323 * allocated with the wmem scope.
3324 *
3325 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3326 * optionally with ENC_BOM.
3327 *
3328 * Specify length in bytes.
3329 *
3330 * XXX - should map surrogate errors to REPLACEMENT CHARACTERs (0xFFFD).
3331 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3332 * XXX - if there are an odd number of bytes, should put a
3333 * REPLACEMENT CHARACTER at the end.
3334 */
3335static uint8_t *
3336tvb_get_utf_16_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3337{
3338 const uint8_t *ptr;
3339
3340 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3341 return get_utf_16_string(scope, ptr, length, encoding);
3342}
3343
3344/*
3345 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3346 * giving the byte order, treat the string of bytes referred to by the
3347 * tvbuff, the offset, and the length as a UCS-4 encoded string in
3348 * the byte order in question, and return a pointer to a UTF-8 string,
3349 * allocated with the wmem scope.
3350 *
3351 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3352 * optionally with ENC_BOM.
3353 *
3354 * Specify length in bytes
3355 *
3356 * XXX - should map lead and trail surrogate values to a "substitute"
3357 * UTF-8 character?
3358 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3359 * XXX - if the number of bytes isn't a multiple of 4, should put a
3360 * REPLACEMENT CHARACTER at the end.
3361 */
3362static char *
3363tvb_get_ucs_4_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3364{
3365 const uint8_t *ptr;
3366
3367 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3368 return (char*)get_ucs_4_string(scope, ptr, length, encoding);
3369}
3370
3371char *
3372tvb_get_ts_23_038_7bits_string_packed(wmem_allocator_t *scope, tvbuff_t *tvb,
3373 const unsigned bit_offset, unsigned no_of_chars)
3374{
3375 unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3376 unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3377 const uint8_t *ptr;
3378
3379 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3379, "tvb && tvb->initialized"
))))
;
3380
3381 ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3382 return (char*)get_ts_23_038_7bits_string_packed(scope, ptr, bit_offset, no_of_chars);
3383}
3384
3385char *
3386tvb_get_ts_23_038_7bits_string_unpacked(wmem_allocator_t *scope, tvbuff_t *tvb,
3387 const unsigned offset, unsigned length)
3388{
3389 const uint8_t *ptr;
3390
3391 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3391, "tvb && tvb->initialized"
))))
;
3392
3393 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3394 return (char*)get_ts_23_038_7bits_string_unpacked(scope, ptr, length);
3395}
3396
3397char *
3398tvb_get_etsi_ts_102_221_annex_a_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3399 const unsigned offset, unsigned length)
3400{
3401 const uint8_t *ptr;
3402
3403 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3403, "tvb && tvb->initialized"
))))
;
3404
3405 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3406 return (char*)get_etsi_ts_102_221_annex_a_string(scope, ptr, length);
3407}
3408
3409char *
3410tvb_get_ascii_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3411 const unsigned bit_offset, unsigned no_of_chars)
3412{
3413 unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3414 unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3415 const uint8_t *ptr;
3416
3417 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3417, "tvb && tvb->initialized"
))))
;
3418
3419 ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3420 return (char*)get_ascii_7bits_string(scope, ptr, bit_offset, no_of_chars);
3421}
3422
3423/*
3424 * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3425 * table, treat the string of bytes referred to by the tvbuff, the offset,
3426 * and the length as a string encoded using one octet per character, with
3427 * octets being mapped by the translation table to 2-byte Unicode Basic
3428 * Multilingual Plane characters (including REPLACEMENT CHARACTER), and
3429 * return a pointer to a UTF-8 string, allocated with the wmem scope.
3430 */
3431static uint8_t *
3432tvb_get_nonascii_unichar2_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[256])
3433{
3434 const uint8_t *ptr;
3435
3436 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3437 return get_nonascii_unichar2_string(scope, ptr, length, table);
3438}
3439
3440/*
3441 * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3442 * referred to by the tvbuff, offset, and length as a GB18030 encoded string,
3443 * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3444 * converted having substituted REPLACEMENT CHARACTER according to the
3445 * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3446 * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3447 *
3448 * As expected, this will also decode GBK and GB2312 strings.
3449 */
3450static uint8_t *
3451tvb_get_gb18030_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3452{
3453 const uint8_t *ptr;
3454
3455 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3456 return get_gb18030_string(scope, ptr, length);
3457}
3458
3459/*
3460 * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3461 * referred to by the tvbuff, offset, and length as a EUC-KR encoded string,
3462 * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3463 * converted having substituted REPLACEMENT CHARACTER according to the
3464 * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3465 * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3466 */
3467static uint8_t *
3468tvb_get_euc_kr_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3469{
3470 const uint8_t *ptr;
3471
3472 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3473 return get_euc_kr_string(scope, ptr, length);
3474}
3475
3476static uint8_t *
3477tvb_get_t61_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3478{
3479 const uint8_t *ptr;
3480
3481 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3482 return get_t61_string(scope, ptr, length);
3483}
3484
3485/*
3486 * Encoding tables for BCD strings.
3487 */
3488static const dgt_set_t Dgt0_9_bcd = {
3489 {
3490 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3491 '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?'
3492 }
3493};
3494
3495static const dgt_set_t Dgt_keypad_abc_tbcd = {
3496 {
3497 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3498 '0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'
3499 }
3500};
3501
3502static const dgt_set_t Dgt_ansi_tbcd = {
3503 {
3504 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3505 '0','1','2','3','4','5','6','7','8','9','?','B','C','*','#','?'
3506 }
3507};
3508
3509static const dgt_set_t Dgt_dect_standard_4bits_tbcd = {
3510 {
3511 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3512 '0','1','2','3','4','5','6','7','8','9','?',' ','?','?','?','?'
3513 }
3514};
3515
3516static uint8_t *
3517tvb_get_apn_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3518 unsigned length)
3519{
3520 wmem_strbuf_t *str;
3521
3522 /*
3523 * This is a domain name.
3524 *
3525 * 3GPP TS 23.003, section 19.4.2 "Fully Qualified Domain Names
3526 * (FQDNs)", subsection 19.4.2.1 "General", says:
3527 *
3528 * The encoding of any identifier used as part of a Fully
3529 * Qualified Domain Name (FQDN) shall follow the Name Syntax
3530 * defined in IETF RFC 2181 [18], IETF RFC 1035 [19] and
3531 * IETF RFC 1123 [20]. An FQDN consists of one or more
3532 * labels. Each label is coded as a one octet length field
3533 * followed by that number of octets coded as 8 bit ASCII
3534 * characters.
3535 *
3536 * so this does not appear to use full-blown DNS compression -
3537 * the upper 2 bits of the length don't indicate that it's a
3538 * pointer or an extended label (RFC 2673).
3539 */
3540 str = wmem_strbuf_new_sized(scope, length + 1);
3541 if (length > 0) {
3542 const uint8_t *ptr;
3543
3544 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3545
3546 for (;;) {
3547 unsigned label_len;
3548
3549 /*
3550 * Process this label.
3551 */
3552 label_len = *ptr;
3553 ptr++;
3554 length--;
3555
3556 while (label_len != 0) {
3557 uint8_t ch;
3558
3559 if (length == 0)
3560 goto end;
3561
3562 ch = *ptr;
3563 if (ch < 0x80)
3564 wmem_strbuf_append_c(str, ch);
3565 else
3566 wmem_strbuf_append_unichar_repl(str)wmem_strbuf_append_unichar(str, 0x00FFFD);
3567 ptr++;
3568 label_len--;
3569 length--;
3570 }
3571
3572 if (length == 0)
3573 goto end;
3574
3575 wmem_strbuf_append_c(str, '.');
3576 }
3577 }
3578
3579end:
3580 return (uint8_t *) wmem_strbuf_finalize(str);
3581}
3582
3583static uint8_t *
3584tvb_get_dect_standard_8bits_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3585{
3586 const uint8_t *ptr;
3587
3588 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3589 return get_dect_standard_8bits_string(scope, ptr, length);
3590}
3591
3592/*
3593 * Given a tvbuff, an offset, a length, and an encoding, allocate a
3594 * buffer big enough to hold a non-null-terminated string of that length
3595 * at that offset, plus a trailing '\0', copy into the buffer the
3596 * string as converted from the appropriate encoding to UTF-8, and
3597 * return a pointer to the string.
3598 */
3599uint8_t *
3600tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3601 const unsigned length, const unsigned encoding)
3602{
3603 uint8_t *strptr;
3604 bool_Bool odd, skip_first;
3605
3606 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3606, "tvb && tvb->initialized"
))))
;
3607
3608 switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
3609
3610 case ENC_ASCII0x00000000:
3611 default:
3612 /*
3613 * For now, we treat bogus values as meaning
3614 * "ASCII" rather than reporting an error,
3615 * for the benefit of old dissectors written
3616 * when the last argument to proto_tree_add_item()
3617 * was a bool for the byte order, not an
3618 * encoding value, and passed non-zero values
3619 * other than true to mean "little-endian".
3620 */
3621 strptr = tvb_get_ascii_string(scope, tvb, offset, length);
3622 break;
3623
3624 case ENC_UTF_80x00000002:
3625 strptr = tvb_get_utf_8_string(scope, tvb, offset, length);
3626 break;
3627
3628 case ENC_UTF_160x00000004:
3629 strptr = tvb_get_utf_16_string(scope, tvb, offset, length,
3630 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3631 break;
3632
3633 case ENC_UCS_20x00000006:
3634 strptr = tvb_get_ucs_2_string(scope, tvb, offset, length,
3635 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3636 break;
3637
3638 case ENC_UCS_40x00000008:
3639 strptr = (uint8_t*)tvb_get_ucs_4_string(scope, tvb, offset, length,
3640 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3641 break;
3642
3643 case ENC_ISO_8859_10x0000000A:
3644 /*
3645 * ISO 8859-1 printable code point values are equal
3646 * to the equivalent Unicode code point value, so
3647 * no translation table is needed.
3648 */
3649 strptr = tvb_get_string_8859_1(scope, tvb, offset, length);
3650 break;
3651
3652 case ENC_ISO_8859_20x0000000C:
3653 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_2);
3654 break;
3655
3656 case ENC_ISO_8859_30x0000000E:
3657 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_3);
3658 break;
3659
3660 case ENC_ISO_8859_40x00000010:
3661 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_4);
3662 break;
3663
3664 case ENC_ISO_8859_50x00000012:
3665 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_5);
3666 break;
3667
3668 case ENC_ISO_8859_60x00000014:
3669 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_6);
3670 break;
3671
3672 case ENC_ISO_8859_70x00000016:
3673 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_7);
3674 break;
3675
3676 case ENC_ISO_8859_80x00000018:
3677 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_8);
3678 break;
3679
3680 case ENC_ISO_8859_90x0000001A:
3681 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_9);
3682 break;
3683
3684 case ENC_ISO_8859_100x0000001C:
3685 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_10);
3686 break;
3687
3688 case ENC_ISO_8859_110x0000001E:
3689 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_11);
3690 break;
3691
3692 case ENC_ISO_8859_130x00000022:
3693 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_13);
3694 break;
3695
3696 case ENC_ISO_8859_140x00000024:
3697 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_14);
3698 break;
3699
3700 case ENC_ISO_8859_150x00000026:
3701 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_15);
3702 break;
3703
3704 case ENC_ISO_8859_160x00000028:
3705 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_16);
3706 break;
3707
3708 case ENC_WINDOWS_12500x0000002A:
3709 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1250);
3710 break;
3711
3712 case ENC_WINDOWS_12510x0000003C:
3713 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1251);
3714 break;
3715
3716 case ENC_WINDOWS_12520x0000003A:
3717 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1252);
3718 break;
3719
3720 case ENC_MAC_ROMAN0x00000030:
3721 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_mac_roman);
3722 break;
3723
3724 case ENC_CP4370x00000032:
3725 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp437);
3726 break;
3727
3728 case ENC_CP8550x0000003E:
3729 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp855);
3730 break;
3731
3732 case ENC_CP8660x00000040:
3733 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp866);
3734 break;
3735
3736 case ENC_ISO_646_BASIC0x00000042:
3737 strptr = tvb_get_iso_646_string(scope, tvb, offset, length, charset_table_iso_646_basic);
3738 break;
3739
3740 case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
3741 {
3742 unsigned bit_offset = offset << 3;
3743 unsigned no_of_chars = (length << 3) / 7;
3744 strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_packed(scope, tvb, bit_offset, no_of_chars);
3745 }
3746 break;
3747
3748 case ENC_ASCII_7BITS0x00000034:
3749 {
3750 unsigned bit_offset = offset << 3;
3751 unsigned no_of_chars = (length << 3) / 7;
3752 strptr = (uint8_t*)tvb_get_ascii_7bits_string(scope, tvb, bit_offset, no_of_chars);
3753 }
3754 break;
3755
3756 case ENC_EBCDIC0x0000002E:
3757 /*
3758 * "Common" EBCDIC, covering all characters with the
3759 * same code point in all Roman-alphabet EBCDIC code
3760 * pages.
3761 */
3762 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic);
3763 break;
3764
3765 case ENC_EBCDIC_CP0370x00000038:
3766 /*
3767 * EBCDIC code page 037.
3768 */
3769 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp037);
3770 break;
3771
3772 case ENC_EBCDIC_CP5000x00000060:
3773 /*
3774 * EBCDIC code page 500.
3775 */
3776 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp500);
3777 break;
3778
3779 case ENC_T610x00000036:
3780 strptr = tvb_get_t61_string(scope, tvb, offset, length);
3781 break;
3782
3783 case ENC_BCD_DIGITS_0_90x00000044:
3784 /*
3785 * Packed BCD, with digits 0-9.
3786 */
3787 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3788 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3789 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt0_9_bcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3790 break;
3791
3792 case ENC_KEYPAD_ABC_TBCD0x00000046:
3793 /*
3794 * Keypad-with-a/b/c "telephony BCD" - packed BCD, with
3795 * digits 0-9 and symbols *, #, a, b, and c.
3796 */
3797 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3798 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3799 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_keypad_abc_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3800 break;
3801
3802 case ENC_KEYPAD_BC_TBCD0x00000048:
3803 /*
3804 * Keypad-with-B/C "telephony BCD" - packed BCD, with
3805 * digits 0-9 and symbols B, C, *, and #.
3806 */
3807 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3808 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3809 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_ansi_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3810 break;
3811
3812 case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
3813 strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_unpacked(scope, tvb, offset, length);
3814 break;
3815
3816 case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
3817 strptr = (uint8_t*)tvb_get_etsi_ts_102_221_annex_a_string(scope, tvb, offset, length);
3818 break;
3819
3820 case ENC_GB180300x00000050:
3821 strptr = tvb_get_gb18030_string(scope, tvb, offset, length);
3822 break;
3823
3824 case ENC_EUC_KR0x00000052:
3825 strptr = tvb_get_euc_kr_string(scope, tvb, offset, length);
3826 break;
3827
3828 case ENC_APN_STR0x00000054:
3829 strptr = tvb_get_apn_string(scope, tvb, offset, length);
3830 break;
3831
3832 case ENC_DECT_STANDARD_8BITS0x00000056:
3833 strptr = tvb_get_dect_standard_8bits_string(scope, tvb, offset, length);
3834 break;
3835
3836 case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
3837 /*
3838 * DECT standard 4bits "telephony BCD" - packed BCD, with
3839 * digits 0-9 and symbol SPACE for 0xb.
3840 */
3841 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3842 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3843 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_dect_standard_4bits_tbcd, skip_first, odd, false0);
3844 break;
3845 }
3846 return strptr;
3847}
3848
3849/*
3850 * This is like tvb_get_string_enc(), except that it handles null-padded
3851 * strings.
3852 *
3853 * Currently, string values are stored as UTF-8 null-terminated strings,
3854 * so nothing needs to be done differently for null-padded strings; we
3855 * could save a little memory by not storing the null padding.
3856 *
3857 * If we ever store string values differently, in a fashion that doesn't
3858 * involve null termination, that might change.
3859 */
3860uint8_t *
3861tvb_get_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3862 const unsigned length, const unsigned encoding)
3863{
3864 return tvb_get_string_enc(scope, tvb, offset, length, encoding);
3865}
3866
3867/*
3868 * These routines are like the above routines, except that they handle
3869 * null-terminated strings. They find the length of that string (and
3870 * throw an exception if the tvbuff ends before we find the null), and
3871 * also return through a pointer the length of the string, in bytes,
3872 * including the terminating null (the terminating null being 2 bytes
3873 * for UCS-2 and UTF-16, 4 bytes for UCS-4, and 1 byte for other
3874 * encodings).
3875 */
3876static uint8_t *
3877tvb_get_ascii_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3878{
3879 unsigned size;
3880 const uint8_t *ptr;
3881
3882 size = tvb_strsize(tvb, offset);
3883 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3884 if (lengthp)
3885 *lengthp = size;
3886 return get_ascii_string(scope, ptr, size);
3887}
3888
3889static uint8_t *
3890tvb_get_iso_646_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
3891{
3892 unsigned size;
3893 const uint8_t *ptr;
3894
3895 size = tvb_strsize(tvb, offset);
3896 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3897 if (lengthp)
3898 *lengthp = size;
3899 return get_iso_646_string(scope, ptr, size, table);
3900}
3901
3902static uint8_t *
3903tvb_get_utf_8_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3904{
3905 unsigned size;
3906 const uint8_t *ptr;
3907
3908 size = tvb_strsize(tvb, offset);
3909 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3910 if (lengthp)
3911 *lengthp = size;
3912 return get_utf_8_string(scope, ptr, size);
3913}
3914
3915static uint8_t *
3916tvb_get_stringz_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3917{
3918 unsigned size;
3919 const uint8_t *ptr;
3920
3921 size = tvb_strsize(tvb, offset);
3922 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3923 if (lengthp)
3924 *lengthp = size;
3925 return get_8859_1_string(scope, ptr, size);
3926}
3927
3928static uint8_t *
3929tvb_get_stringz_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
3930{
3931 unsigned size;
3932 const uint8_t *ptr;
3933
3934 size = tvb_strsize(tvb, offset);
3935 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3936 if (lengthp)
3937 *lengthp = size;
3938 return get_unichar2_string(scope, ptr, size, table);
3939}
3940
3941/*
3942 * Given a tvbuff and an offset, with the offset assumed to refer to
3943 * a null-terminated string, find the length of that string (and throw
3944 * an exception if the tvbuff ends before we find the null), ensure that
3945 * the TVB is flat, and return a pointer to the string (in the TVB).
3946 * Also return the length of the string (including the terminating null)
3947 * through a pointer.
3948 *
3949 * As long as we aren't using composite TVBs, this saves the cycles used
3950 * (often unnecessarily) in allocating a buffer and copying the string into
3951 * it. OTOH, the string returned isn't valid UTF-8, so it shouldn't be
3952 * added to the tree, the columns, etc., just used with various other
3953 * functions that operate on strings that don't have a tvb_ equivalent.
3954 * That's hard to enforce, which is why this is deprecated.
3955 */
3956const uint8_t *
3957tvb_get_const_stringz(tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3958{
3959 unsigned size;
3960 const uint8_t *strptr;
3961
3962 size = tvb_strsize(tvb, offset);
3963 strptr = ensure_contiguous_unsigned(tvb, offset, size);
3964 if (lengthp)
3965 *lengthp = size;
3966 return strptr;
3967}
3968
3969static char *
3970tvb_get_ucs_2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3971{
3972 unsigned size; /* Number of bytes in string */
3973 const uint8_t *ptr;
3974
3975 size = tvb_unicode_strsize(tvb, offset);
3976 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3977 if (lengthp)
3978 *lengthp = size;
3979 return (char*)get_ucs_2_string(scope, ptr, size, encoding);
3980}
3981
3982static char *
3983tvb_get_utf_16_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3984{
3985 unsigned size;
3986 const uint8_t *ptr;
3987
3988 size = tvb_unicode_strsize(tvb, offset);
3989 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3990 if (lengthp)
3991 *lengthp = size;
3992 return (char*)get_utf_16_string(scope, ptr, size, encoding);
3993}
3994
3995static char *
3996tvb_get_ucs_4_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3997{
3998 unsigned end_offset, size;
3999 gunichar uchar;
4000 const uint8_t *ptr;
4001
4002 end_offset = offset;
4003 do {
4004 /* Endianness doesn't matter when looking for null */
4005 uchar = tvb_get_ntohl(tvb, end_offset);
4006 /* Make sure we don't overflow */
4007 if (ckd_add(&end_offset, end_offset, 4)__builtin_add_overflow((end_offset), (4), (&end_offset))) {
4008 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
4009 }
4010 } while(uchar != 0);
4011 size = end_offset - offset;
4012
4013 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4014 if (lengthp)
4015 *lengthp = size;
4016 return (char*)get_ucs_4_string(scope, ptr, size, encoding);
4017}
4018
4019static uint8_t *
4020tvb_get_nonascii_unichar2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[256])
4021{
4022 unsigned size;
4023 const uint8_t *ptr;
4024
4025 size = tvb_strsize(tvb, offset);
4026 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4027 if (lengthp)
4028 *lengthp = size;
4029 return get_nonascii_unichar2_string(scope, ptr, size, table);
4030}
4031
4032static uint8_t *
4033tvb_get_t61_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4034{
4035 unsigned size;
4036 const uint8_t *ptr;
4037
4038 size = tvb_strsize(tvb, offset);
4039 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4040 if (lengthp)
4041 *lengthp = size;
4042 return get_t61_string(scope, ptr, size);
4043}
4044
4045static uint8_t *
4046tvb_get_gb18030_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4047{
4048 unsigned size;
4049 const uint8_t *ptr;
4050
4051 size = tvb_strsize(tvb, offset);
4052 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4053 if (lengthp)
4054 *lengthp = size;
4055 return get_gb18030_string(scope, ptr, size);
4056}
4057
4058static uint8_t *
4059tvb_get_euc_kr_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4060{
4061 unsigned size;
4062 const uint8_t *ptr;
4063
4064 size = tvb_strsize(tvb, offset);
4065 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4066 if (lengthp)
4067 *lengthp = size;
4068 return get_euc_kr_string(scope, ptr, size);
4069}
4070
4071static uint8_t *
4072tvb_get_dect_standard_8bits_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4073{
4074 unsigned size;
4075 const uint8_t *ptr;
4076
4077 size = tvb_strsize(tvb, offset);
4078 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4079 if (lengthp)
4080 *lengthp = size;
4081 return get_dect_standard_8bits_string(scope, ptr, size);
4082}
4083
4084uint8_t *
4085tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4086{
4087 uint8_t *strptr;
4088
4089 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4089, "tvb && tvb->initialized"
))))
;
4090
4091 switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
4092
4093 case ENC_ASCII0x00000000:
4094 default:
4095 /*
4096 * For now, we treat bogus values as meaning
4097 * "ASCII" rather than reporting an error,
4098 * for the benefit of old dissectors written
4099 * when the last argument to proto_tree_add_item()
4100 * was a bool for the byte order, not an
4101 * encoding value, and passed non-zero values
4102 * other than true to mean "little-endian".
4103 */
4104 strptr = tvb_get_ascii_stringz(scope, tvb, offset, lengthp);
4105 break;
4106
4107 case ENC_UTF_80x00000002:
4108 strptr = tvb_get_utf_8_stringz(scope, tvb, offset, lengthp);
4109 break;
4110
4111 case ENC_UTF_160x00000004:
4112 strptr = (uint8_t*)tvb_get_utf_16_stringz(scope, tvb, offset, lengthp,
4113 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4114 break;
4115
4116 case ENC_UCS_20x00000006:
4117 strptr = (uint8_t*)tvb_get_ucs_2_stringz(scope, tvb, offset, lengthp,
4118 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4119 break;
4120
4121 case ENC_UCS_40x00000008:
4122 strptr = (uint8_t*)tvb_get_ucs_4_stringz(scope, tvb, offset, lengthp,
4123 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4124 break;
4125
4126 case ENC_ISO_8859_10x0000000A:
4127 /*
4128 * ISO 8859-1 printable code point values are equal
4129 * to the equivalent Unicode code point value, so
4130 * no translation table is needed.
4131 */
4132 strptr = tvb_get_stringz_8859_1(scope, tvb, offset, lengthp);
4133 break;
4134
4135 case ENC_ISO_8859_20x0000000C:
4136 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_2);
4137 break;
4138
4139 case ENC_ISO_8859_30x0000000E:
4140 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_3);
4141 break;
4142
4143 case ENC_ISO_8859_40x00000010:
4144 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_4);
4145 break;
4146
4147 case ENC_ISO_8859_50x00000012:
4148 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_5);
4149 break;
4150
4151 case ENC_ISO_8859_60x00000014:
4152 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_6);
4153 break;
4154
4155 case ENC_ISO_8859_70x00000016:
4156 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_7);
4157 break;
4158
4159 case ENC_ISO_8859_80x00000018:
4160 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_8);
4161 break;
4162
4163 case ENC_ISO_8859_90x0000001A:
4164 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_9);
4165 break;
4166
4167 case ENC_ISO_8859_100x0000001C:
4168 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_10);
4169 break;
4170
4171 case ENC_ISO_8859_110x0000001E:
4172 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_11);
4173 break;
4174
4175 case ENC_ISO_8859_130x00000022:
4176 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_13);
4177 break;
4178
4179 case ENC_ISO_8859_140x00000024:
4180 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_14);
4181 break;
4182
4183 case ENC_ISO_8859_150x00000026:
4184 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_15);
4185 break;
4186
4187 case ENC_ISO_8859_160x00000028:
4188 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_16);
4189 break;
4190
4191 case ENC_WINDOWS_12500x0000002A:
4192 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1250);
4193 break;
4194
4195 case ENC_WINDOWS_12510x0000003C:
4196 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1251);
4197 break;
4198
4199 case ENC_WINDOWS_12520x0000003A:
4200 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1252);
4201 break;
4202
4203 case ENC_MAC_ROMAN0x00000030:
4204 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_mac_roman);
4205 break;
4206
4207 case ENC_CP4370x00000032:
4208 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp437);
4209 break;
4210
4211 case ENC_CP8550x0000003E:
4212 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp855);
4213 break;
4214
4215 case ENC_CP8660x00000040:
4216 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp866);
4217 break;
4218
4219 case ENC_ISO_646_BASIC0x00000042:
4220 strptr = tvb_get_iso_646_stringz(scope, tvb, offset, lengthp, charset_table_iso_646_basic);
4221 break;
4222
4223 case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
4224 case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
4225 case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
4226 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"
)
;
4227 break;
4228
4229 case ENC_ASCII_7BITS0x00000034:
4230 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"
)
;
4231 break;
4232
4233 case ENC_EBCDIC0x0000002E:
4234 /*
4235 * "Common" EBCDIC, covering all characters with the
4236 * same code point in all Roman-alphabet EBCDIC code
4237 * pages.
4238 */
4239 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic);
4240 break;
4241
4242 case ENC_EBCDIC_CP0370x00000038:
4243 /*
4244 * EBCDIC code page 037.
4245 */
4246 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp037);
4247 break;
4248
4249 case ENC_EBCDIC_CP5000x00000060:
4250 /*
4251 * EBCDIC code page 500.
4252 */
4253 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp500);
4254 break;
4255
4256 case ENC_T610x00000036:
4257 strptr = tvb_get_t61_stringz(scope, tvb, offset, lengthp);
4258 break;
4259
4260 case ENC_GB180300x00000050:
4261 strptr = tvb_get_gb18030_stringz(scope, tvb, offset, lengthp);
4262 break;
4263
4264 case ENC_EUC_KR0x00000052:
4265 strptr = tvb_get_euc_kr_stringz(scope, tvb, offset, lengthp);
4266 break;
4267
4268 case ENC_DECT_STANDARD_8BITS0x00000056:
4269 strptr = tvb_get_dect_standard_8bits_stringz(scope, tvb, offset, lengthp);
4270 break;
4271 }
4272
4273 return strptr;
4274}
4275
4276/* Looks for a stringz (NUL-terminated string) in tvbuff and copies
4277 * no more than bufsize number of bytes, including terminating NUL, to buffer.
4278 * Returns length of string (not including terminating NUL).
4279 * In this way, it acts like snprintf().
4280 *
4281 * bufsize MUST be greater than 0.
4282 *
4283 * This function does not otherwise throw an exception for running out of room
4284 * in the buffer or running out of remaining bytes in the tvbuffer. It will
4285 * copy as many bytes to the buffer as possible (the lesser of bufsize - 1
4286 * and the number of remaining captured bytes) and then NUL terminate the
4287 * string.
4288 *
4289 * *bytes_copied will contain the number of bytes actually copied,
4290 * including the terminating-NUL if present in the frame, but not
4291 * if it was supplied by the function instead of copied from packet data.
4292 * [Not currently used, but could be used to determine how much to advance
4293 * the offset.]
4294 */
4295static unsigned
4296_tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const unsigned offset, const unsigned bufsize, uint8_t* buffer, unsigned *bytes_copied)
4297{
4298 int exception;
4299 int stringlen;
4300 unsigned limit;
4301 unsigned len = 0;
4302
4303 /* Only read to end of tvbuff, w/o throwing exception. */
4304 exception = validate_offset_and_remaining(tvb, offset, &len);
4305 if (exception)
4306 THROW(exception)except_throw(1, (exception), ((void*)0));
4307
4308 /* There must at least be room for the terminating NUL. */
4309 DISSECTOR_ASSERT(bufsize != 0)((void) ((bufsize != 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4309, "bufsize != 0"
))))
;
4310
4311 /* If there's no room for anything else, just return the NUL. */
4312 if (bufsize == 1) {
4313 buffer[0] = 0;
4314 if (len && tvb_get_uint8(tvb, offset) == 0) {
4315 *bytes_copied = 1;
4316 } else {
4317 *bytes_copied = 0;
4318 }
4319 return 0;
4320 }
4321
4322 /* validate_offset_and_remaining() won't throw an exception if we're
4323 * looking at the byte immediately after the end of the tvbuff. */
4324 if (len == 0) {
4325 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
4326 }
4327
4328 if (len < bufsize) {
4329 limit = len;
4330 }
4331 else {
4332 limit = bufsize - 1;
4333 }
4334
4335 stringlen = tvb_strnlen(tvb, offset, limit);
4336 /* If NUL wasn't found, copy the data up to the limit and terminate */
4337 if (stringlen == -1) {
4338 tvb_memcpy(tvb, buffer, offset, limit);
4339 buffer[limit] = 0;
4340 *bytes_copied = limit;
4341 return limit;
4342 }
4343
4344 /* Copy the string to buffer */
4345 tvb_memcpy(tvb, buffer, offset, stringlen + 1);
4346 *bytes_copied = stringlen + 1;
4347 return (unsigned)stringlen;
4348}
4349
4350unsigned
4351tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const unsigned offset, const unsigned bufsize, uint8_t* buffer)
4352{
4353 unsigned bytes_copied;
4354
4355 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4355, "tvb && tvb->initialized"
))))
;
4356
4357 return _tvb_get_raw_bytes_as_stringz(tvb, offset, bufsize, buffer, &bytes_copied);
4358}
4359
4360/*
4361 * Given a tvbuff, an offset into the tvbuff, a buffer, and a buffer size,
4362 * extract as many raw bytes from the tvbuff, starting at the offset,
4363 * as 1) are available in the tvbuff and 2) will fit in the buffer, leaving
4364 * room for a terminating NUL.
4365 */
4366unsigned
4367tvb_get_raw_bytes_as_string(tvbuff_t *tvb, const unsigned offset, char *buffer, size_t bufsize)
4368{
4369 unsigned len = 0;
4370
4371 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4371, "tvb && tvb->initialized"
))))
;
4372
4373 /* There must be room for the string and the terminating NUL. */
4374 DISSECTOR_ASSERT(bufsize > 0)((void) ((bufsize > 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4374, "bufsize > 0"
))))
;
4375
4376 /* bufsize is size_t, but tvbuffers only have up to unsigned bytes */
4377 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"
, 4377, "bufsize - 1 < (2147483647 *2U +1U)"))))
;
4378
4379 len = _tvb_captured_length_remaining(tvb, offset);
4380 if (len == 0) {
4381 buffer[0] = '\0';
4382 return 0;
4383 }
4384 if (len > (bufsize - 1))
4385 len = (unsigned)(bufsize - 1);
4386
4387 /* Copy the string to buffer */
4388 tvb_memcpy(tvb, buffer, offset, len);
4389 buffer[len] = '\0';
4390 return len;
4391}
4392
4393bool_Bool
4394tvb_ascii_isprint(tvbuff_t *tvb, const unsigned offset, const unsigned length)
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 /* XXX - Perhaps this function should return false instead of throwing
4399 * an exception. */
4400 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4401
4402 for (unsigned i = 0; i < length; i++, buf++)
4403 if (!g_ascii_isprint(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_PRINT) != 0))
4404 return false0;
4405
4406 return true1;
4407}
4408
4409bool_Bool
4410tvb_ascii_isprint_remaining(tvbuff_t *tvb, const unsigned offset)
4411{
4412 int exception;
4413 unsigned length;
4414
4415 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4415, "tvb && tvb->initialized"
))))
;
4416
4417 exception = validate_offset_and_remaining(tvb, offset, &length);
4418 if (exception)
4419 THROW(exception)except_throw(1, (exception), ((void*)0));
4420
4421 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4422
4423 for (unsigned i = 0; i < length; i++, buf++)
4424 if (!g_ascii_isprint(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_PRINT) != 0))
4425 return false0;
4426
4427 return true1;
4428}
4429
4430bool_Bool
4431tvb_utf_8_isprint(tvbuff_t *tvb, const unsigned offset, const unsigned length)
4432{
4433 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4433, "tvb && tvb->initialized"
))))
;
4434
4435 /* XXX - Perhaps this function should return false instead of throwing
4436 * an exception. */
4437 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4438
4439 return isprint_utf8_string((const char*)buf, length);
4440}
4441
4442bool_Bool
4443tvb_utf_8_isprint_remaining(tvbuff_t *tvb, const unsigned offset)
4444{
4445 int exception;
4446 unsigned length;
4447
4448 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4448, "tvb && tvb->initialized"
))))
;
4449
4450 exception = validate_offset_and_remaining(tvb, offset, &length);
4451 if (exception)
4452 THROW(exception)except_throw(1, (exception), ((void*)0));
4453
4454 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4455
4456 return isprint_utf8_string((const char*)buf, length);
4457}
4458
4459bool_Bool
4460tvb_ascii_isdigit(tvbuff_t *tvb, const unsigned offset, const unsigned length)
4461{
4462 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4462, "tvb && tvb->initialized"
))))
;
4463
4464 /* XXX - Perhaps this function should return false instead of throwing
4465 * an exception. */
4466 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4467
4468 for (unsigned i = 0; i < length; i++, buf++)
4469 if (!g_ascii_isdigit(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_DIGIT) != 0))
4470 return false0;
4471
4472 return true1;
4473}
4474
4475static ws_mempbrk_pattern pbrk_crlf;
4476
4477static bool_Bool
4478_tvb_find_line_end_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, unsigned *linelen, unsigned *next_offset)
4479{
4480 static bool_Bool compiled = false0;
4481 unsigned eob_offset;
4482 unsigned eol_offset;
4483 unsigned char found_needle = 0;
4484
4485 if (!compiled) {
4486 ws_mempbrk_compile(&pbrk_crlf, "\r\n");
4487 compiled = true1;
4488 }
4489
4490 eob_offset = offset + limit;
4491
4492 /*
4493 * Look either for a CR or an LF.
4494 */
4495 if (!_tvb_ws_mempbrk_uint8_length(tvb, offset, limit, &pbrk_crlf, &eol_offset, &found_needle)) {
4496 /*
4497 * No CR or LF - line is presumably continued in next packet.
4498 */
4499 /*
4500 * Pretend the line runs to the end of the tvbuff.
4501 */
4502 if (linelen)
4503 *linelen = eob_offset - offset;
4504 if (next_offset)
4505 *next_offset = eob_offset;
4506 /*
4507 * Tell our caller we saw no EOL, so they can try to
4508 * desegment and get the entire line into one tvbuff.
4509 */
4510 return false0;
4511 } else {
4512 /*
4513 * Find the number of bytes between the starting offset
4514 * and the CR or LF.
4515 */
4516 if (linelen)
4517 *linelen = eol_offset - offset;
4518
4519 /*
4520 * Is it a CR?
4521 */
4522 if (found_needle == '\r') {
4523 /*
4524 * Yes - is it followed by an LF?
4525 */
4526 if (eol_offset + 1 >= eob_offset) {
4527 /*
4528 * Dunno - the next byte isn't in this
4529 * tvbuff.
4530 */
4531 if (next_offset)
4532 *next_offset = eob_offset;
4533 /*
4534 * We'll return false, although that
4535 * runs the risk that if the line
4536 * really *is* terminated with a CR,
4537 * we won't properly dissect this
4538 * tvbuff.
4539 *
4540 * It's probably more likely that
4541 * the line ends with CR-LF than
4542 * that it ends with CR by itself.
4543 *
4544 * XXX - Return a third value?
4545 */
4546 return false0;
4547 } else {
4548 /*
4549 * Well, we can at least look at the next
4550 * byte.
4551 */
4552 if (tvb_get_uint8(tvb, eol_offset + 1) == '\n') {
4553 /*
4554 * It's an LF; skip over the CR.
4555 */
4556 eol_offset++;
4557 }
4558 }
4559 }
4560
4561 /*
4562 * Return the offset of the character after the last
4563 * character in the line, skipping over the last character
4564 * in the line terminator.
4565 */
4566 if (next_offset)
4567 *next_offset = eol_offset + 1;
4568 }
4569 return true1;
4570}
4571
4572/*
4573 * Given a tvbuff, an offset into the tvbuff, and a length that starts
4574 * at that offset (which may be -1 for "all the way to the end of the
4575 * tvbuff"), find the end of the (putative) line that starts at the
4576 * specified offset in the tvbuff, going no further than the specified
4577 * length.
4578 *
4579 * Return the length of the line (not counting the line terminator at
4580 * the end), or, if we don't find a line terminator:
4581 *
4582 * if "desegment" is true, return -1;
4583 *
4584 * if "desegment" is false, return the amount of data remaining in
4585 * the buffer.
4586 *
4587 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4588 * character past the line terminator, or past the end of the buffer if
4589 * we don't find a line terminator. (It's not set if we return -1.)
4590 */
4591int
4592tvb_find_line_end(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset, const bool_Bool desegment)
4593{
4594 unsigned linelen;
4595 unsigned abs_next_offset;
4596 unsigned limit;
4597 int exception;
4598
4599 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4599, "tvb && tvb->initialized"
))))
;
4600
4601 exception = validate_offset_and_remaining(tvb, offset, &limit);
4602 if (exception)
4603 THROW(exception)except_throw(1, (exception), ((void*)0));
4604
4605 /* Only search to end of tvbuff, w/o throwing exception. */
4606 if (len >= 0 && limit > (unsigned) len) {
4607 /* Maximum length doesn't go past end of tvbuff; search
4608 to that value. */
4609 limit = (unsigned) len;
4610 }
4611
4612 if (!_tvb_find_line_end_length(tvb, offset, limit, &linelen, &abs_next_offset) && desegment) {
4613 return -1;
4614 }
4615 if (next_offset) {
4616 *next_offset = (int)abs_next_offset;
4617 }
4618 return (int)linelen;
4619}
4620
4621bool_Bool
4622tvb_find_line_end_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *linelen, unsigned *next_offset)
4623{
4624 unsigned limit;
4625 int exception;
4626
4627 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4627, "tvb && tvb->initialized"
))))
;
4628
4629 exception = validate_offset_and_remaining(tvb, offset, &limit);
4630 if (exception)
4631 THROW(exception)except_throw(1, (exception), ((void*)0));
4632
4633 return _tvb_find_line_end_length(tvb, offset, limit, linelen, next_offset);
4634}
4635
4636bool_Bool
4637tvb_find_line_end_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *linelen, unsigned *next_offset)
4638{
4639 unsigned limit;
4640 int exception;
4641
4642 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4642, "tvb && tvb->initialized"
))))
;
4643
4644 exception = validate_offset_and_remaining(tvb, offset, &limit);
4645 if (exception)
4646 THROW(exception)except_throw(1, (exception), ((void*)0));
4647
4648 /* Only search to end of tvbuff, w/o throwing exception. */
4649 if (limit > maxlength) {
4650 /* Maximum length doesn't go past end of tvbuff; search
4651 to that value. */
4652 limit = maxlength;
4653 }
4654
4655 return _tvb_find_line_end_length(tvb, offset, limit, linelen, next_offset);
4656}
4657
4658static ws_mempbrk_pattern pbrk_crlf_dquote;
4659
4660static bool_Bool
4661_tvb_find_line_end_unquoted_length(tvbuff_t *tvb, const unsigned offset, unsigned limit, unsigned *linelen, unsigned *next_offset)
4662{
4663 unsigned cur_offset, char_offset;
4664 bool_Bool is_quoted;
4665 unsigned char c = 0;
4666 unsigned eob_offset;
4667 static bool_Bool compiled = false0;
4668 unsigned len;
4669 bool_Bool found;
4670
4671 if (!compiled) {
4672 ws_mempbrk_compile(&pbrk_crlf_dquote, "\r\n\"");
4673 compiled = true1;
4674 }
4675
4676 eob_offset = offset + limit;
4677
4678 cur_offset = offset;
4679 is_quoted = false0;
4680 for (;;) {
4681 len = limit - (cur_offset - offset);
4682 /*
4683 * Is this part of the string quoted?
4684 */
4685 if (is_quoted) {
4686 /*
4687 * Yes - look only for the terminating quote.
4688 */
4689 found = _tvb_find_uint8_length(tvb, cur_offset, len, '"', &char_offset);
4690 } else {
4691 /*
4692 * Look either for a CR, an LF, or a '"'.
4693 */
4694 found = _tvb_ws_mempbrk_uint8_length(tvb, cur_offset, len, &pbrk_crlf_dquote, &char_offset, &c);
4695 }
4696 if (!found) {
4697 /*
4698 * Not found - line is presumably continued in
4699 * next packet.
4700 * We pretend the line runs to the end of the tvbuff.
4701 */
4702 if (linelen)
4703 *linelen = eob_offset - offset;
4704 if (next_offset)
4705 *next_offset = eob_offset;
4706 break;
4707 }
4708
4709 if (is_quoted) {
4710 /*
4711 * We're processing a quoted string.
4712 * We only looked for ", so we know it's a ";
4713 * as we're processing a quoted string, it's a
4714 * closing quote.
4715 */
4716 is_quoted = false0;
4717 } else {
4718 /*
4719 * OK, what is it?
4720 */
4721 if (c == '"') {
4722 /*
4723 * Un-quoted "; it begins a quoted
4724 * string.
4725 */
4726 is_quoted = true1;
4727 } else {
4728 /*
4729 * It's a CR or LF; we've found a line
4730 * terminator.
4731 *
4732 * Find the number of bytes between the
4733 * starting offset and the CR or LF.
4734 */
4735 if (linelen)
4736 *linelen = char_offset - offset;
4737
4738 /*
4739 * Is it a CR?
4740 */
4741 if (c == '\r') {
4742 /*
4743 * Yes; is it followed by an LF?
4744 */
4745 if (char_offset + 1 < eob_offset &&
4746 tvb_get_uint8(tvb, char_offset + 1)
4747 == '\n') {
4748 /*
4749 * Yes; skip over the CR.
4750 */
4751 char_offset++;
4752 }
4753 }
4754
4755 /*
4756 * Return the offset of the character after
4757 * the last character in the line, skipping
4758 * over the last character in the line
4759 * terminator, and quit.
4760 */
4761 if (next_offset)
4762 *next_offset = char_offset + 1;
4763 break;
4764 }
4765 }
4766
4767 /*
4768 * Step past the character we found.
4769 */
4770 cur_offset = char_offset + 1;
4771 if (cur_offset >= eob_offset) {
4772 /*
4773 * The character we found was the last character
4774 * in the tvbuff - line is presumably continued in
4775 * next packet.
4776 * We pretend the line runs to the end of the tvbuff.
4777 */
4778 if (linelen)
4779 *linelen = eob_offset - offset;
4780 if (next_offset)
4781 *next_offset = eob_offset;
4782 break;
4783 }
4784 }
4785 return found;
4786}
4787
4788/*
4789 * Given a tvbuff, an offset into the tvbuff, and a length that starts
4790 * at that offset (which may be -1 for "all the way to the end of the
4791 * tvbuff"), find the end of the (putative) line that starts at the
4792 * specified offset in the tvbuff, going no further than the specified
4793 * length.
4794 *
4795 * However, treat quoted strings inside the buffer specially - don't
4796 * treat newlines in quoted strings as line terminators.
4797 *
4798 * Return the length of the line (not counting the line terminator at
4799 * the end), or the amount of data remaining in the buffer if we don't
4800 * find a line terminator.
4801 *
4802 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4803 * character past the line terminator, or past the end of the buffer if
4804 * we don't find a line terminator.
4805 */
4806int
4807tvb_find_line_end_unquoted(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset)
4808{
4809 unsigned linelen;
4810 unsigned abs_next_offset;
4811 unsigned limit;
4812 int exception;
4813
4814 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4814, "tvb && tvb->initialized"
))))
;
4815
4816 exception = validate_offset_and_remaining(tvb, offset, &limit);
4817 if (exception)
4818 THROW(exception)except_throw(1, (exception), ((void*)0));
4819
4820 /* Only search to end of tvbuff, w/o throwing exception. */
4821 if (len >= 0 && limit > (unsigned) len) {
4822 /* Maximum length doesn't go past end of tvbuff; search
4823 to that value. */
4824 limit = (unsigned) len;
4825 }
4826
4827 _tvb_find_line_end_unquoted_length(tvb, offset, limit, &linelen, &abs_next_offset);
4828 if (next_offset) {
4829 *next_offset = (int)abs_next_offset;
4830 }
4831 return (int)linelen;
4832}
4833
4834bool_Bool
4835tvb_find_line_end_unquoted_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *linelen, unsigned *next_offset)
4836{
4837 unsigned limit;
4838 int exception;
4839
4840 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4840, "tvb && tvb->initialized"
))))
;
4841
4842 exception = validate_offset_and_remaining(tvb, offset, &limit);
4843 if (exception)
4844 THROW(exception)except_throw(1, (exception), ((void*)0));
4845
4846 return _tvb_find_line_end_unquoted_length(tvb, offset, limit, linelen, next_offset);
4847}
4848
4849bool_Bool
4850tvb_find_line_end_unquoted_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *linelen, unsigned *next_offset)
4851{
4852 unsigned limit;
4853 int exception;
4854
4855 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4855, "tvb && tvb->initialized"
))))
;
4856
4857 exception = validate_offset_and_remaining(tvb, offset, &limit);
4858 if (exception)
4859 THROW(exception)except_throw(1, (exception), ((void*)0));
4860
4861 /* Only search to end of tvbuff, w/o throwing exception. */
4862 if (limit > maxlength) {
4863 /* Maximum length doesn't go past end of tvbuff; search
4864 to that value. */
4865 limit = maxlength;
4866 }
4867
4868 return _tvb_find_line_end_unquoted_length(tvb, offset, limit, linelen, next_offset);
4869}
4870
4871/*
4872 * Copied from the mgcp dissector. (This function should be moved to /epan )
4873 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
4874 * character following offset or offset + maxlength -1 whichever
4875 * is smaller.
4876 *
4877 * Parameters:
4878 * tvb - The tvbuff in which we are skipping whitespace.
4879 * offset - The offset in tvb from which we begin trying to skip whitespace.
4880 * maxlength - The maximum distance from offset that we may try to skip
4881 * whitespace.
4882 *
4883 * Returns: The position in tvb of the first non-whitespace
4884 * character following offset or offset + maxlength -1 whichever
4885 * is smaller.
4886 */
4887unsigned
4888tvb_skip_wsp(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
4889{
4890 unsigned counter;
4891 unsigned end, tvb_len;
4892 uint8_t tempchar;
4893
4894 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4894, "tvb && tvb->initialized"
))))
;
4895
4896 /* Get the length remaining */
4897 /*tvb_len = tvb_captured_length(tvb);*/
4898 tvb_len = tvb->length;
4899
4900 if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
4901 end = tvb_len;
4902 }
4903
4904 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
4905 /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
4906 * slightly faster but also tests for vertical tab and form feed. */
4907 for (counter = offset;
4908 counter < end &&
4909 ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
4910 tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
4911 counter++);
4912
4913 return counter;
4914}
4915
4916unsigned
4917tvb_skip_wsp_return(tvbuff_t *tvb, const unsigned offset)
4918{
4919 unsigned counter;
4920 uint8_t tempchar;
4921
4922 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4922, "tvb && tvb->initialized"
))))
;
4923
4924 /* XXX - DISSECTOR_ASSERT(offset > 0) and then subtract 1 from offset?
4925 * The way this is used the caller almost always wants to subtract one
4926 * from the offset of a non WSP separator, and they might forget to do
4927 * so and then this function return the offset past the separator. */
4928
4929 /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
4930 * slightly faster but also tests for vertical tab and form feed. */
4931 for (counter = offset; counter > 0 &&
4932 ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
4933 tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
4934 counter++;
4935
4936 return counter;
4937}
4938
4939unsigned
4940tvb_skip_uint8(tvbuff_t *tvb, unsigned offset, const unsigned maxlength, const uint8_t ch)
4941{
4942 unsigned end, tvb_len;
4943
4944 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4944, "tvb && tvb->initialized"
))))
;
4945
4946 /* Get the length remaining */
4947 /*tvb_len = tvb_captured_length(tvb);*/
4948 tvb_len = tvb->length;
4949
4950 if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
4951 end = tvb_len;
4952 }
4953
4954 while (offset < end) {
4955 uint8_t tempch = tvb_get_uint8(tvb, offset);
4956
4957 if (tempch != ch)
4958 break;
4959 offset++;
4960 }
4961
4962 return offset;
4963}
4964
4965static ws_mempbrk_pattern pbrk_whitespace;
4966
4967static bool_Bool
4968_tvb_get_token_len_length(tvbuff_t *tvb, const unsigned offset, unsigned limit, unsigned *tokenlen, unsigned *next_offset)
4969{
4970 unsigned eot_offset;
4971 unsigned char found_needle = 0;
4972 static bool_Bool compiled = false0;
4973
4974 if (!compiled) {
4975 ws_mempbrk_compile(&pbrk_whitespace, " \r\n");
4976 compiled = true1;
4977 }
4978
4979 /*
4980 * Look either for a space, CR, or LF.
4981 */
4982 if (!_tvb_ws_mempbrk_uint8_length(tvb, offset, limit, &pbrk_whitespace, &eot_offset, &found_needle)) {
4983 /*
4984 * No space, CR or LF - token is presumably continued in next packet.
4985 */
4986 /*
4987 * Pretend the token runs to the end of the tvbuff.
4988 */
4989 if (tokenlen)
4990 *tokenlen = eot_offset - offset;
4991 if (next_offset)
4992 *next_offset = eot_offset;
4993 /*
4994 * Tell our caller we saw no whitespace, so they can
4995 * try to desegment and get the entire line
4996 * into one tvbuff.
4997 */
4998 return false0;
4999 }
5000
5001 /*
5002 * Find the number of bytes between the starting offset
5003 * and the space, CR or LF.
5004 */
5005 if (tokenlen)
5006 *tokenlen = eot_offset - offset;
5007
5008 /*
5009 * Return the offset of the character after the token delimiter,
5010 * skipping over the last character in the separator.
5011 *
5012 * XXX - get_token_len() from strutil.h returns the start offset of
5013 * the next token by skipping trailing spaces (but not spaces that
5014 * follow a CR or LF, only consecutive spaces). Should we align
5015 * the two functions? Most dissectors want to skip extra spaces,
5016 * and while the dissector _can_ follow up with tvb_skip_wsp, this
5017 * probably causes dissectors to use tvb_get_ptr + get_token_len,
5018 * which we want to discourage. OTOH, IMAP, which uses this, says
5019 * "in all cases, SP refers to exactly one space. It is NOT permitted
5020 * to substitute TAB, insert additional spaces, or otherwise treat
5021 * SP as being equivalent to linear whitespace (LWSP)."
5022 * https://www.rfc-editor.org/rfc/rfc9051.html#name-formal-syntax
5023 *
5024 * XXX - skip over CR-LF as a unit like tvb_find_line_end()?
5025 * get_token_len() doesn't, probably because most dissectors have
5026 * already found the line end before, but it probably makes sense
5027 * to do and unlike above it's unlikely it would break any protocol
5028 * (and might even fix some.)
5029 */
5030 if (next_offset)
5031 *next_offset = eot_offset + 1;
5032
5033 return true1;
5034}
5035
5036int tvb_get_token_len(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset, const bool_Bool desegment)
5037{
5038 unsigned tokenlen;
5039 unsigned abs_next_offset;
5040 unsigned limit;
5041 int exception;
5042
5043 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5043, "tvb && tvb->initialized"
))))
;
5044
5045 exception = validate_offset_and_remaining(tvb, offset, &limit);
5046 if (exception)
5047 THROW(exception)except_throw(1, (exception), ((void*)0));
5048
5049 /* Only search to end of tvbuff, w/o throwing exception. */
5050 if (len >= 0 && limit > (unsigned) len) {
5051 /* Maximum length doesn't go past end of tvbuff; search
5052 to that value. */
5053 limit = (unsigned) len;
5054 }
5055
5056 if (!_tvb_get_token_len_length(tvb, offset, limit, &tokenlen, &abs_next_offset) && desegment) {
5057 return -1;
5058 }
5059 if (next_offset) {
5060 *next_offset = (int)abs_next_offset;
5061 }
5062 return (int)tokenlen;
5063}
5064
5065bool_Bool
5066tvb_get_token_len_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *tokenlen, unsigned *next_offset)
5067{
5068 unsigned limit;
5069 int exception;
5070
5071 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5071, "tvb && tvb->initialized"
))))
;
5072
5073 exception = validate_offset_and_remaining(tvb, offset, &limit);
5074 if (exception)
5075 THROW(exception)except_throw(1, (exception), ((void*)0));
5076
5077 return _tvb_get_token_len_length(tvb, offset, limit, tokenlen, next_offset);
5078}
5079
5080bool_Bool
5081tvb_get_token_len_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *tokenlen, unsigned *next_offset)
5082{
5083 unsigned limit;
5084 int exception;
5085
5086 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5086, "tvb && tvb->initialized"
))))
;
5087
5088 exception = validate_offset_and_remaining(tvb, offset, &limit);
5089 if (exception)
5090 THROW(exception)except_throw(1, (exception), ((void*)0));
5091
5092 /* Only search to end of tvbuff, w/o throwing exception. */
5093 if (limit > maxlength) {
5094 /* Maximum length doesn't go past end of tvbuff; search
5095 to that value. */
5096 limit = maxlength;
5097 }
5098
5099 return _tvb_get_token_len_length(tvb, offset, limit, tokenlen, next_offset);
5100}
5101
5102/*
5103 * Format a bunch of data from a tvbuff as bytes, returning a pointer
5104 * to the string with the formatted data, with "punct" as a byte
5105 * separator.
5106 */
5107char *
5108tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned len, const char punct)
5109{
5110 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)
;
5111}
5112
5113/*
5114 * Given a wmem scope, a tvbuff, an offset, a length, an input digit
5115 * set, and a boolean indicator, fetch BCD-encoded digits from a
5116 * tvbuff starting from either the low or high half byte of the
5117 * first byte depending on the boolean indicator (true means "start
5118 * with the high half byte, ignoring the low half byte", and false
5119 * means "start with the low half byte and proceed to the high half
5120 * byte), formating the digits into characters according to the
5121 * input digit set, and return a pointer to a UTF-8 string, allocated
5122 * using the wmem scope. A nibble of 0xf is considered a 'filler'
5123 * and will end the conversion. Similarly if odd is set the last
5124 * high nibble will be omitted. (Note that if both skip_first and
5125 * odd are true, then both the first and last semi-octet are skipped,
5126 * i.e. an even number of nibbles are considered.)
5127 */
5128char *
5129tvb_get_bcd_string(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, int len, const dgt_set_t *dgt, bool_Bool skip_first, bool_Bool odd, bool_Bool bigendian)
5130{
5131 const uint8_t *ptr;
5132 int i = 0;
5133 char *digit_str;
5134 uint8_t octet, nibble;
5135
5136 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5136, "tvb && tvb->initialized"
))))
;
5137
5138 if (len == -1) {
5139 /*
5140 * Run to the end of the captured data.
5141 *
5142 * XXX - captured, or total?
5143 */
5144 /*length = tvb_captured_length(tvb);*/
5145 len = tvb->length;
5146 if (len < offset) {
5147 return (char *)"";
5148 }
5149 len -= offset;
5150 }
5151
5152 ptr = ensure_contiguous(tvb, offset, len);
5153
5154 /*
5155 * XXX - map illegal digits (digits that map to 0) to REPLACEMENT
5156 * CHARACTER, and have all the tables in epan/tvbuff.c use 0 rather
5157 * than '?'?
5158 */
5159 digit_str = (char *)wmem_alloc(scope, len*2 + 1);
5160
5161 while (len > 0) {
5162 octet = *ptr;
5163 if (!skip_first) {
5164 if (bigendian) {
5165 nibble = (octet >> 4) & 0x0f;
5166 } else {
5167 nibble = octet & 0x0f;
5168 }
5169 if (nibble == 0x0f) {
5170 /*
5171 * Stop digit.
5172 */
5173 break;
5174 }
5175 digit_str[i] = dgt->out[nibble];
5176 i++;
5177 }
5178 skip_first = false0;
5179
5180 /*
5181 * unpack second value in byte
5182 */
5183 if (bigendian) {
5184 nibble = octet & 0x0f;
5185 } else {
5186 nibble = octet >> 4;
5187 }
5188
5189 if (nibble == 0x0f) {
5190 /*
5191 * This is the stop digit or a filler digit. Ignore
5192 * it.
5193 */
5194 break;
5195 }
5196 if ((len == 1) && (odd == true1 )){
5197 /* Last octet, skip last high nibble in case of odd number of digits */
5198 break;
5199 }
5200 digit_str[i] = dgt->out[nibble];
5201 i++;
5202
5203 ptr++;
5204 len--;
5205 }
5206 digit_str[i] = '\0';
5207 return digit_str;
5208}
5209
5210/* XXXX Fix me - needs odd indicator added */
5211const char *
5212tvb_bcd_dig_to_str(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, const int len, const dgt_set_t *dgt, bool_Bool skip_first)
5213{
5214 if (!dgt)
5215 dgt = &Dgt0_9_bcd;
5216
5217 return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, false0);
5218}
5219
5220const char *
5221tvb_bcd_dig_to_str_be(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, const int len, const dgt_set_t *dgt, bool_Bool skip_first)
5222{
5223 if (!dgt)
5224 dgt = &Dgt0_9_bcd;
5225
5226 return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, true1);
5227}
5228
5229/*
5230 * Format a bunch of data from a tvbuff as bytes, returning a pointer
5231 * to the string with the formatted data.
5232 */
5233char *tvb_bytes_to_str(wmem_allocator_t *allocator, tvbuff_t *tvb,
5234 const int offset, const int len)
5235{
5236 DISSECTOR_ASSERT(len >= 0)((void) ((len >= 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5236, "len >= 0"
))))
;
5237 return bytes_to_str(allocator, ensure_contiguous(tvb, offset, len), len)bytes_to_str_maxlen(allocator, ensure_contiguous(tvb, offset,
len), len, 36)
;
5238}
5239
5240/* Find a needle tvbuff within a haystack tvbuff. */
5241int
5242tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const int haystack_offset)
5243{
5244 unsigned haystack_abs_offset = 0, haystack_abs_length = 0;
5245 const uint8_t *haystack_data;
5246 const uint8_t *needle_data;
5247 const unsigned needle_len = needle_tvb->length;
5248 const uint8_t *location;
5249
5250 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", 5250, "haystack_tvb && haystack_tvb->initialized"
))))
;
5251
5252 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
5253 return -1;
5254 }
5255
5256 /* Get pointers to the tvbuffs' data. */
5257 haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
5258 needle_data = ensure_contiguous(needle_tvb, 0, -1);
5259
5260 check_offset_length(haystack_tvb, haystack_offset, -1,
5261 &haystack_abs_offset, &haystack_abs_length);
5262
5263 location = ws_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
5264 needle_data, needle_len);
5265
5266 if (location) {
5267 return (int) (location - haystack_data);
5268 }
5269
5270 return -1;
5271}
5272
5273unsigned
5274tvb_raw_offset(tvbuff_t *tvb)
5275{
5276 if (!(tvb->flags & TVBUFF_RAW_OFFSET0x00000002)) {
5277 tvb->raw_offset = tvb_offset_from_real_beginning(tvb);
5278 tvb->flags |= TVBUFF_RAW_OFFSET0x00000002;
5279 }
5280 return tvb->raw_offset;
5281}
5282
5283void
5284tvb_set_fragment(tvbuff_t *tvb)
5285{
5286 tvb->flags |= TVBUFF_FRAGMENT0x00000001;
5287}
5288
5289struct tvbuff *
5290tvb_get_ds_tvb(tvbuff_t *tvb)
5291{
5292 return(tvb->ds_tvb);
5293}
5294
5295unsigned
5296tvb_get_varint(tvbuff_t *tvb, unsigned offset, unsigned maxlen, uint64_t *value, const unsigned encoding)
5297{
5298 *value = 0;
5299
5300 switch (encoding & ENC_VARINT_MASK(0x00000002|0x00000004|0x00000008|0x00000010)) {
5301 case ENC_VARINT_PROTOBUF0x00000002:
5302 {
5303 unsigned i;
5304 uint64_t b; /* current byte */
5305
5306 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5307 b = tvb_get_uint8(tvb, offset++);
5308 *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5309
5310 if (b < 0x80) {
5311 /* end successfully because of last byte's msb(most significant bit) is zero */
5312 return i + 1;
5313 }
5314 }
5315 break;
5316 }
5317
5318 case ENC_VARINT_ZIGZAG0x00000008:
5319 {
5320 unsigned i;
5321 uint64_t b; /* current byte */
5322
5323 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5324 b = tvb_get_uint8(tvb, offset++);
5325 *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5326
5327 if (b < 0x80) {
5328 /* end successfully because of last byte's msb(most significant bit) is zero */
5329 *value = (*value >> 1) ^ ((*value & 1) ? -1 : 0);
5330 return i + 1;
5331 }
5332 }
5333 break;
5334 }
5335
5336 case ENC_VARINT_SDNV0x00000010:
5337 {
5338 /* Decodes similar to protobuf but in MSByte order */
5339 unsigned i;
5340 uint64_t b; /* current byte */
5341
5342 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5343 b = tvb_get_uint8(tvb, offset++);
5344 if ((i == 9) && (*value >= UINT64_C(1)1UL<<(64-7))) {
5345 // guaranteed overflow, not valid SDNV
5346 return 0;
5347 }
5348 *value <<= 7;
5349 *value |= (b & 0x7F); /* add lower 7 bits to val */
5350
5351 if (b < 0x80) {
5352 /* end successfully because of last byte's msb(most significant bit) is zero */
5353 return i + 1;
5354 }
5355 }
5356 break;
5357 }
5358
5359 case ENC_VARINT_QUIC0x00000004:
5360 {
5361 /* calculate variable length */
5362 *value = tvb_get_uint8(tvb, offset);
5363 switch((*value) >> 6) {
5364 case 0: /* 0b00 => 1 byte length (6 bits Usable) */
5365 (*value) &= 0x3F;
5366 return 1;
5367 case 1: /* 0b01 => 2 bytes length (14 bits Usable) */
5368 *value = tvb_get_ntohs(tvb, offset) & 0x3FFF;
5369 return 2;
5370 case 2: /* 0b10 => 4 bytes length (30 bits Usable) */
5371 *value = tvb_get_ntohl(tvb, offset) & 0x3FFFFFFF;
5372 return 4;
5373 case 3: /* 0b11 => 8 bytes length (62 bits Usable) */
5374 *value = tvb_get_ntoh64(tvb, offset) & UINT64_C(0x3FFFFFFFFFFFFFFF)0x3FFFFFFFFFFFFFFFUL;
5375 return 8;
5376 default: /* No Possible */
5377 ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/tvbuff.c", 5377,
__func__, "assertion \"not reached\" failed")
;
5378 break;
5379 }
5380 break;
5381 }
5382
5383 default:
5384 DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\""
, "epan/tvbuff.c", 5384))
;
5385 }
5386
5387 return 0; /* 10 bytes scanned, but no bytes' msb is zero */
5388}
5389
5390/*
5391 * Editor modelines - https://www.wireshark.org/tools/modelines.html
5392 *
5393 * Local variables:
5394 * c-basic-offset: 8
5395 * tab-width: 8
5396 * indent-tabs-mode: t
5397 * End:
5398 *
5399 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
5400 * :indentSize=8:tabSize=8:noTabs=false:
5401 */