Make FT_STRINGZ items work as apply/prepare as filter menu items
[obnox/wireshark/wip.git] / wiretap / file_access.c
index 5efc3fb735e942706bd5cbfe039a7b4039901599..cf8792c74a29d7355b0be39bebd73c5bcf43ba29 100644 (file)
@@ -1,6 +1,6 @@
 /* file_access.c
  *
- * $Id: file_access.c,v 1.5 2003/10/31 00:43:21 guy Exp $
+ * $Id$
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
 #include <fcntl.h>
 #endif
 
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
 #include <errno.h>
 
-#ifdef HAVE_IO_H
-#include <io.h>        /* open/close on win32 */
-#endif
+#include "file_util.h"
 
 #include "wtap-int.h"
 #include "file_wrappers.h"
 #include "buffer.h"
 #include "lanalyzer.h"
+#include "airopeek9.h"
 #include "ngsniffer.h"
 #include "radcom.h"
 #include "ascend.h"
 #include "libpcap.h"
 #include "snoop.h"
 #include "iptrace.h"
+#include "iseries.h"
 #include "netmon.h"
 #include "netxray.h"
 #include "toshiba.h"
+#include "eyesdn.h"
 #include "i4btrace.h"
 #include "csids.h"
 #include "pppdump.h"
@@ -72,6 +69,9 @@
 #include "erf.h"
 #include "hcidump.h"
 #include "network_instruments.h"
+#include "k12.h"
+#include "ber.h"
+#include "catapult_dct2000.h"
 
 /* The open_file_* routines should return:
  *
@@ -90,7 +90,7 @@
  * should be discovered as a libpcap file, not a toshiba file.
  */
 
