Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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