Update URL
[metze/wireshark/wip.git] / wiretap / visual.c
index 5dc45b6021381aa5dbd68c39120a0b3d2e1b2f1c..2bf675a4448065aaacecc899c7f46c29b203ac5a 100644 (file)
@@ -2,8 +2,6 @@
  * File read and write routines for Visual Networks cap files.
  * Copyright (c) 2001, Tom Nisbet  tnisbet@visualnetworks.com
  *
- * $Id$
- *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
  *
@@ -27,7 +25,6 @@
 #include <string.h>
 #include "wtap-int.h"
 #include "file_wrappers.h"
-#include "buffer.h"
 #include "visual.h"
 
 /*
  * The first packet index is (4 * num_pkts) octets from the end of the file
  * and the last index is in the last four octets of the file.
  *
- * All integer and time values are stored in little-endian format, except for 
+ * All integer and time values are stored in little-endian format, except for
  *  the ATM Packet Header, which is stored in network byte order.
  *
- * [ File Header ] 
- * 
+ * [ File Header ]
+ *
  *
  * [ Packet Header 1 ] [(opt) ATM Packet Header] [ Data ]
  * ...
@@ -106,7 +103,7 @@ struct visual_pkt_hdr
 /* This structure is used to extract information */
 struct visual_atm_hdr
 {
-   guint16 vpi;           /* 4 bits of zeros; 12 bits of ATM VPI */ 
+   guint16 vpi;           /* 4 bits of zeros; 12 bits of ATM VPI */
    guint16 vci;           /* ATM VCI */
    guint8  info;          /* 4 bits version; 3 bits unused-zero; 1 bit direction */
    guint8  category;      /* indicates type of traffic. 4 bits of status + 4 bits of type */
@@ -144,14 +141,14 @@ struct visual_read_info
 {
     guint32 num_pkts;           /* Number of pkts in the file */
     guint32 current_pkt;        /* Next packet to be read */
-    double  start_time;         /* Capture start time in microseconds */
+    time_t  start_time;         /* Capture start time in seconds */
 };
 
 
 /* Additional information for writing Visual files */
 struct visual_write_info
 {
-    guint start_time;        /* Capture start time in seconds */
+    time_t  start_time;         /* Capture start time in seconds */
     int     index_table_index;  /* Index of the next index entry */
     int     index_table_size;   /* Allocated size of the index table */
     guint32 * index_table;      /* File offsets for the packets */
@@ -163,61 +160,48 @@ struct visual_write_info
 static gboolean visual_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean visual_seek_read(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, Buffer *buf, int packet_size,
-    int *err, gchar **err_info);
-static gboolean visual_process_packet_header(wtap *wth, FILE_T fh,
-    struct wtap_pkthdr *phdr, struct visual_pkt_hdr *vpkt_hdrp,
-    int *err, gchar **err_info);
-static void visual_fill_in_chdlc_encapsulation(struct wtap_pkthdr *phdr,
-            guint8 encap_hint, Buffer *buf);
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
+static gboolean visual_read_packet(wtap *wth, FILE_T fh,
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
 static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const guint8 *pd, int *err);
-static gboolean visual_dump_close(wtap_dumper *wdh, int *err);
+    const guint8 *pd, int *err, gchar **err_info);
+static gboolean visual_dump_finish(wtap_dumper *wdh, int *err);
 static void visual_dump_free(wtap_dumper *wdh);
 
 
 /* Open a file for reading */
-int visual_open(wtap *wth, int *err, gchar **err_info)
+wtap_open_return_val visual_open(wtap *wth, int *err, gchar **err_info)
 {
-    int bytes_read;
     char magic[sizeof visual_magic];
     struct visual_file_hdr vfile_hdr;
     struct visual_read_info * visual;
     int encap;
 
     /* Check the magic string at the start of the file */
-    errno = WTAP_ERR_CANT_READ;
-    bytes_read = file_read(magic, sizeof magic, wth->fh);
-    if (bytes_read != sizeof magic)
+    if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info))
     {
-        *err = file_error(wth->fh, err_info);
-        if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-            return -1;
-        return 0;
+        if (*err != WTAP_ERR_SHORT_READ)
+            return WTAP_OPEN_ERROR;
+        return WTAP_OPEN_NOT_MINE;
     }
     if (memcmp(magic, visual_magic, sizeof visual_magic) != 0)
     {
-        return 0;
+        return WTAP_OPEN_NOT_MINE;
     }
 
     /* Read the rest of the file header. */
