PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/epan/tvbuff.c

https://github.com/labx-technologies-llc/wireshark
C | 3263 lines | 1938 code | 479 blank | 846 comment | 352 complexity | 646822b1306509f30d99c22c7c91629b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /* tvbuff.c
  2. *
  3. * Testy, Virtual(-izable) Buffer of guint8*'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. * $Id$
  13. *
  14. * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
  15. *
  16. * Code to convert IEEE floating point formats to native floating point
  17. * derived from code Copyright (c) Ashok Narayanan, 2000
  18. *
  19. * Wireshark - Network traffic analyzer
  20. * By Gerald Combs <gerald@wireshark.org>
  21. * Copyright 1998 Gerald Combs
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License
  25. * as published by the Free Software Foundation; either version 2
  26. * of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  36. */
  37. #include "config.h"
  38. #include <string.h>
  39. #ifdef HAVE_LIBZ
  40. #include <zlib.h>
  41. #endif
  42. #include "wsutil/pint.h"
  43. #include "tvbuff.h"
  44. #include "tvbuff-int.h"
  45. #include "strutil.h"
  46. #include "emem.h"
  47. #include "charsets.h"
  48. #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
  49. static guint64
  50. _tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint total_no_of_bits);
  51. tvbuff_t *
  52. tvb_new(const struct tvb_ops *ops)
  53. {
  54. tvbuff_t *tvb;
  55. gsize size = ops->tvb_size ? ops->tvb_size : sizeof(*tvb);
  56. g_assert(size >= sizeof(*tvb));
  57. tvb = (tvbuff_t *) g_slice_alloc(size);
  58. tvb->previous = NULL;
  59. tvb->next = NULL;
  60. tvb->ops = ops;
  61. tvb->initialized = FALSE;
  62. tvb->flags = 0;
  63. tvb->length = 0;
  64. tvb->reported_length = 0;
  65. tvb->real_data = NULL;
  66. tvb->raw_offset = -1;
  67. tvb->ds_tvb = NULL;
  68. return tvb;
  69. }
  70. static void
  71. tvb_free_internal(tvbuff_t *tvb)
  72. {
  73. gsize size;
  74. DISSECTOR_ASSERT(tvb);
  75. if (tvb->ops->tvb_free)
  76. tvb->ops->tvb_free(tvb);
  77. size = (tvb->ops->tvb_size) ? tvb->ops->tvb_size : sizeof(*tvb);
  78. g_slice_free1(size, tvb);
  79. }
  80. /* XXX: just call tvb_free_chain();
  81. * Not removed so that existing dissectors using tvb_free() need not be changed.
  82. * I'd argue that existing calls to tvb_free() should have actually beeen
  83. * calls to tvb_free_chain() although the calls were OK as long as no
  84. * subsets, etc had been created on the tvb. */
  85. void
  86. tvb_free(tvbuff_t *tvb)
  87. {
  88. tvb_free_chain(tvb);
  89. }
  90. void
  91. tvb_free_chain(tvbuff_t *tvb)
  92. {
  93. tvbuff_t *next_tvb;
  94. DISSECTOR_ASSERT(tvb);
  95. DISSECTOR_ASSERT_HINT(tvb->previous==NULL, "tvb_free_chain(): tvb must be initial tvb in chain");
  96. while (tvb) {
  97. next_tvb=tvb->next;
  98. DISSECTOR_ASSERT_HINT((next_tvb==NULL) || (tvb==next_tvb->previous), "tvb_free_chain(): corrupt tvb chain ?");
  99. tvb_free_internal(tvb);
  100. tvb = next_tvb;
  101. }
  102. }
  103. tvbuff_t *
  104. tvb_new_chain(tvbuff_t *parent, tvbuff_t *backing)
  105. {
  106. tvbuff_t *tvb = tvb_new_proxy(backing);
  107. tvb_add_to_chain(parent, tvb);
  108. return tvb;
  109. }
  110. void
  111. tvb_add_to_chain(tvbuff_t *parent, tvbuff_t *child)
  112. {
  113. DISSECTOR_ASSERT(parent);
  114. DISSECTOR_ASSERT(child);
  115. DISSECTOR_ASSERT(!child->next);
  116. DISSECTOR_ASSERT(!child->previous);
  117. child->next = parent->next;
  118. child->previous = parent;
  119. if (parent->next)
  120. parent->next->previous = child;
  121. parent->next = child;
  122. }
  123. /*
  124. * Check whether that offset goes more than one byte past the
  125. * end of the buffer.
  126. *
  127. * If not, return 0; otherwise, return exception
  128. */
  129. static inline int
  130. validate_offset(const tvbuff_t *tvb, const guint abs_offset)
  131. {
  132. int exception;
  133. if (G_LIKELY(abs_offset <= tvb->length))
  134. exception = 0;
  135. else if (abs_offset <= tvb->reported_length)
  136. exception = BoundsError;
  137. else {
  138. if (tvb->flags & TVBUFF_FRAGMENT)
  139. exception = FragmentBoundsError;
  140. else
  141. exception = ReportedBoundsError;
  142. }
  143. return exception;
  144. }
  145. static int
  146. compute_offset(const tvbuff_t *tvb, const gint offset, guint *offset_ptr)
  147. {
  148. int exception;
  149. if (offset >= 0) {
  150. /* Positive offset - relative to the beginning of the packet. */
  151. if ((guint) offset > tvb->reported_length) {
  152. if (tvb->flags & TVBUFF_FRAGMENT) {
  153. exception = FragmentBoundsError;
  154. } else {
  155. exception = ReportedBoundsError;
  156. }
  157. return exception;
  158. }
  159. else if ((guint) offset > tvb->length) {
  160. return BoundsError;
  161. }
  162. else {
  163. *offset_ptr = offset;
  164. }
  165. }
  166. else {
  167. /* Negative offset - relative to the end of the packet. */
  168. if ((guint) -offset > tvb->reported_length) {
  169. if (tvb->flags & TVBUFF_FRAGMENT) {
  170. exception = FragmentBoundsError;
  171. } else {
  172. exception = ReportedBoundsError;
  173. }
  174. return exception;
  175. }
  176. else if ((guint) -offset > tvb->length) {
  177. return BoundsError;
  178. }
  179. else {
  180. *offset_ptr = tvb->length + offset;
  181. }
  182. }
  183. return 0;
  184. }
  185. static int
  186. compute_offset_and_remaining(const tvbuff_t *tvb, const gint offset, guint *offset_ptr, guint *rem_len)
  187. {
  188. int exception;
  189. exception = compute_offset(tvb, offset, offset_ptr);
  190. if (!exception)
  191. *rem_len = tvb->length - *offset_ptr;
  192. return exception;
  193. }
  194. /* Computes the absolute offset and length based on a possibly-negative offset
  195. * and a length that is possible -1 (which means "to the end of the data").
  196. * Returns integer indicating whether the offset is in bounds (0) or
  197. * not (exception number). The integer ptrs are modified with the new offset and length.
  198. * No exception is thrown.
  199. *
  200. * XXX - we return success (0), if the offset is positive and right
  201. * after the end of the tvbuff (i.e., equal to the length). We do this
  202. * so that a dissector constructing a subset tvbuff for the next protocol
  203. * will get a zero-length tvbuff, not an exception, if there's no data
  204. * left for the next protocol - we want the next protocol to be the one
  205. * that gets an exception, so the error is reported as an error in that
  206. * protocol rather than the containing protocol. */
  207. static int
  208. check_offset_length_no_exception(const tvbuff_t *tvb,
  209. const gint offset, gint const length_val,
  210. guint *offset_ptr, guint *length_ptr)
  211. {
  212. guint end_offset;
  213. int exception;
  214. DISSECTOR_ASSERT(offset_ptr);
  215. DISSECTOR_ASSERT(length_ptr);
  216. /* Compute the offset */
  217. exception = compute_offset(tvb, offset, offset_ptr);
  218. if (exception)
  219. return exception;
  220. if (length_val < -1) {
  221. /* XXX - ReportedBoundsError? */
  222. return BoundsError;
  223. }
  224. /* Compute the length */
  225. if (length_val == -1)
  226. *length_ptr = tvb->length - *offset_ptr;
  227. else
  228. *length_ptr = length_val;
  229. /*
  230. * Compute the offset of the first byte past the length.
  231. */
  232. end_offset = *offset_ptr + *length_ptr;
  233. /*
  234. * Check for an overflow
  235. */
  236. if (end_offset < *offset_ptr)
  237. exception = BoundsError;
  238. else
  239. exception = validate_offset(tvb, end_offset);
  240. return exception;
  241. }
  242. /* Checks (+/-) offset and length and throws an exception if
  243. * either is out of bounds. Sets integer ptrs to the new offset
  244. * and length. */
  245. static void
  246. check_offset_length(const tvbuff_t *tvb,
  247. const gint offset, gint const length_val,
  248. guint *offset_ptr, guint *length_ptr)
  249. {
  250. int exception;
  251. exception = check_offset_length_no_exception(tvb, offset, length_val, offset_ptr, length_ptr);
  252. if (exception)
  253. THROW(exception);
  254. }
  255. void
  256. tvb_check_offset_length(const tvbuff_t *tvb,
  257. const gint offset, gint const length_val,
  258. guint *offset_ptr, guint *length_ptr)
  259. {
  260. check_offset_length(tvb, offset, length_val, offset_ptr, length_ptr);
  261. }
  262. static const unsigned char left_aligned_bitmask[] = {
  263. 0xff,
  264. 0x80,
  265. 0xc0,
  266. 0xe0,
  267. 0xf0,
  268. 0xf8,
  269. 0xfc,
  270. 0xfe
  271. };
  272. tvbuff_t *
  273. tvb_new_octet_aligned(tvbuff_t *tvb, guint32 bit_offset, gint32 no_of_bits)
  274. {
  275. tvbuff_t *sub_tvb = NULL;
  276. guint32 byte_offset;
  277. gint32 datalen, i;
  278. guint8 left, right, remaining_bits, *buf;
  279. const guint8 *data;
  280. byte_offset = bit_offset >> 3;
  281. left = bit_offset % 8; /* for left-shifting */
  282. right = 8 - left; /* for right-shifting */
  283. if (no_of_bits == -1) {
  284. datalen = tvb_length_remaining(tvb, byte_offset);
  285. remaining_bits = 0;
  286. } else {
  287. datalen = no_of_bits >> 3;
  288. remaining_bits = no_of_bits % 8;
  289. if (remaining_bits) {
  290. datalen++;
  291. }
  292. }
  293. /* already aligned -> shortcut */
  294. if ((left == 0) && (remaining_bits == 0)) {
  295. return tvb_new_subset(tvb, byte_offset, datalen, -1);
  296. }
  297. DISSECTOR_ASSERT(datalen>0);
  298. /* if at least one trailing byte is available, we must use the content
  299. * of that byte for the last shift (i.e. tvb_get_ptr() must use datalen + 1
  300. * if non extra byte is available, the last shifted byte requires
  301. * special treatment
  302. */
  303. if (tvb_length_remaining(tvb, byte_offset) > datalen) {
  304. data = tvb_get_ptr(tvb, byte_offset, datalen + 1);
  305. /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
  306. buf = (guint8 *)g_malloc(datalen);
  307. /* shift tvb data bit_offset bits to the left */
  308. for (i = 0; i < datalen; i++)
  309. buf[i] = (data[i] << left) | (data[i+1] >> right);
  310. } else {
  311. data = tvb_get_ptr(tvb, byte_offset, datalen);
  312. /* Do this allocation AFTER tvb_get_ptr() (which could throw an exception) */
  313. buf = (guint8 *)g_malloc(datalen);
  314. /* shift tvb data bit_offset bits to the left */
  315. for (i = 0; i < (datalen-1); i++)
  316. buf[i] = (data[i] << left) | (data[i+1] >> right);
  317. buf[datalen-1] = data[datalen-1] << left; /* set last octet */
  318. }
  319. buf[datalen-1] &= left_aligned_bitmask[remaining_bits];
  320. sub_tvb = tvb_new_child_real_data(tvb, buf, datalen, datalen);
  321. tvb_set_free_cb(sub_tvb, g_free);
  322. return sub_tvb;
  323. }
  324. static tvbuff_t *
  325. tvb_generic_clone_offset_len(tvbuff_t *tvb, guint offset, guint len)
  326. {
  327. tvbuff_t *cloned_tvb;
  328. guint8 *data = (guint8 *) g_malloc(len);
  329. tvb_memcpy(tvb, data, offset, len);
  330. cloned_tvb = tvb_new_real_data(data, len, len);
  331. tvb_set_free_cb(cloned_tvb, g_free);
  332. return cloned_tvb;
  333. }
  334. tvbuff_t *
  335. tvb_clone_offset_len(tvbuff_t *tvb, guint offset, guint len)
  336. {
  337. if (tvb->ops->tvb_clone) {
  338. tvbuff_t *cloned_tvb;
  339. cloned_tvb = tvb->ops->tvb_clone(tvb, offset, len);
  340. if (cloned_tvb)
  341. return cloned_tvb;
  342. }
  343. return tvb_generic_clone_offset_len(tvb, offset, len);
  344. }
  345. tvbuff_t *
  346. tvb_clone(tvbuff_t *tvb)
  347. {
  348. return tvb_clone_offset_len(tvb, 0, tvb->length);
  349. }
  350. guint
  351. tvb_length(const tvbuff_t *tvb)
  352. {
  353. DISSECTOR_ASSERT(tvb && tvb->initialized);
  354. return tvb->length;
  355. }
  356. gint
  357. tvb_length_remaining(const tvbuff_t *tvb, const gint offset)
  358. {
  359. guint abs_offset, rem_length;
  360. int exception;
  361. DISSECTOR_ASSERT(tvb && tvb->initialized);
  362. exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
  363. if (exception)
  364. return -1;
  365. return rem_length;
  366. }
  367. guint
  368. tvb_ensure_length_remaining(const tvbuff_t *tvb, const gint offset)
  369. {
  370. guint abs_offset, rem_length;
  371. int exception;
  372. DISSECTOR_ASSERT(tvb && tvb->initialized);
  373. exception = compute_offset_and_remaining(tvb, offset, &abs_offset, &rem_length);
  374. if (exception)
  375. THROW(exception);
  376. if (rem_length == 0) {
  377. /*
  378. * This routine ensures there's at least one byte available.
  379. * There aren't any bytes available, so throw the appropriate
  380. * exception.
  381. */
  382. if (abs_offset >= tvb->reported_length) {
  383. if (tvb->flags & TVBUFF_FRAGMENT) {
  384. THROW(FragmentBoundsError);
  385. } else {
  386. THROW(ReportedBoundsError);
  387. }
  388. } else
  389. THROW(BoundsError);
  390. }
  391. return rem_length;
  392. }
  393. /* Validates that 'length' bytes are available starting from
  394. * offset (pos/neg). Does not throw an exception. */
  395. gboolean
  396. tvb_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
  397. {
  398. guint abs_offset, abs_length;
  399. int exception;
  400. DISSECTOR_ASSERT(tvb && tvb->initialized);
  401. exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
  402. if (exception)
  403. return FALSE;
  404. return TRUE;
  405. }
  406. /* Validates that 'length' bytes are available starting from
  407. * offset (pos/neg). Throws an exception if they aren't. */
  408. void
  409. tvb_ensure_bytes_exist(const tvbuff_t *tvb, const gint offset, const gint length)
  410. {
  411. guint abs_offset, abs_length;
  412. DISSECTOR_ASSERT(tvb && tvb->initialized);
  413. /*
  414. * -1 doesn't mean "until end of buffer", as that's pointless
  415. * for this routine. We must treat it as a Really Large Positive
  416. * Number, so that we throw an exception; we throw
  417. * ReportedBoundsError, as if it were past even the end of a
  418. * reassembled packet, and past the end of even the data we
  419. * didn't capture.
  420. *
  421. * We do the same with other negative lengths.
  422. */
  423. if (length < 0) {
  424. THROW(ReportedBoundsError);
  425. }
  426. check_offset_length(tvb, offset, length, &abs_offset, &abs_length);
  427. }
  428. gboolean
  429. tvb_offset_exists(const tvbuff_t *tvb, const gint offset)
  430. {
  431. guint abs_offset;
  432. int exception;
  433. DISSECTOR_ASSERT(tvb && tvb->initialized);
  434. exception = compute_offset(tvb, offset, &abs_offset);
  435. if (exception)
  436. return FALSE;
  437. if (abs_offset < tvb->length) {
  438. return TRUE;
  439. }
  440. else {
  441. return FALSE;
  442. }
  443. }
  444. guint
  445. tvb_reported_length(const tvbuff_t *tvb)
  446. {
  447. DISSECTOR_ASSERT(tvb && tvb->initialized);
  448. return tvb->reported_length;
  449. }
  450. gint
  451. tvb_reported_length_remaining(const tvbuff_t *tvb, const gint offset)
  452. {
  453. guint abs_offset;
  454. int exception;
  455. DISSECTOR_ASSERT(tvb && tvb->initialized);
  456. exception = compute_offset(tvb, offset, &abs_offset);
  457. if (exception)
  458. return -1;
  459. if (tvb->reported_length >= abs_offset)
  460. return tvb->reported_length - abs_offset;
  461. else
  462. return -1;
  463. }
  464. /* Set the reported length of a tvbuff to a given value; used for protocols
  465. * whose headers contain an explicit length and where the calling
  466. * dissector's payload may include padding as well as the packet for
  467. * this protocol.
  468. * Also adjusts the data length. */
  469. void
  470. tvb_set_reported_length(tvbuff_t *tvb, const guint reported_length)
  471. {
  472. DISSECTOR_ASSERT(tvb && tvb->initialized);
  473. if (reported_length > tvb->reported_length)
  474. THROW(ReportedBoundsError);
  475. tvb->reported_length = reported_length;
  476. if (reported_length < tvb->length)
  477. tvb->length = reported_length;
  478. }
  479. #if 0
  480. static const guint8*
  481. first_real_data_ptr(tvbuff_t *tvb)
  482. {
  483. tvbuff_t *member;
  484. switch(tvb->type) {
  485. case TVBUFF_REAL_DATA:
  486. return tvb->real_data;
  487. case TVBUFF_SUBSET:
  488. member = tvb->tvbuffs.subset.tvb;
  489. return first_real_data_ptr(member);
  490. case TVBUFF_COMPOSITE:
  491. member = tvb->tvbuffs.composite.tvbs->data;
  492. return first_real_data_ptr(member);
  493. }
  494. DISSECTOR_ASSERT_NOT_REACHED();
  495. return NULL;
  496. }
  497. #endif
  498. guint
  499. tvb_offset_from_real_beginning_counter(const tvbuff_t *tvb, const guint counter)
  500. {
  501. if (tvb->ops->tvb_offset)
  502. return tvb->ops->tvb_offset(tvb, counter);
  503. DISSECTOR_ASSERT_NOT_REACHED();
  504. return 0;
  505. }
  506. guint
  507. tvb_offset_from_real_beginning(const tvbuff_t *tvb)
  508. {
  509. return tvb_offset_from_real_beginning_counter(tvb, 0);
  510. }
  511. static const guint8*
  512. ensure_contiguous_no_exception(tvbuff_t *tvb, const gint offset, const gint length, int *pexception)
  513. {
  514. guint abs_offset, abs_length;
  515. int exception;
  516. exception = check_offset_length_no_exception(tvb, offset, length, &abs_offset, &abs_length);
  517. if (exception) {
  518. if (pexception)
  519. *pexception = exception;
  520. return NULL;
  521. }
  522. /*
  523. * We know that all the data is present in the tvbuff, so
  524. * no exceptions should be thrown.
  525. */
  526. if (tvb->real_data)
  527. return tvb->real_data + abs_offset;
  528. if (tvb->ops->tvb_get_ptr)
  529. return tvb->ops->tvb_get_ptr(tvb, abs_offset, abs_length);
  530. DISSECTOR_ASSERT_NOT_REACHED();
  531. return NULL;
  532. }
  533. static const guint8*
  534. ensure_contiguous(tvbuff_t *tvb, const gint offset, const gint length)
  535. {
  536. int exception = 0;
  537. const guint8 *p;
  538. p = ensure_contiguous_no_exception(tvb, offset, length, &exception);
  539. if (p == NULL) {
  540. DISSECTOR_ASSERT(exception > 0);
  541. THROW(exception);
  542. }
  543. return p;
  544. }
  545. static const guint8*
  546. fast_ensure_contiguous(tvbuff_t *tvb, const gint offset, const guint length)
  547. {
  548. guint end_offset;
  549. guint u_offset;
  550. DISSECTOR_ASSERT(tvb && tvb->initialized);
  551. /* We don't check for overflow in this fast path so we only handle simple types */
  552. DISSECTOR_ASSERT(length <= 8);
  553. if (offset < 0 || !tvb->real_data) {
  554. return ensure_contiguous(tvb, offset, length);
  555. }
  556. u_offset = offset;
  557. end_offset = u_offset + length;
  558. if (end_offset <= tvb->length) {
  559. return tvb->real_data + u_offset;
  560. }
  561. if (end_offset > tvb->reported_length) {
  562. if (tvb->flags & TVBUFF_FRAGMENT) {
  563. THROW(FragmentBoundsError);
  564. } else {
  565. THROW(ReportedBoundsError);
  566. }
  567. /* not reached */
  568. }
  569. THROW(BoundsError);
  570. /* not reached */
  571. return NULL;
  572. }
  573. static const guint8*
  574. guint8_pbrk(const guint8* haystack, size_t haystacklen, const guint8 *needles, guchar *found_needle)
  575. {
  576. gchar tmp[256] = { 0 };
  577. const guint8 *haystack_end;
  578. while (*needles)
  579. tmp[*needles++] = 1;
  580. haystack_end = haystack + haystacklen;
  581. while (haystack < haystack_end) {
  582. if (tmp[*haystack]) {
  583. if (found_needle)
  584. *found_needle = *haystack;
  585. return haystack;
  586. }
  587. haystack++;
  588. }
  589. return NULL;
  590. }
  591. /************** ACCESSORS **************/
  592. void *
  593. tvb_memcpy(tvbuff_t *tvb, void *target, const gint offset, size_t length)
  594. {
  595. guint abs_offset, abs_length;
  596. DISSECTOR_ASSERT(tvb && tvb->initialized);
  597. /*
  598. * XXX - we should eliminate the "length = -1 means 'to the end
  599. * of the tvbuff'" convention, and use other means to achieve
  600. * that; this would let us eliminate a bunch of checks for
  601. * negative lengths in cases where the protocol has a 32-bit
  602. * length field.
  603. *
  604. * Allowing -1 but throwing an assertion on other negative
  605. * lengths is a bit more work with the length being a size_t;
  606. * instead, we check for a length <= 2^31-1.
  607. */
  608. DISSECTOR_ASSERT(length <= 0x7FFFFFFF);
  609. check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);
  610. if (tvb->real_data) {
  611. return memcpy(target, tvb->real_data + abs_offset, abs_length);
  612. }
  613. if (tvb->ops->tvb_memcpy)
  614. return tvb->ops->tvb_memcpy(tvb, target, abs_offset, abs_length);
  615. /* XXX, fallback to slower method */
  616. DISSECTOR_ASSERT_NOT_REACHED();
  617. return NULL;
  618. }
  619. /*
  620. * XXX - this doesn't treat a length of -1 as an error.
  621. * If it did, this could replace some code that calls
  622. * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
  623. * data to it.
  624. *
  625. * "composite_get_ptr()" depends on -1 not being
  626. * an error; does anything else depend on this routine treating -1 as
  627. * meaning "to the end of the buffer"?
  628. */
  629. void *
  630. tvb_memdup(tvbuff_t *tvb, const gint offset, size_t length)
  631. {
  632. guint abs_offset, abs_length;
  633. void *duped;
  634. DISSECTOR_ASSERT(tvb && tvb->initialized);
  635. check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);
  636. duped = g_malloc(abs_length);
  637. return tvb_memcpy(tvb, duped, abs_offset, abs_length);
  638. }
  639. /*
  640. * XXX - this doesn't treat a length of -1 as an error.
  641. * If it did, this could replace some code that calls
  642. * "tvb_ensure_bytes_exist()" and then allocates a buffer and copies
  643. * data to it.
  644. *
  645. * "composite_get_ptr()" depends on -1 not being
  646. * an error; does anything else depend on this routine treating -1 as
  647. * meaning "to the end of the buffer"?
  648. *
  649. * This function allocates memory from a buffer with packet lifetime.
  650. * You do not have to free this buffer, it will be automatically freed
  651. * when wireshark starts decoding the next packet.
  652. * Do not use this function if you want the allocated memory to be persistent
  653. * after the current packet has been dissected.
  654. */
  655. void *
  656. ep_tvb_memdup(tvbuff_t *tvb, const gint offset, size_t length)
  657. {
  658. guint abs_offset, abs_length;
  659. void *duped;
  660. DISSECTOR_ASSERT(tvb && tvb->initialized);
  661. check_offset_length(tvb, offset, (gint) length, &abs_offset, &abs_length);
  662. duped = ep_alloc(abs_length);
  663. return tvb_memcpy(tvb, duped, abs_offset, abs_length);
  664. }
  665. const guint8*
  666. tvb_get_ptr(tvbuff_t *tvb, const gint offset, const gint length)
  667. {
  668. return ensure_contiguous(tvb, offset, length);
  669. }
  670. /* ---------------- */
  671. guint8
  672. tvb_get_guint8(tvbuff_t *tvb, const gint offset)
  673. {
  674. const guint8 *ptr;
  675. ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint8));
  676. return *ptr;
  677. }
  678. guint16
  679. tvb_get_ntohs(tvbuff_t *tvb, const gint offset)
  680. {
  681. const guint8 *ptr;
  682. ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
  683. return pntohs(ptr);
  684. }
  685. guint32
  686. tvb_get_ntoh24(tvbuff_t *tvb, const gint offset)
  687. {
  688. const guint8 *ptr;
  689. ptr = fast_ensure_contiguous(tvb, offset, 3);
  690. return pntoh24(ptr);
  691. }
  692. guint32
  693. tvb_get_ntohl(tvbuff_t *tvb, const gint offset)
  694. {
  695. const guint8 *ptr;
  696. ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
  697. return pntohl(ptr);
  698. }
  699. guint64
  700. tvb_get_ntoh40(tvbuff_t *tvb, const gint offset)
  701. {
  702. const guint8 *ptr;
  703. ptr = fast_ensure_contiguous(tvb, offset, 5);
  704. return pntoh40(ptr);
  705. }
  706. guint64
  707. tvb_get_ntoh48(tvbuff_t *tvb, const gint offset)
  708. {
  709. const guint8 *ptr;
  710. ptr = fast_ensure_contiguous(tvb, offset, 6);
  711. return pntoh48(ptr);
  712. }
  713. guint64
  714. tvb_get_ntoh56(tvbuff_t *tvb, const gint offset)
  715. {
  716. const guint8 *ptr;
  717. ptr = fast_ensure_contiguous(tvb, offset, 7);
  718. return pntoh56(ptr);
  719. }
  720. guint64
  721. tvb_get_ntoh64(tvbuff_t *tvb, const gint offset)
  722. {
  723. const guint8 *ptr;
  724. ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
  725. return pntoh64(ptr);
  726. }
  727. /*
  728. * Stuff for IEEE float handling on platforms that don't have IEEE
  729. * format as the native floating-point format.
  730. *
  731. * For now, we treat only the VAX as such a platform.
  732. *
  733. * XXX - other non-IEEE boxes that can run UNIX include some Crays,
  734. * and possibly other machines.
  735. *
  736. * It appears that the official Linux port to System/390 and
  737. * zArchitecture uses IEEE format floating point (not a
  738. * huge surprise).
  739. *
  740. * I don't know whether there are any other machines that
  741. * could run Wireshark and that don't use IEEE format.
  742. * As far as I know, all of the main commercial microprocessor
  743. * families on which OSes that support Wireshark can run
  744. * use IEEE format (x86, 68k, SPARC, MIPS, PA-RISC, Alpha,
  745. * IA-64, and so on).
  746. */
  747. #if defined(vax)
  748. #include <math.h>
  749. /*
  750. * Single-precision.
  751. */
  752. #define IEEE_SP_NUMBER_WIDTH 32 /* bits in number */
  753. #define IEEE_SP_EXP_WIDTH 8 /* bits in exponent */
  754. #define IEEE_SP_MANTISSA_WIDTH 23 /* IEEE_SP_NUMBER_WIDTH - 1 - IEEE_SP_EXP_WIDTH */
  755. #define IEEE_SP_SIGN_MASK 0x80000000
  756. #define IEEE_SP_EXPONENT_MASK 0x7F800000
  757. #define IEEE_SP_MANTISSA_MASK 0x007FFFFF
  758. #define IEEE_SP_INFINITY IEEE_SP_EXPONENT_MASK
  759. #define IEEE_SP_IMPLIED_BIT (1 << IEEE_SP_MANTISSA_WIDTH)
  760. #define IEEE_SP_INFINITE ((1 << IEEE_SP_EXP_WIDTH) - 1)
  761. #define IEEE_SP_BIAS ((1 << (IEEE_SP_EXP_WIDTH - 1)) - 1)
  762. static int
  763. ieee_float_is_zero(const guint32 w)
  764. {
  765. return ((w & ~IEEE_SP_SIGN_MASK) == 0);
  766. }
  767. static gfloat
  768. get_ieee_float(const guint32 w)
  769. {
  770. long sign;
  771. long exponent;
  772. long mantissa;
  773. sign = w & IEEE_SP_SIGN_MASK;
  774. exponent = w & IEEE_SP_EXPONENT_MASK;
  775. mantissa = w & IEEE_SP_MANTISSA_MASK;
  776. if (ieee_float_is_zero(w)) {
  777. /* number is zero, unnormalized, or not-a-number */
  778. return 0.0;
  779. }
  780. #if 0
  781. /*
  782. * XXX - how to handle this?
  783. */
  784. if (IEEE_SP_INFINITY == exponent) {
  785. /*
  786. * number is positive or negative infinity, or a special value
  787. */
  788. return (sign? MINUS_INFINITY: PLUS_INFINITY);
  789. }
  790. #endif
  791. exponent = ((exponent >> IEEE_SP_MANTISSA_WIDTH) - IEEE_SP_BIAS) -
  792. IEEE_SP_MANTISSA_WIDTH;
  793. mantissa |= IEEE_SP_IMPLIED_BIT;
  794. if (sign)
  795. return -mantissa * pow(2, exponent);
  796. else
  797. return mantissa * pow(2, exponent);
  798. }
  799. /*
  800. * Double-precision.
  801. * We assume that if you don't have IEEE floating-point, you have a
  802. * compiler that understands 64-bit integral quantities.
  803. */
  804. #define IEEE_DP_NUMBER_WIDTH 64 /* bits in number */
  805. #define IEEE_DP_EXP_WIDTH 11 /* bits in exponent */
  806. #define IEEE_DP_MANTISSA_WIDTH 52 /* IEEE_DP_NUMBER_WIDTH - 1 - IEEE_DP_EXP_WIDTH */
  807. #define IEEE_DP_SIGN_MASK 0x8000000000000000LL
  808. #define IEEE_DP_EXPONENT_MASK 0x7FF0000000000000LL
  809. #define IEEE_DP_MANTISSA_MASK 0x000FFFFFFFFFFFFFLL
  810. #define IEEE_DP_INFINITY IEEE_DP_EXPONENT_MASK
  811. #define IEEE_DP_IMPLIED_BIT (1LL << IEEE_DP_MANTISSA_WIDTH)
  812. #define IEEE_DP_INFINITE ((1 << IEEE_DP_EXP_WIDTH) - 1)
  813. #define IEEE_DP_BIAS ((1 << (IEEE_DP_EXP_WIDTH - 1)) - 1)
  814. static int
  815. ieee_double_is_zero(const guint64 w)
  816. {
  817. return ((w & ~IEEE_SP_SIGN_MASK) == 0);
  818. }
  819. static gdouble
  820. get_ieee_double(const guint64 w)
  821. {
  822. gint64 sign;
  823. gint64 exponent;
  824. gint64 mantissa;
  825. sign = w & IEEE_DP_SIGN_MASK;
  826. exponent = w & IEEE_DP_EXPONENT_MASK;
  827. mantissa = w & IEEE_DP_MANTISSA_MASK;
  828. if (ieee_double_is_zero(w)) {
  829. /* number is zero, unnormalized, or not-a-number */
  830. return 0.0;
  831. }
  832. #if 0
  833. /*
  834. * XXX - how to handle this?
  835. */
  836. if (IEEE_DP_INFINITY == exponent) {
  837. /*
  838. * number is positive or negative infinity, or a special value
  839. */
  840. return (sign? MINUS_INFINITY: PLUS_INFINITY);
  841. }
  842. #endif
  843. exponent = ((exponent >> IEEE_DP_MANTISSA_WIDTH) - IEEE_DP_BIAS) -
  844. IEEE_DP_MANTISSA_WIDTH;
  845. mantissa |= IEEE_DP_IMPLIED_BIT;
  846. if (sign)
  847. return -mantissa * pow(2, exponent);
  848. else
  849. return mantissa * pow(2, exponent);
  850. }
  851. #endif
  852. /*
  853. * Fetches an IEEE single-precision floating-point number, in
  854. * big-endian form, and returns a "float".
  855. *
  856. * XXX - should this be "double", in case there are IEEE single-
  857. * precision numbers that won't fit in some platform's native
  858. * "float" format?
  859. */
  860. gfloat
  861. tvb_get_ntohieee_float(tvbuff_t *tvb, const int offset)
  862. {
  863. #if defined(vax)
  864. return get_ieee_float(tvb_get_ntohl(tvb, offset));
  865. #else
  866. union {
  867. gfloat f;
  868. guint32 w;
  869. } ieee_fp_union;
  870. ieee_fp_union.w = tvb_get_ntohl(tvb, offset);
  871. return ieee_fp_union.f;
  872. #endif
  873. }
  874. /*
  875. * Fetches an IEEE double-precision floating-point number, in
  876. * big-endian form, and returns a "double".
  877. */
  878. gdouble
  879. tvb_get_ntohieee_double(tvbuff_t *tvb, const int offset)
  880. {
  881. #if defined(vax)
  882. union {
  883. guint32 w[2];
  884. guint64 dw;
  885. } ieee_fp_union;
  886. #else
  887. union {
  888. gdouble d;
  889. guint32 w[2];
  890. } ieee_fp_union;
  891. #endif
  892. #ifdef WORDS_BIGENDIAN
  893. ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset);
  894. ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset+4);
  895. #else
  896. ieee_fp_union.w[0] = tvb_get_ntohl(tvb, offset+4);
  897. ieee_fp_union.w[1] = tvb_get_ntohl(tvb, offset);
  898. #endif
  899. #if defined(vax)
  900. return get_ieee_double(ieee_fp_union.dw);
  901. #else
  902. return ieee_fp_union.d;
  903. #endif
  904. }
  905. guint16
  906. tvb_get_letohs(tvbuff_t *tvb, const gint offset)
  907. {
  908. const guint8 *ptr;
  909. ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
  910. return pletohs(ptr);
  911. }
  912. guint32
  913. tvb_get_letoh24(tvbuff_t *tvb, const gint offset)
  914. {
  915. const guint8 *ptr;
  916. ptr = fast_ensure_contiguous(tvb, offset, 3);
  917. return pletoh24(ptr);
  918. }
  919. guint32
  920. tvb_get_letohl(tvbuff_t *tvb, const gint offset)
  921. {
  922. const guint8 *ptr;
  923. ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
  924. return pletohl(ptr);
  925. }
  926. guint64
  927. tvb_get_letoh40(tvbuff_t *tvb, const gint offset)
  928. {
  929. const guint8 *ptr;
  930. ptr = fast_ensure_contiguous(tvb, offset, 5);
  931. return pletoh40(ptr);
  932. }
  933. guint64
  934. tvb_get_letoh48(tvbuff_t *tvb, const gint offset)
  935. {
  936. const guint8 *ptr;
  937. ptr = fast_ensure_contiguous(tvb, offset, 6);
  938. return pletoh48(ptr);
  939. }
  940. guint64
  941. tvb_get_letoh56(tvbuff_t *tvb, const gint offset)
  942. {
  943. const guint8 *ptr;
  944. ptr = fast_ensure_contiguous(tvb, offset, 7);
  945. return pletoh56(ptr);
  946. }
  947. guint64
  948. tvb_get_letoh64(tvbuff_t *tvb, const gint offset)
  949. {
  950. const guint8 *ptr;
  951. ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
  952. return pletoh64(ptr);
  953. }
  954. /*
  955. * Fetches an IEEE single-precision floating-point number, in
  956. * little-endian form, and returns a "float".
  957. *
  958. * XXX - should this be "double", in case there are IEEE single-
  959. * precision numbers that won't fit in some platform's native
  960. * "float" format?
  961. */
  962. gfloat
  963. tvb_get_letohieee_float(tvbuff_t *tvb, const int offset)
  964. {
  965. #if defined(vax)
  966. return get_ieee_float(tvb_get_letohl(tvb, offset));
  967. #else
  968. union {
  969. gfloat f;
  970. guint32 w;
  971. } ieee_fp_union;
  972. ieee_fp_union.w = tvb_get_letohl(tvb, offset);
  973. return ieee_fp_union.f;
  974. #endif
  975. }
  976. /*
  977. * Fetches an IEEE double-precision floating-point number, in
  978. * little-endian form, and returns a "double".
  979. */
  980. gdouble
  981. tvb_get_letohieee_double(tvbuff_t *tvb, const int offset)
  982. {
  983. #if defined(vax)
  984. union {
  985. guint32 w[2];
  986. guint64 dw;
  987. } ieee_fp_union;
  988. #else
  989. union {
  990. gdouble d;
  991. guint32 w[2];
  992. } ieee_fp_union;
  993. #endif
  994. #ifdef WORDS_BIGENDIAN
  995. ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset+4);
  996. ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset);
  997. #else
  998. ieee_fp_union.w[0] = tvb_get_letohl(tvb, offset);
  999. ieee_fp_union.w[1] = tvb_get_letohl(tvb, offset+4);
  1000. #endif
  1001. #if defined(vax)
  1002. return get_ieee_double(ieee_fp_union.dw);
  1003. #else
  1004. return ieee_fp_union.d;
  1005. #endif
  1006. }
  1007. /* Fetch an IPv4 address, in network byte order.
  1008. * We do *not* convert them to host byte order; we leave them in
  1009. * network byte order. */
  1010. guint32
  1011. tvb_get_ipv4(tvbuff_t *tvb, const gint offset)
  1012. {
  1013. const guint8 *ptr;
  1014. guint32 addr;
  1015. ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
  1016. memcpy(&addr, ptr, sizeof addr);
  1017. return addr;
  1018. }
  1019. /* Fetch an IPv6 address. */
  1020. void
  1021. tvb_get_ipv6(tvbuff_t *tvb, const gint offset, struct e_in6_addr *addr)
  1022. {
  1023. const guint8 *ptr;
  1024. ptr = ensure_contiguous(tvb, offset, sizeof(*addr));
  1025. memcpy(addr, ptr, sizeof *addr);
  1026. }
  1027. /* Fetch a GUID. */
  1028. void
  1029. tvb_get_ntohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid)
  1030. {
  1031. ensure_contiguous(tvb, offset, sizeof(*guid));
  1032. guid->data1 = tvb_get_ntohl(tvb, offset);
  1033. guid->data2 = tvb_get_ntohs(tvb, offset + 4);
  1034. guid->data3 = tvb_get_ntohs(tvb, offset + 6);
  1035. tvb_memcpy(tvb, guid->data4, offset + 8, sizeof guid->data4);
  1036. }
  1037. void
  1038. tvb_get_letohguid(tvbuff_t *tvb, const gint offset, e_guid_t *guid)
  1039. {
  1040. ensure_contiguous(tvb, offset, sizeof(*guid));
  1041. guid->data1 = tvb_get_letohl(tvb, offset);
  1042. guid->data2 = tvb_get_letohs(tvb, offset + 4);
  1043. guid->data3 = tvb_get_letohs(tvb, offset + 6);
  1044. tvb_memcpy(tvb, guid->data4, offset + 8, sizeof guid->data4);
  1045. }
  1046. /*
  1047. * NOTE: to support code written when proto_tree_add_item() took a
  1048. * gboolean as its last argument, with FALSE meaning "big-endian"
  1049. * and TRUE meaning "little-endian", we treat any non-zero value of
  1050. * "representation" as meaning "little-endian".
  1051. */
  1052. void
  1053. tvb_get_guid(tvbuff_t *tvb, const gint offset, e_guid_t *guid, const guint representation)
  1054. {
  1055. if (representation) {
  1056. tvb_get_letohguid(tvb, offset, guid);
  1057. } else {
  1058. tvb_get_ntohguid(tvb, offset, guid);
  1059. }
  1060. }
  1061. static const guint8 inverse_bit_mask8[] = {
  1062. 0xff,
  1063. 0x7f,
  1064. 0x3f,
  1065. 0x1f,
  1066. 0x0f,
  1067. 0x07,
  1068. 0x03,
  1069. 0x01
  1070. };
  1071. static const guint8 bit_mask8[] = {
  1072. 0x00,
  1073. 0x01,
  1074. 0x03,
  1075. 0x07,
  1076. 0x0f,
  1077. 0x1f,
  1078. 0x3f,
  1079. 0x7f,
  1080. 0xff
  1081. };
  1082. /* Get 1 - 8 bits */
  1083. guint8
  1084. tvb_get_bits8(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits)
  1085. {
  1086. return (guint8)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
  1087. }
  1088. /* Get 1 - 16 bits */
  1089. void
  1090. tvb_get_bits_buf(tvbuff_t *tvb, guint bit_offset, gint no_of_bits, guint8 *buf, gboolean lsb0)
  1091. {
  1092. guint8 bit_mask, bit_shift;
  1093. /* Byte align offset */
  1094. gint offset = bit_offset >> 3;
  1095. bit_offset = bit_offset & 0x7;
  1096. bit_mask = (lsb0) ? 0xff : inverse_bit_mask8[bit_offset];
  1097. bit_shift = (lsb0) ? bit_offset : (8 - bit_offset);
  1098. if (G_LIKELY(bit_offset != 0)) {
  1099. guint16 value = (guint16) tvb_get_guint8(tvb, offset);
  1100. while (no_of_bits >= 8) {
  1101. offset++;
  1102. value = ((value & bit_mask) << 8) | tvb_get_guint8(tvb, offset);
  1103. if (lsb0)
  1104. *buf++ = (guint8) (GUINT16_SWAP_LE_BE(value) >> bit_shift);
  1105. else
  1106. *buf++ = (guint8) (value >> bit_shift);
  1107. no_of_bits -= 8;
  1108. }
  1109. /* something left? */
  1110. if (no_of_bits > 0) {
  1111. guint8 tot_no_bits = bit_offset+no_of_bits;
  1112. /* Overlaps with next byte? Get next byte */
  1113. if (tot_no_bits > 8) {
  1114. offset++;
  1115. value = ((value & bit_mask) << 8) | tvb_get_guint8(tvb, offset);
  1116. }
  1117. if (lsb0) {
  1118. if (tot_no_bits > 8)
  1119. value = (GUINT16_SWAP_LE_BE(value) >> bit_offset) & (bit_mask8[no_of_bits]);
  1120. else
  1121. value = (value >> bit_offset) & (bit_mask8[no_of_bits]);
  1122. /* value = (value & ((1 << tot_no_bits)-1)) >> bit_offset; */
  1123. } else {
  1124. if (tot_no_bits > 8)
  1125. value = value >> (16 - tot_no_bits);
  1126. else
  1127. value = (value & bit_mask) >> (8-tot_no_bits);
  1128. }
  1129. *buf = (guint8) value;
  1130. }
  1131. } else {
  1132. /* fast code path for bit_offset == 0 */
  1133. while (no_of_bits >= 8) {
  1134. *buf++ = tvb_get_guint8(tvb, offset);
  1135. offset++;
  1136. no_of_bits -= 8;
  1137. }
  1138. /* something left? */
  1139. if (no_of_bits > 0) {
  1140. if (lsb0)
  1141. *buf = tvb_get_guint8(tvb, offset) & bit_mask8[no_of_bits]; /* read: ((1 << no_of_bits)-1) */
  1142. else
  1143. *buf = tvb_get_guint8(tvb, offset) >> (8-no_of_bits);
  1144. }
  1145. }
  1146. }
  1147. guint8 *
  1148. ep_tvb_get_bits(tvbuff_t *tvb, guint bit_offset, gint no_of_bits, gboolean lsb0)
  1149. {
  1150. gint no_of_bytes;
  1151. guint8 *buf;
  1152. /* XXX, no_of_bits == -1 -> to end of tvb? */
  1153. if (no_of_bits < 0) {
  1154. DISSECTOR_ASSERT_NOT_REACHED();
  1155. }
  1156. no_of_bytes = (no_of_bits >> 3) + ((no_of_bits & 0x7) != 0); /* ceil(no_of_bits / 8.0) */
  1157. buf = (guint8 *)ep_alloc(no_of_bytes);
  1158. tvb_get_bits_buf(tvb, bit_offset, no_of_bits, buf, lsb0);
  1159. return buf;
  1160. }
  1161. /* Get 9 - 16 bits */
  1162. guint16
  1163. tvb_get_bits16(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits,const guint encoding _U_)
  1164. {
  1165. /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
  1166. return (guint16)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
  1167. }
  1168. /* Get 1 - 32 bits */
  1169. guint32
  1170. tvb_get_bits32(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding _U_)
  1171. {
  1172. /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
  1173. return (guint32)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
  1174. }
  1175. /* Get 1 - 64 bits */
  1176. guint64
  1177. tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint no_of_bits, const guint encoding _U_)
  1178. {
  1179. /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
  1180. return _tvb_get_bits64(tvb, bit_offset, no_of_bits);
  1181. }
  1182. /*
  1183. * This function will dissect a sequence of bits that does not need to be byte aligned; the bits
  1184. * set will be shown in the tree as ..10 10.. and the integer value returned if return_value is set.
  1185. * Offset should be given in bits from the start of the tvb.
  1186. * The function tolerates requests for more than 64 bits, but will only return the least significant 64 bits.
  1187. */
  1188. static guint64
  1189. _tvb_get_bits64(tvbuff_t *tvb, guint bit_offset, const gint total_no_of_bits)
  1190. {
  1191. guint64 value;
  1192. guint octet_offset = bit_offset >> 3;
  1193. guint8 required_bits_in_first_octet = 8 - (bit_offset % 8);
  1194. if(required_bits_in_first_octet > total_no_of_bits)
  1195. {
  1196. /* the required bits don't extend to the end of the first octet */
  1197. guint8 right_shift = required_bits_in_first_octet - total_no_of_bits;
  1198. value = (tvb_get_guint8(tvb, octet_offset) >> right_shift) & bit_mask8[total_no_of_bits % 8];
  1199. }
  1200. else
  1201. {
  1202. guint8 remaining_bit_length = total_no_of_bits;
  1203. /* get the bits up to the first octet boundary */
  1204. value = 0;
  1205. required_bits_in_first_octet %= 8;
  1206. if(required_bits_in_first_octet != 0)
  1207. {
  1208. value = tvb_get_guint8(tvb, octet_offset) & bit_mask8[required_bits_in_first_octet];
  1209. remaining_bit_length -= required_bits_in_first_octet;
  1210. octet_offset ++;
  1211. }
  1212. /* take the biggest words, shorts or octets that we can */
  1213. while (remaining_bit_length > 7)
  1214. {
  1215. switch (remaining_bit_length >> 4)
  1216. {
  1217. case 0:
  1218. /* 8 - 15 bits. (note that 0 - 7 would have dropped out of the while() loop) */
  1219. value <<= 8;
  1220. value += tvb_get_guint8(tvb, octet_offset);
  1221. remaining_bit_length -= 8;
  1222. octet_offset ++;
  1223. break;
  1224. case 1:
  1225. /* 16 - 31 bits */
  1226. value <<= 16;
  1227. value += tvb_get_ntohs(tvb, octet_offset);
  1228. remaining_bit_length -= 16;
  1229. octet_offset += 2;
  1230. break;
  1231. case 2:
  1232. case 3:
  1233. /* 32 - 63 bits */
  1234. value <<= 32;
  1235. value += tvb_get_ntohl(tvb, octet_offset);
  1236. remaining_bit_length -= 32;
  1237. octet_offset += 4;
  1238. break;
  1239. default:
  1240. /* 64 bits (or more???) */
  1241. value = tvb_get_ntoh64(tvb, octet_offset);
  1242. remaining_bit_length -= 64;
  1243. octet_offset += 8;
  1244. break;
  1245. }
  1246. }
  1247. /* get bits from any partial octet at the tail */
  1248. if(remaining_bit_length)
  1249. {
  1250. value <<= remaining_bit_length;
  1251. value += (tvb_get_guint8(tvb, octet_offset) >> (8 - remaining_bit_length));
  1252. }
  1253. }
  1254. return value;
  1255. }
  1256. /* Get 1 - 32 bits (should be deprecated as same as tvb_get_bits32??) */
  1257. guint32
  1258. tvb_get_bits(tvbuff_t *tvb, const guint bit_offset, const gint no_of_bits, const guint encoding _U_)
  1259. {
  1260. /* note that encoding has no meaning here, as the tvb is considered to contain an octet array */
  1261. return (guint32)_tvb_get_bits64(tvb, bit_offset, no_of_bits);
  1262. }
  1263. static gint
  1264. tvb_find_guint8_generic(tvbuff_t *tvb, guint abs_offset, guint limit, guint8 needle)
  1265. {
  1266. const guint8 *ptr;
  1267. const guint8 *result;
  1268. ptr = tvb_get_ptr(tvb, abs_offset, limit);
  1269. result = (const guint8 *) memchr(ptr, needle, limit);
  1270. if (!result)
  1271. return -1;
  1272. return (gint) ((result - ptr) + abs_offset);
  1273. }
  1274. /* Find first occurrence of needle in tvbuff, starting at offset. Searches
  1275. * at most maxlength number of bytes; if maxlength is -1, searches to
  1276. * end of tvbuff.
  1277. * Returns the offset of the found needle, or -1 if not found.
  1278. * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
  1279. * in that case, -1 will be returned if the boundary is reached before
  1280. * finding needle. */
  1281. gint
  1282. tvb_find_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 needle)
  1283. {
  1284. const guint8 *result;
  1285. guint abs_offset;
  1286. guint tvbufflen;
  1287. guint limit;
  1288. DISSECTOR_ASSERT(tvb && tvb->initialized);
  1289. check_offset_length(tvb, offset, -1, &abs_offset, &tvbufflen);
  1290. /* Only search to end of tvbuff, w/o throwing exception. */
  1291. if (maxlength == -1) {
  1292. /* No maximum length specified; search to end of tvbuff. */
  1293. limit = tvbufflen;
  1294. }
  1295. else if (tvbufflen < (guint) maxlength) {
  1296. /* Maximum length goes past end of tvbuff; search to end
  1297. of tvbuff. */
  1298. limit = tvbufflen;
  1299. }
  1300. else {
  1301. /* Maximum length doesn't go past end of tvbuff; search
  1302. to that value. */
  1303. limit = maxlength;
  1304. }
  1305. /* If we have real data, perform our search now. */
  1306. if (tvb->real_data) {
  1307. result = (const guint8 *)memchr(tvb->real_data + abs_offset, needle, limit);
  1308. if (result == NULL) {
  1309. return -1;
  1310. }
  1311. else {
  1312. return (gint) (result - tvb->real_data);
  1313. }
  1314. }
  1315. if (tvb->ops->tvb_find_guint8)
  1316. return tvb->ops->tvb_find_guint8(tvb, abs_offset, limit, needle);
  1317. return tvb_find_guint8_generic(tvb, offset, limit, needle);
  1318. }
  1319. static gint
  1320. tvb_pbrk_guint8_generic(tvbuff_t *tvb, guint abs_offset, guint limit, const guint8 *needles, guchar *found_needle)
  1321. {
  1322. const guint8 *ptr;
  1323. const guint8 *result;
  1324. ptr = tvb_get_ptr(tvb, abs_offset, limit);
  1325. result = guint8_pbrk(ptr, limit, needles, found_needle);
  1326. if (!result)
  1327. return -1;
  1328. return (gint) ((result - ptr) + abs_offset);
  1329. }
  1330. /* Find first occurrence of any of the needles in tvbuff, starting at offset.
  1331. * Searches at most maxlength number of bytes; if maxlength is -1, searches
  1332. * to end of tvbuff.
  1333. * Returns the offset of the found needle, or -1 if not found.
  1334. * Will not throw an exception, even if maxlength exceeds boundary of tvbuff;
  1335. * in that case, -1 will be returned if the boundary is reached before
  1336. * finding needle. */
  1337. gint
  1338. tvb_pbrk_guint8(tvbuff_t *tvb, const gint offset, const gint maxlength, const guint8 *needles, guchar *found_needle)
  1339. {
  1340. const guint8 *result;
  1341. guint abs_offset;
  1342. guint tvbufflen;
  1343. guint limit;
  1344. DISSECTOR_ASSERT(tvb && tvb->initialized);
  1345. check_offset_length(tvb, offset, -1, &abs_offset, &tvbufflen);
  1346. /* Only search to end of tvbuff, w/o throwing exception. */
  1347. if (maxlength == -1) {
  1348. /* No maximum length specified; search to end of tvbuff. */
  1349. limit = tvbufflen;
  1350. }
  1351. else if (tvbufflen < (guint) maxlength) {
  1352. /* Maximum length goes past end of tvbuff; search to end
  1353. of tvbuff. */
  1354. limit = tvbufflen;
  1355. }
  1356. else {
  1357. /* Maximum length doesn't go past end of tvbuff; search
  1358. to that value. */
  1359. limit = maxlength;
  1360. }
  1361. /* If we have real data, perform our search now. */
  1362. if (tvb->real_data) {
  1363. result = guint8_pbrk(tvb->real_data + abs_offset, limit, needles, found_needle);
  1364. if (result == NULL) {
  1365. return -1;
  1366. }
  1367. else {
  1368. return (gint) (result - tvb->real_data);
  1369. }
  1370. }
  1371. if (tvb->ops->tvb_pbrk_guint8)
  1372. return tvb->ops->tvb_pbrk_guint8(tvb, abs_offset, limit, needles, found_needle);
  1373. return tvb_pbrk_guint8_generic(tvb, abs_offset, limit, needles, found_needle);
  1374. }
  1375. /* Find size of stringz (NUL-terminated string) by looking for terminating
  1376. * NUL. The size of the string includes the terminating NUL.
  1377. *
  1378. * If the NUL isn't found, it throws the appropriate exception.
  1379. */
  1380. guint
  1381. tvb_strsize(tvbuff_t *tvb, const gint offset)
  1382. {
  1383. guint abs_offset, junk_length;
  1384. gint nul_offset;
  1385. DISSECTOR_ASSERT(tvb && tvb->initialized);
  1386. check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
  1387. nul_offset = tvb_find_guint8(tvb, abs_offset, -1, 0);
  1388. if (nul_offset == -1) {
  1389. /*
  1390. * OK, we hit the end of the tvbuff, so we should throw
  1391. * an exception.
  1392. *
  1393. * Did we hit the end of the captured data, or the end
  1394. * of the actual data? If there's less captured data
  1395. * than actual data, we presumably hit the end of the
  1396. * captured data, otherwise we hit the end of the actual
  1397. * data.
  1398. */
  1399. if (tvb->length < tvb->reported_length) {
  1400. THROW(BoundsError);
  1401. } else {
  1402. if (tvb->flags & TVBUFF_FRAGMENT) {
  1403. THROW(FragmentBoundsError);
  1404. } else {
  1405. THROW(ReportedBoundsError);
  1406. }
  1407. }
  1408. }
  1409. return (nul_offset - abs_offset) + 1;
  1410. }
  1411. /* UTF-16/UCS-2 version of tvb_strsize */
  1412. /* Returns number of bytes including the (two-bytes) null terminator */
  1413. guint
  1414. tvb_unicode_strsize(tvbuff_t *tvb, const gint offset)
  1415. {
  1416. guint i = 0;
  1417. gunichar2 uchar;
  1418. DISSECTOR_ASSERT(tvb && tvb->initialized);
  1419. do {
  1420. /* Endianness doesn't matter when looking for null */
  1421. uchar = tvb_get_ntohs(tvb, offset + i);
  1422. i += 2;
  1423. } while(uchar != 0);
  1424. return i;
  1425. }
  1426. /* Find length of string by looking for end of string ('\0'), up to
  1427. * 'maxlength' characters'; if 'maxlength' is -1, searches to end
  1428. * of tvbuff.
  1429. * Returns -1 if 'maxlength' reached before finding EOS. */
  1430. gint
  1431. tvb_strnlen(tvbuff_t *tvb, const gint offset, const guint maxlength)
  1432. {
  1433. gint result_offset;
  1434. guint abs_offset, junk_length;
  1435. DISSECTOR_ASSERT(tvb && tvb->initialized);
  1436. check_offset_length(tvb, offset, 0, &abs_offset, &junk_length);
  1437. result_offset = tvb_find_guint8(tvb, abs_offset, maxlength, 0);
  1438. if (result_offset == -1) {
  1439. return -1;
  1440. }
  1441. else {
  1442. return result_offset - abs_offset;
  1443. }
  1444. }
  1445. /*
  1446. * Implement strneql etc
  1447. */
  1448. /*
  1449. * Call strncmp after checking if enough chars left, returning 0 if
  1450. * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
  1451. */
  1452. gint
  1453. tvb_strneql(tvbuff_t *tvb, const gint offset, const gchar *str, const size_t size)
  1454. {
  1455. const guint8 *ptr;
  1456. ptr = ensure_contiguous_no_exception(tvb, offset, (gint)size, NULL);
  1457. if (ptr) {
  1458. int cmp = strncmp((const char *)ptr, str, size);
  1459. /*
  1460. * Return 0 if equal, -1 otherwise.
  1461. */
  1462. return (cmp == 0 ? 0 : -1);
  1463. } else {
  1464. /*
  1465. * Not enough characters in the tvbuff to match the
  1466. * string.
  1467. */
  1468. return -1;
  1469. }
  1470. }
  1471. /*
  1472. * Call g_ascii_strncasecmp after checking if enough chars left, returning
  1473. * 0 if it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
  1474. */
  1475. gint
  1476. tvb_strncaseeql(tvbuff_t *tvb, const gint offset, const gchar *str, const size_t size)
  1477. {
  1478. const guint8 *ptr;
  1479. ptr = ensure_contiguous_no_exception(tvb, offset, (gint)size, NULL);
  1480. if (ptr) {
  1481. int cmp = g_ascii_strncasecmp((const char *)ptr, str, size);
  1482. /*
  1483. * Return 0 if equal, -1 otherwise.
  1484. */
  1485. return (cmp == 0 ? 0 : -1);
  1486. } else {
  1487. /*
  1488. * Not enough characters in the tvbuff to match the
  1489. * string.
  1490. */
  1491. return -1;
  1492. }
  1493. }
  1494. /*
  1495. * Call memcmp after checking if enough chars left, returning 0 if
  1496. * it returns 0 (meaning "equal") and -1 otherwise, otherwise return -1.
  1497. */
  1498. gint
  1499. tvb_memeql(tvbuff_t *tvb, const gint offset, const guint8 *str, size_t size)
  1500. {
  1501. const guint8 *ptr;
  1502. ptr = ensure_contiguous_no_exception(tvb, offset, (gint) size, NULL);
  1503. if (ptr) {
  1504. int cmp = memcmp(ptr, str, size);
  1505. /*
  1506. * Return 0 if equal, -1 otherwise.
  1507. */
  1508. return (cmp == 0 ? 0 : -1);
  1509. } else {
  1510. /*
  1511. * Not enough characters in the tvbuff to match the
  1512. * string.
  1513. */
  1514. return -1;
  1515. }
  1516. }
  1517. /* Convert a string from Unicode to ASCII. At the moment we fake it by
  1518. * replacing all non-ASCII characters with a '.' )-: The caller must
  1519. * free the result returned. The len parameter is the number of guint16's
  1520. * to convert from Unicode. */
  1521. /* XXX - this function has been superceded by tvb_get_unicode_string() */
  1522. char *
  1523. tvb_fake_unicode(tvbuff_t *tvb, int offset, const int len, const gboolean little_endian)
  1524. {
  1525. char *buffer;
  1526. int i;
  1527. guint16 character;
  1528. /* Make sure we have enough data before allocating the buffer,
  1529. so we don't blow up if the length is huge. */
  1530. tvb_ensure_bytes_exist(tvb, offset, 2*len);
  1531. /* We know we won't throw an exception, so we don't have to worry
  1532. about leaking this buffer. */
  1533. buffer = (char *)g_malloc(len + 1);
  1534. for (i = 0; i < len; i++) {
  1535. character = little_endian ? tvb_get_letohs(tvb, offset)
  1536. : tvb_get_ntohs(tvb, offset);
  1537. buffer[i] = character < 256 ? character : '.';
  1538. offset += 2;
  1539. }
  1540. buffer[len] = 0;
  1541. return buffer;
  1542. }
  1543. /* Convert a string from Unicode to ASCII. At the moment we fake it by
  1544. * replacing all non-ASCII characters with a '.' )-: The len parameter is
  1545. * the number of guint16's to convert from Unicode.
  1546. *
  1547. * This function allocates memory from a buffer with packet lifetime.
  1548. * You do not have to free this buffer, it will be automatically freed
  1549. * when wireshark starts decoding the next packet.
  1550. */
  1551. /* XXX: This has been replaced by tvb_get_ephemeral_unicode_string() */
  1552. char *
  1553. tvb_get_ephemeral_faked_unicode(tvbuff_t *tvb, int offset, const int len, const gboolean little_endian)
  1554. {
  1555. char *buffer;
  1556. int i;
  1557. guint16 character;
  1558. /* Make sure we have enough data before allocating the buffer,
  1559. so we don't blow up if the length is huge. */
  1560. tvb_ensure_bytes_exist(tvb, offset, 2*len);
  1561. /* We know we won't throw an exception, so we don't have to worry
  1562. about leaking this buffer. */
  1563. buffer = (char *)ep_alloc(len + 1);
  1564. for (i = 0; i < len; i++) {
  1565. character = little_endian ? tvb_get_letohs(tvb, offset)
  1566. : tvb_get_ntohs(tvb, offset);
  1567. buffer[i] = character < 256 ? character : '.';
  1568. offset += 2;
  1569. }
  1570. buffer[len] = 0;
  1571. return buffer;
  1572. }
  1573. /*
  1574. * Format the data in the tvb from offset for length ...
  1575. */
  1576. gchar *
  1577. tvb_format_text(tvbuff_t *tvb, const gint offset, const gint size)
  1578. {
  1579. const guint8 *ptr;
  1580. gint len;
  1581. len = (size > 0) ? size : 0;
  1582. if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
  1583. len = tvb_length_remaining(tvb, offset);
  1584. ptr = ensure_contiguous(tvb, offset, len);
  1585. }
  1586. return format_text(ptr, len);
  1587. }
  1588. /*
  1589. * Format the data in the tvb from offset for length ...
  1590. */
  1591. gchar *
  1592. tvb_format_text_wsp(tvbuff_t *tvb, const gint offset, const gint size)
  1593. {
  1594. const guint8 *ptr;
  1595. gint len;
  1596. len = (size > 0) ? size : 0;
  1597. if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
  1598. len = tvb_length_remaining(tvb, offset);
  1599. ptr = ensure_contiguous(tvb, offset, len);
  1600. }
  1601. return format_text_wsp(ptr, len);
  1602. }
  1603. /*
  1604. * Like "tvb_format_text()", but for null-padded strings; don't show
  1605. * the null padding characters as "\000".
  1606. */
  1607. gchar *
  1608. tvb_format_stringzpad(tvbuff_t *tvb, const gint offset, const gint size)
  1609. {
  1610. const guint8 *ptr, *p;
  1611. gint len;
  1612. gint stringlen;
  1613. len = (size > 0) ? size : 0;
  1614. if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
  1615. len = tvb_length_remaining(tvb, offset);
  1616. ptr = ensure_contiguous(tvb, offset, len);
  1617. }
  1618. for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
  1619. ;
  1620. return format_text(ptr, stringlen);
  1621. }
  1622. /*
  1623. * Like "tvb_format_text_wsp()", but for null-padded strings; don't show
  1624. * the null padding characters as "\000".
  1625. */
  1626. gchar *
  1627. tvb_format_stringzpad_wsp(tvbuff_t *tvb, const gint offset, const gint size)
  1628. {
  1629. const guint8 *ptr, *p;
  1630. gint len;
  1631. gint stringlen;
  1632. len = (size > 0) ? size : 0;
  1633. if ((ptr = ensure_contiguous(tvb, offset, size)) == NULL) {
  1634. len = tvb_length_remaining(tvb, offset);
  1635. ptr = ensure_contiguous(tvb, offset, len);
  1636. }
  1637. for (p = ptr, stringlen = 0; stringlen < len && *p != '\0'; p++, stringlen++)
  1638. ;
  1639. return format_text_wsp(ptr, stringlen);
  1640. }
  1641. /*
  1642. * Given a tvbuff, an offset, and a length, allocate a buffer big enough
  1643. * to hold a non-null-terminated string of that length at that offset,
  1644. * plus a trailing '\0', copy the string into it, and return a pointer
  1645. * to the string.
  1646. *
  1647. * Throws an exception if the tvbuff ends before the string does.
  1648. */
  1649. guint8 *
  1650. tvb_get_string(tvbuff_t *tvb, const gint offset, const gint length)
  1651. {
  1652. const guint8 *ptr;
  1653. guint8 *strbuf = NULL;
  1654. tvb_ensure_bytes_exist(tvb, offset, length);
  1655. ptr = ensure_contiguous(tvb, offset, length);
  1656. strbuf = (guint8 *)g_malloc(length + 1);
  1657. if (length != 0) {
  1658. memcpy(strbuf, ptr, length);
  1659. }
  1660. strbuf[length] = '\0';
  1661. return strbuf;
  1662. }
  1663. /*
  1664. * Unicode (UTF-16) version of tvb_get_string()
  1665. * XXX - this is UCS-2, not UTF-16, as it doesn't handle surrogate pairs
  1666. *
  1667. * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
  1668. *
  1669. * Specify length in bytes
  1670. *
  1671. * Returns an UTF-8 string that must be freed by the caller
  1672. */
  1673. gchar *
  1674. tvb_get_unicode_string(tvbuff_t *tvb, const gint offset, gint length, const guint encoding)
  1675. {
  1676. gunichar2 uchar;
  1677. gint i; /* Byte counter for tvbuff */
  1678. GString *strbuf = NULL;
  1679. tvb_ensure_bytes_exist(tvb, offset, length);
  1680. strbuf = g_string_new(NULL);
  1681. for(i = 0; i < length; i += 2) {
  1682. if (encoding == ENC_BIG_ENDIAN)
  1683. uchar = tvb_get_ntohs(tvb, offset + i);
  1684. else
  1685. uchar = tvb_get_letohs(tvb, offset + i);
  1686. g_string_append_unichar(strbuf, uchar);
  1687. }
  1688. return g_string_free(strbuf, FALSE);
  1689. }
  1690. /*
  1691. * Given a tvbuff, an offset, a length, and an encoding, allocate a
  1692. * buffer big enoug…

Large files files are truncated, but you can click here to view the full file