PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/epan/print.c

https://github.com/labx-technologies-llc/wireshark
C | 1811 lines | 1495 code | 164 blank | 152 comment | 167 complexity | 4a63a1e05e848d850b4b55c1386ee403 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /* print.c
  2. * Routines for printing packet analysis trees.
  3. *
  4. * $Id$
  5. *
  6. * Gilbert Ramirez <gram@alumni.rice.edu>
  7. *
  8. * Wireshark - Network traffic analyzer
  9. * By Gerald Combs <gerald@wireshark.org>
  10. * Copyright 1998 Gerald Combs
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25. */
  26. #include "config.h"
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <glib.h>
  30. #include <epan/epan.h>
  31. #include <epan/epan_dissect.h>
  32. #include <epan/tvbuff.h>
  33. #include <epan/packet.h>
  34. #include <epan/emem.h>
  35. #include <epan/expert.h>
  36. #include <epan/packet-range.h>
  37. #include "print.h"
  38. #include "isprint.h"
  39. #include "ps.h"
  40. #include "version_info.h"
  41. #include <wsutil/file_util.h>
  42. #include <epan/charsets.h>
  43. #include <epan/dissectors/packet-data.h>
  44. #include <epan/dissectors/packet-frame.h>
  45. #include <epan/filesystem.h>
  46. #define PDML_VERSION "0"
  47. #define PSML_VERSION "0"
  48. typedef struct {
  49. int level;
  50. print_stream_t *stream;
  51. gboolean success;
  52. GSList *src_list;
  53. print_dissections_e print_dissections;
  54. gboolean print_hex_for_data;
  55. packet_char_enc encoding;
  56. epan_dissect_t *edt;
  57. } print_data;
  58. typedef struct {
  59. int level;
  60. FILE *fh;
  61. GSList *src_list;
  62. epan_dissect_t *edt;
  63. } write_pdml_data;
  64. typedef struct {
  65. output_fields_t *fields;
  66. epan_dissect_t *edt;
  67. } write_field_data_t;
  68. struct _output_fields {
  69. gboolean print_header;
  70. gchar separator;
  71. gchar occurrence;
  72. gchar aggregator;
  73. GPtrArray *fields;
  74. GHashTable *field_indicies;
  75. GPtrArray **field_values;
  76. gchar quote;
  77. gboolean includes_col_fields;
  78. };
  79. GHashTable *output_only_tables = NULL;
  80. static gboolean write_headers = FALSE;
  81. static const gchar *get_field_hex_value(GSList *src_list, field_info *fi);
  82. static void proto_tree_print_node(proto_node *node, gpointer data);
  83. static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
  84. static const guint8 *get_field_data(GSList *src_list, field_info *fi);
  85. static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
  86. static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
  87. guint length, packet_char_enc encoding);
  88. static void ps_clean_string(char *out, const char *in,
  89. int outbuf_size);
  90. static void print_escaped_xml(FILE *fh, const char *unescaped_string);
  91. static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
  92. static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
  93. static FILE *
  94. open_print_dest(gboolean to_file, const char *dest)
  95. {
  96. FILE *fh;
  97. /* Open the file or command for output */
  98. if (to_file)
  99. fh = ws_fopen(dest, "w");
  100. else
  101. fh = popen(dest, "w");
  102. return fh;
  103. }
  104. static gboolean
  105. close_print_dest(gboolean to_file, FILE *fh)
  106. {
  107. /* Close the file or command */
  108. if (to_file)
  109. return (fclose(fh) == 0);
  110. else
  111. return (pclose(fh) == 0);
  112. }
  113. #define MAX_PS_LINE_LENGTH 256
  114. gboolean
  115. proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
  116. print_stream_t *stream)
  117. {
  118. print_data data;
  119. /* Create the output */
  120. data.level = 0;
  121. data.stream = stream;
  122. data.success = TRUE;
  123. data.src_list = edt->pi.data_src;
  124. data.encoding = (packet_char_enc)edt->pi.fd->flags.encoding;
  125. data.print_dissections = print_args->print_dissections;
  126. /* If we're printing the entire packet in hex, don't
  127. print uninterpreted data fields in hex as well. */
  128. data.print_hex_for_data = !print_args->print_hex;
  129. data.edt = edt;
  130. proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
  131. return data.success;
  132. }
  133. #define MAX_INDENT 160
  134. /* Print a tree's data, and any child nodes. */
  135. static void
  136. proto_tree_print_node(proto_node *node, gpointer data)
  137. {
  138. field_info *fi = PNODE_FINFO(node);
  139. print_data *pdata = (print_data*) data;
  140. const guint8 *pd;
  141. gchar label_str[ITEM_LABEL_LENGTH];
  142. gchar *label_ptr;
  143. /* dissection with an invisible proto tree? */
  144. g_assert(fi);
  145. /* Don't print invisible entries. */
  146. if (PROTO_ITEM_IS_HIDDEN(node))
  147. return;
  148. /* Give up if we've already gotten an error. */
  149. if (!pdata->success)
  150. return;
  151. /* was a free format label produced? */
  152. if (fi->rep) {
  153. label_ptr = fi->rep->representation;
  154. }
  155. else { /* no, make a generic label */
  156. label_ptr = label_str;
  157. proto_item_fill_label(fi, label_str);
  158. }
  159. if (PROTO_ITEM_IS_GENERATED(node))
  160. label_ptr = g_strconcat("[", label_ptr, "]", NULL);
  161. pdata->success = print_line(pdata->stream, pdata->level, label_ptr);
  162. if (PROTO_ITEM_IS_GENERATED(node))
  163. g_free(label_ptr);
  164. if (!pdata->success)
  165. return;
  166. /*
  167. * If -O is specified, only display the protocols which are in the
  168. * lookup table. Only check on the first level: once we start printing
  169. * a tree, print the rest of the subtree. Otherwise we won't print
  170. * subitems whose abbreviation doesn't match the protocol--for example
  171. * text items (whose abbreviation is simply "text").
  172. */
  173. if ((output_only_tables != NULL) && (pdata->level == 0)
  174. && (g_hash_table_lookup(output_only_tables, fi->hfinfo->abbrev) == NULL)) {
  175. return;
  176. }
  177. /* If it's uninterpreted data, dump it (unless our caller will
  178. be printing the entire packet in hex). */
  179. if ((fi->hfinfo->id == proto_data) && (pdata->print_hex_for_data)) {
  180. /*
  181. * Find the data for this field.
  182. */
  183. pd = get_field_data(pdata->src_list, fi);
  184. if (pd) {
  185. if (!print_line(pdata->stream, 0, "")) {
  186. pdata->success = FALSE;
  187. return;
  188. }
  189. if (!print_hex_data_buffer(pdata->stream, pd,
  190. fi->length, pdata->encoding)) {
  191. pdata->success = FALSE;
  192. return;
  193. }
  194. }
  195. }
  196. /* If we're printing all levels, or if this node is one with a
  197. subtree and its subtree is expanded, recurse into the subtree,
  198. if it exists. */
  199. g_assert((fi->tree_type >= -1) && (fi->tree_type < num_tree_types));
  200. if ((pdata->print_dissections == print_dissections_expanded) ||
  201. ((pdata->print_dissections == print_dissections_as_displayed) &&
  202. (fi->tree_type >= 0) && tree_expanded(fi->tree_type))) {
  203. if (node->first_child != NULL) {
  204. pdata->level++;
  205. proto_tree_children_foreach(node,
  206. proto_tree_print_node, pdata);
  207. pdata->level--;
  208. if (!pdata->success)
  209. return;
  210. }
  211. }
  212. }
  213. #define PDML2HTML_XSL "pdml2html.xsl"
  214. void
  215. write_pdml_preamble(FILE *fh, const gchar *filename)
  216. {
  217. time_t t = time(NULL);
  218. char *ts = asctime(localtime(&t));
  219. ts[strlen(ts)-1] = 0; /* overwrite \n */
  220. fputs("<?xml version=\"1.0\"?>\n", fh);
  221. fputs("<?xml-stylesheet type=\"text/xsl\" href=\"" PDML2HTML_XSL "\"?>\n", fh);
  222. fprintf(fh, "<!-- You can find " PDML2HTML_XSL " in %s or at http://anonsvn.wireshark.org/trunk/wireshark/" PDML2HTML_XSL ". -->\n", get_datafile_dir());
  223. fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
  224. fprintf(fh, "creator=\"%s/%s\" time=\"%s\" capture_file=\"%s\">\n", PACKAGE, VERSION, ts, filename ? filename : "");
  225. }
  226. void
  227. proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
  228. {
  229. write_pdml_data data;
  230. /* Create the output */
  231. data.level = 0;
  232. data.fh = fh;
  233. data.src_list = edt->pi.data_src;
  234. data.edt = edt;
  235. fprintf(fh, "<packet>\n");
  236. /* Print a "geninfo" protocol as required by PDML */
  237. print_pdml_geninfo(edt->tree, fh);
  238. proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
  239. &data);
  240. fprintf(fh, "</packet>\n\n");
  241. }
  242. /* Write out a tree's data, and any child nodes, as PDML */
  243. static void
  244. proto_tree_write_node_pdml(proto_node *node, gpointer data)
  245. {
  246. field_info *fi = PNODE_FINFO(node);
  247. write_pdml_data *pdata = (write_pdml_data*) data;
  248. const gchar *label_ptr;
  249. gchar label_str[ITEM_LABEL_LENGTH];
  250. char *dfilter_string;
  251. size_t chop_len;
  252. int i;
  253. gboolean wrap_in_fake_protocol;
  254. /* dissection with an invisible proto tree? */
  255. g_assert(fi);
  256. /* Will wrap up top-level field items inside a fake protocol wrapper to
  257. preserve the PDML schema */
  258. wrap_in_fake_protocol =
  259. (((fi->hfinfo->type != FT_PROTOCOL) ||
  260. (fi->hfinfo->id == proto_data)) &&
  261. (pdata->level == 0));
  262. /* Indent to the correct level */
  263. for (i = -1; i < pdata->level; i++) {
  264. fputs(" ", pdata->fh);
  265. }
  266. if (wrap_in_fake_protocol) {
  267. /* Open fake protocol wrapper */
  268. fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
  269. /* Indent to increased level before writing out field */
  270. pdata->level++;
  271. for (i = -1; i < pdata->level; i++) {
  272. fputs(" ", pdata->fh);
  273. }
  274. }
  275. /* Text label. It's printed as a field with no name. */
  276. if (fi->hfinfo->id == hf_text_only) {
  277. /* Get the text */
  278. if (fi->rep) {
  279. label_ptr = fi->rep->representation;
  280. }
  281. else {
  282. label_ptr = "";
  283. }
  284. /* Show empty name since it is a required field */
  285. fputs("<field name=\"", pdata->fh);
  286. fputs("\" show=\"", pdata->fh);
  287. print_escaped_xml(pdata->fh, label_ptr);
  288. fprintf(pdata->fh, "\" size=\"%d", fi->length);
  289. if (node->parent && node->parent->finfo && (fi->start < node->parent->finfo->start)) {
  290. fprintf(pdata->fh, "\" pos=\"%d", node->parent->finfo->start + fi->start);
  291. } else {
  292. fprintf(pdata->fh, "\" pos=\"%d", fi->start);
  293. }
  294. fputs("\" value=\"", pdata->fh);
  295. write_pdml_field_hex_value(pdata, fi);
  296. if (node->first_child != NULL) {
  297. fputs("\">\n", pdata->fh);
  298. }
  299. else {
  300. fputs("\"/>\n", pdata->fh);
  301. }
  302. }
  303. /* Uninterpreted data, i.e., the "Data" protocol, is
  304. * printed as a field instead of a protocol. */
  305. else if (fi->hfinfo->id == proto_data) {
  306. /* Write out field with data */
  307. fputs("<field name=\"data\" value=\"", pdata->fh);
  308. write_pdml_field_hex_value(pdata, fi);
  309. fputs("\">\n", pdata->fh);
  310. }
  311. /* Normal protocols and fields */
  312. else {
  313. if ((fi->hfinfo->type == FT_PROTOCOL) && (fi->hfinfo->id != proto_expert)) {
  314. fputs("<proto name=\"", pdata->fh);
  315. }
  316. else {
  317. fputs("<field name=\"", pdata->fh);
  318. }
  319. print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
  320. #if 0
  321. /* PDML spec, see:
  322. * http://www.nbee.org/doku.php?id=netpdl:pdml_specification
  323. *
  324. * the show fields contains things in 'human readable' format
  325. * showname: contains only the name of the field
  326. * show: contains only the data of the field
  327. * showdtl: contains additional details of the field data
  328. * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
  329. *
  330. * XXX - the showname shouldn't contain the field data itself
  331. * (like it's contained in the fi->rep->representation).
  332. * Unfortunately, we don't have the field data representation for
  333. * all fields, so this isn't currently possible */
  334. fputs("\" showname=\"", pdata->fh);
  335. print_escaped_xml(pdata->fh, fi->hfinfo->name);
  336. #endif
  337. if (fi->rep) {
  338. fputs("\" showname=\"", pdata->fh);
  339. print_escaped_xml(pdata->fh, fi->rep->representation);
  340. }
  341. else {
  342. label_ptr = label_str;
  343. proto_item_fill_label(fi, label_str);
  344. fputs("\" showname=\"", pdata->fh);
  345. print_escaped_xml(pdata->fh, label_ptr);
  346. }
  347. if (PROTO_ITEM_IS_HIDDEN(node))
  348. fprintf(pdata->fh, "\" hide=\"yes");
  349. fprintf(pdata->fh, "\" size=\"%d", fi->length);
  350. if (node->parent && node->parent->finfo && (fi->start < node->parent->finfo->start)) {
  351. fprintf(pdata->fh, "\" pos=\"%d", node->parent->finfo->start + fi->start);
  352. } else {
  353. fprintf(pdata->fh, "\" pos=\"%d", fi->start);
  354. }
  355. /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
  356. /* show, value, and unmaskedvalue attributes */
  357. switch (fi->hfinfo->type)
  358. {
  359. case FT_PROTOCOL:
  360. break;
  361. case FT_NONE:
  362. fputs("\" show=\"\" value=\"", pdata->fh);
  363. break;
  364. default:
  365. /* XXX - this is a hack until we can just call
  366. * fvalue_to_string_repr() for *all* FT_* types. */
  367. dfilter_string = proto_construct_match_selected_string(fi,
  368. pdata->edt);
  369. if (dfilter_string != NULL) {
  370. chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
  371. /* XXX - Remove double-quotes. Again, once we
  372. * can call fvalue_to_string_repr(), we can
  373. * ask it not to produce the version for
  374. * display-filters, and thus, no
  375. * double-quotes. */
  376. if (dfilter_string[strlen(dfilter_string)-1] == '"') {
  377. dfilter_string[strlen(dfilter_string)-1] = '\0';
  378. chop_len++;
  379. }
  380. fputs("\" show=\"", pdata->fh);
  381. print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
  382. }
  383. /*
  384. * XXX - should we omit "value" for any fields?
  385. * What should we do for fields whose length is 0?
  386. * They might come from a pseudo-header or from
  387. * the capture header (e.g., time stamps), or
  388. * they might be generated fields.
  389. */
  390. if (fi->length > 0) {
  391. fputs("\" value=\"", pdata->fh);
  392. if (fi->hfinfo->bitmask!=0) {
  393. fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
  394. fputs("\" unmaskedvalue=\"", pdata->fh);
  395. write_pdml_field_hex_value(pdata, fi);
  396. }
  397. else {
  398. write_pdml_field_hex_value(pdata, fi);
  399. }
  400. }
  401. }
  402. if (node->first_child != NULL) {
  403. fputs("\">\n", pdata->fh);
  404. }
  405. else if (fi->hfinfo->id == proto_data) {
  406. fputs("\">\n", pdata->fh);
  407. }
  408. else {
  409. fputs("\"/>\n", pdata->fh);
  410. }
  411. }
  412. /* We always print all levels for PDML. Recurse here. */
  413. if (node->first_child != NULL) {
  414. pdata->level++;
  415. proto_tree_children_foreach(node,
  416. proto_tree_write_node_pdml, pdata);
  417. pdata->level--;
  418. }
  419. /* Take back the extra level we added for fake wrapper protocol */
  420. if (wrap_in_fake_protocol) {
  421. pdata->level--;
  422. }
  423. if (node->first_child != NULL) {
  424. /* Indent to correct level */
  425. for (i = -1; i < pdata->level; i++) {
  426. fputs(" ", pdata->fh);
  427. }
  428. /* Close off current element */
  429. /* Data and expert "protocols" use simple tags */
  430. if ((fi->hfinfo->id != proto_data) && (fi->hfinfo->id != proto_expert)) {
  431. if (fi->hfinfo->type == FT_PROTOCOL) {
  432. fputs("</proto>\n", pdata->fh);
  433. }
  434. else {
  435. fputs("</field>\n", pdata->fh);
  436. }
  437. } else {
  438. fputs("</field>\n", pdata->fh);
  439. }
  440. }
  441. /* Close off fake wrapper protocol */
  442. if (wrap_in_fake_protocol) {
  443. fputs("</proto>\n", pdata->fh);
  444. }
  445. }
  446. /* Print info for a 'geninfo' pseudo-protocol. This is required by
  447. * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
  448. * but we produce a 'geninfo' protocol in the PDML to conform to spec.
  449. * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
  450. static void
  451. print_pdml_geninfo(proto_tree *tree, FILE *fh)
  452. {
  453. guint32 num, len, caplen;
  454. nstime_t *timestamp;
  455. GPtrArray *finfo_array;
  456. field_info *frame_finfo;
  457. /* Get frame protocol's finfo. */
  458. finfo_array = proto_find_finfo(tree, proto_frame);
  459. if (g_ptr_array_len(finfo_array) < 1) {
  460. return;
  461. }
  462. frame_finfo = (field_info *)finfo_array->pdata[0];
  463. g_ptr_array_free(finfo_array, TRUE);
  464. /* frame.number --> geninfo.num */
  465. finfo_array = proto_find_finfo(tree, hf_frame_number);
  466. if (g_ptr_array_len(finfo_array) < 1) {
  467. return;
  468. }
  469. num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
  470. g_ptr_array_free(finfo_array, TRUE);
  471. /* frame.frame_len --> geninfo.len */
  472. finfo_array = proto_find_finfo(tree, hf_frame_len);
  473. if (g_ptr_array_len(finfo_array) < 1) {
  474. return;
  475. }
  476. len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
  477. g_ptr_array_free(finfo_array, TRUE);
  478. /* frame.cap_len --> geninfo.caplen */
  479. finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
  480. if (g_ptr_array_len(finfo_array) < 1) {
  481. return;
  482. }
  483. caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
  484. g_ptr_array_free(finfo_array, TRUE);
  485. /* frame.time --> geninfo.timestamp */
  486. finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
  487. if (g_ptr_array_len(finfo_array) < 1) {
  488. return;
  489. }
  490. timestamp = (nstime_t *)fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
  491. g_ptr_array_free(finfo_array, TRUE);
  492. /* Print geninfo start */
  493. fprintf(fh,
  494. " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
  495. frame_finfo->length);
  496. /* Print geninfo.num */
  497. fprintf(fh,
  498. " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
  499. num, num, frame_finfo->length);
  500. /* Print geninfo.len */
  501. fprintf(fh,
  502. " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%u\"/>\n",
  503. len, len, frame_finfo->length);
  504. /* Print geninfo.caplen */
  505. fprintf(fh,
  506. " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
  507. caplen, caplen, frame_finfo->length);
  508. /* Print geninfo.timestamp */
  509. fprintf(fh,
  510. " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
  511. abs_time_to_str(timestamp, ABSOLUTE_TIME_LOCAL, TRUE), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
  512. /* Print geninfo end */
  513. fprintf(fh,
  514. " </proto>\n");
  515. }
  516. void
  517. write_pdml_finale(FILE *fh)
  518. {
  519. fputs("</pdml>\n", fh);
  520. }
  521. void
  522. write_psml_preamble(FILE *fh)
  523. {
  524. fputs("<?xml version=\"1.0\"?>\n", fh);
  525. fputs("<psml version=\"" PSML_VERSION "\" ", fh);
  526. fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
  527. write_headers = TRUE;
  528. }
  529. void
  530. proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
  531. {
  532. gint i;
  533. /* if this is the first packet, we have to create the PSML structure output */
  534. if (write_headers) {
  535. fprintf(fh, "<structure>\n");
  536. for (i = 0; i < edt->pi.cinfo->num_cols; i++) {
  537. fprintf(fh, "<section>");
  538. print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
  539. fprintf(fh, "</section>\n");
  540. }
  541. fprintf(fh, "</structure>\n\n");
  542. write_headers = FALSE;
  543. }
  544. fprintf(fh, "<packet>\n");
  545. for (i = 0; i < edt->pi.cinfo->num_cols; i++) {
  546. fprintf(fh, "<section>");
  547. print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
  548. fprintf(fh, "</section>\n");
  549. }
  550. fprintf(fh, "</packet>\n\n");
  551. }
  552. void
  553. write_psml_finale(FILE *fh)
  554. {
  555. fputs("</psml>\n", fh);
  556. }
  557. void
  558. write_csv_preamble(FILE *fh _U_)
  559. {
  560. write_headers = TRUE;
  561. }
  562. static gchar *csv_massage_str(const gchar *source, const gchar *exceptions)
  563. {
  564. gchar *csv_str;
  565. gchar *tmp_str;
  566. csv_str = g_strescape(source, exceptions);
  567. tmp_str = csv_str;
  568. while ( (tmp_str = strstr(tmp_str, "\\\"")) != NULL )
  569. *tmp_str = '\"';
  570. return csv_str;
  571. }
  572. static void csv_write_str(const char *str, char sep, FILE *fh)
  573. {
  574. gchar *csv_str;
  575. csv_str = csv_massage_str(str, NULL);
  576. fprintf(fh, "\"%s\"%c", csv_str, sep);
  577. g_free(csv_str);
  578. }
  579. void
  580. proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
  581. {
  582. gint i;
  583. /* if this is the first packet, we have to write the CSV header */
  584. if (write_headers) {
  585. for (i = 0; i < edt->pi.cinfo->num_cols - 1; i++)
  586. csv_write_str(edt->pi.cinfo->col_title[i], ',', fh);
  587. csv_write_str(edt->pi.cinfo->col_title[i], '\n', fh);
  588. write_headers = FALSE;
  589. }
  590. for (i = 0; i < edt->pi.cinfo->num_cols - 1; i++)
  591. csv_write_str(edt->pi.cinfo->col_data[i], ',', fh);
  592. csv_write_str(edt->pi.cinfo->col_data[i], '\n', fh);
  593. }
  594. void
  595. write_csv_finale(FILE *fh _U_)
  596. {
  597. }
  598. void
  599. write_carrays_preamble(FILE *fh _U_)
  600. {
  601. }
  602. void
  603. proto_tree_write_carrays(guint32 num, FILE *fh, epan_dissect_t *edt)
  604. {
  605. guint32 i = 0, src_num = 0;
  606. GSList *src_le;
  607. tvbuff_t *tvb;
  608. const char *name;
  609. const guchar *cp;
  610. guint length;
  611. char ascii[9];
  612. struct data_source *src;
  613. for (src_le = edt->pi.data_src; src_le != NULL; src_le = src_le->next) {
  614. memset(ascii, 0, sizeof(ascii));
  615. src = (struct data_source *)src_le->data;
  616. tvb = get_data_source_tvb(src);
  617. length = tvb_length(tvb);
  618. if (length == 0)
  619. continue;
  620. cp = tvb_get_ptr(tvb, 0, length);
  621. name = get_data_source_name(src);
  622. if (name)
  623. fprintf(fh, "/* %s */\n", name);
  624. if (src_num) {
  625. fprintf(fh, "static const unsigned char pkt%u_%u[%u] = {\n",
  626. num, src_num, length);
  627. } else {
  628. fprintf(fh, "static const unsigned char pkt%u[%u] = {\n",
  629. num, length);
  630. }
  631. src_num++;
  632. for (i = 0; i < length; i++) {
  633. fprintf(fh, "0x%02x", *(cp + i));
  634. ascii[i % 8] = isprint(*(cp + i)) ? *(cp + i) : '.';
  635. if (i == (length - 1)) {
  636. guint rem;
  637. rem = length % 8;
  638. if (rem) {
  639. guint j;
  640. for ( j = 0; j < 8 - rem; j++ )
  641. fprintf(fh, " ");
  642. }
  643. fprintf(fh, " /* %s */\n};\n\n", ascii);
  644. break;
  645. }
  646. if (!((i + 1) % 8)) {
  647. fprintf(fh, ", /* %s */\n", ascii);
  648. memset(ascii, 0, sizeof(ascii));
  649. }
  650. else {
  651. fprintf(fh, ", ");
  652. }
  653. }
  654. }
  655. }
  656. void
  657. write_carrays_finale(FILE *fh _U_)
  658. {
  659. }
  660. /*
  661. * Find the data source for a specified field, and return a pointer
  662. * to the data in it. Returns NULL if the data is out of bounds.
  663. */
  664. /* XXX: What am I missing ?
  665. * Why bother searching for fi->ds_tvb for the matching tvb
  666. * in the data_source list ?
  667. * IOW: Why not just use fi->ds_tvb for the arg to tvb_get_ptr() ?
  668. */
  669. static const guint8 *
  670. get_field_data(GSList *src_list, field_info *fi)
  671. {
  672. GSList *src_le;
  673. tvbuff_t *src_tvb;
  674. gint length, tvbuff_length;
  675. struct data_source *src;
  676. for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
  677. src = (struct data_source *)src_le->data;
  678. src_tvb = get_data_source_tvb(src);
  679. if (fi->ds_tvb == src_tvb) {
  680. /*
  681. * Found it.
  682. *
  683. * XXX - a field can have a length that runs past
  684. * the end of the tvbuff. Ideally, that should
  685. * be fixed when adding an item to the protocol
  686. * tree, but checking the length when doing
  687. * that could be expensive. Until we fix that,
  688. * we'll do the check here.
  689. */
  690. tvbuff_length = tvb_length_remaining(src_tvb,
  691. fi->start);
  692. if (tvbuff_length < 0) {
  693. return NULL;
  694. }
  695. length = fi->length;
  696. if (length > tvbuff_length)
  697. length = tvbuff_length;
  698. return tvb_get_ptr(src_tvb, fi->start, length);
  699. }
  700. }
  701. g_assert_not_reached();
  702. return NULL; /* not found */
  703. }
  704. /* Print a string, escaping out certain characters that need to
  705. * escaped out for XML. */
  706. static void
  707. print_escaped_xml(FILE *fh, const char *unescaped_string)
  708. {
  709. const char *p;
  710. char temp_str[8];
  711. for (p = unescaped_string; *p != '\0'; p++) {
  712. switch (*p) {
  713. case '&':
  714. fputs("&amp;", fh);
  715. break;
  716. case '<':
  717. fputs("&lt;", fh);
  718. break;
  719. case '>':
  720. fputs("&gt;", fh);
  721. break;
  722. case '"':
  723. fputs("&quot;", fh);
  724. break;
  725. case '\'':
  726. fputs("&apos;", fh);
  727. break;
  728. default:
  729. if (g_ascii_isprint(*p))
  730. fputc(*p, fh);
  731. else {
  732. g_snprintf(temp_str, sizeof(temp_str), "\\x%x", (guint8)*p);
  733. fputs(temp_str, fh);
  734. }
  735. }
  736. }
  737. }
  738. static void
  739. write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
  740. {
  741. int i;
  742. const guint8 *pd;
  743. if (!fi->ds_tvb)
  744. return;
  745. if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
  746. fprintf(pdata->fh, "field length invalid!");
  747. return;
  748. }
  749. /* Find the data for this field. */
  750. pd = get_field_data(pdata->src_list, fi);
  751. if (pd) {
  752. /* Print a simple hex dump */
  753. for (i = 0 ; i < fi->length; i++) {
  754. fprintf(pdata->fh, "%02x", pd[i]);
  755. }
  756. }
  757. }
  758. gboolean
  759. print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
  760. {
  761. gboolean multiple_sources;
  762. GSList *src_le;
  763. tvbuff_t *tvb;
  764. const char *name;
  765. char *line;
  766. const guchar *cp;
  767. guint length;
  768. struct data_source *src;
  769. /*
  770. * Set "multiple_sources" iff this frame has more than one
  771. * data source; if it does, we need to print the name of
  772. * the data source before printing the data from the
  773. * data source.
  774. */
  775. multiple_sources = (edt->pi.data_src->next != NULL);
  776. for (src_le = edt->pi.data_src; src_le != NULL;
  777. src_le = src_le->next) {
  778. src = (struct data_source *)src_le->data;
  779. tvb = get_data_source_tvb(src);
  780. if (multiple_sources) {
  781. name = get_data_source_name(src);
  782. line = g_strdup_printf("%s:", name);
  783. print_line(stream, 0, line);
  784. g_free(line);
  785. }
  786. length = tvb_length(tvb);
  787. if (length == 0)
  788. return TRUE;
  789. cp = tvb_get_ptr(tvb, 0, length);
  790. if (!print_hex_data_buffer(stream, cp, length,
  791. (packet_char_enc)edt->pi.fd->flags.encoding))
  792. return FALSE;
  793. }
  794. return TRUE;
  795. }
  796. /*
  797. * This routine is based on a routine created by Dan Lasley
  798. * <DLASLEY@PROMUS.com>.
  799. *
  800. * It was modified for Wireshark by Gilbert Ramirez and others.
  801. */
  802. #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
  803. #define BYTES_PER_LINE 16 /* max byte values printed on a line */
  804. #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
  805. /* max number of characters hex dump takes -
  806. 2 digits plus trailing blank */
  807. #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
  808. /* number of characters those bytes take;
  809. 3 characters per byte of hex dump,
  810. 2 blanks separating hex from ASCII,
  811. 1 character per byte of ASCII dump */
  812. #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
  813. /* number of characters per line;
  814. offset, 2 blanks separating offset
  815. from data dump, data dump */
  816. static gboolean
  817. print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
  818. guint length, packet_char_enc encoding)
  819. {
  820. register unsigned int ad, i, j, k, l;
  821. guchar c;
  822. gchar line[MAX_LINE_LEN + 1];
  823. unsigned int use_digits;
  824. static gchar binhex[16] = {
  825. '0', '1', '2', '3', '4', '5', '6', '7',
  826. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  827. /*
  828. * How many of the leading digits of the offset will we supply?
  829. * We always supply at least 4 digits, but if the maximum offset
  830. * won't fit in 4 digits, we use as many digits as will be needed.
  831. */
  832. if (((length - 1) & 0xF0000000) != 0)
  833. use_digits = 8; /* need all 8 digits */
  834. else if (((length - 1) & 0x0F000000) != 0)
  835. use_digits = 7; /* need 7 digits */
  836. else if (((length - 1) & 0x00F00000) != 0)
  837. use_digits = 6; /* need 6 digits */
  838. else if (((length - 1) & 0x000F0000) != 0)
  839. use_digits = 5; /* need 5 digits */
  840. else
  841. use_digits = 4; /* we'll supply 4 digits */
  842. ad = 0;
  843. i = 0;
  844. j = 0;
  845. k = 0;
  846. while (i < length) {
  847. if ((i & 15) == 0) {
  848. /*
  849. * Start of a new line.
  850. */
  851. j = 0;
  852. l = use_digits;
  853. do {
  854. l--;
  855. c = (ad >> (l*4)) & 0xF;
  856. line[j++] = binhex[c];
  857. } while (l != 0);
  858. line[j++] = ' ';
  859. line[j++] = ' ';
  860. memset(line+j, ' ', DATA_DUMP_LEN);
  861. /*
  862. * Offset in line of ASCII dump.
  863. */
  864. k = j + HEX_DUMP_LEN + 2;
  865. }
  866. c = *cp++;
  867. line[j++] = binhex[c>>4];
  868. line[j++] = binhex[c&0xf];
  869. j++;
  870. if (encoding == PACKET_CHAR_ENC_CHAR_EBCDIC) {
  871. c = EBCDIC_to_ASCII1(c);
  872. }
  873. line[k++] = ((c >= ' ') && (c < 0x7f)) ? c : '.';
  874. i++;
  875. if (((i & 15) == 0) || (i == length)) {
  876. /*
  877. * We'll be starting a new line, or
  878. * we're finished printing this buffer;
  879. * dump out the line we've constructed,
  880. * and advance the offset.
  881. */
  882. line[k] = '\0';
  883. if (!print_line(stream, 0, line))
  884. return FALSE;
  885. ad += 16;
  886. }
  887. }
  888. return TRUE;
  889. }
  890. static
  891. void ps_clean_string(char *out, const char *in, int outbuf_size)
  892. {
  893. int rd, wr;
  894. char c;
  895. if (in == NULL) {
  896. out[0] = '\0';
  897. return;
  898. }
  899. for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
  900. c = in[rd];
  901. switch (c) {
  902. case '(':
  903. case ')':
  904. case '\\':
  905. out[wr] = '\\';
  906. out[++wr] = c;
  907. break;
  908. default:
  909. out[wr] = c;
  910. break;
  911. }
  912. if (c == 0) {
  913. break;
  914. }
  915. }
  916. }
  917. /* Some formats need stuff at the beginning of the output */
  918. gboolean
  919. print_preamble(print_stream_t *self, gchar *filename, const char *version_string)
  920. {
  921. return self->ops->print_preamble ? (self->ops->print_preamble)(self, filename, version_string) : TRUE;
  922. }
  923. gboolean
  924. print_line(print_stream_t *self, int indent, const char *line)
  925. {
  926. return (self->ops->print_line)(self, indent, line);
  927. }
  928. /* Insert bookmark */
  929. gboolean
  930. print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
  931. {
  932. return self->ops->print_bookmark ? (self->ops->print_bookmark)(self, name, title) : TRUE;
  933. }
  934. gboolean
  935. new_page(print_stream_t *self)
  936. {
  937. return self->ops->new_page ? (self->ops->new_page)(self) : TRUE;
  938. }
  939. /* Some formats need stuff at the end of the output */
  940. gboolean
  941. print_finale(print_stream_t *self)
  942. {
  943. return self->ops->print_finale ? (self->ops->print_finale)(self) : TRUE;
  944. }
  945. gboolean
  946. destroy_print_stream(print_stream_t *self)
  947. {
  948. return self->ops->destroy ? (self->ops->destroy)(self) : TRUE;
  949. }
  950. typedef struct {
  951. gboolean to_file;
  952. FILE *fh;
  953. } output_text;
  954. static gboolean
  955. print_line_text(print_stream_t *self, int indent, const char *line)
  956. {
  957. static char spaces[MAX_INDENT];
  958. size_t ret;
  959. output_text *output = (output_text *)self->data;
  960. unsigned int num_spaces;
  961. /* should be space, if NUL -> initialize */
  962. if (!spaces[0]) {
  963. int i;
  964. for (i = 0; i < MAX_INDENT; i++)
  965. spaces[i] = ' ';
  966. }
  967. /* Prepare the tabs for printing, depending on tree level */
  968. num_spaces = indent * 4;
  969. if (num_spaces > MAX_INDENT)
  970. num_spaces = MAX_INDENT;
  971. ret = fwrite(spaces, 1, num_spaces, output->fh);
  972. if (ret == num_spaces) {
  973. fputs(line, output->fh);
  974. putc('\n', output->fh);
  975. }
  976. return !ferror(output->fh);
  977. }
  978. static gboolean
  979. new_page_text(print_stream_t *self)
  980. {
  981. output_text *output = (output_text *)self->data;
  982. fputs("\f", output->fh);
  983. return !ferror(output->fh);
  984. }
  985. static gboolean
  986. destroy_text(print_stream_t *self)
  987. {
  988. output_text *output = (output_text *)self->data;
  989. gboolean ret;
  990. ret = close_print_dest(output->to_file, output->fh);
  991. g_free(output);
  992. g_free(self);
  993. return ret;
  994. }
  995. static const print_stream_ops_t print_text_ops = {
  996. NULL, /* preamble */
  997. print_line_text,
  998. NULL, /* bookmark */
  999. new_page_text,
  1000. NULL, /* finale */
  1001. destroy_text
  1002. };
  1003. static print_stream_t *
  1004. print_stream_text_alloc(gboolean to_file, FILE *fh)
  1005. {
  1006. print_stream_t *stream;
  1007. output_text *output;
  1008. output = (output_text *)g_malloc(sizeof *output);
  1009. output->to_file = to_file;
  1010. output->fh = fh;
  1011. stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
  1012. stream->ops = &print_text_ops;
  1013. stream->data = output;
  1014. return stream;
  1015. }
  1016. print_stream_t *
  1017. print_stream_text_new(gboolean to_file, const char *dest)
  1018. {
  1019. FILE *fh;
  1020. fh = open_print_dest(to_file, dest);
  1021. if (fh == NULL)
  1022. return NULL;
  1023. return print_stream_text_alloc(to_file, fh);
  1024. }
  1025. print_stream_t *
  1026. print_stream_text_stdio_new(FILE *fh)
  1027. {
  1028. return print_stream_text_alloc(TRUE, fh);
  1029. }
  1030. typedef struct {
  1031. gboolean to_file;
  1032. FILE *fh;
  1033. } output_ps;
  1034. static gboolean
  1035. print_preamble_ps(print_stream_t *self, gchar *filename, const char *version_string)
  1036. {
  1037. output_ps *output = (output_ps *)self->data;
  1038. char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
  1039. print_ps_preamble(output->fh);
  1040. fputs("%% the page title\n", output->fh);
  1041. ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
  1042. fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, version_string);
  1043. fputs("\n", output->fh);
  1044. return !ferror(output->fh);
  1045. }
  1046. static gboolean
  1047. print_line_ps(print_stream_t *self, int indent, const char *line)
  1048. {
  1049. output_ps *output = (output_ps *)self->data;
  1050. char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
  1051. ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
  1052. fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
  1053. return !ferror(output->fh);
  1054. }
  1055. static gboolean
  1056. print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
  1057. {
  1058. output_ps *output = (output_ps *)self->data;
  1059. char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
  1060. /*
  1061. * See the Adobe "pdfmark reference":
  1062. *
  1063. * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
  1064. *
  1065. * The pdfmark stuff tells code that turns PostScript into PDF
  1066. * things that it should do.
  1067. *
  1068. * The /OUT stuff creates a bookmark that goes to the
  1069. * destination with "name" as the name and "title" as the title.
  1070. *
  1071. * The "/DEST" creates the destination.
  1072. */
  1073. ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
  1074. fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
  1075. psbuffer);
  1076. fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
  1077. output->fh);
  1078. fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
  1079. output->fh);
  1080. fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
  1081. return !ferror(output->fh);
  1082. }
  1083. static gboolean
  1084. new_page_ps(print_stream_t *self)
  1085. {
  1086. output_ps *output = (output_ps *)self->data;
  1087. fputs("formfeed\n", output->fh);
  1088. return !ferror(output->fh);
  1089. }
  1090. static gboolean
  1091. print_finale_ps(print_stream_t *self)
  1092. {
  1093. output_ps *output = (output_ps *)self->data;
  1094. print_ps_finale(output->fh);
  1095. return !ferror(output->fh);
  1096. }
  1097. static gboolean
  1098. destroy_ps(print_stream_t *self)
  1099. {
  1100. output_ps *output = (output_ps *)self->data;
  1101. gboolean ret;
  1102. ret = close_print_dest(output->to_file, output->fh);
  1103. g_free(output);
  1104. g_free(self);
  1105. return ret;
  1106. }
  1107. static const print_stream_ops_t print_ps_ops = {
  1108. print_preamble_ps,
  1109. print_line_ps,
  1110. print_bookmark_ps,
  1111. new_page_ps,
  1112. print_finale_ps,
  1113. destroy_ps
  1114. };
  1115. static print_stream_t *
  1116. print_stream_ps_alloc(gboolean to_file, FILE *fh)
  1117. {
  1118. print_stream_t *stream;
  1119. output_ps *output;
  1120. output = (output_ps *)g_malloc(sizeof *output);
  1121. output->to_file = to_file;
  1122. output->fh = fh;
  1123. stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
  1124. stream->ops = &print_ps_ops;
  1125. stream->data = output;
  1126. return stream;
  1127. }
  1128. print_stream_t *
  1129. print_stream_ps_new(gboolean to_file, const char *dest)
  1130. {
  1131. FILE *fh;
  1132. fh = open_print_dest(to_file, dest);
  1133. if (fh == NULL)
  1134. return NULL;
  1135. return print_stream_ps_alloc(to_file, fh);
  1136. }
  1137. print_stream_t *
  1138. print_stream_ps_stdio_new(FILE *fh)
  1139. {
  1140. return print_stream_ps_alloc(TRUE, fh);
  1141. }
  1142. output_fields_t* output_fields_new(void)
  1143. {
  1144. output_fields_t* fields = g_new(output_fields_t, 1);
  1145. fields->print_header = FALSE;
  1146. fields->separator = '\t';
  1147. fields->occurrence = 'a';
  1148. fields->aggregator = ',';
  1149. fields->fields = NULL; /*Do lazy initialisation */
  1150. fields->field_indicies = NULL;
  1151. fields->field_values = NULL;
  1152. fields->quote ='\0';
  1153. fields->includes_col_fields = FALSE;
  1154. return fields;
  1155. }
  1156. gsize output_fields_num_fields(output_fields_t* fields)
  1157. {
  1158. g_assert(fields);
  1159. if (NULL == fields->fields) {
  1160. return 0;
  1161. } else {
  1162. return fields->fields->len;
  1163. }
  1164. }
  1165. void output_fields_free(output_fields_t* fields)
  1166. {
  1167. g_assert(fields);
  1168. if (NULL != fields->fields) {
  1169. gsize i;
  1170. if (NULL != fields->field_indicies) {
  1171. /* Keys are stored in fields->fields, values are
  1172. * integers.
  1173. */
  1174. g_hash_table_destroy(fields->field_indicies);
  1175. }
  1176. if (NULL != fields->field_values) {
  1177. g_free(fields->field_values);
  1178. }
  1179. for(i = 0; i < fields->fields->len; ++i) {
  1180. gchar* field = (gchar *)g_ptr_array_index(fields->fields,i);
  1181. g_free(field);
  1182. }
  1183. g_ptr_array_free(fields->fields, TRUE);
  1184. }
  1185. g_free(fields);
  1186. }
  1187. #define COLUMN_FIELD_FILTER "col."
  1188. void output_fields_add(output_fields_t *fields, const gchar *field)
  1189. {
  1190. gchar *field_copy;
  1191. g_assert(fields);
  1192. g_assert(field);
  1193. if (NULL == fields->fields) {
  1194. fields->fields = g_ptr_array_new();
  1195. }
  1196. field_copy = g_strdup(field);
  1197. g_ptr_array_add(fields->fields, field_copy);
  1198. /* See if we have a column as a field entry */
  1199. if (!strncmp(field, COLUMN_FIELD_FILTER, strlen(COLUMN_FIELD_FILTER)))
  1200. fields->includes_col_fields = TRUE;
  1201. }
  1202. gboolean output_fields_set_option(output_fields_t *info, gchar *option)
  1203. {
  1204. const gchar *option_name;
  1205. const gchar *option_value;
  1206. g_assert(info);
  1207. g_assert(option);
  1208. if ('\0' == *option) {
  1209. return FALSE; /* Is this guarded against by option parsing? */
  1210. }
  1211. option_name = strtok(option, "=");
  1212. if (!option_name) {
  1213. return FALSE;
  1214. }
  1215. option_value = option + strlen(option_name) + 1;
  1216. if (0 == strcmp(option_name, "header")) {
  1217. switch (NULL == option_value ? '\0' : *option_value) {
  1218. case 'n':
  1219. info->print_header = FALSE;
  1220. break;
  1221. case 'y':
  1222. info->print_header = TRUE;
  1223. break;
  1224. default:
  1225. return FALSE;
  1226. }
  1227. return TRUE;
  1228. }
  1229. if (0 == strcmp(option_name, "separator")) {
  1230. switch (NULL == option_value ? '\0' : *option_value) {
  1231. case '\0':
  1232. return FALSE;
  1233. case '/':
  1234. switch (*++option_value) {
  1235. case 't':
  1236. info->separator = '\t';
  1237. break;
  1238. case 's':
  1239. info->separator = ' ';
  1240. break;
  1241. default:
  1242. info->separator = '\\';
  1243. }
  1244. break;
  1245. default:
  1246. info->separator = *option_value;
  1247. break;
  1248. }
  1249. return TRUE;
  1250. }
  1251. if (0 == strcmp(option_name, "occurrence")) {
  1252. switch (NULL == option_value ? '\0' : *option_value) {
  1253. case 'f':
  1254. case 'l':
  1255. case 'a':
  1256. info->occurrence = *option_value;
  1257. break;
  1258. default:
  1259. return FALSE;
  1260. }
  1261. return TRUE;
  1262. }
  1263. if (0 == strcmp(option_name, "aggregator")) {
  1264. switch (NULL == option_value ? '\0' : *option_value) {
  1265. case '\0':
  1266. return FALSE;
  1267. case '/':
  1268. switch (*++option_value) {
  1269. case 's':
  1270. info->aggregator = ' ';
  1271. break;
  1272. default:
  1273. info->aggregator = '\\';
  1274. }
  1275. break;
  1276. default:
  1277. info->aggregator = *option_value;
  1278. break;
  1279. }
  1280. return TRUE;
  1281. }
  1282. if (0 == strcmp(option_name, "quote")) {
  1283. switch (NULL == option_value ? '\0' : *option_value) {
  1284. default: /* Fall through */
  1285. case '\0':
  1286. info->quote = '\0';
  1287. return FALSE;
  1288. case 'd':
  1289. info->quote = '"';
  1290. break;
  1291. case 's':
  1292. info->quote = '\'';
  1293. break;
  1294. case 'n':
  1295. info->quote = '\0';
  1296. break;
  1297. }
  1298. return TRUE;
  1299. }
  1300. return FALSE;
  1301. }
  1302. void output_fields_list_options(FILE *fh)
  1303. {
  1304. fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
  1305. fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
  1306. fputs("separator=/t|/s|<character> Set the separator to use;\n \"/t\" = tab, \"/s\" = space (def: /t: tab)\n", fh);
  1307. fputs("occurrence=f|l|a Select the occurrence of a field to use;\n \"f\" = first, \"l\" = last, \"a\" = all (def: a: all)\n", fh);
  1308. fputs("aggregator=,|/s|<character> Set the aggregator to use;\n \",\" = comma, \"/s\" = space (def: ,: comma)\n", fh);
  1309. fputs("quote=d|s|n Print either d: double-quotes, s: single quotes or \n n: no quotes around field values (def: n: none)\n", fh);
  1310. }
  1311. gboolean output_fields_has_cols(output_fields_t* fields)
  1312. {
  1313. g_assert(fields);
  1314. return fields->includes_col_fields;
  1315. }
  1316. void write_fields_preamble(output_fields_t* fields, FILE *fh)
  1317. {
  1318. gsize i;
  1319. g_assert(fields);
  1320. g_assert(fh);
  1321. g_assert(fields->fields);
  1322. if (!fields->print_header) {
  1323. return;
  1324. }
  1325. for(i = 0; i < fields->fields->len; ++i) {
  1326. const gchar* field = (const gchar *)g_ptr_array_index(fields->fields,i);
  1327. if (i != 0 ) {
  1328. fputc(fields->separator, fh);
  1329. }
  1330. fputs(field, fh);
  1331. }
  1332. fputc('\n', fh);
  1333. }
  1334. static void format_field_values(output_fields_t* fields, gpointer field_index, const gchar* value)
  1335. {
  1336. guint indx;
  1337. GPtrArray* fv_p;
  1338. if ((NULL == value) || ('\0' == *value))
  1339. return;
  1340. /* Unwrap change made to disambiguiate zero / null */
  1341. indx = GPOINTER_TO_UINT(field_index) - 1;
  1342. if (fields->field_values[indx] == NULL) {
  1343. fields->field_values[indx] = g_ptr_array_new();
  1344. }
  1345. /* Essentially: fieldvalues[indx] is a 'GPtrArray *' with each array entry */
  1346. /* pointing to a string which is (part of) the final output string. */
  1347. fv_p = fields->field_values[indx];
  1348. switch (fields->occurrence) {
  1349. case 'f':
  1350. /* print the value of only the first occurrence of the field */
  1351. if (g_ptr_array_len(fv_p) != 0)
  1352. return;
  1353. break;
  1354. case 'l':
  1355. /* print the value of only the last occurrence of the field */
  1356. g_ptr_array_set_size(fv_p, 0);
  1357. break;
  1358. case 'a':
  1359. /* print the value of all accurrences of the field */
  1360. /* If not the first, add the 'aggregator' */
  1361. if (g_ptr_array_len(fv_p) > 0) {
  1362. g_ptr_array_add(fv_p, (gpointer)ep_strdup_printf("%c", fields->aggregator));
  1363. }
  1364. break;
  1365. default:
  1366. g_assert_not_reached();
  1367. break;
  1368. }
  1369. g_ptr_array_add(fv_p, (gpointer)value);
  1370. }
  1371. static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
  1372. {
  1373. write_field_data_t *call_data;
  1374. field_info *fi;
  1375. gpointer field_index;
  1376. call_data = (write_field_data_t *)data;
  1377. fi = PNODE_FINFO(node);
  1378. /* dissection with an invisible proto tree? */
  1379. g_assert(fi);
  1380. field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
  1381. if (NULL != field_index) {
  1382. format_field_values(call_data->fields, field_index,
  1383. get_node_field_value(fi, call_data->edt) /* static or ep_alloc'd string */
  1384. );
  1385. }
  1386. /* Recurse here. */
  1387. if (node->first_child != NULL) {
  1388. proto_tree_children_foreach(node, proto_tree_get_node_field_values,
  1389. call_data);
  1390. }
  1391. }
  1392. void proto_tree_write_fields(output_fields_t *fields, epan_dissect_t *edt, column_info *cinfo, FILE *fh)
  1393. {
  1394. gsize i;
  1395. gint col;
  1396. gchar *col_name;
  1397. gpointer field_index;
  1398. write_field_data_t data;
  1399. g_assert(fields);
  1400. g_assert(fields->fields);
  1401. g_assert(edt);
  1402. g_assert(fh);
  1403. data.fields = fields;
  1404. data.edt = edt;
  1405. if (NULL == fields->field_indicies) {
  1406. /* Prepare a lookup table from string abbreviation for field to its index. */
  1407. fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
  1408. i = 0;
  1409. while (i < fields->fields->len) {
  1410. gchar *field = (gchar *)g_ptr_array_index(fields->fields, i);
  1411. /* Store field indicies +1 so that zero is not a valid value,
  1412. * and can be distinguished from NULL as a pointer.
  1413. */
  1414. ++i;
  1415. g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
  1416. }
  1417. }
  1418. /* Array buffer to store values for this packet */
  1419. /* Allocate an array for the 'GPtrarray *' the first time */
  1420. /* ths function is invoked for a file; */
  1421. /* Any and all 'GPtrArray *' are freed (after use) each */
  1422. /* time (each packet) this function is invoked for a flle. */
  1423. /* XXX: ToDo: use packet-scope'd memory & (if/when implemented) wmem ptr_array */
  1424. if (NULL == fields->field_values)
  1425. fields->field_values = g_new0(GPtrArray*, fields->fields->len); /* free'd in output_fields_free() */
  1426. proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
  1427. &data);
  1428. if (fields->includes_col_fields) {
  1429. for (col = 0; col < cinfo->num_cols; col++) {
  1430. /* Prepend COLUMN_FIELD_FILTER as the field name */
  1431. col_name = ep_strdup_printf("%s%s", COLUMN_FIELD_FILTER, cinfo->col_title[col]);
  1432. field_index = g_hash_table_lookup(fields->field_indicies, col_name);
  1433. if (NULL != field_index) {
  1434. format_field_values(fields, field_index, cinfo->col_data[col]);
  1435. }
  1436. }
  1437. }
  1438. for(i = 0; i < fields->fields->len; ++i) {
  1439. if (0 != i) {
  1440. fputc(fields->separator, fh);
  1441. }
  1442. if (NULL != fields->field_values[i]) {
  1443. GPtrArray *fv_p;
  1444. gsize j;
  1445. fv_p = fields->field_values[i];
  1446. if (fields->quote != '\0') {
  1447. fputc(fields->quote, fh);
  1448. }
  1449. /* Output the array of (partial) field values */
  1450. for (j = 0; j < g_ptr_array_len(fv_p); j++ ) {
  1451. fputs((gchar *)g_ptr_array_index(fv_p, j), fh);
  1452. }
  1453. if (fields->quote != '\0') {
  1454. fputc(fields->quote, fh);
  1455. }
  1456. g_ptr_array_free(fv_p, TRUE); /* get ready for the next packet */
  1457. fields->field_values[i] = NULL;
  1458. }
  1459. }
  1460. }
  1461. void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
  1462. {
  1463. /* Nothing to do */
  1464. }
  1465. /* Returns an ep_alloced string or a static constant*/
  1466. const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
  1467. {
  1468. if (fi->hfinfo->id == hf_text_only) {
  1469. /* Text label.
  1470. * Get the text */
  1471. if (fi->rep) {
  1472. return fi->rep->representation;
  1473. }
  1474. else {
  1475. return get_field_hex_value(edt->pi.data_src, fi);
  1476. }
  1477. }
  1478. else if (fi->hfinfo->id == proto_data) {
  1479. /* Uninterpreted data, i.e., the "Data" protocol, is
  1480. * printed as a field instead of a protocol. */
  1481. return get_field_hex_value(edt->pi.data_src, fi);
  1482. }
  1483. else {
  1484. /* Normal protocols and fields */
  1485. gchar *dfilter_string;
  1486. size_t chop_len;
  1487. switch (fi->hfinfo->type)
  1488. {
  1489. case FT_PROTOCOL:
  1490. /* Print out the full details for the protocol. */
  1491. if (fi->rep) {
  1492. return fi->rep->representation;
  1493. } else {
  1494. /* Just print out the protocol abbreviation */
  1495. return fi->hfinfo->abbrev;
  1496. }
  1497. case FT_NONE:
  1498. /* Return "1" so that the presence of a field of type
  1499. * FT_NONE can be checked when using -T fields */
  1500. return "1";
  1501. default:
  1502. /* XXX - this is a hack until we can just call
  1503. * fvalue_to_string_repr() for *all* FT_* types. */
  1504. dfilter_string = proto_construct_match_selected_string(fi,
  1505. edt);
  1506. if (dfilter_string != NULL) {
  1507. chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
  1508. /* XXX - Remove double-quotes. Again, once we
  1509. * can call fvalue_to_string_repr(), we can
  1510. * ask it not to produce the version for
  1511. * display-filters, and thus, no
  1512. * double-quotes. */
  1513. if (dfilter_string[strlen(dfilter_string)-1] == '"') {
  1514. dfilter_string[strlen(dfilter_string)-1] = '\0';
  1515. chop_len++;
  1516. }
  1517. return &(dfilter_string[chop_len]);
  1518. } else {
  1519. return get_field_hex_value(edt->pi.data_src, fi);
  1520. }
  1521. }
  1522. }
  1523. }
  1524. static const gchar*
  1525. get_field_hex_value(GSList *src_list, field_info *fi)
  1526. {
  1527. const guint8 *pd;
  1528. if (!fi->ds_tvb)
  1529. return NULL;
  1530. if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
  1531. return "field length invalid!";
  1532. }
  1533. /* Find the data for this field. */
  1534. pd = get_field_data(src_list, fi);
  1535. if (pd) {
  1536. int i;
  1537. gchar *buffer;
  1538. gchar *p;
  1539. int len;
  1540. const int chars_per_byte = 2;
  1541. len = chars_per_byte * fi->length;
  1542. buffer = ep_alloc_array(gchar, len + 1);
  1543. buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
  1544. p = buffer;
  1545. /* Print a simple hex dump */
  1546. for (i = 0 ; i < fi->length; i++) {
  1547. g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
  1548. p += chars_per_byte;
  1549. }
  1550. return buffer;
  1551. } else {
  1552. return NULL;
  1553. }
  1554. }