pcap-util.c:EXTRACT_BE_U_2:
   78|  5.39k|{
   79|       |	return ((uint16_t)ntohs(*(const uint16_t *)(p)));
   80|  5.39k|}

pcap_freecode:
 1620|  4.61k|{
 1621|  4.61k|	program->bf_len = 0;
 1622|  4.61k|	if (program->bf_insns != NULL) {
  ------------------
  |  Branch (1622:6): [True: 0, False: 4.61k]
  ------------------
 1623|      0|		free(program->bf_insns);
 1624|       |		program->bf_insns = NULL;
 1625|      0|	}
 1626|  4.61k|}

linktype_to_dlt:
 1502|  4.61k|{
 1503|       |	/*
 1504|       |	 * All values in the low matching range were handed out before
 1505|       |	 * assigning DLT_* codes became a free-for-all, so they're the
 1506|       |	 * same on all platforms, and are thus used as the LINKTYPE_*
 1507|       |	 * codes in capture files.
 1508|       |	 */
 1509|  4.61k|	if (linktype >= LINKTYPE_LOW_MATCHING_MIN &&
  ------------------
  |  |  110|  9.22k|#define LINKTYPE_LOW_MATCHING_MIN	0		/* lowest value in this "matching" range */
  ------------------
  |  Branch (1509:6): [True: 4.61k, False: 0]
  ------------------
 1510|  4.61k|	    linktype <= LINKTYPE_LOW_MATCHING_MAX)
  ------------------
  |  |  123|  4.61k|#define LINKTYPE_LOW_MATCHING_MAX	LINKTYPE_FDDI	/* highest value in this "matching" range */
  |  |  ------------------
  |  |  |  |  121|  4.61k|#define LINKTYPE_FDDI		DLT_FDDI
  |  |  |  |  ------------------
  |  |  |  |  |  |   77|  4.61k|#define DLT_FDDI	10	/* FDDI */
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  |  Branch (1510:6): [True: 732, False: 3.88k]
  ------------------
 1511|    732|		return (linktype);
 1512|       |
 1513|       |#if LINKTYPE_PFSYNC != DLT_PFSYNC
 1514|       |	/*
 1515|       |	 * DLT_PFSYNC has a code on several platforms that's in the
 1516|       |	 * non-matching range, a code on FreeBSD that's in the high
 1517|       |	 * matching range and that's *not* equal to LINKTYPE_PFSYNC,
 1518|       |	 * and has a code on the rmaining platforms that's equal
 1519|       |	 * to LINKTYPE_PFSYNC, which is in the high matching range.
 1520|       |	 *
 1521|       |	 * Map LINKTYPE_PFSYNC to whatever DLT_PFSYNC is on this
 1522|       |	 * platform, if the two aren't equal.
 1523|       |	 */
 1524|       |	if (linktype == LINKTYPE_PFSYNC)
 1525|       |		return (DLT_PFSYNC);
 1526|       |#endif
 1527|       |
 1528|       |	/*
 1529|       |	 * DLT_PKTAP is defined as DLT_USER2 - which is in the high
 1530|       |	 * matching range - on Darwin because Apple used DLT_USER2
 1531|       |	 * on systems that users ran, not just as an internal thing.
 1532|       |	 *
 1533|       |	 * We map LINKTYPE_PKTAP to the platform's DLT_PKTAP for
 1534|       |	 * the benefit of software that's expecting DLT_PKTAP
 1535|       |	 * (even if that's DLT_USER2) for an Apple PKTAP capture.
 1536|       |	 *
 1537|       |	 * (Yes, this is an annoyance if you want to read a
 1538|       |	 * LINKTYPE_USER2 packet as something other than DLT_PKTAP
 1539|       |	 * on a Darwin-based OS, as, on that OS, DLT_PKTAP and DLT_USER2
 1540|       |	 * are the same.  Feel free to complain to Apple about this.)
 1541|       |	 */
 1542|       |#if LINKTYPE_PKTAP != DLT_PKTAP
 1543|       |	if (linktype == LINKTYPE_PKTAP)
 1544|       |		return (DLT_PKTAP);
 1545|       |#endif
 1546|       |
 1547|       |	/*
 1548|       |	 * These DLT_* codes have different values on different
 1549|       |	 * platforms, so we assigned them LINKTYPE_* codes just
 1550|       |	 * below the lower bound of the high matching range;
 1551|       |	 * those values should never be equal to any DLT_*
 1552|       |	 * code, so that should avoid collisions.
 1553|       |	 *
 1554|       |	 * That way, for example, "raw IP" packets will have
 1555|       |	 * LINKTYPE_RAW as the code in all savefiles for
 1556|       |	 * which the code that writes them maps to that
 1557|       |	 * value, regardless of the platform on which they
 1558|       |	 * were written, so they should be readable on all
 1559|       |	 * platforms without having to determine on which
 1560|       |	 * platform they were written.
 1561|       |	 *
 1562|       |	 * We map the LINKTYPE_* codes to the corresponding
 1563|       |	 * DLT_* code on this platform.
 1564|       |	 */
 1565|  3.88k|	if (linktype == LINKTYPE_ATM_RFC1483)
  ------------------
  |  |  152|  3.88k|#define LINKTYPE_ATM_RFC1483	100		/* LLC/SNAP-encapsulated ATM */
  ------------------
  |  Branch (1565:6): [True: 1, False: 3.88k]
  ------------------
 1566|      1|		return (DLT_ATM_RFC1483);
  ------------------
  |  |  109|      1|#define DLT_ATM_RFC1483	11	/* LLC-encapsulated ATM */
  ------------------
 1567|  3.88k|	if (linktype == LINKTYPE_RAW)
  ------------------
  |  |  153|  3.88k|#define LINKTYPE_RAW		101		/* raw IP */
  ------------------
  |  Branch (1567:6): [True: 199, False: 3.68k]
  ------------------
 1568|    199|		return (DLT_RAW);
  ------------------
  |  |  114|    199|#define DLT_RAW		12	/* raw IP */
  ------------------
 1569|  3.68k|	if (linktype == LINKTYPE_SLIP_BSDOS)
  ------------------
  |  |  154|  3.68k|#define LINKTYPE_SLIP_BSDOS	102		/* BSD/OS SLIP BPF header */
  ------------------
  |  Branch (1569:6): [True: 3, False: 3.67k]
  ------------------
 1570|      3|		return (DLT_SLIP_BSDOS);
  ------------------
  |  |  130|      3|#define DLT_SLIP_BSDOS	15	/* BSD/OS Serial Line IP */
  ------------------
 1571|  3.67k|	if (linktype == LINKTYPE_PPP_BSDOS)
  ------------------
  |  |  155|  3.67k|#define LINKTYPE_PPP_BSDOS	103		/* BSD/OS PPP BPF header */
  ------------------
  |  Branch (1571:6): [True: 3, False: 3.67k]
  ------------------
 1572|      3|		return (DLT_PPP_BSDOS);
  ------------------
  |  |  131|      3|#define DLT_PPP_BSDOS	16	/* BSD/OS Point-to-point Protocol */
  ------------------
 1573|       |
 1574|       |	/*
 1575|       |	 * These DLT_* codes were originally defined on some platform,
 1576|       |	 * and weren't defined on other platforms.
 1577|       |	 *
 1578|       |	 * At least some of them have values, on at least one platform,
 1579|       |	 * that collide with other DLT_* codes on other platforms, e.g.
 1580|       |	 * DLT_LOOP, so we don't just define them, on all platforms,
 1581|       |	 * as having the same value as on the original platform.
 1582|       |	 *
 1583|       |	 * Therefore, we assigned new LINKTYPE_* codes to them, and,
 1584|       |	 * on the platforms where they weren't originally defined,
 1585|       |	 * define the DLT_* codes to have the same value as the
 1586|       |	 * corresponding LINKTYPE_* codes.
 1587|       |	 *
 1588|       |	 * This means that, for capture files with the original
 1589|       |	 * platform's DLT_* code rather than the LINKTYPE_* code
 1590|       |	 * as a link-layer type, we will recognize those types
 1591|       |	 * on that platform, but not on other platforms.
 1592|       |	 *
 1593|       |	 * We map the LINKTYPE_* codes to the corresponding
 1594|       |	 * DLT_* code on platforms where the two codes differ..
 1595|       |	 */
 1596|       |#ifdef DLT_FR
 1597|       |	/* BSD/OS Frame Relay */
 1598|       |	if (linktype == LINKTYPE_FRELAY)
 1599|       |		return (DLT_FR);
 1600|       |#endif
 1601|       |#if LINKTYPE_NETBSD_HDLC != DLT_HDLC
 1602|       |	/* NetBSD HDLC */
 1603|       |	if (linktype == LINKTYPE_NETBSD_HDLC)
 1604|       |		return (DLT_HDLC);
 1605|       |#endif
 1606|       |#if LINKTYPE_C_HDLC != DLT_C_HDLC
 1607|       |	/* BSD/OS Cisco HDLC */
 1608|       |	if (linktype == LINKTYPE_C_HDLC)
 1609|       |		return (DLT_C_HDLC);
 1610|       |#endif
 1611|       |#if LINKTYPE_LOOP != DLT_LOOP
 1612|       |	/* OpenBSD DLT_LOOP */
 1613|       |	if (linktype == LINKTYPE_LOOP)
 1614|       |		return (DLT_LOOP);
 1615|       |#endif
 1616|       |#if LINKTYPE_ENC != DLT_ENC
 1617|       |	/* OpenBSD DLT_ENC */
 1618|       |	if (linktype == LINKTYPE_ENC)
 1619|       |		return (DLT_ENC);
 1620|       |#endif
 1621|       |
 1622|       |	/*
 1623|       |	 * These DLT_* codes are not on all platforms, but, so far,
 1624|       |	 * there don't appear to be any platforms that define
 1625|       |	 * other codes with those values; we map them to
 1626|       |	 * different LINKTYPE_* values anyway, just in case.
 1627|       |	 *
 1628|       |	 * LINKTYPE_ATM_CLIP is a special case.  DLT_ATM_CLIP is
 1629|       |	 * not on all platforms, but, so far, there don't appear
 1630|       |	 * to be any platforms that define it as anything other
 1631|       |	 * than 19; we define LINKTYPE_ATM_CLIP as something
 1632|       |	 * other than 19, just in case.  That value is in the
 1633|       |	 * high matching range, so we have to check for it.
 1634|       |	 */
 1635|       |	/* Linux ATM Classical IP */
 1636|  3.67k|	if (linktype == LINKTYPE_ATM_CLIP)
  ------------------
  |  |  171|  3.67k|#define LINKTYPE_ATM_CLIP	106		/* Linux Classical IP over ATM */
  ------------------
  |  Branch (1636:6): [True: 2, False: 3.67k]
  ------------------
 1637|      2|		return (DLT_ATM_CLIP);
  ------------------
  |  |  229|      2|#define DLT_ATM_CLIP	19	/* Linux Classical IP over ATM */
  ------------------
 1638|       |
 1639|       |	/*
 1640|       |	 * For all other values, return the linktype code as the
 1641|       |	 * DLT_* code.
 1642|       |	 *
 1643|       |	 * If the code is in the high matching range, the
 1644|       |	 * DLT_* code is the same as the LINKTYPE_* code.
 1645|       |	 *
 1646|       |	 * If the code is greater than the maximum value in
 1647|       |	 * the high matching range, it may be a value from
 1648|       |	 * a newer version of libpcap; we provide it in case
 1649|       |	 * the program' capable of handling it.
 1650|       |	 *
 1651|       |	 * If the code is less than the minimum value in the
 1652|       |	 * high matching range, it might be from a capture
 1653|       |	 * written by code that doesn't map non-matching range
 1654|       |	 * DLT_* codes to the appropriate LINKTYPE_* code, so
 1655|       |	 * we'll just pass it through, so that *if it was written
 1656|       |	 * on this platform* it will be interpreted correctly.
 1657|       |	 * (We don't know whether it was written on this platform,
 1658|       |	 * but at least this way there's *some* chance that it
 1659|       |	 * can be read.)
 1660|       |	 */
 1661|  3.67k|	return linktype;
 1662|  3.67k|}
max_snaplen_for_dlt:
 1683|  44.8k|{
 1684|  44.8k|	switch (dlt) {
 1685|       |
 1686|    580|	case DLT_DBUS:
  ------------------
  |  | 1179|    580|#define DLT_DBUS		231
  ------------------
  |  Branch (1686:2): [True: 580, False: 44.2k]
  ------------------
 1687|    580|		return 128*1024*1024;
 1688|       |
 1689|    245|	case DLT_EBHSCR:
  ------------------
  |  | 1558|    245|#define DLT_EBHSCR	        279
  ------------------
  |  Branch (1689:2): [True: 245, False: 44.6k]
  ------------------
 1690|    245|		return 8*1024*1024;
 1691|       |
 1692|  2.71k|	case DLT_USBPCAP:
  ------------------
  |  | 1323|  2.71k|#define DLT_USBPCAP		249
  ------------------
  |  Branch (1692:2): [True: 2.71k, False: 42.1k]
  ------------------
 1693|  2.71k|		return 1024*1024;
 1694|       |
 1695|  41.3k|	default:
  ------------------
  |  Branch (1695:2): [True: 41.3k, False: 3.54k]
  ------------------
 1696|  41.3k|		return MAXIMUM_SNAPLEN;
  ------------------
  |  |  177|  41.3k|#define MAXIMUM_SNAPLEN		262144
  ------------------
 1697|  44.8k|	}
 1698|  44.8k|}

pcap-util.c:is_isochronous_transfer_completion:
   43|  2.09k|{
   44|  2.09k|	return (hdr->transfer_type == URB_ISOCHRONOUS &&
  ------------------
  |  |   43|  4.19k|#define URB_ISOCHRONOUS   0x0
  ------------------
  |  Branch (44:10): [True: 1.89k, False: 207]
  ------------------
   45|  1.89k|	    hdr->event_type == URB_COMPLETE &&
  ------------------
  |  |   52|  3.98k|#define URB_COMPLETE      'C'
  ------------------
  |  Branch (45:6): [True: 1.67k, False: 221]
  ------------------
   46|  1.67k|	    (hdr->endpoint_number & URB_TRANSFER_IN));
  ------------------
  |  |   42|  1.67k|#define URB_TRANSFER_IN   0x80
  ------------------
  |  Branch (46:6): [True: 1.66k, False: 7]
  ------------------
   47|  2.09k|}
pcap-util.c:iso_pseudo_header_len:
   55|  3.96k|{
   56|  3.96k|	return (sizeof(pcap_usb_header_mmapped) +
   57|  3.96k|	    usb_hdr->ndesc * sizeof (usb_isodesc));
   58|  3.96k|}
pcap-util.c:incoming_isochronous_transfer_completed_len:
   71|    958|{
   72|    958|	const pcap_usb_header_mmapped *hdr;
   73|    958|	u_int bytes_left;
   74|    958|	const usb_isodesc *descs;
   75|    958|	u_int pre_truncation_data_len;
   76|       |
   77|       |	/*
   78|       |	 * All callers of this routine must ensure that pkth->caplen is
   79|       |	 * >= sizeof (pcap_usb_header_mmapped).
   80|       |	 */
   81|    958|	bytes_left = phdr->caplen;
   82|    958|	bytes_left -= sizeof (pcap_usb_header_mmapped);
   83|       |
   84|    958|	hdr = (const pcap_usb_header_mmapped *) bp;
   85|    958|	descs = (const usb_isodesc *) (bp + sizeof(pcap_usb_header_mmapped));
   86|       |
   87|       |	/*
   88|       |	 * Find the end of the last chunk of data in the buffer
   89|       |	 * referred to by the isochronous descriptors; that indicates
   90|       |	 * how far into the buffer the data would have gone.
   91|       |	 *
   92|       |	 * Make sure we don't run past the end of the captured data
   93|       |	 * while processing the isochronous descriptors.
   94|       |	 */
   95|    958|	pre_truncation_data_len = 0;
   96|    958|	for (uint32_t desc = 0;
   97|  4.57k|	    desc < hdr->ndesc && bytes_left >= sizeof (usb_isodesc);
  ------------------
  |  Branch (97:6): [True: 3.62k, False: 958]
  |  Branch (97:27): [True: 3.62k, False: 0]
  ------------------
   98|  3.62k|	    desc++, bytes_left -= sizeof (usb_isodesc)) {
   99|  3.62k|		u_int desc_end;
  100|       |
  101|  3.62k|		if (descs[desc].len != 0) {
  ------------------
  |  Branch (101:7): [True: 2.77k, False: 846]
  ------------------
  102|       |			/*
  103|       |			 * Compute the end offset of the data
  104|       |			 * for this descriptor, i.e. the offset
  105|       |			 * of the byte after the data.  Clamp
  106|       |			 * the sum at UINT_MAX, so that it fits
  107|       |			 * in a u_int.
  108|       |			 */
  109|  2.77k|			desc_end = u_int_sum(descs[desc].offset,
  110|  2.77k|			    descs[desc].len);
  111|  2.77k|			if (desc_end > pre_truncation_data_len)
  ------------------
  |  Branch (111:8): [True: 752, False: 2.02k]
  ------------------
  112|    752|				pre_truncation_data_len = desc_end;
  113|  2.77k|		}
  114|  3.62k|	}
  115|       |
  116|       |	/*
  117|       |	 * Return the sum of the total header length (memory-mapped
  118|       |	 * header and ISO descriptors) and the data length, clamped
  119|       |	 * to UINT_MAX.
  120|       |	 *
  121|       |	 * We've made sure that the number of descriptors is
  122|       |	 * <= USB_MAXDESC, so we know that the total size,
  123|       |	 * in bytes, of the descriptors fits in a 32-bit
  124|       |	 * integer.
  125|       |	 */
  126|    958|	return (u_int_sum(iso_pseudo_header_len(hdr), pre_truncation_data_len));
  127|    958|}
pcap-util.c:u_int_sum:
   34|  3.73k|{
   35|  3.73k|	return (((b) <= UINT_MAX - (a)) ? (a) + (b) : UINT_MAX);
  ------------------
  |  Branch (35:10): [True: 2.42k, False: 1.30k]
  ------------------
   36|  3.73k|}

pcapint_post_process:
  508|  42.8k|{
  509|  42.8k|	if (swapped)
  ------------------
  |  Branch (509:6): [True: 29.2k, False: 13.6k]
  ------------------
  510|  29.2k|		swap_pseudo_headers(linktype, hdr, data);
  511|       |
  512|       |	/*
  513|       |	 * Is this a memory-mapped Linux USB capture?
  514|       |	 */
  515|  42.8k|	if (linktype == DLT_USB_LINUX_MMAPPED) {
  ------------------
  |  | 1042|  42.8k|#define DLT_USB_LINUX_MMAPPED	220
  ------------------
  |  Branch (515:6): [True: 7.99k, False: 34.9k]
  ------------------
  516|       |		/*
  517|       |		 * Yes.
  518|       |		 *
  519|       |		 * In older versions of libpcap, in memory-mapped Linux
  520|       |		 * USB captures, the original length of completion events
  521|       |		 * for incoming isochronous transfers was miscalculated;
  522|       |		 * it needed to be calculated based on the offsets and
  523|       |		 * lengths in the descriptors, not on the raw URB length,
  524|       |		 * but it wasn't.
  525|       |		 *
  526|       |		 * If this packet contains transferred data (yes, data_flag
  527|       |		 * is 0 if we *do* have data), it's a completion event
  528|       |		 * for an incoming isochronous transfer, and the
  529|       |		 * transfer length appears to have been calculated
  530|       |		 * from the raw URB length, fix it.
  531|       |		 *
  532|       |		 * We only do this if we have the full USB pseudo-header,
  533|       |		 * because we will have to look at that header and at
  534|       |		 * all of the isochronous descriptors.
  535|       |		 */
  536|  7.99k|		if (hdr->caplen < sizeof (pcap_usb_header_mmapped)) {
  ------------------
  |  Branch (536:7): [True: 4.81k, False: 3.18k]
  ------------------
  537|       |			/*
  538|       |			 * We don't have the full pseudo-header.
  539|       |			 */
  540|  4.81k|			return;
  541|  4.81k|		}
  542|       |
  543|  3.18k|		const pcap_usb_header_mmapped *usb_hdr =
  544|  3.18k|		    (const pcap_usb_header_mmapped *) data;
  545|       |
  546|       |		/*
  547|       |		 * Make sure the number of descriptors is sane.
  548|       |		 *
  549|       |		 * The Linux binary USB monitor code limits the number of
  550|       |		 * isochronous descriptors to 128; if the number in the file
  551|       |		 * is larger than that, either 1) the file's been damaged
  552|       |		 * or 2) the file was produced after the number was raised
  553|       |		 * in the kernel.
  554|       |		 *
  555|       |		 * In case 1), the number can't be trusted, so don't rely on
  556|       |		 * it to attempt to fix the original length field in the pcap
  557|       |		 * or pcapng header.
  558|       |		 *
  559|       |		 * In case 2), the system was probably running a version of
  560|       |		 * libpcap that didn't miscalculate the original length, so
  561|       |		 * it probably doesn't need to be fixed.
  562|       |		 *
  563|       |		 * This avoids the possibility of the product of the number of
  564|       |		 * descriptors and the size of descriptors won't overflow an
  565|       |		 * unsigned 32-bit integer.
  566|       |		 */
  567|  3.18k|		if (usb_hdr->ndesc > USB_MAXDESC)
  ------------------
  |  |  132|  3.18k|#define USB_MAXDESC	128
  ------------------
  |  Branch (567:7): [True: 777, False: 2.40k]
  ------------------
  568|    777|			return;
  569|       |
  570|  2.40k|		if (!usb_hdr->data_flag &&
  ------------------
  |  Branch (570:7): [True: 2.09k, False: 305]
  ------------------
  571|  2.09k|		    is_isochronous_transfer_completion(usb_hdr) &&
  ------------------
  |  Branch (571:7): [True: 1.66k, False: 435]
  ------------------
  572|  1.66k|		    packet_length_might_be_wrong(hdr, usb_hdr)) {
  ------------------
  |  Branch (572:7): [True: 1.34k, False: 319]
  ------------------
  573|  1.34k|			u_int len;
  574|       |
  575|       |			/*
  576|       |			 * Make sure we have all of the descriptors,
  577|       |			 * as we will have to look at all of them.
  578|       |			 *
  579|       |			 * If not, we don't bother trying to fix
  580|       |			 * anything.
  581|       |			 */
  582|  1.34k|			if (hdr->caplen < iso_pseudo_header_len(usb_hdr))
  ------------------
  |  Branch (582:8): [True: 386, False: 958]
  ------------------
  583|    386|				return;
  584|       |
  585|       |			/*
  586|       |			 * Calculate what the length should have been.
  587|       |			 */
  588|    958|			len = incoming_isochronous_transfer_completed_len(hdr,
  589|    958|			    data);
  590|       |
  591|       |			/*
  592|       |			 * len is the smaller of UINT_MAX and the total
  593|       |			 * header plus data length.  That's guaranteed
  594|       |			 * to fit in a UINT_MAX.
  595|       |			 *
  596|       |			 * Don't reduce the original length to a value
  597|       |			 * below the captured length, however, as that
  598|       |			 * is bogus.
  599|       |			 */
  600|    958|			if (len >= hdr->caplen)
  ------------------
  |  Branch (600:8): [True: 711, False: 247]
  ------------------
  601|    711|				hdr->len = len;
  602|       |
  603|       |			/*
  604|       |			 * If the captured length is greater than the
  605|       |			 * length, use the captured length.
  606|       |			 *
  607|       |			 * For completion events for incoming isochronous
  608|       |			 * transfers, it's based on data_len, which is
  609|       |			 * calculated the same way we calculated
  610|       |			 * pre_truncation_data_len above, except that
  611|       |			 * it has access to all the isochronous descriptors,
  612|       |			 * not just the ones that the kernel were able to
  613|       |			 * provide us or, for a capture file, that weren't
  614|       |			 * sliced off by a snapshot length.
  615|       |			 *
  616|       |			 * However, it might have been reduced by the USB
  617|       |			 * capture mechanism arbitrarily limiting the amount
  618|       |			 * of data it provides to userland, or by the libpcap
  619|       |			 * capture code limiting it to being no more than the
  620|       |			 * snapshot, so we don't want to just use it all the
  621|       |			 * time; we only do so to try to get a better estimate
  622|       |			 * of the actual length - and to make sure the
  623|       |			 * original length is always >= the captured length.
  624|       |			 */
  625|    958|			if (hdr->caplen > hdr->len)
  ------------------
  |  Branch (625:8): [True: 218, False: 740]
  ------------------
  626|    218|				hdr->len = hdr->caplen;
  627|    958|		}
  628|  2.40k|	}
  629|  42.8k|}
pcap-util.c:swap_pseudo_headers:
  453|  29.2k|{
  454|       |	/*
  455|       |	 * Convert pseudo-headers from the byte order of
  456|       |	 * the host on which the file was saved to our
  457|       |	 * byte order, as necessary.
  458|       |	 */
  459|  29.2k|	switch (linktype) {
  ------------------
  |  Branch (459:10): [True: 19.2k, False: 9.92k]
  ------------------
  460|       |
  461|  3.00k|	case DLT_PFLOG:
  ------------------
  |  |  372|  3.00k|#define DLT_PFLOG	117
  ------------------
  |  Branch (461:2): [True: 3.00k, False: 26.2k]
  ------------------
  462|  3.00k|		swap_pflog_header(hdr, data);
  463|  3.00k|		break;
  464|       |
  465|  3.09k|	case DLT_LINUX_SLL:
  ------------------
  |  |  352|  3.09k|#define DLT_LINUX_SLL	113
  ------------------
  |  Branch (465:2): [True: 3.09k, False: 26.1k]
  ------------------
  466|  3.09k|		swap_linux_sll_socketcan_header(hdr, data);
  467|  3.09k|		break;
  468|       |
  469|  3.24k|	case DLT_LINUX_SLL2:
  ------------------
  |  | 1530|  3.24k|#define DLT_LINUX_SLL2	276
  ------------------
  |  Branch (469:2): [True: 3.24k, False: 25.9k]
  ------------------
  470|  3.24k|		swap_linux_sll2_socketcan_header(hdr, data);
  471|  3.24k|		break;
  472|       |
  473|  2.60k|	case DLT_USB_LINUX:
  ------------------
  |  |  805|  2.60k|#define DLT_USB_LINUX		189
  ------------------
  |  Branch (473:2): [True: 2.60k, False: 26.6k]
  ------------------
  474|  2.60k|		swap_linux_usb_header(hdr, data, 0);
  475|  2.60k|		break;
  476|       |
  477|  5.89k|	case DLT_USB_LINUX_MMAPPED:
  ------------------
  |  | 1042|  5.89k|#define DLT_USB_LINUX_MMAPPED	220
  ------------------
  |  Branch (477:2): [True: 5.89k, False: 23.3k]
  ------------------
  478|  5.89k|		swap_linux_usb_header(hdr, data, 1);
  479|  5.89k|		break;
  480|       |
  481|  1.46k|	case DLT_NFLOG:
  ------------------
  |  | 1226|  1.46k|#define DLT_NFLOG		239
  ------------------
  |  Branch (481:2): [True: 1.46k, False: 27.7k]
  ------------------
  482|  1.46k|		swap_nflog_header(hdr, data);
  483|  1.46k|		break;
  484|  29.2k|	}
  485|  29.2k|}
