From Marko Hrastovec via
[metze/wireshark/wip.git] / editcap.c
index 4ee9e6aaca31785735bc6d04352ad36317ac1c76..8c8cee9fb4e83f743f9f1973b9ef2104da341299 100644 (file)
--- a/editcap.c
+++ b/editcap.c
 #endif
 
 #include <wsutil/privileges.h>
+#include <wsutil/filesystem.h>
 #include <wsutil/report_err.h>
 #include <wsutil/strnatcmp.h>
 #include <wsutil/md5.h>
-
-/*
- * The symbols declared in the below are exported from libwireshark,
- * but we don't want to link whole libwireshark to editcap.
- * We link the object directly instead and this needs a little trick
- * with the WS_BUILD_DLL #define.
- */
-#define WS_BUILD_DLL
-#define RESET_SYMBOL_EXPORT /* wsutil/wsgetopt.h set export behavior above. */
-#include "epan/plugins.h"
-#include "epan/filesystem.h"
-#undef WS_BUILD_DLL
-#define RESET_SYMBOL_EXPORT
+#include <wsutil/plugins.h>
 
 #include "svnversion.h"
 
@@ -111,18 +100,18 @@ struct select_item {
  */
 typedef struct _fd_hash_t {
     md5_byte_t digest[16];
-    guint32 len;
-    nstime_t time;
+    guint32    len;
+    nstime_t   time;
 } fd_hash_t;
 
-#define DEFAULT_DUP_DEPTH 5     /* Used with -d */
-#define MAX_DUP_DEPTH 1000000   /* the maximum window (and actual size of fd_hash[]) for de-duplication */
+#define DEFAULT_DUP_DEPTH       5   /* Used with -d */
+#define MAX_DUP_DEPTH     1000000   /* the maximum window (and actual size of fd_hash[]) for de-duplication */
 
 static fd_hash_t fd_hash[MAX_DUP_DEPTH];
-static int dup_window = DEFAULT_DUP_DEPTH;
-static int cur_dup_entry = 0;
+static int       dup_window    = DEFAULT_DUP_DEPTH;
+static int       cur_dup_entry = 0;
 
-#define ONE_MILLION 1000000
+#define ONE_MILLION    1000000
 #define ONE_BILLION 1000000000
 
 /* Weights of different errors we can introduce */
@@ -143,47 +132,51 @@ struct time_adjustment {
     int is_negative;
 };
 
-#define MAX_SELECTIONS  512
-static struct select_item selectfrm[MAX_SELECTIONS];
-static int max_selected = -1;
-static int keep_em = 0;
+typedef struct _chop_t {
+    int len_begin;
+    int off_begin_pos;
+    int off_begin_neg;
+    int len_end;
+    int off_end_pos;
+    int off_end_neg;
+} chop_t;
+
+#define MAX_SELECTIONS 512
+static struct select_item     selectfrm[MAX_SELECTIONS];
+static int                    max_selected              = -1;
+static int                    keep_em                   = 0;
 #ifdef PCAP_NG_DEFAULT
-static int out_file_type = WTAP_FILE_PCAPNG; /* default to pcapng   */
+static int                    out_file_type_subtype     = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcapng   */
 #else
-static int out_file_type = WTAP_FILE_PCAP;   /* default to pcap     */
+static int                    out_file_type_subtype     = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcap     */
 #endif
-static int out_frame_type = -2;              /* Leave frame type alone */
-static int verbose = 0;                      /* Not so verbose         */
-static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
-static nstime_t relative_time_window = {0, 0}; /* de-dup time window */
-static double err_prob = 0.0;
-static time_t starttime = 0;
-static time_t stoptime = 0;
-static gboolean check_startstop = FALSE;
-static gboolean dup_detect = FALSE;
-static gboolean dup_detect_by_time = FALSE;
-
-static int do_strict_time_adjustment = FALSE;
-static struct time_adjustment strict_time_adj = {{0, 0}, 0}; /* strict time adjustment */
-static nstime_t previous_time = {0, 0}; /* previous time */
+static int                    out_frame_type            = -2; /* Leave frame type alone */
+static int                    verbose                   = 0;  /* Not so verbose         */
+static struct time_adjustment time_adj                  = {{0, 0}, 0}; /* no adjustment */
+static nstime_t               relative_time_window      = {0, 0}; /* de-dup time window */
+static double                 err_prob                  = 0.0;
+static time_t                 starttime                 = 0;
+static time_t                 stoptime                  = 0;
+static gboolean               check_startstop           = FALSE;
+static gboolean               dup_detect                = FALSE;
+static gboolean               dup_detect_by_time        = FALSE;
+
+static int                    do_strict_time_adjustment = FALSE;
+static struct time_adjustment strict_time_adj           = {{0, 0}, 0}; /* strict time adjustment */
+static nstime_t               previous_time             = {0, 0}; /* previous time */
 
 static int find_dct2000_real_data(guint8 *buf);
+static void handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
+                            const struct wtap_pkthdr *in_phdr, guint8 **buf,
+                            gboolean adjlen);
 
 static gchar *
