Replace "svn" with "git" all over the place.
[metze/wireshark/wip.git] / file.c
diff --git a/file.c b/file.c
index e5c75f556e025f10f33dc912b0d087e1b29099d2..87ec088311194393666ba5c157347c8cac60c24a 100644 (file)
--- a/file.c
+++ b/file.c
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #include <fcntl.h>
 #endif
 
-#include <epan/epan.h>
-#include <epan/filesystem.h>
+#include <wsutil/tempfile.h>
+#include <wsutil/file_util.h>
+#include <wsutil/filesystem.h>
 
-#include "color.h"
-#include "color_filters.h"
-#include "cfile.h"
+#include <wiretap/merge.h>
+
+#include <epan/exceptions.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/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"
 
-#ifdef HAVE_LIBPCAP
-gboolean auto_scroll_live;
+#include "version_info.h"
+
+/* Needed for addrinfo */
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
 #endif
 
-static guint32 cum_bytes;
-static nstime_t first_ts;
-static nstime_t prev_dis_ts;
-static nstime_t prev_cap_ts;
+#ifdef HAVE_NETINET_IN_H
+# include <netinet/in.h>
+#endif
 
-static gulong computed_elapsed;
+#ifdef HAVE_NETDB_H
+# include <netdb.h>
+#endif
+
+#ifdef HAVE_WINSOCK2_H
+# include <winsock2.h>
+#endif
+
+#if defined(_WIN32) && defined(INET6)
+# include <ws2tcpip.h>
+#endif
+
+#ifdef HAVE_LIBPCAP
+gboolean auto_scroll_live;
+#endif
 
 static void cf_reset_state(capture_file *cf);
 