pcap-util.c:swap_pflog_header:
   52|  3.00k|{
   53|  3.00k|	u_int caplen = hdr->caplen;
   54|  3.00k|	u_int length = hdr->len;
   55|  3.00k|	u_int pfloghdr_length;
   56|  3.00k|	struct pfloghdr *pflhdr = (struct pfloghdr *)buf;
   57|       |
   58|  3.00k|	if (caplen < (u_int) (offsetof(struct pfloghdr, uid) + sizeof pflhdr->uid) ||
  ------------------
  |  Branch (58:6): [True: 513, False: 2.49k]
  ------------------
   59|  2.49k|	    length < (u_int) (offsetof(struct pfloghdr, uid) + sizeof pflhdr->uid)) {
  ------------------
  |  Branch (59:6): [True: 200, False: 2.29k]
  ------------------
   60|       |		/* Not enough data to have the uid field */
   61|    713|		return;
   62|    713|	}
   63|       |
   64|  2.29k|	pfloghdr_length = pflhdr->length;
   65|       |
   66|  2.29k|	if (pfloghdr_length < (u_int) (offsetof(struct pfloghdr, uid) + sizeof pflhdr->uid)) {
  ------------------
  |  Branch (66:6): [True: 235, False: 2.05k]
  ------------------
   67|       |		/* Header doesn't include uid field */
   68|    235|		return;
   69|    235|	}
   70|  2.05k|	pflhdr->uid = PCAP_BSWAP_32(pflhdr->uid);
  ------------------
  |  |   99|  2.05k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
   71|       |
   72|  2.05k|	if (caplen < (u_int) (offsetof(struct pfloghdr, pid) + sizeof pflhdr->pid) ||
  ------------------
  |  Branch (72:6): [True: 223, False: 1.83k]
  ------------------
   73|  1.83k|	    length < (u_int) (offsetof(struct pfloghdr, pid) + sizeof pflhdr->pid)) {
  ------------------
  |  Branch (73:6): [True: 198, False: 1.63k]
  ------------------
   74|       |		/* Not enough data to have the pid field */
   75|    421|		return;
   76|    421|	}
   77|  1.63k|	if (pfloghdr_length < (u_int) (offsetof(struct pfloghdr, pid) + sizeof pflhdr->pid)) {
  ------------------
  |  Branch (77:6): [True: 218, False: 1.41k]
  ------------------
   78|       |		/* Header doesn't include pid field */
   79|    218|		return;
   80|    218|	}
   81|  1.41k|	pflhdr->pid = PCAP_BSWAP_32(pflhdr->pid);
  ------------------
  |  |   99|  1.41k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
   82|       |
   83|  1.41k|	if (caplen < (u_int) (offsetof(struct pfloghdr, rule_uid) + sizeof pflhdr->rule_uid) ||
  ------------------
  |  Branch (83:6): [True: 224, False: 1.19k]
  ------------------
   84|  1.19k|	    length < (u_int) (offsetof(struct pfloghdr, rule_uid) + sizeof pflhdr->rule_uid)) {
  ------------------
  |  Branch (84:6): [True: 69, False: 1.12k]
  ------------------
   85|       |		/* Not enough data to have the rule_uid field */
   86|    293|		return;
   87|    293|	}
   88|  1.12k|	if (pfloghdr_length < (u_int) (offsetof(struct pfloghdr, rule_uid) + sizeof pflhdr->rule_uid)) {
  ------------------
  |  Branch (88:6): [True: 203, False: 920]
  ------------------
   89|       |		/* Header doesn't include rule_uid field */
   90|    203|		return;
   91|    203|	}
   92|    920|	pflhdr->rule_uid = PCAP_BSWAP_32(pflhdr->rule_uid);
  ------------------
  |  |   99|    920|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
   93|       |
   94|    920|	if (caplen < (u_int) (offsetof(struct pfloghdr, rule_pid) + sizeof pflhdr->rule_pid) ||
  ------------------
  |  Branch (94:6): [True: 224, False: 696]
  ------------------
   95|    696|	    length < (u_int) (offsetof(struct pfloghdr, rule_pid) + sizeof pflhdr->rule_pid)) {
  ------------------
  |  Branch (95:6): [True: 198, False: 498]
  ------------------
   96|       |		/* Not enough data to have the rule_pid field */
   97|    422|		return;
   98|    422|	}
   99|    498|	if (pfloghdr_length < (u_int) (offsetof(struct pfloghdr, rule_pid) + sizeof pflhdr->rule_pid)) {
  ------------------
  |  Branch (99:6): [True: 246, False: 252]
  ------------------
  100|       |		/* Header doesn't include rule_pid field */
  101|    246|		return;
  102|    246|	}
  103|    252|	pflhdr->rule_pid = PCAP_BSWAP_32(pflhdr->rule_pid);
  ------------------
  |  |   99|    252|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  104|    252|}
pcap-util.c:swap_linux_sll_socketcan_header:
  191|  3.09k|{
  192|  3.09k|	u_int caplen = hdr->caplen;
  193|  3.09k|	u_int length = hdr->len;
  194|  3.09k|	struct sll_header *shdr = (struct sll_header *)buf;
  195|       |
  196|  3.09k|	if (caplen < (u_int) sizeof(struct sll_header) ||
  ------------------
  |  Branch (196:6): [True: 246, False: 2.84k]
  ------------------
  197|  2.84k|	    length < (u_int) sizeof(struct sll_header)) {
  ------------------
  |  Branch (197:6): [True: 205, False: 2.64k]
  ------------------
  198|       |		/* Not enough data to have the protocol field */
  199|    451|		return;
  200|    451|	}
  201|       |
  202|       |	/*
  203|       |	 * Byte-swap what needs to be byte-swapped.
  204|       |	 */
  205|  2.64k|	swap_socketcan_header(EXTRACT_BE_U_2(&shdr->sll_protocol),
  206|  2.64k|	    caplen - (u_int) sizeof(struct sll_header),
  207|  2.64k|	    length - (u_int) sizeof(struct sll_header),
  208|  2.64k|	    buf + sizeof(struct sll_header));
  209|  2.64k|}
pcap-util.c:swap_socketcan_header:
  124|  5.39k|{
  125|  5.39k|	pcap_can_socketcan_hdr *hdrp;
  126|  5.39k|	pcap_can_socketcan_xl_hdr *xl_hdrp;
  127|       |
  128|  5.39k|	switch (protocol) {
  129|       |
  130|    948|	case LINUX_SLL_P_CAN:
  ------------------
  |  |  142|    948|#define LINUX_SLL_P_CAN		0x000C	/* CAN frames, with SocketCAN pseudo-headers */
  ------------------
  |  Branch (130:2): [True: 948, False: 4.45k]
  ------------------
  131|  1.83k|	case LINUX_SLL_P_CANFD:
  ------------------
  |  |  143|  1.83k|#define LINUX_SLL_P_CANFD	0x000D	/* CAN FD frames, with SocketCAN pseudo-headers */
  ------------------
  |  Branch (131:2): [True: 888, False: 4.51k]
  ------------------
  132|       |		/*
  133|       |		 * CAN classic/CAN FD packet; fix up the packet's header
  134|       |		 * by byte-swapping the CAN ID field.
  135|       |		 */
  136|  1.83k|		hdrp = (pcap_can_socketcan_hdr *)buf;
  137|  1.83k|		if (caplen < (u_int) (offsetof(pcap_can_socketcan_hdr, can_id) + sizeof hdrp->can_id) ||
  ------------------
  |  Branch (137:7): [True: 799, False: 1.03k]
  ------------------
  138|  1.25k|		    length < (u_int) (offsetof(pcap_can_socketcan_hdr, can_id) + sizeof hdrp->can_id)) {
  ------------------
  |  Branch (138:7): [True: 456, False: 581]
  ------------------
  139|       |			/* Not enough data to have the can_id field */
  140|  1.25k|			return;
  141|  1.25k|		}
  142|    581|		hdrp->can_id = PCAP_BSWAP_32(hdrp->can_id);
  ------------------
  |  |   99|    581|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  143|    581|		break;
  144|       |
  145|  3.08k|	case LINUX_SLL_P_CANXL:
  ------------------
  |  |  144|  3.08k|#define LINUX_SLL_P_CANXL	0x000E	/* CAN XL frames, with SocketCAN pseudo-headers */
  ------------------
  |  Branch (145:2): [True: 3.08k, False: 2.31k]
  ------------------
  146|       |		/*
  147|       |		 * CAN XL packet; fix up the packet's header by
  148|       |		 * byte-swapping the priority/VCID field, the
  149|       |		 * payload length, and the acceptance field.
  150|       |		 */
  151|  3.08k|		xl_hdrp = (pcap_can_socketcan_xl_hdr *)buf;
  152|  3.08k|		if (caplen < (u_int) (offsetof(pcap_can_socketcan_xl_hdr, priority_vcid) + sizeof xl_hdrp->priority_vcid) ||
  ------------------
  |  Branch (152:7): [True: 400, False: 2.68k]
  ------------------
  153|  2.68k|		    length < (u_int) (offsetof(pcap_can_socketcan_xl_hdr, priority_vcid) + sizeof xl_hdrp->priority_vcid)) {
  ------------------
  |  Branch (153:7): [True: 396, False: 2.28k]
  ------------------
  154|       |			/* Not enough data to have the priority_vcid field */
  155|    796|			return;
  156|    796|		}
  157|  2.28k|		xl_hdrp->priority_vcid = PCAP_BSWAP_32(xl_hdrp->priority_vcid);
  ------------------
  |  |   99|  2.28k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  158|  2.28k|		if (caplen < (u_int) (offsetof(pcap_can_socketcan_xl_hdr, payload_length) + sizeof xl_hdrp->payload_length) ||
  ------------------
  |  Branch (158:7): [True: 467, False: 1.82k]
  ------------------
  159|  1.82k|		    length < (u_int) (offsetof(pcap_can_socketcan_xl_hdr, payload_length) + sizeof xl_hdrp->payload_length)) {
  ------------------
  |  Branch (159:7): [True: 399, False: 1.42k]
  ------------------
  160|       |			/* Not enough data to have the payload_length field */
  161|    866|			return;
  162|    866|		}
  163|  1.42k|		xl_hdrp->payload_length = PCAP_BSWAP_16(xl_hdrp->payload_length);
  ------------------
  |  |  109|  1.42k|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  164|  1.42k|		if (caplen < (u_int) (offsetof(pcap_can_socketcan_xl_hdr, acceptance_field) + sizeof xl_hdrp->acceptance_field) ||
  ------------------
  |  Branch (164:7): [True: 473, False: 948]
  ------------------
  165|    948|		    length < (u_int) (offsetof(pcap_can_socketcan_xl_hdr, acceptance_field) + sizeof xl_hdrp->acceptance_field)) {
  ------------------
  |  Branch (165:7): [True: 395, False: 553]
  ------------------
  166|       |			/* Not enough data to have the acceptance_field field */
  167|    868|			return;
  168|    868|		}
  169|    553|		xl_hdrp->acceptance_field = PCAP_BSWAP_32(xl_hdrp->acceptance_field);
  ------------------
  |  |   99|    553|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  170|    553|		break;
  171|       |
  172|    479|	default:
  ------------------
  |  Branch (172:2): [True: 479, False: 4.91k]
  ------------------
  173|       |		/*
  174|       |		 * Not a CAN packet; nothing to do.
  175|       |		 */
  176|    479|		break;
  177|  5.39k|	}
  178|  5.39k|}
pcap-util.c:swap_linux_sll2_socketcan_header:
  216|  3.24k|{
  217|  3.24k|	u_int caplen = hdr->caplen;
  218|  3.24k|	u_int length = hdr->len;
  219|  3.24k|	struct sll2_header *shdr = (struct sll2_header *)buf;
  220|       |
  221|  3.24k|	if (caplen < (u_int) sizeof(struct sll2_header) ||
  ------------------
  |  Branch (221:6): [True: 283, False: 2.95k]
  ------------------
  222|  2.95k|	    length < (u_int) sizeof(struct sll2_header)) {
  ------------------
  |  Branch (222:6): [True: 200, False: 2.75k]
  ------------------
  223|       |		/* Not enough data to have the protocol field */
  224|    483|		return;
  225|    483|	}
  226|       |
  227|       |	/*
  228|       |	 * Byte-swap what needs to be byte-swapped.
  229|       |	 */
  230|  2.75k|	swap_socketcan_header(EXTRACT_BE_U_2(&shdr->sll2_protocol),
  231|  2.75k|	    caplen - (u_int) sizeof(struct sll2_header),
  232|  2.75k|	    length - (u_int) sizeof(struct sll2_header),
  233|  2.75k|	    buf + sizeof(struct sll2_header));
  234|  2.75k|}
pcap-util.c:swap_linux_usb_header:
  248|  8.49k|{
  249|  8.49k|	pcap_usb_header_mmapped *uhdr = (pcap_usb_header_mmapped *)buf;
  250|  8.49k|	bpf_u_int32 offset = 0;
  251|       |
  252|       |	/*
  253|       |	 * "offset" is the offset *past* the field we're swapping;
  254|       |	 * we skip the field *before* checking to make sure
  255|       |	 * the captured data length includes the entire field.
  256|       |	 */
  257|       |
  258|       |	/*
  259|       |	 * The URB id is a totally opaque value; do we really need to
  260|       |	 * convert it to the reading host's byte order???
  261|       |	 */
  262|  8.49k|	offset += 8;			/* skip past id */
  263|  8.49k|	if (hdr->caplen < offset)
  ------------------
  |  Branch (263:6): [True: 847, False: 7.65k]
  ------------------
  264|    847|		return;
  265|  7.65k|	uhdr->id = swap_4_byte_aligned_uint64(uhdr->id);
  266|       |
  267|  7.65k|	offset += 4;			/* skip past various 1-byte fields */
  268|       |
  269|  7.65k|	offset += 2;			/* skip past bus_id */
  270|  7.65k|	if (hdr->caplen < offset)
  ------------------
  |  Branch (270:6): [True: 397, False: 7.25k]
  ------------------
  271|    397|		return;
  272|  7.25k|	uhdr->bus_id = PCAP_BSWAP_16(uhdr->bus_id);
  ------------------
  |  |  109|  7.25k|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  273|       |
  274|  7.25k|	offset += 2;			/* skip past various 1-byte fields */
  275|       |
  276|  7.25k|	offset += 8;			/* skip past ts_sec */
  277|  7.25k|	if (hdr->caplen < offset)
  ------------------
  |  Branch (277:6): [True: 400, False: 6.85k]
  ------------------
  278|    400|		return;
  279|  6.85k|	uhdr->ts_sec = swap_4_byte_aligned_int64(uhdr->ts_sec);
  280|       |
  281|  6.85k|	offset += 4;			/* skip past ts_usec */
  282|  6.85k|	if (hdr->caplen < offset)
  ------------------
  |  Branch (282:6): [True: 404, False: 6.45k]
  ------------------
  283|    404|		return;
  284|  6.45k|	uhdr->ts_usec = PCAP_BSWAP_32(uhdr->ts_usec);
  ------------------
  |  |   99|  6.45k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  285|       |
  286|  6.45k|	offset += 4;			/* skip past status */
  287|  6.45k|	if (hdr->caplen < offset)
  ------------------
  |  Branch (287:6): [True: 411, False: 6.04k]
  ------------------
  288|    411|		return;
  289|  6.04k|	uhdr->status = PCAP_BSWAP_32(uhdr->status);
  ------------------
  |  |   99|  6.04k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  290|       |
  291|  6.04k|	offset += 4;			/* skip past urb_len */
  292|  6.04k|	if (hdr->caplen < offset)
  ------------------
  |  Branch (292:6): [True: 397, False: 5.64k]
  ------------------
  293|    397|		return;
  294|  5.64k|	uhdr->urb_len = PCAP_BSWAP_32(uhdr->urb_len);
  ------------------
  |  |   99|  5.64k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  295|       |
  296|  5.64k|	offset += 4;			/* skip past data_len */
  297|  5.64k|	if (hdr->caplen < offset)
  ------------------
  |  Branch (297:6): [True: 395, False: 5.24k]
  ------------------
  298|    395|		return;
  299|  5.24k|	uhdr->data_len = PCAP_BSWAP_32(uhdr->data_len);
  ------------------
  |  |   99|  5.24k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  300|       |
  301|  5.24k|	if (uhdr->transfer_type == URB_ISOCHRONOUS) {
  ------------------
  |  |   43|  5.24k|#define URB_ISOCHRONOUS   0x0
  ------------------
  |  Branch (301:6): [True: 3.86k, False: 1.38k]
  ------------------
  302|  3.86k|		offset += 4;			/* skip past s.iso.error_count */
  303|  3.86k|		if (hdr->caplen < offset)
  ------------------
  |  Branch (303:7): [True: 390, False: 3.47k]
  ------------------
  304|    390|			return;
  305|  3.47k|		uhdr->s.iso.error_count = PCAP_BSWAP_32(uhdr->s.iso.error_count);
  ------------------
  |  |   99|  3.47k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  306|       |
  307|  3.47k|		offset += 4;			/* skip past s.iso.numdesc */
  308|  3.47k|		if (hdr->caplen < offset)
  ------------------
  |  Branch (308:7): [True: 391, False: 3.08k]
  ------------------
  309|    391|			return;
  310|  3.08k|		uhdr->s.iso.numdesc = PCAP_BSWAP_32(uhdr->s.iso.numdesc);
  ------------------
  |  |   99|  3.08k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  311|  3.08k|	} else
  312|  1.38k|		offset += 8;			/* skip USB setup header */
  313|       |
  314|       |	/*
  315|       |	 * With the old header, there are no isochronous descriptors
  316|       |	 * after the header.
  317|       |	 *
  318|       |	 * With the new header, the actual number of descriptors in
  319|       |	 * the header is not s.iso.numdesc, it's ndesc - only the
  320|       |	 * first N descriptors, for some value of N, are put into
  321|       |	 * the header, and ndesc is set to the actual number copied.
  322|       |	 * In addition, if s.iso.numdesc is negative, no descriptors
  323|       |	 * are captured, and ndesc is set to 0.
  324|       |	 */
  325|  4.46k|	if (header_len_64_bytes) {
  ------------------
  |  Branch (325:6): [True: 3.99k, False: 477]
  ------------------
  326|       |		/*
  327|       |		 * This is either the "version 1" header, with
  328|       |		 * 16 bytes of additional fields at the end, or
  329|       |		 * a "version 0" header from a memory-mapped
  330|       |		 * capture, with 16 bytes of zeroed-out padding
  331|       |		 * at the end.  Byte swap them as if this were
  332|       |		 * a "version 1" header.
  333|       |		 */
  334|  3.99k|		offset += 4;			/* skip past interval */
  335|  3.99k|		if (hdr->caplen < offset)
  ------------------
  |  Branch (335:7): [True: 395, False: 3.59k]
  ------------------
  336|    395|			return;
  337|  3.59k|		uhdr->interval = PCAP_BSWAP_32(uhdr->interval);
  ------------------
  |  |   99|  3.59k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  338|       |
  339|  3.59k|		offset += 4;			/* skip past start_frame */
  340|  3.59k|		if (hdr->caplen < offset)
  ------------------
  |  Branch (340:7): [True: 196, False: 3.39k]
  ------------------
  341|    196|			return;
  342|  3.39k|		uhdr->start_frame = PCAP_BSWAP_32(uhdr->start_frame);
  ------------------
  |  |   99|  3.39k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  343|       |
  344|  3.39k|		offset += 4;			/* skip past xfer_flags */
  345|  3.39k|		if (hdr->caplen < offset)
  ------------------
  |  Branch (345:7): [True: 196, False: 3.20k]
  ------------------
  346|    196|			return;
  347|  3.20k|		uhdr->xfer_flags = PCAP_BSWAP_32(uhdr->xfer_flags);
  ------------------
  |  |   99|  3.20k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  348|       |
  349|  3.20k|		offset += 4;			/* skip past ndesc */
  350|  3.20k|		if (hdr->caplen < offset)
  ------------------
  |  Branch (350:7): [True: 201, False: 3.00k]
  ------------------
  351|    201|			return;
  352|  3.00k|		uhdr->ndesc = PCAP_BSWAP_32(uhdr->ndesc);
  ------------------
  |  |   99|  3.00k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  353|       |
  354|  3.00k|		if (uhdr->transfer_type == URB_ISOCHRONOUS) {
  ------------------
  |  |   43|  3.00k|#define URB_ISOCHRONOUS   0x0
  ------------------
  |  Branch (354:7): [True: 2.37k, False: 625]
  ------------------
  355|       |			/* swap the values in struct linux_usb_isodesc */
  356|  2.37k|			usb_isodesc *pisodesc;
  357|  2.37k|			uint32_t i;
  358|       |
  359|  2.37k|			pisodesc = (usb_isodesc *)(void *)(buf+offset);
  360|   260k|			for (i = 0; i < uhdr->ndesc; i++) {
  ------------------
  |  Branch (360:16): [True: 259k, False: 1.34k]
  ------------------
  361|   259k|				offset += 4;		/* skip past status */
  362|   259k|				if (hdr->caplen < offset)
  ------------------
  |  Branch (362:9): [True: 445, False: 258k]
  ------------------
  363|    445|					return;
  364|   258k|				pisodesc->status = PCAP_BSWAP_32(pisodesc->status);
  ------------------
  |  |   99|   258k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  365|       |
  366|   258k|				offset += 4;		/* skip past offset */
  367|   258k|				if (hdr->caplen < offset)
  ------------------
  |  Branch (367:9): [True: 222, False: 258k]
  ------------------
  368|    222|					return;
  369|   258k|				pisodesc->offset = PCAP_BSWAP_32(pisodesc->offset);
  ------------------
  |  |   99|   258k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  370|       |
  371|   258k|				offset += 4;		/* skip past len */
  372|   258k|				if (hdr->caplen < offset)
  ------------------
  |  Branch (372:9): [True: 363, False: 258k]
  ------------------
  373|    363|					return;
  374|   258k|				pisodesc->len = PCAP_BSWAP_32(pisodesc->len);
  ------------------
  |  |   99|   258k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  375|       |
  376|   258k|				offset += 4;		/* skip past padding */
  377|       |
  378|   258k|				pisodesc++;
  379|   258k|			}
  380|  2.37k|		}
  381|  3.00k|	}
  382|  4.46k|}
pcap-util.c:swap_nflog_header:
  397|  1.46k|{
  398|  1.46k|	u_char *p = buf;
  399|  1.46k|	nflog_hdr_t *nfhdr = (nflog_hdr_t *)buf;
  400|  1.46k|	nflog_tlv_t *tlv;
  401|  1.46k|	u_int caplen = hdr->caplen;
  402|  1.46k|	u_int length = hdr->len;
  403|  1.46k|	u_int size;
  404|       |
  405|  1.46k|	if (caplen < (u_int) sizeof(nflog_hdr_t) ||
  ------------------
  |  Branch (405:6): [True: 248, False: 1.21k]
  ------------------
  406|  1.21k|	    length < (u_int) sizeof(nflog_hdr_t)) {
  ------------------
  |  Branch (406:6): [True: 197, False: 1.01k]
  ------------------
  407|       |		/* Not enough data to have any TLVs. */
  408|    445|		return;
  409|    445|	}
  410|       |
  411|  1.01k|	if (nfhdr->nflog_version != 0) {
  ------------------
  |  Branch (411:6): [True: 223, False: 794]
  ------------------
  412|       |		/* Unknown NFLOG version */
  413|    223|		return;
  414|    223|	}
  415|       |
  416|    794|	length -= sizeof(nflog_hdr_t);
  417|    794|	caplen -= sizeof(nflog_hdr_t);
  418|    794|	p += sizeof(nflog_hdr_t);
  419|       |
  420|  1.54k|	while (caplen >= sizeof(nflog_tlv_t)) {
  ------------------
  |  Branch (420:9): [True: 1.08k, False: 468]
  ------------------
  421|  1.08k|		tlv = (nflog_tlv_t *) p;
  422|       |
  423|       |		/* Swap the type and length. */
  424|  1.08k|		tlv->tlv_type = PCAP_BSWAP_16(tlv->tlv_type);
  ------------------
  |  |  109|  1.08k|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  425|  1.08k|		tlv->tlv_length = PCAP_BSWAP_16(tlv->tlv_length);
  ------------------
  |  |  109|  1.08k|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  426|       |
  427|       |		/* Get the length of the TLV. */
  428|  1.08k|		size = tlv->tlv_length;
  429|  1.08k|		if (size % 4 != 0)
  ------------------
  |  Branch (429:7): [True: 603, False: 477]
  ------------------
  430|    603|			size += 4 - size % 4;
  431|       |
  432|       |		/* Is the TLV's length less than the minimum? */
  433|  1.08k|		if (size < sizeof(nflog_tlv_t)) {
  ------------------
  |  Branch (433:7): [True: 129, False: 951]
  ------------------
  434|       |			/* Yes. Give up now. */
  435|    129|			return;
  436|    129|		}
  437|       |
  438|       |		/* Do we have enough data for the full TLV? */
  439|    951|		if (caplen < size || length < size) {
  ------------------
  |  Branch (439:7): [True: 197, False: 754]
  |  Branch (439:24): [True: 0, False: 754]
  ------------------
  440|       |			/* No. */
  441|    197|			return;
  442|    197|		}
  443|       |
  444|       |		/* Skip over the TLV. */
  445|    754|		length -= size;
  446|    754|		caplen -= size;
  447|    754|		p += size;
  448|    754|	}
  449|    794|}
pcap-util.c:packet_length_might_be_wrong:
  490|  1.66k|{
  491|  1.66k|	uint32_t old_style_packet_length;
  492|       |
  493|       |	/*
  494|       |	 * Calculate the packet length the old way.
  495|       |	 * We know that the multiplication won't overflow, but
  496|       |	 * we don't know that the additions won't.  Calculate
  497|       |	 * it with no overflow checks, as that's how it
  498|       |	 * would have been calculated when it was captured.
  499|       |	 */
  500|  1.66k|	old_style_packet_length = iso_pseudo_header_len(usb_hdr) +
  501|  1.66k|	    usb_hdr->urb_len;
  502|  1.66k|	return (hdr->len == old_style_packet_length);
  503|  1.66k|}