-abs_time_to_str_with_sec_resolution(const struct wtap_nstime *abs_time)
+abs_time_to_str_with_sec_resolution(const nstime_t *abs_time)
 {
     struct tm *tmp;
-    gchar *buf = (gchar *)g_malloc(16);
-
-#if (defined _WIN32) && (_MSC_VER < 1500)
-    /* calling localtime() on MSVC 2005 with huge values causes it to crash */
-    /* XXX - find the exact value that still does work */
-    /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
-    if (abs_time->secs > 2000000000)
-        tmp = NULL;
-    else
-#endif
-        tmp = localtime(&abs_time->secs);
+    gchar     *buf = (gchar *)g_malloc(16);
+
+    tmp = localtime(&abs_time->secs);
 
     if (tmp) {
         g_snprintf(buf, 16, "%d%02d%02d%02d%02d%02d",
@@ -200,11 +193,11 @@ abs_time_to_str_with_sec_resolution(const struct wtap_nstime *abs_time)
     return buf;
 }
 
-static gchar*
-fileset_get_filename_by_pattern(guint idx, const struct wtap_nstime *time_val,
+static gchar *
+fileset_get_filename_by_pattern(guint idx, const nstime_t *time_val,
                                 gchar *fprefix, gchar *fsuffix)
 {
-    gchar filenum[5+1];
+    gchar  filenum[5+1];
     gchar *timestr;
     gchar *abs_str;
 
@@ -264,29 +257,34 @@ add_selection(char *sel)
 
     if (++max_selected >= MAX_SELECTIONS) {
         /* Let the user know we stopped selecting */
-        printf("Out of room for packet selections!\n");
+        fprintf(stderr, "Out of room for packet selections!\n");
         return(FALSE);
     }
 
-    printf("Add_Selected: %s\n", sel);
+    if (verbose)
+        fprintf(stderr, "Add_Selected: %s\n", sel);
 
     if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
-        printf("Not inclusive ...");
+        if (verbose)
+            fprintf(stderr, "Not inclusive ...");
 
         selectfrm[max_selected].inclusive = 0;
         selectfrm[max_selected].first = atoi(sel);
 
-        printf(" %i\n", selectfrm[max_selected].first);
+        if (verbose)
+            fprintf(stderr, " %i\n", selectfrm[max_selected].first);
     } else {
-        printf("Inclusive ...");
+        if (verbose)
+            fprintf(stderr, "Inclusive ...");
 
         next = locn + 1;
         selectfrm[max_selected].inclusive = 1;
         selectfrm[max_selected].first = atoi(sel);
         selectfrm[max_selected].second = atoi(next);
 
-        printf(" %i, %i\n", selectfrm[max_selected].first,
-               selectfrm[max_selected].second);
+        if (verbose)
+            fprintf(stderr, " %i, %i\n", selectfrm[max_selected].first,
+                   selectfrm[max_selected].second);
     }
 
     return(TRUE);
@@ -316,7 +314,7 @@ selected(int recno)
 static gboolean
 check_timestamp(wtap *wth)
 {
-    struct wtap_pkthdrpkthdr = wtap_phdr(wth);
+    struct wtap_pkthdr *pkthdr = wtap_phdr(wth);
 
     return (pkthdr->ts.secs >= starttime) && (pkthdr->ts.secs < stoptime);
 }
@@ -324,9 +322,9 @@ check_timestamp(wtap *wth)
 static void
 set_time_adjustment(char *optarg_str_p)
 {
-    char *frac, *end;
-    long val;
-    size_t frac_digits;
+    char   *frac, *end;
+    long    val;
+    size_t  frac_digits;
 
     if (!optarg_str_p)
         return;
@@ -394,9 +392,9 @@ set_time_adjustment(char *optarg_str_p)
 static void
 set_strict_time_adj(char *optarg_str_p)
 {
-    char *frac, *end;
-    long val;
-    size_t frac_digits;
+    char   *frac, *end;
+    long    val;
+    size_t  frac_digits;
 
     if (!optarg_str_p)
         return;
@@ -468,9 +466,9 @@ set_strict_time_adj(char *optarg_str_p)
 static void
 set_rel_time(char *optarg_str_p)
 {
-    char *frac, *end;
-    long val;
-    size_t frac_digits;
+    char   *frac, *end;
+    long    val;
+    size_t  frac_digits;
 
     if (!optarg_str_p)
         return;
@@ -596,7 +594,7 @@ is_duplicate_rel_time(guint8* fd, guint32 len, const nstime_t *current) {
      * in strict chronologically increasing order (which is NOT
      * always the case!!).
      *
-     * The fd_hash[] table was deliberatly created large (1,000,000).
+     * The fd_hash[] table was deliberately created large (1,000,000).
      * Looking for time related duplicates in large trace files with
      * non-fractional dup time window values can potentially take
      * a long time to complete.
@@ -721,7 +719,9 @@ usage(gboolean is_error)
     fprintf(output, "                         then the bytes chopped will be offset from that value.\n");
     fprintf(output, "                         Positive offsets are from the packet beginning,\n");
     fprintf(output, "                         negative offsets are from the packet end. You can use\n");
-    fprintf(output, "                         this option more than once.\n");
+    fprintf(output, "                         this option more than once, allowing up to 2 chopping\n");
+    fprintf(output, "                         regions within a packet provided that at least 1\n");
+    fprintf(output, "                         choplen is positive and at least 1 is negative.\n");
     fprintf(output, "  -L                     adjust the frame length when chopping and/or snapping\n");
     fprintf(output, "  -t <time adjustment>   adjust the timestamp of each packet;\n");
     fprintf(output, "                         <time adjustment> is in relative seconds (e.g. -0.5).\n");
@@ -754,7 +754,7 @@ usage(gboolean is_error)
     fprintf(output, "  -v                     verbose output.\n");
     fprintf(output, "                         If -v is used with any of the 'Duplicate Packet\n");
     fprintf(output, "                         Removal' options (-d, -D or -w) then Packet lengths\n");
-    fprintf(output, "                         and MD5 hashes are printed to standard-out.\n");
+    fprintf(output, "                         and MD5 hashes are printed to standard-error.\n");
     fprintf(output, "\n");
 }
 
@@ -791,12 +791,12 @@ list_capture_types(void) {
     struct string_elem *captypes;
     GSList *list = NULL;
 
-    captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES);
+    captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES);
     fprintf(stderr, "editcap: The available capture file types for the \"-F\" flag are:\n");
-    for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
+    for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
         if (wtap_dump_can_open(i)) {
-            captypes[i].sstr = wtap_file_type_short_string(i);
-            captypes[i].lstr = wtap_file_type_string(i);
+            captypes[i].sstr = wtap_file_type_subtype_short_string(i);
+            captypes[i].lstr = wtap_file_type_subtype_string(i);
             list = g_slist_insert_sorted(list, &captypes[i], string_compare);
         }
     }
@@ -840,39 +840,36 @@ failure_message(const char *msg_format _U_, va_list ap _U_)
 int
 main(int argc, char *argv[])
 {
-    wtap *wth;
-    int i, j, err;
-    gchar *err_info;
-    int opt;
-
-    char *p;
-    guint32 snaplen = 0;                /* No limit               */
-    int choplen_begin = 0;              /* No chop at beginning   */
-    int choplen_end = 0;                /* No chop at end         */
-    int chopoff_begin_pos = 0, chopoff_begin_neg = 0;/* Offsets for chopping from beginning */
-    int chopoff_end_pos = 0, chopoff_end_neg = 0;    /* Offset for chopping from end */
-    gboolean adjlen = FALSE;
-    wtap_dumper *pdh = NULL;
-    unsigned int count = 1;
-    unsigned int duplicate_count = 0;
-    gint64 data_offset;
-    struct wtap_pkthdr snap_phdr;
-    const struct wtap_pkthdr *phdr;
-    int err_type;
-    wtapng_section_t *shb_hdr;
+    wtap         *wth;
+    int           i, j, err;
+    gchar        *err_info;
+    int           opt;
+
+    char         *p;
+    guint32       snaplen            = 0; /* No limit               */
+    chop_t        chop               = {0, 0, 0, 0, 0, 0}; /* No chop */
+    gboolean      adjlen             = FALSE;
+    wtap_dumper  *pdh                = NULL;
+    unsigned int  count              = 1;
+    unsigned int  duplicate_count    = 0;
+    gint64        data_offset;
+    int           err_type;
+    guint8       *buf;
+    guint32       read_count         = 0;
+    int           split_packet_count = 0;
+    int           written_count      = 0;
+    char         *filename           = NULL;
+    gboolean      ts_okay            = TRUE;
+    int           secs_per_block     = 0;
+    int           block_cnt          = 0;
+    nstime_t      block_start;
+    gchar        *fprefix            = NULL;
+    gchar        *fsuffix            = NULL;
+
+    const struct wtap_pkthdr    *phdr;
+    struct wtap_pkthdr           snap_phdr;
     wtapng_iface_descriptions_t *idb_inf;
-    guint8 *buf;
-    guint32 read_count = 0;
-    int split_packet_count = 0;
-    int written_count = 0;
-    char *filename = NULL;
-    gboolean ts_okay = TRUE;
-    int secs_per_block = 0;
-    int block_cnt = 0;
-    nstime_t block_start;
-    gchar *fprefix = NULL;
-    gchar *fsuffix = NULL;
-    char appname[100];
+    wtapng_section_t            *shb_hdr;
 
 #ifdef HAVE_PLUGINS
     char* init_progfile_dir_error;
@@ -894,8 +891,17 @@ main(int argc, char *argv[])
         g_warning("editcap: init_progfile_dir(): %s", init_progfile_dir_error);
         g_free(init_progfile_dir_error);
     } else {
+        /* Register all the plugin types we have. */
+        wtap_register_plugin_types(); /* Types known to libwiretap */
+
         init_report_err(failure_message,NULL,NULL,NULL);
-        init_plugins();
+
+        /* Scan for plugins.  This does *not* call their registration routines;
+           that's done later. */
+        scan_plugins();
+
+        /* Register all libwiretap plugin modules. */
+        register_all_wiretap_modules();
     }
 #endif
 
@@ -973,17 +979,17 @@ main(int argc, char *argv[])
             }
 
             if (choplen > 0) {
-                choplen_begin += choplen;
+                chop.len_begin += choplen;
                 if (chopoff > 0)
-                    chopoff_begin_pos += chopoff;
+                    chop.off_begin_pos += chopoff;
                 else
-                    chopoff_begin_neg += chopoff;
+                    chop.off_begin_neg += chopoff;
             } else if (choplen < 0) {
-                choplen_end += choplen;
+                chop.len_end += choplen;
                 if (chopoff > 0)
-                    chopoff_end_pos += chopoff;
+                    chop.off_end_pos += chopoff;
                 else
-                    chopoff_end_neg += chopoff;
+                    chop.off_end_neg += chopoff;
             }
             break;
         }
@@ -1021,8 +1027,8 @@ main(int argc, char *argv[])
             break;
 
         case 'F':
-            out_file_type = wtap_short_string_to_file_type(optarg);
-            if (out_file_type < 0) {
+            out_file_type_subtype = wtap_short_string_to_file_type_subtype(optarg);
+            if (out_file_type_subtype < 0) {
                 fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
                         optarg);
                 list_capture_types();
@@ -1032,7 +1038,7 @@ main(int argc, char *argv[])
 
         case 'h':
             usage(FALSE);
-            exit(1);
+            exit(0);
             break;
 
         case 'i': /* break capture file based on time interval */
@@ -1109,7 +1115,7 @@ main(int argc, char *argv[])
     }
 
 #ifdef DEBUG
-    printf("Optind = %i, argc = %i\n", optind, argc);
+    fprintf(stderr, "Optind = %i, argc = %i\n", optind, argc);
 #endif
 
     if ((argc - optind) < 1) {
@@ -1160,7 +1166,7 @@ main(int argc, char *argv[])
 
     if (verbose) {
         fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
-                wtap_file_type_string(wtap_file_type(wth)));
+                wtap_file_type_subtype_string(wtap_file_type_subtype(wth)));
     }
 
     shb_hdr = wtap_file_get_shb_info(wth);
@@ -1208,11 +1214,10 @@ main(int argc, char *argv[])
 
                 /* If we don't have an application name add Editcap */
                 if (shb_hdr->shb_user_appl == NULL) {
-                    g_snprintf(appname, sizeof(appname), "Editcap " VERSION);
-                    shb_hdr->shb_user_appl = appname;
+                    shb_hdr->shb_user_appl = "Editcap " VERSION;
                 }
 
-                pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
+                pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
                                         snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
                                         FALSE /* compressed */, shb_hdr, idb_inf, &err);
 
