Bug Summary

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