Bug Summary

File:epan/tvbuff.c
Warning:line 499, column 17
The left expression of the compound assignment uses uninitialized memory

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