From 25bbbd7c5c031ca166e48141457c2135f465c8b6 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Tue, 13 Nov 2018 15:05:33 -0800 Subject: Give an error if an output file isn't specified. Just silently not bothering to do any work isn't all that useful in that case; giving the usage message indicates that you need both input and output files. Change-Id: I9512d3e45e1e9a9d4bccb28b49aeea8c12ad0100 Reviewed-on: https://code.wireshark.org/review/30614 Reviewed-by: Guy Harris --- editcap.c | 783 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 390 insertions(+), 393 deletions(-) (limited to 'editcap.c') diff --git a/editcap.c b/editcap.c index 92cb1a7c0c..a99615ecf5 100644 --- a/editcap.c +++ b/editcap.c @@ -1326,7 +1326,7 @@ real_main(int argc, char *argv[]) fprintf(stderr, "Optind = %i, argc = %i\n", optind, argc); #endif - if ((argc - optind) < 1) { + if ((argc - optind) < 2) { print_usage(stderr); ret = INVALID_OPTION; goto clean_exit; @@ -1403,124 +1403,91 @@ real_main(int argc, char *argv[]) wtap_dump_params_init(&ng_params, wth); + if (out_frame_type == -2) + out_frame_type = wtap_file_encap(wth); + /* - * Now, process the rest, if any ... we only write if there is an extra - * argument or so ... + * Now process the arguments following the input and output file + * names, if any; they specify packets to include/exclude. */ + for (i = optind + 2; i < argc; i++) + if (add_selection(argv[i], &max_packet_number) == FALSE) + break; - if ((argc - optind) >= 2) { - if (out_frame_type == -2) - out_frame_type = wtap_file_encap(wth); - - for (i = optind + 2; i < argc; i++) - if (add_selection(argv[i], &max_packet_number) == FALSE) - break; - - if (keep_em == FALSE) - max_packet_number = G_MAXUINT; + if (keep_em == FALSE) + max_packet_number = G_MAXUINT; - if (dup_detect || dup_detect_by_time) { - for (i = 0; i < dup_window; i++) { - memset(&fd_hash[i].digest, 0, 16); - fd_hash[i].len = 0; - nstime_set_unset(&fd_hash[i].frame_time); - } + if (dup_detect || dup_detect_by_time) { + for (i = 0; i < dup_window; i++) { + memset(&fd_hash[i].digest, 0, 16); + fd_hash[i].len = 0; + nstime_set_unset(&fd_hash[i].frame_time); } + } - /* Read all of the packets in turn */ - while (wtap_read(wth, &read_err, &read_err_info, &data_offset)) { - if (max_packet_number <= read_count) - break; - - read_count++; + /* Read all of the packets in turn */ + while (wtap_read(wth, &read_err, &read_err_info, &data_offset)) { + if (max_packet_number <= read_count) + break; - rec = wtap_get_rec(wth); + read_count++; - /* Extra actions for the first packet */ - if (read_count == 1) { - if (split_packet_count != 0 || secs_per_block != 0) { - if (!fileset_extract_prefix_suffix(argv[optind+1], &fprefix, &fsuffix)) { - ret = CANT_EXTRACT_PREFIX; - goto clean_exit; - } + rec = wtap_get_rec(wth); - filename = fileset_get_filename_by_pattern(block_cnt++, rec, fprefix, fsuffix); - } else { - filename = g_strdup(argv[optind+1]); + /* Extra actions for the first packet */ + if (read_count == 1) { + if (split_packet_count != 0 || secs_per_block != 0) { + if (!fileset_extract_prefix_suffix(argv[optind+1], &fprefix, &fsuffix)) { + ret = CANT_EXTRACT_PREFIX; + goto clean_exit; } - g_assert(filename); - /* If we don't have an application name add Editcap */ - if (wtap_block_get_string_option_value(g_array_index(ng_params.shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) { - wtap_block_add_string_option_format(g_array_index(ng_params.shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "Editcap " VERSION); - } + filename = fileset_get_filename_by_pattern(block_cnt++, rec, fprefix, fsuffix); + } else { + filename = g_strdup(argv[optind+1]); + } + g_assert(filename); - pdh = editcap_dump_open(filename, - snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), - &ng_params, &write_err); + /* If we don't have an application name add Editcap */ + if (wtap_block_get_string_option_value(g_array_index(ng_params.shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) { + wtap_block_add_string_option_format(g_array_index(ng_params.shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "Editcap " VERSION); + } - if (pdh == NULL) { - cfile_dump_open_failure_message("editcap", filename, - write_err, - out_file_type_subtype); - ret = INVALID_FILE; - goto clean_exit; - } - } /* first packet only handling */ + pdh = editcap_dump_open(filename, + snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), + &ng_params, &write_err); + if (pdh == NULL) { + cfile_dump_open_failure_message("editcap", filename, + write_err, + out_file_type_subtype); + ret = INVALID_FILE; + goto clean_exit; + } + } /* first packet only handling */ - buf = wtap_get_buf_ptr(wth); - /* - * Not all packets have time stamps. Only process the time - * stamp if we have one. - */ - if (rec->presence_flags & WTAP_HAS_TS) { - if (nstime_is_unset(&block_start)) { - block_start = rec->ts; - } - if (secs_per_block != 0) { - while (((guint32)(rec->ts.secs - block_start.secs) > secs_per_block) - || ((guint32)(rec->ts.secs - block_start.secs) == secs_per_block - && rec->ts.nsecs >= block_start.nsecs )) { /* time for the next file */ - - if (!wtap_dump_close(pdh, &write_err)) { - cfile_close_failure_message(filename, write_err); - ret = WRITE_ERROR; - goto clean_exit; - } - block_start.secs = block_start.secs + secs_per_block; /* reset for next interval */ - g_free(filename); - filename = fileset_get_filename_by_pattern(block_cnt++, rec, fprefix, fsuffix); - g_assert(filename); - - if (verbose) - fprintf(stderr, "Continuing writing in file %s\n", filename); - - pdh = editcap_dump_open(filename, - snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), - &ng_params, &write_err); - - if (pdh == NULL) { - cfile_dump_open_failure_message("editcap", filename, - write_err, - out_file_type_subtype); - ret = INVALID_FILE; - goto clean_exit; - } - } - } - } /* time stamp handling */ + buf = wtap_get_buf_ptr(wth); + + /* + * Not all packets have time stamps. Only process the time + * stamp if we have one. + */ + if (rec->presence_flags & WTAP_HAS_TS) { + if (nstime_is_unset(&block_start)) { + block_start = rec->ts; + } + if (secs_per_block != 0) { + while (((guint32)(rec->ts.secs - block_start.secs) > secs_per_block) + || ((guint32)(rec->ts.secs - block_start.secs) == secs_per_block + && rec->ts.nsecs >= block_start.nsecs )) { /* time for the next file */ - if (split_packet_count != 0) { - /* time for the next file? */ - if (written_count > 0 && (written_count % split_packet_count) == 0) { if (!wtap_dump_close(pdh, &write_err)) { cfile_close_failure_message(filename, write_err); ret = WRITE_ERROR; goto clean_exit; } - + block_start.secs = block_start.secs + secs_per_block; /* reset for next interval */ g_free(filename); filename = fileset_get_filename_by_pattern(block_cnt++, rec, fprefix, fsuffix); g_assert(filename); @@ -1531,6 +1498,7 @@ real_main(int argc, char *argv[]) pdh = editcap_dump_open(filename, snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), &ng_params, &write_err); + if (pdh == NULL) { cfile_dump_open_failure_message("editcap", filename, write_err, @@ -1539,80 +1507,90 @@ real_main(int argc, char *argv[]) goto clean_exit; } } - } /* split packet handling */ + } + } /* time stamp handling */ + + if (split_packet_count != 0) { + /* time for the next file? */ + if (written_count > 0 && (written_count % split_packet_count) == 0) { + if (!wtap_dump_close(pdh, &write_err)) { + cfile_close_failure_message(filename, write_err); + ret = WRITE_ERROR; + goto clean_exit; + } - if (check_startstop) { - /* - * Is the packet in the selected timeframe? - * If the packet has no time stamp, the answer is "no". - */ - if (rec->presence_flags & WTAP_HAS_TS) - ts_okay = (rec->ts.secs >= starttime) && (rec->ts.secs < stoptime); - else - ts_okay = FALSE; - } else { - /* - * No selected timeframe, so all packets are "in the - * selected timeframe". - */ - ts_okay = TRUE; + g_free(filename); + filename = fileset_get_filename_by_pattern(block_cnt++, rec, fprefix, fsuffix); + g_assert(filename); + + if (verbose) + fprintf(stderr, "Continuing writing in file %s\n", filename); + + pdh = editcap_dump_open(filename, + snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), + &ng_params, &write_err); + if (pdh == NULL) { + cfile_dump_open_failure_message("editcap", filename, + write_err, + out_file_type_subtype); + ret = INVALID_FILE; + goto clean_exit; + } } + } /* split packet handling */ - if (ts_okay && ((!selected(count) && !keep_em) - || (selected(count) && keep_em))) { + if (check_startstop) { + /* + * Is the packet in the selected timeframe? + * If the packet has no time stamp, the answer is "no". + */ + if (rec->presence_flags & WTAP_HAS_TS) + ts_okay = (rec->ts.secs >= starttime) && (rec->ts.secs < stoptime); + else + ts_okay = FALSE; + } else { + /* + * No selected timeframe, so all packets are "in the + * selected timeframe". + */ + ts_okay = TRUE; + } - if (verbose && !dup_detect && !dup_detect_by_time) - fprintf(stderr, "Packet: %u\n", count); + if (ts_okay && ((!selected(count) && !keep_em) + || (selected(count) && keep_em))) { - /* We simply write it, perhaps after truncating it; we could - * do other things, like modify it. */ + if (verbose && !dup_detect && !dup_detect_by_time) + fprintf(stderr, "Packet: %u\n", count); - rec = wtap_get_rec(wth); + /* We simply write it, perhaps after truncating it; we could + * do other things, like modify it. */ - if (rec->presence_flags & WTAP_HAS_TS) { - /* Do we adjust timestamps to ensure strict chronological - * order? */ - if (do_strict_time_adjustment) { - if (previous_time.secs || previous_time.nsecs) { - if (!strict_time_adj.is_negative) { - nstime_t current; - nstime_t delta; - - current = rec->ts; - - nstime_delta(&delta, ¤t, &previous_time); - - if (delta.secs < 0 || delta.nsecs < 0) { - /* - * A negative delta indicates that the current packet - * has an absolute timestamp less than the previous packet - * that it is being compared to. This is NOT a normal - * situation since trace files usually have packets in - * chronological order (oldest to newest). - * Copy and change rather than modify - * returned rec. - */ - /* fprintf(stderr, "++out of order, need to adjust this packet!\n"); */ - temp_rec = *rec; - temp_rec.ts.secs = previous_time.secs + strict_time_adj.tv.secs; - temp_rec.ts.nsecs = previous_time.nsecs; - if (temp_rec.ts.nsecs + strict_time_adj.tv.nsecs >= ONE_BILLION) { - /* carry */ - temp_rec.ts.secs++; - temp_rec.ts.nsecs += strict_time_adj.tv.nsecs - ONE_BILLION; - } else { - temp_rec.ts.nsecs += strict_time_adj.tv.nsecs; - } - rec = &temp_rec; - } - } else { + rec = wtap_get_rec(wth); + + if (rec->presence_flags & WTAP_HAS_TS) { + /* Do we adjust timestamps to ensure strict chronological + * order? */ + if (do_strict_time_adjustment) { + if (previous_time.secs || previous_time.nsecs) { + if (!strict_time_adj.is_negative) { + nstime_t current; + nstime_t delta; + + current = rec->ts; + + nstime_delta(&delta, ¤t, &previous_time); + + if (delta.secs < 0 || delta.nsecs < 0) { /* - * A negative strict time adjustment is requested. - * Unconditionally set each timestamp to previous - * packet's timestamp plus delta. - * Copy and change rather than modify returned - * rec. + * A negative delta indicates that the current packet + * has an absolute timestamp less than the previous packet + * that it is being compared to. This is NOT a normal + * situation since trace files usually have packets in + * chronological order (oldest to newest). + * Copy and change rather than modify + * returned rec. */ + /* fprintf(stderr, "++out of order, need to adjust this packet!\n"); */ temp_rec = *rec; temp_rec.ts.secs = previous_time.secs + strict_time_adj.tv.secs; temp_rec.ts.nsecs = previous_time.nsecs; @@ -1625,82 +1603,138 @@ real_main(int argc, char *argv[]) } rec = &temp_rec; } - } - previous_time = rec->ts; - } - - if (time_adj.tv.secs != 0) { - /* Copy and change rather than modify returned rec */ - temp_rec = *rec; - if (time_adj.is_negative) - temp_rec.ts.secs -= time_adj.tv.secs; - else - temp_rec.ts.secs += time_adj.tv.secs; - rec = &temp_rec; - } - - if (time_adj.tv.nsecs != 0) { - /* Copy and change rather than modify returned rec */ - temp_rec = *rec; - if (time_adj.is_negative) { /* subtract */ - if (temp_rec.ts.nsecs < time_adj.tv.nsecs) { /* borrow */ - temp_rec.ts.secs--; - temp_rec.ts.nsecs += ONE_BILLION; - } - temp_rec.ts.nsecs -= time_adj.tv.nsecs; - } else { /* add */ - if (temp_rec.ts.nsecs + time_adj.tv.nsecs >= ONE_BILLION) { + } else { + /* + * A negative strict time adjustment is requested. + * Unconditionally set each timestamp to previous + * packet's timestamp plus delta. + * Copy and change rather than modify returned + * rec. + */ + temp_rec = *rec; + temp_rec.ts.secs = previous_time.secs + strict_time_adj.tv.secs; + temp_rec.ts.nsecs = previous_time.nsecs; + if (temp_rec.ts.nsecs + strict_time_adj.tv.nsecs >= ONE_BILLION) { /* carry */ temp_rec.ts.secs++; - temp_rec.ts.nsecs += time_adj.tv.nsecs - ONE_BILLION; + temp_rec.ts.nsecs += strict_time_adj.tv.nsecs - ONE_BILLION; } else { - temp_rec.ts.nsecs += time_adj.tv.nsecs; + temp_rec.ts.nsecs += strict_time_adj.tv.nsecs; } + rec = &temp_rec; } - rec = &temp_rec; } - } /* time stamp adjustment */ + previous_time = rec->ts; + } - if (rec->rec_type == REC_TYPE_PACKET) { - if (snaplen != 0) { - /* Limit capture length to snaplen */ - if (rec->rec_header.packet_header.caplen > snaplen) { - /* Copy and change rather than modify returned wtap_rec */ - temp_rec = *rec; - temp_rec.rec_header.packet_header.caplen = snaplen; - rec = &temp_rec; + if (time_adj.tv.secs != 0) { + /* Copy and change rather than modify returned rec */ + temp_rec = *rec; + if (time_adj.is_negative) + temp_rec.ts.secs -= time_adj.tv.secs; + else + temp_rec.ts.secs += time_adj.tv.secs; + rec = &temp_rec; + } + + if (time_adj.tv.nsecs != 0) { + /* Copy and change rather than modify returned rec */ + temp_rec = *rec; + if (time_adj.is_negative) { /* subtract */ + if (temp_rec.ts.nsecs < time_adj.tv.nsecs) { /* borrow */ + temp_rec.ts.secs--; + temp_rec.ts.nsecs += ONE_BILLION; } - /* If -L, also set reported length to snaplen */ - if (adjlen && rec->rec_header.packet_header.len > snaplen) { - /* Copy and change rather than modify returned phdr */ - temp_rec = *rec; - temp_rec.rec_header.packet_header.len = snaplen; - rec = &temp_rec; + temp_rec.ts.nsecs -= time_adj.tv.nsecs; + } else { /* add */ + if (temp_rec.ts.nsecs + time_adj.tv.nsecs >= ONE_BILLION) { + /* carry */ + temp_rec.ts.secs++; + temp_rec.ts.nsecs += time_adj.tv.nsecs - ONE_BILLION; + } else { + temp_rec.ts.nsecs += time_adj.tv.nsecs; } } - - /* - * CHOP - * Copy and change rather than modify returned phdr. - */ - temp_rec = *rec; - handle_chopping(chop, &temp_rec.rec_header.packet_header, - &rec->rec_header.packet_header, &buf, - adjlen); rec = &temp_rec; + } + } /* time stamp adjustment */ - /* remove vlan info */ - if (rem_vlan) { - /* Copy and change rather than modify returned rec */ + if (rec->rec_type == REC_TYPE_PACKET) { + if (snaplen != 0) { + /* Limit capture length to snaplen */ + if (rec->rec_header.packet_header.caplen > snaplen) { + /* Copy and change rather than modify returned wtap_rec */ temp_rec = *rec; - remove_vlan_info(&rec->rec_header.packet_header, buf, - &temp_rec.rec_header.packet_header.caplen); + temp_rec.rec_header.packet_header.caplen = snaplen; rec = &temp_rec; } + /* If -L, also set reported length to snaplen */ + if (adjlen && rec->rec_header.packet_header.len > snaplen) { + /* Copy and change rather than modify returned phdr */ + temp_rec = *rec; + temp_rec.rec_header.packet_header.len = snaplen; + rec = &temp_rec; + } + } + + /* + * CHOP + * Copy and change rather than modify returned phdr. + */ + temp_rec = *rec; + handle_chopping(chop, &temp_rec.rec_header.packet_header, + &rec->rec_header.packet_header, &buf, + adjlen); + rec = &temp_rec; + + /* remove vlan info */ + if (rem_vlan) { + /* Copy and change rather than modify returned rec */ + temp_rec = *rec; + remove_vlan_info(&rec->rec_header.packet_header, buf, + &temp_rec.rec_header.packet_header.caplen); + rec = &temp_rec; + } - /* suppress duplicates by packet window */ - if (dup_detect) { - if (is_duplicate(buf, rec->rec_header.packet_header.caplen)) { + /* suppress duplicates by packet window */ + if (dup_detect) { + if (is_duplicate(buf, rec->rec_header.packet_header.caplen)) { + if (verbose) { + fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ", + count, + rec->rec_header.packet_header.caplen); + for (i = 0; i < 16; i++) + fprintf(stderr, "%02x", + (unsigned char)fd_hash[cur_dup_entry].digest[i]); + fprintf(stderr, "\n"); + } + duplicate_count++; + count++; + continue; + } else { + if (verbose) { + fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ", + count, + rec->rec_header.packet_header.caplen); + for (i = 0; i < 16; i++) + fprintf(stderr, "%02x", + (unsigned char)fd_hash[cur_dup_entry].digest[i]); + fprintf(stderr, "\n"); + } + } + } /* suppression of duplicates */ + + if (rec->presence_flags & WTAP_HAS_TS) { + /* suppress duplicates by time window */ + if (dup_detect_by_time) { + nstime_t current; + + current.secs = rec->ts.secs; + current.nsecs = rec->ts.nsecs; + + if (is_duplicate_rel_time(buf, + rec->rec_header.packet_header.caplen, + ¤t)) { if (verbose) { fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ", count, @@ -1724,204 +1758,167 @@ real_main(int argc, char *argv[]) fprintf(stderr, "\n"); } } - } /* suppression of duplicates */ - - if (rec->presence_flags & WTAP_HAS_TS) { - /* suppress duplicates by time window */ - if (dup_detect_by_time) { - nstime_t current; + } + } /* suppress duplicates by time window */ + } - current.secs = rec->ts.secs; - current.nsecs = rec->ts.nsecs; - - if (is_duplicate_rel_time(buf, - rec->rec_header.packet_header.caplen, - ¤t)) { - if (verbose) { - fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ", - count, - rec->rec_header.packet_header.caplen); - for (i = 0; i < 16; i++) - fprintf(stderr, "%02x", - (unsigned char)fd_hash[cur_dup_entry].digest[i]); - fprintf(stderr, "\n"); - } - duplicate_count++; - count++; - continue; - } else { - if (verbose) { - fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ", - count, - rec->rec_header.packet_header.caplen); - for (i = 0; i < 16; i++) - fprintf(stderr, "%02x", - (unsigned char)fd_hash[cur_dup_entry].digest[i]); - fprintf(stderr, "\n"); - } - } - } - } /* suppress duplicates by time window */ + /* Random error mutation */ + do_mutation = FALSE; + caplen = 0; + if (err_prob > 0.0) { + switch (rec->rec_type) { + + case REC_TYPE_PACKET: + caplen = rec->rec_header.packet_header.caplen; + do_mutation = TRUE; + break; + + case REC_TYPE_FT_SPECIFIC_EVENT: + case REC_TYPE_FT_SPECIFIC_REPORT: + caplen = rec->rec_header.ft_specific_header.record_len; + do_mutation = TRUE; + break; + + case REC_TYPE_SYSCALL: + caplen = rec->rec_header.syscall_header.event_filelen; + do_mutation = TRUE; + break; } - /* Random error mutation */ - do_mutation = FALSE; - caplen = 0; - if (err_prob > 0.0) { - switch (rec->rec_type) { - - case REC_TYPE_PACKET: - caplen = rec->rec_header.packet_header.caplen; - do_mutation = TRUE; - break; - - case REC_TYPE_FT_SPECIFIC_EVENT: - case REC_TYPE_FT_SPECIFIC_REPORT: - caplen = rec->rec_header.ft_specific_header.record_len; - do_mutation = TRUE; - break; - - case REC_TYPE_SYSCALL: - caplen = rec->rec_header.syscall_header.event_filelen; - do_mutation = TRUE; - break; - } - - if (change_offset > caplen) { - fprintf(stderr, "change offset %u is longer than caplen %u in packet %u\n", - change_offset, caplen, count); - do_mutation = FALSE; - } + if (change_offset > caplen) { + fprintf(stderr, "change offset %u is longer than caplen %u in packet %u\n", + change_offset, caplen, count); + do_mutation = FALSE; } + } - if (do_mutation) { - int real_data_start = 0; + if (do_mutation) { + int real_data_start = 0; - /* Protect non-protocol data */ - switch (rec->rec_type) { + /* Protect non-protocol data */ + switch (rec->rec_type) { - case REC_TYPE_PACKET: - if (wtap_file_type_subtype(wth) == WTAP_FILE_TYPE_SUBTYPE_CATAPULT_DCT2000) - real_data_start = find_dct2000_real_data(buf); - break; - } + case REC_TYPE_PACKET: + if (wtap_file_type_subtype(wth) == WTAP_FILE_TYPE_SUBTYPE_CATAPULT_DCT2000) + real_data_start = find_dct2000_real_data(buf); + break; + } - real_data_start += change_offset; + real_data_start += change_offset; - for (i = real_data_start; i < (int) caplen; i++) { - if (rand() <= err_prob * RAND_MAX) { - err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1); + for (i = real_data_start; i < (int) caplen; i++) { + if (rand() <= err_prob * RAND_MAX) { + err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1); - if (err_type < ERR_WT_BIT) { - buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1)); - err_type = ERR_WT_TOTAL; - } else { - err_type -= ERR_WT_BYTE; - } + if (err_type < ERR_WT_BIT) { + buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1)); + err_type = ERR_WT_TOTAL; + } else { + err_type -= ERR_WT_BYTE; + } - if (err_type < ERR_WT_BYTE) { - buf[i] = rand() / (RAND_MAX / 255 + 1); - err_type = ERR_WT_TOTAL; - } else { - err_type -= ERR_WT_BYTE; - } + if (err_type < ERR_WT_BYTE) { + buf[i] = rand() / (RAND_MAX / 255 + 1); + err_type = ERR_WT_TOTAL; + } else { + err_type -= ERR_WT_BYTE; + } - if (err_type < ERR_WT_ALNUM) { - buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)]; - err_type = ERR_WT_TOTAL; - } else { - err_type -= ERR_WT_ALNUM; - } + if (err_type < ERR_WT_ALNUM) { + buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)]; + err_type = ERR_WT_TOTAL; + } else { + err_type -= ERR_WT_ALNUM; + } - if (err_type < ERR_WT_FMT) { - if ((unsigned int)i < caplen - 2) - g_strlcpy((char*) &buf[i], "%s", 2); - err_type = ERR_WT_TOTAL; - } else { - err_type -= ERR_WT_FMT; - } + if (err_type < ERR_WT_FMT) { + if ((unsigned int)i < caplen - 2) + g_strlcpy((char*) &buf[i], "%s", 2); + err_type = ERR_WT_TOTAL; + } else { + err_type -= ERR_WT_FMT; + } - if (err_type < ERR_WT_AA) { - for (j = i; j < (int) caplen; j++) - buf[j] = 0xAA; - i = caplen; - } + if (err_type < ERR_WT_AA) { + for (j = i; j < (int) caplen; j++) + buf[j] = 0xAA; + i = caplen; } } - } /* random error mutation */ - - /* Find a packet comment we may need to write */ - if (frames_user_comments) { - const char *comment = - (const char*)g_tree_lookup(frames_user_comments, GUINT_TO_POINTER(read_count)); - /* XXX: What about comment changed to no comment? */ - if (comment != NULL) { - /* Copy and change rather than modify returned rec */ - temp_rec = *rec; - temp_rec.opt_comment = g_strdup(comment); - temp_rec.has_comment_changed = TRUE; - rec = &temp_rec; - } else { - /* Copy and change rather than modify returned rec */ - temp_rec = *rec; - temp_rec.has_comment_changed = FALSE; - rec = &temp_rec; - } } - - /* Attempt to dump out current frame to the output file */ - if (!wtap_dump(pdh, rec, buf, &write_err, &write_err_info)) { - cfile_write_failure_message("editcap", argv[optind], - filename, - write_err, write_err_info, - read_count, - out_file_type_subtype); - ret = DUMP_ERROR; - goto clean_exit; + } /* random error mutation */ + + /* Find a packet comment we may need to write */ + if (frames_user_comments) { + const char *comment = + (const char*)g_tree_lookup(frames_user_comments, GUINT_TO_POINTER(read_count)); + /* XXX: What about comment changed to no comment? */ + if (comment != NULL) { + /* Copy and change rather than modify returned rec */ + temp_rec = *rec; + temp_rec.opt_comment = g_strdup(comment); + temp_rec.has_comment_changed = TRUE; + rec = &temp_rec; + } else { + /* Copy and change rather than modify returned rec */ + temp_rec = *rec; + temp_rec.has_comment_changed = FALSE; + rec = &temp_rec; } - written_count++; } - count++; - } - - g_free(fprefix); - g_free(fsuffix); - - if (read_err != 0) { - /* Print a message noting that the read failed somewhere along the - * line. */ - cfile_read_failure_message("editcap", argv[optind], read_err, - read_err_info); - } - if (!pdh) { - /* No valid packages found, open the outfile so we can write an - * empty header */ - g_free (filename); - filename = g_strdup(argv[optind+1]); - - pdh = editcap_dump_open(filename, - snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth), - &ng_params, &write_err); - if (pdh == NULL) { - cfile_dump_open_failure_message("editcap", filename, - write_err, - out_file_type_subtype); - ret = INVALID_FILE; + /* Attempt to dump out current frame to the output file */ + if (!wtap_dump(pdh, rec, buf, &write_err, &write_err_info)) { + cfile_write_failure_message("editcap", argv[optind], + filename, + write_err, write_err_info, + read_count, + out_file_type_subtype); + ret = DUMP_ERROR; goto clean_exit; } + written_count++; } + count++; + } + + g_free(fprefix); + g_free(fsuffix); + + if (read_err != 0) { + /* Print a message noting that the read failed somewhere along the + * line. */ + cfile_read_failure_message("editcap", argv[optind], read_err, + read_err_info); + } - if (!wtap_dump_close(pdh, &write_err)) { - cfile_close_failure_message(filename, write_err); - ret = WRITE_ERROR; + if (!pdh) { + /* No valid packages found, open the outfile so we can write an + * empty header */ + g_free (filename); + filename = g_strdup(argv[optind+1]); + + pdh = editcap_dump_open(filename, + snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth), + &ng_params, &write_err); + if (pdh == NULL) { + cfile_dump_open_failure_message("editcap", filename, + write_err, + out_file_type_subtype); + ret = INVALID_FILE; goto clean_exit; } - g_free(filename); + } - if (frames_user_comments) { - g_tree_destroy(frames_user_comments); - } + if (!wtap_dump_close(pdh, &write_err)) { + cfile_close_failure_message(filename, write_err); + ret = WRITE_ERROR; + goto clean_exit; + } + g_free(filename); + + if (frames_user_comments) { + g_tree_destroy(frames_user_comments); } if (dup_detect) { -- cgit v1.2.3