Copy change from Samba repository:
[metze/wireshark/wip.git] / editcap.c
index ea9a1656e4e04132132eb764dc8e284dafbde313..cc1a0c9d386f87b002ef616468bfd467e049a4e4 100644 (file)
--- a/editcap.c
+++ b/editcap.c
@@ -1,4 +1,5 @@
-/* Edit capture files.  We can delete packets, adjust timestamps, or
+/* editcap.c
+ * Edit capture files.  We can delete packets, adjust timestamps, or
  * simply convert from one format to another format.
  *
  * Originally written by Richard Sharpe.
@@ -63,7 +64,6 @@
 #endif
 
 #ifdef _WIN32
-#include <wsutil/file_util.h>
 #include <wsutil/unicode-utils.h>
 #include <process.h>    /* getpid */
 #ifdef HAVE_WINSOCK2_H
 
 #include <wsutil/crash_info.h>
 #include <wsutil/filesystem.h>
+#include <wsutil/file_util.h>
 #include <wsutil/md5.h>
 #include <wsutil/plugins.h>
 #include <wsutil/privileges.h>
 #include <wsutil/report_err.h>
 #include <wsutil/strnatcmp.h>
 #include <wsutil/str_util.h>
-#include <wsutil/ws_version_info.h>
+#include <ws_version_info.h>
 #include <wsutil/pint.h>
 #include <wiretap/wtap_opttypes.h>
 #include <wiretap/pcapng.h>
@@ -283,11 +284,11 @@ add_selection(char *sel, guint* max_selection)
 
         selectfrm[max_selected].inclusive = FALSE;
         selectfrm[max_selected].first = (guint)strtoul(sel, NULL, 10);
-        if (selectfrm[max_selected].first < *max_selection)
+        if (selectfrm[max_selected].first > *max_selection)
             *max_selection = selectfrm[max_selected].first;
 
         if (verbose)
-            fprintf(stderr, " %i\n", selectfrm[max_selected].first);
+            fprintf(stderr, " %u\n", selectfrm[max_selected].first);
     } else {
         if (verbose)
             fprintf(stderr, "Inclusive ...");
@@ -302,11 +303,11 @@ add_selection(char *sel, guint* max_selection)
             /* Not a valid number, presume all */
             selectfrm[max_selected].second = *max_selection = G_MAXUINT;
         }
-        else if (selectfrm[max_selected].second < *max_selection)
+        else if (selectfrm[max_selected].second > *max_selection)
             *max_selection = selectfrm[max_selected].second;
 
         if (verbose)
-            fprintf(stderr, " %i, %i\n", selectfrm[max_selected].first,
+            fprintf(stderr, " %u, %u\n", selectfrm[max_selected].first,
                    selectfrm[max_selected].second);
     }
 
@@ -736,9 +737,9 @@ print_usage(FILE *output)
     fprintf(output, "                         given time (format as YYYY-MM-DD hh:mm:ss).\n");
     fprintf(output, "\n");
     fprintf(output, "Duplicate packet removal:\n");
-    fprintf(output, "  --novlan                remove vlan info from packets before checking for duplicates.\n");
+    fprintf(output, "  --novlan               remove vlan info from packets before checking for duplicates.\n");
     fprintf(output, "  -d                     remove packet if duplicate (window == %d).\n", DEFAULT_DUP_DEPTH);
-    fprintf(output, "  -D <dup window>        remove packet if duplicate; configurable <dup window>\n");
+    fprintf(output, "  -D <dup window>        remove packet if duplicate; configurable <dup window>.\n");
     fprintf(output, "                         Valid <dup window> values are 0 to %d.\n", MAX_DUP_DEPTH);
     fprintf(output, "                         NOTE: A <dup window> of 0 with -v (verbose option) is\n");
     fprintf(output, "                         useful to print MD5 hashes.\n");
@@ -746,14 +747,14 @@ print_usage(FILE *output)
     fprintf(output, "                         LESS THAN <dup time window> prior to current packet.\n");
     fprintf(output, "                         A <dup time window> is specified in relative seconds\n");
     fprintf(output, "                         (e.g. 0.000001).\n");
