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