@@ -1243,7 +1248,7 @@ main(int argc, char *argv[])
                     if (verbose)
                         fprintf(stderr, "Continuing writing in file %s\n", filename);
 
-                    pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
+                    pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
                                             snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
                                             FALSE /* compressed */, shb_hdr, idb_inf, &err);
 
@@ -1271,7 +1276,7 @@ main(int argc, char *argv[])
                     if (verbose)
                         fprintf(stderr, "Continuing writing in file %s\n", filename);
 
-                    pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
+                    pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
                                             snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
                                             FALSE /* compressed */, shb_hdr, idb_inf, &err);
                     if (pdh == NULL) {
@@ -1289,7 +1294,7 @@ main(int argc, char *argv[])
                             || (selected(count) && keep_em))) {
 
                 if (verbose && !dup_detect && !dup_detect_by_time)
-                    printf("Packet: %u\n", count);
+                    fprintf(stderr, "Packet: %u\n", count);
 
                 /* We simply write it, perhaps after truncating it; we could
                  * do other things, like modify it. */
@@ -1310,76 +1315,9 @@ main(int argc, char *argv[])
                 }
 
                 /* CHOP */
-                /* If we're not chopping anything from one side, then the
-                 * offset for that side is meaningless. */
-                if (choplen_begin == 0)
-                    chopoff_begin_pos = chopoff_begin_neg = 0;
-                if (choplen_end == 0)
-                    chopoff_end_pos = chopoff_end_neg = 0;
-
-                if (chopoff_begin_neg < 0) {
-                    chopoff_begin_pos += phdr->caplen + chopoff_begin_neg;
-                    chopoff_begin_neg = 0;
-                }
-                if (chopoff_end_pos > 0) {
-                    chopoff_end_neg += chopoff_end_pos - phdr->caplen;
-                    chopoff_end_pos = 0;
-                }
-
-                /* Make sure we don't chop off more than we have available */
-                if (phdr->caplen < (guint32)(chopoff_begin_pos - chopoff_end_neg)) {
-                    choplen_begin = 0;
-                    choplen_end = 0;
-                }
-                if ((guint32)(choplen_begin - choplen_end) >
-                    (phdr->caplen - (guint32)(chopoff_begin_pos - chopoff_end_neg))) {
-                    choplen_begin = phdr->caplen - (chopoff_begin_pos - chopoff_end_neg);
-                    choplen_end = 0;
-                }
-
-                /* Handle chopping from the beginning.  Note that if a
-                 * beginning offset was specified, we need to keep that piece */
-                if (choplen_begin > 0) {
-                    snap_phdr = *phdr;
-
-                    if (chopoff_begin_pos > 0) {
-                        memmove(&buf[chopoff_begin_pos],
-                                &buf[chopoff_begin_pos + choplen_begin],
-                                snap_phdr.caplen - choplen_begin);
-                    } else {
-                        buf += choplen_begin;
-                    }
-                    snap_phdr.caplen -= choplen_begin;
-
-                    if (adjlen) {
-                        if (phdr->len > (guint32)choplen_begin)
-                            snap_phdr.len -= choplen_begin;
-                        else
-                            snap_phdr.len = 0;
-                    }
-                    phdr = &snap_phdr;
-                }
-
-                /* Handle chopping from the end.  Note that if an ending offset
-                 * was specified, we need to keep that piece */
-                if (choplen_end < 0) {
-                    snap_phdr = *phdr;
-
-                    if (chopoff_end_neg < 0) {
-                        memmove(&buf[(gint)snap_phdr.caplen + (choplen_end + chopoff_end_neg)],
-                                &buf[(gint)snap_phdr.caplen + chopoff_end_neg], -
-                                chopoff_end_neg);
-                    }
-                    snap_phdr.caplen += choplen_end;
-
-                    if (adjlen) {
-                        if (((signed int) phdr->len + choplen_end) > 0)
-                            snap_phdr.len += choplen_end;
-                        else
-                            snap_phdr.len = 0;
-                    }
-                    phdr = &snap_phdr;
-                }
+                snap_phdr = *phdr;
+                handle_chopping(chop, &snap_phdr, phdr, &buf, adjlen);
+                phdr = &snap_phdr;
 
                 /* Do we adjust timestamps to ensure strict chronological
                  * order? */
