From 982fc7184d46621948e53b485c7504c9d11f3350 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 17 May 2024 17:00:59 +0200 Subject: Merging upstream version 4.2.5. Signed-off-by: Daniel Baumann --- reordercap.c | 94 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 40 deletions(-) (limited to 'reordercap.c') diff --git a/reordercap.c b/reordercap.c index ed4a3649..a8aba004 100644 --- a/reordercap.c +++ b/reordercap.c @@ -276,27 +276,6 @@ main(int argc, char *argv[]) } DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth)); - wtap_dump_params_init(¶ms, wth); - - /* Open outfile (same filetype/encap as input file) */ - if (strcmp(outfile, "-") == 0) { - pdh = wtap_dump_open_stdout(wtap_file_type_subtype(wth), - WTAP_UNCOMPRESSED, ¶ms, &err, &err_info); - } else { - pdh = wtap_dump_open(outfile, wtap_file_type_subtype(wth), - WTAP_UNCOMPRESSED, ¶ms, &err, &err_info); - } - g_free(params.idb_inf); - params.idb_inf = NULL; - - if (pdh == NULL) { - cfile_dump_open_failure_message(outfile, err, err_info, - wtap_file_type_subtype(wth)); - wtap_dump_params_cleanup(¶ms); - ret = OUTPUT_FILE_ERROR; - goto clean_exit; - } - /* Allocate the array of frame pointers. */ frames = g_ptr_array_new(); @@ -332,40 +311,75 @@ main(int argc, char *argv[]) printf("%u frames, %u out of order\n", frames->len, wrong_order_count); + wtap_dump_params_init(¶ms, wth); + /* Sort the frames */ + /* XXX - Does this handle multiple SHBs correctly? */ if (wrong_order_count > 0) { g_ptr_array_sort(frames, frames_compare); } - /* Write out each sorted frame in turn */ - wtap_rec_init(&rec); - ws_buffer_init(&buf, 1514); - for (i = 0; i < frames->len; i++) { - FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i]; - /* Avoid writing if already sorted and configured to */ - if (write_output_regardless || (wrong_order_count > 0)) { + /* Avoid writing if already sorted and configured to */ + if (write_output_regardless || (wrong_order_count > 0)) { + /* Open outfile (same filetype/encap as input file) */ + if (strcmp(outfile, "-") == 0) { + pdh = wtap_dump_open_stdout(wtap_file_type_subtype(wth), + WTAP_UNCOMPRESSED, ¶ms, &err, &err_info); + } else { + pdh = wtap_dump_open(outfile, wtap_file_type_subtype(wth), + WTAP_UNCOMPRESSED, ¶ms, &err, &err_info); + } + g_free(params.idb_inf); + params.idb_inf = NULL; + + if (pdh == NULL) { + cfile_dump_open_failure_message(outfile, err, err_info, + wtap_file_type_subtype(wth)); + wtap_dump_params_cleanup(¶ms); + ret = OUTPUT_FILE_ERROR; + goto clean_exit; + } + + + /* Write out each sorted frame in turn */ + wtap_rec_init(&rec); + ws_buffer_init(&buf, 1514); + for (i = 0; i < frames->len; i++) { + FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i]; + frame_write(frame, wth, pdh, &rec, &buf, infile, outfile); + + g_slice_free(FrameRecord_t, frame); } - g_slice_free(FrameRecord_t, frame); - } - wtap_rec_cleanup(&rec); - ws_buffer_free(&buf); - if (!write_output_regardless && (wrong_order_count == 0)) { + wtap_rec_cleanup(&rec); + ws_buffer_free(&buf); + + + + /* Close outfile */ + if (!wtap_dump_close(pdh, NULL, &err, &err_info)) { + cfile_close_failure_message(outfile, err, err_info); + wtap_dump_params_cleanup(¶ms); + ret = OUTPUT_FILE_ERROR; + goto clean_exit; + } + } else { printf("Not writing output file because input file is already in order.\n"); + + /* Free frame memory */ + for (i = 0; i < frames->len; i++) { + FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i]; + + g_slice_free(FrameRecord_t, frame); + } } + /* Free the whole array */ g_ptr_array_free(frames, TRUE); - /* Close outfile */ - if (!wtap_dump_close(pdh, NULL, &err, &err_info)) { - cfile_close_failure_message(outfile, err, err_info); - wtap_dump_params_cleanup(¶ms); - ret = OUTPUT_FILE_ERROR; - goto clean_exit; - } wtap_dump_params_cleanup(¶ms); /* Finally, close infile and release resources. */ -- cgit v1.2.3