Bug Summary

File:builds/wireshark/wireshark/epan/tvbuff.c
Warning:line 579, 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/lua5.4 -isystem /usr/include/libxml2 -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-25-100333-3641-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"
))))
;
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) {
485 datalen++;
486 }
487
488 /* already aligned -> shortcut */
489 if (((left == 0) && (remaining_bits == 0)) || datalen == 0) {
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) {
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);
503
504 /* shift tvb data bit_offset bits to the left */
505 for (i = 0; i < datalen; i++)
506 buf[i] = (data[i] << left) | (data[i+1] >> right);
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);
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"
))))
;
4
Assuming 'tvb' is non-null
5
Assuming field 'initialized' is true
6
'?' condition is true
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) {
7
Assuming 'remaining_bits' is 0
546 dst_len++;
547 }
548
549 /* already aligned -> shortcut */
550 if (((right == 0) && (remaining_bits == 0)) || dst_len == 0) {
8
Assuming 'right' is not equal to 0
9
Assuming 'dst_len' is not equal to 0
10
Taking false branch
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) {
11
Taking false branch
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);
12
Memory is allocated
566
567 for (i = 0; i < (dst_len - 1); i++)
13
Assuming the condition is false
14
Loop condition is false. Execution continues on line 571
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
14.1
'src_len' is <= 'dst_len'
> dst_len)
15
Taking false branch
574 buf[i] |= (data[i+1] << left);
575 /* Preserve only remaining bits in last octet if not multiple of 8 */
576 if (remaining_bits
15.1
'remaining_bits' is 0
)
16
Taking false branch
577 buf[i] &= ((1 << remaining_bits) - 1);
578
579 sub_tvb = tvb_new_child_real_data(tvb, buf, dst_len, dst_len);
17
Potential leak of memory pointed to by 'buf'
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
2034bool_Bool
2035tvb_get_string_uint16(tvbuff_t *tvb, const unsigned offset, const unsigned length,
2036 const unsigned encoding, uint16_t *value, unsigned *endoff)
2037{
2038 const uint8_t *ptr;
2039 const uint8_t *endptr;
2040 const uint8_t **endptrptr = endoff ? &endptr : NULL((void*)0);
2041 bool_Bool success;
2042
2043 validate_single_byte_ascii_encoding(encoding);
2044
2045 ptr = ensure_contiguous_unsigned(tvb, offset, length);
2046
2047 switch (encoding & ENC_STRING0x07000000) {
2048 case ENC_STR_HEX0x02000000:
2049 success = ws_hexbuftou16(ptr, length, endptrptr, value);
2050 break;
2051 case ENC_STR_DEC0x04000000:
2052 success = ws_buftou16(ptr, length, endptrptr, value);
2053 break;
2054 case ENC_STR_NUM0x01000000:
2055 default:
2056 success = ws_basebuftou16(ptr, length, endptrptr, value, 0);
2057 }
2058
2059 if (endoff) {
2060 // 0 <= endptr - ptr <= length
2061 *endoff = offset + (uint32_t)(endptr - ptr);
2062 }
2063
2064 return success;
2065}
2066
2067bool_Bool
2068tvb_get_string_uint8(tvbuff_t *tvb, const unsigned offset, const unsigned length,
2069 const unsigned encoding, uint8_t *value, unsigned *endoff)
2070{
2071 const uint8_t *ptr;
2072 const uint8_t *endptr;
2073 const uint8_t **endptrptr = endoff ? &endptr : NULL((void*)0);
2074 bool_Bool success;
2075
2076 validate_single_byte_ascii_encoding(encoding);
2077
2078 ptr = ensure_contiguous_unsigned(tvb, offset, length);
2079
2080 switch (encoding & ENC_STRING0x07000000) {
2081 case ENC_STR_HEX0x02000000:
2082 success = ws_hexbuftou8(ptr, length, endptrptr, value);
2083 break;
2084 case ENC_STR_DEC0x04000000:
2085 success = ws_buftou8(ptr, length, endptrptr, value);
2086 break;
2087 case ENC_STR_NUM0x01000000:
2088 default:
2089 success = ws_basebuftou8(ptr, length, endptrptr, value, 0);
2090 }
2091
2092 if (endoff) {
2093 // 0 <= endptr - ptr <= length
2094 *endoff = offset + (uint32_t)(endptr - ptr);
2095 }
2096
2097 return success;
2098}
2099
2100/*
2101 * Is the character a WSP character, as per RFC 5234? (space or tab).
2102 */
2103#define IS_WSP(c)((c) == ' ' || (c) == '\t') ((c) == ' ' || (c) == '\t')
2104
2105/* support hex-encoded time values? */
2106nstime_t*
2107tvb_get_string_time(tvbuff_t *tvb, const unsigned offset, const unsigned length,
2108 const unsigned encoding, nstime_t *ns, unsigned *endoff)
2109{
2110 char *begin;
2111 const char *ptr;
2112 const char *end = NULL((void*)0);
2113 int num_chars = 0;
2114 int utc_offset = 0;
2115
2116 validate_single_byte_ascii_encoding(encoding);
2117
2118 DISSECTOR_ASSERT(ns)((void) ((ns) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\""
, "epan/tvbuff.c", 2118, "ns"))))
;
2119
2120 begin = (char*) tvb_get_raw_string(NULL((void*)0), tvb, offset, length);
2121 ptr = begin;
2122
2123 while (IS_WSP(*ptr)((*ptr) == ' ' || (*ptr) == '\t'))
2124 ptr++;
2125
2126 if (*ptr) {
2127 if ((encoding & ENC_ISO_8601_DATE_TIME0x00030000) == ENC_ISO_8601_DATE_TIME0x00030000) {
2128 if (!(end = iso8601_to_nstime(ns, ptr, ISO8601_DATETIME))) {
2129
2130
2131 goto fail;
2132 }
2133 } else if ((encoding & ENC_ISO_8601_DATE_TIME_BASIC0x00100000) == ENC_ISO_8601_DATE_TIME_BASIC0x00100000) {
2134 if (!(end = iso8601_to_nstime(ns, ptr, ISO8601_DATETIME_BASIC))) {
2135
2136
2137 goto fail;
2138 }
2139 } else {
2140 struct tm tm;
2141
2142 memset(&tm, 0, sizeof(tm));
2143 tm.tm_isdst = -1;
2144 ns->secs = 0;
2145 ns->nsecs = 0;
2146
2147 /* note: sscanf is known to be inconsistent across platforms with respect
2148 to whether a %n is counted as a return value or not, so we have to use
2149 '>=' a lot */
2150 if (encoding & ENC_ISO_8601_DATE0x00010000) {
2151 /* 2014-04-07 */
2152 if (sscanf(ptr, "%d-%d-%d%n",
2153 &tm.tm_year,
2154 &tm.tm_mon,
2155 &tm.tm_mday,
2156 &num_chars) >= 3)
2157 {
2158 end = ptr + num_chars;
2159 tm.tm_mon--;
2160 if (tm.tm_year > 1900) tm.tm_year -= 1900;
2161 } else {
2162 goto fail;
2163 }
2164 }
2165 else if (encoding & ENC_ISO_8601_TIME0x00020000) {
2166 /* 2014-04-07 */
2167 if (sscanf(ptr, "%d:%d:%d%n",
2168 &tm.tm_hour,
2169 &tm.tm_min,
2170 &tm.tm_sec,
2171 &num_chars) >= 2)
2172 {
2173 /* what should we do about day/month/year? */
2174 /* setting it to "now" for now */
2175 time_t time_now = time(NULL((void*)0));
2176 struct tm *tm_now = gmtime(&time_now);
2177 if (tm_now != NULL((void*)0)) {
2178 tm.tm_year = tm_now->tm_year;
2179 tm.tm_mon = tm_now->tm_mon;
2180 tm.tm_mday = tm_now->tm_mday;
2181 } else {
2182 /* The second before the Epoch */
2183 tm.tm_year = 69;
2184 tm.tm_mon = 12;
2185 tm.tm_mday = 31;
2186 }
2187 end = ptr + num_chars;
2188 } else {
2189 goto fail;
2190 }
2191 }
2192 else if (encoding & ENC_IMF_DATE_TIME0x00040000) {
2193 /*
2194 * Match [dow,] day month year hh:mm[:ss] with
2195 * two-digit years (RFC 822) or four-digit
2196 * years (RFCs 1123, 2822, 5822). Skip
2197 * the day of week since it is locale
2198 * dependent and does not affect the resulting
2199 * date anyway.
2200 */
2201 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] == ',')
2202 ptr += 4; /* Skip day of week. */
2203
2204 /*
2205 * Parse the day-of-month and month
2206 * name.
2207 */
2208 char month_name[4] = { 0 };
2209
2210 if (sscanf(ptr, "%d %3s%n",
2211 &tm.tm_mday,
2212 month_name,
2213 &num_chars) < 2)
2214 {
2215 /* Not matched. */
2216 goto fail;
2217 }
2218 if (!parse_month_name(month_name, &tm.tm_mon)) {
2219 goto fail;
2220 }
2221 ptr += num_chars;
2222 while (IS_WSP(*ptr)((*ptr) == ' ' || (*ptr) == '\t'))
2223 ptr++;
2224
2225 /*
2226 * Scan the year. Treat 2-digit years
2227 * differently from 4-digit years.
2228 */
2229 uint32_t year;
2230 const char *yearendp;
2231
2232 if (!ws_strtou32(ptr, &yearendp, &year)) {
2233 goto fail;
2234 }
2235 if (!IS_WSP(*yearendp)((*yearendp) == ' ' || (*yearendp) == '\t')) {
2236 /* Not followed by WSP. */
2237 goto fail;
2238 }
2239 if (yearendp - ptr < 2) {
2240 /* 1-digit year. Error. */
2241 goto fail;
2242 }
2243 if (yearendp - ptr == 2) {
2244 /*
2245 * 2-digit year.
2246 *
2247 * Match RFC 2822/RFC 5322 behavior;
2248 * add 2000 to years from 0 to
2249 * 49 and 1900 to uears from 50
2250 * to 99.
2251 */
2252 if (year <= 49) {
2253 year += 2000;
2254 } else {
2255 year += 1900;
2256 }
2257 } else if (yearendp - ptr == 3) {
2258 /*
2259 * 3-digit year.
2260 *
2261 * Match RFC 2822/RFC 5322 behavior;
2262 * add 1900 to the year.
2263 */
2264 year += 1900;
2265 }
2266 tm.tm_year = year - 1900;
2267 ptr = yearendp;
2268 while (IS_WSP(*ptr)((*ptr) == ' ' || (*ptr) == '\t'))
2269 ptr++;
2270
2271 /* Parse the time. */
2272 if (sscanf(ptr, "%d:%d%n:%d%n",
2273 &tm.tm_hour,
2274 &tm.tm_min,
2275 &num_chars,
2276 &tm.tm_sec,
2277 &num_chars) < 2)
2278 {
2279 goto fail;
2280 }
2281 ptr += num_chars;
2282 while (IS_WSP(*ptr)((*ptr) == ' ' || (*ptr) == '\t'))
2283 ptr++;
2284
2285 /*
2286 * Parse the time zone.
2287 * Check for obs-zone values first.
2288 */
2289 if (g_ascii_strncasecmp(ptr, "UT", 2) == 0)
2290 {
2291 ptr += 2;
2292 }
2293 else if (g_ascii_strncasecmp(ptr, "GMT", 3) == 0)
2294 {
2295 ptr += 3;
2296 }
2297 else
2298 {
2299 char sign;
2300 int off_hr;
2301 int off_min;
2302
2303 if (sscanf(ptr, "%c%2d%2d%n",
2304 &sign,
2305 &off_hr,
2306 &off_min,
2307 &num_chars) < 3)
2308 {
2309 goto fail;
2310 }
2311
2312 /*
2313 * If sign is '+', there's a positive
2314 * UTC offset.
2315 *
2316 * If sign is '-', there's a negative
2317 * UTC offset.
2318 *
2319 * Otherwise, that's an invalid UTC
2320 * offset string.
2321 */
2322 if (sign == '+')
2323 utc_offset += (off_hr * 3600) + (off_min * 60);
2324 else if (sign == '-')
2325 utc_offset -= (off_hr * 3600) + (off_min * 60);
2326 else {
2327 /* Sign must be + or - */
2328 goto fail;
2329 }
2330 ptr += num_chars;
2331 }
2332 end = ptr;
2333 }
2334 ns->secs = mktime_utc(&tm);
2335 if (ns->secs == (time_t)-1 && errno(*__errno_location ()) != 0) {
2336 goto fail;
2337 }
2338 ns->secs += utc_offset;
2339 }
2340 } else {
2341 /* Empty string */
2342 goto fail;
2343 }
2344
2345 if (endoff)
2346 *endoff = (unsigned)(offset + (end - begin));
2347 wmem_free(NULL((void*)0), begin);
2348 return ns;
2349
2350fail:
2351 wmem_free(NULL((void*)0), begin);
2352 return NULL((void*)0);
2353}
2354
2355/* Fetch an IPv4 address, in network byte order.
2356 * We do *not* convert them to host byte order; we leave them in
2357 * network byte order. */
2358uint32_t
2359tvb_get_ipv4(tvbuff_t *tvb, const unsigned offset)
2360{
2361 const uint8_t *ptr;
2362 uint32_t addr;
2363
2364 ptr = fast_ensure_contiguous(tvb, offset, sizeof(uint32_t));
2365 memcpy(&addr, ptr, sizeof addr);
2366 return addr;
2367}
2368
2369/* Fetch an IPv6 address. */
2370void
2371tvb_get_ipv6(tvbuff_t *tvb, const unsigned offset, ws_in6_addr *addr)
2372{
2373 const uint8_t *ptr;
2374
2375 ptr = ensure_contiguous_unsigned(tvb, offset, sizeof(*addr));
2376 memcpy(addr, ptr, sizeof *addr);
2377}
2378
2379/*
2380 * These routines return the length of the address in bytes on success
2381 * and -1 if the prefix length is too long.
2382 */
2383int
2384tvb_get_ipv4_addr_with_prefix_len(tvbuff_t *tvb, const unsigned offset, ws_in4_addr *addr,
2385 uint32_t prefix_len)
2386{
2387 uint8_t addr_len;
2388
2389 if (prefix_len > 32)
2390 return -1;
2391
2392 addr_len = (prefix_len + 7) / 8;
2393 *addr = 0;
2394 tvb_memcpy(tvb, addr, offset, addr_len);
2395 if (prefix_len % 8)
2396 ((uint8_t*)addr)[addr_len - 1] &= ((0xff00 >> (prefix_len % 8)) & 0xff);
2397 return addr_len;
2398}
2399
2400/*
2401 * These routines return the length of the address in bytes on success
2402 * and -1 if the prefix length is too long.
2403 */
2404int
2405tvb_get_ipv6_addr_with_prefix_len(tvbuff_t *tvb, const unsigned offset, ws_in6_addr *addr,
2406 uint32_t prefix_len)
2407{
2408 uint32_t addr_len;
2409
2410 if (prefix_len > 128)
2411 return -1;
2412
2413 addr_len = (prefix_len + 7) / 8;
2414 memset(addr->bytes, 0, 16);
2415 tvb_memcpy(tvb, addr->bytes, offset, addr_len);
2416 if (prefix_len % 8) {
2417 addr->bytes[addr_len - 1] &=
2418 ((0xff00 >> (prefix_len % 8)) & 0xff);
2419 }
2420
2421 return addr_len;
2422}
2423
2424/* Fetch a GUID. */
2425void
2426tvb_get_ntohguid(tvbuff_t *tvb, const unsigned offset, e_guid_t *guid)
2427{
2428 const uint8_t *ptr = ensure_contiguous_unsigned(tvb, offset, GUID_LEN16);
2429
2430 guid->data1 = pntohu32(ptr + 0);
2431 guid->data2 = pntohu16(ptr + 4);
2432 guid->data3 = pntohu16(ptr + 6);
2433 memcpy(guid->data4, ptr + 8, sizeof guid->data4);
2434}
2435
2436void
2437tvb_get_letohguid(tvbuff_t *tvb, const unsigned offset, e_guid_t *guid)
2438{
2439 const uint8_t *ptr = ensure_contiguous_unsigned(tvb, offset, GUID_LEN16);
2440
2441 guid->data1 = pletohu32(ptr + 0);
2442 guid->data2 = pletohu16(ptr + 4);
2443 guid->data3 = pletohu16(ptr + 6);
2444 memcpy(guid->data4, ptr + 8, sizeof guid->data4);
2445}
2446
2447void
2448tvb_get_guid(tvbuff_t *tvb, const unsigned offset, e_guid_t *guid, const unsigned encoding)
2449{
2450 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
2451 tvb_get_letohguid(tvb, offset, guid);
2452 } else {
2453 tvb_get_ntohguid(tvb, offset, guid);
2454 }
2455}
2456
2457static const uint8_t bit_mask8[] = {
2458 0x00,
2459 0x01,
2460 0x03,
2461 0x07,
2462 0x0f,
2463 0x1f,
2464 0x3f,
2465 0x7f,
2466 0xff
2467};
2468
2469
2470/* Get a variable amount of bits
2471 *
2472 * Return a byte array with bit limited data.
2473 * When encoding is ENC_BIG_ENDIAN, the data is aligned to the left.
2474 * When encoding is ENC_LITTLE_ENDIAN, the data is aligned to the right.
2475 */
2476uint8_t *
2477tvb_get_bits_array(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned bit_offset,
2478 size_t no_of_bits, size_t *data_length, const unsigned encoding)
2479{
2480 tvbuff_t *sub_tvb;
2481 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
1
Assuming the condition is true
2
Taking true branch
2482 sub_tvb = tvb_new_octet_right_aligned(tvb, bit_offset, (int32_t) no_of_bits);
3
Calling 'tvb_new_octet_right_aligned'
2483 } else {
2484 sub_tvb = tvb_new_octet_aligned(tvb, bit_offset, (int32_t) no_of_bits);
2485 }
2486 *data_length = tvb_reported_length(sub_tvb);
2487 return (uint8_t*)tvb_memdup(scope, sub_tvb, 0, *data_length);
2488}
2489
2490/* Get 1 - 8 bits */
2491uint8_t
2492tvb_get_bits8(tvbuff_t *tvb, unsigned bit_offset, const unsigned no_of_bits)
2493{
2494 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", 2494
, "no_of_bits <= 8", "Too many bits requested for 8-bit return type"
))))
;
2495 return (uint8_t)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
2496}
2497
2498/* Get 1 - 16 bits */
2499uint16_t
2500tvb_get_bits16(tvbuff_t *tvb, unsigned bit_offset, const unsigned no_of_bits, const unsigned encoding)
2501{
2502 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", 2502
, "no_of_bits <= 16", "Too many bits requested for 16-bit return type"
))))
;
2503 return (uint16_t)tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
2504}
2505
2506/* Get 1 - 32 bits */
2507uint32_t
2508tvb_get_bits32(tvbuff_t *tvb, unsigned bit_offset, const unsigned no_of_bits, const unsigned encoding)
2509{
2510 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", 2510
, "no_of_bits <= 32", "Too many bits requested for 32-bit return type"
))))
;
2511 return (uint32_t)tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
2512}
2513
2514/* Get 1 - 64 bits */
2515uint64_t
2516tvb_get_bits64(tvbuff_t *tvb, unsigned bit_offset, const unsigned no_of_bits, const unsigned encoding)
2517{
2518 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", 2518
, "no_of_bits <= 64", "Too many bits requested for 64-bit return type"
))))
;
2519
2520 /* encoding determines bit numbering within octet array */
2521 if (encoding & ENC_LITTLE_ENDIAN0x80000000) {
2522 return _tvb_get_bits64_le(tvb, bit_offset, no_of_bits);
2523 } else {
2524 return _tvb_get_bits64(tvb, bit_offset, no_of_bits);
2525 }
2526}
2527
2528/*
2529 * This function will dissect a sequence of bits that does not need to be byte aligned; the bits
2530 * set will be shown in the tree as ..10 10.. and the integer value returned if return_value is set.
2531 * Offset should be given in bits from the start of the tvb.
2532 * Bits within octet are numbered from MSB (0) to LSB (7). Bit at bit_offset is return value most significant bit.
2533 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
2534 */
2535static uint64_t
2536_tvb_get_bits64(tvbuff_t *tvb, unsigned bit_offset, const unsigned total_no_of_bits)
2537{
2538 uint64_t value;
2539 unsigned octet_offset = bit_offset >> 3;
2540 uint8_t required_bits_in_first_octet = 8 - (bit_offset % 8);
2541
2542 if(required_bits_in_first_octet > total_no_of_bits)
2543 {
2544 /* the required bits don't extend to the end of the first octet */
2545 uint8_t right_shift = required_bits_in_first_octet - total_no_of_bits;
2546 value = (tvb_get_uint8(tvb, octet_offset) >> right_shift) & bit_mask8[total_no_of_bits % 8];
2547 }
2548 else
2549 {
2550 uint8_t remaining_bit_length = total_no_of_bits;
2551
2552 /* get the bits up to the first octet boundary */
2553 value = 0;
2554 required_bits_in_first_octet %= 8;
2555 if(required_bits_in_first_octet != 0)
2556 {
2557 value = tvb_get_uint8(tvb, octet_offset) & bit_mask8[required_bits_in_first_octet];
2558 remaining_bit_length -= required_bits_in_first_octet;
2559 octet_offset ++;
2560 }
2561 /* take the biggest words, shorts or octets that we can */
2562 while (remaining_bit_length > 7)
2563 {
2564 switch (remaining_bit_length >> 4)
2565 {
2566 case 0:
2567 /* 8 - 15 bits. (note that 0 - 7 would have dropped out of the while() loop) */
2568 value <<= 8;
2569 value += tvb_get_uint8(tvb, octet_offset);
2570 remaining_bit_length -= 8;
2571 octet_offset ++;
2572 break;
2573
2574 case 1:
2575 /* 16 - 31 bits */
2576 value <<= 16;
2577 value += tvb_get_ntohs(tvb, octet_offset);
2578 remaining_bit_length -= 16;
2579 octet_offset += 2;
2580 break;
2581
2582 case 2:
2583 case 3:
2584 /* 32 - 63 bits */
2585 value <<= 32;
2586 value += tvb_get_ntohl(tvb, octet_offset);
2587 remaining_bit_length -= 32;
2588 octet_offset += 4;
2589 break;
2590
2591 default:
2592 /* 64 bits (or more???) */
2593 value = tvb_get_ntoh64(tvb, octet_offset);
2594 remaining_bit_length -= 64;
2595 octet_offset += 8;
2596 break;
2597 }
2598 }
2599 /* get bits from any partial octet at the tail */
2600 if(remaining_bit_length)
2601 {
2602 value <<= remaining_bit_length;
2603 value += (tvb_get_uint8(tvb, octet_offset) >> (8 - remaining_bit_length));
2604 }
2605 }
2606 return value;
2607}
2608
2609/*
2610 * Offset should be given in bits from the start of the tvb.
2611 * Bits within octet are numbered from LSB (0) to MSB (7). Bit at bit_offset is return value least significant bit.
2612 * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
2613 */
2614static uint64_t
2615_tvb_get_bits64_le(tvbuff_t *tvb, unsigned bit_offset, const unsigned total_no_of_bits)
2616{
2617 uint64_t value = 0;
2618 unsigned octet_offset = bit_offset / 8;
2619 unsigned remaining_bits = total_no_of_bits;
2620 unsigned shift = 0;
2621
2622 if (remaining_bits > 64)
2623 {
2624 remaining_bits = 64;
2625 }
2626
2627 if (bit_offset % 8)
2628 {
2629 /* not aligned, extract bits from first octet */
2630 shift = 8 - (bit_offset % 8);
2631 value = tvb_get_uint8(tvb, octet_offset) >> (bit_offset % 8);
2632 if (shift > remaining_bits)
2633 {
2634 /* keep only the requested bits */
2635 value &= (UINT64_C(1)1UL << remaining_bits) - 1;
2636 remaining_bits = 0;
2637 }
2638 else
2639 {
2640 remaining_bits -= shift;
2641 }
2642 octet_offset++;
2643 }
2644
2645 while (remaining_bits > 0)
2646 {
2647 /* take the biggest words, shorts or octets that we can */
2648 if (remaining_bits >= 32)
2649 {
2650 value |= ((uint64_t)tvb_get_letohl(tvb, octet_offset) << shift);
2651 shift += 32;
2652 remaining_bits -= 32;
2653 octet_offset += 4;
2654 }
2655 else if (remaining_bits >= 16)
2656 {
2657 value |= ((uint64_t)tvb_get_letohs(tvb, octet_offset) << shift);
2658 shift += 16;
2659 remaining_bits -= 16;
2660 octet_offset += 2;
2661 }
2662 else if (remaining_bits >= 8)
2663 {
2664 value |= ((uint64_t)tvb_get_uint8(tvb, octet_offset) << shift);
2665 shift += 8;
2666 remaining_bits -= 8;
2667 octet_offset += 1;
2668 }
2669 else
2670 {
2671 unsigned mask = (1 << remaining_bits) - 1;
2672 value |= (((uint64_t)tvb_get_uint8(tvb, octet_offset) & mask) << shift);
2673 shift += remaining_bits;
2674 remaining_bits = 0;
2675 octet_offset += 1;
2676 }
2677 }
2678 return value;
2679}
2680
2681/* Get 1 - 32 bits (should be deprecated as same as tvb_get_bits32??) */
2682uint32_t
2683tvb_get_bits(tvbuff_t *tvb, const unsigned bit_offset, const unsigned no_of_bits, const unsigned encoding)
2684{
2685 return (uint32_t)tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
2686}
2687
2688static bool_Bool
2689tvb_find_uint8_generic(tvbuff_t *tvb, unsigned abs_offset, unsigned limit, uint8_t needle, unsigned *end_offset)
2690{
2691 const uint8_t *ptr;
2692 const uint8_t *result;
2693
2694 if (end_offset) {
2695 *end_offset = abs_offset + limit;
2696 }
2697
2698 ptr = ensure_contiguous_unsigned(tvb, abs_offset, limit); /* tvb_get_ptr() */
2699 if (!ptr)
2700 return false0;
2701
2702 result = (const uint8_t *) memchr(ptr, needle, limit);
2703 if (!result)
2704 return false0;
2705
2706 if (end_offset) {
2707 *end_offset = (unsigned)((result - ptr) + abs_offset);
2708 }
2709 return true1;
2710}
2711
2712static bool_Bool
2713_tvb_find_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, const uint8_t needle, unsigned *end_offset)
2714{
2715 const uint8_t *result;
2716
2717 /* If we have real data, perform our search now. */
2718 if (tvb->real_data) {
2719 result = (const uint8_t *)memchr(tvb->real_data + offset, needle, limit);
2720 if (result == NULL((void*)0)) {
2721 if (end_offset) {
2722 *end_offset = offset + limit;
2723 }
2724 return false0;
2725 }
2726 else {
2727 if (end_offset) {
2728 *end_offset = (unsigned)(result - tvb->real_data);
2729 }
2730 return true1;
2731 }
2732 }
2733
2734 if (tvb->ops->tvb_find_uint8)
2735 return tvb->ops->tvb_find_uint8(tvb, offset, limit, needle, end_offset);
2736
2737 return tvb_find_uint8_generic(tvb, offset, limit, needle, end_offset);
2738}
2739
2740bool_Bool
2741tvb_find_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, const uint8_t needle, unsigned *end_offset)
2742{
2743 unsigned limit = 0;
2744 int exception;
2745
2746 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2746, "tvb && tvb->initialized"
))))
;
2747
2748 exception = validate_offset_and_remaining(tvb, offset, &limit);
2749 if (exception)
2750 THROW(exception)except_throw(1, (exception), ((void*)0));
2751
2752 /* Only search to end of tvbuff, w/o throwing exception. */
2753 if (limit > maxlength) {
2754 /* Maximum length doesn't go past end of tvbuff; search
2755 to that value. */
2756 limit = maxlength;
2757 }
2758
2759 return _tvb_find_uint8_length(tvb, offset, limit, needle, end_offset);
2760}
2761
2762bool_Bool
2763tvb_find_uint8_remaining(tvbuff_t *tvb, const unsigned offset, const uint8_t needle, unsigned *end_offset)
2764{
2765 unsigned limit = 0;
2766 int exception;
2767
2768 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2768, "tvb && tvb->initialized"
))))
;
2769
2770 exception = validate_offset_and_remaining(tvb, offset, &limit);
2771 if (exception)
2772 THROW(exception)except_throw(1, (exception), ((void*)0));
2773
2774 return _tvb_find_uint8_length(tvb, offset, limit, needle, end_offset);
2775}
2776
2777static bool_Bool
2778_tvb_find_uint16_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, const uint16_t needle, unsigned *end_offset)
2779{
2780 const uint8_t needle1 = ((needle & 0xFF00) >> 8);
2781 const uint8_t needle2 = ((needle & 0x00FF) >> 0);
2782 unsigned searched_bytes = 0;
2783 unsigned pos = offset;
2784
2785 if (end_offset) {
2786 *end_offset = offset + limit;
2787 }
2788
2789 do {
2790 if (!_tvb_find_uint8_length(tvb, pos, limit - searched_bytes, needle1, &pos)) {
2791 return false0;
2792 }
2793
2794 /* Bytes searched so far (not counting the second byte) */
2795 searched_bytes = pos - offset + 1;
2796
2797 /* Test vs. equality to account for the second byte */
2798 if (searched_bytes >= limit) {
2799 return false0;
2800 }
2801
2802 if (_tvb_find_uint8_length(tvb, pos + 1, 1, needle2, NULL((void*)0))) {
2803 if (end_offset) {
2804 *end_offset = pos;
2805 }
2806 return true1;
2807 }
2808
2809 pos += 1;
2810 searched_bytes += 1;
2811 } while (searched_bytes < limit);
2812
2813 return false0;
2814}
2815
2816bool_Bool
2817tvb_find_uint16_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, const uint16_t needle, unsigned *end_offset)
2818{
2819 unsigned limit = 0;
2820 int exception;
2821
2822 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2822, "tvb && tvb->initialized"
))))
;
2823
2824 exception = validate_offset_and_remaining(tvb, offset, &limit);
2825 if (exception)
2826 THROW(exception)except_throw(1, (exception), ((void*)0));
2827
2828 /* Only search to end of tvbuff, w/o throwing exception. */
2829 if (limit > maxlength) {
2830 /* Maximum length doesn't go past end of tvbuff; search
2831 to that value. */
2832 limit = maxlength;
2833 }
2834
2835 return _tvb_find_uint16_length(tvb, offset, limit, needle, end_offset);
2836}
2837
2838bool_Bool
2839tvb_find_uint16_remaining(tvbuff_t *tvb, const unsigned offset, const uint16_t needle, unsigned *end_offset)
2840{
2841 unsigned limit = 0;
2842 int exception;
2843
2844 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2844, "tvb && tvb->initialized"
))))
;
2845
2846 exception = validate_offset_and_remaining(tvb, offset, &limit);
2847 if (exception)
2848 THROW(exception)except_throw(1, (exception), ((void*)0));
2849
2850 return _tvb_find_uint16_length(tvb, offset, limit, needle, end_offset);
2851}
2852
2853static inline bool_Bool
2854tvb_ws_mempbrk_uint8_generic(tvbuff_t *tvb, unsigned abs_offset, unsigned limit, const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
2855{
2856 const uint8_t *ptr;
2857 const uint8_t *result;
2858
2859 if (found_offset) {
2860 *found_offset = abs_offset + limit;
2861 }
2862
2863 ptr = ensure_contiguous_unsigned(tvb, abs_offset, limit); /* tvb_get_ptr */
2864 if (!ptr)
2865 return false0;
2866
2867 result = ws_mempbrk_exec(ptr, limit, pattern, found_needle);
2868 if (!result)
2869 return false0;
2870
2871 if (found_offset) {
2872 *found_offset = (unsigned)((result - ptr) + abs_offset);
2873 }
2874 return true1;
2875}
2876
2877static bool_Bool
2878_tvb_ws_mempbrk_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit,
2879 const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
2880{
2881 const uint8_t *result;
2882
2883 /* If we have real data, perform our search now. */
2884 if (tvb->real_data) {
2885 result = ws_mempbrk_exec(tvb->real_data + offset, limit, pattern, found_needle);
2886 if (result == NULL((void*)0)) {
2887 if (found_offset) {
2888 *found_offset = offset + limit;
2889 }
2890 return false0;
2891 }
2892 else {
2893 if (found_offset) {
2894 *found_offset = (unsigned)(result - tvb->real_data);
2895 }
2896 return true1;
2897 }
2898 }
2899
2900 if (tvb->ops->tvb_ws_mempbrk_pattern_uint8)
2901 return tvb->ops->tvb_ws_mempbrk_pattern_uint8(tvb, offset, limit, pattern, found_offset, found_needle);
2902
2903 return tvb_ws_mempbrk_uint8_generic(tvb, offset, limit, pattern, found_offset, found_needle);
2904}
2905
2906bool_Bool
2907tvb_ws_mempbrk_uint8_remaining(tvbuff_t *tvb, const unsigned offset,
2908 const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
2909{
2910 unsigned limit = 0;
2911 int exception;
2912
2913 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2913, "tvb && tvb->initialized"
))))
;
2914
2915 exception = validate_offset_and_remaining(tvb, offset, &limit);
2916 if (exception)
2917 THROW(exception)except_throw(1, (exception), ((void*)0));
2918
2919 return _tvb_ws_mempbrk_uint8_length(tvb, offset, limit, pattern, found_offset, found_needle);
2920}
2921
2922bool_Bool
2923tvb_ws_mempbrk_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength,
2924 const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
2925{
2926 unsigned limit = 0;
2927 int exception;
2928
2929 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2929, "tvb && tvb->initialized"
))))
;
2930
2931 exception = validate_offset_and_remaining(tvb, offset, &limit);
2932 if (exception)
2933 THROW(exception)except_throw(1, (exception), ((void*)0));
2934
2935 /* Only search to end of tvbuff, w/o throwing exception. */
2936 if (limit > maxlength) {
2937 /* Maximum length doesn't go past end of tvbuff; search
2938 to that value. */
2939 limit = maxlength;
2940 }
2941
2942 return _tvb_ws_mempbrk_uint8_length(tvb, offset, limit, pattern, found_offset, found_needle);
2943}
2944
2945/* Find size of stringz (NUL-terminated string) by looking for terminating
2946 * NUL. The size of the string includes the terminating NUL.
2947 *
2948 * If the NUL isn't found, it throws the appropriate exception.
2949 */
2950unsigned
2951tvb_strsize(tvbuff_t *tvb, const unsigned offset)
2952{
2953 unsigned nul_offset;
2954
2955 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2955, "tvb && tvb->initialized"
))))
;
2956
2957 validate_offset(tvb, offset);
2958 if (!tvb_find_uint8_remaining(tvb, offset, 0, &nul_offset)) {
2959 /*
2960 * OK, we hit the end of the tvbuff, so we should throw
2961 * an exception.
2962 */
2963 if (tvb->length < tvb->contained_length) {
2964 THROW(BoundsError)except_throw(1, (1), ((void*)0));
2965 } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
2966 THROW(FragmentBoundsError)except_throw(1, (4), ((void*)0));
2967 } else if (tvb->length < tvb->reported_length) {
2968 THROW(ContainedBoundsError)except_throw(1, (2), ((void*)0));
2969 } else {
2970 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
2971 }
2972 }
2973 return (nul_offset - offset) + 1;
2974}
2975
2976/* UTF-16/UCS-2 version of tvb_strsize */
2977/* Returns number of bytes including the (two-bytes) null terminator */
2978unsigned
2979tvb_unicode_strsize(tvbuff_t *tvb, const unsigned offset)
2980{
2981 unsigned cur_offset = offset;
2982 gunichar2 uchar;
2983
2984 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2984, "tvb && tvb->initialized"
))))
;
2985
2986 /* Note: don't use tvb_find_uint16 because it must be aligned */
2987 do {
2988 /* Endianness doesn't matter when looking for null */
2989 uchar = tvb_get_ntohs(tvb, cur_offset);
2990 /* Make sure we don't overflow */
2991 if (ckd_add(&cur_offset, cur_offset, 2)__builtin_add_overflow((cur_offset), (2), (&cur_offset))) {
2992 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
2993 }
2994 } while(uchar != 0);
2995
2996 return cur_offset - offset;
2997}
2998
2999/* UTF-32/UCS-4 version of tvb_strsize */
3000/* Returns number of bytes including the (four-bytes) null terminator */
3001static unsigned
3002tvb_ucs_4_strsize(tvbuff_t *tvb, const unsigned offset)
3003{
3004
3005 unsigned end_offset;
3006 gunichar uchar;
3007
3008 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3008, "tvb && tvb->initialized"
))))
;
3009 end_offset = offset;
3010 do {
3011 /* Endianness doesn't matter when looking for null */
3012 uchar = tvb_get_ntohl(tvb, end_offset);
3013 /* Make sure we don't overflow */
3014 if (ckd_add(&end_offset, end_offset, 4)__builtin_add_overflow((end_offset), (4), (&end_offset))) {
3015 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
3016 }
3017 } while(uchar != 0);
3018 return end_offset - offset;
3019}
3020
3021unsigned
3022tvb_strsize_enc(tvbuff_t *tvb, const unsigned offset, const unsigned encoding)
3023{
3024 switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
3025 case ENC_UTF_160x00000004:
3026 case ENC_UCS_20x00000006:
3027 return tvb_unicode_strsize(tvb, offset);
3028
3029 case ENC_UCS_40x00000008:
3030 return tvb_ucs_4_strsize(tvb, offset);
3031
3032 case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
3033 case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
3034 case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
3035 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"
)
;
3036 break;
3037
3038 case ENC_ASCII_7BITS0x00000034:
3039 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"
)
;
3040 break;
3041
3042 case ENC_APN_STR0x00000054:
3043 /* At least as defined in 3GPP TS 23.003 Clause 9.1, null-termination
3044 * does make sense as internal nulls are not allowed. */
3045 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"
)
;
3046 break;
3047
3048 case ENC_BCD_DIGITS_0_90x00000044:
3049 case ENC_KEYPAD_ABC_TBCD0x00000046:
3050 case ENC_KEYPAD_BC_TBCD0x00000048:
3051 case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
3052 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."
)
;
3053 break;
3054
3055 case ENC_ASCII0x00000000:
3056 case ENC_UTF_80x00000002:
3057 case ENC_ISO_8859_10x0000000A:
3058 case ENC_ISO_8859_20x0000000C:
3059 case ENC_ISO_8859_30x0000000E:
3060 case ENC_ISO_8859_40x00000010:
3061 case ENC_ISO_8859_50x00000012:
3062 case ENC_ISO_8859_60x00000014:
3063 case ENC_ISO_8859_70x00000016:
3064 case ENC_ISO_8859_80x00000018:
3065 case ENC_ISO_8859_90x0000001A:
3066 case ENC_ISO_8859_100x0000001C:
3067 case ENC_ISO_8859_110x0000001E:
3068 case ENC_ISO_8859_130x00000022:
3069 case ENC_ISO_8859_140x00000024:
3070 case ENC_ISO_8859_150x00000026:
3071 case ENC_ISO_8859_160x00000028:
3072 case ENC_WINDOWS_12500x0000002A:
3073 case ENC_WINDOWS_12510x0000003C:
3074 case ENC_WINDOWS_12520x0000003A:
3075 case ENC_MAC_ROMAN0x00000030:
3076 case ENC_CP4370x00000032:
3077 case ENC_CP8550x0000003E:
3078 case ENC_CP8660x00000040:
3079 case ENC_ISO_646_BASIC0x00000042:
3080 case ENC_EBCDIC0x0000002E:
3081 case ENC_EBCDIC_CP0370x00000038:
3082 case ENC_EBCDIC_CP5000x00000060:
3083 case ENC_T610x00000036:
3084 case ENC_GB180300x00000050:
3085 case ENC_EUC_KR0x00000052:
3086 case ENC_DECT_STANDARD_8BITS0x00000056:
3087 default:
3088 return tvb_strsize(tvb, offset);
3089 }
3090}
3091
3092/* Find length of string by looking for end of string ('\0'), up to
3093 * 'maxlength' characters'; if 'maxlength' is -1, searches to end
3094 * of tvbuff.
3095 * Returns -1 if 'maxlength' reached before finding EOS. */
3096int
3097tvb_strnlen(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
3098{
3099 unsigned result_offset;
3100
3101 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3101, "tvb && tvb->initialized"
))))
;
3102
3103 /* TODO - this needs a variant that returns a bool
3104 * and sets a unsigned offset to the value if true. */
3105 if (!tvb_find_uint8_length(tvb, offset, maxlength, 0, &result_offset)) {
3106 return -1;
3107 }
3108 else {
3109 return (int)(result_offset - offset);
3110 }
3111}
3112
3113/*
3114 * Implement strneql etc
3115 */
3116
3117/*
3118 * Call strncmp after checking if enough chars left, returning 0 if
3119 * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
3120 */
3121int
3122tvb_strneql(tvbuff_t *tvb, const unsigned offset, const char *str, const size_t size)
3123{
3124 const uint8_t *ptr;
3125
3126 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3127
3128 if (ptr) {
3129 int cmp = strncmp((const char *)ptr, str, size);
3130
3131 /*
3132 * Return 0 if equal, -1 otherwise.
3133 */
3134 return (cmp == 0 ? 0 : -1);
3135 } else {
3136 /*
3137 * Not enough characters in the tvbuff to match the
3138 * string.
3139 */
3140 return -1;
3141 }
3142}
3143
3144/*
3145 * Call g_ascii_strncasecmp after checking if enough chars left, returning
3146 * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
3147 */
3148int
3149tvb_strncaseeql(tvbuff_t *tvb, const unsigned offset, const char *str, const size_t size)
3150{
3151 const uint8_t *ptr;
3152
3153 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3154
3155 if (ptr) {
3156 int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
3157
3158 /*
3159 * Return 0 if equal, -1 otherwise.
3160 */
3161 return (cmp == 0 ? 0 : -1);
3162 } else {
3163 /*
3164 * Not enough characters in the tvbuff to match the
3165 * string.
3166 */
3167 return -1;
3168 }
3169}
3170
3171/*
3172 * Check that the tvbuff contains at least size bytes, starting at
3173 * offset, and that those bytes are equal to str. Return 0 for success
3174 * and -1 for error. This function does not throw an exception.
3175 */
3176int
3177tvb_memeql(tvbuff_t *tvb, const unsigned offset, const uint8_t *str, size_t size)
3178{
3179 const uint8_t *ptr;
3180
3181 ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3182
3183 if (ptr) {
3184 int cmp = memcmp(ptr, str, size);
3185
3186 /*
3187 * Return 0 if equal, -1 otherwise.
3188 */
3189 return (cmp == 0 ? 0 : -1);
3190 } else {
3191 /*
3192 * Not enough characters in the tvbuff to match the
3193 * string.
3194 */
3195 return -1;
3196 }
3197}
3198
3199/**
3200 * Format the data in the tvb from offset for size.
3201 */
3202char *
3203tvb_format_text(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3204{
3205 const uint8_t *ptr;
3206
3207 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3208 return format_text(scope, (const char*)ptr, size);
3209}
3210
3211/*
3212 * Format the data in the tvb from offset for length ...
3213 */
3214char *
3215tvb_format_text_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3216{
3217 const uint8_t *ptr;
3218
3219 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3220 return format_text_wsp(allocator, (const char*)ptr, size);
3221}
3222
3223/**
3224 * Like "tvb_format_text()", but for null-padded strings; don't show
3225 * the null padding characters as "\000".
3226 */
3227char *
3228tvb_format_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3229{
3230 const uint8_t *ptr, *p;
3231 unsigned stringlen;
3232
3233 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3234 for (p = ptr, stringlen = 0; stringlen < size && *p != '\0'; p++, stringlen++)
3235 ;
3236 return format_text(scope, (const char*)ptr, stringlen);
3237}
3238
3239/*
3240 * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
3241 * the null padding characters as "\000".
3242 */
3243char *
3244tvb_format_stringzpad_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3245{
3246 const uint8_t *ptr, *p;
3247 unsigned stringlen;
3248
3249 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3250 for (p = ptr, stringlen = 0; stringlen < size && *p != '\0'; p++, stringlen++)
3251 ;
3252 return format_text_wsp(allocator, (const char*)ptr, stringlen);
3253}
3254
3255/*
3256 * All string functions below take a scope as an argument.
3257 *
3258 *
3259 * If scope is NULL, memory is allocated with g_malloc() and user must
3260 * explicitly free it with g_free().
3261 * If scope is not NULL, memory is allocated with the corresponding pool
3262 * lifetime.
3263 *
3264 * All functions throw an exception if the tvbuff ends before the string
3265 * does.
3266 */
3267
3268/*
3269 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3270 * of bytes referred to by the tvbuff, offset, and length as an ASCII string,
3271 * with all bytes with the high-order bit set being invalid, and return a
3272 * pointer to a UTF-8 string, allocated using the wmem scope.
3273 *
3274 * Octets with the highest bit set will be converted to the Unicode
3275 * REPLACEMENT CHARACTER.
3276 */
3277static uint8_t *
3278tvb_get_ascii_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3279{
3280 const uint8_t *ptr;
3281
3282 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3283 return get_ascii_string(scope, ptr, length);
3284}
3285
3286/*
3287 * Given a wmem scope, a tvbuff, an offset, a length, and a translation table,
3288 * treat the string of bytes referred to by the tvbuff, offset, and length
3289 * as a string encoded using one octet per character, with octets with the
3290 * high-order bit clear being mapped by the translation table to 2-byte
3291 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3292 * CHARACTER) and octets with the high-order bit set being mapped to
3293 * REPLACEMENT CHARACTER, and return a pointer to a UTF-8 string,
3294 * allocated using the wmem scope.
3295 *
3296 * Octets with the highest bit set will be converted to the Unicode
3297 * REPLACEMENT CHARACTER.
3298 */
3299static uint8_t *
3300tvb_get_iso_646_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3301{
3302 const uint8_t *ptr;
3303
3304 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3305 return get_iso_646_string(scope, ptr, length, table);
3306}
3307
3308/*
3309 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3310 * of bytes referred to by the tvbuff, the offset. and the length as a UTF-8
3311 * string, and return a pointer to a UTF-8 string, allocated using the wmem
3312 * scope, with all ill-formed sequences replaced with the Unicode REPLACEMENT
3313 * CHARACTER according to the recommended "best practices" given in the Unicode
3314 * Standard and specified by W3C/WHATWG.
3315 *
3316 * Note that in conformance with the Unicode Standard, this treats three
3317 * byte sequences corresponding to UTF-16 surrogate halves (paired or unpaired)
3318 * and two byte overlong encodings of 7-bit ASCII characters as invalid and
3319 * substitutes REPLACEMENT CHARACTER for them. Explicit support for nonstandard
3320 * derivative encoding formats (e.g. CESU-8, Java Modified UTF-8, WTF-8) could
3321 * be added later.
3322 */
3323static uint8_t *
3324tvb_get_utf_8_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3325{
3326 const uint8_t *ptr;
3327
3328 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3329 return get_utf_8_string(scope, ptr, length);
3330}
3331
3332/*
3333 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3334 * of bytes referred to by the tvbuff, the offset, and the length as a
3335 * raw string, and return a pointer to that string, allocated using the
3336 * wmem scope. This means a null is appended at the end, but no replacement
3337 * checking is done otherwise, unlike tvb_get_utf_8_string().
3338 */
3339static inline uint8_t *
3340tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3341{
3342 uint8_t *strbuf;
3343
3344 tvb_ensure_bytes_exist(tvb, offset, length);
3345 strbuf = (uint8_t *)wmem_alloc(scope, length + 1);
3346 tvb_memcpy(tvb, strbuf, offset, length);
3347 strbuf[length] = '\0';
3348 return strbuf;
3349}
3350
3351/*
3352 * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3353 * of bytes referred to by the tvbuff, the offset, and the length as an
3354 * ISO 8859/1 string, and return a pointer to a UTF-8 string, allocated
3355 * using the wmem scope.
3356 */
3357static uint8_t *
3358tvb_get_string_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3359{
3360 const uint8_t *ptr;
3361
3362 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3363 return get_8859_1_string(scope, ptr, length);
3364}
3365
3366/*
3367 * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3368 * table, treat the string of bytes referred to by the tvbuff, the offset,
3369 * and the length as a string encoded using one octet per character, with
3370 * octets with the high-order bit clear being ASCII and octets with the
3371 * high-order bit set being mapped by the translation table to 2-byte
3372 * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3373 * CHARACTER), and return a pointer to a UTF-8 string, allocated with the
3374 * wmem scope.
3375 */
3376static uint8_t *
3377tvb_get_string_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3378{
3379 const uint8_t *ptr;
3380
3381 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3382 return get_unichar2_string(scope, ptr, length, table);
3383}
3384
3385/*
3386 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3387 * giving the byte order, treat the string of bytes referred to by the
3388 * tvbuff, the offset, and the length as a UCS-2 encoded string in
3389 * the byte order in question, containing characters from the Basic
3390 * Multilingual Plane (plane 0) of Unicode, and return a pointer to a
3391 * UTF-8 string, allocated with the wmem scope.
3392 *
3393 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3394 * optionally with ENC_BOM.
3395 *
3396 * Specify length in bytes.
3397 *
3398 * XXX - should map lead and trail surrogate values to REPLACEMENT
3399 * CHARACTERs (0xFFFD)?
3400 * XXX - if there are an odd number of bytes, should put a
3401 * REPLACEMENT CHARACTER at the end.
3402 */
3403static uint8_t *
3404tvb_get_ucs_2_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3405{
3406 const uint8_t *ptr;
3407
3408 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3409 return get_ucs_2_string(scope, ptr, length, encoding);
3410}
3411
3412/*
3413 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3414 * giving the byte order, treat the string of bytes referred to by the
3415 * tvbuff, the offset, and the length as a UTF-16 encoded string in
3416 * the byte order in question, and return a pointer to a UTF-8 string,
3417 * allocated with the wmem scope.
3418 *
3419 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3420 * optionally with ENC_BOM.
3421 *
3422 * Specify length in bytes.
3423 *
3424 * XXX - should map surrogate errors to REPLACEMENT CHARACTERs (0xFFFD).
3425 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3426 * XXX - if there are an odd number of bytes, should put a
3427 * REPLACEMENT CHARACTER at the end.
3428 */
3429static uint8_t *
3430tvb_get_utf_16_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3431{
3432 const uint8_t *ptr;
3433
3434 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3435 return get_utf_16_string(scope, ptr, length, encoding);
3436}
3437
3438/*
3439 * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3440 * giving the byte order, treat the string of bytes referred to by the
3441 * tvbuff, the offset, and the length as a UCS-4 encoded string in
3442 * the byte order in question, and return a pointer to a UTF-8 string,
3443 * allocated with the wmem scope.
3444 *
3445 * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3446 * optionally with ENC_BOM.
3447 *
3448 * Specify length in bytes
3449 *
3450 * XXX - should map lead and trail surrogate values to a "substitute"
3451 * UTF-8 character?
3452 * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3453 * XXX - if the number of bytes isn't a multiple of 4, should put a
3454 * REPLACEMENT CHARACTER at the end.
3455 */
3456static char *
3457tvb_get_ucs_4_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3458{
3459 const uint8_t *ptr;
3460
3461 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3462 return (char*)get_ucs_4_string(scope, ptr, length, encoding);
3463}
3464
3465char *
3466tvb_get_ts_23_038_7bits_string_packed(wmem_allocator_t *scope, tvbuff_t *tvb,
3467 const unsigned bit_offset, unsigned no_of_chars)
3468{
3469 unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3470 unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3471 const uint8_t *ptr;
3472
3473 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3473, "tvb && tvb->initialized"
))))
;
3474
3475 ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3476 return (char*)get_ts_23_038_7bits_string_packed(scope, ptr, bit_offset, no_of_chars);
3477}
3478
3479char *
3480tvb_get_ts_23_038_7bits_string_unpacked(wmem_allocator_t *scope, tvbuff_t *tvb,
3481 const unsigned offset, unsigned length)
3482{
3483 const uint8_t *ptr;
3484
3485 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3485, "tvb && tvb->initialized"
))))
;
3486
3487 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3488 return (char*)get_ts_23_038_7bits_string_unpacked(scope, ptr, length);
3489}
3490
3491char *
3492tvb_get_etsi_ts_102_221_annex_a_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3493 const unsigned offset, unsigned length)
3494{
3495 const uint8_t *ptr;
3496
3497 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3497, "tvb && tvb->initialized"
))))
;
3498
3499 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3500 return (char*)get_etsi_ts_102_221_annex_a_string(scope, ptr, length);
3501}
3502
3503char *
3504tvb_get_ascii_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3505 const unsigned bit_offset, unsigned no_of_chars)
3506{
3507 unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3508 unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3509 const uint8_t *ptr;
3510
3511 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3511, "tvb && tvb->initialized"
))))
;
3512
3513 ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3514 return (char*)get_ascii_7bits_string(scope, ptr, bit_offset, no_of_chars);
3515}
3516
3517/*
3518 * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3519 * table, treat the string of bytes referred to by the tvbuff, the offset,
3520 * and the length as a string encoded using one octet per character, with
3521 * octets being mapped by the translation table to 2-byte Unicode Basic
3522 * Multilingual Plane characters (including REPLACEMENT CHARACTER), and
3523 * return a pointer to a UTF-8 string, allocated with the wmem scope.
3524 */
3525static uint8_t *
3526tvb_get_nonascii_unichar2_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[256])
3527{
3528 const uint8_t *ptr;
3529
3530 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3531 return get_nonascii_unichar2_string(scope, ptr, length, table);
3532}
3533
3534/*
3535 * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3536 * referred to by the tvbuff, offset, and length as a GB18030 encoded string,
3537 * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3538 * converted having substituted REPLACEMENT CHARACTER according to the
3539 * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3540 * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3541 *
3542 * As expected, this will also decode GBK and GB2312 strings.
3543 */
3544static uint8_t *
3545tvb_get_gb18030_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3546{
3547 const uint8_t *ptr;
3548
3549 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3550 return get_gb18030_string(scope, ptr, length);
3551}
3552
3553/*
3554 * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3555 * referred to by the tvbuff, offset, and length as a EUC-KR encoded string,
3556 * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3557 * converted having substituted REPLACEMENT CHARACTER according to the
3558 * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3559 * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3560 */
3561static uint8_t *
3562tvb_get_euc_kr_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3563{
3564 const uint8_t *ptr;
3565
3566 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3567 return get_euc_kr_string(scope, ptr, length);
3568}
3569
3570static uint8_t *
3571tvb_get_t61_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3572{
3573 const uint8_t *ptr;
3574
3575 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3576 return get_t61_string(scope, ptr, length);
3577}
3578
3579/*
3580 * Encoding tables for BCD strings.
3581 */
3582static const dgt_set_t Dgt0_9_bcd = {
3583 {
3584 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3585 '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?'
3586 }
3587};
3588
3589static const dgt_set_t Dgt_keypad_abc_tbcd = {
3590 {
3591 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3592 '0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'
3593 }
3594};
3595
3596static const dgt_set_t Dgt_ansi_tbcd = {
3597 {
3598 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3599 '0','1','2','3','4','5','6','7','8','9','?','B','C','*','#','?'
3600 }
3601};
3602
3603static const dgt_set_t Dgt_dect_standard_4bits_tbcd = {
3604 {
3605 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3606 '0','1','2','3','4','5','6','7','8','9','?',' ','?','?','?','?'
3607 }
3608};
3609
3610static uint8_t *
3611tvb_get_apn_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3612 unsigned length)
3613{
3614 wmem_strbuf_t *str;
3615
3616 /*
3617 * This is a domain name.
3618 *
3619 * 3GPP TS 23.003, section 19.4.2 "Fully Qualified Domain Names
3620 * (FQDNs)", subsection 19.4.2.1 "General", says:
3621 *
3622 * The encoding of any identifier used as part of a Fully
3623 * Qualified Domain Name (FQDN) shall follow the Name Syntax
3624 * defined in IETF RFC 2181 [18], IETF RFC 1035 [19] and
3625 * IETF RFC 1123 [20]. An FQDN consists of one or more
3626 * labels. Each label is coded as a one octet length field
3627 * followed by that number of octets coded as 8 bit ASCII
3628 * characters.
3629 *
3630 * so this does not appear to use full-blown DNS compression -
3631 * the upper 2 bits of the length don't indicate that it's a
3632 * pointer or an extended label (RFC 2673).
3633 */
3634 str = wmem_strbuf_new_sized(scope, length + 1);
3635 if (length > 0) {
3636 const uint8_t *ptr;
3637
3638 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3639
3640 for (;;) {
3641 unsigned label_len;
3642
3643 /*
3644 * Process this label.
3645 */
3646 label_len = *ptr;
3647 ptr++;
3648 length--;
3649
3650 while (label_len != 0) {
3651 uint8_t ch;
3652
3653 if (length == 0)
3654 goto end;
3655
3656 ch = *ptr;
3657 if (ch < 0x80)
3658 wmem_strbuf_append_c(str, ch);
3659 else
3660 wmem_strbuf_append_unichar_repl(str)wmem_strbuf_append_unichar(str, 0x00FFFD);
3661 ptr++;
3662 label_len--;
3663 length--;
3664 }
3665
3666 if (length == 0)
3667 goto end;
3668
3669 wmem_strbuf_append_c(str, '.');
3670 }
3671 }
3672
3673end:
3674 return (uint8_t *) wmem_strbuf_finalize(str);
3675}
3676
3677static uint8_t *
3678tvb_get_dect_standard_8bits_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3679{
3680 const uint8_t *ptr;
3681
3682 ptr = ensure_contiguous_unsigned(tvb, offset, length);
3683 return get_dect_standard_8bits_string(scope, ptr, length);
3684}
3685
3686/*
3687 * Given a tvbuff, an offset, a length, and an encoding, allocate a
3688 * buffer big enough to hold a non-null-terminated string of that length
3689 * at that offset, plus a trailing '\0', copy into the buffer the
3690 * string as converted from the appropriate encoding to UTF-8, and
3691 * return a pointer to the string.
3692 */
3693uint8_t *
3694tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3695 const unsigned length, const unsigned encoding)
3696{
3697 uint8_t *strptr;
3698 bool_Bool odd, skip_first;
3699
3700 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3700, "tvb && tvb->initialized"
))))
;
3701
3702 switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
3703
3704 case ENC_ASCII0x00000000:
3705 default:
3706 /*
3707 * For now, we treat bogus values as meaning
3708 * "ASCII" rather than reporting an error,
3709 * for the benefit of old dissectors written
3710 * when the last argument to proto_tree_add_item()
3711 * was a bool for the byte order, not an
3712 * encoding value, and passed non-zero values
3713 * other than true to mean "little-endian".
3714 */
3715 strptr = tvb_get_ascii_string(scope, tvb, offset, length);
3716 break;
3717
3718 case ENC_UTF_80x00000002:
3719 strptr = tvb_get_utf_8_string(scope, tvb, offset, length);
3720 break;
3721
3722 case ENC_UTF_160x00000004:
3723 strptr = tvb_get_utf_16_string(scope, tvb, offset, length,
3724 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3725 break;
3726
3727 case ENC_UCS_20x00000006:
3728 strptr = tvb_get_ucs_2_string(scope, tvb, offset, length,
3729 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3730 break;
3731
3732 case ENC_UCS_40x00000008:
3733 strptr = (uint8_t*)tvb_get_ucs_4_string(scope, tvb, offset, length,
3734 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3735 break;
3736
3737 case ENC_ISO_8859_10x0000000A:
3738 /*
3739 * ISO 8859-1 printable code point values are equal
3740 * to the equivalent Unicode code point value, so
3741 * no translation table is needed.
3742 */
3743 strptr = tvb_get_string_8859_1(scope, tvb, offset, length);
3744 break;
3745
3746 case ENC_ISO_8859_20x0000000C:
3747 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_2);
3748 break;
3749
3750 case ENC_ISO_8859_30x0000000E:
3751 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_3);
3752 break;
3753
3754 case ENC_ISO_8859_40x00000010:
3755 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_4);
3756 break;
3757
3758 case ENC_ISO_8859_50x00000012:
3759 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_5);
3760 break;
3761
3762 case ENC_ISO_8859_60x00000014:
3763 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_6);
3764 break;
3765
3766 case ENC_ISO_8859_70x00000016:
3767 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_7);
3768 break;
3769
3770 case ENC_ISO_8859_80x00000018:
3771 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_8);
3772 break;
3773
3774 case ENC_ISO_8859_90x0000001A:
3775 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_9);
3776 break;
3777
3778 case ENC_ISO_8859_100x0000001C:
3779 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_10);
3780 break;
3781
3782 case ENC_ISO_8859_110x0000001E:
3783 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_11);
3784 break;
3785
3786 case ENC_ISO_8859_130x00000022:
3787 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_13);
3788 break;
3789
3790 case ENC_ISO_8859_140x00000024:
3791 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_14);
3792 break;
3793
3794 case ENC_ISO_8859_150x00000026:
3795 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_15);
3796 break;
3797
3798 case ENC_ISO_8859_160x00000028:
3799 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_16);
3800 break;
3801
3802 case ENC_WINDOWS_12500x0000002A:
3803 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1250);
3804 break;
3805
3806 case ENC_WINDOWS_12510x0000003C:
3807 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1251);
3808 break;
3809
3810 case ENC_WINDOWS_12520x0000003A:
3811 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1252);
3812 break;
3813
3814 case ENC_MAC_ROMAN0x00000030:
3815 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_mac_roman);
3816 break;
3817
3818 case ENC_CP4370x00000032:
3819 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp437);
3820 break;
3821
3822 case ENC_CP8550x0000003E:
3823 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp855);
3824 break;
3825
3826 case ENC_CP8660x00000040:
3827 strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp866);
3828 break;
3829
3830 case ENC_ISO_646_BASIC0x00000042:
3831 strptr = tvb_get_iso_646_string(scope, tvb, offset, length, charset_table_iso_646_basic);
3832 break;
3833
3834 case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
3835 {
3836 unsigned bit_offset = offset << 3;
3837 unsigned no_of_chars = (length << 3) / 7;
3838 strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_packed(scope, tvb, bit_offset, no_of_chars);
3839 }
3840 break;
3841
3842 case ENC_ASCII_7BITS0x00000034:
3843 {
3844 unsigned bit_offset = offset << 3;
3845 unsigned no_of_chars = (length << 3) / 7;
3846 strptr = (uint8_t*)tvb_get_ascii_7bits_string(scope, tvb, bit_offset, no_of_chars);
3847 }
3848 break;
3849
3850 case ENC_EBCDIC0x0000002E:
3851 /*
3852 * "Common" EBCDIC, covering all characters with the
3853 * same code point in all Roman-alphabet EBCDIC code
3854 * pages.
3855 */
3856 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic);
3857 break;
3858
3859 case ENC_EBCDIC_CP0370x00000038:
3860 /*
3861 * EBCDIC code page 037.
3862 */
3863 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp037);
3864 break;
3865
3866 case ENC_EBCDIC_CP5000x00000060:
3867 /*
3868 * EBCDIC code page 500.
3869 */
3870 strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp500);
3871 break;
3872
3873 case ENC_T610x00000036:
3874 strptr = tvb_get_t61_string(scope, tvb, offset, length);
3875 break;
3876
3877 case ENC_BCD_DIGITS_0_90x00000044:
3878 /*
3879 * Packed BCD, with digits 0-9.
3880 */
3881 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3882 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3883 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt0_9_bcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3884 break;
3885
3886 case ENC_KEYPAD_ABC_TBCD0x00000046:
3887 /*
3888 * Keypad-with-a/b/c "telephony BCD" - packed BCD, with
3889 * digits 0-9 and symbols *, #, a, b, and c.
3890 */
3891 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3892 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3893 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_keypad_abc_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3894 break;
3895
3896 case ENC_KEYPAD_BC_TBCD0x00000048:
3897 /*
3898 * Keypad-with-B/C "telephony BCD" - packed BCD, with
3899 * digits 0-9 and symbols B, C, *, and #.
3900 */
3901 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3902 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3903 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_ansi_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3904 break;
3905
3906 case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
3907 strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_unpacked(scope, tvb, offset, length);
3908 break;
3909
3910 case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
3911 strptr = (uint8_t*)tvb_get_etsi_ts_102_221_annex_a_string(scope, tvb, offset, length);
3912 break;
3913
3914 case ENC_GB180300x00000050:
3915 strptr = tvb_get_gb18030_string(scope, tvb, offset, length);
3916 break;
3917
3918 case ENC_EUC_KR0x00000052:
3919 strptr = tvb_get_euc_kr_string(scope, tvb, offset, length);
3920 break;
3921
3922 case ENC_APN_STR0x00000054:
3923 strptr = tvb_get_apn_string(scope, tvb, offset, length);
3924 break;
3925
3926 case ENC_DECT_STANDARD_8BITS0x00000056:
3927 strptr = tvb_get_dect_standard_8bits_string(scope, tvb, offset, length);
3928 break;
3929
3930 case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
3931 /*
3932 * DECT standard 4bits "telephony BCD" - packed BCD, with
3933 * digits 0-9 and symbol SPACE for 0xb.
3934 */
3935 odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3936 skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3937 strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_dect_standard_4bits_tbcd, skip_first, odd, false0);
3938 break;
3939 }
3940 return strptr;
3941}
3942
3943/*
3944 * This is like tvb_get_string_enc(), except that it handles null-padded
3945 * strings.
3946 *
3947 * Currently, string values are stored as UTF-8 null-terminated strings,
3948 * so nothing needs to be done differently for null-padded strings; we
3949 * could save a little memory by not storing the null padding.
3950 *
3951 * If we ever store string values differently, in a fashion that doesn't
3952 * involve null termination, that might change.
3953 */
3954uint8_t *
3955tvb_get_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3956 const unsigned length, const unsigned encoding)
3957{
3958 return tvb_get_string_enc(scope, tvb, offset, length, encoding);
3959}
3960
3961/*
3962 * These routines are like the above routines, except that they handle
3963 * null-terminated strings. They find the length of that string (and
3964 * throw an exception if the tvbuff ends before we find the null), and
3965 * also return through a pointer the length of the string, in bytes,
3966 * including the terminating null (the terminating null being 2 bytes
3967 * for UCS-2 and UTF-16, 4 bytes for UCS-4, and 1 byte for other
3968 * encodings).
3969 */
3970static uint8_t *
3971tvb_get_ascii_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3972{
3973 unsigned size;
3974 const uint8_t *ptr;
3975
3976 size = tvb_strsize(tvb, offset);
3977 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3978 if (lengthp)
3979 *lengthp = size;
3980 return get_ascii_string(scope, ptr, size);
3981}
3982
3983static uint8_t *
3984tvb_get_iso_646_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
3985{
3986 unsigned size;
3987 const uint8_t *ptr;
3988
3989 size = tvb_strsize(tvb, offset);
3990 ptr = ensure_contiguous_unsigned(tvb, offset, size);
3991 if (lengthp)
3992 *lengthp = size;
3993 return get_iso_646_string(scope, ptr, size, table);
3994}
3995
3996static uint8_t *
3997tvb_get_utf_8_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3998{
3999 unsigned size;
4000 const uint8_t *ptr;
4001
4002 size = tvb_strsize(tvb, offset);
4003 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4004 if (lengthp)
4005 *lengthp = size;
4006 return get_utf_8_string(scope, ptr, size);
4007}
4008
4009static uint8_t *
4010tvb_get_stringz_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4011{
4012 unsigned size;
4013 const uint8_t *ptr;
4014
4015 size = tvb_strsize(tvb, offset);
4016 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4017 if (lengthp)
4018 *lengthp = size;
4019 return get_8859_1_string(scope, ptr, size);
4020}
4021
4022static uint8_t *
4023tvb_get_stringz_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
4024{
4025 unsigned size;
4026 const uint8_t *ptr;
4027
4028 size = tvb_strsize(tvb, offset);
4029 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4030 if (lengthp)
4031 *lengthp = size;
4032 return get_unichar2_string(scope, ptr, size, table);
4033}
4034
4035/*
4036 * Given a tvbuff and an offset, with the offset assumed to refer to
4037 * a null-terminated string, find the length of that string (and throw
4038 * an exception if the tvbuff ends before we find the null), ensure that
4039 * the TVB is flat, and return a pointer to the string (in the TVB).
4040 * Also return the length of the string (including the terminating null)
4041 * through a pointer.
4042 *
4043 * As long as we aren't using composite TVBs, this saves the cycles used
4044 * (often unnecessarily) in allocating a buffer and copying the string into
4045 * it. OTOH, the string returned isn't valid UTF-8, so it shouldn't be
4046 * added to the tree, the columns, etc., just used with various other
4047 * functions that operate on strings that don't have a tvb_ equivalent.
4048 * That's hard to enforce, which is why this is deprecated.
4049 */
4050const uint8_t *
4051tvb_get_const_stringz(tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4052{
4053 unsigned size;
4054 const uint8_t *strptr;
4055
4056 size = tvb_strsize(tvb, offset);
4057 strptr = ensure_contiguous_unsigned(tvb, offset, size);
4058 if (lengthp)
4059 *lengthp = size;
4060 return strptr;
4061}
4062
4063static char *
4064tvb_get_ucs_2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4065{
4066 unsigned size; /* Number of bytes in string */
4067 const uint8_t *ptr;
4068
4069 size = tvb_unicode_strsize(tvb, offset);
4070 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4071 if (lengthp)
4072 *lengthp = size;
4073 return (char*)get_ucs_2_string(scope, ptr, size, encoding);
4074}
4075
4076static char *
4077tvb_get_utf_16_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4078{
4079 unsigned size;
4080 const uint8_t *ptr;
4081
4082 size = tvb_unicode_strsize(tvb, offset);
4083 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4084 if (lengthp)
4085 *lengthp = size;
4086 return (char*)get_utf_16_string(scope, ptr, size, encoding);
4087}
4088
4089static char *
4090tvb_get_ucs_4_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4091{
4092 unsigned size;
4093 const uint8_t *ptr;
4094
4095 size = tvb_ucs_4_strsize(tvb, offset);
4096
4097 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4098 if (lengthp)
4099 *lengthp = size;
4100 return (char*)get_ucs_4_string(scope, ptr, size, encoding);
4101}
4102
4103static uint8_t *
4104tvb_get_nonascii_unichar2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[256])
4105{
4106 unsigned size;
4107 const uint8_t *ptr;
4108
4109 size = tvb_strsize(tvb, offset);
4110 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4111 if (lengthp)
4112 *lengthp = size;
4113 return get_nonascii_unichar2_string(scope, ptr, size, table);
4114}
4115
4116static uint8_t *
4117tvb_get_t61_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4118{
4119 unsigned size;
4120 const uint8_t *ptr;
4121
4122 size = tvb_strsize(tvb, offset);
4123 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4124 if (lengthp)
4125 *lengthp = size;
4126 return get_t61_string(scope, ptr, size);
4127}
4128
4129static uint8_t *
4130tvb_get_gb18030_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4131{
4132 unsigned size;
4133 const uint8_t *ptr;
4134
4135 size = tvb_strsize(tvb, offset);
4136 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4137 if (lengthp)
4138 *lengthp = size;
4139 return get_gb18030_string(scope, ptr, size);
4140}
4141
4142static uint8_t *
4143tvb_get_euc_kr_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4144{
4145 unsigned size;
4146 const uint8_t *ptr;
4147
4148 size = tvb_strsize(tvb, offset);
4149 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4150 if (lengthp)
4151 *lengthp = size;
4152 return get_euc_kr_string(scope, ptr, size);
4153}
4154
4155static uint8_t *
4156tvb_get_dect_standard_8bits_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4157{
4158 unsigned size;
4159 const uint8_t *ptr;
4160
4161 size = tvb_strsize(tvb, offset);
4162 ptr = ensure_contiguous_unsigned(tvb, offset, size);
4163 if (lengthp)
4164 *lengthp = size;
4165 return get_dect_standard_8bits_string(scope, ptr, size);
4166}
4167
4168uint8_t *
4169tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4170{
4171 uint8_t *strptr;
4172
4173 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4173, "tvb && tvb->initialized"
))))
;
4174
4175 switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
4176
4177 case ENC_ASCII0x00000000:
4178 default:
4179 /*
4180 * For now, we treat bogus values as meaning
4181 * "ASCII" rather than reporting an error,
4182 * for the benefit of old dissectors written
4183 * when the last argument to proto_tree_add_item()
4184 * was a bool for the byte order, not an
4185 * encoding value, and passed non-zero values
4186 * other than true to mean "little-endian".
4187 */
4188 strptr = tvb_get_ascii_stringz(scope, tvb, offset, lengthp);
4189 break;
4190
4191 case ENC_UTF_80x00000002:
4192 strptr = tvb_get_utf_8_stringz(scope, tvb, offset, lengthp);
4193 break;
4194
4195 case ENC_UTF_160x00000004:
4196 strptr = (uint8_t*)tvb_get_utf_16_stringz(scope, tvb, offset, lengthp,
4197 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4198 break;
4199
4200 case ENC_UCS_20x00000006:
4201 strptr = (uint8_t*)tvb_get_ucs_2_stringz(scope, tvb, offset, lengthp,
4202 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4203 break;
4204
4205 case ENC_UCS_40x00000008:
4206 strptr = (uint8_t*)tvb_get_ucs_4_stringz(scope, tvb, offset, lengthp,
4207 encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4208 break;
4209
4210 case ENC_ISO_8859_10x0000000A:
4211 /*
4212 * ISO 8859-1 printable code point values are equal
4213 * to the equivalent Unicode code point value, so
4214 * no translation table is needed.
4215 */
4216 strptr = tvb_get_stringz_8859_1(scope, tvb, offset, lengthp);
4217 break;
4218
4219 case ENC_ISO_8859_20x0000000C:
4220 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_2);
4221 break;
4222
4223 case ENC_ISO_8859_30x0000000E:
4224 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_3);
4225 break;
4226
4227 case ENC_ISO_8859_40x00000010:
4228 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_4);
4229 break;
4230
4231 case ENC_ISO_8859_50x00000012:
4232 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_5);
4233 break;
4234
4235 case ENC_ISO_8859_60x00000014:
4236 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_6);
4237 break;
4238
4239 case ENC_ISO_8859_70x00000016:
4240 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_7);
4241 break;
4242
4243 case ENC_ISO_8859_80x00000018:
4244 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_8);
4245 break;
4246
4247 case ENC_ISO_8859_90x0000001A:
4248 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_9);
4249 break;
4250
4251 case ENC_ISO_8859_100x0000001C:
4252 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_10);
4253 break;
4254
4255 case ENC_ISO_8859_110x0000001E:
4256 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_11);
4257 break;
4258
4259 case ENC_ISO_8859_130x00000022:
4260 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_13);
4261 break;
4262
4263 case ENC_ISO_8859_140x00000024:
4264 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_14);
4265 break;
4266
4267 case ENC_ISO_8859_150x00000026:
4268 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_15);
4269 break;
4270
4271 case ENC_ISO_8859_160x00000028:
4272 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_16);
4273 break;
4274
4275 case ENC_WINDOWS_12500x0000002A:
4276 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1250);
4277 break;
4278
4279 case ENC_WINDOWS_12510x0000003C:
4280 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1251);
4281 break;
4282
4283 case ENC_WINDOWS_12520x0000003A:
4284 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1252);
4285 break;
4286
4287 case ENC_MAC_ROMAN0x00000030:
4288 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_mac_roman);
4289 break;
4290
4291 case ENC_CP4370x00000032:
4292 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp437);
4293 break;
4294
4295 case ENC_CP8550x0000003E:
4296 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp855);
4297 break;
4298
4299 case ENC_CP8660x00000040:
4300 strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp866);
4301 break;
4302
4303 case ENC_ISO_646_BASIC0x00000042:
4304 strptr = tvb_get_iso_646_stringz(scope, tvb, offset, lengthp, charset_table_iso_646_basic);
4305 break;
4306
4307 case ENC_BCD_DIGITS_0_90x00000044:
4308 case ENC_KEYPAD_ABC_TBCD0x00000046:
4309 case ENC_KEYPAD_BC_TBCD0x00000048:
4310 case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
4311 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."
)
;
4312 break;
4313
4314 case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
4315 case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
4316 case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
4317 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"
)
;
4318 break;
4319
4320 case ENC_ASCII_7BITS0x00000034:
4321 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"
)
;
4322 break;
4323
4324 case ENC_EBCDIC0x0000002E:
4325 /*
4326 * "Common" EBCDIC, covering all characters with the
4327 * same code point in all Roman-alphabet EBCDIC code
4328 * pages.
4329 */
4330 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic);
4331 break;
4332
4333 case ENC_EBCDIC_CP0370x00000038:
4334 /*
4335 * EBCDIC code page 037.
4336 */
4337 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp037);
4338 break;
4339
4340 case ENC_EBCDIC_CP5000x00000060:
4341 /*
4342 * EBCDIC code page 500.
4343 */
4344 strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp500);
4345 break;
4346
4347 case ENC_T610x00000036:
4348 strptr = tvb_get_t61_stringz(scope, tvb, offset, lengthp);
4349 break;
4350
4351 case ENC_GB180300x00000050:
4352 strptr = tvb_get_gb18030_stringz(scope, tvb, offset, lengthp);
4353 break;
4354
4355 case ENC_EUC_KR0x00000052:
4356 strptr = tvb_get_euc_kr_stringz(scope, tvb, offset, lengthp);
4357 break;
4358
4359 case ENC_APN_STR0x00000054:
4360 /* At least as defined in 3GPP TS 23.003 Clause 9.1, null-termination
4361 * does make sense as internal nulls are not allowed. */
4362 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"
)
;
4363 break;
4364
4365 case ENC_DECT_STANDARD_8BITS0x00000056:
4366 strptr = tvb_get_dect_standard_8bits_stringz(scope, tvb, offset, lengthp);
4367 break;
4368 }
4369
4370 return strptr;
4371}
4372
4373/* Looks for a stringz (NUL-terminated string) in tvbuff and copies
4374 * no more than bufsize number of bytes, including terminating NUL, to buffer.
4375 * Returns length of string (not including terminating NUL).
4376 * In this way, it acts like snprintf().
4377 *
4378 * bufsize MUST be greater than 0.
4379 *
4380 * This function does not otherwise throw an exception for running out of room
4381 * in the buffer or running out of remaining bytes in the tvbuffer. It will
4382 * copy as many bytes to the buffer as possible (the lesser of bufsize - 1
4383 * and the number of remaining captured bytes) and then NUL terminate the
4384 * string.
4385 *
4386 * *bytes_copied will contain the number of bytes actually copied,
4387 * including the terminating-NUL if present in the frame, but not
4388 * if it was supplied by the function instead of copied from packet data.
4389 * [Not currently used, but could be used to determine how much to advance
4390 * the offset.]
4391 */
4392static unsigned
4393_tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const unsigned offset, const unsigned bufsize, uint8_t* buffer, unsigned *bytes_copied)
4394{
4395 int exception;
4396 int stringlen;
4397 unsigned limit;
4398 unsigned len = 0;
4399
4400 /* Only read to end of tvbuff, w/o throwing exception. */
4401 exception = validate_offset_and_remaining(tvb, offset, &len);
4402 if (exception)
4403 THROW(exception)except_throw(1, (exception), ((void*)0));
4404
4405 /* There must at least be room for the terminating NUL. */
4406 DISSECTOR_ASSERT(bufsize != 0)((void) ((bufsize != 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4406, "bufsize != 0"
))))
;
4407
4408 /* If there's no room for anything else, just return the NUL. */
4409 if (bufsize == 1) {
4410 buffer[0] = 0;
4411 if (len && tvb_get_uint8(tvb, offset) == 0) {
4412 *bytes_copied = 1;
4413 } else {
4414 *bytes_copied = 0;
4415 }
4416 return 0;
4417 }
4418
4419 /* validate_offset_and_remaining() won't throw an exception if we're
4420 * looking at the byte immediately after the end of the tvbuff. */
4421 if (len == 0) {
4422 THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
4423 }
4424
4425 if (len < bufsize) {
4426 limit = len;
4427 }
4428 else {
4429 limit = bufsize - 1;
4430 }
4431
4432 stringlen = tvb_strnlen(tvb, offset, limit);
4433 /* If NUL wasn't found, copy the data up to the limit and terminate */
4434 if (stringlen == -1) {
4435 tvb_memcpy(tvb, buffer, offset, limit);
4436 buffer[limit] = 0;
4437 *bytes_copied = limit;
4438 return limit;
4439 }
4440
4441 /* Copy the string to buffer */
4442 tvb_memcpy(tvb, buffer, offset, stringlen + 1);
4443 *bytes_copied = stringlen + 1;
4444 return (unsigned)stringlen;
4445}
4446
4447unsigned
4448tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const unsigned offset, const unsigned bufsize, uint8_t* buffer)
4449{
4450 unsigned bytes_copied;
4451
4452 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4452, "tvb && tvb->initialized"
))))
;
4453
4454 return _tvb_get_raw_bytes_as_stringz(tvb, offset, bufsize, buffer, &bytes_copied);
4455}
4456
4457/*
4458 * Given a tvbuff, an offset into the tvbuff, a buffer, and a buffer size,
4459 * extract as many raw bytes from the tvbuff, starting at the offset,
4460 * as 1) are available in the tvbuff and 2) will fit in the buffer, leaving
4461 * room for a terminating NUL.
4462 */
4463unsigned
4464tvb_get_raw_bytes_as_string(tvbuff_t *tvb, const unsigned offset, char *buffer, size_t bufsize)
4465{
4466 unsigned len = 0;
4467
4468 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4468, "tvb && tvb->initialized"
))))
;
4469
4470 /* There must be room for the string and the terminating NUL. */
4471 DISSECTOR_ASSERT(bufsize > 0)((void) ((bufsize > 0) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4471, "bufsize > 0"
))))
;
4472
4473 /* bufsize is size_t, but tvbuffers only have up to unsigned bytes */
4474 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"
, 4474, "bufsize - 1 < (2147483647 *2U +1U)"))))
;
4475
4476 len = _tvb_captured_length_remaining(tvb, offset);
4477 if (len == 0) {
4478 buffer[0] = '\0';
4479 return 0;
4480 }
4481 if (len > (bufsize - 1))
4482 len = (unsigned)(bufsize - 1);
4483
4484 /* Copy the string to buffer */
4485 tvb_memcpy(tvb, buffer, offset, len);
4486 buffer[len] = '\0';
4487 return len;
4488}
4489
4490bool_Bool
4491tvb_ascii_isprint(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_isprint(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_PRINT) != 0))
4501 return false0;
4502
4503 return true1;
4504}
4505
4506bool_Bool
4507tvb_ascii_isprint_remaining(tvbuff_t *tvb, const unsigned offset)
4508{
4509 int exception;
4510 unsigned length;
4511
4512 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4512, "tvb && tvb->initialized"
))))
;
4513
4514 exception = validate_offset_and_remaining(tvb, offset, &length);
4515 if (exception)
4516 THROW(exception)except_throw(1, (exception), ((void*)0));
4517
4518 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4519
4520 for (unsigned i = 0; i < length; i++, buf++)
4521 if (!g_ascii_isprint(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_PRINT) != 0))
4522 return false0;
4523
4524 return true1;
4525}
4526
4527bool_Bool
4528tvb_utf_8_isprint(tvbuff_t *tvb, const unsigned offset, const unsigned length)
4529{
4530 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4530, "tvb && tvb->initialized"
))))
;
4531
4532 /* XXX - Perhaps this function should return false instead of throwing
4533 * an exception. */
4534 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4535
4536 return isprint_utf8_string((const char*)buf, length);
4537}
4538
4539bool_Bool
4540tvb_utf_8_isprint_remaining(tvbuff_t *tvb, const unsigned offset)
4541{
4542 int exception;
4543 unsigned length;
4544
4545 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4545, "tvb && tvb->initialized"
))))
;
4546
4547 exception = validate_offset_and_remaining(tvb, offset, &length);
4548 if (exception)
4549 THROW(exception)except_throw(1, (exception), ((void*)0));
4550
4551 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4552
4553 return isprint_utf8_string((const char*)buf, length);
4554}
4555
4556bool_Bool
4557tvb_ascii_isdigit(tvbuff_t *tvb, const unsigned offset, const unsigned length)
4558{
4559 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4559, "tvb && tvb->initialized"
))))
;
4560
4561 /* XXX - Perhaps this function should return false instead of throwing
4562 * an exception. */
4563 const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4564
4565 for (unsigned i = 0; i < length; i++, buf++)
4566 if (!g_ascii_isdigit(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_DIGIT) != 0))
4567 return false0;
4568
4569 return true1;
4570}
4571
4572static ws_mempbrk_pattern pbrk_crlf;
4573
4574static bool_Bool
4575_tvb_find_line_end_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, unsigned *linelen, unsigned *next_offset)
4576{
4577 static bool_Bool compiled = false0;
4578 unsigned eob_offset;
4579 unsigned eol_offset;
4580 unsigned char found_needle = 0;
4581
4582 if (!compiled) {
4583 ws_mempbrk_compile(&pbrk_crlf, "\r\n");
4584 compiled = true1;
4585 }
4586
4587 eob_offset = offset + limit;
4588
4589 /*
4590 * Look either for a CR or an LF.
4591 */
4592 if (!_tvb_ws_mempbrk_uint8_length(tvb, offset, limit, &pbrk_crlf, &eol_offset, &found_needle)) {
4593 /*
4594 * No CR or LF - line is presumably continued in next packet.
4595 */
4596 /*
4597 * Pretend the line runs to the end of the tvbuff.
4598 */
4599 if (linelen)
4600 *linelen = eob_offset - offset;
4601 if (next_offset)
4602 *next_offset = eob_offset;
4603 /*
4604 * Tell our caller we saw no EOL, so they can try to
4605 * desegment and get the entire line into one tvbuff.
4606 */
4607 return false0;
4608 } else {
4609 /*
4610 * Find the number of bytes between the starting offset
4611 * and the CR or LF.
4612 */
4613 if (linelen)
4614 *linelen = eol_offset - offset;
4615
4616 /*
4617 * Is it a CR?
4618 */
4619 if (found_needle == '\r') {
4620 /*
4621 * Yes - is it followed by an LF?
4622 */
4623 if (eol_offset + 1 >= eob_offset) {
4624 /*
4625 * Dunno - the next byte isn't in this
4626 * tvbuff.
4627 */
4628 if (next_offset)
4629 *next_offset = eob_offset;
4630 /*
4631 * We'll return false, although that
4632 * runs the risk that if the line
4633 * really *is* terminated with a CR,
4634 * we won't properly dissect this
4635 * tvbuff.
4636 *
4637 * It's probably more likely that
4638 * the line ends with CR-LF than
4639 * that it ends with CR by itself.
4640 *
4641 * XXX - Return a third value?
4642 */
4643 return false0;
4644 } else {
4645 /*
4646 * Well, we can at least look at the next
4647 * byte.
4648 */
4649 if (tvb_get_uint8(tvb, eol_offset + 1) == '\n') {
4650 /*
4651 * It's an LF; skip over the CR.
4652 */
4653 eol_offset++;
4654 }
4655 }
4656 }
4657
4658 /*
4659 * Return the offset of the character after the last
4660 * character in the line, skipping over the last character
4661 * in the line terminator.
4662 */
4663 if (next_offset)
4664 *next_offset = eol_offset + 1;
4665 }
4666 return true1;
4667}
4668
4669bool_Bool
4670tvb_find_line_end_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *linelen, unsigned *next_offset)
4671{
4672 unsigned limit;
4673 int exception;
4674
4675 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4675, "tvb && tvb->initialized"
))))
;
4676
4677 exception = validate_offset_and_remaining(tvb, offset, &limit);
4678 if (exception)
4679 THROW(exception)except_throw(1, (exception), ((void*)0));
4680
4681 return _tvb_find_line_end_length(tvb, offset, limit, linelen, next_offset);
4682}
4683
4684bool_Bool
4685tvb_find_line_end_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *linelen, unsigned *next_offset)
4686{
4687 unsigned limit;
4688 int exception;
4689
4690 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4690, "tvb && tvb->initialized"
))))
;
4691
4692 exception = validate_offset_and_remaining(tvb, offset, &limit);
4693 if (exception)
4694 THROW(exception)except_throw(1, (exception), ((void*)0));
4695
4696 /* Only search to end of tvbuff, w/o throwing exception. */
4697 if (limit > maxlength) {
4698 /* Maximum length doesn't go past end of tvbuff; search
4699 to that value. */
4700 limit = maxlength;
4701 }
4702
4703 return _tvb_find_line_end_length(tvb, offset, limit, linelen, next_offset);
4704}
4705
4706static ws_mempbrk_pattern pbrk_crlf_dquote;
4707
4708static bool_Bool
4709_tvb_find_line_end_unquoted_length(tvbuff_t *tvb, const unsigned offset, unsigned limit, unsigned *linelen, unsigned *next_offset)
4710{
4711 unsigned cur_offset, char_offset;
4712 bool_Bool is_quoted;
4713 unsigned char c = 0;
4714 unsigned eob_offset;
4715 static bool_Bool compiled = false0;
4716 unsigned len;
4717 bool_Bool found;
4718
4719 if (!compiled) {
4720 ws_mempbrk_compile(&pbrk_crlf_dquote, "\r\n\"");
4721 compiled = true1;
4722 }
4723
4724 eob_offset = offset + limit;
4725
4726 cur_offset = offset;
4727 is_quoted = false0;
4728 for (;;) {
4729 len = limit - (cur_offset - offset);
4730 /*
4731 * Is this part of the string quoted?
4732 */
4733 if (is_quoted) {
4734 /*
4735 * Yes - look only for the terminating quote.
4736 */
4737 found = _tvb_find_uint8_length(tvb, cur_offset, len, '"', &char_offset);
4738 } else {
4739 /*
4740 * Look either for a CR, an LF, or a '"'.
4741 */
4742 found = _tvb_ws_mempbrk_uint8_length(tvb, cur_offset, len, &pbrk_crlf_dquote, &char_offset, &c);
4743 }
4744 if (!found) {
4745 /*
4746 * Not found - line is presumably continued in
4747 * next packet.
4748 * We pretend the line runs to the end of the tvbuff.
4749 */
4750 if (linelen)
4751 *linelen = eob_offset - offset;
4752 if (next_offset)
4753 *next_offset = eob_offset;
4754 break;
4755 }
4756
4757 if (is_quoted) {
4758 /*
4759 * We're processing a quoted string.
4760 * We only looked for ", so we know it's a ";
4761 * as we're processing a quoted string, it's a
4762 * closing quote.
4763 */
4764 is_quoted = false0;
4765 } else {
4766 /*
4767 * OK, what is it?
4768 */
4769 if (c == '"') {
4770 /*
4771 * Un-quoted "; it begins a quoted
4772 * string.
4773 */
4774 is_quoted = true1;
4775 } else {
4776 /*
4777 * It's a CR or LF; we've found a line
4778 * terminator.
4779 *
4780 * Find the number of bytes between the
4781 * starting offset and the CR or LF.
4782 */
4783 if (linelen)
4784 *linelen = char_offset - offset;
4785
4786 /*
4787 * Is it a CR?
4788 */
4789 if (c == '\r') {
4790 /*
4791 * Yes; is it followed by an LF?
4792 */
4793 if (char_offset + 1 < eob_offset &&
4794 tvb_get_uint8(tvb, char_offset + 1)
4795 == '\n') {
4796 /*
4797 * Yes; skip over the CR.
4798 */
4799 char_offset++;
4800 }
4801 }
4802
4803 /*
4804 * Return the offset of the character after
4805 * the last character in the line, skipping
4806 * over the last character in the line
4807 * terminator, and quit.
4808 */
4809 if (next_offset)
4810 *next_offset = char_offset + 1;
4811 break;
4812 }
4813 }
4814
4815 /*
4816 * Step past the character we found.
4817 */
4818 cur_offset = char_offset + 1;
4819 if (cur_offset >= eob_offset) {
4820 /*
4821 * The character we found was the last character
4822 * in the tvbuff - line is presumably continued in
4823 * next packet.
4824 * We pretend the line runs to the end of the tvbuff.
4825 */
4826 if (linelen)
4827 *linelen = eob_offset - offset;
4828 if (next_offset)
4829 *next_offset = eob_offset;
4830 break;
4831 }
4832 }
4833 return found;
4834}
4835
4836/*
4837 * Given a tvbuff, an offset into the tvbuff, and a length that starts
4838 * at that offset (which may be -1 for "all the way to the end of the
4839 * tvbuff"), find the end of the (putative) line that starts at the
4840 * specified offset in the tvbuff, going no further than the specified
4841 * length.
4842 *
4843 * However, treat quoted strings inside the buffer specially - don't
4844 * treat newlines in quoted strings as line terminators.
4845 *
4846 * Return the length of the line (not counting the line terminator at
4847 * the end), or the amount of data remaining in the buffer if we don't
4848 * find a line terminator.
4849 *
4850 * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4851 * character past the line terminator, or past the end of the buffer if
4852 * we don't find a line terminator.
4853 */
4854int
4855tvb_find_line_end_unquoted(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset)
4856{
4857 unsigned linelen;
4858 unsigned abs_next_offset;
4859 unsigned limit;
4860 int exception;
4861
4862 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4862, "tvb && tvb->initialized"
))))
;
4863
4864 exception = validate_offset_and_remaining(tvb, offset, &limit);
4865 if (exception)
4866 THROW(exception)except_throw(1, (exception), ((void*)0));
4867
4868 /* Only search to end of tvbuff, w/o throwing exception. */
4869 if (len >= 0 && limit > (unsigned) len) {
4870 /* Maximum length doesn't go past end of tvbuff; search
4871 to that value. */
4872 limit = (unsigned) len;
4873 }
4874
4875 _tvb_find_line_end_unquoted_length(tvb, offset, limit, &linelen, &abs_next_offset);
4876 if (next_offset) {
4877 *next_offset = (int)abs_next_offset;
4878 }
4879 return (int)linelen;
4880}
4881
4882bool_Bool
4883tvb_find_line_end_unquoted_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *linelen, unsigned *next_offset)
4884{
4885 unsigned limit;
4886 int exception;
4887
4888 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4888, "tvb && tvb->initialized"
))))
;
4889
4890 exception = validate_offset_and_remaining(tvb, offset, &limit);
4891 if (exception)
4892 THROW(exception)except_throw(1, (exception), ((void*)0));
4893
4894 return _tvb_find_line_end_unquoted_length(tvb, offset, limit, linelen, next_offset);
4895}
4896
4897bool_Bool
4898tvb_find_line_end_unquoted_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *linelen, unsigned *next_offset)
4899{
4900 unsigned limit;
4901 int exception;
4902
4903 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4903, "tvb && tvb->initialized"
))))
;
4904
4905 exception = validate_offset_and_remaining(tvb, offset, &limit);
4906 if (exception)
4907 THROW(exception)except_throw(1, (exception), ((void*)0));
4908
4909 /* Only search to end of tvbuff, w/o throwing exception. */
4910 if (limit > maxlength) {
4911 /* Maximum length doesn't go past end of tvbuff; search
4912 to that value. */
4913 limit = maxlength;
4914 }
4915
4916 return _tvb_find_line_end_unquoted_length(tvb, offset, limit, linelen, next_offset);
4917}
4918
4919/*
4920 * Copied from the mgcp dissector. (This function should be moved to /epan )
4921 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
4922 * character following offset or offset + maxlength -1 whichever
4923 * is smaller.
4924 *
4925 * Parameters:
4926 * tvb - The tvbuff in which we are skipping whitespace.
4927 * offset - The offset in tvb from which we begin trying to skip whitespace.
4928 * maxlength - The maximum distance from offset that we may try to skip
4929 * whitespace.
4930 *
4931 * Returns: The position in tvb of the first non-whitespace
4932 * character following offset or offset + maxlength -1 whichever
4933 * is smaller.
4934 */
4935unsigned
4936tvb_skip_wsp(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
4937{
4938 unsigned counter;
4939 unsigned end, tvb_len;
4940 uint8_t tempchar;
4941
4942 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4942, "tvb && tvb->initialized"
))))
;
4943
4944 /* Get the length remaining */
4945 /*tvb_len = tvb_captured_length(tvb);*/
4946 tvb_len = tvb->length;
4947
4948 if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
4949 end = tvb_len;
4950 }
4951
4952 /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
4953 /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
4954 * slightly faster but also tests for vertical tab and form feed. */
4955 for (counter = offset;
4956 counter < end &&
4957 ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
4958 tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
4959 counter++);
4960
4961 return counter;
4962}
4963
4964unsigned
4965tvb_skip_wsp_return(tvbuff_t *tvb, const unsigned offset)
4966{
4967 unsigned counter;
4968 uint8_t tempchar;
4969
4970 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4970, "tvb && tvb->initialized"
))))
;
4971
4972 /* XXX - DISSECTOR_ASSERT(offset > 0) and then subtract 1 from offset?
4973 * The way this is used the caller almost always wants to subtract one
4974 * from the offset of a non WSP separator, and they might forget to do
4975 * so and then this function return the offset past the separator. */
4976
4977 /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
4978 * slightly faster but also tests for vertical tab and form feed. */
4979 for (counter = offset; counter > 0 &&
4980 ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
4981 tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
4982 counter++;
4983
4984 return counter;
4985}
4986
4987unsigned
4988tvb_skip_uint8(tvbuff_t *tvb, unsigned offset, const unsigned maxlength, const uint8_t ch)
4989{
4990 unsigned end, tvb_len;
4991
4992 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4992, "tvb && tvb->initialized"
))))
;
4993
4994 /* Get the length remaining */
4995 /*tvb_len = tvb_captured_length(tvb);*/
4996 tvb_len = tvb->length;
4997
4998 if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
4999 end = tvb_len;
5000 }
5001
5002 while (offset < end) {
5003 uint8_t tempch = tvb_get_uint8(tvb, offset);
5004
5005 if (tempch != ch)
5006 break;
5007 offset++;
5008 }
5009
5010 return offset;
5011}
5012
5013static ws_mempbrk_pattern pbrk_whitespace;
5014
5015static bool_Bool
5016_tvb_get_token_len_length(tvbuff_t *tvb, const unsigned offset, unsigned limit, unsigned *tokenlen, unsigned *next_offset)
5017{
5018 unsigned eot_offset;
5019 unsigned char found_needle = 0;
5020 static bool_Bool compiled = false0;
5021
5022 if (!compiled) {
5023 ws_mempbrk_compile(&pbrk_whitespace, " \r\n");
5024 compiled = true1;
5025 }
5026
5027 /*
5028 * Look either for a space, CR, or LF.
5029 */
5030 if (!_tvb_ws_mempbrk_uint8_length(tvb, offset, limit, &pbrk_whitespace, &eot_offset, &found_needle)) {
5031 /*
5032 * No space, CR or LF - token is presumably continued in next packet.
5033 */
5034 /*
5035 * Pretend the token runs to the end of the tvbuff.
5036 */
5037 if (tokenlen)
5038 *tokenlen = eot_offset - offset;
5039 if (next_offset)
5040 *next_offset = eot_offset;
5041 /*
5042 * Tell our caller we saw no whitespace, so they can
5043 * try to desegment and get the entire line
5044 * into one tvbuff.
5045 */
5046 return false0;
5047 }
5048
5049 /*
5050 * Find the number of bytes between the starting offset
5051 * and the space, CR or LF.
5052 */
5053 if (tokenlen)
5054 *tokenlen = eot_offset - offset;
5055
5056 /*
5057 * Return the offset of the character after the token delimiter,
5058 * skipping over the last character in the separator.
5059 *
5060 * XXX - get_token_len() from strutil.h returns the start offset of
5061 * the next token by skipping trailing spaces (but not spaces that
5062 * follow a CR or LF, only consecutive spaces). Should we align
5063 * the two functions? Most dissectors want to skip extra spaces,
5064 * and while the dissector _can_ follow up with tvb_skip_wsp, this
5065 * probably causes dissectors to use tvb_get_ptr + get_token_len,
5066 * which we want to discourage. OTOH, IMAP, which uses this, says
5067 * "in all cases, SP refers to exactly one space. It is NOT permitted
5068 * to substitute TAB, insert additional spaces, or otherwise treat
5069 * SP as being equivalent to linear whitespace (LWSP)."
5070 * https://www.rfc-editor.org/rfc/rfc9051.html#name-formal-syntax
5071 *
5072 * XXX - skip over CR-LF as a unit like tvb_find_line_end()?
5073 * get_token_len() doesn't, probably because most dissectors have
5074 * already found the line end before, but it probably makes sense
5075 * to do and unlike above it's unlikely it would break any protocol
5076 * (and might even fix some.)
5077 */
5078 if (next_offset)
5079 *next_offset = eot_offset + 1;
5080
5081 return true1;
5082}
5083
5084int tvb_get_token_len(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset, const bool_Bool desegment)
5085{
5086 unsigned tokenlen;
5087 unsigned abs_next_offset;
5088 unsigned limit;
5089 int exception;
5090
5091 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5091, "tvb && tvb->initialized"
))))
;
5092
5093 exception = validate_offset_and_remaining(tvb, offset, &limit);
5094 if (exception)
5095 THROW(exception)except_throw(1, (exception), ((void*)0));
5096
5097 /* Only search to end of tvbuff, w/o throwing exception. */
5098 if (len >= 0 && limit > (unsigned) len) {
5099 /* Maximum length doesn't go past end of tvbuff; search
5100 to that value. */
5101 limit = (unsigned) len;
5102 }
5103
5104 if (!_tvb_get_token_len_length(tvb, offset, limit, &tokenlen, &abs_next_offset) && desegment) {
5105 return -1;
5106 }
5107 if (next_offset) {
5108 *next_offset = (int)abs_next_offset;
5109 }
5110 return (int)tokenlen;
5111}
5112
5113bool_Bool
5114tvb_get_token_len_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *tokenlen, unsigned *next_offset)
5115{
5116 unsigned limit;
5117 int exception;
5118
5119 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5119, "tvb && tvb->initialized"
))))
;
5120
5121 exception = validate_offset_and_remaining(tvb, offset, &limit);
5122 if (exception)
5123 THROW(exception)except_throw(1, (exception), ((void*)0));
5124
5125 return _tvb_get_token_len_length(tvb, offset, limit, tokenlen, next_offset);
5126}
5127
5128bool_Bool
5129tvb_get_token_len_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *tokenlen, unsigned *next_offset)
5130{
5131 unsigned limit;
5132 int exception;
5133
5134 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5134, "tvb && tvb->initialized"
))))
;
5135
5136 exception = validate_offset_and_remaining(tvb, offset, &limit);
5137 if (exception)
5138 THROW(exception)except_throw(1, (exception), ((void*)0));
5139
5140 /* Only search to end of tvbuff, w/o throwing exception. */
5141 if (limit > maxlength) {
5142 /* Maximum length doesn't go past end of tvbuff; search
5143 to that value. */
5144 limit = maxlength;
5145 }
5146
5147 return _tvb_get_token_len_length(tvb, offset, limit, tokenlen, next_offset);
5148}
5149
5150/*
5151 * Format a bunch of data from a tvbuff as bytes, returning a pointer
5152 * to the string with the formatted data, with "punct" as a byte
5153 * separator.
5154 */
5155char *
5156tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned len, const char punct)
5157{
5158 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)
;
5159}
5160
5161/*
5162 * Given a wmem scope, a tvbuff, an offset, a length, an input digit
5163 * set, and a boolean indicator, fetch BCD-encoded digits from a
5164 * tvbuff starting from either the low or high half byte of the
5165 * first byte depending on the boolean indicator (true means "start
5166 * with the high half byte, ignoring the low half byte", and false
5167 * means "start with the low half byte and proceed to the high half
5168 * byte), formating the digits into characters according to the
5169 * input digit set, and return a pointer to a UTF-8 string, allocated
5170 * using the wmem scope. A nibble of 0xf is considered a 'filler'
5171 * and will end the conversion. Similarly if odd is set the last
5172 * high nibble will be omitted. (Note that if both skip_first and
5173 * odd are true, then both the first and last semi-octet are skipped,
5174 * i.e. an even number of nibbles are considered.)
5175 */
5176char *
5177tvb_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)
5178{
5179 const uint8_t *ptr;
5180 int i = 0;
5181 char *digit_str;
5182 uint8_t octet, nibble;
5183
5184 DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug
("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5184, "tvb && tvb->initialized"
))))
;
5185
5186 ptr = ensure_contiguous_unsigned(tvb, offset, len);
5187
5188 /*
5189 * XXX - map illegal digits (digits that map to 0) to REPLACEMENT
5190 * CHARACTER, and have all the tables in epan/tvbuff.c use 0 rather
5191 * than '?'?
5192 */
5193 digit_str = (char *)wmem_alloc(scope, len*2 + 1);
5194
5195 while (len > 0) {
5196 octet = *ptr;
5197 if (!skip_first) {
5198 if (bigendian) {
5199 nibble = (octet >> 4) & 0x0f;
5200 } else {
5201 nibble = octet & 0x0f;
5202 }
5203 if (nibble == 0x0f) {
5204 /*
5205 * Stop digit.
5206 */
5207 break;
5208 }
5209 digit_str[i] = dgt->out[nibble];
5210 i++;
5211 }
5212 skip_first = false0;
5213
5214 /*
5215 * unpack second value in byte
5216 */
5217 if (bigendian) {
5218 nibble = octet & 0x0f;
5219 } else {
5220 nibble = octet >> 4;
5221 }
5222
5223 if (nibble == 0x0f) {
5224 /*
5225 * This is the stop digit or a filler digit. Ignore
5226 * it.
5227 */
5228 break;
5229 }
5230 if ((len == 1) && (odd == true1 )){
5231 /* Last octet, skip last high nibble in case of odd number of digits */
5232 break;
5233 }
5234 digit_str[i] = dgt->out[nibble];
5235 i++;
5236
5237 ptr++;
5238 len--;
5239 }
5240 digit_str[i] = '\0';
5241 return digit_str;
5242}
5243
5244/* XXXX Fix me - needs odd indicator added (or just use of tvb_get_bcd_string / proto_tree_add_item) */
5245const char *
5246tvb_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)
5247{
5248 if (!dgt)
5249 dgt = &Dgt0_9_bcd;
5250
5251 return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, false0);
5252}
5253
5254const char *
5255tvb_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)
5256{
5257 if (!dgt)
5258 dgt = &Dgt0_9_bcd;
5259
5260 return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, true1);
5261}
5262
5263/*
5264 * Format a bunch of data from a tvbuff as bytes, returning a pointer
5265 * to the string with the formatted data.
5266 */
5267char *
5268tvb_bytes_to_str(wmem_allocator_t *allocator, tvbuff_t *tvb, const unsigned offset, const unsigned len)
5269{
5270 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)
;
5271}
5272
5273/* Find a needle tvbuff within a haystack tvbuff. */
5274int
5275tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const int haystack_offset)
5276{
5277 unsigned haystack_abs_offset = 0, haystack_abs_length = 0;
5278 const uint8_t *haystack_data;
5279 const uint8_t *needle_data;
5280 const unsigned needle_len = needle_tvb->length;
5281 const uint8_t *location;
5282
5283 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", 5283, "haystack_tvb && haystack_tvb->initialized"
))))
;
5284
5285 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
5286 return -1;
5287 }
5288
5289 /* Get pointers to the tvbuffs' data. */
5290 haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
5291 needle_data = ensure_contiguous(needle_tvb, 0, -1);
5292
5293 check_offset_length(haystack_tvb, haystack_offset, -1,
5294 &haystack_abs_offset, &haystack_abs_length);
5295
5296 location = ws_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
5297 needle_data, needle_len);
5298
5299 if (location) {
5300 return (int) (location - haystack_data);
5301 }
5302
5303 return -1;
5304}
5305
5306/* Find a needle tvbuff within a haystack tvbuff. */
5307bool_Bool
5308tvb_find_tvb_remaining(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const unsigned haystack_offset, unsigned *found_offset)
5309{
5310 const uint8_t *haystack_data;
5311 const uint8_t *needle_data;
5312 const unsigned needle_len = needle_tvb->length;
5313 const uint8_t *location;
5314 int exception;
5315 unsigned haystack_rem_length;
5316
5317 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", 5317, "haystack_tvb && haystack_tvb->initialized"
))))
;
5318 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", 5318, "needle_tvb && needle_tvb->initialized"
))))
;
5319
5320 if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
5321 return false0;
5322 }
5323
5324 exception = validate_offset_and_remaining(haystack_tvb, haystack_offset, &haystack_rem_length);
5325 if (exception)
5326 THROW(exception)except_throw(1, (exception), ((void*)0));
5327
5328 /* Get pointers to the tvbuffs' data. */
5329 haystack_data = ensure_contiguous_unsigned(haystack_tvb, haystack_offset, haystack_rem_length);
5330 needle_data = ensure_contiguous_unsigned(needle_tvb, 0, needle_len);
5331
5332 location = ws_memmem(haystack_data, haystack_rem_length,
5333 needle_data, needle_len);
5334
5335 if (location) {
5336 if (found_offset)
5337 *found_offset = (unsigned) (location - haystack_data) + haystack_offset;
5338 return true1;
5339 }
5340
5341 return false0;
5342}
5343
5344unsigned
5345tvb_raw_offset(tvbuff_t *tvb)
5346{
5347 if (!(tvb->flags & TVBUFF_RAW_OFFSET0x00000002)) {
5348 tvb->raw_offset = tvb_offset_from_real_beginning(tvb);
5349 tvb->flags |= TVBUFF_RAW_OFFSET0x00000002;
5350 }
5351 return tvb->raw_offset;
5352}
5353
5354void
5355tvb_set_fragment(tvbuff_t *tvb)
5356{
5357 tvb->flags |= TVBUFF_FRAGMENT0x00000001;
5358}
5359
5360struct tvbuff *
5361tvb_get_ds_tvb(tvbuff_t *tvb)
5362{
5363 return(tvb->ds_tvb);
5364}
5365
5366unsigned
5367tvb_get_varint(tvbuff_t *tvb, unsigned offset, unsigned maxlen, uint64_t *value, const unsigned encoding)
5368{
5369 *value = 0;
5370
5371 switch (encoding & ENC_VARINT_MASK(0x00000002|0x00000004|0x00000008|0x00000010)) {
5372 case ENC_VARINT_PROTOBUF0x00000002:
5373 {
5374 unsigned i;
5375 uint64_t b; /* current byte */
5376
5377 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5378 b = tvb_get_uint8(tvb, offset++);
5379 *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5380
5381 if (b < 0x80) {
5382 /* end successfully because of last byte's msb(most significant bit) is zero */
5383 return i + 1;
5384 }
5385 }
5386 break;
5387 }
5388
5389 case ENC_VARINT_ZIGZAG0x00000008:
5390 {
5391 unsigned i;
5392 uint64_t b; /* current byte */
5393
5394 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5395 b = tvb_get_uint8(tvb, offset++);
5396 *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5397
5398 if (b < 0x80) {
5399 /* end successfully because of last byte's msb(most significant bit) is zero */
5400 *value = (*value >> 1) ^ ((*value & 1) ? -1 : 0);
5401 return i + 1;
5402 }
5403 }
5404 break;
5405 }
5406
5407 case ENC_VARINT_SDNV0x00000010:
5408 {
5409 /* Decodes similar to protobuf but in MSByte order */
5410 unsigned i;
5411 uint64_t b; /* current byte */
5412
5413 for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5414 b = tvb_get_uint8(tvb, offset++);
5415 if ((i == 9) && (*value >= UINT64_C(1)1UL<<(64-7))) {
5416 // guaranteed overflow, not valid SDNV
5417 return 0;
5418 }
5419 *value <<= 7;
5420 *value |= (b & 0x7F); /* add lower 7 bits to val */
5421
5422 if (b < 0x80) {
5423 /* end successfully because of last byte's msb(most significant bit) is zero */
5424 return i + 1;
5425 }
5426 }
5427 break;
5428 }
5429
5430 case ENC_VARINT_QUIC0x00000004:
5431 {
5432 /* calculate variable length */
5433 *value = tvb_get_uint8(tvb, offset);
5434 switch((*value) >> 6) {
5435 case 0: /* 0b00 => 1 byte length (6 bits Usable) */
5436 (*value) &= 0x3F;
5437 return 1;
5438 case 1: /* 0b01 => 2 bytes length (14 bits Usable) */
5439 *value = tvb_get_ntohs(tvb, offset) & 0x3FFF;
5440 return 2;
5441 case 2: /* 0b10 => 4 bytes length (30 bits Usable) */
5442 *value = tvb_get_ntohl(tvb, offset) & 0x3FFFFFFF;
5443 return 4;
5444 case 3: /* 0b11 => 8 bytes length (62 bits Usable) */
5445 *value = tvb_get_ntoh64(tvb, offset) & UINT64_C(0x3FFFFFFFFFFFFFFF)0x3FFFFFFFFFFFFFFFUL;
5446 return 8;
5447 default: /* No Possible */
5448 ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/tvbuff.c", 5448,
__func__, "assertion \"not reached\" failed")
;
5449 break;
5450 }
5451 break;
5452 }
5453
5454 default:
5455 DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\""
, "epan/tvbuff.c", 5455))
;
5456 }
5457
5458 return 0; /* 10 bytes scanned, but no bytes' msb is zero */
5459}
5460
5461/*
5462 * Editor modelines - https://www.wireshark.org/tools/modelines.html
5463 *
5464 * Local variables:
5465 * c-basic-offset: 8
5466 * tab-width: 8
5467 * indent-tabs-mode: t
5468 * End:
5469 *
5470 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
5471 * :indentSize=8:tabSize=8:noTabs=false:
5472 */