3 * Routines for JFIF image/jpeg media dissection
4 * Copyright 2004, Olivier Biot.
6 * $Id: packet-image-jfif.c,v 1.5 2004/05/31 01:24:03 obiot Exp $
8 * Refer to the AUTHORS file or the AUTHORS section in the man page
9 * for contacting the author(s) of this file.
11 * Ethereal - Network traffic analyzer
12 * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 /* Edit this file with 4-space tabulation */
51 #ifdef NEED_SNPRINTF_H
52 # include "snprintf.h"
55 #include <epan/packet.h>
57 /* General-purpose debug logger.
58 * Requires double parentheses because of variable arguments of printf().
60 * Enable debug logging for JFIF by defining AM_CFLAGS
61 * so that it contains "-DDEBUG_image_jfif" or "-DDEBUG_image"
63 #if (defined(DEBUG_image_jfif) || defined(DEBUG_image))
65 printf("%s:%u: ", __FILE__, __LINE__); \
72 #define PLURALIZE(x) ((x) == 1 ? "" : "s")
74 #define IMG_JFIF "image-jfif"
76 /************************** Variable declarations **************************/
78 #define MARKER_TEM 0xFF01
80 /* 0xFF02 -- 0xFFBF are reserved */
82 #define MARKER_SOF0 0xFFC0
83 #define MARKER_SOF1 0xFFC1
84 #define MARKER_SOF2 0xFFC2
85 #define MARKER_SOF3 0xFFC3
87 #define MARKER_DHT 0xFFC4
89 #define MARKER_SOF5 0xFFC5
90 #define MARKER_SOF6 0xFFC6
91 #define MARKER_SOF7 0xFFC7
92 #define MARKER_SOF8 0xFFC8
93 #define MARKER_SOF9 0xFFC9
94 #define MARKER_SOF10 0xFFCA
95 #define MARKER_SOF11 0xFFCB
97 #define MARKER_DAC 0xFFCC
99 #define MARKER_SOF13 0xFFCD
100 #define MARKER_SOF14 0xFFCE
101 #define MARKER_SOF15 0xFFCF
103 #define MARKER_RST0 0xFFD0
104 #define MARKER_RST1 0xFFD1
105 #define MARKER_RST2 0xFFD2
106 #define MARKER_RST3 0xFFD3
107 #define MARKER_RST4 0xFFD4
108 #define MARKER_RST5 0xFFD5
109 #define MARKER_RST6 0xFFD6
110 #define MARKER_RST7 0xFFD7
112 #define MARKER_FFDB 0xFFDB
113 #define MARKER_SOI 0xFFD8
114 #define MARKER_EOI 0xFFD9
115 #define MARKER_SOS 0xFFDA
116 #define MARKER_DQT 0xFFDB
117 #define MARKER_DNL 0xFFDC
118 #define MARKER_DRI 0xFFDD
119 #define MARKER_DHP 0xFFDE
120 #define MARKER_EXP 0xFFDF
122 #define MARKER_APP0 0xFFE0
123 #define MARKER_APP1 0xFFE1
124 #define MARKER_APP2 0xFFE2
125 #define MARKER_APP3 0xFFE3
126 #define MARKER_APP4 0xFFE4
127 #define MARKER_APP5 0xFFE5
128 #define MARKER_APP6 0xFFE6
129 #define MARKER_APP7 0xFFE7
130 #define MARKER_APP8 0xFFE8
131 #define MARKER_APP9 0xFFE9
132 #define MARKER_APP10 0xFFEA
133 #define MARKER_APP11 0xFFEB
134 #define MARKER_APP12 0xFFEC
135 #define MARKER_APP13 0xFFED
136 #define MARKER_APP14 0xFFEE
137 #define MARKER_APP15 0xFFEF
139 #define MARKER_JPG0 0xFFF0
140 #define MARKER_JPG1 0xFFF1
141 #define MARKER_JPG2 0xFFF2
142 #define MARKER_JPG3 0xFFF3
143 #define MARKER_JPG4 0xFFF4
144 #define MARKER_JPG5 0xFFF5
145 #define MARKER_JPG6 0xFFF6
146 #define MARKER_JPG7 0xFFF7
147 #define MARKER_JPG8 0xFFF8
148 #define MARKER_JPG9 0xFFF9
149 #define MARKER_JPG10 0xFFFA
150 #define MARKER_JPG11 0xFFFB
151 #define MARKER_JPG12 0xFFFC
152 #define MARKER_JPG13 0xFFFD
154 #define MARKER_COM 0xFFFE
156 #define marker_has_length(marker) ( ! ( \
157 ((marker) == MARKER_TEM) \
158 || ((marker) == MARKER_SOI) \
159 || ((marker) == MARKER_EOI) \
160 || ( ((marker) >= MARKER_RST0) && ((marker) <= MARKER_RST7) ) \
164 static const value_string vals_marker[] = {
165 { MARKER_TEM, "Reserved - For temporary private use in arithmetic coding" },
166 { MARKER_SOF0, "Start of Frame (non-differential, Huffman coding) - Baseline DCT" },
167 { MARKER_SOF1, "Start of Frame (non-differential, Huffman coding) - Extended sequential DCT" },
168 { MARKER_SOF2, "Start of Frame (non-differential, Huffman coding) - Progressive DCT" },
169 { MARKER_SOF3, "Start of Frame (non-differential, Huffman coding) - Lossless (sequential)" },
170 { MARKER_DHT, "Define Huffman table(s)" },
171 { MARKER_SOF5, "Start of Frame (differential, Huffman coding) - Differential sequential DCT" },
172 { MARKER_SOF6, "Start of Frame (differential, Huffman coding) - Differential progressive DCT" },
173 { MARKER_SOF7, "Start of Frame (differential, Huffman coding) - Differential lossless (sequential)" },
174 { MARKER_SOF8, "Start of Frame (non-differential, arithmetic coding) - Reserved for JPEG extensions" },
175 { MARKER_SOF9, "Start of Frame (non-differential, arithmetic coding) - Extended sequential DCT" },
176 { MARKER_SOF10, "Start of Frame (non-differential, arithmetic coding) - Progressive DCT" },
177 { MARKER_SOF11, "Start of Frame (non-differential, arithmetic coding) - Lossless (sequential)" },
178 { MARKER_DAC, "Define arithmetic coding conditioning(s)" },
179 { MARKER_SOF13, "Start of Frame (differential, arithmetic coding) - Differential sequential DCT" },
180 { MARKER_SOF14, "Start of Frame (differential, arithmetic coding) - Differential progressive DCT" },
181 { MARKER_SOF15, "Start of Frame (differential, arithmetic coding) - Differential lossless (sequential)" },
182 { MARKER_RST0, "Restart interval termination - Restart with modulo 8 count 0" },
183 { MARKER_RST1, "Restart interval termination - Restart with modulo 8 count 1" },
184 { MARKER_RST2, "Restart interval termination - Restart with modulo 8 count 2" },
185 { MARKER_RST3, "Restart interval termination - Restart with modulo 8 count 3" },
186 { MARKER_RST4, "Restart interval termination - Restart with modulo 8 count 4" },
187 { MARKER_RST5, "Restart interval termination - Restart with modulo 8 count 5" },
188 { MARKER_RST6, "Restart interval termination - Restart with modulo 8 count 6" },
189 { MARKER_RST7, "Restart interval termination - Restart with modulo 8 count 7" },
190 { MARKER_SOI, "Start of Image" },
191 { MARKER_EOI, "End of Image" },
192 { MARKER_SOS, "Start of Scan" },
193 { MARKER_DQT, "Define quantization table(s)" },
194 { MARKER_DNL, "Define number of lines" },
195 { MARKER_DRI, "Define restart interval" },
196 { MARKER_DHP, "Define hierarchical progression" },
197 { MARKER_EXP, "Expand reference component(s)" },
198 { MARKER_APP0, "Reserved for application segments - 0" },
199 { MARKER_APP1, "Reserved for application segments - 1" },
200 { MARKER_APP2, "Reserved for application segments - 2" },
201 { MARKER_APP3, "Reserved for application segments - 3" },
202 { MARKER_APP4, "Reserved for application segments - 4" },
203 { MARKER_APP5, "Reserved for application segments - 5" },
204 { MARKER_APP6, "Reserved for application segments - 6" },
205 { MARKER_APP7, "Reserved for application segments - 7" },
206 { MARKER_APP8, "Reserved for application segments - 8" },
207 { MARKER_APP9, "Reserved for application segments - 9" },
208 { MARKER_APP10, "Reserved for application segments - 10" },
209 { MARKER_APP11, "Reserved for application segments - 11" },
210 { MARKER_APP12, "Reserved for application segments - 12" },
211 { MARKER_APP13, "Reserved for application segments - 13" },
212 { MARKER_APP14, "Reserved for application segments - 14" },
213 { MARKER_APP15, "Reserved for application segments - 15" },
214 { MARKER_JPG0, "Reserved for JPEG extensions - 0" },
215 { MARKER_JPG1, "Reserved for JPEG extensions - 1" },
216 { MARKER_JPG2, "Reserved for JPEG extensions - 2" },
217 { MARKER_JPG3, "Reserved for JPEG extensions - 3" },
218 { MARKER_JPG4, "Reserved for JPEG extensions - 4" },
219 { MARKER_JPG5, "Reserved for JPEG extensions - 5" },
220 { MARKER_JPG6, "Reserved for JPEG extensions - 6" },
221 { MARKER_JPG7, "Reserved for JPEG extensions - 7" },
222 { MARKER_JPG8, "Reserved for JPEG extensions - 8" },
223 { MARKER_JPG9, "Reserved for JPEG extensions - 9" },
224 { MARKER_JPG10, "Reserved for JPEG extensions - 10" },
225 { MARKER_JPG11, "Reserved for JPEG extensions - 11" },
226 { MARKER_JPG12, "Reserved for JPEG extensions - 12" },
227 { MARKER_JPG13, "Reserved for JPEG extensions - 13" },
231 static const value_string vals_units[] = {
232 { 0, "No units; Xdensity and Ydensity specify the pixel aspect ratio" },
233 { 1, "Dots per inch" },
234 { 2, "Dots per centimeter" },
238 static const value_string vals_extension_code[] = {
239 { 0x10, "Thumbnail encoded using JPEG" },
240 { 0x11, "Thumbnail encoded using 1 byte (8 bits) per pixel" },
241 { 0x13, "Thumbnail encoded using 3 bytes (24 bits) per pixel" },
245 static const value_string vals_exif_tags[] = {
247 * Tags related to image data structure:
249 { 0x0100, "ImageWidth" },
250 { 0x0101, "ImageLength" },
251 { 0x0102, "BitsPerSample" },
252 { 0x0103, "Compression" },
253 { 0x0106, "PhotometricInterpretation" },
254 { 0x0112, "Orientation" },
255 { 0x0115, "SamplesPerPixel" },
256 { 0x011C, "PlanarConfiguration" },
257 { 0x0212, "YCbCrSubSampling" },
258 { 0x0213, "YCbCrPositioning" },
259 { 0x011A, "XResolution" },
260 { 0x011B, "YResolution" },
261 { 0x0128, "ResolutionUnit" },
263 * Tags relating to recording offset:
265 { 0x0111, "StripOffsets" },
266 { 0x0116, "RowsPerStrip" },
267 { 0x0117, "StripByteCounts" },
268 { 0x0201, "JPEGInterchangeFormat" },
269 { 0x0202, "JPEGInterchangeFormatLength" },
271 * Tags relating to image data characteristics:
273 { 0x012D, "TransferFunction" },
274 { 0x013E, "WhitePoint" },
275 { 0x013F, "PrimaryChromaticities" },
276 { 0x0211, "YCbCrCoefficients" },
277 { 0x0214, "ReferenceBlackWhite" },
281 { 0x0132, "DateTime" },
282 { 0x010E, "ImageDescription" },
285 { 0x0131, "Software" },
286 { 0x013B, "Artist" },
287 { 0x8296, "Copyright" },
291 { 0x8769, "Exif IFD Pointer"},
292 { 0x8825, "GPS IFD Pointer"},
293 { 0xA005, "Interoperability IFD Pointer"},
298 static const value_string vals_exif_types[] = {
303 { 0x0005, "RATIONAL" },
305 { 0x0007, "UNDEFINED" },
308 { 0x000A, "SRATIONAL" },
313 /* Initialize the protocol and registered fields */
314 static int proto_jfif = -1;
317 static gint hf_marker = -1;
319 static gint hf_marker_segment = -1;
320 static gint hf_len = -1;
322 static gint hf_identifier = -1;
323 /* MARKER_APP0 - JFIF */
324 static gint hf_version = -1;
325 static gint hf_version_major = -1;
326 static gint hf_version_minor = -1;
327 static gint hf_units = -1;
328 static gint hf_xdensity = -1;
329 static gint hf_ydensity = -1;
330 static gint hf_xthumbnail = -1;
331 static gint hf_ythumbnail = -1;
332 static gint hf_rgb = -1;
333 /* MARKER_APP0 - JFXX */
334 static gint hf_extension_code = -1;
336 static gint hf_sof_header = -1;
337 static gint hf_sof_precision = -1;
338 static gint hf_sof_lines = -1;
339 static gint hf_sof_samples_per_line = -1;
340 static gint hf_sof_nf = -1;
341 static gint hf_sof_c_i = -1;
342 static gint hf_sof_h_i = -1;
343 static gint hf_sof_v_i = -1;
344 static gint hf_sof_tq_i = -1;
347 static gint hf_sos_header = -1;
348 static gint hf_sos_ns = -1;
349 static gint hf_sos_cs_j = -1;
350 static gint hf_sos_td_j = -1;
351 static gint hf_sos_ta_j = -1;
352 static gint hf_sos_ss = -1;
353 static gint hf_sos_se = -1;
354 static gint hf_sos_ah = -1;
355 static gint hf_sos_al = -1;
357 /* Initialize the subtree pointers */
358 static gint ett_jfif = -1;
359 static gint ett_marker_segment = -1;
360 static gint ett_details = -1;
363 /**************** GIF related declarations and definitions ****************/
366 /************************** Function prototypes **************************/
370 dissect_jfif(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
373 proto_register_jfif(void);
377 /****************** JFIF protocol dissection functions ******************/
379 #define ErrorInvalidJFIF "This is not a valid JFIF (JPEG) object"
383 * Process a marker segment (with length).
386 process_marker_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
387 guint16 marker, const char *marker_name)
389 proto_item *ti = NULL;
390 proto_tree *subtree = NULL;
395 ti = proto_tree_add_item(tree, hf_marker_segment,
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, FALSE);
402 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
404 proto_tree_add_text(subtree, tvb, 4, -1,
405 "Remaining segment data (%u bytes)", len - 2);
409 * Process a Start of Frame header (with length).
412 process_sof_header(proto_tree *tree, tvbuff_t *tvb, guint32 len _U_,
413 guint16 marker, const char *marker_name)
415 proto_item *ti = NULL;
416 proto_tree *subtree = NULL;
421 ti = proto_tree_add_item(tree, hf_sof_header,
423 subtree = proto_item_add_subtree(ti, ett_marker_segment);
425 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
426 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
428 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
430 proto_tree_add_item(subtree, hf_sof_precision, tvb, 4, 1, FALSE);
432 proto_tree_add_item(subtree, hf_sof_lines, tvb, 5, 2, FALSE);
434 proto_tree_add_item(subtree, hf_sof_samples_per_line, tvb, 7, 2, FALSE);
436 proto_tree_add_item(subtree, hf_sof_nf, tvb, 9, 1, FALSE);
438 guint8 count = tvb_get_guint8(tvb, 9);
441 proto_tree_add_item(subtree, hf_sof_c_i, tvb, offset++, 1, FALSE);
442 proto_tree_add_item(subtree, hf_sof_h_i, tvb, offset, 1, FALSE);
443 proto_tree_add_item(subtree, hf_sof_v_i, tvb, offset++, 1, FALSE);
444 proto_tree_add_item(subtree, hf_sof_tq_i, tvb, offset++, 1, FALSE);
451 * Process a Start of Segment header (with length).
454 process_sos_header(proto_tree *tree, tvbuff_t *tvb, guint32 len _U_,
455 guint16 marker, const char *marker_name)
457 proto_item *ti = NULL;
458 proto_tree *subtree = NULL;
464 ti = proto_tree_add_item(tree, hf_sos_header,
466 subtree = proto_item_add_subtree(ti, ett_marker_segment);
468 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
469 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
471 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
473 proto_tree_add_item(subtree, hf_sos_ns, tvb, 4, 1, FALSE);
475 guint8 count = tvb_get_guint8(tvb, 4);
478 proto_tree_add_item(subtree, hf_sos_cs_j, tvb, offset++, 1, FALSE);
479 proto_tree_add_item(subtree, hf_sos_td_j, tvb, offset, 1, FALSE);
480 proto_tree_add_item(subtree, hf_sos_ta_j, tvb, offset++, 1, FALSE);
485 proto_tree_add_item(subtree, hf_sos_ss, tvb, offset++, 1, FALSE);
486 proto_tree_add_item(subtree, hf_sos_se, tvb, offset++, 1, FALSE);
488 proto_tree_add_item(subtree, hf_sos_ah, tvb, offset, 1, FALSE);
489 proto_tree_add_item(subtree, hf_sos_al, tvb, offset++, 1, FALSE);
492 /* Process an APP0 block.
494 * XXX - This code only works on US-ASCII systems!!!
497 process_app0_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
498 guint16 marker, const char *marker_name)
500 proto_item *ti = NULL;
501 proto_tree *subtree = NULL;
502 proto_tree *subtree_details = NULL;
510 ti = proto_tree_add_item(tree, hf_marker_segment,
512 subtree = proto_item_add_subtree(ti, ett_marker_segment);
514 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
515 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
517 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
519 str = tvb_get_stringz(tvb, 4, &str_size);
520 ti = proto_tree_add_item(subtree, hf_identifier, tvb, 4, str_size, FALSE);
521 if (strcmp(str, "JFIF") == 0) {
523 ti = proto_tree_add_none_format(subtree, hf_version,
524 tvb, 9, 2, "Version: %u.%u",
525 tvb_get_guint8(tvb, 9),
526 tvb_get_guint8(tvb, 10));
527 subtree_details = proto_item_add_subtree(ti, ett_details);
528 proto_tree_add_item(subtree_details, hf_version_major,
530 proto_tree_add_item(subtree_details, hf_version_minor,
533 proto_tree_add_item(subtree, hf_units,
537 proto_tree_add_item(subtree, hf_xdensity,
539 proto_tree_add_item(subtree, hf_ydensity,
543 proto_tree_add_item(subtree, hf_xthumbnail,
545 proto_tree_add_item(subtree, hf_ythumbnail,
548 guint16 x = tvb_get_guint8(tvb, 16);
549 guint16 y = tvb_get_guint8(tvb, 17);
551 proto_tree_add_item(subtree, hf_rgb,
552 tvb, 18, 3 * (x * y), FALSE);
553 offset = 18 + (3 * (x * y));
558 } else if (strcmp(str, "JFXX") == 0) {
559 proto_tree_add_item(subtree, hf_extension_code,
562 guint8 code = tvb_get_guint8(tvb, 9);
564 case 0x10: /* Thumbnail coded using JPEG */
566 case 0x11: /* thumbnail stored using 1 byte per pixel */
568 case 0x13: /* thumbnail stored using 3 bytes per pixel */
574 } else { /* Unknown */
575 proto_item_append_text(ti, " (unknown identifier)");
576 offset = 4 + str_size;
578 proto_tree_add_text(subtree, tvb, offset, -1,
579 "Remaining segment data (%u bytes)", len - 2 - str_size);
585 /* Process an APP1 block.
587 * XXX - This code only works on US-ASCII systems!!!
590 process_app1_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
591 guint16 marker, const char *marker_name)
593 proto_item *ti = NULL;
594 proto_tree *subtree = NULL;
595 proto_tree *subtree_details = NULL;
598 guint32 offset, tiff_start;
603 ti = proto_tree_add_item(tree, hf_marker_segment,
605 subtree = proto_item_add_subtree(ti, ett_marker_segment);
607 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
608 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
610 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
612 str = tvb_get_stringz(tvb, 4, &str_size);
613 ti = proto_tree_add_item(subtree, hf_identifier, tvb, 4, str_size, FALSE);
614 offset = tiff_start = 4 + str_size;
615 if (strcmp(str, "Exif") == 0) {
619 gboolean is_little_endian;
624 offset++; /* Skip a byte supposed to be 0x00 */
626 val_16 = tvb_get_ntohs(tvb, offset);
627 if (val_16 == 0x4949) {
628 is_little_endian = TRUE;
629 proto_tree_add_text(subtree, tvb, offset, 2, "Endianness: little endian");
630 } else if (val_16 == 0x4D4D) {
631 is_little_endian = FALSE;
632 proto_tree_add_text(subtree, tvb, offset, 2, "Endianness: big endian");
634 /* Error: invalid endianness encoding */
635 proto_tree_add_text(subtree, tvb, offset, 2,
636 "Incorrect endianness encoding - skipping the remainder of this application marker");
641 * Fixed value 42 = 0x002a
647 if (is_little_endian) {
648 val_16 = tvb_get_letohs(tvb, offset);
650 val_16 = tvb_get_ntohs(tvb, offset);
652 proto_tree_add_text(subtree, tvb, offset, 4,
653 "Start offset of IFD starting from the TIFF header start: %u bytes", val_16);
656 * Skip the following portion
658 proto_tree_add_text(subtree, tvb, offset, val_16 + tiff_start - offset,
659 "Skipped data between end of TIFF header and start of IFD (%u bytes)",
660 val_16 + tiff_start - offset);
661 offset = val_16 + tiff_start + 1;
663 * Process the "0th" IFD
665 if (is_little_endian) {
666 num_fields = tvb_get_letohs(tvb, offset);
668 num_fields = tvb_get_ntohs(tvb, offset);
670 proto_tree_add_text(subtree, tvb, offset, 2, "Number of fields in this IFD: %u", num_fields);
672 while (num_fields-- > 0) {
676 if (is_little_endian) {
677 tag = tvb_get_letohs(tvb, offset);
678 type = tvb_get_letohs(tvb, offset + 2);
679 count = tvb_get_letohl(tvb, offset + 4);
680 off = tvb_get_letohl(tvb, offset + 8);
682 tag = tvb_get_ntohs(tvb, offset);
683 type = tvb_get_ntohs(tvb, offset + 2);
684 count = tvb_get_ntohl(tvb, offset + 4);
685 off = tvb_get_ntohl(tvb, offset + 8);
687 /* TODO - refine this */
688 proto_tree_add_text(subtree, tvb, offset, 2,
689 "Exif Tag: 0x%04X (%s), Type: %u (%s), Count: %u, "
690 "Value offset from start of TIFF header: %u",
691 tag, val_to_str(tag, vals_exif_tags, "Unknown Exif tag"),
692 type, val_to_str(type, vals_exif_types, "Unknown Exif type"),
697 * Offset to the "1st" IFD
699 if (is_little_endian) {
700 val_32 = tvb_get_letohl(tvb, offset);
702 val_32 = tvb_get_ntohl(tvb, offset);
704 proto_tree_add_text(subtree, tvb, offset, 4,
705 "Offset to next IFD from start of TIFF header: %u bytes", val_32);
706 /* TODO - Continue parsing the "1th" IFD */
708 proto_tree_add_text(subtree, tvb, offset, -1, "Remainder of APP1 marker skipped");
710 proto_item_append_text(ti, " (Unknown identifier)");
714 /* Process an APP2 block.
716 * XXX - This code only works on US-ASCII systems!!!
719 process_app2_segment(proto_tree *tree, tvbuff_t *tvb, guint32 len,
720 guint16 marker, const char *marker_name)
722 proto_item *ti = NULL;
723 proto_tree *subtree = NULL;
724 proto_tree *subtree_details = NULL;
731 ti = proto_tree_add_item(tree, hf_marker_segment,
733 subtree = proto_item_add_subtree(ti, ett_marker_segment);
735 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
736 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
738 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, FALSE);
740 str = tvb_get_stringz(tvb, 4, &str_size);
741 ti = proto_tree_add_item(subtree, hf_identifier, tvb, 4, str_size, FALSE);
742 if (strcmp(str, "FPXR") == 0) {
743 proto_tree_add_text(tree, tvb, 0, -1, "Exif FlashPix APP2 application marker");
745 proto_item_append_text(ti, " (Unknown identifier)");
750 dissect_jfif(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
752 proto_tree *subtree = NULL;
754 guint tvb_len = tvb_reported_length(tvb);
760 /* Add summary to INFO column if it is enabled */
761 if (check_col(pinfo->cinfo, COL_INFO))
762 col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "(JPEG JFIF image)");
765 ti = proto_tree_add_item(tree, proto_jfif,
767 subtree = proto_item_add_subtree(ti, ett_jfif);
770 marker = tvb_get_ntohs(tvb, 0);
771 if (marker != MARKER_SOI) {
773 proto_tree_add_text(subtree, tvb, 0, 2, ErrorInvalidJFIF);
778 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, FALSE);
783 * Process the remaining markers and marker segments
785 while (offset < tvb_len) {
787 marker = tvb_get_ntohs(tvb, offset);
788 str = match_strval(marker, vals_marker);
789 if (str) { /* Known marker */
790 if (marker_has_length(marker)) { /* Marker segment */
791 /* Length of marker segment = 2 + len */
792 len = tvb_get_ntohs(tvb, offset + 2);
793 tmp_tvb = tvb_new_subset(tvb, offset, 2 + len, 2 + len);
796 process_app0_segment(subtree, tmp_tvb, len, marker, str);
799 process_app1_segment(subtree, tmp_tvb, len, marker, str);
802 process_app2_segment(subtree, tmp_tvb, len, marker, str);
818 process_sof_header(subtree, tmp_tvb, len, marker, str);
821 process_sos_header(subtree, tmp_tvb, len, marker, str);
823 * TODO - dissect a scan; the scan data is encoded.
825 proto_tree_add_text(subtree, tvb, offset + 2 + len, -1,
826 "JFIF dissection stops here (dissection of a scan is not yet implemented)");
830 process_marker_segment(subtree, tmp_tvb, len, marker, str);
834 } else { /* Marker but no segment */
836 proto_tree_add_item(subtree, hf_marker,
837 tvb, offset, 2, FALSE);
840 } else { /* Reserved! */
841 ti = proto_tree_add_item(subtree, hf_marker,
842 tvb, offset, 2, FALSE);
843 proto_item_append_text(ti, " (Reserved)");
852 /****************** Register the protocol with Ethereal ******************/
855 /* This format is required because a script is used to build the C function
856 * that calls the protocol registration. */
859 proto_register_jfif(void)
862 * Setup list of header fields.
864 static hf_register_info hf[] = {
869 FT_UINT8, BASE_HEX, VALS(vals_marker), 0x00,
875 { &hf_marker_segment,
877 IMG_JFIF "marker_segment",
878 FT_NONE, BASE_NONE, NULL, 0x00,
886 FT_UINT16, BASE_DEC, 0, 0x00,
887 "Length of segment (including length field)",
894 IMG_JFIF ".identifier",
895 FT_STRINGZ, BASE_NONE, NULL, 0x00,
896 "Identifier of the segment",
900 /* MARKER_APP0 - JFIF */
904 FT_NONE, BASE_NONE, NULL, 0x00,
911 IMG_JFIF ".version.major",
912 FT_UINT8, BASE_DEC, NULL, 0x00,
913 "JFIF Major Version",
919 IMG_JFIF ".version.minor",
920 FT_UINT8, BASE_DEC, NULL, 0x00,
921 "JFIF Minor Version",
928 FT_UINT8, BASE_DEC, VALS(vals_units), 0x00,
929 "Units used in this segment",
935 IMG_JFIF ".Xdensity",
936 FT_UINT16, BASE_DEC, NULL, 0x00,
937 "Horizontal pixel density",
943 IMG_JFIF ".Ydensity",
944 FT_UINT16, BASE_DEC, NULL, 0x00,
945 "Vertical pixel density",
951 IMG_JFIF ".Xthumbnail",
952 FT_UINT16, BASE_DEC, NULL, 0x00,
953 "Thumbnail horizontal pixel count",
959 IMG_JFIF ".Ythumbnail",
960 FT_UINT16, BASE_DEC, NULL, 0x00,
961 "Thumbnail vertical pixel count",
966 { "RGB values of thumbnail pixels",
968 FT_BYTES, BASE_NONE, NULL, 0x00,
969 "RGB values of the thumbnail pixels (24 bit per pixel, Xthumbnail x Ythumbnail pixels)",
973 /* MARKER_APP0 - JFXX */
974 { &hf_extension_code,
976 IMG_JFIF ".extension.code",
977 FT_UINT8, BASE_HEX, VALS(vals_extension_code), 0x00,
978 "JFXX extension code for thumbnail encoding",
982 /* Header: Start of Frame (MARKER_SOF) */
984 { "Start of Frame header",
986 FT_NONE, BASE_NONE, NULL, 0x00,
987 "Start of Frame header",
992 { "Sample Precision (bits)",
993 IMG_JFIF ".sof.precision",
994 FT_UINT8, BASE_DEC, NULL, 0x00,
995 "Specifies the precision in bits for the samples of the components in the frame.",
1001 IMG_JFIF ".sof.lines",
1002 FT_UINT16, BASE_DEC, NULL, 0x00,
1003 "Specifies the maximum number of lines in the source image.",
1007 { &hf_sof_samples_per_line,
1008 { "Samples per line",
1009 IMG_JFIF ".sof.samples_per_line",
1010 FT_UINT16, BASE_DEC, NULL, 0x00,
1011 "Specifies the maximum number of samples per line in the source image.",
1016 { "Number of image components in frame",
1018 FT_UINT8, BASE_DEC, NULL, 0x00,
1019 "Specifies the number of source image components in the frame.",
1024 { "Component identifier",
1025 IMG_JFIF ".sof.c_i",
1026 FT_UINT8, BASE_DEC, NULL, 0x00,
1027 "Assigns a unique label to the ith component in the sequence "
1028 "of frame component specification parameters.",
1033 { "Horizontal sampling factor",
1034 IMG_JFIF ".sof.h_i",
1035 FT_UINT8, BASE_DEC, NULL, 0xF0,
1036 "Specifies the relationship between the component horizontal "
1037 "dimension and maximum image dimension X.",
1042 { "Vertical sampling factor",
1043 IMG_JFIF ".sof.v_i",
1044 FT_UINT8, BASE_DEC, NULL, 0x0F,
1045 "Specifies the relationship between the component vertical "
1046 "dimension and maximum image dimension Y.",
1051 { "Quantization table destination selector",
1052 IMG_JFIF ".sof.tq_i",
1053 FT_UINT8, BASE_DEC, NULL, 0x00,
1054 "Specifies one of four possible quantization table "
1055 "destinations from which the quantization table to use "
1056 "for dequantization of DCT coefficients of component Ci "
1062 /* Header: Start of Segment (MARKER_SOS) */
1064 { "Start of Segment header",
1065 IMG_JFIF ".header.sos",
1066 FT_NONE, BASE_NONE, NULL, 0x00,
1067 "Start of Segment header",
1072 { "Number of image components in scan",
1074 FT_UINT8, BASE_DEC, NULL, 0x00,
1075 "Specifies the number of source image components in the scan.",
1080 { "Scan component selector",
1081 IMG_JFIF ".sos.component_selector",
1082 FT_UINT8, BASE_DEC, NULL, 0x00,
1083 "Selects which of the Nf image components specified in the "
1084 "frame parameters shall be the jth component in the scan.",
1089 { "DC entropy coding table destination selector",
1090 IMG_JFIF ".sos.dc_entropy_selector",
1091 FT_UINT8, BASE_DEC, NULL, 0xF0,
1092 "Specifies one of four possible DC entropy coding table "
1093 "destinations from which the entropy table needed "
1094 "for decoding of the DC coefficients of component Csj "
1100 { "AC entropy coding table destination selector",
1101 IMG_JFIF ".sos.ac_entropy_selector",
1102 FT_UINT8, BASE_DEC, NULL, 0x0F,
1103 "Specifies one of four possible AC entropy coding table "
1104 "destinations from which the entropy table needed "
1105 "for decoding of the AC coefficients of component Csj "
1111 { "Start of spectral or predictor selection",
1113 FT_UINT8, BASE_DEC, NULL, 0x00,
1114 "In the DCT modes of operation, this parameter specifies the "
1115 "first DCT coefficient in each block in zig-zag order which "
1116 "shall be coded in the scan. This parameter shall be set to "
1117 "zero for the sequential DCT processes. "
1118 "In the lossless mode of operations this parameter is used "
1119 "to select the predictor.",
1124 { "End of spectral selection",
1126 FT_UINT8, BASE_DEC, NULL, 0x00,
1127 "Specifies the last DCT coefficient in each block in zig-zag "
1128 "order which shall be coded in the scan. This parameter shall "
1129 "be set to 63 for the sequential DCT processes. "
1130 "In the lossless mode of operations this parameter has no "
1131 "meaning. It shall be set to zero.",
1136 { "Successive approximation bit position high",
1138 FT_UINT8, BASE_DEC, NULL, 0xF0,
1139 "This parameter specifies the point transform used in the "
1140 "preceding scan (i.e. successive approximation bit position "
1141 "low in the preceding scan) for the band of coefficients "
1142 "specified by Ss and Se. This parameter shall be set to zero "
1143 "for the first scan of each band of coefficients. "
1144 "In the lossless mode of operations this parameter has no "
1145 "meaning. It shall be set to zero.",
1150 { "Successive approximation bit position low or point transform",
1152 FT_UINT8, BASE_DEC, NULL, 0x0F,
1153 "In the DCT modes of operation this parameter specifies the "
1154 "point transform, i.e. bit position low, used before coding "
1155 "the band of coefficients specified by Ss and Se. "
1156 "This parameter shall be set to zero for the sequential DCT "
1157 "processes. In the lossless mode of operations, this "
1158 "parameter specifies the point transform, Pt.",
1164 /* Setup protocol subtree array */
1165 static gint *ett[] = {
1167 &ett_marker_segment,
1171 /* Register the protocol name and description */
1172 proto_jfif = proto_register_protocol(
1173 "JPEG File Interchange Format",
1174 "JFIF (JPEG) image",
1178 /* Required function calls to register the header fields
1179 * and subtrees used */
1180 proto_register_field_array(proto_jfif, hf, array_length(hf));
1181 proto_register_subtree_array(ett, array_length(ett));
1183 register_dissector("image-jfif", dissect_jfif, proto_jfif);
1188 proto_reg_handoff_jfif(void)
1190 dissector_handle_t jfif_handle;
1192 jfif_handle = create_dissector_handle(dissect_jfif, proto_jfif);
1194 /* Register the GIF media type */
1195 dissector_add_string("media_type", "image/jfif", jfif_handle);
1196 dissector_add_string("media_type", "image/jpg", jfif_handle);
1197 dissector_add_string("media_type", "image/jpeg", jfif_handle);