-    errno = WTAP_ERR_CANT_READ;
-    bytes_read = file_read(&vfile_hdr, sizeof vfile_hdr, wth->fh);
-    if (bytes_read != sizeof vfile_hdr)
+    if (!wtap_read_bytes(wth->fh, &vfile_hdr, sizeof vfile_hdr, err, err_info))
     {
-        *err = file_error(wth->fh, err_info);
-        if (*err == 0)
-            *err = WTAP_ERR_SHORT_READ;
-        return -1;
+        return WTAP_OPEN_ERROR;
     }
 
     /* Verify the file version is known */
-    vfile_hdr.file_version = pletohs(&vfile_hdr.file_version);
+    vfile_hdr.file_version = pletoh16(&vfile_hdr.file_version);
     if (vfile_hdr.file_version != 1)
     {
         *err = WTAP_ERR_UNSUPPORTED;
         *err_info = g_strdup_printf("visual: file version %u unsupported", vfile_hdr.file_version);
-        return -1;
+        return WTAP_OPEN_ERROR;
     }
 
     /* Translate the encapsulation type; these values are SNMP ifType
@@ -228,58 +212,58 @@ int visual_open(wtap *wth, int *err, gchar **err_info)
        the first packet is read.
 
        XXX - should we use WTAP_ENCAP_PER_PACKET for that? */
-    switch (pletohs(&vfile_hdr.media_type))
+    switch (pletoh16(&vfile_hdr.media_type))
     {
-    case  6:   /* ethernet-csmacd */
+    case  6:    /* ethernet-csmacd */
         encap = WTAP_ENCAP_ETHERNET;
         break;
 
-    case  9:   /* IEEE802.5 */
+    case  9:    /* IEEE802.5 */
         encap = WTAP_ENCAP_TOKEN_RING;
         break;
 
-    case 16:   /* lapb */
+    case 16:    /* lapb */
         encap = WTAP_ENCAP_LAPB;
         break;
 
-    case 22:   /* propPointToPointSerial */
-    case 118:  /* HDLC */
+    case 22:    /* propPointToPointSerial */
+    case 118:   /* HDLC */
         encap = WTAP_ENCAP_CHDLC_WITH_PHDR;
         break;
 
-    case 32:   /* frame-relay */
+    case 32:    /* frame-relay */
         encap = WTAP_ENCAP_FRELAY_WITH_PHDR;
         break;
 
-    case 37:   /* ATM */
+    case 37:    /* ATM */
        encap = WTAP_ENCAP_ATM_PDUS;
        break;
 
     default:
-        *err = WTAP_ERR_UNSUPPORTED_ENCAP;
+        *err = WTAP_ERR_UNSUPPORTED;
         *err_info = g_strdup_printf("visual: network type %u unknown or unsupported",
                                      vfile_hdr.media_type);
-        return -1;
+        return WTAP_OPEN_ERROR;
     }
 
     /* Fill in the wiretap struct with data from the file header */
-    wth->file_type = WTAP_FILE_VISUAL_NETWORKS;
+    wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_VISUAL_NETWORKS;
     wth->file_encap = encap;
-    wth->snapshot_length = pletohs(&vfile_hdr.max_length);
+    wth->snapshot_length = pletoh16(&vfile_hdr.max_length);
 
     /* Set up the pointers to the handlers for this file type */
     wth->subtype_read = visual_read;
     wth->subtype_seek_read = visual_seek_read;
-    wth->tsprecision = WTAP_FILE_TSPREC_USEC;
+    wth->file_tsprec = WTAP_TSPREC_MSEC;
 
     /* Add Visual-specific information to the wiretap struct for later use. */
     visual = (struct visual_read_info *)g_malloc(sizeof(struct visual_read_info));
     wth->priv = (void *)visual;
