### Bug Summary

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

### Annotated Source Code

Press '?' to see keyboard shortcuts

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

Keyboard shortcuts:

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

Close
Show only relevant lines Show control flow arrows

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