pcap-util.c:swap_4_byte_aligned_uint64:
  120|  7.65k|{
  121|  7.65k|	return (pcap_4_byte_aligned_uint64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
  ------------------
  |  |   99|  15.3k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
              	return (pcap_4_byte_aligned_uint64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
  ------------------
  |  |   99|  7.65k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  122|  7.65k|}
pcap-util.c:swap_4_byte_aligned_int64:
  128|  6.85k|{
  129|  6.85k|	return (pcap_4_byte_aligned_int64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
  ------------------
  |  |   99|  13.7k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
              	return (pcap_4_byte_aligned_int64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
  ------------------
  |  |   99|  6.85k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  130|  6.85k|}

pcapint_grow_buffer:
  547|  4.15k|{
  548|  4.15k|	void *bigger_buffer;
  549|       |
  550|  4.15k|	bigger_buffer = realloc(p->buffer, bufsize);
  551|  4.15k|	if (bigger_buffer == NULL) {
  ------------------
  |  Branch (551:6): [True: 0, False: 4.15k]
  ------------------
  552|      0|		snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  553|      0|		return (0);
  554|      0|	}
  555|  4.15k|	p->buffer = bigger_buffer;
  556|  4.15k|	p->bufsize = bufsize;
  557|  4.15k|	return (1);
  558|  4.15k|}
pcapint_oneshot:
  567|  42.8k|{
  568|  42.8k|	struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
  569|       |
  570|  42.8k|	*sp->hdr = *h;
  571|  42.8k|	*sp->pkt = pkt;
  572|  42.8k|}
pcap_next_ex:
  591|  47.5k|{
  592|  47.5k|	struct oneshot_userdata s;
  593|       |
  594|  47.5k|	s.hdr = &p->pcap_header;
  595|  47.5k|	s.pkt = pkt_data;
  596|  47.5k|	s.pd = p;
  597|       |
  598|       |	/* Saves a pointer to the packet headers */
  599|  47.5k|	*pkt_header= &p->pcap_header;
  600|       |
  601|  47.5k|	if (p->rfile != NULL) {
  ------------------
  |  Branch (601:6): [True: 47.5k, False: 0]
  ------------------
  602|  47.5k|		int status;
  603|       |
  604|       |		/* We are on an offline capture */
  605|  47.5k|		status = pcapint_offline_read(p, 1, p->oneshot_callback,
  606|  47.5k|		    (u_char *)&s);
  607|       |
  608|       |		/*
  609|       |		 * Return codes for pcapint_offline_read() are:
  610|       |		 *   -  0: EOF
  611|       |		 *   - -1: error
  612|       |		 *   - >0: OK - result is number of packets read, so
  613|       |		 *         it will be 1 in this case, as we've passed
  614|       |		 *         a maximum packet count of 1
  615|       |		 * The first one ('0') conflicts with the return code of
  616|       |		 * 0 from pcap_read() meaning "no packets arrived before
  617|       |		 * the timeout expired", so we map it to -2 so you can
  618|       |		 * distinguish between an EOF from a savefile and a
  619|       |		 * "no packets arrived before the timeout expired, try
  620|       |		 * again" from a live capture.
  621|       |		 */
  622|  47.5k|		if (status == 0)
  ------------------
  |  Branch (622:7): [True: 3.73k, False: 43.7k]
  ------------------
  623|  3.73k|			return (-2);
  624|  43.7k|		else
  625|  43.7k|			return (status);
  626|  47.5k|	}
  627|       |
  628|       |	/*
  629|       |	 * Return codes for pcap_read() are:
  630|       |	 *   -  0: timeout
  631|       |	 *   - -1: error
  632|       |	 *   - -2: loop was broken out of with pcap_breakloop()
  633|       |	 *   - >0: OK, result is number of packets captured, so
  634|       |	 *         it will be 1 in this case, as we've passed
  635|       |	 *         a maximum packet count of 1
  636|       |	 * The first one ('0') conflicts with the return code of 0 from
  637|       |	 * pcapint_offline_read() meaning "end of file".
  638|       |	*/
  639|      0|	return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s));
  640|  47.5k|}
pcapint_open_offline_common:
 2955|  4.96k|{
 2956|  4.96k|	pcap_t *p;
 2957|       |
 2958|  4.96k|	p = pcap_alloc_pcap_t(ebuf, total_size, private_offset);
 2959|  4.96k|	if (p == NULL)
  ------------------
  |  Branch (2959:6): [True: 0, False: 4.96k]
  ------------------
 2960|      0|		return (NULL);
 2961|       |
 2962|  4.96k|	p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
  ------------------
  |  |  539|  4.96k|#define PCAP_TSTAMP_PRECISION_MICRO	0	/* use timestamps with microsecond precision, default */
  ------------------
 2963|       |
 2964|  4.96k|	return (p);
 2965|  4.96k|}
pcap_datalink:
 3014|  4.61k|{
 3015|  4.61k|	if (!p->activated)
  ------------------
  |  Branch (3015:6): [True: 0, False: 4.61k]
  ------------------
 3016|      0|		return (PCAP_ERROR_NOT_ACTIVATED);
  ------------------
  |  |  364|      0|#define PCAP_ERROR_NOT_ACTIVATED	-3	/* the capture needs to be activated */
  ------------------
 3017|  4.61k|	return (p->linktype);
 3018|  4.61k|}
pcap_datalink_val_to_description:
 3455|  15.2k|{
 3456|  15.2k|	int i;
 3457|       |
 3458|  1.10M|	for (i = 0; dlt_choices[i].name != NULL; i++) {
  ------------------
  |  Branch (3458:14): [True: 1.10M, False: 272]
  ------------------
 3459|  1.10M|		if (dlt_choices[i].dlt == dlt)
  ------------------
  |  Branch (3459:7): [True: 14.9k, False: 1.08M]
  ------------------
 3460|  14.9k|			return (dlt_choices[i].description);
 3461|  1.10M|	}
 3462|    272|	return (NULL);
 3463|  15.2k|}
pcap_close:
 4352|  4.61k|{
 4353|  4.61k|	p->cleanup_op(p);
 4354|       |
 4355|       |	/*
 4356|       |	 * Free information set by pcap_create() *after* calling
 4357|       |	 * the module's cleanup routine; that routine might have
 4358|       |	 * to use p->opt.device (see commit
 4359|       |	 * e333a6044f7d2d3225a6a22205b6b7c1e389945f).
 4360|       |	 */
 4361|  4.61k|	if (p->opt.device != NULL) {
  ------------------
  |  Branch (4361:6): [True: 0, False: 4.61k]
  ------------------
 4362|      0|		free(p->opt.device);
 4363|       |		p->opt.device = NULL;
 4364|      0|	}
 4365|  4.61k|	free(p);
 4366|  4.61k|}
pcap.c:pcap_alloc_pcap_t:
 2505|  4.96k|{
 2506|  4.96k|	char *chunk;
 2507|  4.96k|	pcap_t *p;
 2508|       |
 2509|       |	/*
 2510|       |	 * total_size is the size of a structure containing a pcap_t
 2511|       |	 * followed by a private structure.
 2512|       |	 */
 2513|  4.96k|	chunk = calloc(total_size, 1);
 2514|  4.96k|	if (chunk == NULL) {
  ------------------
  |  Branch (2514:6): [True: 0, False: 4.96k]
  ------------------
 2515|      0|		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
 2516|      0|		    errno, "malloc");
 2517|      0|		return (NULL);
 2518|      0|	}
 2519|       |
 2520|       |	/*
 2521|       |	 * Get a pointer to the pcap_t at the beginning.
 2522|       |	 */
 2523|  4.96k|	p = (pcap_t *)chunk;
 2524|       |
 2525|       |#ifdef _WIN32
 2526|       |	p->handle = INVALID_HANDLE_VALUE;	/* not opened yet */
 2527|       |#else /* _WIN32 */
 2528|  4.96k|	p->fd = -1;	/* not opened yet */
 2529|  4.96k|	p->selectable_fd = -1;
 2530|  4.96k|	p->required_select_timeout = NULL;
 2531|  4.96k|#endif /* _WIN32 */
 2532|       |
 2533|       |	/*
 2534|       |	 * private_offset is the offset, in bytes, of the private
 2535|       |	 * data from the beginning of the structure.
 2536|       |	 *
 2537|       |	 * Set the pointer to the private data; that's private_offset
 2538|       |	 * bytes past the pcap_t.
 2539|       |	 */
 2540|  4.96k|	p->priv = (void *)(chunk + private_offset);
 2541|       |
 2542|  4.96k|	return (p);
 2543|  4.96k|}

pcapint_sf_cleanup:
  238|  4.61k|{
  239|  4.61k|	if (p->rfile != stdin)
  ------------------
  |  Branch (239:6): [True: 4.61k, False: 0]
  ------------------
  240|  4.61k|		(void)fclose(p->rfile);
  241|  4.61k|	if (p->buffer != NULL)
  ------------------
  |  Branch (241:6): [True: 4.61k, False: 0]
  ------------------
  242|  4.61k|		free(p->buffer);
  243|  4.61k|	pcap_freecode(&p->fcode);
  244|  4.61k|}
pcapint_adjust_snapshot:
  440|  7.30k|{
  441|  7.30k|	if (snaplen == 0 || snaplen > INT_MAX) {
  ------------------
  |  Branch (441:6): [True: 1.27k, False: 6.03k]
  |  Branch (441:22): [True: 1.78k, False: 4.25k]
  ------------------
  442|       |		/*
  443|       |		 * Bogus snapshot length; use the maximum for this
  444|       |		 * link-layer type as a fallback.
  445|       |		 *
  446|       |		 * XXX - we don't clamp snapshot lengths that are
  447|       |		 * <= INT_MAX but > max_snaplen_for_dlt(linktype),
  448|       |		 * so a capture file could cause us to allocate
  449|       |		 * a Really Big Buffer.
  450|       |		 */
  451|  3.05k|		snaplen = max_snaplen_for_dlt(linktype);
  452|  3.05k|	}
  453|  7.30k|	return snaplen;
  454|  7.30k|}
pcap_fopen_offline_with_tstamp_precision:
  469|  5.36k|{
  470|  5.36k|	pcap_t *p;
  471|  5.36k|	uint8_t magic[4];
  472|  5.36k|	size_t amt_read;
  473|  5.36k|	u_int i;
  474|  5.36k|	int err;
  475|       |
  476|       |	/*
  477|       |	 * Fail if we were passed a NULL fp.
  478|       |	 *
  479|       |	 * That shouldn't happen if we're opening with a path name, but
  480|       |	 * it could happen if buggy code is opening with a FILE * and
  481|       |	 * didn't bother to make sure the FILE * isn't null.
  482|       |	 */
  483|  5.36k|	if (fp == NULL) {
  ------------------
  |  Branch (483:6): [True: 0, False: 5.36k]
  ------------------
  484|      0|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  485|      0|		    "Null FILE * pointer provided to savefile open routine");
  486|      0|		return (NULL);
  487|      0|	}
  488|       |
  489|       |	/*
  490|       |	 * Read the first 4 bytes of the file; the network analyzer dump
  491|       |	 * file formats we support (pcap and pcapng), and several other
  492|       |	 * formats we might support in the future (such as snoop, DOS and
  493|       |	 * Windows Sniffer, and Microsoft Network Monitor) all have magic
  494|       |	 * numbers that are unique in their first 4 bytes.
  495|       |	 */
  496|  5.36k|	amt_read = fread(&magic, 1, sizeof(magic), fp);
  497|  5.36k|	if (amt_read != sizeof(magic)) {
  ------------------
  |  Branch (497:6): [True: 0, False: 5.36k]
  ------------------
  498|      0|		if (ferror(fp)) {
  ------------------
  |  Branch (498:7): [True: 0, False: 0]
  ------------------
  499|      0|			pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  500|      0|			    errno, "error reading dump file");
  501|      0|		} else {
  502|      0|			snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  503|      0|			    "truncated dump file; tried to read %zu file header bytes, only got %zu",
  504|      0|			    sizeof(magic), amt_read);
  505|      0|		}
  506|      0|		return (NULL);
  507|      0|	}
  508|       |
  509|       |	/*
  510|       |	 * Try all file types.
  511|       |	 */
  512|  7.15k|	for (i = 0; i < N_FILE_TYPES; i++) {
  ------------------
  |  |  461|  7.15k|#define	N_FILE_TYPES	(sizeof check_headers / sizeof check_headers[0])
  ------------------
  |  Branch (512:14): [True: 6.85k, False: 302]
  ------------------
  513|  6.85k|		p = (*check_headers[i])(magic, fp, precision, errbuf, &err);
  514|  6.85k|		if (p != NULL) {
  ------------------
  |  Branch (514:7): [True: 4.61k, False: 2.23k]
  ------------------
  515|       |			/* Yup, that's it. */
  516|  4.61k|			goto found;
  517|  4.61k|		}
  518|  2.23k|		if (err) {
  ------------------
  |  Branch (518:7): [True: 450, False: 1.78k]
  ------------------
  519|       |			/*
  520|       |			 * Error trying to read the header.
  521|       |			 */
  522|    450|			return (NULL);
  523|    450|		}
  524|  2.23k|	}
  525|       |
  526|       |	/*
  527|       |	 * Well, who knows what this mess is....
  528|       |	 */
  529|    302|	snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format");
  ------------------
  |  |  149|    302|#define PCAP_ERRBUF_SIZE 256
  ------------------
  530|    302|	return (NULL);
  531|       |
  532|  4.61k|found:
  533|  4.61k|	p->rfile = fp;
  534|       |
  535|       |	/* Padding only needed for live capture fcode */
  536|  4.61k|	p->fddipad = 0;
  537|       |
  538|  4.61k|#if !defined(_WIN32)
  539|       |	/*
  540|       |	 * You can do "select()" and "poll()" on plain files on most
  541|       |	 * platforms, and should be able to do so on pipes.
  542|       |	 *
  543|       |	 * You can't do "select()" on anything other than sockets in
  544|       |	 * Windows, so, on Win32 systems, we don't have "selectable_fd".
  545|       |	 */
  546|  4.61k|	p->selectable_fd = fileno(fp);
  547|  4.61k|#endif
  548|       |
  549|  4.61k|	p->can_set_rfmon_op = sf_cant_set_rfmon;
  550|  4.61k|	p->read_op = pcapint_offline_read;
  551|  4.61k|	p->inject_op = sf_inject;
  552|  4.61k|	p->setfilter_op = pcapint_install_bpf_program;
  553|  4.61k|	p->setdirection_op = sf_setdirection;
  554|  4.61k|	p->set_datalink_op = NULL;	/* we don't support munging link-layer headers */
  555|  4.61k|	p->getnonblock_op = sf_getnonblock;
  556|  4.61k|	p->setnonblock_op = sf_setnonblock;
  557|  4.61k|	p->stats_op = sf_stats;
  558|       |#ifdef _WIN32
  559|       |	p->stats_ex_op = sf_stats_ex;
  560|       |	p->setbuff_op = sf_setbuff;
  561|       |	p->setmode_op = sf_setmode;
  562|       |	p->setmintocopy_op = sf_setmintocopy;
  563|       |	p->getevent_op = sf_getevent;
  564|       |	p->oid_get_request_op = sf_oid_get_request;
  565|       |	p->oid_set_request_op = sf_oid_set_request;
  566|       |	p->sendqueue_transmit_op = sf_sendqueue_transmit;
  567|       |	p->setuserbuffer_op = sf_setuserbuffer;
  568|       |	p->live_dump_op = sf_live_dump;
  569|       |	p->live_dump_ended_op = sf_live_dump_ended;
  570|       |#endif
  571|       |
  572|       |	/*
  573|       |	 * For offline captures, the standard one-shot callback can
  574|       |	 * be used for pcap_next()/pcap_next_ex().
  575|       |	 */
  576|  4.61k|	p->oneshot_callback = pcapint_oneshot;
  577|       |
  578|       |	/*
  579|       |	 * Default breakloop operation.
  580|       |	 */
  581|  4.61k|	p->breakloop_op = pcapint_breakloop_common;
  582|       |
  583|       |	/*
  584|       |	 * For link-layer headers in which the packet type is indicated
  585|       |	 * by an AF_ value, we don't know what OS generated it, so we
  586|       |	 * don't know what numerical value corresponds to AF_INET6, and
  587|       |	 * we don't know the byte order of the host that originally
  588|       |	 * captured the packets in the file (as opposed to the host
  589|       |	 * that *wrote* this file), so we don't know the byte order
  590|       |	 * of multi-byte AF_ values.
  591|       |	 *
  592|       |	 * That requires different filtering code than a live capture.
  593|       |	 *
  594|       |	 * Savefiles don't require any other special BPF code generation.
  595|       |	 */
  596|  4.61k|	p->bpf_codegen_flags = BPF_OFFLINE_AF_HANDLING;
  ------------------
  |  |  427|  4.61k|#define BPF_OFFLINE_AF_HANDLING	0x00000004
  ------------------
  597|       |
  598|  4.61k|	p->activated = 1;
  599|       |
  600|  4.61k|	return (p);
  601|  5.36k|}
pcap_fopen_offline:
  611|  5.36k|{
  612|  5.36k|	return (pcap_fopen_offline_with_tstamp_precision(fp,
  613|  5.36k|	    PCAP_TSTAMP_PRECISION_MICRO, errbuf));
  ------------------
  |  |  539|  5.36k|#define PCAP_TSTAMP_PRECISION_MICRO	0	/* use timestamps with microsecond precision, default */
  ------------------
  614|  5.36k|}
pcapint_offline_read:
  624|  47.5k|{
  625|  47.5k|	int n = 0;
  626|  47.5k|	u_char *data;
  627|       |
  628|       |	/*
  629|       |	 * This can conceivably process more than INT_MAX packets,
  630|       |	 * which would overflow the packet count, causing it either
  631|       |	 * to look like a negative number, and thus cause us to
  632|       |	 * return a value that looks like an error, or overflow
  633|       |	 * back into positive territory, and thus cause us to
  634|       |	 * return a too-low count.
  635|       |	 *
  636|       |	 * Therefore, if the packet count is unlimited, we clip
  637|       |	 * it at INT_MAX; this routine is not expected to
  638|       |	 * process packets indefinitely, so that's not an issue.
  639|       |	 */
  640|  47.5k|	if (PACKET_COUNT_IS_UNLIMITED(cnt))
  ------------------
  |  |  463|  47.5k|#define PACKET_COUNT_IS_UNLIMITED(count)	((count) <= 0)
  |  |  ------------------
  |  |  |  Branch (463:42): [True: 0, False: 47.5k]
  |  |  ------------------
  ------------------
  641|      0|		cnt = INT_MAX;
  642|       |
  643|  47.5k|	for (;;) {
  644|  47.5k|		struct pcap_pkthdr h;
  645|  47.5k|		int status;
  646|       |
  647|       |		/*
  648|       |		 * Has "pcap_breakloop()" been called?
  649|       |		 * If so, return immediately - if we haven't read any
  650|       |		 * packets, clear the flag and return -2 to indicate
  651|       |		 * that we were told to break out of the loop, otherwise
  652|       |		 * leave the flag set, so that the *next* call will break
  653|       |		 * out of the loop without having read any packets, and
  654|       |		 * return the number of packets we've processed so far.
  655|       |		 */
  656|  47.5k|		if (p->break_loop) {
  ------------------
  |  Branch (656:7): [True: 0, False: 47.5k]
  ------------------
  657|      0|			if (n == 0) {
  ------------------
  |  Branch (657:8): [True: 0, False: 0]
  ------------------
  658|      0|				p->break_loop = 0;
  659|      0|				return (-2);
  660|      0|			} else
  661|      0|				return (n);
  662|      0|		}
  663|       |
  664|  47.5k|		status = p->next_packet_op(p, &h, &data);
  665|  47.5k|		if (status < 0) {
  ------------------
  |  Branch (665:7): [True: 879, False: 46.6k]
  ------------------
  666|       |			/*
  667|       |			 * Error.  Pass it back to the caller.
  668|       |			 */
  669|    879|			return (status);
  670|    879|		}
  671|  46.6k|		if (status == 0) {
  ------------------
  |  Branch (671:7): [True: 3.73k, False: 42.8k]
  ------------------
  672|       |			/*
  673|       |			 * EOF.  Nothing more to process;
  674|       |			 */
  675|  3.73k|			break;
  676|  3.73k|		}
  677|       |
  678|       |		/*
  679|       |		 * OK, we've read a packet; run it through the filter
  680|       |		 * and, if it passes, process it.
  681|       |		 */
  682|  42.8k|		if (p->fcode.bf_insns == NULL ||
  ------------------
  |  Branch (682:7): [True: 42.8k, False: 0]
  ------------------
  683|      0|		    pcapint_filter(p->fcode.bf_insns, p->fcode.bf_len,
  ------------------
  |  Branch (683:7): [True: 0, False: 0]
  ------------------
  684|  42.8k|		                   data, h.len, h.caplen)) {
  685|  42.8k|			(*callback)(user, &h, data);
  686|  42.8k|			n++;	/* count the packet */
  687|  42.8k|			if (n >= cnt)
  ------------------
  |  Branch (687:8): [True: 42.8k, False: 0]
  ------------------
  688|  42.8k|				break;
  689|  42.8k|		}
  690|  42.8k|	}
  691|       |	/*XXX this breaks semantics tcpslice expects */
  692|  46.6k|	return (n);
  693|  47.5k|}

pcap_check_header:
  220|  5.36k|{
  221|  5.36k|	bpf_u_int32 magic_int;
  222|  5.36k|	struct pcap_file_header hdr;
  223|  5.36k|	size_t amt_read;
  224|  5.36k|	pcap_t *p;
  225|  5.36k|	int swapped = 0;
  226|  5.36k|	struct pcap_sf *ps;
  227|       |
  228|       |	/*
  229|       |	 * Assume no read errors.
  230|       |	 */
  231|  5.36k|	*err = 0;
  232|       |
  233|       |	/*
  234|       |	 * Check whether the first 4 bytes of the file are the magic
  235|       |	 * number for a pcap savefile, or for a byte-swapped pcap
  236|       |	 * savefile.
  237|       |	 */
  238|  5.36k|	memcpy(&magic_int, magic, sizeof(magic_int));
  239|  5.36k|	if (magic_int != TCPDUMP_MAGIC &&
  ------------------
  |  |   71|  10.7k|#define TCPDUMP_MAGIC		0xa1b2c3d4
  ------------------
  |  Branch (239:6): [True: 4.66k, False: 703]
  ------------------
  240|  4.66k|	    magic_int != KUZNETZOV_TCPDUMP_MAGIC &&
  ------------------
  |  |   76|  10.0k|#define KUZNETZOV_TCPDUMP_MAGIC	0xa1b2cd34
  ------------------
  |  Branch (240:6): [True: 4.63k, False: 28]
  ------------------
  241|  4.63k|	    magic_int != NSEC_TCPDUMP_MAGIC) {
  ------------------
  |  |   94|  4.63k|#define NSEC_TCPDUMP_MAGIC	0xa1b23c4d
  ------------------
  |  Branch (241:6): [True: 4.58k, False: 52]
  ------------------
  242|  4.58k|		magic_int = PCAP_BSWAP_32(magic_int);
  ------------------
  |  |   99|  4.58k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  243|  4.58k|		if (magic_int != TCPDUMP_MAGIC &&
  ------------------
  |  |   71|  9.16k|#define TCPDUMP_MAGIC		0xa1b2c3d4
  ------------------
  |  Branch (243:7): [True: 2.14k, False: 2.43k]
  ------------------
  244|  2.14k|		    magic_int != KUZNETZOV_TCPDUMP_MAGIC &&
  ------------------
  |  |   76|  6.72k|#define KUZNETZOV_TCPDUMP_MAGIC	0xa1b2cd34
  ------------------
  |  Branch (244:7): [True: 2.09k, False: 55]
  ------------------
  245|  2.09k|		    magic_int != NSEC_TCPDUMP_MAGIC)
  ------------------
  |  |   94|  2.09k|#define NSEC_TCPDUMP_MAGIC	0xa1b23c4d
  ------------------
  |  Branch (245:7): [True: 1.48k, False: 604]
  ------------------
  246|  1.48k|			return (NULL);	/* nope */
  247|  3.09k|		swapped = 1;
  248|  3.09k|	}
  249|       |
  250|       |	/*
  251|       |	 * They are.  Put the magic number in the header, and read
  252|       |	 * the rest of the header.
  253|       |	 */
  254|  3.87k|	hdr.magic = magic_int;
  255|  3.87k|	amt_read = fread(((char *)&hdr) + sizeof hdr.magic, 1,
  256|  3.87k|	    sizeof(hdr) - sizeof(hdr.magic), fp);
  257|  3.87k|	if (amt_read != sizeof(hdr) - sizeof(hdr.magic)) {
  ------------------
  |  Branch (257:6): [True: 0, False: 3.87k]
  ------------------
  258|      0|		if (ferror(fp)) {
  ------------------
  |  Branch (258:7): [True: 0, False: 0]
  ------------------
  259|      0|			pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  260|      0|			    errno, "error reading dump file");
  261|      0|		} else {
  262|      0|			snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  263|      0|			    "truncated dump file; tried to read %zu file header bytes, only got %zu",
  264|      0|			    sizeof(hdr), amt_read);
  265|      0|		}
  266|      0|		*err = 1;
  267|      0|		return (NULL);
  268|      0|	}
  269|       |
  270|       |	/*
  271|       |	 * If it's a byte-swapped capture file, byte-swap the header.
  272|       |	 */
  273|  3.87k|	if (swapped) {
  ------------------
  |  Branch (273:6): [True: 3.09k, False: 783]
  ------------------
  274|  3.09k|		hdr.version_major = PCAP_BSWAP_16(hdr.version_major);
  ------------------
  |  |  109|  3.09k|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  275|  3.09k|		hdr.version_minor = PCAP_BSWAP_16(hdr.version_minor);
  ------------------
  |  |  109|  3.09k|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  276|  3.09k|		hdr.thiszone = PCAP_BSWAP_32(hdr.thiszone);
  ------------------
  |  |   99|  3.09k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  277|  3.09k|		hdr.sigfigs = PCAP_BSWAP_32(hdr.sigfigs);
  ------------------
  |  |   99|  3.09k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  278|  3.09k|		hdr.snaplen = PCAP_BSWAP_32(hdr.snaplen);
  ------------------
  |  |   99|  3.09k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  279|  3.09k|		hdr.linktype = PCAP_BSWAP_32(hdr.linktype);
  ------------------
  |  |   99|  3.09k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  280|  3.09k|	}
  281|       |
  282|       |	/*
  283|       |	 * currently only versions 2.[0-4] are supported with
  284|       |	 * the exception of 543.0 for DG/UX tcpdump.
  285|       |	 */
  286|  3.87k|	if (! ((hdr.version_major == PCAP_VERSION_MAJOR &&
  ------------------
  |  |  146|  7.75k|#define PCAP_VERSION_MAJOR 2
  ------------------
  |  Branch (286:10): [True: 3.39k, False: 488]
  ------------------
  287|  3.39k|		hdr.version_minor <= PCAP_VERSION_MINOR) ||
  ------------------
  |  |  147|  3.39k|#define PCAP_VERSION_MINOR 4
  ------------------
  |  Branch (287:3): [True: 3.37k, False: 14]
  ------------------
  288|    502|	       (hdr.version_major == 543 &&
  ------------------
  |  Branch (288:10): [True: 465, False: 37]
  ------------------
  289|    465|		hdr.version_minor == 0))) {
  ------------------
  |  Branch (289:3): [True: 449, False: 16]
  ------------------
  290|     53|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     53|#define PCAP_ERRBUF_SIZE 256
  ------------------
  291|     53|			 "unsupported pcap savefile version %u.%u",
  292|     53|			 hdr.version_major, hdr.version_minor);
  293|     53|		*err = 1;
  294|     53|		return NULL;
  295|     53|	}
  296|       |
  297|       |	/*
  298|       |	 * Check the main reserved field.
  299|       |	 */
  300|  3.82k|	if (LT_RESERVED1(hdr.linktype) != 0) {
  ------------------
  |  |  267|  3.82k|#define LT_RESERVED1(x)			((x) & 0x03FF0000)
  ------------------
  |  Branch (300:6): [True: 10, False: 3.81k]
  ------------------
  301|     10|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     10|#define PCAP_ERRBUF_SIZE 256
  ------------------
  302|     10|			 "savefile linktype reserved field not zero (0x%08x)",
  303|     10|			 LT_RESERVED1(hdr.linktype));
  ------------------
  |  |  267|     10|#define LT_RESERVED1(x)			((x) & 0x03FF0000)
  ------------------
  304|     10|		*err = 1;
  305|     10|		return NULL;
  306|     10|	}
  307|       |
  308|       |	/*
  309|       |	 * OK, this is a good pcap file.
  310|       |	 * Allocate a pcap_t for it.
  311|       |	 */
  312|  3.81k|	p = PCAP_OPEN_OFFLINE_COMMON(errbuf, struct pcap_sf);
  ------------------
  |  |  589|  3.81k|	pcapint_open_offline_common(ebuf, \
  |  |  590|  3.81k|	    sizeof (struct { pcap_t __common; type __private; }), \
  |  |  591|  3.81k|	    offsetof (struct { pcap_t __common; type __private; }, __private))
  ------------------
  313|  3.81k|	if (p == NULL) {
  ------------------
  |  Branch (313:6): [True: 0, False: 3.81k]
  ------------------
  314|       |		/* Allocation failed. */
  315|      0|		*err = 1;
  316|      0|		return (NULL);
  317|      0|	}
  318|  3.81k|	p->swapped = swapped;
  319|  3.81k|	p->version_major = hdr.version_major;
  320|  3.81k|	p->version_minor = hdr.version_minor;
  321|  3.81k|	p->linktype = linktype_to_dlt(LT_LINKTYPE(hdr.linktype));
  ------------------
  |  |  265|  3.81k|#define LT_LINKTYPE(x)			((x) & 0x0000FFFF)
  ------------------
  322|  3.81k|	p->linktype_ext = LT_LINKTYPE_EXT(hdr.linktype);
  ------------------
  |  |  266|  3.81k|#define LT_LINKTYPE_EXT(x)		((x) & 0xFFFF0000)
  ------------------
  323|  3.81k|	p->snapshot = pcapint_adjust_snapshot(p->linktype, hdr.snaplen);
  324|       |
  325|  3.81k|	p->next_packet_op = pcap_next_packet;
  326|       |
  327|  3.81k|	ps = p->priv;
  328|       |
  329|  3.81k|	p->opt.tstamp_precision = precision;
  330|       |
  331|       |	/*
  332|       |	 * Will we need to scale the timestamps to match what the
  333|       |	 * user wants?
  334|       |	 */
  335|  3.81k|	switch (precision) {
  336|       |
  337|  3.81k|	case PCAP_TSTAMP_PRECISION_MICRO:
  ------------------
  |  |  539|  3.81k|#define PCAP_TSTAMP_PRECISION_MICRO	0	/* use timestamps with microsecond precision, default */
  ------------------
  |  Branch (337:2): [True: 3.81k, False: 0]
  ------------------
  338|  3.81k|		if (magic_int == NSEC_TCPDUMP_MAGIC) {
  ------------------
  |  |   94|  3.81k|#define NSEC_TCPDUMP_MAGIC	0xa1b23c4d
  ------------------
  |  Branch (338:7): [True: 632, False: 3.18k]
  ------------------
  339|       |			/*
  340|       |			 * The file has nanoseconds, the user
  341|       |			 * wants microseconds; scale the
  342|       |			 * precision down.
  343|       |			 */
  344|    632|			ps->scale_type = SCALE_DOWN;
  345|  3.18k|		} else {
  346|       |			/*
  347|       |			 * The file has microseconds, the
  348|       |			 * user wants microseconds; nothing to do.
  349|       |			 */
  350|  3.18k|			ps->scale_type = PASS_THROUGH;
  351|  3.18k|		}
  352|  3.81k|		break;
  353|       |
  354|      0|	case PCAP_TSTAMP_PRECISION_NANO:
  ------------------
  |  |  540|      0|#define PCAP_TSTAMP_PRECISION_NANO	1	/* use timestamps with nanosecond precision */
  ------------------
  |  Branch (354:2): [True: 0, False: 3.81k]
  ------------------
  355|      0|		if (magic_int == NSEC_TCPDUMP_MAGIC) {
  ------------------
  |  |   94|      0|#define NSEC_TCPDUMP_MAGIC	0xa1b23c4d
  ------------------
  |  Branch (355:7): [True: 0, False: 0]
  ------------------
  356|       |			/*
  357|       |			 * The file has nanoseconds, the
  358|       |			 * user wants nanoseconds; nothing to do.
  359|       |			 */
  360|      0|			ps->scale_type = PASS_THROUGH;
  361|      0|		} else {
  362|       |			/*
  363|       |			 * The file has microseconds, the user
  364|       |			 * wants nanoseconds; scale the
  365|       |			 * precision up.
  366|       |			 */
  367|      0|			ps->scale_type = SCALE_UP;
  368|      0|		}
  369|      0|		break;
  370|       |
  371|      0|	default:
  ------------------
  |  Branch (371:2): [True: 0, False: 3.81k]
  ------------------
  372|      0|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  373|      0|		    "unknown time stamp resolution %u", precision);
  374|      0|		free(p);
  375|      0|		*err = 1;
  376|      0|		return (NULL);
  377|  3.81k|	}
  378|       |
  379|       |	/*
  380|       |	 * We interchanged the caplen and len fields at version 2.3,
  381|       |	 * in order to match the bpf header layout.  But unfortunately
  382|       |	 * some files were written with version 2.3 in their headers
  383|       |	 * but without the interchanged fields.
  384|       |	 *
  385|       |	 * In addition, DG/UX tcpdump writes out files with a version
  386|       |	 * number of 543.0, and with the caplen and len fields in the
  387|       |	 * pre-2.3 order.
  388|       |	 */
  389|  3.81k|	switch (hdr.version_major) {
  390|       |
  391|  3.36k|	case 2:
  ------------------
  |  Branch (391:2): [True: 3.36k, False: 449]
  ------------------
  392|  3.36k|		if (hdr.version_minor < 3)
  ------------------
  |  Branch (392:7): [True: 1.44k, False: 1.91k]
  ------------------
  393|  1.44k|			ps->lengths_swapped = SWAPPED;
  394|  1.91k|		else if (hdr.version_minor == 3)
  ------------------
  |  Branch (394:12): [True: 1.33k, False: 587]
  ------------------
  395|  1.33k|			ps->lengths_swapped = MAYBE_SWAPPED;
  396|    587|		else
  397|    587|			ps->lengths_swapped = NOT_SWAPPED;
  398|  3.36k|		break;
  399|       |
  400|    449|	case 543:
  ------------------
  |  Branch (400:2): [True: 449, False: 3.36k]
  ------------------
  401|    449|		ps->lengths_swapped = SWAPPED;
  402|    449|		break;
  403|       |
  404|      0|	default:
  ------------------
  |  Branch (404:2): [True: 0, False: 3.81k]
  ------------------
  405|      0|		ps->lengths_swapped = NOT_SWAPPED;
  406|      0|		break;
  407|  3.81k|	}
  408|       |
  409|  3.81k|	if (magic_int == KUZNETZOV_TCPDUMP_MAGIC) {
  ------------------
  |  |   76|  3.81k|#define KUZNETZOV_TCPDUMP_MAGIC	0xa1b2cd34
  ------------------
  |  Branch (409:6): [True: 76, False: 3.74k]
  ------------------
  410|       |		/*
  411|       |		 * XXX - the patch that's in some versions of libpcap
  412|       |		 * changes the packet header but not the magic number,
  413|       |		 * and some other versions with this magic number have
  414|       |		 * some extra debugging information in the packet header;
  415|       |		 * we'd have to use some hacks^H^H^H^H^Hheuristics to
  416|       |		 * detect those variants.
  417|       |		 *
  418|       |		 * Wireshark does that, but it does so by trying to read
  419|       |		 * the first two packets of the file with each of the
  420|       |		 * record header formats.  That currently means it seeks
  421|       |		 * backwards and retries the reads, which doesn't work
  422|       |		 * on pipes.  We want to be able to read from a pipe, so
  423|       |		 * that strategy won't work; we'd have to buffer some
  424|       |		 * data ourselves and read from that buffer in order to
  425|       |		 * make that work.
  426|       |		 */
  427|     76|		ps->hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
  428|       |
  429|     76|		if (p->linktype == DLT_EN10MB) {
  ------------------
  |  |   68|     76|#define DLT_EN10MB	1	/* Ethernet (10Mb) */
  ------------------
  |  Branch (429:7): [True: 57, False: 19]
  ------------------
  430|       |			/*
  431|       |			 * This capture might have been done in raw mode
  432|       |			 * or cooked mode.
  433|       |			 *
  434|       |			 * If it was done in cooked mode, p->snapshot was
  435|       |			 * passed to recvfrom() as the buffer size, meaning
  436|       |			 * that the most packet data that would be copied
  437|       |			 * would be p->snapshot.  However, a faked Ethernet
  438|       |			 * header would then have been added to it, so the
  439|       |			 * most data that would be in a packet in the file
  440|       |			 * would be p->snapshot + 14.
  441|       |			 *
  442|       |			 * We can't easily tell whether the capture was done
  443|       |			 * in raw mode or cooked mode, so we'll assume it was
  444|       |			 * cooked mode, and add 14 to the snapshot length.
  445|       |			 * That means that, for a raw capture, the snapshot
  446|       |			 * length will be misleading if you use it to figure
  447|       |			 * out why a capture doesn't have all the packet data,
  448|       |			 * but there's not much we can do to avoid that.
  449|       |			 *
  450|       |			 * But don't grow the snapshot length past the
  451|       |			 * maximum value of an int.
  452|       |			 */
  453|     57|			if (p->snapshot <= INT_MAX - 14)
  ------------------
  |  Branch (453:8): [True: 54, False: 3]
  ------------------
  454|     54|				p->snapshot += 14;
  455|      3|			else
  456|      3|				p->snapshot = INT_MAX;
  457|     57|		}
  458|     76|	} else
  459|  3.74k|		ps->hdrsize = sizeof(struct pcap_sf_pkthdr);
  460|       |
  461|       |	/*
  462|       |	 * Allocate a buffer for the packet data.
  463|       |	 * Choose the minimum of the file's snapshot length and 2K bytes;
  464|       |	 * that should be enough for most network packets - we'll grow it
  465|       |	 * if necessary.  That way, we don't allocate a huge chunk of
  466|       |	 * memory just because there's a huge snapshot length, as the
  467|       |	 * snapshot length might be larger than the size of the largest
  468|       |	 * packet.
  469|       |	 */
  470|  3.81k|	p->bufsize = p->snapshot;
  471|  3.81k|	if (p->bufsize > 2048)
  ------------------
  |  Branch (471:6): [True: 3.49k, False: 319]
  ------------------
  472|  3.49k|		p->bufsize = 2048;
  473|  3.81k|	p->buffer = malloc(p->bufsize);
  474|  3.81k|	if (p->buffer == NULL) {
  ------------------
  |  Branch (474:6): [True: 0, False: 3.81k]
  ------------------
  475|      0|		snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  476|      0|		free(p);
  477|      0|		*err = 1;
  478|      0|		return (NULL);
  479|      0|	}
  480|       |
  481|  3.81k|	p->cleanup_op = pcapint_sf_cleanup;
  482|       |
  483|  3.81k|	return (p);
  484|  3.81k|}