-    visual->num_pkts = pletohl(&vfile_hdr.num_pkts);
-    visual->start_time = ((double) pletohl(&vfile_hdr.start_time)) * 1000000;
+    visual->num_pkts = pletoh32(&vfile_hdr.num_pkts);
+    visual->start_time = pletoh32(&vfile_hdr.start_time);
     visual->current_pkt = 1;
 
-    return 1;
+    return WTAP_OPEN_MINE;
 }
 
 
@@ -291,7 +275,6 @@ static gboolean visual_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset)
 {
     struct visual_read_info *visual = (struct visual_read_info *)wth->priv;
-    struct visual_pkt_hdr vpkt_hdr;
 
     /* Check for the end of the packet data.  Note that a check for file EOF
        will not work because there are index values stored after the last
@@ -305,92 +288,59 @@ static gboolean visual_read(wtap *wth, int *err, gchar **err_info,
 
     *data_offset = file_tell(wth->fh);
 
-    if (!visual_process_packet_header(wth, wth->fh, &wth->phdr, &vpkt_hdr,
-            err, err_info))
-        return FALSE;
-
-    if (!wtap_read_packet_bytes(wth->fh, wth->frame_buffer, wth->phdr.caplen,
-            err, err_info))
-        return FALSE;
-
-    if (wth->file_encap == WTAP_ENCAP_CHDLC_WITH_PHDR)
-    {
-        visual_fill_in_chdlc_encapsulation(&wth->phdr, vpkt_hdr.encap_hint,
-                    wth->frame_buffer);
-    }
-    return TRUE;
+    return visual_read_packet(wth, wth->fh, &wth->phdr, wth->frame_buffer,
+            err, err_info);
 }
 
 /* Read packet header and data for random access. */
 static gboolean visual_seek_read(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, Buffer *buf, int len,
-    int *err, gchar **err_info)
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
 {
-    struct visual_pkt_hdr vpkt_hdr;
-
     /* Seek to the packet header */
     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
         return FALSE;
 
-    /* Read and process the packet header. */
-    if (!visual_process_packet_header(wth, wth->random_fh, phdr, &vpkt_hdr,
-            err, err_info))
-        return FALSE;
-
-    /* Read the packet data. */
-    if (!wtap_read_packet_bytes(wth->random_fh, buf, len, err, err_info))
+    /* Read the packet. */
+    if (!visual_read_packet(wth, wth->random_fh, phdr, buf, err, err_info)) {
+        if (*err == 0)
+            *err = WTAP_ERR_SHORT_READ;
         return FALSE;
-
-    if (wth->file_encap == WTAP_ENCAP_CHDLC_WITH_PHDR)
-    {
-        visual_fill_in_chdlc_encapsulation(phdr, vpkt_hdr.encap_hint, buf);
     }
-
     return TRUE;
 }
 
 static gboolean
-visual_process_packet_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
-        struct visual_pkt_hdr *vpkt_hdrp, int *err, gchar **err_info)
+visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
+        Buffer *buf, int *err, gchar **err_info)
 {
     struct visual_read_info *visual = (struct visual_read_info *)wth->priv;
-    int bytes_read;
+    struct visual_pkt_hdr vpkt_hdr;
     guint32 packet_size;
     struct visual_atm_hdr vatm_hdr;
-    double  t;
-    time_t  secs;
-    guint32 usecs;
+    guint32 relmsecs;
     guint32 packet_status;
+    guint8 *pd;
 
     /* Read the packet header. */
-    errno = WTAP_ERR_CANT_READ;
-    bytes_read = file_read(vpkt_hdrp, (unsigned int)sizeof *vpkt_hdrp, fh);
-    if (bytes_read < 0 || (size_t)bytes_read != sizeof *vpkt_hdrp)
+    if (!wtap_read_bytes_or_eof(fh, &vpkt_hdr, (unsigned int)sizeof vpkt_hdr, err, err_info))
     {
-        *err = file_error(fh, err_info);
-        if (*err == 0 && bytes_read != 0)
-        {
-            *err = WTAP_ERR_SHORT_READ;
-        }
         return FALSE;
     }
 
     /* Get the included length of data. This includes extra headers + payload */
-    packet_size = pletohs(&vpkt_hdrp->incl_len);
+    packet_size = pletoh16(&vpkt_hdr.incl_len);
 
+    phdr->rec_type = REC_TYPE_PACKET;
     phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
 
     /* Set the packet time and length. */
-    t = visual->start_time;
-    t += ((double)pletohl(&vpkt_hdrp->ts_delta))*1000;
-    secs = (time_t)(t/1000000);
-    usecs = (guint32)(t - secs*1000000);
-    phdr->ts.secs = secs;
-    phdr->ts.nsecs = usecs * 1000;
-    
-    phdr->len = pletohs(&vpkt_hdrp->orig_len);
+    relmsecs = pletoh32(&vpkt_hdr.ts_delta);
+    phdr->ts.secs = visual->start_time + relmsecs/1000;
+    phdr->ts.nsecs = (relmsecs % 1000)*1000000;
 
-    packet_status = pletohl(&vpkt_hdrp->status);
+    phdr->len = pletoh16(&vpkt_hdr.orig_len);
+
+    packet_status = pletoh32(&vpkt_hdr.status);
 
     /* Do encapsulation-specific processing.
 
@@ -428,7 +378,7 @@ visual_process_packet_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
            set the FCS length to 0? */
         phdr->pseudo_header.eth.fcs_len = -1;
         break;
-    
+
     case WTAP_ENCAP_CHDLC_WITH_PHDR:
         /* This has a 2-byte FCS. */
         if (phdr->len < 2)
@@ -486,19 +436,12 @@ visual_process_packet_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
 
            ATM packets have an additional packet header; read and
            process it. */
-        errno = WTAP_ERR_CANT_READ;
-        bytes_read = file_read(&vatm_hdr, (unsigned int)sizeof vatm_hdr, fh);
-        if (bytes_read < 0 || (size_t)bytes_read != sizeof vatm_hdr)
+        if (!wtap_read_bytes(fh, &vatm_hdr, (unsigned int)sizeof vatm_hdr, err, err_info))
         {
-            *err = file_error(fh, err_info);
-            if (*err == 0)
-            {
-                *err = WTAP_ERR_SHORT_READ;
-            }
             return FALSE;
         }
-       
-        /* Remove ATM header from length of included bytes in capture, as 
+
+        /* Remove ATM header from length of included bytes in capture, as
            this header was appended by the processor doing the packet
            reassembly, and was not transmitted across the wire */
         packet_size -= (guint32)sizeof vatm_hdr;
@@ -511,10 +454,10 @@ visual_process_packet_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
         /* Next two items not supported. Defaulting to zero */
         phdr->pseudo_header.atm.aal5t_u2u = 0;
         phdr->pseudo_header.atm.aal5t_chksum = 0;
-       
+
         /* Flags appear only to convey that packet is a raw cell. Set to 0 */
-        phdr->pseudo_header.atm.flags = 0; 
-       
+        phdr->pseudo_header.atm.flags = 0;
+
         /* Not supported. Defaulting to zero */
         phdr->pseudo_header.atm.aal2_cid = 0;
 
@@ -527,17 +470,17 @@ visual_process_packet_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
         case VN_AAL2:
             phdr->pseudo_header.atm.aal = AAL_2;
             break;
-       
+
         case VN_AAL34:
             phdr->pseudo_header.atm.aal = AAL_3_4;
             break;
-       
+
         case VN_AAL5:
             phdr->pseudo_header.atm.aal = AAL_5;
             phdr->pseudo_header.atm.type = TRAF_LLCMX;
-            phdr->pseudo_header.atm.aal5t_len = pntohl(&vatm_hdr.data_length);
+            phdr->pseudo_header.atm.aal5t_len = pntoh32(&vatm_hdr.data_length);
             break;
-       
+
         case VN_OAM:
         /* Marking next 3 as OAM versus unknown */
         case VN_O191:
@@ -551,10 +494,10 @@ visual_process_packet_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
             phdr->pseudo_header.atm.aal = AAL_UNKNOWN;
             break;
         }