-    fprintf(output, "  -a <framenum>:<comment>  Add or replace comment for given frame number\n");
+    fprintf(output, "  -a <framenum>:<comment> Add or replace comment for given frame number\n");
     fprintf(output, "\n");
     fprintf(output, "  -I <bytes to ignore>   ignore the specified bytes at the beginning of\n");
-    fprintf(output, "                         the frame during MD5 hash calculation\n");
+    fprintf(output, "                         the frame during MD5 hash calculation.\n");
     fprintf(output, "                         Useful to remove duplicated packets taken on\n");
-    fprintf(output, "                         several routers(differents mac addresses for \n");
-    fprintf(output, "                         example)\n");
-    fprintf(output, "                         e.g. -I 26 in case of Ether/IP/ will ignore \n");
+    fprintf(output, "                         several routers (different mac addresses for\n");
+    fprintf(output, "                         example).\n");
+    fprintf(output, "                         e.g. -I 26 in case of Ether/IP/ will ignore\n");
     fprintf(output, "                         ether(14) and IP header(20 - 4(src ip) - 4(dst ip)).\n");
     fprintf(output, "\n");
     fprintf(output, "           NOTE: The use of the 'Duplicate packet removal' options with\n");
@@ -773,8 +774,8 @@ print_usage(FILE *output)
     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 (i.e. reported) length when chopping\n");
-    fprintf(output, "                         and/or snapping\n");
-    fprintf(output, "  -t <time adjustment>   adjust the timestamp of each packet;\n");
+    fprintf(output, "                         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");
     fprintf(output, "  -S <strict adjustment> adjust timestamp of packets if necessary to insure\n");
     fprintf(output, "                         strict chronological increasing order. The <strict\n");
@@ -905,9 +906,9 @@ failure_message(const char *msg_format _U_, va_list ap _U_)
 
 static wtap_dumper *
 editcap_dump_open(const char *filename, guint32 snaplen,
-                  wtap_optionblock_t shb_hdr,
+                  GArray* shb_hdrs,
                   wtapng_iface_descriptions_t *idb_inf,
-                  wtap_optionblock_t nrb_hdr, int *write_err)
+                  GArray* nrb_hdrs, int *write_err)
 {
   wtap_dumper *pdh;
 
@@ -915,13 +916,11 @@ editcap_dump_open(const char *filename, guint32 snaplen,
     /* Write to the standard output. */
     pdh = wtap_dump_open_stdout_ng(out_file_type_subtype, out_frame_type,
                                    snaplen, FALSE /* compressed */,
-                                   shb_hdr, idb_inf, nrb_hdr,
-                                   write_err);
+                                   shb_hdrs, idb_inf, nrb_hdrs, write_err);
   } else {
     pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
                             snaplen, FALSE /* compressed */,
-                            shb_hdr, idb_inf, nrb_hdr,
-                           write_err);
+                            shb_hdrs, idb_inf, nrb_hdrs, write_err);
   }
   return pdh;
 }
@@ -963,12 +962,12 @@ main(int argc, char *argv[])
     gchar        *fprefix            = NULL;
     gchar        *fsuffix            = NULL;
     guint32       change_offset      = 0;
-    guint         max_packet_number  = G_MAXUINT;
+    guint         max_packet_number  = 0;
     const struct wtap_pkthdr    *phdr;
     struct wtap_pkthdr           temp_phdr;
     wtapng_iface_descriptions_t *idb_inf = NULL;
-    wtap_optionblock_t           shb_hdr = NULL;
-    wtap_optionblock_t           nrb_hdr = NULL;
+    GArray                      *shb_hdrs = NULL;
+    GArray                      *nrb_hdrs = NULL;
     char                        *shb_user_appl;
 
 #ifdef HAVE_PLUGINS
@@ -1165,7 +1164,7 @@ main(int argc, char *argv[])
                         optarg);
                 exit(1);
             }
-            srand( (unsigned int) (time(NULL) + getpid()) );
+            srand( (unsigned int) (time(NULL) + ws_getpid()) );
             break;
 
         case 'F':
