Fix an unintialized-value-used warning that Valgrind detected in the capture
[metze/wireshark/wip.git] / wiretap / vwr.c
index 3adb7924d247147bd40fb2f9d27fe50d0a0d3154..ae2af4fd709b377cdf792e5e7497ca21647feccc 100644 (file)
@@ -1,6 +1,6 @@
 /* vwr.c
  * Copyright (c) 2011 by Tom Alexander <talexander@ixiacom.com>
- * 
+ *
  * $Id$
  *
  * Wiretap Library
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <errno.h>
 #include <string.h>
@@ -35,9 +33,6 @@
 
 
 /* platform-specific definitions for portability */
-static int find_signature(register guint8*, int, register guint32,
-                            register guint8);
-static guint64 get_signature_ts(register guint8*, int);
 
 /* unsigned long long constants */
 #   define NS_IN_US             G_GINT64_CONSTANT(1000U)        /* nanoseconds-to-microseconds */
@@ -45,6 +40,18 @@ static guint64 get_signature_ts(register guint8*, int);
 #   define US_IN_SEC            G_GINT64_CONSTANT(1000000U)     /* microseconds-to-seconds */
 #   define LL_ZERO              G_GINT64_CONSTANT(0U)           /* zero in unsigned long long */
 
+/*
+ * Fetch a 64-bit value in "Corey-endian" form.
+ */
+#define pcoreytohll(p)  ((guint64)*((const guint8 *)(p)+4)<<56|  \
+                         (guint64)*((const guint8 *)(p)+5)<<48|  \
+                         (guint64)*((const guint8 *)(p)+6)<<40|  \
+                         (guint64)*((const guint8 *)(p)+7)<<32|  \
+                         (guint64)*((const guint8 *)(p)+0)<<24|  \
+                         (guint64)*((const guint8 *)(p)+1)<<16|  \
+                         (guint64)*((const guint8 *)(p)+2)<<8|   \
+                         (guint64)*((const guint8 *)(p)+3)<<0)
+
 /* .vwr log file defines */
 #define B_SIZE      32768                           /* max var len message = 32 kB */
 #define VT_FRAME    0                               /* varlen msg is a frame */
@@ -70,6 +77,9 @@ typedef struct {
 
 } stats_common_fields;
 
+/* Size of those fields - regardless of how the compiler packs them */
+#define STATS_COMMON_FIELDS_LEN (2+2+2+2+4+2+2+4+4+8+8+4+4)
+
 /* Veriwave-specific extended radiotap header fields (following vwr_rtap_hdr above) */
 /* structure elements correspond one-to-one with the RADIOTAP_PRESENT bitmask below */
 /* NOTE: must ensure that elements are aligned to their "natural" packing */
@@ -90,6 +100,9 @@ typedef struct {
 
 } ext_rtap_fields;
 
+/* Size of those fields - regardless of how the compiler packs them */
+#define EXT_RTAP_FIELDS_LEN (2+2+2+1+1+1+1+2+2+2+4)
+
 /* Veriwave-specific Ethernettap header */
 typedef struct {
     guint16 it_len;                                 /* WHOLE radiotap header length (incl. */
@@ -100,6 +113,9 @@ typedef struct {
     guint32 it_pad2;                                /* pad out header to 16-byte boundary */
 } stats_ethernettap_fields;
 
+/* Size of those fields - regardless of how the compiler packs them */
+#define STATS_ETHERNETTAP_FIELDS_LEN (2+2+2+2+4+4+4)
+
 /* the bitmap offsets of the bits in it_present, above */
 /* also lists the expected field sizes in bytes */
 /* MUST BE IN SAME ORDER AS THE STRUCTURE ELEMENTS ABOVE */
@@ -518,99 +534,103 @@ enum radiotap_type {
 
 
 /*--------------------------------------------------------------------------------------*/
-/* various internal variables */
-
-/* offsets in stats block; these are dependent on the frame type (Ethernet/WLAN) and */
-/* version number of .vwr file, and are set up by setup_defaults() */
-static guint32      STATS_LEN;                      /* length of stats block trailer */
-static guint32      STATS_START_OFF;                /* STATS OFF AFTER HEADER */
-static guint32      VALID_OFF;                      /* bit 6 (0x40) is flow-is-valid flag */
-static guint32      MTYPE_OFF;                      /* offset of modulation type */
-static guint32      VCID_OFF;                       /* offset of VC ID */
-static guint32      FLOWSEQ_OFF;                    /* offset of signature sequence number */
-static guint32      FLOWID_OFF;                     /* offset of flow ID */
-static guint32      OCTET_OFF;                      /* offset of octets */
-static guint32      ERRORS_OFF;                     /* offset of error vector */
-static guint32      PATN_OFF;                       /* offset of pattern match vector */
-static guint32      RSSI_OFF;                       /* RSSI (NOTE: RSSI must be negated!) */
-static guint32      STARTT_OFF;                     /* offset of start time, 64 bits */
-static guint32      ENDT_OFF;                       /* offset of end time, 64 bits */
-static guint32      LATVAL_OFF;                     /* offset of latency, 32 bits */
-static guint32      INFO_OFF;                       /* offset of INFO field, 16 bits */
-static guint32      L1P_1_OFF;                      /* offset 1ST Byte of l1params */
-static guint32      L1P_2_OFF;                      /* offset 2nd Byte of l1params */
-static guint32      L4ID_OFF;                       /* LAYER 4 id offset*/
-static guint32      IPLEN_OFF;                      /* */
-static guint32      PLCP_LENGTH_OFF;                /* plcp length offset*/
-static guint32      FPGA_VERSION_OFF;               /* offset of fpga version field, 16 bits */
-static guint32      HEADER_VERSION_OFF;             /* offset of header version, 16 bits */
-static guint32      RXTX_OFF;                       /* offset of CMD bit, rx or tx */
-static guint32      MT_10_HALF;                     /* 10 Mb/s half-duplex */
-static guint32      MT_10_FULL;                     /* 10 Mb/s full-duplex */
-static guint32      MT_100_HALF;                    /* 100 Mb/s half-duplex */
-static guint32      MT_100_FULL;                    /* 100 Mb/s full-duplex */
-static guint32      MT_1G_HALF;                     /* 1 Gb/s half-duplex */
-static guint32      MT_1G_FULL;                     /* 1 Gb/s full-duplex */
-static guint32      FCS_ERROR;                      /* FCS error in frame */
-static guint32      CRYPTO_ERR;                     /* RX decrypt error flags */
-static guint32      PAYCHK_ERR;                     /* payload checksum failure */
-static guint32      RETRY_ERR;                      /* excessive retries on TX failure */
-static guint32      IS_RX;                          /* TX/RX bit in STATS block */
-static guint32      MT_MASK;                        /* modulation type mask */
-static guint32      VCID_MASK;                      /* VC ID is only 9 bits */
-static guint32      FLOW_VALID;                     /* flow-is-valid flag (else force to 0) */
-static guint32      RX_DECRYPTS;                    /* RX-frame-was-decrypted bits */
-static guint32      TX_DECRYPTS;                    /* TX-frame-was-decrypted bits */
-static guint32      FC_PROT_BIT;                    /* Protected Frame bit in FC1 of frame */
-static guint32      MT_CCKL;                        /* CCK modulation, long preamble */
-static guint32      MT_CCKS;                        /* CCK modulation, short preamble */
-static guint32      MT_OFDM;                        /* OFDM modulation */
-static guint32      MCS_INDEX_MASK;                 /* mcs index type mask */
-static guint32      PREAMBLE_MASK;                  /* preamble type mask */
-static guint32      IS_TX;                          /* FOR FPGA500021 this is true if tx */
-static guint32      FPGA_VERSION;
-static guint32      HEADER_IS_RX;
-static guint32      HEADER_IS_TX;
-static guint32      WEPTYPE;                        /* frame is WEP */
-static guint32      TKIPTYPE;                       /* frame is TKIP */
-static guint32      CCMPTYPE;                       /* frame is CCMP */
-static guint32      FRAME_TYPE_OFF;
-static guint32      IS_TCP;
-static guint32      IS_UDP;
-static guint32      IS_ICMP;
-static guint32      IS_IGMP;
-static guint32      IS_QOS;
-static guint32      IS_VLAN;
+/* Per-capture file private data structure */
+
+typedef struct {
+        /* offsets in stats block; these are dependent on the frame type (Ethernet/WLAN) and */
+        /* version number of .vwr file, and are set up by setup_defaults() */
+        guint32      STATS_LEN;                      /* length of stats block trailer */
+        guint32      STATS_START_OFF;                /* STATS OFF AFTER HEADER */
+        guint32      VALID_OFF;                      /* bit 6 (0x40) is flow-is-valid flag */
+        guint32      MTYPE_OFF;                      /* offset of modulation type */
+        guint32      VCID_OFF;                       /* offset of VC ID */
+        guint32      FLOWSEQ_OFF;                    /* offset of signature sequence number */
+        guint32      FLOWID_OFF;                     /* offset of flow ID */
+        guint32      OCTET_OFF;                      /* offset of octets */
+        guint32      ERRORS_OFF;                     /* offset of error vector */
+        guint32      PATN_OFF;                       /* offset of pattern match vector */
+        guint32      RSSI_OFF;                       /* RSSI (NOTE: RSSI must be negated!) */
+        guint32      STARTT_OFF;                     /* offset of start time, 64 bits */
+        guint32      ENDT_OFF;                       /* offset of end time, 64 bits */
+        guint32      LATVAL_OFF;                     /* offset of latency, 32 bits */
+        guint32      INFO_OFF;                       /* offset of INFO field, 16 bits */
+        guint32      L1P_1_OFF;                      /* offset 1ST Byte of l1params */
+        guint32      L1P_2_OFF;                      /* offset 2nd Byte of l1params */
+        guint32      L4ID_OFF;                       /* LAYER 4 id offset*/
+        guint32      IPLEN_OFF;                      /* */
+        guint32      PLCP_LENGTH_OFF;                /* plcp length offset*/
+        guint32      FPGA_VERSION_OFF;               /* offset of fpga version field, 16 bits */
+        guint32      HEADER_VERSION_OFF;             /* offset of header version, 16 bits */
+        guint32      RXTX_OFF;                       /* offset of CMD bit, rx or tx */
+        guint32      FRAME_TYPE_OFF;
+
+        /* other information about the file in question */
+        guint32      MT_10_HALF;                     /* 10 Mb/s half-duplex */
+        guint32      MT_10_FULL;                     /* 10 Mb/s full-duplex */
+        guint32      MT_100_HALF;                    /* 100 Mb/s half-duplex */
+        guint32      MT_100_FULL;                    /* 100 Mb/s full-duplex */
+        guint32      MT_1G_HALF;                     /* 1 Gb/s half-duplex */
+        guint32      MT_1G_FULL;                     /* 1 Gb/s full-duplex */
+        guint32      FCS_ERROR;                      /* FCS error in frame */
+        guint32      CRYPTO_ERR;                     /* RX decrypt error flags */
+        guint32      PAYCHK_ERR;                     /* payload checksum failure */
+        guint32      RETRY_ERR;                      /* excessive retries on TX failure */
+        guint8       IS_RX;                          /* TX/RX bit in STATS block */
+        guint8       MT_MASK;                        /* modulation type mask */
+        guint16      VCID_MASK;                      /* VC ID is only 9 bits */
+        guint32      FLOW_VALID;                     /* flow-is-valid flag (else force to 0) */
+        guint16      QOS_VALID;
+        guint32      RX_DECRYPTS;                    /* RX-frame-was-decrypted bits */
+        guint32      TX_DECRYPTS;                    /* TX-frame-was-decrypted bits */
+        guint32      FC_PROT_BIT;                    /* Protected Frame bit in FC1 of frame */
+        guint32      MT_CCKL;                        /* CCK modulation, long preamble */
+        guint32      MT_CCKS;                        /* CCK modulation, short preamble */
+        guint32      MT_OFDM;                        /* OFDM modulation */
+        guint32      MCS_INDEX_MASK;                 /* mcs index type mask */
+        guint32      FPGA_VERSION;
+        guint32      HEADER_IS_RX;
+        guint32      HEADER_IS_TX;
+        guint32      WEPTYPE;                        /* frame is WEP */
+        guint32      TKIPTYPE;                       /* frame is TKIP */
+        guint32      CCMPTYPE;                       /* frame is CCMP */
+        guint32      IS_TCP;
+        guint32      IS_UDP;
+        guint32      IS_ICMP;
+        guint32      IS_IGMP;
+        guint16      IS_QOS;
+        guint32      IS_VLAN;
+} vwr_t;
 
 /* internal utility functions */
-static int          decode_msg(register guint8 *, int *);
+static int          decode_msg(vwr_t *vwr, register guint8 *, int *, int *);
 static guint8       get_ofdm_rate(guint8 *);
 static guint8       get_cck_rate(guint8 *plcp);
-static void         setup_defaults(guint16);
+static void         setup_defaults(vwr_t *, guint16);
 
 static gboolean     vwr_read(wtap *, int *, gchar **, gint64 *);
-static gboolean     vwr_seek_read(wtap *, gint64, union wtap_pseudo_header *, guchar *,
+static gboolean     vwr_seek_read(wtap *, gint64, struct wtap_pkthdr *phdr, guchar *,
                                   int, int *, gchar **);
 
-static int          vwr_read_rec_header(FILE_T, int *, int *, gchar **);
-static void         vwr_read_rec_data(wtap *, guint8 *, guint8 *, int);
+static gboolean     vwr_read_rec_header(vwr_t *, FILE_T, int *, int *, int *, gchar **);
+static void         vwr_read_rec_data(wtap *, struct wtap_pkthdr *, guint8 *, guint8 *, int);
 
 static int          vwr_get_fpga_version(wtap *, int *, gchar **);
 
 
-static void         vwr_read_rec_data_vVW510021(wtap *, guint8 *, guint8 *, int);
-static void         vwr_read_rec_data_ethernet(wtap *, guint8 *, guint8 *, int);
+static void         vwr_read_rec_data_vVW510021(wtap *, struct wtap_pkthdr *, guint8 *, guint8 *, int, int);
+static void         vwr_read_rec_data_ethernet(wtap *, struct wtap_pkthdr *, guint8 *, guint8 *, int, int);
 
-static int          find_signature(register guint8 *, int, register guint32, register guint8);
+static int          find_signature(register guint8 *, int, int, register guint32, register guint8);
 static guint64      get_signature_ts(register guint8 *, int);
 
 /* open a .vwr file for reading */
 /* this does very little, except setting the wiretap header for a VWR file type */
 /* and the timestamp precision to microseconds */
 
-int vwr_open(wtap *wth, int *err, gchar **err_info _U_)
+int vwr_open(wtap *wth, int *err, gchar **err_info)
 {
     int fpgaVer;
+    vwr_t *vwr;
 
     *err = 0;
 
@@ -622,9 +642,13 @@ int vwr_open(wtap *wth, int *err, gchar **err_info _U_)
         return 0; /* not a VWR file */
     }
 
-    FPGA_VERSION = fpgaVer;
-        /* set the local module options first (this is a WLAN capture) */
-    setup_defaults(fpgaVer);
+    /* This is a vwr file */
+    vwr = (vwr_t *)g_malloc(sizeof(vwr_t));
+    wth->priv = (void *)vwr;
+
+    vwr->FPGA_VERSION = fpgaVer;
+    /* set the local module options first */
+    setup_defaults(vwr, fpgaVer);
 
     wth->snapshot_length = 0;
     wth->subtype_read = vwr_read;
@@ -640,7 +664,7 @@ int vwr_open(wtap *wth, int *err, gchar **err_info _U_)
         wth->file_type = WTAP_FILE_VWR_80211;
         wth->file_encap = WTAP_ENCAP_IXVERIWAVE;
 
-    } 
+    }
     else if (fpgaVer == vVW510012_E_FPGA) {
         wth->file_type = WTAP_FILE_VWR_ETH;
         wth->file_encap = WTAP_ENCAP_IXVERIWAVE;
@@ -664,24 +688,19 @@ int vwr_open(wtap *wth, int *err, gchar **err_info _U_)
 /* frame, and a 64-byte statistics block trailer */
 /* the PLCP frame consists of a 4-byte or 6-byte PLCP header, followed by the MAC frame */
 
-static gboolean vwr_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data_offset)
+static gboolean vwr_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
 {
-    int         ret;
+    vwr_t       *vwr = (vwr_t *)wth->priv;
     guint8      rec[B_SIZE];                        /* local buffer (holds input record) */
-    int         rec_size = 0;
+    int         rec_size = 0, IS_TX;
     guint8      *data_ptr;
     guint16     pkt_len;                            /* length of radiotap headers */
 
     /* read the next frame record header in the capture file; if no more frames, return */
-    /* if we found a frame record, set the data_offset value to the start of the frame */
-    /* record (i.e., the record header for the frame) */
-    if ((ret = vwr_read_rec_header(wth->fh, &rec_size, err, err_info)) <= 0) {
-        *err_info = g_strdup_printf("Record not readable or EOF encountered");
+    if (!vwr_read_rec_header(vwr, wth->fh, &rec_size, &IS_TX, err, err_info))
         return(FALSE);                                  /* Read error or EOF */
-    } else
-        wth->data_offset += ret;                        /* bump offset past header */
 
-    *data_offset = (wth->data_offset - 16);             /* set offset for random seek @PLCP */
+    *data_offset = (file_tell(wth->fh) - 16);           /* set offset for random seek @PLCP */
 
     /* got a frame record; read over entire record (frame + trailer) into a local buffer */
     /* if we don't get it all, then declare an error, we can't process the frame */
@@ -691,35 +710,39 @@ static gboolean vwr_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data
             *err = WTAP_ERR_SHORT_READ;
         return(FALSE);
     }
-    else
-        wth->data_offset += rec_size;                   /* got it OK, bump to next rec */
 
-    
+    if (rec_size < (int)vwr->STATS_LEN) {
+        *err = file_error(wth->fh, err_info);
+        if (*err == 0)
+            *err_info = g_strdup_printf("vwr: Invalid record length %d (must be at least %u)", rec_size, vwr->STATS_LEN);
+            *err = WTAP_ERR_BAD_FILE;
+        return(FALSE);
+    }
 
     /* before writing anything out, make sure the buffer has enough space for everything */
-    if ((FPGA_VERSION == vVW510021_W_FPGA) || (FPGA_VERSION == vVW510006_W_FPGA) )
+    if ((vwr->FPGA_VERSION == vVW510021_W_FPGA) || (vwr->FPGA_VERSION == vVW510006_W_FPGA) )
     /* frames are always 802.11 with an extended radiotap header */
-        pkt_len = (guint16)(rec_size + sizeof(stats_common_fields) + sizeof(ext_rtap_fields));
+        pkt_len = (guint16)(rec_size + STATS_COMMON_FIELDS_LEN + EXT_RTAP_FIELDS_LEN);
     else
         /* frames are always ethernet with an extended ethernettap header */
-        pkt_len = (guint16)(rec_size + sizeof(stats_common_fields) + sizeof(stats_ethernettap_fields));
+        pkt_len = (guint16)(rec_size + STATS_COMMON_FIELDS_LEN + STATS_ETHERNETTAP_FIELDS_LEN);
     buffer_assure_space(wth->frame_buffer, pkt_len);
     data_ptr = buffer_start_ptr(wth->frame_buffer);
 
     /* now format up the frame data */
-    switch (FPGA_VERSION)
+    switch (vwr->FPGA_VERSION)
     {
         case vVW510006_W_FPGA:
-            vwr_read_rec_data(wth, data_ptr, rec, rec_size);
+            vwr_read_rec_data(wth, &wth->phdr, data_ptr, rec, rec_size);
             break;
         case vVW510021_W_FPGA:
-            vwr_read_rec_data_vVW510021(wth, data_ptr, rec, rec_size);
+            vwr_read_rec_data_vVW510021(wth, &wth->phdr, data_ptr, rec, rec_size, IS_TX);
             break;
         case vVW510012_E_FPGA:
-            vwr_read_rec_data_ethernet(wth, data_ptr, rec, rec_size);
+            vwr_read_rec_data_ethernet(wth, &wth->phdr, data_ptr, rec, rec_size, IS_TX);
             break;
         case vVW510024_E_FPGA:
-            vwr_read_rec_data_ethernet(wth, data_ptr, rec, rec_size);
+            vwr_read_rec_data_ethernet(wth, &wth->phdr, data_ptr, rec, rec_size, IS_TX);
             break;
     }
 
@@ -739,18 +762,20 @@ static gboolean vwr_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data
 
 /* read a random frame in the middle of a file; the start of the PLCP frame is @ seek_off */
 
-static gboolean vwr_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header _U_, guchar *pd, int pkt_size _U_,
-    int *err, gchar **err_info _U_)
+static gboolean vwr_seek_read(wtap *wth, gint64 seek_off,
+    struct wtap_pkthdr *phdr, guchar *pd, int pkt_size _U_,
+    int *err, gchar **err_info)
 {
+    vwr_t       *vwr = (vwr_t *)wth->priv;
     guint8      rec[B_SIZE];                        /* local buffer (holds input record) */
-    int         rec_size;
+    int         rec_size, IS_TX;
 
     /* first seek to the indicated record header */
     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
         return(FALSE);
 
     /* read in the record header */
-    if (vwr_read_rec_header(wth->random_fh, &rec_size, err, err_info) <= 0)
+    if (!vwr_read_rec_header(vwr, wth->random_fh, &rec_size, &IS_TX, err, err_info))
         return(FALSE);                                  /* Read error or EOF */
 
     /* read over the entire record (frame + trailer) into a local buffer */
@@ -763,18 +788,18 @@ static gboolean vwr_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_head
     }
 
     /* now format up the frame data into the passed buffer, according to the FPGA type */
