### Bug Summary

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

### Annotated Source Code

Press '?' to see keyboard shortcuts

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

Keyboard shortcuts:

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

Close
Show only relevant lines Show control flow arrows

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