### Bug Summary

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