From 36074c9828ff0f169656c2a0d5d022611b013a55 Mon Sep 17 00:00:00 2001 From: Chris Maynard Date: Fri, 9 Dec 2011 19:44:28 +0000 Subject: [PATCH] Export all data sources of a frame to a C array. Fixes https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=4988 svn path=/trunk/; revision=40136 --- file.c | 9 ++++++-- print.c | 69 +++++++++++++++++++++++++++++++++++++++++++-------------- print.h | 4 ++-- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/file.c b/file.c index f6b9af91d8..f63abd69c6 100644 --- a/file.c +++ b/file.c @@ -2708,12 +2708,17 @@ cf_write_csv_packets(capture_file *cf, print_args_t *print_args) static gboolean write_carrays_packet(capture_file *cf _U_, frame_data *fdata, - union wtap_pseudo_header *pseudo_header _U_, + union wtap_pseudo_header *pseudo_header, const guint8 *pd, void *argsp) { FILE *fh = argsp; + epan_dissect_t edt; + + epan_dissect_init(&edt, TRUE, TRUE); + epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL); + proto_tree_write_carrays(fdata->num, fh, &edt); + epan_dissect_cleanup(&edt); - proto_tree_write_carrays(pd, fdata->cap_len, fdata->num, fh); return !ferror(fh); } diff --git a/print.c b/print.c index 7243b8c0bf..876b04d02f 100644 --- a/print.c +++ b/print.c @@ -41,6 +41,7 @@ #include "packet-range.h" #include "print.h" +#include "isprint.h" #include "ps.h" #include "version_info.h" #include @@ -692,28 +693,62 @@ write_carrays_preamble(FILE *fh _U_) } void -proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh) +proto_tree_write_carrays(guint32 num, FILE *fh, epan_dissect_t *edt) { - guint32 i = 0; + guint32 i = 0, src_num = 0; + GSList *src_le; + data_source *src; + tvbuff_t *tvb; + const char *name; + const guchar *cp; + guint length; + char ascii[9]; - if (!len) - return; + for (src_le = edt->pi.data_src; src_le != NULL; src_le = src_le->next) { + memset(ascii, 0, sizeof(ascii)); + src = (data_source *)src_le->data; + tvb = src->tvb; + length = tvb_length(tvb); + if (length == 0) + continue; - fprintf(fh, "char pkt%u[] = {\n", num); + cp = tvb_get_ptr(tvb, 0, length); - for (i = 0; i < len; i++) { - - fprintf(fh, "0x%02x", *(pd + i)); - - if (i == (len - 1)) { - fprintf(fh, " };\n\n"); - break; - } - - if (!((i + 1) % 8)) { - fprintf(fh, ", \n"); + name = get_data_source_name(src); + if (name) + fprintf(fh, "/* %s */\n", name); + if (src_num) { + fprintf(fh, "static const unsigned char pkt%u_%u[%u] = {\n", + num, src_num, length); } else { - fprintf(fh, ", "); + fprintf(fh, "static const unsigned char pkt%u[%u] = {\n", + num, length); + } + src_num++; + + for (i = 0; i < length; i++) { + fprintf(fh, "0x%02x", *(cp + i)); + ascii[i % 8] = isprint(*(cp + i)) ? *(cp + i) : '.'; + + if (i == (length - 1)) { + guint rem; + rem = length % 8; + if (rem) { + guint j; + for ( j = 0; j < 8 - rem; j++ ) + fprintf(fh, " "); + } + fprintf(fh, " /* %s */\n};\n\n", ascii); + break; + } + + if (!((i + 1) % 8)) { + fprintf(fh, ", /* %s */\n", ascii); + memset(ascii, 0, sizeof(ascii)); + } + else { + fprintf(fh, ", "); + } } } } diff --git a/print.h b/print.h index 8ab23ac257..f4fd827f82 100644 --- a/print.h +++ b/print.h @@ -103,7 +103,7 @@ typedef struct { print_dissections_e print_dissections; gboolean print_hex; /* TRUE if we should also print hex data; FALSE if we should print only if not dissected. */ - gboolean print_formfeed; /* TRUE if a formfeed should be printed + gboolean print_formfeed; /* TRUE if a formfeed should be printed before each new packet */ } print_args_t; @@ -146,7 +146,7 @@ extern void proto_tree_write_csv(epan_dissect_t *edt, FILE *fh); extern void write_csv_finale(FILE *fh); extern void write_carrays_preamble(FILE *fh); -extern void proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh); +extern void proto_tree_write_carrays(guint32 num, FILE *fh, epan_dissect_t *edt); extern void write_carrays_finale(FILE *fh); extern void write_fields_preamble(output_fields_t* fields, FILE *fh);