sf-pcap.c:pcap_next_packet:
  493|  44.4k|{
  494|  44.4k|	struct pcap_sf *ps = p->priv;
  495|  44.4k|	struct pcap_sf_patched_pkthdr sf_hdr;
  496|  44.4k|	FILE *fp = p->rfile;
  497|  44.4k|	size_t amt_read;
  498|  44.4k|	bpf_u_int32 t;
  499|       |
  500|       |	/*
  501|       |	 * Read the packet header; the structure we use as a buffer
  502|       |	 * is the longer structure for files generated by the patched
  503|       |	 * libpcap, but if the file has the magic number for an
  504|       |	 * unpatched libpcap we only read as many bytes as the regular
  505|       |	 * header has.
  506|       |	 */
  507|  44.4k|	amt_read = fread(&sf_hdr, 1, ps->hdrsize, fp);
  508|  44.4k|	if (amt_read != ps->hdrsize) {
  ------------------
  |  Branch (508:6): [True: 3.53k, False: 40.9k]
  ------------------
  509|  3.53k|		if (ferror(fp)) {
  ------------------
  |  Branch (509:7): [True: 0, False: 3.53k]
  ------------------
  510|      0|			pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  511|      0|			    errno, "error reading dump file");
  512|      0|			return (-1);
  513|  3.53k|		} else {
  514|  3.53k|			if (amt_read != 0) {
  ------------------
  |  Branch (514:8): [True: 148, False: 3.38k]
  ------------------
  515|    148|				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|    148|#define PCAP_ERRBUF_SIZE 256
  ------------------
  516|    148|				    "truncated dump file; tried to read %zu header bytes, only got %zu",
  517|    148|				    ps->hdrsize, amt_read);
  518|    148|				return (-1);
  519|    148|			}
  520|       |			/* EOF */
  521|  3.38k|			return (0);
  522|  3.53k|		}
  523|  3.53k|	}
  524|       |
  525|  40.9k|	if (p->swapped) {
  ------------------
  |  Branch (525:6): [True: 28.7k, False: 12.2k]
  ------------------
  526|       |		/* these were written in opposite byte order */
  527|  28.7k|		hdr->caplen = PCAP_BSWAP_32(sf_hdr.caplen);
  ------------------
  |  |   99|  28.7k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  528|  28.7k|		hdr->len = PCAP_BSWAP_32(sf_hdr.len);
  ------------------
  |  |   99|  28.7k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  529|  28.7k|		hdr->ts.tv_sec = PCAP_BSWAP_32(sf_hdr.ts.tv_sec);
  ------------------
  |  |   99|  28.7k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  530|  28.7k|		hdr->ts.tv_usec = PCAP_BSWAP_32(sf_hdr.ts.tv_usec);
  ------------------
  |  |   99|  28.7k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  531|  28.7k|	} else {
  532|  12.2k|		hdr->caplen = sf_hdr.caplen;
  533|  12.2k|		hdr->len = sf_hdr.len;
  534|  12.2k|		hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
  535|  12.2k|		hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
  536|  12.2k|	}
  537|       |
  538|  40.9k|	switch (ps->scale_type) {
  ------------------
  |  Branch (538:10): [True: 40.9k, False: 0]
  ------------------
  539|       |
  540|  32.4k|	case PASS_THROUGH:
  ------------------
  |  Branch (540:2): [True: 32.4k, False: 8.45k]
  ------------------
  541|       |		/*
  542|       |		 * Just pass the time stamp through.
  543|       |		 */
  544|  32.4k|		break;
  545|       |
  546|      0|	case SCALE_UP:
  ------------------
  |  Branch (546:2): [True: 0, False: 40.9k]
  ------------------
  547|       |		/*
  548|       |		 * File has microseconds, user wants nanoseconds; convert
  549|       |		 * it.
  550|       |		 */
  551|      0|		hdr->ts.tv_usec = hdr->ts.tv_usec * 1000;
  552|      0|		break;
  553|       |
  554|  8.45k|	case SCALE_DOWN:
  ------------------
  |  Branch (554:2): [True: 8.45k, False: 32.4k]
  ------------------
  555|       |		/*
  556|       |		 * File has nanoseconds, user wants microseconds; convert
  557|       |		 * it.
  558|       |		 */
  559|  8.45k|		hdr->ts.tv_usec = hdr->ts.tv_usec / 1000;
  560|  8.45k|		break;
  561|  40.9k|	}
  562|       |
  563|       |	/* Swap the caplen and len fields, if necessary. */
  564|  40.9k|	switch (ps->lengths_swapped) {
  ------------------
  |  Branch (564:10): [True: 40.9k, False: 0]
  ------------------
  565|       |
  566|  6.01k|	case NOT_SWAPPED:
  ------------------
  |  Branch (566:2): [True: 6.01k, False: 34.9k]
  ------------------
  567|  6.01k|		break;
  568|       |
  569|  19.9k|	case MAYBE_SWAPPED:
  ------------------
  |  Branch (569:2): [True: 19.9k, False: 20.9k]
  ------------------
  570|  19.9k|		if (hdr->caplen <= hdr->len) {
  ------------------
  |  Branch (570:7): [True: 11.6k, False: 8.37k]
  ------------------
  571|       |			/*
  572|       |			 * The captured length is <= the actual length,
  573|       |			 * so presumably they weren't swapped.
  574|       |			 */
  575|  11.6k|			break;
  576|  11.6k|		}
  577|       |		/* FALLTHROUGH */
  578|       |
  579|  23.3k|	case SWAPPED:
  ------------------
  |  Branch (579:2): [True: 14.9k, False: 26.0k]
  ------------------
  580|  23.3k|		t = hdr->caplen;
  581|  23.3k|		hdr->caplen = hdr->len;
  582|  23.3k|		hdr->len = t;
  583|  23.3k|		break;
  584|  40.9k|	}
  585|       |
  586|       |	/*
  587|       |	 * Is the packet bigger than we consider sane?
  588|       |	 */
  589|  40.9k|	if (hdr->caplen > max_snaplen_for_dlt(p->linktype)) {
  ------------------
  |  Branch (589:6): [True: 88, False: 40.8k]
  ------------------
  590|       |		/*
  591|       |		 * Yes.  This may be a damaged or fuzzed file.
  592|       |		 *
  593|       |		 * Is it bigger than the snapshot length?
  594|       |		 * (We don't treat that as an error if it's not
  595|       |		 * bigger than the maximum we consider sane; see
  596|       |		 * below.)
  597|       |		 */
  598|     88|		if (hdr->caplen > (bpf_u_int32)p->snapshot) {
  ------------------
  |  Branch (598:7): [True: 76, False: 12]
  ------------------
  599|     76|			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     76|#define PCAP_ERRBUF_SIZE 256
  ------------------
  600|     76|			    "invalid packet capture length %u, bigger than "
  601|     76|			    "snaplen of %d", hdr->caplen, p->snapshot);
  602|     76|		} else {
  603|     12|			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     12|#define PCAP_ERRBUF_SIZE 256
  ------------------
  604|     12|			    "invalid packet capture length %u, bigger than "
  605|     12|			    "maximum of %u", hdr->caplen,
  606|     12|			    max_snaplen_for_dlt(p->linktype));
  607|     12|		}
  608|     88|		return (-1);
  609|     88|	}
  610|       |
  611|  40.8k|	if (hdr->caplen > (bpf_u_int32)p->snapshot) {
  ------------------
  |  Branch (611:6): [True: 3.64k, False: 37.2k]
  ------------------
  612|       |		/*
  613|       |		 * The packet is bigger than the snapshot length
  614|       |		 * for this file.
  615|       |		 *
  616|       |		 * This can happen due to Solaris 2.3 systems tripping
  617|       |		 * over the BUFMOD problem and not setting the snapshot
  618|       |		 * length correctly in the savefile header.
  619|       |		 *
  620|       |		 * libpcap 0.4 and later on Solaris 2.3 should set the
  621|       |		 * snapshot length correctly in the pcap file header,
  622|       |		 * even though they don't set a snapshot length in bufmod
  623|       |		 * (the buggy bufmod chops off the *beginning* of the
  624|       |		 * packet if a snapshot length is specified); they should
  625|       |		 * also reduce the captured length, as supplied to the
  626|       |		 * per-packet callback, to the snapshot length if it's
  627|       |		 * greater than the snapshot length, so the code using
  628|       |		 * libpcap should see the packet cut off at the snapshot
  629|       |		 * length, even though the full packet is copied up to
  630|       |		 * userland.
  631|       |		 *
  632|       |		 * However, perhaps some versions of libpcap failed to
  633|       |		 * set the snapshot length correctly in the file header
  634|       |		 * or the per-packet header, or perhaps this is a
  635|       |		 * corrupted savefile or a savefile built/modified by a
  636|       |		 * fuzz tester, so we check anyway.  We grow the buffer
  637|       |		 * to be big enough for the snapshot length, read up
  638|       |		 * to the snapshot length, discard the rest of the
  639|       |		 * packet, and report the snapshot length as the captured
  640|       |		 * length; we don't want to hand our caller a packet
  641|       |		 * bigger than the snapshot length, because they might
  642|       |		 * be assuming they'll never be handed such a packet,
  643|       |		 * and might copy the packet into a snapshot-length-
  644|       |		 * sized buffer, assuming it'll fit.
  645|       |		 */
  646|  3.64k|		size_t bytes_to_discard;
  647|  3.64k|		size_t bytes_to_read, bytes_read;
  648|  3.64k|		char discard_buf[4096];
  649|       |
  650|  3.64k|		if (hdr->caplen > p->bufsize) {
  ------------------
  |  Branch (650:7): [True: 3.64k, False: 0]
  ------------------
  651|       |			/*
  652|       |			 * Grow the buffer to the snapshot length.
  653|       |			 */
  654|  3.64k|			if (!pcapint_grow_buffer(p, p->snapshot, p->errbuf))
  ------------------
  |  Branch (654:8): [True: 0, False: 3.64k]
  ------------------
  655|      0|				return (-1);
  656|  3.64k|		}
  657|       |
  658|       |		/*
  659|       |		 * Read the first p->snapshot bytes into the buffer.
  660|       |		 */
  661|  3.64k|		amt_read = fread(p->buffer, 1, p->snapshot, fp);
  662|  3.64k|		if (amt_read != (bpf_u_int32)p->snapshot) {
  ------------------
  |  Branch (662:7): [True: 48, False: 3.59k]
  ------------------
  663|     48|			if (ferror(fp)) {
  ------------------
  |  Branch (663:8): [True: 0, False: 48]
  ------------------
  664|      0|				pcapint_fmt_errmsg_for_errno(p->errbuf,
  665|      0|				     PCAP_ERRBUF_SIZE, errno,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  666|      0|				    "error reading dump file");
  667|     48|			} else {
  668|       |				/*
  669|       |				 * Yes, this uses hdr->caplen; technically,
  670|       |				 * it's true, because we would try to read
  671|       |				 * and discard the rest of those bytes, and
  672|       |				 * that would fail because we got EOF before
  673|       |				 * the read finished.
  674|       |				 */
  675|     48|				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     48|#define PCAP_ERRBUF_SIZE 256
  ------------------
  676|     48|				    "truncated dump file; tried to read %d captured bytes, only got %zu",
  677|     48|				    p->snapshot, amt_read);
  678|     48|			}
  679|     48|			return (-1);
  680|     48|		}
  681|       |
  682|       |		/*
  683|       |		 * Now read and discard what's left.
  684|       |		 */
  685|  3.59k|		bytes_to_discard = hdr->caplen - p->snapshot;
  686|  3.59k|		bytes_read = amt_read;
  687|  7.51k|		while (bytes_to_discard != 0) {
  ------------------
  |  Branch (687:10): [True: 3.97k, False: 3.54k]
  ------------------
  688|  3.97k|			bytes_to_read = bytes_to_discard;
  689|  3.97k|			if (bytes_to_read > sizeof (discard_buf))
  ------------------
  |  Branch (689:8): [True: 406, False: 3.56k]
  ------------------
  690|    406|				bytes_to_read = sizeof (discard_buf);
  691|  3.97k|			amt_read = fread(discard_buf, 1, bytes_to_read, fp);
  692|  3.97k|			bytes_read += amt_read;
  693|  3.97k|			if (amt_read != bytes_to_read) {
  ------------------
  |  Branch (693:8): [True: 53, False: 3.91k]
  ------------------
  694|     53|				if (ferror(fp)) {
  ------------------
  |  Branch (694:9): [True: 0, False: 53]
  ------------------
  695|      0|					pcapint_fmt_errmsg_for_errno(p->errbuf,
  696|      0|					    PCAP_ERRBUF_SIZE, errno,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  697|      0|					    "error reading dump file");
  698|     53|				} else {
  699|     53|					snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     53|#define PCAP_ERRBUF_SIZE 256
  ------------------
  700|     53|					    "truncated dump file; tried to read %u captured bytes, only got %zu",
  701|     53|					    hdr->caplen, bytes_read);
  702|     53|				}
  703|     53|				return (-1);
  704|     53|			}
  705|  3.91k|			bytes_to_discard -= amt_read;
  706|  3.91k|		}
  707|       |
  708|       |		/*
  709|       |		 * Adjust caplen accordingly, so we don't get confused later
  710|       |		 * as to how many bytes we have to play with.
  711|       |		 */
  712|  3.54k|		hdr->caplen = p->snapshot;
  713|  37.2k|	} else {
  714|       |		/*
  715|       |		 * The packet is within the snapshot length for this file.
  716|       |		 */
  717|  37.2k|		if (hdr->caplen > p->bufsize) {
  ------------------
  |  Branch (717:7): [True: 446, False: 36.7k]
  ------------------
  718|       |			/*
  719|       |			 * Grow the buffer to the next power of 2, or
  720|       |			 * the snaplen, whichever is lower.
  721|       |			 */
  722|    446|			u_int new_bufsize;
  723|       |
  724|    446|			new_bufsize = hdr->caplen;
  725|       |			/*
  726|       |			 * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  727|       |			 */
  728|    446|			new_bufsize--;
  729|    446|			new_bufsize |= new_bufsize >> 1;
  730|    446|			new_bufsize |= new_bufsize >> 2;
  731|    446|			new_bufsize |= new_bufsize >> 4;
  732|    446|			new_bufsize |= new_bufsize >> 8;
  733|    446|			new_bufsize |= new_bufsize >> 16;
  734|    446|			new_bufsize++;
  735|       |
  736|    446|			if (new_bufsize > (u_int)p->snapshot)
  ------------------
  |  Branch (736:8): [True: 35, False: 411]
  ------------------
  737|     35|				new_bufsize = p->snapshot;
  738|       |
  739|    446|			if (!pcapint_grow_buffer(p, new_bufsize, p->errbuf))
  ------------------
  |  Branch (739:8): [True: 0, False: 446]
  ------------------
  740|      0|				return (-1);
  741|    446|		}
  742|       |
  743|       |		/* read the packet itself */
  744|  37.2k|		amt_read = fread(p->buffer, 1, hdr->caplen, fp);
  745|  37.2k|		if (amt_read != hdr->caplen) {
  ------------------
  |  Branch (745:7): [True: 90, False: 37.1k]
  ------------------
  746|     90|			if (ferror(fp)) {
  ------------------
  |  Branch (746:8): [True: 0, False: 90]
  ------------------
  747|      0|				pcapint_fmt_errmsg_for_errno(p->errbuf,
  748|      0|				    PCAP_ERRBUF_SIZE, errno,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  749|      0|				    "error reading dump file");
  750|     90|			} else {
  751|     90|				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     90|#define PCAP_ERRBUF_SIZE 256
  ------------------
  752|     90|				    "truncated dump file; tried to read %u captured bytes, only got %zu",
  753|     90|				    hdr->caplen, amt_read);
  754|     90|			}
  755|     90|			return (-1);
  756|     90|		}
  757|  37.2k|	}
  758|  40.6k|	*data = p->buffer;
  759|       |
  760|  40.6k|	pcapint_post_process(p->linktype, p->swapped, hdr, *data);
  761|       |
  762|  40.6k|	return (1);
  763|  40.8k|}