-    switch (FPGA_VERSION) {
+    switch (vwr->FPGA_VERSION) {
         case vVW510006_W_FPGA:
-            vwr_read_rec_data(wth, pd, rec, rec_size);
+            vwr_read_rec_data(wth, phdr, pd, rec, rec_size);
             break;
         case vVW510021_W_FPGA:
-            vwr_read_rec_data_vVW510021(wth, pd, rec, rec_size);
+            vwr_read_rec_data_vVW510021(wth, phdr, pd, rec, rec_size, IS_TX);
             break;
         case vVW510012_E_FPGA:
-            vwr_read_rec_data_ethernet(wth, pd, rec, rec_size);
+            vwr_read_rec_data_ethernet(wth, phdr, pd, rec, rec_size, IS_TX);
             break;
         case vVW510024_E_FPGA:
-            vwr_read_rec_data_ethernet(wth, pd, rec, rec_size);
+            vwr_read_rec_data_ethernet(wth, phdr, pd, rec, rec_size, IS_TX);
             break;
     }
 
@@ -783,18 +808,15 @@ static gboolean vwr_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_head
 
 /* scan down in the input capture file to find the next frame header */
 /* decode and skip over all non-frame messages that are in the way */
-/* return the offset into the file for the first byte of the frame (if found); -1 on */
-/* error; zero if EOF with no frame */
-/* also return the frame size in bytes */
+/* return TRUE on success, FALSE on EOF or error */
+/* also return the frame size in bytes and the "is transmitted frame" flag */
 
-static int vwr_read_rec_header(FILE_T fh, int *rec_size, int *err, gchar **err_info)
+static gboolean vwr_read_rec_header(vwr_t *vwr, FILE_T fh, int *rec_size, int *IS_TX, int *err, gchar **err_info)
 {
-    int     bytes_read, file_off;
     int     f_len, v_type;
     guint8  header[16];
 
     errno = WTAP_ERR_CANT_READ;
-    file_off = 0;
     *rec_size = 0;
 
     /* read out the file data in 16-byte messages, stopping either after we find a frame, */
@@ -803,35 +825,28 @@ static int vwr_read_rec_header(FILE_T fh, int *rec_size, int *err, gchar **err_i
     /* variable-length item, we read the variable length item out and discard it */
     /* if we find a frame, we return (with the header in the passed buffer) */
     while (1) {
-        if ((bytes_read = file_read(header, 16, fh)) != 16) {
+        if (file_read(header, 16, fh) != 16) {
             *err = file_error(fh, err_info);
-            if (*err != 0)
-                return(-1);
-            else
-                return(0);
+            return(FALSE);
         }
-        else
-            file_off += bytes_read;
 
         /* got a header; invoke decode-message function to parse and process it */
         /* if the function returns a length, then a frame or variable-length message */
         /* follows the 16-byte message */
         /* if the variable length message is not a frame, simply skip over it */
-        if ((f_len = decode_msg(header, &v_type)) != 0) {
+        if ((f_len = decode_msg(vwr, header, &v_type, IS_TX)) != 0) {
             if (f_len > B_SIZE) {
                 *err = WTAP_ERR_BAD_FILE;
                 *err_info = g_strdup_printf("vwr: Invalid message record length %d", f_len);
-                return(-1);
+                return(FALSE);
             }
             else if (v_type != VT_FRAME) {
                 if (file_seek(fh, f_len, SEEK_CUR, err) < 0)
-                    return(-1);
-                else
-                    file_off += f_len;
+                    return(FALSE);
             }
             else {
                 *rec_size = f_len;
-                return(file_off);
+                return(TRUE);
             }
         }
     }
@@ -841,14 +856,14 @@ static int vwr_read_rec_header(FILE_T fh, int *rec_size, int *err, gchar **err_i
 /* return FPGA version if it's a known version, UNKNOWN_FPGA if it's not, */
 /* and -1 on an I/O error. */
 
-static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info _U_)
+static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info)
 {
     guint8      rec[B_SIZE];                        /* local buffer (holds input record) */
     guint8      header[16];
     int         rec_size = 0;
     guint8      i;
     guint8      *s_510006_ptr = NULL;
-    guint8      *s_510024_ptr = NULL; 
+    guint8      *s_510024_ptr = NULL;
     guint8      *s_510012_ptr = NULL;               /* stats pointers */
     gint64      filePos = -1;
     guint32     frame_type = 0;
@@ -875,7 +890,7 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info _U_)
         /* if the function returns a length, then a frame or variable-length message */
         /* follows the 16-byte message */
         /* if the variable length message is not a frame, simply skip over it */
-        if ((f_len = decode_msg(header, &v_type)) != 0) {
+        if ((f_len = decode_msg(NULL, header, &v_type, NULL)) != 0) {
             if (f_len > B_SIZE) {
                 /* Treat this here as an indication that the file probably */
                 /* isn't an vwr file. */
@@ -891,9 +906,9 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info _U_)
                 /* if we don't get it all, assume this isn't a vwr file */
                 if (file_read(rec, rec_size, wth->fh) != rec_size) {
                     *err = file_error(wth->fh, err_info);
-                    if (*err == 0)
-                        return(UNKNOWN_FPGA); /* short read - not a vwr file */
-                    return(-1);
+                    if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+                        return(-1);
+                    return(UNKNOWN_FPGA); /* short read - not a vwr file */
                 }
 
 
@@ -907,17 +922,16 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info _U_)
                 if (rec_size > v22_W_STATS_LEN) {
                     s_510006_ptr = &(rec[rec_size - v22_W_STATS_LEN]);      /* point to 510006 WLAN */
                                                                             /* stats block */
-                
-                    data_length = (s_510006_ptr[v22_W_OCTET_OFF] << 8) + s_510006_ptr[v22_W_OCTET_OFF + 1];
+
+                    data_length = pntohs(&s_510006_ptr[v22_W_OCTET_OFF]);
                     i = 0;
                     while (((data_length + i) % 4) != 0)
                         i = i + 1;
 
-                    frame_type = (s_510006_ptr[v22_W_FRAME_TYPE_OFF] << 24) | (s_510006_ptr[v22_W_FRAME_TYPE_OFF + 1] << 16) |
-                                 (s_510006_ptr[v22_W_FRAME_TYPE_OFF + 2] << 8) | (s_510006_ptr[v22_W_FRAME_TYPE_OFF + 3]);
+                    frame_type = pntohl(&s_510006_ptr[v22_W_FRAME_TYPE_OFF]);
 
                     if (rec_size == (data_length + v22_W_STATS_LEN + i) && (frame_type & v22_W_IS_80211) == 0x1000000) {
-                        fpga_version = vVW510006_W_FPGA; 
+                        fpga_version = vVW510006_W_FPGA;
                     }
                 }
 
@@ -925,13 +939,13 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info _U_)
                 if ((rec_size > v22_E_STATS_LEN) && (fpga_version == 1000)) {
                     s_510012_ptr = &(rec[rec_size - v22_E_STATS_LEN]);      /* point to 510012 enet */
                                                                             /* stats block */
-                    data_length = (s_510012_ptr[v22_E_OCTET_OFF] << 8) + s_510012_ptr[v22_E_OCTET_OFF + 1];
+                    data_length = pntohs(&s_510012_ptr[v22_E_OCTET_OFF]);
                     i = 0;
                     while (((data_length + i) % 4) != 0)
                         i = i + 1;
 
                     if (rec_size == (data_length + v22_E_STATS_LEN + i))
-                        fpga_version = vVW510012_E_FPGA; 
+                        fpga_version = vVW510012_E_FPGA;
                 }
 
 
@@ -947,22 +961,20 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info _U_)
 
                     /*the 12 is from the 12 bytes of plcp header */
                     if (rec_size == (data_length + vVW510021_W_STATS_LEN +vVW510021_W_AFTERHEADER_LEN+12+i))
-                        fpga_version = vVW510021_W_FPGA; 
+                        fpga_version = vVW510021_W_FPGA;
                 }
 
                 /* Finally the Series II Ethernet */
                 if ((rec_size > vVW510024_E_STATS_LEN) && (fpga_version == 1000)) {
                     s_510024_ptr = &(rec[rec_size - vVW510024_E_STATS_LEN]);    /* point to 510024 ENET */
-                    data_length = (s_510024_ptr[vVW510024_E_MSDU_LENGTH_OFF] << 8) + s_510024_ptr[vVW510024_E_MSDU_LENGTH_OFF + 1];
+                    data_length = pntohs(&s_510024_ptr[vVW510024_E_MSDU_LENGTH_OFF]);
 
                     i = 0;
                     while (((data_length + i) % 4) != 0)
                         i = i + 1;
 
-                    if (rec_size == (data_length + vVW510024_E_STATS_LEN + i)) {
-                        if (s_510024_ptr != NULL) 
-                            fpga_version = vVW510024_E_FPGA; 
-                    }
+                    if (rec_size == (data_length + vVW510024_E_STATS_LEN + i))
+                        fpga_version = vVW510024_E_FPGA;
                 }
                 if (fpga_version != 1000)
                 {
@@ -978,17 +990,19 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info _U_)
     }
 
     *err = file_error(wth->fh, err_info);
-    if (*err == 0)
-        return(UNKNOWN_FPGA); /* short read - not a vwr file */
-    return(-1);
+    if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+        return(-1);
+    return(UNKNOWN_FPGA); /* short read - not a vwr file */
 }
 
 /* copy the actual packet data from the capture file into the target data block */
 /* the packet is constructed as a 38-byte VeriWave-extended Radiotap header plus the raw */
 /* MAC octets */
 
