#include <string.h> and/or #include <stdio.h> not needed.
[obnox/wireshark/wip.git] / epan / dissectors / packet-text-media.c
1 /* packet-text-media.c
2  * Routines for text-based media dissection.
3  *
4  * NOTE - The media type is either found in pinfo->match_string
5  *        or in pinfo->private_data.
6  *
7  * (C) Olivier Biot, 2004.
8  *
9  * $Id$
10  *
11  * Refer to the AUTHORS file or the AUTHORS section in the man page
12  * for contacting the author(s) of this file.
13  *
14  * Wireshark - Network traffic analyzer
15  * By Gerald Combs <gerald@wireshark.org>
16  * Copyright 1998 Gerald Combs
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  */
32
33 /* Edit this file with 4-space tabs */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <ctype.h>
40
41 #include <glib.h>
42 #include <epan/packet.h>
43 #include <epan/strutil.h>
44
45
46 /*
47  * Media dissector for line-based text media like text/plain, message/http.
48  *
49  * TODO - character set and chunked transfer-coding
50  */
51
52 /* Filterable header fields */
53 static gint proto_text_lines = -1;
54
55 /* Subtrees */
56 static gint ett_text_lines = -1;
57
58 static void
59 dissect_text_lines(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
60 {
61         proto_tree      *subtree;
62         proto_item      *ti;
63         gint            offset = 0, next_offset;
64         gint            len;
65         const char      *data_name;
66
67         data_name = pinfo->match_string;
68         if (! (data_name && data_name[0])) {
69                 /*
70                  * No information from "match_string"
71                  */
72                 data_name = (char *)(pinfo->private_data);
73                 if (! (data_name && data_name[0])) {
74                         /*
75                          * No information from "private_data"
76                          */
77                         data_name = NULL;
78                 }
79         }
80
81         if (data_name && check_col(pinfo->cinfo, COL_INFO))
82                 col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "(%s)",
83                                 data_name);
84
85         if (tree) {
86                 ti = proto_tree_add_item(tree, proto_text_lines,
87                                 tvb, 0, -1, FALSE);
88                 if (data_name)
89                         proto_item_append_text(ti, ": %s", data_name);
90                 subtree = proto_item_add_subtree(ti, ett_text_lines);
91                 /* Read the media line by line */
92                 while (tvb_reported_length_remaining(tvb, offset) != 0) {
93                         /*
94                          * XXX - we need to be passed the parameters
95                          * of the content type via "pinfo->private_data",
96                          * so that we know the character set.  We'd
97                          * have to handle that character set, which
98                          * might be a multibyte character set such
99                          * as "iso-10646-ucs-2", or might require other
100                          * special processing.
101                          */
102                         len = tvb_find_line_end(tvb, offset,
103                                         tvb_ensure_length_remaining(tvb, offset),
104                                         &next_offset, FALSE);
105                         if (len == -1)
106                                 break;
107
108                         /* We use next_offset - offset instead of len in the
109                          * call to tvb_format_text() so it will include the
110                          * line terminator(s) (\r and/or \n) in the display.
111                          */
112                         proto_tree_add_text(subtree, tvb, offset, next_offset - offset,
113                                             "%s", tvb_format_text(tvb, offset,
114                                                                   next_offset - offset));
115                         offset = next_offset;
116                 }
117         }
118 }
119
120 void
121 proto_register_text_lines(void)
122 {
123         static gint *ett[] = {
124                 &ett_text_lines,
125         };
126
127         proto_register_subtree_array(ett, array_length(ett));
128
129         proto_text_lines = proto_register_protocol(
130                         "Line-based text data", /* Long name */
131                         "Line-based text data", /* Short name */
132                         "data-text-lines");             /* Filter name */
133         register_dissector("data-text-lines", dissect_text_lines, proto_text_lines);
134 }
135
136 void
137 proto_reg_handoff_text_lines(void)
138 {
139         dissector_handle_t text_lines_handle;
140
141         text_lines_handle = find_dissector("data-text-lines");
142
143         dissector_add_string("media_type", "text/plain", text_lines_handle); /* RFC 2046 */
144         dissector_add_string("media_type", "text/richtext", text_lines_handle);  /* RFC 1341 */
145         dissector_add_string("media_type", "text/enriched", text_lines_handle);  /* RFC 1896 */
146         /* W3C line-based textual media */
147         dissector_add_string("media_type", "text/html", text_lines_handle);
148         dissector_add_string("media_type", "text/xml-external-parsed-entity", text_lines_handle);
149         dissector_add_string("media_type", "text/css", text_lines_handle);
150         dissector_add_string("media_type", "application/xml-external-parsed-entity", text_lines_handle);
151         dissector_add_string("media_type", "text/javascript", text_lines_handle);
152         dissector_add_string("media_type", "application/x-javascript", text_lines_handle);
153         dissector_add_string("media_type", "application/x-www-form-urlencoded", text_lines_handle);
154         dissector_add_string("media_type", "application/x-ns-proxy-autoconfig", text_lines_handle);
155
156         dissector_add_string("media_type", "text/vnd.sun.j2me.app-descriptor", text_lines_handle);
157         dissector_add_string("media_type", "application/vnd.poc.refer-to", text_lines_handle);
158         dissector_add_string("media_type", "application/vnd.drm.message", text_lines_handle);
159
160         dissector_add_string("media_type", "application/json", text_lines_handle); /* RFC 4627 */
161
162         dissector_add_string("media_type", "application/x-wms-logplaystats", text_lines_handle);
163         dissector_add_string("media_type", "application/x-rtsp-udp-packetpair", text_lines_handle);
164 }