MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-image-jfif.c
blob3f3823f95b0cd540dafa751372ae1a1ca214aed7
1 /* packet-image-jfif.c
3 * Routines for JFIF image/jpeg media dissection
4 * Copyright 2004, Olivier Biot.
6 * $Id$
8 * Refer to the AUTHORS file or the AUTHORS section in the man page
9 * for contacting the author(s) of this file.
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * JFIF media decoding functionality provided by Olivier Biot.
17 * The JFIF specifications are found at several locations, such as:
18 * http://www.jpeg.org/public/jfif.pdf
19 * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
21 * The Exif specifications are found at several locations, such as:
22 * http://www.exif.org/
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * as published by the Free Software Foundation; either version 2
27 * of the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39 #include "config.h"
41 #include <string.h>
43 #include <glib.h>
45 #include <epan/packet.h>
47 /* General-purpose debug logger.
48 * Requires double parentheses because of variable arguments of printf().
50 * Enable debug logging for JFIF by defining AM_CFLAGS
51 * so that it contains "-DDEBUG_image_jfif" or "-DDEBUG_image"
53 #if (defined(DEBUG_image_jfif) || defined(DEBUG_image))
54 #define DebugLog(x) \
55 g_print("%s:%u: ", __FILE__, __LINE__); \
56 g_print x
57 #else
58 #define DebugLog(x) ;
59 #endif
61 #define IMG_JFIF "image-jfif"
63 /************************** Variable declarations **************************/
65 #define MARKER_TEM 0xFF01
67 /* 0xFF02 -- 0xFFBF are reserved */
69 #define MARKER_SOF0 0xFFC0
70 #define MARKER_SOF1 0xFFC1
71 #define MARKER_SOF2 0xFFC2
72 #define MARKER_SOF3 0xFFC3
74 #define MARKER_DHT 0xFFC4
76 #define MARKER_SOF5 0xFFC5
77 #define MARKER_SOF6 0xFFC6
78 #define MARKER_SOF7 0xFFC7
79 #define MARKER_SOF8 0xFFC8
80 #define MARKER_SOF9 0xFFC9
81 #define MARKER_SOF10 0xFFCA
82 #define MARKER_SOF11 0xFFCB
84 #define MARKER_DAC 0xFFCC
86 #define MARKER_SOF13 0xFFCD
87 #define MARKER_SOF14 0xFFCE
88 #define MARKER_SOF15 0xFFCF
90 #define MARKER_RST0 0xFFD0
91 #define MARKER_RST1 0xFFD1
92 #define MARKER_RST2 0xFFD2
93 #define MARKER_RST3 0xFFD3
94 #define MARKER_RST4 0xFFD4
95 #define MARKER_RST5 0xFFD5
96 #define MARKER_RST6 0xFFD6
97 #define MARKER_RST7 0xFFD7
99 #define MARKER_FFDB 0xFFDB
100 #define MARKER_SOI 0xFFD8
101 #define MARKER_EOI 0xFFD9
102 #define MARKER_SOS 0xFFDA
103 #define MARKER_DQT 0xFFDB
104 #define MARKER_DNL 0xFFDC
105 #define MARKER_DRI 0xFFDD
106 #define MARKER_DHP 0xFFDE
107 #define MARKER_EXP 0xFFDF
109 #define MARKER_APP0 0xFFE0
110 #define MARKER_APP1 0xFFE1
111 #define MARKER_APP2 0xFFE2
112 #define MARKER_APP3 0xFFE3
113 #define MARKER_APP4 0xFFE4
114 #define MARKER_APP5 0xFFE5
115 #define MARKER_APP6 0xFFE6
116 #define MARKER_APP7 0xFFE7
117 #define MARKER_APP8 0xFFE8
118 #define MARKER_APP9 0xFFE9
119 #define MARKER_APP10 0xFFEA
120 #define MARKER_APP11 0xFFEB
121 #define MARKER_APP12 0xFFEC
122 #define MARKER_APP13 0xFFED
123 #define MARKER_APP14 0xFFEE
124 #define MARKER_APP15 0xFFEF
126 #define MARKER_JPG0 0xFFF0
127 #define MARKER_JPG1 0xFFF1
128 #define MARKER_JPG2 0xFFF2
129 #define MARKER_JPG3 0xFFF3
130 #define MARKER_JPG4 0xFFF4
131 #define MARKER_JPG5 0xFFF5
132 #define MARKER_JPG6 0xFFF6
133 #define MARKER_JPG7 0xFFF7
134 #define MARKER_JPG8 0xFFF8
135 #define MARKER_JPG9 0xFFF9
136 #define MARKER_JPG10 0xFFFA
137 #define MARKER_JPG11 0xFFFB
138 #define MARKER_JPG12 0xFFFC
139 #define MARKER_JPG13 0xFFFD
141 #define MARKER_COM 0xFFFE
143 #define marker_has_length(marker) ( ! ( \
144 ((marker) == MARKER_TEM) \
145 || ((marker) == MARKER_SOI) \
146 || ((marker) == MARKER_EOI) \
147 || ( ((marker) >= MARKER_RST0) && ((marker) <= MARKER_RST7) ) \
151 static const value_string vals_marker[] = {
152 { MARKER_TEM, "Reserved - For temporary private use in arithmetic coding" },
153 { MARKER_SOF0, "Start of Frame (non-differential, Huffman coding) - Baseline DCT" },
154 { MARKER_SOF1, "Start of Frame (non-differential, Huffman coding) - Extended sequential DCT" },
155 { MARKER_SOF2, "Start of Frame (non-differential, Huffman coding) - Progressive DCT" },
156 { MARKER_SOF3, "Start of Frame (non-differential, Huffman coding) - Lossless (sequential)" },
157 { MARKER_DHT, "Define Huffman table(s)" },
158 { MARKER_SOF5, "Start of Frame (differential, Huffman coding) - Differential sequential DCT" },
159 { MARKER_SOF6, "Start of Frame (differential, Huffman coding) - Differential progressive DCT" },
160 { MARKER_SOF7, "Start of Frame (differential, Huffman coding) - Differential lossless (sequential)" },
161 { MARKER_SOF8, "Start of Frame (non-differential, arithmetic coding) - Reserved for JPEG extensions" },
162 { MARKER_SOF9, "Start of Frame (non-differential, arithmetic coding) - Extended sequential DCT" },
163 { MARKER_SOF10, "Start of Frame (non-differential, arithmetic coding) - Progressive DCT" },
164 { MARKER_SOF11, "Start of Frame (non-differential, arithmetic coding) - Lossless (sequential)" },
165 { MARKER_DAC, "Define arithmetic coding conditioning(s)" },
166 { MARKER_SOF13, "Start of Frame (differential, arithmetic coding) - Differential sequential DCT" },
167 { MARKER_SOF14, "Start of Frame (differential, arithmetic coding) - Differential progressive DCT" },
168 { MARKER_SOF15, "Start of Frame (differential, arithmetic coding) - Differential lossless (sequential)" },
169 { MARKER_RST0, "Restart interval termination - Restart with modulo 8 count 0" },
170 { MARKER_RST1, "Restart interval termination - Restart with modulo 8 count 1" },
171 { MARKER_RST2, "Restart interval termination - Restart with modulo 8 count 2" },
172 { MARKER_RST3, "Restart interval termination - Restart with modulo 8 count 3" },
173 { MARKER_RST4, "Restart interval termination - Restart with modulo 8 count 4" },
174 { MARKER_RST5, "Restart interval termination - Restart with modulo 8 count 5" },
175 { MARKER_RST6, "Restart interval termination - Restart with modulo 8 count 6" },
176 { MARKER_RST7, "Restart interval termination - Restart with modulo 8 count 7" },
177 { MARKER_SOI, "Start of Image" },
178 { MARKER_EOI, "End of Image" },
179 { MARKER_SOS, "Start of Scan" },
180 { MARKER_DQT, "Define quantization table(s)" },
181 { MARKER_DNL, "Define number of lines" },
182 { MARKER_DRI, "Define restart interval" },
183 { MARKER_DHP, "Define hierarchical progression" },
184 { MARKER_EXP, "Expand reference component(s)" },
185 { MARKER_APP0, "Reserved for application segments - 0" },
186 { MARKER_APP1, "Reserved for application segments - 1" },
187 { MARKER_APP2, "Reserved for application segments - 2" },
188 { MARKER_APP3, "Reserved for application segments - 3" },
189 { MARKER_APP4, "Reserved for application segments - 4" },
190 { MARKER_APP5, "Reserved for application segments - 5" },
191 { MARKER_APP6, "Reserved for application segments - 6" },
192 { MARKER_APP7, "Reserved for application segments - 7" },
193 { MARKER_APP8, "Reserved for application segments - 8" },
194 { MARKER_APP9, "Reserved for application segments - 9" },
195 { MARKER_APP10, "Reserved for application segments - 10" },
196 { MARKER_APP11, "Reserved for application segments - 11" },
197 { MARKER_APP12, "Reserved for application segments - 12" },
198 { MARKER_APP13, "Reserved for application segments - 13" },
199 { MARKER_APP14, "Reserved for application segments - 14" },
200 { MARKER_APP15, "Reserved for application segments - 15" },
201 { MARKER_JPG0, "Reserved for JPEG extensions - 0" },
202 { MARKER_JPG1, "Reserved for JPEG extensions - 1" },
203 { MARKER_JPG2, "Reserved for JPEG extensions - 2" },
204 { MARKER_JPG3, "Reserved for JPEG extensions - 3" },
205 { MARKER_JPG4, "Reserved for JPEG extensions - 4" },
206 { MARKER_JPG5, "Reserved for JPEG extensions - 5" },
207 { MARKER_JPG6, "Reserved for JPEG extensions - 6" },
208 { MARKER_JPG7, "Reserved for JPEG extensions - 7" },
209 { MARKER_JPG8, "Reserved for JPEG extensions - 8" },
210 { MARKER_JPG9, "Reserved for JPEG extensions - 9" },
211 { MARKER_JPG10, "Reserved for JPEG extensions - 10" },
212 { MARKER_JPG11, "Reserved for JPEG extensions - 11" },
213 { MARKER_JPG12, "Reserved for JPEG extensions - 12" },
214 { MARKER_JPG13, "Reserved for JPEG extensions - 13" },
215 { MARKER_COM, "Comment" },
216 { 0x00, NULL }
219 static const value_string vals_units[] = {
220 { 0, "No units; Xdensity and Ydensity specify the pixel aspect ratio" },
221 { 1, "Dots per inch" },
222 { 2, "Dots per centimeter" },
223 { 0x00, NULL }
226 static const value_string vals_extension_code[] = {
227 { 0x10, "Thumbnail encoded using JPEG" },
228 { 0x11, "Thumbnail encoded using 1 byte (8 bits) per pixel" },
229 { 0x13, "Thumbnail encoded using 3 bytes (24 bits) per pixel" },
230 { 0x00, NULL }
233 static const value_string vals_exif_tags[] = {
235 * Tags related to image data structure:
237 { 0x0100, "ImageWidth" },
238 { 0x0101, "ImageLength" },
239 { 0x0102, "BitsPerSample" },
240 { 0x0103, "Compression" },
241 { 0x0106, "PhotometricInterpretation" },
242 { 0x0112, "Orientation" },
243 { 0x0115, "SamplesPerPixel" },
244 { 0x011C, "PlanarConfiguration" },
245 { 0x0212, "YCbCrSubSampling" },
246 { 0x0213, "YCbCrPositioning" },
247 { 0x011A, "XResolution" },
248 { 0x011B, "YResolution" },
249 { 0x0128, "ResolutionUnit" },
251 * Tags relating to recording offset:
253 { 0x0111, "StripOffsets" },
254 { 0x0116, "RowsPerStrip" },
255 { 0x0117, "StripByteCounts" },
256 { 0x0201, "JPEGInterchangeFormat" },
257 { 0x0202, "JPEGInterchangeFormatLength" },
259 * Tags relating to image data characteristics:
261 { 0x012D, "TransferFunction" },
262 { 0x013E, "WhitePoint" },
263 { 0x013F, "PrimaryChromaticities" },
264 { 0x0211, "YCbCrCoefficients" },
265 { 0x0214, "ReferenceBlackWhite" },
267 * Other tags:
269 { 0x0132, "DateTime" },
270 { 0x010E, "ImageDescription" },
271 { 0x010F, "Make" },
272 { 0x0110, "Model" },
273 { 0x0131, "Software" },
274 { 0x013B, "Artist" },
275 { 0x8296, "Copyright" },
277 * Exif-specific IFD:
279 { 0x8769, "Exif IFD Pointer"},
280 { 0x8825, "GPS IFD Pointer"},
281 { 0xA005, "Interoperability IFD Pointer"},
283 { 0x0000, NULL }
286 static const value_string vals_exif_types[] = {
287 { 0x0001, "BYTE" },
288 { 0x0002, "ASCII" },
289 { 0x0003, "SHORT" },
290 { 0x0004, "LONG" },
291 { 0x0005, "RATIONAL" },
292 /* 0x0006 */
293 { 0x0007, "UNDEFINED" },
294 /* 0x0008 */
295 { 0x0009, "SLONG" },
296 { 0x000A, "SRATIONAL" },
298 { 0x0000, NULL }
301 /* Initialize the protocol and registered fields */
302 static int proto_jfif = -1;
304 /* Marker */
305 static gint hf_marker = -1;
306 /* Marker segment */
307 static gint hf_marker_segment = -1;
308 static gint hf_len = -1;
309 /* MARKER_APP0 */
310 static gint hf_identifier = -1;
311 /* MARKER_APP0 - JFIF */
312 static gint hf_version = -1;
313 static gint hf_version_major = -1;
314 static gint hf_version_minor = -1;
315 static gint hf_units = -1;
316 static gint hf_xdensity = -1;
317 static gint hf_ydensity = -1;
318 static gint hf_xthumbnail = -1;
319 static gint hf_ythumbnail = -1;
320 static gint hf_rgb = -1;
321 /* MARKER_APP0 - JFXX */
322 static gint hf_extension_code = -1;
323 /* start of Frame */
324 static gint hf_sof_header = -1;
325 static gint hf_sof_precision = -1;
326 static gint hf_sof_lines = -1;
327 static gint hf_sof_samples_per_line = -1;
328 static gint hf_sof_nf = -1;
329 static gint hf_sof_c_i = -1;
330 static gint hf_sof_h_i = -1;
331 static gint hf_sof_v_i = -1;
332 static gint hf_sof_tq_i = -1;
334 /* Start of Scan */
335 static gint hf_sos_header = -1;
336 static gint hf_sos_ns = -1;
337 static gint hf_sos_cs_j = -1;
338 static gint hf_sos_td_j = -1;
339 static gint hf_sos_ta_j = -1;
340 static gint hf_sos_ss = -1;
341 static gint hf_sos_se = -1;
342 static gint hf_sos_ah = -1;
343 static gint hf_sos_al = -1;
345 /* Initialize the subtree pointers */
346 static gint ett_jfif = -1;
347 static gint ett_marker_segment = -1;
348 static gint ett_details = -1;
351 /****************** JFIF protocol dissection functions ******************/
353 #define ErrorInvalidJFIF "This is not a valid JFIF (JPEG) object"
357 * Process a marker segment (with length).
359 static void
360 process_marker_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
361 guint16 marker, const char *marker_name)
363 proto_item *ti = NULL;
364 proto_tree *subtree = NULL;
366 if (! tree)
367 return;
369 ti = proto_tree_add_item(tree, hf_marker_segment,
370 tvb, 0, -1, ENC_NA);
371 subtree = proto_item_add_subtree(ti, ett_marker_segment);
373 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
374 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
376 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
378 proto_tree_add_text(subtree, tvb, 4, -1,
379 "Remaining segment data (%u bytes)", len - 2);
383 * Process a Start of Frame header (with length).
385 static void
386 process_sof_header(proto_tree *tree, tvbuff_t *tvb, guint32 len _U_,
387 guint16 marker, const char *marker_name)
389 proto_item *ti = NULL;
390 proto_tree *subtree = NULL;
392 if (! tree)
393 return;
395 ti = proto_tree_add_item(tree, hf_sof_header,
396 tvb, 0, -1, ENC_NA);
397 subtree = proto_item_add_subtree(ti, ett_marker_segment);
399 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
400 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
402 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
404 proto_tree_add_item(subtree, hf_sof_precision, tvb, 4, 1, ENC_BIG_ENDIAN);
406 proto_tree_add_item(subtree, hf_sof_lines, tvb, 5, 2, ENC_BIG_ENDIAN);
408 proto_tree_add_item(subtree, hf_sof_samples_per_line, tvb, 7, 2, ENC_BIG_ENDIAN);
410 proto_tree_add_item(subtree, hf_sof_nf, tvb, 9, 1, ENC_BIG_ENDIAN);
412 guint8 count = tvb_get_guint8(tvb, 9);
413 guint32 offset = 10;
414 while (count > 0) {
415 proto_tree_add_item(subtree, hf_sof_c_i, tvb, offset++, 1, ENC_BIG_ENDIAN);
416 proto_tree_add_item(subtree, hf_sof_h_i, tvb, offset, 1, ENC_BIG_ENDIAN);
417 proto_tree_add_item(subtree, hf_sof_v_i, tvb, offset++, 1, ENC_BIG_ENDIAN);
418 proto_tree_add_item(subtree, hf_sof_tq_i, tvb, offset++, 1, ENC_BIG_ENDIAN);
419 count--;
425 * Process a Start of Segment header (with length).
427 static void
428 process_sos_header(proto_tree *tree, tvbuff_t *tvb, guint32 len _U_,
429 guint16 marker, const char *marker_name)
431 proto_item *ti = NULL;
432 proto_tree *subtree = NULL;
433 guint32 offset;
435 if (! tree)
436 return;
438 ti = proto_tree_add_item(tree, hf_sos_header,
439 tvb, 0, -1, ENC_NA);
440 subtree = proto_item_add_subtree(ti, ett_marker_segment);
442 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
443 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
445 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
447 proto_tree_add_item(subtree, hf_sos_ns, tvb, 4, 1, ENC_BIG_ENDIAN);
449 guint8 count = tvb_get_guint8(tvb, 4);
450 offset = 5;
451 while (count > 0) {
452 proto_tree_add_item(subtree, hf_sos_cs_j, tvb, offset++, 1, ENC_BIG_ENDIAN);
453 proto_tree_add_item(subtree, hf_sos_td_j, tvb, offset, 1, ENC_BIG_ENDIAN);
454 proto_tree_add_item(subtree, hf_sos_ta_j, tvb, offset++, 1, ENC_BIG_ENDIAN);
455 count--;
459 proto_tree_add_item(subtree, hf_sos_ss, tvb, offset++, 1, ENC_BIG_ENDIAN);
460 proto_tree_add_item(subtree, hf_sos_se, tvb, offset++, 1, ENC_BIG_ENDIAN);
462 proto_tree_add_item(subtree, hf_sos_ah, tvb, offset, 1, ENC_BIG_ENDIAN);
463 proto_tree_add_item(subtree, hf_sos_al, tvb, offset, 1, ENC_BIG_ENDIAN);
464 /* offset ++ */;
467 /* Process an APP0 block.
469 * XXX - This code only works on US-ASCII systems!!!
471 static int
472 process_app0_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
473 guint16 marker, const char *marker_name)
475 proto_item *ti = NULL;
476 proto_tree *subtree = NULL;
477 proto_tree *subtree_details = NULL;
478 guint32 offset;
479 char *str;
480 gint str_size;
482 if (!tree)
483 return 0 ;
485 ti = proto_tree_add_item(tree, hf_marker_segment,
486 tvb, 0, -1, ENC_NA);
487 subtree = proto_item_add_subtree(ti, ett_marker_segment);
489 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
490 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
492 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
494 str = tvb_get_stringz(wmem_packet_scope(), tvb, 4, &str_size);
495 ti = proto_tree_add_item(subtree, hf_identifier, tvb, 4, str_size, ENC_ASCII|ENC_NA);
496 if (strcmp(str, "JFIF") == 0) {
497 /* Version */
498 ti = proto_tree_add_none_format(subtree, hf_version,
499 tvb, 9, 2, "Version: %u.%u",
500 tvb_get_guint8(tvb, 9),
501 tvb_get_guint8(tvb, 10));
502 subtree_details = proto_item_add_subtree(ti, ett_details);
503 proto_tree_add_item(subtree_details, hf_version_major,
504 tvb, 9, 1, ENC_BIG_ENDIAN);
505 proto_tree_add_item(subtree_details, hf_version_minor,
506 tvb, 10, 1, ENC_BIG_ENDIAN);
508 proto_tree_add_item(subtree, hf_units,
509 tvb, 11, 1, ENC_BIG_ENDIAN);
511 /* Aspect ratio */
512 proto_tree_add_item(subtree, hf_xdensity,
513 tvb, 12, 2, ENC_BIG_ENDIAN);
514 proto_tree_add_item(subtree, hf_ydensity,
515 tvb, 14, 2, ENC_BIG_ENDIAN);
517 /* Thumbnail */
518 proto_tree_add_item(subtree, hf_xthumbnail,
519 tvb, 16, 1, ENC_BIG_ENDIAN);
520 proto_tree_add_item(subtree, hf_ythumbnail,
521 tvb, 17, 1, ENC_BIG_ENDIAN);
523 guint16 x = tvb_get_guint8(tvb, 16);
524 guint16 y = tvb_get_guint8(tvb, 17);
525 if (x || y) {
526 proto_tree_add_item(subtree, hf_rgb,
527 tvb, 18, 3 * (x * y), ENC_NA);
528 offset = 18 + (3 * (x * y));
529 } else {
530 offset = 18;
533 } else if (strcmp(str, "JFXX") == 0) {
534 proto_tree_add_item(subtree, hf_extension_code,
535 tvb, 9, 1, ENC_BIG_ENDIAN);
537 guint8 code = tvb_get_guint8(tvb, 9);
538 switch (code) {
539 case 0x10: /* Thumbnail coded using JPEG */
540 break;
541 case 0x11: /* thumbnail stored using 1 byte per pixel */
542 break;
543 case 0x13: /* thumbnail stored using 3 bytes per pixel */
544 break;
545 default: /* Error */
546 break;
549 offset = 10;
550 } else { /* Unknown */
551 proto_item_append_text(ti, " (unknown identifier)");
552 offset = 4 + str_size;
554 proto_tree_add_text(subtree, tvb, offset, -1,
555 "Remaining segment data (%u bytes)", len - 2 - str_size);
557 return offset;
560 /* Process an APP1 block.
562 * XXX - This code only works on US-ASCII systems!!!
564 static int
565 process_app1_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
566 guint16 marker, const char *marker_name)
568 proto_item *ti = NULL;
569 proto_tree *subtree = NULL;
570 char *str;
571 gint str_size;
572 int offset = 0;
573 int tiff_start;
575 if (!tree)
576 return 0;
578 ti = proto_tree_add_item(tree, hf_marker_segment,
579 tvb, 0, -1, ENC_NA);
580 subtree = proto_item_add_subtree(ti, ett_marker_segment);
582 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
583 proto_tree_add_item(subtree, hf_marker, tvb, offset, 2, ENC_BIG_ENDIAN);
584 offset += 2;
586 proto_tree_add_item(subtree, hf_len, tvb, offset, 2, ENC_BIG_ENDIAN);
587 offset += 2;
589 str = tvb_get_stringz(wmem_packet_scope(), tvb, offset, &str_size);
590 ti = proto_tree_add_item(subtree, hf_identifier, tvb, offset, str_size, ENC_ASCII|ENC_NA);
591 offset += str_size;
592 if (strcmp(str, "Exif") == 0) {
594 * Endianness
596 gboolean is_little_endian;
597 guint16 val_16;
598 guint32 val_32;
599 guint16 num_fields;
601 offset++; /* Skip a byte supposed to be 0x00 */
603 tiff_start = offset;
604 val_16 = tvb_get_ntohs(tvb, offset);
605 if (val_16 == 0x4949) {
606 is_little_endian = TRUE;
607 proto_tree_add_text(subtree, tvb, offset, 2, "Endianness: little endian");
608 } else if (val_16 == 0x4D4D) {
609 is_little_endian = FALSE;
610 proto_tree_add_text(subtree, tvb, offset, 2, "Endianness: big endian");
611 } else {
612 /* Error: invalid endianness encoding */
613 proto_tree_add_text(subtree, tvb, offset, 2,
614 "Incorrect endianness encoding - skipping the remainder of this application marker");
615 return offset;
617 offset += 2;
619 * Fixed value 42 = 0x002a
621 offset += 2;
623 * Offset to IFD
625 if (is_little_endian) {
626 val_32 = tvb_get_letohl(tvb, offset);
627 } else {
628 val_32 = tvb_get_ntohl(tvb, offset);
631 * Check for a bogus val_32 value.
632 * XXX - bogus value message should also deal with a
633 * value that's too large and causes an overflow.
634 * Or should it just check against the segment length,
635 * which is 16 bits?
637 if (val_32 + tiff_start < (guint32)offset + 4) {
638 proto_tree_add_text(subtree, tvb, offset, 4,
639 "Start offset of IFD starting from the TIFF header start: %u bytes (bogus, should be >= %u",
640 val_32, offset + 4 - tiff_start);
641 return offset;
643 proto_tree_add_text(subtree, tvb, offset, 4,
644 "Start offset of IFD starting from the TIFF header start: %u bytes",
645 val_32);
646 offset += 4;
648 * Skip the following portion
650 if (val_32 + tiff_start > (guint32)offset) {
651 proto_tree_add_text(subtree, tvb, offset, val_32 + tiff_start - offset,
652 "Skipped data between end of TIFF header and start of IFD (%u bytes)",
653 val_32 + tiff_start - offset);
655 for (;;) {
656 offset = val_32 + tiff_start;
658 * Process the IFD
660 if (is_little_endian) {
661 num_fields = tvb_get_letohs(tvb, offset);
662 } else {
663 num_fields = tvb_get_ntohs(tvb, offset);
665 proto_tree_add_text(subtree, tvb, offset, 2, "Number of fields in this IFD: %u", num_fields);
666 offset += 2;
667 while (num_fields-- > 0) {
668 guint16 tag, type;
669 guint32 count, off;
671 if (is_little_endian) {
672 tag = tvb_get_letohs(tvb, offset);
673 type = tvb_get_letohs(tvb, offset + 2);
674 count = tvb_get_letohl(tvb, offset + 4);
675 off = tvb_get_letohl(tvb, offset + 8);
676 } else {
677 tag = tvb_get_ntohs(tvb, offset);
678 type = tvb_get_ntohs(tvb, offset + 2);
679 count = tvb_get_ntohl(tvb, offset + 4);
680 off = tvb_get_ntohl(tvb, offset + 8);
682 /* TODO - refine this */
683 proto_tree_add_text(subtree, tvb, offset, 2,
684 "Exif Tag: 0x%04X (%s), Type: %u (%s), Count: %u, "
685 "Value offset from start of TIFF header: %u",
686 tag, val_to_str_const(tag, vals_exif_tags, "Unknown Exif tag"),
687 type, val_to_str_const(type, vals_exif_types, "Unknown Exif type"),
688 count, off);
689 offset += 12;
692 * Offset to the next IFD
694 if (is_little_endian) {
695 val_32 = tvb_get_letohl(tvb, offset);
696 } else {
697 val_32 = tvb_get_ntohl(tvb, offset);
699 if (val_32 != 0 &&
700 val_32 + tiff_start < (guint32)offset + 4) {
701 proto_tree_add_text(subtree, tvb, offset, 4,
702 "Offset to next IFD from start of TIFF header: %u bytes (bogus, should be >= %u)",
703 val_32, offset + 4 - tiff_start);
704 return offset;
706 proto_tree_add_text(subtree, tvb, offset, 4,
707 "Offset to next IFD from start of TIFF header: %u bytes",
708 val_32);
709 offset += 4;
710 if (val_32 == 0)
711 break;
713 } else {
714 proto_tree_add_text(subtree, tvb, offset, -1,
715 "Remaining segment data (%u bytes)", len - 2 - str_size);
716 proto_item_append_text(ti, " (Unknown identifier)");
718 return offset;
721 /* Process an APP2 block.
723 * XXX - This code only works on US-ASCII systems!!!
725 static void
726 process_app2_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
727 guint16 marker, const char *marker_name)
729 proto_item *ti = NULL;
730 proto_tree *subtree = NULL;
731 char *str;
732 gint str_size;
734 if (!tree)
735 return;
737 ti = proto_tree_add_item(tree, hf_marker_segment,
738 tvb, 0, -1, ENC_NA);
739 subtree = proto_item_add_subtree(ti, ett_marker_segment);
741 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
742 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
744 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
746 str = tvb_get_stringz(wmem_packet_scope(), tvb, 4, &str_size);
747 ti = proto_tree_add_item(subtree, hf_identifier, tvb, 4, str_size, ENC_ASCII|ENC_NA);
748 if (strcmp(str, "FPXR") == 0) {
749 proto_tree_add_text(tree, tvb, 0, -1, "Exif FlashPix APP2 application marker");
750 } else {
751 proto_tree_add_text(subtree, tvb, 4 + str_size, -1,
752 "Remaining segment data (%u bytes)", len - 2 - str_size);
753 proto_item_append_text(ti, " (Unknown identifier)");
757 static gint
758 dissect_jfif(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
760 proto_tree *subtree = NULL;
761 proto_item *ti = NULL;
762 gint tvb_len = tvb_reported_length(tvb);
763 gint32 start_entropy = 0;
764 gint32 start_fill, start_marker;
766 /* check if we have a full JFIF in tvb */
767 if (tvb_len < 20)
768 return 0;
769 if (tvb_get_ntohs(tvb, 0) != MARKER_SOI)
770 return 0;
771 if (tvb_memeql(tvb, 6, "JFIF", 5))
772 return 0;
774 /* Add summary to INFO column if it is enabled */
775 col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "(JPEG JFIF image)");
777 ti = proto_tree_add_item(tree, proto_jfif,
778 tvb, 0, -1, ENC_NA);
779 subtree = proto_item_add_subtree(ti, ett_jfif);
781 for (; ; ) {
782 const char *str;
783 guint16 marker;
785 start_fill = start_entropy;
787 for (; ; ) {
788 start_fill = tvb_find_guint8(tvb, start_fill, -1, 0xFF);
790 if (start_fill == -1 || tvb_len - start_fill == 1
791 || tvb_get_guint8(tvb, start_fill + 1) != 0) /* FF 00 is FF escaped */
792 break;
794 start_fill += 2;
797 if (start_fill == -1) start_fill = tvb_len;
799 if (start_fill != start_entropy)
800 proto_tree_add_text(subtree, tvb, start_entropy, start_fill - start_entropy,
801 "Entropy-coded segment (dissection is not yet implemented)");
803 if (start_fill == tvb_len) break;
805 start_marker = start_fill;
807 while (tvb_get_guint8(tvb, start_marker + 1) == 0xFF)
808 ++start_marker;
810 if (start_marker != start_fill)
811 proto_tree_add_text(subtree, tvb, start_fill, start_marker - start_fill,
812 "Fill bytes");
814 marker = tvb_get_ntohs(tvb, start_marker);
815 str = try_val_to_str(marker, vals_marker);
816 if (str) { /* Known marker */
817 if (marker_has_length(marker)) { /* Marker segment */
818 /* Length of marker segment = 2 + len */
819 const guint16 len = tvb_get_ntohs(tvb, start_marker + 2);
820 tvbuff_t *tmp_tvb = tvb_new_subset(tvb, start_marker, 2 + len, 2 + len);
821 switch (marker) {
822 case MARKER_APP0:
823 process_app0_segment(subtree, tmp_tvb, len, marker, str);
824 break;
825 case MARKER_APP1:
826 process_app1_segment(subtree, tmp_tvb, len, marker, str);
827 break;
828 case MARKER_APP2:
829 process_app2_segment(subtree, tmp_tvb, len, marker, str);
830 break;
831 case MARKER_SOF0:
832 case MARKER_SOF1:
833 case MARKER_SOF2:
834 case MARKER_SOF3:
835 case MARKER_SOF5:
836 case MARKER_SOF6:
837 case MARKER_SOF7:
838 case MARKER_SOF8:
839 case MARKER_SOF9:
840 case MARKER_SOF10:
841 case MARKER_SOF11:
842 case MARKER_SOF13:
843 case MARKER_SOF14:
844 case MARKER_SOF15:
845 process_sof_header(subtree, tmp_tvb, len, marker, str);
846 break;
847 case MARKER_SOS:
848 process_sos_header(subtree, tmp_tvb, len, marker, str);
849 break;
850 default:
851 process_marker_segment(subtree, tmp_tvb, len, marker, str);
852 break;
854 start_entropy = start_marker + 2 + len;
855 } else { /* Marker but no segment */
856 /* Length = 2 */
857 proto_tree_add_item(subtree, hf_marker,
858 tvb, start_marker, 2, ENC_BIG_ENDIAN);
859 start_entropy = start_marker + 2;
861 } else { /* Reserved! */
862 ti = proto_tree_add_item(subtree, hf_marker,
863 tvb, start_marker, 2, ENC_BIG_ENDIAN);
864 proto_item_append_text(ti, " (Reserved)");
865 return tvb_len;
869 return tvb_len;
872 static gboolean
873 dissect_jfif_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
875 return dissect_jfif(tvb, pinfo, tree, NULL) > 0;
878 /****************** Register the protocol with Wireshark ******************/
880 void
881 proto_register_jfif(void)
884 * Setup list of header fields.
886 static hf_register_info hf[] = {
887 /* Marker */
888 { &hf_marker,
889 { "Marker",
890 IMG_JFIF ".marker",
891 FT_UINT8, BASE_HEX, VALS(vals_marker), 0x00,
892 "JFIF Marker",
893 HFILL
896 /* Marker segment */
897 { &hf_marker_segment,
898 { "Marker segment",
899 IMG_JFIF ".marker_segment",
900 FT_NONE, BASE_NONE, NULL, 0x00,
901 NULL,
902 HFILL
905 { &hf_len,
906 { "Length",
907 IMG_JFIF ".length",
908 FT_UINT16, BASE_DEC, 0, 0x00,
909 "Length of segment (including length field)",
910 HFILL
913 /* MARKER_APP0 */
914 { &hf_identifier,
915 { "Identifier",
916 IMG_JFIF ".identifier",
917 FT_STRINGZ, BASE_NONE, NULL, 0x00,
918 "Identifier of the segment",
919 HFILL
922 /* MARKER_APP0 - JFIF */
923 { &hf_version,
924 { "Version",
925 IMG_JFIF ".version",
926 FT_NONE, BASE_NONE, NULL, 0x00,
927 "JFIF Version",
928 HFILL
931 { &hf_version_major,
932 { "Major Version",
933 IMG_JFIF ".version.major",
934 FT_UINT8, BASE_DEC, NULL, 0x00,
935 "JFIF Major Version",
936 HFILL
939 { &hf_version_minor,
940 { "Minor Version",
941 IMG_JFIF ".version.minor",
942 FT_UINT8, BASE_DEC, NULL, 0x00,
943 "JFIF Minor Version",
944 HFILL
947 { &hf_units,
948 { "Units",
949 IMG_JFIF ".units",
950 FT_UINT8, BASE_DEC, VALS(vals_units), 0x00,
951 "Units used in this segment",
952 HFILL
955 { &hf_xdensity,
956 { "Xdensity",
957 IMG_JFIF ".Xdensity",
958 FT_UINT16, BASE_DEC, NULL, 0x00,
959 "Horizontal pixel density",
960 HFILL
963 { &hf_ydensity,
964 { "Ydensity",
965 IMG_JFIF ".Ydensity",
966 FT_UINT16, BASE_DEC, NULL, 0x00,
967 "Vertical pixel density",
968 HFILL
971 { &hf_xthumbnail,
972 { "Xthumbnail",
973 IMG_JFIF ".Xthumbnail",
974 FT_UINT16, BASE_DEC, NULL, 0x00,
975 "Thumbnail horizontal pixel count",
976 HFILL
979 { &hf_ythumbnail,
980 { "Ythumbnail",
981 IMG_JFIF ".Ythumbnail",
982 FT_UINT16, BASE_DEC, NULL, 0x00,
983 "Thumbnail vertical pixel count",
984 HFILL
987 { &hf_rgb,
988 { "RGB values of thumbnail pixels",
989 IMG_JFIF ".RGB",
990 FT_BYTES, BASE_NONE, NULL, 0x00,
991 "RGB values of the thumbnail pixels (24 bit per pixel, Xthumbnail x Ythumbnail pixels)",
992 HFILL
995 /* MARKER_APP0 - JFXX */
996 { &hf_extension_code,
997 { "Extension code",
998 IMG_JFIF ".extension.code",
999 FT_UINT8, BASE_HEX, VALS(vals_extension_code), 0x00,
1000 "JFXX extension code for thumbnail encoding",
1001 HFILL
1004 /* Header: Start of Frame (MARKER_SOF) */
1005 { &hf_sof_header,
1006 { "Start of Frame header",
1007 IMG_JFIF ".sof",
1008 FT_NONE, BASE_NONE, NULL, 0x00,
1009 NULL,
1010 HFILL
1013 { &hf_sof_precision,
1014 { "Sample Precision (bits)",
1015 IMG_JFIF ".sof.precision",
1016 FT_UINT8, BASE_DEC, NULL, 0x00,
1017 "Specifies the precision in bits for the samples of the components in the frame.",
1018 HFILL
1021 { &hf_sof_lines,
1022 { "Lines",
1023 IMG_JFIF ".sof.lines",
1024 FT_UINT16, BASE_DEC, NULL, 0x00,
1025 "Specifies the maximum number of lines in the source image.",
1026 HFILL
1029 { &hf_sof_samples_per_line,
1030 { "Samples per line",
1031 IMG_JFIF ".sof.samples_per_line",
1032 FT_UINT16, BASE_DEC, NULL, 0x00,
1033 "Specifies the maximum number of samples per line in the source image.",
1034 HFILL
1037 { &hf_sof_nf,
1038 { "Number of image components in frame",
1039 IMG_JFIF ".sof.nf",
1040 FT_UINT8, BASE_DEC, NULL, 0x00,
1041 "Specifies the number of source image components in the frame.",
1042 HFILL
1045 { &hf_sof_c_i,
1046 { "Component identifier",
1047 IMG_JFIF ".sof.c_i",
1048 FT_UINT8, BASE_DEC, NULL, 0x00,
1049 "Assigns a unique label to the ith component in the sequence of frame component specification parameters.",
1050 HFILL
1053 { &hf_sof_h_i,
1054 { "Horizontal sampling factor",
1055 IMG_JFIF ".sof.h_i",
1056 FT_UINT8, BASE_DEC, NULL, 0xF0,
1057 "Specifies the relationship between the component horizontal dimension and maximum image dimension X.",
1058 HFILL
1061 { &hf_sof_v_i,
1062 { "Vertical sampling factor",
1063 IMG_JFIF ".sof.v_i",
1064 FT_UINT8, BASE_DEC, NULL, 0x0F,
1065 "Specifies the relationship between the component vertical dimension and maximum image dimension Y.",
1066 HFILL
1069 { &hf_sof_tq_i,
1070 { "Quantization table destination selector",
1071 IMG_JFIF ".sof.tq_i",
1072 FT_UINT8, BASE_DEC, NULL, 0x00,
1073 "Specifies one of four possible quantization table destinations from which the quantization table to"
1074 " use for dequantization of DCT coefficients of component Ci is retrieved.",
1075 HFILL
1079 /* Header: Start of Segment (MARKER_SOS) */
1080 { &hf_sos_header,
1081 { "Start of Segment header",
1082 IMG_JFIF ".header.sos",
1083 FT_NONE, BASE_NONE, NULL, 0x00,
1084 NULL,
1085 HFILL
1088 { &hf_sos_ns,
1089 { "Number of image components in scan",
1090 IMG_JFIF ".sos.ns",
1091 FT_UINT8, BASE_DEC, NULL, 0x00,
1092 "Specifies the number of source image components in the scan.",
1093 HFILL
1096 { &hf_sos_cs_j,
1097 { "Scan component selector",
1098 IMG_JFIF ".sos.component_selector",
1099 FT_UINT8, BASE_DEC, NULL, 0x00,
1100 "Selects which of the Nf image components specified in the frame parameters shall be the jth"
1101 " component in the scan.",
1102 HFILL
1105 { &hf_sos_td_j,
1106 { "DC entropy coding table destination selector",
1107 IMG_JFIF ".sos.dc_entropy_selector",
1108 FT_UINT8, BASE_DEC, NULL, 0xF0,
1109 "Specifies one of four possible DC entropy coding table destinations from which the entropy"
1110 " table needed for decoding of the DC coefficients of component Csj is retrieved.",
1111 HFILL
1114 { &hf_sos_ta_j,
1115 { "AC entropy coding table destination selector",
1116 IMG_JFIF ".sos.ac_entropy_selector",
1117 FT_UINT8, BASE_DEC, NULL, 0x0F,
1118 "Specifies one of four possible AC entropy coding table destinations from which the entropy"
1119 " table needed for decoding of the AC coefficients of component Csj is retrieved.",
1120 HFILL
1123 { &hf_sos_ss,
1124 { "Start of spectral or predictor selection",
1125 IMG_JFIF ".sos.ss",
1126 FT_UINT8, BASE_DEC, NULL, 0x00,
1127 "In the DCT modes of operation, this parameter specifies the first DCT coefficient in"
1128 " each block in zig-zag order which shall be coded in the scan. This parameter shall"
1129 " be set to zero for the sequential DCT processes. In the lossless mode of operations"
1130 " this parameter is used to select the predictor.",
1131 HFILL
1134 { &hf_sos_se,
1135 { "End of spectral selection",
1136 IMG_JFIF ".sos.se",
1137 FT_UINT8, BASE_DEC, NULL, 0x00,
1138 "Specifies the last DCT coefficient in each block in zig-zag order which shall be coded"
1139 " in the scan. This parameter shall be set to 63 for the sequential DCT processes. In the"
1140 " lossless mode of operations this parameter has no meaning. It shall be set to zero.",
1141 HFILL
1144 { &hf_sos_ah,
1145 { "Successive approximation bit position high",
1146 IMG_JFIF ".sos.ah",
1147 FT_UINT8, BASE_DEC, NULL, 0xF0,
1148 "This parameter specifies the point transform used in the preceding scan (i.e. successive"
1149 " approximation bit position low in the preceding scan) for the band of coefficients"
1150 " specified by Ss and Se. This parameter shall be set to zero for the first scan of each"
1151 " band of coefficients. In the lossless mode of operations this parameter has no meaning."
1152 " It shall be set to zero.",
1153 HFILL
1156 { &hf_sos_al,
1157 { "Successive approximation bit position low or point transform",
1158 IMG_JFIF ".sos.al",
1159 FT_UINT8, BASE_DEC, NULL, 0x0F,
1160 "In the DCT modes of operation this parameter specifies the point transform, i.e. bit"
1161 " position low, used before coding the band of coefficients specified by Ss and Se."
1162 " This parameter shall be set to zero for the sequential DCT processes. In the lossless"
1163 " mode of operations, this parameter specifies the point transform, Pt.",
1164 HFILL
1169 /* Setup protocol subtree array */
1170 static gint *ett[] = {
1171 &ett_jfif,
1172 &ett_marker_segment,
1173 &ett_details,
1176 /* Register the protocol name and description */
1177 proto_jfif = proto_register_protocol(
1178 "JPEG File Interchange Format",
1179 "JFIF (JPEG) image",
1180 IMG_JFIF
1183 /* Required function calls to register the header fields
1184 * and subtrees used */
1185 proto_register_field_array(proto_jfif, hf, array_length(hf));
1186 proto_register_subtree_array(ett, array_length(ett));
1188 new_register_dissector(IMG_JFIF, dissect_jfif, proto_jfif);
1192 void
1193 proto_reg_handoff_jfif(void)
1195 dissector_handle_t jfif_handle = find_dissector(IMG_JFIF);
1197 /* Register the JPEG media type */
1198 dissector_add_string("media_type", "image/jfif", jfif_handle);
1199 dissector_add_string("media_type", "image/jpg", jfif_handle);
1200 dissector_add_string("media_type", "image/jpeg", jfif_handle);
1202 dissector_add_uint("wtap_encap", WTAP_ENCAP_JPEG_JFIF, jfif_handle);
1204 heur_dissector_add("http", dissect_jfif_heur, proto_jfif);
1205 heur_dissector_add("wtap_file", dissect_jfif_heur, proto_jfif);