### Bug Summary

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

### Annotated Source Code

Press '?' to see keyboard shortcuts

Show analyzer invocation
clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name tvbuff.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/epan -isystem /builds/wireshark/wireshark/build/epan -isystem /usr/include/mit-krb5 -isystem /usr/include/lua5.5 -isystem /usr/include/libxml2 -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D epan_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-07-20-100411-3661-1 -x c /builds/wireshark/wireshark/epan/tvbuff.c

Keyboard shortcuts:

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

Close
Show only relevant lines Show control flow arrows

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