-static void vwr_read_rec_data(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_size)
+static void vwr_read_rec_data(wtap *wth, struct wtap_pkthdr *phdr,
+    guint8 *data_ptr, guint8 *rec, int rec_size)
 {
+    vwr_t           *vwr = (vwr_t *)wth->priv;
     int             bytes_written = 0;              /* bytes output to buf so far */
     register int    i;                              /* temps */
     register guint8 *s_ptr, *m_ptr;                 /* stats and MPDU pointers */
@@ -1015,62 +1029,56 @@ static void vwr_read_rec_data(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_
 
     /* calculate the start of the statistics block in the buffer */
     /* also get a bunch of fields from the stats block */
-    s_ptr = &(rec[rec_size - STATS_LEN]);               /* point to it */
-    m_type = s_ptr[MTYPE_OFF] & (guint8)MT_MASK;
-    f_tx = !(s_ptr[MTYPE_OFF] & (guint8)IS_RX);
-    octets = (s_ptr[OCTET_OFF] << 8) + s_ptr[OCTET_OFF + 1];
-    vc_id = ((s_ptr[VCID_OFF] << 8) + s_ptr[VCID_OFF + 1]) & (guint8)VCID_MASK;
-    flow_seq = s_ptr[FLOWSEQ_OFF];
-
-    f_flow = (s_ptr[VALID_OFF] & (guint8)FLOW_VALID) != 0;
-    f_qos = (s_ptr[MTYPE_OFF] & (guint8)IS_QOS) != 0;
-    frame_type = (s_ptr[FRAME_TYPE_OFF] << 24) | (s_ptr[FRAME_TYPE_OFF + 1] << 16) |
-                 (s_ptr[FRAME_TYPE_OFF + 2] << 8) | (s_ptr[FRAME_TYPE_OFF + 3]);
-
-    latency = (s_ptr[LATVAL_OFF + 6] << 8) | (s_ptr[LATVAL_OFF + 7]);   /* latency MSbytes */
+    s_ptr = &(rec[rec_size - vwr->STATS_LEN]);               /* point to it */
+    m_type = s_ptr[vwr->MTYPE_OFF] & vwr->MT_MASK;
+    f_tx = !(s_ptr[vwr->MTYPE_OFF] & vwr->IS_RX);
+    octets = pntohs(&s_ptr[vwr->OCTET_OFF]);
+    vc_id = pntohs(&s_ptr[vwr->VCID_OFF]) & vwr->VCID_MASK;
+    flow_seq = s_ptr[vwr->FLOWSEQ_OFF];
+
+    f_flow = (s_ptr[vwr->VALID_OFF] & (guint8)vwr->FLOW_VALID) != 0;
+    f_qos = (s_ptr[vwr->MTYPE_OFF] & vwr->IS_QOS) != 0;
+    frame_type = pntohl(&s_ptr[vwr->FRAME_TYPE_OFF]);
+
+    /* XXX - this is 48 bits, in a weird byte order */
+    latency = pntohs(&s_ptr[vwr->LATVAL_OFF + 6]);   /* latency MSbytes */
     for (i = 0; i < 4; i++)
-        latency = (latency << 8) | s_ptr[LATVAL_OFF + i];
+        latency = (latency << 8) | s_ptr[vwr->LATVAL_OFF + i];
 
-    flow_id = 0;                                    /* init flow ID to 0 */
-    flow_id = (s_ptr[FLOWID_OFF + 1] << 8) + s_ptr[FLOWID_OFF + 2];     /* only 16 LSBs kept */
-    errors = (s_ptr[ERRORS_OFF] << 8) + s_ptr[ERRORS_OFF + 1];
+    flow_id = pntohs(&s_ptr[vwr->FLOWID_OFF + 1]);   /* only 16 LSBs kept */
+    errors = pntohs(&s_ptr[vwr->ERRORS_OFF]);
 
-    info = (s_ptr[INFO_OFF] << 8) + s_ptr[INFO_OFF + 1];
-    rssi = (s_ptr[RSSI_OFF] & 0x80) ? (-1 * (s_ptr[RSSI_OFF] & 0x7f)) : s_ptr[RSSI_OFF];
+    info = pntohs(&s_ptr[vwr->INFO_OFF]);
+    rssi = (s_ptr[vwr->RSSI_OFF] & 0x80) ? (-1 * (s_ptr[vwr->RSSI_OFF] & 0x7f)) : s_ptr[vwr->RSSI_OFF];
     /*if ((info && AGGREGATE_MASK) != 0)*/
     /* this length includes the Start_Spacing + Delimiter + MPDU + Padding for each piece of the aggregate*/
-        /*ht_len = (int)rec[PLCP_LENGTH_OFF] + ((int)rec[PLCP_LENGTH_OFF+1] << 8);*/
+        /*ht_len = (int)pletohs(&rec[PLCP_LENGTH_OFF]);*/
 
     /* decode OFDM or CCK PLCP header and determine rate and short preamble flag */
     /* the SIGNAL byte is always the first byte of the PLCP header in the frame */
-    if (m_type == MT_OFDM)
+    if (m_type == vwr->MT_OFDM)
         rate = get_ofdm_rate(rec);
-    else if ((m_type == MT_CCKL) || (m_type == MT_CCKS))
+    else if ((m_type == vwr->MT_CCKL) || (m_type == vwr->MT_CCKS))
         rate = get_cck_rate(rec);
     else
         rate = 1;
     /* calculate the MPDU size/ptr stuff; MPDU starts at 4 or 6 depending on OFDM/CCK */
     /* note that the number of octets in the frame also varies depending on OFDM/CCK, */
     /*  because the PLCP header is prepended to the actual MPDU */
-    m_ptr = &(rec[((m_type == MT_OFDM) ? 4 : 6)]);
-    octets -= (m_type == MT_OFDM) ? 4 : 6;
+    m_ptr = &(rec[((m_type == vwr->MT_OFDM) ? 4 : 6)]);
+    octets -= (m_type == vwr->MT_OFDM) ? 4 : 6;
 
     /* sanity check the octets field to determine if it is OK (or segfaults result) */
     /* if it's greater, then truncate to actual record size */
-    if (octets > (rec_size - (int)STATS_LEN))
-        octets = (rec_size - (int)STATS_LEN);
+    if (octets > (rec_size - (int)vwr->STATS_LEN))
+        octets = (rec_size - (int)vwr->STATS_LEN);
     msdu_length = octets;
 
 
     /* calculate start & end times (in sec/usec), converting 64-bit times to usec */
-    for (i = 0; i < 4; i++)                             /* 64-bit times are "Corey-endian" */
-        s_time = (s_time << 8) | s_ptr[STARTT_OFF + i + 4];
-    for (i = 0; i < 4; i++)
-        s_time = (s_time << 8) | s_ptr[STARTT_OFF + i];
-    for (i = 0; i < 4; i++)
-        e_time = (e_time << 8) | s_ptr[ENDT_OFF + i + 4];
-    for (i = 0; i < 4; i++)
-        e_time = (e_time << 8) | s_ptr[ENDT_OFF + i];
+    /* 64-bit times are "Corey-endian" */
+    s_time = pcoreytohll(&s_ptr[vwr->STARTT_OFF]);
+    e_time = pcoreytohll(&s_ptr[vwr->ENDT_OFF]);
 
     /* find the packet duration (difference between start and end times) */
     d_time = (guint32)((e_time - s_time) / NS_IN_US);   /* find diff, converting to usec */
@@ -1086,19 +1094,19 @@ static void vwr_read_rec_data(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_
     /* extract the 32 LSBs of the signature timestamp field from the data block*/
     mac_snap = (f_qos ? 34 : 32);                       /* 24 (MAC) + 2 (QoS) + 8 (SNAP) */
 
-    if (frame_type & IS_TCP)                            /* signature offset for TCP frame */
+    if (frame_type & vwr->IS_TCP)                       /* signature offset for TCP frame */
     {
         pay_off = mac_snap + 40;
     }
-    else if (frame_type & IS_UDP)                       /* signature offset for UDP frame */
+    else if (frame_type & vwr->IS_UDP)                  /* signature offset for UDP frame */
     {
         pay_off = mac_snap + 28;
     }
-    else if (frame_type & IS_ICMP)                      /* signature offset for ICMP frame */
+    else if (frame_type & vwr->IS_ICMP)                 /* signature offset for ICMP frame */
     {
         pay_off = mac_snap + 24;
     }
-    else if (frame_type & IS_IGMP)                      /* signature offset for IGMPv2 frame */
+    else if (frame_type & vwr->IS_IGMP)                 /* signature offset for IGMPv2 frame */
     {
         pay_off = mac_snap + 28;
     }
@@ -1107,7 +1115,7 @@ static void vwr_read_rec_data(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_
         pay_off = mac_snap + 20;
     }
 
-    sig_off = find_signature(m_ptr, pay_off, flow_id, flow_seq);
+    sig_off = find_signature(m_ptr, pay_off, rec_size, flow_id, flow_seq);
     if ((m_ptr[sig_off] == 0xdd) && (sig_off + 15 <= msdu_length) && (f_flow != 0))
         sig_ts = get_signature_ts(m_ptr, sig_off);
     else
@@ -1119,28 +1127,27 @@ static void vwr_read_rec_data(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_
     /* block), and should always represent the actual number of bytes in the file */
     /* len is the length of the original packet before truncation;  */
     /* the FCS is NOT included */
-    r_hdr_len = sizeof(stats_common_fields) + sizeof(er_fields);
+    r_hdr_len = STATS_COMMON_FIELDS_LEN + EXT_RTAP_FIELDS_LEN;
 
-    wth->phdr.len = (msdu_length - 4) + r_hdr_len;
-    wth->phdr.caplen = (octets - 4) + r_hdr_len;
+    phdr->len = (msdu_length - 4) + r_hdr_len;
+    phdr->caplen = (octets - 4) + r_hdr_len;
 
-    wth->phdr.presence_flags = WTAP_HAS_TS;
+    phdr->presence_flags = WTAP_HAS_TS;
 
-    wth->phdr.ts.secs = (time_t)s_sec;
-    wth->phdr.ts.nsecs = (long)(s_usec * 1000);
-    wth->phdr.pkt_encap = WTAP_ENCAP_IXVERIWAVE;
+    phdr->ts.secs = (time_t)s_sec;
+    phdr->ts.nsecs = (int)(s_usec * 1000);
+    phdr->pkt_encap = WTAP_ENCAP_IXVERIWAVE;
 
-    /* generate and write out the radiotap header, set the version number to 1 (extended) */
+    /* generate and copy out the radiotap header, set the port type to 0 (WLAN) */
     common_fields.vw_port_type = 0;
-    common_fields.it_len = sizeof(stats_common_fields);
-    er_fields.it_len = sizeof(er_fields);
+    common_fields.it_len = STATS_COMMON_FIELDS_LEN;
+    er_fields.it_len = EXT_RTAP_FIELDS_LEN;
 
     /* create the extended radiotap header fields */
-    er_fields.flags = 0;
-    er_fields.flags = (m_type == MT_CCKS) ? RADIOTAP_F_SHORTPRE : 0;
+    er_fields.flags = (m_type == vwr->MT_CCKS) ? RADIOTAP_F_SHORTPRE : 0;
 
     er_fields.rate = rate;
-    er_fields.chanflags = (m_type == MT_OFDM) ? CHAN_OFDM : CHAN_CCK;
+    er_fields.chanflags = (m_type == vwr->MT_OFDM) ? CHAN_OFDM : CHAN_CCK;
     er_fields.signal = f_tx ? 100 : (gint8)rssi;
     er_fields.tx_power = f_tx ? ((gint8)rssi) : 100;
 
@@ -1148,17 +1155,17 @@ static void vwr_read_rec_data(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_
     er_fields.vw_flags = 0;
     if (f_tx)
         er_fields.vw_flags |= RADIOTAP_VWF_TXF;
-    if (errors & FCS_ERROR)
+    if (errors & vwr->FCS_ERROR)
         er_fields.vw_flags |= RADIOTAP_VWF_FCSERR;
-    if (!f_tx && (errors & CRYPTO_ERR))
+    if (!f_tx && (errors & vwr->CRYPTO_ERR))
         er_fields.vw_flags |= RADIOTAP_VWF_DCRERR;
-    if (!f_tx && (errors & RETRY_ERR))
+    if (!f_tx && (errors & vwr->RETRY_ERR))
         er_fields.vw_flags |= RADIOTAP_VWF_RETRERR;
-    if (info & WEPTYPE)
+    if (info & vwr->WEPTYPE)
         er_fields.vw_flags |= RADIOTAP_VWF_IS_WEP;
-    else if (info & TKIPTYPE)
+    else if (info & vwr->TKIPTYPE)
         er_fields.vw_flags |= RADIOTAP_VWF_IS_TKIP;
-    else if (info & CCMPTYPE)
+    else if (info & vwr->CCMPTYPE)
         er_fields.vw_flags |= RADIOTAP_VWF_IS_CCMP;
 
     er_fields.vw_errors = (guint32)errors;
@@ -1177,14 +1184,63 @@ static void vwr_read_rec_data(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_
     common_fields.vw_startt = start_time;                   /* record start & end times of frame */
     common_fields.vw_endt = end_time;
 
-    memcpy(&data_ptr[bytes_written], &common_fields, sizeof(common_fields));
-    bytes_written += sizeof(common_fields);
-
-    memcpy(&data_ptr[bytes_written], &er_fields, sizeof(er_fields));
-    bytes_written += sizeof(er_fields);
-
-    /* finally, dump the whole MAC frame to file as-is; exclude FCS */
-    if ( rec_size < ((int)msdu_length + (int)STATS_LEN) ) 
+    /* put common_fields into the packet buffer in little-endian byte order */
+    phtoles(&data_ptr[bytes_written], common_fields.vw_port_type);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_fields.it_len);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_fields.vw_msdu_length);
+    bytes_written += 2;
+    /* padding */
+    memset(&data_ptr[bytes_written], 0, 2);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], common_fields.vw_flowid);
+    bytes_written += 4;
+    phtoles(&data_ptr[bytes_written], common_fields.vw_vcid);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_fields.vw_seqnum);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], common_fields.vw_latency);
+    bytes_written += 4;
+    phtolel(&data_ptr[bytes_written], common_fields.vw_sig_ts);
+    bytes_written += 4;
+    phtolell(&data_ptr[bytes_written], common_fields.vw_startt);
+    bytes_written += 8;
+    phtolell(&data_ptr[bytes_written], common_fields.vw_endt);
+    bytes_written += 8;
+    phtolel(&data_ptr[bytes_written], common_fields.vw_pktdur);
+    bytes_written += 4;
+    /* padding */
+    memset(&data_ptr[bytes_written], 0, 4);
+    bytes_written += 4;
+
+    /* put er_fields into the packet buffer in little-endian byte order */
+    phtoles(&data_ptr[bytes_written], er_fields.it_len);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], er_fields.flags);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], er_fields.chanflags);
+    bytes_written += 2;
+    data_ptr[bytes_written] = er_fields.rate;
+    bytes_written += 1;
+    data_ptr[bytes_written] = er_fields.signal;
+    bytes_written += 1;
+    data_ptr[bytes_written] = er_fields.tx_power;
+    bytes_written += 1;
+    /* padding */
+    data_ptr[bytes_written] = 0;
+    bytes_written += 1;
+    phtoles(&data_ptr[bytes_written], er_fields.vw_flags);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], er_fields.vw_ht_length);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], er_fields.vw_info);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], er_fields.vw_errors);
+    bytes_written += 4;
+
+    /* finally, copy the whole MAC frame to the packet buffer as-is; exclude FCS */
+    if ( rec_size < ((int)msdu_length + (int)vwr->STATS_LEN) )
         /*something's been truncated, DUMP AS-IS*/
         memcpy(&data_ptr[bytes_written], m_ptr, octets);
     else if (octets >= 4)