pcap_ng_check_header:
  778|  1.48k|{
  779|  1.48k|	bpf_u_int32 magic_int;
  780|  1.48k|	size_t amt_read;
  781|  1.48k|	bpf_u_int32 total_length;
  782|  1.48k|	bpf_u_int32 byte_order_magic;
  783|  1.48k|	struct block_header *bhdrp;
  784|  1.48k|	struct section_header_block *shbp;
  785|  1.48k|	pcap_t *p;
  786|  1.48k|	int swapped = 0;
  787|  1.48k|	struct pcap_ng_sf *ps;
  788|  1.48k|	int status;
  789|  1.48k|	struct block_cursor cursor;
  790|  1.48k|	struct interface_description_block *idbp;
  791|       |
  792|       |	/*
  793|       |	 * Assume no read errors.
  794|       |	 */
  795|  1.48k|	*err = 0;
  796|       |
  797|       |	/*
  798|       |	 * Check whether the first 4 bytes of the file are the block
  799|       |	 * type for a pcapng savefile.
  800|       |	 */
  801|  1.48k|	memcpy(&magic_int, magic, sizeof(magic_int));
  802|  1.48k|	if (magic_int != BT_SHB) {
  ------------------
  |  |   86|  1.48k|#define BT_SHB			0x0A0D0D0A
  ------------------
  |  Branch (802:6): [True: 218, False: 1.26k]
  ------------------
  803|       |		/*
  804|       |		 * XXX - check whether this looks like what the block
  805|       |		 * type would be after being munged by mapping between
  806|       |		 * UN*X and DOS/Windows text file format and, if it
  807|       |		 * does, look for the byte-order magic number in
  808|       |		 * the appropriate place and, if we find it, report
  809|       |		 * this as possibly being a pcapng file transferred
  810|       |		 * between UN*X and Windows in text file format?
  811|       |		 */
  812|    218|		return (NULL);	/* nope */
  813|    218|	}
  814|       |
  815|       |	/*
  816|       |	 * OK, they are.  However, that's just \n\r\r\n, so it could,
  817|       |	 * conceivably, be an ordinary text file.
  818|       |	 *
  819|       |	 * It could not, however, conceivably be any other type of
  820|       |	 * capture file, so we can read the rest of the putative
  821|       |	 * Section Header Block; put the block type in the common
  822|       |	 * header, read the rest of the common header and the
  823|       |	 * fixed-length portion of the SHB, and look for the byte-order
  824|       |	 * magic value.
  825|       |	 */
  826|  1.26k|	amt_read = fread(&total_length, 1, sizeof(total_length), fp);
  827|  1.26k|	if (amt_read < sizeof(total_length)) {
  ------------------
  |  Branch (827:6): [True: 0, False: 1.26k]
  ------------------
  828|      0|		if (ferror(fp)) {
  ------------------
  |  Branch (828:7): [True: 0, False: 0]
  ------------------
  829|      0|			pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  830|      0|			    errno, "error reading dump file");
  831|      0|			*err = 1;
  832|      0|			return (NULL);	/* fail */
  833|      0|		}
  834|       |
  835|       |		/*
  836|       |		 * Possibly a weird short text file, so just say
  837|       |		 * "not pcapng".
  838|       |		 */
  839|      0|		return (NULL);
  840|      0|	}
  841|  1.26k|	amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp);
  842|  1.26k|	if (amt_read < sizeof(byte_order_magic)) {
  ------------------
  |  Branch (842:6): [True: 0, False: 1.26k]
  ------------------
  843|      0|		if (ferror(fp)) {
  ------------------
  |  Branch (843:7): [True: 0, False: 0]
  ------------------
  844|      0|			pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  845|      0|			    errno, "error reading dump file");
  846|      0|			*err = 1;
  847|      0|			return (NULL);	/* fail */
  848|      0|		}
  849|       |
  850|       |		/*
  851|       |		 * Possibly a weird short text file, so just say
  852|       |		 * "not pcapng".
  853|       |		 */
  854|      0|		return (NULL);
  855|      0|	}
  856|  1.26k|	if (byte_order_magic != BYTE_ORDER_MAGIC) {
  ------------------
  |  |   99|  1.26k|#define BYTE_ORDER_MAGIC	0x1A2B3C4D
  ------------------
  |  Branch (856:6): [True: 452, False: 817]
  ------------------
  857|    452|		byte_order_magic = PCAP_BSWAP_32(byte_order_magic);
  ------------------
  |  |   99|    452|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  858|    452|		if (byte_order_magic != BYTE_ORDER_MAGIC) {
  ------------------
  |  |   99|    452|#define BYTE_ORDER_MAGIC	0x1A2B3C4D
  ------------------
  |  Branch (858:7): [True: 84, False: 368]
  ------------------
  859|       |			/*
  860|       |			 * Not a pcapng file.
  861|       |			 */
  862|     84|			return (NULL);
  863|     84|		}
  864|    368|		swapped = 1;
  865|    368|		total_length = PCAP_BSWAP_32(total_length);
  ------------------
  |  |   99|    368|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  866|    368|	}
  867|       |
  868|       |	/*
  869|       |	 * Check the sanity of the total length.
  870|       |	 */
  871|  1.18k|	if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer) ||
  ------------------
  |  Branch (871:6): [True: 4, False: 1.18k]
  ------------------
  872|  1.18k|            (total_length > BT_SHB_INSANE_MAX)) {
  ------------------
  |  |   87|  1.18k|#define BT_SHB_INSANE_MAX       1024U*1024U*1U  /* 1MB should be enough */
  ------------------
  |  Branch (872:13): [True: 35, False: 1.14k]
  ------------------
  873|     39|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     39|#define PCAP_ERRBUF_SIZE 256
  ------------------
  874|     39|		    "Section Header Block in pcapng dump file has invalid length %zu < _%u_ < %u (BT_SHB_INSANE_MAX)",
  875|     39|		    sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer),
  876|     39|		    total_length,
  877|     39|		    BT_SHB_INSANE_MAX);
  ------------------
  |  |   87|     39|#define BT_SHB_INSANE_MAX       1024U*1024U*1U  /* 1MB should be enough */
  ------------------
  878|       |
  879|     39|		*err = 1;
  880|     39|		return (NULL);
  881|     39|	}
  882|       |
  883|       |	/*
  884|       |	 * OK, this is a good pcapng file.
  885|       |	 * Allocate a pcap_t for it.
  886|       |	 */
  887|  1.14k|	p = PCAP_OPEN_OFFLINE_COMMON(errbuf, struct pcap_ng_sf);
  ------------------
  |  |  589|  1.14k|	pcapint_open_offline_common(ebuf, \
  |  |  590|  1.14k|	    sizeof (struct { pcap_t __common; type __private; }), \
  |  |  591|  1.14k|	    offsetof (struct { pcap_t __common; type __private; }, __private))
  ------------------
  888|  1.14k|	if (p == NULL) {
  ------------------
  |  Branch (888:6): [True: 0, False: 1.14k]
  ------------------
  889|       |		/* Allocation failed. */
  890|      0|		*err = 1;
  891|      0|		return (NULL);
  892|      0|	}
  893|  1.14k|	p->swapped = swapped;
  894|  1.14k|	ps = p->priv;
  895|       |
  896|       |	/*
  897|       |	 * What precision does the user want?
  898|       |	 */
  899|  1.14k|	switch (precision) {
  900|       |
  901|  1.14k|	case PCAP_TSTAMP_PRECISION_MICRO:
  ------------------
  |  |  539|  1.14k|#define PCAP_TSTAMP_PRECISION_MICRO	0	/* use timestamps with microsecond precision, default */
  ------------------
  |  Branch (901:2): [True: 1.14k, False: 0]
  ------------------
  902|  1.14k|		ps->user_tsresol = 1000000;
  903|  1.14k|		break;
  904|       |
  905|      0|	case PCAP_TSTAMP_PRECISION_NANO:
  ------------------
  |  |  540|      0|#define PCAP_TSTAMP_PRECISION_NANO	1	/* use timestamps with nanosecond precision */
  ------------------
  |  Branch (905:2): [True: 0, False: 1.14k]
  ------------------
  906|      0|		ps->user_tsresol = 1000000000;
  907|      0|		break;
  908|       |
  909|      0|	default:
  ------------------
  |  Branch (909:2): [True: 0, False: 1.14k]
  ------------------
  910|      0|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  911|      0|		    "unknown time stamp resolution %u", precision);
  912|      0|		free(p);
  913|      0|		*err = 1;
  914|      0|		return (NULL);
  915|  1.14k|	}
  916|       |
  917|  1.14k|	p->opt.tstamp_precision = precision;
  918|       |
  919|       |	/*
  920|       |	 * Allocate a buffer into which to read blocks.  We default to
  921|       |	 * the maximum of:
  922|       |	 *
  923|       |	 *	the total length of the SHB for which we read the header;
  924|       |	 *
  925|       |	 *	2K, which should be more than large enough for an Enhanced
  926|       |	 *	Packet Block containing a full-size Ethernet frame, and
  927|       |	 *	leaving room for some options.
  928|       |	 *
  929|       |	 * If we find a bigger block, we reallocate the buffer, up to
  930|       |	 * the maximum size.  We start out with a maximum size of
  931|       |	 * INITIAL_MAX_BLOCKSIZE; if we see any link-layer header types
  932|       |	 * with a maximum snapshot that results in a larger maximum
  933|       |	 * block length, we boost the maximum.
  934|       |	 */
  935|  1.14k|	p->bufsize = 2048;
  936|  1.14k|	if (p->bufsize < total_length)
  ------------------
  |  Branch (936:6): [True: 48, False: 1.09k]
  ------------------
  937|     48|		p->bufsize = total_length;
  938|  1.14k|	p->buffer = malloc(p->bufsize);
  939|  1.14k|	if (p->buffer == NULL) {
  ------------------
  |  Branch (939:6): [True: 0, False: 1.14k]
  ------------------
  940|      0|		snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  941|      0|		free(p);
  942|      0|		*err = 1;
  943|      0|		return (NULL);
  944|      0|	}
  945|  1.14k|	ps->max_blocksize = INITIAL_MAX_BLOCKSIZE;
  ------------------
  |  |  238|  1.14k|#define INITIAL_MAX_BLOCKSIZE	(16*1024*1024)
  ------------------
  946|       |
  947|       |	/*
  948|       |	 * Copy the stuff we've read to the buffer, and read the rest
  949|       |	 * of the SHB.
  950|       |	 */
  951|  1.14k|	bhdrp = (struct block_header *)p->buffer;
  952|  1.14k|	shbp = (struct section_header_block *)(p->buffer + sizeof(struct block_header));
  953|  1.14k|	bhdrp->block_type = magic_int;
  954|  1.14k|	bhdrp->total_length = total_length;
  955|  1.14k|	shbp->byte_order_magic = byte_order_magic;
  956|  1.14k|	if (read_bytes(fp,
  ------------------
  |  Branch (956:6): [True: 58, False: 1.08k]
  ------------------
  957|  1.14k|	    p->buffer + (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
  958|  1.14k|	    total_length - (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
  959|  1.14k|	    1, errbuf) == -1)
  960|     58|		goto fail;
  961|       |
  962|  1.08k|	if (p->swapped) {
  ------------------
  |  Branch (962:6): [True: 350, False: 738]
  ------------------
  963|       |		/*
  964|       |		 * Byte-swap the fields we've read.
  965|       |		 */
  966|    350|		shbp->major_version = PCAP_BSWAP_16(shbp->major_version);
  ------------------
  |  |  109|    350|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  967|    350|		shbp->minor_version = PCAP_BSWAP_16(shbp->minor_version);
  ------------------
  |  |  109|    350|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  968|       |
  969|       |		/*
  970|       |		 * XXX - we don't care about the section length.
  971|       |		 */
  972|    350|	}
  973|       |	/* Currently only SHB versions 1.0 and 1.2 are supported;
  974|       |	   version 1.2 is treated as being the same as version 1.0.
  975|       |	   See the current version of the pcapng specification.
  976|       |
  977|       |	   Version 1.2 is written by some programs that write additional
  978|       |	   block types (which can be read by any code that handles them,
  979|       |	   regardless of whether the minor version if 0 or 2, so that's
  980|       |	   not a reason to change the minor version number).
  981|       |
  982|       |	   XXX - the pcapng specification says that readers should
  983|       |	   just ignore sections with an unsupported version number;
  984|       |	   presumably they can also report an error if they skip
  985|       |	   all the way to the end of the file without finding
  986|       |	   any versions that they support. */
  987|  1.08k|	if (! (shbp->major_version == PCAP_NG_VERSION_MAJOR &&
  ------------------
  |  |  106|  2.17k|#define PCAP_NG_VERSION_MAJOR	1
  ------------------
  |  Branch (987:9): [True: 1.07k, False: 17]
  ------------------
  988|  1.07k|	       (shbp->minor_version == PCAP_NG_VERSION_MINOR ||
  ------------------
  |  |  107|  2.14k|#define PCAP_NG_VERSION_MINOR	0
  ------------------
  |  Branch (988:10): [True: 1.03k, False: 36]
  ------------------
  989|     36|	        shbp->minor_version == 2))) {
  ------------------
  |  Branch (989:10): [True: 18, False: 18]
  ------------------
  990|     35|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     35|#define PCAP_ERRBUF_SIZE 256
  ------------------
  991|     35|		    "unsupported pcapng savefile version %u.%u",
  992|     35|		    shbp->major_version, shbp->minor_version);
  993|     35|		goto fail;
  994|     35|	}
  995|  1.05k|	p->version_major = shbp->major_version;
  996|  1.05k|	p->version_minor = shbp->minor_version;
  997|       |
  998|       |	/*
  999|       |	 * Save the time stamp resolution the user requested.
 1000|       |	 */
 1001|  1.05k|	p->opt.tstamp_precision = precision;
 1002|       |
 1003|       |	/*
 1004|       |	 * Now start looking for an Interface Description Block.
 1005|       |	 */
 1006|  1.59k|	for (;;) {
 1007|       |		/*
 1008|       |		 * Read the next block.
 1009|       |		 */
 1010|  1.59k|		status = read_block(fp, p, &cursor, errbuf);
 1011|  1.59k|		if (status == 0) {
  ------------------
  |  Branch (1011:7): [True: 19, False: 1.57k]
  ------------------
 1012|       |			/* EOF - no IDB in this file */
 1013|     19|			snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     19|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1014|     19|			    "the capture file has no Interface Description Blocks");
 1015|     19|			goto fail;
 1016|     19|		}
 1017|  1.57k|		if (status == -1)
  ------------------
  |  Branch (1017:7): [True: 166, False: 1.41k]
  ------------------
 1018|    166|			goto fail;	/* error */
 1019|  1.41k|		switch (cursor.block_type) {
 1020|       |
 1021|    865|		case BT_IDB:
  ------------------
  |  |  112|    865|#define BT_IDB			0x00000001
  ------------------
  |  Branch (1021:3): [True: 865, False: 547]
  ------------------
 1022|       |			/*
 1023|       |			 * Get a pointer to the fixed-length portion of the
 1024|       |			 * IDB.
 1025|       |			 */
 1026|    865|			idbp = get_from_block_data(&cursor, sizeof(*idbp),
 1027|    865|			    errbuf);
 1028|    865|			if (idbp == NULL)
  ------------------
  |  Branch (1028:8): [True: 1, False: 864]
  ------------------
 1029|      1|				goto fail;	/* error */
 1030|       |
 1031|       |			/*
 1032|       |			 * Byte-swap it if necessary.
 1033|       |			 */
 1034|    864|			if (p->swapped) {
  ------------------
  |  Branch (1034:8): [True: 270, False: 594]
  ------------------
 1035|    270|				idbp->linktype = PCAP_BSWAP_16(idbp->linktype);
  ------------------
  |  |  109|    270|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
 1036|    270|				idbp->snaplen = PCAP_BSWAP_32(idbp->snaplen);
  ------------------
  |  |   99|    270|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1037|    270|			}
 1038|       |
 1039|       |			/*
 1040|       |			 * Try to add this interface.
 1041|       |			 */
 1042|    864|			if (!add_interface(p, idbp, &cursor, errbuf))
  ------------------
  |  Branch (1042:8): [True: 66, False: 798]
  ------------------
 1043|     66|				goto fail;
 1044|       |
 1045|    798|			goto done;
 1046|       |
 1047|    798|		case BT_EPB:
  ------------------
  |  |  141|      1|#define BT_EPB			0x00000006
  ------------------
  |  Branch (1047:3): [True: 1, False: 1.41k]
  ------------------
 1048|      2|		case BT_SPB:
  ------------------
  |  |  155|      2|#define BT_SPB			0x00000003
  ------------------
  |  Branch (1048:3): [True: 1, False: 1.41k]
  ------------------
 1049|      3|		case BT_PB:
  ------------------
  |  |  165|      3|#define BT_PB			0x00000002
  ------------------
  |  Branch (1049:3): [True: 1, False: 1.41k]
  ------------------
 1050|       |			/*
 1051|       |			 * Saw a packet before we saw any IDBs.  That's
 1052|       |			 * not valid, as we don't know what link-layer
 1053|       |			 * encapsulation the packet has.
 1054|       |			 */
 1055|      3|			snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      3|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1056|      3|			    "the capture file has a packet block before any Interface Description Blocks");
 1057|      3|			goto fail;
 1058|       |
 1059|    544|		default:
  ------------------
  |  Branch (1059:3): [True: 544, False: 868]
  ------------------
 1060|       |			/*
 1061|       |			 * Just ignore it.
 1062|       |			 */
 1063|    544|			break;
 1064|  1.41k|		}
 1065|  1.41k|	}
 1066|       |
 1067|    798|done:
 1068|    798|	p->linktype = linktype_to_dlt(idbp->linktype);
 1069|    798|	p->snapshot = pcapint_adjust_snapshot(p->linktype, idbp->snaplen);
 1070|    798|	p->linktype_ext = 0;
 1071|       |
 1072|       |	/*
 1073|       |	 * If the maximum block size for a packet with the maximum
 1074|       |	 * snapshot length for this DLT_ is bigger than the current
 1075|       |	 * maximum block size, increase the maximum.
 1076|       |	 */
 1077|    798|	if (MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype)) > ps->max_blocksize)
  ------------------
  |  |  246|    798|	(sizeof (struct block_header) + \
  |  |  247|    798|	 sizeof (struct enhanced_packet_block) + \
  |  |  248|    798|	 (max_snaplen) + 131072 + \
  |  |  249|    798|	 sizeof (struct block_trailer))
  ------------------
  |  Branch (1077:6): [True: 34, False: 764]
  ------------------
 1078|     34|		ps->max_blocksize = MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype));
  ------------------
  |  |  246|     34|	(sizeof (struct block_header) + \
  |  |  247|     34|	 sizeof (struct enhanced_packet_block) + \
  |  |  248|     34|	 (max_snaplen) + 131072 + \
  |  |  249|     34|	 sizeof (struct block_trailer))
  ------------------
 1079|       |
 1080|    798|	p->next_packet_op = pcap_ng_next_packet;
 1081|    798|	p->cleanup_op = pcap_ng_cleanup;
 1082|       |
 1083|    798|	return (p);
 1084|       |
 1085|    348|fail:
 1086|    348|	free(ps->ifaces);
 1087|    348|	free(p->buffer);
 1088|    348|	free(p);
 1089|    348|	*err = 1;
 1090|       |	return (NULL);
 1091|  1.05k|}
sf-pcapng.c:read_bytes:
  258|  16.7k|{
  259|  16.7k|	size_t amt_read;
  260|       |
  261|  16.7k|	amt_read = fread(buf, 1, bytes_to_read, fp);
  262|  16.7k|	if (amt_read != bytes_to_read) {
  ------------------
  |  Branch (262:6): [True: 504, False: 16.2k]
  ------------------
  263|    504|		if (ferror(fp)) {
  ------------------
  |  Branch (263:7): [True: 0, False: 504]
  ------------------
  264|      0|			pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  265|      0|			    errno, "error reading dump file");
  266|    504|		} else {
  267|    504|			if (amt_read == 0 && !fail_on_eof)
  ------------------
  |  Branch (267:8): [True: 401, False: 103]
  |  Branch (267:25): [True: 365, False: 36]
  ------------------
  268|    365|				return (0);	/* EOF */
  269|    139|			snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|    139|#define PCAP_ERRBUF_SIZE 256
  ------------------
  270|    139|			    "truncated pcapng dump file; tried to read %zu bytes, only got %zu",
  271|    139|			    bytes_to_read, amt_read);
  272|    139|		}
  273|    139|		return (-1);
  274|    504|	}
  275|  16.2k|	return (1);
  276|  16.7k|}
sf-pcapng.c:read_block:
  280|  8.02k|{
  281|  8.02k|	struct pcap_ng_sf *ps;
  282|  8.02k|	int status;
  283|  8.02k|	struct block_header bhdr;
  284|  8.02k|	struct block_trailer *btrlr;
  285|  8.02k|	u_char *bdata;
  286|  8.02k|	size_t data_remaining;
  287|       |
  288|  8.02k|	ps = p->priv;
  289|       |
  290|  8.02k|	status = read_bytes(fp, &bhdr, sizeof(bhdr), 0, errbuf);
  291|  8.02k|	if (status <= 0)
  ------------------
  |  Branch (291:6): [True: 399, False: 7.62k]
  ------------------
  292|    399|		return (status);	/* error or EOF */
  293|       |
  294|  7.62k|	if (p->swapped) {
  ------------------
  |  Branch (294:6): [True: 2.44k, False: 5.17k]
  ------------------
  295|  2.44k|		bhdr.block_type = PCAP_BSWAP_32(bhdr.block_type);
  ------------------
  |  |   99|  2.44k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  296|  2.44k|		bhdr.total_length = PCAP_BSWAP_32(bhdr.total_length);
  ------------------
  |  |   99|  2.44k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  297|  2.44k|	}
  298|       |
  299|       |	/*
  300|       |	 * Is this block "too small" - i.e., is it shorter than a block
  301|       |	 * header plus a block trailer?
  302|       |	 */
  303|  7.62k|	if (bhdr.total_length < sizeof(struct block_header) +
  ------------------
  |  Branch (303:6): [True: 7, False: 7.61k]
  ------------------
  304|  7.62k|	    sizeof(struct block_trailer)) {
  305|      7|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      7|#define PCAP_ERRBUF_SIZE 256
  ------------------
  306|      7|		    "block in pcapng dump file has a length of %u < %zu",
  307|      7|		    bhdr.total_length,
  308|      7|		    sizeof(struct block_header) + sizeof(struct block_trailer));
  309|      7|		return (-1);
  310|      7|	}
  311|       |
  312|       |	/*
  313|       |	 * Is the block total length a multiple of 4?
  314|       |	 */
  315|  7.61k|	if ((bhdr.total_length % 4) != 0) {
  ------------------
  |  Branch (315:6): [True: 8, False: 7.60k]
  ------------------
  316|       |		/*
  317|       |		 * No.  Report that as an error.
  318|       |		 */
  319|      8|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      8|#define PCAP_ERRBUF_SIZE 256
  ------------------
  320|      8|		    "block in pcapng dump file has a length of %u that is not a multiple of 4",
  321|      8|		    bhdr.total_length);
  322|      8|		return (-1);
  323|      8|	}
  324|       |
  325|       |	/*
  326|       |	 * Is the buffer big enough?
  327|       |	 */
  328|  7.60k|	if (p->bufsize < bhdr.total_length) {
  ------------------
  |  Branch (328:6): [True: 135, False: 7.47k]
  ------------------
  329|       |		/*
  330|       |		 * No - make it big enough, unless it's too big, in
  331|       |		 * which case we fail.
  332|       |		 */
  333|    135|		if (bhdr.total_length > ps->max_blocksize) {
  ------------------
  |  Branch (333:7): [True: 69, False: 66]
  ------------------
  334|     69|			snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcapng block size %u > maximum %u", bhdr.total_length,
  ------------------
  |  |  149|     69|#define PCAP_ERRBUF_SIZE 256
  ------------------
  335|     69|			    ps->max_blocksize);
  336|     69|			return (-1);
  337|     69|		}
  338|     66|		if (!pcapint_grow_buffer(p, bhdr.total_length, errbuf))
  ------------------
  |  Branch (338:7): [True: 0, False: 66]
  ------------------
  339|      0|			return (-1);
  340|     66|	}
  341|       |
  342|       |	/*
  343|       |	 * Copy the stuff we've read to the buffer, and read the rest
  344|       |	 * of the block.
  345|       |	 */
  346|  7.53k|	memcpy(p->buffer, &bhdr, sizeof(bhdr));
  347|  7.53k|	bdata = p->buffer + sizeof(bhdr);
  348|  7.53k|	data_remaining = bhdr.total_length - sizeof(bhdr);
  349|  7.53k|	if (read_bytes(fp, bdata, data_remaining, 1, errbuf) == -1)
  ------------------
  |  Branch (349:6): [True: 47, False: 7.49k]
  ------------------
  350|     47|		return (-1);
  351|       |
  352|       |	/*
  353|       |	 * Get the block size from the trailer.
  354|       |	 */
  355|  7.49k|	btrlr = (struct block_trailer *)(bdata + data_remaining - sizeof (struct block_trailer));
  356|  7.49k|	if (p->swapped)
  ------------------
  |  Branch (356:6): [True: 2.40k, False: 5.09k]
  ------------------
  357|  2.40k|		btrlr->total_length = PCAP_BSWAP_32(btrlr->total_length);
  ------------------
  |  |   99|  2.40k|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  358|       |
  359|       |	/*
  360|       |	 * Is the total length from the trailer the same as the total
  361|       |	 * length from the header?
  362|       |	 */
  363|  7.49k|	if (bhdr.total_length != btrlr->total_length) {
  ------------------
  |  Branch (363:6): [True: 44, False: 7.44k]
  ------------------
  364|       |		/*
  365|       |		 * No.
  366|       |		 */
  367|     44|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     44|#define PCAP_ERRBUF_SIZE 256
  ------------------
  368|     44|		    "block total length in header %u and trailer %u don't match",
  369|     44|		    bhdr.total_length, btrlr->total_length);
  370|     44|		return (-1);
  371|     44|	}
  372|       |
  373|       |	/*
  374|       |	 * Initialize the cursor.
  375|       |	 */
  376|  7.44k|	cursor->data = bdata;
  377|  7.44k|	cursor->data_remaining = data_remaining - sizeof(struct block_trailer);
  378|  7.44k|	cursor->block_type = bhdr.block_type;
  379|  7.44k|	return (1);
  380|  7.49k|}
sf-pcapng.c:get_from_block_data:
  385|  13.9k|{
  386|  13.9k|	void *data;
  387|       |
  388|       |	/*
  389|       |	 * Make sure we have the specified amount of data remaining in
  390|       |	 * the block data.
  391|       |	 */
  392|  13.9k|	if (cursor->data_remaining < chunk_size) {
  ------------------
  |  Branch (392:6): [True: 118, False: 13.7k]
  ------------------
  393|    118|		snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|    118|#define PCAP_ERRBUF_SIZE 256
  ------------------
  394|    118|		    "block of type %u in pcapng dump file is too short",
  395|    118|		    cursor->block_type);
  396|    118|		return (NULL);
  397|    118|	}
  398|       |
  399|       |	/*
  400|       |	 * Return the current pointer, and skip past the chunk.
  401|       |	 */
  402|  13.7k|	data = cursor->data;
  403|  13.7k|	cursor->data += chunk_size;
  404|  13.7k|	cursor->data_remaining -= chunk_size;
  405|  13.7k|	return (data);
  406|  13.9k|}
sf-pcapng.c:add_interface:
  588|  3.52k|{
  589|  3.52k|	struct pcap_ng_sf *ps;
  590|  3.52k|	bpf_u_int32 new_ifcount;
  591|  3.52k|	bpf_u_int32 new_if_index;
  592|  3.52k|	uint64_t tsresol;
  593|  3.52k|	int64_t tsoffset;
  594|  3.52k|	int is_binary;
  595|       |
  596|  3.52k|	ps = p->priv;
  597|       |
  598|       |	/*
  599|       |	 * Count this interface.
  600|       |	 */
  601|  3.52k|	new_ifcount = ps->ifcount + 1;
  602|       |
  603|       |	/*
  604|       |	 * The array of interfaces is 0-based, so the current number of
  605|       |	 * interfaces will be the index of the new interface.
  606|       |	 */
  607|  3.52k|	new_if_index = ps->ifcount;
  608|       |
  609|       |	/*
  610|       |	 * Grow the array of per-interface information as necessary.
  611|       |	 */
  612|  3.52k|	if (new_ifcount > ps->ifaces_size) {
  ------------------
  |  Branch (612:6): [True: 1.23k, False: 2.29k]
  ------------------
  613|       |		/*
  614|       |		 * We need to grow the array.
  615|       |		 */
  616|  1.23k|		bpf_u_int32 new_ifaces_size;
  617|  1.23k|		struct pcap_ng_if *new_ifaces;
  618|       |
  619|  1.23k|		if (ps->ifaces_size == 0) {
  ------------------
  |  Branch (619:7): [True: 864, False: 369]
  ------------------
  620|       |			/*
  621|       |			 * It's currently empty.
  622|       |			 *
  623|       |			 * (The Clang static analyzer doesn't do enough,
  624|       |			 * err, umm, dataflow *analysis* to realize that
  625|       |			 * ps->ifaces_size == 0 if ps->ifaces == NULL,
  626|       |			 * and so complains about a possible zero argument
  627|       |			 * to realloc(), so we check for the former
  628|       |			 * condition to shut it up.
  629|       |			 *
  630|       |			 * However, it doesn't complain that one of the
  631|       |			 * multiplications below could overflow, which is
  632|       |			 * a real, albeit extremely unlikely, problem (you'd
  633|       |			 * need a pcapng file with tens of millions of
  634|       |			 * interfaces).)
  635|       |			 */
  636|    864|			new_ifaces_size = 1;
  637|    864|			new_ifaces = malloc(sizeof (struct pcap_ng_if));
  638|    864|		} else {
  639|       |			/*
  640|       |			 * It's not currently empty; double its size.
  641|       |			 * (Perhaps overkill once we have a lot of interfaces.)
  642|       |			 *
  643|       |			 * Check for overflow if we double it.
  644|       |			 */
  645|    369|			if (ps->ifaces_size * 2 < ps->ifaces_size) {
  ------------------
  |  Branch (645:8): [True: 0, False: 369]
  ------------------
  646|       |				/*
  647|       |				 * The maximum number of interfaces before
  648|       |				 * ps->ifaces_size overflows is the largest
  649|       |				 * possible 32-bit power of 2, as we do
  650|       |				 * size doubling.
  651|       |				 */
  652|      0|				snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  653|      0|				    "more than %u interfaces in the file",
  654|      0|				    0x80000000U);
  655|      0|				return (0);
  656|      0|			}
  657|       |
  658|       |			/*
  659|       |			 * ps->ifaces_size * 2 doesn't overflow, so it's
  660|       |			 * safe to multiply.
  661|       |			 */
  662|    369|			new_ifaces_size = ps->ifaces_size * 2;
  663|       |
  664|       |			/*
  665|       |			 * Now make sure that's not so big that it overflows
  666|       |			 * if we multiply by sizeof (struct pcap_ng_if).
  667|       |			 *
  668|       |			 * That can happen on 32-bit platforms, with a 32-bit
  669|       |			 * size_t; it shouldn't happen on 64-bit platforms,
  670|       |			 * with a 64-bit size_t, as new_ifaces_size is
  671|       |			 * 32 bits.
  672|       |			 */
  673|    369|			if (new_ifaces_size * sizeof (struct pcap_ng_if) < new_ifaces_size) {
  ------------------
  |  Branch (673:8): [True: 0, False: 369]
  ------------------
  674|       |				/*
  675|       |				 * As this fails only with 32-bit size_t,
  676|       |				 * the multiplication was 32x32->32, and
  677|       |				 * the largest 32-bit value that can safely
  678|       |				 * be multiplied by sizeof (struct pcap_ng_if)
  679|       |				 * without overflow is the largest 32-bit
  680|       |				 * (unsigned) value divided by
  681|       |				 * sizeof (struct pcap_ng_if).
  682|       |				 */
  683|      0|				snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  684|      0|				    "more than %u interfaces in the file",
  685|      0|				    0xFFFFFFFFU / ((u_int)sizeof (struct pcap_ng_if)));
  686|      0|				return (0);
  687|      0|			}
  688|    369|			new_ifaces = realloc(ps->ifaces, new_ifaces_size * sizeof (struct pcap_ng_if));
  689|    369|		}
  690|  1.23k|		if (new_ifaces == NULL) {
  ------------------
  |  Branch (690:7): [True: 0, False: 1.23k]
  ------------------
  691|       |			/*
  692|       |			 * We ran out of memory.
  693|       |			 * Give up.
  694|       |			 */
  695|      0|			snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      0|#define PCAP_ERRBUF_SIZE 256
  ------------------
  696|      0|			    "out of memory for per-interface information (%u interfaces)",
  697|      0|			    new_ifcount);
  698|      0|			return (0);
  699|      0|		}
  700|  1.23k|		ps->ifaces_size = new_ifaces_size;
  701|  1.23k|		ps->ifaces = new_ifaces;
  702|  1.23k|	}
  703|       |
  704|  3.52k|	ps->ifaces[new_if_index].snaplen = idbp->snaplen;
  705|       |
  706|       |	/*
  707|       |	 * Set the default time stamp resolution and offset.
  708|       |	 */
  709|  3.52k|	tsresol = 1000000;	/* microsecond resolution */
  710|  3.52k|	is_binary = 0;		/* which is a power of 10 */
  711|  3.52k|	tsoffset = 0;		/* absolute timestamps */
  712|       |
  713|       |	/*
  714|       |	 * Now look for various time stamp options, so we know
  715|       |	 * how to interpret the time stamps for this interface.
  716|       |	 */
  717|  3.52k|	if (process_idb_options(p, cursor, &tsresol, &tsoffset, &is_binary,
  ------------------
  |  Branch (717:6): [True: 69, False: 3.46k]
  ------------------
  718|  3.52k|	    errbuf) == -1)
  719|     69|		return (0);
  720|       |
  721|  3.46k|	ps->ifaces[new_if_index].tsresol = tsresol;
  722|  3.46k|	ps->ifaces[new_if_index].tsoffset = tsoffset;
  723|       |
  724|       |	/*
  725|       |	 * Determine whether we're scaling up or down or not
  726|       |	 * at all for this interface.
  727|       |	 */
  728|  3.46k|	if (tsresol == ps->user_tsresol) {
  ------------------
  |  Branch (728:6): [True: 2.57k, False: 881]
  ------------------
  729|       |		/*
  730|       |		 * The resolution is the resolution the user wants,
  731|       |		 * so we don't have to do scaling.
  732|       |		 */
  733|  2.57k|		ps->ifaces[new_if_index].scale_type = PASS_THROUGH;
  734|  2.57k|	} else if (tsresol > ps->user_tsresol) {
  ------------------
  |  Branch (734:13): [True: 461, False: 420]
  ------------------
  735|       |		/*
  736|       |		 * The resolution is greater than what the user wants,
  737|       |		 * so we have to scale the timestamps down.
  738|       |		 */
  739|    461|		if (is_binary)
  ------------------
  |  Branch (739:7): [True: 244, False: 217]
  ------------------
  740|    244|			ps->ifaces[new_if_index].scale_type = SCALE_DOWN_BIN;
  741|    217|		else {
  742|       |			/*
  743|       |			 * Calculate the scale factor.
  744|       |			 */
  745|    217|			ps->ifaces[new_if_index].scale_factor = tsresol/ps->user_tsresol;
  746|    217|			ps->ifaces[new_if_index].scale_type = SCALE_DOWN_DEC;
  747|    217|		}
  748|    461|	} else {
  749|       |		/*
  750|       |		 * The resolution is less than what the user wants,
  751|       |		 * so we have to scale the timestamps up.
  752|       |		 */
  753|    420|		if (is_binary)
  ------------------
  |  Branch (753:7): [True: 209, False: 211]
  ------------------
  754|    209|			ps->ifaces[new_if_index].scale_type = SCALE_UP_BIN;
  755|    211|		else {
  756|       |			/*
  757|       |			 * Calculate the scale factor.
  758|       |			 */
  759|    211|			ps->ifaces[new_if_index].scale_factor = ps->user_tsresol/tsresol;
  760|    211|			ps->ifaces[new_if_index].scale_type = SCALE_UP_DEC;
  761|    211|		}
  762|    420|	}
  763|       |
  764|       |	/*
  765|       |	 * We've succeeded, so update the interface count.
  766|       |	 */
  767|  3.46k|	ps->ifcount = new_ifcount;
  768|  3.46k|	return (1);
  769|  3.52k|}
