### Bug Summary

File: | builds/wireshark/wireshark/epan/tvbuff.c
Warning: | line 520, 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/lua5.4 -isystem /usr/include/libxml2 -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D epan_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu11 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /builds/wireshark/wireshark/sbout/2026-04-26-100332-3641-1 -x c /builds/wireshark/wireshark/epan/tvbuff.c

Keyboard shortcuts:

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

Close
Show only relevant lines Show control flow arrows

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