@@ -1204,21 +1260,23 @@ static void vwr_read_rec_data(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_
 /* the packet is constructed as a 38-byte VeriWave-extended Radiotap header plus the raw */
 /* MAC octets */
 
-static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_size)
+static void vwr_read_rec_data_vVW510021(wtap *wth, struct wtap_pkthdr *phdr,
+    guint8 *data_ptr, guint8 *rec, int rec_size, int IS_TX)
 {
+    vwr_t           *vwr = (vwr_t *)wth->priv;
     int             bytes_written = 0;              /* bytes output to buf so far */
-    int PLCP_OFF = 8;
+    int             PLCP_OFF = 8;
     register int    i;                              /* temps */
     register        guint8  *s_start_ptr,*s_trail_ptr, *m_ptr,*plcp_ptr; /* stats & MPDU ptr */
     gint16          msdu_length, actual_octets;        /* octets in frame */
     guint8          l1p_1,l1p_2, flow_seq, plcp_type, mcs_index;   /* mod (CCK-L/CCK-S/OFDM) */
     guint64         s_time = LL_ZERO, e_time = LL_ZERO; /* start/end */
                                                     /*  times, nsec */
-    guint64 latency = LL_ZERO;
+    guint64         latency = LL_ZERO;
     guint64         start_time, s_sec, s_usec = LL_ZERO; /* start time, sec + usec */
     guint64         end_time;                       /* end time */
     guint16         info, validityBits;             /* INFO/ERRORS fields in stats blk */
-    guint32         errors = 0;
+    guint32         errors;
     gint16          rssi;                           /* RSSI, signed 16-bit number */
     int             f_tx;                           /* flag: if set, is a TX frame */
     int             f_flow, f_qos;                  /* flags: flow valid, frame is QoS */
@@ -1227,7 +1285,7 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
     guint16         vc_id, ht_len=0;                /* VC ID , total ip length*/
     guint32         flow_id, d_time;                /* flow ID, packet duration*/
     guint16         r_hdr_len;                      /* length of radiotap headers */
-    ext_rtap_fields     er_fields;                  /* extended radiotap fields */
+    ext_rtap_fields er_fields;                      /* extended radiotap fields */
     stats_common_fields common_fields;              /* extended radiotap fields */
     gint8           tx_power = 0;                   /* transmit power value in dBm */
     int             mac_snap, sig_off, pay_off;     /* MAC+SNAP header len, signature offset */
@@ -1235,56 +1293,53 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
     guint16         chanflags = 0;                  /* extended radio tap channel flags */
     guint16         radioflags = 0;                 /* extended radio tap flags */
     guint64         delta_b;                        /* Used for calculating latency */
-    
+
     /* calculate the start of the statistics block in the buffer */
     /* also get a bunch of fields from the stats block */
     s_start_ptr = &(rec[0]);
-    s_trail_ptr = &(rec[rec_size - STATS_LEN]);             /* point to it */
+    s_trail_ptr = &(rec[rec_size - vwr->STATS_LEN]);             /* point to it */
 
-    l1p_1 = s_start_ptr[L1P_1_OFF];
+    l1p_1 = s_start_ptr[vwr->L1P_1_OFF];
     mcs_index = l1p_1 & (guint8)vVW510021_W_MCS_MASK;
-    l1p_2 = s_start_ptr[L1P_2_OFF];
+    l1p_2 = s_start_ptr[vwr->L1P_2_OFF];
     plcp_type = l1p_2 & (guint8)vVW510021_W_PLCPC_MASK;
-    msdu_length = (256 * (s_start_ptr[OCTET_OFF + 1] & 0x1f)) + s_start_ptr[OCTET_OFF];
+    msdu_length = (256 * (s_start_ptr[vwr->OCTET_OFF + 1] & 0x1f)) + s_start_ptr[vwr->OCTET_OFF];
     /* If the packet has an MSDU length of 0, then bail - malformed packet */
     /* if (msdu_length < 4) return; */
     actual_octets = msdu_length;
-    
+
 
     f_tx = IS_TX;
-    vc_id = ((s_start_ptr[VCID_OFF] << 8) | (s_start_ptr[VCID_OFF + 1]));
-    flow_seq = s_trail_ptr[FLOWSEQ_OFF];
-    validityBits = (s_trail_ptr[VALID_OFF] << 8) + s_trail_ptr[VALID_OFF + 1];
+    vc_id = pntohs(&s_start_ptr[vwr->VCID_OFF]);
+    flow_seq = s_trail_ptr[vwr->FLOWSEQ_OFF];
+    validityBits = pntohs(&s_trail_ptr[vwr->VALID_OFF]);
 
-    f_flow = (validityBits & FLOW_VALID) != 0;
-    f_qos = (validityBits & IS_QOS) != 0;
+    f_flow = (validityBits & vwr->FLOW_VALID) != 0;
+    f_qos = (validityBits & vwr->IS_QOS) != 0;
 
-    frame_type = (s_trail_ptr[FRAME_TYPE_OFF] << 24) | (s_trail_ptr[FRAME_TYPE_OFF + 1] << 16) |
-                 (s_trail_ptr[FRAME_TYPE_OFF + 2] << 8) | (s_trail_ptr[FRAME_TYPE_OFF + 3]);
+    frame_type = pntohl(&s_trail_ptr[vwr->FRAME_TYPE_OFF]);
 
-    flow_id = 0x00000000; latency = 0x00000000;               /* clear flow ID & latency */
-    flow_id = (s_trail_ptr[FLOWID_OFF] << 16) | (s_trail_ptr[FLOWID_OFF + 1] << 8) |
-                                s_trail_ptr[FLOWID_OFF + 2]; /* all 24 bits valid */
+    latency = 0x00000000;                                     /* clear latency */
+    flow_id = pntoh24(&s_trail_ptr[vwr->FLOWID_OFF]);         /* all 24 bits valid */
     /* for tx latency is duration, for rx latency is timestamp */
     /* get 48-bit latency value */
-    tsid = (s_trail_ptr[LATVAL_OFF + 6] << 8) | (s_trail_ptr[LATVAL_OFF + 7]);
+    tsid = (s_trail_ptr[vwr->LATVAL_OFF + 6] << 8) | (s_trail_ptr[vwr->LATVAL_OFF + 7]);
 
     for (i = 0; i < 4; i++)
-        tsid = (tsid << 8) | s_trail_ptr[LATVAL_OFF + i];
+        tsid = (tsid << 8) | s_trail_ptr[vwr->LATVAL_OFF + i];
 
-    for (i = 0; i < 4; i++)
-        errors = (errors << 8) | s_trail_ptr[ERRORS_OFF + i];
-    info = (s_trail_ptr[INFO_OFF] << 8) + s_trail_ptr[INFO_OFF + 1];
-    if ((info && 0xFC00) != 0)
+    errors = pntohl(&s_trail_ptr[vwr->ERRORS_OFF]);
+    info = pntohs(&s_trail_ptr[vwr->INFO_OFF]);
+    if ((info & 0xFC00) != 0)
     /* this length includes the Start_Spacing + Delimiter + MPDU + Padding for each piece of the aggregate*/
-        ht_len = s_start_ptr[PLCP_LENGTH_OFF] + (s_start_ptr[PLCP_LENGTH_OFF+1] << 8);
+        ht_len = pletohs(&s_start_ptr[vwr->PLCP_LENGTH_OFF]);
 
-    rssi = s_start_ptr[RSSI_OFF];
+    rssi = s_start_ptr[vwr->RSSI_OFF];
     if (f_tx) {
         if (rssi & 0x80)
-        tx_power = -1 * (rssi & 0x7f);
+            tx_power = -1 * (rssi & 0x7f);
         else
-        tx_power = rssi & 0x7f;
+            tx_power = rssi & 0x7f;
     } else {
         if (rssi > 128) rssi = rssi - 256;  /* Signed 2's complement */
     }
@@ -1321,11 +1376,11 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
     else {
         rate = 1;
     }
-    
+
     /* calculate the MPDU size/ptr stuff; MPDU starts at 4 or 6 depending on OFDM/CCK */
     /* note that the number of octets in the frame also varies depending on OFDM/CCK, */
     /*  because the PLCP header is prepended to the actual MPDU */
-    /*the 8 is from the 8 bytes of stats block that precede the plcps , 
+    /*the 8 is from the 8 bytes of stats block that precede the plcps ,
     the 12 is for 11 bytes plcp and 1 byte of pad before the data*/
     m_ptr = &(rec[8+12]);
 
@@ -1336,19 +1391,14 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
         msdu_length = (rec_size );
     }
 */