-static int read_packet(capture_file *cf, dfilter_t *dfcode,
-    gboolean filtering_tap_listeners, guint tap_flags, 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 refilter, gboolean redissect);
+static void rescan_packets(capture_file *cf, const char *action, const char *action_item, gboolean redissect);
 
 typedef enum {
   MR_NOTMATCHED,
@@ -106,11 +126,11 @@ static match_result match_protocol_tree(capture_file *cf, frame_data *fdata,
 static void match_subtree_text(proto_node *node, gpointer data);
 static match_result match_summary_line(capture_file *cf, frame_data *fdata,
     void *criterion);
-static match_result match_ascii_and_unicode(capture_file *cf, frame_data *fdata,
+static match_result match_narrow_and_wide(capture_file *cf, frame_data *fdata,
     void *criterion);
-static match_result match_ascii(capture_file *cf, frame_data *fdata,
+static match_result match_narrow(capture_file *cf, frame_data *fdata,
     void *criterion);
-static match_result match_unicode(capture_file *cf, frame_data *fdata,
+static match_result match_wide(capture_file *cf, frame_data *fdata,
     void *criterion);
 static match_result match_binary(capture_file *cf, frame_data *fdata,
     void *criterion);
@@ -124,6 +144,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);
@@ -143,7 +165,7 @@ static void ref_time_packets(capture_file *cf);
  */
 typedef struct {
   cf_callback_t cb_fct;
-  gpointer user_data;
+  gpointer      user_data;
 } cf_callback_data_t;
 
 static GList *cf_callbacks = NULL;
@@ -152,13 +174,13 @@ static void
 cf_callback_invoke(int event, gpointer data)
 {
   cf_callback_data_t *cb;
-  GList *cb_item = cf_callbacks;
+  GList              *cb_item = cf_callbacks;
 
   /* there should be at least one interested */
   g_assert(cb_item != NULL);
 
-  while(cb_item != NULL) {
-    cb = cb_item->data;
+  while (cb_item != NULL) {
+    cb = (cf_callback_data_t *)cb_item->data;
     cb->cb_fct(event, data, cb->user_data);
     cb_item = g_list_next(cb_item);
   }
@@ -170,22 +192,22 @@ cf_callback_add(cf_callback_t func, gpointer user_data)
 {
   cf_callback_data_t *cb;
 
-  cb = g_malloc(sizeof(cf_callback_data_t));
+  cb = g_new(cf_callback_data_t,1);
   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
 cf_callback_remove(cf_callback_t func)
 {
   cf_callback_data_t *cb;
-  GList *cb_item = cf_callbacks;
+  GList              *cb_item = cf_callbacks;
 
-  while(cb_item != NULL) {
-    cb = cb_item->data;
-    if(cb->cb_fct == func) {
+  while (cb_item != NULL) {
+    cb = (cf_callback_data_t *)cb_item->data;
+    if (cb->cb_fct == func) {
       cf_callbacks = g_list_remove(cf_callbacks, cb);
       g_free(cb);
       return;
@@ -204,12 +226,12 @@ cf_timestamp_auto_precision(capture_file *cf)
 
 
   /* don't try to get the file's precision if none is opened */
-  if(cf->state == FILE_CLOSED) {
+  if (cf->state == FILE_CLOSED) {
     return;
   }
 
   /* if we are in auto mode, set precision of current file */
-  if(prec == TS_PREC_AUTO ||
+  if (prec == TS_PREC_AUTO ||
      prec == TS_PREC_AUTO_SEC ||
      prec == TS_PREC_AUTO_DSEC ||
      prec == TS_PREC_AUTO_CSEC ||
@@ -244,40 +266,80 @@ cf_timestamp_auto_precision(capture_file *cf)
      "command-line-specified" format. */
   for (i = 0; i < cf->cinfo.num_cols; i++) {
     if (col_has_time_fmt(&cf->cinfo, i)) {
-      new_packet_list_resize_column(i);
+      packet_list_resize_column(i);
     }
   }
 }
 
 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;
 }
 
-static void compute_elapsed(GTimeVal *start_time)
+/*
+ * GLIB_CHECK_VERSION(2,28,0) adds g_get_real_time which could minimize or
+ * replace this
+ */
+static void compute_elapsed(capture_file *cf, GTimeVal *start_time)
 {
-  gdouble    delta_time;
-  GTimeVal   time_now;
+  gdouble  delta_time;
+  GTimeVal time_now;
 
   g_get_current_time(&time_now);
 
   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
 cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
 {
-  wtap       *wth;
-  gchar       *err_info;
+  wtap  *wth;
+  gchar *err_info;
 
   wth = wtap_open_offline(fname, err, &err_info, TRUE);
   if (wth == NULL)
@@ -287,10 +349,14 @@ cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
      and fill in the information for this file. */
   cf_close(cf);
 
-  /* Cleanup all data structures used for dissection. */
-  cleanup_dissection();
-  /* Initialize all data structures used for dissection. */
-  init_dissection();
+  /* XXX - we really want to initialize this after we've read all
+     the packets, so we know how much we'll ultimately need. */
+  buffer_init(&cf->buf, 1500);
+
+  /* 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;
@@ -309,9 +375,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;
@@ -333,18 +399,18 @@ 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);
-  nstime_set_unset(&prev_dis_ts);
-  nstime_set_unset(&prev_cap_ts);
-  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);
   /* XXX needed ? */
-  new_packet_list_queue_draw();
-  fileset_file_opened(fname);
+  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);
   }
@@ -362,7 +428,7 @@ fail:
 /*
  * Add an encapsulation type to cf->linktypes.
  */
-void
+static void
 cf_add_encapsulation_type(capture_file *cf, int encap)
 {
   guint i;
@@ -406,6 +472,9 @@ cf_reset_state(capture_file *cf)
   /* ...which means we have no changes to that file to save. */
   cf->unsaved_changes = FALSE;
 
+  /* Free up the packet buffer. */
+  buffer_free(&cf->buf);
+
   dfilter_free(cf->rfcode);
   cf->rfcode = NULL;
   if (cf->frames != NULL) {
@@ -418,6 +487,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;
@@ -433,9 +506,9 @@ cf_reset_state(capture_file *cf)
   cf->linktypes = NULL;
 
   /* Clear the packet list. */
-  new_packet_list_freeze();
-  new_packet_list_clear();
-  new_packet_list_thaw();
+  packet_list_freeze();
+  packet_list_clear();
+  packet_list_thaw();
 
   cf->f_datalen = 0;
   nstime_set_zero(&cf->elapsed_time);
@@ -444,21 +517,20 @@ cf_reset_state(capture_file *cf)
 
   /* We have no file open. */
   cf->state = FILE_CLOSED;
-
-  fileset_file_closed();
 }
 
 /* Reset everything to a pristine state */
 void
 cf_close(capture_file *cf)
 {
-  if(cf->state != FILE_CLOSED) {
+  if (cf->state != FILE_CLOSED) {
     cf_callback_invoke(cf_cb_file_closing, 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);
   }
@@ -467,7 +539,7 @@ cf_close(capture_file *cf)
 static float
 calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_str, gulong status_size)
 {
-  float   progbar_val;
+  float progbar_val;
 
   progbar_val = (gfloat) file_pos / (gfloat) size;
   if (progbar_val > 1.0) {
@@ -499,27 +571,17 @@ calc_progbar_val(capture_file *cf, gint64 size, gint64 file_pos, gchar *status_s
 cf_read_status_t
 cf_read(capture_file *cf, gboolean reloading)
 {
-  int         err;
-  gchar       *err_info;
-  gchar       *name_ptr;
-  gint64       data_offset;
-  gint64       file_pos;
-  progdlg_t *volatile progbar = NULL;
-  gboolean     stop_flag;
-  volatile gint64 size;
-  volatile float progbar_val;
-  GTimeVal     start_time;
-  gchar        status_str[100];
-  volatile gint64 progbar_nextstep;
-  volatile gint64 progbar_quantum;
-  dfilter_t   *dfcode;
-  gboolean    filtering_tap_listeners;
-  guint       tap_flags;
-  volatile int count = 0;
-#ifdef HAVE_LIBPCAP
-  volatile int displayed_once = 0;
-#endif
-  gboolean compiled;
+  int                  err;
+  gchar               *err_info;
+  gchar               *name_ptr;
+  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;
+  gboolean             compiled;
 
   /* Compile the current display filter.
    * We assume this will not fail since cf->dfilter is only set in
@@ -528,11 +590,10 @@ cf_read(capture_file *cf, gboolean reloading)
   compiled = dfilter_compile(cf->dfilter, &dfcode);
   g_assert(!cf->dfilter || (compiled && dfcode));
 
-  /* Do we have any tap listeners with filters? */
-  filtering_tap_listeners = have_filtering_tap_listeners();
-
   /* Get the union of the flags for all tap listeners. */
   tap_flags = union_of_tap_listener_flags();
+  create_proto_tree =
+    (dfcode != NULL || have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
 
   reset_tap_listeners();
 
@@ -547,105 +608,124 @@ cf_read(capture_file *cf, gboolean reloading)
      XXX - do we know this at open time? */
   cf->iscompressed = wtap_iscompressed(cf->wth);
 
-  /* Find the size of the file. */
-  size = wtap_file_size(cf->wth, NULL);
-
-  /* Update the progress bar when it gets to this value. */
-  progbar_nextstep = 0;
-  /* When we reach the value that triggers a progress bar update,
-     bump that value by this amount. */
-  if (size >= 0){
-    progbar_quantum = size/N_PROGBAR_UPDATES;
-    if (progbar_quantum < MIN_QUANTUM)
-      progbar_quantum = MIN_QUANTUM;
-  }else
-    progbar_quantum = 0;
-  /* Progress so far. */
-  progbar_val = 0.0f;
-
   /* The packet list window will be empty until the file is completly loaded */
-  new_packet_list_freeze();
+  packet_list_freeze();
 
   stop_flag = FALSE;
   g_get_current_time(&start_time);
 
-  while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
-    if (size >= 0) {
-      count++;
-      file_pos = wtap_read_so_far(cf->wth);
+  epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
 
-      /* Create the progress bar if necessary.
-       * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
-       */
-      if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)){
-        progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
-        if (reloading)
-          progbar = delayed_create_progress_dlg("Reloading", name_ptr,
-                                                TRUE, &stop_flag, &start_time, progbar_val);
-        else
-          progbar = delayed_create_progress_dlg("Loading", name_ptr,
-                                                TRUE, &stop_flag, &start_time, progbar_val);
-      }
+  TRY {
+#ifdef HAVE_LIBPCAP
+    int     displayed_once    = 0;
+#endif
+    int     count             = 0;
 
-      /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
-         when we update it, we have to run the GTK+ main loop to get it
-         to repaint what's pending, and doing so may involve an "ioctl()"
-         to see if there's any pending input from an X server, and doing
-         that for every packet can be costly, especially on a big file. */
-      if (file_pos >= progbar_nextstep) {
-        if (progbar != NULL) {
+    gint64  size;
+    gint64  file_pos;
+    gint64  data_offset;
+
+    gint64  progbar_quantum;
+    gint64  progbar_nextstep;
+    float   progbar_val;
+    gchar   status_str[100];
+
+    column_info *cinfo;
+
+    cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
+
+    /* Find the size of the file. */
+    size = wtap_file_size(cf->wth, NULL);
+
+    /* Update the progress bar when it gets to this value. */
+    progbar_nextstep = 0;
+    /* When we reach the value that triggers a progress bar update,
+       bump that value by this amount. */
+    if (size >= 0) {
+      progbar_quantum = size/N_PROGBAR_UPDATES;
+      if (progbar_quantum < MIN_QUANTUM)
+        progbar_quantum = MIN_QUANTUM;
+    }else
+      progbar_quantum = 0;
+
+    while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
+      if (size >= 0) {
+        count++;
+        file_pos = wtap_read_so_far(cf->wth);
+
+        /* Create the progress bar if necessary.
+         * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
+         */
+        if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)) {
           progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
-          /* update the packet bar content on the first run or frequently on very large files */
+          if (reloading)
+            progbar = delayed_create_progress_dlg(cf->window, "Reloading", name_ptr,
+                TRUE, &stop_flag, &start_time, progbar_val);
+          else
+            progbar = delayed_create_progress_dlg(cf->window, "Loading", name_ptr,
+                TRUE, &stop_flag, &start_time, progbar_val);
+        }
+
+        /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
+           when we update it, we have to run the GTK+ main loop to get it
+           to repaint what's pending, and doing so may involve an "ioctl()"
+           to see if there's any pending input from an X server, and doing
+           that for every packet can be costly, especially on a big file. */
+        if (file_pos >= progbar_nextstep) {
+          if (progbar != NULL) {
+            progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
+            /* update the packet bar content on the first run or frequently on very large files */
 #ifdef HAVE_LIBPCAP
-          if (progbar_quantum > 500000 || displayed_once == 0) {
-            if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->count != 0) {
-              displayed_once = 1;
-              packets_bar_update();
+            if (progbar_quantum > 500000 || displayed_once == 0) {
+              if ((auto_scroll_live || displayed_once == 0 || cf->displayed_count < 1000) && cf->count != 0) {
+                displayed_once = 1;
+                packets_bar_update();
+              }
             }
-          }
 #endif /* HAVE_LIBPCAP */
-          update_progress_dlg(progbar, progbar_val, status_str);
+            update_progress_dlg(progbar, progbar_val, status_str);
+          }
+          progbar_nextstep += progbar_quantum;
         }
-        progbar_nextstep += progbar_quantum;
       }
-    }
 
-    if (stop_flag) {
-      /* Well, the user decided to abort the read. He/She will be warned and
-         it might be enough for him/her to work with the already loaded
-         packets.
-         This is especially true for very large capture files, where you don't
-         want to wait loading the whole file (which may last minutes or even
-         hours even on fast machines) just to see that it was the wrong file. */
-      break;
-    }
-    TRY {
-      read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
+      if (stop_flag) {
+        /* Well, the user decided to abort the read. He/She will be warned and
+           it might be enough for him/her to work with the already loaded
+           packets.
+           This is especially true for very large capture files, where you don't
+           want to wait loading the whole file (which may last minutes or even
+           hours even on fast machines) just to see that it was the wrong file. */
+        break;
+      }
+      read_packet(cf, dfcode, &edt, cinfo, data_offset);
     }
-    CATCH(OutOfMemoryError) {
-      simple_message_box(ESD_TYPE_ERROR, NULL,
-                     "Some infos / workarounds can be found at:\n"
-                     "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
-                     "Sorry, but Wireshark has run out of memory and has to terminate now!");
+  }
+  CATCH(OutOfMemoryError) {
+    simple_message_box(ESD_TYPE_ERROR, NULL,
+                   "Some infos / workarounds can be found at:\n"
+                   "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
+                   "Sorry, but Wireshark has run out of memory and has to terminate now!");
 #if 0
-      /* Could we close the current capture and free up memory from that? */
-      break;
+    /* Could we close the current capture and free up memory from that? */
 #else
-      /* we have to terminate, as we cannot recover from the memory error */
-      exit(1);
+    /* we have to terminate, as we cannot recover from the memory error */
+    exit(1);
 #endif
-    }
-    ENDTRY;
   }
+  ENDTRY;
 
   /* Free the display name */
   g_free(name_ptr);
 
   /* Cleanup and release all dfilter resources */
-  if (dfcode != NULL){
+  if (dfcode != NULL) {
     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);
@@ -661,7 +741,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
@@ -672,7 +752,7 @@ cf_read(capture_file *cf, gboolean reloading)
   cf->current_frame = frame_data_sequence_find(cf->frames, cf->first_displayed);
   cf->current_row = 0;
 
-  new_packet_list_thaw();
+  packet_list_thaw();
   if (reloading)
     cf_callback_invoke(cf_cb_file_reload_finished, cf);
   else
@@ -680,11 +760,11 @@ cf_read(capture_file *cf, gboolean reloading)
 
   /* If we have any displayed packets to select, select the first of those
      packets by making the first row the selected row. */
-  if (cf->first_displayed != 0){
-    new_packet_list_select_first_row();
+  if (cf->first_displayed != 0) {
+    packet_list_select_first_row();
   }
 
-  if(stop_flag) {
+  if (stop_flag) {
     simple_message_box(ESD_TYPE_WARN, NULL,
                   "The remaining packets in the file were discarded.\n"
                   "\n"
@@ -752,25 +832,16 @@ 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)
 {
-  gint64 data_offset = 0;
-  gchar *err_info;
-  volatile int newly_displayed_packets = 0;
-  dfilter_t   *dfcode;
-  gboolean filtering_tap_listeners;
-  guint tap_flags;
-  gboolean compiled;
+  gchar            *err_info;
+  int               newly_displayed_packets = 0;
+  dfilter_t        *dfcode;
+  epan_dissect_t       edt;
+  gboolean create_proto_tree;
+  guint             tap_flags;
+  gboolean          compiled;
 
   /* Compile the current display filter.
    * We assume this will not fail since cf->dfilter is only set in
@@ -779,78 +850,85 @@ cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
   compiled = dfilter_compile(cf->dfilter, &dfcode);
   g_assert(!cf->dfilter || (compiled && dfcode));
 
-  /* Do we have any tap listeners with filters? */
-  filtering_tap_listeners = have_filtering_tap_listeners();
-
   /* Get the union of the flags for all tap listeners. */
   tap_flags = union_of_tap_listener_flags();
+  create_proto_tree =
+    (dfcode != NULL || have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
 
   *err = 0;
 
-  new_packet_list_check_end();
+  packet_list_check_end();
   /* Don't freeze/thaw the list when doing live capture */
-  /*new_packet_list_freeze();*/
+  /*packet_list_freeze();*/
 
   /*g_log(NULL, G_LOG_LEVEL_MESSAGE, "cf_continue_tail: %u new: %u", cf->count, to_read);*/
 
-  while (to_read != 0) {
-    wtap_cleareof(cf->wth);
-    if (!wtap_read(cf->wth, err, &err_info, &data_offset)) {
-      break;
-    }
-    if (cf->state == FILE_READ_ABORTED) {
-      /* Well, the user decided to exit Wireshark.  Break out of the
-         loop, and let the code below (which is called even if there
-         aren't any packets left to read) exit. */
-      break;
-    }
-    TRY{
-      if (read_packet(cf, dfcode, filtering_tap_listeners, tap_flags,
-                      data_offset) != -1) {
+  epan_dissect_init(&edt, cf->epan, create_proto_tree, FALSE);
+
+  TRY {
+    gint64 data_offset = 0;
+    column_info *cinfo;
+
+    cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
+
+    while (to_read != 0) {
+      wtap_cleareof(cf->wth);
+      if (!wtap_read(cf->wth, err, &err_info, &data_offset)) {
+        break;
+      }
+      if (cf->state == FILE_READ_ABORTED) {
+        /* Well, the user decided to exit Wireshark.  Break out of the
+           loop, and let the code below (which is called even if there
+           aren't any packets left to read) exit. */
+        break;
+      }
+      if (read_packet(cf, dfcode, &edt, (column_info *) cinfo, data_offset) != -1) {
         newly_displayed_packets++;
       }
+      to_read--;
     }
-    CATCH(OutOfMemoryError) {
-      simple_message_box(ESD_TYPE_ERROR, NULL,
-                     "Some infos / workarounds can be found at:\n"
-                     "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
-                     "Sorry, but Wireshark has run out of memory and has to terminate now!");
+  }
+  CATCH(OutOfMemoryError) {
+    simple_message_box(ESD_TYPE_ERROR, NULL,
+                   "Some infos / workarounds can be found at:\n"
+                   "http://wiki.wireshark.org/KnownBugs/OutOfMemory",
+                   "Sorry, but Wireshark has run out of memory and has to terminate now!");
 #if 0
-      /* Could we close the current capture and free up memory from that? */
-      return CF_READ_ABORTED;
+    /* Could we close the current capture and free up memory from that? */
+    return CF_READ_ABORTED;
 #else
-      /* we have to terminate, as we cannot recover from the memory error */
-      exit(1);
+    /* we have to terminate, as we cannot recover from the memory error */
+    exit(1);
 #endif
-    }
-    ENDTRY;
-    to_read--;
   }
+  ENDTRY;
 
   /* Update the file encapsulation; it might have changed based on the
      packets we've read. */
   cf->lnk_t = wtap_file_encap(cf->wth);
 
   /* Cleanup and release all dfilter resources */
-  if (dfcode != NULL){
+  if (dfcode != NULL) {
     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);*/
 
   /* Don't freeze/thaw the list when doing live capture */
-  /*new_packet_list_thaw();*/
+  /*packet_list_thaw();*/
   /* With the new packet list the first packet
    * isn't automatically selected.
    */
-  if(!cf->current_frame)
-    new_packet_list_select_first_row();
+  if (!cf->current_frame)
+    packet_list_select_first_row();
 
   /* moving to the end of the packet list - if the user requested so and
      we have some new packets. */
   if (newly_displayed_packets && auto_scroll_live && cf->count != 0)
-      new_packet_list_moveto_end();
+      packet_list_moveto_end();
 
   if (cf->state == FILE_READ_ABORTED) {
     /* Well, the user decided to exit Wireshark.  Return CF_READ_ABORTED
@@ -862,8 +940,9 @@ cf_continue_tail(capture_file *cf, volatile int to_read, int *err)
   } else if (*err != 0) {
     /* We got an error reading the capture file.
        XXX - pop up a dialog box instead? */
-    g_warning("Error \"%s\" while reading: \"%s\"\n",
-        wtap_strerror(*err), cf->filename);
+    g_warning("Error \"%s\" while reading: \"%s\" (\"%s\")",
+        wtap_strerror(*err), err_info, cf->filename);
+    g_free(err_info);
 
     return CF_READ_ERROR;
   } else
@@ -878,12 +957,14 @@ cf_fake_continue_tail(capture_file *cf) {
 cf_read_status_t
 cf_finish_tail(capture_file *cf, int *err)
 {
-  gchar *err_info;
-  gint64 data_offset;
-  dfilter_t   *dfcode;
-  gboolean filtering_tap_listeners;
-  guint tap_flags;
-  gboolean compiled;
+  gchar     *err_info;
+  gint64     data_offset;
+  dfilter_t *dfcode;
+  column_info *cinfo;
+  epan_dissect_t edt;
+  gboolean   create_proto_tree;
+  guint      tap_flags;
+  gboolean   compiled;
 
   /* Compile the current display filter.
    * We assume this will not fail since cf->dfilter is only set in
@@ -892,20 +973,22 @@ cf_finish_tail(capture_file *cf, int *err)
   compiled = dfilter_compile(cf->dfilter, &dfcode);
   g_assert(!cf->dfilter || (compiled && dfcode));
 
-  /* Do we have any tap listeners with filters? */
-  filtering_tap_listeners = have_filtering_tap_listeners();
-
   /* Get the union of the flags for all tap listeners. */
   tap_flags = union_of_tap_listener_flags();
+  cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
+  create_proto_tree =
+    (dfcode != NULL || have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
 
-  if(cf->wth == NULL) {
+  if (cf->wth == NULL) {
     cf_close(cf);
     return CF_READ_ERROR;
   }
 
-  new_packet_list_check_end();
+  packet_list_check_end();
   /* Don't freeze/thaw the list when doing live capture */
-  /*new_packet_list_freeze();*/
+  /*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) {
@@ -914,16 +997,18 @@ cf_finish_tail(capture_file *cf, int *err)
          aren't any packets left to read) exit. */
       break;
     }
-    read_packet(cf, dfcode, filtering_tap_listeners, tap_flags, data_offset);
+    read_packet(cf, dfcode, &edt, cinfo, data_offset);
   }
 
   /* Cleanup and release all dfilter resources */
-  if (dfcode != NULL){
+  if (dfcode != NULL) {
     dfilter_free(dfcode);
   }
 
+  epan_dissect_cleanup(&edt);
+
   /* Don't freeze/thaw the list when doing live capture */
-  /*new_packet_list_thaw();*/
+  /*packet_list_thaw();*/
 
   if (cf->state == FILE_READ_ABORTED) {
     /* Well, the user decided to abort the read.  We're only called
@@ -936,7 +1021,7 @@ cf_finish_tail(capture_file *cf, int *err)
   }
 
   if (auto_scroll_live && cf->count != 0)
-    new_packet_list_moveto_end();
+    packet_list_moveto_end();
 
   /* We're done reading sequentially through the file. */
   cf->state = FILE_READ_DONE;
@@ -960,6 +1045,10 @@ cf_finish_tail(capture_file *cf, int *err)
   if (*err != 0) {
     /* We got an error reading the capture file.
        XXX - pop up a dialog box? */
+
+    g_warning("Error \"%s\" while reading: \"%s\" (\"%s\")",
+        wtap_strerror(*err), err_info, cf->filename);
+    g_free(err_info);
     return CF_READ_ERROR;
   } else {
     return CF_READ_OK;
@@ -975,7 +1064,7 @@ cf_get_display_name(capture_file *cf)
   /* Return a name to use in displays */
   if (!cf->is_tempfile) {
     /* Get the last component of the file name, and use that. */
-    if (cf->filename){
+    if (cf->filename) {
       displayname = g_filename_display_basename(cf->filename);
     } else {
       displayname=g_strdup("(No file)");
@@ -1069,88 +1158,50 @@ void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
   cf->rfcode = rfcode;
 }
 
-static void
-find_and_mark_frame_depended_upon(gpointer data, gpointer user_data)
-{
-  frame_data *dependent_fd;
-  guint32 dependent_frame = GPOINTER_TO_UINT(data);
-  capture_file *cf = (capture_file *)user_data;
-
-  dependent_fd = frame_data_sequence_find(cf->frames, dependent_frame);
-  dependent_fd->flags.dependent_of_displayed = 1;
-}
-
 static int
 add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
-    dfilter_t *dfcode, gboolean filtering_tap_listeners,
-    guint tap_flags,
-    union wtap_pseudo_header *pseudo_header, const guchar *buf,
-    gboolean refilter,
-    gboolean add_to_packet_list)
+    epan_dissect_t *edt, dfilter_t *dfcode, column_info *cinfo,
+    struct wtap_pkthdr *phdr, const guint8 *buf, gboolean add_to_packet_list)
 {
-  gboolean  create_proto_tree = FALSE;
-  epan_dissect_t edt;
-  column_info *cinfo;
-  gint row = -1;
-
-  cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
+  gint            row               = -1;
 
   frame_data_set_before_dissect(fdata, &cf->elapsed_time,
-                                &first_ts, &prev_dis_ts, &prev_cap_ts);
-
-  /* If either
-    + we have a display filter and are re-applying it;
-    + we have tap listeners with filters;
-    + we have tap listeners that require a protocol tree;
-
-     allocate a protocol tree root node, so that we'll construct
-     a protocol tree against which a filter expression can be
-     evaluated. */
-  if ((dfcode != NULL && refilter) ||
-      filtering_tap_listeners || (tap_flags & TL_REQUIRES_PROTO_TREE))
-      create_proto_tree = TRUE;
+                                &cf->ref, cf->prev_dis);
+  cf->prev_cap = fdata;
 
-  /* Dissect the frame. */
-  epan_dissect_init(&edt, create_proto_tree, FALSE);
-
-  if (dfcode != NULL && refilter) {
-      epan_dissect_prime_dfilter(&edt, dfcode);
+  if (dfcode != NULL) {
+      epan_dissect_prime_dfilter(edt, dfcode);
   }
 
-  tap_queue_init(&edt);
-  epan_dissect_run(&edt, pseudo_header, buf, fdata, cinfo);
-  tap_push_tapped_queue(&edt);
-
-  /* If we have a display filter, apply it if we're refiltering, otherwise
-     leave the "passed_dfilter" flag alone.
+  /* 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 we don't have a display filter, set "passed_dfilter" to 1. */
   if (dfcode != NULL) {
-    if (refilter) {
-      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);
-      }
+    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);
     }
   } else
     fdata->flags.passed_dfilter = 1;
 
-  if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
+  if (fdata->flags.passed_dfilter || fdata->flags.ref_time)
     cf->displayed_count++;
 
   if (add_to_packet_list) {
     /* We fill the needed columns from new_packet_list */
-      row = new_packet_list_append(cinfo, fdata, &edt.pi);
+      row = packet_list_append(cinfo, fdata);
   }
 
-  if(fdata->flags.passed_dfilter || fdata->flags.ref_time)
+  if (fdata->flags.passed_dfilter || fdata->flags.ref_time)
   {
-    frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
+    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.
 
@@ -1172,28 +1223,27 @@ 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 filtering_tap_listeners, guint tap_flags, gint64 offset)
+read_packet(capture_file *cf, dfilter_t *dfcode, epan_dissect_t *edt,
+            column_info *cinfo, gint64 offset)
 {
-  const struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
-  union wtap_pseudo_header *pseudo_header = wtap_pseudoheader(cf->wth);
-  const guchar *buf = wtap_buf_ptr(cf->wth);
+  struct wtap_pkthdr *phdr = wtap_phdr(cf->wth);
+  const guint8 *buf = wtap_buf_ptr(cf->wth);
   frame_data    fdlocal;
   guint32       framenum;
   frame_data   *fdata;
-  int           passed;
+  gboolean      passed;
   int           row = -1;
 
   /* Add this packet's link-layer encapsulation type to cf->linktypes, if
      it's not already there.
-     XXX - yes, this is O(N), so if every packet had a different 
+     XXX - yes, this is O(N), so if every packet had a different
      link-layer encapsulation type, it'd be O(N^2) to read the file, but
      there are probably going to be a small number of encapsulation types
      in a file. */
@@ -1203,16 +1253,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, pseudo_header, 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) {
@@ -1220,14 +1271,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,
-                                      filtering_tap_listeners, tap_flags,
-                                      pseudo_header, buf, TRUE, TRUE);
+      row = add_packet_to_packet_list(fdata, cf, edt, dfcode,
+                                      cinfo, phdr, buf, TRUE);
     }
   }
 
@@ -1238,26 +1288,28 @@ cf_status_t
 cf_merge_files(char **out_filenamep, int in_file_count,
                char *const *in_filenames, int file_type, gboolean do_append)
 {
-  merge_in_file_t  *in_files, *in_file;
-  char             *out_filename;
-  char             *tmpname;
-  int               out_fd;
-  wtap_dumper      *pdh;
-  int               open_err, read_err, write_err, close_err;
-  gchar            *err_info;
-  int               err_fileno;
-  int               i;
-  gboolean          got_read_error = FALSE, got_write_error = FALSE;
-  gint64            data_offset;
-  progdlg_t        *progbar = NULL;
-  gboolean          stop_flag;
-  gint64            f_len, file_pos;
-  float             progbar_val;
-  GTimeVal          start_time;
-  gchar             status_str[100];
-  gint64            progbar_nextstep;
-  gint64            progbar_quantum;
-  gchar            *display_basename;
+  merge_in_file_t *in_files, *in_file;
+  char            *out_filename;
+  char            *tmpname;
+  int              out_fd;
+  wtap_dumper     *pdh;
+  int              open_err, read_err, write_err, close_err;
+  gchar           *err_info;
+  int              err_fileno;
+  int              i;
+  gboolean         got_read_error     = FALSE, got_write_error = FALSE;
+  gint64           data_offset;
+  progdlg_t       *progbar            = NULL;
+  gboolean         stop_flag;
+  gint64           f_len, file_pos;
+  float            progbar_val;
+  GTimeVal         start_time;
+  gchar            status_str[100];
+  gint64           progbar_nextstep;
+  gint64           progbar_quantum;
+  gchar           *display_basename;
+  int              selected_frame_type;
+  gboolean         fake_interface_ids = FALSE;
 
   /* open the input files */
   if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
@@ -1288,17 +1340,107 @@ cf_merge_files(char **out_filenamep, int in_file_count,
     return CF_ERROR;
   }
 
-  pdh = wtap_dump_fdopen(out_fd, file_type,
-      merge_select_frame_type(in_file_count, in_files),
-      merge_max_snapshot_length(in_file_count, in_files),
-      FALSE /* compressed */, &open_err);
-  if (pdh == NULL) {
-    ws_close(out_fd);
-    merge_close_in_files(in_file_count, in_files);
-    g_free(in_files);
-    cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
-                              file_type);
-    return CF_ERROR;
+  selected_frame_type = merge_select_frame_type(in_file_count, in_files);
+
+  /* If we are trying to merge a number of libpcap files with different encapsulation types
+   * change the output file type to pcapng and create SHB and IDB:s for the new file use the
+   * interface index stored in in_files per file to change the phdr before writing the datablock.
+   * XXX should it be an option to convert to pcapng?
+   *
+   * 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_TYPE_SUBTYPE_PCAP)) {
+    /* Write output in pcapng format */
+    wtapng_section_t            *shb_hdr;
+    wtapng_iface_descriptions_t *idb_inf, *idb_inf_merge_file;
+    wtapng_if_descr_t            int_data, *file_int_data;
+    GString                     *comment_gstr;
+
+    fake_interface_ids = TRUE;
+    /* Create SHB info */
+    shb_hdr      = wtap_file_get_shb_info(in_files[0].wth);
+    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_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);
+    }
+    shb_hdr->section_length = -1;
+    /* options */
+    shb_hdr->opt_comment   = g_string_free(comment_gstr, FALSE);  /* NULL if not available */
+    shb_hdr->shb_hardware  = NULL;        /* NULL if not available, UTF-8 string containing the        */
+                                          /*  description of the hardware used to create this section. */
+    shb_hdr->shb_os        = NULL;        /* NULL if not available, UTF-8 string containing the name   */
+                                          /*  of the operating system used to create this section.     */
+    shb_hdr->shb_user_appl = "Wireshark"; /* NULL if not available, UTF-8 string containing the name   */
+                                          /*  of the application used to create this section.          */
+
+    /* create fake IDB info */
+    idb_inf = g_new(wtapng_iface_descriptions_t,1);
+    idb_inf->number_of_interfaces = in_file_count; /* TODO make this the number of DIFFERENT encapsulation types
+                                                    * check that snaplength is the same too?
+                                                    */
+    idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
+
+    for (i = 0; i < in_file_count; i++) {
+      idb_inf_merge_file               = wtap_file_get_idb_info(in_files[i].wth);
+      /* read the interface data from the in file to our combined interfca data */
+      file_int_data = &g_array_index (idb_inf_merge_file->interface_data, wtapng_if_descr_t, 0);
+      int_data.wtap_encap            = file_int_data->wtap_encap;
+      int_data.time_units_per_second = file_int_data->time_units_per_second;
+      int_data.link_type             = file_int_data->link_type;
+      int_data.snap_len              = file_int_data->snap_len;
+      int_data.if_name               = g_strdup(file_int_data->if_name);
+      int_data.opt_comment           = NULL;
+      int_data.if_description        = NULL;
+      int_data.if_speed              = 0;
+      int_data.if_tsresol            = 6;
+      int_data.if_filter_str         = NULL;
+      int_data.bpf_filter_len        = 0;
+      int_data.if_filter_bpf_bytes   = NULL;
+      int_data.if_os                 = NULL;
+      int_data.if_fcslen             = -1;
+      int_data.num_stat_entries      = 0;          /* Number of ISB:s */
+      int_data.interface_statistics  = NULL;
+
+      g_array_append_val(idb_inf->interface_data, int_data);
+      g_free(idb_inf_merge_file);
+
+      /* Set fake interface Id in per file data */
+      in_files[i].interface_id = i;
+    }
+
+    pdh = wtap_dump_fdopen_ng(out_fd, file_type,
+                              selected_frame_type,
+                              merge_max_snapshot_length(in_file_count, in_files),
+                              FALSE /* compressed */, shb_hdr, idb_inf /* wtapng_iface_descriptions_t *idb_inf */, &open_err);
+
+    if (pdh == NULL) {
+      ws_close(out_fd);
+      merge_close_in_files(in_file_count, in_files);
+      g_free(in_files);
+      cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
+                                file_type);
+      return CF_ERROR;
+    }
+
+  } else {
+
+    pdh = wtap_dump_fdopen(out_fd, file_type,
+                           selected_frame_type,
+                           merge_max_snapshot_length(in_file_count, in_files),
+                           FALSE /* compressed */, &open_err);
+    if (pdh == NULL) {
+      ws_close(out_fd);
+      merge_close_in_files(in_file_count, in_files);
+      g_free(in_files);
+      cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
+                                file_type);
+      return CF_ERROR;
+    }
   }
 
   /* Get the sum of the sizes of all the files. */
@@ -1347,7 +1489,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
        large file, we might take considerably longer than that standard
        time in order to get to the next progress bar step). */
     if (progbar == NULL) {
-      progbar = delayed_create_progress_dlg("Merging", "files",
+      progbar = delayed_create_progress_dlg(NULL, "Merging", "files",
         FALSE, &stop_flag, &start_time, progbar_val);
     }
 
@@ -1382,8 +1524,20 @@ cf_merge_files(char **out_filenamep, int in_file_count,
       break;
     }
 
-    if (!wtap_dump(pdh, wtap_phdr(in_file->wth), wtap_pseudoheader(in_file->wth),
-         wtap_buf_ptr(in_file->wth), &write_err)) {
+    /* 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;
+
+      phdr = wtap_phdr(in_file->wth);
+      phdr->interface_id = in_file->interface_id;
+      phdr->presence_flags = phdr->presence_flags | WTAP_HAS_INTERFACE_ID;
+    }
+    if (!wtap_dump(pdh, wtap_phdr(in_file->wth),
+                   wtap_buf_ptr(in_file->wth), &write_err)) {
       got_write_error = TRUE;
       break;
     }
@@ -1394,11 +1548,18 @@ cf_merge_files(char **out_filenamep, int in_file_count,
     destroy_progress_dlg(progbar);
 
   merge_close_in_files(in_file_count, in_files);
-  if (!got_read_error && !got_write_error) {
+  if (!got_write_error) {
     if (!wtap_dump_close(pdh, &write_err))
       got_write_error = TRUE;
-  } else
-    wtap_dump_close(pdh, &close_err);
+  } else {
+    /*
+     * We already got a write error; no need to report another
+     * write error on close.
+     *
+     * Don't overwrite the earlier write error.
+     */
+    (void)wtap_dump_close(pdh, &close_err);
+  }
 
   if (got_read_error) {
     /*
@@ -1464,14 +1625,29 @@ cf_merge_files(char **out_filenamep, int in_file_count,
 
       case WTAP_ERR_UNSUPPORTED_ENCAP:
         /*
-         * This is a problem with the particular frame we're writing;
-         * note that, and give the frame number.
+         * This is a problem with the particular frame we're writing and
+         * the file type and subtype we're writing; note that, and report
+         * the frame number and file type/subtype.
          */
         display_basename = g_filename_display_basename(in_file->filename);
         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;
+
+      case WTAP_ERR_PACKET_TOO_LARGE:
+        /*
+         * This is a problem with the particular frame we're writing and
+         * the file type and subtype we're writing; note that, and report
+         * the frame number and file type/subtype.
+         */
+        display_basename = g_filename_display_basename(in_file->filename);
+        simple_error_message_box(
+                      "Frame %u of \"%s\" is too large for a \"%s\" file.",
+                      in_file->packet_num, display_basename,
+                      wtap_file_type_subtype_string(file_type));
         g_free(display_basename);
         break;
 
@@ -1503,8 +1679,8 @@ cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
 {
   const char *filter_new = dftext ? dftext : "";
   const char *filter_old = cf->dfilter ? cf->dfilter : "";
-  dfilter_t   *dfcode;
-  GTimeVal     start_time;
+  dfilter_t  *dfcode;
+  GTimeVal    start_time;
 
   /* if new filter equals old one, do nothing unless told to do so */
   if (!force && strcmp(filter_new, filter_old) == 0) {
@@ -1525,14 +1701,10 @@ cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
     dftext = g_strdup(dftext);
     if (!dfilter_compile(dftext, &dfcode)) {
       /* The attempt failed; report an error. */
-      gchar *safe_dftext = simple_dialog_format_message(dftext);
-      gchar *safe_dfilter_error_msg = simple_dialog_format_message(dfilter_error_msg);
       simple_message_box(ESD_TYPE_ERROR, NULL,
           "See the help for a description of the display filter syntax.",
           "\"%s\" isn't a valid display filter: %s",
-          safe_dftext, safe_dfilter_error_msg);
-      g_free(safe_dfilter_error_msg);
-      g_free(safe_dftext);
+          dftext, dfilter_error_msg);
       g_free(dftext);
       return CF_ERROR;
     }
@@ -1554,9 +1726,9 @@ cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
   /* Now rescan the packet list, applying the new filter, but not
      throwing away information constructed on a previous pass. */
   if (dftext == NULL) {
-    rescan_packets(cf, "Resetting", "Filter", TRUE, FALSE);
+    rescan_packets(cf, "Resetting", "Filter", FALSE);
   } else {
-    rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
+    rescan_packets(cf, "Filtering", dftext, FALSE);
   }
 
   /* Cleanup and release all dfilter resources */
@@ -1568,21 +1740,22 @@ cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
 void
 cf_reftime_packets(capture_file *cf)
 {
-
   ref_time_packets(cf);
 }
 
 void
 cf_redissect_packets(capture_file *cf)
 {
-  rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
+  if (cf->state != FILE_CLOSED) {
+    rescan_packets(cf, "Reprocessing", "all packets", TRUE);
+  }
 }
 
 gboolean
-cf_read_frame_r(capture_file *cf, frame_data *fdata,
-                union wtap_pseudo_header *pseudo_header, guint8 *pd)
+cf_read_frame_r(capture_file *cf, const frame_data *fdata,
+                struct wtap_pkthdr *phdr, Buffer *buf)
 {
-  int err;
+  int    err;
   gchar *err_info;
   gchar *display_basename;
 
@@ -1596,14 +1769,14 @@ cf_read_frame_r(capture_file *cf, frame_data *fdata,
       return FALSE;
     }
 
-    *pseudo_header = frame->ph;
-    memcpy(pd, frame->pd, fdata->cap_len);
+    *phdr = frame->phdr;
+    buffer_assure_space(buf, frame->phdr.caplen);
+    memcpy(buffer_start_ptr(buf), frame->pd, frame->phdr.caplen);
     return TRUE;
   }
 #endif
 
-  if (!wtap_seek_read(cf->wth, fdata->file_off, pseudo_header, pd,
-                      fdata->cap_len, &err, &err_info)) {
+  if (!wtap_seek_read(cf->wth, fdata->file_off, phdr, buf, &err, &err_info)) {
     display_basename = g_filename_display_basename(cf->filename);
     switch (err) {
 
@@ -1634,7 +1807,7 @@ cf_read_frame_r(capture_file *cf, frame_data *fdata,
 gboolean
 cf_read_frame(capture_file *cf, frame_data *fdata)
 {
-  return cf_read_frame_r(cf, fdata, &cf->pseudo_header, cf->pd);
+  return cf_read_frame_r(cf, fdata, &cf->phdr, &cf->buf);
 }
 
 /* Rescan the list of packets, reconstructing the CList.
@@ -1645,15 +1818,12 @@ cf_read_frame(capture_file *cf, frame_data *fdata)
    "action_item" describes what we're doing; it's used in the progress
    dialog box.
 
-   "refilter" is TRUE if we need to re-evaluate the filter expression.
-
    "redissect" is TRUE if we need to make the dissectors reconstruct
    any state information they have (because a preference that affects
    some dissector has changed, meaning some dissector might construct
    its state differently from the way it was constructed the last time). */
 static void
-rescan_packets(capture_file *cf, const char *action, const char *action_item,
-        gboolean refilter, gboolean redissect)
+rescan_packets(capture_file *cf, const char *action, const char *action_item, gboolean redissect)
 {
   /* Rescan packets new packet list */
   guint32     framenum;
@@ -1669,11 +1839,14 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
   gchar       status_str[100];
   int         progbar_nextstep;
   int         progbar_quantum;
-  dfilter_t   *dfcode;
-  gboolean    filtering_tap_listeners;
+  epan_dissect_t  edt;
+  dfilter_t  *dfcode;
+  column_info *cinfo;
+  gboolean    create_proto_tree;
   guint       tap_flags;
   gboolean    add_to_packet_list = FALSE;
-  gboolean compiled;
+  gboolean    compiled;
+  guint32     frames_count;
 
   /* Compile the current display filter.
    * We assume this will not fail since cf->dfilter is only set in
@@ -1682,11 +1855,11 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
   compiled = dfilter_compile(cf->dfilter, &dfcode);
   g_assert(!cf->dfilter || (compiled && dfcode));
 
-  /* Do we have any tap listeners with filters? */
-  filtering_tap_listeners = have_filtering_tap_listeners();
-
   /* Get the union of the flags for all tap listeners. */
   tap_flags = union_of_tap_listener_flags();
+  cinfo = (tap_flags & TL_REQUIRES_COLUMNS) ? &cf->cinfo : NULL;
+  create_proto_tree =
+    (dfcode != NULL || have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
 
   reset_tap_listeners();
   /* Which frame, if any, is the currently selected frame?
@@ -1700,7 +1873,7 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
 
   /* Freeze the packet list while we redo it, so we don't get any
      screen updates while it happens. */
-  new_packet_list_freeze();
+  packet_list_freeze();
 
   if (redissect) {
     /* We need to re-initialize all the state information that protocols
@@ -1712,14 +1885,13 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
        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. */
-    new_packet_list_clear();
+    packet_list_clear();
     add_to_packet_list = TRUE;
   }
 
@@ -1733,10 +1905,10 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
   /* 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);
-  nstime_set_unset(&prev_dis_ts);
-  nstime_set_unset(&prev_cap_ts);
-  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;
@@ -1762,7 +1934,11 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
 
   selected_frame_seen = FALSE;
 
-  for (framenum = 1; framenum <= cf->count; framenum++) {
+  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);
 
     /* Create the progress bar if necessary.
@@ -1771,7 +1947,7 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
        large file, we might take considerably longer than that standard
        time in order to get to the next progress bar step). */
     if (progbar == NULL)
-      progbar = delayed_create_progress_dlg(action, action_item, TRUE,
+      progbar = delayed_create_progress_dlg(cf->window, action, action_item, TRUE,
                                             &stop_flag, &start_time,
                                             progbar_val);
 
@@ -1785,11 +1961,11 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
        * with count == 0, so let's assert that
        */
       g_assert(cf->count > 0);
-      progbar_val = (gfloat) count / cf->count;
+      progbar_val = (gfloat) count / frames_count;
 
       if (progbar != NULL) {
         g_snprintf(status_str, sizeof(status_str),
-                  "%4u of %u frames", count, cf->count);
+                  "%4u of %u frames", count, frames_count);
         update_progress_dlg(progbar, progbar_val, status_str);
       }
 
@@ -1819,16 +1995,12 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
        * as not visited, free the GSList referring to the state
        * data (the per-frame data itself was freed by
        * "init_dissection()"), and null out the GSList pointer. */
-      fdata->flags.visited = 0;
-      frame_data_cleanup(fdata);
+      frame_data_reset(fdata);
+      frames_count = cf->count;
     }
 
-    if (redissect || refilter) {
-      /* If we're redissecting or refiltering then any frame dependencies 
-       * from the previous dissection/filtering are no longer valid.
-       */
-      fdata->flags.dependent_of_displayed = 0;
-    }
+    /* Frame dependencies from the previous dissection/filtering are no longer valid. */
+    fdata->flags.dependent_of_displayed = 0;
 
     if (!cf_read_frame(cf, fdata))
       break; /* error reading the frame */
@@ -1840,9 +2012,10 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
       preceding_frame_num = prev_frame_num;
       preceding_frame = prev_frame;
     }
-    add_packet_to_packet_list(fdata, cf, dfcode, filtering_tap_listeners,
-                                    tap_flags, &cf->pseudo_header, cf->pd,
-                                    refilter,
+
+    add_packet_to_packet_list(fdata, cf, &edt, dfcode,
+                                    cinfo, &cf->phdr,
+                                    buffer_start_ptr(&cf->buf),
                                     add_to_packet_list);
 
     /* If this frame is displayed, and this is the first frame we've
@@ -1865,10 +2038,13 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
     prev_frame = fdata;
   }
 
+  epan_dissect_cleanup(&edt);
+
   /* We are done redissecting the packet list. */
   cf->redissecting = FALSE;
 
   if (redissect) {
+      frames_count = cf->count;
     /* Clear out what remains of the visited flags and per-frame data
        pointers.
 
@@ -1878,10 +2054,9 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
        even though the user requested that the scan stop, and that
        would leave the user stuck with an Wireshark grinding on
        until it finishes.  Should we just stick them with that? */
-    for (; framenum <= cf->count; framenum++) {
+    for (; framenum <= frames_count; framenum++) {
       fdata = frame_data_sequence_find(cf->frames, framenum);
-      fdata->flags.visited = 0;
-      frame_data_cleanup(fdata);
+      frame_data_reset(fdata);
     }
   }
 
@@ -1892,12 +2067,12 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
 
   /* Unfreeze the packet list. */
   if (!add_to_packet_list)
-    new_packet_list_recreate_visible_rows();
+    packet_list_recreate_visible_rows();
 
   /* Compute the time it took to filter the file */
-  compute_elapsed(&start_time);
+  compute_elapsed(cf, &start_time);
 
-  new_packet_list_thaw();
+  packet_list_thaw();
 
   if (selected_frame_num == -1) {
     /* The selected frame didn't pass the filter. */
@@ -1945,9 +2120,9 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
     /* Set to invalid to force update of packet list and packet details */
     cf->current_row = -1;
     if (selected_frame_num == 0) {
-      new_packet_list_select_first_row();
+      packet_list_select_first_row();
     }else{
-      if (!new_packet_list_select_row_from_data(selected_frame)) {
+      if (!packet_list_select_row_from_data(selected_frame)) {
         /* We didn't find a row corresponding to this frame.
            This means that the frame isn't being displayed currently,
            so we can't select it. */
@@ -1971,18 +2146,19 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
 static void
 ref_time_packets(capture_file *cf)
 {
-  guint32 framenum;
+  guint32     framenum;
   frame_data *fdata;
+  nstime_t rel_ts;
 
-  nstime_set_unset(&first_ts);
-  nstime_set_unset(&prev_dis_ts);
-  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
@@ -1991,55 +2167,54 @@ 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 (nstime_is_unset(&prev_dis_ts)) {
-        prev_dis_ts = fdata->abs_ts;
+    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 ) {
-        nstime_delta(&fdata->del_dis_ts, &fdata->abs_ts, &prev_dis_ts);
-        prev_dis_ts = fdata->abs_ts;
+    if ( fdata->flags.passed_dfilter ) {
+        fdata->prev_dis_num = cf->prev_dis->num;
+        cf->prev_dis = fdata;
     }
 
     /*
      * Byte counts
      */
-    if( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ){
+    if ( (fdata->flags.passed_dfilter) || (fdata->flags.ref_time) ) {
         /* This frame either passed the display filter list or is marked as
         a time reference frame.  All time reference frames are displayed
         even if they dont pass the display filter */
-        if(fdata->flags.ref_time){
+        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;
         }
     }
   }
@@ -2055,24 +2230,27 @@ static psp_return_t
 process_specified_packets(capture_file *cf, packet_range_t *range,
     const char *string1, const char *string2, gboolean terminate_is_stop,
     gboolean (*callback)(capture_file *, frame_data *,
-                         union wtap_pseudo_header *, const guint8 *, void *),
+                         struct wtap_pkthdr *, const guint8 *, void *),
     void *callback_args)
 {
-  guint32 framenum;
-  frame_data *fdata;
-  union wtap_pseudo_header pseudo_header;
-  guint8      pd[WTAP_MAX_PACKET_SIZE+1];
-  psp_return_t ret = PSP_FINISHED;
-
-  progdlg_t  *progbar = NULL;
-  int         progbar_count;
-  float       progbar_val;
-  gboolean    progbar_stop_flag;
-  GTimeVal    progbar_start_time;
-  gchar       progbar_status_str[100];
-  int         progbar_nextstep;
-  int         progbar_quantum;
-  range_process_e process_this;
+  guint32          framenum;
+  frame_data      *fdata;
+  Buffer           buf;
+  psp_return_t     ret     = PSP_FINISHED;
+
+  progdlg_t       *progbar = NULL;
+  int              progbar_count;
+  float            progbar_val;
+  gboolean         progbar_stop_flag;
+  GTimeVal         progbar_start_time;
+  gchar            progbar_status_str[100];
+  int              progbar_nextstep;
+  int              progbar_quantum;
+  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. */
   progbar_nextstep = 0;
@@ -2101,7 +2279,7 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
        large file, we might take considerably longer than that standard
        time in order to get to the next progress bar step). */
     if (progbar == NULL)
-      progbar = delayed_create_progress_dlg(string1, string2,
+      progbar = delayed_create_progress_dlg(cf->window, string1, string2,
                                             terminate_is_stop,
                                             &progbar_stop_flag,
                                             &progbar_start_time,
@@ -2151,13 +2329,13 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
     }
 
     /* Get the packet */
-    if (!cf_read_frame_r(cf, fdata, &pseudo_header, pd)) {
+    if (!cf_read_frame_r(cf, fdata, &phdr, &buf)) {
       /* Attempt to get the packet failed. */
       ret = PSP_FAILED;
       break;
     }
     /* Process the packet */
-    if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
+    if (!callback(cf, fdata, &phdr, buffer_start_ptr(&buf), callback_args)) {
       /* Callback failed.  We assume it reported the error appropriately. */
       ret = PSP_FAILED;
       break;
@@ -2169,27 +2347,25 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
   if (progbar != NULL)
     destroy_progress_dlg(progbar);
 
+  buffer_free(&buf);
+
   return ret;
 }
 
 typedef struct {
-  gboolean construct_protocol_tree;
+  epan_dissect_t edt;
   column_info *cinfo;
 } retap_callback_args_t;
 
 static gboolean
 retap_packet(capture_file *cf _U_, frame_data *fdata,
-             union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+             struct wtap_pkthdr *phdr, const guint8 *pd,
              void *argsp)
 {
-  retap_callback_args_t *args = argsp;
-  epan_dissect_t edt;
+  retap_callback_args_t *args = (retap_callback_args_t *)argsp;
 
-  epan_dissect_init(&edt, args->construct_protocol_tree, FALSE);
-  tap_queue_init(&edt);
-  epan_dissect_run(&edt, pseudo_header, pd, fdata, args->cinfo);
-  tap_push_tapped_queue(&edt);
-  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;
 }
@@ -2197,10 +2373,12 @@ retap_packet(capture_file *cf _U_, frame_data *fdata,
 cf_read_status_t
 cf_retap_packets(capture_file *cf)
 {
-  packet_range_t range;
+  packet_range_t        range;
   retap_callback_args_t callback_args;
-  gboolean filtering_tap_listeners;
-  guint tap_flags;
+  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();
@@ -2209,8 +2387,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;
@@ -2218,13 +2396,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);
+  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;
@@ -2253,39 +2438,33 @@ typedef struct {
   char         *line_buf;
   int           line_buf_len;
   gint         *col_widths;
+  int           num_visible_cols;
+  gint         *visible_cols;
+  epan_dissect_t edt;
 } print_callback_args_t;
 
 static gboolean
 print_packet(capture_file *cf, frame_data *fdata,
-             union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+             struct wtap_pkthdr *phdr, const guint8 *pd,
              void *argsp)
 {
-  print_callback_args_t *args = argsp;
-  epan_dissect_t edt;
+  print_callback_args_t *args = (print_callback_args_t *)argsp;
   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);
+  char            bookmark_name[9+10+1];  /* "__frameNNNNNNNNNN__\0" */
+  char            bookmark_title[6+10+1]; /* "Frame NNNNNNNNNN__\0"  */
 
   /* 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, pseudo_header, 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, pseudo_header, 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))
@@ -2304,6 +2483,8 @@ print_packet(capture_file *cf, frame_data *fdata,
   g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
 
   if (args->print_args->print_summary) {
+    if (!args->print_args->print_col_headings)
+        args->print_header_line = FALSE;
     if (args->print_header_line) {
       if (!print_line(args->print_args->stream, 0, args->header_line_buf))
         goto fail;
@@ -2311,9 +2492,9 @@ print_packet(capture_file *cf, frame_data *fdata,
     }
     cp = &args->line_buf[0];
     line_len = 0;
-    for (i = 0; i < cf->cinfo.num_cols; i++) {
+    for (i = 0; i < args->num_visible_cols; i++) {
       /* Find the length of the string for this column. */
-      column_len = (int) strlen(cf->cinfo.col_data[i]);
+      column_len = (int) strlen(cf->cinfo.col_data[args->visible_cols[i]]);
       if (args->col_widths[i] > column_len)
          column_len = args->col_widths[i];
 
@@ -2323,17 +2504,17 @@ print_packet(capture_file *cf, frame_data *fdata,
       if (line_len > args->line_buf_len) {
         cp_off = (int) (cp - args->line_buf);
         args->line_buf_len = 2 * line_len;
-        args->line_buf = g_realloc(args->line_buf, args->line_buf_len + 1);
+        args->line_buf = (char *)g_realloc(args->line_buf, args->line_buf_len + 1);
         cp = args->line_buf + cp_off;
       }
 
       /* Right-justify the packet number column. */
-      if (cf->cinfo.col_fmt[i] == COL_NUMBER)
-        g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[i]);
+      if (cf->cinfo.col_fmt[args->visible_cols[i]] == COL_NUMBER)
+        g_snprintf(cp, column_len+1, "%*s", args->col_widths[i], cf->cinfo.col_data[args->visible_cols[i]]);
       else
-        g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[i]);
+        g_snprintf(cp, column_len+1, "%-*s", args->col_widths[i], cf->cinfo.col_data[args->visible_cols[i]]);
       cp += column_len;
-      if (i != cf->cinfo.num_cols - 1)
+      if (i != args->num_visible_cols - 1)
         *cp++ = ' ';
     }
     *cp = '\0';
@@ -2366,56 +2547,63 @@ 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). */
     args->print_separator = TRUE;
 
     /* Print a header line if we print any more packet summaries */
-    args->print_header_line = TRUE;
+    if (args->print_args->print_col_headings)
+        args->print_header_line = TRUE;
   }
 
   if (args->print_args->print_hex) {
+    if (args->print_args->print_summary || (args->print_args->print_dissections != print_dissections_none)) {
+      if (!print_line(args->print_args->stream, 0, ""))
+        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). */
     args->print_separator = TRUE;
 
     /* Print a header line if we print any more packet summaries */
-    args->print_header_line = TRUE;
+    if (args->print_args->print_col_headings)
+        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) {
+  if (args->print_args->print_formfeed) {
     args->print_formfeed = TRUE;
   }
 
   return TRUE;
 
 fail:
-  epan_dissect_cleanup(&edt);
+  epan_dissect_reset(&args->edt);
   return FALSE;
 }
 
 cf_print_status_t
 cf_print_packets(capture_file *cf, print_args_t *print_args)
 {
-  int         i;
   print_callback_args_t callback_args;
-  gint        data_width;
-  char        *cp;
-  int         cp_off;
-  int         column_len;
-  int         line_len;
-  psp_return_t ret;
+  gint          data_width;
+  char         *cp;
+  int           i, cp_off, column_len, line_len;
+  int           num_visible_col = 0, last_visible_col = 0, visible_col_count;
+  psp_return_t  ret;
+  GList        *clp;
+  fmt_data     *cfmt;
+  gboolean      proto_tree_needed;
 
   callback_args.print_args = print_args;
-  callback_args.print_header_line = TRUE;
+  callback_args.print_header_line = print_args->print_col_headings;
   callback_args.header_line_buf = NULL;
   callback_args.header_line_buf_len = 256;
   callback_args.print_formfeed = FALSE;
@@ -2423,8 +2611,10 @@ cf_print_packets(capture_file *cf, print_args_t *print_args)
   callback_args.line_buf = NULL;
   callback_args.line_buf_len = 256;
   callback_args.col_widths = NULL;
+  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_gitversion)) {
     destroy_print_stream(print_args->stream);
     return CF_PRINT_WRITE_ERROR;
   }
@@ -2432,29 +2622,58 @@ cf_print_packets(capture_file *cf, print_args_t *print_args)
   if (print_args->print_summary) {
     /* We're printing packet summaries.  Allocate the header line buffer
        and get the column widths. */
-    callback_args.header_line_buf = g_malloc(callback_args.header_line_buf_len + 1);
+    callback_args.header_line_buf = (char *)g_malloc(callback_args.header_line_buf_len + 1);
+
+    /* Find the number of visible columns and the last visible column */
+    for (i = 0; i < prefs.num_cols; i++) {
+
+        clp = g_list_nth(prefs.col_list, i);
+        if (clp == NULL) /* Sanity check, Invalid column requested */
+            continue;
+
+        cfmt = (fmt_data *) clp->data;
+        if (cfmt->visible) {
+            num_visible_col++;
+            last_visible_col = i;
+        }
+    }
 
     /* Find the widths for each of the columns - maximum of the
        width of the title and the width of the data - and construct
        a buffer with a line containing the column titles. */
-    callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * cf->cinfo.num_cols);
+    callback_args.num_visible_cols = num_visible_col;
+    callback_args.col_widths = (gint *) g_malloc(sizeof(gint) * num_visible_col);
+    callback_args.visible_cols = (gint *) g_malloc(sizeof(gint) * num_visible_col);
     cp = &callback_args.header_line_buf[0];
     line_len = 0;
+    visible_col_count = 0;
     for (i = 0; i < cf->cinfo.num_cols; i++) {
+
+      clp = g_list_nth(prefs.col_list, i);
+      if (clp == NULL) /* Sanity check, Invalid column requested */
+          continue;
+
+      cfmt = (fmt_data *) clp->data;
+      if (cfmt->visible == FALSE)
+          continue;
+
+      /* Save the order of visible columns */
+      callback_args.visible_cols[visible_col_count] = i;
+
       /* Don't pad the last column. */
-      if (i == cf->cinfo.num_cols - 1)
-        callback_args.col_widths[i] = 0;
+      if (i == last_visible_col)
+        callback_args.col_widths[visible_col_count] = 0;
       else {
-        callback_args.col_widths[i] = (gint) strlen(cf->cinfo.col_title[i]);
+        callback_args.col_widths[visible_col_count] = (gint) strlen(cf->cinfo.col_title[i]);
         data_width = get_column_char_width(get_column_format(i));
-        if (data_width > callback_args.col_widths[i])
-          callback_args.col_widths[i] = data_width;
+        if (data_width > callback_args.col_widths[visible_col_count])
+          callback_args.col_widths[visible_col_count] = data_width;
       }
 
       /* Find the length of the string for this column. */
       column_len = (int) strlen(cf->cinfo.col_title[i]);
       if (callback_args.col_widths[i] > column_len)
-        column_len = callback_args.col_widths[i];
+        column_len = callback_args.col_widths[visible_col_count];
 
       /* Make sure there's room in the line buffer for the column; if not,
          double its length. */
@@ -2462,37 +2681,49 @@ cf_print_packets(capture_file *cf, print_args_t *print_args)
       if (line_len > callback_args.header_line_buf_len) {
         cp_off = (int) (cp - callback_args.header_line_buf);
         callback_args.header_line_buf_len = 2 * line_len;
-        callback_args.header_line_buf = g_realloc(callback_args.header_line_buf,
+        callback_args.header_line_buf = (char *)g_realloc(callback_args.header_line_buf,
                                                   callback_args.header_line_buf_len + 1);
         cp = callback_args.header_line_buf + cp_off;
       }
 
       /* Right-justify the packet number column. */
 /*      if (cf->cinfo.col_fmt[i] == COL_NUMBER)
-        g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
+        g_snprintf(cp, column_len+1, "%*s", callback_args.col_widths[visible_col_count], cf->cinfo.col_title[i]);
       else*/
-      g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
+      g_snprintf(cp, column_len+1, "%-*s", callback_args.col_widths[visible_col_count], cf->cinfo.col_title[i]);
       cp += column_len;
       if (i != cf->cinfo.num_cols - 1)
         *cp++ = ' ';
+
+      visible_col_count++;
     }
     *cp = '\0';
 
     /* Now start out the main line buffer with the same length as the
        header line buffer. */
     callback_args.line_buf_len = callback_args.header_line_buf_len;
-    callback_args.line_buf = g_malloc(callback_args.line_buf_len + 1);
+    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);
+  g_free(callback_args.visible_cols);
 
   switch (ret) {
 
@@ -2531,31 +2762,35 @@ 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,
-                  union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+                  struct wtap_pkthdr *phdr, const guint8 *pd,
           void *argsp)
 {
-  FILE *fh = 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, pseudo_header, 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)
 {
-  FILE        *fh;
-  psp_return_t ret;
+  write_packet_callback_args_t callback_args;
+  FILE         *fh;
+  psp_return_t  ret;
 
   fh = ws_fopen(print_args->file, "w");
   if (fh == NULL)
@@ -2567,11 +2802,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) {
 
@@ -2603,34 +2843,31 @@ cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
 
 static gboolean
 write_psml_packet(capture_file *cf, frame_data *fdata,
-                  union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+                  struct wtap_pkthdr *phdr, const guint8 *pd,
           void *argsp)
 {
-  FILE *fh = 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, pseudo_header, 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)
 {
-  FILE        *fh;
-  psp_return_t ret;
+  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)
@@ -2642,11 +2879,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) {
 
@@ -2678,34 +2924,31 @@ cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
 
 static gboolean
 write_csv_packet(capture_file *cf, frame_data *fdata,
-                 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+                 struct wtap_pkthdr *phdr, const guint8 *pd,
                  void *argsp)
 {
-  FILE *fh = 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, pseudo_header, 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)
 {
-  FILE        *fh;
-  psp_return_t ret;
+  write_packet_callback_args_t callback_args;
+  gboolean        proto_tree_needed;
+  FILE         *fh;
+  psp_return_t  ret;
 
   fh = ws_fopen(print_args->file, "w");
   if (fh == NULL)
@@ -2717,11 +2960,19 @@ 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);
+                                  write_csv_packet, &callback_args);
+
+  epan_dissect_cleanup(&callback_args.edt);
 
   switch (ret) {
 
@@ -2753,25 +3004,24 @@ cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
 
 static gboolean
 write_carrays_packet(capture_file *cf _U_, frame_data *fdata,
-             union wtap_pseudo_header *pseudo_header,
+             struct wtap_pkthdr *phdr,
              const guint8 *pd, void *argsp)
 {
-  FILE *fh = 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, pseudo_header, 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)
 {
-  FILE        *fh;
-  psp_return_t ret;
+  write_packet_callback_args_t callback_args;
+  FILE         *fh;
+  psp_return_t  ret;
 
   fh = ws_fopen(print_args->file, "w");
 
@@ -2785,12 +3035,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. */
@@ -2819,7 +3075,7 @@ gboolean
 cf_find_packet_protocol_tree(capture_file *cf, const char *string,
                              search_direction dir)
 {
-  match_data        mdata;
+  match_data mdata;
 
   mdata.string = string;
   mdata.string_len = strlen(string);
@@ -2841,8 +3097,8 @@ cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree,  match_data *md
 static match_result
 match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
 {
-  match_data        *mdata = criterion;
-  epan_dissect_t    edt;
+  match_data     *mdata = (match_data *)criterion;
+  epan_dissect_t  edt;
 
   /* Load the frame's data. */
   if (!cf_read_frame(cf, fdata)) {
@@ -2851,9 +3107,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->pseudo_header, cf->pd, 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;
@@ -2866,19 +3122,20 @@ match_protocol_tree(capture_file *cf, frame_data *fdata, void *criterion)
 static void
 match_subtree_text(proto_node *node, gpointer data)
 {
-  match_data    *mdata = (match_data *) data;
-  const gchar   *string = mdata->string;
+  match_data   *mdata      = (match_data *) data;
+  const gchar  *string     = mdata->string;
   size_t        string_len = mdata->string_len;
-  capture_file  *cf = mdata->cf;
-  field_info    *fi = PNODE_FINFO(node);
+  capture_file *cf         = mdata->cf;
+  field_info   *fi         = PNODE_FINFO(node);
   gchar         label_str[ITEM_LABEL_LENGTH];
-  gchar         *label_ptr;
+  gchar        *label_ptr;
   size_t        label_len;
   guint32       i;
   guint8        c_char;
-  size_t        c_match = 0;
+  size_t        c_match    = 0;
 
-  g_assert(fi && "dissection with an invisible proto tree?");
+  /* dissection with an invisible proto tree? */
+  g_assert(fi);
 
   if (mdata->frame_matched) {
     /* We already had a match; don't bother doing any more work. */
@@ -2925,7 +3182,7 @@ gboolean
 cf_find_packet_summary_line(capture_file *cf, const char *string,
                             search_direction dir)
 {
-  match_data        mdata;
+  match_data mdata;
 
   mdata.string = string;
   mdata.string_len = strlen(string);
@@ -2935,17 +3192,17 @@ cf_find_packet_summary_line(capture_file *cf, const char *string,
 static match_result
 match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
 {
-  match_data        *mdata = criterion;
-  const gchar       *string = mdata->string;
-  size_t            string_len = mdata->string_len;
-  epan_dissect_t    edt;
-  const char        *info_column;
-  size_t            info_column_len;
-  match_result      result = MR_NOTMATCHED;
-  gint              colx;
-  guint32           i;
-  guint8            c_char;
-  size_t            c_match = 0;
+  match_data     *mdata      = (match_data *)criterion;
+  const gchar    *string     = mdata->string;
+  size_t          string_len = mdata->string_len;
+  epan_dissect_t  edt;
+  const char     *info_column;
+  size_t          info_column_len;
+  match_result    result     = MR_NOTMATCHED;
+  gint            colx;
+  guint32         i;
+  guint8          c_char;
+  size_t          c_match    = 0;
 
   /* Load the frame's data. */
   if (!cf_read_frame(cf, fdata)) {
@@ -2954,9 +3211,10 @@ 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->pseudo_header, cf->pd, fdata, &cf->cinfo);
+  epan_dissect_run(&edt, &cf->phdr, frame_tvbuff_new_buffer(fdata, &cf->buf), fdata,
+                   &cf->cinfo);
 
   /* Find the Info column */
   for (colx = 0; colx < cf->cinfo.num_cols; colx++) {
@@ -2986,9 +3244,20 @@ match_summary_line(capture_file *cf, frame_data *fdata, void *criterion)
 
 typedef struct {
     const guint8 *data;
-    size_t data_len;
+    size_t        data_len;
 } cbs_t;    /* "Counted byte string" */
 
+
+/*
+ * The current match_* routines only support ASCII case insensitivity and don't
+ * convert UTF-8 inputs to UTF-16 for matching.
+ *
+ * We could modify them to use the GLib Unicode routines or the International
+ * Components for Unicode library but it's not apparent that we could do so
+ * without consuming a lot more CPU and memory or that searching would be
+ * significantly better.
+ */
+
 gboolean
 cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
                     search_direction dir)
@@ -3003,14 +3272,14 @@ cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
     /* String search - what type of string? */
     switch (cf->scs_type) {
 
-    case SCS_ASCII_AND_UNICODE:
-      return find_packet(cf, match_ascii_and_unicode, &info, dir);
+    case SCS_NARROW_AND_WIDE:
+      return find_packet(cf, match_narrow_and_wide, &info, dir);
 
-    case SCS_ASCII:
-      return find_packet(cf, match_ascii, &info, dir);
+    case SCS_NARROW:
+      return find_packet(cf, match_narrow, &info, dir);
 
-    case SCS_UNICODE:
-      return find_packet(cf, match_unicode, &info, dir);
+    case SCS_WIDE:
+      return find_packet(cf, match_wide, &info, dir);
 
     default:
       g_assert_not_reached();
@@ -3021,16 +3290,17 @@ cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size,
 }
 
 static match_result
-match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
+match_narrow_and_wide(capture_file *cf, frame_data *fdata, void *criterion)
 {
-  cbs_t        *info criterion;
+  cbs_t        *info       = (cbs_t *)criterion;
   const guint8 *ascii_text = info->data;
-  size_t       textlen = info->data_len;
-  match_result result;
-  guint32      buf_len;
-  guint32      i;
-  guint8       c_char;
-  size_t       c_match = 0;
+  size_t        textlen    = info->data_len;
+  match_result  result;
+  guint32       buf_len;
+  guint8       *pd;
+  guint32       i;
+  guint8        c_char;
+  size_t        c_match    = 0;
 
   /* Load the frame's data. */
   if (!cf_read_frame(cf, fdata)) {
@@ -3039,10 +3309,11 @@ match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
   }
 
   result = MR_NOTMATCHED;
-  buf_len = fdata->pkt_len;
+  buf_len = fdata->cap_len;
+  pd = buffer_start_ptr(&cf->buf);
   i = 0;
   while (i < buf_len) {
-    c_char = cf->pd[i];
+    c_char = pd[i];
     if (cf->case_type)
       c_char = toupper(c_char);
     if (c_char != '\0') {
@@ -3067,16 +3338,17 @@ match_ascii_and_unicode(capture_file *cf, frame_data *fdata, void *criterion)
 }
 
 static match_result
-match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
+match_narrow(capture_file *cf, frame_data *fdata, void *criterion)
 {
-  cbs_t        *info = criterion;
+  guint8       *pd;
+  cbs_t        *info       = (cbs_t *)criterion;
   const guint8 *ascii_text = info->data;
-  size_t       textlen = info->data_len;
-  match_result result;
-  guint32      buf_len;
-  guint32      i;
-  guint8       c_char;
-  size_t       c_match = 0;
+  size_t        textlen    = info->data_len;
+  match_result  result;
+  guint32       buf_len;
+  guint32       i;
+  guint8        c_char;
+  size_t        c_match    = 0;
 
   /* Load the frame's data. */
   if (!cf_read_frame(cf, fdata)) {
@@ -3085,10 +3357,11 @@ match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
   }
 
   result = MR_NOTMATCHED;
-  buf_len = fdata->pkt_len;
+  buf_len = fdata->cap_len;
+  pd = buffer_start_ptr(&cf->buf);
   i = 0;
   while (i < buf_len) {
-    c_char = cf->pd[i];
+    c_char = pd[i];
     if (cf->case_type)
       c_char = toupper(c_char);
     if (c_char == ascii_text[c_match]) {
@@ -3112,16 +3385,17 @@ match_ascii(capture_file *cf, frame_data *fdata, void *criterion)
 }
 
 static match_result
-match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
+match_wide(capture_file *cf, frame_data *fdata, void *criterion)
 {
-  cbs_t        *info criterion;
+  cbs_t        *info       = (cbs_t *)criterion;
   const guint8 *ascii_text = info->data;
-  size_t       textlen = info->data_len;
-  match_result result;
-  guint32      buf_len;
-  guint32      i;
-  guint8       c_char;
-  size_t       c_match = 0;
+  size_t        textlen    = info->data_len;
+  match_result  result;
+  guint32       buf_len;
+  guint8       *pd;
+  guint32       i;
+  guint8        c_char;
+  size_t        c_match    = 0;
 
   /* Load the frame's data. */
   if (!cf_read_frame(cf, fdata)) {
@@ -3130,10 +3404,11 @@ match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
   }
 
   result = MR_NOTMATCHED;
-  buf_len = fdata->pkt_len;
+  buf_len = fdata->cap_len;
+  pd = buffer_start_ptr(&cf->buf);
   i = 0;
   while (i < buf_len) {
-    c_char = cf->pd[i];
+    c_char = pd[i];
     if (cf->case_type)
       c_char = toupper(c_char);
     if (c_char == ascii_text[c_match]) {
@@ -3159,13 +3434,14 @@ match_unicode(capture_file *cf, frame_data *fdata, void *criterion)
 static match_result
 match_binary(capture_file *cf, frame_data *fdata, void *criterion)
 {
-  cbs_t        *info criterion;
+  cbs_t        *info        = (cbs_t *)criterion;
   const guint8 *binary_data = info->data;
-  size_t       datalen = info->data_len;
-  match_result result;
-  guint32      buf_len;
-  guint32      i;
-  size_t       c_match = 0;
+  size_t        datalen     = info->data_len;
+  match_result  result;
+  guint32       buf_len;
+  guint8       *pd;
+  guint32       i;
+  size_t        c_match     = 0;
 
   /* Load the frame's data. */
   if (!cf_read_frame(cf, fdata)) {
@@ -3174,10 +3450,11 @@ match_binary(capture_file *cf, frame_data *fdata, void *criterion)
   }
 
   result = MR_NOTMATCHED;
-  buf_len = fdata->pkt_len;
+  buf_len = fdata->cap_len;
+  pd = buffer_start_ptr(&cf->buf);
   i = 0;
   while (i < buf_len) {
-    if (cf->pd[i] == binary_data[c_match]) {
+    if (pd[i] == binary_data[c_match]) {
       c_match += 1;
       if (c_match == datalen) {
         result = MR_MATCHED;
@@ -3208,7 +3485,7 @@ cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
                               search_direction dir)
 {
   dfilter_t *sfcode;
-  gboolean result;
+  gboolean   result;
 
   if (!dfilter_compile(filter, &sfcode)) {
      /*
@@ -3232,9 +3509,9 @@ cf_find_packet_dfilter_string(capture_file *cf, const char *filter,
 static match_result
 match_dfilter(capture_file *cf, frame_data *fdata, void *criterion)
 {
-  dfilter_t      *sfcode = criterion;
-  epan_dissect_t edt;
-  match_result   result;
+  dfilter_t      *sfcode = (dfilter_t *)criterion;
+  epan_dissect_t  edt;
+  match_result    result;
 
   /* Load the frame's data. */
   if (!cf_read_frame(cf, fdata)) {
@@ -3242,9 +3519,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->pseudo_header, cf->pd, 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;
@@ -3322,7 +3599,7 @@ find_packet(capture_file *cf,
          large file, we might take considerably longer than that standard
          time in order to get to the next progress bar step). */
       if (progbar == NULL)
-         progbar = delayed_create_progress_dlg("Searching", title,
+         progbar = delayed_create_progress_dlg(cf->window, "Searching", title,
            FALSE, &stop_flag, &start_time, progbar_val);
 
       /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
@@ -3430,7 +3707,7 @@ find_packet(capture_file *cf,
   if (new_fd != NULL) {
     /* Find and select */
     cf->search_in_progress = TRUE;
-    found = new_packet_list_select_row_from_data(new_fd);
+    found = packet_list_select_row_from_data(new_fd);
     cf->search_in_progress = FALSE;
     cf->search_pos = 0; /* Reset the position */
     if (!found) {
@@ -3466,7 +3743,7 @@ cf_goto_frame(capture_file *cf, guint fnumber)
     return FALSE;   /* we failed to go to that packet */
   }
 
-  if (!new_packet_list_select_row_from_data(fdata)) {
+  if (!packet_list_select_row_from_data(fdata)) {
     /* We didn't find a row corresponding to this frame.
        This means that the frame isn't being displayed currently,
        so we can't select it. */
@@ -3482,7 +3759,7 @@ gboolean
 cf_goto_top_frame(void)
 {
   /* Find and select */
-  new_packet_list_select_first_row();
+  packet_list_select_first_row();
   return TRUE;  /* we got to that packet */
 }
 
@@ -3490,7 +3767,7 @@ gboolean
 cf_goto_bottom_frame(void)
 {
   /* Find and select */
-  new_packet_list_select_last_row();
+  packet_list_select_last_row();
   return TRUE;  /* we got to that packet */
 }
 
@@ -3500,8 +3777,8 @@ cf_goto_bottom_frame(void)
 gboolean
 cf_goto_framenum(capture_file *cf)
 {
-  header_field_info       *hfinfo;
-  guint32                 framenum;
+  header_field_info *hfinfo;
+  guint32            framenum;
 
   if (cf->finfo_selected) {
     hfinfo = cf->finfo_selected->hfinfo;
@@ -3521,10 +3798,10 @@ void
 cf_select_packet(capture_file *cf, int row)
 {
   epan_dissect_t *old_edt;
-  frame_data *fdata;
+  frame_data     *fdata;
 
   /* Get the frame data struct pointer for this frame */
-  fdata = new_packet_list_get_row_data(row);
+  fdata = packet_list_get_row_data(row);
 
   if (fdata == NULL) {
     /* XXX - if a GtkCList's selection mode is GTK_SELECTION_BROWSE, when
@@ -3574,11 +3851,11 @@ 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->pseudo_header, cf->pd, cf->current_frame,
-          NULL);
+  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);
 
@@ -3680,11 +3957,11 @@ const gchar *
 cf_read_shb_comment(capture_file *cf)
 {
   wtapng_section_t *shb_inf;
-  const gchar *temp_str;
+  const gchar      *temp_str;
 
   /* Get info from SHB */
   shb_inf = wtap_file_get_shb_info(cf->wth);
-  if(shb_inf == NULL)
+  if (shb_inf == NULL)
         return NULL;
   temp_str = shb_inf->opt_comment;
   g_free(shb_inf);
@@ -3718,32 +3995,97 @@ 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;
 }
 
 /*
- * Does this capture file have any comments?
+ * What types of comments does this capture file have?
  */
-gboolean
-cf_has_comments(capture_file *cf)
+guint32
+cf_comment_types(capture_file *cf)
 {
-  return (cf_read_shb_comment(cf) != NULL || cf->packet_comment_count != 0);
+  guint32 comment_types = 0;
+
+  if (cf_read_shb_comment(cf) != NULL)
+    comment_types |= WTAP_COMMENT_PER_SECTION;
+  if (cf->packet_comment_count != 0)
+    comment_types |= WTAP_COMMENT_PER_PACKET;
+  return comment_types;
 }
 
 typedef struct {
@@ -3761,15 +4103,22 @@ typedef struct {
  */
 static gboolean
 save_packet(capture_file *cf _U_, frame_data *fdata,
-            union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+            struct wtap_pkthdr *phdr, const guint8 *pd,
             void *argsp)
 {
-  save_callback_args_t *args = argsp;
-  struct wtap_pkthdr hdr;
+  save_callback_args_t *args = (save_callback_args_t *)argsp;
+  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 */
   /* XXX - these are the only flags that correspond to data that we have
      in the frame_data structure and that matter on a per-packet basis.
 
@@ -3784,35 +4133,53 @@ 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_ts)
+  if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID)
     hdr.presence_flags |= WTAP_HAS_INTERFACE_ID;
+  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;
   hdr.caplen       = fdata->cap_len;
   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.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
   hdr.drop_count   =
   hdr.pack_flags   =     /* XXX - 0 for now (any value for "we don't have it"?) */
 #endif
   /* and save the packet */
-  if (!wtap_dump(args->pdh, &hdr, pseudo_header, pd, &err)) {
+  if (!wtap_dump(args->pdh, &hdr, pd, &err)) {
     if (err < 0) {
       /* Wiretap error. */
       switch (err) {
 
       case WTAP_ERR_UNSUPPORTED_ENCAP:
         /*
-         * This is a problem with the particular frame we're writing;
-         * note that, and give the frame number.
+         * This is a problem with the particular frame we're writing and
+         * the file type and subtype we're writing; note that, and report
+         * the frame number and file type/subtype.
          */
         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;
+
+      case WTAP_ERR_PACKET_TOO_LARGE:
+        /*
+         * This is a problem with the particular frame we're writing and
+         * the file type and subtype we're writing; note that, and report
+         * the frame number and file type/subtype.
+         */
+        simple_error_message_box(
+                      "Frame %u is larger than Wireshark supports in a \"%s\" file.",
+                      fdata->num, wtap_file_type_subtype_string(args->file_type));
         break;
 
       default:
@@ -3829,31 +4196,116 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
     }
     return FALSE;
   }
+
+  g_free(hdr.opt_comment);
   return TRUE;
 }
 
 /*
- * Can this capture file be saved in any format except by copying the raw data?
+ * Can this capture file be written out in any format using Wiretap
+ * rather than by copying the raw data?
+ */
+gboolean
+cf_can_write_with_wiretap(capture_file *cf)
+{
+  /* We don't care whether we support the comments in this file or not;
+     if we can't, we'll offer the user the option of discarding the
+     comments. */
+  return wtap_dump_can_write(cf->linktypes, 0);
+}
+
+/*
+ * Should we let the user do a save?
+ *
+ * We should if:
+ *
+ *  the file has unsaved changes, and we can save it in some
+ *  format through Wiretap
+ *
+ * or
+ *
+ *  the file is a temporary file and has no unsaved changes (so
+ *  that "saving" it just means copying it).
+ *
+ * XXX - we shouldn't allow files to be edited if they can't be saved,
+ * so cf->unsaved_changes should be true only if the file can be saved.
+ *
+ * We don't care whether we support the comments in this file or not;
+ * if we can't, we'll offer the user the option of discarding the
+ * comments.
+ */
+gboolean
+cf_can_save(capture_file *cf)
+{
+  if (cf->unsaved_changes && wtap_dump_can_write(cf->linktypes, 0)) {
+    /* Saved changes, and we can write it out with Wiretap. */
+    return TRUE;
+  }
+
+  if (cf->is_tempfile && !cf->unsaved_changes) {
+    /*
+     * Temporary file with no unsaved changes, so we can just do a
+     * raw binary copy.
+     */
+    return TRUE;
+  }
+
+  /* Nothing to save. */
+  return FALSE;
+}
+
+/*
+ * Should we let the user do a "save as"?
+ *
+ * That's true if:
+ *
+ *  we can save it in some format through Wiretap
+ *
+ * or
+ *
+ *  the file is a temporary file and has no unsaved changes (so
+ *  that "saving" it just means copying it).
+ *
+ * XXX - we shouldn't allow files to be edited if they can't be saved,
+ * so cf->unsaved_changes should be true only if the file can be saved.
+ *
+ * We don't care whether we support the comments in this file or not;
+ * if we can't, we'll offer the user the option of discarding the
+ * comments.
  */
 gboolean
 cf_can_save_as(capture_file *cf)
 {
-  int ft;
-
-  for (ft = 0; ft < WTAP_NUM_FILE_TYPES; ft++) {
-    /* To save a file with Wiretap, Wiretap has to handle that format,
-       and its code to handle that format must be able to write a file
-       with this file's encapsulation types. */
-    if (wtap_dump_can_write_encaps(ft, cf->linktypes)) {
-      /* OK, we can write it out in this type. */
-      return TRUE;
-    }
+  if (wtap_dump_can_write(cf->linktypes, 0)) {
+    /* We can write it out with Wiretap. */
+    return TRUE;
   }
 
-  /* No, we couldn't save it in any format. */
+  if (cf->is_tempfile && !cf->unsaved_changes) {
+    /*
+     * Temporary file with no unsaved changes, so we can just do a
+     * raw binary copy.
+     */
+    return TRUE;
+  }
+
+  /* Nothing to save. */
   return FALSE;
 }
 
+/*
+ * Does this file have unsaved data?
+ */
+gboolean
+cf_has_unsaved_data(capture_file *cf)
+{
+  /*
+   * If this is a temporary file, or a file with unsaved changes, it
+   * has unsaved data.
+   */
+  return cf->is_tempfile || cf->unsaved_changes;
+}
+
 /*
  * Quick scan to find packet offsets.
  */
@@ -3861,23 +4313,23 @@ static cf_read_status_t
 rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
 {
   const struct wtap_pkthdr *phdr;
-  gchar       *err_info;
-  gchar       *name_ptr;
-  gint64       data_offset;
-  gint64       file_pos;
-  progdlg_t *volatile progbar = NULL;
-  gboolean     stop_flag;
-  volatile gint64 size;
-  volatile float progbar_val;
-  GTimeVal     start_time;
-  gchar        status_str[100];
-  volatile gint64 progbar_nextstep;
-  volatile gint64 progbar_quantum;
-  guint32       framenum;
-  frame_data   *fdata;
-  volatile int count = 0;
+  gchar               *err_info;
+  gchar               *name_ptr;
+  gint64               data_offset;
+  gint64               file_pos;
+  progdlg_t           *progbar        = NULL;
+  gboolean             stop_flag;
+  gint64               size;
+  float                progbar_val;
+  GTimeVal             start_time;
+  gchar                status_str[100];
+  gint64               progbar_nextstep;
+  gint64               progbar_quantum;
+  guint32              framenum;
+  frame_data          *fdata;
+  int                  count          = 0;
 #ifdef HAVE_LIBPCAP
-  volatile int displayed_once = 0;
+  int                  displayed_once = 0;
 #endif
 
   /* Close the old handle. */
@@ -3905,7 +4357,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);
@@ -3931,14 +4383,12 @@ rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
   progbar_nextstep = 0;
   /* When we reach the value that triggers a progress bar update,
      bump that value by this amount. */
-  if (size >= 0){
+  if (size >= 0) {
     progbar_quantum = size/N_PROGBAR_UPDATES;
     if (progbar_quantum < MIN_QUANTUM)
       progbar_quantum = MIN_QUANTUM;
   }else
     progbar_quantum = 0;
-  /* Progress so far. */
-  progbar_val = 0.0f;
 
   stop_flag = FALSE;
   g_get_current_time(&start_time);
@@ -3956,9 +4406,9 @@ rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
       /* Create the progress bar if necessary.
        * Check whether it should be created or not every MIN_NUMBER_OF_PACKET
        */
-      if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)){
+      if ((progbar == NULL) && !(count % MIN_NUMBER_OF_PACKET)) {
         progbar_val = calc_progbar_val(cf, size, file_pos, status_str, sizeof(status_str));
-        progbar = delayed_create_progress_dlg("Rescanning", name_ptr,
+        progbar = delayed_create_progress_dlg(cf->window, "Rescanning", name_ptr,
                                               TRUE, &stop_flag, &start_time, progbar_val);
       }
 
@@ -3994,7 +4444,7 @@ rescan_file(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
 
     /* Add this packet's link-layer encapsulation type to cf->linktypes, if
        it's not already there.
-       XXX - yes, this is O(N), so if every packet had a different 
+       XXX - yes, this is O(N), so if every packet had a different
        link-layer encapsulation type, it'd be O(N^2) to read the file, but
        there are probably going to be a small number of encapsulation types
        in a file. */
@@ -4015,7 +4465,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
@@ -4092,30 +4542,34 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
                 gboolean compressed, gboolean discard_comments,
                 gboolean dont_reopen)
 {
-  gchar        *fname_new = NULL;
-  int           err;
-  gchar        *err_info;
+  gchar           *err_info;
+  gchar           *fname_new = NULL;
+  wtap_dumper     *pdh;
+  frame_data      *fdata;
+  addrinfo_lists_t *addr_lists;
+  guint            framenum;
+  int              err;
+#ifdef _WIN32
+  gchar           *display_basename;
+#endif
   enum {
      SAVE_WITH_MOVE,
      SAVE_WITH_COPY,
      SAVE_WITH_WTAP
-  }             how_to_save;
-  wtap_dumper  *pdh;
+  }                    how_to_save;
   save_callback_args_t callback_args;
-#ifdef _WIN32
-  gchar        *display_basename;
-#endif
-  guint         framenum;
-  frame_data   *fdata;
 
   cf_callback_invoke(cf_cb_file_save_started, (gpointer)fname);
 
+  addr_lists = get_addrinfo_list();
+
   if (save_format == cf->cd_t && compressed == cf->iscompressed
-      && !discard_comments && !cf->unsaved_changes) {
+      && !discard_comments && !cf->unsaved_changes
+      && !(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, so we can just move
-       or copy the raw data. */
+       in memory that aren't saved to the file, and we have no name
+       resolution blocks to write, so we can just move or copy the raw data. */
 
     if (cf->is_tempfile) {
       /* The file being saved is a temporary file from a live
@@ -4193,10 +4647,14 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
 
     wtapng_section_t *shb_hdr = NULL;
     wtapng_iface_descriptions_t *idb_inf = NULL;
+    int encap;
 
     shb_hdr = wtap_file_get_shb_info(cf->wth);
     idb_inf = wtap_file_get_idb_info(cf->wth);
 
+    /* Determine what file encapsulation type we should use. */
+    encap = wtap_dump_file_encap_type(cf->linktypes);
+
     if (file_exists(fname)) {
       /* We're overwriting an existing file; write out to a new file,
          and, if that succeeds, rename the new file on top of the
@@ -4206,10 +4664,10 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
          we *HAVE* to do that, otherwise we're overwriting the file
          from which we're reading the packets that we're writing!) */
       fname_new = g_strdup_printf("%s~", fname);
-      pdh = wtap_dump_open_ng(fname_new, save_format, cf->lnk_t, cf->snap,
+      pdh = wtap_dump_open_ng(fname_new, save_format, encap, cf->snap,
                               compressed, shb_hdr, idb_inf, &err);
     } else {
-      pdh = wtap_dump_open_ng(fname, save_format, cf->lnk_t, cf->snap,
+      pdh = wtap_dump_open_ng(fname, save_format, encap, cf->snap,
                               compressed, shb_hdr, idb_inf, &err);
     }
     g_free(idb_inf);
@@ -4221,7 +4679,7 @@ cf_save_packets(capture_file *cf, const char *fname, guint save_format,
     }
 
     /* Add address resolution */
-    wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list());
+    wtap_dump_set_addrinfo_list(pdh, addr_lists);
 
     /* Iterate through the list of packets, processing all the packets. */
     callback_args.pdh = pdh;
@@ -4358,16 +4816,21 @@ 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;
 
@@ -4391,12 +4854,13 @@ cf_export_specified_packets(capture_file *cf, const char *fname,
                             packet_range_t *range, guint save_format,
                             gboolean compressed)
 {
-  gchar        *fname_new = NULL;
-  int           err;
-  wtap_dumper  *pdh;
-  save_callback_args_t callback_args;
-  wtapng_section_t *shb_hdr = NULL;
-  wtapng_iface_descriptions_t *idb_inf = NULL;
+  gchar                       *fname_new = NULL;
+  int                          err;
+  wtap_dumper                 *pdh;
+  save_callback_args_t         callback_args;
+  wtapng_section_t            *shb_hdr;
+  wtapng_iface_descriptions_t *idb_inf;
+  int                          encap;
 
   cf_callback_invoke(cf_cb_file_export_specified_packets_started, (gpointer)fname);
 
@@ -4410,6 +4874,9 @@ cf_export_specified_packets(capture_file *cf, const char *fname,
   shb_hdr = wtap_file_get_shb_info(cf->wth);
   idb_inf = wtap_file_get_idb_info(cf->wth);
 
+  /* Determine what file encapsulation type we should use. */
+  encap = wtap_dump_file_encap_type(cf->linktypes);
+
   if (file_exists(fname)) {
     /* We're overwriting an existing file; write out to a new file,
        and, if that succeeds, rename the new file on top of the
@@ -4419,10 +4886,10 @@ cf_export_specified_packets(capture_file *cf, const char *fname,
        we *HAVE* to do that, otherwise we're overwriting the file
        from which we're reading the packets that we're writing!) */
     fname_new = g_strdup_printf("%s~", fname);
-    pdh = wtap_dump_open_ng(fname_new, save_format, cf->lnk_t, cf->snap,
+    pdh = wtap_dump_open_ng(fname_new, save_format, encap, cf->snap,
                             compressed, shb_hdr, idb_inf, &err);
   } else {
-    pdh = wtap_dump_open_ng(fname, save_format, cf->lnk_t, cf->snap,
+    pdh = wtap_dump_open_ng(fname, save_format, encap, cf->snap,
                             compressed, shb_hdr, idb_inf, &err);
   }
   g_free(idb_inf);
@@ -4554,7 +5021,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:
@@ -4728,9 +5195,9 @@ cf_close_failure_alert_box(const char *filename, int err)
 /* Reload the current capture file. */
 void
 cf_reload(capture_file *cf) {
-  gchar *filename;
-  gboolean is_tempfile;
-  int err;
+  gchar    *filename;
+  gboolean  is_tempfile;
+  int       err;
 
   /* If the file could be opened, "cf_open()" calls "cf_close()"
      to get rid of state for the old capture file before filling in state