Enhance Vendor Specific Atheros IE
[obnox/wireshark/wip.git] / editcap.c
index 0c54146401a71341d19a0f94dfe6aafe3937861f..770781b75ddca50ec525bb0a5b69b454b0d9a5f3 100644 (file)
--- a/editcap.c
+++ b/editcap.c
@@ -49,6 +49,8 @@
 #endif
 
 #ifdef _WIN32
+#include <windows.h>
+#include <shellapi.h>
 #include <process.h>    /* getpid */
 #ifdef HAVE_WINSOCK2_H
 #include <winsock2.h>
@@ -697,7 +699,9 @@ usage(gboolean is_error)
   fprintf(output, "\n");
   fprintf(output, "Packet manipulation:\n");
   fprintf(output, "  -s <snaplen>           truncate each packet to max. <snaplen> bytes of data.\n");
-  fprintf(output, "  -C <choplen>           chop each packet at the end by <choplen> bytes.\n");
+  fprintf(output, "  -C <choplen>           chop each packet by <choplen> bytes. Positive values\n");
+  fprintf(output, "                         chop at the packet beginning, negative values at the\n");
+  fprintf(output, "                         packet end.\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");
@@ -813,9 +817,15 @@ main(int argc, char *argv[])
   int i, j, err;
   gchar *err_info;
   int opt;
+
+#ifdef _WIN32
+  LPWSTR              *wc_argv;
+  int                  wc_argc;
+#endif  /* _WIN32 */
+
   char *p;
   unsigned int snaplen = 0;             /* No limit               */
-  unsigned int choplen = 0;             /* No chop                */
+  int choplen = 0;                      /* No chop                */
   wtap_dumper *pdh = NULL;
   int count = 1;
   unsigned duplicate_count = 0;
@@ -838,6 +848,16 @@ main(int argc, char *argv[])
   char* init_progfile_dir_error;
 #endif
 
+#ifdef _WIN32
+  /* Convert our arg list to UTF-8. */
+  wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc);
+  if (wc_argv && wc_argc == argc) {
+    for (i = 0; i < argc; i++) {
+      argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL);
+    }
+  } /* XXX else bail because something is horribly, horribly wrong? */
+#endif /* _WIN32 */
+
   /*
    * Get credential information for later use.
    */
@@ -894,7 +914,7 @@ main(int argc, char *argv[])
       break;
 
     case 'C':
-      choplen = strtol(optarg, &p, 10);
+         choplen = strtol(optarg, &p, 10);
       if (p == optarg || *p != '\0') {
         fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
             optarg);
@@ -1113,6 +1133,7 @@ main(int argc, char *argv[])
 
     while (wtap_read(wth, &err, &err_info, &data_offset)) {
       phdr = wtap_phdr(wth);
+      buf = wtap_buf_ptr(wth);
 
       if (nstime_is_unset(&block_start)) {  /* should only be the first packet */
         block_start.secs = phdr->ts.secs;
@@ -1210,9 +1231,17 @@ main(int argc, char *argv[])
 
         phdr = wtap_phdr(wth);
 
-        if (choplen != 0 && phdr->caplen > choplen) {
+        if (choplen < 0 && (phdr->caplen + choplen) > 0) {
+          snap_phdr = *phdr;
+          snap_phdr.caplen += choplen;
+          phdr = &snap_phdr;
+        }
+
+        if (choplen > 0 && phdr->caplen > (unsigned int) choplen) {
           snap_phdr = *phdr;
           snap_phdr.caplen -= choplen;
+          snap_phdr.len -= choplen;
+                 buf += choplen;
           phdr = &snap_phdr;
         }
 
@@ -1317,7 +1346,6 @@ main(int argc, char *argv[])
 
         /* suppress duplicates by packet window */
         if (dup_detect) {
-          buf = wtap_buf_ptr(wth);
           if (is_duplicate(buf, phdr->caplen)) {
             if (verbose) {
               fprintf(stdout, "Skipped: %u, Len: %u, MD5 Hash: ", count, phdr->caplen);
@@ -1347,8 +1375,6 @@ main(int argc, char *argv[])
           current.secs = phdr->ts.secs;
           current.nsecs = phdr->ts.nsecs;
 
-          buf = wtap_buf_ptr(wth);
-
           if (is_duplicate_rel_time(buf, phdr->caplen, &current)) {
             if (verbose) {
               fprintf(stdout, "Skipped: %u, Len: %u, MD5 Hash: ", count, phdr->caplen);
@@ -1374,7 +1400,6 @@ main(int argc, char *argv[])
         /* Random error mutation */
         if (err_prob > 0.0) {
           int real_data_start = 0;
-          buf = wtap_buf_ptr(wth);
           /* Protect non-protocol data */
           if (wtap_file_type(wth) == WTAP_FILE_CATAPULT_DCT2000) {
             real_data_start = find_dct2000_real_data(buf);
@@ -1422,8 +1447,7 @@ main(int argc, char *argv[])
           }
         }
 
-        if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
-                       &err)) {
+        if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), buf, &err)) {
           fprintf(stderr, "editcap: Error writing to %s: %s\n",
                   filename, wtap_strerror(err));
           exit(2);