### Bug Summary

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