Bug Summary

File:builds/wireshark/wireshark/epan/tvbuff.c
Warning:line 2536, column 41
Subtraction of a null pointer (from variable 'end') and a non-null pointer (from variable 'begin') results in undefined behavior

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