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