3 * Routines for image/tiff dissection
4 * Copyright 2021, Daniel Dulaney.
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * The TIFF 6 specification can be found at:
11 * https://www.adobe.io/content/dam/udp/en/open/standards/tiff/TIFF6.pdf
13 * SPDX-License-Identifier: GPL-2.0-or-later
17 #include <epan/packet.h>
18 #include <epan/expert.h>
19 #include <wsutil/array.h>
21 void proto_reg_handoff_tiff(void);
22 void proto_register_tiff(void);
24 static int proto_tiff
;
27 static int hf_tiff_header_endianness
;
28 static int hf_tiff_header_magic
;
29 static int hf_tiff_header_lead_ifd
;
32 static int hf_tiff_ifd_count
;
33 static int hf_tiff_ifd_next
;
36 static int hf_tiff_entry_tag
;
37 static int hf_tiff_entry_type
;
38 static int hf_tiff_entry_count
;
39 static int hf_tiff_entry_offset
;
40 static int hf_tiff_entry_unknown
;
43 static expert_field ei_tiff_unknown_tag
;
44 static expert_field ei_tiff_bad_entry
;
45 static expert_field ei_tiff_zero_denom
;
52 #define TIFF_TAG_NEW_SUBFILE_TYPE 254
55 #define TIFF_TAG_SUBFILE_TYPE 255
58 #define TIFF_TAG_IMAGE_WIDTH 256
59 static int hf_tiff_image_width
;
61 #define TIFF_TAG_IMAGE_LENGTH 257
62 static int hf_tiff_image_length
;
64 #define TIFF_TAG_BITS_PER_SAMPLE 258
65 static int hf_tiff_bits_per_sample
;
67 #define TIFF_TAG_COMPRESSION 259
68 static int hf_tiff_compression
;
70 #define TIFF_TAG_PHOTOMETRIC_INTERPRETATION 262
71 static int hf_tiff_photometric_interp
;
73 #define TIFF_TAG_THRESHHOLDING 263
74 static int hf_tiff_threshholding
;
76 #define TIFF_TAG_CELL_WIDTH 264
77 static int hf_tiff_cell_width
;
79 #define TIFF_TAG_CELL_LENGTH 265
80 static int hf_tiff_cell_length
;
82 #define TIFF_TAG_FILL_ORDER 266
83 static int hf_tiff_fill_order
;
85 #define TIFF_TAG_DOCUMENT_NAME 269
86 static int hf_tiff_document_name
;
88 #define TIFF_TAG_IMAGE_DESCRIPTION 270
89 static int hf_tiff_image_description
;
91 #define TIFF_TAG_MAKE 271
92 static int hf_tiff_make
;
94 #define TIFF_TAG_MODEL 272
95 static int hf_tiff_model
;
97 #define TIFF_TAG_STRIP_OFFSETS 273
98 static int hf_tiff_strip_offset
;
100 #define TIFF_TAG_ORIENTATION 274
101 static int hf_tiff_orientation
;
103 #define TIFF_TAG_SAMPLES_PER_PIXEL 277
104 static int hf_tiff_samples_per_pixel
;
106 #define TIFF_TAG_ROWS_PER_STRIP 278
107 static int hf_tiff_rows_per_strip
;
109 #define TIFF_TAG_STRIP_BYTE_COUNTS 279
110 static int hf_tiff_strip_byte_count
;
112 #define TIFF_TAG_MIN_SAMPLE_VALUE 280
115 #define TIFF_TAG_MAX_SAMPLE_VALUE 281
118 #define TIFF_TAG_X_RESOLUTION 282
119 static int hf_tiff_x_res_numer
;
120 static int hf_tiff_x_res_denom
;
121 static int hf_tiff_x_res_approx
;
123 #define TIFF_TAG_Y_RESOLUTION 283
124 static int hf_tiff_y_res_numer
;
125 static int hf_tiff_y_res_denom
;
126 static int hf_tiff_y_res_approx
;
128 #define TIFF_TAG_PLANAR_CONFIGURATION 284
129 static int hf_tiff_planar_configuration
;
131 #define TIFF_TAG_PAGE_NAME 285
132 static int hf_tiff_page_name
;
134 #define TIFF_TAG_X_POSITION 286
137 #define TIFF_TAG_Y_POSITION 287
140 #define TIFF_TAG_FREE_OFFSETS 288
143 #define TIFF_TAG_FREE_BYTE_COUNTS 289
146 #define TIFF_TAG_GRAY_RESPONSE_UNIT 290
147 static int hf_tiff_gray_response_unit
;
149 #define TIFF_TAG_GRAY_RESPONSE_CURVE 291
152 #define TIFF_TAG_T4_OPTIONS 292
155 #define TIFF_TAG_T6_OPTIONS 293
156 static int hf_tiff_t6_options
;
157 static int hf_tiff_t6_unused
;
158 static int hf_tiff_t6_allow_uncompresed
;
160 #define TIFF_TAG_RESOLUTION_UNIT 296
161 static int hf_tiff_resolution_unit
;
163 #define TIFF_TAG_PAGE_NUMBER 297
166 #define TIFF_TAG_TRANSFER_FUNCTION 301
169 #define TIFF_TAG_SOFTWARE 305
170 static int hf_tiff_software
;
172 #define TIFF_TAG_DATE_TIME 306
173 static int hf_tiff_date_time
;
175 #define TIFF_TAG_ARTIST 315
176 static int hf_tiff_artist
;
178 #define TIFF_TAG_HOST_COMPUTER 316
179 static int hf_tiff_host_computer
;
181 #define TIFF_TAG_PREDICTOR 317
182 static int hf_tiff_predictor
;
184 #define TIFF_TAG_WHITE_POINT 318
187 #define TIFF_TAG_PRIMARY_CHROMATICITIES 319
190 #define TIFF_TAG_COLOR_MAP 320
193 #define TIFF_TAG_HALFTONE_HINTS 321
196 #define TIFF_TAG_TILE_WIDTH 322
197 static int hf_tiff_tile_width
;
199 #define TIFF_TAG_TILE_LENGTH 323
200 static int hf_tiff_tile_length
;
202 #define TIFF_TAG_TILE_OFFSETS 324
205 #define TIFF_TAG_TILE_BYTE_COUNTS 325
208 #define TIFF_TAG_INK_SET 332
209 static int hf_tiff_ink_set
;
211 #define TIFF_TAG_INK_NAMES 333
214 #define TIFF_TAG_NUMBER_OF_INKS 334
215 static int hf_tiff_number_of_inks
;
217 #define TIFF_TAG_DOT_RANGE 336
220 #define TIFF_TAG_TARGET_PRINTER 337
221 static int hf_tiff_target_printer
;
223 #define TIFF_TAG_EXTRA_SAMPLES 338
226 #define TIFF_TAG_SAMPLE_FORMAT 339
229 #define TIFF_TAG_S_MIN_SAMPLE_VALUE 340
232 #define TIFF_TAG_S_MAX_SAMPLE_VALUE 341
235 #define TIFF_TAG_TRANSFER_RANGE 342
238 #define TIFF_TAG_JPEG_PROC 512
241 #define TIFF_TAG_JPEG_INTERCHANGE_FORMAT 513
244 #define TIFF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH 514
247 #define TIFF_TAG_JPEG_RESTART_INTERVAL 515
250 #define TIFF_TAG_JPEG_LOSSLESS_PREDICTORS 517
253 #define TIFF_TAG_JPEG_POINT_TRANSFORMS 518
256 #define TIFF_TAG_JPEG_Q_TABLES 519
259 #define TIFF_TAG_JPEG_DC_TABLES 520
262 #define TIFF_TAG_JPEG_AC_TABLES 521
265 #define TIFF_TAG_YCBCR_COEFFICIENTS 529
268 #define TIFF_TAG_YCBCR_SUBSAMPLING 530
271 #define TIFF_TAG_YCBCR_POSITIONING 531
274 #define TIFF_TAG_REFERENCE_BLACK_WHITE 532
277 #define TIFF_TAG_COPYRIGHT 0x8298
278 static int hf_tiff_copyright
;
280 static const value_string tiff_endianness_names
[] = {
281 { 0x4949, "Little-Endian" },
282 { 0x4D4D, "Big-Endian" },
286 static const value_string tiff_tag_names
[] = {
287 { TIFF_TAG_NEW_SUBFILE_TYPE
, "New Subfile Type" },
288 { TIFF_TAG_SUBFILE_TYPE
, "Subfile Type" },
289 { TIFF_TAG_IMAGE_WIDTH
, "Image Width" },
290 { TIFF_TAG_IMAGE_LENGTH
, "Image Length" },
291 { TIFF_TAG_BITS_PER_SAMPLE
, "Bits Per Sample" },
292 { TIFF_TAG_COMPRESSION
, "Compression" },
293 { TIFF_TAG_PHOTOMETRIC_INTERPRETATION
, "Photometric Interpretation" },
294 { TIFF_TAG_THRESHHOLDING
, "Threshholding" },
295 { TIFF_TAG_CELL_WIDTH
, "Cell Width" },
296 { TIFF_TAG_CELL_LENGTH
, "Cell Length" },
297 { TIFF_TAG_FILL_ORDER
, "Fill Order" },
298 { TIFF_TAG_DOCUMENT_NAME
, "Document Name" },
299 { TIFF_TAG_IMAGE_DESCRIPTION
, "Image Description" },
300 { TIFF_TAG_MAKE
, "Make" },
301 { TIFF_TAG_MODEL
, "Model" },
302 { TIFF_TAG_STRIP_OFFSETS
, "Strip Offsets" },
303 { TIFF_TAG_ORIENTATION
, "Orientation" },
304 { TIFF_TAG_SAMPLES_PER_PIXEL
, "Samples Per Pixel" },
305 { TIFF_TAG_ROWS_PER_STRIP
, "Rows Per Strip" },
306 { TIFF_TAG_STRIP_BYTE_COUNTS
, "Strip Byte Counts" },
307 { TIFF_TAG_MIN_SAMPLE_VALUE
, "Min Sample Value" },
308 { TIFF_TAG_MAX_SAMPLE_VALUE
, "Max Sample Value" },
309 { TIFF_TAG_X_RESOLUTION
, "X Resolution" },
310 { TIFF_TAG_Y_RESOLUTION
, "Y Resolution" },
311 { TIFF_TAG_PLANAR_CONFIGURATION
, "Planar Configuration" },
312 { TIFF_TAG_PAGE_NAME
, "Page Name" },
313 { TIFF_TAG_X_POSITION
, "X Position" },
314 { TIFF_TAG_Y_POSITION
, "Y Position" },
315 { TIFF_TAG_FREE_OFFSETS
, "Free Offsets" },
316 { TIFF_TAG_FREE_BYTE_COUNTS
, "Free Byte Counts" },
317 { TIFF_TAG_GRAY_RESPONSE_UNIT
, "Gray Response Unit" },
318 { TIFF_TAG_GRAY_RESPONSE_CURVE
, "Gray Response Curve" },
319 { TIFF_TAG_T4_OPTIONS
, "T4 Options" },
320 { TIFF_TAG_T6_OPTIONS
, "T6 Options" },
321 { TIFF_TAG_RESOLUTION_UNIT
, "Resolution Unit" },
322 { TIFF_TAG_PAGE_NUMBER
, "Page Number" },
323 { TIFF_TAG_TRANSFER_FUNCTION
, "Transfer Function" },
324 { TIFF_TAG_SOFTWARE
, "Software" },
325 { TIFF_TAG_DATE_TIME
, "Date Time" },
326 { TIFF_TAG_ARTIST
, "Artist" },
327 { TIFF_TAG_HOST_COMPUTER
, "Host Computer" },
328 { TIFF_TAG_PREDICTOR
, "Predictor" },
329 { TIFF_TAG_WHITE_POINT
, "White Point" },
330 { TIFF_TAG_PRIMARY_CHROMATICITIES
, "Primary Chromaticities" },
331 { TIFF_TAG_COLOR_MAP
, "Color Map" },
332 { TIFF_TAG_HALFTONE_HINTS
, "Halftone Hints" },
333 { TIFF_TAG_TILE_WIDTH
, "Tile Width" },
334 { TIFF_TAG_TILE_LENGTH
, "Tile Length" },
335 { TIFF_TAG_TILE_OFFSETS
, "Tile Offsets" },
336 { TIFF_TAG_TILE_BYTE_COUNTS
, "Tile Byte Counts" },
337 { TIFF_TAG_INK_SET
, "Ink Set" },
338 { TIFF_TAG_INK_NAMES
, "Ink Names" },
339 { TIFF_TAG_NUMBER_OF_INKS
, "Number Of Inks" },
340 { TIFF_TAG_DOT_RANGE
, "Dot Range" },
341 { TIFF_TAG_TARGET_PRINTER
, "Target Printer" },
342 { TIFF_TAG_EXTRA_SAMPLES
, "Extra Samples" },
343 { TIFF_TAG_SAMPLE_FORMAT
, "Sample Format" },
344 { TIFF_TAG_S_MIN_SAMPLE_VALUE
, "S Min Sample Value" },
345 { TIFF_TAG_S_MAX_SAMPLE_VALUE
, "S Max Sample Value" },
346 { TIFF_TAG_TRANSFER_RANGE
, "Transfer Range" },
347 { TIFF_TAG_JPEG_PROC
, "JPEG Proc" },
348 { TIFF_TAG_JPEG_INTERCHANGE_FORMAT
, "JPEG Interchange Format" },
349 { TIFF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH
, "JPEG Interchange Format Length" },
350 { TIFF_TAG_JPEG_RESTART_INTERVAL
, "JPEG Restart Interval" },
351 { TIFF_TAG_JPEG_LOSSLESS_PREDICTORS
, "JPEG Lossless Predictors" },
352 { TIFF_TAG_JPEG_POINT_TRANSFORMS
, "JPEG Point Transforms" },
353 { TIFF_TAG_JPEG_Q_TABLES
, "JPEG Q Tables" },
354 { TIFF_TAG_JPEG_DC_TABLES
, "JPEG DC Tables" },
355 { TIFF_TAG_JPEG_AC_TABLES
, "JPEG AC Tables" },
356 { TIFF_TAG_YCBCR_COEFFICIENTS
, "YCbCr Coefficients" },
357 { TIFF_TAG_YCBCR_SUBSAMPLING
, "YCbCr Subsampling" },
358 { TIFF_TAG_YCBCR_POSITIONING
, "YCbCr Positioning" },
359 { TIFF_TAG_REFERENCE_BLACK_WHITE
, "Reference Black White" },
360 { TIFF_TAG_COPYRIGHT
, "Copyright" },
364 #define TIFF_TYPE_BYTE 1
365 #define TIFF_TYPE_ASCII 2
366 #define TIFF_TYPE_SHORT 3
367 #define TIFF_TYPE_LONG 4
368 #define TIFF_TYPE_RATIONAL 5
369 #define TIFF_TYPE_SBYTE 6
370 #define TIFF_TYPE_UNDEFINED 7
371 #define TIFF_TYPE_SSHORT 8
372 #define TIFF_TYPE_SLONG 9
373 #define TIFF_TYPE_SRATIONAL 10
374 #define TIFF_TYPE_FLOAT 11
375 #define TIFF_TYPE_DOUBLE 12
377 static const value_string tiff_type_names
[] = {
378 { TIFF_TYPE_BYTE
, "Byte" },
379 { TIFF_TYPE_ASCII
, "ASCII" },
380 { TIFF_TYPE_SHORT
, "Unsigned Short" },
381 { TIFF_TYPE_LONG
, "Unsigned Long" },
382 { TIFF_TYPE_RATIONAL
, "Rational" },
383 { TIFF_TYPE_SBYTE
, "Signed Byte" },
384 { TIFF_TYPE_UNDEFINED
, "Undefined" },
385 { TIFF_TYPE_SSHORT
, "Signed Short" },
386 { TIFF_TYPE_SLONG
, "Signed Long" },
387 { TIFF_TYPE_SRATIONAL
, "Signed Rational" },
388 { TIFF_TYPE_FLOAT
, "Float" },
389 { TIFF_TYPE_DOUBLE
, "Double" },
393 static const value_string tiff_compression_names
[] = {
394 { 1, "Uncompressed" },
396 { 3, "Group 3 Fax" },
397 { 4, "Group 4 Fax" },
400 { 32773, "PackBits" },
404 static const value_string tiff_photometric_interp_names
[] = {
405 { 0, "White is Zero" },
406 { 1, "Black is Zero" },
408 { 3, "RGB Palette" },
409 { 4, "Transparency Mask" },
412 /* N.B. no value for 7 (see Appendix A) */
417 static const value_string tiff_threshholding_names
[] = {
424 static const value_string tiff_fill_order_names
[] = {
425 { 1, "High-order first" },
426 { 2, "Low-order first" },
430 static const value_string tiff_orientation_names
[] = {
431 { 1, "Origin at Top-Left, Horizontal Rows" },
432 { 2, "Origin at Top-Right, Horizontal Rows" },
433 { 3, "Origin at Bottom-Right, Horizontal Rows" },
434 { 4, "Origin at Bottom-Left, Horizontal Rows" },
435 { 5, "Origin at Top-Left, Vertical Rows" },
436 { 6, "Origin at Top-Right, Vertical Rows" },
437 { 7, "Origin at Bottom-Right, Vertical Rows" },
438 { 8, "Origin at Bottom-Left, Vertical Rows" },
442 static const value_string tiff_planar_configuration_names
[] = {
448 static const value_string tiff_gray_response_unit_names
[] = {
451 { 3, "Thousandths" },
452 { 4, "Ten-thousandths" },
453 { 5, "Hundred-thousandths" },
457 static const value_string tiff_allow_uncompressed_names
[] = {
458 { 0, "Not Allowed" },
463 static const value_string tiff_resolution_unit_names
[] = {
470 static const value_string tiff_predictor_names
[] = {
471 { 1, "No Predictor" },
472 { 2, "Horizontal Differencing" },
476 static const value_string tiff_ink_set_names
[] = {
482 // Return the length of the given data type.
484 // If the type isn't known, return -1.
486 tiff_type_len(const uint16_t type
) {
488 case TIFF_TYPE_BYTE
: return 1;
489 case TIFF_TYPE_ASCII
: return 1;
490 case TIFF_TYPE_SHORT
: return 2;
491 case TIFF_TYPE_LONG
: return 4;
492 case TIFF_TYPE_RATIONAL
: return 8;
493 case TIFF_TYPE_SBYTE
: return 1;
494 case TIFF_TYPE_UNDEFINED
: return 1;
495 case TIFF_TYPE_SSHORT
: return 2;
496 case TIFF_TYPE_SLONG
: return 4;
497 case TIFF_TYPE_SRATIONAL
: return 8;
498 case TIFF_TYPE_FLOAT
: return 4;
499 case TIFF_TYPE_DOUBLE
: return 8;
505 // Return the length of the given array of data.
507 // If the type isn't known, return -1.
509 tiff_data_len(const uint16_t type
, const uint32_t count
) {
510 const int field
= tiff_type_len(type
);
511 if (field
< 0) return -1;
512 else return field
* count
;
516 dissect_tiff_tag_unknown(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, uint32_t offset
, uint16_t type
, uint32_t count
, int encoding _U_
)
518 const int len
= tiff_data_len(type
, count
);
520 expert_add_info(pinfo
, tree
, &ei_tiff_unknown_tag
);
522 uint32_t item_offset
;
524 // If we can't determine the length, that's an issue
525 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Could not determine length of entry");
527 } else if (len
<= 4) {
528 // If the length is <= 4, the item is located directly at the offset
529 item_offset
= offset
;
531 // If the length is >4, the offset is a pointer indicating where the item is located
532 proto_tree_add_item_ret_uint(tree
, hf_tiff_entry_offset
, tvb
, offset
, 4, encoding
, &item_offset
);
535 proto_tree_add_item(tree
, hf_tiff_entry_unknown
, tvb
, item_offset
, len
, encoding
);
539 dissect_tiff_single_uint(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, uint32_t offset
, uint16_t type
, uint32_t count
, int encoding
, int hfindex
) {
541 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected a single item; found %d items", count
);
545 if (type
== TIFF_TYPE_BYTE
) {
546 proto_tree_add_item(tree
, hfindex
, tvb
, offset
, 1, encoding
);
547 } else if (type
== TIFF_TYPE_SHORT
) {
548 proto_tree_add_item(tree
, hfindex
, tvb
, offset
, 2, encoding
);
549 } else if (type
== TIFF_TYPE_LONG
) {
550 proto_tree_add_item(tree
, hfindex
, tvb
, offset
, 4, encoding
);
552 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected an unsigned integer, found type %s", val_to_str_const(type
, tiff_type_names
, "Unknown"));
557 dissect_tiff_array_uint(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, uint32_t offset
, uint16_t type
, uint32_t count
, int encoding
, int hfindex
) {
558 if (!(type
== TIFF_TYPE_BYTE
|| type
== TIFF_TYPE_SHORT
|| type
== TIFF_TYPE_LONG
)) {
559 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected an unsigned integer, found type %s", val_to_str_const(type
, tiff_type_names
, "Unknown"));
564 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "At least 1 item; found %d items", count
);
568 const int item_len
= tiff_type_len(type
);
569 const int len
= tiff_data_len(type
, count
);
571 uint32_t item_offset
;
572 if (len
<= 0 || item_len
<= 0) {
573 // If we can't determine the length, that's an issue
574 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Could not determine length of entry");
576 } else if (len
<= 4) {
577 // If the length is <= 4, the item is located directly at the offset
578 item_offset
= offset
;
580 // If the length is >4, the offset is a pointer indicating where the item is located
581 proto_tree_add_item_ret_uint(tree
, hf_tiff_entry_offset
, tvb
, offset
, 4, encoding
, &item_offset
);
585 for (uint32_t i
= 0; i
< count
; i
++) {
586 proto_tree_add_item(tree
, hfindex
, tvb
, item_offset
+ item_len
* i
, item_len
, encoding
);
591 dissect_tiff_single_string(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, uint16_t type
, uint32_t count
, int encoding
, int hfindex
) {
592 if (type
!= TIFF_TYPE_ASCII
) {
593 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected an ASCII string");
597 uint32_t item_offset
;
599 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected at least one byte for an ASCII string; got zero");
601 } else if (count
<= 4) {
602 // If there are 4 or fewer bytes, the string is embedded in the pointer
603 item_offset
= offset
;
605 // If the length is >4, the offset is a pointer indicating where the item is located
606 proto_tree_add_item_ret_uint(tree
, hf_tiff_entry_offset
, tvb
, offset
, 4, encoding
, &item_offset
);
609 proto_tree_add_item(tree
, hfindex
, tvb
, item_offset
, count
, ENC_ASCII
);
613 dissect_tiff_single_urational(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, uint16_t type
, uint32_t count
, int encoding
, int hfnumer
, int hfdenom
, int hfapprox
) {
615 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected a single item; found %d items", count
);
619 if (type
!= TIFF_TYPE_RATIONAL
) {
620 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected an unsigned rational");
624 uint32_t item_offset
;
625 proto_tree_add_item_ret_uint(tree
, hf_tiff_entry_offset
, tvb
, offset
, 4, encoding
, &item_offset
);
629 proto_tree_add_item_ret_uint(tree
, hfnumer
, tvb
, item_offset
, 4, encoding
, &numer
);
630 proto_item
*denom_ti
= proto_tree_add_item_ret_uint(tree
, hfdenom
, tvb
, item_offset
+ 4, 4, encoding
, &denom
);
633 proto_item
*approx_item
= proto_tree_add_double(tree
, hfapprox
, tvb
, item_offset
, 8, (double)numer
/ (double)denom
);
634 proto_item_set_generated(approx_item
);
637 expert_add_info_format(pinfo
, denom_ti
, &ei_tiff_zero_denom
, "Denominator is zero");
642 dissect_tiff_t6_options(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, unsigned offset
, uint16_t type
, uint32_t count
, int encoding
) {
644 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected a single item; found %d items", count
);
648 if (type
!= TIFF_TYPE_LONG
) {
649 expert_add_info_format(pinfo
, tree
, &ei_tiff_bad_entry
, "Expected an unsigned long");
653 proto_item
*t6_ti
= proto_tree_add_item(tree
, hf_tiff_t6_options
, tvb
, offset
, 4, encoding
);
654 proto_tree
*t6_tree
= proto_item_add_subtree(t6_ti
, ett_t6
);
655 proto_tree_add_item(t6_tree
, hf_tiff_t6_unused
, tvb
, offset
, 4, encoding
);
656 proto_tree_add_item(t6_tree
, hf_tiff_t6_allow_uncompresed
, tvb
, offset
, 4, encoding
);
660 dissect_tiff_entry(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, uint32_t offset
, int encoding
) {
661 const uint16_t tag
= tvb_get_uint16(tvb
, offset
, encoding
);
663 proto_tree
*entry_tree
= proto_tree_add_subtree_format(tree
, tvb
, offset
, 12, ett_ifd
, NULL
, "%s", val_to_str_const(tag
, tiff_tag_names
, "Unknown Entry"));
665 proto_tree_add_item(entry_tree
, hf_tiff_entry_tag
, tvb
, offset
, 2, encoding
);
669 proto_tree_add_item_ret_uint(entry_tree
, hf_tiff_entry_type
, tvb
, offset
+ 2, 2, encoding
, &type
);
670 proto_tree_add_item_ret_uint(entry_tree
, hf_tiff_entry_count
, tvb
, offset
+ 4, 4, encoding
, &count
);
673 case TIFF_TAG_IMAGE_WIDTH
:
674 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_image_width
);
676 case TIFF_TAG_IMAGE_LENGTH
:
677 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_image_length
);
679 case TIFF_TAG_BITS_PER_SAMPLE
:
680 dissect_tiff_array_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_bits_per_sample
);
682 case TIFF_TAG_COMPRESSION
:
683 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_compression
);
685 case TIFF_TAG_PHOTOMETRIC_INTERPRETATION
:
686 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_photometric_interp
);
688 case TIFF_TAG_THRESHHOLDING
:
689 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_threshholding
);
691 case TIFF_TAG_CELL_WIDTH
:
692 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_cell_width
);
694 case TIFF_TAG_CELL_LENGTH
:
695 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_cell_length
);
697 case TIFF_TAG_FILL_ORDER
:
698 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_fill_order
);
700 case TIFF_TAG_DOCUMENT_NAME
:
701 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_document_name
);
703 case TIFF_TAG_IMAGE_DESCRIPTION
:
704 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_image_description
);
707 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_make
);
710 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_model
);
712 case TIFF_TAG_STRIP_OFFSETS
:
713 dissect_tiff_array_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_strip_offset
);
715 case TIFF_TAG_ORIENTATION
:
716 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_orientation
);
718 case TIFF_TAG_SAMPLES_PER_PIXEL
:
719 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_samples_per_pixel
);
721 case TIFF_TAG_ROWS_PER_STRIP
:
722 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_rows_per_strip
);
724 case TIFF_TAG_STRIP_BYTE_COUNTS
:
725 dissect_tiff_array_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_strip_byte_count
);
727 case TIFF_TAG_X_RESOLUTION
:
728 dissect_tiff_single_urational(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_x_res_numer
, hf_tiff_x_res_denom
, hf_tiff_x_res_approx
);
730 case TIFF_TAG_Y_RESOLUTION
:
731 dissect_tiff_single_urational(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_y_res_numer
, hf_tiff_y_res_denom
, hf_tiff_y_res_approx
);
733 case TIFF_TAG_PLANAR_CONFIGURATION
:
734 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_planar_configuration
);
736 case TIFF_TAG_PAGE_NAME
:
737 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_page_name
);
739 case TIFF_TAG_GRAY_RESPONSE_UNIT
:
740 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_gray_response_unit
);
742 case TIFF_TAG_T6_OPTIONS
:
743 dissect_tiff_t6_options(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
);
745 case TIFF_TAG_RESOLUTION_UNIT
:
746 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_resolution_unit
);
748 case TIFF_TAG_SOFTWARE
:
749 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_software
);
751 case TIFF_TAG_DATE_TIME
:
752 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_date_time
);
754 case TIFF_TAG_ARTIST
:
755 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_artist
);
757 case TIFF_TAG_HOST_COMPUTER
:
758 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_host_computer
);
760 case TIFF_TAG_PREDICTOR
:
761 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_predictor
);
763 case TIFF_TAG_TILE_WIDTH
:
764 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_tile_width
);
766 case TIFF_TAG_TILE_LENGTH
:
767 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_tile_length
);
769 case TIFF_TAG_INK_SET
:
770 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_ink_set
);
772 case TIFF_TAG_NUMBER_OF_INKS
:
773 dissect_tiff_single_uint(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_number_of_inks
);
775 case TIFF_TAG_TARGET_PRINTER
:
776 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_target_printer
);
778 case TIFF_TAG_COPYRIGHT
:
779 dissect_tiff_single_string(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
, hf_tiff_copyright
);
782 dissect_tiff_tag_unknown(tvb
, pinfo
, entry_tree
, offset
+ 8, type
, count
, encoding
);
786 // Dissect an IFD with all of its fields, starting at the given offset
788 // Return the offset of the next IFD, or 0 if there isn't one
790 dissect_tiff_ifd(tvbuff_t
*tvb
, packet_info
*pinfo _U_
, proto_tree
*tree
, uint32_t offset
, int encoding
) {
791 uint16_t ifd_count
= tvb_get_uint16(tvb
, offset
, encoding
);
792 int ifd_length
= 2 + (ifd_count
* 12) + 4;
794 proto_tree
*ifd_tree
= proto_tree_add_subtree(tree
, tvb
, offset
, ifd_length
, ett_ifd
, NULL
, "Image File Directory");
796 proto_tree_add_item(ifd_tree
, hf_tiff_ifd_count
, tvb
, offset
, 2, encoding
);
799 for (int i
= 0; i
< ifd_count
; i
++) {
800 dissect_tiff_entry(tvb
, pinfo
, ifd_tree
, offset
, encoding
);
804 proto_tree_add_item(ifd_tree
, hf_tiff_ifd_next
, tvb
, offset
, 4, encoding
);
805 uint32_t ifd_next
= tvb_get_uint32(tvb
, offset
, encoding
);
811 dissect_tiff(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
) {
814 // Reject if we don't have enough room for the heuristics
815 if (tvb_captured_length(tvb
) < 4) {
819 // Figure out if we're big-endian or little endian
820 uint16_t raw_encoding
= tvb_get_ntohs(tvb
, 0);
823 if (raw_encoding
== 0x4949) {
824 encoding
= ENC_LITTLE_ENDIAN
;
825 } else if (raw_encoding
== 0x4D4D) {
826 encoding
= ENC_BIG_ENDIAN
;
828 // If we don't recognize the endianness, abort with nothing decoded
832 magic
= tvb_get_uint16(tvb
, 2, encoding
);
834 // If the magic number isn't 42, abort with nothing decoded
839 proto_item
*ti
= proto_tree_add_item(tree
, proto_tiff
, tvb
, 0, -1, ENC_NA
);
840 proto_tree
*tiff_tree
= proto_item_add_subtree(ti
, ett_tiff
);
842 // Dissect the rest of the header
843 proto_tree_add_item(tiff_tree
, hf_tiff_header_endianness
, tvb
, 0, 2, encoding
);
844 proto_tree_add_item(tiff_tree
, hf_tiff_header_magic
, tvb
, 2, 2, encoding
);
845 proto_tree_add_item_ret_uint(tiff_tree
, hf_tiff_header_lead_ifd
, tvb
, 4, 4, encoding
, &ifd_offset
);
847 // Keep dissecting IFDs until the offset to the next one is zero
848 while (ifd_offset
!= 0) {
849 ifd_offset
= dissect_tiff_ifd(tvb
, pinfo
, tiff_tree
, ifd_offset
, encoding
);
852 return tvb_captured_length(tvb
);
856 proto_register_tiff(void)
858 static hf_register_info hf
[] = {
859 { &hf_tiff_header_endianness
,
860 { "Endianness", "tiff.endianness",
861 FT_UINT16
, BASE_HEX
, VALS(tiff_endianness_names
),
864 { &hf_tiff_header_magic
,
865 { "Magic", "tiff.magic",
866 FT_UINT16
, BASE_HEX
, NULL
,
869 { &hf_tiff_header_lead_ifd
,
870 { "Lead IFD Offset", "tiff.lead_ifd",
871 FT_UINT32
, BASE_HEX
, NULL
,
874 { &hf_tiff_ifd_count
,
875 { "Number of Entries", "tiff.ifd_count",
876 FT_UINT16
, BASE_DEC
, NULL
,
880 { "Next IFD Offset", "tiff.next_ifd",
881 FT_UINT32
, BASE_HEX
, NULL
,
884 { &hf_tiff_entry_tag
,
886 FT_UINT16
, BASE_DEC
, VALS(tiff_tag_names
),
889 { &hf_tiff_entry_type
,
890 { "Type", "tiff.type",
891 FT_UINT16
, BASE_DEC
, VALS(tiff_type_names
),
894 { &hf_tiff_entry_count
,
895 { "Count", "tiff.count",
896 FT_UINT32
, BASE_DEC
, NULL
,
899 { &hf_tiff_entry_offset
,
900 { "Offset", "tiff.offset",
901 FT_UINT32
, BASE_DEC
, NULL
,
904 { &hf_tiff_entry_unknown
,
905 { "Unknown Data", "tiff.unknown",
906 FT_BYTES
, BASE_NONE
, NULL
,
909 { &hf_tiff_image_width
,
910 { "Image Width", "tiff.image_width",
911 FT_UINT32
, BASE_DEC
, NULL
,
914 { &hf_tiff_image_length
,
915 { "Image Length", "tiff.image_length",
916 FT_UINT32
, BASE_DEC
, NULL
,
919 { &hf_tiff_bits_per_sample
,
920 { "Bits per Sample", "tiff.bits_per_sample",
921 FT_UINT16
, BASE_DEC
, NULL
,
924 { &hf_tiff_compression
,
925 { "Compression", "tiff.compression",
926 FT_UINT16
, BASE_DEC
, VALS(tiff_compression_names
),
929 { &hf_tiff_photometric_interp
,
930 { "Photometric Interpretation", "tiff.photometric_interp",
931 FT_UINT16
, BASE_DEC
, VALS(tiff_photometric_interp_names
),
934 { &hf_tiff_threshholding
,
935 { "Threshholding", "tiff.threshholding",
936 FT_UINT16
, BASE_DEC
, VALS(tiff_threshholding_names
),
939 { &hf_tiff_cell_width
,
940 { "Cell Width", "tiff.cell_width",
941 FT_UINT16
, BASE_DEC
, NULL
,
944 { &hf_tiff_cell_length
,
945 { "Cell Length", "tiff.cell_length",
946 FT_UINT16
, BASE_DEC
, NULL
,
949 { &hf_tiff_fill_order
,
950 { "Fill Order", "tiff.fill_order",
951 FT_UINT16
, BASE_DEC
, VALS(tiff_fill_order_names
),
954 { &hf_tiff_document_name
,
955 { "Document Name", "tiff.document_name",
956 FT_STRINGZ
, BASE_NONE
, NULL
,
959 { &hf_tiff_image_description
,
960 { "Image Description", "tiff.image_description",
961 FT_STRINGZ
, BASE_NONE
, NULL
,
965 { "Make", "tiff.make",
966 FT_STRINGZ
, BASE_NONE
, NULL
,
970 { "Model", "tiff.model",
971 FT_STRINGZ
, BASE_NONE
, NULL
,
974 { &hf_tiff_strip_offset
,
975 { "Strip Offset", "tiff.strip_offset",
976 FT_UINT32
, BASE_DEC
, NULL
,
979 { &hf_tiff_orientation
,
980 { "Orientation", "tiff.orientation",
981 FT_UINT16
, BASE_DEC
, VALS(tiff_orientation_names
),
984 { &hf_tiff_samples_per_pixel
,
985 { "Samples per Pixel", "tiff.samples_per_pixel",
986 FT_UINT16
, BASE_DEC
, NULL
,
989 { &hf_tiff_rows_per_strip
,
990 { "Rows per Strip", "tiff.rows_per_strip",
991 FT_UINT16
, BASE_DEC
, NULL
,
994 { &hf_tiff_strip_byte_count
,
995 { "Strip Byte Count", "tiff.strip_byte_count",
996 FT_UINT32
, BASE_DEC
, NULL
,
999 { &hf_tiff_x_res_numer
,
1000 { "X Resolution Numerator", "tiff.x_res_numer",
1001 FT_UINT32
, BASE_DEC
, NULL
,
1004 { &hf_tiff_x_res_denom
,
1005 { "X Resolution Denominator", "tiff.x_res_denom",
1006 FT_UINT32
, BASE_DEC
, NULL
,
1009 { &hf_tiff_x_res_approx
,
1010 { "X Resolution Approximation", "tiff.x_res_approx",
1011 FT_DOUBLE
, BASE_NONE
, NULL
,
1014 { &hf_tiff_y_res_numer
,
1015 { "Y Resolution Numerator", "tiff.y_res_numer",
1016 FT_UINT32
, BASE_DEC
, NULL
,
1019 { &hf_tiff_y_res_denom
,
1020 { "Y Resolution Denominator", "tiff.y_res_denom",
1021 FT_UINT32
, BASE_DEC
, NULL
,
1024 { &hf_tiff_y_res_approx
,
1025 { "Y Resolution Approximation", "tiff.y_res_approx",
1026 FT_DOUBLE
, BASE_NONE
, NULL
,
1029 { &hf_tiff_planar_configuration
,
1030 { "Planar Configuration", "tiff.planar_configuration",
1031 FT_UINT16
, BASE_DEC
, VALS(tiff_planar_configuration_names
),
1034 { &hf_tiff_page_name
,
1035 { "Page Name", "tiff.page_name",
1036 FT_STRINGZ
, BASE_NONE
, NULL
,
1039 { &hf_tiff_gray_response_unit
,
1040 { "Gray Response Unit", "tiff.gray_response_unit",
1041 FT_UINT16
, BASE_DEC
, VALS(tiff_gray_response_unit_names
),
1044 { &hf_tiff_t6_options
,
1045 { "T6 Options", "tiff.t6",
1046 FT_UINT32
, BASE_HEX
, NULL
,
1049 { &hf_tiff_t6_unused
,
1050 { "Unused", "tiff.t6.unused",
1051 FT_UINT32
, BASE_HEX
, NULL
,
1052 0xFFFFFFFD, NULL
, HFILL
}
1054 { &hf_tiff_t6_allow_uncompresed
,
1055 { "Allow Uncompressed", "tiff.t6.allow_uncompressed",
1056 FT_UINT32
, BASE_HEX
, VALS(tiff_allow_uncompressed_names
),
1057 0x00000002, NULL
, HFILL
}
1059 { &hf_tiff_resolution_unit
,
1060 { "Resolution Unit", "tiff.resolution_unit",
1061 FT_UINT16
, BASE_DEC
, VALS(tiff_resolution_unit_names
),
1064 { &hf_tiff_software
,
1065 { "Software", "tiff.software",
1066 FT_STRINGZ
, BASE_NONE
, NULL
,
1069 { &hf_tiff_date_time
,
1070 { "Date/Time", "tiff.date_time",
1071 FT_STRINGZ
, BASE_NONE
, NULL
,
1075 { "Artist", "tiff.artist",
1076 FT_STRINGZ
, BASE_NONE
, NULL
,
1079 { &hf_tiff_host_computer
,
1080 { "Host Computer", "tiff.host_computer",
1081 FT_STRINGZ
, BASE_NONE
, NULL
,
1084 { &hf_tiff_predictor
,
1085 { "Predictor", "tiff.predictor",
1086 FT_UINT16
, BASE_DEC
, VALS(tiff_predictor_names
),
1089 { &hf_tiff_tile_width
,
1090 { "Tile Width", "tiff.tile_width",
1091 FT_UINT32
, BASE_DEC
, NULL
,
1094 { &hf_tiff_tile_length
,
1095 { "Tile Width", "tiff.tile_length",
1096 FT_UINT32
, BASE_DEC
, NULL
,
1100 { "Ink Set", "tiff.ink_set",
1101 FT_UINT16
, BASE_DEC
, VALS(tiff_ink_set_names
),
1104 { &hf_tiff_number_of_inks
,
1105 { "Number of Inks", "tiff.number_of_inks",
1106 FT_UINT16
, BASE_DEC
, NULL
,
1109 { &hf_tiff_target_printer
,
1110 { "Target Printer", "tiff.target_printer",
1111 FT_STRINGZ
, BASE_NONE
, NULL
,
1114 { &hf_tiff_copyright
,
1115 { "Copyright", "tiff.copyright",
1116 FT_STRINGZ
, BASE_NONE
, NULL
,
1121 static int *ett
[] = {
1127 static ei_register_info ei
[] = {
1128 { &ei_tiff_unknown_tag
,
1129 { "tiff.unknown_tag", PI_UNDECODED
, PI_NOTE
,
1130 "Unknown tag", EXPFILL
}
1132 { &ei_tiff_bad_entry
,
1133 { "tiff.bad_entry", PI_PROTOCOL
, PI_WARN
,
1134 "Invalid entry contents", EXPFILL
}
1136 { &ei_tiff_zero_denom
,
1137 { "tiff.zero_denom", PI_PROTOCOL
, PI_WARN
,
1138 "Zero denominator", EXPFILL
}
1143 proto_tiff
= proto_register_protocol("Tagged Image File Format", "TIFF image", "tiff");
1145 register_dissector("tiff", dissect_tiff
, proto_tiff
);
1146 proto_register_field_array(proto_tiff
, hf
, array_length(hf
));
1147 proto_register_subtree_array(ett
, array_length(ett
));
1149 expert_module_t
*expert_tiff
= expert_register_protocol(proto_tiff
);
1150 expert_register_field_array(expert_tiff
, ei
, array_length(ei
));
1154 dissect_tiff_heur(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data
)
1156 return dissect_tiff(tvb
, pinfo
, tree
, data
) > 0;
1160 proto_reg_handoff_tiff(void)
1162 dissector_handle_t tiff_handle
= find_dissector("tiff");
1164 // Register the TIFF media type
1165 dissector_add_string("media_type", "image/tiff", tiff_handle
);
1167 // Register the TIFF heuristic dissector
1168 heur_dissector_add("wtap_file", dissect_tiff_heur
, "TIFF file", "tiff_wtap", proto_tiff
, HEURISTIC_ENABLE
);