-        phdr->pseudo_header.atm.vpi = pntohs(&vatm_hdr.vpi) & 0x0FFF;
-        phdr->pseudo_header.atm.vci = pntohs(&vatm_hdr.vci);
-        phdr->pseudo_header.atm.cells = pntohs(&vatm_hdr.cell_count);
-       
+        phdr->pseudo_header.atm.vpi = pntoh16(&vatm_hdr.vpi) & 0x0FFF;
+        phdr->pseudo_header.atm.vci = pntoh16(&vatm_hdr.vci);
+        phdr->pseudo_header.atm.cells = pntoh16(&vatm_hdr.cell_count);
+
         /* Using bit value of 1 (DCE -> DTE) to indicate From Network */
         phdr->pseudo_header.atm.channel = vatm_hdr.info & FROM_NETWORK;
         break;
@@ -578,65 +521,60 @@ visual_process_packet_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
         return FALSE;
     }
 
-    /* Sanity check */
-    if (phdr->len < phdr->caplen)
-    {
-        phdr->len = phdr->caplen;
-    }
-
-    return TRUE;
-}
-
-static void visual_fill_in_chdlc_encapsulation(struct wtap_pkthdr *phdr,
-            guint8 encap_hint, Buffer *buf)
-{
-    guint8 *pd;
-
-    /* Fill in the encapsulation.  Visual files have a media type in the
-       file header and an encapsulation type in each packet header.  Files
-       with a media type of HDLC can be either Cisco EtherType or PPP.
-
-       The encapsulation hint values we've seen are:
-
-         2 - seen in an Ethernet capture
-         13 - seen in a PPP capture; possibly also seen in Cisco HDLC
-              captures
-         14 - seen in a PPP capture; probably seen only for PPP.
-
-       According to bug 2005, the collection probe can be configured
-       for PPP, in which case the encapsulation hint is 14, or can
-       be configured for auto-detect, in which case the encapsulation
-       hint is 13, and the encapsulation must be guessed from the
-       packet contents.  Auto-detect is the default. */
-    pd = buffer_start_ptr(buf);
+    /* Read the packet data */
+    if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
+        return FALSE;
 
-    /* If PPP is specified in the encap hint, then use that */
-    if (encap_hint == 14)
+    if (wth->file_encap == WTAP_ENCAP_CHDLC_WITH_PHDR)
     {
-        /* But first we need to examine the first three octets to
-           try to determine the proper encapsulation, see RFC 2364. */
-        if (phdr->caplen >= 3 &&
-            (0xfe == pd[0]) && (0xfe == pd[1]) && (0x03 == pd[2]))
+        /* Fill in the encapsulation.  Visual files have a media type in the
+           file header and an encapsulation type in each packet header.  Files
+           with a media type of HDLC can be either Cisco EtherType or PPP.
+
+           The encapsulation hint values we've seen are:
+
+             2 - seen in an Ethernet capture
+             13 - seen in a PPP capture; possibly also seen in Cisco HDLC
+                  captures
+             14 - seen in a PPP capture; probably seen only for PPP.
+
+           According to bug 2005, the collection probe can be configured
+           for PPP, in which case the encapsulation hint is 14, or can
+           be configured for auto-detect, in which case the encapsulation
+           hint is 13, and the encapsulation must be guessed from the
+           packet contents.  Auto-detect is the default. */
+        pd = ws_buffer_start_ptr(buf);
+
+        /* If PPP is specified in the encap hint, then use that */
+        if (vpkt_hdr.encap_hint == 14)
         {
-            /* It is actually LLC encapsulated PPP */
-            phdr->pkt_encap = WTAP_ENCAP_ATM_RFC1483;
+            /* But first we need to examine the first three octets to
+               try to determine the proper encapsulation, see RFC 2364. */
+            if (packet_size >= 3 &&
+                (0xfe == pd[0]) && (0xfe == pd[1]) && (0x03 == pd[2]))
+            {
+                /* It is actually LLC encapsulated PPP */
+                phdr->pkt_encap = WTAP_ENCAP_ATM_RFC1483;
+            }
+            else
+            {
+                /* It is actually PPP */
+                phdr->pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
+            }
         }
         else
         {
-            /* It is actually PPP */
-            phdr->pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
-        }
-    }
-    else
-    {
-        /* Otherwise, we need to examine the first two octets to
-           try to determine the encapsulation. */
-        if (phdr->caplen >= 2 && (0xff == pd[0]) && (0x03 == pd[1]))
-        {
-            /* It is actually PPP */
-            phdr->pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
+            /* Otherwise, we need to examine the first two octets to
+               try to determine the encapsulation. */
+            if (packet_size >= 2 && (0xff == pd[0]) && (0x03 == pd[1]))
+            {
+                /* It is actually PPP */
+                phdr->pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
+            }
         }
     }
+
+    return TRUE;
 }
 
 /* Check for media types that may be written in Visual file format.
@@ -661,7 +599,7 @@ int visual_dump_can_write_encap(int encap)
         return 0;
     }
 
-    return WTAP_ERR_UNSUPPORTED_ENCAP;
+    return WTAP_ERR_UNWRITABLE_ENCAP;
 }
 
 
@@ -674,7 +612,7 @@ gboolean visual_dump_open(wtap_dumper *wdh, int *err)
 
     /* Set the write routines for a visual file. */
     wdh->subtype_write = visual_dump;