@@ -1402,7 +1340,7 @@ main(int argc, char *argv[])
                                  * situation since trace files usually have packets in
                                  * chronological order (oldest to newest).
                                  */
-                                /* printf("++out of order, need to adjust this packet!\n"); */
+                                /* fprintf(stderr, "++out of order, need to adjust this packet!\n"); */
                                 snap_phdr = *phdr;
                                 snap_phdr.ts.secs = previous_time.secs + strict_time_adj.tv.tv_sec;
                                 snap_phdr.ts.nsecs = previous_time.nsecs;
@@ -1475,24 +1413,24 @@ main(int argc, char *argv[])
                 if (dup_detect) {
                     if (is_duplicate(buf, phdr->caplen)) {
                         if (verbose) {
-                            fprintf(stdout, "Skipped: %u, Len: %u, MD5 Hash: ",
+                            fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
                                     count, phdr->caplen);
                             for (i = 0; i < 16; i++)
-                                fprintf(stdout, "%02x",
+                                fprintf(stderr, "%02x",
                                         (unsigned char)fd_hash[cur_dup_entry].digest[i]);
-                            fprintf(stdout, "\n");
+                            fprintf(stderr, "\n");
                         }
                         duplicate_count++;
                         count++;
                         continue;
                     } else {
                         if (verbose) {
-                            fprintf(stdout, "Packet: %u, Len: %u, MD5 Hash: ",
+                            fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
                                     count, phdr->caplen);
                             for (i = 0; i < 16; i++)
-                                fprintf(stdout, "%02x",
+                                fprintf(stderr, "%02x",
                                         (unsigned char)fd_hash[cur_dup_entry].digest[i]);
-                            fprintf(stdout, "\n");
+                            fprintf(stderr, "\n");
                         }
                     }
                 }
@@ -1501,29 +1439,29 @@ main(int argc, char *argv[])
                 if (dup_detect_by_time) {
                     nstime_t current;
 
-                    current.secs = phdr->ts.secs;
+                    current.secs  = phdr->ts.secs;
                     current.nsecs = phdr->ts.nsecs;
 
                     if (is_duplicate_rel_time(buf, phdr->caplen, &current)) {
                         if (verbose) {
-                            fprintf(stdout, "Skipped: %u, Len: %u, MD5 Hash: ",
+                            fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
                                     count, phdr->caplen);
                             for (i = 0; i < 16; i++)
-                                fprintf(stdout, "%02x",
+                                fprintf(stderr, "%02x",
                                         (unsigned char)fd_hash[cur_dup_entry].digest[i]);
-                            fprintf(stdout, "\n");
+                            fprintf(stderr, "\n");
                         }
                         duplicate_count++;
                         count++;
                         continue;
                     } else {
                         if (verbose) {
-                            fprintf(stdout, "Packet: %u, Len: %u, MD5 Hash: ",
+                            fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
                                     count, phdr->caplen);
                             for (i = 0; i < 16; i++)
-                                fprintf(stdout, "%02x",
+                                fprintf(stderr, "%02x",
                                         (unsigned char)fd_hash[cur_dup_entry].digest[i]);
-                            fprintf(stdout, "\n");
+                            fprintf(stderr, "\n");
                         }
                     }
                 }
@@ -1533,7 +1471,7 @@ main(int argc, char *argv[])
                     int real_data_start = 0;
 
                     /* Protect non-protocol data */
-                    if (wtap_file_type(wth) == WTAP_FILE_CATAPULT_DCT2000)
+                    if (wtap_file_type_subtype(wth) == WTAP_FILE_TYPE_SUBTYPE_CATAPULT_DCT2000)
                         real_data_start = find_dct2000_real_data(buf);
 
                     for (i = real_data_start; i < (int) phdr->caplen; i++) {
@@ -1583,11 +1521,27 @@ main(int argc, char *argv[])
                     case WTAP_ERR_UNSUPPORTED_ENCAP:
                         /*
                          * This is a problem with the particular frame we're
-                         * writing; note that, and give the frame number.
+                         * writing and the file type and subtype we're
+                         * writing; note that, and report the frame number
+                         * and file type/subtype.
                          */
                         fprintf(stderr,
-                                "editcap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
-                                read_count, argv[optind]);
+                                "editcap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file\n.",
+                                read_count, argv[optind],
+                                wtap_file_type_subtype_string(out_file_type_subtype));
+                        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.
+                         */
+                        fprintf(stderr,
+                                "editcap: Frame %u of \"%s\" is too large for a \"%s\" file\n.",
+                                read_count, argv[optind],
+                                wtap_file_type_subtype_string(out_file_type_subtype));
                         break;
 
                     default:
@@ -1627,7 +1581,7 @@ main(int argc, char *argv[])
             g_free (filename);
             filename = g_strdup(argv[optind+1]);
 
-            pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
+            pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
                                     snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth),
                                     FALSE /* compressed */, shb_hdr, idb_inf, &err);
             if (pdh == NULL) {
@@ -1650,11 +1604,11 @@ main(int argc, char *argv[])
     }
 
     if (dup_detect) {
-        fprintf(stdout, "%u packet%s seen, %u packet%s skipped with duplicate window of %u packets.\n",
+        fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate window of %i packets.\n",
                 count - 1, plurality(count - 1, "", "s"), duplicate_count,
                 plurality(duplicate_count, "", "s"), dup_window);
     } else if (dup_detect_by_time) {
-        fprintf(stdout, "%u packet%s seen, %u packet%s skipped with duplicate time window equal to or less than %ld.%09ld seconds.\n",
+        fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate time window equal to or less than %ld.%09ld seconds.\n",
                 count - 1, plurality(count - 1, "", "s"), duplicate_count,
                 plurality(duplicate_count, "", "s"),
                 (long)relative_time_window.secs,
@@ -1687,16 +1641,113 @@ find_dct2000_real_data(guint8 *buf)
     return n;
 }
 
+/*
+ * We support up to 2 chopping regions in a single pass: one specified by the
+ * positive chop length, and one by the negative chop length.
+ */
+static void
+handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
+                const struct wtap_pkthdr *in_phdr, guint8 **buf,
+                gboolean adjlen)
+{
+    /* If we're not chopping anything from one side, then the offset for that
+     * side is meaningless. */
+    if (chop.len_begin == 0)
+        chop.off_begin_pos = chop.off_begin_neg = 0;
+    if (chop.len_end == 0)
+        chop.off_end_pos = chop.off_end_neg = 0;
+
+    if (chop.off_begin_neg < 0) {
+        chop.off_begin_pos += in_phdr->caplen + chop.off_begin_neg;
+        chop.off_begin_neg = 0;
+    }
+    if (chop.off_end_pos > 0) {
+        chop.off_end_neg += chop.off_end_pos - in_phdr->caplen;
+        chop.off_end_pos = 0;
+    }
+
+    /* If we've crossed chopping regions, swap them */
+    if (chop.len_begin && chop.len_end) {
+        if (chop.off_begin_pos > ((int)in_phdr->caplen + chop.off_end_neg)) {
+            int tmp_len, tmp_off;
+
+            tmp_off = in_phdr->caplen + chop.off_end_neg + chop.len_end;
+            tmp_len = -chop.len_end;
+
+            chop.off_end_neg = chop.len_begin + chop.off_begin_pos - in_phdr->caplen;
+            chop.len_end = -chop.len_begin;
+
+            chop.len_begin = tmp_len;
+            chop.off_begin_pos = tmp_off;
+        }
+    }
+
+    /* Make sure we don't chop off more than we have available */
+    if (in_phdr->caplen < (guint32)(chop.off_begin_pos - chop.off_end_neg)) {
+        chop.len_begin = 0;
+        chop.len_end = 0;
+    }
+    if ((guint32)(chop.len_begin - chop.len_end) >
+        (in_phdr->caplen - (guint32)(chop.off_begin_pos - chop.off_end_neg))) {
+        chop.len_begin = in_phdr->caplen - (chop.off_begin_pos - chop.off_end_neg);
+        chop.len_end = 0;
+    }
+
+    /* Handle chopping from the beginning.  Note that if a beginning offset
+     * was specified, we need to keep that piece */
+    if (chop.len_begin > 0) {
+        *out_phdr = *in_phdr;
+
+        if (chop.off_begin_pos > 0) {
+            memmove(*buf + chop.off_begin_pos,
+                    *buf + chop.off_begin_pos + chop.len_begin,
+                    out_phdr->caplen - chop.len_begin);
+        } else {
+            *buf += chop.len_begin;
+        }
+        out_phdr->caplen -= chop.len_begin;
+
+        if (adjlen) {
+            if (in_phdr->len > (guint32)chop.len_begin)
+                out_phdr->len -= chop.len_begin;
+            else
+                out_phdr->len = 0;
+        }
+        in_phdr = out_phdr;
+    }
+
+    /* Handle chopping from the end.  Note that if an ending offset was
+     * specified, we need to keep that piece */
+    if (chop.len_end < 0) {
+        *out_phdr = *in_phdr;
+
+        if (chop.off_end_neg < 0) {
+            memmove(*buf + (gint)out_phdr->caplen + (chop.len_end + chop.off_end_neg),
+                    *buf + (gint)out_phdr->caplen + chop.off_end_neg,
+                    -chop.off_end_neg);
+        }
+        out_phdr->caplen += chop.len_end;
+
+        if (adjlen) {
+            if (((signed int) in_phdr->len + chop.len_end) > 0)
+                out_phdr->len += chop.len_end;
+            else
+                out_phdr->len = 0;
+        }
+        /*in_phdr = out_phdr;*/
+    }
+}
+
 /*
  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
  *
  * Local variables:
  * c-basic-offset: 4
- * tab-width: 4
+ * tab-width: 8
  * indent-tabs-mode: nil
  * End:
  *
- * vi: set shiftwidth=4 tabstop=4 expandtab:
- * :indentSize=4:tabSize=4:noTabs=true:
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
  */