-    if (msdu_length > (rec_size - (int)STATS_LEN)) {
-        msdu_length = (rec_size - (int)STATS_LEN);
+    if (msdu_length > (rec_size - (int)vwr->STATS_LEN)) {
+        msdu_length = (rec_size - (int)vwr->STATS_LEN);
     }
 
     /* calculate start & end times (in sec/usec), converting 64-bit times to usec */
-    for (i = 0; i < 4; i++)                             /* 64-bit times are "Corey-endian" */
-        s_time = (s_time << 8) | s_trail_ptr[STARTT_OFF + i + 4];
-    for (i = 0; i < 4; i++)
-        s_time = (s_time << 8) | s_trail_ptr[STARTT_OFF + i];
-    for (i = 0; i < 4; i++)
-        e_time = (e_time << 8) | s_trail_ptr[ENDT_OFF + i + 4];
-    for (i = 0; i < 4; i++)
-        e_time = (e_time << 8) | s_trail_ptr[ENDT_OFF + i];
+    /* 64-bit times are "Corey-endian" */
+    s_time = pcoreytohll(&s_trail_ptr[vwr->STARTT_OFF]);
+    e_time = pcoreytohll(&s_trail_ptr[vwr->ENDT_OFF]);
 
     /* find the packet duration (difference between start and end times) */
     d_time = (guint32)((e_time - s_time) / NS_IN_US);  /* find diff, converting to usec */
@@ -1364,19 +1414,19 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
     /* extract the 32 LSBs of the signature timestamp field */
     mac_snap = (f_qos ? 34 : 32);                       /* 24 (MAC) + 2 (QoS) + 8 (SNAP) */
 
-    if (frame_type & IS_TCP)                            /* signature offset for TCP frame */
+    if (frame_type & vwr->IS_TCP)                            /* signature offset for TCP frame */
     {
         pay_off = mac_snap + 40;
     }
-    else if (frame_type & IS_UDP)                       /* signature offset for UDP frame */
+    else if (frame_type & vwr->IS_UDP)                       /* signature offset for UDP frame */
     {
         pay_off = mac_snap + 28;
     }
-    else if (frame_type & IS_ICMP)                      /* signature offset for ICMP frame */
+    else if (frame_type & vwr->IS_ICMP)                      /* signature offset for ICMP frame */
     {
         pay_off = mac_snap + 24;
     }
-    else if (frame_type & IS_IGMP)                      /* signature offset for IGMPv2 frame */
+    else if (frame_type & vwr->IS_IGMP)                      /* signature offset for IGMPv2 frame */
     {
         pay_off = mac_snap + 28;
     }
@@ -1385,15 +1435,13 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
         pay_off = mac_snap + 20;
     }
 
-    sig_off = find_signature(m_ptr, pay_off, flow_id, flow_seq);
+    sig_off = find_signature(m_ptr, pay_off, rec_size, flow_id, flow_seq);
     if ((m_ptr[sig_off] == 0xdd) && (sig_off + 15 <= msdu_length) && (f_flow != 0))
         sig_ts = get_signature_ts(m_ptr, sig_off);
     else
         sig_ts = 0;
 
     /* Set latency based on rx/tx and signature timestamp */
-
-        /* Set latency based on rx/tx and signature timestamp */
     if (!IS_TX) {
         if (tsid < s_time) {
             latency = s_time - tsid;
@@ -1414,31 +1462,31 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
     /* block), and should always represent the actual number of bytes in the file */
     /* len is the length of the original packet before truncation */
     /* the FCS is NOT included */
-    r_hdr_len = sizeof(common_fields) + sizeof(er_fields);
-    wth->phdr.len = (actual_octets - 4) + r_hdr_len;
-    wth->phdr.caplen = (msdu_length - 4) + r_hdr_len;
+    r_hdr_len = STATS_COMMON_FIELDS_LEN + EXT_RTAP_FIELDS_LEN;
+    phdr->len = (actual_octets - 4) + r_hdr_len;
+    phdr->caplen = (msdu_length - 4) + r_hdr_len;
 
-    wth->phdr.presence_flags = WTAP_HAS_TS;
+    phdr->presence_flags = WTAP_HAS_TS;
 
-    wth->phdr.ts.secs = (time_t)s_sec;
-    wth->phdr.ts.nsecs = (long)(s_usec * 1000);
-    wth->phdr.pkt_encap = WTAP_ENCAP_IXVERIWAVE;
+    phdr->ts.secs = (time_t)s_sec;
+    phdr->ts.nsecs = (int)(s_usec * 1000);
+    phdr->pkt_encap = WTAP_ENCAP_IXVERIWAVE;
 
-    /* generate and write out the radiotap header, set the version number to 1 (extended) */
+    /* generate and copy out the radiotap header, set the port type to 0 (WLAN) */
     common_fields.vw_port_type = 0;
-    common_fields.it_len = sizeof(stats_common_fields);
-    er_fields.it_len = sizeof(er_fields);
+    common_fields.it_len = STATS_COMMON_FIELDS_LEN;
+    er_fields.it_len = EXT_RTAP_FIELDS_LEN;
 
     /* create the extended radiotap header fields */
     er_fields.flags = radioflags;
-    if (info & (guint16)vVW510021_W_IS_WEP)
+    if (info & vVW510021_W_IS_WEP)
         er_fields.flags |= RADIOTAP_F_WEP;
-    if ((l1p_1 & (guint8)PREAMBLE_MASK) != vVW510021_W_IS_LONGPREAMBLE)
+    if ((l1p_1 & vVW510021_W_PREAMBLE_MASK) != vVW510021_W_IS_LONGPREAMBLE)
         er_fields.flags |= RADIOTAP_F_SHORTPRE;
 
     er_fields.rate = rate;
-    er_fields.chanflags = chanflags; 
-    
+    er_fields.chanflags = chanflags;
+
     if (f_tx) {
         er_fields.tx_power = (gint8)tx_power;
         er_fields.signal = 100;
@@ -1452,17 +1500,17 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
     er_fields.vw_flags = 0;
     if (f_tx)
         er_fields.vw_flags |= RADIOTAP_VWF_TXF;
-    if (errors & FCS_ERROR)
+    if (errors & vwr->FCS_ERROR)
         er_fields.vw_flags |= RADIOTAP_VWF_FCSERR;
-    if (!f_tx && (errors & CRYPTO_ERR))
-            er_fields.vw_flags |= RADIOTAP_VWF_DCRERR;
-    if (!f_tx && (errors & RETRY_ERR))
-            er_fields.vw_flags |= RADIOTAP_VWF_RETRERR;
-    if (info & WEPTYPE)
+    if (!f_tx && (errors & vwr->CRYPTO_ERR))
+        er_fields.vw_flags |= RADIOTAP_VWF_DCRERR;
+    if (!f_tx && (errors & vwr->RETRY_ERR))
+        er_fields.vw_flags |= RADIOTAP_VWF_RETRERR;
+    if (info & vwr->WEPTYPE)
         er_fields.vw_flags |= RADIOTAP_VWF_IS_WEP;
-    else if (info & TKIPTYPE)
+    else if (info & vwr->TKIPTYPE)
         er_fields.vw_flags |= RADIOTAP_VWF_IS_TKIP;
-    else if (info & CCMPTYPE)
+    else if (info & vwr->CCMPTYPE)
         er_fields.vw_flags |= RADIOTAP_VWF_IS_CCMP;
 
     er_fields.vw_errors = (guint32)errors;
@@ -1487,14 +1535,63 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
     common_fields.vw_endt = end_time;
     common_fields.vw_sig_ts = (guint32)(sig_ts);/* 32 LSBs of signature  */
 
-    memcpy(&data_ptr[bytes_written], &common_fields, sizeof(common_fields));
-    bytes_written += sizeof(common_fields);
-
-    memcpy(&data_ptr[bytes_written], &er_fields, sizeof(er_fields));
-    bytes_written += sizeof(er_fields);
-
-    /* finally, dump the whole MAC frame to file as-is; exclude 4-byte FCS */
-    if ( rec_size < ((int)actual_octets + (int)STATS_LEN) ) 
+    /* put common_fields into the packet buffer in little-endian byte order */
+    phtoles(&data_ptr[bytes_written], common_fields.vw_port_type);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_fields.it_len);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_fields.vw_msdu_length);
+    bytes_written += 2;
+    /* padding */
+    memset(&data_ptr[bytes_written], 0, 2);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], common_fields.vw_flowid);
+    bytes_written += 4;
+    phtoles(&data_ptr[bytes_written], common_fields.vw_vcid);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_fields.vw_seqnum);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], common_fields.vw_latency);
+    bytes_written += 4;
+    phtolel(&data_ptr[bytes_written], common_fields.vw_sig_ts);
+    bytes_written += 4;
+    phtolell(&data_ptr[bytes_written], common_fields.vw_startt);
+    bytes_written += 8;
+    phtolell(&data_ptr[bytes_written], common_fields.vw_endt);
+    bytes_written += 8;
+    phtolel(&data_ptr[bytes_written], common_fields.vw_pktdur);
+    bytes_written += 4;
+    /* padding */
+    memset(&data_ptr[bytes_written], 0, 4);
+    bytes_written += 4;
+
+    /* put er_fields into the packet buffer in little-endian byte order */
+    phtoles(&data_ptr[bytes_written], er_fields.it_len);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], er_fields.flags);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], er_fields.chanflags);
+    bytes_written += 2;
+    data_ptr[bytes_written] = er_fields.rate;
+    bytes_written += 1;
+    data_ptr[bytes_written] = er_fields.signal;
+    bytes_written += 1;
+    data_ptr[bytes_written] = er_fields.tx_power;
+    bytes_written += 1;
+    /* padding */
+    data_ptr[bytes_written] = 0;
+    bytes_written += 1;
+    phtoles(&data_ptr[bytes_written], er_fields.vw_flags);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], er_fields.vw_ht_length);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], er_fields.vw_info);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], er_fields.vw_errors);
+    bytes_written += 4;
+
+    /* finally, copy the whole MAC frame to the packet buffer as-is; exclude 4-byte FCS */
+    if ( rec_size < ((int)actual_octets + (int)vwr->STATS_LEN) )
         /*something's been truncated, DUMP AS-IS*/
         memcpy(&data_ptr[bytes_written], m_ptr, msdu_length);
     else if (msdu_length >= 4)
@@ -1508,12 +1605,14 @@ static void vwr_read_rec_data_vVW510021(wtap *wth, guint8 *data_ptr, guint8 *rec
 /* the packet is constructed as a 38-byte VeriWave-extended Radiotap header plus the raw */
 /* MAC octets */
 
-static void vwr_read_rec_data_ethernet(wtap *wth, guint8 *data_ptr, guint8 *rec, int rec_size)
+static void vwr_read_rec_data_ethernet(wtap *wth, struct wtap_pkthdr *phdr,
+    guint8 *data_ptr, guint8 *rec, int rec_size, int IS_TX)
 {
+    vwr_t           *vwr = (vwr_t *)wth->priv;
     int             bytes_written = 0;              /* bytes output to buf so far */
     register int    i;                              /* temps */
     register guint8 *s_ptr, *m_ptr;                 /* stats and MPDU pointers */
-    gint16          msdu_length,actual_octets;      /* octets in frame */
+    guint16         msdu_length,actual_octets;      /* octets in frame */
     guint8          flow_seq;                       /* seqnum */
     guint64         s_time = LL_ZERO, e_time = LL_ZERO; /* start/end */
                                                         /* times, nsec */
@@ -1533,68 +1632,58 @@ static void vwr_read_rec_data_ethernet(wtap *wth, guint8 *data_ptr, guint8 *rec,
     guint64         sig_ts, tsid;                   /* 32 LSBs of timestamp in signature */
     guint64         delta_b;    /* Used for calculating latency */
 
-    flow_id = 0x00000000;                           /* initialize flow ID  to 0 */
-
     /* calculate the start of the statistics block in the buffer */
     /* also get a bunch of fields from the stats block */
     m_ptr = &(rec[0]);                              /* point to the data block */
-    s_ptr = &(rec[rec_size - STATS_LEN]);           /* point to the stats block */
-    
-    msdu_length = (s_ptr[OCTET_OFF] << 8) + s_ptr[OCTET_OFF + 1];
+    s_ptr = &(rec[rec_size - vwr->STATS_LEN]);      /* point to the stats block */
+
+    msdu_length = pntohs(&s_ptr[vwr->OCTET_OFF]);
     actual_octets = msdu_length;
     /* sanity check the msdu_length field to determine if it is OK (or segfaults result) */
     /* if it's greater, then truncate to the indicated message length */
-    if (msdu_length > (rec_size - (int)STATS_LEN)) {
-        msdu_length = (rec_size - (int)STATS_LEN);
+    if (msdu_length > (rec_size - (int)vwr->STATS_LEN)) {
+        msdu_length = (rec_size - (int)vwr->STATS_LEN);
     }
 
-    vc_id = (((s_ptr[VCID_OFF] << 8) | (s_ptr[VCID_OFF + 1]))) & VCID_MASK;
-    flow_seq = s_ptr[FLOWSEQ_OFF];
-    frame_type = (s_ptr[FRAME_TYPE_OFF] << 24) | (s_ptr[FRAME_TYPE_OFF + 1] << 16) |
-             (s_ptr[FRAME_TYPE_OFF + 2] << 8) | (s_ptr[FRAME_TYPE_OFF + 3]);
+    vc_id = pntohs(&s_ptr[vwr->VCID_OFF]) & vwr->VCID_MASK;
+    flow_seq = s_ptr[vwr->FLOWSEQ_OFF];
+    frame_type = pntohl(&s_ptr[vwr->FRAME_TYPE_OFF]);
 
-    if (FPGA_VERSION == vVW510024_E_FPGA) {
-        validityBits = (s_ptr[VALID_OFF] << 8) + s_ptr[VALID_OFF + 1];
-        f_flow = validityBits & FLOW_VALID;
+    if (vwr->FPGA_VERSION == vVW510024_E_FPGA) {
+        validityBits = pntohs(&s_ptr[vwr->VALID_OFF]);
+        f_flow = validityBits & vwr->FLOW_VALID;
 
-        mac_len = (validityBits & IS_VLAN) ? 16 : 14;           /* MAC hdr length based on VLAN tag */
+        mac_len = (validityBits & vwr->IS_VLAN) ? 16 : 14;           /* MAC hdr length based on VLAN tag */
 
 
-        errors = (s_ptr[ERRORS_OFF] << 8) + s_ptr[ERRORS_OFF + 1];
+        errors = pntohs(&s_ptr[vwr->ERRORS_OFF]);
     }
     else {
-        validityBits = 0;
-        f_flow = s_ptr[VALID_OFF] & FLOW_VALID;
-        mac_len = (frame_type & IS_VLAN) ? 16 : 14;             /* MAC hdr length based on VLAN tag */
+        f_flow = s_ptr[vwr->VALID_OFF] & vwr->FLOW_VALID;
+        mac_len = (frame_type & vwr->IS_VLAN) ? 16 : 14;             /* MAC hdr length based on VLAN tag */
 
 
         /*for older fpga errors is only represented by 16 bits)*/
-        errors = (s_ptr[ERRORS_OFF] << 8) + s_ptr[ERRORS_OFF + 1];
+        errors = pntohs(&s_ptr[vwr->ERRORS_OFF]);
     }
 
-    info = (s_ptr[INFO_OFF] << 8) + s_ptr[INFO_OFF + 1];
+    info = pntohs(&s_ptr[vwr->INFO_OFF]);
     /*  24 LSBs */
-    flow_id = (s_ptr[FLOWID_OFF] << 16) | (s_ptr[FLOWID_OFF + 1] << 8) |
-                s_ptr[FLOWID_OFF + 2];
+    flow_id = pntoh24(&s_ptr[vwr->FLOWID_OFF]);
 
     /* for tx latency is duration, for rx latency is timestamp */
     /* get 64-bit latency value */
-    tsid = (s_ptr[LATVAL_OFF + 6] << 8) | (s_ptr[LATVAL_OFF + 7]);
+    tsid = (s_ptr[vwr->LATVAL_OFF + 6] << 8) | (s_ptr[vwr->LATVAL_OFF + 7]);
     for (i = 0; i < 4; i++)
-        tsid = (tsid << 8) | s_ptr[LATVAL_OFF + i];
+        tsid = (tsid << 8) | s_ptr[vwr->LATVAL_OFF + i];
 
 
-    l4id = (s_ptr[L4ID_OFF] << 8) + s_ptr[L4ID_OFF + 1];
+    l4id = pntohs(&s_ptr[vwr->L4ID_OFF]);
 
     /* calculate start & end times (in sec/usec), converting 64-bit times to usec */
-    for (i = 0; i < 4; i++)                             /* 64-bit times are "Corey-endian" */
-        s_time = (s_time << 8) | s_ptr[STARTT_OFF + i + 4];
-    for (i = 0; i < 4; i++)
-        s_time = (s_time << 8) | s_ptr[STARTT_OFF + i];
-    for (i = 0; i < 4; i++)
-        e_time = (e_time << 8) | s_ptr[ENDT_OFF + i + 4];
-    for (i = 0; i < 4; i++)
-        e_time = (e_time << 8) | s_ptr[ENDT_OFF + i];
+    /* 64-bit times are "Corey-endian" */
+    s_time = pcoreytohll(&s_ptr[vwr->STARTT_OFF]);
+    e_time = pcoreytohll(&s_ptr[vwr->ENDT_OFF]);
 
     /* find the packet duration (difference between start and end times) */
     d_time = (guint32)((e_time - s_time));  /* find diff, leaving in nsec for Ethernet */
@@ -1607,19 +1696,19 @@ static void vwr_read_rec_data_ethernet(wtap *wth, guint8 *data_ptr, guint8 *rec,
     /* also convert the packet end time to seconds and microseconds */
     end_time = e_time / NS_IN_US;                       /* convert to microseconds first */
 
-    if (frame_type & IS_TCP)                            /* signature offset for TCP frame */
+    if (frame_type & vwr->IS_TCP)                       /* signature offset for TCP frame */
     {
         pay_off = mac_len + 40;
     }
-    else if (frame_type & IS_UDP)                       /* signature offset for UDP frame */
+    else if (frame_type & vwr->IS_UDP)                  /* signature offset for UDP frame */
     {
         pay_off = mac_len + 28;
     }
-    else if (frame_type & IS_ICMP)                      /* signature offset for ICMP frame */
+    else if (frame_type & vwr->IS_ICMP)                 /* signature offset for ICMP frame */
     {
         pay_off = mac_len + 24;
     }
-    else if (frame_type & IS_IGMP)                      /* signature offset for IGMPv2 frame */
+    else if (frame_type & vwr->IS_IGMP)                 /* signature offset for IGMPv2 frame */
     {
         pay_off = mac_len + 28;
     }
@@ -1628,7 +1717,7 @@ static void vwr_read_rec_data_ethernet(wtap *wth, guint8 *data_ptr, guint8 *rec,
         pay_off = mac_len + 20;
     }
 
-    sig_off = find_signature(m_ptr, pay_off, flow_id, flow_seq);
+    sig_off = find_signature(m_ptr, pay_off, rec_size, flow_id, flow_seq);
     if ((m_ptr[sig_off] == 0xdd) && (sig_off + 15 <= msdu_length) && (f_flow != 0))
         sig_ts = get_signature_ts(m_ptr, sig_off);
     else
@@ -1654,20 +1743,20 @@ static void vwr_read_rec_data_ethernet(wtap *wth, guint8 *data_ptr, guint8 *rec,
     /* block), and should always represent the actual number of bytes in the file */
     /* len is the length of the original packet before truncation*/
     /* the FCS is NEVER included */
-    e_hdr_len = sizeof(common_hdr) + sizeof(etap_hdr);
-    wth->phdr.len = (actual_octets - 4) + e_hdr_len;
-    wth->phdr.caplen = (msdu_length - 4) + e_hdr_len;
+    e_hdr_len = STATS_COMMON_FIELDS_LEN + STATS_ETHERNETTAP_FIELDS_LEN;
+    phdr->len = (actual_octets - 4) + e_hdr_len;
+    phdr->caplen = (msdu_length - 4) + e_hdr_len;
 
-    wth->phdr.presence_flags = WTAP_HAS_TS;
+    phdr->presence_flags = WTAP_HAS_TS;
 
-    wth->phdr.ts.secs = (time_t)s_sec;
-    wth->phdr.ts.nsecs = (long)(s_usec * 1000);
-    wth->phdr.pkt_encap = WTAP_ENCAP_IXVERIWAVE;
+    phdr->ts.secs = (time_t)s_sec;
+    phdr->ts.nsecs = (int)(s_usec * 1000);
+    phdr->pkt_encap = WTAP_ENCAP_IXVERIWAVE;
 
-    /* generate and write out the ETHERNETTAP header, set the version number to 1 */
+    /* generate and copy out the ETHERNETTAP header, set the port type to 1 (Ethernet) */
     common_hdr.vw_port_type = 1;
-    common_hdr.it_len = sizeof(common_hdr);
-    etap_hdr.it_len = sizeof(etap_hdr);
+    common_hdr.it_len = STATS_COMMON_FIELDS_LEN;
+    etap_hdr.it_len = STATS_ETHERNETTAP_FIELDS_LEN;
 
     etap_hdr.vw_errors = (guint32)errors;
     etap_hdr.vw_info = (guint16)info;
@@ -1687,7 +1776,7 @@ static void vwr_read_rec_data_ethernet(wtap *wth, guint8 *data_ptr, guint8 *rec,
     etap_hdr.vw_flags = 0;
     if (IS_TX)
         etap_hdr.vw_flags |= RADIOTAP_VWF_TXF;
-    if (errors & FCS_ERROR)
+    if (errors & vwr->FCS_ERROR)
         etap_hdr.vw_flags |= RADIOTAP_VWF_FCSERR;
     common_hdr.vw_startt = start_time;                  /* record start & end times of frame */
     common_hdr.vw_endt = end_time;
@@ -1695,13 +1784,56 @@ static void vwr_read_rec_data_ethernet(wtap *wth, guint8 *data_ptr, guint8 *rec,
 
     etap_hdr.it_pad2 = 0;
 
-    memcpy(&data_ptr[bytes_written], &common_hdr, sizeof(common_hdr));
-    bytes_written += sizeof(common_hdr);
-    memcpy(&data_ptr[bytes_written], &etap_hdr, sizeof(etap_hdr));
-    bytes_written += sizeof(etap_hdr);
-
-    /* finally, dump the whole MAC frame to file as-is; ALWAYS exclude 4-byte FCS */
-    if ( rec_size < ((int)actual_octets + (int)STATS_LEN) ) 
+    /* put common_hdr into the packet buffer in little-endian byte order */
+    phtoles(&data_ptr[bytes_written], common_hdr.vw_port_type);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_hdr.it_len);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_hdr.vw_msdu_length);
+    bytes_written += 2;
+    /* padding */
+    memset(&data_ptr[bytes_written], 0, 2);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], common_hdr.vw_flowid);
+    bytes_written += 4;
+    phtoles(&data_ptr[bytes_written], common_hdr.vw_vcid);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], common_hdr.vw_seqnum);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], common_hdr.vw_latency);
+    bytes_written += 4;
+    phtolel(&data_ptr[bytes_written], common_hdr.vw_sig_ts);
+    bytes_written += 4;
+    phtolell(&data_ptr[bytes_written], common_hdr.vw_startt);
+    bytes_written += 8;
+    phtolell(&data_ptr[bytes_written], common_hdr.vw_endt);
+    bytes_written += 8;
+    phtolel(&data_ptr[bytes_written], common_hdr.vw_pktdur);
+    bytes_written += 4;
+    /* padding */
+    memset(&data_ptr[bytes_written], 0, 4);
+    bytes_written += 4;
+
+    /* put etap_hdr into the packet buffer in little-endian byte order */
+    phtoles(&data_ptr[bytes_written], etap_hdr.it_len);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], etap_hdr.vw_flags);
+    bytes_written += 2;
+    phtoles(&data_ptr[bytes_written], etap_hdr.vw_info);
+    bytes_written += 2;
+    /* padding */
+    memset(&data_ptr[bytes_written], 0, 2);
+    bytes_written += 2;
+    phtolel(&data_ptr[bytes_written], etap_hdr.vw_errors);
+    bytes_written += 4;
+    phtolel(&data_ptr[bytes_written], etap_hdr.vw_l4id);
+    bytes_written += 4;
+    /* padding */
+    memset(&data_ptr[bytes_written], 0, 4);
+    bytes_written += 4;
+
+    /* finally, copy the whole MAC frame to the packet bufffer as-is; ALWAYS exclude 4-byte FCS */
+    if ( rec_size < ((int)actual_octets + (int)vwr->STATS_LEN) )
         /*something's been truncated, DUMP AS-IS*/
         memcpy(&data_ptr[bytes_written], m_ptr, msdu_length);
     else if (msdu_length >= 4)
