### Bug Summary

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

### Annotated Source Code

Press '?' to see keyboard shortcuts

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

Keyboard shortcuts:

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

Close
Show only relevant lines Show control flow arrows

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