sf-pcapng.c:process_idb_options:
  457|  3.52k|{
  458|  3.52k|	struct option_header *opthdr;
  459|  3.52k|	void *optvalue;
  460|  3.52k|	int saw_tsresol, saw_tsoffset;
  461|  3.52k|	uint8_t tsresol_opt;
  462|  3.52k|	u_int i;
  463|       |
  464|  3.52k|	saw_tsresol = 0;
  465|  3.52k|	saw_tsoffset = 0;
  466|  5.70k|	while (cursor->data_remaining != 0) {
  ------------------
  |  Branch (466:9): [True: 2.51k, False: 3.18k]
  ------------------
  467|       |		/*
  468|       |		 * Get the option header.
  469|       |		 */
  470|  2.51k|		opthdr = get_opthdr_from_block_data(p, cursor, errbuf);
  471|  2.51k|		if (opthdr == NULL) {
  ------------------
  |  Branch (471:7): [True: 0, False: 2.51k]
  ------------------
  472|       |			/*
  473|       |			 * Option header is cut short.
  474|       |			 */
  475|      0|			return (-1);
  476|      0|		}
  477|       |
  478|       |		/*
  479|       |		 * Get option value.
  480|       |		 */
  481|  2.51k|		optvalue = get_optvalue_from_block_data(cursor, opthdr,
  482|  2.51k|		    errbuf);
  483|  2.51k|		if (optvalue == NULL) {
  ------------------
  |  Branch (483:7): [True: 26, False: 2.48k]
  ------------------
  484|       |			/*
  485|       |			 * Option value is cut short.
  486|       |			 */
  487|     26|			return (-1);
  488|     26|		}
  489|       |
  490|  2.48k|		switch (opthdr->option_code) {
  491|       |
  492|    285|		case OPT_ENDOFOPT:
  ------------------
  |  |   67|    285|#define OPT_ENDOFOPT	0	/* end of options */
  ------------------
  |  Branch (492:3): [True: 285, False: 2.20k]
  ------------------
  493|    285|			if (opthdr->option_length != 0) {
  ------------------
  |  Branch (493:8): [True: 10, False: 275]
  ------------------
  494|     10|				snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     10|#define PCAP_ERRBUF_SIZE 256
  ------------------
  495|     10|				    "Interface Description Block has opt_endofopt option with length %u != 0",
  496|     10|				    opthdr->option_length);
  497|     10|				return (-1);
  498|     10|			}
  499|    275|			goto done;
  500|       |
  501|    901|		case IF_TSRESOL:
  ------------------
  |  |  131|    901|#define IF_TSRESOL	9	/* interface's time stamp resolution */
  ------------------
  |  Branch (501:3): [True: 901, False: 1.58k]
  ------------------
  502|    901|			if (opthdr->option_length != 1) {
  ------------------
  |  Branch (502:8): [True: 3, False: 898]
  ------------------
  503|      3|				snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      3|#define PCAP_ERRBUF_SIZE 256
  ------------------
  504|      3|				    "Interface Description Block has if_tsresol option with length %u != 1",
  505|      3|				    opthdr->option_length);
  506|      3|				return (-1);
  507|      3|			}
  508|    898|			if (saw_tsresol) {
  ------------------
  |  Branch (508:8): [True: 2, False: 896]
  ------------------
  509|      2|				snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      2|#define PCAP_ERRBUF_SIZE 256
  ------------------
  510|      2|				    "Interface Description Block has more than one if_tsresol option");
  511|      2|				return (-1);
  512|      2|			}
  513|    896|			saw_tsresol = 1;
  514|    896|			memcpy(&tsresol_opt, optvalue, sizeof(tsresol_opt));
  515|    896|			if (tsresol_opt & 0x80) {
  ------------------
  |  Branch (515:8): [True: 461, False: 435]
  ------------------
  516|       |				/*
  517|       |				 * Resolution is negative power of 2.
  518|       |				 */
  519|    461|				uint8_t tsresol_shift = (tsresol_opt & 0x7F);
  520|       |
  521|    461|				if (tsresol_shift > 63) {
  ------------------
  |  Branch (521:9): [True: 7, False: 454]
  ------------------
  522|       |					/*
  523|       |					 * Resolution is too high; 2^-{res}
  524|       |					 * won't fit in a 64-bit value.
  525|       |					 */
  526|      7|					snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      7|#define PCAP_ERRBUF_SIZE 256
  ------------------
  527|      7|					    "Interface Description Block if_tsresol option resolution 2^-%u is too high",
  528|      7|					    tsresol_shift);
  529|      7|					return (-1);
  530|      7|				}
  531|    454|				*is_binary = 1;
  532|    454|				*tsresol = ((uint64_t)1) << tsresol_shift;
  533|    454|			} else {
  534|       |				/*
  535|       |				 * Resolution is negative power of 10.
  536|       |				 */
  537|    435|				if (tsresol_opt > 19) {
  ------------------
  |  Branch (537:9): [True: 4, False: 431]
  ------------------
  538|       |					/*
  539|       |					 * Resolution is too high; 2^-{res}
  540|       |					 * won't fit in a 64-bit value (the
  541|       |					 * largest power of 10 that fits
  542|       |					 * in a 64-bit value is 10^19, as
  543|       |					 * the largest 64-bit unsigned
  544|       |					 * value is ~1.8*10^19).
  545|       |					 */
  546|      4|					snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      4|#define PCAP_ERRBUF_SIZE 256
  ------------------
  547|      4|					    "Interface Description Block if_tsresol option resolution 10^-%u is too high",
  548|      4|					    tsresol_opt);
  549|      4|					return (-1);
  550|      4|				}
  551|    431|				*is_binary = 0;
  552|    431|				*tsresol = 1;
  553|  3.96k|				for (i = 0; i < tsresol_opt; i++)
  ------------------
  |  Branch (553:17): [True: 3.53k, False: 431]
  ------------------
  554|  3.53k|					*tsresol *= 10;
  555|    431|			}
  556|    885|			break;
  557|       |
  558|    885|		case IF_TSOFFSET:
  ------------------
  |  |  136|    409|#define IF_TSOFFSET	14	/* time stamp offset for this interface */
  ------------------
  |  Branch (558:3): [True: 409, False: 2.08k]
  ------------------
  559|    409|			if (opthdr->option_length != 8) {
  ------------------
  |  Branch (559:8): [True: 16, False: 393]
  ------------------
  560|     16|				snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     16|#define PCAP_ERRBUF_SIZE 256
  ------------------
  561|     16|				    "Interface Description Block has if_tsoffset option with length %u != 8",
  562|     16|				    opthdr->option_length);
  563|     16|				return (-1);
  564|     16|			}
  565|    393|			if (saw_tsoffset) {
  ------------------
  |  Branch (565:8): [True: 1, False: 392]
  ------------------
  566|      1|				snprintf(errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      1|#define PCAP_ERRBUF_SIZE 256
  ------------------
  567|      1|				    "Interface Description Block has more than one if_tsoffset option");
  568|      1|				return (-1);
  569|      1|			}
  570|    392|			saw_tsoffset = 1;
  571|    392|			memcpy(tsoffset, optvalue, sizeof(*tsoffset));
  572|    392|			if (p->swapped)
  ------------------
  |  Branch (572:8): [True: 195, False: 197]
  ------------------
  573|    195|				*tsoffset = PCAP_BSWAP_64(*tsoffset);
  ------------------
  |  |   85|    195|#define PCAP_BSWAP_64(y) __builtin_bswap64((uint64_t)(y))
  ------------------
  574|    392|			break;
  575|       |
  576|    894|		default:
  ------------------
  |  Branch (576:3): [True: 894, False: 1.59k]
  ------------------
  577|    894|			break;
  578|  2.48k|		}
  579|  2.48k|	}
  580|       |
  581|  3.46k|done:
  582|  3.46k|	return (0);
  583|  3.52k|}
sf-pcapng.c:get_opthdr_from_block_data:
  410|  2.51k|{
  411|  2.51k|	struct option_header *opthdr;
  412|       |
  413|  2.51k|	opthdr = get_from_block_data(cursor, sizeof(*opthdr), errbuf);
  414|  2.51k|	if (opthdr == NULL) {
  ------------------
  |  Branch (414:6): [True: 0, False: 2.51k]
  ------------------
  415|       |		/*
  416|       |		 * Option header is cut short.
  417|       |		 */
  418|      0|		return (NULL);
  419|      0|	}
  420|       |
  421|       |	/*
  422|       |	 * Byte-swap it if necessary.
  423|       |	 */
  424|  2.51k|	if (p->swapped) {
  ------------------
  |  Branch (424:6): [True: 953, False: 1.56k]
  ------------------
  425|    953|		opthdr->option_code = PCAP_BSWAP_16(opthdr->option_code);
  ------------------
  |  |  109|    953|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  426|    953|		opthdr->option_length = PCAP_BSWAP_16(opthdr->option_length);
  ------------------
  |  |  109|    953|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
  427|    953|	}
  428|       |
  429|  2.51k|	return (opthdr);
  430|  2.51k|}
sf-pcapng.c:get_optvalue_from_block_data:
  435|  2.51k|{
  436|  2.51k|	size_t padded_option_len;
  437|  2.51k|	void *optvalue;
  438|       |
  439|       |	/* Pad option length to 4-byte boundary */
  440|  2.51k|	padded_option_len = opthdr->option_length;
  441|  2.51k|	padded_option_len = ((padded_option_len + 3)/4)*4;
  442|       |
  443|  2.51k|	optvalue = get_from_block_data(cursor, padded_option_len, errbuf);
  444|  2.51k|	if (optvalue == NULL) {
  ------------------
  |  Branch (444:6): [True: 26, False: 2.48k]
  ------------------
  445|       |		/*
  446|       |		 * Option value is cut short.
  447|       |		 */
  448|     26|		return (NULL);
  449|     26|	}
  450|       |
  451|  2.48k|	return (optvalue);
  452|  2.51k|}
sf-pcapng.c:pcap_ng_cleanup:
 1095|    798|{
 1096|    798|	struct pcap_ng_sf *ps = p->priv;
 1097|       |
 1098|    798|	free(ps->ifaces);
 1099|    798|	pcapint_sf_cleanup(p);
 1100|    798|}
sf-pcapng.c:pcap_ng_next_packet:
 1109|  3.02k|{
 1110|  3.02k|	struct pcap_ng_sf *ps = p->priv;
 1111|  3.02k|	struct block_cursor cursor;
 1112|  3.02k|	int status;
 1113|  3.02k|	struct enhanced_packet_block *epbp;
 1114|  3.02k|	struct simple_packet_block *spbp;
 1115|  3.02k|	struct packet_block *pbp;
 1116|  3.02k|	bpf_u_int32 interface_id = 0xFFFFFFFF;
 1117|  3.02k|	struct interface_description_block *idbp;
 1118|  3.02k|	struct section_header_block *shbp;
 1119|  3.02k|	FILE *fp = p->rfile;
 1120|  3.02k|	uint64_t t, sec, frac;
 1121|       |
 1122|       |	/*
 1123|       |	 * Look for an Enhanced Packet Block, a Simple Packet Block,
 1124|       |	 * or a Packet Block.
 1125|       |	 */
 1126|  6.42k|	for (;;) {
 1127|       |		/*
 1128|       |		 * Read the block type and length; those are common
 1129|       |		 * to all blocks.
 1130|       |		 */
 1131|  6.42k|		status = read_block(fp, p, &cursor, p->errbuf);
 1132|  6.42k|		if (status == 0)
  ------------------
  |  Branch (1132:7): [True: 346, False: 6.07k]
  ------------------
 1133|    346|			return (0);	/* EOF */
 1134|  6.07k|		if (status == -1)
  ------------------
  |  Branch (1134:7): [True: 43, False: 6.03k]
  ------------------
 1135|     43|			return (-1);	/* error */
 1136|  6.03k|		switch (cursor.block_type) {
 1137|       |
 1138|    473|		case BT_EPB:
  ------------------
  |  |  141|    473|#define BT_EPB			0x00000006
  ------------------
  |  Branch (1138:3): [True: 473, False: 5.56k]
  ------------------
 1139|       |			/*
 1140|       |			 * Get a pointer to the fixed-length portion of the
 1141|       |			 * EPB.
 1142|       |			 */
 1143|    473|			epbp = get_from_block_data(&cursor, sizeof(*epbp),
 1144|    473|			    p->errbuf);
 1145|    473|			if (epbp == NULL)
  ------------------
  |  Branch (1145:8): [True: 4, False: 469]
  ------------------
 1146|      4|				return (-1);	/* error */
 1147|       |
 1148|       |			/*
 1149|       |			 * Byte-swap it if necessary.
 1150|       |			 */
 1151|    469|			if (p->swapped) {
  ------------------
  |  Branch (1151:8): [True: 243, False: 226]
  ------------------
 1152|       |				/* these were written in opposite byte order */
 1153|    243|				interface_id = PCAP_BSWAP_32(epbp->interface_id);
  ------------------
  |  |   99|    243|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1154|    243|				hdr->caplen = PCAP_BSWAP_32(epbp->caplen);
  ------------------
  |  |   99|    243|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1155|    243|				hdr->len = PCAP_BSWAP_32(epbp->len);
  ------------------
  |  |   99|    243|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1156|    243|				t = ((uint64_t)PCAP_BSWAP_32(epbp->timestamp_high)) << 32 |
  ------------------
  |  |   99|    243|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1157|    243|				    PCAP_BSWAP_32(epbp->timestamp_low);
  ------------------
  |  |   99|    243|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1158|    243|			} else {
 1159|    226|				interface_id = epbp->interface_id;
 1160|    226|				hdr->caplen = epbp->caplen;
 1161|    226|				hdr->len = epbp->len;
 1162|    226|				t = ((uint64_t)epbp->timestamp_high) << 32 |
 1163|    226|				    epbp->timestamp_low;
 1164|    226|			}
 1165|    469|			goto found;
 1166|       |
 1167|  1.51k|		case BT_SPB:
  ------------------
  |  |  155|  1.51k|#define BT_SPB			0x00000003
  ------------------
  |  Branch (1167:3): [True: 1.51k, False: 4.52k]
  ------------------
 1168|       |			/*
 1169|       |			 * Get a pointer to the fixed-length portion of the
 1170|       |			 * SPB.
 1171|       |			 */
 1172|  1.51k|			spbp = get_from_block_data(&cursor, sizeof(*spbp),
 1173|  1.51k|			    p->errbuf);
 1174|  1.51k|			if (spbp == NULL)
  ------------------
  |  Branch (1174:8): [True: 1, False: 1.50k]
  ------------------
 1175|      1|				return (-1);	/* error */
 1176|       |
 1177|       |			/*
 1178|       |			 * SPB packets are assumed to have arrived on
 1179|       |			 * the first interface.
 1180|       |			 */
 1181|  1.50k|			interface_id = 0;
 1182|       |
 1183|       |			/*
 1184|       |			 * Byte-swap it if necessary.
 1185|       |			 */
 1186|  1.50k|			if (p->swapped) {
  ------------------
  |  Branch (1186:8): [True: 320, False: 1.18k]
  ------------------
 1187|       |				/* these were written in opposite byte order */
 1188|    320|				hdr->len = PCAP_BSWAP_32(spbp->len);
  ------------------
  |  |   99|    320|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1189|    320|			} else
 1190|  1.18k|				hdr->len = spbp->len;
 1191|       |
 1192|       |			/*
 1193|       |			 * The SPB doesn't give the captured length;
 1194|       |			 * it's the minimum of the snapshot length
 1195|       |			 * and the packet length.
 1196|       |			 */
 1197|  1.50k|			hdr->caplen = hdr->len;
 1198|  1.50k|			if (hdr->caplen > (bpf_u_int32)p->snapshot)
  ------------------
  |  Branch (1198:8): [True: 244, False: 1.26k]
  ------------------
 1199|    244|				hdr->caplen = p->snapshot;
 1200|  1.50k|			t = 0;	/* no time stamps */
 1201|  1.50k|			goto found;
 1202|       |
 1203|    428|		case BT_PB:
  ------------------
  |  |  165|    428|#define BT_PB			0x00000002
  ------------------
  |  Branch (1203:3): [True: 428, False: 5.60k]
  ------------------
 1204|       |			/*
 1205|       |			 * Get a pointer to the fixed-length portion of the
 1206|       |			 * PB.
 1207|       |			 */
 1208|    428|			pbp = get_from_block_data(&cursor, sizeof(*pbp),
 1209|    428|			    p->errbuf);
 1210|    428|			if (pbp == NULL)
  ------------------
  |  Branch (1210:8): [True: 4, False: 424]
  ------------------
 1211|      4|				return (-1);	/* error */
 1212|       |
 1213|       |			/*
 1214|       |			 * Byte-swap it if necessary.
 1215|       |			 */
 1216|    424|			if (p->swapped) {
  ------------------
  |  Branch (1216:8): [True: 201, False: 223]
  ------------------
 1217|       |				/* these were written in opposite byte order */
 1218|    201|				interface_id = PCAP_BSWAP_16(pbp->interface_id);
  ------------------
  |  |  109|    201|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
 1219|    201|				hdr->caplen = PCAP_BSWAP_32(pbp->caplen);
  ------------------
  |  |   99|    201|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1220|    201|				hdr->len = PCAP_BSWAP_32(pbp->len);
  ------------------
  |  |   99|    201|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1221|    201|				t = ((uint64_t)PCAP_BSWAP_32(pbp->timestamp_high)) << 32 |
  ------------------
  |  |   99|    201|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1222|    201|				    PCAP_BSWAP_32(pbp->timestamp_low);
  ------------------
  |  |   99|    201|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1223|    223|			} else {
 1224|    223|				interface_id = pbp->interface_id;
 1225|    223|				hdr->caplen = pbp->caplen;
 1226|    223|				hdr->len = pbp->len;
 1227|    223|				t = ((uint64_t)pbp->timestamp_high) << 32 |
 1228|    223|				    pbp->timestamp_low;
 1229|    223|			}
 1230|    424|			goto found;
 1231|       |
 1232|  2.69k|		case BT_IDB:
  ------------------
  |  |  112|  2.69k|#define BT_IDB			0x00000001
  ------------------
  |  Branch (1232:3): [True: 2.69k, False: 3.33k]
  ------------------
 1233|       |			/*
 1234|       |			 * Interface Description Block.  Get a pointer
 1235|       |			 * to its fixed-length portion.
 1236|       |			 */
 1237|  2.69k|			idbp = get_from_block_data(&cursor, sizeof(*idbp),
 1238|  2.69k|			    p->errbuf);
 1239|  2.69k|			if (idbp == NULL)
  ------------------
  |  Branch (1239:8): [True: 1, False: 2.69k]
  ------------------
 1240|      1|				return (-1);	/* error */
 1241|       |
 1242|       |			/*
 1243|       |			 * Byte-swap it if necessary.
 1244|       |			 */
 1245|  2.69k|			if (p->swapped) {
  ------------------
  |  Branch (1245:8): [True: 819, False: 1.87k]
  ------------------
 1246|    819|				idbp->linktype = PCAP_BSWAP_16(idbp->linktype);
  ------------------
  |  |  109|    819|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
 1247|    819|				idbp->snaplen = PCAP_BSWAP_32(idbp->snaplen);
  ------------------
  |  |   99|    819|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1248|    819|			}
 1249|       |
 1250|       |			/*
 1251|       |			 * If the link-layer type or snapshot length
 1252|       |			 * differ from the ones for the first IDB we
 1253|       |			 * saw, quit.
 1254|       |			 *
 1255|       |			 * XXX - just discard packets from those
 1256|       |			 * interfaces?
 1257|       |			 */
 1258|  2.69k|			if (p->linktype != idbp->linktype) {
  ------------------
  |  Branch (1258:8): [True: 1, False: 2.69k]
  ------------------
 1259|      1|				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      1|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1260|      1|				    "an interface has a type %u different from the type of the first interface",
 1261|      1|				    idbp->linktype);
 1262|      1|				return (-1);
 1263|      1|			}
 1264|       |
 1265|       |			/*
 1266|       |			 * Check against the *adjusted* value of this IDB's
 1267|       |			 * snapshot length.
 1268|       |			 */
 1269|  2.69k|			if ((bpf_u_int32)p->snapshot !=
  ------------------
  |  Branch (1269:8): [True: 30, False: 2.66k]
  ------------------
 1270|  2.69k|			    pcapint_adjust_snapshot(p->linktype, idbp->snaplen)) {
 1271|     30|				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     30|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1272|     30|				    "an interface has a snapshot length %u different from the snapshot length of the first interface",
 1273|     30|				    idbp->snaplen);
 1274|     30|				return (-1);
 1275|     30|			}
 1276|       |
 1277|       |			/*
 1278|       |			 * Try to add this interface.
 1279|       |			 */
 1280|  2.66k|			if (!add_interface(p, idbp, &cursor, p->errbuf))
  ------------------
  |  Branch (1280:8): [True: 3, False: 2.66k]
  ------------------
 1281|      3|				return (-1);
 1282|  2.66k|			break;
 1283|       |
 1284|  2.66k|		case BT_SHB:
  ------------------
  |  |   86|    597|#define BT_SHB			0x0A0D0D0A
  ------------------
  |  Branch (1284:3): [True: 597, False: 5.43k]
  ------------------
 1285|       |			/*
 1286|       |			 * Section Header Block.  Get a pointer
 1287|       |			 * to its fixed-length portion.
 1288|       |			 */
 1289|    597|			shbp = get_from_block_data(&cursor, sizeof(*shbp),
 1290|    597|			    p->errbuf);
 1291|    597|			if (shbp == NULL)
  ------------------
  |  Branch (1291:8): [True: 3, False: 594]
  ------------------
 1292|      3|				return (-1);	/* error */
 1293|       |
 1294|       |			/*
 1295|       |			 * Assume the byte order of this section is
 1296|       |			 * the same as that of the previous section.
 1297|       |			 * We'll check for that later.
 1298|       |			 */
 1299|    594|			if (p->swapped) {
  ------------------
  |  Branch (1299:8): [True: 263, False: 331]
  ------------------
 1300|    263|				shbp->byte_order_magic =
 1301|    263|				    PCAP_BSWAP_32(shbp->byte_order_magic);
  ------------------
  |  |   99|    263|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
 1302|    263|				shbp->major_version =
 1303|    263|				    PCAP_BSWAP_16(shbp->major_version);
  ------------------
  |  |  109|    263|#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
  ------------------
 1304|    263|			}
 1305|       |
 1306|       |			/*
 1307|       |			 * Make sure the byte order doesn't change;
 1308|       |			 * pcap_is_swapped() shouldn't change its
 1309|       |			 * return value in the middle of reading a capture.
 1310|       |			 */
 1311|    594|			switch (shbp->byte_order_magic) {
 1312|       |
 1313|    424|			case BYTE_ORDER_MAGIC:
  ------------------
  |  |   99|    424|#define BYTE_ORDER_MAGIC	0x1A2B3C4D
  ------------------
  |  Branch (1313:4): [True: 424, False: 170]
  ------------------
 1314|       |				/*
 1315|       |				 * OK.
 1316|       |				 */
 1317|    424|				break;
 1318|       |
 1319|      2|			case PCAP_BSWAP_32(BYTE_ORDER_MAGIC):
  ------------------
  |  |   99|      2|#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
  ------------------
  |  Branch (1319:4): [True: 2, False: 592]
  ------------------
 1320|       |				/*
 1321|       |				 * Byte order changes.
 1322|       |				 */
 1323|      2|				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|      2|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1324|      2|				    "the file has sections with different byte orders");
 1325|      2|				return (-1);
 1326|       |
 1327|    168|			default:
  ------------------
  |  Branch (1327:4): [True: 168, False: 426]
  ------------------
 1328|       |				/*
 1329|       |				 * Not a valid SHB.
 1330|       |				 */
 1331|    168|				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|    168|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1332|    168|				    "the file has a section with a bad byte order magic field");
 1333|    168|				return (-1);
 1334|    594|			}
 1335|       |
 1336|       |			/*
 1337|       |			 * Make sure the major version is the version
 1338|       |			 * we handle.
 1339|       |			 */
 1340|    424|			if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
  ------------------
  |  |  106|    424|#define PCAP_NG_VERSION_MAJOR	1
  ------------------
  |  Branch (1340:8): [True: 16, False: 408]
  ------------------
 1341|     16|				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     16|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1342|     16|				    "unknown pcapng savefile major version number %u",
 1343|     16|				    shbp->major_version);
 1344|     16|				return (-1);
 1345|     16|			}
 1346|       |
 1347|       |			/*
 1348|       |			 * Reset the interface count; this section should
 1349|       |			 * have its own set of IDBs.  If any of them
 1350|       |			 * don't have the same interface type, snapshot
 1351|       |			 * length, or resolution as the first interface
 1352|       |			 * we saw, we'll fail.  (And if we don't see
 1353|       |			 * any IDBs, we'll fail when we see a packet
 1354|       |			 * block.)
 1355|       |			 */
 1356|    408|			ps->ifcount = 0;
 1357|    408|			break;
 1358|       |
 1359|    331|		default:
  ------------------
  |  Branch (1359:3): [True: 331, False: 5.70k]
  ------------------
 1360|       |			/*
 1361|       |			 * Not a packet block, IDB, or SHB; ignore it.
 1362|       |			 */
 1363|    331|			break;
 1364|  6.03k|		}
 1365|  6.03k|	}
 1366|       |
 1367|  2.40k|found:
 1368|       |	/*
 1369|       |	 * Is the interface ID an interface we know?
 1370|       |	 */
 1371|  2.40k|	if (interface_id >= ps->ifcount) {
  ------------------
  |  Branch (1371:6): [True: 58, False: 2.34k]
  ------------------
 1372|       |		/*
 1373|       |		 * Yes.  Fail.
 1374|       |		 */
 1375|     58|		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     58|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1376|     58|		    "a packet arrived on interface %u, but there's no Interface Description Block for that interface",
 1377|     58|		    interface_id);
 1378|     58|		return (-1);
 1379|     58|	}
 1380|       |
 1381|  2.34k|	if (hdr->caplen > (bpf_u_int32)p->snapshot) {
  ------------------
  |  Branch (1381:6): [True: 40, False: 2.30k]
  ------------------
 1382|     40|		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  ------------------
  |  |  149|     40|#define PCAP_ERRBUF_SIZE 256
  ------------------
 1383|     40|		    "invalid packet capture length %u, bigger than "
 1384|     40|		    "snaplen of %d", hdr->caplen, p->snapshot);
 1385|     40|		return (-1);
 1386|     40|	}
 1387|       |
 1388|       |	/*
 1389|       |	 * Convert the time stamp to seconds and fractions of a second,
 1390|       |	 * with the fractions being in units of the file-supplied resolution.
 1391|       |	 */
 1392|  2.30k|	sec = t / ps->ifaces[interface_id].tsresol + ps->ifaces[interface_id].tsoffset;
 1393|  2.30k|	frac = t % ps->ifaces[interface_id].tsresol;
 1394|       |
 1395|       |	/*
 1396|       |	 * Convert the fractions from units of the file-supplied resolution
 1397|       |	 * to units of the user-requested resolution.
 1398|       |	 */
 1399|  2.30k|	switch (ps->ifaces[interface_id].scale_type) {
  ------------------
  |  Branch (1399:10): [True: 2.30k, False: 0]
  ------------------
 1400|       |
 1401|    905|	case PASS_THROUGH:
  ------------------
  |  Branch (1401:2): [True: 905, False: 1.39k]
  ------------------
 1402|       |		/*
 1403|       |		 * The interface resolution is what the user wants,
 1404|       |		 * so we're done.
 1405|       |		 */
 1406|    905|		break;
 1407|       |
 1408|    450|	case SCALE_UP_DEC:
  ------------------
  |  Branch (1408:2): [True: 450, False: 1.85k]
  ------------------
 1409|       |		/*
 1410|       |		 * The interface resolution is less than what the user
 1411|       |		 * wants; scale the fractional part up to the units of
 1412|       |		 * the resolution the user requested by multiplying by
 1413|       |		 * the quotient of the user-requested resolution and the
 1414|       |		 * file-supplied resolution.
 1415|       |		 *
 1416|       |		 * Those resolutions are both powers of 10, and the user-
 1417|       |		 * requested resolution is greater than the file-supplied
 1418|       |		 * resolution, so the quotient in question is an integer.
 1419|       |		 * We've calculated that quotient already, so we just
 1420|       |		 * multiply by it.
 1421|       |		 */
 1422|    450|		frac *= ps->ifaces[interface_id].scale_factor;
 1423|    450|		break;
 1424|       |
 1425|    294|	case SCALE_UP_BIN:
  ------------------
  |  Branch (1425:2): [True: 294, False: 2.01k]
  ------------------
 1426|       |		/*
 1427|       |		 * The interface resolution is less than what the user
 1428|       |		 * wants; scale the fractional part up to the units of
 1429|       |		 * the resolution the user requested by multiplying by
 1430|       |		 * the quotient of the user-requested resolution and the
 1431|       |		 * file-supplied resolution.
 1432|       |		 *
 1433|       |		 * The file-supplied resolution is a power of 2, so the
 1434|       |		 * quotient is not an integer, so, in order to do this
 1435|       |		 * entirely with integer arithmetic, we multiply by the
 1436|       |		 * user-requested resolution and divide by the file-
 1437|       |		 * supplied resolution.
 1438|       |		 *
 1439|       |		 * XXX - Is there something clever we could do here,
 1440|       |		 * given that we know that the file-supplied resolution
 1441|       |		 * is a power of 2?  Doing a multiplication followed by
 1442|       |		 * a division runs the risk of overflowing, and involves
 1443|       |		 * two non-simple arithmetic operations.
 1444|       |		 */
 1445|    294|		frac *= ps->user_tsresol;
 1446|    294|		frac /= ps->ifaces[interface_id].tsresol;
 1447|    294|		break;
 1448|       |
 1449|    194|	case SCALE_DOWN_DEC:
  ------------------
  |  Branch (1449:2): [True: 194, False: 2.11k]
  ------------------
 1450|       |		/*
 1451|       |		 * The interface resolution is greater than what the user
 1452|       |		 * wants; scale the fractional part up to the units of
 1453|       |		 * the resolution the user requested by multiplying by
 1454|       |		 * the quotient of the user-requested resolution and the
 1455|       |		 * file-supplied resolution.
 1456|       |		 *
 1457|       |		 * Those resolutions are both powers of 10, and the user-
 1458|       |		 * requested resolution is less than the file-supplied
 1459|       |		 * resolution, so the quotient in question isn't an
 1460|       |		 * integer, but its reciprocal is, and we can just divide
 1461|       |		 * by the reciprocal of the quotient.  We've calculated
 1462|       |		 * the reciprocal of that quotient already, so we must
 1463|       |		 * divide by it.
 1464|       |		 */
 1465|    194|		frac /= ps->ifaces[interface_id].scale_factor;
 1466|    194|		break;
 1467|       |
 1468|       |
 1469|    461|	case SCALE_DOWN_BIN:
  ------------------
  |  Branch (1469:2): [True: 461, False: 1.84k]
  ------------------
 1470|       |		/*
 1471|       |		 * The interface resolution is greater than what the user
 1472|       |		 * wants; convert the fractional part to units of the
 1473|       |		 * resolution the user requested by multiplying by the
 1474|       |		 * quotient of the user-requested resolution and the
 1475|       |		 * file-supplied resolution.  We do that by multiplying
 1476|       |		 * by the user-requested resolution and dividing by the
 1477|       |		 * file-supplied resolution, as the quotient might not
 1478|       |		 * fit in an integer.
 1479|       |		 *
 1480|       |		 * The file-supplied resolution is a power of 2, so the
 1481|       |		 * quotient is not an integer, and neither is its
 1482|       |		 * reciprocal, so, in order to do this entirely with
 1483|       |		 * integer arithmetic, we multiply by the user-requested
 1484|       |		 * resolution and divide by the file-supplied resolution.
 1485|       |		 *
 1486|       |		 * XXX - Is there something clever we could do here,
 1487|       |		 * given that we know that the file-supplied resolution
 1488|       |		 * is a power of 2?  Doing a multiplication followed by
 1489|       |		 * a division runs the risk of overflowing, and involves
 1490|       |		 * two non-simple arithmetic operations.
 1491|       |		 */
 1492|    461|		frac *= ps->user_tsresol;
 1493|    461|		frac /= ps->ifaces[interface_id].tsresol;
 1494|    461|		break;
 1495|  2.30k|	}
 1496|       |#ifdef _WIN32
 1497|       |	/*
 1498|       |	 * tv_sec and tv_usec in the Windows struct timeval are both
 1499|       |	 * longs.
 1500|       |	 */
 1501|       |	hdr->ts.tv_sec = (long)sec;
 1502|       |	hdr->ts.tv_usec = (long)frac;
 1503|       |#else
 1504|       |	/*
 1505|       |	 * tv_sec in the UN*X struct timeval is a time_t; tv_usec is
 1506|       |	 * suseconds_t in UN*Xes that work the way the current Single
 1507|       |	 * UNIX Standard specify - but not all older UN*Xes necessarily
 1508|       |	 * support that type, so just cast to int.
 1509|       |	 */
 1510|  2.30k|	hdr->ts.tv_sec = (time_t)sec;
 1511|  2.30k|	hdr->ts.tv_usec = (int)frac;
 1512|  2.30k|#endif
 1513|       |
 1514|       |	/*
 1515|       |	 * Get a pointer to the packet data.
 1516|       |	 */
 1517|  2.30k|	*data = get_from_block_data(&cursor, hdr->caplen, p->errbuf);
 1518|  2.30k|	if (*data == NULL)
  ------------------
  |  Branch (1518:6): [True: 78, False: 2.22k]
  ------------------
 1519|     78|		return (-1);
 1520|       |
 1521|  2.22k|	pcapint_post_process(p->linktype, p->swapped, hdr, *data);
 1522|       |
 1523|  2.22k|	return (1);
 1524|  2.30k|}

