Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / file-jpeg.c
blob27712e323a3fd761579a3e6908883acc1d4947eb
1 /* file-jpeg.c
3 * Routines for JFIF image/jpeg media dissection
4 * Copyright 2004, Olivier Biot.
6 * Refer to the AUTHORS file or the AUTHORS section in the man page
7 * for contacting the author(s) of this file.
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * JFIF media decoding functionality provided by Olivier Biot.
15 * The JFIF specifications are found at several locations, such as:
16 * https://www.w3.org/Graphics/JPEG/jfif3.pdf
17 * https://www.w3.org/Graphics/JPEG/itu-t81.pdf
19 * The Exif specifications are found at several locations, such as:
20 * http://www.exif.org/
22 * SPDX-License-Identifier: GPL-2.0-or-later
25 #include "config.h"
27 #include <epan/packet.h>
28 #include <epan/expert.h>
29 #include <epan/unit_strings.h>
30 #include <wsutil/array.h>
31 #include <wiretap/wtap.h>
33 void proto_register_jfif(void);
34 void proto_reg_handoff_jfif(void);
36 /* General-purpose debug logger.
37 * Requires double parentheses because of variable arguments of printf().
39 * Enable debug logging for JFIF by defining AM_CFLAGS
40 * so that it contains "-DDEBUG_image_jfif" or "-DDEBUG_image"
42 #if (defined(DEBUG_image_jfif) || defined(DEBUG_image))
43 #define DebugLog(x) \
44 g_print("%s:%u: ", __FILE__, __LINE__); \
45 g_print x
46 #else
47 #define DebugLog(x) ;
48 #endif
50 /************************** Variable declarations **************************/
52 #define MARKER_TEM 0xFF01
54 /* 0xFF02 -- 0xFFBF are reserved */
56 #define MARKER_SOF0 0xFFC0
57 #define MARKER_SOF1 0xFFC1
58 #define MARKER_SOF2 0xFFC2
59 #define MARKER_SOF3 0xFFC3
61 #define MARKER_DHT 0xFFC4
63 #define MARKER_SOF5 0xFFC5
64 #define MARKER_SOF6 0xFFC6
65 #define MARKER_SOF7 0xFFC7
66 #define MARKER_SOF8 0xFFC8
67 #define MARKER_SOF9 0xFFC9
68 #define MARKER_SOF10 0xFFCA
69 #define MARKER_SOF11 0xFFCB
71 #define MARKER_DAC 0xFFCC
73 #define MARKER_SOF13 0xFFCD
74 #define MARKER_SOF14 0xFFCE
75 #define MARKER_SOF15 0xFFCF
77 #define MARKER_RST0 0xFFD0
78 #define MARKER_RST1 0xFFD1
79 #define MARKER_RST2 0xFFD2
80 #define MARKER_RST3 0xFFD3
81 #define MARKER_RST4 0xFFD4
82 #define MARKER_RST5 0xFFD5
83 #define MARKER_RST6 0xFFD6
84 #define MARKER_RST7 0xFFD7
86 #define MARKER_SOI 0xFFD8
87 #define MARKER_EOI 0xFFD9
88 #define MARKER_SOS 0xFFDA
89 #define MARKER_DQT 0xFFDB
90 #define MARKER_DNL 0xFFDC
91 #define MARKER_DRI 0xFFDD
92 #define MARKER_DHP 0xFFDE
93 #define MARKER_EXP 0xFFDF
95 #define MARKER_APP0 0xFFE0
96 #define MARKER_APP1 0xFFE1
97 #define MARKER_APP2 0xFFE2
98 #define MARKER_APP3 0xFFE3
99 #define MARKER_APP4 0xFFE4
100 #define MARKER_APP5 0xFFE5
101 #define MARKER_APP6 0xFFE6
102 #define MARKER_APP7 0xFFE7
103 #define MARKER_APP8 0xFFE8
104 #define MARKER_APP9 0xFFE9
105 #define MARKER_APP10 0xFFEA
106 #define MARKER_APP11 0xFFEB
107 #define MARKER_APP12 0xFFEC
108 #define MARKER_APP13 0xFFED
109 #define MARKER_APP14 0xFFEE
110 #define MARKER_APP15 0xFFEF
112 #define MARKER_JPG0 0xFFF0
113 #define MARKER_JPG1 0xFFF1
114 #define MARKER_JPG2 0xFFF2
115 #define MARKER_JPG3 0xFFF3
116 #define MARKER_JPG4 0xFFF4
117 #define MARKER_JPG5 0xFFF5
118 #define MARKER_JPG6 0xFFF6
119 #define MARKER_JPG7 0xFFF7
120 #define MARKER_JPG8 0xFFF8
121 #define MARKER_JPG9 0xFFF9
122 #define MARKER_JPG10 0xFFFA
123 #define MARKER_JPG11 0xFFFB
124 #define MARKER_JPG12 0xFFFC
125 #define MARKER_JPG13 0xFFFD
127 #define MARKER_COM 0xFFFE
129 #define marker_has_length(marker) ( ! ( \
130 ((marker) == MARKER_TEM) \
131 || ((marker) == MARKER_SOI) \
132 || ((marker) == MARKER_EOI) \
133 || ( ((marker) >= MARKER_RST0) && ((marker) <= MARKER_RST7) ) \
137 static const value_string vals_marker[] = {
138 { MARKER_TEM, "Reserved - For temporary private use in arithmetic coding" },
139 { MARKER_SOF0, "Start of Frame (non-differential, Huffman coding) - Baseline DCT" },
140 { MARKER_SOF1, "Start of Frame (non-differential, Huffman coding) - Extended sequential DCT" },
141 { MARKER_SOF2, "Start of Frame (non-differential, Huffman coding) - Progressive DCT" },
142 { MARKER_SOF3, "Start of Frame (non-differential, Huffman coding) - Lossless (sequential)" },
143 { MARKER_DHT, "Define Huffman table(s)" },
144 { MARKER_SOF5, "Start of Frame (differential, Huffman coding) - Differential sequential DCT" },
145 { MARKER_SOF6, "Start of Frame (differential, Huffman coding) - Differential progressive DCT" },
146 { MARKER_SOF7, "Start of Frame (differential, Huffman coding) - Differential lossless (sequential)" },
147 { MARKER_SOF8, "Start of Frame (non-differential, arithmetic coding) - Reserved for JPEG extensions" },
148 { MARKER_SOF9, "Start of Frame (non-differential, arithmetic coding) - Extended sequential DCT" },
149 { MARKER_SOF10, "Start of Frame (non-differential, arithmetic coding) - Progressive DCT" },
150 { MARKER_SOF11, "Start of Frame (non-differential, arithmetic coding) - Lossless (sequential)" },
151 { MARKER_DAC, "Define arithmetic coding conditioning(s)" },
152 { MARKER_SOF13, "Start of Frame (differential, arithmetic coding) - Differential sequential DCT" },
153 { MARKER_SOF14, "Start of Frame (differential, arithmetic coding) - Differential progressive DCT" },
154 { MARKER_SOF15, "Start of Frame (differential, arithmetic coding) - Differential lossless (sequential)" },
155 { MARKER_RST0, "Restart interval termination - Restart with modulo 8 count 0" },
156 { MARKER_RST1, "Restart interval termination - Restart with modulo 8 count 1" },
157 { MARKER_RST2, "Restart interval termination - Restart with modulo 8 count 2" },
158 { MARKER_RST3, "Restart interval termination - Restart with modulo 8 count 3" },
159 { MARKER_RST4, "Restart interval termination - Restart with modulo 8 count 4" },
160 { MARKER_RST5, "Restart interval termination - Restart with modulo 8 count 5" },
161 { MARKER_RST6, "Restart interval termination - Restart with modulo 8 count 6" },
162 { MARKER_RST7, "Restart interval termination - Restart with modulo 8 count 7" },
163 { MARKER_SOI, "Start of Image" },
164 { MARKER_EOI, "End of Image" },
165 { MARKER_SOS, "Start of Scan" },
166 { MARKER_DQT, "Define quantization table(s)" },
167 { MARKER_DNL, "Define number of lines" },
168 { MARKER_DRI, "Define restart interval" },
169 { MARKER_DHP, "Define hierarchical progression" },
170 { MARKER_EXP, "Expand reference component(s)" },
171 { MARKER_APP0, "Reserved for application segments - 0" },
172 { MARKER_APP1, "Reserved for application segments - 1" },
173 { MARKER_APP2, "Reserved for application segments - 2" },
174 { MARKER_APP3, "Reserved for application segments - 3" },
175 { MARKER_APP4, "Reserved for application segments - 4" },
176 { MARKER_APP5, "Reserved for application segments - 5" },
177 { MARKER_APP6, "Reserved for application segments - 6" },
178 { MARKER_APP7, "Reserved for application segments - 7" },
179 { MARKER_APP8, "Reserved for application segments - 8" },
180 { MARKER_APP9, "Reserved for application segments - 9" },
181 { MARKER_APP10, "Reserved for application segments - 10" },
182 { MARKER_APP11, "Reserved for application segments - 11" },
183 { MARKER_APP12, "Reserved for application segments - 12" },
184 { MARKER_APP13, "Reserved for application segments - 13" },
185 { MARKER_APP14, "Reserved for application segments - 14" },
186 { MARKER_APP15, "Reserved for application segments - 15" },
187 { MARKER_JPG0, "Reserved for JPEG extensions - 0" },
188 { MARKER_JPG1, "Reserved for JPEG extensions - 1" },
189 { MARKER_JPG2, "Reserved for JPEG extensions - 2" },
190 { MARKER_JPG3, "Reserved for JPEG extensions - 3" },
191 { MARKER_JPG4, "Reserved for JPEG extensions - 4" },
192 { MARKER_JPG5, "Reserved for JPEG extensions - 5" },
193 { MARKER_JPG6, "Reserved for JPEG extensions - 6" },
194 { MARKER_JPG7, "Reserved for JPEG extensions - 7" },
195 { MARKER_JPG8, "Reserved for JPEG extensions - 8" },
196 { MARKER_JPG9, "Reserved for JPEG extensions - 9" },
197 { MARKER_JPG10, "Reserved for JPEG extensions - 10" },
198 { MARKER_JPG11, "Reserved for JPEG extensions - 11" },
199 { MARKER_JPG12, "Reserved for JPEG extensions - 12" },
200 { MARKER_JPG13, "Reserved for JPEG extensions - 13" },
201 { MARKER_COM, "Comment" },
202 { 0x00, NULL }
205 static const value_string vals_units[] = {
206 { 0, "No units; Xdensity and Ydensity specify the pixel aspect ratio" },
207 { 1, "Dots per inch" },
208 { 2, "Dots per centimeter" },
209 { 0x00, NULL }
212 static const value_string vals_extension_code[] = {
213 { 0x10, "Thumbnail encoded using JPEG" },
214 { 0x11, "Thumbnail encoded using 1 byte (8 bits) per pixel" },
215 { 0x13, "Thumbnail encoded using 3 bytes (24 bits) per pixel" },
216 { 0x00, NULL }
219 enum {
220 EXIF_TAG_EXIF_IFD_POINTER = 0x8769,
221 EXIF_TAG_GPS_IFD_POINTER = 0x8825,
222 EXIF_TAG_INTEROP_IFD_POINTER = 0xA005,
225 static const value_string vals_ifd_tags[] = {
227 * Tags related to image data structure:
229 { 0x0100, "ImageWidth" },
230 { 0x0101, "ImageLength" },
231 { 0x0102, "BitsPerSample" },
232 { 0x0103, "Compression" },
233 { 0x0106, "PhotometricInterpretation" },
234 { 0x0112, "Orientation" },
235 { 0x0115, "SamplesPerPixel" },
236 { 0x011C, "PlanarConfiguration" },
237 { 0x0212, "YCbCrSubSampling" },
238 { 0x0213, "YCbCrPositioning" },
239 { 0x011A, "XResolution" },
240 { 0x011B, "YResolution" },
241 { 0x0128, "ResolutionUnit" },
243 * Tags relating to recording offset:
245 { 0x0111, "StripOffsets" },
246 { 0x0116, "RowsPerStrip" },
247 { 0x0117, "StripByteCounts" },
248 { 0x0201, "JPEGInterchangeFormat" },
249 { 0x0202, "JPEGInterchangeFormatLength" },
251 * Tags relating to image data characteristics:
253 { 0x012D, "TransferFunction" },
254 { 0x013E, "WhitePoint" },
255 { 0x013F, "PrimaryChromaticities" },
256 { 0x0211, "YCbCrCoefficients" },
257 { 0x0214, "ReferenceBlackWhite" },
259 * Other tags:
261 { 0x0132, "DateTime" },
262 { 0x010E, "ImageDescription" },
263 { 0x010F, "Make" },
264 { 0x0110, "Model" },
265 { 0x0131, "Software" },
266 { 0x013B, "Artist" },
267 { 0x8298, "Copyright" },
269 * Exif-specific IFD:
271 { EXIF_TAG_EXIF_IFD_POINTER, "Exif IFD Pointer"},
272 { EXIF_TAG_GPS_IFD_POINTER, "GPS IFD Pointer"},
273 { EXIF_TAG_INTEROP_IFD_POINTER, "Interoperability IFD Pointer"},
275 { 0x0000, NULL }
278 static const value_string vals_ifd_tags_exif[] = {
280 * Tags relating to version:
282 { 0x9000, "ExifVersion" },
283 { 0xA000, "FlashpixVersion" },
285 * Tags relating to image data characteristics:
287 { 0xA001, "ColorSpace" },
288 { 0xA500, "Gamma" },
290 * Tags relating to image configuration:
292 { 0x9101, "ComponentsConfiguration" },
293 { 0x9102, "CompressedBitsPerPixel" },
294 { 0xA002, "PixelXDimension" },
295 { 0xA003, "PixelYDimension" },
297 * Tags relating to user information:
299 { 0x927C, "MakerNote" },
300 { 0x9286, "UserComment" },
302 * Tags relating to related file information:
304 { 0xA004, "RelatedSoundFile" },
306 * Tags relating to date and time:
308 { 0x9003, "DateTimeOriginal" },
309 { 0x9004, "DateTimeDigitized" },
310 { 0x9010, "OffsetTime" },
311 { 0x9011, "OffsetTimeOriginal" },
312 { 0x9012, "OffsetTimeDigitized" },
313 { 0x9290, "SubSecTime" },
314 { 0x9291, "SubSecTimeOriginal" },
315 { 0x9292, "SubSecTimeDigitized" },
317 * Tags relating to picture-taking conditions:
319 { 0x829A, "ExposureTime" },
320 { 0x829D, "FNumber" },
321 { 0x8822, "ExposureProgram" },
322 { 0x8824, "SpectralSensitivity" },
323 { 0x8827, "PhotographicSensitivity" },
324 { 0x8828, "OECF" },
325 { 0x8830, "SensitivityType" },
326 { 0x8831, "StandardOutputSensitivity" },
327 { 0x8832, "RecommendedExposureIndex" },
328 { 0x8833, "ISOSpeed" },
329 { 0x8834, "ISOSpeedLatitudeyyy" },
330 { 0x8835, "ISOSpeedLatitudezzz" },
331 { 0x9201, "ShutterSpeedValue" },
332 { 0x9202, "ApertureValue" },
333 { 0x9203, "BrightnessValue" },
334 { 0x9204, "ExposureBiasValue" },
335 { 0x9205, "MaxApertureValue" },
336 { 0x9206, "SubjectDistance" },
337 { 0x9207, "MeteringMode" },
338 { 0x9208, "LightSource" },
339 { 0x9209, "Flash" },
340 { 0x920A, "FocalLength" },
341 { 0x9214, "SubjectArea" },
342 { 0xA20B, "FlashEnergy" },
343 { 0xA20C, "SpatialFrequencyResponse" },
344 { 0xA20E, "FocalPlaneXResolution" },
345 { 0xA20F, "FocalPlaneYResolution" },
346 { 0xA210, "FocalPlaneResolutionUnit" },
347 { 0xA214, "SubjectLocation" },
348 { 0xA215, "ExposureIndex" },
349 { 0xA217, "SensingMethod" },
350 { 0xA300, "FileSource" },
351 { 0xA301, "SceneType" },
352 { 0xA302, "CFAPattern" },
353 { 0xA401, "CustomRendered" },
354 { 0xA402, "ExposureMode" },
355 { 0xA403, "WhiteBalance" },
356 { 0xA404, "DigitalZoomRatio" },
357 { 0xA405, "FocalLengthIn35mmFilm" },
358 { 0xA406, "SceneCaptureType" },
359 { 0xA407, "GainControl" },
360 { 0xA408, "Contrast" },
361 { 0xA409, "Saturation" },
362 { 0xA40A, "Sharpness" },
363 { 0xA40B, "DeviceSettingDescription" },
364 { 0xA40C, "SubjectDistanceRange" },
365 { 0xA460, "CompositeImage" },
366 { 0xA461, "SourceImageNumberOfCompositeImage" },
367 { 0xA462, "SourceExposureTimesOfCompositeImage" },
369 * Tags relating to shooting situation:
371 { 0x9400, "Temperature" },
372 { 0x9401, "Humidity" },
373 { 0x9402, "Pressure" },
374 { 0x9403, "WaterDepth" },
375 { 0x9404, "Acceleration" },
376 { 0x9405, "CameraElevationAngle" },
378 * Other tags:
380 { 0xA420, "ImageUniqueID" },
381 { 0xA430, "CameraOwnerName" },
382 { 0xA431, "BodySerialNumber" },
383 { 0xA432, "LensSpecification" },
384 { 0xA433, "LensMake" },
385 { 0xA434, "LensModel" },
386 { 0xA435, "LensSerialNumber" },
388 { 0x0000, NULL }
391 static const value_string vals_ifd_tags_gps[] = {
393 * Tags relating to GPS:
395 { 0x00, "GPSVersionID" },
396 { 0x01, "GPSLatitudeRef" },
397 { 0x02, "GPSLatitude" },
398 { 0x03, "GPSLongitudeRef" },
399 { 0x04, "GPSLongitude" },
400 { 0x05, "GPSAltitudeRef" },
401 { 0x06, "GPSAltitude" },
402 { 0x07, "GPSTimeStamp" },
403 { 0x08, "GPSSatellites" },
404 { 0x09, "GPSStatus" },
405 { 0x0A, "GPSMeasureMode" },
406 { 0x0B, "GPSDOP" },
407 { 0x0C, "GPSSpeedRef" },
408 { 0x0D, "GPSSpeed" },
409 { 0x0E, "GPSTrackRef" },
410 { 0x0F, "GPSTrack" },
411 { 0x10, "GPSImgDirectionRef" },
412 { 0x11, "GPSImgDirection" },
413 { 0x12, "GPSMapDatum" },
414 { 0x13, "GPSDestLatitudeRef" },
415 { 0x14, "GPSDestLatitude" },
416 { 0x15, "GPSDestLongitudeRef" },
417 { 0x16, "GPSDestLongitude" },
418 { 0x17, "GPSDestBearingRef" },
419 { 0x18, "GPSDestBearing" },
420 { 0x19, "GPSDestDistanceRef" },
421 { 0x1A, "GPSDestDistance" },
422 { 0x1B, "GPSProcessingMethod" },
423 { 0x1C, "GPSAreaInformation" },
424 { 0x1D, "GPSDateStamp" },
425 { 0x1E, "GPSDifferential" },
426 { 0x1F, "GPSHPositioningError" },
427 { 0x00, NULL }
430 static const value_string vals_ifd_tags_interop[] = {
432 * Tags relating to interoperability:
434 { 0x1, "InteroperabilityIndex" },
435 { 0x0, NULL }
438 enum {
439 EXIF_TYPE_BYTE = 0x0001,
440 EXIF_TYPE_ASCII = 0x0002,
441 EXIF_TYPE_SHORT = 0x0003,
442 EXIF_TYPE_LONG = 0x0004,
443 EXIF_TYPE_RATIONAL = 0x0005,
444 /* 0x0006 */
445 EXIF_TYPE_UNDEFINED = 0x0007,
446 /* 0x0008 */
447 EXIF_TYPE_SLONG = 0x0009,
448 EXIF_TYPE_SRATIONAL = 0x000A,
451 static const value_string vals_exif_types[] = {
452 { EXIF_TYPE_BYTE, "BYTE" },
453 { EXIF_TYPE_ASCII, "ASCII" },
454 { EXIF_TYPE_SHORT, "SHORT" },
455 { EXIF_TYPE_LONG, "LONG" },
456 { EXIF_TYPE_RATIONAL, "RATIONAL" },
457 { EXIF_TYPE_UNDEFINED, "UNDEFINED" },
458 { EXIF_TYPE_SLONG, "SLONG" },
459 { EXIF_TYPE_SRATIONAL, "SRATIONAL" },
461 { 0x0000, NULL }
464 /* Initialize the protocol and registered fields */
465 static int proto_jfif;
467 /* Marker */
468 static int hf_marker;
469 /* Marker segment */
470 static int hf_marker_segment;
471 static int hf_len;
472 /* MARKER_APP0 */
473 static int hf_identifier;
474 /* MARKER_APP0 - JFIF */
475 static int hf_version;
476 static int hf_version_major;
477 static int hf_version_minor;
478 static int hf_units;
479 static int hf_xdensity;
480 static int hf_ydensity;
481 static int hf_xthumbnail;
482 static int hf_ythumbnail;
483 static int hf_rgb;
484 /* MARKER_APP0 - JFXX */
485 static int hf_extension_code;
486 /* start of Frame */
487 static int hf_sof_header;
488 static int hf_sof_precision;
489 static int hf_sof_lines;
490 static int hf_sof_samples_per_line;
491 static int hf_sof_nf;
492 static int hf_sof_c_i;
493 static int hf_sof_h_i;
494 static int hf_sof_v_i;
495 static int hf_sof_tq_i;
497 /* Start of Scan */
498 static int hf_sos_header;
499 static int hf_sos_ns;
500 static int hf_sos_cs_j;
501 static int hf_sos_td_j;
502 static int hf_sos_ta_j;
503 static int hf_sos_ss;
504 static int hf_sos_se;
505 static int hf_sos_ah;
506 static int hf_sos_al;
508 /* Comment */
509 static int hf_comment_header;
510 static int hf_comment;
512 static int hf_remain_seg_data;
513 static int hf_endianness;
514 static int hf_start_ifd_offset;
515 static int hf_next_ifd_offset;
516 static int hf_exif_flashpix_marker;
517 static int hf_entropy_coded_segment;
518 static int hf_fill_bytes;
519 static int hf_skipped_tiff_data;
520 static int hf_ifd_num_fields;
521 static int hf_ifd_tag;
522 static int hf_ifd_tag_exif;
523 static int hf_ifd_tag_gps;
524 static int hf_ifd_tag_interop;
525 static int hf_ifd_type;
526 static int hf_ifd_count;
527 static int hf_ifd_offset;
528 static int hf_ifd_value_byte;
529 static int hf_ifd_value_ascii;
530 static int hf_ifd_value_short;
531 static int hf_ifd_value_long;
532 static int hf_ifd_value_rational;
533 static int hf_ifd_value_rational_numerator;
534 static int hf_ifd_value_rational_denominator;
535 static int hf_ifd_value_undefined;
536 static int hf_ifd_value_slong;
537 static int hf_ifd_value_srational;
538 static int hf_ifd_value_srational_numerator;
539 static int hf_ifd_value_srational_denominator;
542 /* Initialize the subtree pointers */
543 static int ett_jfif;
544 static int ett_marker_segment;
545 static int ett_details;
546 static int ett_ifd;
547 static int ett_rational;
548 static int ett_srational;
550 static expert_field ei_file_jpeg_first_identifier_not_jfif;
551 static expert_field ei_start_ifd_offset;
552 static expert_field ei_next_ifd_offset;
553 static expert_field ei_ifd_value_offset;
555 /****************** JFIF protocol dissection functions ******************/
559 * Process a marker segment (with length).
561 static void
562 process_marker_segment(proto_tree *tree, tvbuff_t *tvb, uint32_t len,
563 uint16_t marker, const char *marker_name)
565 proto_item *ti;
566 proto_tree *subtree;
568 if (!tree)
569 return;
571 ti = proto_tree_add_item(tree, hf_marker_segment,
572 tvb, 0, -1, ENC_NA);
573 subtree = proto_item_add_subtree(ti, ett_marker_segment);
575 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
576 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
578 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
580 proto_tree_add_bytes_format_value(subtree, hf_remain_seg_data, tvb, 4, -1, NULL, "%u bytes", len - 2);
584 * Process a Start of Frame header (with length).
586 static void
587 process_sof_header(proto_tree *tree, tvbuff_t *tvb, uint32_t len _U_,
588 uint16_t marker, const char *marker_name)
590 proto_item *ti;
591 proto_tree *subtree;
592 uint8_t count;
593 uint32_t offset;
595 if (!tree)
596 return;
598 ti = proto_tree_add_item(tree, hf_sof_header,
599 tvb, 0, -1, ENC_NA);
600 subtree = proto_item_add_subtree(ti, ett_marker_segment);
602 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
603 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
605 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
607 proto_tree_add_item(subtree, hf_sof_precision, tvb, 4, 1, ENC_BIG_ENDIAN);
609 proto_tree_add_item(subtree, hf_sof_lines, tvb, 5, 2, ENC_BIG_ENDIAN);
611 proto_tree_add_item(subtree, hf_sof_samples_per_line, tvb, 7, 2, ENC_BIG_ENDIAN);
613 proto_tree_add_item(subtree, hf_sof_nf, tvb, 9, 1, ENC_BIG_ENDIAN);
614 count = tvb_get_uint8(tvb, 9);
615 offset = 10;
616 while (count > 0) {
617 proto_tree_add_item(subtree, hf_sof_c_i, tvb, offset++, 1, ENC_BIG_ENDIAN);
618 proto_tree_add_item(subtree, hf_sof_h_i, tvb, offset, 1, ENC_BIG_ENDIAN);
619 proto_tree_add_item(subtree, hf_sof_v_i, tvb, offset++, 1, ENC_BIG_ENDIAN);
620 proto_tree_add_item(subtree, hf_sof_tq_i, tvb, offset++, 1, ENC_BIG_ENDIAN);
621 count--;
626 * Process a Start of Segment header (with length).
628 static void
629 process_sos_header(proto_tree *tree, tvbuff_t *tvb, uint32_t len _U_,
630 uint16_t marker, const char *marker_name)
632 proto_item *ti;
633 proto_tree *subtree;
634 uint8_t count;
635 uint32_t offset;
637 if (!tree)
638 return;
640 ti = proto_tree_add_item(tree, hf_sos_header,
641 tvb, 0, -1, ENC_NA);
642 subtree = proto_item_add_subtree(ti, ett_marker_segment);
644 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
645 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
647 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
649 proto_tree_add_item(subtree, hf_sos_ns, tvb, 4, 1, ENC_BIG_ENDIAN);
650 count = tvb_get_uint8(tvb, 4);
651 offset = 5;
652 while (count > 0) {
653 proto_tree_add_item(subtree, hf_sos_cs_j, tvb, offset++, 1, ENC_BIG_ENDIAN);
654 proto_tree_add_item(subtree, hf_sos_td_j, tvb, offset, 1, ENC_BIG_ENDIAN);
655 proto_tree_add_item(subtree, hf_sos_ta_j, tvb, offset++, 1, ENC_BIG_ENDIAN);
656 count--;
659 proto_tree_add_item(subtree, hf_sos_ss, tvb, offset++, 1, ENC_BIG_ENDIAN);
660 proto_tree_add_item(subtree, hf_sos_se, tvb, offset++, 1, ENC_BIG_ENDIAN);
662 proto_tree_add_item(subtree, hf_sos_ah, tvb, offset, 1, ENC_BIG_ENDIAN);
663 proto_tree_add_item(subtree, hf_sos_al, tvb, offset, 1, ENC_BIG_ENDIAN);
664 /* offset ++ */;
668 * Process a Comment header (with length).
670 static void
671 process_comment_header(proto_tree *tree, tvbuff_t *tvb, uint32_t len,
672 uint16_t marker, const char *marker_name)
674 proto_item *ti;
675 proto_tree *subtree;
677 if (!tree)
678 return;
680 ti = proto_tree_add_item(tree, hf_comment_header,
681 tvb, 0, -1, ENC_NA);
682 subtree = proto_item_add_subtree(ti, ett_marker_segment);
684 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
685 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
687 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
689 proto_tree_add_item(subtree, hf_comment, tvb, 4, len-2, ENC_ASCII);
693 /* Process an APP0 block.
695 * XXX - This code only works on US-ASCII systems!!!
697 static int
698 process_app0_segment(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, uint32_t len,
699 uint16_t marker, const char *marker_name)
701 proto_item *ti;
702 proto_tree *subtree;
703 proto_tree *subtree_details = NULL;
704 uint32_t offset;
705 char *str;
706 int str_size;
707 uint16_t x, y;
709 if (!tree)
710 return 0;
712 ti = proto_tree_add_item(tree, hf_marker_segment,
713 tvb, 0, -1, ENC_NA);
714 subtree = proto_item_add_subtree(ti, ett_marker_segment);
716 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
717 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
719 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
721 str = (char *)tvb_get_stringz_enc(pinfo->pool, tvb, 4, &str_size, ENC_ASCII);
722 ti = proto_tree_add_item(subtree, hf_identifier, tvb, 4, str_size, ENC_ASCII);
723 if (strcmp(str, "JFIF") == 0) {
724 /* Version */
725 ti = proto_tree_add_none_format(subtree, hf_version,
726 tvb, 9, 2, "Version: %u.%u",
727 tvb_get_uint8(tvb, 9),
728 tvb_get_uint8(tvb, 10));
729 subtree_details = proto_item_add_subtree(ti, ett_details);
730 proto_tree_add_item(subtree_details, hf_version_major,
731 tvb, 9, 1, ENC_BIG_ENDIAN);
732 proto_tree_add_item(subtree_details, hf_version_minor,
733 tvb, 10, 1, ENC_BIG_ENDIAN);
735 proto_tree_add_item(subtree, hf_units,
736 tvb, 11, 1, ENC_BIG_ENDIAN);
738 /* Aspect ratio */
739 proto_tree_add_item(subtree, hf_xdensity,
740 tvb, 12, 2, ENC_BIG_ENDIAN);
741 proto_tree_add_item(subtree, hf_ydensity,
742 tvb, 14, 2, ENC_BIG_ENDIAN);
744 /* Thumbnail */
745 proto_tree_add_item(subtree, hf_xthumbnail,
746 tvb, 16, 1, ENC_BIG_ENDIAN);
747 proto_tree_add_item(subtree, hf_ythumbnail,
748 tvb, 17, 1, ENC_BIG_ENDIAN);
749 x = tvb_get_uint8(tvb, 16);
750 y = tvb_get_uint8(tvb, 17);
751 if (x || y) {
752 proto_tree_add_item(subtree, hf_rgb,
753 tvb, 18, 3 * (x * y), ENC_NA);
754 offset = 18 + (3 * (x * y));
755 } else {
756 offset = 18;
759 else if (strcmp(str, "JFXX") == 0) {
760 proto_tree_add_item(subtree, hf_extension_code,
761 tvb, 9, 1, ENC_BIG_ENDIAN);
762 /* XXX - dissect the extension based on its extension code */
763 offset = 10;
765 else { /* Unknown */
766 proto_item_append_text(ti, " (unknown identifier)");
767 offset = 4 + str_size;
769 proto_tree_add_bytes_format_value(subtree, hf_remain_seg_data, tvb, offset, -1, NULL, "%u bytes", len - 2 - str_size);
771 return offset;
774 static void
775 // NOLINTNEXTLINE(misc-no-recursion)
776 process_tiff_ifd_chain(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo,
777 unsigned encoding, uint32_t start_ifd_offset,
778 int hf_tag, const char *ifd_type_desc)
780 uint32_t next_ifd_offset = start_ifd_offset;
782 for (unsigned ifd_index = 0;; ++ifd_index) {
783 int offset = next_ifd_offset;
785 * Process the IFD
787 uint32_t num_fields = tvb_get_uint16(tvb, offset, encoding);
788 proto_tree *subtree_ifd = proto_tree_add_subtree_format(tree, tvb, offset, num_fields * 12 + 6,
789 ett_ifd, NULL, "%s #%u", ifd_type_desc, ifd_index);
790 proto_tree_add_item(subtree_ifd, hf_ifd_num_fields, tvb, offset, 2, encoding);
791 offset += 2;
792 while (num_fields-- > 0) {
793 uint32_t field_tag, field_type, value_count, value_size;
794 int value_hf;
796 proto_tree_add_item_ret_uint(subtree_ifd, hf_tag, tvb, offset, 2, encoding, &field_tag);
797 offset += 2;
798 proto_tree_add_item_ret_uint(subtree_ifd, hf_ifd_type, tvb, offset, 2, encoding, &field_type);
799 offset += 2;
800 proto_tree_add_item_ret_uint(subtree_ifd, hf_ifd_count, tvb, offset, 4, encoding, &value_count);
801 offset += 4;
803 switch (field_type) {
804 case EXIF_TYPE_BYTE:
805 value_size = 1; value_hf = hf_ifd_value_byte; break;
806 case EXIF_TYPE_ASCII:
807 value_size = 1; value_hf = hf_ifd_value_ascii; break;
808 case EXIF_TYPE_SHORT:
809 value_size = 2; value_hf = hf_ifd_value_short; break;
810 case EXIF_TYPE_LONG:
811 value_size = 4; value_hf = hf_ifd_value_long; break;
812 case EXIF_TYPE_RATIONAL:
813 value_size = 8; value_hf = hf_ifd_value_rational; break;
814 case EXIF_TYPE_UNDEFINED:
815 value_size = 1; value_hf = hf_ifd_value_undefined; break;
816 case EXIF_TYPE_SLONG:
817 value_size = 4; value_hf = hf_ifd_value_slong; break;
818 case EXIF_TYPE_SRATIONAL:
819 value_size = 8; value_hf = hf_ifd_value_srational; break;
820 default:
821 value_size = 0; value_hf = -1; break;
824 int value_offset = -1;
825 proto_tree *value_parent = NULL;
827 if (value_size == 0 || 4 / value_size < value_count) {
828 /* The value(s) are located outside the IFD, and the offset field points to them. */
829 uint32_t value_offset_uint;
830 proto_item *offset_item = proto_tree_add_item_ret_uint(
831 subtree_ifd, hf_ifd_offset, tvb, offset, 4, encoding, &value_offset_uint);
833 if (value_offset_uint < tvb_reported_length(tvb)) {
834 value_offset = (int)value_offset_uint;
835 } else {
836 expert_add_info_format(pinfo, offset_item, &ei_ifd_value_offset,
837 "bogus, should be < %u", tvb_reported_length(tvb));
840 value_parent = tree;
841 } else {
842 /* The value(s) are small enough to fit directly in the offset field. */
843 value_offset = offset;
844 value_parent = subtree_ifd;
847 if (value_offset >= 0) {
848 if (value_hf == hf_ifd_value_ascii || value_hf == hf_ifd_value_undefined)
849 proto_tree_add_item(value_parent, value_hf, tvb, value_offset, value_count, ENC_NA);
850 else if (value_size != 0)
851 for (uint32_t i = 0; i < value_count; ++i) {
852 proto_item *value_item = proto_tree_add_item(value_parent, value_hf, tvb,
853 value_offset, value_size, encoding);
855 if (value_hf == hf_ifd_value_rational) {
856 proto_tree *subtree_value = proto_item_add_subtree(value_item, ett_rational);
857 uint32_t num, denom;
858 proto_tree_add_item_ret_uint(
859 subtree_value, hf_ifd_value_rational_numerator, tvb,
860 value_offset, 4, encoding, &num);
861 proto_tree_add_item_ret_uint(
862 subtree_value, hf_ifd_value_rational_denominator, tvb,
863 value_offset + 4, 4, encoding, &denom);
864 proto_item_set_text(value_item, "Value: %"PRIu32"/%"PRIu32, num, denom);
866 else if (value_hf == hf_ifd_value_srational) {
867 proto_tree *subtree_value = proto_item_add_subtree(value_item, ett_srational);
868 int32_t num, denom;
869 proto_tree_add_item_ret_int(
870 subtree_value, hf_ifd_value_srational_numerator, tvb,
871 value_offset, 4, encoding, &num);
872 proto_tree_add_item_ret_int(
873 subtree_value, hf_ifd_value_srational_denominator, tvb,
874 value_offset + 4, 4, encoding, &denom);
875 proto_item_set_text(value_item, "Value: %"PRIi32"/%"PRIi32, num, denom);
877 else if (value_hf == hf_ifd_value_long && value_count == 1 && hf_tag == hf_ifd_tag) {
878 uint32_t extension_ifd_offset = tvb_get_uint32(tvb, value_offset, encoding);
879 int extension_hf_ifd_tag = -1;
880 const char *extension_ifd_type_desc = NULL;
882 switch (field_tag) {
883 case EXIF_TAG_EXIF_IFD_POINTER:
884 extension_hf_ifd_tag = hf_ifd_tag_exif;
885 extension_ifd_type_desc = "Exif IFD";
886 break;
887 case EXIF_TAG_GPS_IFD_POINTER:
888 extension_hf_ifd_tag = hf_ifd_tag_gps;
889 extension_ifd_type_desc = "GPS IFD";
890 break;
891 case EXIF_TAG_INTEROP_IFD_POINTER:
892 extension_hf_ifd_tag = hf_ifd_tag_interop;
893 extension_ifd_type_desc = "Interoperability IFD";
894 break;
897 if (extension_ifd_type_desc) {
898 if (extension_ifd_offset < tvb_reported_length(tvb)) {
899 increment_dissection_depth(pinfo);
900 process_tiff_ifd_chain(tree, tvb, pinfo, encoding,
901 extension_ifd_offset, extension_hf_ifd_tag,
902 extension_ifd_type_desc);
903 decrement_dissection_depth(pinfo);
904 } else {
905 expert_add_info_format(pinfo, value_item, &ei_start_ifd_offset,
906 "bogus, should be < %u", tvb_reported_length(tvb));
911 value_offset += value_size;
914 offset += 4;
917 * Offset to the next IFD
919 proto_item *next_ifd_offset_item = proto_tree_add_item_ret_uint(
920 subtree_ifd, hf_next_ifd_offset, tvb, offset, 4, encoding, &next_ifd_offset);
921 offset += 4;
923 if (next_ifd_offset == 0)
924 break;
926 if (next_ifd_offset < (uint32_t)offset) {
927 expert_add_info_format(pinfo, next_ifd_offset_item, &ei_next_ifd_offset,
928 " (bogus, should be >= %u)", offset);
929 return;
934 static void
935 process_tiff(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo)
938 * Endianness
940 unsigned encoding;
941 int offset = 0;
943 uint16_t byte_order = tvb_get_ntohs(tvb, offset);
944 if (byte_order == 0x4949) {
945 encoding = ENC_LITTLE_ENDIAN;
946 proto_tree_add_uint_format_value(tree, hf_endianness, tvb, offset, 2, byte_order, "little endian");
947 } else if (byte_order == 0x4D4D) {
948 encoding = ENC_BIG_ENDIAN;
949 proto_tree_add_uint_format_value(tree, hf_endianness, tvb, offset, 2, byte_order, "big endian");
950 } else {
951 /* Error: invalid endianness encoding */
952 proto_tree_add_uint_format_value(tree, hf_endianness, tvb, offset, 2, byte_order,
953 "Incorrect encoding 0x%04x- skipping the remainder of this application marker", byte_order);
954 return;
956 offset += 2;
958 * Fixed value 42 = 0x002a
960 offset += 2;
962 * Offset to IFD
964 uint32_t start_ifd_offset;
965 proto_item* start_ifd_offset_item = proto_tree_add_item_ret_uint(
966 tree, hf_start_ifd_offset, tvb, offset, 4, encoding, &start_ifd_offset);
967 offset += 4;
969 * Check for a bogus offset value.
970 * XXX - bogus value message should also deal with a
971 * value that's too large and causes an overflow.
972 * Or should it just check against the segment length,
973 * which is 16 bits?
975 if (start_ifd_offset < (uint32_t)offset) {
976 expert_add_info_format(pinfo, start_ifd_offset_item, &ei_start_ifd_offset,
977 " (bogus, should be >= %u)", offset);
978 return;
981 process_tiff_ifd_chain(tree, tvb, pinfo, encoding, start_ifd_offset,
982 hf_ifd_tag, "Image File Directory");
985 /* Process an APP1 block.
987 * XXX - This code only works on US-ASCII systems!!!
989 static void
990 process_app1_segment(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, uint32_t len,
991 uint16_t marker, const char *marker_name, bool show_first_identifier_not_jfif)
993 proto_item *ti;
994 proto_tree *subtree;
995 char *str;
996 int str_size;
997 int offset = 0;
999 ti = proto_tree_add_item(tree, hf_marker_segment,
1000 tvb, 0, -1, ENC_NA);
1001 subtree = proto_item_add_subtree(ti, ett_marker_segment);
1003 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
1004 proto_tree_add_item(subtree, hf_marker, tvb, offset, 2, ENC_BIG_ENDIAN);
1005 offset += 2;
1007 proto_tree_add_item(subtree, hf_len, tvb, offset, 2, ENC_BIG_ENDIAN);
1008 offset += 2;
1010 str = (char*)tvb_get_stringz_enc(pinfo->pool, tvb, offset, &str_size, ENC_ASCII);
1011 ti = proto_tree_add_item(subtree, hf_identifier, tvb, offset, str_size, ENC_ASCII);
1012 offset += str_size;
1014 if (show_first_identifier_not_jfif && strcmp(str, "JFIF") != 0) {
1015 expert_add_info(pinfo, ti, &ei_file_jpeg_first_identifier_not_jfif);
1018 if (strcmp(str, "Exif") == 0) {
1019 offset++; /* Skip a byte supposed to be 0x00 */
1021 tvbuff_t *tvb_tiff = tvb_new_subset_remaining(tvb, offset);
1022 process_tiff(subtree, tvb_tiff, pinfo);
1023 } else {
1024 proto_tree_add_bytes_format_value(subtree, hf_remain_seg_data, tvb, offset, -1, NULL, "%u bytes", len - 2 - str_size);
1025 proto_item_append_text(ti, " (Unknown identifier)");
1029 /* Process an APP2 block.
1031 * XXX - This code only works on US-ASCII systems!!!
1033 static void
1034 process_app2_segment(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, uint32_t len,
1035 uint16_t marker, const char *marker_name)
1037 proto_item *ti;
1038 proto_tree *subtree;
1039 char *str;
1040 int str_size;
1042 if (!tree)
1043 return;
1045 ti = proto_tree_add_item(tree, hf_marker_segment,
1046 tvb, 0, -1, ENC_NA);
1047 subtree = proto_item_add_subtree(ti, ett_marker_segment);
1049 proto_item_append_text(ti, ": %s (0x%04X)", marker_name, marker);
1050 proto_tree_add_item(subtree, hf_marker, tvb, 0, 2, ENC_BIG_ENDIAN);
1052 proto_tree_add_item(subtree, hf_len, tvb, 2, 2, ENC_BIG_ENDIAN);
1054 str = (char*)tvb_get_stringz_enc(pinfo->pool, tvb, 4, &str_size, ENC_ASCII);
1055 ti = proto_tree_add_item(subtree, hf_identifier, tvb, 4, str_size, ENC_ASCII);
1056 if (strcmp(str, "FPXR") == 0) {
1057 proto_tree_add_item(tree, hf_exif_flashpix_marker, tvb, 0, -1, ENC_NA);
1058 } else {
1059 proto_tree_add_bytes_format_value(subtree, hf_remain_seg_data, tvb, 4 + str_size, -1, NULL, "%u bytes", len - 2 - str_size);
1060 proto_item_append_text(ti, " (Unknown identifier)");
1064 static int
1065 dissect_jfif(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
1067 proto_tree *subtree;
1068 proto_item *ti;
1069 int tvb_len = tvb_reported_length(tvb);
1070 int32_t start_entropy = 0;
1071 int32_t start_fill, start_marker;
1072 bool show_first_identifier_not_jfif = false;
1074 /* check if we have a full JFIF in tvb */
1075 if (tvb_len < 20)
1076 return 0;
1077 /* Start Of Image marker must come first */
1078 if (tvb_get_ntohs(tvb, 0) != MARKER_SOI)
1079 return 0;
1080 /* Check identifier field in first App segment is "JFIF", although "Exif" from App1
1081 can/does appear here too... */
1082 if (tvb_memeql(tvb, 6, (const uint8_t*)"Exif", 5) == 0) {
1083 show_first_identifier_not_jfif = true;
1085 else if (tvb_memeql(tvb, 6, (const uint8_t*)"JFIF", 5)) {
1086 return 0;
1089 /* Add summary to INFO column if it is enabled */
1090 col_append_sep_str(pinfo->cinfo, COL_INFO, " ", "(JPEG JFIF image)");
1092 ti = proto_tree_add_item(tree, proto_jfif,
1093 tvb, 0, -1, ENC_NA);
1094 subtree = proto_item_add_subtree(ti, ett_jfif);
1096 for (; ; ) {
1097 const char *str;
1098 uint16_t marker;
1100 start_fill = start_entropy;
1102 for (; ; ) {
1103 start_fill = tvb_find_uint8(tvb, start_fill, -1, 0xFF);
1105 if (start_fill == -1 || tvb_len - start_fill == 1
1106 || tvb_get_uint8(tvb, start_fill + 1) != 0) /* FF 00 is FF escaped */
1107 break;
1109 start_fill += 2;
1112 if (start_fill == -1) start_fill = tvb_len;
1114 if (start_fill != start_entropy)
1115 proto_tree_add_item(subtree, hf_entropy_coded_segment, tvb, start_entropy, start_fill - start_entropy, ENC_NA);
1117 if (start_fill == tvb_len) break;
1119 start_marker = start_fill;
1121 while (tvb_get_uint8(tvb, start_marker + 1) == 0xFF)
1122 ++start_marker;
1124 if (start_marker != start_fill)
1125 proto_tree_add_item(subtree, hf_fill_bytes, tvb, start_fill, start_marker - start_fill, ENC_NA);
1127 marker = tvb_get_ntohs(tvb, start_marker);
1128 str = try_val_to_str(marker, vals_marker);
1129 if (str) { /* Known marker */
1130 if (marker_has_length(marker)) { /* Marker segment */
1131 /* Length of marker segment = 2 + len */
1132 const uint16_t len = tvb_get_ntohs(tvb, start_marker + 2);
1133 tvbuff_t *tmp_tvb = tvb_new_subset_length(tvb, start_marker, 2 + len);
1134 switch (marker) {
1135 case MARKER_APP0:
1136 process_app0_segment(subtree, tmp_tvb, pinfo, len, marker, str);
1137 break;
1138 case MARKER_APP1:
1139 process_app1_segment(subtree, tmp_tvb, pinfo, len, marker, str, show_first_identifier_not_jfif);
1140 show_first_identifier_not_jfif = false;
1141 break;
1142 case MARKER_APP2:
1143 process_app2_segment(subtree, tmp_tvb, pinfo, len, marker, str);
1144 break;
1145 case MARKER_SOF0:
1146 case MARKER_SOF1:
1147 case MARKER_SOF2:
1148 case MARKER_SOF3:
1149 case MARKER_SOF5:
1150 case MARKER_SOF6:
1151 case MARKER_SOF7:
1152 case MARKER_SOF8:
1153 case MARKER_SOF9:
1154 case MARKER_SOF10:
1155 case MARKER_SOF11:
1156 case MARKER_SOF13:
1157 case MARKER_SOF14:
1158 case MARKER_SOF15:
1159 process_sof_header(subtree, tmp_tvb, len, marker, str);
1160 break;
1161 case MARKER_SOS:
1162 process_sos_header(subtree, tmp_tvb, len, marker, str);
1163 break;
1164 case MARKER_COM:
1165 process_comment_header(subtree, tmp_tvb, len, marker, str);
1166 break;
1167 default:
1168 process_marker_segment(subtree, tmp_tvb, len, marker, str);
1169 break;
1171 start_entropy = start_marker + 2 + len;
1172 } else { /* Marker but no segment */
1173 /* Length = 2 */
1174 proto_tree_add_item(subtree, hf_marker,
1175 tvb, start_marker, 2, ENC_BIG_ENDIAN);
1176 start_entropy = start_marker + 2;
1178 } else { /* Reserved! */
1179 ti = proto_tree_add_item(subtree, hf_marker,
1180 tvb, start_marker, 2, ENC_BIG_ENDIAN);
1181 proto_item_append_text(ti, " (Reserved)");
1182 return tvb_len;
1186 return tvb_len;
1189 static bool
1190 dissect_jfif_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1192 return dissect_jfif(tvb, pinfo, tree, data) > 0;
1195 /****************** Register the protocol with Wireshark ******************/
1197 void
1198 proto_register_jfif(void)
1201 * Setup list of header fields.
1203 static hf_register_info hf[] = {
1204 /* Marker */
1205 { &hf_marker,
1206 { "Marker",
1207 "image-jfif.marker",
1208 FT_UINT16, BASE_HEX, VALS(vals_marker), 0x0,
1209 "JFIF Marker",
1210 HFILL
1213 /* Marker segment */
1214 { &hf_marker_segment,
1215 { "Marker segment",
1216 "image-jfif.marker_segment",
1217 FT_NONE, BASE_NONE, NULL, 0x0,
1218 NULL,
1219 HFILL
1222 { &hf_len,
1223 { "Length",
1224 "image-jfif.length",
1225 FT_UINT16, BASE_DEC, 0, 0x0,
1226 "Length of segment (including length field)",
1227 HFILL
1230 /* MARKER_APP0 */
1231 { &hf_identifier,
1232 { "Identifier",
1233 "image-jfif.identifier",
1234 FT_STRINGZ, BASE_NONE, NULL, 0x0,
1235 "Identifier of the segment",
1236 HFILL
1239 /* MARKER_APP0 - JFIF */
1240 { &hf_version,
1241 { "Version",
1242 "image-jfif.version",
1243 FT_NONE, BASE_NONE, NULL, 0x0,
1244 "JFIF Version",
1245 HFILL
1248 { &hf_version_major,
1249 { "Major Version",
1250 "image-jfif.version.major",
1251 FT_UINT8, BASE_DEC, NULL, 0x0,
1252 "JFIF Major Version",
1253 HFILL
1256 { &hf_version_minor,
1257 { "Minor Version",
1258 "image-jfif.version.minor",
1259 FT_UINT8, BASE_DEC, NULL, 0x0,
1260 "JFIF Minor Version",
1261 HFILL
1264 { &hf_units,
1265 { "Units",
1266 "image-jfif.units",
1267 FT_UINT8, BASE_DEC, VALS(vals_units), 0x0,
1268 "Units used in this segment",
1269 HFILL
1272 { &hf_xdensity,
1273 { "Xdensity",
1274 "image-jfif.Xdensity",
1275 FT_UINT16, BASE_DEC, NULL, 0x0,
1276 "Horizontal pixel density",
1277 HFILL
1280 { &hf_ydensity,
1281 { "Ydensity",
1282 "image-jfif.Ydensity",
1283 FT_UINT16, BASE_DEC, NULL, 0x0,
1284 "Vertical pixel density",
1285 HFILL
1288 { &hf_xthumbnail,
1289 { "Xthumbnail",
1290 "image-jfif.Xthumbnail",
1291 FT_UINT16, BASE_DEC, NULL, 0x0,
1292 "Thumbnail horizontal pixel count",
1293 HFILL
1296 { &hf_ythumbnail,
1297 { "Ythumbnail",
1298 "image-jfif.Ythumbnail",
1299 FT_UINT16, BASE_DEC, NULL, 0x0,
1300 "Thumbnail vertical pixel count",
1301 HFILL
1304 { &hf_rgb,
1305 { "RGB values of thumbnail pixels",
1306 "image-jfif.RGB",
1307 FT_BYTES, BASE_NONE, NULL, 0x0,
1308 "RGB values of the thumbnail pixels (24 bit per pixel, Xthumbnail x Ythumbnail pixels)",
1309 HFILL
1312 /* MARKER_APP0 - JFXX */
1313 { &hf_extension_code,
1314 { "Extension code",
1315 "image-jfif.extension.code",
1316 FT_UINT8, BASE_HEX, VALS(vals_extension_code), 0x0,
1317 "JFXX extension code for thumbnail encoding",
1318 HFILL
1321 /* Header: Start of Frame (MARKER_SOF) */
1322 { &hf_sof_header,
1323 { "Start of Frame header",
1324 "image-jfif.sof",
1325 FT_NONE, BASE_NONE, NULL, 0x0,
1326 NULL,
1327 HFILL
1330 { &hf_sof_precision,
1331 { "Sample Precision (bits)",
1332 "image-jfif.sof.precision",
1333 FT_UINT8, BASE_DEC, NULL, 0x0,
1334 "Specifies the precision in bits for the samples of the components in the frame.",
1335 HFILL
1338 { &hf_sof_lines,
1339 { "Lines",
1340 "image-jfif.sof.lines",
1341 FT_UINT16, BASE_DEC, NULL, 0x0,
1342 "Specifies the maximum number of lines in the source image.",
1343 HFILL
1346 { &hf_sof_samples_per_line,
1347 { "Samples per line",
1348 "image-jfif.sof.samples_per_line",
1349 FT_UINT16, BASE_DEC, NULL, 0x0,
1350 "Specifies the maximum number of samples per line in the source image.",
1351 HFILL
1354 { &hf_sof_nf,
1355 { "Number of image components in frame",
1356 "image-jfif.sof.nf",
1357 FT_UINT8, BASE_DEC, NULL, 0x0,
1358 "Specifies the number of source image components in the frame.",
1359 HFILL
1362 { &hf_sof_c_i,
1363 { "Component identifier",
1364 "image-jfif.sof.c_i",
1365 FT_UINT8, BASE_DEC, NULL, 0x0,
1366 "Assigns a unique label to the ith component in the sequence of frame component specification parameters.",
1367 HFILL
1370 { &hf_sof_h_i,
1371 { "Horizontal sampling factor",
1372 "image-jfif.sof.h_i",
1373 FT_UINT8, BASE_DEC, NULL, 0xF0,
1374 "Specifies the relationship between the component horizontal dimension and maximum image dimension X.",
1375 HFILL
1378 { &hf_sof_v_i,
1379 { "Vertical sampling factor",
1380 "image-jfif.sof.v_i",
1381 FT_UINT8, BASE_DEC, NULL, 0x0F,
1382 "Specifies the relationship between the component vertical dimension and maximum image dimension Y.",
1383 HFILL
1386 { &hf_sof_tq_i,
1387 { "Quantization table destination selector",
1388 "image-jfif.sof.tq_i",
1389 FT_UINT8, BASE_DEC, NULL, 0x0,
1390 "Specifies one of four possible quantization table destinations from which the quantization table to"
1391 " use for dequantization of DCT coefficients of component Ci is retrieved.",
1392 HFILL
1396 /* Header: Start of Segment (MARKER_SOS) */
1397 { &hf_sos_header,
1398 { "Start of Segment header",
1399 "image-jfif.header.sos",
1400 FT_NONE, BASE_NONE, NULL, 0x0,
1401 NULL,
1402 HFILL
1405 { &hf_sos_ns,
1406 { "Number of image components in scan",
1407 "image-jfif.sos.ns",
1408 FT_UINT8, BASE_DEC, NULL, 0x0,
1409 "Specifies the number of source image components in the scan.",
1410 HFILL
1413 { &hf_sos_cs_j,
1414 { "Scan component selector",
1415 "image-jfif.sos.component_selector",
1416 FT_UINT8, BASE_DEC, NULL, 0x0,
1417 "Selects which of the Nf image components specified in the frame parameters shall be the jth"
1418 " component in the scan.",
1419 HFILL
1422 { &hf_sos_td_j,
1423 { "DC entropy coding table destination selector",
1424 "image-jfif.sos.dc_entropy_selector",
1425 FT_UINT8, BASE_DEC, NULL, 0xF0,
1426 "Specifies one of four possible DC entropy coding table destinations from which the entropy"
1427 " table needed for decoding of the DC coefficients of component Csj is retrieved.",
1428 HFILL
1431 { &hf_sos_ta_j,
1432 { "AC entropy coding table destination selector",
1433 "image-jfif.sos.ac_entropy_selector",
1434 FT_UINT8, BASE_DEC, NULL, 0x0F,
1435 "Specifies one of four possible AC entropy coding table destinations from which the entropy"
1436 " table needed for decoding of the AC coefficients of component Csj is retrieved.",
1437 HFILL
1440 { &hf_sos_ss,
1441 { "Start of spectral or predictor selection",
1442 "image-jfif.sos.ss",
1443 FT_UINT8, BASE_DEC, NULL, 0x0,
1444 "In the DCT modes of operation, this parameter specifies the first DCT coefficient in"
1445 " each block in zig-zag order which shall be coded in the scan. This parameter shall"
1446 " be set to zero for the sequential DCT processes. In the lossless mode of operations"
1447 " this parameter is used to select the predictor.",
1448 HFILL
1451 { &hf_sos_se,
1452 { "End of spectral selection",
1453 "image-jfif.sos.se",
1454 FT_UINT8, BASE_DEC, NULL, 0x0,
1455 "Specifies the last DCT coefficient in each block in zig-zag order which shall be coded"
1456 " in the scan. This parameter shall be set to 63 for the sequential DCT processes. In the"
1457 " lossless mode of operations this parameter has no meaning. It shall be set to zero.",
1458 HFILL
1461 { &hf_sos_ah,
1462 { "Successive approximation bit position high",
1463 "image-jfif.sos.ah",
1464 FT_UINT8, BASE_DEC, NULL, 0xF0,
1465 "This parameter specifies the point transform used in the preceding scan (i.e. successive"
1466 " approximation bit position low in the preceding scan) for the band of coefficients"
1467 " specified by Ss and Se. This parameter shall be set to zero for the first scan of each"
1468 " band of coefficients. In the lossless mode of operations this parameter has no meaning."
1469 " It shall be set to zero.",
1470 HFILL
1473 { &hf_sos_al,
1474 { "Successive approximation bit position low or point transform",
1475 "image-jfif.sos.al",
1476 FT_UINT8, BASE_DEC, NULL, 0x0F,
1477 "In the DCT modes of operation this parameter specifies the point transform, i.e. bit"
1478 " position low, used before coding the band of coefficients specified by Ss and Se."
1479 " This parameter shall be set to zero for the sequential DCT processes. In the lossless"
1480 " mode of operations, this parameter specifies the point transform, Pt.",
1481 HFILL
1485 /* Header: Comment (MARKER_COM) */
1486 { &hf_comment_header,
1487 { "Comment header",
1488 "image-jfif.header.comment",
1489 FT_NONE, BASE_NONE, NULL, 0x0,
1490 NULL,
1491 HFILL
1494 { &hf_comment,
1495 { "Comment",
1496 "image-jfif.comment",
1497 FT_STRING, BASE_NONE, NULL, 0x0,
1498 NULL,
1499 HFILL
1502 { &hf_remain_seg_data,
1503 { "Remaining segment data",
1504 "image-jfif.remain_seg_data",
1505 FT_BYTES, BASE_NONE, NULL, 0x0,
1506 NULL,
1507 HFILL
1510 { &hf_endianness,
1511 { "Endianness",
1512 "image-jfif.endianness",
1513 FT_UINT16, BASE_HEX, NULL, 0x0,
1514 NULL,
1515 HFILL
1518 { &hf_start_ifd_offset,
1519 { "Start offset of IFD starting from the TIFF header start",
1520 "image-jfif.start_ifd_offset",
1521 FT_UINT32, BASE_DEC|BASE_UNIT_STRING, UNS(&units_byte_bytes), 0x0,
1522 NULL,
1523 HFILL
1526 { &hf_next_ifd_offset,
1527 { "Offset to next IFD from start of TIFF header",
1528 "image-jfif.next_ifd_offset",
1529 FT_UINT32, BASE_DEC|BASE_UNIT_STRING, UNS(&units_byte_bytes), 0x0,
1530 NULL,
1531 HFILL
1534 { &hf_exif_flashpix_marker,
1535 { "Exif FlashPix APP2 application marker",
1536 "image-jfif.exif_flashpix_marker",
1537 FT_NONE, BASE_NONE, NULL, 0x0,
1538 NULL,
1539 HFILL
1542 { &hf_entropy_coded_segment,
1543 { "Entropy-coded segment (dissection is not yet implemented)",
1544 "image-jfif.entropy_coded_segment",
1545 FT_BYTES, BASE_NONE, NULL, 0x0,
1546 NULL,
1547 HFILL
1550 { &hf_fill_bytes,
1551 { "Fill bytes",
1552 "image-jfif.fill_bytes",
1553 FT_BYTES, BASE_NONE, NULL, 0x0,
1554 NULL,
1555 HFILL
1558 { &hf_skipped_tiff_data,
1559 { "Skipped data between end of TIFF header and start of IFD",
1560 "image-jfif.skipped_tiff_data",
1561 FT_BYTES, BASE_NONE, NULL, 0x0,
1562 NULL,
1563 HFILL
1566 { &hf_ifd_num_fields,
1567 { "Number of fields in this IFD",
1568 "image-jfif.ifd.num_fields",
1569 FT_UINT16, BASE_DEC, NULL, 0x0,
1570 NULL,
1571 HFILL
1574 { &hf_ifd_tag,
1575 { "Tag",
1576 "image-jfif.ifd.tag",
1577 FT_UINT16, BASE_DEC, VALS(vals_ifd_tags), 0x0,
1578 NULL,
1579 HFILL
1582 { &hf_ifd_tag_exif,
1583 { "Tag",
1584 "image-jfif.ifd.tag_exif",
1585 FT_UINT16, BASE_DEC, VALS(vals_ifd_tags_exif), 0x0,
1586 NULL,
1587 HFILL
1590 { &hf_ifd_tag_gps,
1591 { "Tag",
1592 "image-jfif.ifd.tag_gps",
1593 FT_UINT16, BASE_DEC, VALS(vals_ifd_tags_gps), 0x0,
1594 NULL,
1595 HFILL
1598 { &hf_ifd_tag_interop,
1599 { "Tag",
1600 "image-jfif.ifd.tag_interop",
1601 FT_UINT16, BASE_DEC, VALS(vals_ifd_tags_interop), 0x0,
1602 NULL,
1603 HFILL
1606 { &hf_ifd_type,
1607 { "Type",
1608 "image-jfif.ifd.type",
1609 FT_UINT16, BASE_DEC, VALS(vals_exif_types), 0x0,
1610 NULL,
1611 HFILL
1614 { &hf_ifd_count,
1615 { "Count",
1616 "image-jfif.ifd.count",
1617 FT_UINT32, BASE_DEC, NULL, 0x0,
1618 NULL,
1619 HFILL
1622 { &hf_ifd_offset,
1623 { "Value offset from start of TIFF header",
1624 "image-jfif.ifd.offset",
1625 FT_UINT32, BASE_DEC, NULL, 0x0,
1626 NULL,
1627 HFILL
1630 { &hf_ifd_value_byte,
1631 { "Value",
1632 "image-jfif.ifd.value_byte",
1633 FT_UINT8, BASE_DEC, NULL, 0x0,
1634 NULL,
1635 HFILL
1638 { &hf_ifd_value_ascii,
1639 { "Value",
1640 "image-jfif.ifd.value_ascii",
1641 FT_STRING, BASE_NONE, NULL, 0x0,
1642 NULL,
1643 HFILL
1646 { &hf_ifd_value_short,
1647 { "Value",
1648 "image-jfif.ifd.value_short",
1649 FT_UINT16, BASE_DEC, NULL, 0x0,
1650 NULL,
1651 HFILL
1654 { &hf_ifd_value_long,
1655 { "Value",
1656 "image-jfif.ifd.value_long",
1657 FT_UINT32, BASE_DEC, NULL, 0x0,
1658 NULL,
1659 HFILL
1662 { &hf_ifd_value_rational,
1663 { "Value",
1664 "image-jfif.ifd.value_rational",
1665 FT_NONE, BASE_NONE, NULL, 0x0,
1666 NULL,
1667 HFILL
1670 { &hf_ifd_value_rational_numerator,
1671 { "Numerator",
1672 "image-jfif.ifd.value_rational.numerator",
1673 FT_UINT32, BASE_DEC, NULL, 0x0,
1674 NULL,
1675 HFILL
1678 { &hf_ifd_value_rational_denominator,
1679 { "Denominator",
1680 "image-jfif.ifd.value_rational.denominator",
1681 FT_UINT32, BASE_DEC, NULL, 0x0,
1682 NULL,
1683 HFILL
1686 { &hf_ifd_value_undefined,
1687 { "Value (raw)",
1688 "image-jfif.ifd.value_undefined",
1689 FT_BYTES, BASE_NONE, NULL, 0x0,
1690 NULL,
1691 HFILL
1694 { &hf_ifd_value_slong,
1695 { "Value",
1696 "image-jfif.ifd.value_slong",
1697 FT_INT32, BASE_DEC, NULL, 0x0,
1698 NULL,
1699 HFILL
1702 { &hf_ifd_value_srational,
1703 { "Value",
1704 "image-jfif.ifd.value_srational",
1705 FT_NONE, BASE_NONE, NULL, 0x0,
1706 NULL,
1707 HFILL
1710 { &hf_ifd_value_srational_numerator,
1711 { "Numerator",
1712 "image-jfif.ifd.value_srational.numerator",
1713 FT_INT32, BASE_DEC, NULL, 0x0,
1714 NULL,
1715 HFILL
1718 { &hf_ifd_value_srational_denominator,
1719 { "Denominator",
1720 "image-jfif.ifd.value_srational.denominator",
1721 FT_INT32, BASE_DEC, NULL, 0x0,
1722 NULL,
1723 HFILL
1728 /* Setup protocol subtree array */
1729 static int *ett[] = {
1730 &ett_jfif,
1731 &ett_marker_segment,
1732 &ett_details,
1733 &ett_ifd,
1734 &ett_rational,
1735 &ett_srational,
1738 static ei_register_info ei[] = {
1739 { &ei_file_jpeg_first_identifier_not_jfif,
1740 { "image-jfif.app0-identifier-not-jfif", PI_PROTOCOL, PI_WARN,
1741 "Initial App0 segment with \"JFIF\" Identifier not found", EXPFILL }},
1742 { &ei_start_ifd_offset,
1743 { "image-jfif.start_ifd_offset.invalid", PI_PROTOCOL, PI_WARN,
1744 "Invalid value", EXPFILL }},
1745 { &ei_next_ifd_offset,
1746 { "image-jfif.next_ifd_offset.invalid", PI_PROTOCOL, PI_WARN,
1747 "Invalid value", EXPFILL }},
1748 { &ei_ifd_value_offset,
1749 { "image-jfif.ifd_value_offset.invalid", PI_PROTOCOL, PI_WARN,
1750 "Invalid value", EXPFILL }},
1753 expert_module_t* expert_jfif;
1755 /* Register the protocol name and description */
1756 proto_jfif = proto_register_protocol(
1757 "JPEG File Interchange Format",
1758 "JFIF (JPEG) image",
1759 "image-jfif"
1762 /* Required function calls to register the header fields
1763 * and subtrees used */
1764 proto_register_field_array(proto_jfif, hf, array_length(hf));
1765 proto_register_subtree_array(ett, array_length(ett));
1767 expert_jfif = expert_register_protocol(proto_jfif);
1768 expert_register_field_array(expert_jfif, ei, array_length(ei));
1770 register_dissector("image-jfif", dissect_jfif, proto_jfif);
1774 void
1775 proto_reg_handoff_jfif(void)
1777 dissector_handle_t jfif_handle = find_dissector("image-jfif");
1779 /* Register the JPEG media type */
1780 dissector_add_string("media_type", "image/jfif", jfif_handle);
1781 dissector_add_string("media_type", "image/jpg", jfif_handle);
1782 dissector_add_string("media_type", "image/jpeg", jfif_handle);
1784 dissector_add_uint("wtap_encap", WTAP_ENCAP_JPEG_JFIF, jfif_handle);
1786 heur_dissector_add("http", dissect_jfif_heur, "JPEG file in HTTP", "jfif_http", proto_jfif, HEURISTIC_ENABLE);
1787 heur_dissector_add("wtap_file", dissect_jfif_heur, "JPEG file", "jfif_wtap", proto_jfif, HEURISTIC_ENABLE);
1791 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1793 * Local variables:
1794 * c-basic-offset: 4
1795 * tab-width: 8
1796 * indent-tabs-mode: nil
1797 * End:
1799 * vi: set shiftwidth=4 tabstop=8 expandtab:
1800 * :indentSize=4:tabSize=8:noTabs=true: