The "file types" we have are actually combinations of types and
[metze/wireshark/wip.git] / file.c
diff --git a/file.c b/file.c
index 3f9607279ce55c61f14836b47d98618c681b2a7a..4a936af5ecd2d6fe472999684f1f7844a33c776a 100644 (file)
--- a/file.c
+++ b/file.c
 #include <fcntl.h>
 #endif
 
-#include <epan/epan.h>
-#include <epan/expert.h>
-#include <epan/filesystem.h>
+#include <wsutil/tempfile.h>
+#include <wsutil/file_util.h>
 
-#include "color.h"
-#include "color_filters.h"
-#include "cfile.h"
+#include <wiretap/merge.h>
+
+#include <epan/epan-int.h>
+#include <epan/epan.h>
 #include <epan/column.h>
 #include <epan/packet.h>
 #include <epan/column-utils.h>
-#include "packet-range.h"
-#include "print.h"
-#include "file.h"
-#include "fileset.h"
-#include "tempfile.h"
-#include "merge.h"
-
+#include <epan/expert.h>
+#include <epan/filesystem.h>
 #include <epan/prefs.h>
 #include <epan/dfilter/dfilter.h>
 #include <epan/epan_dissect.h>
 #include <epan/dissectors/packet-ber.h>
 #include <epan/timestamp.h>
 #include <epan/dfilter/dfilter-macro.h>
-#include <wsutil/file_util.h>
 #include <epan/strutil.h>
 #include <epan/addr_resolv.h>
 
+#include "color.h"
+#include "color_filters.h"
+#include "cfile.h"
+#include "file.h"
+#include "fileset.h"
+#include "frame_tvbuff.h"
+
 #include "ui/alert_box.h"
 #include "ui/simple_dialog.h"
 #include "ui/main_statusbar.h"
 #include "ui/progress_dlg.h"
 #include "ui/ui_util.h"
 
+#include "version_info.h"
+
 /* Needed for addrinfo */
 #ifdef HAVE_SYS_TYPES_H
 # include <sys/types.h>
 gboolean auto_scroll_live;
 #endif
 
-static guint32 cum_bytes;
-static nstime_t first_ts;
-static frame_data *prev_dis;
-static frame_data *prev_cap;
-
-static gulong computed_elapsed;
-
 static void cf_reset_state(capture_file *cf);
 
-static int read_packet(capture_file *cf, dfilter_t *dfcode,
-    gboolean create_proto_tree, column_info *cinfo, gint64 offset);
+static int read_packet(capture_file *cf, dfilter_t *dfcode, epan_dissect_t *edt,
+    column_info *cinfo, gint64 offset);
 
 static void rescan_packets(capture_file *cf, const char *action, const char *action_item, gboolean redissect);
 
@@ -147,6 +143,8 @@ static gboolean find_packet(capture_file *cf,
     match_result (*match_function)(capture_file *, frame_data *, void *),
     void *criterion, search_direction dir);
 
+static const char *cf_get_user_packet_comment(capture_file *cf, const frame_data *fd);
+
 static void cf_open_failure_alert_box(const char *filename, int err,
                       gchar *err_info, gboolean for_writing,
                       int file_type);
@@ -197,7 +195,7 @@ cf_callback_add(cf_callback_t func, gpointer user_data)
   cb->cb_fct = func;
   cb->user_data = user_data;
 
-  cf_callbacks = g_list_append(cf_callbacks, cb);
+  cf_callbacks = g_list_prepend(cf_callbacks, cb);
 }
 
 void
@@ -273,21 +271,16 @@ cf_timestamp_auto_precision(capture_file *cf)
 }
 
 gulong
-cf_get_computed_elapsed(void)
-{
-  return computed_elapsed;
-}
-
-static void reset_elapsed(void)
+cf_get_computed_elapsed(capture_file *cf)
 {
-  computed_elapsed = 0;
+  return cf->computed_elapsed;
 }
 
 /*
  * GLIB_CHECK_VERSION(2,28,0) adds g_get_real_time which could minimize or
  * replace this
  */
-static void compute_elapsed(GTimeVal *start_time)
+static void compute_elapsed(capture_file *cf, GTimeVal *start_time)
 {
   gdouble  delta_time;
   GTimeVal time_now;
@@ -297,7 +290,48 @@ static void compute_elapsed(GTimeVal *start_time)
   delta_time = (time_now.tv_sec - start_time->tv_sec) * 1e6 +
     time_now.tv_usec - start_time->tv_usec;
 
-  computed_elapsed = (gulong) (delta_time / 1000); /* ms */
+  cf->computed_elapsed = (gulong) (delta_time / 1000); /* ms */
+}
+
+static const nstime_t *
+ws_get_frame_ts(void *data, guint32 frame_num)
+{
+  capture_file *cf = (capture_file *) data;
+
+  if (cf->prev_dis && cf->prev_dis->num == frame_num)
+    return &cf->prev_dis->abs_ts;
+
+  if (cf->prev_cap && cf->prev_cap->num == frame_num)
+    return &cf->prev_cap->abs_ts;
+
+  if (cf->frames) {
+    frame_data *fd = frame_data_sequence_find(cf->frames, frame_num);
+
+    return (fd) ? &fd->abs_ts : NULL;
+  }
+
+  return NULL;
+}
+
+static const char *
+ws_get_user_comment(void *data, const frame_data *fd)
+{
+  capture_file *cf = (capture_file *) data;
+
+  return cf_get_user_packet_comment(cf, fd);
+}
+
+static epan_t *
+ws_epan_new(capture_file *cf)
+{
+  epan_t *epan = epan_new();
+
+  epan->data = cf;
+  epan->get_frame_ts = ws_get_frame_ts;
+  epan->get_interface_name = cap_file_get_interface_name;
+  epan->get_user_comment = ws_get_user_comment;
+
+  return epan;
 }
 
 cf_status_t
@@ -318,10 +352,10 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
      the packets, so we know how much we'll ultimately need. */
   buffer_init(&cf->buf, 1500);
 
-  /* Cleanup all data structures used for dissection. */
-  cleanup_dissection();
-  /* Initialize all data structures used for dissection. */
-  init_dissection();
+  /* Create new epan session for dissection.
+   * (The old one was freed in cf_close().)
+   */
+  cf->epan = ws_epan_new(cf);
 
   /* We're about to start reading the file. */
   cf->state = FILE_READ_IN_PROGRESS;
@@ -340,9 +374,9 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
   /* No user changes yet. */
   cf->unsaved_changes = FALSE;
 
-  reset_elapsed();
+  cf->computed_elapsed = 0;
 
-  cf->cd_t        = wtap_file_type(cf->wth);
+  cf->cd_t        = wtap_file_type_subtype(cf->wth);
   cf->linktypes = g_array_sized_new(FALSE, FALSE, (guint) sizeof(int), 1);
   cf->count     = 0;
   cf->packet_comment_count = 0;
@@ -364,10 +398,10 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
   cf->frames = new_frame_data_sequence();
 
   nstime_set_zero(&cf->elapsed_time);
-  nstime_set_unset(&first_ts);
-  prev_dis = NULL;
-  prev_cap = NULL;
-  cum_bytes = 0;
+  cf->ref = NULL;
+  cf->prev_dis = NULL;
+  cf->prev_cap = NULL;
+  cf->cum_bytes = 0;
 
   /* Adjust timestamp precision if auto is selected, col width will be adjusted */
   cf_timestamp_auto_precision(cf);
@@ -375,7 +409,7 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
   packet_list_queue_draw();
   cf_callback_invoke(cf_cb_file_opened, cf);
 
-  if (cf->cd_t == WTAP_FILE_BER) {
+  if (cf->cd_t == WTAP_FILE_TYPE_SUBTYPE_BER) {
     /* tell the BER dissector the file name */
     ber_set_filename(cf->filename);
   }
@@ -452,6 +486,10 @@ cf_reset_state(capture_file *cf)
     cf->edited_frames = NULL;
   }
 #endif
+  if (cf->frames_user_comments) {
+    g_tree_destroy(cf->frames_user_comments);
+    cf->frames_user_comments = NULL;
+  }
   cf_unselect_packet(cf);   /* nothing to select */
   cf->first_displayed = 0;
   cf->last_displayed = 0;
@@ -490,7 +528,8 @@ cf_close(capture_file *cf)
   /* close things, if not already closed before */
     color_filters_cleanup();
     cf_reset_state(cf);
-    cleanup_dissection();
+    epan_free(cf->epan);
+    cf->epan = NULL;
 
     cf_callback_invoke(cf_cb_file_closed, cf);
   }
@@ -537,6 +576,7 @@ cf_read(capture_file *cf, gboolean reloading)
   progdlg_t           *progbar        = NULL;
   gboolean             stop_flag;
   GTimeVal             start_time;
+  epan_dissect_t       edt;
   dfilter_t           *dfcode;
   volatile gboolean    create_proto_tree;
   guint                tap_flags;
@@ -573,6 +613,8 @@ cf_read(capture_file *cf, gboolean reloading)
   stop_flag = FALSE;
   g_get_current_time(&start_time);
 
+  epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
+
   TRY {
 #ifdef HAVE_LIBPCAP
     int     displayed_once    = 0;
@@ -605,8 +647,6 @@ cf_read(capture_file *cf, gboolean reloading)
         progbar_quantum = MIN_QUANTUM;
     }else
       progbar_quantum = 0;
-    /* Progress so far. */
-    progbar_val = 0.0f;
 
     while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
       if (size >= 0) {
@@ -658,7 +698,7 @@ cf_read(capture_file *cf, gboolean reloading)
            hours even on fast machines) just to see that it was the wrong file. */
         break;
       }
-      read_packet(cf, dfcode, create_proto_tree, cinfo, data_offset);
+      read_packet(cf, dfcode, &edt, cinfo, data_offset);
     }
   }
   CATCH(OutOfMemoryError) {
@@ -683,6 +723,8 @@ cf_read(capture_file *cf, gboolean reloading)
     dfilter_free(dfcode);
   }
 
+  epan_dissect_cleanup(&edt);
+
   /* We're done reading the file; destroy the progress bar if it was created. */
   if (progbar != NULL)
     destroy_progress_dlg(progbar);
@@ -698,7 +740,7 @@ cf_read(capture_file *cf, gboolean reloading)
   postseq_cleanup_all_protocols();
 
   /* compute the time it took to load the file */
-  compute_elapsed(&start_time);
+  compute_elapsed(cf, &start_time);
 
   /* Set the file encapsulation type now; we don't know what it is until
      we've looked at all the packets, as we don't know until then whether
@@ -789,22 +831,14 @@ cf_read(capture_file *cf, gboolean reloading)
 }
 
 #ifdef HAVE_LIBPCAP
-cf_status_t
-cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
-{
-  cf_status_t cf_status;
-
-  cf_status = cf_open(cf, fname, is_tempfile, err);
-  return cf_status;
-}
-
 cf_read_status_t
 cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
 {
   gchar            *err_info;
   int               newly_displayed_packets = 0;
   dfilter_t        *dfcode;
-  volatile gboolean create_proto_tree;
+  epan_dissect_t       edt;
+  gboolean create_proto_tree;
   guint             tap_flags;
   gboolean          compiled;
 
@@ -828,6 +862,8 @@ cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
 
   /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
 
+  epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
+
   TRY {
     gint64 data_offset = 0;
     column_info *cinfo;
@@ -845,7 +881,7 @@ cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
            aren't any packets left to read) exit. */
         break;
       }
-      if (read_packet(cf, dfcode, create_proto_tree, (column_info *) cinfo, data_offset) != -1) {
+      if (read_packet(cf, dfcode, &edt, (column_info *) cinfo, data_offset) != -1) {
         newly_displayed_packets++;
       }
       to_read--;
@@ -875,6 +911,8 @@ cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
     dfilter_free(dfcode);
   }
 
+  epan_dissect_cleanup(&edt);
+
   /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: count %u state: %u err: %u",
     cf->count, cf->state, *err);*/
 
@@ -922,6 +960,7 @@ cf_finish_tail(capture_file *cf, int *err)
   gint64     data_offset;
   dfilter_t *dfcode;
   column_info *cinfo;
+  epan_dissect_t edt;
   gboolean   create_proto_tree;
   guint      tap_flags;
   gboolean   compiled;
@@ -948,6 +987,8 @@ cf_finish_tail(capture_file *cf, int *err)
   /* Don't freeze/thaw the list when doing live capture */
   /*packet_list_freeze();*/
 
+  epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
+
   while ((wtap_read(cf->wth, err, &err_info, &data_offset))) {
     if (cf->state == FILE_READ_ABORTED) {
       /* Well, the user decided to abort the read.  Break out of the
@@ -955,7 +996,7 @@ cf_finish_tail(capture_file *cf, int *err)
          aren't any packets left to read) exit. */
       break;
     }
-    read_packet(cf, dfcode, create_proto_tree, cinfo, data_offset);
+    read_packet(cf, dfcode, &edt, cinfo, data_offset);
   }
 
   /* Cleanup and release all dfilter resources */
@@ -963,6 +1004,8 @@ cf_finish_tail(capture_file *cf, int *err)
     dfilter_free(dfcode);
   }
 
+  epan_dissect_cleanup(&edt);
+
   /* Don't freeze/thaw the list when doing live capture */
   /*packet_list_thaw();*/
 
@@ -1116,35 +1159,32 @@ void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
 
 static int
 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
