aboutsummaryrefslogtreecommitdiffstats
path: root/print.c
diff options
context:
space:
mode:
authorChris Maynard <Christopher.Maynard@GTECH.COM>2010-11-08 17:05:30 +0000
committerChris Maynard <Christopher.Maynard@GTECH.COM>2010-11-08 17:05:30 +0000
commit3100cc2d408095237d38767716ff7b75f5404eb2 (patch)
treea1703935b4fe1b38bd0c6a97b33e3bc5f3790b6e /print.c
parent803910c1aa72c56f2ff8d6fcb7dc1633c253b5aa (diff)
Ensure strings are properly quoted for CSV output. Fixes bug 1297.
svn path=/trunk/; revision=34806
Diffstat (limited to 'print.c')
-rw-r--r--print.c42
1 files changed, 30 insertions, 12 deletions
diff --git a/print.c b/print.c
index 010e52eecd..5089c8dd60 100644
--- a/print.c
+++ b/print.c
@@ -615,25 +615,43 @@ write_csv_preamble(FILE *fh _U_)
write_headers = TRUE;
}
-void
-proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
+static gchar *csv_massage_str(const gchar *source, const gchar *exceptions)
{
- gint i;
+ gchar *csv_str;
+ gchar *tmp_str;
+
+ csv_str = g_strescape(source, exceptions);
+ tmp_str = csv_str;
+ while ( tmp_str = strstr(tmp_str, "\\\"") )
+ *tmp_str = '\"';
+ return csv_str;
+}
- /* if this is the first packet, we have to write the CSV header */
- if(write_headers) {
- for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
- fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
+static void csv_write_str(const char *str, char sep, FILE *fh)
+{
+ gchar *csv_str;
- fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
+ csv_str = csv_massage_str(str, NULL);
+ fprintf(fh, "\"%s\"%c", csv_str, sep);
+ g_free(csv_str);
+}
- write_headers = FALSE;
- }
+void
+proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
+{
+ gint i;
+ /* if this is the first packet, we have to write the CSV header */
+ if(write_headers) {
for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
- fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
+ csv_write_str(edt->pi.cinfo->col_title[i], ',', fh);
+ csv_write_str(edt->pi.cinfo->col_title[i], '\n', fh);
+ write_headers = FALSE;
+ }
- fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
+ for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
+ csv_write_str(edt->pi.cinfo->col_data[i], ',', fh);
+ csv_write_str(edt->pi.cinfo->col_data[i], '\n', fh);
}
void