forked from hfiguiere/exifprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
4751 lines (4544 loc) · 207 KB
/
process.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* EXIFPROBE - TIFF/JPEG/EXIF image file probe */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* Copyright (C) 2002, 2005 by Duane H. Hesser. All rights reserved. */
/* */
/* See the file LICENSE.EXIFPROBE for terms of use. */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
#ifndef lint
static char *ModuleId = "@(#) $Id: process.c,v 1.47 2005/07/25 22:05:10 alex Exp $";
#endif
/* Process segment types such as TIFF IFD, JPEG APP segments, and */
/* JPEG basic segments. */
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include "defs.h"
#include "summary.h"
#include "maker.h"
#include "datadefs.h"
#include "misc.h"
#include "tags.h"
#include "extern.h"
#include "ciff.h"
/* Read and decode a TIFF IFD, attempting to describe the location */
/* and structure of the data as it is read. Entry values which live */
/* at an offset from the entry are displayed following the last entry */
/* in the IFD (actually after the "next ifd offset") if */
/* VALUE_AT_OFFSET is set in Print_options, so that most IFDS will */
/* have a VALUES section following the entry lines, in just the */
/* manner that the file is laid out by spec. Tag names for offset */
/* values are repeated when the value is printed. If VALUE_AT_OFFSET */
/* is not set, the value is printed immediately, unless the tag */
/* represents a subsection, in which case the offset of the */
/* subsection is printed as the tag value, and the subsection handled */
/* in a second pass, after all entries are done. */
/* Returns a file offset which represents the "highest" position of */
/* the file pointer read by this routine and its subroutines, which */
/* may be used by the caller to read the remainder of the file. This */
/* number (less one) is printed as the offset of the end of the IFD. */
/* At the same time, the function notices non-zero values of "next */
/* ifd offset" which may be contained in this ifd, and passes it as */
/* "max_offset" to routines which read subifds, on the assumption */
/* that this ifd should not write into the next ifd. That value is */
/* used to mark segments which appear to be out of order in the file. */
/* If the routine detects by this means that *this* ifd is outside */
/* its natural bounds, and cannot be contained within its parent, it */
/* returns 0, rather than the maximum value offset, so that the */
/* parent routine will not improperly alter its maximum offset. */
/* The arguments are the byteorder to be used when reading integer */
/* data (usually taken from the TIFF header), the offset of the start */
/* of the directory, relative to the TIFF header, the offset of the */
/* TIFF header from the beginning of the file, the sequence number of */
/* the IFD, and formatting values (indent and address width) to be */
/* used when printing. */
/* The routine also attempts to record the location, size, image type */
/* and compression method used for actual image data contained within */
/* or referenced by a tiff IFD. This information is printed at the */
/* end, as a summary of images found in the file. */
unsigned long
process_tiff_ifd(FILE *inptr,unsigned short byteorder,unsigned long ifd_offset,
unsigned long fileoffset_base,unsigned long max_offset,
struct image_summary *summary_entry,char *parent_name,
int ifdtype,int ifdnum,int subifdnum,int indent)
{
struct ifd_entry *entry_ptr = NULL;
struct image_summary *tmp_summary_entry;
unsigned long cur_ifd_offset,next_ifd_offset,current_offset;
unsigned long start_entry_offset,entry_offset,value_offset;
unsigned long value_start_offset = 0UL;
unsigned long offset_limit = 0UL;
unsigned long max_value_offset = 0UL;
unsigned long max_ifd_offset = 0UL;
unsigned long thn_offset = 0UL; /* JPegInterchangeFormat */
unsigned long thn_length = 0UL; /* JpegInterChangeFormat */
unsigned long tmp_length = 0L;
unsigned long alt_length = 0L;
unsigned long limit_offset = 0L;
unsigned long filesize = 0UL;
unsigned short marker,alt_byteorder = 0;
int invalid_entry = 0;
int value_is_offset = 0;
int use_second_pass = 0;
int status = 0;
int chpr = 0;
int entry_num,num_entries,i;
char dirnamebuf[16];
char *ifdname,*prefix,*dirname,*nameoftag,*tprefix;
char *fulldirname = CNULL;
char *listname = CNULL;
if(inptr == (FILE *)0)
{
fprintf(stderr,"%s: no open file pointer to read TIFF IFD\n",
Progname);
return(0L);
}
if(Debug & OUT_DEBUG)
{
printf("PS=%d,",(Print_options & PRINT_SECTION) > 0);
printf("PV=%d,",(Print_options & PRINT_VALUE) > 0);
printf("PO=%d,",(Print_options & PRINT_OFFSET) > 0);
printf("PVAO=%d\n",(Print_options & PRINT_VALUE_AT_OFFSET) > 0);
}
filesize = get_filesize(inptr);
next_ifd_offset = ifd_offset + fileoffset_base;
while(next_ifd_offset)
{
clearerr(inptr);
/* max_offset, if set, is the maximum offset which the parent */
/* is willing to claim. max_ifd_offset is the maximum offset */
/* reached by this ifd, which may be constrained by the */
/* "next_ifd_offset" of chained ifds. */
cur_ifd_offset = next_ifd_offset;
if(max_offset && (cur_ifd_offset > max_offset))
prefix = ">"; /* outside the parent */
else
prefix = "@";
print_tag_address(SECTION,cur_ifd_offset,indent,prefix);
switch(ifdtype)
{
case TIFF_IFD:
if(PRINT_SECTION)
chpr += printf("<IFD %d>",ifdnum);
ifdname = "IFD";
sprintf(dirnamebuf,"Ifd%d",ifdnum);
dirname = dirnamebuf;
break;
case TIFF_SUBIFD:
if(PRINT_SECTION)
chpr += printf("<SubIFD %d of IFD %d>",subifdnum,ifdnum);
ifdname = "SubIFD";
sprintf(dirnamebuf,"SubIfd%d",subifdnum);
dirname = dirnamebuf;
break;
case INTEROP_IFD:
if(PRINT_SECTION)
chpr += printf("<Interoperability SubIFD>");
ifdname = "Interoperability SubIFD";
dirname = "Interop";
break;
case GPS_IFD:
if(PRINT_SECTION)
chpr += printf("<GPS SubIFD>");
ifdname = "GPS SubIFD";
dirname = "Gps";
break;
case EXIF_IFD: /* This shouldn't happen */
if(PRINT_SECTION)
chpr += printf("<EXIF IFD>");
ifdname = "EXIF IFD";
dirname = "Exif";
break;
case MAKER_SUBIFD:
ifdname = "MakerNote SubIFD";
if(PRINT_SECTION)
chpr += printf("<%s>",ifdname);
dirname = CNULL;
break;
default:
if(PRINT_SECTION)
chpr += printf("<UNKNOWN IFD TYPE %d>",ifdtype); /* SECTION */
ifdname = "UNKNOWN IFD TYPE";
dirname = CNULL;
break;
}
if(dirname)
listname = fulldirname = splice(parent_name,".",dirname);
else
listname = parent_name;
num_entries = read_ushort(inptr,byteorder,cur_ifd_offset);
max_value_offset = max_ifd_offset = 0L;
if(ferror(inptr) || feof(inptr))
{
chpr = newline(chpr);
printred("#========= WARNING: FAILED to read number of entries for IFD at offset ");
chpr += printf("%lu",cur_ifd_offset);
if(ferror(inptr))
why(stdout);
chpr = newline(chpr);
goto blewit;
}
/* The file pointer is now at the start of the IFD entries */
current_offset = entry_offset = start_entry_offset = ftell(inptr);
if(PRINT_SECTION)
{
chpr += printf(" %d entries ",num_entries);
chpr += printf("starting at file offset %#lx=%lu",
start_entry_offset,start_entry_offset);
chpr = newline(chpr);
}
if((summary_entry == NULL) || summary_entry->entry_lock)
summary_entry = new_summary_entry(summary_entry,0,ifdtype);
else
summary_entry = last_summary_entry(summary_entry);
if(summary_entry)
{
summary_entry->datatype = ifdtype;
summary_entry->imageformat = IMGFMT_NOIMAGE;
summary_entry->filesubformat |= FILESUBFMT_TIFF;
if((ifdtype == TIFF_IFD) || (ifdtype == TIFF_SUBIFD) || (ifdtype == MAKER_SUBIFD))
{
summary_entry->ifdnum = ifdnum;
summary_entry->subifdnum = subifdnum;
}
}
use_second_pass = value_is_offset = 0;
indent += SMALLINDENT;
for(entry_num = 0; entry_num < num_entries; ++entry_num)
{
entry_ptr = read_ifd_entry(inptr,byteorder,entry_offset);
if((entry_ptr->value_type == 0) || (entry_ptr->value_type > DOUBLE) ||
ferror(inptr) || feof(inptr))
{
print_tag_address(ENTRY,entry_offset,indent,prefix);
print_taginfo(entry_ptr,listname,SMALLINDENT,ifdtype,ifdnum,subifdnum);
if((PRINT_ENTRY))
printred(" INVALID ENTRY");
chpr = newline(chpr);
/* If there are a few invalid entries in an otherwise */
/* valid IFD, the invalid entries should be reported. */
/* An invalid IFD, with 50,000 entries or so, all */
/* invalid, should be nipped in the bud. */
clearerr(inptr);
current_offset = ftell(inptr);
if(max_offset > 0)
limit_offset = max_offset;
else
{
if(fseek(inptr,0L,SEEK_END) != -1)
{
limit_offset = ftell(inptr);
fseek(inptr,current_offset,SEEK_SET);
}
}
/* If there's an error on input, or we can't check */
/* for absurd num_entries, give up. */
if(!ferror(inptr) && !feof(inptr) && (limit_offset > 0))
{
/* If the number of entries would read past the */
/* size of the IFD, or past EOF, give up */
if((ifd_offset + (12 * num_entries)) < limit_offset)
{
/* Limit the number of consecutive failures. */
/* An apparently valid entry resets the count */
/* to 0. */
if(invalid_entry++ < MAX_INVALID_ENTRIES)
{
entry_offset = current_offset;
clearerr(inptr);
continue;
}
}
}
chpr = newline(chpr);
goto blewit;
}
invalid_entry = 0;
current_offset = ftell(inptr);
switch(entry_ptr->tag)
{
case TIFFTAG_OldSubFileType: /* old, deprecated */
if(summary_entry)
{
summary_entry->filesubformat |= FILESUBFMT_TIFFEP;
if(entry_ptr->value == 1)
summary_entry->subfiletype = PRIMARY_TYPE;
else if(entry_ptr->value == 2)
summary_entry->subfiletype = REDUCED_RES_TYPE;
else if(entry_ptr->value == 3)
summary_entry->subfiletype = PAGE_TYPE;
}
break;
case TIFFTAG_NewSubFileType:
if(summary_entry)
{
summary_entry->filesubformat |= FILESUBFMT_TIFFEP;
if(entry_ptr->value == 0)
summary_entry->subfiletype = PRIMARY_TYPE;
else if(entry_ptr->value == 1)
summary_entry->subfiletype = REDUCED_RES_TYPE;
else if(entry_ptr->value == 2)
summary_entry->subfiletype = PAGE_TYPE;
else if(entry_ptr->value == 3)
summary_entry->subfiletype = MASK_TYPE;
}
break;
case INTEROPTAG_RelatedImageWidth: /* ###%%% ??? */
case TIFFTAG_ImageWidth:
if(summary_entry)
summary_entry->pixel_width = entry_ptr->value;
break;
case INTEROPTAG_RelatedImageLength: /* ###%%% ??? */
case TIFFTAG_ImageLength:
if(summary_entry)
summary_entry->pixel_height = entry_ptr->value;
break;
case TIFFTAG_Compression:
if(summary_entry)
{
summary_entry->compression = entry_ptr->value;
if((entry_ptr->value == 7) || (entry_ptr->value == 6))
{
if(entry_ptr->value == 6)
summary_entry->filesubformat |= FILESUBFMT_TIFFOLD;
/* Some of this may have to be undone */
/* later, if we find out they lied */
summary_entry->entry_lock = lock_number(summary_entry);
if(summary_entry->imageformat == 0)
summary_entry->imageformat = IMGFMT_JPEG;
}
else
{
summary_entry->entry_lock = lock_number(summary_entry);
if(summary_entry->imageformat == 0)
summary_entry->imageformat = IMGFMT_TIFF;
if(summary_entry->compression == 34713)
summary_entry->filesubformat |= FILESUBFMT_NEF;
/* If there is no subfiletype tag, this */
/* could be the primary, or it could be a */
/* thumbnail */
if(summary_entry->subfiletype <= POSSIBLE_PRIMARY_TYPE)
summary_entry->subfiletype = POSSIBLE_PRIMARY_TYPE;
}
}
break;
case TIFFTAG_PhotometricInterpretation:
/* It appears that the PMI values used in Olympus */
/* ORF files (1 or 2 for the primary image) do */
/* not correspond to legitimate TIFF PMI values. */
/* Record them "as is" here, but they shouldn't */
/* be *interpreted* in the same way as TIFF files */
if(summary_entry)
summary_entry->imagesubformat = entry_ptr->value | IMGSUBFMT_VALUE_IS_PMI;
break;
case TIFFTAG_JPEGInterchangeFormat:
/* actually an offset... */
/* This method of JPEG-in-TIFF is discouraged by */
/* the TIFF spec (after Technote 2), for good and */
/* sufficient reason> */
thn_offset = entry_ptr->value + fileoffset_base;
break;
case TIFFTAG_JPEGInterchangeFormatLength:
thn_length = entry_ptr->value;
break;
case TIFFTAG_StripOffsets:
case TIFFTAG_TileOffsets:
/* If there are multiple strips, this will be */
/* wrong, but will be overwritten in the second */
/* pass. */
if(summary_entry)
{
summary_entry->offset = entry_ptr->value;
if((summary_entry->compression != 6) &&
(summary_entry->compression != 7))
{
summary_entry->entry_lock = lock_number(summary_entry);
}
}
break;
case TIFFTAG_StripByteCounts:
case TIFFTAG_TileByteCounts:
/* This may also be overwritten in the second */
/* pass */
if(summary_entry)
{
summary_entry->length = entry_ptr->value;
if((summary_entry->compression != 6) &&
(summary_entry->compression != 7))
{
summary_entry->entry_lock = lock_number(summary_entry);
}
}
break;
case TIFFEPTAG_TIFF_EPStandardID:
if(summary_entry)
summary_entry->filesubformat |= FILESUBFMT_TIFFEP;
break;
case TIFFTAG_Make:
case TIFFTAG_Model:
case TIFFTAG_Software:
{
char *makename,*modelname,*swname;
/* These items will be read and printed in */
/* the second pass, if one is made. This */
/* grabs them and records them in globals, */
/* which may be used later by the makernote */
/* code. */
switch(entry_ptr->tag)
{
case TIFFTAG_Make:
if(Make_name == NULL)
{
Make_name = strdup_value(entry_ptr,
inptr,
fileoffset_base);
}
break;
case TIFFTAG_Model:
if(Model_name == NULL)
{
Model_name = strdup_value(entry_ptr,
inptr,
fileoffset_base);
}
break;
case TIFFTAG_Software:
if(Software_name == NULL)
{
Software_name = strdup_value(entry_ptr,
inptr,
fileoffset_base);
}
break;
}
}
break;
case TIFFTAG_BitsPerSample:
if(summary_entry)
{
/* Do not override information from */
/* earlier sections; the tiff section in */
/* e.g. MRW sections can lie if the image */
/* has been improperly handled by */
/* other-party software. */
if(summary_entry->bps[0] == 0)
{
if(entry_ptr->count < 3)
{
summary_entry->bps[0] = (unsigned short)entry_ptr->value & 0xffff;
if(entry_ptr->count == 2)
summary_entry->bps[1] =
(unsigned short)((entry_ptr->value & 0xffff0000) >> 16);
}
else
{
summary_entry->bps[0] = read_ushort(inptr,byteorder,entry_ptr->value);
for(i = 1; (i < entry_ptr->count) && (i < MAXSAMPLE); ++i)
summary_entry->bps[i] = read_ushort(inptr,byteorder,HERE);
}
}
}
break;
case TIFFTAG_SamplesPerPixel:
if((summary_entry) && (summary_entry->spp == 0))
summary_entry->spp = entry_ptr->value;
break;
case DNGTAG_DNGVersion: /* A DNG-specific tag */
if(summary_entry)
summary_entry->filesubformat |= FILESUBFMT_DNG;
break;
case TIFFTAG_CR2_0xc5d9: /* A CR2-specific tag */
if(summary_entry)
{
summary_entry->filesubformat |= FILESUBFMT_CR2;
if((summary_entry->compression == 6) &&
(summary_entry->imagesubformat == IMGSUBFMT_RGB))
{
summary_entry->subfiletype = REDUCED_RES_TYPE;
}
}
break;
case TIFFTAG_CR2_0xc5d8:
case TIFFTAG_CR2_0xc5e0:
case TIFFTAG_CR2_SLICE:
/* These tags appear (so far) only in the */
/* weird IFD containing the lossless jpeg */
/* primary. It is possible to check for other */
/* things, e.g. just compression, strip */
/* offset and bytecount (1 each), but for now */
/* just assume that any of these tags means */
/* CR2. The compression at this point will be */
/* 6, but will be re-marked JPEG_SOF_3 when */
/* the image is scanned. */
if(summary_entry)
{
summary_entry->filesubformat |= FILESUBFMT_CR2;
summary_entry->subfiletype = PRIMARY_TYPE;
}
break;
case TIFFTAG_SubIFDtag:
case EXIFTAG_ExifIFDPointer:
case EXIFTAG_GPSInfoIFDPointer:
case EXIFTAG_Interoperability:
case TIFFTAG_PrintIM:
use_second_pass++;
break;
default:
break;
}
print_tag_address(ENTRY,entry_offset,indent,prefix);
value_offset = print_entry(inptr,byteorder,entry_ptr,fileoffset_base,
summary_entry,listname,ifdtype,
ifdnum, subifdnum, SMALLINDENT);
/* Keep track of how far into the file reading progresses */
if(value_offset == 0UL)
value_offset = current_offset;
if(value_offset > max_value_offset)
max_value_offset = value_offset;
if((is_offset(entry_ptr)))
++use_second_pass;
entry_offset = current_offset;
}
next_ifd_offset = read_ulong(inptr,byteorder,current_offset);
if(next_ifd_offset > 0UL)
{
if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
{
print_tag_address(SECTION,current_offset,indent,prefix);
chpr += printf("**** next IFD offset %lu",next_ifd_offset);
next_ifd_offset += fileoffset_base;
chpr += printf("(+ %lu = %#lx/%lu)",fileoffset_base,
next_ifd_offset,next_ifd_offset);
}
else
next_ifd_offset += fileoffset_base;
/* Corrupt file will cause infinite loops. So we abort.
* It is possible this can be worked around better.
* See Issue #9 https://github.com/hfiguiere/exifprobe/issues/9
*/
if(next_ifd_offset < ftell(inptr)) {
printred("\nReading IFD backwards. INVALID FILE. ABORTING.\n");
exit(1);
} else if (next_ifd_offset > filesize) {
printred("\nReading IFD past EOF. INVALID FILE. ABORTING.\n");
exit(1);
}
/* We should be able to tolerate these */
if ((ifdtype != TIFF_IFD) && (ifdtype != TIFF_SUBIFD))
{
if(PRINT_SECTION)
printred(" INVALID NEXT IFD OFFSET ");
else
{
chpr = newline(chpr);
printred("# ========= WARNING: INVALID NEXT IFD OFFSET ");
chpr += printf("%ld in ",next_ifd_offset);
/* Until someone creates a subifd in a subifd, */
/* this is enough. */
if(ifdtype == TIFF_IFD)
chpr += printf("IFD %d =========",ifdnum);
else if(ifdtype == TIFF_SUBIFD)
chpr += printf("SUBIFD %d of IFD %d =========",subifdnum,ifdnum);
else
chpr += printf("%s =========",ifdname);
}
next_ifd_offset = 0L;
}
/* This is the limit of offsets which should be part of */
/* this ifd; a limit may have been passed in from the */
/* parent, if it had a length of this section. This will */
/* replace that number unconditionally, although it may */
/* be that one should be checked against the other... */
/* This can be checked against max_value_offset, and */
/* value_offset in the second pass. */
max_offset = next_ifd_offset;
chpr = newline(chpr);
}
else
{
if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
{
print_tag_address(SECTION,current_offset,indent,prefix);
chpr += printf("**** next IFD offset 0");
chpr = newline(chpr);
}
}
if(max_value_offset < current_offset)
max_ifd_offset = current_offset;
else
max_ifd_offset = max_value_offset;
if(Debug & END_OF_SECTION_DEBUG)
printf("mo=%lu, mvo=%lu, mio=%lu, nio=%lu, ol=%lu\n",max_offset,max_value_offset,max_ifd_offset,next_ifd_offset,offset_limit);
/* If we made it through the first pass, we should be able to */
/* get some info from the second pass, so don't let a failure */
/* here stop us. It will fail later. */
if(ferror(inptr) || feof(inptr))
{
chpr += printf("#========= WARNING: READ NEXT IFD OFFSET FAILED =========");
chpr = newline(chpr);
why(stdout);
clearerr(inptr); /* keep going... */
}
else
current_offset = ftell(inptr);
value_offset = current_offset;
if(use_second_pass)
{
if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
{
print_tag_address(SECTION,value_offset,indent,prefix);
chpr += printf("============= VALUES, ");
if(ifdtype == TIFF_IFD)
chpr += printf("IFD %d",ifdnum);
else if(ifdtype == TIFF_SUBIFD)
chpr += printf("SubIFD %d of IFD %d",subifdnum,ifdnum);
else if(ifdtype == INTEROP_IFD)
chpr += printf("Interoperability SubIFD");
else if(ifdtype == EXIF_IFD) /* shouldn't happen */
chpr += printf("EXIF IFD");
else if(ifdtype == MAKER_SUBIFD)
chpr += printf("%s",ifdname);
else
chpr += printf("UNKNOWN IFD TYPE %d",ifdtype);
chpr += printf(" ============");
chpr = newline(chpr);
}
/* Second pass, to evaluate entries which are stored */
/* indirectly (the value requires more than 4 bytes). */
/* This time we have to explicitly seek to each entry, */
/* since the value processing may send the file pointer */
/* off to exotic places. */
entry_offset = start_entry_offset;
for(entry_num = 0; entry_num < num_entries; ++entry_num)
{
entry_ptr = read_ifd_entry(inptr,byteorder,entry_offset);
if((entry_ptr->value_type == 0) || (entry_ptr->value_type > DOUBLE) ||
ferror(inptr) || feof(inptr))
{
/* If the first pass made it through invalid */
/* entries, this pass should just ignore them and */
/* quietly continue. */
clearerr(inptr);
entry_offset = current_offset = ftell(inptr);
continue;
}
entry_offset = current_offset = ftell(inptr);
value_is_offset = is_offset(entry_ptr);
switch(entry_ptr->tag)
{
case TIFFTAG_SubIFDtag:
value_offset = process_subifd(inptr,byteorder,
entry_ptr, fileoffset_base,0L,
summary_entry,listname,
ifdnum,++subifdnum,TIFF_SUBIFD,
indent);
/* subifds are not part of the IFD; throw */
/* away this value for now. */
value_offset = 0;
break;
case EXIFTAG_ExifIFDPointer:
value_start_offset = entry_ptr->value + fileoffset_base;
value_offset = process_exif_ifd(inptr,byteorder,
entry_ptr->value,fileoffset_base,
next_ifd_offset,summary_entry,listname,
ifdnum,indent);
if(max_offset && (value_offset > max_offset))
offset_limit = value_offset;
break;
case EXIFTAG_GPSInfoIFDPointer:
value_offset = process_gps_ifd(inptr,byteorder,
entry_ptr->value,fileoffset_base,
next_ifd_offset,summary_entry,listname,
ifdnum,indent);
break;
case EXIFTAG_Interoperability:
/* This doesn't belong in a TIFF IFD, but */
/* be prepared. */
/* Also we make sure we are not calling on */
/* the same ifd offset */
PUSHCOLOR(INTEROP_COLOR);
if (entry_ptr->value != ifd_offset) {
value_offset = process_tiff_ifd(inptr,byteorder,
entry_ptr->value,fileoffset_base,
next_ifd_offset,summary_entry,
listname,INTEROP_IFD,ifdnum,0,
indent);
}
value_offset = 0;
POPCOLOR();
break;
case TIFFTAG_StripOffsets:
case TIFFTAG_TileOffsets:
/* Update the summary values set in the first */
/* pass */
/* Bad strip offsets seem to be one of the */
/* more common errors, especially when images */
/* have been processed by editing software. */
/* Offsets greater than filesize are easy to */
/* detect, so check for them, and test to see */
/* if they're just written with the wrong */
/* byteorder. */
/* ###%%% need to record number of offsets */
/* and location of offsets for multi-tile */
/* (multi-strip?) images jpeg-compressed by */
/* tile (seen in DNG) */
if((value_is_offset) && summary_entry)
{
alt_byteorder = byteorder;
summary_entry->chunkhow = entry_ptr->tag;
summary_entry->chunktype = entry_ptr->value_type;
/* ###%%% combine short and long cases; just switch read_xxx() */
if(entry_ptr->value_type == LONG)
{
unsigned long tmp_offset;
tmp_offset = read_ulong(inptr,byteorder,entry_ptr->value + fileoffset_base);
summary_entry->offset = tmp_offset;
summary_entry->offset_loc = entry_ptr->value +fileoffset_base;
summary_entry->noffsets = entry_ptr->count;
if(tmp_offset > filesize)
{
chpr = newline(chpr);
PUSHCOLOR(HI_RED);
print_filename();
chpr += printf("# WARNING: initial stripoffset (%#lx/%lu) > filesize...wrong byteorder? ",
tmp_offset,tmp_offset);
print_byteorder(byteorder,1);
chpr = newline(chpr);
if(byteorder == TIFF_MOTOROLA)
alt_byteorder = TIFF_INTEL;
else
alt_byteorder = TIFF_MOTOROLA;
tmp_offset = read_ulong(inptr,alt_byteorder,
entry_ptr->value + fileoffset_base);
print_filename();
chpr += printf("# WARNING: initial stripoffset using alt byteorder [");
print_byteorder(alt_byteorder,1);
chpr += printf("] is %#lx/%lu ",
tmp_offset,tmp_offset);
chpr = newline(chpr);
if(tmp_offset < filesize)
summary_entry->offset = tmp_offset;
POPCOLOR();
}
}
else
{
unsigned short tmp_offset;
tmp_offset = read_ushort(inptr,byteorder,entry_ptr->value + fileoffset_base);
summary_entry->offset = tmp_offset;
summary_entry->offset_loc = entry_ptr->value +fileoffset_base;
summary_entry->noffsets = entry_ptr->count;
if((unsigned long)tmp_offset > filesize)
{
chpr = newline(chpr);
PUSHCOLOR(HI_RED);
print_filename();
chpr += printf("# WARNING: initial stripoffset (%#x/%u) > filesize...wrong byteorder? ",
tmp_offset,tmp_offset);
print_byteorder(byteorder,1);
chpr = newline(chpr);
if(byteorder == TIFF_MOTOROLA)
alt_byteorder = TIFF_INTEL;
else
alt_byteorder = TIFF_MOTOROLA;
tmp_offset = read_ushort(inptr,alt_byteorder,
entry_ptr->value + fileoffset_base);
print_filename();
chpr += printf("# WARNING: initial stripoffset using alt byteorder [");
print_byteorder(alt_byteorder,1);
chpr += printf("] is %#x/%u ",
tmp_offset,tmp_offset);
chpr = newline(chpr);
if(tmp_offset < filesize)
summary_entry->offset = tmp_offset;
POPCOLOR();
}
}
}
if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
{
print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
indent,prefix);
print_tagid(entry_ptr,SMALLINDENT,ifdtype);
value_offset =
print_offset_value(inptr,byteorder,entry_ptr,
fileoffset_base,listname,
ifdtype,indent,1);
}
break;
case TIFFTAG_StripByteCounts:
case TIFFTAG_TileByteCounts:
if((value_is_offset) && summary_entry)
{
tmp_length =
sum_strip_bytecounts(inptr,byteorder,
entry_ptr->value + fileoffset_base,
entry_ptr->count,entry_ptr->value_type);
summary_entry->length = tmp_length;
summary_entry->length_loc = entry_ptr->value + fileoffset_base;
if(summary_entry->noffsets != entry_ptr->count)
printred(" # Warning: number of bytecounts != number of offsets");
if(tmp_length > filesize)
{
chpr = newline(chpr);
PUSHCOLOR(HI_RED);
print_filename();
chpr += printf("# WARNING: total stripbytecount (%#lx/%lu) > filesize (%lu) ... wrong byteorder? ",
tmp_length,tmp_length,filesize);
print_byteorder(byteorder,1);
chpr = newline(chpr);
if(byteorder == TIFF_MOTOROLA)
alt_byteorder = TIFF_INTEL;
else
alt_byteorder = TIFF_MOTOROLA;
alt_length =
sum_strip_bytecounts(inptr,alt_byteorder,
entry_ptr->value + fileoffset_base,
entry_ptr->count,entry_ptr->value_type);
print_filename();
chpr += printf("# WARNING: total stripbytecount using alt byteorder [");
print_byteorder(alt_byteorder,1);
chpr += printf("] is %#lx/%lu ",
alt_length,alt_length);
chpr = newline(chpr);
if(alt_length < filesize)
summary_entry->length = alt_length;
else
{
print_filename();
chpr += printf("# WARNING: file may be truncated");
chpr = newline(chpr);
}
POPCOLOR();
}
}
if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
{
print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
indent,prefix);
print_tagid(entry_ptr,SMALLINDENT,ifdtype);
value_offset =
print_offset_value(inptr,byteorder,entry_ptr,
fileoffset_base,listname,
ifdtype,indent,1);
/* ###%%% find a way to print total */
/* ByteCount, at least in LIST mode */
}
break;
case TIFFTAG_JPEGTables:
nameoftag = tagname(entry_ptr->tag);
if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
{
if(PRINT_SECTION)
{
chpr = newline(chpr);
print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
indent,prefix);
chpr += printf("# Start of %s length %lu",nameoftag,
entry_ptr->count);
chpr = newline(chpr);
}
}
marker = read_ushort(inptr,TIFF_MOTOROLA,fileoffset_base + entry_ptr->value);
/* Need a new entry; this IFD's entry may be */
/* used for an image, but may not be locked */
/* yet. */
tmp_summary_entry = new_summary_entry(summary_entry,0,IMGFMT_JPEG);
value_offset = process_jpeg_segments(inptr,
fileoffset_base + entry_ptr->value,
marker,entry_ptr->count,
tmp_summary_entry,listname,
prefix,indent+SMALLINDENT);
if(tmp_summary_entry)
tmp_summary_entry->imagesubformat = TIFFTAG_JPEGTables;
if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
{
if((PRINT_SECTION))
{
/* ###%% check for early EOI; */
if((status = jpeg_status(0) == JPEG_EARLY_EOI))
chpr = newline(chpr);
jpeg_status(status);
print_tag_address(ENTRY,fileoffset_base + entry_ptr->value + entry_ptr->count + 1,
indent,prefix);
chpr += printf("# End of %s",nameoftag);
print_jpeg_status();
chpr = newline(chpr);
}
}
break;
case TIFFTAG_PrintIM:
if(next_ifd_offset && (cur_ifd_offset > next_ifd_offset))
prefix = "+";
else
prefix = "@";
process_pim(inptr,byteorder,entry_ptr->value,fileoffset_base,
entry_ptr->count,tagname(entry_ptr->tag),
listname,prefix,indent);
chpr = newline(0);
break;
default:
if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
{
print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
indent,prefix);
print_tagid(entry_ptr,SMALLINDENT,ifdtype);
value_offset =
print_offset_value(inptr,byteorder,entry_ptr,
fileoffset_base,listname,
ifdtype,indent,1);
}
break;
}
if(value_offset == 0L) /* bad entry; try the next one */
clearerr(inptr);
else if(value_offset > filesize)
goto blewit;
else if(value_offset > max_value_offset)
max_value_offset = value_offset;
}
}
else if(current_offset > max_value_offset)
max_value_offset = current_offset;
max_ifd_offset = max_value_offset;
indent -= SMALLINDENT;
/* max_ifd_offset is the maximum offset we've read so far; */
/* max_offset, if set, is the highest offset of this ifd, */
/* according to either the parent or the recently-read */
/* next_ifd_offset. */
if(max_offset && (max_ifd_offset > max_offset))
prefix = "?";
if(Debug & END_OF_SECTION_DEBUG)
printf("mo=%lu, mvo=%lu, mio=%lu, nio=%lu, ol=%lu, prefix=%s\n",
max_offset,max_value_offset,max_ifd_offset,
next_ifd_offset,offset_limit,prefix);
/* ================== show jpeg thumbnail =================== */
if(thn_offset || thn_length)
{
int startindent;
if(Debug & END_OF_SECTION_DEBUG)
printf("mo=%lu, ol=%lu, thno=%lu\n",max_offset,offset_limit,thn_offset);
/* This will be set only if a JpegInterchangeFormat tag */
/* has been seen, so this must be a JPEG thumbnail. The */
/* Exif spec indicates that the data should be written */
/* within the values section of the IFD, so scan and */
/* report here. If it appears to be written outside the */
/* bounds of the IFD, mark it. "max_offset" is the */
/* parent's idea (if any) of the extent of the IFD. */
/* "offset_limit" is the beginning of the Exif section, */
/* if present. */
if(max_offset && (thn_offset > max_offset))
tprefix = ">";
else if(offset_limit && (thn_offset > offset_limit))
tprefix = "+";
else
tprefix = prefix;
startindent = charsprinted();
print_tag_address(SECTION,thn_offset,indent + SMALLINDENT,tprefix);
if(PRINT_SECTION)
{
if(thn_length)
chpr += printf("#### Start of JPEG thumbnail data for IFD %d",ifdnum);
else
{
PUSHCOLOR(RED);
chpr += printf("#### ZERO LENGTH JPEG thumbnail for IFD %d",ifdnum);
POPCOLOR();
}
if((ifdtype == TIFF_SUBIFD) || (subifdnum >= 0))
chpr += printf(" SubIFD %d",subifdnum);
if(ifdtype == MAKER_IFD)
chpr += printf(" %s",ifdname);
else if(ifdtype == MAKER_SUBIFD)