Minor proto_reg_handoff cleanup
[obnox/wireshark/wip.git] / editcap.c
1 /* Edit capture files.  We can delete packets, adjust timestamps, or
2  * simply convert from one format to another format.
3  *
4  * $Id$
5  *
6  * Originally written by Richard Sharpe.
7  * Improved by Guy Harris.
8  * Further improved by Richard Sharpe.
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdarg.h>
19
20 /*
21  * Just make sure we include the prototype for strptime as well
22  * (needed for glibc 2.2) but make sure we do this only if not
23  * yet defined.
24  */
25
26 #ifndef __USE_XOPEN
27 #  define __USE_XOPEN
28 #endif
29
30 #include <time.h>
31 #include <glib.h>
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37
38
39 #ifdef HAVE_SYS_TIME_H
40 #include <sys/time.h>
41 #endif
42
43 #include "wtap.h"
44
45 #ifdef NEED_GETOPT_H
46 #include "getopt.h"
47 #endif
48
49 #ifdef _WIN32
50 #include <process.h>    /* getpid */
51 #endif
52
53 #ifdef NEED_STRPTIME_H
54 # include "strptime.h"
55 #endif
56
57 #include "epan/crypt/crypt-md5.h"
58 #include "epan/plugins.h"
59 #include "epan/report_err.h"
60 #include "epan/filesystem.h"
61 #include <wsutil/privileges.h>
62 #include "epan/nstime.h"
63
64 #include "svnversion.h"
65
66 /*
67  * Some globals so we can pass things to various routines
68  */
69
70 struct select_item {
71
72   int inclusive;
73   int first, second;
74
75 };
76
77
78 /*
79  * Duplicate frame detection
80  */
81 typedef struct _fd_hash_t {
82   md5_byte_t digest[16];
83   guint32 len;
84 } fd_hash_t;
85
86 #define DUP_DEPTH 5
87 fd_hash_t fd_hash[DUP_DEPTH];
88 int cur_dup = 0;
89
90 #define ONE_MILLION 1000000
91
92 /* Weights of different errors we can introduce */
93 /* We should probably make these command-line arguments */
94 /* XXX - Should we add a bit-level error? */
95 #define ERR_WT_BIT   5  /* Flip a random bit */
96 #define ERR_WT_BYTE  5  /* Substitute a random byte */
97 #define ERR_WT_ALNUM 5  /* Substitute a random character in [A-Za-z0-9] */
98 #define ERR_WT_FMT   2  /* Substitute "%s" */
99 #define ERR_WT_AA    1  /* Fill the remainder of the buffer with 0xAA */
100 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
101
102 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
103 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
104
105
106 struct time_adjustment {
107   struct timeval tv;
108   int is_negative;
109 };
110
111 #define MAX_SELECTIONS 512
112 static struct select_item selectfrm[MAX_SELECTIONS];
113 static int max_selected = -1;
114 static int keep_em = 0;
115 static int out_file_type = WTAP_FILE_PCAP;   /* default to "libpcap"   */
116 static int out_frame_type = -2;              /* Leave frame type alone */
117 static int verbose = 0;                      /* Not so verbose         */
118 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
119 static double err_prob = 0.0;
120 static time_t starttime = 0;
121 static time_t stoptime = 0;
122 static gboolean check_startstop = FALSE;
123 static gboolean dup_detect = FALSE;
124
125 static int find_dct2000_real_data(guint8 *buf);
126
127 /* Add a selection item, a simple parser for now */
128 static gboolean
129 add_selection(char *sel)
130 {
131   char *locn;
132   char *next;
133
134   if (++max_selected >= MAX_SELECTIONS) {
135     /* Let the user know we stopped selecting */
136     printf("Out of room for packet selections!\n");
137     return(FALSE);
138   }
139
140   printf("Add_Selected: %s\n", sel);
141
142   if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
143
144     printf("Not inclusive ...");
145
146     selectfrm[max_selected].inclusive = 0;
147     selectfrm[max_selected].first = atoi(sel);
148
149     printf(" %i\n", selectfrm[max_selected].first);
150
151   }
152   else {
153
154     printf("Inclusive ...");
155
156     next = locn + 1;
157     selectfrm[max_selected].inclusive = 1;
158     selectfrm[max_selected].first = atoi(sel);
159     selectfrm[max_selected].second = atoi(next);
160
161     printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
162
163   }
164
165   return(TRUE);
166 }
167
168 /* Was the packet selected? */
169
170 static int
171 selected(int recno)
172 {
173   int i = 0;
174
175   for (i = 0; i<= max_selected; i++) {
176
177     if (selectfrm[i].inclusive) {
178       if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
179         return 1;
180     }
181     else {
182       if (recno == selectfrm[i].first)
183         return 1;
184     }
185   }
186
187   return 0;
188
189 }
190
191 /* is the packet in the selected timeframe */
192 static gboolean
193 check_timestamp(wtap *wth)
194 {
195   struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
196
197   return ( pkthdr->ts.secs >= starttime ) && ( pkthdr->ts.secs <= stoptime );
198 }
199
200 static void
201 set_time_adjustment(char *optarg)
202 {
203   char *frac, *end;
204   long val;
205   int frac_digits;
206
207   if (!optarg)
208     return;
209
210   /* skip leading whitespace */
211   while (*optarg == ' ' || *optarg == '\t') {
212       optarg++;
213   }
214
215   /* check for a negative adjustment */
216   if (*optarg == '-') {
217       time_adj.is_negative = 1;
218       optarg++;
219   }
220
221   /* collect whole number of seconds, if any */
222   if (*optarg == '.') {         /* only fractional (i.e., .5 is ok) */
223       val  = 0;
224       frac = optarg;
225   } else {
226       val = strtol(optarg, &frac, 10);
227       if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
228           fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
229                   optarg);
230           exit(1);
231       }
232       if (val < 0) {            /* implies '--' since we caught '-' above  */
233           fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
234                   optarg);
235           exit(1);
236       }
237   }
238   time_adj.tv.tv_sec = val;
239
240   /* now collect the partial seconds, if any */
241   if (*frac != '\0') {             /* chars left, so get fractional part */
242     val = strtol(&(frac[1]), &end, 10);
243     if (*frac != '.' || end == NULL || end == frac
244         || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
245       fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
246               optarg);
247       exit(1);
248     }
249   }
250   else {
251     return;                     /* no fractional digits */
252   }
253
254   /* adjust fractional portion from fractional to numerator
255    * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
256   if (frac && end) {            /* both are valid */
257     frac_digits = end - frac - 1;   /* fractional digit count (remember '.') */
258     while(frac_digits < 6) {    /* this is frac of 10^6 */
259       val *= 10;
260       frac_digits++;
261     }
262   }
263   time_adj.tv.tv_usec = val;
264 }
265
266 static gboolean
267 is_duplicate(guint8* fd, guint32 len) {
268   int i;
269   md5_state_t ms;
270
271   cur_dup++;
272   if (cur_dup >= DUP_DEPTH)
273     cur_dup = 0;
274
275   /* Calculate our digest */
276   md5_init(&ms);
277   md5_append(&ms, fd, len);
278   md5_finish(&ms, fd_hash[cur_dup].digest);
279
280   fd_hash[cur_dup].len = len;
281
282   /* Look for duplicates */
283   for (i = 0; i < DUP_DEPTH; i++) {
284     if (i == cur_dup)
285       continue;
286
287     if (fd_hash[i].len == fd_hash[cur_dup].len &&
288         memcmp(fd_hash[i].digest, fd_hash[cur_dup].digest, 16) == 0) {
289       return TRUE;
290     }
291   }
292
293   return FALSE;
294 }
295
296 static void
297 usage(void)
298 {
299   fprintf(stderr, "Editcap %s"
300 #ifdef SVNVERSION
301           " (" SVNVERSION ")"
302 #endif
303           "\n", VERSION);
304   fprintf(stderr, "Edit and/or translate the format of capture files.\n");
305   fprintf(stderr, "See http://www.wireshark.org for more information.\n");
306   fprintf(stderr, "\n");
307   fprintf(stderr, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
308   fprintf(stderr, "\n");
309   fprintf(stderr, "A single packet or a range of packets can be selected.\n");
310   fprintf(stderr, "\n");
311   fprintf(stderr, "Packet selection:\n");
312   fprintf(stderr, "  -r                     keep the selected packets, default is to delete them\n");
313   fprintf(stderr, "  -A <start time>        don't output packets whose timestamp is before the\n");
314   fprintf(stderr, "                         given time (format as YYYY-MM-DD hh:mm:ss)\n");
315   fprintf(stderr, "  -B <stop time>         don't output packets whose timestamp is after the\n");
316   fprintf(stderr, "                         given time (format as YYYY-MM-DD hh:mm:ss)\n");
317   fprintf(stderr, "  -d                     remove duplicate packets\n");
318   fprintf(stderr, "\n");
319   fprintf(stderr, "Packet manipulation:\n");
320   fprintf(stderr, "  -s <snaplen>           truncate each packet to max. <snaplen> bytes of data\n");
321   fprintf(stderr, "  -C <choplen>           chop each packet at the end by <choplen> bytes\n");
322   fprintf(stderr, "  -t <time adjustment>   adjust the timestamp of each packet,\n");
323   fprintf(stderr, "                         <time adjustment> is in relative seconds (e.g. -0.5)\n");
324   fprintf(stderr, "  -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
325   fprintf(stderr, "                         that a particular packet byte will be randomly changed\n");
326   fprintf(stderr, "\n");
327   fprintf(stderr, "Output File(s):\n");
328   fprintf(stderr, "  -c <packets per file>  split the packet output to different files,\n");
329   fprintf(stderr, "                         based on uniform packet counts \n");
330   fprintf(stderr, "                         with a maximum of <packets per file> each\n");
331   fprintf(stderr, "  -i <seconds per file>  split the packet output to different files,\n");
332   fprintf(stderr, "                         based on uniform time intervals \n");
333   fprintf(stderr, "                         with a maximum of <seconds per file> each\n");
334   fprintf(stderr, "  -F <capture type>      set the output file type, default is libpcap\n");
335   fprintf(stderr, "                         an empty \"-F\" option will list the file types\n");
336   fprintf(stderr, "  -T <encap type>        set the output file encapsulation type,\n");
337   fprintf(stderr, "                         default is the same as the input file\n");
338   fprintf(stderr, "                         an empty \"-T\" option will list the encapsulation types\n");
339   fprintf(stderr, "\n");
340   fprintf(stderr, "Miscellaneous:\n");
341   fprintf(stderr, "  -h                     display this help and exit\n");
342   fprintf(stderr, "  -v                     verbose output\n");
343   fprintf(stderr, "\n");
344 }
345
346 static void
347 list_capture_types(void) {
348     int i;
349
350     fprintf(stderr, "editcap: The available capture file types for \"F\":\n");
351     for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
352       if (wtap_dump_can_open(i))
353         fprintf(stderr, "    %s - %s\n",
354           wtap_file_type_short_string(i), wtap_file_type_string(i));
355     }
356 }
357
358 static void
359 list_encap_types(void) {
360     int i;
361     const char *string;
362
363     fprintf(stderr, "editcap: The available encapsulation types for \"T\":\n");
364     for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
365         string = wtap_encap_short_string(i);
366         if (string != NULL)
367           fprintf(stderr, "    %s - %s\n",
368             string, wtap_encap_string(i));
369     }
370 }
371
372 /*
373  *  Don't report failures to load plugins because most (non-wiretap) plugins
374  *  *should* fail to load (because we're not linked against libwireshark and
375  *  dissector plugins need libwireshark).
376  */
377 static void
378 failure_message(const char *msg_format _U_, va_list ap _U_)
379 {
380         return;
381 }
382
383 int
384 main(int argc, char *argv[])
385 {
386   wtap *wth;
387   int i, j, err;
388   gchar *err_info;
389   extern char *optarg;
390   extern int optind;
391   int opt;
392   char *p;
393   unsigned int snaplen = 0;             /* No limit               */
394   unsigned int choplen = 0;             /* No chop                */
395   wtap_dumper *pdh;
396   int count = 1;
397   gint64 data_offset;
398   struct wtap_pkthdr snap_phdr;
399   const struct wtap_pkthdr *phdr;
400   int err_type;
401   guint8 *buf;
402   int split_packet_count = 0;
403   int written_count = 0;
404   char *filename;
405   size_t filenamelen = 0;
406   gboolean check_ts;
407   int secs_per_block = 0;
408   int block_cnt = 0;
409   nstime_t block_start;
410
411 #ifdef HAVE_PLUGINS
412   char* init_progfile_dir_error;
413 #endif
414
415   /*
416    * Get credential information for later use.
417    */
418   get_credential_info();
419
420 #ifdef HAVE_PLUGINS
421   /* Register wiretap plugins */
422   if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
423     g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
424     g_free(init_progfile_dir_error);
425   } else {
426     init_report_err(failure_message,NULL,NULL);
427     init_plugins();
428   }
429 #endif
430
431   /* Process the options */
432   while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:i:t:T:v")) !=-1) {
433
434     switch (opt) {
435
436     case 'E':
437       err_prob = strtod(optarg, &p);
438       if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
439         fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
440             optarg);
441         exit(1);
442       }
443       srand( (unsigned int) (time(NULL) + getpid()) );
444       break;
445
446     case 'F':
447       out_file_type = wtap_short_string_to_file_type(optarg);
448       if (out_file_type < 0) {
449         fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
450             optarg);
451         list_capture_types();
452         exit(1);
453       }
454       break;
455
456     case 'c':
457       split_packet_count = strtol(optarg, &p, 10);
458       if (p == optarg || *p != '\0') {
459         fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
460             optarg);
461         exit(1);
462       }
463       if (split_packet_count <= 0) {
464         fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
465             split_packet_count);
466         exit(1);
467       }
468       break;
469
470     case 'C':
471       choplen = strtol(optarg, &p, 10);
472       if (p == optarg || *p != '\0') {
473         fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
474             optarg);
475         exit(1);
476       }
477       break;
478
479     case 'd':
480       dup_detect = TRUE;
481       for (i = 0; i < DUP_DEPTH; i++) {
482         memset(&fd_hash[i].digest, 0, 16);
483         fd_hash[i].len = 0;
484       }
485       break;
486
487     case '?':              /* Bad options if GNU getopt */
488       switch(optopt) {
489       case'F':
490         list_capture_types();
491         break;
492       case'T':
493         list_encap_types();
494         break;
495       default:
496         usage();
497       }
498       exit(1);
499       break;
500
501     case 'h':
502       usage();
503       exit(1);
504       break;
505
506     case 'r':
507       keep_em = !keep_em;  /* Just invert */
508       break;
509
510     case 's':
511       snaplen = strtol(optarg, &p, 10);
512       if (p == optarg || *p != '\0') {
513         fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
514                 optarg);
515         exit(1);
516       }
517       break;
518
519     case 't':
520       set_time_adjustment(optarg);
521       break;
522
523     case 'T':
524       out_frame_type = wtap_short_string_to_encap(optarg);
525       if (out_frame_type < 0) {
526         fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
527             optarg);
528         list_encap_types();
529         exit(1);
530       }
531       break;
532
533     case 'v':
534       verbose = !verbose;  /* Just invert */
535       break;
536
537     case 'i': /* break capture file based on time interval */
538       secs_per_block = atoi(optarg);
539       nstime_set_unset(&block_start);
540       if(secs_per_block <= 0) {
541         fprintf(stderr, "editcap: \"%s\" isn't a valid time interval\n\n", optarg);
542         exit(1);
543         }
544       break;
545
546     case 'A':
547     {
548       struct tm starttm;
549
550       memset(&starttm,0,sizeof(struct tm));
551
552       if(!strptime(optarg,"%Y-%m-%d %T",&starttm)) {
553         fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n", optarg);
554         exit(1);
555       }
556
557       check_startstop = TRUE;
558       starttm.tm_isdst = -1;
559
560       starttime = mktime(&starttm);
561       break;
562     }
563
564     case 'B':
565     {
566       struct tm stoptm;
567
568       memset(&stoptm,0,sizeof(struct tm));
569
570       if(!strptime(optarg,"%Y-%m-%d %T",&stoptm)) {
571         fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n", optarg);
572         exit(1);
573       }
574       check_startstop = TRUE;
575       stoptm.tm_isdst = -1;
576       stoptime = mktime(&stoptm);
577       break;
578     }
579     }
580
581   }
582
583 #ifdef DEBUG
584   printf("Optind = %i, argc = %i\n", optind, argc);
585 #endif
586
587   if ((argc - optind) < 1) {
588
589     usage();
590     exit(1);
591
592   }
593
594   if (check_startstop && !stoptime) {
595     struct tm stoptm;
596     /* XXX: will work until 2035 */
597     memset(&stoptm,0,sizeof(struct tm));
598     stoptm.tm_year = 135;
599     stoptm.tm_mday = 31;
600     stoptm.tm_mon = 11;
601
602     stoptime = mktime(&stoptm);
603   }
604
605   if (starttime > stoptime) {
606     fprintf(stderr, "editcap: start time is after the stop time\n");
607     exit(1);
608   }
609
610   if (split_packet_count > 0 && secs_per_block > 0) {
611     fprintf(stderr, "editcap: can't split on both packet count and time interval\n");
612     fprintf(stderr, "editcap: at the same time\n");
613     exit(1);
614   }
615
616   wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
617
618   if (!wth) {
619     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
620         wtap_strerror(err));
621     switch (err) {
622
623     case WTAP_ERR_UNSUPPORTED:
624     case WTAP_ERR_UNSUPPORTED_ENCAP:
625     case WTAP_ERR_BAD_RECORD:
626       fprintf(stderr, "(%s)\n", err_info);
627       g_free(err_info);
628       break;
629     }
630     exit(1);
631
632   }
633
634   if (verbose) {
635     fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
636             wtap_file_type_string(wtap_file_type(wth)));
637   }
638
639   /*
640    * Now, process the rest, if any ... we only write if there is an extra
641    * argument or so ...
642    */
643
644   if ((argc - optind) >= 2) {
645
646     if (out_frame_type == -2)
647       out_frame_type = wtap_file_encap(wth);
648
649     if (split_packet_count > 0) {
650       filenamelen = strlen(argv[optind+1]) + 20;
651       filename = (char *) g_malloc(filenamelen);
652       if (!filename) {
653         exit(5);
654       }
655       g_snprintf(filename, filenamelen, "%s-%05d", argv[optind+1], 0);
656     } else {
657       if (secs_per_block > 0) {
658         filenamelen = strlen(argv[optind+1]) + 7;
659         filename = (char *) g_malloc(filenamelen);
660         if (!filename) {
661           exit(5);
662           }
663         g_snprintf(filename, filenamelen, "%s-%05d", argv[optind+1], block_cnt);
664         }
665       else {
666         filename = argv[optind+1];
667         }
668       }
669
670     pdh = wtap_dump_open(filename, out_file_type,
671         out_frame_type, wtap_snapshot_length(wth),
672         FALSE /* compressed */, &err);
673     if (pdh == NULL) {
674
675       fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
676               wtap_strerror(err));
677       exit(1);
678     }
679
680     for (i = optind + 2; i < argc; i++)
681       if (add_selection(argv[i]) == FALSE)
682         break;
683
684     while (wtap_read(wth, &err, &err_info, &data_offset)) {
685
686       if (secs_per_block > 0) {
687         phdr = wtap_phdr(wth);
688
689         if (nstime_is_unset(&block_start)) {  /* should only be the first packet */
690           block_start.secs = phdr->ts.secs;
691           block_start.nsecs = phdr->ts.nsecs; 
692           } 
693
694         while ((phdr->ts.secs - block_start.secs >  secs_per_block) || 
695             (phdr->ts.secs - block_start.secs == secs_per_block && 
696                 phdr->ts.nsecs >= block_start.nsecs )) { /* time for the next file */
697
698           if (!wtap_dump_close(pdh, &err)) {
699             fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
700                 wtap_strerror(err));
701             exit(1);
702             }
703           block_start.secs = block_start.secs +  secs_per_block; /* reset for next interval */
704           g_snprintf(filename, filenamelen, "%s-%05d",argv[optind+1], ++block_cnt);
705
706           if (verbose) {
707             fprintf(stderr, "Continuing writing in file %s\n", filename);
708             }
709
710           pdh = wtap_dump_open(filename, out_file_type,
711              out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
712
713           if (pdh == NULL) {
714             fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
715               wtap_strerror(err));
716             exit(1);
717           }
718         }
719       }
720
721       if (split_packet_count > 0 && (written_count % split_packet_count == 0)) {
722         if (!wtap_dump_close(pdh, &err)) {
723
724           fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
725               wtap_strerror(err));
726           exit(1);
727         }
728
729         g_snprintf(filename, filenamelen, "%s-%05d",argv[optind+1], count / split_packet_count);
730
731         if (verbose) {
732           fprintf(stderr, "Continuing writing in file %s\n", filename);
733         }
734
735         pdh = wtap_dump_open(filename, out_file_type,
736             out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
737         if (pdh == NULL) {
738
739           fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
740               wtap_strerror(err));
741           exit(1);
742         }
743       }
744
745       check_ts = check_timestamp(wth);
746
747       if ( ((check_startstop && check_ts) || (!check_startstop && !check_ts)) && ((!selected(count) && !keep_em) ||
748           (selected(count) && keep_em)) ) {
749
750         if (verbose)
751           printf("Packet: %u\n", count);
752
753         /* We simply write it, perhaps after truncating it; we could do other
754            things, like modify it. */
755
756         phdr = wtap_phdr(wth);
757
758         if (choplen != 0 && phdr->caplen > choplen) {
759           snap_phdr = *phdr;
760           snap_phdr.caplen -= choplen;
761           phdr = &snap_phdr;
762         }
763
764         if (snaplen != 0 && phdr->caplen > snaplen) {
765           snap_phdr = *phdr;
766           snap_phdr.caplen = snaplen;
767           phdr = &snap_phdr;
768         }
769
770         /* assume that if the frame's tv_sec is 0, then
771          * the timestamp isn't supported */
772         if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
773           snap_phdr = *phdr;
774           if (time_adj.is_negative)
775             snap_phdr.ts.secs -= time_adj.tv.tv_sec;
776           else
777             snap_phdr.ts.secs += time_adj.tv.tv_sec;
778           phdr = &snap_phdr;
779         }
780
781         /* assume that if the frame's tv_sec is 0, then
782          * the timestamp isn't supported */
783         if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
784           snap_phdr = *phdr;
785           if (time_adj.is_negative) { /* subtract */
786             if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
787               snap_phdr.ts.secs--;
788               snap_phdr.ts.nsecs += ONE_MILLION * 1000;
789             }
790             snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
791           } else {                  /* add */
792             if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
793               /* carry */
794               snap_phdr.ts.secs++;
795               snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
796             } else {
797               snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
798             }
799           }
800           phdr = &snap_phdr;
801         }
802
803         if (dup_detect) {
804           buf = wtap_buf_ptr(wth);
805           if (is_duplicate(buf, phdr->caplen)) {
806             if (verbose)
807               printf("Skipping duplicate: %u\n", count);
808             count++;
809             continue;
810           }
811         }
812
813         /* Random error mutation */
814         if (err_prob > 0.0) {
815           int real_data_start = 0;
816           buf = wtap_buf_ptr(wth);
817           /* Protect non-protocol data */
818           if (wtap_file_type(wth) == WTAP_FILE_CATAPULT_DCT2000) {
819             real_data_start = find_dct2000_real_data(buf);
820           }
821           for (i = real_data_start; i < (int) phdr->caplen; i++) {
822             if (rand() <= err_prob * RAND_MAX) {
823               err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
824
825               if (err_type < ERR_WT_BIT) {
826                 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
827                 err_type = ERR_WT_TOTAL;
828               } else {
829                 err_type -= ERR_WT_BYTE;
830               }
831
832               if (err_type < ERR_WT_BYTE) {
833                 buf[i] = rand() / (RAND_MAX / 255 + 1);
834                 err_type = ERR_WT_TOTAL;
835               } else {
836                 err_type -= ERR_WT_BYTE;
837               }
838
839               if (err_type < ERR_WT_ALNUM) {
840                 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
841                 err_type = ERR_WT_TOTAL;
842               } else {
843                 err_type -= ERR_WT_ALNUM;
844               }
845
846               if (err_type < ERR_WT_FMT) {
847                 if ((unsigned int)i < phdr->caplen - 2)
848                   strncpy((char*) &buf[i],  "%s", 2);
849                 err_type = ERR_WT_TOTAL;
850               } else {
851                 err_type -= ERR_WT_FMT;
852               }
853
854               if (err_type < ERR_WT_AA) {
855                 for (j = i; j < (int) phdr->caplen; j++) {
856                   buf[j] = 0xAA;
857                 }
858                 i = phdr->caplen;
859               }
860             }
861           }
862         }
863
864         if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
865                        &err)) {
866           fprintf(stderr, "editcap: Error writing to %s: %s\n",
867                   filename, wtap_strerror(err));
868           exit(1);
869         }
870         written_count++;
871       }
872       count++;
873     }
874
875     if (err != 0) {
876       /* Print a message noting that the read failed somewhere along the line. */
877       fprintf(stderr,
878               "editcap: An error occurred while reading \"%s\": %s.\n",
879               argv[optind], wtap_strerror(err));
880       switch (err) {
881
882       case WTAP_ERR_UNSUPPORTED:
883       case WTAP_ERR_UNSUPPORTED_ENCAP:
884       case WTAP_ERR_BAD_RECORD:
885         fprintf(stderr, "(%s)\n", err_info);
886         g_free(err_info);
887         break;
888       }
889     }
890
891     if (!wtap_dump_close(pdh, &err)) {
892
893       fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
894           wtap_strerror(err));
895       exit(1);
896
897     }
898   }
899
900   return 0;
901 }
902
903 /* Skip meta-information read from file to return offset of real
904    protocol data */
905 static int find_dct2000_real_data(guint8 *buf)
906 {
907   int n=0;
908
909   for (n=0; buf[n] != '\0'; n++);   /* Context name */
910   n++;
911   n++;                              /* Context port number */
912   for (; buf[n] != '\0'; n++);      /* Timestamp */
913   n++;
914   for (; buf[n] != '\0'; n++);      /* Protocol name */
915   n++;
916   for (; buf[n] != '\0'; n++);      /* Variant number (as string) */
917   n++;
918   for (; buf[n] != '\0'; n++);      /* Outhdr (as string) */
919   n++;
920   n += 2;                           /* Direction & encap */
921
922   return n;
923 }