-    wdh->subtype_close = visual_dump_close;
+    wdh->subtype_finish = visual_dump_finish;
 
     /* Create a struct to hold file information for the duration
        of the write */
@@ -688,8 +626,8 @@ gboolean visual_dump_open(wtap_dumper *wdh, int *err)
     /* All of the fields in the file header aren't known yet so
        just skip over it for now.  It will be created after all
        of the packets have been written. */
-    if (wtap_dump_file_seek(wdh, CAPTUREFILE_HEADER_SIZE, SEEK_SET, err) == -1) 
-       return FALSE;
+    if (wtap_dump_file_seek(wdh, CAPTUREFILE_HEADER_SIZE, SEEK_SET, err) == -1)
+        return FALSE;
 
     return TRUE;
 }
@@ -698,7 +636,7 @@ gboolean visual_dump_open(wtap_dumper *wdh, int *err)
 /* Write a packet to a Visual dump file.
    Returns TRUE on success, FALSE on failure. */
 static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const guint8 *pd, int *err)
+    const guint8 *pd, int *err, gchar **err_info _U_)
 {
     const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
     struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
@@ -707,6 +645,18 @@ static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
     guint delta_msec;
     guint32 packet_status;
 
+    /* We can only write packet records. */
+    if (phdr->rec_type != REC_TYPE_PACKET) {
+        *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
+        return FALSE;
+    }
+
+    /* Don't write anything we're not willing to read. */
+    if (phdr->caplen > WTAP_MAX_PACKET_SIZE) {
+        *err = WTAP_ERR_PACKET_TOO_LARGE;
+        return FALSE;
+    }
+
     /* If the visual structure was never allocated then nothing useful
        can be done. */
     if (visual == 0)
@@ -722,7 +672,7 @@ static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
     if (visual->index_table_index == 0)
     {
         /* This is the first packet.  Save its start time as the file time. */
-        visual->start_time = (guint32) phdr->ts.secs;
+        visual->start_time = phdr->ts.secs;
 
         /* Initialize the index table */
         visual->index_table = (guint32 *)g_malloc(1024 * sizeof *visual->index_table);
@@ -731,12 +681,12 @@ static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
 
     /* Calculate milliseconds since capture start. */
     delta_msec = phdr->ts.nsecs / 1000000;
-    delta_msec += ( (guint32) phdr->ts.secs - visual->start_time) * 1000;
-    vpkt_hdr.ts_delta = htolel(delta_msec);
+    delta_msec += (guint32)((phdr->ts.secs - visual->start_time) * 1000);
+    vpkt_hdr.ts_delta = GUINT32_TO_LE(delta_msec);
 
     /* Fill in the length fields. */
-    vpkt_hdr.orig_len = htoles(phdr->len);
-    vpkt_hdr.incl_len = htoles(phdr->caplen);
+    vpkt_hdr.orig_len = GUINT16_TO_LE(phdr->len);
+    vpkt_hdr.incl_len = GUINT16_TO_LE(phdr->caplen);
 
     /* Fill in the encapsulation hint for the file's media type. */
     switch (wdh->encap)
@@ -780,7 +730,7 @@ static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
             ((pseudo_header->x25.flags & FROM_DCE) ? 0x00 : PS_SENT);
         break;
     }
-    vpkt_hdr.status = htolel(packet_status);
+    vpkt_hdr.status = GUINT32_TO_LE(packet_status);
 
     /* Write the packet header. */
     if (!wtap_dump_file_write(wdh, &vpkt_hdr, hdr_size, err))
@@ -798,7 +748,7 @@ static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
         visual->index_table = (guint32 *)g_realloc(visual->index_table,
             visual->index_table_size * sizeof *visual->index_table);
     }
