Keep track, in Wiretap, of whether the file is compressed, and provide
authorGuy Harris <guy@alum.mit.edu>
Thu, 24 May 2012 05:05:29 +0000 (05:05 -0000)
committerGuy Harris <guy@alum.mit.edu>
Thu, 24 May 2012 05:05:29 +0000 (05:05 -0000)
an API to fetch that.

When doing "Save" on a compressed file, write it out compressed.

In the Statistics -> Summary dialog and in capinfos, report whether the
file is gzip-compressed.

svn path=/trunk/; revision=42818

12 files changed:
capinfos.c
cfile.h
file.c
summary.c
summary.h
ui/gtk/capture_file_dlg.c
ui/gtk/summary_dlg.c
wiretap/file_wrappers.c
wiretap/file_wrappers.h
wiretap/wtap.c
wiretap/wtap.def
wiretap/wtap.h

index 4ffc0646f3c555f531cf22a943975f7d335078ea..7c67bb26054938abe98a89a3478725c0473d5fd9 100644 (file)
@@ -191,6 +191,7 @@ typedef enum {
 typedef struct _capture_info {
   const char    *filename;
   guint16       file_type;
+  gboolean      iscompressed;
   int           file_encap;
   gint64        filesize;
 
@@ -358,7 +359,9 @@ print_stats(const gchar *filename, capture_info *cf_info)
   stop_time_t = (time_t)cf_info->stop_time;
 
   if (filename)           printf     ("File name:           %s\n", filename);
-  if (cap_file_type)      printf     ("File type:           %s\n", file_type_string);
+  if (cap_file_type)      printf     ("File type:           %s%s\n",
+                                      file_type_string,
+                                      cf_info->iscompressed ? " (gzip compressed)" : "");
   if (cap_file_encap)     printf     ("File encapsulation:  %s\n", file_encap_string);
   if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) {
     int i;
@@ -749,6 +752,7 @@ process_cap_file(wtap *wth, const char *filename)
 
   /* File Type */
   cf_info.file_type = wtap_file_type(wth);
+  cf_info.iscompressed = wtap_iscompressed(wth);
 
   /* File Encapsulation */
   cf_info.file_encap = wtap_file_encap(wth);
diff --git a/cfile.h b/cfile.h
index 63bd0eee90b47ae00fee83cb121b5c311343d4cb..d33f361fb49d411883e6785a8643f9abc7f5debf 100644 (file)
--- a/cfile.h
+++ b/cfile.h
@@ -73,6 +73,7 @@ typedef struct _capture_file {
   gboolean     unsaved_changes; /* Does the capture file have changes that have not been saved? */
   gint64       f_datalen;       /* Size of capture file data (uncompressed) */
   guint16      cd_t;            /* File type of capture file */
+  gboolean     iscompressed;    /* TRUE if the file is compressed */
   int          lnk_t;           /* Link-layer type with which to save capture */
   guint32      count;           /* Total number of frames */
   guint32      displayed_count; /* Number of displayed frames */
diff --git a/file.c b/file.c
index e04d4a41942db589e60d3b38c79409701cbd8dc7..bfbc293ab18282e702323dad1c5cd7c67a17b6a2 100644 (file)
--- a/file.c
+++ b/file.c
@@ -530,6 +530,9 @@ cf_read(capture_file *cf, gboolean reloading)
   else
     cf_callback_invoke(cf_cb_file_read_started, cf);
 
+  /* Record whether the file is compressed. */
+  cf->iscompressed = wtap_iscompressed(cf->wth);
+
   /* Find the size of the file. */
   size = wtap_file_size(cf->wth, NULL);
 
@@ -3818,7 +3821,8 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
 
   cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
 
-  if (save_format == cf->cd_t && !cf->unsaved_changes) {
+  if (save_format == cf->cd_t && compressed == cf->iscompressed
+      && !cf->unsaved_changes) {
     /* We're saving in the format it's already in, and there are no
        changes we have in memory that aren't saved to the file, so
        we can just move or copy the raw data. */
index 3e3af32148cc544b40a657a4619b62af455ab1d4..74f3874c5893b5a55d62403587024fe9cc3a1eb9 100644 (file)
--- a/summary.c
+++ b/summary.c
@@ -141,6 +141,7 @@ summary_fill_in(capture_file *cf, summary_tally *st)
   st->filename = cf->filename;
   st->file_length = cf->f_datalen;
   st->file_type = cf->cd_t;
+  st->iscompressed = cf->iscompressed;
   st->is_tempfile = cf->is_tempfile;
   st->encap_type = cf->lnk_t;
   st->has_snap = cf->has_snap;
index f359990345cea8efdb8f005099793c4daad98e11..ee3f712b6fbab0f058c0668c20cdb2a8b3fb94de 100644 (file)
--- a/summary.h
+++ b/summary.h
@@ -33,7 +33,7 @@ typedef struct iface_options_tag {
     char       *name;
     char       *descr;
     char       *cfilter;
-       char    *isb_comment;
+    char       *isb_comment;
     guint64    drops;                          /**< number of packet drops */
     gboolean   drops_known;    /**< TRUE if number of packet drops is known */
     gboolean   has_snap;               /**< TRUE if maximum capture packet length is known */
@@ -64,6 +64,7 @@ typedef struct _summary_tally {
     const char *filename;
     gint64             file_length;    /**< file length in bytes */
     int                        file_type;              /**< wiretap file type */
+    int         iscompressed;           /**< TRUE if file is compressed */
     int                        encap_type;             /**< wiretap encapsulation type */
     gboolean   has_snap;               /**< TRUE if maximum capture packet length is known */
     int                        snap;                   /**< Maximum captured packet length */
index 202d0ef3732de4ae079f18a7aaf7c8bc0ce40d4e..9730443fcc5fe2ae0fcacf8d13695d00ddb8167a 100644 (file)
@@ -1190,10 +1190,7 @@ file_save_cmd(action_after_save_e action_after_save, gpointer action_after_save_
      a copy and free it later. */
   fname = g_strdup(cfile.filename);
 
-  /* XXX - if we're editing a compressed capture file, we should
-     remember that it's compressed, and write it out in compressed
-     form. */
-  cf_save_packets(&cfile, fname, cfile.cd_t, FALSE);
+  cf_save_packets(&cfile, fname, cfile.cd_t, cfile.iscompressed);
   g_free(fname);
 }
 
index bf0ba04f960ffda1084a351a3a2e92809ba0cef8..18bd8831dec1236e452df46f3f14fd8b9340f65b 100644 (file)
@@ -238,7 +238,9 @@ summary_open_cb(GtkWidget *w _U_, gpointer d _U_)
   add_string_to_table(table, &row, "Length:", string_buff);
 
   /* format */
-  g_snprintf(string_buff, SUM_STR_MAX, "%s", wtap_file_type_string(summary.file_type));
+  g_snprintf(string_buff, SUM_STR_MAX, "%s%s",
+             wtap_file_type_string(summary.file_type),
+             summary.iscompressed? " (gzip compressed)" : "");
   add_string_to_table(table, &row, "Format:", string_buff);
 
   /* encapsulation */
index 94840f01bf88f13f7c1a7fd3a2975179e8247115..67437acee98ab0322ee34f8bdba9e0c026615441 100644 (file)
@@ -121,6 +121,7 @@ struct wtap_reader {
        gint64 start;           /* where the gzip data started, for rewinding */
        gint64 raw;             /* where the raw data started, for seeking */
        int compression;        /* 0: ?, 1: uncompressed, 2: zlib */
+       gboolean is_compressed; /* FALSE if completely uncompressed, TRUE otherwise */
        /* seek request */
        gint64 skip;            /* amount to skip (already rewound if backwards) */
        int seek;               /* true if seek request pending */
@@ -128,8 +129,8 @@ struct wtap_reader {
        int err;                /* error code */
        const char *err_info;   /* additional error information string for some errors */
 
-       unsigned int  avail_in;  /* number of bytes available at next_in */
-       unsigned char *next_in;  /* next input byte */
+       unsigned int  avail_in; /* number of bytes available at next_in */
+       unsigned char *next_in; /* next input byte */
 #ifdef HAVE_LIBZ
        /* zlib inflate stream */
        z_stream strm;          /* stream structure in-place (not a pointer) */
@@ -140,7 +141,7 @@ struct wtap_reader {
        void *fast_seek_cur;
 };
 
-/* values for gz_state compression */
+/* values for wtap_reader compression */
 #define UNKNOWN                0       /* look for a gzip header */
 #define UNCOMPRESSED   1       /* copy input directly */
 #ifdef HAVE_LIBZ
@@ -651,6 +652,7 @@ gz_head(FILE_T state)
                        inflateReset(&(state->strm));
                        state->strm.adler = crc32(0L, Z_NULL, 0);
                        state->compression = ZLIB;
+                       state->is_compressed = TRUE;
 #ifdef Z_BLOCK
                        if (state->fast_seek) {
                                struct zlib_cur_seek_point *cur = g_malloc(sizeof(struct zlib_cur_seek_point));
@@ -786,6 +788,9 @@ filed_open(int fd)
        /* open the file with the appropriate mode (or just use fd) */
        state->fd = fd;
 
+       /* we don't yet know whether it's compressed */
+       state->is_compressed = FALSE;
+
        /* save the current position for rewinding (only if reading) */
        state->start = ws_lseek64(state->fd, 0, SEEK_CUR);
        if (state->start == -1) state->start = 0;
@@ -1085,6 +1090,12 @@ file_fstat(FILE_T stream, ws_statb64 *statb, int *err)
        return 0;
 }
 
+gboolean
+file_iscompressed(FILE_T stream)
+{
+       return stream->is_compressed;
+}
+
 int 
 file_read(void *buf, unsigned int len, FILE_T file)
 {
index b594a7330bb312f7ce6db83440c8a396f83e731b..4ef46fb297c96668146fc2b69bc9123a268c600a 100644 (file)
@@ -35,6 +35,7 @@ extern gint64 file_skip(FILE_T file, gint64 delta, int *err);
 extern gint64 file_tell(FILE_T stream);
 extern gint64 file_tell_raw(FILE_T stream);
 extern int file_fstat(FILE_T stream, ws_statb64 *statb, int *err);
+extern gboolean file_iscompressed(FILE_T stream);
 extern int file_read(void *buf, unsigned int count, FILE_T file);
 extern int file_getc(FILE_T stream);
 extern char *file_gets(char *buf, int len, FILE_T stream);
index 0d58ffa2b57f5d898487a93a022fed8e1dce5847..cf63c9c0530d885168f6847cb76ff3bef99d88e2 100644 (file)
@@ -78,6 +78,12 @@ wtap_file_type(wtap *wth)
        return wth->file_type;
 }
 
+gboolean
+wtap_iscompressed(wtap *wth)
+{
+       return file_iscompressed((wth->fh == NULL) ? wth->random_fh : wth->fh);
+}
+
 guint
 wtap_snapshot_length(wtap *wth)
 {
index a677312e8aa686d7186e7f59337d55f6fb29d46c..48b8316265d58403621b2dd914c2fcd2495e9ffa 100644 (file)
@@ -61,6 +61,7 @@ wtap_fstat
 wtap_get_bytes_dumped
 wtap_get_num_encap_types
 wtap_get_num_file_types
+wtap_iscompressed
 wtap_open_offline
 wtap_pcap_encap_to_wtap_encap
 wtap_phdr
index 45c26f4a17d8693b970afcf1f98a48848bea8bc0..21463444152163a3af36fc166e122dfdbff469ee 100644 (file)
@@ -1074,6 +1074,7 @@ guint8 *wtap_buf_ptr(wtap *wth);
  * from the file so far. */
 gint64 wtap_read_so_far(wtap *wth);
 gint64 wtap_file_size(wtap *wth, int *err);
+gboolean wtap_iscompressed(wtap *wth);
 guint wtap_snapshot_length(wtap *wth); /* per file */
 int wtap_file_type(wtap *wth);
 int wtap_file_encap(wtap *wth);