### 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 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-06-09-100419-3529-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 | /* Get 1 - 32 bits (should be deprecated as same as tvb_get_bits32??) */
2871 | uint32_t
2872 | tvb_get_bits(tvbuff_t *tvb, const unsigned bit_offset, const unsigned no_of_bits, const unsigned encoding)
2873 | {
2874 | return (uint32_t)tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
2875 | }
2876 |
2877 | static bool_Bool
2878 | tvb_find_uint8_generic(tvbuff_t *tvb, unsigned abs_offset, unsigned limit, uint8_t needle, unsigned *end_offset)
2879 | {
2880 | const uint8_t *ptr;
2881 | const uint8_t *result;
2882 |
2883 | if (end_offset) {
2884 | *end_offset = abs_offset + limit;
2885 | }
2886 |
2887 | ptr = ensure_contiguous_unsigned(tvb, abs_offset, limit); /* tvb_get_ptr() */
2888 | if (!ptr)
2889 | return false0;
2890 |
2891 | result = (const uint8_t *) memchr(ptr, needle, limit);
2892 | if (!result)
2893 | return false0;
2894 |
2895 | if (end_offset) {
2896 | *end_offset = (unsigned)((result - ptr) + abs_offset);
2897 | }
2898 | return true1;
2899 | }
2900 |
2901 | static bool_Bool
2902 | _tvb_find_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, const uint8_t needle, unsigned *end_offset)
2903 | {
2904 | const uint8_t *result;
2905 |
2906 | /* If we have real data, perform our search now. */
2907 | if (tvb->real_data) {
2908 | result = (const uint8_t *)memchr(tvb->real_data + offset, needle, limit);
2909 | if (result == NULL((void*)0)) {
2910 | if (end_offset) {
2911 | *end_offset = offset + limit;
2912 | }
2913 | return false0;
2914 | }
2915 | else {
2916 | if (end_offset) {
2917 | *end_offset = (unsigned)(result - tvb->real_data);
2918 | }
2919 | return true1;
2920 | }
2921 | }
2922 |
2923 | if (tvb->ops->tvb_find_uint8)
2924 | return tvb->ops->tvb_find_uint8(tvb, offset, limit, needle, end_offset);
2925 |
2926 | return tvb_find_uint8_generic(tvb, offset, limit, needle, end_offset);
2927 | }
2928 |
2929 | bool_Bool
2930 | tvb_find_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, const uint8_t needle, unsigned *end_offset)
2931 | {
2932 | unsigned limit = 0;
2933 | int exception;
2934 |
2935 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2935, "tvb && tvb->initialized" ))));
2936 |
2937 | exception = validate_offset_and_remaining(tvb, offset, &limit);
2938 | if (exception)
2939 | THROW(exception)except_throw(1, (exception), ((void*)0));
2940 |
2941 | /* Only search to end of tvbuff, w/o throwing exception. */
2942 | if (limit > maxlength) {
2943 | /* Maximum length doesn't go past end of tvbuff; search
2944 | to that value. */
2945 | limit = maxlength;
2946 | }
2947 |
2948 | return _tvb_find_uint8_length(tvb, offset, limit, needle, end_offset);
2949 | }
2950 |
2951 | bool_Bool
2952 | tvb_find_uint8_remaining(tvbuff_t *tvb, const unsigned offset, const uint8_t needle, unsigned *end_offset)
2953 | {
2954 | unsigned limit = 0;
2955 | int exception;
2956 |
2957 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 2957, "tvb && tvb->initialized" ))));
2958 |
2959 | exception = validate_offset_and_remaining(tvb, offset, &limit);
2960 | if (exception)
2961 | THROW(exception)except_throw(1, (exception), ((void*)0));
2962 |
2963 | return _tvb_find_uint8_length(tvb, offset, limit, needle, end_offset);
2964 | }
2965 |
2966 | static bool_Bool
2967 | _tvb_find_uint16_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, const uint16_t needle, unsigned *end_offset)
2968 | {
2969 | const uint8_t needle1 = ((needle & 0xFF00) >> 8);
2970 | const uint8_t needle2 = ((needle & 0x00FF) >> 0);
2971 | unsigned searched_bytes = 0;
2972 | unsigned pos = offset;
2973 |
2974 | if (end_offset) {
2975 | *end_offset = offset + limit;
2976 | }
2977 |
2978 | do {
2979 | if (!_tvb_find_uint8_length(tvb, pos, limit - searched_bytes, needle1, &pos)) {
2980 | return false0;
2981 | }
2982 |
2983 | /* Bytes searched so far (not counting the second byte) */
2984 | searched_bytes = pos - offset + 1;
2985 |
2986 | /* Test vs. equality to account for the second byte */
2987 | if (searched_bytes >= limit) {
2988 | return false0;
2989 | }
2990 |
2991 | if (_tvb_find_uint8_length(tvb, pos + 1, 1, needle2, NULL((void*)0))) {
2992 | if (end_offset) {
2993 | *end_offset = pos;
2994 | }
2995 | return true1;
2996 | }
2997 |
2998 | pos += 1;
2999 | searched_bytes += 1;
3000 | } while (searched_bytes < limit);
3001 |
3002 | return false0;
3003 | }
3004 |
3005 | bool_Bool
3006 | tvb_find_uint16_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, const uint16_t needle, unsigned *end_offset)
3007 | {
3008 | unsigned limit = 0;
3009 | int exception;
3010 |
3011 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3011, "tvb && tvb->initialized" ))));
3012 |
3013 | exception = validate_offset_and_remaining(tvb, offset, &limit);
3014 | if (exception)
3015 | THROW(exception)except_throw(1, (exception), ((void*)0));
3016 |
3017 | /* Only search to end of tvbuff, w/o throwing exception. */
3018 | if (limit > maxlength) {
3019 | /* Maximum length doesn't go past end of tvbuff; search
3020 | to that value. */
3021 | limit = maxlength;
3022 | }
3023 |
3024 | return _tvb_find_uint16_length(tvb, offset, limit, needle, end_offset);
3025 | }
3026 |
3027 | bool_Bool
3028 | tvb_find_uint16_remaining(tvbuff_t *tvb, const unsigned offset, const uint16_t needle, unsigned *end_offset)
3029 | {
3030 | unsigned limit = 0;
3031 | int exception;
3032 |
3033 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3033, "tvb && tvb->initialized" ))));
3034 |
3035 | exception = validate_offset_and_remaining(tvb, offset, &limit);
3036 | if (exception)
3037 | THROW(exception)except_throw(1, (exception), ((void*)0));
3038 |
3039 | return _tvb_find_uint16_length(tvb, offset, limit, needle, end_offset);
3040 | }
3041 |
3042 | static inline bool_Bool
3043 | 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)
3044 | {
3045 | const uint8_t *ptr;
3046 | const uint8_t *result;
3047 |
3048 | if (found_offset) {
3049 | *found_offset = abs_offset + limit;
3050 | }
3051 |
3052 | ptr = ensure_contiguous_unsigned(tvb, abs_offset, limit); /* tvb_get_ptr */
3053 | if (!ptr)
3054 | return false0;
3055 |
3056 | result = ws_mempbrk_exec(ptr, limit, pattern, found_needle);
3057 | if (!result)
3058 | return false0;
3059 |
3060 | if (found_offset) {
3061 | *found_offset = (unsigned)((result - ptr) + abs_offset);
3062 | }
3063 | return true1;
3064 | }
3065 |
3066 | static bool_Bool
3067 | _tvb_ws_mempbrk_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit,
3068 | const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
3069 | {
3070 | const uint8_t *result;
3071 |
3072 | /* If we have real data, perform our search now. */
3073 | if (tvb->real_data) {
3074 | result = ws_mempbrk_exec(tvb->real_data + offset, limit, pattern, found_needle);
3075 | if (result == NULL((void*)0)) {
3076 | if (found_offset) {
3077 | *found_offset = offset + limit;
3078 | }
3079 | return false0;
3080 | }
3081 | else {
3082 | if (found_offset) {
3083 | *found_offset = (unsigned)(result - tvb->real_data);
3084 | }
3085 | return true1;
3086 | }
3087 | }
3088 |
3089 | if (tvb->ops->tvb_ws_mempbrk_pattern_uint8)
3090 | return tvb->ops->tvb_ws_mempbrk_pattern_uint8(tvb, offset, limit, pattern, found_offset, found_needle);
3091 |
3092 | return tvb_ws_mempbrk_uint8_generic(tvb, offset, limit, pattern, found_offset, found_needle);
3093 | }
3094 |
3095 | bool_Bool
3096 | tvb_ws_mempbrk_uint8_remaining(tvbuff_t *tvb, const unsigned offset,
3097 | const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
3098 | {
3099 | unsigned limit = 0;
3100 | int exception;
3101 |
3102 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3102, "tvb && tvb->initialized" ))));
3103 |
3104 | exception = validate_offset_and_remaining(tvb, offset, &limit);
3105 | if (exception)
3106 | THROW(exception)except_throw(1, (exception), ((void*)0));
3107 |
3108 | return _tvb_ws_mempbrk_uint8_length(tvb, offset, limit, pattern, found_offset, found_needle);
3109 | }
3110 |
3111 | bool_Bool
3112 | tvb_ws_mempbrk_uint8_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength,
3113 | const ws_mempbrk_pattern* pattern, unsigned *found_offset, unsigned char *found_needle)
3114 | {
3115 | unsigned limit = 0;
3116 | int exception;
3117 |
3118 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3118, "tvb && tvb->initialized" ))));
3119 |
3120 | exception = validate_offset_and_remaining(tvb, offset, &limit);
3121 | if (exception)
3122 | THROW(exception)except_throw(1, (exception), ((void*)0));
3123 |
3124 | /* Only search to end of tvbuff, w/o throwing exception. */
3125 | if (limit > maxlength) {
3126 | /* Maximum length doesn't go past end of tvbuff; search
3127 | to that value. */
3128 | limit = maxlength;
3129 | }
3130 |
3131 | return _tvb_ws_mempbrk_uint8_length(tvb, offset, limit, pattern, found_offset, found_needle);
3132 | }
3133 |
3134 | /* Find size of stringz (NUL-terminated string) by looking for terminating
3135 | * NUL. The size of the string includes the terminating NUL.
3136 | *
3137 | * If the NUL isn't found, it throws the appropriate exception.
3138 | */
3139 | unsigned
3140 | tvb_strsize(tvbuff_t *tvb, const unsigned offset)
3141 | {
3142 | unsigned nul_offset;
3143 |
3144 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3144, "tvb && tvb->initialized" ))));
3145 |
3146 | validate_offset(tvb, offset);
3147 | if (!tvb_find_uint8_remaining(tvb, offset, 0, &nul_offset)) {
3148 | /*
3149 | * OK, we hit the end of the tvbuff, so we should throw
3150 | * an exception.
3151 | */
3152 | if (tvb->length < tvb->contained_length) {
3153 | THROW(BoundsError)except_throw(1, (1), ((void*)0));
3154 | } else if (tvb->flags & TVBUFF_FRAGMENT0x00000001) {
3155 | THROW(FragmentBoundsError)except_throw(1, (4), ((void*)0));
3156 | } else if (tvb->length < tvb->reported_length) {
3157 | THROW(ContainedBoundsError)except_throw(1, (2), ((void*)0));
3158 | } else {
3159 | THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
3160 | }
3161 | }
3162 | return (nul_offset - offset) + 1;
3163 | }
3164 |
3165 | /* UTF-16/UCS-2 version of tvb_strsize */
3166 | /* Returns number of bytes including the (two-bytes) null terminator */
3167 | unsigned
3168 | tvb_unicode_strsize(tvbuff_t *tvb, const unsigned offset)
3169 | {
3170 | unsigned cur_offset = offset;
3171 | gunichar2 uchar;
3172 |
3173 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3173, "tvb && tvb->initialized" ))));
3174 |
3175 | /* Note: don't use tvb_find_uint16 because it must be aligned */
3176 | do {
3177 | /* Endianness doesn't matter when looking for null */
3178 | uchar = tvb_get_ntohs(tvb, cur_offset);
3179 | /* Make sure we don't overflow */
3180 | if (ckd_add(&cur_offset, cur_offset, 2)__builtin_add_overflow((cur_offset), (2), (&cur_offset))) {
3181 | THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
3182 | }
3183 | } while(uchar != 0);
3184 |
3185 | return cur_offset - offset;
3186 | }
3187 |
3188 | /* UTF-32/UCS-4 version of tvb_strsize */
3189 | /* Returns number of bytes including the (four-bytes) null terminator */
3190 | static unsigned
3191 | tvb_ucs_4_strsize(tvbuff_t *tvb, const unsigned offset)
3192 | {
3193 |
3194 | unsigned end_offset;
3195 | gunichar uchar;
3196 |
3197 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3197, "tvb && tvb->initialized" ))));
3198 | end_offset = offset;
3199 | do {
3200 | /* Endianness doesn't matter when looking for null */
3201 | uchar = tvb_get_ntohl(tvb, end_offset);
3202 | /* Make sure we don't overflow */
3203 | if (ckd_add(&end_offset, end_offset, 4)__builtin_add_overflow((end_offset), (4), (&end_offset))) {
3204 | THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
3205 | }
3206 | } while(uchar != 0);
3207 | return end_offset - offset;
3208 | }
3209 |
3210 | unsigned
3211 | tvb_strsize_enc(tvbuff_t *tvb, const unsigned offset, const unsigned encoding)
3212 | {
3213 | switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
3214 | case ENC_UTF_160x00000004:
3215 | case ENC_UCS_20x00000006:
3216 | return tvb_unicode_strsize(tvb, offset);
3217 |
3218 | case ENC_UCS_40x00000008:
3219 | return tvb_ucs_4_strsize(tvb, offset);
3220 |
3221 | case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
3222 | case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
3223 | case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
3224 | 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" );
3225 | break;
3226 |
3227 | case ENC_ASCII_7BITS0x00000034:
3228 | 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" );
3229 | break;
3230 |
3231 | case ENC_APN_STR0x00000054:
3232 | /* At least as defined in 3GPP TS 23.003 Clause 9.1, null-termination
3233 | * does make sense as internal nulls are not allowed. */
3234 | 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" );
3235 | break;
3236 |
3237 | case ENC_BCD_DIGITS_0_90x00000044:
3238 | case ENC_KEYPAD_ABC_TBCD0x00000046:
3239 | case ENC_KEYPAD_BC_TBCD0x00000048:
3240 | case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
3241 | 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." );
3242 | break;
3243 |
3244 | case ENC_ASCII0x00000000:
3245 | case ENC_UTF_80x00000002:
3246 | case ENC_ISO_8859_10x0000000A:
3247 | case ENC_ISO_8859_20x0000000C:
3248 | case ENC_ISO_8859_30x0000000E:
3249 | case ENC_ISO_8859_40x00000010:
3250 | case ENC_ISO_8859_50x00000012:
3251 | case ENC_ISO_8859_60x00000014:
3252 | case ENC_ISO_8859_70x00000016:
3253 | case ENC_ISO_8859_80x00000018:
3254 | case ENC_ISO_8859_90x0000001A:
3255 | case ENC_ISO_8859_100x0000001C:
3256 | case ENC_ISO_8859_110x0000001E:
3257 | case ENC_ISO_8859_130x00000022:
3258 | case ENC_ISO_8859_140x00000024:
3259 | case ENC_ISO_8859_150x00000026:
3260 | case ENC_ISO_8859_160x00000028:
3261 | case ENC_WINDOWS_12500x0000002A:
3262 | case ENC_WINDOWS_12510x0000003C:
3263 | case ENC_WINDOWS_12520x0000003A:
3264 | case ENC_MAC_ROMAN0x00000030:
3265 | case ENC_CP4370x00000032:
3266 | case ENC_CP8550x0000003E:
3267 | case ENC_CP8660x00000040:
3268 | case ENC_ISO_646_BASIC0x00000042:
3269 | case ENC_EBCDIC0x0000002E:
3270 | case ENC_EBCDIC_CP0370x00000038:
3271 | case ENC_EBCDIC_CP5000x00000060:
3272 | case ENC_T610x00000036:
3273 | case ENC_GB180300x00000050:
3274 | case ENC_EUC_KR0x00000052:
3275 | case ENC_DECT_STANDARD_8BITS0x00000056:
3276 | default:
3277 | return tvb_strsize(tvb, offset);
3278 | }
3279 | }
3280 |
3281 | /* Find length of string by looking for end of string ('\0'), up to
3282 | * 'maxlength' characters'; if 'maxlength' is -1, searches to end
3283 | * of tvbuff.
3284 | * Returns -1 if 'maxlength' reached before finding EOS. */
3285 | int
3286 | tvb_strnlen(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
3287 | {
3288 | unsigned result_offset;
3289 |
3290 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3290, "tvb && tvb->initialized" ))));
3291 |
3292 | /* TODO - this needs a variant that returns a bool
3293 | * and sets a unsigned offset to the value if true. */
3294 | if (!tvb_find_uint8_length(tvb, offset, maxlength, 0, &result_offset)) {
3295 | return -1;
3296 | }
3297 | else {
3298 | return (int)(result_offset - offset);
3299 | }
3300 | }
3301 |
3302 | /*
3303 | * Implement strneql etc
3304 | */
3305 |
3306 | /*
3307 | * Call strncmp after checking if enough chars left, returning 0 if
3308 | * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
3309 | */
3310 | int
3311 | tvb_strneql(tvbuff_t *tvb, const unsigned offset, const char *str, const size_t size)
3312 | {
3313 | const uint8_t *ptr;
3314 |
3315 | ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3316 |
3317 | if (ptr) {
3318 | int cmp = strncmp((const char *)ptr, str, size);
3319 |
3320 | /*
3321 | * Return 0 if equal, -1 otherwise.
3322 | */
3323 | return (cmp == 0 ? 0 : -1);
3324 | } else {
3325 | /*
3326 | * Not enough characters in the tvbuff to match the
3327 | * string.
3328 | */
3329 | return -1;
3330 | }
3331 | }
3332 |
3333 | /*
3334 | * Call g_ascii_strncasecmp after checking if enough chars left, returning
3335 | * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
3336 | */
3337 | int
3338 | tvb_strncaseeql(tvbuff_t *tvb, const unsigned offset, const char *str, const size_t size)
3339 | {
3340 | const uint8_t *ptr;
3341 |
3342 | ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3343 |
3344 | if (ptr) {
3345 | int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
3346 |
3347 | /*
3348 | * Return 0 if equal, -1 otherwise.
3349 | */
3350 | return (cmp == 0 ? 0 : -1);
3351 | } else {
3352 | /*
3353 | * Not enough characters in the tvbuff to match the
3354 | * string.
3355 | */
3356 | return -1;
3357 | }
3358 | }
3359 |
3360 | /*
3361 | * Check that the tvbuff contains at least size bytes, starting at
3362 | * offset, and that those bytes are equal to str. Return 0 for success
3363 | * and -1 for error. This function does not throw an exception.
3364 | */
3365 | int
3366 | tvb_memeql(tvbuff_t *tvb, const unsigned offset, const uint8_t *str, size_t size)
3367 | {
3368 | const uint8_t *ptr;
3369 |
3370 | ptr = ensure_contiguous_unsigned_no_exception(tvb, offset, (unsigned)size, NULL((void*)0));
3371 |
3372 | if (ptr) {
3373 | int cmp = memcmp(ptr, str, size);
3374 |
3375 | /*
3376 | * Return 0 if equal, -1 otherwise.
3377 | */
3378 | return (cmp == 0 ? 0 : -1);
3379 | } else {
3380 | /*
3381 | * Not enough characters in the tvbuff to match the
3382 | * string.
3383 | */
3384 | return -1;
3385 | }
3386 | }
3387 |
3388 | /**
3389 | * Format the data in the tvb from offset for size.
3390 | */
3391 | char *
3392 | tvb_format_text(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3393 | {
3394 | const uint8_t *ptr;
3395 |
3396 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3397 | return format_text(scope, (const char*)ptr, size);
3398 | }
3399 |
3400 | /*
3401 | * Format the data in the tvb from offset for length ...
3402 | */
3403 | char *
3404 | tvb_format_text_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3405 | {
3406 | const uint8_t *ptr;
3407 |
3408 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3409 | return format_text_wsp(allocator, (const char*)ptr, size);
3410 | }
3411 |
3412 | /**
3413 | * Like "tvb_format_text()", but for null-padded strings; don't show
3414 | * the null padding characters as "\000".
3415 | */
3416 | char *
3417 | tvb_format_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3418 | {
3419 | const uint8_t *ptr, *p;
3420 | unsigned stringlen;
3421 |
3422 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3423 | for (p = ptr, stringlen = 0; stringlen < size && *p != '\0'; p++, stringlen++)
3424 | ;
3425 | return format_text(scope, (const char*)ptr, stringlen);
3426 | }
3427 |
3428 | /*
3429 | * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
3430 | * the null padding characters as "\000".
3431 | */
3432 | char *
3433 | tvb_format_stringzpad_wsp(wmem_allocator_t* allocator, tvbuff_t *tvb, const unsigned offset, const unsigned size)
3434 | {
3435 | const uint8_t *ptr, *p;
3436 | unsigned stringlen;
3437 |
3438 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3439 | for (p = ptr, stringlen = 0; stringlen < size && *p != '\0'; p++, stringlen++)
3440 | ;
3441 | return format_text_wsp(allocator, (const char*)ptr, stringlen);
3442 | }
3443 |
3444 | /*
3445 | * All string functions below take a scope as an argument.
3446 | *
3447 | *
3448 | * If scope is NULL, memory is allocated with g_malloc() and user must
3449 | * explicitly free it with g_free().
3450 | * If scope is not NULL, memory is allocated with the corresponding pool
3451 | * lifetime.
3452 | *
3453 | * All functions throw an exception if the tvbuff ends before the string
3454 | * does.
3455 | */
3456 |
3457 | /*
3458 | * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3459 | * of bytes referred to by the tvbuff, offset, and length as an ASCII string,
3460 | * with all bytes with the high-order bit set being invalid, and return a
3461 | * pointer to a UTF-8 string, allocated using the wmem scope.
3462 | *
3463 | * Octets with the highest bit set will be converted to the Unicode
3464 | * REPLACEMENT CHARACTER.
3465 | */
3466 | static uint8_t *
3467 | tvb_get_ascii_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3468 | {
3469 | const uint8_t *ptr;
3470 |
3471 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3472 | return get_ascii_string(scope, ptr, length);
3473 | }
3474 |
3475 | /*
3476 | * Given a wmem scope, a tvbuff, an offset, a length, and a translation table,
3477 | * treat the string of bytes referred to by the tvbuff, offset, and length
3478 | * as a string encoded using one octet per character, with octets with the
3479 | * high-order bit clear being mapped by the translation table to 2-byte
3480 | * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3481 | * CHARACTER) and octets with the high-order bit set being mapped to
3482 | * REPLACEMENT CHARACTER, and return a pointer to a UTF-8 string,
3483 | * allocated using the wmem scope.
3484 | *
3485 | * Octets with the highest bit set will be converted to the Unicode
3486 | * REPLACEMENT CHARACTER.
3487 | */
3488 | static uint8_t *
3489 | tvb_get_iso_646_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3490 | {
3491 | const uint8_t *ptr;
3492 |
3493 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3494 | return get_iso_646_string(scope, ptr, length, table);
3495 | }
3496 |
3497 | /*
3498 | * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3499 | * of bytes referred to by the tvbuff, the offset. and the length as a UTF-8
3500 | * string, and return a pointer to a UTF-8 string, allocated using the wmem
3501 | * scope, with all ill-formed sequences replaced with the Unicode REPLACEMENT
3502 | * CHARACTER according to the recommended "best practices" given in the Unicode
3503 | * Standard and specified by W3C/WHATWG.
3504 | *
3505 | * Note that in conformance with the Unicode Standard, this treats three
3506 | * byte sequences corresponding to UTF-16 surrogate halves (paired or unpaired)
3507 | * and two byte overlong encodings of 7-bit ASCII characters as invalid and
3508 | * substitutes REPLACEMENT CHARACTER for them. Explicit support for nonstandard
3509 | * derivative encoding formats (e.g. CESU-8, Java Modified UTF-8, WTF-8) could
3510 | * be added later.
3511 | */
3512 | static uint8_t *
3513 | tvb_get_utf_8_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3514 | {
3515 | const uint8_t *ptr;
3516 |
3517 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3518 | return get_utf_8_string(scope, ptr, length);
3519 | }
3520 |
3521 | /*
3522 | * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3523 | * of bytes referred to by the tvbuff, the offset, and the length as a
3524 | * raw string, and return a pointer to that string, allocated using the
3525 | * wmem scope. This means a null is appended at the end, but no replacement
3526 | * checking is done otherwise, unlike tvb_get_utf_8_string().
3527 | */
3528 | static inline uint8_t *
3529 | tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3530 | {
3531 | uint8_t *strbuf;
3532 |
3533 | tvb_ensure_bytes_exist(tvb, offset, length);
3534 | strbuf = (uint8_t *)wmem_alloc(scope, length + 1);
3535 | tvb_memcpy(tvb, strbuf, offset, length);
3536 | strbuf[length] = '\0';
3537 | return strbuf;
3538 | }
3539 |
3540 | /*
3541 | * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3542 | * of bytes referred to by the tvbuff, the offset, and the length as an
3543 | * ISO 8859/1 string, and return a pointer to a UTF-8 string, allocated
3544 | * using the wmem scope.
3545 | */
3546 | static uint8_t *
3547 | tvb_get_string_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3548 | {
3549 | const uint8_t *ptr;
3550 |
3551 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3552 | return get_8859_1_string(scope, ptr, length);
3553 | }
3554 |
3555 | /*
3556 | * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3557 | * table, treat the string of bytes referred to by the tvbuff, the offset,
3558 | * and the length as a string encoded using one octet per character, with
3559 | * octets with the high-order bit clear being ASCII and octets with the
3560 | * high-order bit set being mapped by the translation table to 2-byte
3561 | * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3562 | * CHARACTER), and return a pointer to a UTF-8 string, allocated with the
3563 | * wmem scope.
3564 | */
3565 | static uint8_t *
3566 | tvb_get_string_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3567 | {
3568 | const uint8_t *ptr;
3569 |
3570 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3571 | return get_unichar2_string(scope, ptr, length, table);
3572 | }
3573 |
3574 | /*
3575 | * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3576 | * giving the byte order, treat the string of bytes referred to by the
3577 | * tvbuff, the offset, and the length as a UCS-2 encoded string in
3578 | * the byte order in question, containing characters from the Basic
3579 | * Multilingual Plane (plane 0) of Unicode, and return a pointer to a
3580 | * UTF-8 string, allocated with the wmem scope.
3581 | *
3582 | * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3583 | * optionally with ENC_BOM.
3584 | *
3585 | * Specify length in bytes.
3586 | *
3587 | * XXX - should map lead and trail surrogate values to REPLACEMENT
3588 | * CHARACTERs (0xFFFD)?
3589 | * XXX - if there are an odd number of bytes, should put a
3590 | * REPLACEMENT CHARACTER at the end.
3591 | */
3592 | static uint8_t *
3593 | tvb_get_ucs_2_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3594 | {
3595 | const uint8_t *ptr;
3596 |
3597 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3598 | return get_ucs_2_string(scope, ptr, length, encoding);
3599 | }
3600 |
3601 | /*
3602 | * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3603 | * giving the byte order, treat the string of bytes referred to by the
3604 | * tvbuff, the offset, and the length as a UTF-16 encoded string in
3605 | * the byte order in question, and return a pointer to a UTF-8 string,
3606 | * allocated with the wmem scope.
3607 | *
3608 | * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3609 | * optionally with ENC_BOM.
3610 | *
3611 | * Specify length in bytes.
3612 | *
3613 | * XXX - should map surrogate errors to REPLACEMENT CHARACTERs (0xFFFD).
3614 | * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3615 | * XXX - if there are an odd number of bytes, should put a
3616 | * REPLACEMENT CHARACTER at the end.
3617 | */
3618 | static uint8_t *
3619 | tvb_get_utf_16_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3620 | {
3621 | const uint8_t *ptr;
3622 |
3623 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3624 | return get_utf_16_string(scope, ptr, length, encoding);
3625 | }
3626 |
3627 | /*
3628 | * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3629 | * giving the byte order, treat the string of bytes referred to by the
3630 | * tvbuff, the offset, and the length as a UCS-4 encoded string in
3631 | * the byte order in question, and return a pointer to a UTF-8 string,
3632 | * allocated with the wmem scope.
3633 | *
3634 | * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3635 | * optionally with ENC_BOM.
3636 | *
3637 | * Specify length in bytes
3638 | *
3639 | * XXX - should map lead and trail surrogate values to a "substitute"
3640 | * UTF-8 character?
3641 | * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3642 | * XXX - if the number of bytes isn't a multiple of 4, should put a
3643 | * REPLACEMENT CHARACTER at the end.
3644 | */
3645 | static char *
3646 | tvb_get_ucs_4_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3647 | {
3648 | const uint8_t *ptr;
3649 |
3650 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3651 | return (char*)get_ucs_4_string(scope, ptr, length, encoding);
3652 | }
3653 |
3654 | char *
3655 | tvb_get_ts_23_038_7bits_string_packed(wmem_allocator_t *scope, tvbuff_t *tvb,
3656 | const unsigned bit_offset, unsigned no_of_chars)
3657 | {
3658 | unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3659 | unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3660 | const uint8_t *ptr;
3661 |
3662 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3662, "tvb && tvb->initialized" ))));
3663 |
3664 | ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3665 | return (char*)get_ts_23_038_7bits_string_packed(scope, ptr, bit_offset, no_of_chars);
3666 | }
3667 |
3668 | char *
3669 | tvb_get_ts_23_038_7bits_string_unpacked(wmem_allocator_t *scope, tvbuff_t *tvb,
3670 | const unsigned offset, unsigned length)
3671 | {
3672 | const uint8_t *ptr;
3673 |
3674 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3674, "tvb && tvb->initialized" ))));
3675 |
3676 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3677 | return (char*)get_ts_23_038_7bits_string_unpacked(scope, ptr, length);
3678 | }
3679 |
3680 | char *
3681 | tvb_get_etsi_ts_102_221_annex_a_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3682 | const unsigned offset, unsigned length)
3683 | {
3684 | const uint8_t *ptr;
3685 |
3686 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3686, "tvb && tvb->initialized" ))));
3687 |
3688 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3689 | return (char*)get_etsi_ts_102_221_annex_a_string(scope, ptr, length);
3690 | }
3691 |
3692 | char *
3693 | tvb_get_ascii_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3694 | const unsigned bit_offset, unsigned no_of_chars)
3695 | {
3696 | unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3697 | unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3698 | const uint8_t *ptr;
3699 |
3700 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3700, "tvb && tvb->initialized" ))));
3701 |
3702 | ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3703 | return (char*)get_ascii_7bits_string(scope, ptr, bit_offset, no_of_chars);
3704 | }
3705 |
3706 | /*
3707 | * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3708 | * table, treat the string of bytes referred to by the tvbuff, the offset,
3709 | * and the length as a string encoded using one octet per character, with
3710 | * octets being mapped by the translation table to 2-byte Unicode Basic
3711 | * Multilingual Plane characters (including REPLACEMENT CHARACTER), and
3712 | * return a pointer to a UTF-8 string, allocated with the wmem scope.
3713 | */
3714 | static uint8_t *
3715 | tvb_get_nonascii_unichar2_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[256])
3716 | {
3717 | const uint8_t *ptr;
3718 |
3719 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3720 | return get_nonascii_unichar2_string(scope, ptr, length, table);
3721 | }
3722 |
3723 | /*
3724 | * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3725 | * referred to by the tvbuff, offset, and length as a GB18030 encoded string,
3726 | * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3727 | * converted having substituted REPLACEMENT CHARACTER according to the
3728 | * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3729 | * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3730 | *
3731 | * As expected, this will also decode GBK and GB2312 strings.
3732 | */
3733 | static uint8_t *
3734 | tvb_get_gb18030_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3735 | {
3736 | const uint8_t *ptr;
3737 |
3738 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3739 | return get_gb18030_string(scope, ptr, length);
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 EUC-KR 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 | static uint8_t *
3751 | tvb_get_euc_kr_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3752 | {
3753 | const uint8_t *ptr;
3754 |
3755 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3756 | return get_euc_kr_string(scope, ptr, length);
3757 | }
3758 |
3759 | static uint8_t *
3760 | tvb_get_t61_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3761 | {
3762 | const uint8_t *ptr;
3763 |
3764 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3765 | return get_t61_string(scope, ptr, length);
3766 | }
3767 |
3768 | /*
3769 | * Encoding tables for BCD strings.
3770 | */
3771 | static const dgt_set_t Dgt0_9_bcd = {
3772 | {
3773 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3774 | '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?'
3775 | }
3776 | };
3777 |
3778 | static const dgt_set_t Dgt_keypad_abc_tbcd = {
3779 | {
3780 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3781 | '0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'
3782 | }
3783 | };
3784 |
3785 | static const dgt_set_t Dgt_ansi_tbcd = {
3786 | {
3787 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3788 | '0','1','2','3','4','5','6','7','8','9','?','B','C','*','#','?'
3789 | }
3790 | };
3791 |
3792 | static const dgt_set_t Dgt_dect_standard_4bits_tbcd = {
3793 | {
3794 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3795 | '0','1','2','3','4','5','6','7','8','9','?',' ','?','?','?','?'
3796 | }
3797 | };
3798 |
3799 | static uint8_t *
3800 | tvb_get_apn_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3801 | unsigned length)
3802 | {
3803 | wmem_strbuf_t *str;
3804 |
3805 | /*
3806 | * This is a domain name.
3807 | *
3808 | * 3GPP TS 23.003, section 19.4.2 "Fully Qualified Domain Names
3809 | * (FQDNs)", subsection 19.4.2.1 "General", says:
3810 | *
3811 | * The encoding of any identifier used as part of a Fully
3812 | * Qualified Domain Name (FQDN) shall follow the Name Syntax
3813 | * defined in IETF RFC 2181 [18], IETF RFC 1035 [19] and
3814 | * IETF RFC 1123 [20]. An FQDN consists of one or more
3815 | * labels. Each label is coded as a one octet length field
3816 | * followed by that number of octets coded as 8 bit ASCII
3817 | * characters.
3818 | *
3819 | * so this does not appear to use full-blown DNS compression -
3820 | * the upper 2 bits of the length don't indicate that it's a
3821 | * pointer or an extended label (RFC 2673).
3822 | */
3823 | str = wmem_strbuf_new_sized(scope, length + 1);
3824 | if (length > 0) {
3825 | const uint8_t *ptr;
3826 |
3827 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3828 |
3829 | for (;;) {
3830 | unsigned label_len;
3831 |
3832 | /*
3833 | * Process this label.
3834 | */
3835 | label_len = *ptr;
3836 | ptr++;
3837 | length--;
3838 |
3839 | while (label_len != 0) {
3840 | uint8_t ch;
3841 |
3842 | if (length == 0)
3843 | goto end;
3844 |
3845 | ch = *ptr;
3846 | if (ch < 0x80)
3847 | wmem_strbuf_append_c(str, ch);
3848 | else
3849 | wmem_strbuf_append_unichar_repl(str)wmem_strbuf_append_unichar(str, 0x00FFFD);
3850 | ptr++;
3851 | label_len--;
3852 | length--;
3853 | }
3854 |
3855 | if (length == 0)
3856 | goto end;
3857 |
3858 | wmem_strbuf_append_c(str, '.');
3859 | }
3860 | }
3861 |
3862 | end:
3863 | return (uint8_t *) wmem_strbuf_finalize(str);
3864 | }
3865 |
3866 | static uint8_t *
3867 | tvb_get_dect_standard_8bits_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3868 | {
3869 | const uint8_t *ptr;
3870 |
3871 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3872 | return get_dect_standard_8bits_string(scope, ptr, length);
3873 | }
3874 |
3875 | /*
3876 | * Given a tvbuff, an offset, a length, and an encoding, allocate a
3877 | * buffer big enough to hold a non-null-terminated string of that length
3878 | * at that offset, plus a trailing '\0', copy into the buffer the
3879 | * string as converted from the appropriate encoding to UTF-8, and
3880 | * return a pointer to the string.
3881 | */
3882 | uint8_t *
3883 | tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3884 | const unsigned length, const unsigned encoding)
3885 | {
3886 | uint8_t *strptr;
3887 | bool_Bool odd, skip_first;
3888 |
3889 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3889, "tvb && tvb->initialized" ))));
3890 |
3891 | switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
3892 |
3893 | case ENC_ASCII0x00000000:
3894 | default:
3895 | /*
3896 | * For now, we treat bogus values as meaning
3897 | * "ASCII" rather than reporting an error,
3898 | * for the benefit of old dissectors written
3899 | * when the last argument to proto_tree_add_item()
3900 | * was a bool for the byte order, not an
3901 | * encoding value, and passed non-zero values
3902 | * other than true to mean "little-endian".
3903 | */
3904 | strptr = tvb_get_ascii_string(scope, tvb, offset, length);
3905 | break;
3906 |
3907 | case ENC_UTF_80x00000002:
3908 | strptr = tvb_get_utf_8_string(scope, tvb, offset, length);
3909 | break;
3910 |
3911 | case ENC_UTF_160x00000004:
3912 | strptr = tvb_get_utf_16_string(scope, tvb, offset, length,
3913 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3914 | break;
3915 |
3916 | case ENC_UCS_20x00000006:
3917 | strptr = tvb_get_ucs_2_string(scope, tvb, offset, length,
3918 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3919 | break;
3920 |
3921 | case ENC_UCS_40x00000008:
3922 | strptr = (uint8_t*)tvb_get_ucs_4_string(scope, tvb, offset, length,
3923 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3924 | break;
3925 |
3926 | case ENC_ISO_8859_10x0000000A:
3927 | /*
3928 | * ISO 8859-1 printable code point values are equal
3929 | * to the equivalent Unicode code point value, so
3930 | * no translation table is needed.
3931 | */
3932 | strptr = tvb_get_string_8859_1(scope, tvb, offset, length);
3933 | break;
3934 |
3935 | case ENC_ISO_8859_20x0000000C:
3936 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_2);
3937 | break;
3938 |
3939 | case ENC_ISO_8859_30x0000000E:
3940 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_3);
3941 | break;
3942 |
3943 | case ENC_ISO_8859_40x00000010:
3944 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_4);
3945 | break;
3946 |
3947 | case ENC_ISO_8859_50x00000012:
3948 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_5);
3949 | break;
3950 |
3951 | case ENC_ISO_8859_60x00000014:
3952 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_6);
3953 | break;
3954 |
3955 | case ENC_ISO_8859_70x00000016:
3956 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_7);
3957 | break;
3958 |
3959 | case ENC_ISO_8859_80x00000018:
3960 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_8);
3961 | break;
3962 |
3963 | case ENC_ISO_8859_90x0000001A:
3964 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_9);
3965 | break;
3966 |
3967 | case ENC_ISO_8859_100x0000001C:
3968 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_10);
3969 | break;
3970 |
3971 | case ENC_ISO_8859_110x0000001E:
3972 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_11);
3973 | break;
3974 |
3975 | case ENC_ISO_8859_130x00000022:
3976 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_13);
3977 | break;
3978 |
3979 | case ENC_ISO_8859_140x00000024:
3980 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_14);
3981 | break;
3982 |
3983 | case ENC_ISO_8859_150x00000026:
3984 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_15);
3985 | break;
3986 |
3987 | case ENC_ISO_8859_160x00000028:
3988 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_16);
3989 | break;
3990 |
3991 | case ENC_WINDOWS_12500x0000002A:
3992 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1250);
3993 | break;
3994 |
3995 | case ENC_WINDOWS_12510x0000003C:
3996 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1251);
3997 | break;
3998 |
3999 | case ENC_WINDOWS_12520x0000003A:
4000 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1252);
4001 | break;
4002 |
4003 | case ENC_MAC_ROMAN0x00000030:
4004 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_mac_roman);
4005 | break;
4006 |
4007 | case ENC_CP4370x00000032:
4008 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp437);
4009 | break;
4010 |
4011 | case ENC_CP8550x0000003E:
4012 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp855);
4013 | break;
4014 |
4015 | case ENC_CP8660x00000040:
4016 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp866);
4017 | break;
4018 |
4019 | case ENC_ISO_646_BASIC0x00000042:
4020 | strptr = tvb_get_iso_646_string(scope, tvb, offset, length, charset_table_iso_646_basic);
4021 | break;
4022 |
4023 | case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
4024 | {
4025 | unsigned bit_offset = offset << 3;
4026 | unsigned no_of_chars = (length << 3) / 7;
4027 | strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_packed(scope, tvb, bit_offset, no_of_chars);
4028 | }
4029 | break;
4030 |
4031 | case ENC_ASCII_7BITS0x00000034:
4032 | {
4033 | unsigned bit_offset = offset << 3;
4034 | unsigned no_of_chars = (length << 3) / 7;
4035 | strptr = (uint8_t*)tvb_get_ascii_7bits_string(scope, tvb, bit_offset, no_of_chars);
4036 | }
4037 | break;
4038 |
4039 | case ENC_EBCDIC0x0000002E:
4040 | /*
4041 | * "Common" EBCDIC, covering all characters with the
4042 | * same code point in all Roman-alphabet EBCDIC code
4043 | * pages.
4044 | */
4045 | strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic);
4046 | break;
4047 |
4048 | case ENC_EBCDIC_CP0370x00000038:
4049 | /*
4050 | * EBCDIC code page 037.
4051 | */
4052 | strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp037);
4053 | break;
4054 |
4055 | case ENC_EBCDIC_CP5000x00000060:
4056 | /*
4057 | * EBCDIC code page 500.
4058 | */
4059 | strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp500);
4060 | break;
4061 |
4062 | case ENC_T610x00000036:
4063 | strptr = tvb_get_t61_string(scope, tvb, offset, length);
4064 | break;
4065 |
4066 | case ENC_BCD_DIGITS_0_90x00000044:
4067 | /*
4068 | * Packed BCD, with digits 0-9.
4069 | */
4070 | odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
4071 | skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
4072 | strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt0_9_bcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
4073 | break;
4074 |
4075 | case ENC_KEYPAD_ABC_TBCD0x00000046:
4076 | /*
4077 | * Keypad-with-a/b/c "telephony BCD" - packed BCD, with
4078 | * digits 0-9 and symbols *, #, a, b, and c.
4079 | */
4080 | odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
4081 | skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
4082 | strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_keypad_abc_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
4083 | break;
4084 |
4085 | case ENC_KEYPAD_BC_TBCD0x00000048:
4086 | /*
4087 | * Keypad-with-B/C "telephony BCD" - packed BCD, with
4088 | * digits 0-9 and symbols B, C, *, and #.
4089 | */
4090 | odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
4091 | skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
4092 | strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_ansi_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
4093 | break;
4094 |
4095 | case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
4096 | strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_unpacked(scope, tvb, offset, length);
4097 | break;
4098 |
4099 | case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
4100 | strptr = (uint8_t*)tvb_get_etsi_ts_102_221_annex_a_string(scope, tvb, offset, length);
4101 | break;
4102 |
4103 | case ENC_GB180300x00000050:
4104 | strptr = tvb_get_gb18030_string(scope, tvb, offset, length);
4105 | break;
4106 |
4107 | case ENC_EUC_KR0x00000052:
4108 | strptr = tvb_get_euc_kr_string(scope, tvb, offset, length);
4109 | break;
4110 |
4111 | case ENC_APN_STR0x00000054:
4112 | strptr = tvb_get_apn_string(scope, tvb, offset, length);
4113 | break;
4114 |
4115 | case ENC_DECT_STANDARD_8BITS0x00000056:
4116 | strptr = tvb_get_dect_standard_8bits_string(scope, tvb, offset, length);
4117 | break;
4118 |
4119 | case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
4120 | /*
4121 | * DECT standard 4bits "telephony BCD" - packed BCD, with
4122 | * digits 0-9 and symbol SPACE for 0xb.
4123 | */
4124 | odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
4125 | skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
4126 | strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_dect_standard_4bits_tbcd, skip_first, odd, false0);
4127 | break;
4128 | }
4129 | return strptr;
4130 | }
4131 |
4132 | /*
4133 | * This is like tvb_get_string_enc(), except that it handles null-padded
4134 | * strings.
4135 | *
4136 | * Currently, string values are stored as UTF-8 null-terminated strings,
4137 | * so nothing needs to be done differently for null-padded strings; we
4138 | * could save a little memory by not storing the null padding.
4139 | *
4140 | * If we ever store string values differently, in a fashion that doesn't
4141 | * involve null termination, that might change.
4142 | */
4143 | uint8_t *
4144 | tvb_get_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
4145 | const unsigned length, const unsigned encoding)
4146 | {
4147 | return tvb_get_string_enc(scope, tvb, offset, length, encoding);
4148 | }
4149 |
4150 | /*
4151 | * These routines are like the above routines, except that they handle
4152 | * null-terminated strings. They find the length of that string (and
4153 | * throw an exception if the tvbuff ends before we find the null), and
4154 | * also return through a pointer the length of the string, in bytes,
4155 | * including the terminating null (the terminating null being 2 bytes
4156 | * for UCS-2 and UTF-16, 4 bytes for UCS-4, and 1 byte for other
4157 | * encodings).
4158 | */
4159 | static uint8_t *
4160 | tvb_get_ascii_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4161 | {
4162 | unsigned size;
4163 | const uint8_t *ptr;
4164 |
4165 | size = tvb_strsize(tvb, offset);
4166 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4167 | if (lengthp)
4168 | *lengthp = size;
4169 | return get_ascii_string(scope, ptr, size);
4170 | }
4171 |
4172 | static uint8_t *
4173 | tvb_get_iso_646_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
4174 | {
4175 | unsigned size;
4176 | const uint8_t *ptr;
4177 |
4178 | size = tvb_strsize(tvb, offset);
4179 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4180 | if (lengthp)
4181 | *lengthp = size;
4182 | return get_iso_646_string(scope, ptr, size, table);
4183 | }
4184 |
4185 | static uint8_t *
4186 | tvb_get_utf_8_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4187 | {
4188 | unsigned size;
4189 | const uint8_t *ptr;
4190 |
4191 | size = tvb_strsize(tvb, offset);
4192 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4193 | if (lengthp)
4194 | *lengthp = size;
4195 | return get_utf_8_string(scope, ptr, size);
4196 | }
4197 |
4198 | static uint8_t *
4199 | tvb_get_stringz_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4200 | {
4201 | unsigned size;
4202 | const uint8_t *ptr;
4203 |
4204 | size = tvb_strsize(tvb, offset);
4205 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4206 | if (lengthp)
4207 | *lengthp = size;
4208 | return get_8859_1_string(scope, ptr, size);
4209 | }
4210 |
4211 | static uint8_t *
4212 | tvb_get_stringz_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
4213 | {
4214 | unsigned size;
4215 | const uint8_t *ptr;
4216 |
4217 | size = tvb_strsize(tvb, offset);
4218 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4219 | if (lengthp)
4220 | *lengthp = size;
4221 | return get_unichar2_string(scope, ptr, size, table);
4222 | }
4223 |
4224 | /*
4225 | * Given a tvbuff and an offset, with the offset assumed to refer to
4226 | * a null-terminated string, find the length of that string (and throw
4227 | * an exception if the tvbuff ends before we find the null), ensure that
4228 | * the TVB is flat, and return a pointer to the string (in the TVB).
4229 | * Also return the length of the string (including the terminating null)
4230 | * through a pointer.
4231 | *
4232 | * As long as we aren't using composite TVBs, this saves the cycles used
4233 | * (often unnecessarily) in allocating a buffer and copying the string into
4234 | * it. OTOH, the string returned isn't valid UTF-8, so it shouldn't be
4235 | * added to the tree, the columns, etc., just used with various other
4236 | * functions that operate on strings that don't have a tvb_ equivalent.
4237 | * That's hard to enforce, which is why this is deprecated.
4238 | */
4239 | const uint8_t *
4240 | tvb_get_const_stringz(tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4241 | {
4242 | unsigned size;
4243 | const uint8_t *strptr;
4244 |
4245 | size = tvb_strsize(tvb, offset);
4246 | strptr = ensure_contiguous_unsigned(tvb, offset, size);
4247 | if (lengthp)
4248 | *lengthp = size;
4249 | return strptr;
4250 | }
4251 |
4252 | static char *
4253 | tvb_get_ucs_2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4254 | {
4255 | unsigned size; /* Number of bytes in string */
4256 | const uint8_t *ptr;
4257 |
4258 | size = tvb_unicode_strsize(tvb, offset);
4259 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4260 | if (lengthp)
4261 | *lengthp = size;
4262 | return (char*)get_ucs_2_string(scope, ptr, size, encoding);
4263 | }
4264 |
4265 | static char *
4266 | tvb_get_utf_16_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4267 | {
4268 | unsigned size;
4269 | const uint8_t *ptr;
4270 |
4271 | size = tvb_unicode_strsize(tvb, offset);
4272 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4273 | if (lengthp)
4274 | *lengthp = size;
4275 | return (char*)get_utf_16_string(scope, ptr, size, encoding);
4276 | }
4277 |
4278 | static char *
4279 | tvb_get_ucs_4_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4280 | {
4281 | unsigned size;
4282 | const uint8_t *ptr;
4283 |
4284 | size = tvb_ucs_4_strsize(tvb, offset);
4285 |
4286 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4287 | if (lengthp)
4288 | *lengthp = size;
4289 | return (char*)get_ucs_4_string(scope, ptr, size, encoding);
4290 | }
4291 |
4292 | static uint8_t *
4293 | tvb_get_nonascii_unichar2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[256])
4294 | {
4295 | unsigned size;
4296 | const uint8_t *ptr;
4297 |
4298 | size = tvb_strsize(tvb, offset);
4299 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4300 | if (lengthp)
4301 | *lengthp = size;
4302 | return get_nonascii_unichar2_string(scope, ptr, size, table);
4303 | }
4304 |
4305 | static uint8_t *
4306 | tvb_get_t61_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4307 | {
4308 | unsigned size;
4309 | const uint8_t *ptr;
4310 |
4311 | size = tvb_strsize(tvb, offset);
4312 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4313 | if (lengthp)
4314 | *lengthp = size;
4315 | return get_t61_string(scope, ptr, size);
4316 | }
4317 |
4318 | static uint8_t *
4319 | tvb_get_gb18030_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4320 | {
4321 | unsigned size;
4322 | const uint8_t *ptr;
4323 |
4324 | size = tvb_strsize(tvb, offset);
4325 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4326 | if (lengthp)
4327 | *lengthp = size;
4328 | return get_gb18030_string(scope, ptr, size);
4329 | }
4330 |
4331 | static uint8_t *
4332 | tvb_get_euc_kr_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4333 | {
4334 | unsigned size;
4335 | const uint8_t *ptr;
4336 |
4337 | size = tvb_strsize(tvb, offset);
4338 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4339 | if (lengthp)
4340 | *lengthp = size;
4341 | return get_euc_kr_string(scope, ptr, size);
4342 | }
4343 |
4344 | static uint8_t *
4345 | tvb_get_dect_standard_8bits_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4346 | {
4347 | unsigned size;
4348 | const uint8_t *ptr;
4349 |
4350 | size = tvb_strsize(tvb, offset);
4351 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4352 | if (lengthp)
4353 | *lengthp = size;
4354 | return get_dect_standard_8bits_string(scope, ptr, size);
4355 | }
4356 |
4357 | uint8_t *
4358 | tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4359 | {
4360 | uint8_t *strptr;
4361 |
4362 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4362, "tvb && tvb->initialized" ))));
4363 |
4364 | switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
4365 |
4366 | case ENC_ASCII0x00000000:
4367 | default:
4368 | /*
4369 | * For now, we treat bogus values as meaning
4370 | * "ASCII" rather than reporting an error,
4371 | * for the benefit of old dissectors written
4372 | * when the last argument to proto_tree_add_item()
4373 | * was a bool for the byte order, not an
4374 | * encoding value, and passed non-zero values
4375 | * other than true to mean "little-endian".
4376 | */
4377 | strptr = tvb_get_ascii_stringz(scope, tvb, offset, lengthp);
4378 | break;
4379 |
4380 | case ENC_UTF_80x00000002:
4381 | strptr = tvb_get_utf_8_stringz(scope, tvb, offset, lengthp);
4382 | break;
4383 |
4384 | case ENC_UTF_160x00000004:
4385 | strptr = (uint8_t*)tvb_get_utf_16_stringz(scope, tvb, offset, lengthp,
4386 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4387 | break;
4388 |
4389 | case ENC_UCS_20x00000006:
4390 | strptr = (uint8_t*)tvb_get_ucs_2_stringz(scope, tvb, offset, lengthp,
4391 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4392 | break;
4393 |
4394 | case ENC_UCS_40x00000008:
4395 | strptr = (uint8_t*)tvb_get_ucs_4_stringz(scope, tvb, offset, lengthp,
4396 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4397 | break;
4398 |
4399 | case ENC_ISO_8859_10x0000000A:
4400 | /*
4401 | * ISO 8859-1 printable code point values are equal
4402 | * to the equivalent Unicode code point value, so
4403 | * no translation table is needed.
4404 | */
4405 | strptr = tvb_get_stringz_8859_1(scope, tvb, offset, lengthp);
4406 | break;
4407 |
4408 | case ENC_ISO_8859_20x0000000C:
4409 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_2);
4410 | break;
4411 |
4412 | case ENC_ISO_8859_30x0000000E:
4413 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_3);
4414 | break;
4415 |
4416 | case ENC_ISO_8859_40x00000010:
4417 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_4);
4418 | break;
4419 |
4420 | case ENC_ISO_8859_50x00000012:
4421 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_5);
4422 | break;
4423 |
4424 | case ENC_ISO_8859_60x00000014:
4425 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_6);
4426 | break;
4427 |
4428 | case ENC_ISO_8859_70x00000016:
4429 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_7);
4430 | break;
4431 |
4432 | case ENC_ISO_8859_80x00000018:
4433 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_8);
4434 | break;
4435 |
4436 | case ENC_ISO_8859_90x0000001A:
4437 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_9);
4438 | break;
4439 |
4440 | case ENC_ISO_8859_100x0000001C:
4441 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_10);
4442 | break;
4443 |
4444 | case ENC_ISO_8859_110x0000001E:
4445 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_11);
4446 | break;
4447 |
4448 | case ENC_ISO_8859_130x00000022:
4449 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_13);
4450 | break;
4451 |
4452 | case ENC_ISO_8859_140x00000024:
4453 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_14);
4454 | break;
4455 |
4456 | case ENC_ISO_8859_150x00000026:
4457 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_15);
4458 | break;
4459 |
4460 | case ENC_ISO_8859_160x00000028:
4461 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_16);
4462 | break;
4463 |
4464 | case ENC_WINDOWS_12500x0000002A:
4465 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1250);
4466 | break;
4467 |
4468 | case ENC_WINDOWS_12510x0000003C:
4469 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1251);
4470 | break;
4471 |
4472 | case ENC_WINDOWS_12520x0000003A:
4473 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1252);
4474 | break;
4475 |
4476 | case ENC_MAC_ROMAN0x00000030:
4477 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_mac_roman);
4478 | break;
4479 |
4480 | case ENC_CP4370x00000032:
4481 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp437);
4482 | break;
4483 |
4484 | case ENC_CP8550x0000003E:
4485 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp855);
4486 | break;
4487 |
4488 | case ENC_CP8660x00000040:
4489 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp866);
4490 | break;
4491 |
4492 | case ENC_ISO_646_BASIC0x00000042:
4493 | strptr = tvb_get_iso_646_stringz(scope, tvb, offset, lengthp, charset_table_iso_646_basic);
4494 | break;
4495 |
4496 | case ENC_BCD_DIGITS_0_90x00000044:
4497 | case ENC_KEYPAD_ABC_TBCD0x00000046:
4498 | case ENC_KEYPAD_BC_TBCD0x00000048:
4499 | case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
4500 | 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." );
4501 | break;
4502 |
4503 | case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
4504 | case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
4505 | case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
4506 | 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" );
4507 | break;
4508 |
4509 | case ENC_ASCII_7BITS0x00000034:
4510 | 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" );
4511 | break;
4512 |
4513 | case ENC_EBCDIC0x0000002E:
4514 | /*
4515 | * "Common" EBCDIC, covering all characters with the
4516 | * same code point in all Roman-alphabet EBCDIC code
4517 | * pages.
4518 | */
4519 | strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic);
4520 | break;
4521 |
4522 | case ENC_EBCDIC_CP0370x00000038:
4523 | /*
4524 | * EBCDIC code page 037.
4525 | */
4526 | strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp037);
4527 | break;
4528 |
4529 | case ENC_EBCDIC_CP5000x00000060:
4530 | /*
4531 | * EBCDIC code page 500.
4532 | */
4533 | strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp500);
4534 | break;
4535 |
4536 | case ENC_T610x00000036:
4537 | strptr = tvb_get_t61_stringz(scope, tvb, offset, lengthp);
4538 | break;
4539 |
4540 | case ENC_GB180300x00000050:
4541 | strptr = tvb_get_gb18030_stringz(scope, tvb, offset, lengthp);
4542 | break;
4543 |
4544 | case ENC_EUC_KR0x00000052:
4545 | strptr = tvb_get_euc_kr_stringz(scope, tvb, offset, lengthp);
4546 | break;
4547 |
4548 | case ENC_APN_STR0x00000054:
4549 | /* At least as defined in 3GPP TS 23.003 Clause 9.1, null-termination
4550 | * does make sense as internal nulls are not allowed. */
4551 | 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" );
4552 | break;
4553 |
4554 | case ENC_DECT_STANDARD_8BITS0x00000056:
4555 | strptr = tvb_get_dect_standard_8bits_stringz(scope, tvb, offset, lengthp);
4556 | break;
4557 | }
4558 |
4559 | return strptr;
4560 | }
4561 |
4562 | /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
4563 | * no more than bufsize number of bytes, including terminating NUL, to buffer.
4564 | * Returns length of string (not including terminating NUL).
4565 | * In this way, it acts like snprintf().
4566 | *
4567 | * bufsize MUST be greater than 0.
4568 | *
4569 | * This function does not otherwise throw an exception for running out of room
4570 | * in the buffer or running out of remaining bytes in the tvbuffer. It will
4571 | * copy as many bytes to the buffer as possible (the lesser of bufsize - 1
4572 | * and the number of remaining captured bytes) and then NUL terminate the
4573 | * string.
4574 | *
4575 | * *bytes_copied will contain the number of bytes actually copied,
4576 | * including the terminating-NUL if present in the frame, but not
4577 | * if it was supplied by the function instead of copied from packet data.
4578 | * [Not currently used, but could be used to determine how much to advance
4579 | * the offset.]
4580 | */
4581 | static unsigned
4582 | _tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const unsigned offset, const unsigned bufsize, uint8_t* buffer, unsigned *bytes_copied)
4583 | {
4584 | int exception;
4585 | int stringlen;
4586 | unsigned limit;
4587 | unsigned len = 0;
4588 |
4589 | /* Only read to end of tvbuff, w/o throwing exception. */
4590 | exception = validate_offset_and_remaining(tvb, offset, &len);
4591 | if (exception)
4592 | THROW(exception)except_throw(1, (exception), ((void*)0));
4593 |
4594 | /* There must at least be room for the terminating NUL. */
4595 | DISSECTOR_ASSERT(bufsize != 0)((void) ((bufsize != 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4595, "bufsize != 0" ))));
4596 |
4597 | /* If there's no room for anything else, just return the NUL. */
4598 | if (bufsize == 1) {
4599 | buffer[0] = 0;
4600 | if (len && tvb_get_uint8(tvb, offset) == 0) {
4601 | *bytes_copied = 1;
4602 | } else {
4603 | *bytes_copied = 0;
4604 | }
4605 | return 0;
4606 | }
4607 |
4608 | /* validate_offset_and_remaining() won't throw an exception if we're
4609 | * looking at the byte immediately after the end of the tvbuff. */
4610 | if (len == 0) {
4611 | THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
4612 | }
4613 |
4614 | if (len < bufsize) {
4615 | limit = len;
4616 | }
4617 | else {
4618 | limit = bufsize - 1;
4619 | }
4620 |
4621 | stringlen = tvb_strnlen(tvb, offset, limit);
4622 | /* If NUL wasn't found, copy the data up to the limit and terminate */
4623 | if (stringlen == -1) {
4624 | tvb_memcpy(tvb, buffer, offset, limit);
4625 | buffer[limit] = 0;
4626 | *bytes_copied = limit;
4627 | return limit;
4628 | }
4629 |
4630 | /* Copy the string to buffer */
4631 | tvb_memcpy(tvb, buffer, offset, stringlen + 1);
4632 | *bytes_copied = stringlen + 1;
4633 | return (unsigned)stringlen;
4634 | }
4635 |
4636 | unsigned
4637 | tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const unsigned offset, const unsigned bufsize, uint8_t* buffer)
4638 | {
4639 | unsigned bytes_copied;
4640 |
4641 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4641, "tvb && tvb->initialized" ))));
4642 |
4643 | return _tvb_get_raw_bytes_as_stringz(tvb, offset, bufsize, buffer, &bytes_copied);
4644 | }
4645 |
4646 | /*
4647 | * Given a tvbuff, an offset into the tvbuff, a buffer, and a buffer size,
4648 | * extract as many raw bytes from the tvbuff, starting at the offset,
4649 | * as 1) are available in the tvbuff and 2) will fit in the buffer, leaving
4650 | * room for a terminating NUL.
4651 | */
4652 | unsigned
4653 | tvb_get_raw_bytes_as_string(tvbuff_t *tvb, const unsigned offset, char *buffer, size_t bufsize)
4654 | {
4655 | unsigned len = 0;
4656 |
4657 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4657, "tvb && tvb->initialized" ))));
4658 |
4659 | /* There must be room for the string and the terminating NUL. */
4660 | DISSECTOR_ASSERT(bufsize > 0)((void) ((bufsize > 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4660, "bufsize > 0" ))));
4661 |
4662 | /* bufsize is size_t, but tvbuffers only have up to unsigned bytes */
4663 | 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" , 4663, "bufsize - 1 < (2147483647 *2U +1U)"))));
4664 |
4665 | len = _tvb_captured_length_remaining(tvb, offset);
4666 | if (len == 0) {
4667 | buffer[0] = '\0';
4668 | return 0;
4669 | }
4670 | if (len > (bufsize - 1))
4671 | len = (unsigned)(bufsize - 1);
4672 |
4673 | /* Copy the string to buffer */
4674 | tvb_memcpy(tvb, buffer, offset, len);
4675 | buffer[len] = '\0';
4676 | return len;
4677 | }
4678 |
4679 | bool_Bool
4680 | tvb_ascii_isprint(tvbuff_t *tvb, const unsigned offset, const unsigned length)
4681 | {
4682 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4682, "tvb && tvb->initialized" ))));
4683 |
4684 | /* XXX - Perhaps this function should return false instead of throwing
4685 | * an exception. */
4686 | const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4687 |
4688 | for (unsigned i = 0; i < length; i++, buf++)
4689 | if (!g_ascii_isprint(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_PRINT) != 0))
4690 | return false0;
4691 |
4692 | return true1;
4693 | }
4694 |
4695 | bool_Bool
4696 | tvb_ascii_isprint_remaining(tvbuff_t *tvb, const unsigned offset)
4697 | {
4698 | int exception;
4699 | unsigned length;
4700 |
4701 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4701, "tvb && tvb->initialized" ))));
4702 |
4703 | exception = validate_offset_and_remaining(tvb, offset, &length);
4704 | if (exception)
4705 | THROW(exception)except_throw(1, (exception), ((void*)0));
4706 |
4707 | const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4708 |
4709 | for (unsigned i = 0; i < length; i++, buf++)
4710 | if (!g_ascii_isprint(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_PRINT) != 0))
4711 | return false0;
4712 |
4713 | return true1;
4714 | }
4715 |
4716 | bool_Bool
4717 | tvb_utf_8_isprint(tvbuff_t *tvb, const unsigned offset, const unsigned length)
4718 | {
4719 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4719, "tvb && tvb->initialized" ))));
4720 |
4721 | /* XXX - Perhaps this function should return false instead of throwing
4722 | * an exception. */
4723 | const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4724 |
4725 | return isprint_utf8_string((const char*)buf, length);
4726 | }
4727 |
4728 | bool_Bool
4729 | tvb_utf_8_isprint_remaining(tvbuff_t *tvb, const unsigned offset)
4730 | {
4731 | int exception;
4732 | unsigned length;
4733 |
4734 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4734, "tvb && tvb->initialized" ))));
4735 |
4736 | exception = validate_offset_and_remaining(tvb, offset, &length);
4737 | if (exception)
4738 | THROW(exception)except_throw(1, (exception), ((void*)0));
4739 |
4740 | const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4741 |
4742 | return isprint_utf8_string((const char*)buf, length);
4743 | }
4744 |
4745 | bool_Bool
4746 | tvb_ascii_isdigit(tvbuff_t *tvb, const unsigned offset, const unsigned length)
4747 | {
4748 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4748, "tvb && tvb->initialized" ))));
4749 |
4750 | /* XXX - Perhaps this function should return false instead of throwing
4751 | * an exception. */
4752 | const uint8_t* buf = ensure_contiguous_unsigned(tvb, offset, length);
4753 |
4754 | for (unsigned i = 0; i < length; i++, buf++)
4755 | if (!g_ascii_isdigit(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_DIGIT) != 0))
4756 | return false0;
4757 |
4758 | return true1;
4759 | }
4760 |
4761 | static ws_mempbrk_pattern pbrk_crlf;
4762 |
4763 | static bool_Bool
4764 | _tvb_find_line_end_length(tvbuff_t *tvb, const unsigned offset, const unsigned limit, unsigned *linelen, unsigned *next_offset)
4765 | {
4766 | static bool_Bool compiled = false0;
4767 | unsigned eob_offset;
4768 | unsigned eol_offset;
4769 | unsigned char found_needle = 0;
4770 |
4771 | if (!compiled) {
4772 | ws_mempbrk_compile(&pbrk_crlf, "\r\n");
4773 | compiled = true1;
4774 | }
4775 |
4776 | eob_offset = offset + limit;
4777 |
4778 | /*
4779 | * Look either for a CR or an LF.
4780 | */
4781 | if (!_tvb_ws_mempbrk_uint8_length(tvb, offset, limit, &pbrk_crlf, &eol_offset, &found_needle)) {
4782 | /*
4783 | * No CR or LF - line is presumably continued in next packet.
4784 | */
4785 | /*
4786 | * Pretend the line runs to the end of the tvbuff.
4787 | */
4788 | if (linelen)
4789 | *linelen = eob_offset - offset;
4790 | if (next_offset)
4791 | *next_offset = eob_offset;
4792 | /*
4793 | * Tell our caller we saw no EOL, so they can try to
4794 | * desegment and get the entire line into one tvbuff.
4795 | */
4796 | return false0;
4797 | } else {
4798 | /*
4799 | * Find the number of bytes between the starting offset
4800 | * and the CR or LF.
4801 | */
4802 | if (linelen)
4803 | *linelen = eol_offset - offset;
4804 |
4805 | /*
4806 | * Is it a CR?
4807 | */
4808 | if (found_needle == '\r') {
4809 | /*
4810 | * Yes - is it followed by an LF?
4811 | */
4812 | if (eol_offset + 1 >= eob_offset) {
4813 | /*
4814 | * Dunno - the next byte isn't in this
4815 | * tvbuff.
4816 | */
4817 | if (next_offset)
4818 | *next_offset = eob_offset;
4819 | /*
4820 | * We'll return false, although that
4821 | * runs the risk that if the line
4822 | * really *is* terminated with a CR,
4823 | * we won't properly dissect this
4824 | * tvbuff.
4825 | *
4826 | * It's probably more likely that
4827 | * the line ends with CR-LF than
4828 | * that it ends with CR by itself.
4829 | *
4830 | * XXX - Return a third value?
4831 | */
4832 | return false0;
4833 | } else {
4834 | /*
4835 | * Well, we can at least look at the next
4836 | * byte.
4837 | */
4838 | if (tvb_get_uint8(tvb, eol_offset + 1) == '\n') {
4839 | /*
4840 | * It's an LF; skip over the CR.
4841 | */
4842 | eol_offset++;
4843 | }
4844 | }
4845 | }
4846 |
4847 | /*
4848 | * Return the offset of the character after the last
4849 | * character in the line, skipping over the last character
4850 | * in the line terminator.
4851 | */
4852 | if (next_offset)
4853 | *next_offset = eol_offset + 1;
4854 | }
4855 | return true1;
4856 | }
4857 |
4858 | bool_Bool
4859 | tvb_find_line_end_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *linelen, unsigned *next_offset)
4860 | {
4861 | unsigned limit;
4862 | int exception;
4863 |
4864 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4864, "tvb && tvb->initialized" ))));
4865 |
4866 | exception = validate_offset_and_remaining(tvb, offset, &limit);
4867 | if (exception)
4868 | THROW(exception)except_throw(1, (exception), ((void*)0));
4869 |
4870 | return _tvb_find_line_end_length(tvb, offset, limit, linelen, next_offset);
4871 | }
4872 |
4873 | bool_Bool
4874 | tvb_find_line_end_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *linelen, unsigned *next_offset)
4875 | {
4876 | unsigned limit;
4877 | int exception;
4878 |
4879 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4879, "tvb && tvb->initialized" ))));
4880 |
4881 | exception = validate_offset_and_remaining(tvb, offset, &limit);
4882 | if (exception)
4883 | THROW(exception)except_throw(1, (exception), ((void*)0));
4884 |
4885 | /* Only search to end of tvbuff, w/o throwing exception. */
4886 | if (limit > maxlength) {
4887 | /* Maximum length doesn't go past end of tvbuff; search
4888 | to that value. */
4889 | limit = maxlength;
4890 | }
4891 |
4892 | return _tvb_find_line_end_length(tvb, offset, limit, linelen, next_offset);
4893 | }
4894 |
4895 | static ws_mempbrk_pattern pbrk_crlf_dquote;
4896 |
4897 | static bool_Bool
4898 | _tvb_find_line_end_unquoted_length(tvbuff_t *tvb, const unsigned offset, unsigned limit, unsigned *linelen, unsigned *next_offset)
4899 | {
4900 | unsigned cur_offset, char_offset;
4901 | bool_Bool is_quoted;
4902 | unsigned char c = 0;
4903 | unsigned eob_offset;
4904 | static bool_Bool compiled = false0;
4905 | unsigned len;
4906 | bool_Bool found;
4907 |
4908 | if (!compiled) {
4909 | ws_mempbrk_compile(&pbrk_crlf_dquote, "\r\n\"");
4910 | compiled = true1;
4911 | }
4912 |
4913 | eob_offset = offset + limit;
4914 |
4915 | cur_offset = offset;
4916 | is_quoted = false0;
4917 | for (;;) {
4918 | len = limit - (cur_offset - offset);
4919 | /*
4920 | * Is this part of the string quoted?
4921 | */
4922 | if (is_quoted) {
4923 | /*
4924 | * Yes - look only for the terminating quote.
4925 | */
4926 | found = _tvb_find_uint8_length(tvb, cur_offset, len, '"', &char_offset);
4927 | } else {
4928 | /*
4929 | * Look either for a CR, an LF, or a '"'.
4930 | */
4931 | found = _tvb_ws_mempbrk_uint8_length(tvb, cur_offset, len, &pbrk_crlf_dquote, &char_offset, &c);
4932 | }
4933 | if (!found) {
4934 | /*
4935 | * Not found - line is presumably continued in
4936 | * next packet.
4937 | * We pretend the line runs to the end of the tvbuff.
4938 | */
4939 | if (linelen)
4940 | *linelen = eob_offset - offset;
4941 | if (next_offset)
4942 | *next_offset = eob_offset;
4943 | break;
4944 | }
4945 |
4946 | if (is_quoted) {
4947 | /*
4948 | * We're processing a quoted string.
4949 | * We only looked for ", so we know it's a ";
4950 | * as we're processing a quoted string, it's a
4951 | * closing quote.
4952 | */
4953 | is_quoted = false0;
4954 | } else {
4955 | /*
4956 | * OK, what is it?
4957 | */
4958 | if (c == '"') {
4959 | /*
4960 | * Un-quoted "; it begins a quoted
4961 | * string.
4962 | */
4963 | is_quoted = true1;
4964 | } else {
4965 | /*
4966 | * It's a CR or LF; we've found a line
4967 | * terminator.
4968 | *
4969 | * Find the number of bytes between the
4970 | * starting offset and the CR or LF.
4971 | */
4972 | if (linelen)
4973 | *linelen = char_offset - offset;
4974 |
4975 | /*
4976 | * Is it a CR?
4977 | */
4978 | if (c == '\r') {
4979 | /*
4980 | * Yes; is it followed by an LF?
4981 | */
4982 | if (char_offset + 1 < eob_offset &&
4983 | tvb_get_uint8(tvb, char_offset + 1)
4984 | == '\n') {
4985 | /*
4986 | * Yes; skip over the CR.
4987 | */
4988 | char_offset++;
4989 | }
4990 | }
4991 |
4992 | /*
4993 | * Return the offset of the character after
4994 | * the last character in the line, skipping
4995 | * over the last character in the line
4996 | * terminator, and quit.
4997 | */
4998 | if (next_offset)
4999 | *next_offset = char_offset + 1;
5000 | break;
5001 | }
5002 | }
5003 |
5004 | /*
5005 | * Step past the character we found.
5006 | */
5007 | cur_offset = char_offset + 1;
5008 | if (cur_offset >= eob_offset) {
5009 | /*
5010 | * The character we found was the last character
5011 | * in the tvbuff - line is presumably continued in
5012 | * next packet.
5013 | * We pretend the line runs to the end of the tvbuff.
5014 | */
5015 | if (linelen)
5016 | *linelen = eob_offset - offset;
5017 | if (next_offset)
5018 | *next_offset = eob_offset;
5019 | break;
5020 | }
5021 | }
5022 | return found;
5023 | }
5024 |
5025 | /*
5026 | * Given a tvbuff, an offset into the tvbuff, and a length that starts
5027 | * at that offset (which may be -1 for "all the way to the end of the
5028 | * tvbuff"), find the end of the (putative) line that starts at the
5029 | * specified offset in the tvbuff, going no further than the specified
5030 | * length.
5031 | *
5032 | * However, treat quoted strings inside the buffer specially - don't
5033 | * treat newlines in quoted strings as line terminators.
5034 | *
5035 | * Return the length of the line (not counting the line terminator at
5036 | * the end), or the amount of data remaining in the buffer if we don't
5037 | * find a line terminator.
5038 | *
5039 | * If "next_offset" is not NULL, set "*next_offset" to the offset of the
5040 | * character past the line terminator, or past the end of the buffer if
5041 | * we don't find a line terminator.
5042 | */
5043 | int
5044 | tvb_find_line_end_unquoted(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset)
5045 | {
5046 | unsigned linelen;
5047 | unsigned abs_next_offset;
5048 | unsigned limit;
5049 | int exception;
5050 |
5051 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5051, "tvb && tvb->initialized" ))));
5052 |
5053 | exception = validate_offset_and_remaining(tvb, offset, &limit);
5054 | if (exception)
5055 | THROW(exception)except_throw(1, (exception), ((void*)0));
5056 |
5057 | /* Only search to end of tvbuff, w/o throwing exception. */
5058 | if (len >= 0 && limit > (unsigned) len) {
5059 | /* Maximum length doesn't go past end of tvbuff; search
5060 | to that value. */
5061 | limit = (unsigned) len;
5062 | }
5063 |
5064 | _tvb_find_line_end_unquoted_length(tvb, offset, limit, &linelen, &abs_next_offset);
5065 | if (next_offset) {
5066 | *next_offset = (int)abs_next_offset;
5067 | }
5068 | return (int)linelen;
5069 | }
5070 |
5071 | bool_Bool
5072 | tvb_find_line_end_unquoted_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *linelen, unsigned *next_offset)
5073 | {
5074 | unsigned limit;
5075 | int exception;
5076 |
5077 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5077, "tvb && tvb->initialized" ))));
5078 |
5079 | exception = validate_offset_and_remaining(tvb, offset, &limit);
5080 | if (exception)
5081 | THROW(exception)except_throw(1, (exception), ((void*)0));
5082 |
5083 | return _tvb_find_line_end_unquoted_length(tvb, offset, limit, linelen, next_offset);
5084 | }
5085 |
5086 | bool_Bool
5087 | tvb_find_line_end_unquoted_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *linelen, unsigned *next_offset)
5088 | {
5089 | unsigned limit;
5090 | int exception;
5091 |
5092 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5092, "tvb && tvb->initialized" ))));
5093 |
5094 | exception = validate_offset_and_remaining(tvb, offset, &limit);
5095 | if (exception)
5096 | THROW(exception)except_throw(1, (exception), ((void*)0));
5097 |
5098 | /* Only search to end of tvbuff, w/o throwing exception. */
5099 | if (limit > maxlength) {
5100 | /* Maximum length doesn't go past end of tvbuff; search
5101 | to that value. */
5102 | limit = maxlength;
5103 | }
5104 |
5105 | return _tvb_find_line_end_unquoted_length(tvb, offset, limit, linelen, next_offset);
5106 | }
5107 |
5108 | /*
5109 | * Copied from the mgcp dissector. (This function should be moved to /epan )
5110 | * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
5111 | * character following offset or offset + maxlength -1 whichever
5112 | * is smaller.
5113 | *
5114 | * Parameters:
5115 | * tvb - The tvbuff in which we are skipping whitespace.
5116 | * offset - The offset in tvb from which we begin trying to skip whitespace.
5117 | * maxlength - The maximum distance from offset that we may try to skip
5118 | * whitespace.
5119 | *
5120 | * Returns: The position in tvb of the first non-whitespace
5121 | * character following offset or offset + maxlength -1 whichever
5122 | * is smaller.
5123 | */
5124 | unsigned
5125 | tvb_skip_wsp(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
5126 | {
5127 | unsigned counter;
5128 | unsigned end, tvb_len;
5129 | uint8_t tempchar;
5130 |
5131 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5131, "tvb && tvb->initialized" ))));
5132 |
5133 | /* Get the length remaining */
5134 | /*tvb_len = tvb_captured_length(tvb);*/
5135 | tvb_len = tvb->length;
5136 |
5137 | if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
5138 | end = tvb_len;
5139 | }
5140 |
5141 | /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
5142 | /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
5143 | * slightly faster but also tests for vertical tab and form feed. */
5144 | for (counter = offset;
5145 | counter < end &&
5146 | ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
5147 | tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
5148 | counter++);
5149 |
5150 | return counter;
5151 | }
5152 |
5153 | unsigned
5154 | tvb_skip_wsp_return(tvbuff_t *tvb, const unsigned offset)
5155 | {
5156 | unsigned counter;
5157 | uint8_t tempchar;
5158 |
5159 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5159, "tvb && tvb->initialized" ))));
5160 |
5161 | /* XXX - DISSECTOR_ASSERT(offset > 0) and then subtract 1 from offset?
5162 | * The way this is used the caller almost always wants to subtract one
5163 | * from the offset of a non WSP separator, and they might forget to do
5164 | * so and then this function return the offset past the separator. */
5165 |
5166 | /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
5167 | * slightly faster but also tests for vertical tab and form feed. */
5168 | for (counter = offset; counter > 0 &&
5169 | ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
5170 | tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
5171 | counter++;
5172 |
5173 | return counter;
5174 | }
5175 |
5176 | unsigned
5177 | tvb_skip_uint8(tvbuff_t *tvb, unsigned offset, const unsigned maxlength, const uint8_t ch)
5178 | {
5179 | unsigned end, tvb_len;
5180 |
5181 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5181, "tvb && tvb->initialized" ))));
5182 |
5183 | /* Get the length remaining */
5184 | /*tvb_len = tvb_captured_length(tvb);*/
5185 | tvb_len = tvb->length;
5186 |
5187 | if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
5188 | end = tvb_len;
5189 | }
5190 |
5191 | while (offset < end) {
5192 | uint8_t tempch = tvb_get_uint8(tvb, offset);
5193 |
5194 | if (tempch != ch)
5195 | break;
5196 | offset++;
5197 | }
5198 |
5199 | return offset;
5200 | }
5201 |
5202 | static ws_mempbrk_pattern pbrk_whitespace;
5203 |
5204 | static bool_Bool
5205 | _tvb_get_token_len_length(tvbuff_t *tvb, const unsigned offset, unsigned limit, unsigned *tokenlen, unsigned *next_offset)
5206 | {
5207 | unsigned eot_offset;
5208 | unsigned char found_needle = 0;
5209 | static bool_Bool compiled = false0;
5210 |
5211 | if (!compiled) {
5212 | ws_mempbrk_compile(&pbrk_whitespace, " \r\n");
5213 | compiled = true1;
5214 | }
5215 |
5216 | /*
5217 | * Look either for a space, CR, or LF.
5218 | */
5219 | if (!_tvb_ws_mempbrk_uint8_length(tvb, offset, limit, &pbrk_whitespace, &eot_offset, &found_needle)) {
5220 | /*
5221 | * No space, CR or LF - token is presumably continued in next packet.
5222 | */
5223 | /*
5224 | * Pretend the token runs to the end of the tvbuff.
5225 | */
5226 | if (tokenlen)
5227 | *tokenlen = eot_offset - offset;
5228 | if (next_offset)
5229 | *next_offset = eot_offset;
5230 | /*
5231 | * Tell our caller we saw no whitespace, so they can
5232 | * try to desegment and get the entire line
5233 | * into one tvbuff.
5234 | */
5235 | return false0;
5236 | }
5237 |
5238 | /*
5239 | * Find the number of bytes between the starting offset
5240 | * and the space, CR or LF.
5241 | */
5242 | if (tokenlen)
5243 | *tokenlen = eot_offset - offset;
5244 |
5245 | /*
5246 | * Return the offset of the character after the token delimiter,
5247 | * skipping over the last character in the separator.
5248 | *
5249 | * XXX - get_token_len() from strutil.h returns the start offset of
5250 | * the next token by skipping trailing spaces (but not spaces that
5251 | * follow a CR or LF, only consecutive spaces). Should we align
5252 | * the two functions? Most dissectors want to skip extra spaces,
5253 | * and while the dissector _can_ follow up with tvb_skip_wsp, this
5254 | * probably causes dissectors to use tvb_get_ptr + get_token_len,
5255 | * which we want to discourage. OTOH, IMAP, which uses this, says
5256 | * "in all cases, SP refers to exactly one space. It is NOT permitted
5257 | * to substitute TAB, insert additional spaces, or otherwise treat
5258 | * SP as being equivalent to linear whitespace (LWSP)."
5259 | * https://www.rfc-editor.org/rfc/rfc9051.html#name-formal-syntax
5260 | *
5261 | * XXX - skip over CR-LF as a unit like tvb_find_line_end()?
5262 | * get_token_len() doesn't, probably because most dissectors have
5263 | * already found the line end before, but it probably makes sense
5264 | * to do and unlike above it's unlikely it would break any protocol
5265 | * (and might even fix some.)
5266 | */
5267 | if (next_offset)
5268 | *next_offset = eot_offset + 1;
5269 |
5270 | return true1;
5271 | }
5272 |
5273 | int tvb_get_token_len(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset, const bool_Bool desegment)
5274 | {
5275 | unsigned tokenlen;
5276 | unsigned abs_next_offset;
5277 | unsigned limit;
5278 | int exception;
5279 |
5280 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5280, "tvb && tvb->initialized" ))));
5281 |
5282 | exception = validate_offset_and_remaining(tvb, offset, &limit);
5283 | if (exception)
5284 | THROW(exception)except_throw(1, (exception), ((void*)0));
5285 |
5286 | /* Only search to end of tvbuff, w/o throwing exception. */
5287 | if (len >= 0 && limit > (unsigned) len) {
5288 | /* Maximum length doesn't go past end of tvbuff; search
5289 | to that value. */
5290 | limit = (unsigned) len;
5291 | }
5292 |
5293 | if (!_tvb_get_token_len_length(tvb, offset, limit, &tokenlen, &abs_next_offset) && desegment) {
5294 | return -1;
5295 | }
5296 | if (next_offset) {
5297 | *next_offset = (int)abs_next_offset;
5298 | }
5299 | return (int)tokenlen;
5300 | }
5301 |
5302 | bool_Bool
5303 | tvb_get_token_len_remaining(tvbuff_t *tvb, const unsigned offset, unsigned *tokenlen, unsigned *next_offset)
5304 | {
5305 | unsigned limit;
5306 | int exception;
5307 |
5308 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5308, "tvb && tvb->initialized" ))));
5309 |
5310 | exception = validate_offset_and_remaining(tvb, offset, &limit);
5311 | if (exception)
5312 | THROW(exception)except_throw(1, (exception), ((void*)0));
5313 |
5314 | return _tvb_get_token_len_length(tvb, offset, limit, tokenlen, next_offset);
5315 | }
5316 |
5317 | bool_Bool
5318 | tvb_get_token_len_length(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength, unsigned *tokenlen, unsigned *next_offset)
5319 | {
5320 | unsigned limit;
5321 | int exception;
5322 |
5323 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5323, "tvb && tvb->initialized" ))));
5324 |
5325 | exception = validate_offset_and_remaining(tvb, offset, &limit);
5326 | if (exception)
5327 | THROW(exception)except_throw(1, (exception), ((void*)0));
5328 |
5329 | /* Only search to end of tvbuff, w/o throwing exception. */
5330 | if (limit > maxlength) {
5331 | /* Maximum length doesn't go past end of tvbuff; search
5332 | to that value. */
5333 | limit = maxlength;
5334 | }
5335 |
5336 | return _tvb_get_token_len_length(tvb, offset, limit, tokenlen, next_offset);
5337 | }
5338 |
5339 | /*
5340 | * Format a bunch of data from a tvbuff as bytes, returning a pointer
5341 | * to the string with the formatted data, with "punct" as a byte
5342 | * separator.
5343 | */
5344 | char *
5345 | tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned len, const char punct)
5346 | {
5347 | 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);
5348 | }
5349 |
5350 | /*
5351 | * Given a wmem scope, a tvbuff, an offset, a length, an input digit
5352 | * set, and a boolean indicator, fetch BCD-encoded digits from a
5353 | * tvbuff starting from either the low or high half byte of the
5354 | * first byte depending on the boolean indicator (true means "start
5355 | * with the high half byte, ignoring the low half byte", and false
5356 | * means "start with the low half byte and proceed to the high half
5357 | * byte), formating the digits into characters according to the
5358 | * input digit set, and return a pointer to a UTF-8 string, allocated
5359 | * using the wmem scope. A nibble of 0xf is considered a 'filler'
5360 | * and will end the conversion. Similarly if odd is set the last
5361 | * high nibble will be omitted. (Note that if both skip_first and
5362 | * odd are true, then both the first and last semi-octet are skipped,
5363 | * i.e. an even number of nibbles are considered.)
5364 | */
5365 | char *
5366 | 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)
5367 | {
5368 | const uint8_t *ptr;
5369 | int i = 0;
5370 | char *digit_str;
5371 | uint8_t octet, nibble;
5372 |
5373 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 5373, "tvb && tvb->initialized" ))));
5374 |
5375 | ptr = ensure_contiguous_unsigned(tvb, offset, len);
5376 |
5377 | /*
5378 | * XXX - map illegal digits (digits that map to 0) to REPLACEMENT
5379 | * CHARACTER, and have all the tables in epan/tvbuff.c use 0 rather
5380 | * than '?'?
5381 | */
5382 | digit_str = (char *)wmem_alloc(scope, len*2 + 1);
5383 |
5384 | while (len > 0) {
5385 | octet = *ptr;
5386 | if (!skip_first) {
5387 | if (bigendian) {
5388 | nibble = (octet >> 4) & 0x0f;
5389 | } else {
5390 | nibble = octet & 0x0f;
5391 | }
5392 | if (nibble == 0x0f) {
5393 | /*
5394 | * Stop digit.
5395 | */
5396 | break;
5397 | }
5398 | digit_str[i] = dgt->out[nibble];
5399 | i++;
5400 | }
5401 | skip_first = false0;
5402 |
5403 | /*
5404 | * unpack second value in byte
5405 | */
5406 | if (bigendian) {
5407 | nibble = octet & 0x0f;
5408 | } else {
5409 | nibble = octet >> 4;
5410 | }
5411 |
5412 | if (nibble == 0x0f) {
5413 | /*
5414 | * This is the stop digit or a filler digit. Ignore
5415 | * it.
5416 | */
5417 | break;
5418 | }
5419 | if ((len == 1) && (odd == true1 )){
5420 | /* Last octet, skip last high nibble in case of odd number of digits */
5421 | break;
5422 | }
5423 | digit_str[i] = dgt->out[nibble];
5424 | i++;
5425 |
5426 | ptr++;
5427 | len--;
5428 | }
5429 | digit_str[i] = '\0';
5430 | return digit_str;
5431 | }
5432 |
5433 | /* XXXX Fix me - needs odd indicator added (or just use of tvb_get_bcd_string / proto_tree_add_item) */
5434 | const char *
5435 | 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)
5436 | {
5437 | if (!dgt)
5438 | dgt = &Dgt0_9_bcd;
5439 |
5440 | return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, false0);
5441 | }
5442 |
5443 | const char *
5444 | 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)
5445 | {
5446 | if (!dgt)
5447 | dgt = &Dgt0_9_bcd;
5448 |
5449 | return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, true1);
5450 | }
5451 |
5452 | /*
5453 | * Format a bunch of data from a tvbuff as bytes, returning a pointer
5454 | * to the string with the formatted data.
5455 | */
5456 | char *
5457 | tvb_bytes_to_str(wmem_allocator_t *allocator, tvbuff_t *tvb, const unsigned offset, const unsigned len)
5458 | {
5459 | 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);
5460 | }
5461 |
5462 | /* Find a needle tvbuff within a haystack tvbuff. */
5463 | int
5464 | tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const int haystack_offset)
5465 | {
5466 | unsigned haystack_abs_offset = 0, haystack_abs_length = 0;
5467 | const uint8_t *haystack_data;
5468 | const uint8_t *needle_data;
5469 | const unsigned needle_len = needle_tvb->length;
5470 | const uint8_t *location;
5471 |
5472 | 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", 5472, "haystack_tvb && haystack_tvb->initialized" ))));
5473 |
5474 | if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
5475 | return -1;
5476 | }
5477 |
5478 | /* Get pointers to the tvbuffs' data. */
5479 | haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
5480 | needle_data = ensure_contiguous(needle_tvb, 0, -1);
5481 |
5482 | check_offset_length(haystack_tvb, haystack_offset, -1,
5483 | &haystack_abs_offset, &haystack_abs_length);
5484 |
5485 | location = ws_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
5486 | needle_data, needle_len);
5487 |
5488 | if (location) {
5489 | return (int) (location - haystack_data);
5490 | }
5491 |
5492 | return -1;
5493 | }
5494 |
5495 | /* Find a needle tvbuff within a haystack tvbuff. */
5496 | bool_Bool
5497 | tvb_find_tvb_remaining(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const unsigned haystack_offset, unsigned *found_offset)
5498 | {
5499 | const uint8_t *haystack_data;
5500 | const uint8_t *needle_data;
5501 | const unsigned needle_len = needle_tvb->length;
5502 | const uint8_t *location;
5503 | int exception;
5504 | unsigned haystack_rem_length;
5505 |
5506 | 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", 5506, "haystack_tvb && haystack_tvb->initialized" ))));
5507 | 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", 5507, "needle_tvb && needle_tvb->initialized" ))));
5508 |
5509 | if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
5510 | return false0;
5511 | }
5512 |
5513 | exception = validate_offset_and_remaining(haystack_tvb, haystack_offset, &haystack_rem_length);
5514 | if (exception)
5515 | THROW(exception)except_throw(1, (exception), ((void*)0));
5516 |
5517 | /* Get pointers to the tvbuffs' data. */
5518 | haystack_data = ensure_contiguous_unsigned(haystack_tvb, haystack_offset, haystack_rem_length);
5519 | needle_data = ensure_contiguous_unsigned(needle_tvb, 0, needle_len);
5520 |
5521 | location = ws_memmem(haystack_data, haystack_rem_length,
5522 | needle_data, needle_len);
5523 |
5524 | if (location) {
5525 | if (found_offset)
5526 | *found_offset = (unsigned) (location - haystack_data) + haystack_offset;
5527 | return true1;
5528 | }
5529 |
5530 | return false0;
5531 | }
5532 |
5533 | unsigned
5534 | tvb_raw_offset(tvbuff_t *tvb)
5535 | {
5536 | if (!(tvb->flags & TVBUFF_RAW_OFFSET0x00000002)) {
5537 | tvb->raw_offset = tvb_offset_from_real_beginning(tvb);
5538 | tvb->flags |= TVBUFF_RAW_OFFSET0x00000002;
5539 | }
5540 | return tvb->raw_offset;
5541 | }
5542 |
5543 | void
5544 | tvb_set_fragment(tvbuff_t *tvb)
5545 | {
5546 | tvb->flags |= TVBUFF_FRAGMENT0x00000001;
5547 | }
5548 |
5549 | struct tvbuff *
5550 | tvb_get_ds_tvb(tvbuff_t *tvb)
5551 | {
5552 | return(tvb->ds_tvb);
5553 | }
5554 |
5555 | unsigned
5556 | tvb_get_varint(tvbuff_t *tvb, unsigned offset, unsigned maxlen, uint64_t *value, const unsigned encoding)
5557 | {
5558 | *value = 0;
5559 |
5560 | switch (encoding & ENC_VARINT_MASK(0x00000002|0x00000004|0x00000008|0x00000010)) {
5561 | case ENC_VARINT_PROTOBUF0x00000002:
5562 | {
5563 | unsigned i;
5564 | uint64_t b; /* current byte */
5565 |
5566 | for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5567 | b = tvb_get_uint8(tvb, offset++);
5568 | *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5569 |
5570 | if (b < 0x80) {
5571 | /* end successfully because of last byte's msb(most significant bit) is zero */
5572 | return i + 1;
5573 | }
5574 | }
5575 | break;
5576 | }
5577 |
5578 | case ENC_VARINT_ZIGZAG0x00000008:
5579 | {
5580 | unsigned i;
5581 | uint64_t b; /* current byte */
5582 |
5583 | for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5584 | b = tvb_get_uint8(tvb, offset++);
5585 | *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5586 |
5587 | if (b < 0x80) {
5588 | /* end successfully because of last byte's msb(most significant bit) is zero */
5589 | *value = (*value >> 1) ^ ((*value & 1) ? -1 : 0);
5590 | return i + 1;
5591 | }
5592 | }
5593 | break;
5594 | }
5595 |
5596 | case ENC_VARINT_SDNV0x00000010:
5597 | {
5598 | /* Decodes similar to protobuf but in MSByte order */
5599 | unsigned i;
5600 | uint64_t b; /* current byte */
5601 |
5602 | for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5603 | b = tvb_get_uint8(tvb, offset++);
5604 | if ((i == 9) && (*value >= UINT64_C(1)1UL<<(64-7))) {
5605 | // guaranteed overflow, not valid SDNV
5606 | return 0;
5607 | }
5608 | *value <<= 7;
5609 | *value |= (b & 0x7F); /* add lower 7 bits to val */
5610 |
5611 | if (b < 0x80) {
5612 | /* end successfully because of last byte's msb(most significant bit) is zero */
5613 | return i + 1;
5614 | }
5615 | }
5616 | break;
5617 | }
5618 |
5619 | case ENC_VARINT_QUIC0x00000004:
5620 | {
5621 | /* calculate variable length */
5622 | *value = tvb_get_uint8(tvb, offset);
5623 | switch((*value) >> 6) {
5624 | case 0: /* 0b00 => 1 byte length (6 bits Usable) */
5625 | (*value) &= 0x3F;
5626 | return 1;
5627 | case 1: /* 0b01 => 2 bytes length (14 bits Usable) */
5628 | *value = tvb_get_ntohs(tvb, offset) & 0x3FFF;
5629 | return 2;
5630 | case 2: /* 0b10 => 4 bytes length (30 bits Usable) */
5631 | *value = tvb_get_ntohl(tvb, offset) & 0x3FFFFFFF;
5632 | return 4;
5633 | case 3: /* 0b11 => 8 bytes length (62 bits Usable) */
5634 | *value = tvb_get_ntoh64(tvb, offset) & UINT64_C(0x3FFFFFFFFFFFFFFF)0x3FFFFFFFFFFFFFFFUL;
5635 | return 8;
5636 | default: /* No Possible */
5637 | ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/tvbuff.c", 5637, __func__, "assertion \"not reached\" failed");
5638 | break;
5639 | }
5640 | break;
5641 | }
5642 |
5643 | default:
5644 | DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\"" , "epan/tvbuff.c", 5644));
5645 | }
5646 |
5647 | return 0; /* 10 bytes scanned, but no bytes' msb is zero */
5648 | }
5649 |
5650 | /*
5651 | * Editor modelines - https://www.wireshark.org/tools/modelines.html
5652 | *
5653 | * Local variables:
5654 | * c-basic-offset: 8
5655 | * tab-width: 8
5656 | * indent-tabs-mode: t
5657 | * End:
5658 | *
5659 | * vi: set shiftwidth=8 tabstop=8 noexpandtab:
5660 | * :indentSize=8:tabSize=8:noTabs=false:
5661 | */