-    visual->index_table[visual->index_table_index] = htolel(visual->next_offset);
+    visual->index_table[visual->index_table_index] = GUINT32_TO_LE(visual->next_offset);
 
     /* Update the table index and offset for the next frame. */
     visual->index_table_index++;
@@ -810,7 +760,7 @@ static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
 
 /* Finish writing to a dump file.
    Returns TRUE on success, FALSE on failure. */
-static gboolean visual_dump_close(wtap_dumper *wdh, int *err)
+static gboolean visual_dump_finish(wtap_dumper *wdh, int *err)
 {
     struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
     size_t n_to_write;
@@ -837,7 +787,7 @@ static gboolean visual_dump_close(wtap_dumper *wdh, int *err)
 
     /* Write the magic number at the start of the file. */
     if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
-       return FALSE;
+        return FALSE;
     magicp = visual_magic;
     magic_size = sizeof visual_magic;
     if (!wtap_dump_file_write(wdh, magicp, magic_size, err))
@@ -848,36 +798,36 @@ static gboolean visual_dump_close(wtap_dumper *wdh, int *err)
 
     /* Initialize the file header with zeroes for the reserved fields. */
     memset(&vfile_hdr, '\0', sizeof vfile_hdr);
-    vfile_hdr.num_pkts = htolel(visual->index_table_index);
-    vfile_hdr.start_time = htolel(visual->start_time);
-    vfile_hdr.max_length = htoles(65535);
-    vfile_hdr.file_flags = htoles(1);  /* indexes are present */
-    vfile_hdr.file_version = htoles(1);
+    vfile_hdr.num_pkts = GUINT32_TO_LE(visual->index_table_index);
+    vfile_hdr.start_time = GUINT32_TO_LE(visual->start_time);
+    vfile_hdr.max_length = GUINT16_TO_LE(65535);
+    vfile_hdr.file_flags = GUINT16_TO_LE(1);  /* indexes are present */
+    vfile_hdr.file_version = GUINT16_TO_LE(1);
     g_strlcpy(vfile_hdr.description, "Wireshark file", 64);
 
     /* Translate the encapsulation type */
     switch (wdh->encap)
     {
     case WTAP_ENCAP_ETHERNET:
-        vfile_hdr.media_type = htoles(6);
+        vfile_hdr.media_type = GUINT16_TO_LE(6);
         break;
 
     case WTAP_ENCAP_TOKEN_RING:
-        vfile_hdr.media_type = htoles(9);
+        vfile_hdr.media_type = GUINT16_TO_LE(9);
         break;
 
     case WTAP_ENCAP_LAPB:
-        vfile_hdr.media_type = htoles(16);
+        vfile_hdr.media_type = GUINT16_TO_LE(16);
         break;
 
     case WTAP_ENCAP_PPP:        /* PPP is differentiated from CHDLC in PktHdr */
     case WTAP_ENCAP_PPP_WITH_PHDR:
     case WTAP_ENCAP_CHDLC_WITH_PHDR:
-        vfile_hdr.media_type = htoles(22);
+        vfile_hdr.media_type = GUINT16_TO_LE(22);
         break;
 
     case WTAP_ENCAP_FRELAY_WITH_PHDR:
-        vfile_hdr.media_type = htoles(32);
+        vfile_hdr.media_type = GUINT16_TO_LE(32);
         break;
     }
 
@@ -906,3 +856,16 @@ static void visual_dump_free(wtap_dumper *wdh)
             g_free(visual->index_table);
     }
 }
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 4
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
+ */