-static int (*const open_routines[])(wtap *, int *) = {
+static int (*const open_routines[])(wtap *, int *, char **) = {
        /* Files that have magic bytes in fixed locations. These
         * are easy to identify.
         */
@@ -106,7 +106,11 @@ static int (*const open_routines[])(wtap *, int *) = {
        visual_open,
        _5views_open,
        network_instruments_open,
-
+       airopeek9_open,
+       dbs_etherwatch_open,
+       k12_open,
+       catapult_dct2000_open,
+       ber_open,
        /* Files that don't have magic bytes at a fixed location,
         * but that instead require a heuristic of some sort to
         * identify them.  This includes the ASCII trace files that
@@ -115,12 +119,13 @@ static int (*const open_routines[])(wtap *, int *) = {
         */
        etherpeek_open,
        pppdump_open,
+       iseries_open,
        ascend_open,
+       eyesdn_open,
        toshiba_open,
        i4btrace_open,
        csids_open,
        vms_open,
-       dbs_etherwatch_open,
        cosine_open,
        erf_open,
        hcidump_open,
@@ -150,20 +155,33 @@ static int (*const open_routines[])(wtap *, int *) = {
 /* Opens a file and prepares a wtap struct.
    If "do_random" is TRUE, it opens the file twice; the second open
    allows the application to do random-access I/O without moving
-   the seek offset for sequential I/O, which is used by Ethereal
+   the seek offset for sequential I/O, which is used by Wireshark
    so that it can do sequential I/O to a capture file that's being
    written to as new packets arrive independently of random I/O done
    to display protocol trees for packets when they're selected. */
-wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random)
+wtap* wtap_open_offline(const char *filename, int *err, char **err_info,
+    gboolean do_random)
 {
        struct stat statb;
        wtap    *wth;
        unsigned int    i;
+       gboolean use_stdin = FALSE;
+
+       /* open standard input if filename is '-' */
+       if (strcmp(filename, "-") == 0)
+               use_stdin = TRUE;
 
        /* First, make sure the file is valid */
-       if (stat(filename, &statb) < 0) {
-               *err = errno;
-               return NULL;
+       if (use_stdin) {
+               if (fstat(0, &statb) < 0) {
+                       *err = errno;
+                       return NULL;
+               }
+       } else {
+               if (eth_stat(filename, &statb) < 0) {
+                       *err = errno;
+                       return NULL;
+               }
        }
        if (S_ISFIFO(statb.st_mode)) {
                /*
@@ -195,6 +213,18 @@ wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random)
                return NULL;
        }
 
+       /*
+        * We need two independent descriptors for random access, so
+        * they have different file positions.  If we're opening the
+        * standard input, we can only dup it to get additional
+        * descriptors, so we can't have two independent descriptors,
+        * and thus can't do random access.
+        */
+       if (use_stdin && do_random) {
+               *err = WTAP_ERR_RANDOM_OPEN_STDIN;
+               return NULL;
+       }
+
        errno = ENOMEM;
        wth = g_malloc(sizeof(wtap));
        if (wth == NULL) {
@@ -202,14 +232,20 @@ wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random)
                return NULL;
        }
 
-/* Win32 needs the O_BINARY flag for open() */
-#ifndef O_BINARY
-#define O_BINARY       0
-#endif
-
        /* Open the file */
        errno = WTAP_ERR_CANT_OPEN;
-       wth->fd = open(filename, O_RDONLY|O_BINARY);
+       if (use_stdin) {
+               /*
+                * We dup FD 0, so that we don't have to worry about
+                * an fclose or gzclose of wth->fh closing the standard
+                * input of the process.
+                */
+               wth->fd = eth_dup(0);
+#ifdef _WIN32
+               _setmode(wth->fd, O_BINARY);
+#endif
+       } else
+               wth->fd = eth_open(filename, O_RDONLY|O_BINARY, 0000 /* no creation so don't matter */);
        if (wth->fd < 0) {
                *err = errno;
                g_free(wth);
@@ -217,6 +253,7 @@ wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random)
        }
        if (!(wth->fh = filed_open(wth->fd, "rb"))) {
                *err = errno;
+               eth_close(wth->fd);
                g_free(wth);
                return NULL;
        }
@@ -236,6 +273,7 @@ wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random)
        wth->data_offset = 0;
        wth->subtype_sequential_close = NULL;
        wth->subtype_close = NULL;
+       wth->tsprecision = WTAP_FILE_TSPREC_USEC;
 
        /* Try all file types */
        for (i = 0; i < N_FILE_TYPES; i++) {
@@ -255,7 +293,8 @@ wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random)
                        return NULL;
                }
                wth->data_offset = 0;
-               switch ((*open_routines[i])(wth, err)) {
+               
+               switch ((*open_routines[i])(wth, err, err_info)) {
 
                case -1:
                        /* I/O error - give up */
@@ -291,162 +330,217 @@ success:
 
 /* Table of the file types we know about. */
 static const struct file_type_info {
+    /* the file type name */
+    /* should be NULL for all "pseudo" types that are only internally used and not read/writeable */
        const char *name;
+
+    /* the file type short name, used as a shortcut for the command line tools */
+    /* should be NULL for all "pseudo" types that are are only internally used and not read/writeable */
        const char *short_name;
+    
+    /* the common file extensions for this type (seperated by semicolon) */
+    /* should be *.* if no common extension is applicable */
+    const char *file_extensions;
+    
+    /* the default file extension, used to save this type */
+    /* should be NULL if no default extension is known */
+    const char *file_extension_default;
+    
+    /* can this type be compressed with gzip? */
+       gboolean        can_compress;
+    
+    /* can this type write this encapsulation format? */
+    /* should be NULL is this file type don't have write support */
        int     (*can_write_encap)(int);
+    
+    /* the function to open the capture file for writing */
+    /* should be NULL is this file type don't have write support */
        int     (*dump_open)(wtap_dumper *, gboolean, int *);
 } dump_open_table[WTAP_NUM_FILE_TYPES] = {
-       /* WTAP_FILE_UNKNOWN */
-       { NULL, NULL,
+       /* WTAP_FILE_UNKNOWN (only used internally for initialization) */
+       { NULL, NULL, NULL, NULL, FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_WTAP */
-       { "Wiretap (Ethereal)", NULL,
+       /* WTAP_FILE_WTAP (only used internally while capturing) */
+       { NULL, NULL, NULL, NULL, FALSE,
          NULL, NULL },
 
        /* WTAP_FILE_PCAP */
-       { "libpcap (tcpdump, Ethereal, etc.)", "libpcap",
+       { "Wireshark/tcpdump/... - libpcap", "libpcap", "*.pcap;*.cap", ".pcap", TRUE,
          libpcap_dump_can_write_encap, libpcap_dump_open },
 
-       /* WTAP_FILE_PCAP_SS990417 */
-       { "Red Hat Linux 6.1 libpcap (tcpdump)", "rh6_1libpcap",
+       /* WTAP_FILE_PCAP_NSEC */
+       { "Wireshark - nanosecond libpcap", "nseclibpcap", "*.pcap;*.cap", ".pcap", TRUE,
          libpcap_dump_can_write_encap, libpcap_dump_open },
 
-       /* WTAP_FILE_PCAP_SS990915 */
-       { "SuSE Linux 6.3 libpcap (tcpdump)", "suse6_3libpcap",
-         libpcap_dump_can_write_encap, libpcap_dump_open },
+       /* WTAP_FILE_PCAP_AIX */
+       { "AIX tcpdump - libpcap", "aixlibpcap", "*.pcap;*.cap", ".pcap", TRUE,
+         NULL, NULL },
 
        /* WTAP_FILE_PCAP_SS991029 */
-       { "modified libpcap (tcpdump)", "modlibpcap",
+       { "Modified tcpdump - libpcap", "modlibpcap", "*.pcap;*.cap", ".pcap", TRUE,
          libpcap_dump_can_write_encap, libpcap_dump_open },
 
        /* WTAP_FILE_PCAP_NOKIA */
-       { "Nokia libpcap (tcpdump)", "nokialibpcap",
+       { "Nokia tcpdump - libpcap ", "nokialibpcap", "*.pcap;*.cap", ".pcap", TRUE,
          libpcap_dump_can_write_encap, libpcap_dump_open },
 
-       /* WTAP_FILE_PCAP_AIX */
-       { "AIX libpcap (tcpdump)", NULL,
+       /* WTAP_FILE_PCAP_SS990417 */
+       { "RedHat 6.1 tcpdump - libpcap", "rh6_1libpcap", "*.pcap;*.cap", ".pcap", TRUE,
+         libpcap_dump_can_write_encap, libpcap_dump_open },
+
+       /* WTAP_FILE_PCAP_SS990915 */
+       { "SuSE 6.3 tcpdump - libpcap", "suse6_3libpcap", "*.pcap;*.cap", ".pcap", TRUE,
+         libpcap_dump_can_write_encap, libpcap_dump_open },
+
+       /* WTAP_FILE_5VIEWS */
+       { "Accellent 5Views capture", "5views", "*.5vw", ".5vw", FALSE,
+         _5views_dump_can_write_encap, _5views_dump_open },
+
+       /* WTAP_FILE_IPTRACE_1_0 */
+       { "AIX iptrace 1.0", "iptrace_1", "*.*", NULL, FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_LANALYZER */
-       { "Novell LANalyzer","lanalyzer",
-         lanalyzer_dump_can_write_encap, lanalyzer_dump_open },
+       /* WTAP_FILE_IPTRACE_2_0 */
+       { "AIX iptrace 2.0", "iptrace_2", "*.*", NULL, FALSE,
+         NULL, NULL },
 
-       /* WTAP_FILE_NGSNIFFER_UNCOMPRESSED */
-       { "Network Associates Sniffer (DOS-based)", "ngsniffer",
-         ngsniffer_dump_can_write_encap, ngsniffer_dump_open },
+       /* WTAP_FILE_BER */
+       { "ASN.1 Basic Encoding Rules", "ber", "*.*", NULL, FALSE,
+               NULL, NULL },
 
-       /* WTAP_FILE_NGSNIFFER_COMPRESSED */
-       { "Network Associates Sniffer (DOS-based), compressed", "ngsniffer_comp",
+       /* WTAP_FILE_HCIDUMP */
+       { "Bluetooth HCI dump", "hcidump", "*.*", NULL, FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_SNOOP */
-       { "Sun snoop", "snoop",
-         snoop_dump_can_write_encap, snoop_dump_open },
+       /* WTAP_FILE_CATAPULT_DCT2000 */
+       { "Catapult DCT2000 trace (.out format)", "dct2000", "*.out", ".out", FALSE,
+         catapult_dct2000_dump_can_write_encap, catapult_dct2000_dump_open },
 
-       /* WTAP_FILE_SHOMITI */
-       { "Shomiti/Finisar Surveyor", "shomiti",
+       /* WTAP_FILE_NETXRAY_OLD */
+       { "Cinco Networks NetXRay 1.x", "netxray1", "*.cap", ".cap", FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_IPTRACE_1_0 */
-       { "AIX iptrace 1.0", NULL,
+       /* WTAP_FILE_NETXRAY_1_0 */
+       { "Cinco Networks NetXRay 2.0 or later", "netxray2", "*.cap", ".cap", FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_IPTRACE_2_0 */
-       { "AIX iptrace 2.0", NULL,
+       /* WTAP_FILE_COSINE */
+       { "CoSine IPSX L2 capture", "cosine", "*.*", NULL, FALSE,
+         NULL, NULL },
+
+       /* WTAP_FILE_CSIDS */
+       { "CSIDS IPLog", "csids", "*.*", NULL, FALSE,
+         NULL, NULL },
+
+       /* WTAP_FILE_DBS_ETHERWATCH */
+       { "DBS Etherwatch (VMS)", "etherwatch", "*.*", NULL, FALSE,
+         NULL, NULL},
+
+       /* WTAP_FILE_ERF */
+       { "Endace DAG capture", "erf", "*.erf", ".erf", FALSE,
+         NULL, NULL },
+
+       /* WTAP_FILE_EYESDN */
+       { "EyeSDN USB S0/E1 ISDN trace format", "eyesdn", "*.*", NULL, FALSE,
+               NULL, NULL },
+    
+       /* WTAP_FILE_NETTL */
+       { "HP-UX nettl trace", "nettl", "*.TRC0;*.TRC1", ".TRC0", FALSE,
+         nettl_dump_can_write_encap, nettl_dump_open },
+
+       /* WTAP_FILE_ISERIES */
+       { "IBM iSeries comm. trace (ASCII)", "iseries_ascii", "*.*", NULL, FALSE,
+         NULL, NULL },
+
+       /* WTAP_FILE_ISERIES_UNICODE */
+       { "IBM iSeries comm. trace (UNICODE)", "iseries_unicode", "*.*", NULL, FALSE,
+         NULL, NULL },
+
+       /* WTAP_FILE_I4BTRACE */
+       { "I4B ISDN trace", "i4btrace", "*.*", NULL, FALSE,
+         NULL, NULL },
+
+       /* WTAP_FILE_ASCEND */
+       { "Lucent/Ascend access server trace", "ascend", "*.*", NULL, FALSE,
          NULL, NULL },
 
        /* WTAP_FILE_NETMON_1_x */
-       { "Microsoft Network Monitor 1.x", "netmon1",
+       { "Microsoft NetMon 1.x", "netmon1", "*.cap", ".cap", FALSE,
          netmon_dump_can_write_encap, netmon_dump_open },
 
        /* WTAP_FILE_NETMON_2_x */
-       { "Microsoft Network Monitor 2.x", "netmon2",
+       { "Microsoft NetMon 2.x", "netmon2", "*.cap", ".cap", FALSE,
          netmon_dump_can_write_encap, netmon_dump_open },
 
-       /* WTAP_FILE_NETXRAY_OLD */
-       { "Cinco Networks NetXRay 1.x", NULL,
-         NULL, NULL },
+       /* WTAP_FILE_NGSNIFFER_UNCOMPRESSED */
+       { "NA Sniffer (DOS)", "ngsniffer", "*.cap;*.enc;*.trc;*.fdc;*.syc", ".cap", FALSE,
+         ngsniffer_dump_can_write_encap, ngsniffer_dump_open },
 
-       /* WTAP_FILE_NETXRAY_1_0 */
-       { "Cinco Networks NetXRay 2.0 or later", NULL,
+       /* WTAP_FILE_NGSNIFFER_COMPRESSED */
+       { "NA Sniffer (DOS), compressed", "ngsniffer_comp", "*.caz", ".caz", FALSE,
          NULL, NULL },
 
        /* WTAP_FILE_NETXRAY_1_1 */
-       { "Network Associates Sniffer (Windows-based) 1.1", "ngwsniffer_1_1",
+       { "NA Sniffer (Windows) 1.1", "ngwsniffer_1_1", "*.cap", ".cap", FALSE,
          netxray_dump_can_write_encap_1_1, netxray_dump_open_1_1 },
 
        /* WTAP_FILE_NETXRAY_2_00x */
-       { "Network Associates Sniffer (Windows-based) 2.00x", "ngwsniffer_2_0",
+       { "NA Sniffer (Windows) 2.00x", "ngwsniffer_2_0", "*.cap", ".cap", FALSE,
          netxray_dump_can_write_encap_2_0, netxray_dump_open_2_0 },
 
-       /* WTAP_FILE_RADCOM */
-       { "RADCOM WAN/LAN analyzer", NULL,
-         NULL, NULL },
-
-       /* WTAP_FILE_ASCEND */
-       { "Lucent/Ascend access server trace", NULL,
-         NULL, NULL },
+       /* WTAP_FILE_NETWORK_INSTRUMENTS_V9 */
+       { "Network Instruments Observer (V9)", "niobserverv9", "*.bfr", ".bfr", FALSE,
+         network_instruments_dump_can_write_encap, network_instruments_dump_open },
 
-       /* WTAP_FILE_NETTL */
-       { "HP-UX nettl trace", NULL,
-         NULL, NULL },
+       /* WTAP_FILE_LANALYZER */
+       { "Novell LANalyzer","lanalyzer", "*.tr1", ".tr1", FALSE,
+         lanalyzer_dump_can_write_encap, lanalyzer_dump_open },
 
-       /* WTAP_FILE_TOSHIBA */
-       { "Toshiba Compact ISDN Router snoop trace", NULL,
+       /* WTAP_FILE_PPPDUMP */
+       { "pppd log (pppdump format)", "pppd", "*.*", NULL, FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_I4BTRACE */
-       { "I4B ISDN trace", NULL,
+       /* WTAP_FILE_RADCOM */
+       { "RADCOM WAN/LAN analyzer", "radcom", "*.*", NULL, FALSE,
          NULL, NULL },
 
-        /* WTAP_FILE_CSIDS */
-        { "CSIDS IPLog", NULL,
-          NULL, NULL },
-
-        /* WTAP_FILE_PPPDUMP */
-        { "pppd log (pppdump format)", NULL,
-          NULL, NULL },
-
-       /* WTAP_FILE_ETHERPEEK_V56 */
-       { "EtherPeek/TokenPeek trace (V5 & V6 file format)", NULL,
-         NULL, NULL },
+       /* WTAP_FILE_SNOOP */
+       { "Sun snoop", "snoop", "*.snoop;*.cap", ".snoop", FALSE,
+         snoop_dump_can_write_encap, snoop_dump_open },
 
-       /* WTAP_FILE_ETHERPEEK_V7 */
-       { "EtherPeek/TokenPeek/AiroPeek trace (V7 file format)", NULL,
+       /* WTAP_FILE_SHOMITI */
+       { "Shomiti/Finisar Surveyor", "shomiti", "*.cap", ".cap", FALSE,
          NULL, NULL },
 
        /* WTAP_FILE_VMS */
-       { "TCPIPtrace (VMS)", NULL,
+       { "TCPIPtrace (VMS)", "tcpiptrace", "*.*", NULL, FALSE,
          NULL, NULL},
 
-       /* WTAP_FILE_DBS_ETHERWATCH */
-       { "DBS Etherwatch (VMS)", NULL,
-         NULL, NULL},
+       /* WTAP_FILE_K12 */
+       { "Tektronix K12xx 32-bit .rf5 format", "rf5", "*.rf5", ".rf5", FALSE,
+               k12_dump_can_write_encap, k12_dump_open },
 
-       /* WTAP_FILE_VISUAL_NETWORKS */
-       { "Visual Networks traffic capture", "visual",
-         visual_dump_can_write_encap, visual_dump_open },
-
-       /* WTAP_FILE_COSINE */
-       { "CoSine IPSX L2 capture", "cosine",
+       /* WTAP_FILE_TOSHIBA */
+       { "Toshiba Compact ISDN Router snoop", "toshiba", "*.*", NULL, FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_5VIEWS */
-       { "Accellent 5Views capture", "5views",
-         _5views_dump_can_write_encap, _5views_dump_open },
+       /* WTAP_FILE_VISUAL_NETWORKS */
+       { "Visual Networks traffic capture", "visual", "*.*", NULL, FALSE,
+         visual_dump_can_write_encap, visual_dump_open },
 
-       /* WTAP_FILE_ERF */
-       { "Endace DAG capture", "erf",
+       /* WTAP_FILE_ETHERPEEK_V56 */
+       { "Wildpacket Ether/TokenPeek (V5 & V6)", "peek56", "*.tpc;*.apc;*.pkt;*.wpz", ".pkt", FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_HCIDUMP */
-       { "Bluetooth HCI dump", "hcidump",
+       /* WTAP_FILE_ETHERPEEK_V7 */
+       { "Wildpacket Ether/Token/AiroPeek (V7)", "peek7", "*.tpc;*.apc;*.pkt;*.wpz", ".pkt", FALSE,
          NULL, NULL },
 
-       /* WTAP_FILE_NETWORK_INSTRUMENTS_V9 */
-       { "Network Instruments Observer version 9", "niobserverv9",
+       /* WTAP_FILE_ETHERPEEK_V9 */
+       { "Wildpacket Ether/AiroPeek (V9)", "peek9", "*.tpc;*.apc;*.pkt;*.wpz", ".pkt", FALSE,
          NULL, NULL },
+    
 };
 
 /* Name that should be somewhat descriptive. */
@@ -481,6 +575,24 @@ int wtap_short_string_to_file_type(const char *short_name)
        return -1;      /* no such file type, or we can't write it */
 }
 
+/* file extensions to use. */
+const char *wtap_file_extensions_string(int filetype)
+{
+       if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES)
+               return NULL;
+       else
+               return dump_open_table[filetype].file_extensions;
+}
+
+/* default file extension to use. */
+const char *wtap_file_extension_default_string(int filetype)
+{
+       if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES)
+               return NULL;
+       else
+               return dump_open_table[filetype].file_extension_default;
+}
+
 gboolean wtap_dump_can_open(int filetype)
 {
        if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
@@ -502,84 +614,123 @@ gboolean wtap_dump_can_write_encap(int filetype, int encap)
        return TRUE;
 }
 
-static gboolean wtap_dump_open_check(int filetype, int encap, int *err);
+gboolean wtap_dump_can_compress(int filetype)
+{
+#ifdef HAVE_LIBZ
+       if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
+           || dump_open_table[filetype].can_compress == FALSE)
+               return FALSE;
+
+       return TRUE;
+#else
+       return FALSE;
+#endif
+}
+
+
+static gboolean wtap_dump_open_check(int filetype, int encap, gboolean comressed, int *err);
 static wtap_dumper* wtap_dump_alloc_wdh(int filetype, int encap, int snaplen,
-    int *err);
-static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, int *err);
+    gboolean compressed, int *err);
+static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, gboolean compressed, int *err);
+
+static FILE *wtap_dump_file_open(wtap_dumper *wdh, const char *filename);
+static FILE *wtap_dump_file_fdopen(wtap_dumper *wdh, int fd);
+static int wtap_dump_file_close(wtap_dumper *wdh);
 
 wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
-                               int snaplen, int *err)
+                               int snaplen, gboolean compressed, int *err)
 {
        wtap_dumper *wdh;
        FILE *fh;
 
        /* Check whether we can open a capture file with that file type
           and that encapsulation. */
-       if (!wtap_dump_open_check(filetype, encap, err))
+       if (!wtap_dump_open_check(filetype, encap, compressed, err))
                return NULL;
 
        /* Allocate a data structure for the output stream. */
-       wdh = wtap_dump_alloc_wdh(filetype, encap, snaplen, err);
+       wdh = wtap_dump_alloc_wdh(filetype, encap, snaplen, compressed, err);
        if (wdh == NULL)
                return NULL;    /* couldn't allocate it */
 
-       /* Empty filename means stdout */
-       if (*filename == '\0')
+       /* "-" means stdout */
+       if (strcmp(filename, "-") == 0) {
+               if(compressed) {
+                       g_free(wdh);
+                       return NULL;    /* compress won't work on stdout */
+               }
+#ifdef _WIN32
+               setmode(fileno(stdout), O_BINARY);
+#endif
                wdh->fh = stdout;
-       else {
+       else {
                /* In case "fopen()" fails but doesn't set "errno", set "errno"
                   to a generic "the open failed" error. */
                errno = WTAP_ERR_CANT_OPEN;
-               fh = fopen(filename, "wb");
+               fh = wtap_dump_file_open(wdh, filename);
                if (fh == NULL) {
                        *err = errno;
+                       g_free(wdh);
                        return NULL;    /* can't create file */
                }
                wdh->fh = fh;
        }
 
-       if (!wtap_dump_open_finish(wdh, filetype, err)) {
+       if (!wtap_dump_open_finish(wdh, filetype, compressed, err)) {
                /* Get rid of the file we created; we couldn't finish
                   opening it. */
-               if (wdh->fh != stdout)
-                       unlink(filename);
+               if (wdh->fh != stdout) {
+                       wtap_dump_file_close(wdh);
+                       eth_unlink(filename);
+               }
+               g_free(wdh);
                return NULL;
        }
        return wdh;
 }
 
 wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen,
-                               int *err)
+                               gboolean compressed, int *err)
 {
        wtap_dumper *wdh;
        FILE *fh;
 
        /* Check whether we can open a capture file with that file type
           and that encapsulation. */
-       if (!wtap_dump_open_check(filetype, encap, err))
+       if (!wtap_dump_open_check(filetype, encap, compressed, err))
                return NULL;
 
        /* Allocate a data structure for the output stream. */
-       wdh = wtap_dump_alloc_wdh(filetype, encap, snaplen, err);
+       wdh = wtap_dump_alloc_wdh(filetype, encap, snaplen, compressed, err);
        if (wdh == NULL)
                return NULL;    /* couldn't allocate it */
 
+#ifdef _WIN32
+    if(fd == 1) {
+               setmode(fileno(stdout), O_BINARY);
+    }
+#endif
+
        /* In case "fopen()" fails but doesn't set "errno", set "errno"
           to a generic "the open failed" error. */
        errno = WTAP_ERR_CANT_OPEN;
-       fh = fdopen(fd, "wb");
+       fh = wtap_dump_file_fdopen(wdh, fd);
        if (fh == NULL) {
                *err = errno;
+               g_free(wdh);
                return NULL;    /* can't create standard I/O stream */
        }
        wdh->fh = fh;
 
-       if (!wtap_dump_open_finish(wdh, filetype, err))
+       if (!wtap_dump_open_finish(wdh, filetype, compressed, err)) {
+               wtap_dump_file_close(wdh);
+               g_free(wdh);
                return NULL;
+       }
        return wdh;
 }
 
-static gboolean wtap_dump_open_check(int filetype, int encap, int *err)
+static gboolean wtap_dump_open_check(int filetype, int encap, gboolean compressed, int *err)
 {
        if (!wtap_dump_can_open(filetype)) {
                /* Invalid type, or type we don't know how to write. */
@@ -593,12 +744,23 @@ static gboolean wtap_dump_open_check(int filetype, int encap, int *err)
        if (*err != 0)
                return FALSE;
 
+       /* if compression is wanted, do we support this for this filetype? */
+       if(compressed && !wtap_dump_can_compress(filetype)) {
+               *err = WTAP_ERR_COMPRESSION_NOT_SUPPORTED;
+               return FALSE;
+       }
+
+       *err = (*dump_open_table[filetype].can_write_encap)(encap);
+       if (*err != 0)
+               return FALSE;
+
+
        /* All systems go! */
        return TRUE;
 }
 
 static wtap_dumper* wtap_dump_alloc_wdh(int filetype, int encap, int snaplen,
-                                       int *err)
+                                       gboolean compressed, int *err)
 {
        wtap_dumper *wdh;
 
@@ -611,6 +773,7 @@ static wtap_dumper* wtap_dump_alloc_wdh(int filetype, int encap, int snaplen,
        wdh->file_type = filetype;
        wdh->snaplen = snaplen;
        wdh->encap = encap;
+       wdh->compressed = compressed;
        wdh->bytes_dumped = 0;
        wdh->dump.opaque = NULL;
        wdh->subtype_write = NULL;
@@ -618,49 +781,52 @@ static wtap_dumper* wtap_dump_alloc_wdh(int filetype, int encap, int snaplen,
        return wdh;
 }
 
-static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, int *err)
+static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, gboolean compressed, int *err)
 {
        int fd;
        gboolean cant_seek;
 
        /* Can we do a seek on the file descriptor?
           If not, note that fact. */
-       fd = fileno(wdh->fh);
-       if (lseek(fd, 1, SEEK_CUR) == -1)
-         cant_seek = TRUE;
-       else {
-         /* Undo the seek. */
-         lseek(fd, 0, SEEK_SET);
-         cant_seek = FALSE;
+       if(compressed) {
+               cant_seek = TRUE;
+       } else {
+               fd = fileno(wdh->fh);
+               if (lseek(fd, 1, SEEK_CUR) == -1)
+                 cant_seek = TRUE;
+               else {
+                 /* Undo the seek. */
+                 lseek(fd, 0, SEEK_SET);
+                 cant_seek = FALSE;
+               }
        }
 
        /* Now try to open the file for writing. */
        if (!(*dump_open_table[filetype].dump_open)(wdh, cant_seek, err)) {
-               /* The attempt failed.  Close the stream for the file.
-                  NOTE: this means the FD handed to "wtap_dump_fdopen()"
-                  will be closed if the open fails. */
-               if (wdh->fh != stdout)
-                       fclose(wdh->fh);
-
-               /* Now free up the dumper handle. */
-               g_free(wdh);
                return FALSE;
        }
 
        return TRUE;    /* success! */
 }
 
-FILE* wtap_dump_file(wtap_dumper *wdh)
-{
-       return wdh->fh;
-}
-
 gboolean wtap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
     const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err)
 {
        return (wdh->subtype_write)(wdh, phdr, pseudo_header, pd, err);
 }
 
+void wtap_dump_flush(wtap_dumper *wdh)
+{
+#ifdef HAVE_LIBZ
+       if(wdh->compressed) {
+               gzflush(wdh->fh, Z_SYNC_FLUSH); /* XXX - is Z_SYNC_FLUSH the right one? */
+       } else 
+#endif
+       {
+               fflush(wdh->fh);
+       }
+}
+
 gboolean wtap_dump_close(wtap_dumper *wdh, int *err)
 {
        gboolean ret = TRUE;
@@ -673,7 +839,7 @@ gboolean wtap_dump_close(wtap_dumper *wdh, int *err)
        errno = WTAP_ERR_CANT_CLOSE;
        /* Don't close stdout */
        if (wdh->fh != stdout) {
-               if (fclose(wdh->fh) == EOF) {
+               if (wtap_dump_file_close(wdh) == EOF) {
                        if (ret) {
                                /* The per-format close function succeeded,
                                   but the fclose didn't.  Save the reason
@@ -683,20 +849,97 @@ gboolean wtap_dump_close(wtap_dumper *wdh, int *err)
                        }
                        ret = FALSE;
                }
-       }
+    } else {
+        /* as we don't close stdout, at least try to flush it */
+        wtap_dump_flush(wdh);
+    }
        if (wdh->dump.opaque != NULL)
                g_free(wdh->dump.opaque);
        g_free(wdh);
        return ret;
 }
 
-long wtap_get_bytes_dumped(wtap_dumper *wdh)
+gint64 wtap_get_bytes_dumped(wtap_dumper *wdh)
 {
        return wdh->bytes_dumped;
 }
 
-void wtap_set_bytes_dumped(wtap_dumper *wdh, long bytes_dumped)
+void wtap_set_bytes_dumped(wtap_dumper *wdh, gint64 bytes_dumped)
 {
        wdh->bytes_dumped = bytes_dumped;
 }
 
+
+/* internally open a file for writing (compressed or not) */
+static FILE *wtap_dump_file_open(wtap_dumper *wdh, const char *filename)
+{
+#ifdef HAVE_LIBZ
+       if(wdh->compressed) {
+               return gzopen(filename, "wb");
+       } else 
+#endif
+       {
+               return eth_fopen(filename, "wb");
+       }
+}
+
+/* internally open a file for writing (compressed or not) */
+static FILE *wtap_dump_file_fdopen(wtap_dumper *wdh, int fd)
+{
+#ifdef HAVE_LIBZ
+       if(wdh->compressed) {
+               return gzdopen(fd, "wb");
+       } else 
+#endif
+       {
+               return fdopen(fd, "wb");
+       }
+}
+
+/* internally writing raw bytes (compressed or not) */
+size_t wtap_dump_file_write(wtap_dumper *wdh, const void *buf, unsigned bufsize)
+{
+#ifdef HAVE_LIBZ
+       if(wdh->compressed) {
+               return gzwrite(wdh->fh, buf, bufsize);
+       } else 
+#endif
+       {
+               return fwrite(buf, 1, bufsize, wdh->fh);
+       }
+}
+
+/* internally close a file for writing (compressed or not) */
+static int wtap_dump_file_close(wtap_dumper *wdh)
+{
+#ifdef HAVE_LIBZ
+       if(wdh->compressed) {
+               return gzclose(wdh->fh);
+       } else 
+#endif
+       {
+               return fclose(wdh->fh);
+       }
+}
+
+int wtap_dump_file_ferror(wtap_dumper *wdh)
+{
+#ifdef HAVE_LIBZ
+       int errnum;
+
+       if(wdh->compressed) {
+               gzerror(wdh->fh, &errnum);
+
+               if(errnum == Z_ERRNO) {
+                       return errno;
+               } else {
+                       /* XXX - what to do with this zlib specific number? */
+                       return errnum;
+               }
+       } else 
+#endif
+       {
+               return ferror(wdh->fh);
+       }
+}
+