Bug Summary

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