parse_mpls:
   89|  2.29k|{
   90|  2.29k|    const u_char *mpls_label; /* byte cursor, not a struct pointer - see below */
   91|  2.29k|    const u_char *end_ptr = pktdata + datalen;
   92|  2.29k|    u_char first_nibble;
   93|  2.29k|    eth_hdr_t *eth_hdr;
   94|  2.29k|    bool bos = false;
   95|  2.29k|    uint32_t label;
   96|  2.29k|    int len;
   97|       |
   98|  2.29k|    assert(next_protocol);
  ------------------
  |  Branch (98:5): [True: 0, False: 2.29k]
  |  Branch (98:5): [True: 2.29k, False: 0]
  ------------------
   99|  2.29k|    assert(l2len);
  ------------------
  |  Branch (99:5): [True: 0, False: 2.29k]
  |  Branch (99:5): [True: 2.29k, False: 0]
  ------------------
  100|  2.29k|    assert(l2offset);
  ------------------
  |  Branch (100:5): [True: 0, False: 2.29k]
  |  Branch (100:5): [True: 2.29k, False: 0]
  ------------------
  101|       |
  102|  2.29k|    len = (int)*l2len;
  103|       |
  104|       |    /*
  105|       |     * An MPLS label is four bytes at whatever offset the encapsulation puts
  106|       |     * it, which is not guaranteed to be 4-byte aligned. Casting that to a
  107|       |     * struct tcpr_mpls_label and reading ->entry is undefined behaviour, and
  108|       |     * UBSan says so:
  109|       |     *
  110|       |     *   get.c:115: member access within misaligned address ... for type
  111|       |     *   'struct tcpr_mpls_label', which requires 4 byte alignment
  112|       |     *
  113|       |     * x86 does not care, which is why this went unnoticed - but docs/INSTALL
  114|       |     * lists Solaris and the BSDs, where a misaligned load faults or is fixed
  115|       |     * up in the trap handler (#1100). Copy the four bytes out instead, and
  116|       |     * keep the cursor as a plain byte pointer so no misaligned struct pointer
  117|       |     * is ever formed.
  118|       |     */
  119|  42.9k|    while (!bos) {
  ------------------
  |  Branch (119:12): [True: 41.3k, False: 1.67k]
  ------------------
  120|  41.3k|        uint32_t entry;
  121|       |
  122|  41.3k|        if (pktdata + len + sizeof(entry) > end_ptr) {
  ------------------
  |  Branch (122:13): [True: 388, False: 40.9k]
  ------------------
  123|    388|            warnx("parse_mpls: Need at least %zu bytes for MPLS header but only %u available",
  ------------------
  |  |  123|    388|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  124|    388|                  sizeof(entry) + len,
  125|    388|                  datalen);
  126|    388|            return -1;
  127|    388|        }
  128|       |
  129|  40.9k|        mpls_label = pktdata + len;
  130|  40.9k|        memcpy(&entry, mpls_label, sizeof(entry));
  131|  40.9k|        len += sizeof(entry);
  132|       |
  133|  40.9k|        entry = ntohl(entry);
  134|  40.9k|        bos = (entry & MPLS_LS_S_MASK) != 0;
  ------------------
  |  | 1701|  40.9k|#define MPLS_LS_S_MASK 0x00000100
  ------------------
  135|  40.9k|        label = entry >> MPLS_LS_LABEL_SHIFT;
  ------------------
  |  | 1692|  40.9k|#define MPLS_LS_LABEL_SHIFT 12
  ------------------
  136|  40.9k|        if (label == MPLS_LABEL_GACH) {
  ------------------
  |  | 1685|  40.9k|#define MPLS_LABEL_GACH 13
  ------------------
  |  Branch (136:13): [True: 228, False: 40.6k]
  ------------------
  137|       |            /* Generic Associated Channel Header */
  138|    228|            warn("GACH MPLS label not supported at this time");
  ------------------
  |  |  121|    228|#define warn(x) fprintf(stderr, "Warning: %s\n", x)
  ------------------
  139|    228|            return -1;
  140|    228|        }
  141|  40.9k|    }
  142|       |
  143|       |    /* mpls_label is now a byte cursor, so step past the 4-byte label directly */
  144|  1.67k|    if (mpls_label + sizeof(uint32_t) + 1 > end_ptr) {
  ------------------
  |  Branch (144:9): [True: 269, False: 1.40k]
  ------------------
  145|    269|        warnx("parse_mpls: Need at least %zu bytes for MPLS label but only %u available",
  ------------------
  |  |  123|    269|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  146|    269|              sizeof(uint32_t) + 1,
  147|    269|              datalen);
  148|    269|        return -1;
  149|    269|    }
  150|       |
  151|  1.40k|    first_nibble = *(mpls_label + sizeof(uint32_t)) >> 4;
  152|  1.40k|    switch (first_nibble) {
  153|    298|    case 4:
  ------------------
  |  Branch (153:5): [True: 298, False: 1.11k]
  ------------------
  154|    298|        *next_protocol = ETHERTYPE_IP;
  155|    298|        break;
  156|    228|    case 6:
  ------------------
  |  Branch (156:5): [True: 228, False: 1.18k]
  ------------------
  157|    228|        *next_protocol = ETHERTYPE_IP6;
  ------------------
  |  |  534|    228|#define ETHERTYPE_IP6 0x86DD /* IPv6 */
  ------------------
  158|    228|        break;
  159|    665|    case 0:
  ------------------
  |  Branch (159:5): [True: 665, False: 743]
  ------------------
  160|       |        /* EoMPLS - jump over PW Ethernet Control Word and handle
  161|       |         * inner Ethernet header
  162|       |         */
  163|    665|        if (pktdata + len + 4 + sizeof(*eth_hdr) > end_ptr) {
  ------------------
  |  Branch (163:13): [True: 210, False: 455]
  ------------------
  164|    210|            warnx("parse_mpls: Need at least %zu bytes for EoMPLS header but only %u available",
  ------------------
  |  |  123|    210|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  165|    210|                  sizeof(*eth_hdr) + len + 4,
  166|    210|                  datalen);
  167|    210|            return -1;
  168|    210|        }
  169|       |
  170|    455|        len += 4;
  171|    455|        *l2offset = len;
  172|    455|        eth_hdr = (eth_hdr_t *)(pktdata + len);
  173|    455|        len += sizeof(*eth_hdr);
  174|    455|        *next_protocol = ntohs(eth_hdr->ether_type);
  175|    455|        break;
  176|    217|    default:
  ------------------
  |  Branch (176:5): [True: 217, False: 1.19k]
  ------------------
  177|    217|        warn("parse_mpls:suspect Generic Associated Channel Header");
  ------------------
  |  |  121|    217|#define warn(x) fprintf(stderr, "Warning: %s\n", x)
  ------------------
  178|    217|        return -1;
  179|  1.40k|    }
  180|       |
  181|    981|    *l2len = (uint32_t)len;
  182|    981|    return 0;
  183|  1.40k|}
parse_vlan:
  198|  1.80k|{
  199|  1.80k|    vlan_hdr_t *vlan_hdr;
  200|  1.80k|    if ((size_t)datalen < *l2len + sizeof(*vlan_hdr)) {
  ------------------
  |  Branch (200:9): [True: 298, False: 1.51k]
  ------------------
  201|    298|        warnx("parse_vlan: Need at least %zu bytes for VLAN header but only %u available", sizeof(*vlan_hdr), datalen);
  ------------------
  |  |  123|    298|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  202|    298|        return -1;
  203|    298|    }
  204|  1.51k|    vlan_hdr = (vlan_hdr_t *)(pktdata + *l2len);
  205|  1.51k|    *next_protocol = ntohs(vlan_hdr->vlan_tpid);
  206|  1.51k|    *l2len += sizeof(vlan_hdr_t);
  207|       |
  208|  1.51k|    return 0;
  209|  1.80k|}
get_l2len_protocol:
  290|  32.2k|{
  291|  32.2k|    assert(protocol);
  ------------------
  |  Branch (291:5): [True: 0, False: 32.2k]
  |  Branch (291:5): [True: 32.2k, False: 0]
  ------------------
  292|  32.2k|    assert(l2len);
  ------------------
  |  Branch (292:5): [True: 0, False: 32.2k]
  |  Branch (292:5): [True: 32.2k, False: 0]
  ------------------
  293|  32.2k|    assert(l2offset);
  ------------------
  |  Branch (293:5): [True: 0, False: 32.2k]
  |  Branch (293:5): [True: 32.2k, False: 0]
  ------------------
  294|  32.2k|    assert(vlan_offset);
  ------------------
  |  Branch (294:5): [True: 0, False: 32.2k]
  |  Branch (294:5): [True: 32.2k, False: 0]
  ------------------
  295|       |
  296|  32.2k|    if (!pktdata || !datalen) {
  ------------------
  |  Branch (296:9): [True: 0, False: 32.2k]
  |  Branch (296:21): [True: 0, False: 32.2k]
  ------------------
  297|      0|        err_no_exitx("get_l2len_protocol: invalid L2 parameters: pktdata=0x%p len=%d", pktdata, datalen);
  ------------------
  |  |  142|      0|#define err_no_exitx(y, ...) do {\
  |  |  143|      0|        fprintf(stderr, "\nFatal Error: " y "\n", __VA_ARGS__); \
  |  |  144|      0|        fflush(NULL); \
  |  |  145|      0|    } while (0)
  |  |  ------------------
  |  |  |  Branch (145:14): [Folded, False: 0]
  |  |  ------------------
  ------------------
  298|      0|        return -1;
  299|      0|    }
  300|       |
  301|  32.2k|    *protocol = 0;
  302|  32.2k|    *l2len = 0;
  303|  32.2k|    *l2offset = 0;
  304|  32.2k|    *vlan_offset = 0;
  305|       |
  306|  32.2k|    switch (datalink) {
  307|    983|    case DLT_NULL:
  ------------------
  |  Branch (307:5): [True: 983, False: 31.2k]
  ------------------
  308|       |        /* a 4-byte address family (in the capturing host's byte order) precedes the IP header */
  309|    983|        if (datalen < 5) {
  ------------------
  |  Branch (309:13): [True: 344, False: 639]
  ------------------
  310|    344|            warnx("%s (0x%x): Need at least 5 bytes but only %u available",
  ------------------
  |  |  123|    344|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  311|    344|                  pcap_datalink_val_to_description(datalink),
  312|    344|                  datalink,
  313|    344|                  datalen);
  314|    344|            return -1;
  315|    344|        }
  316|    639|        *l2len = 4;
  317|    639|        if ((pktdata[4] >> 4) == 4) {
  ------------------
  |  Branch (317:13): [True: 198, False: 441]
  ------------------
  318|    198|            *protocol = ETHERTYPE_IP;
  319|    441|        } else if ((pktdata[4] >> 4) == 6) {
  ------------------
  |  Branch (319:20): [True: 206, False: 235]
  ------------------
  320|    206|            *protocol = ETHERTYPE_IP6;
  ------------------
  |  |  534|    206|#define ETHERTYPE_IP6 0x86DD /* IPv6 */
  ------------------
  321|    206|        }
  322|    639|        break;
  323|  6.72k|    case DLT_RAW:
  ------------------
  |  Branch (323:5): [True: 6.72k, False: 25.5k]
  ------------------
  324|  6.72k|        if ((pktdata[0] >> 4) == 4)
  ------------------
  |  Branch (324:13): [True: 557, False: 6.16k]
  ------------------
  325|    557|            *protocol = ETHERTYPE_IP;
  326|  6.16k|        else if ((pktdata[0] >> 4) == 6)
  ------------------
  |  Branch (326:18): [True: 5.82k, False: 340]
  ------------------
  327|  5.82k|            *protocol = ETHERTYPE_IP6;
  ------------------
  |  |  534|  5.82k|#define ETHERTYPE_IP6 0x86DD /* IPv6 */
  ------------------
  328|  6.72k|        break;
  329|  2.33k|    case DLT_JUNIPER_ETHER:
  ------------------
  |  Branch (329:5): [True: 2.33k, False: 29.9k]
  ------------------
  330|  2.33k|        if (datalen < 4) {
  ------------------
  |  Branch (330:13): [True: 255, False: 2.08k]
  ------------------
  331|    255|            warnx("%s (0x%x): Need at least 4 bytes for DLT_JUNIPER_ETHER but only %u available",
  ------------------
  |  |  123|    255|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  332|    255|                  pcap_datalink_val_to_description(datalink),
  333|    255|                  datalink,
  334|    255|                  datalen);
  335|    255|            return -1;
  336|    255|        }
  337|       |
  338|  2.08k|        if (memcmp(pktdata, JUNIPER_PCAP_MAGIC, 3) != 0) {
  ------------------
  |  |   39|  2.08k|#define JUNIPER_PCAP_MAGIC "MGC"
  ------------------
  |  Branch (338:13): [True: 629, False: 1.45k]
  ------------------
  339|    629|            warnx("%s (0x%x): No JUNIPER_PCAP_MAGIC Magic Number found during protocol lookup",
  ------------------
  |  |  123|    629|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  340|    629|                  pcap_datalink_val_to_description(datalink),
  341|    629|                  datalink);
  342|    629|            return -1;
  343|    629|        }
  344|       |
  345|  1.45k|        if ((pktdata[3] & JUNIPER_FLAG_EXT) == JUNIPER_FLAG_EXT) {
  ------------------
  |  |   38|  1.45k|#define JUNIPER_FLAG_EXT 0x80   /* Juniper extensions present */
  ------------------
                      if ((pktdata[3] & JUNIPER_FLAG_EXT) == JUNIPER_FLAG_EXT) {
  ------------------
  |  |   38|  1.45k|#define JUNIPER_FLAG_EXT 0x80   /* Juniper extensions present */
  ------------------
  |  Branch (345:13): [True: 452, False: 1.00k]
  ------------------
  346|    452|            if (datalen < 6) {
  ------------------
  |  Branch (346:17): [True: 223, False: 229]
  ------------------
  347|    223|                warnx("%s (0x%x): Need at least 6 bytes for JUNIPER_FLAG_EXT but only %u available",
  ------------------
  |  |  123|    223|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  348|    223|                      pcap_datalink_val_to_description(datalink),
  349|    223|                      datalink,
  350|    223|                      datalen);
  351|    223|                return -1;
  352|    223|            }
  353|       |
  354|    229|            *l2offset = ntohs(*((uint16_t *)&pktdata[4]));
  355|    229|            *l2offset += 6; /* MGC + flags + ext_total_len */
  356|  1.00k|        } else {
  357|  1.00k|            *l2offset = 4; /* MGC + flags (no header extensions) */
  358|  1.00k|        }
  359|       |
  360|  1.23k|        if ((pktdata[3] & JUNIPER_FLAG_NO_L2) == JUNIPER_FLAG_NO_L2) {
  ------------------
  |  |   37|  1.23k|#define JUNIPER_FLAG_NO_L2 0x02 /* L2 header */
  ------------------
                      if ((pktdata[3] & JUNIPER_FLAG_NO_L2) == JUNIPER_FLAG_NO_L2) {
  ------------------
  |  |   37|  1.23k|#define JUNIPER_FLAG_NO_L2 0x02 /* L2 header */
  ------------------
  |  Branch (360:13): [True: 1.01k, False: 220]
  ------------------
  361|       |            /* no L2 header present - *l2offset is actually IP offset */
  362|  1.01k|            uint32_t ip_hdr_offset = *l2offset;
  363|  1.01k|            uint32_t hdrSpaceNeeded = ip_hdr_offset + 1;
  364|  1.01k|            if (datalen < hdrSpaceNeeded) {
  ------------------
  |  Branch (364:17): [True: 417, False: 595]
  ------------------
  365|    417|                warnx("%s (0x%x): Need at least %u bytes for JUNIPER_FLAG_NO_L2 but only %u available",
  ------------------
  |  |  123|    417|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  366|    417|                      pcap_datalink_val_to_description(datalink),
  367|    417|                      hdrSpaceNeeded,
  368|    417|                      datalink,
  369|    417|                      datalen);
  370|    417|                return -1;
  371|    417|            }
  372|       |
  373|    595|            if ((pktdata[ip_hdr_offset] >> 4) == 4)
  ------------------
  |  Branch (373:17): [True: 195, False: 400]
  ------------------
  374|    195|                *protocol = ETHERTYPE_IP;
  375|    400|            else if ((pktdata[ip_hdr_offset] >> 4) == 6)
  ------------------
  |  Branch (375:22): [True: 195, False: 205]
  ------------------
  376|    195|                *protocol = ETHERTYPE_IP6;
  ------------------
  |  |  534|    195|#define ETHERTYPE_IP6 0x86DD /* IPv6 */
  ------------------
  377|       |
  378|    595|            *l2len = ip_hdr_offset;
  379|    595|            return 0;
  380|  1.01k|        }
  381|       |
  382|       |        /* fall through */
  383|  3.20k|    case DLT_EN10MB: {
  ------------------
  |  Branch (383:5): [True: 2.98k, False: 29.2k]
  ------------------
  384|  3.20k|        eth_hdr_t *eth_hdr;
  385|  3.20k|        uint16_t ether_type;
  386|  3.20k|        uint32_t l2_net_off = sizeof(*eth_hdr) + *l2offset;
  387|       |
  388|  3.20k|        if (datalen <= l2_net_off + 4) {
  ------------------
  |  Branch (388:13): [True: 415, False: 2.79k]
  ------------------
  389|    415|            warnx("%s (0x%x): Need at least %u bytes for DLT_EN10MB but only %u available",
  ------------------
  |  |  123|    415|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  390|    415|                  pcap_datalink_val_to_description(datalink),
  391|    415|                  datalink,
  392|    415|                  l2_net_off + 4,
  393|    415|                  datalen);
  394|    415|            return -1;
  395|    415|        }
  396|       |
  397|  2.79k|        eth_hdr = (eth_hdr_t *)(pktdata + *l2offset);
  398|  2.79k|        ether_type = ntohs(eth_hdr->ether_type);
  399|  2.79k|        if (parse_metadata(pktdata, datalen, &ether_type, &l2_net_off, l2offset, vlan_offset))
  ------------------
  |  Branch (399:13): [True: 1.61k, False: 1.18k]
  ------------------
  400|  1.61k|            return -1;
  401|       |
  402|  1.18k|        *l2len = l2_net_off;
  403|  1.18k|        if (ether_type >= 1536) {
  ------------------
  |  Branch (403:13): [True: 659, False: 522]
  ------------------
  404|       |            /* Ethernet II frame - return in host order */
  405|    659|            *protocol = ether_type;
  406|    659|        } else if (ether_type > 1500) {
  ------------------
  |  Branch (406:20): [True: 202, False: 320]
  ------------------
  407|    202|            warnx("%s (0x%x): unsupported 802.3 length %u",
  ------------------
  |  |  123|    202|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  408|    202|                  pcap_datalink_val_to_description(datalink),
  409|    202|                  datalink,
  410|    202|                  ether_type);
  411|    202|            return -1;
  412|    320|        } else {
  413|       |            /* 803.3 frame */
  414|       |            /* we don't modify 802.3 protocols */
  415|    320|            return -1;
  416|    320|        }
  417|    659|        break;
  418|  1.18k|    }
  419|    660|    case DLT_PPP_SERIAL:
  ------------------
  |  Branch (419:5): [True: 660, False: 31.6k]
  ------------------
  420|    660|        if ((size_t)datalen < sizeof(struct tcpr_pppserial_hdr)) {
  ------------------
  |  Branch (420:13): [True: 194, False: 466]
  ------------------
  421|    194|            warnx("%s (0x%x): Need at least %zu bytes for DLT_PPP_SERIAL but only %u available",
  ------------------
  |  |  123|    194|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  422|    194|                  pcap_datalink_val_to_description(datalink),
  423|    194|                  datalink,
  424|    194|                  sizeof(struct tcpr_pppserial_hdr),
  425|    194|                  datalen);
  426|    194|            return -1;
  427|    194|        }
  428|       |
  429|    466|        struct tcpr_pppserial_hdr *ppp = (struct tcpr_pppserial_hdr *)pktdata;
  430|    466|        *l2len = sizeof(*ppp);
  431|    466|        if (ntohs(ppp->protocol) == 0x0021)
  ------------------
  |  Branch (431:13): [True: 226, False: 240]
  ------------------
  432|    226|            *protocol = ETHERTYPE_IP;
  433|    240|        else
  434|    240|            *protocol = ntohs(ppp->protocol);
  435|       |
  436|    466|        break;
  437|    454|    case DLT_C_HDLC:
  ------------------
  |  Branch (437:5): [True: 454, False: 31.8k]
  ------------------
  438|    454|        if (datalen < CISCO_HDLC_LEN) {
  ------------------
  |  |   28|    454|#define CISCO_HDLC_LEN 4
  ------------------
  |  Branch (438:13): [True: 226, False: 228]
  ------------------
  439|    226|            warnx("%s (0x%x): Need at least %u bytes for DLT_C_HDLC but only %u available",
  ------------------
  |  |  123|    226|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  440|    226|                  pcap_datalink_val_to_description(datalink),
  441|    226|                  datalink,
  442|    226|                  CISCO_HDLC_LEN,
  443|    226|                  datalen);
  444|    226|            return -1;
  445|    226|        }
  446|       |
  447|    228|        hdlc_hdr_t *hdlc_hdr = (hdlc_hdr_t *)pktdata;
  448|    228|        *l2len = sizeof(*hdlc_hdr);
  449|    228|        *protocol = ntohs(hdlc_hdr->protocol);
  450|    228|        break;
  451|  3.05k|    case DLT_LINUX_SLL:
  ------------------
  |  Branch (451:5): [True: 3.05k, False: 29.2k]
  ------------------
  452|  3.05k|        if (datalen < SLL_HDR_LEN) {
  ------------------
  |  |   86|  3.05k|#define SLL_HDR_LEN    16           /* total header length */
  ------------------
  |  Branch (452:13): [True: 209, False: 2.84k]
  ------------------
  453|    209|            warnx("%s (0x%x): Need at least %u bytes for DLT_LINUX_SLL but only %u available",
  ------------------
  |  |  123|    209|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  454|    209|                  pcap_datalink_val_to_description(datalink),
  455|    209|                  datalink,
  456|    209|                  SLL_HDR_LEN,
  457|    209|                  datalen);
  458|    209|            return -1;
  459|    209|        }
  460|       |
  461|  2.84k|        *l2len = SLL_HDR_LEN;
  ------------------
  |  |   86|  2.84k|#define SLL_HDR_LEN    16           /* total header length */
  ------------------
  462|  2.84k|        sll_hdr_t *sll_hdr = (sll_hdr_t *)pktdata;
  463|  2.84k|        *protocol = ntohs(sll_hdr->sll_protocol);
  464|  2.84k|        break;
  465|  3.18k|    case DLT_LINUX_SLL2:
  ------------------
  |  Branch (465:5): [True: 3.18k, False: 29.0k]
  ------------------
  466|  3.18k|        if (datalen < SLL2_HDR_LEN) {
  ------------------
  |  |  100|  3.18k|#define SLL2_HDR_LEN    20              /* total header length */
  ------------------
  |  Branch (466:13): [True: 227, False: 2.95k]
  ------------------
  467|    227|            warnx("%s (0x%x): Need at least %u bytes for DLT_LINUX_SLL2 but only %u available",
  ------------------
  |  |  123|    227|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  468|    227|                  pcap_datalink_val_to_description(datalink),
  469|    227|                  datalink,
  470|    227|                  SLL2_HDR_LEN,
  471|    227|                  datalen);
  472|    227|            return -1;
  473|    227|        }
  474|       |
  475|  2.95k|        *l2len = SLL2_HDR_LEN;
  ------------------
  |  |  100|  2.95k|#define SLL2_HDR_LEN    20              /* total header length */
  ------------------
  476|  2.95k|        sll2_hdr_t *sll2_hdr = (sll2_hdr_t *)pktdata;
  477|  2.95k|        *protocol = ntohs(sll2_hdr->sll2_protocol);
  478|  2.95k|        break;
  479|  11.8k|    default:
  ------------------
  |  Branch (479:5): [True: 11.8k, False: 20.3k]
  ------------------
  480|  11.8k|        warnx("Unable to process unsupported DLT type: %s (0x%x)",
  ------------------
  |  |  123|  11.8k|#define warnx(x, ...) fprintf(stderr, "Warning: " x "\n", __VA_ARGS__)
  ------------------
  481|  11.8k|              pcap_datalink_val_to_description(datalink),
  482|  11.8k|              datalink);
  483|  11.8k|        return -1;
  484|  32.2k|    }
  485|       |
  486|  14.5k|    return 0;
  487|  32.2k|}
get_layer4_v4:
  649|    505|{
  650|    505|    void *ptr;
  651|       |
  652|    505|    assert(ip_hdr);
  ------------------
  |  Branch (652:5): [True: 0, False: 505]
  |  Branch (652:5): [True: 505, False: 0]
  ------------------
  653|    505|    assert(end_ptr);
  ------------------
  |  Branch (653:5): [True: 0, False: 505]
  |  Branch (653:5): [True: 505, False: 0]
  ------------------
  654|       |
  655|    505|    ptr = (u_char *)ip_hdr + (ip_hdr->ip_hl << 2);
  656|       |    /* make sure we don't jump over the end of the buffer */
  657|    505|    if ((u_char *)ptr > end_ptr)
  ------------------
  |  Branch (657:9): [True: 259, False: 246]
  ------------------
  658|    259|        return NULL;
  659|       |
  660|    246|    return ((void *)ptr);
  661|    505|}
get_layer4_v6:
  684|  89.3k|{
  685|  89.3k|    struct tcpr_ipv6_ext_hdr_base *next, *exthdr;
  686|  89.3k|    bool done = false;
  687|  89.3k|    uint8_t proto;
  688|       |
  689|  89.3k|    assert(ip6_hdr);
  ------------------
  |  Branch (689:5): [True: 0, False: 89.3k]
  |  Branch (689:5): [True: 89.3k, False: 0]
  ------------------
  690|  89.3k|    assert(end_ptr);
  ------------------
  |  Branch (690:5): [True: 0, False: 89.3k]
  |  Branch (690:5): [True: 89.3k, False: 0]
  ------------------
  691|       |
  692|       |    /* jump to the end of the IPv6 header */
  693|  89.3k|    next = (struct tcpr_ipv6_ext_hdr_base *)((u_char *)ip6_hdr + TCPR_IPV6_H);
  ------------------
  |  |   97|  89.3k|#define TCPR_IPV6_H 0x28              /**< IPv6 header:         40 bytes */
  ------------------
  694|  89.3k|    if ((u_char *)next > end_ptr)
  ------------------
  |  Branch (694:9): [True: 299, False: 89.0k]
  ------------------
  695|    299|        return NULL;
  696|       |
  697|  89.0k|    proto = ip6_hdr->ip_nh;
  698|   186k|    while (!done) {
  ------------------
  |  Branch (698:12): [True: 98.0k, False: 88.2k]
  ------------------
  699|  98.0k|        dbgx(3, "Processing proto: 0x%hx", (uint16_t)proto);
  ------------------
  |  |  119|  98.0k|#define dbgx(x, y, ...) { }
  ------------------
  700|       |
  701|  98.0k|        switch (proto) {
  702|       |        /* recurse due to v6-in-v6, need to recast next as an IPv6 Header */
  703|  83.7k|        case TCPR_IPV6_NH_IPV6:
  ------------------
  |  |  788|  83.7k|#define TCPR_IPV6_NH_IPV6 41
  ------------------
  |  Branch (703:9): [True: 83.7k, False: 14.2k]
  ------------------
  704|  83.7k|            dbg(3, "recursing due to v6-in-v6");
  ------------------
  |  |  118|  83.7k|#define dbg(x, y) { }
  ------------------
  705|  83.7k|            next = get_layer4_v6((ipv6_hdr_t *)next, end_ptr);
  706|  83.7k|            break;
  707|       |
  708|       |        /* loop again */
  709|    451|        case TCPR_IPV6_NH_AH:
  ------------------
  |  |  790|    451|#define TCPR_IPV6_NH_AH 51
  ------------------
  |  Branch (709:9): [True: 451, False: 97.5k]
  ------------------
  710|  1.25k|        case TCPR_IPV6_NH_ROUTING:
  ------------------
  |  |  810|  1.25k|#define TCPR_IPV6_NH_ROUTING 43
  ------------------
  |  Branch (710:9): [True: 804, False: 97.2k]
  ------------------
  711|  1.65k|        case TCPR_IPV6_NH_DESTOPTS:
  ------------------
  |  |  824|  1.65k|#define TCPR_IPV6_NH_DESTOPTS 60
  ------------------
  |  Branch (711:9): [True: 402, False: 97.6k]
  ------------------
  712|  6.88k|        case TCPR_IPV6_NH_HBH:
  ------------------
  |  |  836|  6.88k|#define TCPR_IPV6_NH_HBH 0
  ------------------
  |  Branch (712:9): [True: 5.23k, False: 92.7k]
  ------------------
  713|  6.88k|            dbgx(3, "Going deeper due to extension header 0x%02X", proto);
  ------------------
  |  |  119|  6.88k|#define dbgx(x, y, ...) { }
  ------------------
  714|  6.88k|            exthdr = get_ipv6_next(next, end_ptr);
  715|  6.88k|            if (!ipv6_exthdr_fits(exthdr, end_ptr)) {
  ------------------
  |  Branch (715:17): [True: 2.25k, False: 4.63k]
  ------------------
  716|  2.25k|                next = NULL;
  717|  2.25k|                done = true;
  718|  2.25k|                break;
  719|  2.25k|            }
  720|  4.63k|            proto = exthdr->ip_nh;
  721|  4.63k|            next = exthdr;
  722|  4.63k|            break;
  723|       |
  724|       |        /*
  725|       |         * handle (unparsable) IPv6 fragment data
  726|       |         */
  727|  3.12k|        case TCPR_IPV6_NH_FRAGMENT:
  ------------------
  |  |  797|  3.12k|#define TCPR_IPV6_NH_FRAGMENT 44
  ------------------
  |  Branch (727:9): [True: 3.12k, False: 94.8k]
  ------------------
  728|       |            // next points to l4 data
  729|  3.12k|            dbgx(3, "Go deeper due to fragment extension header 0x%02X", proto);
  ------------------
  |  |  119|  3.12k|#define dbgx(x, y, ...) { }
  ------------------
  730|  3.12k|            exthdr = get_ipv6_next(next, end_ptr);
  731|  3.12k|            if (!ipv6_exthdr_fits(exthdr, end_ptr)) {
  ------------------
  |  Branch (731:17): [True: 2.05k, False: 1.07k]
  ------------------
  732|  2.05k|                next = NULL;
  733|  2.05k|                done = true;
  734|  2.05k|                break;
  735|  2.05k|            }
  736|  1.07k|            proto = exthdr->ip_nh;
  737|  1.07k|            next = exthdr;
  738|       |            // done = true;
  739|  1.07k|            break;
  740|       |
  741|       |        /*
  742|       |         * Can't handle.  Unparsable IPv6 encrypted data
  743|       |         */
  744|     68|        case TCPR_IPV6_NH_ESP:
  ------------------
  |  |  789|     68|#define TCPR_IPV6_NH_ESP 50
  ------------------
  |  Branch (744:9): [True: 68, False: 97.9k]
  ------------------
  745|     68|            next = NULL;
  746|     68|            done = true;
  747|     68|            break;
  748|       |
  749|       |        /*
  750|       |         * no further processing, either TCP, UDP, ICMP, etc...
  751|       |         */
  752|  4.17k|        default:
  ------------------
  |  Branch (752:9): [True: 4.17k, False: 93.8k]
  ------------------
  753|  4.17k|            if (proto != ip6_hdr->ip_nh && next) {
  ------------------
  |  Branch (753:17): [True: 1.50k, False: 2.67k]
  |  Branch (753:44): [True: 1.50k, False: 0]
  ------------------
  754|       |                /* reading next->ip_len needs the base header to be present */
  755|  1.50k|                if (!ipv6_exthdr_fits(next, end_ptr)) {
  ------------------
  |  Branch (755:21): [True: 0, False: 1.50k]
  ------------------
  756|      0|                    return NULL;
  757|      0|                }
  758|       |
  759|  1.50k|                dbgx(3, "Returning byte offset of this ext header: %u", IPV6_EXTLEN_TO_BYTES(next->ip_len));
  ------------------
  |  |  119|  1.50k|#define dbgx(x, y, ...) { }
  ------------------
  760|  1.50k|                next = (void *)((u_char *)next + IPV6_EXTLEN_TO_BYTES(next->ip_len));
  ------------------
  |  |  292|  1.50k|#define IPV6_EXTLEN_TO_BYTES(x) ((x * 4) + 8)
  ------------------
  761|  1.50k|                if ((u_char*)next > end_ptr)
  ------------------
  |  Branch (761:21): [True: 866, False: 640]
  ------------------
  762|    866|                    return NULL;
  763|  2.67k|            } else {
  764|  2.67k|                dbgx(3, "%s", "Returning end of IPv6 Header");
  ------------------
  |  |  119|  2.67k|#define dbgx(x, y, ...) { }
  ------------------
  765|  2.67k|            }
  766|       |
  767|  3.31k|            done = true;
  768|  98.0k|        } /* switch */
  769|       |
  770|  97.1k|        if (next == NULL)
  ------------------
  |  Branch (770:13): [True: 84.9k, False: 12.2k]
  ------------------
  771|  84.9k|            done = true;
  772|  97.1k|    } /* while */
  773|       |
  774|  88.2k|    return next;
  775|  89.0k|}
get_ipv6_l4proto:
  844|  9.19k|{
  845|  9.19k|    u_char *ptr = (u_char *)ip6_hdr + TCPR_IPV6_H; /* jump to the end of the IPv6 header */
  ------------------
  |  |   97|  9.19k|#define TCPR_IPV6_H 0x28              /**< IPv6 header:         40 bytes */
  ------------------
  846|  9.19k|    uint8_t proto;
  847|  9.19k|    struct tcpr_ipv6_ext_hdr_base *exthdr = NULL;
  848|       |
  849|  9.19k|    assert(ip6_hdr);
  ------------------
  |  Branch (849:5): [True: 0, False: 9.19k]
  |  Branch (849:5): [True: 9.19k, False: 0]
  ------------------
  850|       |
  851|  9.19k|    if (ptr > end_ptr)
  ------------------
  |  Branch (851:9): [True: 240, False: 8.95k]
  ------------------
  852|    240|        return TCPR_IPV6_NH_NO_NEXT;
  ------------------
  |  |  787|    240|#define TCPR_IPV6_NH_NO_NEXT 59
  ------------------
  853|       |
  854|  8.95k|    proto = ip6_hdr->ip_nh;
  855|  10.6k|    while (TRUE) {
  ------------------
  |  Branch (855:12): [True: 10.6k, Folded]
  ------------------
  856|  10.6k|        dbgx(3, "Processing next proto 0x%02X", proto);
  ------------------
  |  |  119|  10.6k|#define dbgx(x, y, ...) { }
  ------------------
  857|  10.6k|        switch (proto) {
  858|       |        /* no further processing for IPV6 types with nothing beyond them */
  859|      0|        case TCPR_IPV6_NH_NO_NEXT:
  ------------------
  |  |  787|      0|#define TCPR_IPV6_NH_NO_NEXT 59
  ------------------
  |  Branch (859:9): [True: 0, False: 10.6k]
  ------------------
  860|  2.71k|        case TCPR_IPV6_NH_FRAGMENT:
  ------------------
  |  |  797|  2.71k|#define TCPR_IPV6_NH_FRAGMENT 44
  ------------------
  |  Branch (860:9): [True: 2.71k, False: 7.98k]
  ------------------
  861|  2.77k|        case TCPR_IPV6_NH_ESP:
  ------------------
  |  |  789|  2.77k|#define TCPR_IPV6_NH_ESP 50
  ------------------
  |  Branch (861:9): [True: 66, False: 10.6k]
  ------------------
  862|  2.77k|            dbg(3, "No-Next or ESP... can't go any further...");
  ------------------
  |  |  118|  2.77k|#define dbg(x, y) { }
  ------------------
  863|  2.77k|            return proto;
  864|       |
  865|       |        /* recurse */
  866|  3.56k|        case TCPR_IPV6_NH_IPV6:
  ------------------
  |  |  788|  3.56k|#define TCPR_IPV6_NH_IPV6 41
  ------------------
  |  Branch (866:9): [True: 3.56k, False: 7.13k]
  ------------------
  867|  3.56k|            dbg(3, "Recursing due to v6 in v6");
  ------------------
  |  |  118|  3.56k|#define dbg(x, y) { }
  ------------------
  868|  3.56k|            return get_ipv6_l4proto((ipv6_hdr_t *)ptr, end_ptr);
  869|       |
  870|       |        /* loop again */
  871|    402|        case TCPR_IPV6_NH_AH:
  ------------------
  |  |  790|    402|#define TCPR_IPV6_NH_AH 51
  ------------------
  |  Branch (871:9): [True: 402, False: 10.2k]
  ------------------
  872|  1.10k|        case TCPR_IPV6_NH_ROUTING:
  ------------------
  |  |  810|  1.10k|#define TCPR_IPV6_NH_ROUTING 43
  ------------------
  |  Branch (872:9): [True: 700, False: 9.99k]
  ------------------
  873|  1.48k|        case TCPR_IPV6_NH_DESTOPTS:
  ------------------
  |  |  824|  1.48k|#define TCPR_IPV6_NH_DESTOPTS 60
  ------------------
  |  Branch (873:9): [True: 380, False: 10.3k]
  ------------------
  874|  3.74k|        case TCPR_IPV6_NH_HBH:
  ------------------
  |  |  836|  3.74k|#define TCPR_IPV6_NH_HBH 0
  ------------------
  |  Branch (874:9): [True: 2.26k, False: 8.43k]
  ------------------
  875|  3.74k|            dbgx(3, "Jumping to next extension header (0x%hhx)", proto);
  ------------------
  |  |  119|  3.74k|#define dbgx(x, y, ...) { }
  ------------------
  876|  3.74k|            exthdr = get_ipv6_next((struct tcpr_ipv6_ext_hdr_base *)ptr, end_ptr);
  877|  3.74k|            if (exthdr == NULL || (u_char *)exthdr + sizeof(*exthdr) > end_ptr)
  ------------------
  |  Branch (877:17): [True: 1.58k, False: 2.16k]
  |  Branch (877:35): [True: 422, False: 1.74k]
  ------------------
  878|  2.00k|                return TCPR_IPV6_NH_NO_NEXT;
  ------------------
  |  |  787|  2.00k|#define TCPR_IPV6_NH_NO_NEXT 59
  ------------------
  879|  1.74k|            proto = exthdr->ip_nh;
  880|  1.74k|            ptr = (u_char *)exthdr;
  881|  1.74k|            break;
  882|       |
  883|       |        /* should be TCP, UDP or the like */
  884|    605|        default:
  ------------------
  |  Branch (884:9): [True: 605, False: 10.0k]
  ------------------
  885|    605|            dbgx(3, "Selecting next L4 Proto as: 0x%02x", proto);
  ------------------
  |  |  119|    605|#define dbgx(x, y, ...) { }
  ------------------
  886|    605|            return proto;
  887|  10.6k|        } /* switch */
  888|  10.6k|    }     /* while */
  889|  8.95k|}
get.c:parse_metadata:
  230|  2.79k|{
  231|  2.79k|    bool done = false;
  232|  2.79k|    assert(next_protocol);
  ------------------
  |  Branch (232:5): [True: 0, False: 2.79k]
  |  Branch (232:5): [True: 2.79k, False: 0]
  ------------------
  233|  2.79k|    assert(l2len);
  ------------------
  |  Branch (233:5): [True: 0, False: 2.79k]
  |  Branch (233:5): [True: 2.79k, False: 0]
  ------------------
  234|  2.79k|    assert(l2offset);
  ------------------
  |  Branch (234:5): [True: 0, False: 2.79k]
  |  Branch (234:5): [True: 2.79k, False: 0]
  ------------------
  235|  2.79k|    assert(vlan_offset);
  ------------------
  |  Branch (235:5): [True: 0, False: 2.79k]
  |  Branch (235:5): [True: 2.79k, False: 0]
  ------------------
  236|       |
  237|  2.79k|    if (!pktdata || !datalen)
  ------------------
  |  Branch (237:9): [True: 0, False: 2.79k]
  |  Branch (237:21): [True: 0, False: 2.79k]
  ------------------
  238|      0|        errx(-1, "parse_metadata: invalid L2 parameters: pktdata=0x%p len=%d", pktdata, datalen);
  ------------------
  |  |  131|      0|#define errx(x, y, ...) do {\
  |  |  132|      0|        fprintf(stderr, "\nFatal Error: " y "\n", __VA_ARGS__); \
  |  |  133|      0|        fflush(NULL); \
  |  |  134|      0|        exit(x); \
  |  |  135|      0|    } while (0)
  |  |  ------------------
  |  |  |  Branch (135:14): [Folded, False: 0]
  |  |  ------------------
  ------------------
  239|       |
  240|  6.46k|    while (!done) {
  ------------------
  |  Branch (240:12): [True: 5.28k, False: 1.18k]
  ------------------
  241|  5.28k|        switch (*next_protocol) {
  242|    462|        case ETHERTYPE_VLAN:
  ------------------
  |  Branch (242:9): [True: 462, False: 4.82k]
  ------------------
  243|    967|        case ETHERTYPE_Q_IN_Q:
  ------------------
  |  |  537|    967|#define ETHERTYPE_Q_IN_Q 0x88A8 /* 802.1ad Service VLAN */
  ------------------
  |  Branch (243:9): [True: 505, False: 4.77k]
  ------------------
  244|  1.80k|        case ETHERTYPE_8021QINQ:
  ------------------
  |  |  540|  1.80k|#define ETHERTYPE_8021QINQ 0x9100 /* 802.1Q in Q VLAN */
  ------------------
  |  Branch (244:9): [True: 841, False: 4.44k]
  ------------------
  245|  1.80k|            if (*vlan_offset == 0)
  ------------------
  |  Branch (245:17): [True: 711, False: 1.09k]
  ------------------
  246|    711|                *vlan_offset = *l2len;
  247|       |
  248|  1.80k|            if (parse_vlan(pktdata, datalen, next_protocol, l2len))
  ------------------
  |  Branch (248:17): [True: 298, False: 1.51k]
  ------------------
  249|    298|                return -1;
  250|       |
  251|  1.51k|            break;
  252|  1.78k|        case ETHERTYPE_MPLS:
  ------------------
  |  |  528|  1.78k|#define ETHERTYPE_MPLS 0x8847 /* MPLS */
  ------------------
  |  Branch (252:9): [True: 1.78k, False: 3.49k]
  ------------------
  253|  2.29k|        case ETHERTYPE_MPLS_MULTI:
  ------------------
  |  |  543|  2.29k|#define ETHERTYPE_MPLS_MULTI 0x8848 /* MPLS multicast packet */
  ------------------
  |  Branch (253:9): [True: 506, False: 4.77k]
  ------------------
  254|  2.29k|            if (parse_mpls(pktdata, datalen, next_protocol, l2len, l2offset))
  ------------------
  |  Branch (254:17): [True: 1.31k, False: 981]
  ------------------
  255|  1.31k|                return -1;
  256|       |
  257|    981|            break;
  258|  1.18k|        default:
  ------------------
  |  Branch (258:9): [True: 1.18k, False: 4.10k]
  ------------------
  259|  1.18k|            done = true;
  260|  5.28k|        }
  261|  5.28k|    }
  262|       |
  263|  1.18k|    return 0;
  264|  2.79k|}
get.c:ipv6_exthdr_fits:
  673|  11.5k|{
  674|  11.5k|    return hdr != NULL && (const u_char *)hdr + sizeof(*hdr) <= end_ptr;
  ------------------
  |  Branch (674:12): [True: 7.66k, False: 3.85k]
  |  Branch (674:27): [True: 7.21k, False: 450]
  ------------------
  675|  11.5k|}
get.c:get_ipv6_next:
  783|  13.7k|{
  784|  13.7k|    uint8_t extlen;
  785|  13.7k|    u_char *ptr;
  786|  13.7k|    assert(exthdr);
  ------------------
  |  Branch (786:5): [True: 0, False: 13.7k]
  |  Branch (786:5): [True: 13.7k, False: 0]
  ------------------
  787|       |
  788|  13.7k|    if ((u_char *)exthdr + sizeof(*exthdr) > end_ptr)
  ------------------
  |  Branch (788:9): [True: 692, False: 13.0k]
  ------------------
  789|    692|        return NULL;
  790|       |
  791|  13.0k|    dbgx(3, "Jumping to next IPv6 header.  Processing 0x%02x", exthdr->ip_nh);
  ------------------
  |  |  119|  13.0k|#define dbgx(x, y, ...) { }
  ------------------
  792|  13.0k|    switch (exthdr->ip_nh) {
  793|       |    /* no further processing */
  794|    584|    case TCPR_IPV6_NH_NO_NEXT:
  ------------------
  |  |  787|    584|#define TCPR_IPV6_NH_NO_NEXT 59
  ------------------
  |  Branch (794:5): [True: 584, False: 12.4k]
  ------------------
  795|  1.18k|    case TCPR_IPV6_NH_ESP:
  ------------------
  |  |  789|  1.18k|#define TCPR_IPV6_NH_ESP 50
  ------------------
  |  Branch (795:5): [True: 603, False: 12.4k]
  ------------------
  796|  1.18k|        dbg(3, "No-Next or ESP... can't go any further...");
  ------------------
  |  |  118|  1.18k|#define dbg(x, y) { }
  ------------------
  797|  1.18k|        return NULL;
  798|       |
  799|       |    /*
  800|       |     * fragment header is fixed size
  801|       |     * FIXME: Frag header has further ext headers (has a ip_nh field)
  802|       |     * but I don't support it because there's never a full L4 + payload beyond.
  803|       |     */
  804|  1.51k|    case TCPR_IPV6_NH_FRAGMENT:
  ------------------
  |  |  797|  1.51k|#define TCPR_IPV6_NH_FRAGMENT 44
  ------------------
  |  Branch (804:5): [True: 1.51k, False: 11.5k]
  ------------------
  805|  1.51k|        dbg(3, "Looks like were a fragment header. Returning some frag'd data.");
  ------------------
  |  |  118|  1.51k|#define dbg(x, y) { }
  ------------------
  806|  1.51k|        ptr = (void *)((u_char *)exthdr + sizeof(struct tcpr_ipv6_frag_hdr));
  807|  1.51k|        if (ptr > end_ptr)
  ------------------
  |  Branch (807:13): [True: 606, False: 904]
  ------------------
  808|    606|            return NULL;
  809|    904|        return (void *)ptr;
  810|       |
  811|       |    /* all the rest require us to go deeper using the ip_len field */
  812|  1.08k|    case TCPR_IPV6_NH_IPV6:
  ------------------
  |  |  788|  1.08k|#define TCPR_IPV6_NH_IPV6 41
  ------------------
  |  Branch (812:5): [True: 1.08k, False: 11.9k]
  ------------------
  813|  1.96k|    case TCPR_IPV6_NH_ROUTING:
  ------------------
  |  |  810|  1.96k|#define TCPR_IPV6_NH_ROUTING 43
  ------------------
  |  Branch (813:5): [True: 886, False: 12.1k]
  ------------------
  814|  2.91k|    case TCPR_IPV6_NH_DESTOPTS:
  ------------------
  |  |  824|  2.91k|#define TCPR_IPV6_NH_DESTOPTS 60
  ------------------
  |  Branch (814:5): [True: 942, False: 12.1k]
  ------------------
  815|  7.69k|    case TCPR_IPV6_NH_HBH:
  ------------------
  |  |  836|  7.69k|#define TCPR_IPV6_NH_HBH 0
  ------------------
  |  Branch (815:5): [True: 4.78k, False: 8.28k]
  ------------------
  816|  8.79k|    case TCPR_IPV6_NH_AH:
  ------------------
  |  |  790|  8.79k|#define TCPR_IPV6_NH_AH 51
  ------------------
  |  Branch (816:5): [True: 1.09k, False: 11.9k]
  ------------------
  817|  8.79k|        extlen = IPV6_EXTLEN_TO_BYTES(exthdr->ip_len);
  ------------------
  |  |  292|  8.79k|#define IPV6_EXTLEN_TO_BYTES(x) ((x * 4) + 8)
  ------------------
  818|  8.79k|        if (extlen == 0) {
  ------------------
  |  Branch (818:13): [True: 846, False: 7.94k]
  ------------------
  819|    846|            dbg(3, "Malformed IPv6 extension header...");
  ------------------
  |  |  118|    846|#define dbg(x, y) { }
  ------------------
  820|    846|            return NULL;
  821|    846|        }
  822|  7.94k|        dbgx(3,
  ------------------
  |  |  119|  7.94k|#define dbgx(x, y, ...) { }
  ------------------
  823|  7.94k|             "Looks like we're an ext header (0x%hhx).  Jumping %u bytes"
  824|  7.94k|             " to the next",
  825|  7.94k|             exthdr->ip_nh,
  826|  7.94k|             extlen);
  827|  7.94k|        ptr = (u_char *)exthdr + extlen;
  828|  7.94k|        if (ptr > end_ptr)
  ------------------
  |  Branch (828:13): [True: 2.11k, False: 5.83k]
  ------------------
  829|  2.11k|            return NULL;
  830|  5.83k|        return (void *)ptr;
  831|       |
  832|  1.58k|    default:
  ------------------
  |  Branch (832:5): [True: 1.58k, False: 11.4k]
  ------------------
  833|  1.58k|        dbg(3, "Must not be a v6 extension header... returning self");
  ------------------
  |  |  118|  1.58k|#define dbg(x, y) { }
  ------------------
  834|  1.58k|        return (void *)exthdr;
  835|  13.0k|    }
  836|  13.0k|}

LLVMFuzzerTestOneInput:
   53|  5.37k|{
   54|  5.37k|    FILE *fp;
   55|  5.37k|    pcap_t *pcap;
   56|  5.37k|    char errbuf[PCAP_ERRBUF_SIZE];
   57|  5.37k|    struct pcap_pkthdr *pkthdr;
   58|  5.37k|    const u_char *pktdata;
   59|  5.37k|    int datalink;
   60|  5.37k|    int rc;
   61|  5.37k|    unsigned int packets = 0;
   62|       |
   63|       |    /* a pcap file header is 24 bytes; below that there is nothing to open */
   64|  5.37k|    if (size < 24)
  ------------------
  |  Branch (64:9): [True: 8, False: 5.36k]
  ------------------
   65|      8|        return 0;
   66|       |
   67|       |    /*
   68|       |     * fmemopen wants a writable pointer but libpcap only reads. Casting away
   69|       |     * const on the fuzzer's buffer is not on - copy it.
   70|       |     */
   71|  5.36k|    void *copy = malloc(size);
   72|  5.36k|    if (copy == NULL)
  ------------------
  |  Branch (72:9): [True: 0, False: 5.36k]
  ------------------
   73|      0|        return 0;
   74|  5.36k|    memcpy(copy, data, size);
   75|       |
   76|  5.36k|    fp = fmemopen(copy, size, "rb");
   77|  5.36k|    if (fp == NULL) {
  ------------------
  |  Branch (77:9): [True: 0, False: 5.36k]
  ------------------
   78|      0|        free(copy);
   79|      0|        return 0;
   80|      0|    }
   81|       |
   82|  5.36k|    pcap = pcap_fopen_offline(fp, errbuf);
   83|  5.36k|    if (pcap == NULL) {
  ------------------
  |  Branch (83:9): [True: 752, False: 4.61k]
  ------------------
   84|       |        /* malformed header: libpcap rejected it, which is the correct outcome */
   85|    752|        fclose(fp);
   86|    752|        free(copy);
   87|    752|        return 0;
   88|    752|    }
   89|       |    /* from here on the pcap_t owns fp and closes it in pcap_close() - closing
   90|       |     * it again here is a double free, and it is the harness that would be
   91|       |     * wrong, not libpcap */
   92|       |
   93|  4.61k|    datalink = pcap_datalink(pcap);
   94|       |
   95|  47.5k|    while ((rc = pcap_next_ex(pcap, &pkthdr, &pktdata)) == 1) {
  ------------------
  |  Branch (95:12): [True: 42.8k, False: 4.61k]
  ------------------
   96|  42.8k|        uint16_t ethertype;
   97|  42.8k|        uint32_t l2len, l2offset, vlan_offset;
   98|       |
   99|       |        /*
  100|       |         * Cap the work per input. A crafted pcap can claim an enormous packet
  101|       |         * count, and the fuzzer's time is better spent on new shapes than on
  102|       |         * one pathological file.
  103|       |         */
  104|  42.8k|        if (++packets > 512)
  ------------------
  |  Branch (104:13): [True: 1, False: 42.8k]
  ------------------
  105|      1|            break;
  106|       |
  107|  42.8k|        if (pkthdr->caplen == 0)
  ------------------
  |  Branch (107:13): [True: 10.6k, False: 32.2k]
  ------------------
  108|  10.6k|            continue;
  109|       |
  110|       |        /*
  111|       |         * The parsers take an end pointer computed from caplen. Deliberately
  112|       |         * use caplen, not len: the gap between them is what GHSA-m6w7-8497-g9c9
  113|       |         * was about, and a harness that papered over it would hide the bug
  114|       |         * class it exists to find.
  115|       |         */
  116|  32.2k|        if (get_l2len_protocol(pktdata,
  ------------------
  |  Branch (116:13): [True: 17.1k, False: 15.1k]
  ------------------
  117|  32.2k|                               pkthdr->caplen,
  118|  32.2k|                               datalink,
  119|  32.2k|                               &ethertype,
  120|  32.2k|                               &l2len,
  121|  32.2k|                               &l2offset,
  122|  32.2k|                               &vlan_offset) < 0)
  123|  17.1k|            continue;
  124|       |
  125|  15.1k|        if (l2len > pkthdr->caplen)
  ------------------
  |  Branch (125:13): [True: 0, False: 15.1k]
  ------------------
  126|      0|            continue;
  127|       |
  128|  15.1k|        switch (ethertype) {
  129|  1.47k|        case ETHERTYPE_IP: {
  ------------------
  |  Branch (129:9): [True: 1.47k, False: 13.6k]
  ------------------
  130|  1.47k|            ipv4_hdr_t *ip_hdr = (ipv4_hdr_t *)(pktdata + l2len);
  131|       |
  132|  1.47k|            if (pkthdr->caplen < l2len + sizeof(ipv4_hdr_t))
  ------------------
  |  Branch (132:17): [True: 973, False: 505]
  ------------------
  133|    973|                break;
  134|       |
  135|    505|            (void)get_layer4_v4(ip_hdr, pktdata + pkthdr->caplen - 1);
  136|    505|            break;
  137|  1.47k|        }
  138|       |
  139|  6.45k|        case ETHERTYPE_IP6: {
  ------------------
  |  |  534|  6.45k|#define ETHERTYPE_IP6 0x86DD /* IPv6 */
  ------------------
  |  Branch (139:9): [True: 6.45k, False: 8.65k]
  ------------------
  140|  6.45k|            ipv6_hdr_t *ip6_hdr = (ipv6_hdr_t *)(pktdata + l2len);
  141|       |
  142|  6.45k|            if (pkthdr->caplen < l2len + sizeof(ipv6_hdr_t))
  ------------------
  |  Branch (142:17): [True: 828, False: 5.62k]
  ------------------
  143|    828|                break;
  144|       |
  145|       |            /* GHSA-jj65-mrgg-f5fx */
  146|  5.62k|            (void)get_layer4_v6(ip6_hdr, pktdata + pkthdr->caplen - 1);
  147|  5.62k|            (void)get_ipv6_l4proto(ip6_hdr, pktdata + pkthdr->caplen - 1);
  148|  5.62k|            break;
  149|  6.45k|        }
  150|       |
  151|  7.18k|        default:
  ------------------
  |  Branch (151:9): [True: 7.18k, False: 7.93k]
  ------------------
  152|  7.18k|            break;
  153|  15.1k|        }
  154|  15.1k|    }
  155|       |
  156|  4.61k|    pcap_close(pcap);
  157|  4.61k|    free(copy);
  158|  4.61k|    return 0;
  159|  4.61k|}

