PageRenderTime 34ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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 enough to hold a non-null-terminated string of that length
  1693. * at that offset, plus a trailing '\0', copy into the buffer the
  1694. * string as converted from the appropriate encoding to UTF-8, and
  1695. * return a pointer to the string.
  1696. *
  1697. * Throws an exception if the tvbuff ends before the string does.
  1698. *
  1699. * This function allocates memory from a buffer with packet lifetime.
  1700. * You do not have to free this buffer, it will be automatically freed
  1701. * when wireshark starts decoding the next packet.
  1702. * Do not use this function if you want the allocated memory to be persistent
  1703. * after the current packet has been dissected.
  1704. */
  1705. guint8 *
  1706. tvb_get_ephemeral_string_enc(tvbuff_t *tvb, const gint offset,
  1707. const gint length, const guint encoding)
  1708. {
  1709. const guint8 *ptr;
  1710. guint8 *strbuf;
  1711. switch (encoding & ENC_CHARENCODING_MASK) {
  1712. case ENC_ASCII:
  1713. default:
  1714. /*
  1715. * For now, we treat bogus values as meaning
  1716. * "ASCII" rather than reporting an error,
  1717. * for the benefit of old dissectors written
  1718. * when the last argument to proto_tree_add_item()
  1719. * was a gboolean for the byte order, not an
  1720. * encoding value, and passed non-zero values
  1721. * other than TRUE to mean "little-endian".
  1722. *
  1723. * XXX - should map all octets with the 8th bit
  1724. * not set to a "substitute" UTF-8 character.
  1725. */
  1726. strbuf = tvb_get_ephemeral_string(tvb, offset, length);
  1727. break;
  1728. case ENC_UTF_8:
  1729. /*
  1730. * XXX - should map all invalid UTF-8 sequences
  1731. * to a "substitute" UTF-8 character.
  1732. */
  1733. strbuf = tvb_get_ephemeral_string(tvb, offset, length);
  1734. break;
  1735. case ENC_UTF_16:
  1736. /*
  1737. * XXX - needs to handle surrogate pairs and to map
  1738. * invalid characters and sequences to a "substitute"
  1739. * UTF-8 character.
  1740. */
  1741. strbuf = tvb_get_ephemeral_unicode_string(tvb, offset, length,
  1742. encoding & ENC_LITTLE_ENDIAN);
  1743. break;
  1744. case ENC_UCS_2:
  1745. /*
  1746. * XXX - needs to map values that are not valid UCS-2
  1747. * characters (such as, I think, values used as the
  1748. * components of a UTF-16 surrogate pair) to a
  1749. * "substitute" UTF-8 character.
  1750. */
  1751. strbuf = tvb_get_ephemeral_unicode_string(tvb, offset, length,
  1752. encoding & ENC_LITTLE_ENDIAN);
  1753. break;
  1754. case ENC_EBCDIC:
  1755. /*
  1756. * XXX - do the copy and conversion in one pass.
  1757. *
  1758. * XXX - multiple "dialects" of EBCDIC?
  1759. */
  1760. tvb_ensure_bytes_exist(tvb, offset, length); /* make sure length = -1 fails */
  1761. strbuf = (guint8 *)ep_alloc(length + 1);
  1762. if (length != 0) {
  1763. ptr = ensure_contiguous(tvb, offset, length);
  1764. memcpy(strbuf, ptr, length);
  1765. EBCDIC_to_ASCII(strbuf, length);
  1766. }
  1767. strbuf[length] = '\0';
  1768. break;
  1769. }
  1770. return strbuf;
  1771. }
  1772. guint8 *
  1773. tvb_get_ephemeral_string(tvbuff_t *tvb, const gint offset, const gint length)
  1774. {
  1775. guint8 *strbuf;
  1776. tvb_ensure_bytes_exist(tvb, offset, length); /* make sure length = -1 fails */
  1777. strbuf = (guint8 *)ep_alloc(length + 1);
  1778. tvb_memcpy(tvb, strbuf, offset, length);
  1779. strbuf[length] = '\0';
  1780. return strbuf;
  1781. }
  1782. /*
  1783. * Unicode (UTF-16) version of tvb_get_ephemeral_string()
  1784. * XXX - this is UCS-2, not UTF-16, as it doesn't handle surrogate pairs
  1785. *
  1786. * Encoding parameter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
  1787. *
  1788. * Specify length in bytes
  1789. *
  1790. * Returns an ep_ allocated UTF-8 string
  1791. */
  1792. gchar *
  1793. tvb_get_ephemeral_unicode_string(tvbuff_t *tvb, const gint offset, gint length, const guint encoding)
  1794. {
  1795. gunichar2 uchar;
  1796. gint i; /* Byte counter for tvbuff */
  1797. emem_strbuf_t *strbuf;
  1798. tvb_ensure_bytes_exist(tvb, offset, length);
  1799. strbuf = ep_strbuf_new(NULL);
  1800. for(i = 0; i < length; i += 2) {
  1801. if (encoding == ENC_BIG_ENDIAN)
  1802. uchar = tvb_get_ntohs(tvb, offset + i);
  1803. else
  1804. uchar = tvb_get_letohs(tvb, offset + i);
  1805. ep_strbuf_append_unichar(strbuf, uchar);
  1806. }
  1807. return strbuf->str;
  1808. }
  1809. /*
  1810. * Given a tvbuff, an offset, and a length, allocate a buffer big enough
  1811. * to hold a non-null-terminated string of that length at that offset,
  1812. * plus a trailing '\0', copy the string into it, and return a pointer
  1813. * to the string.
  1814. *
  1815. * Throws an exception if the tvbuff ends before the string does.
  1816. *
  1817. * This function allocates memory from a buffer with capture session lifetime.
  1818. * You do not have to free this buffer, it will be automatically freed
  1819. * when wireshark starts or opens a new capture.
  1820. */
  1821. guint8 *
  1822. tvb_get_seasonal_string(tvbuff_t *tvb, const gint offset, const gint length)
  1823. {
  1824. const guint8 *ptr;
  1825. guint8 *strbuf = NULL;
  1826. tvb_ensure_bytes_exist(tvb, offset, length);
  1827. ptr = ensure_contiguous(tvb, offset, length);
  1828. strbuf = (guint8 *)se_alloc(length + 1);
  1829. if (length != 0) {
  1830. memcpy(strbuf, ptr, length);
  1831. }
  1832. strbuf[length] = '\0';
  1833. return strbuf;
  1834. }
  1835. /*
  1836. * Given a tvbuff, an offset, and an encoding, with the offset assumed
  1837. * to refer to a null-terminated string, find the length of that string
  1838. * (and throw an exception if the tvbuff ends before we find the null),
  1839. * allocate a buffer big enough to hold the string, copy the string into
  1840. * it, and return a pointer to the string; if the encoding is EBCDIC, map
  1841. * the string from EBCDIC to ASCII. Also return the length of the
  1842. * string (including the terminating null) through a pointer.
  1843. */
  1844. guint8 *
  1845. tvb_get_stringz_enc(tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
  1846. {
  1847. guint size;
  1848. guint8 *strptr;
  1849. size = tvb_strsize(tvb, offset);
  1850. strptr = (guint8 *)g_malloc(size);
  1851. tvb_memcpy(tvb, strptr, offset, size);
  1852. if ((encoding & ENC_CHARENCODING_MASK) == ENC_EBCDIC)
  1853. EBCDIC_to_ASCII(strptr, size);
  1854. if (lengthp)
  1855. *lengthp = size;
  1856. return strptr;
  1857. }
  1858. guint8 *
  1859. tvb_get_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
  1860. {
  1861. return tvb_get_stringz_enc(tvb, offset, lengthp, ENC_UTF_8|ENC_NA);
  1862. }
  1863. /*
  1864. * Given a tvbuff and an offset, with the offset assumed to refer to
  1865. * a null-terminated string, find the length of that string (and throw
  1866. * an exception if the tvbuff ends before we find the null), ensure that
  1867. * the TVB is flat, and return a pointer to the string (in the TVB).
  1868. * Also return the length of the string (including the terminating null)
  1869. * through a pointer.
  1870. *
  1871. * As long as we aren't using composite TVBs, this saves the cycles used
  1872. * (often unnecessariliy) in allocating a buffer and copying the string into
  1873. * it. (If we do start using composite TVBs, we may want to replace this
  1874. * function with the _ephemeral versoin.)
  1875. */
  1876. const guint8 *
  1877. tvb_get_const_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
  1878. {
  1879. guint size;
  1880. const guint8 *strptr;
  1881. size = tvb_strsize(tvb, offset);
  1882. strptr = ensure_contiguous(tvb, offset, size);
  1883. if (lengthp)
  1884. *lengthp = size;
  1885. return strptr;
  1886. }
  1887. /*
  1888. * Given a tvbuff and an offset, with the offset assumed to refer to
  1889. * a null-terminated string, find the length of that string (and throw
  1890. * an exception if the tvbuff ends before we find the null), allocate
  1891. * a buffer big enough to hold the string, copy the string into it,
  1892. * and return a pointer to the string. Also return the length of the
  1893. * string (including the terminating null) through a pointer.
  1894. *
  1895. * This function allocates memory from a buffer with packet lifetime.
  1896. * You do not have to free this buffer, it will be automatically freed
  1897. * when wireshark starts decoding the next packet.
  1898. * Do not use this function if you want the allocated memory to be persistent
  1899. * after the current packet has been dissected.
  1900. */
  1901. guint8 *
  1902. tvb_get_ephemeral_stringz_enc(tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
  1903. {
  1904. guint size;
  1905. guint8 *strptr;
  1906. switch (encoding & ENC_CHARENCODING_MASK) {
  1907. case ENC_ASCII:
  1908. default:
  1909. /*
  1910. * For now, we treat bogus values as meaning
  1911. * "ASCII" rather than reporting an error,
  1912. * for the benefit of old dissectors written
  1913. * when the last argument to proto_tree_add_item()
  1914. * was a gboolean for the byte order, not an
  1915. * encoding value, and passed non-zero values
  1916. * other than TRUE to mean "little-endian".
  1917. *
  1918. * XXX - should map all octets with the 8th bit
  1919. * not set to a "substitute" UTF-8 character.
  1920. */
  1921. strptr = tvb_get_ephemeral_stringz(tvb, offset, lengthp);
  1922. break;
  1923. case ENC_UTF_8:
  1924. /*
  1925. * XXX - should map all invalid UTF-8 sequences
  1926. * to a "substitute" UTF-8 character.
  1927. */
  1928. strptr = tvb_get_ephemeral_stringz(tvb, offset, lengthp);
  1929. break;
  1930. case ENC_UTF_16:
  1931. /*
  1932. * XXX - needs to handle surrogate pairs and to map
  1933. * invalid characters and sequences to a "substitute"
  1934. * UTF-8 character.
  1935. */
  1936. strptr = tvb_get_ephemeral_unicode_stringz(tvb, offset, lengthp,
  1937. encoding & ENC_LITTLE_ENDIAN);
  1938. break;
  1939. case ENC_UCS_2:
  1940. /*
  1941. * XXX - needs to map values that are not valid UCS-2
  1942. * characters (such as, I think, values used as the
  1943. * components of a UTF-16 surrogate pair) to a
  1944. * "substitute" UTF-8 character.
  1945. */
  1946. strptr = tvb_get_ephemeral_unicode_stringz(tvb, offset, lengthp,
  1947. encoding & ENC_LITTLE_ENDIAN);
  1948. break;
  1949. case ENC_EBCDIC:
  1950. /*
  1951. * XXX - do the copy and conversion in one pass.
  1952. *
  1953. * XXX - multiple "dialects" of EBCDIC?
  1954. */
  1955. size = tvb_strsize(tvb, offset);
  1956. strptr = (guint8 *)ep_alloc(size);
  1957. tvb_memcpy(tvb, strptr, offset, size);
  1958. EBCDIC_to_ASCII(strptr, size);
  1959. if (lengthp)
  1960. *lengthp = size;
  1961. break;
  1962. }
  1963. return strptr;
  1964. }
  1965. guint8 *
  1966. tvb_get_ephemeral_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
  1967. {
  1968. guint size;
  1969. guint8 *strptr;
  1970. size = tvb_strsize(tvb, offset);
  1971. strptr = (guint8 *)ep_alloc(size);
  1972. tvb_memcpy(tvb, strptr, offset, size);
  1973. if (lengthp)
  1974. *lengthp = size;
  1975. return strptr;
  1976. }
  1977. /*
  1978. * Unicode (UTF-16) version of tvb_get_ephemeral_stringz()
  1979. *
  1980. * Encoding paramter should be ENC_BIG_ENDIAN or ENC_LITTLE_ENDIAN
  1981. *
  1982. * Returns an ep_ allocated UTF-8 string and updates lengthp pointer with length of string (in bytes)
  1983. */
  1984. gchar *
  1985. tvb_get_ephemeral_unicode_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp, const guint encoding)
  1986. {
  1987. gunichar2 uchar;
  1988. gint size; /* Number of UTF-16 characters */
  1989. gint i; /* Byte counter for tvbuff */
  1990. emem_strbuf_t *strbuf;
  1991. size = tvb_unicode_strsize(tvb, offset);
  1992. strbuf = ep_strbuf_new(NULL);
  1993. for(i = 0; i < size; i += 2) {
  1994. if (encoding == ENC_BIG_ENDIAN)
  1995. uchar = tvb_get_ntohs(tvb, offset + i);
  1996. else
  1997. uchar = tvb_get_letohs(tvb, offset + i);
  1998. ep_strbuf_append_unichar(strbuf, uchar);
  1999. }
  2000. if (lengthp)
  2001. *lengthp = i; /* Number of *bytes* processed */
  2002. return strbuf->str;
  2003. }
  2004. /*
  2005. * Given a tvbuff and an offset, with the offset assumed to refer to
  2006. * a null-terminated string, find the length of that string (and throw
  2007. * an exception if the tvbuff ends before we find the null), allocate
  2008. * a buffer big enough to hold the string, copy the string into it,
  2009. * and return a pointer to the string. Also return the length of the
  2010. * string (including the terminating null) through a pointer.
  2011. *
  2012. * This function allocates memory from a buffer with capture session lifetime.
  2013. * You do not have to free this buffer, it will be automatically freed
  2014. * when wireshark starts or opens a new capture.
  2015. */
  2016. guint8 *
  2017. tvb_get_seasonal_stringz(tvbuff_t *tvb, const gint offset, gint *lengthp)
  2018. {
  2019. guint size;
  2020. guint8 *strptr;
  2021. size = tvb_strsize(tvb, offset);
  2022. strptr = (guint8 *)se_alloc(size);
  2023. tvb_memcpy(tvb, strptr, offset, size);
  2024. if (lengthp)
  2025. *lengthp = size;
  2026. return strptr;
  2027. }
  2028. /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
  2029. * no more than bufsize number of bytes, including terminating NUL, to buffer.
  2030. * Returns length of string (not including terminating NUL), or -1 if the string was
  2031. * truncated in the buffer due to not having reached the terminating NUL.
  2032. * In this way, it acts like g_snprintf().
  2033. *
  2034. * bufsize MUST be greater than 0.
  2035. *
  2036. * When processing a packet where the remaining number of bytes is less
  2037. * than bufsize, an exception is not thrown if the end of the packet
  2038. * is reached before the NUL is found. If no NUL is found before reaching
  2039. * the end of the short packet, -1 is still returned, and the string
  2040. * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
  2041. * at the correct spot, terminating the string.
  2042. *
  2043. * *bytes_copied will contain the number of bytes actually copied,
  2044. * including the terminating-NUL.
  2045. */
  2046. static gint
  2047. _tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer, gint *bytes_copied)
  2048. {
  2049. gint stringlen;
  2050. guint abs_offset;
  2051. gint limit, len;
  2052. gboolean decreased_max = FALSE;
  2053. /* Only read to end of tvbuff, w/o throwing exception. */
  2054. check_offset_length(tvb, offset, -1, &abs_offset, &len);
  2055. /* There must at least be room for the terminating NUL. */
  2056. DISSECTOR_ASSERT(bufsize != 0);
  2057. /* If there's no room for anything else, just return the NUL. */
  2058. if (bufsize == 1) {
  2059. buffer[0] = 0;
  2060. *bytes_copied = 1;
  2061. return 0;
  2062. }
  2063. /* check_offset_length() won't throw an exception if we're
  2064. * looking at the byte immediately after the end of the tvbuff. */
  2065. if (len == 0) {
  2066. THROW(ReportedBoundsError);
  2067. }
  2068. /* This should not happen because check_offset_length() would
  2069. * have already thrown an exception if 'offset' were out-of-bounds.
  2070. */
  2071. DISSECTOR_ASSERT(len != -1);
  2072. /*
  2073. * If we've been passed a negative number, bufsize will
  2074. * be huge.
  2075. */
  2076. DISSECTOR_ASSERT(bufsize <= G_MAXINT);
  2077. if ((guint)len < bufsize) {
  2078. limit = len;
  2079. decreased_max = TRUE;
  2080. }
  2081. else {
  2082. limit = bufsize;
  2083. }
  2084. stringlen = tvb_strnlen(tvb, abs_offset, limit - 1);
  2085. /* If NUL wasn't found, copy the data and return -1 */
  2086. if (stringlen == -1) {
  2087. tvb_memcpy(tvb, buffer, abs_offset, limit);
  2088. if (decreased_max) {
  2089. buffer[limit] = 0;
  2090. /* Add 1 for the extra NUL that we set at buffer[limit],
  2091. * pretending that it was copied as part of the string. */
  2092. *bytes_copied = limit + 1;
  2093. }
  2094. else {
  2095. *bytes_copied = limit;
  2096. }
  2097. return -1;
  2098. }
  2099. /* Copy the string to buffer */
  2100. tvb_memcpy(tvb, buffer, abs_offset, stringlen + 1);
  2101. *bytes_copied = stringlen + 1;
  2102. return stringlen;
  2103. }
  2104. /* Looks for a stringz (NUL-terminated string) in tvbuff and copies
  2105. * no more than bufsize number of bytes, including terminating NUL, to buffer.
  2106. * Returns length of string (not including terminating NUL), or -1 if the string was
  2107. * truncated in the buffer due to not having reached the terminating NUL.
  2108. * In this way, it acts like g_snprintf().
  2109. *
  2110. * When processing a packet where the remaining number of bytes is less
  2111. * than bufsize, an exception is not thrown if the end of the packet
  2112. * is reached before the NUL is found. If no NUL is found before reaching
  2113. * the end of the short packet, -1 is still returned, and the string
  2114. * is truncated with a NUL, albeit not at buffer[bufsize - 1], but
  2115. * at the correct spot, terminating the string.
  2116. */
  2117. gint
  2118. tvb_get_nstringz(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer)
  2119. {
  2120. gint bytes_copied;
  2121. DISSECTOR_ASSERT(tvb && tvb->initialized);
  2122. return _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
  2123. }
  2124. /* Like tvb_get_nstringz(), but never returns -1. The string is guaranteed to
  2125. * have a terminating NUL. If the string was truncated when copied into buffer,
  2126. * a NUL is placed at the end of buffer to terminate it.
  2127. */
  2128. gint
  2129. tvb_get_nstringz0(tvbuff_t *tvb, const gint offset, const guint bufsize, guint8* buffer)
  2130. {
  2131. gint len, bytes_copied;
  2132. DISSECTOR_ASSERT(tvb && tvb->initialized);
  2133. len = _tvb_get_nstringz(tvb, offset, bufsize, buffer, &bytes_copied);
  2134. if (len == -1) {
  2135. buffer[bufsize - 1] = 0;
  2136. return bytes_copied - 1;
  2137. }
  2138. else {
  2139. return len;
  2140. }
  2141. }
  2142. /*
  2143. * Given a tvbuff, an offset into the tvbuff, and a length that starts
  2144. * at that offset (which may be -1 for "all the way to the end of the
  2145. * tvbuff"), find the end of the (putative) line that starts at the
  2146. * specified offset in the tvbuff, going no further than the specified
  2147. * length.
  2148. *
  2149. * Return the length of the line (not counting the line terminator at
  2150. * the end), or, if we don't find a line terminator:
  2151. *
  2152. * if "deseg" is true, return -1;
  2153. *
  2154. * if "deseg" is false, return the amount of data remaining in
  2155. * the buffer.
  2156. *
  2157. * Set "*next_offset" to the offset of the character past the line
  2158. * terminator, or past the end of the buffer if we don't find a line
  2159. * terminator. (It's not set if we return -1.)
  2160. */
  2161. gint
  2162. tvb_find_line_end(tvbuff_t *tvb, const gint offset, int len, gint *next_offset, const gboolean desegment)
  2163. {
  2164. gint eob_offset;
  2165. gint eol_offset;
  2166. int linelen;
  2167. guchar found_needle = 0;
  2168. if (len == -1)
  2169. len = tvb_length_remaining(tvb, offset);
  2170. /*
  2171. * XXX - what if "len" is still -1, meaning "offset is past the
  2172. * end of the tvbuff"?
  2173. */
  2174. eob_offset = offset + len;
  2175. /*
  2176. * Look either for a CR or an LF.
  2177. */
  2178. eol_offset = tvb_pbrk_guint8(tvb, offset, len, "\r\n", &found_needle);
  2179. if (eol_offset == -1) {
  2180. /*
  2181. * No CR or LF - line is presumably continued in next packet.
  2182. */
  2183. if (desegment) {
  2184. /*
  2185. * Tell our caller we saw no EOL, so they can
  2186. * try to desegment and get the entire line
  2187. * into one tvbuff.
  2188. */
  2189. return -1;
  2190. } else {
  2191. /*
  2192. * Pretend the line runs to the end of the tvbuff.
  2193. */
  2194. linelen = eob_offset - offset;
  2195. if (next_offset)
  2196. *next_offset = eob_offset;
  2197. }
  2198. } else {
  2199. /*
  2200. * Find the number of bytes between the starting offset
  2201. * and the CR or LF.
  2202. */
  2203. linelen = eol_offset - offset;
  2204. /*
  2205. * Is it a CR?
  2206. */
  2207. if (found_needle == '\r') {
  2208. /*
  2209. * Yes - is it followed by an LF?
  2210. */
  2211. if (eol_offset + 1 >= eob_offset) {
  2212. /*
  2213. * Dunno - the next byte isn't in this
  2214. * tvbuff.
  2215. */
  2216. if (desegment) {
  2217. /*
  2218. * We'll return -1, although that
  2219. * runs the risk that if the line
  2220. * really *is* terminated with a CR,
  2221. * we won't properly dissect this
  2222. * tvbuff.
  2223. *
  2224. * It's probably more likely that
  2225. * the line ends with CR-LF than
  2226. * that it ends with CR by itself.
  2227. */
  2228. return -1;
  2229. }
  2230. } else {
  2231. /*
  2232. * Well, we can at least look at the next
  2233. * byte.
  2234. */
  2235. if (tvb_get_guint8(tvb, eol_offset + 1) == '\n') {
  2236. /*
  2237. * It's an LF; skip over the CR.
  2238. */
  2239. eol_offset++;
  2240. }
  2241. }
  2242. }
  2243. /*
  2244. * Return the offset of the character after the last
  2245. * character in the line, skipping over the last character
  2246. * in the line terminator.
  2247. */
  2248. if (next_offset)
  2249. *next_offset = eol_offset + 1;
  2250. }
  2251. return linelen;
  2252. }
  2253. /*
  2254. * Given a tvbuff, an offset into the tvbuff, and a length that starts
  2255. * at that offset (which may be -1 for "all the way to the end of the
  2256. * tvbuff"), find the end of the (putative) line that starts at the
  2257. * specified offset in the tvbuff, going no further than the specified
  2258. * length.
  2259. *
  2260. * However, treat quoted strings inside the buffer specially - don't
  2261. * treat newlines in quoted strings as line terminators.
  2262. *
  2263. * Return the length of the line (not counting the line terminator at
  2264. * the end), or the amount of data remaining in the buffer if we don't
  2265. * find a line terminator.
  2266. *
  2267. * Set "*next_offset" to the offset of the character past the line
  2268. * terminator, or past the end of the buffer if we don't find a line
  2269. * terminator.
  2270. */
  2271. gint
  2272. tvb_find_line_end_unquoted(tvbuff_t *tvb, const gint offset, int len, gint *next_offset)
  2273. {
  2274. gint cur_offset, char_offset;
  2275. gboolean is_quoted;
  2276. guchar c = 0;
  2277. gint eob_offset;
  2278. int linelen;
  2279. if (len == -1)
  2280. len = tvb_length_remaining(tvb, offset);
  2281. /*
  2282. * XXX - what if "len" is still -1, meaning "offset is past the
  2283. * end of the tvbuff"?
  2284. */
  2285. eob_offset = offset + len;
  2286. cur_offset = offset;
  2287. is_quoted = FALSE;
  2288. for (;;) {
  2289. /*
  2290. * Is this part of the string quoted?
  2291. */
  2292. if (is_quoted) {
  2293. /*
  2294. * Yes - look only for the terminating quote.
  2295. */
  2296. char_offset = tvb_find_guint8(tvb, cur_offset, len,
  2297. '"');
  2298. } else {
  2299. /*
  2300. * Look either for a CR, an LF, or a '"'.
  2301. */
  2302. char_offset = tvb_pbrk_guint8(tvb, cur_offset, len, "\r\n\"", &c);
  2303. }
  2304. if (char_offset == -1) {
  2305. /*
  2306. * Not found - line is presumably continued in
  2307. * next packet.
  2308. * We pretend the line runs to the end of the tvbuff.
  2309. */
  2310. linelen = eob_offset - offset;
  2311. if (next_offset)
  2312. *next_offset = eob_offset;
  2313. break;
  2314. }
  2315. if (is_quoted) {
  2316. /*
  2317. * We're processing a quoted string.
  2318. * We only looked for ", so we know it's a ";
  2319. * as we're processing a quoted string, it's a
  2320. * closing quote.
  2321. */
  2322. is_quoted = FALSE;
  2323. } else {
  2324. /*
  2325. * OK, what is it?
  2326. */
  2327. if (c == '"') {
  2328. /*
  2329. * Un-quoted "; it begins a quoted
  2330. * string.
  2331. */
  2332. is_quoted = TRUE;
  2333. } else {
  2334. /*
  2335. * It's a CR or LF; we've found a line
  2336. * terminator.
  2337. *
  2338. * Find the number of bytes between the
  2339. * starting offset and the CR or LF.
  2340. */
  2341. linelen = char_offset - offset;
  2342. /*
  2343. * Is it a CR?
  2344. */
  2345. if (c == '\r') {
  2346. /*
  2347. * Yes; is it followed by an LF?
  2348. */
  2349. if (char_offset + 1 < eob_offset &&
  2350. tvb_get_guint8(tvb, char_offset + 1)
  2351. == '\n') {
  2352. /*
  2353. * Yes; skip over the CR.
  2354. */
  2355. char_offset++;
  2356. }
  2357. }
  2358. /*
  2359. * Return the offset of the character after
  2360. * the last character in the line, skipping
  2361. * over the last character in the line
  2362. * terminator, and quit.
  2363. */
  2364. if (next_offset)
  2365. *next_offset = char_offset + 1;
  2366. break;
  2367. }
  2368. }
  2369. /*
  2370. * Step past the character we found.
  2371. */
  2372. cur_offset = char_offset + 1;
  2373. if (cur_offset >= eob_offset) {
  2374. /*
  2375. * The character we found was the last character
  2376. * in the tvbuff - line is presumably continued in
  2377. * next packet.
  2378. * We pretend the line runs to the end of the tvbuff.
  2379. */
  2380. linelen = eob_offset - offset;
  2381. if (next_offset)
  2382. *next_offset = eob_offset;
  2383. break;
  2384. }
  2385. }
  2386. return linelen;
  2387. }
  2388. /*
  2389. * Copied from the mgcp dissector. (This function should be moved to /epan )
  2390. * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
  2391. * character following offset or offset + maxlength -1 whichever
  2392. * is smaller.
  2393. *
  2394. * Parameters:
  2395. * tvb - The tvbuff in which we are skipping whitespace.
  2396. * offset - The offset in tvb from which we begin trying to skip whitespace.
  2397. * maxlength - The maximum distance from offset that we may try to skip
  2398. * whitespace.
  2399. *
  2400. * Returns: The position in tvb of the first non-whitespace
  2401. * character following offset or offset + maxlength -1 whichever
  2402. * is smaller.
  2403. */
  2404. gint
  2405. tvb_skip_wsp(tvbuff_t *tvb, const gint offset, const gint maxlength)
  2406. {
  2407. gint counter = offset;
  2408. gint end, tvb_len;
  2409. guint8 tempchar;
  2410. /* Get the length remaining */
  2411. tvb_len = tvb_length(tvb);
  2412. end = offset + maxlength;
  2413. if (end >= tvb_len)
  2414. {
  2415. end = tvb_len;
  2416. }
  2417. /* Skip past spaces, tabs, CRs and LFs until run out or meet something else */
  2418. for (counter = offset;
  2419. counter < end &&
  2420. ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
  2421. tempchar == '\t' || tempchar == '\r' || tempchar == '\n');
  2422. counter++);
  2423. return (counter);
  2424. }
  2425. gint
  2426. tvb_skip_wsp_return(tvbuff_t *tvb, const gint offset) {
  2427. gint counter = offset;
  2428. guint8 tempchar;
  2429. for(counter = offset; counter > 0 &&
  2430. ((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
  2431. tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
  2432. counter++;
  2433. return (counter);
  2434. }
  2435. /*
  2436. * Format a bunch of data from a tvbuff as bytes, returning a pointer
  2437. * to the string with the formatted data, with "punct" as a byte
  2438. * separator.
  2439. */
  2440. gchar *
  2441. tvb_bytes_to_str_punct(tvbuff_t *tvb, const gint offset, const gint len, const gchar punct)
  2442. {
  2443. return bytes_to_str_punct(ensure_contiguous(tvb, offset, len), len, punct);
  2444. }
  2445. /*
  2446. * Given a tvbuff, an offset into the tvbuff, and a length that starts
  2447. * at that offset (which may be -1 for "all the way to the end of the
  2448. * tvbuff"), fetch BCD encoded digits from a tvbuff starting from either
  2449. * the low or high half byte, formating the digits according to an input digit set,
  2450. * if NUll a default digit set of 0-9 returning "?" for overdecadic digits will be used.
  2451. * A pointer to the EP allocated string will be returned.
  2452. * Note a tvbuff content of 0xf is considered a 'filler' and will end the conversion.
  2453. */
  2454. static dgt_set_t Dgt1_9_bcd = {
  2455. {
  2456. /* 0 1 2 3 4 5 6 7 8 9 a b c d e */
  2457. '0','1','2','3','4','5','6','7','8','9','?','?','?','?','?'
  2458. }
  2459. };
  2460. const gchar *
  2461. tvb_bcd_dig_to_ep_str(tvbuff_t *tvb, const gint offset, const gint len, dgt_set_t *dgt, gboolean skip_first)
  2462. {
  2463. int length;
  2464. guint8 octet;
  2465. int i = 0;
  2466. char *digit_str;
  2467. gint t_offset = offset;
  2468. if (!dgt)
  2469. dgt = &Dgt1_9_bcd;
  2470. if (len == -1) {
  2471. length = tvb_length(tvb);
  2472. if (length < offset) {
  2473. return "";
  2474. }
  2475. } else {
  2476. length = offset + len;
  2477. }
  2478. digit_str = (char *)ep_alloc((length - offset)*2+1);
  2479. while (t_offset < length) {
  2480. octet = tvb_get_guint8(tvb,t_offset);
  2481. if (!skip_first) {
  2482. digit_str[i] = dgt->out[octet & 0x0f];
  2483. i++;
  2484. }
  2485. skip_first = FALSE;
  2486. /*
  2487. * unpack second value in byte
  2488. */
  2489. octet = octet >> 4;
  2490. if (octet == 0x0f) /* odd number bytes - hit filler */
  2491. break;
  2492. digit_str[i] = dgt->out[octet & 0x0f];
  2493. i++;
  2494. t_offset++;
  2495. }
  2496. digit_str[i]= '\0';
  2497. return digit_str;
  2498. }
  2499. /*
  2500. * Format a bunch of data from a tvbuff as bytes, returning a pointer
  2501. * to the string with the formatted data.
  2502. */
  2503. gchar *
  2504. tvb_bytes_to_str(tvbuff_t *tvb, const gint offset, const gint len)
  2505. {
  2506. return bytes_to_str(ensure_contiguous(tvb, offset, len), len);
  2507. }
  2508. /* Find a needle tvbuff within a haystack tvbuff. */
  2509. gint
  2510. tvb_find_tvb(tvbuff_t *haystack_tvb, tvbuff_t *needle_tvb, const gint haystack_offset)
  2511. {
  2512. guint haystack_abs_offset, haystack_abs_length;
  2513. const guint8 *haystack_data;
  2514. const guint8 *needle_data;
  2515. const guint needle_len = needle_tvb->length;
  2516. const guint8 *location;
  2517. DISSECTOR_ASSERT(haystack_tvb && haystack_tvb->initialized);
  2518. if (haystack_tvb->length < 1 || needle_tvb->length < 1) {
  2519. return -1;
  2520. }
  2521. /* Get pointers to the tvbuffs' data. */
  2522. haystack_data = ensure_contiguous(haystack_tvb, 0, -1);
  2523. needle_data = ensure_contiguous(needle_tvb, 0, -1);
  2524. check_offset_length(haystack_tvb, haystack_offset, -1,
  2525. &haystack_abs_offset, &haystack_abs_length);
  2526. location = epan_memmem(haystack_data + haystack_abs_offset, haystack_abs_length,
  2527. needle_data, needle_len);
  2528. if (location) {
  2529. return (gint) (location - haystack_data);
  2530. }
  2531. return -1;
  2532. }
  2533. #ifdef HAVE_LIBZ
  2534. /*
  2535. * Uncompresses a zlib compressed packet inside a message of tvb at offset with
  2536. * length comprlen. Returns an uncompressed tvbuffer if uncompression
  2537. * succeeded or NULL if uncompression failed.
  2538. */
  2539. #define TVB_Z_MIN_BUFSIZ 32768
  2540. #define TVB_Z_MAX_BUFSIZ 1048576 * 10
  2541. /* #define TVB_Z_DEBUG 1 */
  2542. #undef TVB_Z_DEBUG
  2543. tvbuff_t *
  2544. tvb_uncompress(tvbuff_t *tvb, const int offset, int comprlen)
  2545. {
  2546. gint err = Z_OK;
  2547. guint bytes_out = 0;
  2548. guint8 *compr = NULL;
  2549. guint8 *uncompr = NULL;
  2550. tvbuff_t *uncompr_tvb = NULL;
  2551. z_streamp strm = NULL;
  2552. Bytef *strmbuf = NULL;
  2553. guint inits_done = 0;
  2554. gint wbits = MAX_WBITS;
  2555. guint8 *next = NULL;
  2556. guint bufsiz = TVB_Z_MIN_BUFSIZ;
  2557. #ifdef TVB_Z_DEBUG
  2558. guint inflate_passes = 0;
  2559. guint bytes_in = tvb_length_remaining(tvb, offset);
  2560. #endif
  2561. if (tvb == NULL) {
  2562. return NULL;
  2563. }
  2564. compr = (guint8 *)tvb_memdup(tvb, offset, comprlen);
  2565. if (!compr)
  2566. return NULL;
  2567. /*
  2568. * Assume that the uncompressed data is at least twice as big as
  2569. * the compressed size.
  2570. */
  2571. bufsiz = tvb_length_remaining(tvb, offset) * 2;
  2572. bufsiz = CLAMP(bufsiz, TVB_Z_MIN_BUFSIZ, TVB_Z_MAX_BUFSIZ);
  2573. #ifdef TVB_Z_DEBUG
  2574. printf("bufsiz: %u bytes\n", bufsiz);
  2575. #endif
  2576. next = compr;
  2577. strm = g_new0(z_stream, 1);
  2578. strm->next_in = next;
  2579. strm->avail_in = comprlen;
  2580. strmbuf = (Bytef *)g_malloc0(bufsiz);
  2581. strm->next_out = strmbuf;
  2582. strm->avail_out = bufsiz;
  2583. err = inflateInit2(strm, wbits);
  2584. inits_done = 1;
  2585. if (err != Z_OK) {
  2586. inflateEnd(strm);
  2587. g_free(strm);
  2588. g_free(compr);
  2589. g_free(strmbuf);
  2590. return NULL;
  2591. }
  2592. while (1) {
  2593. memset(strmbuf, '\0', bufsiz);
  2594. strm->next_out = strmbuf;
  2595. strm->avail_out = bufsiz;
  2596. err = inflate(strm, Z_SYNC_FLUSH);
  2597. if (err == Z_OK || err == Z_STREAM_END) {
  2598. guint bytes_pass = bufsiz - strm->avail_out;
  2599. #ifdef TVB_Z_DEBUG
  2600. ++inflate_passes;
  2601. #endif
  2602. if (uncompr == NULL) {
  2603. /*
  2604. * This is ugly workaround for bug #6480
  2605. * (https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=6480)
  2606. *
  2607. * g_memdup(..., 0) returns NULL (g_malloc(0) also)
  2608. * when uncompr is NULL logic below doesn't create tvb
  2609. * which is later interpreted as decompression failed.
  2610. */
  2611. uncompr = (guint8 *)((bytes_pass || err != Z_STREAM_END) ?
  2612. g_memdup(strmbuf, bytes_pass) :
  2613. g_strdup(""));
  2614. } else {
  2615. guint8 *new_data = (guint8 *)g_malloc0(bytes_out + bytes_pass);
  2616. memcpy(new_data, uncompr, bytes_out);
  2617. memcpy(new_data + bytes_out, strmbuf, bytes_pass);
  2618. g_free(uncompr);
  2619. uncompr = new_data;
  2620. }
  2621. bytes_out += bytes_pass;
  2622. if (err == Z_STREAM_END) {
  2623. inflateEnd(strm);
  2624. g_free(strm);
  2625. g_free(strmbuf);
  2626. break;
  2627. }
  2628. } else if (err == Z_BUF_ERROR) {
  2629. /*
  2630. * It's possible that not enough frames were captured
  2631. * to decompress this fully, so return what we've done
  2632. * so far, if any.
  2633. */
  2634. inflateEnd(strm);
  2635. g_free(strm);
  2636. g_free(strmbuf);
  2637. if (uncompr != NULL) {
  2638. break;
  2639. } else {
  2640. g_free(compr);
  2641. return NULL;
  2642. }
  2643. } else if (err == Z_DATA_ERROR && inits_done == 1
  2644. && uncompr == NULL && (*compr == 0x1f) &&
  2645. (*(compr + 1) == 0x8b)) {
  2646. /*
  2647. * inflate() is supposed to handle both gzip and deflate
  2648. * streams automatically, but in reality it doesn't
  2649. * seem to handle either (at least not within the
  2650. * context of an HTTP response.) We have to try
  2651. * several tweaks, depending on the type of data and
  2652. * version of the library installed.
  2653. */
  2654. /*
  2655. * Gzip file format. Skip past the header, since the
  2656. * fix to make it work (setting windowBits to 31)
  2657. * doesn't work with all versions of the library.
  2658. */
  2659. Bytef *c = compr + 2;
  2660. Bytef flags = 0;
  2661. if (*c == Z_DEFLATED) {
  2662. c++;
  2663. } else {
  2664. inflateEnd(strm);
  2665. g_free(strm);
  2666. g_free(compr);
  2667. g_free(strmbuf);
  2668. return NULL;
  2669. }
  2670. flags = *c;
  2671. /* Skip past the MTIME, XFL, and OS fields. */
  2672. c += 7;
  2673. if (flags & (1 << 2)) {
  2674. /* An Extra field is present. */
  2675. gint xsize = (gint)(*c |
  2676. (*(c + 1) << 8));
  2677. c += xsize;
  2678. }
  2679. if (flags & (1 << 3)) {
  2680. /* A null terminated filename */
  2681. while ((c - compr) < comprlen && *c != '\0') {
  2682. c++;
  2683. }
  2684. c++;
  2685. }
  2686. if (flags & (1 << 4)) {
  2687. /* A null terminated comment */
  2688. while ((c - compr) < comprlen && *c != '\0') {
  2689. c++;
  2690. }
  2691. c++;
  2692. }
  2693. inflateReset(strm);
  2694. next = c;
  2695. strm->next_in = next;
  2696. if (c - compr > comprlen) {
  2697. inflateEnd(strm);
  2698. g_free(strm);
  2699. g_free(compr);
  2700. g_free(strmbuf);
  2701. return NULL;
  2702. }
  2703. comprlen -= (int) (c - compr);
  2704. inflateEnd(strm);
  2705. inflateInit2(strm, wbits);
  2706. inits_done++;
  2707. } else if (err == Z_DATA_ERROR && uncompr == NULL &&
  2708. inits_done <= 3) {
  2709. /*
  2710. * Re-init the stream with a negative
  2711. * MAX_WBITS. This is necessary due to
  2712. * some servers (Apache) not sending
  2713. * the deflate header with the
  2714. * content-encoded response.
  2715. */
  2716. wbits = -MAX_WBITS;
  2717. inflateReset(strm);
  2718. strm->next_in = next;
  2719. strm->avail_in = comprlen;
  2720. inflateEnd(strm);
  2721. memset(strmbuf, '\0', bufsiz);
  2722. strm->next_out = strmbuf;
  2723. strm->avail_out = bufsiz;
  2724. err = inflateInit2(strm, wbits);
  2725. inits_done++;
  2726. if (err != Z_OK) {
  2727. g_free(strm);
  2728. g_free(strmbuf);
  2729. g_free(compr);
  2730. g_free(uncompr);
  2731. return NULL;
  2732. }
  2733. } else {
  2734. inflateEnd(strm);
  2735. g_free(strm);
  2736. g_free(strmbuf);
  2737. if (uncompr == NULL) {
  2738. g_free(compr);
  2739. return NULL;
  2740. }
  2741. break;
  2742. }
  2743. }
  2744. #ifdef TVB_Z_DEBUG
  2745. printf("inflate() total passes: %u\n", inflate_passes);
  2746. printf("bytes in: %u\nbytes out: %u\n\n", bytes_in, bytes_out);
  2747. #endif
  2748. if (uncompr != NULL) {
  2749. uncompr_tvb = tvb_new_real_data((guint8*) uncompr, bytes_out, bytes_out);
  2750. tvb_set_free_cb(uncompr_tvb, g_free);
  2751. }
  2752. g_free(compr);
  2753. return uncompr_tvb;
  2754. }
  2755. #else
  2756. tvbuff_t *
  2757. tvb_uncompress(tvbuff_t *tvb _U_, const int offset _U_, int comprlen _U_)
  2758. {
  2759. return NULL;
  2760. }
  2761. #endif
  2762. tvbuff_t *
  2763. tvb_child_uncompress(tvbuff_t *parent, tvbuff_t *tvb, const int offset, int comprlen)
  2764. {
  2765. tvbuff_t *new_tvb = tvb_uncompress(tvb, offset, comprlen);
  2766. if (new_tvb)
  2767. tvb_set_child_real_data_tvbuff (parent, new_tvb);
  2768. return new_tvb;
  2769. }
  2770. gint
  2771. tvb_raw_offset(tvbuff_t *tvb)
  2772. {
  2773. return ((tvb->raw_offset==-1)?(tvb->raw_offset = tvb_offset_from_real_beginning(tvb)):tvb->raw_offset);
  2774. }
  2775. void
  2776. tvb_set_fragment(tvbuff_t *tvb)
  2777. {
  2778. tvb->flags |= TVBUFF_FRAGMENT;
  2779. }
  2780. struct tvbuff *
  2781. tvb_get_ds_tvb(tvbuff_t *tvb)
  2782. {
  2783. return(tvb->ds_tvb);
  2784. }