-    dfilter_t *dfcode, gboolean create_proto_tree, column_info *cinfo,
+    epan_dissect_t *edt, dfilter_t *dfcode, column_info *cinfo,
     struct wtap_pkthdr *phdr, const guint8 *buf, gboolean add_to_packet_list)
 {
-  epan_dissect_t  edt;
   gint            row               = -1;
 
   frame_data_set_before_dissect(fdata, &cf->elapsed_time,
-                                &first_ts, prev_dis, prev_cap);
-  prev_cap = fdata;
-
-  /* Dissect the frame. */
-  epan_dissect_init(&edt, create_proto_tree, FALSE);
+                                &cf->ref, cf->prev_dis);
+  cf->prev_cap = fdata;
 
   if (dfcode != NULL) {
-      epan_dissect_prime_dfilter(&edt, dfcode);
+      epan_dissect_prime_dfilter(edt, dfcode);
   }
 
-  epan_dissect_run_with_taps(&edt, phdr, buf, fdata, cinfo);
+  /* Dissect the frame. */
+  epan_dissect_run_with_taps(edt, phdr, frame_tvbuff_new(fdata, buf), fdata, cinfo);
 
   /* If we don't have a display filter, set "passed_dfilter" to 1. */
   if (dfcode != NULL) {
-    fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, &edt) ? 1 : 0;
+    fdata->flags.passed_dfilter = dfilter_apply_edt(dfcode, edt) ? 1 : 0;
 
     if (fdata->flags.passed_dfilter) {
       /* This frame passed the display filter but it may depend on other
        * (potentially not displayed) frames.  Find those frames and mark them
        * as depended upon.
        */
-      g_slist_foreach(edt.pi.dependent_frames, find_and_mark_frame_depended_upon, cf->frames);
+      g_slist_foreach(edt->pi.dependent_frames, find_and_mark_frame_depended_upon, cf->frames);
     }
   } else
     fdata->flags.passed_dfilter = 1;
