Bug Summary

File:epan/tvbuff.c
Warning:line 523, column 10
Potential leak of memory pointed to by 'buf'

Annotated Source Code

Press '?' to see keyboard shortcuts

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