### Bug Summary

File: | epan/tvbuff.c
Warning: | line 530, 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-29-100354-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 unsigned offset, const unsigned 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" ))));
4 | ← | Assuming 'tvb' is non-null | →
5 | ← | Assuming field 'initialized' is true | →
6 | ← | '?' condition is true | →
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) {
7 | ← | Assuming 'remaining_bits' is not equal to 0 | →
8 | ← | Taking true branch | →
495 | datalen++;
496 | }
497 |
498 | /* already aligned -> shortcut */
499 | if (((left == 0) && (remaining_bits
9.1 | 'remaining_bits' is not equal to 0
== 0)) || datalen == 0) {
9 | ← | Assuming 'left' is equal to 0 | →
10 | ← | Assuming 'datalen' is not equal to 0 | →
11 | ← | Taking false branch | →
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) {
12 | ← | Assuming the condition is true | →
13 | ← | Taking true branch | →
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);
14 | ← | Memory is allocated | →
513 |
514 | /* shift tvb data bit_offset bits to the left */
515 | for (i = 0; i
14.1 | 'i' is < 'datalen'
< datalen; i++)
15 | ← | Loop condition is true. Entering loop body | →
16 | ← | Assuming 'i' is >= 'datalen' | →
17 | ← | Loop condition is false. Execution continues on line 528 | →
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);
18 | ← | Potential leak of memory pointed to by 'buf'
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" ))));
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) {
556 | dst_len++;
557 | }
558 |
559 | /* already aligned -> shortcut */
560 | if (((right == 0) && (remaining_bits == 0)) || dst_len == 0) {
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) {
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);
576 |
577 | for (i = 0; i < (dst_len - 1); i++)
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 > dst_len)
584 | buf[i] |= (data[i+1] << left);
585 | /* Preserve only remaining bits in last octet if not multiple of 8 */
586 | if (remaining_bits)
587 | buf[i] &= ((1 << remaining_bits) - 1);
588 |
589 | sub_tvb = tvb_new_child_real_data(tvb, buf, dst_len, dst_len);
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 unsigned offset, const unsigned length,
1925 | const unsigned encoding, GByteArray *bytes, unsigned *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 + (unsigned)(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 unsigned offset, const unsigned length,
1977 | const unsigned encoding, nstime_t *ns, unsigned *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 = (unsigned)(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 false | →
2 | ← | Taking false branch | →
2351 | sub_tvb = tvb_new_octet_right_aligned(tvb, bit_offset, (int32_t) no_of_bits);
2352 | } else {
2353 | sub_tvb = tvb_new_octet_aligned(tvb, bit_offset, (int32_t) no_of_bits);
3 | ← | Calling 'tvb_new_octet_aligned' | →
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 | static inline uint8_t *
3195 | tvb_get_raw_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned length)
3196 | {
3197 | uint8_t *strbuf;
3198 |
3199 | tvb_ensure_bytes_exist(tvb, offset, length);
3200 | strbuf = (uint8_t *)wmem_alloc(scope, length + 1);
3201 | tvb_memcpy(tvb, strbuf, offset, length);
3202 | strbuf[length] = '\0';
3203 | return strbuf;
3204 | }
3205 |
3206 | /*
3207 | * Given a wmem scope, a tvbuff, an offset, and a length, treat the string
3208 | * of bytes referred to by the tvbuff, the offset, and the length as an
3209 | * ISO 8859/1 string, and return a pointer to a UTF-8 string, allocated
3210 | * using the wmem scope.
3211 | */
3212 | static uint8_t *
3213 | tvb_get_string_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3214 | {
3215 | const uint8_t *ptr;
3216 |
3217 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3218 | return get_8859_1_string(scope, ptr, length);
3219 | }
3220 |
3221 | /*
3222 | * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3223 | * table, treat the string of bytes referred to by the tvbuff, the offset,
3224 | * and the length as a string encoded using one octet per character, with
3225 | * octets with the high-order bit clear being ASCII and octets with the
3226 | * high-order bit set being mapped by the translation table to 2-byte
3227 | * Unicode Basic Multilingual Plane characters (including REPLACEMENT
3228 | * CHARACTER), and return a pointer to a UTF-8 string, allocated with the
3229 | * wmem scope.
3230 | */
3231 | static uint8_t *
3232 | tvb_get_string_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[0x80])
3233 | {
3234 | const uint8_t *ptr;
3235 |
3236 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3237 | return get_unichar2_string(scope, ptr, length, table);
3238 | }
3239 |
3240 | /*
3241 | * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3242 | * giving the byte order, treat the string of bytes referred to by the
3243 | * tvbuff, the offset, and the length as a UCS-2 encoded string in
3244 | * the byte order in question, containing characters from the Basic
3245 | * Multilingual Plane (plane 0) of Unicode, and return a pointer to a
3246 | * UTF-8 string, allocated with the wmem scope.
3247 | *
3248 | * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3249 | * optionally with ENC_BOM.
3250 | *
3251 | * Specify length in bytes.
3252 | *
3253 | * XXX - should map lead and trail surrogate values to REPLACEMENT
3254 | * CHARACTERs (0xFFFD)?
3255 | * XXX - if there are an odd number of bytes, should put a
3256 | * REPLACEMENT CHARACTER at the end.
3257 | */
3258 | static uint8_t *
3259 | tvb_get_ucs_2_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3260 | {
3261 | const uint8_t *ptr;
3262 |
3263 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3264 | return get_ucs_2_string(scope, ptr, length, encoding);
3265 | }
3266 |
3267 | /*
3268 | * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3269 | * giving the byte order, treat the string of bytes referred to by the
3270 | * tvbuff, the offset, and the length as a UTF-16 encoded string in
3271 | * the byte order in question, and return a pointer to a UTF-8 string,
3272 | * allocated with the wmem scope.
3273 | *
3274 | * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3275 | * optionally with ENC_BOM.
3276 | *
3277 | * Specify length in bytes.
3278 | *
3279 | * XXX - should map surrogate errors to REPLACEMENT CHARACTERs (0xFFFD).
3280 | * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3281 | * XXX - if there are an odd number of bytes, should put a
3282 | * REPLACEMENT CHARACTER at the end.
3283 | */
3284 | static uint8_t *
3285 | tvb_get_utf_16_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3286 | {
3287 | const uint8_t *ptr;
3288 |
3289 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3290 | return get_utf_16_string(scope, ptr, length, encoding);
3291 | }
3292 |
3293 | /*
3294 | * Given a wmem scope, a tvbuff, an offset, a length, and an encoding
3295 | * giving the byte order, treat the string of bytes referred to by the
3296 | * tvbuff, the offset, and the length as a UCS-4 encoded string in
3297 | * the byte order in question, and return a pointer to a UTF-8 string,
3298 | * allocated with the wmem scope.
3299 | *
3300 | * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN,
3301 | * optionally with ENC_BOM.
3302 | *
3303 | * Specify length in bytes
3304 | *
3305 | * XXX - should map lead and trail surrogate values to a "substitute"
3306 | * UTF-8 character?
3307 | * XXX - should map code points > 10FFFF to REPLACEMENT CHARACTERs.
3308 | * XXX - if the number of bytes isn't a multiple of 4, should put a
3309 | * REPLACEMENT CHARACTER at the end.
3310 | */
3311 | static char *
3312 | tvb_get_ucs_4_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned length, const unsigned encoding)
3313 | {
3314 | const uint8_t *ptr;
3315 |
3316 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3317 | return (char*)get_ucs_4_string(scope, ptr, length, encoding);
3318 | }
3319 |
3320 | char *
3321 | tvb_get_ts_23_038_7bits_string_packed(wmem_allocator_t *scope, tvbuff_t *tvb,
3322 | const unsigned bit_offset, unsigned no_of_chars)
3323 | {
3324 | unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3325 | unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3326 | const uint8_t *ptr;
3327 |
3328 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3328, "tvb && tvb->initialized" ))));
3329 |
3330 | ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3331 | return (char*)get_ts_23_038_7bits_string_packed(scope, ptr, bit_offset, no_of_chars);
3332 | }
3333 |
3334 | char *
3335 | tvb_get_ts_23_038_7bits_string_unpacked(wmem_allocator_t *scope, tvbuff_t *tvb,
3336 | const unsigned offset, unsigned length)
3337 | {
3338 | const uint8_t *ptr;
3339 |
3340 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3340, "tvb && tvb->initialized" ))));
3341 |
3342 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3343 | return (char*)get_ts_23_038_7bits_string_unpacked(scope, ptr, length);
3344 | }
3345 |
3346 | char *
3347 | tvb_get_etsi_ts_102_221_annex_a_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3348 | const unsigned offset, unsigned length)
3349 | {
3350 | const uint8_t *ptr;
3351 |
3352 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3352, "tvb && tvb->initialized" ))));
3353 |
3354 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3355 | return (char*)get_etsi_ts_102_221_annex_a_string(scope, ptr, length);
3356 | }
3357 |
3358 | char *
3359 | tvb_get_ascii_7bits_string(wmem_allocator_t *scope, tvbuff_t *tvb,
3360 | const unsigned bit_offset, unsigned no_of_chars)
3361 | {
3362 | unsigned in_offset = bit_offset >> 3; /* Current pointer to the input buffer */
3363 | unsigned length = ((no_of_chars + 1) * 7 + (bit_offset & 0x07)) >> 3;
3364 | const uint8_t *ptr;
3365 |
3366 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3366, "tvb && tvb->initialized" ))));
3367 |
3368 | ptr = ensure_contiguous_unsigned(tvb, in_offset, length);
3369 | return (char*)get_ascii_7bits_string(scope, ptr, bit_offset, no_of_chars);
3370 | }
3371 |
3372 | /*
3373 | * Given a wmem scope, a tvbuff, an offset, a length, and a translation
3374 | * table, treat the string of bytes referred to by the tvbuff, the offset,
3375 | * and the length as a string encoded using one octet per character, with
3376 | * octets being mapped by the translation table to 2-byte Unicode Basic
3377 | * Multilingual Plane characters (including REPLACEMENT CHARACTER), and
3378 | * return a pointer to a UTF-8 string, allocated with the wmem scope.
3379 | */
3380 | static uint8_t *
3381 | tvb_get_nonascii_unichar2_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length, const gunichar2 table[256])
3382 | {
3383 | const uint8_t *ptr;
3384 |
3385 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3386 | return get_nonascii_unichar2_string(scope, ptr, length, table);
3387 | }
3388 |
3389 | /*
3390 | * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3391 | * referred to by the tvbuff, offset, and length as a GB18030 encoded string,
3392 | * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3393 | * converted having substituted REPLACEMENT CHARACTER according to the
3394 | * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3395 | * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3396 | *
3397 | * As expected, this will also decode GBK and GB2312 strings.
3398 | */
3399 | static uint8_t *
3400 | tvb_get_gb18030_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3401 | {
3402 | const uint8_t *ptr;
3403 |
3404 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3405 | return get_gb18030_string(scope, ptr, length);
3406 | }
3407 |
3408 | /*
3409 | * Given a wmem scope, a tvbuff, an offset, and a length, treat the bytes
3410 | * referred to by the tvbuff, offset, and length as a EUC-KR encoded string,
3411 | * and return a pointer to a UTF-8 string, allocated with the wmem scope,
3412 | * converted having substituted REPLACEMENT CHARACTER according to the
3413 | * Unicode Standard 5.22 U+FFFD Substitution for Conversion.
3414 | * ( https://www.unicode.org/versions/Unicode13.0.0/ch05.pdf )
3415 | */
3416 | static uint8_t *
3417 | tvb_get_euc_kr_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3418 | {
3419 | const uint8_t *ptr;
3420 |
3421 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3422 | return get_euc_kr_string(scope, ptr, length);
3423 | }
3424 |
3425 | static uint8_t *
3426 | tvb_get_t61_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3427 | {
3428 | const uint8_t *ptr;
3429 |
3430 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3431 | return get_t61_string(scope, ptr, length);
3432 | }
3433 |
3434 | /*
3435 | * Encoding tables for BCD strings.
3436 | */
3437 | static const dgt_set_t Dgt0_9_bcd = {
3438 | {
3439 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3440 | '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?','?'
3441 | }
3442 | };
3443 |
3444 | static const dgt_set_t Dgt_keypad_abc_tbcd = {
3445 | {
3446 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3447 | '0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'
3448 | }
3449 | };
3450 |
3451 | static const dgt_set_t Dgt_ansi_tbcd = {
3452 | {
3453 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3454 | '0','1','2','3','4','5','6','7','8','9','?','B','C','*','#','?'
3455 | }
3456 | };
3457 |
3458 | static const dgt_set_t Dgt_dect_standard_4bits_tbcd = {
3459 | {
3460 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
3461 | '0','1','2','3','4','5','6','7','8','9','?',' ','?','?','?','?'
3462 | }
3463 | };
3464 |
3465 | static uint8_t *
3466 | tvb_get_apn_string(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3467 | unsigned length)
3468 | {
3469 | wmem_strbuf_t *str;
3470 |
3471 | /*
3472 | * This is a domain name.
3473 | *
3474 | * 3GPP TS 23.003, section 19.4.2 "Fully Qualified Domain Names
3475 | * (FQDNs)", subsection 19.4.2.1 "General", says:
3476 | *
3477 | * The encoding of any identifier used as part of a Fully
3478 | * Qualified Domain Name (FQDN) shall follow the Name Syntax
3479 | * defined in IETF RFC 2181 [18], IETF RFC 1035 [19] and
3480 | * IETF RFC 1123 [20]. An FQDN consists of one or more
3481 | * labels. Each label is coded as a one octet length field
3482 | * followed by that number of octets coded as 8 bit ASCII
3483 | * characters.
3484 | *
3485 | * so this does not appear to use full-blown DNS compression -
3486 | * the upper 2 bits of the length don't indicate that it's a
3487 | * pointer or an extended label (RFC 2673).
3488 | */
3489 | str = wmem_strbuf_new_sized(scope, length + 1);
3490 | if (length > 0) {
3491 | const uint8_t *ptr;
3492 |
3493 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3494 |
3495 | for (;;) {
3496 | unsigned label_len;
3497 |
3498 | /*
3499 | * Process this label.
3500 | */
3501 | label_len = *ptr;
3502 | ptr++;
3503 | length--;
3504 |
3505 | while (label_len != 0) {
3506 | uint8_t ch;
3507 |
3508 | if (length == 0)
3509 | goto end;
3510 |
3511 | ch = *ptr;
3512 | if (ch < 0x80)
3513 | wmem_strbuf_append_c(str, ch);
3514 | else
3515 | wmem_strbuf_append_unichar_repl(str)wmem_strbuf_append_unichar(str, 0x00FFFD);
3516 | ptr++;
3517 | label_len--;
3518 | length--;
3519 | }
3520 |
3521 | if (length == 0)
3522 | goto end;
3523 |
3524 | wmem_strbuf_append_c(str, '.');
3525 | }
3526 | }
3527 |
3528 | end:
3529 | return (uint8_t *) wmem_strbuf_finalize(str);
3530 | }
3531 |
3532 | static uint8_t *
3533 | tvb_get_dect_standard_8bits_string(wmem_allocator_t *scope, tvbuff_t *tvb, unsigned offset, unsigned length)
3534 | {
3535 | const uint8_t *ptr;
3536 |
3537 | ptr = ensure_contiguous_unsigned(tvb, offset, length);
3538 | return get_dect_standard_8bits_string(scope, ptr, length);
3539 | }
3540 |
3541 | /*
3542 | * Given a tvbuff, an offset, a length, and an encoding, allocate a
3543 | * buffer big enough to hold a non-null-terminated string of that length
3544 | * at that offset, plus a trailing '\0', copy into the buffer the
3545 | * string as converted from the appropriate encoding to UTF-8, and
3546 | * return a pointer to the string.
3547 | */
3548 | uint8_t *
3549 | tvb_get_string_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3550 | const unsigned length, const unsigned encoding)
3551 | {
3552 | uint8_t *strptr;
3553 | bool_Bool odd, skip_first;
3554 |
3555 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 3555, "tvb && tvb->initialized" ))));
3556 |
3557 | switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
3558 |
3559 | case ENC_ASCII0x00000000:
3560 | default:
3561 | /*
3562 | * For now, we treat bogus values as meaning
3563 | * "ASCII" rather than reporting an error,
3564 | * for the benefit of old dissectors written
3565 | * when the last argument to proto_tree_add_item()
3566 | * was a bool for the byte order, not an
3567 | * encoding value, and passed non-zero values
3568 | * other than true to mean "little-endian".
3569 | */
3570 | strptr = tvb_get_ascii_string(scope, tvb, offset, length);
3571 | break;
3572 |
3573 | case ENC_UTF_80x00000002:
3574 | strptr = tvb_get_utf_8_string(scope, tvb, offset, length);
3575 | break;
3576 |
3577 | case ENC_UTF_160x00000004:
3578 | strptr = tvb_get_utf_16_string(scope, tvb, offset, length,
3579 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3580 | break;
3581 |
3582 | case ENC_UCS_20x00000006:
3583 | strptr = tvb_get_ucs_2_string(scope, tvb, offset, length,
3584 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3585 | break;
3586 |
3587 | case ENC_UCS_40x00000008:
3588 | strptr = (uint8_t*)tvb_get_ucs_4_string(scope, tvb, offset, length,
3589 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
3590 | break;
3591 |
3592 | case ENC_ISO_8859_10x0000000A:
3593 | /*
3594 | * ISO 8859-1 printable code point values are equal
3595 | * to the equivalent Unicode code point value, so
3596 | * no translation table is needed.
3597 | */
3598 | strptr = tvb_get_string_8859_1(scope, tvb, offset, length);
3599 | break;
3600 |
3601 | case ENC_ISO_8859_20x0000000C:
3602 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_2);
3603 | break;
3604 |
3605 | case ENC_ISO_8859_30x0000000E:
3606 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_3);
3607 | break;
3608 |
3609 | case ENC_ISO_8859_40x00000010:
3610 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_4);
3611 | break;
3612 |
3613 | case ENC_ISO_8859_50x00000012:
3614 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_5);
3615 | break;
3616 |
3617 | case ENC_ISO_8859_60x00000014:
3618 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_6);
3619 | break;
3620 |
3621 | case ENC_ISO_8859_70x00000016:
3622 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_7);
3623 | break;
3624 |
3625 | case ENC_ISO_8859_80x00000018:
3626 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_8);
3627 | break;
3628 |
3629 | case ENC_ISO_8859_90x0000001A:
3630 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_9);
3631 | break;
3632 |
3633 | case ENC_ISO_8859_100x0000001C:
3634 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_10);
3635 | break;
3636 |
3637 | case ENC_ISO_8859_110x0000001E:
3638 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_11);
3639 | break;
3640 |
3641 | case ENC_ISO_8859_130x00000022:
3642 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_13);
3643 | break;
3644 |
3645 | case ENC_ISO_8859_140x00000024:
3646 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_14);
3647 | break;
3648 |
3649 | case ENC_ISO_8859_150x00000026:
3650 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_15);
3651 | break;
3652 |
3653 | case ENC_ISO_8859_160x00000028:
3654 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_iso_8859_16);
3655 | break;
3656 |
3657 | case ENC_WINDOWS_12500x0000002A:
3658 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1250);
3659 | break;
3660 |
3661 | case ENC_WINDOWS_12510x0000003C:
3662 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1251);
3663 | break;
3664 |
3665 | case ENC_WINDOWS_12520x0000003A:
3666 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp1252);
3667 | break;
3668 |
3669 | case ENC_MAC_ROMAN0x00000030:
3670 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_mac_roman);
3671 | break;
3672 |
3673 | case ENC_CP4370x00000032:
3674 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp437);
3675 | break;
3676 |
3677 | case ENC_CP8550x0000003E:
3678 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp855);
3679 | break;
3680 |
3681 | case ENC_CP8660x00000040:
3682 | strptr = tvb_get_string_unichar2(scope, tvb, offset, length, charset_table_cp866);
3683 | break;
3684 |
3685 | case ENC_ISO_646_BASIC0x00000042:
3686 | strptr = tvb_get_iso_646_string(scope, tvb, offset, length, charset_table_iso_646_basic);
3687 | break;
3688 |
3689 | case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
3690 | {
3691 | unsigned bit_offset = offset << 3;
3692 | unsigned no_of_chars = (length << 3) / 7;
3693 | strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_packed(scope, tvb, bit_offset, no_of_chars);
3694 | }
3695 | break;
3696 |
3697 | case ENC_ASCII_7BITS0x00000034:
3698 | {
3699 | unsigned bit_offset = offset << 3;
3700 | unsigned no_of_chars = (length << 3) / 7;
3701 | strptr = (uint8_t*)tvb_get_ascii_7bits_string(scope, tvb, bit_offset, no_of_chars);
3702 | }
3703 | break;
3704 |
3705 | case ENC_EBCDIC0x0000002E:
3706 | /*
3707 | * "Common" EBCDIC, covering all characters with the
3708 | * same code point in all Roman-alphabet EBCDIC code
3709 | * pages.
3710 | */
3711 | strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic);
3712 | break;
3713 |
3714 | case ENC_EBCDIC_CP0370x00000038:
3715 | /*
3716 | * EBCDIC code page 037.
3717 | */
3718 | strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp037);
3719 | break;
3720 |
3721 | case ENC_EBCDIC_CP5000x00000060:
3722 | /*
3723 | * EBCDIC code page 500.
3724 | */
3725 | strptr = tvb_get_nonascii_unichar2_string(scope, tvb, offset, length, charset_table_ebcdic_cp500);
3726 | break;
3727 |
3728 | case ENC_T610x00000036:
3729 | strptr = tvb_get_t61_string(scope, tvb, offset, length);
3730 | break;
3731 |
3732 | case ENC_BCD_DIGITS_0_90x00000044:
3733 | /*
3734 | * Packed BCD, with digits 0-9.
3735 | */
3736 | odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3737 | skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3738 | strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt0_9_bcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3739 | break;
3740 |
3741 | case ENC_KEYPAD_ABC_TBCD0x00000046:
3742 | /*
3743 | * Keypad-with-a/b/c "telephony BCD" - packed BCD, with
3744 | * digits 0-9 and symbols *, #, a, b, and c.
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, &Dgt_keypad_abc_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3749 | break;
3750 |
3751 | case ENC_KEYPAD_BC_TBCD0x00000048:
3752 | /*
3753 | * Keypad-with-B/C "telephony BCD" - packed BCD, with
3754 | * digits 0-9 and symbols B, C, *, and #.
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_ansi_tbcd, skip_first, odd, !(encoding & ENC_LITTLE_ENDIAN0x80000000));
3759 | break;
3760 |
3761 | case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
3762 | strptr = (uint8_t*)tvb_get_ts_23_038_7bits_string_unpacked(scope, tvb, offset, length);
3763 | break;
3764 |
3765 | case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
3766 | strptr = (uint8_t*)tvb_get_etsi_ts_102_221_annex_a_string(scope, tvb, offset, length);
3767 | break;
3768 |
3769 | case ENC_GB180300x00000050:
3770 | strptr = tvb_get_gb18030_string(scope, tvb, offset, length);
3771 | break;
3772 |
3773 | case ENC_EUC_KR0x00000052:
3774 | strptr = tvb_get_euc_kr_string(scope, tvb, offset, length);
3775 | break;
3776 |
3777 | case ENC_APN_STR0x00000054:
3778 | strptr = tvb_get_apn_string(scope, tvb, offset, length);
3779 | break;
3780 |
3781 | case ENC_DECT_STANDARD_8BITS0x00000056:
3782 | strptr = tvb_get_dect_standard_8bits_string(scope, tvb, offset, length);
3783 | break;
3784 |
3785 | case ENC_DECT_STANDARD_4BITS_TBCD0x00000058:
3786 | /*
3787 | * DECT standard 4bits "telephony BCD" - packed BCD, with
3788 | * digits 0-9 and symbol SPACE for 0xb.
3789 | */
3790 | odd = (encoding & ENC_BCD_ODD_NUM_DIG0x00010000) >> 16;
3791 | skip_first = (encoding & ENC_BCD_SKIP_FIRST0x00020000) >> 17;
3792 | strptr = (uint8_t*)tvb_get_bcd_string(scope, tvb, offset, length, &Dgt_dect_standard_4bits_tbcd, skip_first, odd, false0);
3793 | break;
3794 | }
3795 | return strptr;
3796 | }
3797 |
3798 | /*
3799 | * This is like tvb_get_string_enc(), except that it handles null-padded
3800 | * strings.
3801 | *
3802 | * Currently, string values are stored as UTF-8 null-terminated strings,
3803 | * so nothing needs to be done differently for null-padded strings; we
3804 | * could save a little memory by not storing the null padding.
3805 | *
3806 | * If we ever store string values differently, in a fashion that doesn't
3807 | * involve null termination, that might change.
3808 | */
3809 | uint8_t *
3810 | tvb_get_stringzpad(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset,
3811 | const unsigned length, const unsigned encoding)
3812 | {
3813 | return tvb_get_string_enc(scope, tvb, offset, length, encoding);
3814 | }
3815 |
3816 | /*
3817 | * These routines are like the above routines, except that they handle
3818 | * null-terminated strings. They find the length of that string (and
3819 | * throw an exception if the tvbuff ends before we find the null), and
3820 | * also return through a pointer the length of the string, in bytes,
3821 | * including the terminating null (the terminating null being 2 bytes
3822 | * for UCS-2 and UTF-16, 4 bytes for UCS-4, and 1 byte for other
3823 | * encodings).
3824 | */
3825 | static uint8_t *
3826 | tvb_get_ascii_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3827 | {
3828 | unsigned size;
3829 | const uint8_t *ptr;
3830 |
3831 | size = tvb_strsize(tvb, offset);
3832 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3833 | if (lengthp)
3834 | *lengthp = size;
3835 | return get_ascii_string(scope, ptr, size);
3836 | }
3837 |
3838 | static uint8_t *
3839 | tvb_get_iso_646_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
3840 | {
3841 | unsigned size;
3842 | const uint8_t *ptr;
3843 |
3844 | size = tvb_strsize(tvb, offset);
3845 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3846 | if (lengthp)
3847 | *lengthp = size;
3848 | return get_iso_646_string(scope, ptr, size, table);
3849 | }
3850 |
3851 | static uint8_t *
3852 | tvb_get_utf_8_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3853 | {
3854 | unsigned size;
3855 | const uint8_t *ptr;
3856 |
3857 | size = tvb_strsize(tvb, offset);
3858 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3859 | if (lengthp)
3860 | *lengthp = size;
3861 | return get_utf_8_string(scope, ptr, size);
3862 | }
3863 |
3864 | static uint8_t *
3865 | tvb_get_stringz_8859_1(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3866 | {
3867 | unsigned size;
3868 | const uint8_t *ptr;
3869 |
3870 | size = tvb_strsize(tvb, offset);
3871 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3872 | if (lengthp)
3873 | *lengthp = size;
3874 | return get_8859_1_string(scope, ptr, size);
3875 | }
3876 |
3877 | static uint8_t *
3878 | tvb_get_stringz_unichar2(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[0x80])
3879 | {
3880 | unsigned size;
3881 | const uint8_t *ptr;
3882 |
3883 | size = tvb_strsize(tvb, offset);
3884 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3885 | if (lengthp)
3886 | *lengthp = size;
3887 | return get_unichar2_string(scope, ptr, size, table);
3888 | }
3889 |
3890 | /*
3891 | * Given a tvbuff and an offset, with the offset assumed to refer to
3892 | * a null-terminated string, find the length of that string (and throw
3893 | * an exception if the tvbuff ends before we find the null), ensure that
3894 | * the TVB is flat, and return a pointer to the string (in the TVB).
3895 | * Also return the length of the string (including the terminating null)
3896 | * through a pointer.
3897 | *
3898 | * As long as we aren't using composite TVBs, this saves the cycles used
3899 | * (often unnecessarily) in allocating a buffer and copying the string into
3900 | * it. OTOH, the string returned isn't valid UTF-8, so it shouldn't be
3901 | * added to the tree, the columns, etc., just used with various other
3902 | * functions that operate on strings that don't have a tvb_ equivalent.
3903 | * That's hard to enforce, which is why this is deprecated.
3904 | */
3905 | const uint8_t *
3906 | tvb_get_const_stringz(tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3907 | {
3908 | unsigned size;
3909 | const uint8_t *strptr;
3910 |
3911 | size = tvb_strsize(tvb, offset);
3912 | strptr = ensure_contiguous_unsigned(tvb, offset, size);
3913 | if (lengthp)
3914 | *lengthp = size;
3915 | return strptr;
3916 | }
3917 |
3918 | static char *
3919 | tvb_get_ucs_2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3920 | {
3921 | unsigned size; /* Number of bytes in string */
3922 | const uint8_t *ptr;
3923 |
3924 | size = tvb_unicode_strsize(tvb, offset);
3925 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3926 | if (lengthp)
3927 | *lengthp = size;
3928 | return (char*)get_ucs_2_string(scope, ptr, size, encoding);
3929 | }
3930 |
3931 | static char *
3932 | tvb_get_utf_16_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3933 | {
3934 | unsigned size;
3935 | const uint8_t *ptr;
3936 |
3937 | size = tvb_unicode_strsize(tvb, offset);
3938 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3939 | if (lengthp)
3940 | *lengthp = size;
3941 | return (char*)get_utf_16_string(scope, ptr, size, encoding);
3942 | }
3943 |
3944 | static char *
3945 | tvb_get_ucs_4_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
3946 | {
3947 | unsigned end_offset, size;
3948 | gunichar uchar;
3949 | const uint8_t *ptr;
3950 |
3951 | end_offset = offset;
3952 | do {
3953 | /* Endianness doesn't matter when looking for null */
3954 | uchar = tvb_get_ntohl(tvb, end_offset);
3955 | /* Make sure we don't overflow */
3956 | if (ckd_add(&end_offset, end_offset, 4)__builtin_add_overflow((end_offset), (4), (&end_offset))) {
3957 | THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
3958 | }
3959 | } while(uchar != 0);
3960 | size = end_offset - offset;
3961 |
3962 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3963 | if (lengthp)
3964 | *lengthp = size;
3965 | return (char*)get_ucs_4_string(scope, ptr, size, encoding);
3966 | }
3967 |
3968 | static uint8_t *
3969 | tvb_get_nonascii_unichar2_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const gunichar2 table[256])
3970 | {
3971 | unsigned size;
3972 | const uint8_t *ptr;
3973 |
3974 | size = tvb_strsize(tvb, offset);
3975 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3976 | if (lengthp)
3977 | *lengthp = size;
3978 | return get_nonascii_unichar2_string(scope, ptr, size, table);
3979 | }
3980 |
3981 | static uint8_t *
3982 | tvb_get_t61_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3983 | {
3984 | unsigned size;
3985 | const uint8_t *ptr;
3986 |
3987 | size = tvb_strsize(tvb, offset);
3988 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
3989 | if (lengthp)
3990 | *lengthp = size;
3991 | return get_t61_string(scope, ptr, size);
3992 | }
3993 |
3994 | static uint8_t *
3995 | tvb_get_gb18030_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
3996 | {
3997 | unsigned size;
3998 | const uint8_t *ptr;
3999 |
4000 | size = tvb_strsize(tvb, offset);
4001 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4002 | if (lengthp)
4003 | *lengthp = size;
4004 | return get_gb18030_string(scope, ptr, size);
4005 | }
4006 |
4007 | static uint8_t *
4008 | tvb_get_euc_kr_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4009 | {
4010 | unsigned size;
4011 | const uint8_t *ptr;
4012 |
4013 | size = tvb_strsize(tvb, offset);
4014 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4015 | if (lengthp)
4016 | *lengthp = size;
4017 | return get_euc_kr_string(scope, ptr, size);
4018 | }
4019 |
4020 | static uint8_t *
4021 | tvb_get_dect_standard_8bits_stringz(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp)
4022 | {
4023 | unsigned size;
4024 | const uint8_t *ptr;
4025 |
4026 | size = tvb_strsize(tvb, offset);
4027 | ptr = ensure_contiguous_unsigned(tvb, offset, size);
4028 | if (lengthp)
4029 | *lengthp = size;
4030 | return get_dect_standard_8bits_string(scope, ptr, size);
4031 | }
4032 |
4033 | uint8_t *
4034 | tvb_get_stringz_enc(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, unsigned *lengthp, const unsigned encoding)
4035 | {
4036 | uint8_t *strptr;
4037 |
4038 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4038, "tvb && tvb->initialized" ))));
4039 |
4040 | switch (encoding & ENC_CHARENCODING_MASK0x0000FFFE) {
4041 |
4042 | case ENC_ASCII0x00000000:
4043 | default:
4044 | /*
4045 | * For now, we treat bogus values as meaning
4046 | * "ASCII" rather than reporting an error,
4047 | * for the benefit of old dissectors written
4048 | * when the last argument to proto_tree_add_item()
4049 | * was a bool for the byte order, not an
4050 | * encoding value, and passed non-zero values
4051 | * other than true to mean "little-endian".
4052 | */
4053 | strptr = tvb_get_ascii_stringz(scope, tvb, offset, lengthp);
4054 | break;
4055 |
4056 | case ENC_UTF_80x00000002:
4057 | strptr = tvb_get_utf_8_stringz(scope, tvb, offset, lengthp);
4058 | break;
4059 |
4060 | case ENC_UTF_160x00000004:
4061 | strptr = (uint8_t*)tvb_get_utf_16_stringz(scope, tvb, offset, lengthp,
4062 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4063 | break;
4064 |
4065 | case ENC_UCS_20x00000006:
4066 | strptr = (uint8_t*)tvb_get_ucs_2_stringz(scope, tvb, offset, lengthp,
4067 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4068 | break;
4069 |
4070 | case ENC_UCS_40x00000008:
4071 | strptr = (uint8_t*)tvb_get_ucs_4_stringz(scope, tvb, offset, lengthp,
4072 | encoding & (ENC_LITTLE_ENDIAN0x80000000|ENC_BOM0x20000000));
4073 | break;
4074 |
4075 | case ENC_ISO_8859_10x0000000A:
4076 | /*
4077 | * ISO 8859-1 printable code point values are equal
4078 | * to the equivalent Unicode code point value, so
4079 | * no translation table is needed.
4080 | */
4081 | strptr = tvb_get_stringz_8859_1(scope, tvb, offset, lengthp);
4082 | break;
4083 |
4084 | case ENC_ISO_8859_20x0000000C:
4085 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_2);
4086 | break;
4087 |
4088 | case ENC_ISO_8859_30x0000000E:
4089 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_3);
4090 | break;
4091 |
4092 | case ENC_ISO_8859_40x00000010:
4093 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_4);
4094 | break;
4095 |
4096 | case ENC_ISO_8859_50x00000012:
4097 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_5);
4098 | break;
4099 |
4100 | case ENC_ISO_8859_60x00000014:
4101 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_6);
4102 | break;
4103 |
4104 | case ENC_ISO_8859_70x00000016:
4105 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_7);
4106 | break;
4107 |
4108 | case ENC_ISO_8859_80x00000018:
4109 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_8);
4110 | break;
4111 |
4112 | case ENC_ISO_8859_90x0000001A:
4113 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_9);
4114 | break;
4115 |
4116 | case ENC_ISO_8859_100x0000001C:
4117 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_10);
4118 | break;
4119 |
4120 | case ENC_ISO_8859_110x0000001E:
4121 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_11);
4122 | break;
4123 |
4124 | case ENC_ISO_8859_130x00000022:
4125 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_13);
4126 | break;
4127 |
4128 | case ENC_ISO_8859_140x00000024:
4129 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_14);
4130 | break;
4131 |
4132 | case ENC_ISO_8859_150x00000026:
4133 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_15);
4134 | break;
4135 |
4136 | case ENC_ISO_8859_160x00000028:
4137 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_iso_8859_16);
4138 | break;
4139 |
4140 | case ENC_WINDOWS_12500x0000002A:
4141 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1250);
4142 | break;
4143 |
4144 | case ENC_WINDOWS_12510x0000003C:
4145 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1251);
4146 | break;
4147 |
4148 | case ENC_WINDOWS_12520x0000003A:
4149 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp1252);
4150 | break;
4151 |
4152 | case ENC_MAC_ROMAN0x00000030:
4153 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_mac_roman);
4154 | break;
4155 |
4156 | case ENC_CP4370x00000032:
4157 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp437);
4158 | break;
4159 |
4160 | case ENC_CP8550x0000003E:
4161 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp855);
4162 | break;
4163 |
4164 | case ENC_CP8660x00000040:
4165 | strptr = tvb_get_stringz_unichar2(scope, tvb, offset, lengthp, charset_table_cp866);
4166 | break;
4167 |
4168 | case ENC_ISO_646_BASIC0x00000042:
4169 | strptr = tvb_get_iso_646_stringz(scope, tvb, offset, lengthp, charset_table_iso_646_basic);
4170 | break;
4171 |
4172 | case ENC_3GPP_TS_23_038_7BITS_PACKED0x0000002C:
4173 | case ENC_3GPP_TS_23_038_7BITS_UNPACKED0x0000004C:
4174 | case ENC_ETSI_TS_102_221_ANNEX_A0x0000004E:
4175 | 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" );
4176 | break;
4177 |
4178 | case ENC_ASCII_7BITS0x00000034:
4179 | 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" );
4180 | break;
4181 |
4182 | case ENC_EBCDIC0x0000002E:
4183 | /*
4184 | * "Common" EBCDIC, covering all characters with the
4185 | * same code point in all Roman-alphabet EBCDIC code
4186 | * pages.
4187 | */
4188 | strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic);
4189 | break;
4190 |
4191 | case ENC_EBCDIC_CP0370x00000038:
4192 | /*
4193 | * EBCDIC code page 037.
4194 | */
4195 | strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp037);
4196 | break;
4197 |
4198 | case ENC_EBCDIC_CP5000x00000060:
4199 | /*
4200 | * EBCDIC code page 500.
4201 | */
4202 | strptr = tvb_get_nonascii_unichar2_stringz(scope, tvb, offset, lengthp, charset_table_ebcdic_cp500);
4203 | break;
4204 |
4205 | case ENC_T610x00000036:
4206 | strptr = tvb_get_t61_stringz(scope, tvb, offset, lengthp);
4207 | break;
4208 |
4209 | case ENC_GB180300x00000050:
4210 | strptr = tvb_get_gb18030_stringz(scope, tvb, offset, lengthp);
4211 | break;
4212 |
4213 | case ENC_EUC_KR0x00000052:
4214 | strptr = tvb_get_euc_kr_stringz(scope, tvb, offset, lengthp);
4215 | break;
4216 |
4217 | case ENC_DECT_STANDARD_8BITS0x00000056:
4218 | strptr = tvb_get_dect_standard_8bits_stringz(scope, tvb, offset, lengthp);
4219 | break;
4220 | }
4221 |
4222 | return strptr;
4223 | }
4224 |
4225 | /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
4226 | * no more than bufsize number of bytes, including terminating NUL, to buffer.
4227 | * Returns length of string (not including terminating NUL), or -1 if the string was
4228 | * truncated in the buffer due to not having reached the terminating NUL.
4229 | * In this way, it acts like snprintf().
4230 | *
4231 | * bufsize MUST be greater than 0.
4232 | *
4233 | * When processing a packet where the remaining number of bytes is less
4234 | * than bufsize, an exception is not thrown if the end of the packet
4235 | * is reached before the NUL is found. If no NUL is found before reaching
4236 | * the end of the short packet, -1 is still returned, and the string
4237 | * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
4238 | * at the correct spot, terminating the string.
4239 | *
4240 | * *bytes_copied will contain the number of bytes actually copied,
4241 | * including the terminating-NUL.
4242 | */
4243 | static int
4244 | _tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const int offset, const unsigned bufsize, uint8_t* buffer, int *bytes_copied)
4245 | {
4246 | int stringlen;
4247 | unsigned abs_offset = 0;
4248 | int limit;
4249 | unsigned len = 0;
4250 | bool_Bool decreased_max = false0;
4251 |
4252 | /* Only read to end of tvbuff, w/o throwing exception. */
4253 | check_offset_length(tvb, offset, -1, &abs_offset, &len);
4254 |
4255 | /* There must at least be room for the terminating NUL. */
4256 | DISSECTOR_ASSERT(bufsize != 0)((void) ((bufsize != 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4256, "bufsize != 0" ))));
4257 |
4258 | /* If there's no room for anything else, just return the NUL. */
4259 | if (bufsize == 1) {
4260 | buffer[0] = 0;
4261 | *bytes_copied = 1;
4262 | return 0;
4263 | }
4264 |
4265 | /* check_offset_length() won't throw an exception if we're
4266 | * looking at the byte immediately after the end of the tvbuff. */
4267 | if (len == 0) {
4268 | THROW(ReportedBoundsError)except_throw(1, (3), ((void*)0));
4269 | }
4270 |
4271 | /*
4272 | * If we've been passed a negative number, bufsize will
4273 | * be huge.
4274 | */
4275 | DISSECTOR_ASSERT(bufsize <= INT_MAX)((void) ((bufsize <= 2147483647) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4275, "bufsize <= 2147483647" ))));
4276 |
4277 | if ((unsigned)len < bufsize) {
4278 | limit = len;
4279 | decreased_max = true1;
4280 | }
4281 | else {
4282 | limit = bufsize;
4283 | }
4284 |
4285 | stringlen = tvb_strnlen(tvb, abs_offset, limit - 1);
4286 | /* If NUL wasn't found, copy the data and return -1 */
4287 | if (stringlen == -1) {
4288 | tvb_memcpy(tvb, buffer, abs_offset, limit);
4289 | if (decreased_max) {
4290 | buffer[limit] = 0;
4291 | /* Add 1 for the extra NUL that we set at buffer[limit],
4292 | * pretending that it was copied as part of the string. */
4293 | *bytes_copied = limit + 1;
4294 | }
4295 | else {
4296 | *bytes_copied = limit;
4297 | }
4298 | return -1;
4299 | }
4300 |
4301 | /* Copy the string to buffer */
4302 | tvb_memcpy(tvb, buffer, abs_offset, stringlen + 1);
4303 | *bytes_copied = stringlen + 1;
4304 | return stringlen;
4305 | }
4306 |
4307 | int
4308 | tvb_get_raw_bytes_as_stringz(tvbuff_t *tvb, const int offset, const unsigned bufsize, uint8_t* buffer)
4309 | {
4310 | int len, bytes_copied;
4311 |
4312 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4312, "tvb && tvb->initialized" ))));
4313 |
4314 | len = _tvb_get_raw_bytes_as_stringz(tvb, offset, bufsize, buffer, &bytes_copied);
4315 |
4316 | if (len == -1) {
4317 | buffer[bufsize - 1] = 0;
4318 | return bytes_copied - 1;
4319 | }
4320 | else {
4321 | return len;
4322 | }
4323 | }
4324 |
4325 | /*
4326 | * Given a tvbuff, an offset into the tvbuff, a buffer, and a buffer size,
4327 | * extract as many raw bytes from the tvbuff, starting at the offset,
4328 | * as 1) are available in the tvbuff and 2) will fit in the buffer, leaving
4329 | * room for a terminating NUL.
4330 | */
4331 | unsigned
4332 | tvb_get_raw_bytes_as_string(tvbuff_t *tvb, const unsigned offset, char *buffer, size_t bufsize)
4333 | {
4334 | unsigned len = 0;
4335 |
4336 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4336, "tvb && tvb->initialized" ))));
4337 |
4338 | /* There must be room for the string and the terminating NUL. */
4339 | DISSECTOR_ASSERT(bufsize > 0)((void) ((bufsize > 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4339, "bufsize > 0" ))));
4340 |
4341 | /* bufsize is size_t, but tvbuffers only have up to unsigned bytes */
4342 | DISSECTOR_ASSERT(bufsize - 1 < UINT_MAX)((void) ((bufsize - 1 < (2147483647 *2U +1U)) ? (void)0 : ( proto_report_dissector_bug("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c" , 4342, "bufsize - 1 < (2147483647 *2U +1U)"))));
4343 |
4344 | len = _tvb_captured_length_remaining(tvb, offset);
4345 | if (len == 0) {
4346 | buffer[0] = '\0';
4347 | return 0;
4348 | }
4349 | if (len > (bufsize - 1))
4350 | len = (unsigned)(bufsize - 1);
4351 |
4352 | /* Copy the string to buffer */
4353 | tvb_memcpy(tvb, buffer, offset, len);
4354 | buffer[len] = '\0';
4355 | return len;
4356 | }
4357 |
4358 | bool_Bool
4359 | tvb_ascii_isprint(tvbuff_t *tvb, const int offset, const int length)
4360 | {
4361 | const uint8_t* buf = tvb_get_ptr(tvb, offset, length);
4362 | unsigned abs_offset, abs_length = length;
4363 |
4364 | if (length == -1) {
4365 | /* tvb_get_ptr has already checked for exceptions. */
4366 | compute_offset_and_remaining(tvb, offset, &abs_offset, &abs_length);
4367 | }
4368 | for (unsigned i = 0; i < abs_length; i++, buf++)
4369 | if (!g_ascii_isprint(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_PRINT) != 0))
4370 | return false0;
4371 |
4372 | return true1;
4373 | }
4374 |
4375 | bool_Bool
4376 | tvb_utf_8_isprint(tvbuff_t *tvb, const int offset, const int length)
4377 | {
4378 | const uint8_t* buf = tvb_get_ptr(tvb, offset, length);
4379 | unsigned abs_offset, abs_length = length;
4380 |
4381 | if (length == -1) {
4382 | /* tvb_get_ptr has already checked for exceptions. */
4383 | compute_offset_and_remaining(tvb, offset, &abs_offset, &abs_length);
4384 | }
4385 |
4386 | return isprint_utf8_string((const char*)buf, abs_length);
4387 | }
4388 |
4389 | bool_Bool
4390 | tvb_ascii_isdigit(tvbuff_t *tvb, const int offset, const int length)
4391 | {
4392 | const uint8_t* buf = tvb_get_ptr(tvb, offset, length);
4393 | unsigned abs_offset, abs_length = length;
4394 |
4395 | if (length == -1) {
4396 | /* tvb_get_ptr has already checked for exceptions. */
4397 | compute_offset_and_remaining(tvb, offset, &abs_offset, &abs_length);
4398 | }
4399 | for (unsigned i = 0; i < abs_length; i++, buf++)
4400 | if (!g_ascii_isdigit(*buf)((g_ascii_table[(guchar) (*buf)] & G_ASCII_DIGIT) != 0))
4401 | return false0;
4402 |
4403 | return true1;
4404 | }
4405 |
4406 | static ws_mempbrk_pattern pbrk_crlf;
4407 | /*
4408 | * Given a tvbuff, an offset into the tvbuff, and a length that starts
4409 | * at that offset (which may be -1 for "all the way to the end of the
4410 | * tvbuff"), find the end of the (putative) line that starts at the
4411 | * specified offset in the tvbuff, going no further than the specified
4412 | * length.
4413 | *
4414 | * Return the length of the line (not counting the line terminator at
4415 | * the end), or, if we don't find a line terminator:
4416 | *
4417 | * if "desegment" is true, return -1;
4418 | *
4419 | * if "desegment" is false, return the amount of data remaining in
4420 | * the buffer.
4421 | *
4422 | * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4423 | * character past the line terminator, or past the end of the buffer if
4424 | * we don't find a line terminator. (It's not set if we return -1.)
4425 | */
4426 | int
4427 | tvb_find_line_end(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset, const bool_Bool desegment)
4428 | {
4429 | int eob_offset;
4430 | int eol_offset;
4431 | int linelen;
4432 | unsigned char found_needle = 0;
4433 | static bool_Bool compiled = false0;
4434 |
4435 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4435, "tvb && tvb->initialized" ))));
4436 |
4437 | if (len == -1) {
4438 | len = _tvb_captured_length_remaining(tvb, offset);
4439 | /* if offset is past the end of the tvbuff, len is now 0 */
4440 | }
4441 |
4442 | eob_offset = offset + len;
4443 |
4444 | if (!compiled) {
4445 | ws_mempbrk_compile(&pbrk_crlf, "\r\n");
4446 | compiled = true1;
4447 | }
4448 |
4449 | /*
4450 | * Look either for a CR or an LF.
4451 | */
4452 | eol_offset = tvb_ws_mempbrk_pattern_uint8(tvb, offset, len, &pbrk_crlf, &found_needle);
4453 | if (eol_offset == -1) {
4454 | /*
4455 | * No CR or LF - line is presumably continued in next packet.
4456 | */
4457 | if (desegment) {
4458 | /*
4459 | * Tell our caller we saw no EOL, so they can
4460 | * try to desegment and get the entire line
4461 | * into one tvbuff.
4462 | */
4463 | return -1;
4464 | } else {
4465 | /*
4466 | * Pretend the line runs to the end of the tvbuff.
4467 | */
4468 | linelen = eob_offset - offset;
4469 | if (next_offset)
4470 | *next_offset = eob_offset;
4471 | }
4472 | } else {
4473 | /*
4474 | * Find the number of bytes between the starting offset
4475 | * and the CR or LF.
4476 | */
4477 | linelen = eol_offset - offset;
4478 |
4479 | /*
4480 | * Is it a CR?
4481 | */
4482 | if (found_needle == '\r') {
4483 | /*
4484 | * Yes - is it followed by an LF?
4485 | */
4486 | if (eol_offset + 1 >= eob_offset) {
4487 | /*
4488 | * Dunno - the next byte isn't in this
4489 | * tvbuff.
4490 | */
4491 | if (desegment) {
4492 | /*
4493 | * We'll return -1, although that
4494 | * runs the risk that if the line
4495 | * really *is* terminated with a CR,
4496 | * we won't properly dissect this
4497 | * tvbuff.
4498 | *
4499 | * It's probably more likely that
4500 | * the line ends with CR-LF than
4501 | * that it ends with CR by itself.
4502 | */
4503 | return -1;
4504 | }
4505 | } else {
4506 | /*
4507 | * Well, we can at least look at the next
4508 | * byte.
4509 | */
4510 | if (tvb_get_uint8(tvb, eol_offset + 1) == '\n') {
4511 | /*
4512 | * It's an LF; skip over the CR.
4513 | */
4514 | eol_offset++;
4515 | }
4516 | }
4517 | }
4518 |
4519 | /*
4520 | * Return the offset of the character after the last
4521 | * character in the line, skipping over the last character
4522 | * in the line terminator.
4523 | */
4524 | if (next_offset)
4525 | *next_offset = eol_offset + 1;
4526 | }
4527 | return linelen;
4528 | }
4529 |
4530 | static ws_mempbrk_pattern pbrk_crlf_dquote;
4531 | /*
4532 | * Given a tvbuff, an offset into the tvbuff, and a length that starts
4533 | * at that offset (which may be -1 for "all the way to the end of the
4534 | * tvbuff"), find the end of the (putative) line that starts at the
4535 | * specified offset in the tvbuff, going no further than the specified
4536 | * length.
4537 | *
4538 | * However, treat quoted strings inside the buffer specially - don't
4539 | * treat newlines in quoted strings as line terminators.
4540 | *
4541 | * Return the length of the line (not counting the line terminator at
4542 | * the end), or the amount of data remaining in the buffer if we don't
4543 | * find a line terminator.
4544 | *
4545 | * If "next_offset" is not NULL, set "*next_offset" to the offset of the
4546 | * character past the line terminator, or past the end of the buffer if
4547 | * we don't find a line terminator.
4548 | */
4549 | int
4550 | tvb_find_line_end_unquoted(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset)
4551 | {
4552 | int cur_offset, char_offset;
4553 | bool_Bool is_quoted;
4554 | unsigned char c = 0;
4555 | int eob_offset;
4556 | int linelen;
4557 | static bool_Bool compiled = false0;
4558 |
4559 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4559, "tvb && tvb->initialized" ))));
4560 |
4561 | if (len == -1)
4562 | len = _tvb_captured_length_remaining(tvb, offset);
4563 |
4564 | if (!compiled) {
4565 | ws_mempbrk_compile(&pbrk_crlf_dquote, "\r\n\"");
4566 | compiled = true1;
4567 | }
4568 |
4569 | eob_offset = offset + len;
4570 |
4571 | cur_offset = offset;
4572 | is_quoted = false0;
4573 | for (;;) {
4574 | /*
4575 | * Is this part of the string quoted?
4576 | */
4577 | if (is_quoted) {
4578 | /*
4579 | * Yes - look only for the terminating quote.
4580 | */
4581 | char_offset = tvb_find_uint8(tvb, cur_offset, len,
4582 | '"');
4583 | } else {
4584 | /*
4585 | * Look either for a CR, an LF, or a '"'.
4586 | */
4587 | char_offset = tvb_ws_mempbrk_pattern_uint8(tvb, cur_offset, len, &pbrk_crlf_dquote, &c);
4588 | }
4589 | if (char_offset == -1) {
4590 | /*
4591 | * Not found - line is presumably continued in
4592 | * next packet.
4593 | * We pretend the line runs to the end of the tvbuff.
4594 | */
4595 | linelen = eob_offset - offset;
4596 | if (next_offset)
4597 | *next_offset = eob_offset;
4598 | break;
4599 | }
4600 |
4601 | if (is_quoted) {
4602 | /*
4603 | * We're processing a quoted string.
4604 | * We only looked for ", so we know it's a ";
4605 | * as we're processing a quoted string, it's a
4606 | * closing quote.
4607 | */
4608 | is_quoted = false0;
4609 | } else {
4610 | /*
4611 | * OK, what is it?
4612 | */
4613 | if (c == '"') {
4614 | /*
4615 | * Un-quoted "; it begins a quoted
4616 | * string.
4617 | */
4618 | is_quoted = true1;
4619 | } else {
4620 | /*
4621 | * It's a CR or LF; we've found a line
4622 | * terminator.
4623 | *
4624 | * Find the number of bytes between the
4625 | * starting offset and the CR or LF.
4626 | */
4627 | linelen = char_offset - offset;
4628 |
4629 | /*
4630 | * Is it a CR?
4631 | */
4632 | if (c == '\r') {
4633 | /*
4634 | * Yes; is it followed by an LF?
4635 | */
4636 | if (char_offset + 1 < eob_offset &&
4637 | tvb_get_uint8(tvb, char_offset + 1)
4638 | == '\n') {
4639 | /*
4640 | * Yes; skip over the CR.
4641 | */
4642 | char_offset++;
4643 | }
4644 | }
4645 |
4646 | /*
4647 | * Return the offset of the character after
4648 | * the last character in the line, skipping
4649 | * over the last character in the line
4650 | * terminator, and quit.
4651 | */
4652 | if (next_offset)
4653 | *next_offset = char_offset + 1;
4654 | break;
4655 | }
4656 | }
4657 |
4658 | /*
4659 | * Step past the character we found.
4660 | */
4661 | cur_offset = char_offset + 1;
4662 | if (cur_offset >= eob_offset) {
4663 | /*
4664 | * The character we found was the last character
4665 | * in the tvbuff - line is presumably continued in
4666 | * next packet.
4667 | * We pretend the line runs to the end of the tvbuff.
4668 | */
4669 | linelen = eob_offset - offset;
4670 | if (next_offset)
4671 | *next_offset = eob_offset;
4672 | break;
4673 | }
4674 | }
4675 | return linelen;
4676 | }
4677 |
4678 | /*
4679 | * Copied from the mgcp dissector. (This function should be moved to /epan )
4680 | * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
4681 | * character following offset or offset + maxlength -1 whichever
4682 | * is smaller.
4683 | *
4684 | * Parameters:
4685 | * tvb - The tvbuff in which we are skipping whitespace.
4686 | * offset - The offset in tvb from which we begin trying to skip whitespace.
4687 | * maxlength - The maximum distance from offset that we may try to skip
4688 | * whitespace.
4689 | *
4690 | * Returns: The position in tvb of the first non-whitespace
4691 | * character following offset or offset + maxlength -1 whichever
4692 | * is smaller.
4693 | */
4694 | unsigned
4695 | tvb_skip_wsp(tvbuff_t *tvb, const unsigned offset, const unsigned maxlength)
4696 | {
4697 | unsigned counter;
4698 | unsigned end, tvb_len;
4699 | uint8_t tempchar;
4700 |
4701 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4701, "tvb && tvb->initialized" ))));
4702 |
4703 | /* Get the length remaining */
4704 | /*tvb_len = tvb_captured_length(tvb);*/
4705 | tvb_len = tvb->length;
4706 |
4707 | if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
4708 | end = tvb_len;
4709 | }
4710 |
4711 | /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
4712 | /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
4713 | * slightly faster but also tests for vertical tab and form feed. */
4714 | for (counter = offset;
4715 | counter < end &&
4716 | ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
4717 | tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
4718 | counter++);
4719 |
4720 | return counter;
4721 | }
4722 |
4723 | unsigned
4724 | tvb_skip_wsp_return(tvbuff_t *tvb, const unsigned offset)
4725 | {
4726 | unsigned counter;
4727 | uint8_t tempchar;
4728 |
4729 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4729, "tvb && tvb->initialized" ))));
4730 |
4731 | /* XXX - DISSECTOR_ASSERT(offset > 0) and then subtract 1 from offset?
4732 | * The way this is used the caller almost always wants to subtract one
4733 | * from the offset of a non WSP separator, and they might forget to do
4734 | * so and then this function return the offset past the separator. */
4735 |
4736 | /* XXX - The MEGACO dissector uses g_ascii_isspace(), which might be
4737 | * slightly faster but also tests for vertical tab and form feed. */
4738 | for (counter = offset; counter > 0 &&
4739 | ((tempchar = tvb_get_uint8(tvb,counter)) == ' ' ||
4740 | tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
4741 | counter++;
4742 |
4743 | return counter;
4744 | }
4745 |
4746 | unsigned
4747 | tvb_skip_uint8(tvbuff_t *tvb, unsigned offset, const unsigned maxlength, const uint8_t ch)
4748 | {
4749 | unsigned end, tvb_len;
4750 |
4751 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4751, "tvb && tvb->initialized" ))));
4752 |
4753 | /* Get the length remaining */
4754 | /*tvb_len = tvb_captured_length(tvb);*/
4755 | tvb_len = tvb->length;
4756 |
4757 | if (ckd_add(&end, offset, maxlength)__builtin_add_overflow((offset), (maxlength), (&end)) || end > tvb_len) {
4758 | end = tvb_len;
4759 | }
4760 |
4761 | while (offset < end) {
4762 | uint8_t tempch = tvb_get_uint8(tvb, offset);
4763 |
4764 | if (tempch != ch)
4765 | break;
4766 | offset++;
4767 | }
4768 |
4769 | return offset;
4770 | }
4771 |
4772 | static ws_mempbrk_pattern pbrk_whitespace;
4773 |
4774 | int tvb_get_token_len(tvbuff_t *tvb, const unsigned offset, int len, int *next_offset, const bool_Bool desegment)
4775 | {
4776 | int eob_offset;
4777 | int eot_offset;
4778 | int tokenlen;
4779 | unsigned char found_needle = 0;
4780 | static bool_Bool compiled = false0;
4781 |
4782 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4782, "tvb && tvb->initialized" ))));
4783 |
4784 | if (len == -1) {
4785 | len = _tvb_captured_length_remaining(tvb, offset);
4786 | /* if offset is past the end of the tvbuff, len is now 0 */
4787 | }
4788 |
4789 | eob_offset = offset + len;
4790 |
4791 | if (!compiled) {
4792 | ws_mempbrk_compile(&pbrk_whitespace, " \r\n");
4793 | compiled = true1;
4794 | }
4795 |
4796 | /*
4797 | * Look either for a space, CR, or LF.
4798 | */
4799 | eot_offset = tvb_ws_mempbrk_pattern_uint8(tvb, offset, len, &pbrk_whitespace, &found_needle);
4800 | if (eot_offset == -1) {
4801 | /*
4802 | * No space, CR or LF - token is presumably continued in next packet.
4803 | */
4804 | if (desegment) {
4805 | /*
4806 | * Tell our caller we saw no whitespace, so they can
4807 | * try to desegment and get the entire line
4808 | * into one tvbuff.
4809 | */
4810 | return -1;
4811 | }
4812 | else {
4813 | /*
4814 | * Pretend the token runs to the end of the tvbuff.
4815 | */
4816 | tokenlen = eob_offset - offset;
4817 | if (next_offset)
4818 | *next_offset = eob_offset;
4819 | }
4820 | }
4821 | else {
4822 | /*
4823 | * Find the number of bytes between the starting offset
4824 | * and the space, CR or LF.
4825 | */
4826 | tokenlen = eot_offset - offset;
4827 |
4828 | /*
4829 | * Return the offset of the character after the last
4830 | * character in the line, skipping over the last character
4831 | * in the line terminator.
4832 | */
4833 | if (next_offset)
4834 | *next_offset = eot_offset + 1;
4835 | }
4836 | return tokenlen;
4837 | }
4838 |
4839 | /*
4840 | * Format a bunch of data from a tvbuff as bytes, returning a pointer
4841 | * to the string with the formatted data, with "punct" as a byte
4842 | * separator.
4843 | */
4844 | char *
4845 | tvb_bytes_to_str_punct(wmem_allocator_t *scope, tvbuff_t *tvb, const unsigned offset, const unsigned len, const char punct)
4846 | {
4847 | return bytes_to_str_punct(scope, ensure_contiguous_unsigned(tvb, offset, len), len, punct)bytes_to_str_punct_maxlen(scope, ensure_contiguous_unsigned(tvb , offset, len), len, punct, 24);
4848 | }
4849 |
4850 | /*
4851 | * Given a wmem scope, a tvbuff, an offset, a length, an input digit
4852 | * set, and a boolean indicator, fetch BCD-encoded digits from a
4853 | * tvbuff starting from either the low or high half byte of the
4854 | * first byte depending on the boolean indicator (true means "start
4855 | * with the high half byte, ignoring the low half byte", and false
4856 | * means "start with the low half byte and proceed to the high half
4857 | * byte), formating the digits into characters according to the
4858 | * input digit set, and return a pointer to a UTF-8 string, allocated
4859 | * using the wmem scope. A nibble of 0xf is considered a 'filler'
4860 | * and will end the conversion. Similarly if odd is set the last
4861 | * high nibble will be omitted. (Note that if both skip_first and
4862 | * odd are true, then both the first and last semi-octet are skipped,
4863 | * i.e. an even number of nibbles are considered.)
4864 | */
4865 | char *
4866 | 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)
4867 | {
4868 | const uint8_t *ptr;
4869 | int i = 0;
4870 | char *digit_str;
4871 | uint8_t octet, nibble;
4872 |
4873 | DISSECTOR_ASSERT(tvb && tvb->initialized)((void) ((tvb && tvb->initialized) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4873, "tvb && tvb->initialized" ))));
4874 |
4875 | if (len == -1) {
4876 | /*
4877 | * Run to the end of the captured data.
4878 | *
4879 | * XXX - captured, or total?
4880 | */
4881 | /*length = tvb_captured_length(tvb);*/
4882 | len = tvb->length;
4883 | if (len < offset) {
4884 | return (char *)"";
4885 | }
4886 | len -= offset;
4887 | }
4888 |
4889 | ptr = ensure_contiguous(tvb, offset, len);
4890 |
4891 | /*
4892 | * XXX - map illegal digits (digits that map to 0) to REPLACEMENT
4893 | * CHARACTER, and have all the tables in epan/tvbuff.c use 0 rather
4894 | * than '?'?
4895 | */
4896 | digit_str = (char *)wmem_alloc(scope, len*2 + 1);
4897 |
4898 | while (len > 0) {
4899 | octet = *ptr;
4900 | if (!skip_first) {
4901 | if (bigendian) {
4902 | nibble = (octet >> 4) & 0x0f;
4903 | } else {
4904 | nibble = octet & 0x0f;
4905 | }
4906 | if (nibble == 0x0f) {
4907 | /*
4908 | * Stop digit.
4909 | */
4910 | break;
4911 | }
4912 | digit_str[i] = dgt->out[nibble];
4913 | i++;
4914 | }
4915 | skip_first = false0;
4916 |
4917 | /*
4918 | * unpack second value in byte
4919 | */
4920 | if (bigendian) {
4921 | nibble = octet & 0x0f;
4922 | } else {
4923 | nibble = octet >> 4;
4924 | }
4925 |
4926 | if (nibble == 0x0f) {
4927 | /*
4928 | * This is the stop digit or a filler digit. Ignore
4929 | * it.
4930 | */
4931 | break;
4932 | }
4933 | if ((len == 1) && (odd == true1 )){
4934 | /* Last octet, skip last high nibble in case of odd number of digits */
4935 | break;
4936 | }
4937 | digit_str[i] = dgt->out[nibble];
4938 | i++;
4939 |
4940 | ptr++;
4941 | len--;
4942 | }
4943 | digit_str[i] = '\0';
4944 | return digit_str;
4945 | }
4946 |
4947 | /* XXXX Fix me - needs odd indicator added */
4948 | const char *
4949 | 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)
4950 | {
4951 | if (!dgt)
4952 | dgt = &Dgt0_9_bcd;
4953 |
4954 | return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, false0);
4955 | }
4956 |
4957 | const char *
4958 | 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)
4959 | {
4960 | if (!dgt)
4961 | dgt = &Dgt0_9_bcd;
4962 |
4963 | return tvb_get_bcd_string(scope, tvb, offset, len, dgt, skip_first, false0, true1);
4964 | }
4965 |
4966 | /*
4967 | * Format a bunch of data from a tvbuff as bytes, returning a pointer
4968 | * to the string with the formatted data.
4969 | */
4970 | char *tvb_bytes_to_str(wmem_allocator_t *allocator, tvbuff_t *tvb,
4971 | const int offset, const int len)
4972 | {
4973 | DISSECTOR_ASSERT(len >= 0)((void) ((len >= 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/tvbuff.c", 4973, "len >= 0" ))));
4974 | return bytes_to_str(allocator, ensure_contiguous(tvb, offset, len), len)bytes_to_str_maxlen(allocator, ensure_contiguous(tvb, offset, len), len, 36);
4975 | }
4976 |
4977 | /* Find a needle tvbuff within a haystack tvbuff. */
4978 | int
4979 | tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const int haystack_offset)
4980 | {
4981 | unsigned haystack_abs_offset = 0, haystack_abs_length = 0;
4982 | const uint8_t *haystack_data;
4983 | const uint8_t *needle_data;
4984 | const unsigned needle_len = needle_tvb->length;
4985 | const uint8_t *location;
4986 |
4987 | 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", 4987, "haystack_tvb && haystack_tvb->initialized" ))));
4988 |
4989 | if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
4990 | return -1;
4991 | }
4992 |
4993 | /* Get pointers to the tvbuffs' data. */
4994 | haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
4995 | needle_data = ensure_contiguous(needle_tvb, 0, -1);
4996 |
4997 | check_offset_length(haystack_tvb, haystack_offset, -1,
4998 | &haystack_abs_offset, &haystack_abs_length);
4999 |
5000 | location = ws_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
5001 | needle_data, needle_len);
5002 |
5003 | if (location) {
5004 | return (int) (location - haystack_data);
5005 | }
5006 |
5007 | return -1;
5008 | }
5009 |
5010 | int
5011 | tvb_raw_offset(tvbuff_t *tvb)
5012 | {
5013 | return ((tvb->raw_offset==-1) ? (tvb->raw_offset = tvb_offset_from_real_beginning(tvb)) : tvb->raw_offset);
5014 | }
5015 |
5016 | void
5017 | tvb_set_fragment(tvbuff_t *tvb)
5018 | {
5019 | tvb->flags |= TVBUFF_FRAGMENT0x00000001;
5020 | }
5021 |
5022 | struct tvbuff *
5023 | tvb_get_ds_tvb(tvbuff_t *tvb)
5024 | {
5025 | return(tvb->ds_tvb);
5026 | }
5027 |
5028 | unsigned
5029 | tvb_get_varint(tvbuff_t *tvb, unsigned offset, unsigned maxlen, uint64_t *value, const unsigned encoding)
5030 | {
5031 | *value = 0;
5032 |
5033 | switch (encoding & ENC_VARINT_MASK(0x00000002|0x00000004|0x00000008|0x00000010)) {
5034 | case ENC_VARINT_PROTOBUF0x00000002:
5035 | {
5036 | unsigned i;
5037 | uint64_t b; /* current byte */
5038 |
5039 | for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5040 | b = tvb_get_uint8(tvb, offset++);
5041 | *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5042 |
5043 | if (b < 0x80) {
5044 | /* end successfully because of last byte's msb(most significant bit) is zero */
5045 | return i + 1;
5046 | }
5047 | }
5048 | break;
5049 | }
5050 |
5051 | case ENC_VARINT_ZIGZAG0x00000008:
5052 | {
5053 | unsigned i;
5054 | uint64_t b; /* current byte */
5055 |
5056 | for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5057 | b = tvb_get_uint8(tvb, offset++);
5058 | *value |= ((b & 0x7F) << (i * 7)); /* add lower 7 bits to val */
5059 |
5060 | if (b < 0x80) {
5061 | /* end successfully because of last byte's msb(most significant bit) is zero */
5062 | *value = (*value >> 1) ^ ((*value & 1) ? -1 : 0);
5063 | return i + 1;
5064 | }
5065 | }
5066 | break;
5067 | }
5068 |
5069 | case ENC_VARINT_SDNV0x00000010:
5070 | {
5071 | /* Decodes similar to protobuf but in MSByte order */
5072 | unsigned i;
5073 | uint64_t b; /* current byte */
5074 |
5075 | for (i = 0; ((i < FT_VARINT_MAX_LEN10) && (i < maxlen)); ++i) {
5076 | b = tvb_get_uint8(tvb, offset++);
5077 | if ((i == 9) && (*value >= UINT64_C(1)1UL<<(64-7))) {
5078 | // guaranteed overflow, not valid SDNV
5079 | return 0;
5080 | }
5081 | *value <<= 7;
5082 | *value |= (b & 0x7F); /* add lower 7 bits to val */
5083 |
5084 | if (b < 0x80) {
5085 | /* end successfully because of last byte's msb(most significant bit) is zero */
5086 | return i + 1;
5087 | }
5088 | }
5089 | break;
5090 | }
5091 |
5092 | case ENC_VARINT_QUIC0x00000004:
5093 | {
5094 | /* calculate variable length */
5095 | *value = tvb_get_uint8(tvb, offset);
5096 | switch((*value) >> 6) {
5097 | case 0: /* 0b00 => 1 byte length (6 bits Usable) */
5098 | (*value) &= 0x3F;
5099 | return 1;
5100 | case 1: /* 0b01 => 2 bytes length (14 bits Usable) */
5101 | *value = tvb_get_ntohs(tvb, offset) & 0x3FFF;
5102 | return 2;
5103 | case 2: /* 0b10 => 4 bytes length (30 bits Usable) */
5104 | *value = tvb_get_ntohl(tvb, offset) & 0x3FFFFFFF;
5105 | return 4;
5106 | case 3: /* 0b11 => 8 bytes length (62 bits Usable) */
5107 | *value = tvb_get_ntoh64(tvb, offset) & UINT64_C(0x3FFFFFFFFFFFFFFF)0x3FFFFFFFFFFFFFFFUL;
5108 | return 8;
5109 | default: /* No Possible */
5110 | ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/tvbuff.c", 5110, __func__, "assertion \"not reached\" failed");
5111 | break;
5112 | }
5113 | break;
5114 | }
5115 |
5116 | default:
5117 | DISSECTOR_ASSERT_NOT_REACHED()(proto_report_dissector_bug("%s:%u: failed assertion \"DISSECTOR_ASSERT_NOT_REACHED\"" , "epan/tvbuff.c", 5117));
5118 | }
5119 |
5120 | return 0; /* 10 bytes scanned, but no bytes' msb is zero */
5121 | }
5122 |
5123 | /*
5124 | * Editor modelines - https://www.wireshark.org/tools/modelines.html
5125 | *
5126 | * Local variables:
5127 | * c-basic-offset: 8
5128 | * tab-width: 8
5129 | * indent-tabs-mode: t
5130 | * End:
5131 | *
5132 | * vi: set shiftwidth=8 tabstop=8 noexpandtab:
5133 | * :indentSize=8:tabSize=8:noTabs=false:
5134 | */