@@ -1154,13 +1194,13 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
 
   if (add_to_packet_list) {
     /* We fill the needed columns from new_packet_list */
-      row = packet_list_append(cinfo, fdata, &edt.pi);
+      row = packet_list_append(cinfo, fdata, &edt->pi);
   }
 
   if (fdata->flags.passed_dfilter || fdata->flags.ref_time)
   {
-    frame_data_set_after_dissect(fdata, &cum_bytes);
-    prev_dis = fdata;
+    frame_data_set_after_dissect(fdata, &cf->cum_bytes);
+    cf->prev_dis = fdata;
 
     /* If we haven't yet seen the first frame, this is it.
 
@@ -1182,15 +1222,15 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
     cf->last_displayed = fdata->num;
   }
 
-  epan_dissect_cleanup(&edt);
+  epan_dissect_reset(edt);
   return row;
 }
 
 /* read in a new packet */
 /* returns the row of the new packet in the packet list or -1 if not displayed */
 static int
-read_packet(capture_file *cf, dfilter_t *dfcode,
-            gboolean create_proto_tree, column_info *cinfo, gint64 offset)
+read_packet(capture_file *cf, dfilter_t *dfcode, epan_dissect_t *edt,
+            column_info *cinfo, gint64 offset)
 {
   struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
   const guint8 *buf = wtap_buf_ptr(cf->wth);
@@ -1212,16 +1252,17 @@ read_packet(capture_file *cf, dfilter_t *dfcode,
      frames in the file so far. */
   framenum = cf->count + 1;
 
-  frame_data_init(&fdlocal, framenum, phdr, offset, cum_bytes);
+  frame_data_init(&fdlocal, framenum, phdr, offset, cf->cum_bytes);
 
   passed = TRUE;
   if (cf->rfcode) {
-    epan_dissect_t edt;
-    epan_dissect_init(&edt, TRUE, FALSE);
-    epan_dissect_prime_dfilter(&edt, cf->rfcode);
-    epan_dissect_run(&edt, phdr, buf, &fdlocal, NULL);
-    passed = dfilter_apply_edt(cf->rfcode, &edt);
-    epan_dissect_cleanup(&edt);
+    epan_dissect_t rf_edt;
+
+    epan_dissect_init(&rf_edt, cf->epan, TRUE, FALSE);
+    epan_dissect_prime_dfilter(&rf_edt, cf->rfcode);
+    epan_dissect_run(&rf_edt, phdr, frame_tvbuff_new(&fdlocal, buf), &fdlocal, NULL);
+    passed = dfilter_apply_edt(cf->rfcode, &rf_edt);
+    epan_dissect_cleanup(&rf_edt);
   }
 
   if (passed) {
@@ -1229,14 +1270,13 @@ read_packet(capture_file *cf, dfilter_t *dfcode,
     fdata = frame_data_sequence_add(cf->frames, &fdlocal);
 
     cf->count++;
-    if (fdlocal.opt_comment != NULL)
+    if (phdr->opt_comment != NULL)
       cf->packet_comment_count++;
     cf->f_datalen = offset + fdlocal.cap_len;
 
     if (!cf->redissecting) {
-      row = add_packet_to_packet_list(fdata, cf, dfcode,
-                                      create_proto_tree, cinfo,
-                                      phdr, buf, TRUE);
+      row = add_packet_to_packet_list(fdata, cf, edt, dfcode,
+                                      cinfo, phdr, buf, TRUE);
     }
   }
 
@@ -1309,7 +1349,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
    * We need something similar when merging pcapng files possibly with an option to say
    * the same interface(s) used in all in files. SHBs comments should be merged together.
    */
-  if ((selected_frame_type == WTAP_ENCAP_PER_PACKET)&&(file_type == WTAP_FILE_PCAP)) {
+  if ((selected_frame_type == WTAP_ENCAP_PER_PACKET)&&(file_type == WTAP_FILE_TYPE_SUBTYPE_PCAP)) {
     /* Write output in pcapng format */
     wtapng_section_t            *shb_hdr;
     wtapng_iface_descriptions_t *idb_inf, *idb_inf_merge_file;
@@ -1322,7 +1362,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
     comment_gstr = g_string_new("");
     g_string_append_printf(comment_gstr, "%s \n",shb_hdr->opt_comment);
     g_string_append_printf(comment_gstr, "File created by merging: \n");
-    file_type = WTAP_FILE_PCAPNG;
+    file_type = WTAP_FILE_TYPE_SUBTYPE_PCAPNG;
 
     for (i = 0; i < in_file_count; i++) {
         g_string_append_printf(comment_gstr, "File%d: %s \n",i+1,in_files[i].filename);
@@ -1483,9 +1523,10 @@ cf_merge_files(char **out_filenamep, int in_file_count,
       break;
     }
 
-    /* If we have WTAP_ENCAP_PER_PACKETend the infiles are of type WTAP_FILE_PCAP
-     * we need to set the interface id in the paket header = the interface index we used
-     * in the IDBs interface description for this file(encapsulation type).
+    /* If we have WTAP_ENCAP_PER_PACKET and the infiles are of type
+     * WTAP_FILE_TYPE_SUBTYPE_PCAP, we need to set the interface id
+     * in the paket header = the interface index we used in the IDBs
+     * interface description for this file(encapsulation type).
      */
     if (fake_interface_ids) {
       struct wtap_pkthdr *phdr;
@@ -1583,7 +1624,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
         simple_error_message_box(
                       "Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.",
                       in_file->packet_num, display_basename,
-                      wtap_file_type_string(file_type));
+                      wtap_file_type_subtype_string(file_type));
         g_free(display_basename);
         break;
 
@@ -1682,11 +1723,13 @@ cf_reftime_packets(capture_file *cf)
 void
 cf_redissect_packets(capture_file *cf)
 {
-  rescan_packets(cf, "Reprocessing", "all packets", TRUE);
+  if (cf->state != FILE_CLOSED) {
+    rescan_packets(cf, "Reprocessing", "all packets", TRUE);
+  }
 }
 
 gboolean
-cf_read_frame_r(capture_file *cf, frame_data *fdata,
+cf_read_frame_r(capture_file *cf, const frame_data *fdata,
                 struct wtap_pkthdr *phdr, Buffer *buf)
 {
   int    err;
@@ -1704,7 +1747,8 @@ cf_read_frame_r(capture_file *cf, frame_data *fdata,
     }
 
     *phdr = frame->phdr;
-    memcpy(pd, frame->pd, fdata->cap_len);
+    buffer_assure_space(buf, frame->phdr.caplen);
+    memcpy(buffer_start_ptr(buf), frame->pd, frame->phdr.caplen);
     return TRUE;
   }
 #endif
@@ -1773,6 +1817,7 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item, gb
   gchar       status_str[100];
   int         progbar_nextstep;
   int         progbar_quantum;
+  epan_dissect_t  edt;
   dfilter_t  *dfcode;
   column_info *cinfo;
   gboolean    create_proto_tree;
@@ -1818,10 +1863,9 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item, gb
        want to dissect those before their time. */
     cf->redissecting = TRUE;
 
-    /* Cleanup all data structures used for dissection. */
-    cleanup_dissection();
-    /* Initialize all data structures used for dissection. */
-    init_dissection();
+    /* 'reset' dissection session */
+    epan_free(cf->epan);
+    cf->epan = ws_epan_new(cf);
 
     /* We need to redissect the packets so we have to discard our old
      * packet list store. */
@@ -1839,10 +1883,10 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item, gb
   /* Iterate through the list of frames.  Call a routine for each frame
      to check whether it should be displayed and, if so, add it to
      the display list. */
-  nstime_set_unset(&first_ts);
-  prev_dis = NULL;
-  prev_cap = NULL;
-  cum_bytes = 0;
+  cf->ref = NULL;
+  cf->prev_dis = NULL;
+  cf->prev_cap = NULL;
+  cf->cum_bytes = 0;
 
   /* Update the progress bar when it gets to this value. */
   progbar_nextstep = 0;
@@ -1869,6 +1913,9 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item, gb
   selected_frame_seen = FALSE;
 
   frames_count = cf->count;
+
+  epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
+
   for (framenum = 1; framenum <= frames_count; framenum++) {
     fdata = frame_data_sequence_find(cf->frames, framenum);
 
@@ -1943,7 +1990,8 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item, gb
       preceding_frame_num = prev_frame_num;
       preceding_frame = prev_frame;
     }
-    add_packet_to_packet_list(fdata, cf, dfcode, create_proto_tree,
+
+    add_packet_to_packet_list(fdata, cf, &edt, dfcode,
                                     cinfo, &cf->phdr,
                                     buffer_start_ptr(&cf->buf),
                                     add_to_packet_list);
@@ -1968,6 +2016,8 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item, gb
     prev_frame = fdata;
   }
 
+  epan_dissect_cleanup(&edt);
+
   /* We are done redissecting the packet list. */
   cf->redissecting = FALSE;
 
@@ -1998,7 +2048,7 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item, gb
     packet_list_recreate_visible_rows();
 
   /* Compute the time it took to filter the file */
-  compute_elapsed(&start_time);
+  compute_elapsed(cf, &start_time);
 
   packet_list_thaw();
 
@@ -2076,16 +2126,17 @@ ref_time_packets(capture_file *cf)
 {
   guint32     framenum;
   frame_data *fdata;
+  nstime_t rel_ts;
 
-  nstime_set_unset(&first_ts);
-  prev_dis = NULL;
-  cum_bytes = 0;
+  cf->ref = NULL;
+  cf->prev_dis = NULL;
+  cf->cum_bytes = 0;
 
   for (framenum = 1; framenum <= cf->count; framenum++) {
     fdata = frame_data_sequence_find(cf->frames, framenum);
 
     /* just add some value here until we know if it is being displayed or not */
-    fdata->cum_bytes = cum_bytes + fdata->pkt_len;
+    fdata->cum_bytes = cf->cum_bytes + fdata->pkt_len;
 
     /*
      *Timestamps
@@ -2094,39 +2145,38 @@ ref_time_packets(capture_file *cf)
     /* If we don't have the time stamp of the first packet in the
      capture, it's because this is the first packet.  Save the time
      stamp of this packet as the time stamp of the first packet. */
-    if (nstime_is_unset(&first_ts)) {
-        first_ts  = fdata->abs_ts;
-    }
+    if (cf->ref == NULL)
+        cf->ref = fdata;
       /* if this frames is marked as a reference time frame, reset
         firstsec and firstusec to this frame */
-    if (fdata->flags.ref_time) {
-        first_ts = fdata->abs_ts;
-    }
+    if (fdata->flags.ref_time)
+        cf->ref = fdata;
 
     /* If we don't have the time stamp of the previous displayed packet,
      it's because this is the first displayed packet.  Save the time
      stamp of this packet as the time stamp of the previous displayed
      packet. */
-    if (prev_dis == NULL) {
-        prev_dis = fdata;
+    if (cf->prev_dis == NULL) {
+        cf->prev_dis = fdata;
     }
 
     /* Get the time elapsed between the first packet and this packet. */
-    nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
+    fdata->frame_ref_num = (fdata != cf->ref) ? cf->ref->num : 0;
+    nstime_delta(&rel_ts, &fdata->abs_ts, &cf->ref->abs_ts);
 
     /* If it's greater than the current elapsed time, set the elapsed time
      to it (we check for "greater than" so as not to be confused by
      time moving backwards). */
-    if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
-        || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
-        cf->elapsed_time = fdata->rel_ts;
+    if ((gint32)cf->elapsed_time.secs < rel_ts.secs
+        || ((gint32)cf->elapsed_time.secs == rel_ts.secs && (gint32)cf->elapsed_time.nsecs < rel_ts.nsecs)) {
+        cf->elapsed_time = rel_ts;
     }
 
     /* If this frame is displayed, get the time elapsed between the
      previous displayed packet and this packet. */
     if ( fdata->flags.passed_dfilter ) {
-        fdata->prev_dis = prev_dis;
-        prev_dis = fdata;
+        fdata->prev_dis_num = cf->prev_dis->num;
+        cf->prev_dis = fdata;
     }
 
     /*
@@ -2138,11 +2188,11 @@ ref_time_packets(capture_file *cf)
         even if they dont pass the display filter */
         if (fdata->flags.ref_time) {
             /* if this was a TIME REF frame we should reset the cum_bytes field */
-            cum_bytes = fdata->pkt_len;
-            fdata->cum_bytes =  cum_bytes;
+            cf->cum_bytes = fdata->pkt_len;
+            fdata->cum_bytes = cf->cum_bytes;
         } else {
             /* increase cum_bytes with this packets length */
-            cum_bytes += fdata->pkt_len;
+            cf->cum_bytes += fdata->pkt_len;
         }
     }
   }
@@ -2177,6 +2227,7 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
   range_process_e  process_this;
   struct wtap_pkthdr phdr;
 
+  memset(&phdr, 0, sizeof(struct wtap_pkthdr));
   buffer_init(&buf, 1500);
 
   /* Update the progress bar when it gets to this value. */
@@ -2280,7 +2331,7 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
 }
 
 typedef struct {
-  gboolean     construct_protocol_tree;
+  epan_dissect_t edt;
   column_info *cinfo;
 } retap_callback_args_t;
 
@@ -2290,11 +2341,9 @@ retap_packet(capture_file *cf _U_, frame_data *fdata,
              void *argsp)
 {
   retap_callback_args_t *args = (retap_callback_args_t *)argsp;
-  epan_dissect_t         edt;
 
-  epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
-  epan_dissect_run_with_taps(&edt, phdr, pd, fdata, args->cinfo);
-  epan_dissect_cleanup(&edt);
+  epan_dissect_run_with_taps(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, args->cinfo);
+  epan_dissect_reset(&args->edt);
 
   return TRUE;
 }
@@ -2304,8 +2353,10 @@ cf_retap_packets(capture_file *cf)
 {
   packet_range_t        range;
   retap_callback_args_t callback_args;
+  gboolean              construct_protocol_tree;
   gboolean              filtering_tap_listeners;
   guint                 tap_flags;
+  psp_return_t          ret;
 
   /* Do we have any tap listeners with filters? */
   filtering_tap_listeners = have_filtering_tap_listeners();
@@ -2314,8 +2365,8 @@ cf_retap_packets(capture_file *cf)
 
   /* If any tap listeners have filters, or require the protocol tree,
      construct the protocol tree. */
-  callback_args.construct_protocol_tree = filtering_tap_listeners ||
-                                          (tap_flags & TL_REQUIRES_PROTO_TREE);
+  construct_protocol_tree = filtering_tap_listeners ||
+                            (tap_flags & TL_REQUIRES_PROTO_TREE);
 
   /* If any tap listeners require the columns, construct them. */
   callback_args.cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
@@ -2323,13 +2374,20 @@ cf_retap_packets(capture_file *cf)
   /* Reset the tap listeners. */
   reset_tap_listeners();
 
+  epan_dissect_init(&callback_args.edt, cf->epan, construct_protocol_tree, FALSE);
+
   /* Iterate through the list of packets, dissecting all packets and
      re-running the taps. */
   packet_range_init(&range, cf);
   packet_range_process_init(&range);
-  switch (process_specified_packets(cf, &range, "Recalculating statistics on",
-                                    "all packets", TRUE, retap_packet,
-                                    &callback_args)) {
+
+  ret = process_specified_packets(cf, &range, "Recalculating statistics on",
+                                  "all packets", TRUE, retap_packet,
+                                  &callback_args);
+
+  epan_dissect_cleanup(&callback_args.edt);
+
+  switch (ret) {
   case PSP_FINISHED:
     /* Completed successfully. */
     return CF_READ_OK;
@@ -2360,6 +2418,7 @@ typedef struct {
   gint         *col_widths;
   int           num_visible_cols;
   gint         *visible_cols;
+  epan_dissect_t edt;
 } print_callback_args_t;
 
 static gboolean
@@ -2368,31 +2427,22 @@ print_packet(capture_file *cf, frame_data *fdata,
              void *argsp)
 {
   print_callback_args_t *args = (print_callback_args_t *)argsp;
-  epan_dissect_t  edt;
   int             i;
   char           *cp;
   int             line_len;
   int             column_len;
   int             cp_off;
-  gboolean        proto_tree_needed;
   char            bookmark_name[9+10+1];  /* "__frameNNNNNNNNNN__\0" */
   char            bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0"  */
 
-  /* Create the protocol tree, and make it visible, if we're printing
-     the dissection or the hex data.
-     XXX - do we need it if we're just printing the hex data? */
-  proto_tree_needed =
-      args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex || have_custom_cols(&cf->cinfo);
-  epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
-
   /* Fill in the column information if we're printing the summary
      information. */
   if (args->print_args->print_summary) {
-    col_custom_prime_edt(&edt, &cf->cinfo);
-    epan_dissect_run(&edt, phdr, pd, fdata, &cf->cinfo);
-    epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
+    col_custom_prime_edt(&args->edt, &cf->cinfo);
+    epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, &cf->cinfo);
+    epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
   } else
-    epan_dissect_run(&edt, phdr, pd, fdata, NULL);
+    epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, NULL);
 
   if (args->print_formfeed) {
     if (!new_page(args->print_args->stream))
@@ -2475,7 +2525,7 @@ print_packet(capture_file *cf, frame_data *fdata,
     }
 
     /* Print the information in that tree. */
-    if (!proto_tree_print(args->print_args, &edt, args->print_args->stream))
+    if (!proto_tree_print(args->print_args, &args->edt, args->print_args->stream))
       goto fail;
 
     /* Print a blank line if we print anything after this (aka more than one packet). */
@@ -2492,7 +2542,7 @@ print_packet(capture_file *cf, frame_data *fdata,
         goto fail;
     }
     /* Print the full packet data as hex. */
-    if (!print_hex_data(args->print_args->stream, &edt))
+    if (!print_hex_data(args->print_args->stream, &args->edt))
       goto fail;
 
     /* Print a blank line if we print anything after this (aka more than one packet). */
@@ -2503,7 +2553,7 @@ print_packet(capture_file *cf, frame_data *fdata,
         args->print_header_line = TRUE;
   } /* if (args->print_args->print_dissections != print_dissections_none) */
 
-  epan_dissect_cleanup(&edt);
+  epan_dissect_reset(&args->edt);
 
   /* do we want to have a formfeed between each packet from now on? */
   if (args->print_args->print_formfeed) {
@@ -2513,7 +2563,7 @@ print_packet(capture_file *cf, frame_data *fdata,
   return TRUE;
 
 fail:
-  epan_dissect_cleanup(&edt);
+  epan_dissect_reset(&args->edt);
   return FALSE;
 }
 
@@ -2528,6 +2578,7 @@ cf_print_packets(capture_file *cf, print_args_t *print_args)
   psp_return_t  ret;
   GList        *clp;
   fmt_data     *cfmt;
+  gboolean      proto_tree_needed;
 
   callback_args.print_args = print_args;
   callback_args.print_header_line = print_args->print_col_headings;
@@ -2541,7 +2592,7 @@ cf_print_packets(capture_file *cf, print_args_t *print_args)
   callback_args.num_visible_cols = 0;
   callback_args.visible_cols = NULL;
 
-  if (!print_preamble(print_args->stream, cf->filename)) {
+  if (!print_preamble(print_args->stream, cf->filename, wireshark_svnversion)) {
     destroy_print_stream(print_args->stream);
     return CF_PRINT_WRITE_ERROR;
   }
@@ -2632,12 +2683,21 @@ cf_print_packets(capture_file *cf, print_args_t *print_args)
     callback_args.line_buf = (char *)g_malloc(callback_args.line_buf_len + 1);
   } /* if (print_summary) */
 
+  /* Create the protocol tree, and make it visible, if we're printing
+     the dissection or the hex data.
+     XXX - do we need it if we're just printing the hex data? */
+  proto_tree_needed =
+      callback_args.print_args->print_dissections != print_dissections_none ||
+      callback_args.print_args->print_hex ||
+      have_custom_cols(&cf->cinfo);
+  epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
+
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range, "Printing",
                                   "selected packets", TRUE, print_packet,
                                   &callback_args);
-
+  epan_dissect_cleanup(&callback_args.edt);
   g_free(callback_args.header_line_buf);
   g_free(callback_args.line_buf);
   g_free(callback_args.col_widths);
@@ -2680,29 +2740,33 @@ cf_print_packets(capture_file *cf, print_args_t *print_args)
   return CF_PRINT_OK;
 }
 
+typedef struct {
+  FILE *fh;
+  epan_dissect_t edt;
+} write_packet_callback_args_t;
+
 static gboolean
 write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
                   struct wtap_pkthdr *phdr, const guint8 *pd,
           void *argsp)
 {
-  FILE           *fh = (FILE *)argsp;
-  epan_dissect_t  edt;
+  write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
 
   /* Create the protocol tree, but don't fill in the column information. */
-  epan_dissect_init(&edt, TRUE, TRUE);
-  epan_dissect_run(&edt, phdr, pd, fdata, NULL);
+  epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, NULL);
 
   /* Write out the information in that tree. */
-  proto_tree_write_pdml(&edt, fh);
+  proto_tree_write_pdml(&args->edt, args->fh);
 
-  epan_dissect_cleanup(&edt);
+  epan_dissect_reset(&args->edt);
 
-  return !ferror(fh);
+  return !ferror(args->fh);
 }
 
 cf_print_status_t
 cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
 {
+  write_packet_callback_args_t callback_args;
   FILE         *fh;
   psp_return_t  ret;
 
@@ -2716,11 +2780,16 @@ cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
     return CF_PRINT_WRITE_ERROR;
   }
 
+  callback_args.fh = fh;
+  epan_dissect_init(&callback_args.edt, cf->epan, TRUE, TRUE);
+
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
                                   "selected packets", TRUE,
-                                  write_pdml_packet, fh);
+                                  write_pdml_packet, &callback_args);
+
+  epan_dissect_cleanup(&callback_args.edt);
 
   switch (ret) {
 
@@ -2755,32 +2824,29 @@ write_psml_packet(capture_file *cf, frame_data *fdata,
                   struct wtap_pkthdr *phdr, const guint8 *pd,
           void *argsp)
 {
-  FILE           *fh = (FILE *)argsp;
-  epan_dissect_t  edt;
-  gboolean        proto_tree_needed;
+  write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
 
-  /* Fill in the column information, only create the protocol tree
-     if having custom columns. */
-  proto_tree_needed = have_custom_cols(&cf->cinfo);
-  epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
-  col_custom_prime_edt(&edt, &cf->cinfo);
-  epan_dissect_run(&edt, phdr, pd, fdata, &cf->cinfo);
-  epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
+  col_custom_prime_edt(&args->edt, &cf->cinfo);
+  epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, &cf->cinfo);
+  epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
 
   /* Write out the information in that tree. */
-  proto_tree_write_psml(&edt, fh);
+  proto_tree_write_psml(&args->edt, args->fh);
 
-  epan_dissect_cleanup(&edt);
+  epan_dissect_reset(&args->edt);
 
-  return !ferror(fh);
+  return !ferror(args->fh);
 }
 
 cf_print_status_t
 cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
 {
+  write_packet_callback_args_t callback_args;
   FILE         *fh;
   psp_return_t  ret;
 
+  gboolean proto_tree_needed;
+
   fh = ws_fopen(print_args->file, "w");
   if (fh == NULL)
     return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
@@ -2791,11 +2857,20 @@ cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
     return CF_PRINT_WRITE_ERROR;
   }
 
+  callback_args.fh = fh;
+
+  /* Fill in the column information, only create the protocol tree
+     if having custom columns. */
+  proto_tree_needed = have_custom_cols(&cf->cinfo);
+  epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
+
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
                                   "selected packets", TRUE,
-                                  write_psml_packet, fh);
+                                  write_psml_packet, &callback_args);
+
+  epan_dissect_cleanup(&callback_args.edt);
 
   switch (ret) {
 
@@ -2830,29 +2905,26 @@ write_csv_packet(capture_file *cf, frame_data *fdata,
                  struct wtap_pkthdr *phdr, const guint8 *pd,
                  void *argsp)
 {
-  FILE           *fh = (FILE *)argsp;
-  epan_dissect_t  edt;
-  gboolean        proto_tree_needed;
+  write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
 
-  /* Fill in the column information, only create the protocol tree
-     if having custom columns. */
-  proto_tree_needed = have_custom_cols(&cf->cinfo);
-  epan_dissect_init(&edt, proto_tree_needed, proto_tree_needed);
-  col_custom_prime_edt(&edt, &cf->cinfo);
-  epan_dissect_run(&edt, phdr, pd, fdata, &cf->cinfo);
-  epan_dissect_fill_in_columns(&edt, FALSE, TRUE);
+  /* Fill in the column information */
+  col_custom_prime_edt(&args->edt, &cf->cinfo);
+  epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, &cf->cinfo);
+  epan_dissect_fill_in_columns(&args->edt, FALSE, TRUE);
 
   /* Write out the information in that tree. */
-  proto_tree_write_csv(&edt, fh);
+  proto_tree_write_csv(&args->edt, args->fh);
 
-  epan_dissect_cleanup(&edt);
+  epan_dissect_reset(&args->edt);
 
-  return !ferror(fh);
+  return !ferror(args->fh);
 }
 
 cf_print_status_t
 cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
 {
+  write_packet_callback_args_t callback_args;
+  gboolean        proto_tree_needed;
   FILE         *fh;
   psp_return_t  ret;
 
@@ -2866,12 +2938,20 @@ cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
     return CF_PRINT_WRITE_ERROR;
   }
 
+  callback_args.fh = fh;
+
+  /* only create the protocol tree if having custom columns. */
+  proto_tree_needed = have_custom_cols(&cf->cinfo);
+  epan_dissect_init(&callback_args.edt, cf->epan, proto_tree_needed, proto_tree_needed);
+
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
                                   "selected packets", TRUE,
                                   write_csv_packet, fh);
 
+  epan_dissect_cleanup(&callback_args.edt);
+
   switch (ret) {
 
   case PSP_FINISHED:
@@ -2905,20 +2985,19 @@ write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
              struct wtap_pkthdr *phdr,
              const guint8 *pd, void *argsp)
 {
-  FILE           *fh = (FILE *)argsp;
-  epan_dissect_t  edt;
+  write_packet_callback_args_t *args = (write_packet_callback_args_t *)argsp;
 
-  epan_dissect_init(&edt, TRUE, TRUE);
-  epan_dissect_run(&edt, phdr, pd, fdata, NULL);
-  proto_tree_write_carrays(fdata->num, fh, &edt);
-  epan_dissect_cleanup(&edt);
+  epan_dissect_run(&args->edt, phdr, frame_tvbuff_new(fdata, pd), fdata, NULL);
+  proto_tree_write_carrays(fdata->num, args->fh, &args->edt);
+  epan_dissect_reset(&args->edt);
 
-  return !ferror(fh);
+  return !ferror(args->fh);
 }
 
 cf_print_status_t
 cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
 {
+  write_packet_callback_args_t callback_args;
   FILE         *fh;
   psp_return_t  ret;
 
@@ -2934,12 +3013,18 @@ cf_write_carrays_packets(capture_file *cf, print_args_t *print_args)
     return CF_PRINT_WRITE_ERROR;
   }
 
+  callback_args.fh = fh;
+  epan_dissect_init(&callback_args.edt, cf->epan, TRUE, TRUE);
+
   /* Iterate through the list of packets, printing the packets we were
      told to print. */
   ret = process_specified_packets(cf, &print_args->range,
                   "Writing C Arrays",
                   "selected packets", TRUE,
-                                  write_carrays_packet, fh);
+                                  write_carrays_packet, &callback_args);
+
+  epan_dissect_cleanup(&callback_args.edt);
+
   switch (ret) {
   case PSP_FINISHED:
     /* Completed successfully. */
@@ -3000,9 +3085,9 @@ match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
   }
 
   /* Construct the protocol tree, including the displayed text */
-  epan_dissect_init(&edt, TRUE, TRUE);
+  epan_dissect_init(&edt, cf->epan, TRUE, TRUE);
   /* We don't need the column information */
-  epan_dissect_run(&edt, &cf->phdr, buffer_start_ptr(&cf->buf), fdata, NULL);
+  epan_dissect_run(&edt, &cf->phdr, frame_tvbuff_new_buffer(fdata, &cf->buf), fdata, NULL);
 
   /* Iterate through all the nodes, seeing if they have text that matches. */
   mdata->cf = cf;
@@ -3104,9 +3189,9 @@ match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
   }
 
   /* Don't bother constructing the protocol tree */
-  epan_dissect_init(&edt, FALSE, FALSE);
+  epan_dissect_init(&edt, cf->epan, FALSE, FALSE);
   /* Get the column information */
-  epan_dissect_run(&edt, &cf->phdr, buffer_start_ptr(&cf->buf), fdata,
+  epan_dissect_run(&edt, &cf->phdr, frame_tvbuff_new_buffer(fdata, &cf->buf), fdata,
                    &cf->cinfo);
 
   /* Find the Info column */
@@ -3412,9 +3497,9 @@ match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
     return MR_ERROR;
   }
 
-  epan_dissect_init(&edt, TRUE, FALSE);
+  epan_dissect_init(&edt, cf->epan, TRUE, FALSE);
   epan_dissect_prime_dfilter(&edt, sfcode);
-  epan_dissect_run(&edt, &cf->phdr, buffer_start_ptr(&cf->buf), fdata, NULL);
+  epan_dissect_run(&edt, &cf->phdr, frame_tvbuff_new_buffer(fdata, &cf->buf), fdata, NULL);
   result = dfilter_apply_edt(sfcode, &edt) ? MR_MATCHED : MR_NOTMATCHED;
   epan_dissect_cleanup(&edt);
   return result;
@@ -3744,10 +3829,10 @@ cf_select_packet(capture_file *cf, int row)
   old_edt = cf->edt;
   /* Create the logical protocol tree. */
   /* We don't need the columns here. */
-  cf->edt = epan_dissect_new(TRUE, TRUE);
+  cf->edt = epan_dissect_new(cf->epan, TRUE, TRUE);
 
   tap_build_interesting(cf->edt);
-  epan_dissect_run(cf->edt, &cf->phdr, buffer_start_ptr(&cf->buf),
+  epan_dissect_run(cf->edt, &cf->phdr, frame_tvbuff_new_buffer(cf->current_frame, &cf->buf),
                    cf->current_frame, NULL);
 
   dfilter_macro_build_ftv_cache(cf->edt->tree);
@@ -3888,25 +3973,82 @@ cf_update_capture_comment(capture_file *cf, gchar *comment)
   cf->unsaved_changes = TRUE;
 }
 
-void
-cf_update_packet_comment(capture_file *cf, frame_data *fdata, gchar *comment)
+static const char *
+cf_get_user_packet_comment(capture_file *cf, const frame_data *fd)
 {
-  if (fdata->opt_comment != NULL) {
-    /* OK, remove the old comment. */
-    g_free(fdata->opt_comment);
-    fdata->opt_comment = NULL;
-    cf->packet_comment_count--;
+  if (cf->frames_user_comments)
+     return (const char *)g_tree_lookup(cf->frames_user_comments, fd);
+
+  /* g_warning? */
+  return NULL;
+}
+
+char *
+cf_get_comment(capture_file *cf, const frame_data *fd)
+{
+  /* fetch user comment */
+  if (fd->flags.has_user_comment)
+    return g_strdup(cf_get_user_packet_comment(cf, fd));
+
+  /* fetch phdr comment */
+  if (fd->flags.has_phdr_comment) {
+    struct wtap_pkthdr phdr; /* Packet header */
+    Buffer buf; /* Packet data */
+
+    phdr.opt_comment = NULL;
+
+    buffer_init(&buf, 1500);
+    if (!cf_read_frame_r(cf, fd, &phdr, &buf))
+      { /* XXX, what we can do here? */ }
+
+    buffer_free(&buf);
+    return phdr.opt_comment;
   }
-  if (comment != NULL) {
-    /* Add the new comment. */
-    fdata->opt_comment = comment;
-    cf->packet_comment_count++;
+  return NULL;
+}
+
+static int
+frame_cmp(gconstpointer a, gconstpointer b, gpointer user_data _U_)
+{
+  const frame_data *fdata1 = (const frame_data *) a;
+  const frame_data *fdata2 = (const frame_data *) b;
+
+  return (fdata1->num < fdata2->num) ? -1 :
+    (fdata1->num > fdata2->num) ? 1 :
+    0;
+}
+
+gboolean
+cf_set_user_packet_comment(capture_file *cf, frame_data *fd, const gchar *new_comment)
+{
+  char *pkt_comment = cf_get_comment(cf, fd);
+
+  /* Check if the comment has changed */
+  if (!g_strcmp0(pkt_comment, new_comment)) {
+    g_free(pkt_comment);
+    return FALSE;
   }
+  g_free(pkt_comment);
+
+  if (pkt_comment)
+    cf->packet_comment_count--;
+
+  if (new_comment)
+    cf->packet_comment_count++;
+
+  fd->flags.has_user_comment = TRUE;
+
+  if (!cf->frames_user_comments)
+    cf->frames_user_comments = g_tree_new_full(frame_cmp, NULL, NULL, g_free);
+
+  /* insert new packet comment */
+  g_tree_replace(cf->frames_user_comments, fd, g_strdup(new_comment));
 
   expert_update_comment_count(cf->packet_comment_count);
 
   /* OK, we have unsaved changes. */
   cf->unsaved_changes = TRUE;
+  return TRUE;
 }
 
 /*
@@ -3946,6 +4088,12 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
   struct wtap_pkthdr    hdr;
   int           err;
   gchar        *display_basename;
+  const char   *pkt_comment;
+
+  if (fdata->flags.has_user_comment)
+    pkt_comment = cf_get_user_packet_comment(cf, fdata);
+  else
+    pkt_comment = phdr->opt_comment;
 
   /* init the wtap header for saving */
   /* TODO: reuse phdr */
@@ -3963,9 +4111,9 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
   hdr.presence_flags = 0;
   if (fdata->flags.has_ts)
     hdr.presence_flags |= WTAP_HAS_TS;
-  if (fdata->flags.has_if_id)
+  if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID)
     hdr.presence_flags |= WTAP_HAS_INTERFACE_ID;
-  if (fdata->flags.has_pack_flags)
+  if (phdr->presence_flags & WTAP_HAS_PACK_FLAGS)
     hdr.presence_flags |= WTAP_HAS_PACK_FLAGS;
   hdr.ts.secs      = fdata->abs_ts.secs;
   hdr.ts.nsecs     = fdata->abs_ts.nsecs;
@@ -3973,10 +4121,11 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
   hdr.len          = fdata->pkt_len;
   hdr.pkt_encap    = fdata->lnk_t;
   /* pcapng */
-  hdr.interface_id = fdata->interface_id;   /* identifier of the interface. */
+  hdr.interface_id = phdr->interface_id;   /* identifier of the interface. */
   /* options */
-  hdr.pack_flags   = fdata->pack_flags;
-  hdr.opt_comment  = fdata->opt_comment; /* NULL if not available */
+  hdr.pack_flags   = phdr->pack_flags;
+  hdr.opt_comment  = g_strdup(pkt_comment);
+
   /* pseudo */
   hdr.pseudo_header = phdr->pseudo_header;
 #if 0
@@ -3996,7 +4145,7 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
          */
         simple_error_message_box(
                       "Frame %u has a network type that can't be saved in a \"%s\" file.",
-                      fdata->num, wtap_file_type_string(args->file_type));
+                      fdata->num, wtap_file_type_subtype_string(args->file_type));
         break;
 
       default:
@@ -4013,6 +4162,8 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
     }
     return FALSE;
   }
+
+  g_free(hdr.opt_comment);
   return TRUE;
 }
 
@@ -4172,7 +4323,7 @@ rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
   /* No user changes yet. */
   cf->unsaved_changes = FALSE;
 
-  cf->cd_t        = wtap_file_type(cf->wth);
+  cf->cd_t        = wtap_file_type_subtype(cf->wth);
   cf->linktypes = g_array_sized_new(FALSE, FALSE, (guint) sizeof(int), 1);
 
   cf->snap      = wtap_snapshot_length(cf->wth);
@@ -4204,8 +4355,6 @@ rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
       progbar_quantum = MIN_QUANTUM;
   }else
     progbar_quantum = 0;
-  /* Progress so far. */
-  progbar_val = 0.0f;
 
   stop_flag = FALSE;
   g_get_current_time(&start_time);
@@ -4282,7 +4431,7 @@ rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
   wtap_sequential_close(cf->wth);
 
   /* compute the time it took to load the file */
-  compute_elapsed(&start_time);
+  compute_elapsed(cf, &start_time);
 
   /* Set the file encapsulation type now; we don't know what it is until
      we've looked at all the packets, as we don't know until then whether
@@ -4363,7 +4512,7 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
   gchar           *fname_new = NULL;
   wtap_dumper     *pdh;
   frame_data      *fdata;
-  struct addrinfo *addrs;
+  addrinfo_lists_t *addr_lists;
   guint            framenum;
   int              err;
 #ifdef _WIN32
@@ -4378,12 +4527,11 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
 
   cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
 
-  addrs = get_addrinfo_list();
+  addr_lists = get_addrinfo_list();
 
   if (save_format == cf->cd_t && compressed == cf->iscompressed
       && !discard_comments && !cf->unsaved_changes
-      && !(addrs && addrs->ai_next &&
-           wtap_dump_has_name_resolution(save_format))) {
+      && !(addr_lists && wtap_dump_has_name_resolution(save_format))) {
     /* We're saving in the format it's already in, and we're
        not discarding comments, and there are no changes we have
        in memory that aren't saved to the file, and we have no name
@@ -4497,7 +4645,7 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
     }
 
     /* Add address resolution */