@@ -1713,7 +1845,7 @@ static void vwr_read_rec_data_ethernet(wtap *wth, guint8 *data_ptr, guint8 *rec,
 /*--------------------------------------------------------------------------------------*/
 /* utility to split up and decode a 16-byte message record */
 
-static int decode_msg(guint8 *rec, int *v_type)
+static int decode_msg(vwr_t *vwr, guint8 *rec, int *v_type, int *IS_TX)
 {
     guint8  cmd;                        /* components of message */
     guint32 wd2, wd3;
@@ -1722,16 +1854,16 @@ static int decode_msg(guint8 *rec, int *v_type)
 
     /* break up the message record into its pieces */
     cmd = rec[0];
-    wd2 = ((guint32)rec[8] << 24) | ((guint32)rec[9] << 16) | ((guint32)rec[10] << 8) |
-        (guint32)rec[11];
-    wd3 = ((guint32)rec[12] << 24) | ((guint32)rec[13] << 16) | ((guint32)rec[14] << 8) |
-        (guint32)rec[15];
-
-    if ((cmd & HEADER_IS_TX) == HEADER_IS_TX)
-        IS_TX = 1;
-    else if ((cmd & HEADER_IS_RX) == HEADER_IS_RX)
-        IS_TX = 0;
-    else IS_TX = 2; /*NULL case*/
+    wd2 = pntohl(&rec[8]);
+    wd3 = pntohl(&rec[12]);
+
+    if (vwr != NULL) {
+        if ((cmd & vwr->HEADER_IS_TX) == vwr->HEADER_IS_TX)
+            *IS_TX = 1;
+        else if ((cmd & vwr->HEADER_IS_RX) == vwr->HEADER_IS_RX)
+            *IS_TX = 0;
+        else *IS_TX = 2; /*NULL case*/
+    }
     /* now decode based on the command byte */
     switch (cmd) {
         case 0x21:
@@ -1796,223 +1928,222 @@ static guint8 get_cck_rate(guint8 *plcp)
 /*--------------------------------------------------------------------------------------*/
 /* utility to set up offsets and bitmasks for decoding the stats blocks */
 
-static void setup_defaults( guint16 fpga)
+static void setup_defaults(vwr_t *vwr, guint16 fpga)
 {
     switch (fpga) {
         /* WLAN frames */
         case vVW510021_W_FPGA:
-            STATS_LEN = vVW510021_W_STATS_LEN;
-
-            VALID_OFF = vVW510021_W_VALID_OFF;
-            MTYPE_OFF = vVW510021_W_MTYPE_OFF;
-            VCID_OFF = vVW510021_W_VCID_OFF;
-            FLOWSEQ_OFF = vVW510021_W_FLOWSEQ_OFF;
-            FLOWID_OFF = vVW510021_W_FLOWID_OFF;
-
-            /*OCTET_OFF = v22_W_OCTET_OFF;*/
-
-            ERRORS_OFF = vVW510021_W_ERRORS_OFF;
-            PATN_OFF = vVW510021_W_MATCH_OFF;
-            RSSI_OFF = vVW510021_W_RSSI_TXPOWER_OFF;
-            STARTT_OFF = vVW510021_W_STARTT_OFF;
-            ENDT_OFF = vVW510021_W_ENDT_OFF;
-            LATVAL_OFF = vVW510021_W_LATVAL_OFF;
-            INFO_OFF = vVW510021_W_INFO_OFF;
-            FPGA_VERSION_OFF = vVW510021_W_FPGA_VERSION_OFF;
-            HEADER_VERSION_OFF = vVW510021_W_HEADER_VERSION_OFF;
-            OCTET_OFF = vVW510021_W_MSDU_LENGTH_OFF;
-            L1P_1_OFF = vVW510021_W_L1P_1_OFF;
-            L1P_2_OFF = vVW510021_W_L1P_2_OFF;
-            L4ID_OFF = vVW510021_W_L4ID_OFF;
-            IPLEN_OFF = vVW510021_W_IPLEN_OFF;
-            PLCP_LENGTH_OFF = vVW510021_W_PLCP_LENGTH_OFF;
-
-            HEADER_IS_RX = vVW510021_W_HEADER_IS_RX;
-            HEADER_IS_TX = vVW510021_W_HEADER_IS_TX;
-            MT_MASK = vVW510021_W_SEL_MASK;
-            MCS_INDEX_MASK = vVW510021_W_MCS_MASK;
-            PREAMBLE_MASK = vVW510021_W_PREAMBLE_MASK;
-            VCID_MASK = 0xffff;
-            FLOW_VALID = vVW510021_W_FLOW_VALID;
-            STATS_START_OFF = vVW510021_W_HEADER_LEN;
-            FCS_ERROR = vVW510021_W_FCS_ERROR;
-            CRYPTO_ERR = v22_W_CRYPTO_ERR;
-            RETRY_ERR = v22_W_RETRY_ERR;
-
-            /*STATS_START_OFF = 0;*/
-
-            RXTX_OFF = vVW510021_W_RXTX_OFF;
-
-            MT_10_HALF = 0;
-            MT_10_FULL = 0;
-            MT_100_HALF = 0;
-            MT_100_FULL = 0;
-            MT_1G_HALF = 0;
-            MT_1G_FULL = 0;
-            MT_CCKL = v22_W_MT_CCKL;
-            MT_CCKS = v22_W_MT_CCKS;
-            /*MT_OFDM = vVW510021_W_MT_OFDM;*/
-
-            WEPTYPE = vVW510021_W_WEPTYPE;
-            TKIPTYPE = vVW510021_W_TKIPTYPE;
-            CCMPTYPE = vVW510021_W_CCMPTYPE;
-
-            FRAME_TYPE_OFF  =   vVW510021_W_FRAME_TYPE_OFF;
-            IS_TCP      =   vVW510021_W_IS_TCP;
-            IS_UDP      =   vVW510021_W_IS_UDP;
-            IS_ICMP     =   vVW510021_W_IS_ICMP;
-            IS_IGMP     =   vVW510021_W_IS_IGMP;
-            IS_QOS      =   vVW510021_W_QOS_VALID;
+            vwr->STATS_LEN = vVW510021_W_STATS_LEN;
+
+            vwr->VALID_OFF = vVW510021_W_VALID_OFF;
+            vwr->MTYPE_OFF = vVW510021_W_MTYPE_OFF;
+            vwr->VCID_OFF = vVW510021_W_VCID_OFF;
+            vwr->FLOWSEQ_OFF = vVW510021_W_FLOWSEQ_OFF;
+            vwr->FLOWID_OFF = vVW510021_W_FLOWID_OFF;
+
+            /*vwr->OCTET_OFF = v22_W_OCTET_OFF;*/
+
+            vwr->ERRORS_OFF = vVW510021_W_ERRORS_OFF;
+            vwr->PATN_OFF = vVW510021_W_MATCH_OFF;
+            vwr->RSSI_OFF = vVW510021_W_RSSI_TXPOWER_OFF;
+            vwr->STARTT_OFF = vVW510021_W_STARTT_OFF;
+            vwr->ENDT_OFF = vVW510021_W_ENDT_OFF;
+            vwr->LATVAL_OFF = vVW510021_W_LATVAL_OFF;
+            vwr->INFO_OFF = vVW510021_W_INFO_OFF;
+            vwr->FPGA_VERSION_OFF = vVW510021_W_FPGA_VERSION_OFF;
+            vwr->HEADER_VERSION_OFF = vVW510021_W_HEADER_VERSION_OFF;
+            vwr->OCTET_OFF = vVW510021_W_MSDU_LENGTH_OFF;
+            vwr->L1P_1_OFF = vVW510021_W_L1P_1_OFF;
+            vwr->L1P_2_OFF = vVW510021_W_L1P_2_OFF;
+            vwr->L4ID_OFF = vVW510021_W_L4ID_OFF;
+            vwr->IPLEN_OFF = vVW510021_W_IPLEN_OFF;
+            vwr->PLCP_LENGTH_OFF = vVW510021_W_PLCP_LENGTH_OFF;
+
+            vwr->HEADER_IS_RX = vVW510021_W_HEADER_IS_RX;
+            vwr->HEADER_IS_TX = vVW510021_W_HEADER_IS_TX;
+            vwr->MT_MASK = vVW510021_W_SEL_MASK;
+            vwr->MCS_INDEX_MASK = vVW510021_W_MCS_MASK;
+            vwr->VCID_MASK = 0xffff;
+            vwr->FLOW_VALID = vVW510021_W_FLOW_VALID;
+            vwr->STATS_START_OFF = vVW510021_W_HEADER_LEN;
+            vwr->FCS_ERROR = vVW510021_W_FCS_ERROR;
+            vwr->CRYPTO_ERR = v22_W_CRYPTO_ERR;
+            vwr->RETRY_ERR = v22_W_RETRY_ERR;
+
+            /*vwr->STATS_START_OFF = 0;*/
+
+            vwr->RXTX_OFF = vVW510021_W_RXTX_OFF;
+
+            vwr->MT_10_HALF = 0;
+            vwr->MT_10_FULL = 0;
+            vwr->MT_100_HALF = 0;
+            vwr->MT_100_FULL = 0;
+            vwr->MT_1G_HALF = 0;
+            vwr->MT_1G_FULL = 0;
+            vwr->MT_CCKL = v22_W_MT_CCKL;
+            vwr->MT_CCKS = v22_W_MT_CCKS;
+            /*vwr->MT_OFDM = vVW510021_W_MT_OFDM;*/
+
+            vwr->WEPTYPE = vVW510021_W_WEPTYPE;
+            vwr->TKIPTYPE = vVW510021_W_TKIPTYPE;
+            vwr->CCMPTYPE = vVW510021_W_CCMPTYPE;
+
+            vwr->FRAME_TYPE_OFF  =   vVW510021_W_FRAME_TYPE_OFF;
+            vwr->IS_TCP      =   vVW510021_W_IS_TCP;
+            vwr->IS_UDP      =   vVW510021_W_IS_UDP;
+            vwr->IS_ICMP     =   vVW510021_W_IS_ICMP;
+            vwr->IS_IGMP     =   vVW510021_W_IS_IGMP;
+            vwr->IS_QOS      =   vVW510021_W_QOS_VALID;
 
             break;
 
             /* Ethernet frames */
         case vVW510012_E_FPGA:
-            STATS_LEN = v22_E_STATS_LEN;
-
-            VALID_OFF = v22_E_VALID_OFF;
-            MTYPE_OFF = v22_E_MTYPE_OFF;
-            VCID_OFF = v22_E_VCID_OFF;
-            FLOWSEQ_OFF = v22_E_FLOWSEQ_OFF;
-            FLOWID_OFF = v22_E_FLOWID_OFF;
-            OCTET_OFF = v22_E_OCTET_OFF;
-            ERRORS_OFF = v22_E_ERRORS_OFF;
-            PATN_OFF = v22_E_PATN_OFF;
-            RSSI_OFF = v22_E_RSSI_OFF;
-            STARTT_OFF = v22_E_STARTT_OFF;
-            ENDT_OFF = v22_E_ENDT_OFF;
-            LATVAL_OFF = v22_E_LATVAL_OFF;
-            INFO_OFF = v22_E_INFO_OFF;
-            L4ID_OFF = v22_E_L4ID_OFF;
-
-            HEADER_IS_RX = v22_E_HEADER_IS_RX;
-            HEADER_IS_TX = v22_E_HEADER_IS_TX;
-
-            IS_RX = v22_E_IS_RX;
-            MT_MASK = v22_E_MT_MASK;
-            VCID_MASK = v22_E_VCID_MASK;
-            FLOW_VALID = v22_E_FLOW_VALID;
-            FCS_ERROR = v22_E_FCS_ERROR;
-
-            RX_DECRYPTS = v22_E_RX_DECRYPTS;
-            TX_DECRYPTS = v22_E_TX_DECRYPTS;
-            FC_PROT_BIT = v22_E_FC_PROT_BIT;
-
-            MT_10_HALF = v22_E_MT_10_HALF;
-            MT_10_FULL = v22_E_MT_10_FULL;
-            MT_100_HALF = v22_E_MT_100_HALF;
-            MT_100_FULL = v22_E_MT_100_FULL;
-            MT_1G_HALF = v22_E_MT_1G_HALF;
-            MT_1G_FULL = v22_E_MT_1G_FULL;
-            MT_CCKL = 0;
-            MT_CCKS = 0;
-            MT_OFDM = 0;
-
-            FRAME_TYPE_OFF =  v22_E_FRAME_TYPE_OFF;
-            IS_TCP      =   v22_E_IS_TCP;
-            IS_UDP      =   v22_E_IS_UDP;
-            IS_ICMP     =   v22_E_IS_ICMP;
-            IS_IGMP     =   v22_E_IS_IGMP;
-            IS_QOS      =   v22_E_IS_QOS;
-            IS_VLAN     =   v22_E_IS_VLAN;
+            vwr->STATS_LEN = v22_E_STATS_LEN;
+
+            vwr->VALID_OFF = v22_E_VALID_OFF;
+            vwr->MTYPE_OFF = v22_E_MTYPE_OFF;
+            vwr->VCID_OFF = v22_E_VCID_OFF;
+            vwr->FLOWSEQ_OFF = v22_E_FLOWSEQ_OFF;
+            vwr->FLOWID_OFF = v22_E_FLOWID_OFF;
+            vwr->OCTET_OFF = v22_E_OCTET_OFF;
+            vwr->ERRORS_OFF = v22_E_ERRORS_OFF;
+            vwr->PATN_OFF = v22_E_PATN_OFF;
+            vwr->RSSI_OFF = v22_E_RSSI_OFF;
+            vwr->STARTT_OFF = v22_E_STARTT_OFF;
+            vwr->ENDT_OFF = v22_E_ENDT_OFF;
+            vwr->LATVAL_OFF = v22_E_LATVAL_OFF;
+            vwr->INFO_OFF = v22_E_INFO_OFF;
+            vwr->L4ID_OFF = v22_E_L4ID_OFF;
+
+            vwr->HEADER_IS_RX = v22_E_HEADER_IS_RX;
+            vwr->HEADER_IS_TX = v22_E_HEADER_IS_TX;
+
+            vwr->IS_RX = v22_E_IS_RX;
+            vwr->MT_MASK = v22_E_MT_MASK;
+            vwr->VCID_MASK = v22_E_VCID_MASK;
+            vwr->FLOW_VALID = v22_E_FLOW_VALID;
+            vwr->FCS_ERROR = v22_E_FCS_ERROR;
+
+            vwr->RX_DECRYPTS = v22_E_RX_DECRYPTS;
+            vwr->TX_DECRYPTS = v22_E_TX_DECRYPTS;
+            vwr->FC_PROT_BIT = v22_E_FC_PROT_BIT;
+
+            vwr->MT_10_HALF = v22_E_MT_10_HALF;
+            vwr->MT_10_FULL = v22_E_MT_10_FULL;
+            vwr->MT_100_HALF = v22_E_MT_100_HALF;
+            vwr->MT_100_FULL = v22_E_MT_100_FULL;
+            vwr->MT_1G_HALF = v22_E_MT_1G_HALF;
+            vwr->MT_1G_FULL = v22_E_MT_1G_FULL;
+            vwr->MT_CCKL = 0;
+            vwr->MT_CCKS = 0;
+            vwr->MT_OFDM = 0;
+
+            vwr->FRAME_TYPE_OFF =  v22_E_FRAME_TYPE_OFF;
+            vwr->IS_TCP      =   v22_E_IS_TCP;
+            vwr->IS_UDP      =   v22_E_IS_UDP;
+            vwr->IS_ICMP     =   v22_E_IS_ICMP;
+            vwr->IS_IGMP     =   v22_E_IS_IGMP;
+            vwr->IS_QOS      =   v22_E_IS_QOS;
+            vwr->IS_VLAN     =   v22_E_IS_VLAN;
 
             break;
 
             /* WLAN frames */
         case vVW510006_W_FPGA:
-            STATS_LEN = v22_W_STATS_LEN;
-
-            MTYPE_OFF = v22_W_MTYPE_OFF;
-            VALID_OFF = v22_W_VALID_OFF;
-            VCID_OFF = v22_W_VCID_OFF;
-            FLOWSEQ_OFF = v22_W_FLOWSEQ_OFF;
-            FLOWID_OFF = v22_W_FLOWID_OFF;
-            OCTET_OFF = v22_W_OCTET_OFF;
-            ERRORS_OFF = v22_W_ERRORS_OFF;
-            PATN_OFF = v22_W_PATN_OFF;
-            RSSI_OFF = v22_W_RSSI_OFF;
-            STARTT_OFF = v22_W_STARTT_OFF;
-            ENDT_OFF = v22_W_ENDT_OFF;
-            LATVAL_OFF = v22_W_LATVAL_OFF;
-            INFO_OFF = v22_W_INFO_OFF;
-            L4ID_OFF = v22_W_L4ID_OFF;
-            IPLEN_OFF = v22_W_IPLEN_OFF;
-            PLCP_LENGTH_OFF = v22_W_PLCP_LENGTH_OFF;
-
-            FCS_ERROR = v22_W_FCS_ERROR;
-            CRYPTO_ERR = v22_W_CRYPTO_ERR;
-            PAYCHK_ERR = v22_W_PAYCHK_ERR;
-            RETRY_ERR = v22_W_RETRY_ERR;
-            IS_RX = v22_W_IS_RX;
-            MT_MASK = v22_W_MT_MASK;
-            VCID_MASK = v22_W_VCID_MASK;
-            FLOW_VALID = v22_W_FLOW_VALID;
-
-            HEADER_IS_RX = v22_W_HEADER_IS_RX;
-            HEADER_IS_TX = v22_W_HEADER_IS_TX;
-
-            RX_DECRYPTS = v22_W_RX_DECRYPTS;
-            TX_DECRYPTS = v22_W_TX_DECRYPTS;
-            FC_PROT_BIT = v22_W_FC_PROT_BIT;
-
-            MT_10_HALF = 0;
-            MT_10_FULL = 0;
-            MT_100_HALF = 0;
-            MT_100_FULL = 0;
-            MT_1G_HALF = 0;
-            MT_1G_FULL = 0;
-            MT_CCKL = v22_W_MT_CCKL;
-            MT_CCKS = v22_W_MT_CCKS;
-            MT_OFDM = v22_W_MT_OFDM;
-
-            WEPTYPE = v22_W_WEPTYPE;
-            TKIPTYPE = v22_W_TKIPTYPE;
-            CCMPTYPE = v22_W_CCMPTYPE;
-
-            FRAME_TYPE_OFF =   v22_W_FRAME_TYPE_OFF;
-            IS_TCP      =   v22_W_IS_TCP;
-            IS_UDP      =   v22_W_IS_UDP;
-            IS_ICMP     =   v22_W_IS_ICMP;
-            IS_IGMP     =   v22_W_IS_IGMP;
-            IS_QOS      =   v22_W_IS_QOS;
+            vwr->STATS_LEN = v22_W_STATS_LEN;
+
+            vwr->MTYPE_OFF = v22_W_MTYPE_OFF;
+            vwr->VALID_OFF = v22_W_VALID_OFF;
+            vwr->VCID_OFF = v22_W_VCID_OFF;
+            vwr->FLOWSEQ_OFF = v22_W_FLOWSEQ_OFF;
+            vwr->FLOWID_OFF = v22_W_FLOWID_OFF;
+            vwr->OCTET_OFF = v22_W_OCTET_OFF;
+            vwr->ERRORS_OFF = v22_W_ERRORS_OFF;
+            vwr->PATN_OFF = v22_W_PATN_OFF;
+            vwr->RSSI_OFF = v22_W_RSSI_OFF;
+            vwr->STARTT_OFF = v22_W_STARTT_OFF;
+            vwr->ENDT_OFF = v22_W_ENDT_OFF;
+            vwr->LATVAL_OFF = v22_W_LATVAL_OFF;
+            vwr->INFO_OFF = v22_W_INFO_OFF;
+            vwr->L4ID_OFF = v22_W_L4ID_OFF;
+            vwr->IPLEN_OFF = v22_W_IPLEN_OFF;
+            vwr->PLCP_LENGTH_OFF = v22_W_PLCP_LENGTH_OFF;
+
+            vwr->FCS_ERROR = v22_W_FCS_ERROR;
+            vwr->CRYPTO_ERR = v22_W_CRYPTO_ERR;
+            vwr->PAYCHK_ERR = v22_W_PAYCHK_ERR;
+            vwr->RETRY_ERR = v22_W_RETRY_ERR;
+            vwr->IS_RX = v22_W_IS_RX;
+            vwr->MT_MASK = v22_W_MT_MASK;
+            vwr->VCID_MASK = v22_W_VCID_MASK;
+            vwr->FLOW_VALID = v22_W_FLOW_VALID;
+
+            vwr->HEADER_IS_RX = v22_W_HEADER_IS_RX;
+            vwr->HEADER_IS_TX = v22_W_HEADER_IS_TX;
+
+            vwr->RX_DECRYPTS = v22_W_RX_DECRYPTS;
+            vwr->TX_DECRYPTS = v22_W_TX_DECRYPTS;
+            vwr->FC_PROT_BIT = v22_W_FC_PROT_BIT;
+
+            vwr->MT_10_HALF = 0;
+            vwr->MT_10_FULL = 0;
+            vwr->MT_100_HALF = 0;
+            vwr->MT_100_FULL = 0;
+            vwr->MT_1G_HALF = 0;
+            vwr->MT_1G_FULL = 0;
+            vwr->MT_CCKL = v22_W_MT_CCKL;
+            vwr->MT_CCKS = v22_W_MT_CCKS;
+            vwr->MT_OFDM = v22_W_MT_OFDM;
+
+            vwr->WEPTYPE = v22_W_WEPTYPE;
+            vwr->TKIPTYPE = v22_W_TKIPTYPE;
+            vwr->CCMPTYPE = v22_W_CCMPTYPE;
+
+            vwr->FRAME_TYPE_OFF =   v22_W_FRAME_TYPE_OFF;
+            vwr->IS_TCP      =   v22_W_IS_TCP;
+            vwr->IS_UDP      =   v22_W_IS_UDP;
+            vwr->IS_ICMP     =   v22_W_IS_ICMP;
+            vwr->IS_IGMP     =   v22_W_IS_IGMP;
+            vwr->IS_QOS      =   v22_W_IS_QOS;
 
             break;
 
         /* Ethernet frames */
         case vVW510024_E_FPGA:
-            STATS_LEN = vVW510024_E_STATS_LEN;
-
-            VALID_OFF = vVW510024_E_VALID_OFF;
-            VCID_OFF = vVW510024_E_VCID_OFF;
-            FLOWSEQ_OFF = vVW510024_E_FLOWSEQ_OFF;
-            FLOWID_OFF = vVW510024_E_FLOWID_OFF;
-            OCTET_OFF = vVW510024_E_MSDU_LENGTH_OFF;
-            ERRORS_OFF = vVW510024_E_ERRORS_OFF;
-            PATN_OFF = vVW510024_E_MATCH_OFF;
-            STARTT_OFF = vVW510024_E_STARTT_OFF;
-            ENDT_OFF = vVW510024_E_ENDT_OFF;
-            LATVAL_OFF = vVW510024_E_LATVAL_OFF;
-            INFO_OFF = vVW510024_E_INFO_OFF;
-            L4ID_OFF = vVW510024_E_L4ID_OFF;
-            IPLEN_OFF = vVW510024_E_IPLEN_OFF;
-
-            FPGA_VERSION_OFF = vVW510024_E_FPGA_VERSION_OFF;
-            HEADER_VERSION_OFF = vVW510024_E_HEADER_VERSION_OFF;
-
-            HEADER_IS_RX = vVW510024_E_HEADER_IS_RX;
-            HEADER_IS_TX = vVW510024_E_HEADER_IS_TX;
-
-            VCID_MASK = vVW510024_E_VCID_MASK;
-            FLOW_VALID = vVW510024_E_FLOW_VALID;
-            FCS_ERROR = v22_E_FCS_ERROR;
-
-            FRAME_TYPE_OFF  =   vVW510024_E_FRAME_TYPE_OFF;
-            IS_TCP      =   vVW510024_E_IS_TCP;
-            IS_UDP      =   vVW510024_E_IS_UDP;
-            IS_ICMP     =   vVW510024_E_IS_ICMP;
-            IS_IGMP     =   vVW510024_E_IS_IGMP;
-            IS_QOS      =   vVW510024_E_QOS_VALID;
-            IS_VLAN     =   vVW510024_E_IS_VLAN;
+            vwr->STATS_LEN = vVW510024_E_STATS_LEN;
+
+            vwr->VALID_OFF = vVW510024_E_VALID_OFF;
+            vwr->VCID_OFF = vVW510024_E_VCID_OFF;
+            vwr->FLOWSEQ_OFF = vVW510024_E_FLOWSEQ_OFF;
+            vwr->FLOWID_OFF = vVW510024_E_FLOWID_OFF;
+            vwr->OCTET_OFF = vVW510024_E_MSDU_LENGTH_OFF;
+            vwr->ERRORS_OFF = vVW510024_E_ERRORS_OFF;
+            vwr->PATN_OFF = vVW510024_E_MATCH_OFF;
+            vwr->STARTT_OFF = vVW510024_E_STARTT_OFF;
+            vwr->ENDT_OFF = vVW510024_E_ENDT_OFF;
+            vwr->LATVAL_OFF = vVW510024_E_LATVAL_OFF;
+            vwr->INFO_OFF = vVW510024_E_INFO_OFF;
+            vwr->L4ID_OFF = vVW510024_E_L4ID_OFF;
+            vwr->IPLEN_OFF = vVW510024_E_IPLEN_OFF;
+
+            vwr->FPGA_VERSION_OFF = vVW510024_E_FPGA_VERSION_OFF;
+            vwr->HEADER_VERSION_OFF = vVW510024_E_HEADER_VERSION_OFF;
+
+            vwr->HEADER_IS_RX = vVW510024_E_HEADER_IS_RX;
+            vwr->HEADER_IS_TX = vVW510024_E_HEADER_IS_TX;
+
+            vwr->VCID_MASK = vVW510024_E_VCID_MASK;
+            vwr->FLOW_VALID = vVW510024_E_FLOW_VALID;
+            vwr->FCS_ERROR = v22_E_FCS_ERROR;
+
+            vwr->FRAME_TYPE_OFF  =   vVW510024_E_FRAME_TYPE_OFF;
+            vwr->IS_TCP      =   vVW510024_E_IS_TCP;
+            vwr->IS_UDP      =   vVW510024_E_IS_UDP;
+            vwr->IS_ICMP     =   vVW510024_E_IS_ICMP;
+            vwr->IS_IGMP     =   vVW510024_E_IS_IGMP;
+            vwr->IS_QOS      =   vVW510024_E_QOS_VALID;
+            vwr->IS_VLAN     =   vVW510024_E_IS_VLAN;
 
             break;
     }
@@ -2021,7 +2152,7 @@ static void setup_defaults( guint16 fpga)
 
 /* utility routine: check that signature is at specified location; scan for it if not */
 /* if we can't find a signature at all, then simply return the originally supplied offset */
-int find_signature(guint8 *m_ptr, int pay_off, guint32 flow_id, guint8 flow_seq)
+int find_signature(guint8 *m_ptr, int pay_off, gint rec_size, guint32 flow_id, guint8 flow_seq)
 {
     int     tgt;                /* temps */
     guint32 fid;
@@ -2034,14 +2165,13 @@ int find_signature(guint8 *m_ptr, int pay_off, guint32 flow_id, guint8 flow_seq)
     /* payload until maximum scan range exhausted to see if we can find it */
     /* the scanning process consists of looking for a '0xdd', then checking for the correct */
     /* flow ID and sequence number at the appropriate offsets */
-    for (tgt = pay_off; tgt < (pay_off + SIG_SCAN_RANGE); tgt++) {
+    for (tgt = pay_off; tgt < (pay_off + SIG_SCAN_RANGE) && tgt < rec_size; tgt++) {
         if (m_ptr[tgt] == 0xdd) {                       /* found magic byte? check fields */
             if (m_ptr[tgt + 15] == 0xe2) {
                 if (m_ptr[tgt + 4] != flow_seq)
                     continue;
 
-                fid = m_ptr[tgt + 1] | (m_ptr[tgt + 2] << 8) |
-                      (m_ptr[tgt + 3] << 16);
+                fid = pletoh24(&m_ptr[tgt + 1]);
 
                 if (fid != flow_id)
                     continue;
@@ -2053,8 +2183,7 @@ int find_signature(guint8 *m_ptr, int pay_off, guint32 flow_id, guint8 flow_seq)
                 if (m_ptr[tgt + SIG_FSQ_OFF] != flow_seq)   /* check sequence number */
                     continue;                               /* if failed, keep scanning */
 
-                fid = m_ptr[tgt + SIG_FID_OFF] | (m_ptr[tgt + SIG_FID_OFF + 1] << 8) |
-                      (m_ptr[tgt + SIG_FID_OFF + 2] << 16); /* assemble flow ID from signature */
+                fid = pletoh24(&m_ptr[tgt + SIG_FID_OFF]);  /* assemble flow ID from signature */
                 if (fid != flow_id)                         /* check flow ID against expected */
                     continue;                               /* if failed, keep scanning */
 
@@ -2079,8 +2208,7 @@ guint64 get_signature_ts(guint8 *m_ptr,int sig_off)
     else
         ts_offset = 8;
 
-    sig_ts = (m_ptr[sig_off + ts_offset + 3] << 24) | (m_ptr[sig_off + ts_offset + 2] << 16) |
-                 (m_ptr[sig_off + ts_offset + 1] << 8) | (m_ptr[sig_off + ts_offset + 0]);  
+    sig_ts = pletohl(&m_ptr[sig_off + ts_offset]);
 
     return(sig_ts & 0xffffffff);
 }