@@ -1330,9 +1329,9 @@ main(int argc, char *argv[])
                 wtap_file_type_subtype_string(wtap_file_type_subtype(wth)));
     }
 
-    shb_hdr = wtap_file_get_shb_for_new_file(wth);
+    shb_hdrs = wtap_file_get_shb_for_new_file(wth);
     idb_inf = wtap_file_get_idb_info(wth);
-    nrb_hdr = wtap_file_get_nrb_for_new_file(wth);
+    nrb_hdrs = wtap_file_get_nrb_for_new_file(wth);
 
     /*
      * Now, process the rest, if any ... we only write if there is an extra
@@ -1380,16 +1379,13 @@ main(int argc, char *argv[])
                 g_assert(filename);
 
                 /* If we don't have an application name add Editcap */
-                wtap_optionblock_get_option_string(shb_hdr, OPT_SHB_USERAPPL, &shb_user_appl);
-                if (shb_user_appl == NULL) {
-                    shb_user_appl = g_strdup("Editcap " VERSION);
-                    wtap_optionblock_set_option_string(shb_hdr, OPT_SHB_USERAPPL, shb_user_appl);
-                    g_free(shb_user_appl);
+                if (wtap_block_get_string_option_value(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) {
+                    wtap_block_add_string_option_format(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "Editcap " VERSION);
                 }
 
                 pdh = editcap_dump_open(filename,
                                         snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
-                                        shb_hdr, idb_inf, nrb_hdr, &write_err);
+                                        shb_hdrs, idb_inf, nrb_hdrs, &write_err);
 
                 if (pdh == NULL) {
                     fprintf(stderr, "editcap: Can't open or create %s: %s\n",
@@ -1430,7 +1426,7 @@ main(int argc, char *argv[])
 
                         pdh = editcap_dump_open(filename,
                                                 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
-                                                shb_hdr, idb_inf, nrb_hdr, &write_err);
+                                                shb_hdrs, idb_inf, nrb_hdrs, &write_err);
 
                         if (pdh == NULL) {
                             fprintf(stderr, "editcap: Can't open or create %s: %s\n",
@@ -1459,7 +1455,7 @@ main(int argc, char *argv[])
 
                     pdh = editcap_dump_open(filename,
                                             snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
-                                            shb_hdr, idb_inf, nrb_hdr, &write_err);
+                                            shb_hdrs, idb_inf, nrb_hdrs, &write_err);
                     if (pdh == NULL) {
                         fprintf(stderr, "editcap: Can't open or create %s: %s\n",
                                 filename, wtap_strerror(write_err));
@@ -1830,7 +1826,7 @@ main(int argc, char *argv[])
 
             pdh = editcap_dump_open(filename,
                                     snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth),
-                                    shb_hdr, idb_inf, nrb_hdr, &write_err);
+                                    shb_hdrs, idb_inf, nrb_hdrs, &write_err);
             if (pdh == NULL) {
                 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
                         filename, wtap_strerror(write_err));
@@ -1846,10 +1842,10 @@ main(int argc, char *argv[])
                     wtap_strerror(write_err));
             goto error_on_exit;
         }
-        wtap_optionblock_free(shb_hdr);
-        shb_hdr = NULL;
-        wtap_optionblock_free(nrb_hdr);
-        nrb_hdr = NULL;
+        wtap_block_array_free(shb_hdrs);
+        shb_hdrs = NULL;
+        wtap_block_array_free(nrb_hdrs);
+        nrb_hdrs = NULL;
         g_free(filename);
 
         if (frames_user_comments) {
@@ -1872,8 +1868,8 @@ main(int argc, char *argv[])
     return 0;
 
 error_on_exit:
-    wtap_optionblock_free(shb_hdr);
-    wtap_optionblock_free(nrb_hdr);
+    wtap_block_array_free(shb_hdrs);
+    wtap_block_array_free(nrb_hdrs);
     g_free(idb_inf);
     exit(2);
 }
@@ -2014,4 +2010,3 @@ handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
  * vi: set shiftwidth=4 tabstop=8 expandtab:
  * :indentSize=4:tabSize=8:noTabs=true:
  */
-