### Bug Summary

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

### Annotated Source Code

Press '?' to see keyboard shortcuts

Show analyzer invocation
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/2026-03-06-100400-3621-1 -x c /builds/wireshark/wireshark/epan/tvbuff.c

Keyboard shortcuts:

- Use 'j/k' keys for keyboard navigation
- Use 'Shift+S' to show/hide relevant lines
- Use '?' to toggle this window

Close
Show only relevant lines Show control flow arrows

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