1 /* Edit capture files. We can delete packets, adjust timestamps, or
2 * simply convert from one format to another format.
4 * Originally written by Richard Sharpe.
5 * Improved by Guy Harris.
6 * Further improved by Richard Sharpe.
8 * Copyright 2013, Richard Sharpe <realrichardsharpe[AT]gmail.com>
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1998 Gerald Combs
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 * Just make sure we include the prototype for strptime as well
38 * (needed for glibc 2.2) but make sure we do this only if not
53 #ifdef HAVE_SYS_TIME_H
60 #include "wsutil/wsgetopt.h"
64 #include <wsutil/file_util.h>
65 #include <wsutil/unicode-utils.h>
66 #include <process.h> /* getpid */
67 #ifdef HAVE_WINSOCK2_H
72 #ifdef NEED_STRPTIME_H
73 # include "wsutil/strptime.h"
76 #include <wsutil/privileges.h>
77 #include <wsutil/filesystem.h>
78 #include <wsutil/report_err.h>
79 #include <wsutil/strnatcmp.h>
80 #include <wsutil/md5.h>
81 #include <wsutil/plugins.h>
85 #include "ringbuffer.h" /* For RINGBUFFER_MAX_NUM_FILES */
88 * Some globals so we can pass things to various routines
97 * Duplicate frame detection
99 typedef struct _fd_hash_t {
100 md5_byte_t digest[16];
105 #define DEFAULT_DUP_DEPTH 5 /* Used with -d */
106 #define MAX_DUP_DEPTH 1000000 /* the maximum window (and actual size of fd_hash[]) for de-duplication */
108 static fd_hash_t fd_hash[MAX_DUP_DEPTH];
109 static int dup_window = DEFAULT_DUP_DEPTH;
110 static int cur_dup_entry = 0;
112 #define ONE_MILLION 1000000
113 #define ONE_BILLION 1000000000
115 /* Weights of different errors we can introduce */
116 /* We should probably make these command-line arguments */
117 /* XXX - Should we add a bit-level error? */
118 #define ERR_WT_BIT 5 /* Flip a random bit */
119 #define ERR_WT_BYTE 5 /* Substitute a random byte */
120 #define ERR_WT_ALNUM 5 /* Substitute a random character in [A-Za-z0-9] */
121 #define ERR_WT_FMT 2 /* Substitute "%s" */
122 #define ERR_WT_AA 1 /* Fill the remainder of the buffer with 0xAA */
123 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
125 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
126 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
128 struct time_adjustment {
133 typedef struct _chop_t {
142 #define MAX_SELECTIONS 512
143 static struct select_item selectfrm[MAX_SELECTIONS];
144 static int max_selected = -1;
145 static int keep_em = 0;
146 #ifdef PCAP_NG_DEFAULT
147 static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcapng */
149 static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcap */
151 static int out_frame_type = -2; /* Leave frame type alone */
152 static int verbose = 0; /* Not so verbose */
153 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
154 static nstime_t relative_time_window = {0, 0}; /* de-dup time window */
155 static double err_prob = 0.0;
156 static time_t starttime = 0;
157 static time_t stoptime = 0;
158 static gboolean check_startstop = FALSE;
159 static gboolean dup_detect = FALSE;
160 static gboolean dup_detect_by_time = FALSE;
162 static int do_strict_time_adjustment = FALSE;
163 static struct time_adjustment strict_time_adj = {{0, 0}, 0}; /* strict time adjustment */
164 static nstime_t previous_time = {0, 0}; /* previous time */
166 static int find_dct2000_real_data(guint8 *buf);
167 static void handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
168 const struct wtap_pkthdr *in_phdr, guint8 **buf,
172 abs_time_to_str_with_sec_resolution(const nstime_t *abs_time)
175 gchar *buf = (gchar *)g_malloc(16);
177 tmp = localtime(&abs_time->secs);
180 g_snprintf(buf, 16, "%d%02d%02d%02d%02d%02d",
195 fileset_get_filename_by_pattern(guint idx, const nstime_t *time_val,
196 gchar *fprefix, gchar *fsuffix)
202 timestr = abs_time_to_str_with_sec_resolution(time_val);
203 g_snprintf(filenum, sizeof(filenum), "%05u", idx % RINGBUFFER_MAX_NUM_FILES);
204 abs_str = g_strconcat(fprefix, "_", filenum, "_", timestr, fsuffix, NULL);
211 fileset_extract_prefix_suffix(const char *fname, gchar **fprefix, gchar **fsuffix)
213 char *pfx, *last_pathsep;
216 save_file = g_strdup(fname);
217 if (save_file == NULL) {
218 fprintf(stderr, "editcap: Out of memory\n");
222 last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
223 pfx = strrchr(save_file,'.');
224 if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
225 /* The pathname has a "." in it, and it's in the last component
226 * of the pathname (because there is either only one component,
227 * i.e. last_pathsep is null as there are no path separators,
228 * or the "." is after the path separator before the last
231 * Treat it as a separator between the rest of the file name and
232 * the file name suffix, and arrange that the names given to the
233 * ring buffer files have the specified suffix, i.e. put the
234 * changing part of the name *before* the suffix. */
236 *fprefix = g_strdup(save_file);
237 pfx[0] = '.'; /* restore capfile_name */
238 *fsuffix = g_strdup(pfx);
240 /* Either there's no "." in the pathname, or it's in a directory
241 * component, so the last component has no suffix. */
242 *fprefix = g_strdup(save_file);
249 /* Add a selection item, a simple parser for now */
251 add_selection(char *sel)
256 if (++max_selected >= MAX_SELECTIONS) {
257 /* Let the user know we stopped selecting */
258 fprintf(stderr, "Out of room for packet selections!\n");
263 fprintf(stderr, "Add_Selected: %s\n", sel);
265 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
267 fprintf(stderr, "Not inclusive ...");
269 selectfrm[max_selected].inclusive = 0;
270 selectfrm[max_selected].first = atoi(sel);
273 fprintf(stderr, " %i\n", selectfrm[max_selected].first);
276 fprintf(stderr, "Inclusive ...");
279 selectfrm[max_selected].inclusive = 1;
280 selectfrm[max_selected].first = atoi(sel);
281 selectfrm[max_selected].second = atoi(next);
284 fprintf(stderr, " %i, %i\n", selectfrm[max_selected].first,
285 selectfrm[max_selected].second);
291 /* Was the packet selected? */
298 for (i = 0; i <= max_selected; i++) {
299 if (selectfrm[i].inclusive) {
300 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
303 if (recno == selectfrm[i].first)
311 /* is the packet in the selected timeframe */
313 check_timestamp(wtap *wth)
315 struct wtap_pkthdr *pkthdr = wtap_phdr(wth);
317 return (pkthdr->ts.secs >= starttime) && (pkthdr->ts.secs < stoptime);
321 set_time_adjustment(char *optarg_str_p)
330 /* skip leading whitespace */
331 while (*optarg_str_p == ' ' || *optarg_str_p == '\t')
334 /* check for a negative adjustment */
335 if (*optarg_str_p == '-') {
336 time_adj.is_negative = 1;
340 /* collect whole number of seconds, if any */
341 if (*optarg_str_p == '.') { /* only fractional (i.e., .5 is ok) */
345 val = strtol(optarg_str_p, &frac, 10);
346 if (frac == NULL || frac == optarg_str_p
347 || val == LONG_MIN || val == LONG_MAX) {
348 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
352 if (val < 0) { /* implies '--' since we caught '-' above */
353 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
358 time_adj.tv.tv_sec = val;
360 /* now collect the partial seconds, if any */
361 if (*frac != '\0') { /* chars left, so get fractional part */
362 val = strtol(&(frac[1]), &end, 10);
363 /* if more than 6 fractional digits truncate to 6 */
364 if ((end - &(frac[1])) > 6) {
365 frac[7] = 't'; /* 't' for truncate */
366 val = strtol(&(frac[1]), &end, 10);
368 if (*frac != '.' || end == NULL || end == frac || val < 0
369 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
370 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
375 return; /* no fractional digits */
378 /* adjust fractional portion from fractional to numerator
379 * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
380 if (frac && end) { /* both are valid */
381 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
382 while(frac_digits < 6) { /* this is frac of 10^6 */
387 time_adj.tv.tv_usec = (int)val;
391 set_strict_time_adj(char *optarg_str_p)
400 /* skip leading whitespace */
401 while (*optarg_str_p == ' ' || *optarg_str_p == '\t')
405 * check for a negative adjustment
406 * A negative strict adjustment value is a flag
407 * to adjust all frames by the specifed delta time.
409 if (*optarg_str_p == '-') {
410 strict_time_adj.is_negative = 1;
414 /* collect whole number of seconds, if any */
415 if (*optarg_str_p == '.') { /* only fractional (i.e., .5 is ok) */
419 val = strtol(optarg_str_p, &frac, 10);
420 if (frac == NULL || frac == optarg_str_p
421 || val == LONG_MIN || val == LONG_MAX) {
422 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
426 if (val < 0) { /* implies '--' since we caught '-' above */
427 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
432 strict_time_adj.tv.tv_sec = val;
434 /* now collect the partial seconds, if any */
435 if (*frac != '\0') { /* chars left, so get fractional part */
436 val = strtol(&(frac[1]), &end, 10);
437 /* if more than 6 fractional digits truncate to 6 */
438 if ((end - &(frac[1])) > 6) {
439 frac[7] = 't'; /* 't' for truncate */
440 val = strtol(&(frac[1]), &end, 10);
442 if (*frac != '.' || end == NULL || end == frac || val < 0
443 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
444 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
449 return; /* no fractional digits */
452 /* adjust fractional portion from fractional to numerator
453 * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
454 if (frac && end) { /* both are valid */
455 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
456 while(frac_digits < 6) { /* this is frac of 10^6 */
461 strict_time_adj.tv.tv_usec = (int)val;
465 set_rel_time(char *optarg_str_p)
474 /* skip leading whitespace */
475 while (*optarg_str_p == ' ' || *optarg_str_p == '\t')
478 /* ignore negative adjustment */
479 if (*optarg_str_p == '-')
482 /* collect whole number of seconds, if any */
483 if (*optarg_str_p == '.') { /* only fractional (i.e., .5 is ok) */
487 val = strtol(optarg_str_p, &frac, 10);
488 if (frac == NULL || frac == optarg_str_p
489 || val == LONG_MIN || val == LONG_MAX) {
490 fprintf(stderr, "1: editcap: \"%s\" isn't a valid rel time value\n",
494 if (val < 0) { /* implies '--' since we caught '-' above */
495 fprintf(stderr, "2: editcap: \"%s\" isn't a valid rel time value\n",
500 relative_time_window.secs = val;
502 /* now collect the partial seconds, if any */
503 if (*frac != '\0') { /* chars left, so get fractional part */
504 val = strtol(&(frac[1]), &end, 10);
505 /* if more than 9 fractional digits truncate to 9 */
506 if ((end - &(frac[1])) > 9) {
507 frac[10] = 't'; /* 't' for truncate */
508 val = strtol(&(frac[1]), &end, 10);
510 if (*frac != '.' || end == NULL || end == frac || val < 0
511 || val > ONE_BILLION || val == LONG_MIN || val == LONG_MAX) {
512 fprintf(stderr, "3: editcap: \"%s\" isn't a valid rel time value\n",
517 return; /* no fractional digits */
520 /* adjust fractional portion from fractional to numerator
521 * e.g., in "1.5" from 5 to 500000000 since .5*10^9 = 500000000 */
522 if (frac && end) { /* both are valid */
523 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
524 while(frac_digits < 9) { /* this is frac of 10^9 */
529 relative_time_window.nsecs = (int)val;
533 is_duplicate(guint8* fd, guint32 len) {
538 if (cur_dup_entry >= dup_window)
541 /* Calculate our digest */
543 md5_append(&ms, fd, len);
544 md5_finish(&ms, fd_hash[cur_dup_entry].digest);
546 fd_hash[cur_dup_entry].len = len;
548 /* Look for duplicates */
549 for (i = 0; i < dup_window; i++) {
550 if (i == cur_dup_entry)
553 if (fd_hash[i].len == fd_hash[cur_dup_entry].len
554 && memcmp(fd_hash[i].digest, fd_hash[cur_dup_entry].digest, 16) == 0) {
563 is_duplicate_rel_time(guint8* fd, guint32 len, const nstime_t *current) {
568 if (cur_dup_entry >= dup_window)
571 /* Calculate our digest */
573 md5_append(&ms, fd, len);
574 md5_finish(&ms, fd_hash[cur_dup_entry].digest);
576 fd_hash[cur_dup_entry].len = len;
577 fd_hash[cur_dup_entry].time.secs = current->secs;
578 fd_hash[cur_dup_entry].time.nsecs = current->nsecs;
581 * Look for relative time related duplicates.
582 * This is hopefully a reasonably efficient mechanism for
583 * finding duplicates by rel time in the fd_hash[] cache.
584 * We check starting from the most recently added hash
585 * entries and work backwards towards older packets.
586 * This approach allows the dup test to be terminated
587 * when the relative time of a cached entry is found to
588 * be beyond the dup time window.
590 * Of course this assumes that the input trace file is
591 * "well-formed" in the sense that the packet timestamps are
592 * in strict chronologically increasing order (which is NOT
593 * always the case!!).
595 * The fd_hash[] table was deliberately created large (1,000,000).
596 * Looking for time related duplicates in large trace files with
597 * non-fractional dup time window values can potentially take
598 * a long time to complete.
601 for (i = cur_dup_entry - 1;; i--) {
608 if (i == cur_dup_entry) {
610 * We've decremented back to where we started.
616 if (nstime_is_unset(&(fd_hash[i].time))) {
618 * We've decremented to an unused fd_hash[] entry.
624 nstime_delta(&delta, current, &fd_hash[i].time);
626 if (delta.secs < 0 || delta.nsecs < 0) {
628 * A negative delta implies that the current packet
629 * has an absolute timestamp less than the cached packet
630 * that it is being compared to. This is NOT a normal
631 * situation since trace files usually have packets in
632 * chronological order (oldest to newest).
634 * There are several possible ways to deal with this:
635 * 1. 'continue' dup checking with the next cached frame.
636 * 2. 'break' from looking for a duplicate of the current frame.
637 * 3. Take the absolute value of the delta and see if that
638 * falls within the specifed dup time window.
640 * Currently this code does option 1. But it would pretty
641 * easy to add yet-another-editcap-option to select one of
642 * the other behaviors for dealing with out-of-sequence
648 cmp = nstime_cmp(&delta, &relative_time_window);
652 * The delta time indicates that we are now looking at
653 * cached packets beyond the specified dup time window.
657 } else if (fd_hash[i].len == fd_hash[cur_dup_entry].len
658 && memcmp(fd_hash[i].digest, fd_hash[cur_dup_entry].digest, 16) == 0) {
667 usage(gboolean is_error)
676 fprintf(output, "Editcap %s"
678 " (" GITVERSION " from " GITBRANCH ")"
681 fprintf(output, "Edit and/or translate the format of capture files.\n");
682 fprintf(output, "See http://www.wireshark.org for more information.\n");
683 fprintf(output, "\n");
684 fprintf(output, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
685 fprintf(output, "\n");
686 fprintf(output, "<infile> and <outfile> must both be present.\n");
687 fprintf(output, "A single packet or a range of packets can be selected.\n");
688 fprintf(output, "\n");
689 fprintf(output, "Packet selection:\n");
690 fprintf(output, " -r keep the selected packets; default is to delete them.\n");
691 fprintf(output, " -A <start time> only output packets whose timestamp is after (or equal\n");
692 fprintf(output, " to) the given time (format as YYYY-MM-DD hh:mm:ss).\n");
693 fprintf(output, " -B <stop time> only output packets whose timestamp is before the\n");
694 fprintf(output, " given time (format as YYYY-MM-DD hh:mm:ss).\n");
695 fprintf(output, "\n");
696 fprintf(output, "Duplicate packet removal:\n");
697 fprintf(output, " -d remove packet if duplicate (window == %d).\n", DEFAULT_DUP_DEPTH);
698 fprintf(output, " -D <dup window> remove packet if duplicate; configurable <dup window>\n");
699 fprintf(output, " Valid <dup window> values are 0 to %d.\n", MAX_DUP_DEPTH);
700 fprintf(output, " NOTE: A <dup window> of 0 with -v (verbose option) is\n");
701 fprintf(output, " useful to print MD5 hashes.\n");
702 fprintf(output, " -w <dup time window> remove packet if duplicate packet is found EQUAL TO OR\n");
703 fprintf(output, " LESS THAN <dup time window> prior to current packet.\n");
704 fprintf(output, " A <dup time window> is specified in relative seconds\n");
705 fprintf(output, " (e.g. 0.000001).\n");
706 fprintf(output, "\n");
707 fprintf(output, " NOTE: The use of the 'Duplicate packet removal' options with\n");
708 fprintf(output, " other editcap options except -v may not always work as expected.\n");
709 fprintf(output, " Specifically the -r, -t or -S options will very likely NOT have the\n");
710 fprintf(output, " desired effect if combined with the -d, -D or -w.\n");
711 fprintf(output, "\n");
712 fprintf(output, "Packet manipulation:\n");
713 fprintf(output, " -s <snaplen> truncate each packet to max. <snaplen> bytes of data.\n");
714 fprintf(output, " -C [offset:]<choplen> chop each packet by <choplen> bytes. Positive values\n");
715 fprintf(output, " chop at the packet beginning, negative values at the\n");
716 fprintf(output, " packet end. If an optional offset precedes the length,\n");
717 fprintf(output, " then the bytes chopped will be offset from that value.\n");
718 fprintf(output, " Positive offsets are from the packet beginning,\n");
719 fprintf(output, " negative offsets are from the packet end. You can use\n");
720 fprintf(output, " this option more than once, allowing up to 2 chopping\n");
721 fprintf(output, " regions within a packet provided that at least 1\n");
722 fprintf(output, " choplen is positive and at least 1 is negative.\n");
723 fprintf(output, " -L adjust the frame length when chopping and/or snapping\n");
724 fprintf(output, " -t <time adjustment> adjust the timestamp of each packet;\n");
725 fprintf(output, " <time adjustment> is in relative seconds (e.g. -0.5).\n");
726 fprintf(output, " -S <strict adjustment> adjust timestamp of packets if necessary to insure\n");
727 fprintf(output, " strict chronological increasing order. The <strict\n");
728 fprintf(output, " adjustment> is specified in relative seconds with\n");
729 fprintf(output, " values of 0 or 0.000001 being the most reasonable.\n");
730 fprintf(output, " A negative adjustment value will modify timestamps so\n");
731 fprintf(output, " that each packet's delta time is the absolute value\n");
732 fprintf(output, " of the adjustment specified. A value of -0 will set\n");
733 fprintf(output, " all packets to the timestamp of the first packet.\n");
734 fprintf(output, " -E <error probability> set the probability (between 0.0 and 1.0 incl.) that\n");
735 fprintf(output, " a particular packet byte will be randomly changed.\n");
736 fprintf(output, "\n");
737 fprintf(output, "Output File(s):\n");
738 fprintf(output, " -c <packets per file> split the packet output to different files based on\n");
739 fprintf(output, " uniform packet counts with a maximum of\n");
740 fprintf(output, " <packets per file> each.\n");
741 fprintf(output, " -i <seconds per file> split the packet output to different files based on\n");
742 fprintf(output, " uniform time intervals with a maximum of\n");
743 fprintf(output, " <seconds per file> each.\n");
744 fprintf(output, " -F <capture type> set the output file type; default is pcapng. An empty\n");
745 fprintf(output, " \"-F\" option will list the file types.\n");
746 fprintf(output, " -T <encap type> set the output file encapsulation type; default is the\n");
747 fprintf(output, " same as the input file. An empty \"-T\" option will\n");
748 fprintf(output, " list the encapsulation types.\n");
749 fprintf(output, "\n");
750 fprintf(output, "Miscellaneous:\n");
751 fprintf(output, " -h display this help and exit.\n");
752 fprintf(output, " -v verbose output.\n");
753 fprintf(output, " If -v is used with any of the 'Duplicate Packet\n");
754 fprintf(output, " Removal' options (-d, -D or -w) then Packet lengths\n");
755 fprintf(output, " and MD5 hashes are printed to standard-error.\n");
756 fprintf(output, "\n");
760 const char *sstr; /* The short string */
761 const char *lstr; /* The long string */
765 string_compare(gconstpointer a, gconstpointer b)
767 return strcmp(((const struct string_elem *)a)->sstr,
768 ((const struct string_elem *)b)->sstr);
772 string_nat_compare(gconstpointer a, gconstpointer b)
774 return strnatcmp(((const struct string_elem *)a)->sstr,
775 ((const struct string_elem *)b)->sstr);
779 string_elem_print(gpointer data, gpointer not_used _U_)
781 fprintf(stderr, " %s - %s\n",
782 ((struct string_elem *)data)->sstr,
783 ((struct string_elem *)data)->lstr);
787 list_capture_types(void) {
789 struct string_elem *captypes;
792 captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES);
793 fprintf(stderr, "editcap: The available capture file types for the \"-F\" flag are:\n");
794 for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
795 if (wtap_dump_can_open(i)) {
796 captypes[i].sstr = wtap_file_type_subtype_short_string(i);
797 captypes[i].lstr = wtap_file_type_subtype_string(i);
798 list = g_slist_insert_sorted(list, &captypes[i], string_compare);
801 g_slist_foreach(list, string_elem_print, NULL);
807 list_encap_types(void) {
809 struct string_elem *encaps;
812 encaps = (struct string_elem *)g_malloc(sizeof(struct string_elem) * WTAP_NUM_ENCAP_TYPES);
813 fprintf(stderr, "editcap: The available encapsulation types for the \"-T\" flag are:\n");
814 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
815 encaps[i].sstr = wtap_encap_short_string(i);
816 if (encaps[i].sstr != NULL) {
817 encaps[i].lstr = wtap_encap_string(i);
818 list = g_slist_insert_sorted(list, &encaps[i], string_nat_compare);
821 g_slist_foreach(list, string_elem_print, NULL);
828 * Don't report failures to load plugins because most (non-wiretap) plugins
829 * *should* fail to load (because we're not linked against libwireshark and
830 * dissector plugins need libwireshark).
833 failure_message(const char *msg_format _U_, va_list ap _U_)
839 main(int argc, char *argv[])
847 guint32 snaplen = 0; /* No limit */
848 chop_t chop = {0, 0, 0, 0, 0, 0}; /* No chop */
849 gboolean adjlen = FALSE;
850 wtap_dumper *pdh = NULL;
851 unsigned int count = 1;
852 unsigned int duplicate_count = 0;
856 guint32 read_count = 0;
857 int split_packet_count = 0;
858 int written_count = 0;
859 char *filename = NULL;
860 gboolean ts_okay = TRUE;
861 int secs_per_block = 0;
863 nstime_t block_start;
864 gchar *fprefix = NULL;
865 gchar *fsuffix = NULL;
867 const struct wtap_pkthdr *phdr;
868 struct wtap_pkthdr snap_phdr;
869 wtapng_iface_descriptions_t *idb_inf;
870 wtapng_section_t *shb_hdr;
873 char* init_progfile_dir_error;
877 arg_list_utf_16to8(argc, argv);
878 create_app_running_mutex();
882 * Get credential information for later use.
884 init_process_policies();
885 init_open_routines();
888 /* Register wiretap plugins */
889 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
890 g_warning("editcap: init_progfile_dir(): %s", init_progfile_dir_error);
891 g_free(init_progfile_dir_error);
893 /* Register all the plugin types we have. */
894 wtap_register_plugin_types(); /* Types known to libwiretap */
896 init_report_err(failure_message,NULL,NULL,NULL);
898 /* Scan for plugins. This does *not* call their registration routines;
899 that's done later. */
902 /* Register all libwiretap plugin modules. */
903 register_all_wiretap_modules();
907 /* Process the options */
908 while ((opt = getopt(argc, argv, "A:B:c:C:dD:E:F:hi:Lrs:S:t:T:vw:")) != -1) {
914 memset(&starttm,0,sizeof(struct tm));
916 if (!strptime(optarg,"%Y-%m-%d %T", &starttm)) {
917 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
922 check_startstop = TRUE;
923 starttm.tm_isdst = -1;
925 starttime = mktime(&starttm);
933 memset(&stoptm,0,sizeof(struct tm));
935 if (!strptime(optarg,"%Y-%m-%d %T", &stoptm)) {
936 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n",
940 check_startstop = TRUE;
941 stoptm.tm_isdst = -1;
942 stoptime = mktime(&stoptm);
947 split_packet_count = (int)strtol(optarg, &p, 10);
948 if (p == optarg || *p != '\0') {
949 fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
953 if (split_packet_count <= 0) {
954 fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
962 int choplen = 0, chopoff = 0;
964 switch (sscanf(optarg, "%d:%d", &chopoff, &choplen)) {
965 case 1: /* only the chop length was specififed */
970 case 2: /* both an offset and chop length was specified */
974 fprintf(stderr, "editcap: \"%s\" isn't a valid chop length or offset:length\n",
981 chop.len_begin += choplen;
983 chop.off_begin_pos += chopoff;
985 chop.off_begin_neg += chopoff;
986 } else if (choplen < 0) {
987 chop.len_end += choplen;
989 chop.off_end_pos += chopoff;
991 chop.off_end_neg += chopoff;
998 dup_detect_by_time = FALSE;
999 dup_window = DEFAULT_DUP_DEPTH;
1004 dup_detect_by_time = FALSE;
1005 dup_window = (int)strtol(optarg, &p, 10);
1006 if (p == optarg || *p != '\0') {
1007 fprintf(stderr, "editcap: \"%s\" isn't a valid duplicate window value\n",
1011 if (dup_window < 0 || dup_window > MAX_DUP_DEPTH) {
1012 fprintf(stderr, "editcap: \"%d\" duplicate window value must be between 0 and %d inclusive.\n",
1013 dup_window, MAX_DUP_DEPTH);
1019 err_prob = strtod(optarg, &p);
1020 if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
1021 fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
1025 srand( (unsigned int) (time(NULL) + getpid()) );
1029 out_file_type_subtype = wtap_short_string_to_file_type_subtype(optarg);
1030 if (out_file_type_subtype < 0) {
1031 fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
1033 list_capture_types();
1043 case 'i': /* break capture file based on time interval */
1044 secs_per_block = atoi(optarg);
1045 if (secs_per_block <= 0) {
1046 fprintf(stderr, "editcap: \"%s\" isn't a valid time interval\n\n",
1057 keep_em = !keep_em; /* Just invert */
1061 snaplen = (guint32)strtol(optarg, &p, 10);
1062 if (p == optarg || *p != '\0') {
1063 fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
1070 set_strict_time_adj(optarg);
1071 do_strict_time_adjustment = TRUE;
1075 set_time_adjustment(optarg);
1079 out_frame_type = wtap_short_string_to_encap(optarg);
1080 if (out_frame_type < 0) {
1081 fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
1089 verbose = !verbose; /* Just invert */
1094 dup_detect_by_time = TRUE;
1095 dup_window = MAX_DUP_DEPTH;
1096 set_rel_time(optarg);
1099 case '?': /* Bad options if GNU getopt */
1102 list_capture_types();
1117 fprintf(stderr, "Optind = %i, argc = %i\n", optind, argc);
1120 if ((argc - optind) < 1) {
1125 if (check_startstop && !stoptime) {
1128 /* XXX: will work until 2035 */
1129 memset(&stoptm,0,sizeof(struct tm));
1130 stoptm.tm_year = 135;
1131 stoptm.tm_mday = 31;
1134 stoptime = mktime(&stoptm);
1137 nstime_set_unset(&block_start);
1139 if (starttime > stoptime) {
1140 fprintf(stderr, "editcap: start time is after the stop time\n");
1144 if (split_packet_count > 0 && secs_per_block > 0) {
1145 fprintf(stderr, "editcap: can't split on both packet count and time interval\n");
1146 fprintf(stderr, "editcap: at the same time\n");
1150 wth = wtap_open_offline(argv[optind], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1153 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
1154 wtap_strerror(err));
1156 case WTAP_ERR_UNSUPPORTED:
1157 case WTAP_ERR_UNSUPPORTED_ENCAP:
1158 case WTAP_ERR_BAD_FILE:
1159 fprintf(stderr, "(%s)\n", err_info);
1167 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
1168 wtap_file_type_subtype_string(wtap_file_type_subtype(wth)));
1171 shb_hdr = wtap_file_get_shb_info(wth);
1172 idb_inf = wtap_file_get_idb_info(wth);
1175 * Now, process the rest, if any ... we only write if there is an extra
1176 * argument or so ...
1179 if ((argc - optind) >= 2) {
1180 if (out_frame_type == -2)
1181 out_frame_type = wtap_file_encap(wth);
1183 for (i = optind + 2; i < argc; i++)
1184 if (add_selection(argv[i]) == FALSE)
1187 if (dup_detect || dup_detect_by_time) {
1188 for (i = 0; i < dup_window; i++) {
1189 memset(&fd_hash[i].digest, 0, 16);
1191 nstime_set_unset(&fd_hash[i].time);
1195 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1198 phdr = wtap_phdr(wth);
1199 buf = wtap_buf_ptr(wth);
1201 if (nstime_is_unset(&block_start)) { /* should only be the first packet */
1202 block_start.secs = phdr->ts.secs;
1203 block_start.nsecs = phdr->ts.nsecs;
1205 if (split_packet_count > 0 || secs_per_block > 0) {
1206 if (!fileset_extract_prefix_suffix(argv[optind+1], &fprefix, &fsuffix))
1209 filename = fileset_get_filename_by_pattern(block_cnt++, &phdr->ts, fprefix, fsuffix);
1211 filename = g_strdup(argv[optind+1]);
1214 /* If we don't have an application name add Editcap */
1215 if (shb_hdr->shb_user_appl == NULL) {
1216 shb_hdr->shb_user_appl = "Editcap " VERSION;
1219 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
1220 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
1221 FALSE /* compressed */, shb_hdr, idb_inf, &err);
1224 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
1225 filename, wtap_strerror(err));
1232 if (secs_per_block > 0) {
1233 while ((phdr->ts.secs - block_start.secs > secs_per_block)
1234 || (phdr->ts.secs - block_start.secs == secs_per_block
1235 && phdr->ts.nsecs >= block_start.nsecs )) { /* time for the next file */
1237 if (!wtap_dump_close(pdh, &err)) {
1238 fprintf(stderr, "editcap: Error writing to %s: %s\n",
1239 filename, wtap_strerror(err));
1242 block_start.secs = block_start.secs + secs_per_block; /* reset for next interval */
1244 filename = fileset_get_filename_by_pattern(block_cnt++, &phdr->ts, fprefix, fsuffix);
1248 fprintf(stderr, "Continuing writing in file %s\n", filename);
1250 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
1251 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
1252 FALSE /* compressed */, shb_hdr, idb_inf, &err);
1255 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
1256 filename, wtap_strerror(err));
1262 if (split_packet_count > 0) {
1263 /* time for the next file? */
1264 if (written_count > 0 && written_count % split_packet_count == 0) {
1265 if (!wtap_dump_close(pdh, &err)) {
1266 fprintf(stderr, "editcap: Error writing to %s: %s\n",
1267 filename, wtap_strerror(err));
1272 filename = fileset_get_filename_by_pattern(block_cnt++, &phdr->ts, fprefix, fsuffix);
1276 fprintf(stderr, "Continuing writing in file %s\n", filename);
1278 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
1279 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
1280 FALSE /* compressed */, shb_hdr, idb_inf, &err);
1282 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
1283 filename, wtap_strerror(err));
1289 if (check_startstop)
1290 ts_okay = check_timestamp(wth);
1292 if (ts_okay && ((!selected(count) && !keep_em)
1293 || (selected(count) && keep_em))) {
1295 if (verbose && !dup_detect && !dup_detect_by_time)
1296 fprintf(stderr, "Packet: %u\n", count);
1298 /* We simply write it, perhaps after truncating it; we could
1299 * do other things, like modify it. */
1301 phdr = wtap_phdr(wth);
1304 if (phdr->caplen > snaplen) {
1306 snap_phdr.caplen = snaplen;
1309 if (adjlen && phdr->len > snaplen) {
1311 snap_phdr.len = snaplen;
1318 handle_chopping(chop, &snap_phdr, phdr, &buf, adjlen);
1321 /* Do we adjust timestamps to ensure strict chronological
1323 if (do_strict_time_adjustment) {
1324 if (previous_time.secs || previous_time.nsecs) {
1325 if (!strict_time_adj.is_negative) {
1329 current.secs = phdr->ts.secs;
1330 current.nsecs = phdr->ts.nsecs;
1332 nstime_delta(&delta, ¤t, &previous_time);
1334 if (delta.secs < 0 || delta.nsecs < 0) {
1336 * A negative delta indicates that the current packet
1337 * has an absolute timestamp less than the previous packet
1338 * that it is being compared to. This is NOT a normal
1339 * situation since trace files usually have packets in
1340 * chronological order (oldest to newest).
1342 /* fprintf(stderr, "++out of order, need to adjust this packet!\n"); */
1344 snap_phdr.ts.secs = previous_time.secs + strict_time_adj.tv.tv_sec;
1345 snap_phdr.ts.nsecs = previous_time.nsecs;
1346 if (snap_phdr.ts.nsecs + strict_time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
1348 snap_phdr.ts.secs++;
1349 snap_phdr.ts.nsecs += (strict_time_adj.tv.tv_usec - ONE_MILLION) * 1000;
1351 snap_phdr.ts.nsecs += strict_time_adj.tv.tv_usec * 1000;
1357 * A negative strict time adjustment is requested.
1358 * Unconditionally set each timestamp to previous
1359 * packet's timestamp plus delta.
1362 snap_phdr.ts.secs = previous_time.secs + strict_time_adj.tv.tv_sec;
1363 snap_phdr.ts.nsecs = previous_time.nsecs;
1364 if (snap_phdr.ts.nsecs + strict_time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
1366 snap_phdr.ts.secs++;
1367 snap_phdr.ts.nsecs += (strict_time_adj.tv.tv_usec - ONE_MILLION) * 1000;
1369 snap_phdr.ts.nsecs += strict_time_adj.tv.tv_usec * 1000;
1374 previous_time.secs = phdr->ts.secs;
1375 previous_time.nsecs = phdr->ts.nsecs;
1378 /* assume that if the frame's tv_sec is 0, then
1379 * the timestamp isn't supported */
1380 if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
1382 if (time_adj.is_negative)
1383 snap_phdr.ts.secs -= time_adj.tv.tv_sec;
1385 snap_phdr.ts.secs += time_adj.tv.tv_sec;
1389 /* assume that if the frame's tv_sec is 0, then
1390 * the timestamp isn't supported */
1391 if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
1393 if (time_adj.is_negative) { /* subtract */
1394 if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
1395 snap_phdr.ts.secs--;
1396 snap_phdr.ts.nsecs += ONE_MILLION * 1000;
1398 snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
1400 if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
1402 snap_phdr.ts.secs++;
1403 snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
1405 snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
1411 /* suppress duplicates by packet window */
1413 if (is_duplicate(buf, phdr->caplen)) {
1415 fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
1416 count, phdr->caplen);
1417 for (i = 0; i < 16; i++)
1418 fprintf(stderr, "%02x",
1419 (unsigned char)fd_hash[cur_dup_entry].digest[i]);
1420 fprintf(stderr, "\n");
1427 fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
1428 count, phdr->caplen);
1429 for (i = 0; i < 16; i++)
1430 fprintf(stderr, "%02x",
1431 (unsigned char)fd_hash[cur_dup_entry].digest[i]);
1432 fprintf(stderr, "\n");
1437 /* suppress duplicates by time window */
1438 if (dup_detect_by_time) {
1441 current.secs = phdr->ts.secs;
1442 current.nsecs = phdr->ts.nsecs;
1444 if (is_duplicate_rel_time(buf, phdr->caplen, ¤t)) {
1446 fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
1447 count, phdr->caplen);
1448 for (i = 0; i < 16; i++)
1449 fprintf(stderr, "%02x",
1450 (unsigned char)fd_hash[cur_dup_entry].digest[i]);
1451 fprintf(stderr, "\n");
1458 fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
1459 count, phdr->caplen);
1460 for (i = 0; i < 16; i++)
1461 fprintf(stderr, "%02x",
1462 (unsigned char)fd_hash[cur_dup_entry].digest[i]);
1463 fprintf(stderr, "\n");
1468 /* Random error mutation */
1469 if (err_prob > 0.0) {
1470 int real_data_start = 0;
1472 /* Protect non-protocol data */
1473 if (wtap_file_type_subtype(wth) == WTAP_FILE_TYPE_SUBTYPE_CATAPULT_DCT2000)
1474 real_data_start = find_dct2000_real_data(buf);
1476 for (i = real_data_start; i < (int) phdr->caplen; i++) {
1477 if (rand() <= err_prob * RAND_MAX) {
1478 err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
1480 if (err_type < ERR_WT_BIT) {
1481 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
1482 err_type = ERR_WT_TOTAL;
1484 err_type -= ERR_WT_BYTE;
1487 if (err_type < ERR_WT_BYTE) {
1488 buf[i] = rand() / (RAND_MAX / 255 + 1);
1489 err_type = ERR_WT_TOTAL;
1491 err_type -= ERR_WT_BYTE;
1494 if (err_type < ERR_WT_ALNUM) {
1495 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
1496 err_type = ERR_WT_TOTAL;
1498 err_type -= ERR_WT_ALNUM;
1501 if (err_type < ERR_WT_FMT) {
1502 if ((unsigned int)i < phdr->caplen - 2)
1503 g_strlcpy((char*) &buf[i], "%s", 2);
1504 err_type = ERR_WT_TOTAL;
1506 err_type -= ERR_WT_FMT;
1509 if (err_type < ERR_WT_AA) {
1510 for (j = i; j < (int) phdr->caplen; j++)
1518 if (!wtap_dump(pdh, phdr, buf, &err)) {
1520 case WTAP_ERR_UNSUPPORTED_ENCAP:
1522 * This is a problem with the particular frame we're
1523 * writing and the file type and subtype we're
1524 * writing; note that, and report the frame number
1525 * and file type/subtype.
1528 "editcap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file\n.",
1529 read_count, argv[optind],
1530 wtap_file_type_subtype_string(out_file_type_subtype));
1533 case WTAP_ERR_PACKET_TOO_LARGE:
1535 * This is a problem with the particular frame we're
1536 * writing and the file type and subtype we're
1537 * writing; note that, and report the frame number
1538 * and file type/subtype.
1541 "editcap: Frame %u of \"%s\" is too large for a \"%s\" file\n.",
1542 read_count, argv[optind],
1543 wtap_file_type_subtype_string(out_file_type_subtype));
1547 fprintf(stderr, "editcap: Error writing to %s: %s\n",
1548 filename, wtap_strerror(err));
1562 /* Print a message noting that the read failed somewhere along the
1565 "editcap: An error occurred while reading \"%s\": %s.\n",
1566 argv[optind], wtap_strerror(err));
1568 case WTAP_ERR_UNSUPPORTED:
1569 case WTAP_ERR_UNSUPPORTED_ENCAP:
1570 case WTAP_ERR_BAD_FILE:
1571 fprintf(stderr, "(%s)\n", err_info);
1578 /* No valid packages found, open the outfile so we can write an
1581 filename = g_strdup(argv[optind+1]);
1583 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
1584 snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth),
1585 FALSE /* compressed */, shb_hdr, idb_inf, &err);
1587 fprintf(stderr, "editcap: Can't open or create %s: %s\n",
1588 filename, wtap_strerror(err));
1596 if (!wtap_dump_close(pdh, &err)) {
1597 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
1598 wtap_strerror(err));
1606 fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate window of %i packets.\n",
1607 count - 1, plurality(count - 1, "", "s"), duplicate_count,
1608 plurality(duplicate_count, "", "s"), dup_window);
1609 } else if (dup_detect_by_time) {
1610 fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate time window equal to or less than %ld.%09ld seconds.\n",
1611 count - 1, plurality(count - 1, "", "s"), duplicate_count,
1612 plurality(duplicate_count, "", "s"),
1613 (long)relative_time_window.secs,
1614 (long int)relative_time_window.nsecs);
1620 /* Skip meta-information read from file to return offset of real
1623 find_dct2000_real_data(guint8 *buf)
1627 for (n = 0; buf[n] != '\0'; n++); /* Context name */
1629 n++; /* Context port number */
1630 for (; buf[n] != '\0'; n++); /* Timestamp */
1632 for (; buf[n] != '\0'; n++); /* Protocol name */
1634 for (; buf[n] != '\0'; n++); /* Variant number (as string) */
1636 for (; buf[n] != '\0'; n++); /* Outhdr (as string) */
1638 n += 2; /* Direction & encap */
1644 * We support up to 2 chopping regions in a single pass: one specified by the
1645 * positive chop length, and one by the negative chop length.
1648 handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
1649 const struct wtap_pkthdr *in_phdr, guint8 **buf,
1652 /* If we're not chopping anything from one side, then the offset for that
1653 * side is meaningless. */
1654 if (chop.len_begin == 0)
1655 chop.off_begin_pos = chop.off_begin_neg = 0;
1656 if (chop.len_end == 0)
1657 chop.off_end_pos = chop.off_end_neg = 0;
1659 if (chop.off_begin_neg < 0) {
1660 chop.off_begin_pos += in_phdr->caplen + chop.off_begin_neg;
1661 chop.off_begin_neg = 0;
1663 if (chop.off_end_pos > 0) {
1664 chop.off_end_neg += chop.off_end_pos - in_phdr->caplen;
1665 chop.off_end_pos = 0;
1668 /* If we've crossed chopping regions, swap them */
1669 if (chop.len_begin && chop.len_end) {
1670 if (chop.off_begin_pos > ((int)in_phdr->caplen + chop.off_end_neg)) {
1671 int tmp_len, tmp_off;
1673 tmp_off = in_phdr->caplen + chop.off_end_neg + chop.len_end;
1674 tmp_len = -chop.len_end;
1676 chop.off_end_neg = chop.len_begin + chop.off_begin_pos - in_phdr->caplen;
1677 chop.len_end = -chop.len_begin;
1679 chop.len_begin = tmp_len;
1680 chop.off_begin_pos = tmp_off;
1684 /* Make sure we don't chop off more than we have available */
1685 if (in_phdr->caplen < (guint32)(chop.off_begin_pos - chop.off_end_neg)) {
1689 if ((guint32)(chop.len_begin - chop.len_end) >
1690 (in_phdr->caplen - (guint32)(chop.off_begin_pos - chop.off_end_neg))) {
1691 chop.len_begin = in_phdr->caplen - (chop.off_begin_pos - chop.off_end_neg);
1695 /* Handle chopping from the beginning. Note that if a beginning offset
1696 * was specified, we need to keep that piece */
1697 if (chop.len_begin > 0) {
1698 *out_phdr = *in_phdr;
1700 if (chop.off_begin_pos > 0) {
1701 memmove(*buf + chop.off_begin_pos,
1702 *buf + chop.off_begin_pos + chop.len_begin,
1703 out_phdr->caplen - chop.len_begin);
1705 *buf += chop.len_begin;
1707 out_phdr->caplen -= chop.len_begin;
1710 if (in_phdr->len > (guint32)chop.len_begin)
1711 out_phdr->len -= chop.len_begin;
1718 /* Handle chopping from the end. Note that if an ending offset was
1719 * specified, we need to keep that piece */
1720 if (chop.len_end < 0) {
1721 *out_phdr = *in_phdr;
1723 if (chop.off_end_neg < 0) {
1724 memmove(*buf + (gint)out_phdr->caplen + (chop.len_end + chop.off_end_neg),
1725 *buf + (gint)out_phdr->caplen + chop.off_end_neg,
1728 out_phdr->caplen += chop.len_end;
1731 if (((signed int) in_phdr->len + chop.len_end) > 0)
1732 out_phdr->len += chop.len_end;
1736 /*in_phdr = out_phdr;*/
1741 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1746 * indent-tabs-mode: nil
1749 * vi: set shiftwidth=4 tabstop=8 expandtab:
1750 * :indentSize=4:tabSize=8:noTabs=true: