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