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