-    wtap_dump_set_addrinfo_list(pdh, addrs);
+    wtap_dump_set_addrinfo_list(pdh, addr_lists);
 
     /* Iterate through the list of packets, processing all the packets. */
     callback_args.pdh = pdh;
@@ -4634,15 +4782,20 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
       /* Remove SHB comment, if any. */
       wtap_write_shb_comment(cf->wth, NULL);
 
-      /* Remove packet comments. */
+      /* remove all user comments */
       for (framenum = 1; framenum <= cf->count; framenum++) {
         fdata = frame_data_sequence_find(cf->frames, framenum);
-        if (fdata->opt_comment) {
-          g_free(fdata->opt_comment);
-          fdata->opt_comment = NULL;
-          cf->packet_comment_count--;
-        }
+
+        fdata->flags.has_phdr_comment = FALSE;
+        fdata->flags.has_user_comment = FALSE;
       }
+
+      if (cf->frames_user_comments) {
+        g_tree_destroy(cf->frames_user_comments);
+        cf->frames_user_comments = NULL;
+      }
+
+      cf->packet_comment_count = 0;
     }
   }
   return CF_WRITE_OK;
@@ -4834,7 +4987,7 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
       simple_error_message_box(
             "The file \"%s\" is a pipe, and %s capture files can't be "
             "written to a pipe.",
-            display_basename, wtap_file_type_string(file_type));
+            display_basename, wtap_file_type_subtype_string(file_type));
       break;
 
     case WTAP_ERR_UNSUPPORTED_FILE_TYPE: