Bug Summary

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