1 /* packet-jpeg.c
2  *
3  * Routines for RFC 2435 JPEG dissection
4  *
5  * Copyright 2006
6  * Erwin Rol <erwin@erwinrol.com>
7  * Copyright 2001,
8  * Francisco Javier Cabello Torres, <fjcabello@vtools.es>
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * SPDX-License-Identifier: GPL-2.0-or-later
15  */
16 
17 #define NEW_PROTO_TREE_API
18 
19 #include "config.h"
20 
21 #include <epan/packet.h>
22 
23 #include <epan/rtp_pt.h>
24 
25 #include "packet-ber.h"
26 
27 void proto_register_jpeg(void);
28 void proto_reg_handoff_jpeg(void);
29 
30 static dissector_handle_t jpeg_handle;
31 
32 static header_field_info *hfi_jpeg = NULL;
33 
34 static const range_string jpeg_ts_rvals [] = {
35     {0, 0,      "Progressively scanned"},
36     {1, 1,      "Odd field of interlaced signal"},
37     {2, 2,      "Even field of interlaced signal"},
38     {3, 3,      "Interlaced field to be line doubled"},
39     {3, 0xff,   "Unspecified"},
40     {0, 0,      NULL}
41 };
42 
43 static const range_string jpeg_type_rvals [] = {
44     {  0,   0,  "4:2:2 Video"},
45     {  1,   1,  "4:2:0 Video"},
46     {  2,   5,  "Reserved"}, /* Previously assigned by RFC 2035 */
47     {  6,  63,  "Unassigned"},
48     { 64,  64,  "4:2:0 Video, Restart Markers present"},
49     { 65,  65,  "4:2:0 Video, Restart Markers present"},
50     { 66,  69,  "Reserved"}, /* Since [2,5] are reserved */
51     { 70, 127,  "Unassigned, Restart Markers present"},
52     {128, 255,  "Dynamically assigned"},
53     {  0,   0,  NULL}
54 };
55 
56 #define JPEG_HFI_INIT HFI_INIT(proto_jpeg)
57 
58 /* JPEG header fields */
59 static header_field_info hfi_rtp_jpeg_main_hdr JPEG_HFI_INIT = {
60 	"Main Header",
61 	"jpeg.main_hdr",
62 	FT_NONE, BASE_NONE, NULL, 0,
63 	NULL, HFILL
64 };
65 
66 static header_field_info hfi_rtp_jpeg_main_hdr_ts JPEG_HFI_INIT = {
67 	"Type Specific",
68 	"jpeg.main_hdr.ts",
69 	FT_UINT8, BASE_DEC|BASE_RANGE_STRING, RVALS(jpeg_ts_rvals), 0,
70 	NULL, HFILL
71 };
72 
73 static header_field_info hfi_rtp_jpeg_main_hdr_offs JPEG_HFI_INIT = {
74 	"Fragment Offset",
75 	"jpeg.main_hdr.offset",
76 	FT_UINT24, BASE_DEC, NULL, 0,
77 	NULL, HFILL
78 };
79 
80 static header_field_info hfi_rtp_jpeg_main_hdr_type JPEG_HFI_INIT = {
81 	"Type",
82 	"jpeg.main_hdr.type",
83 	FT_UINT8, BASE_DEC|BASE_RANGE_STRING, RVALS(jpeg_type_rvals), 0,
84 	NULL, HFILL
85 };
86 
87 static header_field_info hfi_rtp_jpeg_main_hdr_q JPEG_HFI_INIT = {
88 	"Q",
89 	"jpeg.main_hdr.q",
90 	FT_UINT8, BASE_DEC, NULL, 0,
91 	NULL, HFILL
92 };
93 
94 static header_field_info hfi_rtp_jpeg_main_hdr_width JPEG_HFI_INIT = {
95 	"Width",
96 	"jpeg.main_hdr.width",
97 	FT_UINT8, BASE_DEC, NULL, 0,
98 	NULL, HFILL
99 };
100 
101 static header_field_info hfi_rtp_jpeg_main_hdr_height JPEG_HFI_INIT = {
102 	"Height",
103 	"jpeg.main_hdr.height",
104 	FT_UINT8, BASE_DEC, NULL, 0,
105 	NULL, HFILL
106 };
107 
108 static header_field_info hfi_rtp_jpeg_restart_hdr JPEG_HFI_INIT = {
109 	"Restart Marker Header",
110 	"jpeg.restart_hdr",
111 	FT_NONE, BASE_NONE, NULL, 0,
112 	NULL, HFILL
113 };
114 
115 static header_field_info hfi_rtp_jpeg_restart_hdr_interval JPEG_HFI_INIT = {
116 	"Restart Interval",
117 	"jpeg.restart_hdr.interval",
118 	FT_UINT16, BASE_DEC, NULL, 0,
119 	NULL, HFILL
120 };
121 
122 static header_field_info hfi_rtp_jpeg_restart_hdr_f JPEG_HFI_INIT = {
123 	"F",
124 	"jpeg.restart_hdr.f",
125 	FT_UINT16, BASE_DEC, NULL, 0x8000,
126 	NULL, HFILL
127 };
128 
129 static header_field_info hfi_rtp_jpeg_restart_hdr_l JPEG_HFI_INIT = {
130 	"L",
131 	"jpeg.restart_hdr.l",
132 	FT_UINT16, BASE_DEC, NULL, 0x4000,
133 	NULL, HFILL
134 };
135 
136 static header_field_info hfi_rtp_jpeg_restart_hdr_count JPEG_HFI_INIT = {
137 	"Restart Count",
138 	"jpeg.restart_hdr.count",
139 	FT_UINT16, BASE_DEC, NULL, 0x3FFF,
140 	NULL, HFILL
141 };
142 
143 static header_field_info hfi_rtp_jpeg_qtable_hdr JPEG_HFI_INIT = {
144 	"Quantization Table Header",
145 	"jpeg.qtable_hdr",
146 	FT_NONE, BASE_NONE, NULL, 0,
147 	NULL, HFILL
148 };
149 
150 static header_field_info hfi_rtp_jpeg_qtable_hdr_mbz JPEG_HFI_INIT = {
151 	"MBZ",
152 	"jpeg.qtable_hdr.mbz",
153 	FT_UINT8, BASE_DEC, NULL, 0,
154 	NULL, HFILL
155 };
156 
157 static header_field_info hfi_rtp_jpeg_qtable_hdr_prec JPEG_HFI_INIT = {
158 	"Precision",
159 	"jpeg.qtable_hdr.precision",
160 	FT_UINT8, BASE_DEC, NULL, 0,
161 	NULL, HFILL
162 };
163 
164 static header_field_info hfi_rtp_jpeg_qtable_hdr_length JPEG_HFI_INIT = {
165 	"Length",
166 	"jpeg.qtable_hdr.length",
167 	FT_UINT16, BASE_DEC, NULL, 0,
168 	NULL, HFILL
169 };
170 
171 static header_field_info hfi_rtp_jpeg_qtable_hdr_data JPEG_HFI_INIT = {
172 	"Quantization Table Data",
173 	"jpeg.qtable_hdr.data",
174 	FT_BYTES, BASE_NONE, NULL, 0,
175 	NULL, HFILL
176 };
177 
178 
179 static header_field_info hfi_rtp_jpeg_payload JPEG_HFI_INIT = {
180 	"Payload",
181 	"jpeg.payload",
182 	FT_BYTES, BASE_NONE, NULL, 0,
183 	NULL, HFILL
184 };
185 
186 
187 /* JPEG fields defining a sub tree */
188 static gint ett_jpeg = -1;
189 
190 static int
dissect_jpeg(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)191 dissect_jpeg( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_ )
192 {
193 	proto_item *ti = NULL;
194 	proto_tree *jpeg_tree = NULL;
195 	proto_tree *main_hdr_tree = NULL;
196 	proto_tree *restart_hdr_tree = NULL;
197 	proto_tree *qtable_hdr_tree = NULL;
198 	guint32 fragment_offset = 0;
199 	guint16 len = 0;
200 	guint8 type = 0;
201 	guint8 q = 0;
202 	gint h = 0;
203 	gint w = 0;
204 
205 	unsigned int offset       = 0;
206 
207 	col_set_str(pinfo->cinfo, COL_PROTOCOL, "JPEG");
208 
209 	col_set_str(pinfo->cinfo, COL_INFO, "JPEG message");
210 
211 	if ( tree ) {
212 		ti = proto_tree_add_item( tree, hfi_jpeg, tvb, offset, -1, ENC_NA );
213 		jpeg_tree = proto_item_add_subtree( ti, ett_jpeg );
214 
215 		ti = proto_tree_add_item(jpeg_tree, &hfi_rtp_jpeg_main_hdr, tvb, offset, 8, ENC_NA);
216 		main_hdr_tree = proto_item_add_subtree(ti, ett_jpeg);
217 
218 		proto_tree_add_item(main_hdr_tree, &hfi_rtp_jpeg_main_hdr_ts, tvb, offset, 1, ENC_BIG_ENDIAN);
219 		offset += 1;
220 		proto_tree_add_item(main_hdr_tree, &hfi_rtp_jpeg_main_hdr_offs, tvb, offset, 3, ENC_BIG_ENDIAN);
221 		fragment_offset = tvb_get_ntoh24(tvb, offset);
222 		offset += 3;
223 		proto_tree_add_item(main_hdr_tree, &hfi_rtp_jpeg_main_hdr_type, tvb, offset, 1, ENC_BIG_ENDIAN);
224 		type = tvb_get_guint8(tvb, offset);
225 		offset += 1;
226 		proto_tree_add_item(main_hdr_tree, &hfi_rtp_jpeg_main_hdr_q, tvb, offset, 1, ENC_BIG_ENDIAN);
227 		q = tvb_get_guint8(tvb, offset);
228 		offset += 1;
229 		w = tvb_get_guint8(tvb, offset) * 8;
230 		proto_tree_add_uint(main_hdr_tree, &hfi_rtp_jpeg_main_hdr_width, tvb, offset, 1, w);
231 		offset += 1;
232 		h = tvb_get_guint8(tvb, offset) * 8;
233 		proto_tree_add_uint(main_hdr_tree, &hfi_rtp_jpeg_main_hdr_height, tvb, offset, 1, h);
234 		offset += 1;
235 
236 		if (type >= 64 && type <= 127) {
237 			ti = proto_tree_add_item(jpeg_tree, &hfi_rtp_jpeg_restart_hdr, tvb, offset, 4, ENC_NA);
238 			restart_hdr_tree = proto_item_add_subtree(ti, ett_jpeg);
239 			proto_tree_add_item(restart_hdr_tree, &hfi_rtp_jpeg_restart_hdr_interval, tvb, offset, 2, ENC_BIG_ENDIAN);
240 			offset += 2;
241 			proto_tree_add_item(restart_hdr_tree, &hfi_rtp_jpeg_restart_hdr_f, tvb, offset, 2, ENC_BIG_ENDIAN);
242 			proto_tree_add_item(restart_hdr_tree, &hfi_rtp_jpeg_restart_hdr_l, tvb, offset, 2, ENC_BIG_ENDIAN);
243 			proto_tree_add_item(restart_hdr_tree, &hfi_rtp_jpeg_restart_hdr_count, tvb, offset, 2, ENC_BIG_ENDIAN);
244 			offset += 2;
245 		}
246 
247 		if (q >= 128 && fragment_offset == 0) {
248 			ti = proto_tree_add_item(jpeg_tree, &hfi_rtp_jpeg_qtable_hdr, tvb, offset, -1, ENC_NA);
249 			qtable_hdr_tree = proto_item_add_subtree(ti, ett_jpeg);
250 			proto_tree_add_item(qtable_hdr_tree, &hfi_rtp_jpeg_qtable_hdr_mbz, tvb, offset, 1, ENC_BIG_ENDIAN);
251 			offset += 1;
252 			proto_tree_add_item(qtable_hdr_tree, &hfi_rtp_jpeg_qtable_hdr_prec, tvb, offset, 1, ENC_BIG_ENDIAN);
253 			offset += 1;
254 			proto_tree_add_item(qtable_hdr_tree, &hfi_rtp_jpeg_qtable_hdr_length, tvb, offset, 2, ENC_BIG_ENDIAN);
255 			len = tvb_get_ntohs(tvb, offset);
256 			offset += 2;
257 			if (len > 0) {
258 				proto_tree_add_item(qtable_hdr_tree, &hfi_rtp_jpeg_qtable_hdr_data, tvb, offset, len, ENC_NA);
259 				offset += len;
260 			}
261 			proto_item_set_len(ti, len + 4);
262 		}
263 
264 		/* The rest of the packet is the JPEG data */
265 		proto_tree_add_item( jpeg_tree, &hfi_rtp_jpeg_payload, tvb, offset, -1, ENC_NA );
266 	}
267 	return tvb_captured_length(tvb);
268 }
269 
270 void
proto_register_jpeg(void)271 proto_register_jpeg(void)
272 {
273 #ifndef HAVE_HFI_SECTION_INIT
274 	static header_field_info *hfi[] =
275 	{
276 		&hfi_rtp_jpeg_main_hdr,
277 		&hfi_rtp_jpeg_main_hdr_ts,
278 		&hfi_rtp_jpeg_main_hdr_offs,
279 		&hfi_rtp_jpeg_main_hdr_type,
280 		&hfi_rtp_jpeg_main_hdr_q,
281 		&hfi_rtp_jpeg_main_hdr_width,
282 		&hfi_rtp_jpeg_main_hdr_height,
283 		&hfi_rtp_jpeg_restart_hdr,
284 		&hfi_rtp_jpeg_restart_hdr_interval,
285 		&hfi_rtp_jpeg_restart_hdr_f,
286 		&hfi_rtp_jpeg_restart_hdr_l,
287 		&hfi_rtp_jpeg_restart_hdr_count,
288 		&hfi_rtp_jpeg_qtable_hdr,
289 		&hfi_rtp_jpeg_qtable_hdr_mbz,
290 		&hfi_rtp_jpeg_qtable_hdr_prec,
291 		&hfi_rtp_jpeg_qtable_hdr_length,
292 		&hfi_rtp_jpeg_qtable_hdr_data,
293 		&hfi_rtp_jpeg_payload,
294 	};
295 #endif
296 
297 	static gint *ett[] =
298 	{
299 		&ett_jpeg,
300 	};
301 
302 	int proto_jpeg;
303 
304 	proto_jpeg = proto_register_protocol("RFC 2435 JPEG","JPEG","jpeg");
305 	hfi_jpeg = proto_registrar_get_nth(proto_jpeg);
306 
307 	proto_register_fields(proto_jpeg, hfi, array_length(hfi));
308 	proto_register_subtree_array(ett, array_length(ett));
309 
310 	jpeg_handle = register_dissector("jpeg", dissect_jpeg, proto_jpeg);
311 
312 	/* RFC 2798 */
313 	register_ber_oid_dissector_handle("0.9.2342.19200300.100.1.60", jpeg_handle, proto_jpeg, "jpegPhoto");
314 }
315 
316 void
proto_reg_handoff_jpeg(void)317 proto_reg_handoff_jpeg(void)
318 {
319 	dissector_add_uint("rtp.pt", PT_JPEG, jpeg_handle);
320 }
321 
322 /*
323  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
324  *
325  * Local variables:
326  * c-basic-offset: 8
327  * tab-width: 8
328  * indent-tabs-mode: t
329  * End:
330  *
331  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
332  * :indentSize=8:tabSize=8:noTabs=false:
333  */
334