Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_info.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_info.h"
10
11 #include "dng_camera_profile.h"
12 #include "dng_exceptions.h"
13 #include "dng_globals.h"
14 #include "dng_host.h"
15 #include "dng_tag_codes.h"
16 #include "dng_parse_utils.h"
17 #include "dng_safe_arithmetic.h"
18 #include "dng_sdk_limits.h"
19 #include "dng_tag_types.h"
20 #include "dng_tag_values.h"
21 #include "dng_utils.h"
22
23 /*****************************************************************************/
24
25 dng_info::dng_info ()
26
27 : fTIFFBlockOffset (0)
28 , fTIFFBlockOriginalOffset (kDNGStreamInvalidOffset)
29 , fBigEndian (false)
30 , fMagic (0)
31 , fExif ()
32 , fShared ()
33 , fMainIndex (-1)
34 , fMaskIndex (-1)
35 , fDepthIndex (-1)
36 , fEnhancedIndex (-1)
37 , fIFD ()
38 , fChainedIFD ()
39 , fChainedSubIFD ()
40 , fMakerNoteNextIFD (0)
41
42 {
43
44 }
45
46 /*****************************************************************************/
47
48 dng_info::~dng_info ()
49 {
50
51 for (size_t index = 0; index < fIFD.size (); index++)
52 {
53
54 if (fIFD [index])
55 {
56 delete fIFD [index];
57 fIFD [index] = NULL;
58 }
59
60 }
61
62 for (size_t index2 = 0; index2 < fChainedIFD.size (); index2++)
63 {
64
65 if (fChainedIFD [index2])
66 {
67 delete fChainedIFD [index2];
68 fChainedIFD [index2] = NULL;
69 }
70
71 }
72
73 for (size_t index3 = 0; index3 < fChainedSubIFD.size (); index3++)
74 {
75
76 for (size_t index4 = 0; index4 < fChainedSubIFD [index3].size (); index4++)
77 {
78
79 if (fChainedSubIFD [index3] [index4])
80 {
81 delete fChainedSubIFD [index3] [index4];
82 fChainedSubIFD [index3] [index4] = NULL;
83 }
84
85 }
86
87 }
88
89 }
90
91 /*****************************************************************************/
92
93 void dng_info::ValidateMagic ()
94 {
95
96 switch (fMagic)
97 {
98
99 case magicTIFF:
100 case magicBigTIFF:
101 case magicExtendedProfile:
102 case magicRawCache:
103 case magicPanasonic:
104 case magicOlympusA:
105 case magicOlympusB:
106 {
107
108 return;
109
110 }
111
112 default:
113 {
114
115 #if qDNGValidate
116
117 ReportError ("Invalid TIFF magic number");
118
119 #endif
120
121 ThrowBadFormat ();
122
123 }
124
125 }
126
127 }
128
129 /*****************************************************************************/
130
131 void dng_info::ParseTag (dng_host &host,
132 dng_stream &stream,
133 dng_exif *exif,
134 dng_shared *shared,
135 dng_ifd *ifd,
136 uint32 parentCode,
137 uint32 tagCode,
138 uint32 tagType,
139 uint32 tagCount,
140 uint64 tagOffset,
141 int64 offsetDelta)
142 {
143
144 bool isSubIFD = parentCode >= tcFirstSubIFD &&
145 parentCode <= tcLastSubIFD;
146
147 bool isMainIFD = (parentCode == 0 || isSubIFD) &&
148 ifd &&
149 ifd->fUsesNewSubFileType &&
150 ifd->fNewSubFileType == sfMainImage;
151
152 // Panasonic RAW format stores private tags using tag codes < 254 in
153 // IFD 0. Redirect the parsing of these tags into a logical
154 // "PanasonicRAW" IFD.
155
156 // Panasonic is starting to use some higher numbers also (280..283).
157
158 if (fMagic == 85 && parentCode == 0 && (tagCode < tcNewSubFileType ||
159 (tagCode >= 280 && tagCode <= 283)))
160 {
161
162 parentCode = tcPanasonicRAW;
163
164 ifd = NULL;
165
166 }
167
168 stream.SetReadPosition (tagOffset);
169
170 if (ifd && ifd->ParseTag (host,
171 stream,
172 parentCode,
173 tagCode,
174 tagType,
175 tagCount,
176 tagOffset))
177 {
178
179 return;
180
181 }
182
183 stream.SetReadPosition (tagOffset);
184
185 if (exif && shared && exif->ParseTag (stream,
186 *shared,
187 parentCode,
188 isMainIFD,
189 tagCode,
190 tagType,
191 tagCount,
192 tagOffset))
193 {
194
195 return;
196
197 }
198
199 stream.SetReadPosition (tagOffset);
200
201 if (shared && exif && shared->ParseTag (stream,
202 *exif,
203 parentCode,
204 isMainIFD,
205 tagCode,
206 tagType,
207 tagCount,
208 tagOffset,
209 offsetDelta))
210 {
211
212 return;
213
214 }
215
216 if (parentCode == tcLeicaMakerNote &&
217 tagType == ttUndefined &&
218 tagCount >= 14)
219 {
220
221 if (ParseMakerNoteIFD (host,
222 stream,
223 tagCount,
224 tagOffset,
225 offsetDelta,
226 tagOffset,
227 stream.Length (),
228 tcLeicaMakerNote))
229 {
230
231 return;
232
233 }
234
235 }
236
237 if (parentCode == tcOlympusMakerNote &&
238 tagType == ttUndefined &&
239 tagCount >= 14)
240 {
241
242 uint32 olympusMakerParent = 0;
243
244 switch (tagCode)
245 {
246
247 case 8208:
248 olympusMakerParent = tcOlympusMakerNote8208;
249 break;
250
251 case 8224:
252 olympusMakerParent = tcOlympusMakerNote8224;
253 break;
254
255 case 8240:
256 olympusMakerParent = tcOlympusMakerNote8240;
257 break;
258
259 case 8256:
260 olympusMakerParent = tcOlympusMakerNote8256;
261 break;
262
263 case 8272:
264 olympusMakerParent = tcOlympusMakerNote8272;
265 break;
266
267 case 12288:
268 olympusMakerParent = tcOlympusMakerNote12288;
269 break;
270
271 default:
272 break;
273
274 }
275
276 if (olympusMakerParent)
277 {
278
279 // Olympus made a mistake in some camera models in computing
280 // the size of these sub-tags, so we fudge the count.
281
282 if (ParseMakerNoteIFD (host,
283 stream,
284 stream.Length () - tagOffset,
285 tagOffset,
286 offsetDelta,
287 tagOffset,
288 stream.Length (),
289 olympusMakerParent))
290 {
291
292 return;
293
294 }
295
296 }
297
298 }
299
300 if (parentCode == tcRicohMakerNote &&
301 tagCode == 0x2001 &&
302 tagType == ttUndefined &&
303 tagCount > 22)
304 {
305
306 char header [20];
307
308 stream.SetReadPosition (tagOffset);
309
310 stream.Get (header, sizeof (header));
311
312 if (memcmp (header, "[Ricoh Camera Info]", 19) == 0)
313 {
314
315 ParseMakerNoteIFD (host,
316 stream,
317 tagCount - 20,
318 tagOffset + 20,
319 offsetDelta,
320 tagOffset + 20,
321 tagOffset + tagCount,
322 tcRicohMakerNoteCameraInfo);
323
324 return;
325
326 }
327
328 }
329
330 #if qDNGValidate
331
332 {
333
334 stream.SetReadPosition (tagOffset);
335
336 if (gVerbose)
337 {
338
339 printf ("*");
340
341 DumpTagValues (stream,
342 LookupTagType (tagType),
343 parentCode,
344 tagCode,
345 tagType,
346 tagCount);
347
348 }
349
350 // If type is ASCII, then parse anyway so we report any ASCII
351 // NULL termination or character set errors.
352
353 else if (tagType == ttAscii)
354 {
355
356 dng_string s;
357
358 ParseStringTag (stream,
359 parentCode,
360 tagCode,
361 tagCount,
362 s,
363 false);
364
365 }
366
367 }
368
369 #endif
370
371 }
372
373 /*****************************************************************************/
374
375 bool dng_info::ValidateIFD (dng_stream &stream,
376 uint64 ifdOffset,
377 int64 offsetDelta)
378 {
379
380 bool isBigTIFF = (fMagic == magicBigTIFF);
381
382 // Make sure we have a count.
383
384 if (ifdOffset + (isBigTIFF ? 8 : 2) > stream.Length ())
385 {
386 return false;
387 }
388
389 // Get entry count.
390
391 stream.SetReadPosition (ifdOffset);
392
393 uint64 ifdEntries = isBigTIFF ? stream.Get_uint64 ()
394 : stream.Get_uint16 ();
395
396 if (ifdEntries < 1)
397 {
398 return false;
399 }
400
401 // Make sure we have room for all entries and next IFD link.
402
403 if (ifdOffset + (isBigTIFF ? 8 + ifdEntries * 20 + 8
404 : 2 + ifdEntries * 12 + 4) > stream.Length ())
405 {
406 return false;
407 }
408
409 // Check each entry.
410
411 for (uint64 tag_index = 0; tag_index < ifdEntries; tag_index++)
412 {
413
414 stream.SetReadPosition (isBigTIFF ? ifdOffset + 8 + tag_index * 20
415 : ifdOffset + 2 + tag_index * 12);
416
417 stream.Skip (2); // Ignore tag code.
418
419 uint32 tagType = stream.Get_uint16 ();
420
421 uint64 tagCount = isBigTIFF ? stream.Get_uint64 ()
422 : stream.Get_uint32 ();
423
424 uint64 tag_type_size = (uint64) TagTypeSize (tagType);
425
426 if (tag_type_size == 0)
427 {
428 return false;
429 }
430
431 uint64 tag_data_size = tagCount * tag_type_size;
432
433 // Check overflow.
434
435 if (tag_data_size < tagCount ||
436 tag_data_size < tag_type_size)
437 return false;
438
439 if (tag_data_size > (isBigTIFF ? 8 : 4))
440 {
441
442 uint64 tagOffset = isBigTIFF ? stream.Get_uint64 ()
443 : stream.Get_uint32 ();
444
445 tagOffset += offsetDelta;
446
447 if (SafeUint64Add (tagOffset,
448 tag_data_size) > stream.Length ())
449 {
450 return false;
451 }
452
453 }
454
455 }
456
457 return true;
458
459 }
460
461 /*****************************************************************************/
462
463 void dng_info::ParseIFD (dng_host &host,
464 dng_stream &stream,
465 dng_exif *exif,
466 dng_shared *shared,
467 dng_ifd *ifd,
468 uint64 ifdOffset,
469 int64 offsetDelta,
470 uint32 parentCode)
471 {
472
473 #if qDNGValidate
474
475 bool isMakerNote = (parentCode >= tcFirstMakerNoteIFD &&
476 parentCode <= tcLastMakerNoteIFD);
477
478 #endif
479
480 bool isBigTIFF = (fMagic == magicBigTIFF);
481
482 // TIFF IFDs often read from two very different places in the file,
483 // one for the IFD itself (and small tags), and elsewhere in the file
484 // for large tags. We can reduce the number of calls to the OS
485 // by double buffering reads for the two areas of the file.
486
487 dng_stream_double_buffered ifdStream (stream);
488
489 ifdStream.SetReadPosition (ifdOffset);
490
491 if (ifd)
492 {
493 ifd->fThisIFD = ifdOffset;
494 }
495
496 uint64 ifdEntries = isBigTIFF ? ifdStream.Get_uint64 ()
497 : ifdStream.Get_uint16 ();
498
499 #if qDNGValidate
500
501 bool generateOddOffsetWarnings = !gImagecore;
502
503 if (gVerbose)
504 {
505
506 printf ("%s: Offset = %llu, Entries = %llu\n\n",
507 LookupParentCode (parentCode),
508 (unsigned long long) ifdOffset,
509 (unsigned long long) ifdEntries);
510
511 }
512
513 if (generateOddOffsetWarnings && (ifdOffset & 1) && !isMakerNote)
514 {
515
516 char message [256];
517
518 snprintf (message,
519 256,
520 "%s has odd offset (%u)",
521 LookupParentCode (parentCode),
522 (unsigned) ifdOffset);
523
524 ReportWarning (message);
525
526 }
527
528 uint32 prev_tag_code = 0;
529
530 #endif
531
532 for (uint64 tag_index = 0; tag_index < ifdEntries; tag_index++)
533 {
534
535 ifdStream.SetReadPosition (isBigTIFF ? ifdOffset + 8 + tag_index * 20
536 : ifdOffset + 2 + tag_index * 12);
537
538 uint32 tagCode = ifdStream.Get_uint16 ();
539 uint32 tagType = ifdStream.Get_uint16 ();
540
541 // Minolta 7D files have a bug in the EXIF block where the count
542 // is wrong, and we run off into next IFD link. So if abort parsing
543 // if we get a zero code/type combinations.
544
545 if (tagCode == 0 && tagType == 0)
546 {
547
548 #if qDNGValidate
549
550 char message [256];
551
552 snprintf (message,
553 256,
554 "%s had zero/zero tag code/type entry",
555 LookupParentCode (parentCode));
556
557 ReportWarning (message);
558
559 #endif
560
561 return;
562
563 }
564
565 uint64 tagCount = isBigTIFF ? ifdStream.Get_uint64 ()
566 : ifdStream.Get_uint32 ();
567
568 #if qDNGValidate
569
570 {
571
572 if (tag_index > 0 && tagCode <= prev_tag_code && !isMakerNote)
573 {
574
575 char message [256];
576
577 snprintf (message,
578 256,
579 "%s tags are not sorted in ascending numerical order",
580 LookupParentCode (parentCode));
581
582 ReportWarning (message);
583
584 }
585
586 }
587
588 prev_tag_code = tagCode;
589
590 #endif
591
592 uint32 tag_type_size = TagTypeSize (tagType);
593
594 if (tag_type_size == 0)
595 {
596
597 #if qDNGValidate
598
599 {
600
601 char message [256];
602
603 snprintf (message,
604 256,
605 "%s %s has unknown type (%u)",
606 LookupParentCode (parentCode),
607 LookupTagCode (parentCode, tagCode),
608 (unsigned) tagType);
609
610 ReportWarning (message);
611
612 }
613
614 #endif
615
616 continue;
617
618 }
619
620 bool localTag = true;
621
622 uint64 tagOffset = isBigTIFF ? ifdOffset + 8 + tag_index * 20 + 12
623 : ifdOffset + 2 + tag_index * 12 + 8;
624
625 const uint64 tag_data_size = tagCount * (uint64) tag_type_size;
626
627 // tag_type_size is at least 1.
628
629 if (tag_data_size < tagCount)
630 {
631 ThrowBadFormat ("overflow in tag_data_size");
632 }
633
634 if (tag_data_size > (isBigTIFF ? 8 : 4))
635 {
636
637 tagOffset = isBigTIFF ? ifdStream.Get_uint64 ()
638 : ifdStream.Get_uint32 ();
639
640 #if qDNGValidate
641
642 {
643
644 if (generateOddOffsetWarnings &&
645 !(ifdOffset & 1) &&
646 (tagOffset & 1) &&
647 !isMakerNote &&
648 parentCode != tcKodakDCRPrivateIFD &&
649 parentCode != tcKodakKDCPrivateIFD)
650 {
651
652 char message [256];
653
654 snprintf (message,
655 256,
656 "%s %s has odd data offset (%u)",
657 LookupParentCode (parentCode),
658 LookupTagCode (parentCode, tagCode),
659 (unsigned) tagOffset);
660
661 ReportWarning (message);
662
663 }
664
665 }
666
667 #endif
668
669 tagOffset += offsetDelta;
670
671 localTag = ifdStream.DataInBuffer (tagCount * tag_type_size,
672 tagOffset);
673
674 if (localTag)
675 ifdStream.SetReadPosition (tagOffset);
676 else
677 stream.SetReadPosition (tagOffset);
678
679 }
680
681 // Big TIFF support 64-bit tag counts, but we don't need
682 // that support yet, so ignore tags with huge counts for now.
683
684 if (tagCount <= 0x0FFFFFFFF)
685 {
686
687 ParseTag (host,
688 localTag ? ifdStream : stream,
689 exif,
690 shared,
691 ifd,
692 parentCode,
693 tagCode,
694 tagType,
695 (uint32) tagCount,
696 tagOffset,
697 offsetDelta);
698
699 }
700
701 #if qDNGValidate
702
703 else
704 {
705
706 char message [256];
707
708 snprintf (message,
709 256,
710 "%s %s has larger than 32-bit tag count (%llu)",
711 LookupParentCode (parentCode),
712 LookupTagCode (parentCode, tagCode),
713 (unsigned long long) tagCount);
714
715 ReportWarning (message);
716
717 }
718
719 #endif
720
721 }
722
723 ifdStream.SetReadPosition (isBigTIFF ? ifdOffset + 8 + ifdEntries * 20
724 : ifdOffset + 2 + ifdEntries * 12);
725
726 uint64 nextIFD = isBigTIFF ? ifdStream.Get_uint64 ()
727 : ifdStream.Get_uint32 ();
728
729 #if qDNGValidate
730
731 if (gVerbose)
732 {
733 printf ("NextIFD = %llu\n", (unsigned long long) nextIFD);
734 }
735
736 #endif
737
738 if (ifd)
739 {
740 ifd->fNextIFD = nextIFD;
741 }
742
743 #if qDNGValidate
744
745 if (nextIFD)
746 {
747
748 if (parentCode != 0 &&
749 (parentCode < tcFirstChainedIFD ||
750 parentCode > tcLastChainedIFD ))
751 {
752
753 char message [256];
754
755 snprintf (message,
756 256,
757 "%s has an unexpected non-zero NextIFD (%llu)",
758 LookupParentCode (parentCode),
759 (unsigned long long) nextIFD);
760
761 ReportWarning (message);
762
763 }
764
765 }
766
767 if (gVerbose)
768 {
769 printf ("\n");
770 }
771
772 stream.SetReadPosition (ifdStream.Position ());
773
774 #endif
775
776 }
777
778 /*****************************************************************************/
779
780 bool dng_info::ParseMakerNoteIFD (dng_host &host,
781 dng_stream &stream,
782 uint64 ifdSize,
783 uint64 ifdOffset,
784 int64 offsetDelta,
785 uint64 minOffset,
786 uint64 maxOffset,
787 uint32 parentCode)
788 {
789
790 // Avoid recursion issues where a MakerNote IFD points to itself.
791
792 if (fParseDepth > kMaxParseDepth)
793 {
794 return false;
795 }
796
797 RecursionProtector depthProtect (fParseDepth);
798
799 uint32 tagIndex;
800 uint32 tagCode;
801 uint32 tagType;
802 uint32 tagCount;
803
804 // Assume there is no next IFD pointer.
805
806 fMakerNoteNextIFD = 0;
807
808 // If size is too small to hold a single entry IFD, abort.
809
810 if (ifdSize < 14)
811 {
812 return false;
813 }
814
815 // Get entry count.
816
817 dng_stream_double_buffered ifdStream (stream);
818
819 ifdStream.SetReadPosition (ifdOffset);
820
821 uint32 ifdEntries = ifdStream.Get_uint16 ();
822
823 // Make the entry count if reasonable for the MakerNote size.
824
825 if (ifdEntries < 1 || 2 + ifdEntries * 12 > ifdSize)
826 {
827 return false;
828 }
829
830 // Scan IFD to verify all the tag types are all valid.
831
832 for (tagIndex = 0; tagIndex < ifdEntries; tagIndex++)
833 {
834
835 ifdStream.SetReadPosition (ifdOffset + 2 + tagIndex * 12 + 2);
836
837 tagType = ifdStream.Get_uint16 ();
838
839 // Kludge: Some Canon MakerNotes contain tagType = 0 tags, so we
840 // need to ignore them. This was a "firmware 1.0.4" Canon 40D raw file.
841
842 if (parentCode == tcCanonMakerNote && tagType == 0)
843 {
844 continue;
845 }
846
847 // Ditto for some Apple MakerNotes.
848
849 if (parentCode == tcAppleMakerNote && tagType == 0)
850 {
851 continue;
852 }
853
854 if (TagTypeSize (tagType) == 0)
855 {
856 return false;
857 }
858
859 }
860
861 // OK, the IFD looks reasonable enough to parse.
862
863 #if qDNGValidate
864
865 if (gVerbose)
866 {
867
868 printf ("%s: Offset = %u, Entries = %u\n\n",
869 LookupParentCode (parentCode),
870 (unsigned) ifdOffset,
871 (unsigned) ifdEntries);
872
873 }
874
875 #endif
876
877 for (tagIndex = 0; tagIndex < ifdEntries; tagIndex++)
878 {
879
880 ifdStream.SetReadPosition (ifdOffset + 2 + tagIndex * 12);
881
882 tagCode = ifdStream.Get_uint16 ();
883 tagType = ifdStream.Get_uint16 ();
884 tagCount = ifdStream.Get_uint32 ();
885
886 if (tagType == 0)
887 {
888 continue;
889 }
890
891 uint32 tagSize = 0;
892
893 try
894 {
895
896 tagSize = SafeUint32Mult (tagCount,
897 TagTypeSize (tagType));
898
899 }
900
901 catch (...)
902 {
903 // Detect overflow -- skip invalid tagCount or tagSize.
904 // Works around bug with some Leica M (Typ 240) MakerNotes.
905 continue;
906 }
907
908 uint64 tagOffset = ifdOffset + 2 + tagIndex * 12 + 8;
909
910 bool localTag = true;
911
912 if (tagSize > 4)
913 {
914
915 tagOffset = ifdStream.Get_uint32 () + offsetDelta;
916
917 try
918 {
919
920 if (tagOffset < minOffset ||
921 SafeUint64Add (tagOffset, tagSize) > maxOffset)
922 {
923
924 // Tag data is outside the valid offset range,
925 // so ignore this tag.
926
927 continue;
928
929 }
930
931 }
932
933 catch (...)
934 {
935 // Detect overflow and skip this tag.
936 continue;
937 }
938
939 localTag = ifdStream.DataInBuffer (tagSize, tagOffset);
940
941 ifdStream.SetReadPosition (tagOffset);
942
943 stream.SetReadPosition (tagOffset);
944
945 }
946
947 // Olympus switched to using IFDs in version 3 makernotes.
948
949 if (parentCode == tcOlympusMakerNote &&
950 tagType == ttIFD &&
951 tagCount == 1)
952 {
953
954 uint32 olympusMakerParent = 0;
955
956 switch (tagCode)
957 {
958
959 case 8208:
960 olympusMakerParent = tcOlympusMakerNote8208;
961 break;
962
963 case 8224:
964 olympusMakerParent = tcOlympusMakerNote8224;
965 break;
966
967 case 8240:
968 olympusMakerParent = tcOlympusMakerNote8240;
969 break;
970
971 case 8256:
972 olympusMakerParent = tcOlympusMakerNote8256;
973 break;
974
975 case 8272:
976 olympusMakerParent = tcOlympusMakerNote8272;
977 break;
978
979 case 12288:
980 olympusMakerParent = tcOlympusMakerNote12288;
981 break;
982
983 default:
984 break;
985
986 }
987
988 if (olympusMakerParent)
989 {
990
991 stream.SetReadPosition (tagOffset);
992
993 uint64 subMakerNoteOffset = stream.Get_uint32 () + offsetDelta;
994
995 if (subMakerNoteOffset >= minOffset &&
996 subMakerNoteOffset < maxOffset)
997 {
998
999 if (ParseMakerNoteIFD (host,
1000 stream,
1001 maxOffset - subMakerNoteOffset,
1002 subMakerNoteOffset,
1003 offsetDelta,
1004 minOffset,
1005 maxOffset,
1006 olympusMakerParent))
1007 {
1008
1009 continue;
1010
1011 }
1012
1013 }
1014
1015 }
1016
1017 stream.SetReadPosition (tagOffset);
1018
1019 }
1020
1021 ParseTag (host,
1022 localTag ? ifdStream : stream,
1023 fExif.Get (),
1024 fShared.Get (),
1025 NULL,
1026 parentCode,
1027 tagCode,
1028 tagType,
1029 tagCount,
1030 tagOffset,
1031 offsetDelta);
1032
1033 }
1034
1035 // Grab next IFD pointer, for possible use.
1036
1037 if (ifdSize >= 2 + ifdEntries * 12 + 4)
1038 {
1039
1040 ifdStream.SetReadPosition (ifdOffset + 2 + ifdEntries * 12);
1041
1042 fMakerNoteNextIFD = ifdStream.Get_uint32 ();
1043
1044 }
1045
1046 #if qDNGValidate
1047
1048 if (gVerbose)
1049 {
1050 printf ("\n");
1051 }
1052
1053 #endif
1054
1055 return true;
1056
1057 }
1058
1059 /*****************************************************************************/
1060
1061 void dng_info::ParseMakerNote (dng_host &host,
1062 dng_stream &stream,
1063 uint32 makerNoteCount,
1064 uint64 makerNoteOffset,
1065 int64 offsetDelta,
1066 uint64 minOffset,
1067 uint64 maxOffset)
1068 {
1069
1070 uint8 firstBytes [16];
1071
1072 memset (firstBytes, 0, sizeof (firstBytes));
1073
1074 stream.SetReadPosition (makerNoteOffset);
1075
1076 stream.Get (firstBytes, (uint32) Min_uint64 (sizeof (firstBytes),
1077 makerNoteCount));
1078
1079 // Apple iOS MakerNote.
1080
1081 if (memcmp (firstBytes, "Apple iOS", 9) == 0)
1082 {
1083
1084 stream.SetReadPosition (makerNoteOffset + 12);
1085
1086 bool bigEndian = false;
1087
1088 uint16 endianMark = stream.Get_uint16 ();
1089
1090 if (endianMark == byteOrderMM)
1091 {
1092 bigEndian = true;
1093 }
1094
1095 else if (endianMark != byteOrderII)
1096 {
1097 return;
1098 }
1099
1100 TempBigEndian temp_endian (stream, bigEndian);
1101
1102 if (makerNoteCount > 14)
1103 {
1104
1105 ParseMakerNoteIFD (host,
1106 stream,
1107 makerNoteCount - 14,
1108 makerNoteOffset + 14,
1109 makerNoteOffset,
1110 minOffset,
1111 maxOffset,
1112 tcAppleMakerNote);
1113
1114 }
1115
1116 return;
1117
1118 }
1119
1120 // Epson MakerNote with header.
1121
1122 if (memcmp (firstBytes, "EPSON\000\001\000", 8) == 0)
1123 {
1124
1125 if (makerNoteCount > 8)
1126 {
1127
1128 ParseMakerNoteIFD (host,
1129 stream,
1130 makerNoteCount - 8,
1131 makerNoteOffset + 8,
1132 offsetDelta,
1133 minOffset,
1134 maxOffset,
1135 tcEpsonMakerNote);
1136
1137 }
1138
1139 return;
1140
1141 }
1142
1143 // Fujifilm MakerNote.
1144
1145 if (memcmp (firstBytes, "FUJIFILM", 8) == 0)
1146 {
1147
1148 stream.SetReadPosition (makerNoteOffset + 8);
1149
1150 TempLittleEndian tempEndian (stream);
1151
1152 uint32 ifd_offset = stream.Get_uint32 ();
1153
1154 if (ifd_offset >= 12 && ifd_offset < makerNoteCount)
1155 {
1156
1157 ParseMakerNoteIFD (host,
1158 stream,
1159 makerNoteCount - ifd_offset,
1160 makerNoteOffset + ifd_offset,
1161 makerNoteOffset,
1162 minOffset,
1163 maxOffset,
1164 tcFujiMakerNote);
1165
1166 }
1167
1168 return;
1169
1170 }
1171
1172 // Leica MakerNote for models that store entry offsets relative to the start of
1173 // the MakerNote (e.g., M9).
1174
1175 if ((memcmp (firstBytes, "LEICA\000\000\000", 8) == 0) ||
1176 (memcmp (firstBytes, "LEICA0\003\000", 8) == 0) ||
1177 (memcmp (firstBytes, "LEICA\000\001\000", 8) == 0) ||
1178 (memcmp (firstBytes, "LEICA\000\004\000", 8) == 0) ||
1179 (memcmp (firstBytes, "LEICA\000\005\000", 8) == 0) ||
1180 (memcmp (firstBytes, "LEICA\000\006\000", 8) == 0))
1181 {
1182
1183 if (makerNoteCount > 8)
1184 {
1185
1186 ParseMakerNoteIFD (host,
1187 stream,
1188 makerNoteCount - 8,
1189 makerNoteOffset + 8,
1190 makerNoteOffset,
1191 minOffset,
1192 maxOffset,
1193 tcLeicaMakerNote);
1194
1195 }
1196
1197 return;
1198
1199 }
1200
1201 // Leica MakerNote for models that store absolute entry offsets (i.e., relative
1202 // to the start of the file, e.g., S2).
1203
1204 if ((memcmp (firstBytes, "LEICA\000\002\377", 8) == 0) ||
1205 (memcmp (firstBytes, "LEICA\000\002\000", 8) == 0))
1206 {
1207
1208 if (makerNoteCount > 8)
1209 {
1210
1211 ParseMakerNoteIFD (host,
1212 stream,
1213 makerNoteCount - 8,
1214 makerNoteOffset + 8,
1215 offsetDelta,
1216 minOffset,
1217 maxOffset,
1218 tcLeicaMakerNote);
1219
1220 }
1221
1222 return;
1223
1224 }
1225
1226 // Nikon version 2 MakerNote with header.
1227
1228 if (memcmp (firstBytes, "Nikon\000\002", 7) == 0)
1229 {
1230
1231 stream.SetReadPosition (makerNoteOffset + 10);
1232
1233 bool bigEndian = false;
1234
1235 uint16 endianMark = stream.Get_uint16 ();
1236
1237 if (endianMark == byteOrderMM)
1238 {
1239 bigEndian = true;
1240 }
1241
1242 else if (endianMark != byteOrderII)
1243 {
1244 return;
1245 }
1246
1247 TempBigEndian temp_endian (stream, bigEndian);
1248
1249 uint16 magic = stream.Get_uint16 ();
1250
1251 if (magic != 42)
1252 {
1253 return;
1254 }
1255
1256 uint32 ifd_offset = stream.Get_uint32 ();
1257
1258 if (ifd_offset >= 8 && ifd_offset < makerNoteCount - 10)
1259 {
1260
1261 ParseMakerNoteIFD (host,
1262 stream,
1263 makerNoteCount - 10 - ifd_offset,
1264 makerNoteOffset + 10 + ifd_offset,
1265 makerNoteOffset + 10,
1266 minOffset,
1267 maxOffset,
1268 tcNikonMakerNote);
1269
1270 }
1271
1272 return;
1273
1274 }
1275
1276 // Newer version of Olympus MakerNote with byte order mark.
1277
1278 if (memcmp (firstBytes, "OLYMPUS\000", 8) == 0)
1279 {
1280
1281 stream.SetReadPosition (makerNoteOffset + 8);
1282
1283 bool bigEndian = false;
1284
1285 uint16 endianMark = stream.Get_uint16 ();
1286
1287 if (endianMark == byteOrderMM)
1288 {
1289 bigEndian = true;
1290 }
1291
1292 else if (endianMark != byteOrderII)
1293 {
1294 return;
1295 }
1296
1297 TempBigEndian temp_endian (stream, bigEndian);
1298
1299 uint16 version = stream.Get_uint16 ();
1300
1301 if (version != 3)
1302 {
1303 return;
1304 }
1305
1306 if (makerNoteCount > 12)
1307 {
1308
1309 ParseMakerNoteIFD (host,
1310 stream,
1311 makerNoteCount - 12,
1312 makerNoteOffset + 12,
1313 makerNoteOffset,
1314 minOffset,
1315 maxOffset,
1316 tcOlympusMakerNote);
1317
1318 }
1319
1320 return;
1321
1322 }
1323
1324 // Olympus MakerNote with header.
1325
1326 if (memcmp (firstBytes, "OLYMP", 5) == 0)
1327 {
1328
1329 if (makerNoteCount > 8)
1330 {
1331
1332 ParseMakerNoteIFD (host,
1333 stream,
1334 makerNoteCount - 8,
1335 makerNoteOffset + 8,
1336 offsetDelta,
1337 minOffset,
1338 maxOffset,
1339 tcOlympusMakerNote);
1340
1341 }
1342
1343 return;
1344
1345 }
1346
1347 // OM Digital Solutions cameras (formerly Olympus brand).
1348 // The tags and parent code are the same as Olympus.
1349 // Just a new header identifier "OM SYSTEM" and BOM.
1350
1351 if (memcmp (firstBytes, "OM SYSTEM\000", 10) == 0)
1352 {
1353
1354 stream.SetReadPosition (makerNoteOffset + 12);
1355
1356 bool bigEndian = false;
1357
1358 uint16 endianMark = stream.Get_uint16 ();
1359
1360 if (endianMark == byteOrderMM)
1361 {
1362 bigEndian = true;
1363 }
1364
1365 else if (endianMark != byteOrderII)
1366 {
1367 return;
1368 }
1369
1370 TempBigEndian temp_endian (stream, bigEndian);
1371
1372 uint16 version = stream.Get_uint16 ();
1373
1374 if (version != 4)
1375 {
1376 return;
1377 }
1378
1379 if (makerNoteCount > 16)
1380 {
1381
1382 ParseMakerNoteIFD (host,
1383 stream,
1384 makerNoteCount - 16,
1385 makerNoteOffset + 16,
1386 makerNoteOffset,
1387 minOffset,
1388 maxOffset,
1389 tcOlympusMakerNote);
1390
1391 }
1392
1393 return;
1394
1395 }
1396
1397 // Panasonic MakerNote.
1398
1399 if (memcmp (firstBytes, "Panasonic\000\000\000", 12) == 0)
1400 {
1401
1402 if (makerNoteCount > 12)
1403 {
1404
1405 ParseMakerNoteIFD (host,
1406 stream,
1407 makerNoteCount - 12,
1408 makerNoteOffset + 12,
1409 offsetDelta,
1410 minOffset,
1411 maxOffset,
1412 tcPanasonicMakerNote);
1413
1414 }
1415
1416 return;
1417
1418 }
1419
1420 // Pentax MakerNote, absolute addresses.
1421
1422 if (memcmp (firstBytes, "AOC", 4) == 0)
1423 {
1424
1425 if (makerNoteCount > 6)
1426 {
1427
1428 stream.SetReadPosition (makerNoteOffset + 4);
1429
1430 bool bigEndian = stream.BigEndian ();
1431
1432 uint16 endianMark = stream.Get_uint16 ();
1433
1434 if (endianMark == byteOrderMM)
1435 {
1436 bigEndian = true;
1437 }
1438
1439 else if (endianMark == byteOrderII)
1440 {
1441 bigEndian = false;
1442 }
1443
1444 TempBigEndian temp_endian (stream, bigEndian);
1445
1446 ParseMakerNoteIFD (host,
1447 stream,
1448 makerNoteCount - 6,
1449 makerNoteOffset + 6,
1450 offsetDelta,
1451 minOffset,
1452 maxOffset,
1453 tcPentaxMakerNote);
1454
1455 }
1456
1457 return;
1458
1459 }
1460
1461 // Pentax MakerNote, relative addresses.
1462
1463 if (memcmp (firstBytes, "PENTAX", 6) == 0)
1464 {
1465
1466 if (makerNoteCount > 8)
1467 {
1468
1469 stream.SetReadPosition (makerNoteOffset + 8);
1470
1471 bool bigEndian = stream.BigEndian ();
1472
1473 uint16 endianMark = stream.Get_uint16 ();
1474
1475 if (endianMark == byteOrderMM)
1476 {
1477 bigEndian = true;
1478 }
1479
1480 else if (endianMark == byteOrderII)
1481 {
1482 bigEndian = false;
1483 }
1484
1485 TempBigEndian temp_endian (stream, bigEndian);
1486
1487 ParseMakerNoteIFD (host,
1488 stream,
1489 makerNoteCount - 10,
1490 makerNoteOffset + 10,
1491 makerNoteOffset, // Relative to start of MakerNote.
1492 minOffset,
1493 maxOffset,
1494 tcPentaxMakerNote);
1495
1496 }
1497
1498 return;
1499
1500 }
1501
1502 // Ricoh MakerNote.
1503
1504 if (memcmp (firstBytes, "RICOH", 5) == 0 ||
1505 memcmp (firstBytes, "Ricoh", 5) == 0)
1506 {
1507
1508 if (makerNoteCount > 8)
1509 {
1510
1511 TempBigEndian tempEndian (stream);
1512
1513 ParseMakerNoteIFD (host,
1514 stream,
1515 makerNoteCount - 8,
1516 makerNoteOffset + 8,
1517 offsetDelta,
1518 minOffset,
1519 maxOffset,
1520 tcRicohMakerNote);
1521
1522 }
1523
1524 return;
1525
1526 }
1527
1528 // Nikon MakerNote without header.
1529
1530 if (fExif->fMake.StartsWith ("NIKON"))
1531 {
1532
1533 ParseMakerNoteIFD (host,
1534 stream,
1535 makerNoteCount,
1536 makerNoteOffset,
1537 offsetDelta,
1538 minOffset,
1539 maxOffset,
1540 tcNikonMakerNote);
1541
1542 return;
1543
1544 }
1545
1546 // Canon MakerNote.
1547
1548 if (fExif->fMake.StartsWith ("CANON"))
1549 {
1550
1551 ParseMakerNoteIFD (host,
1552 stream,
1553 makerNoteCount,
1554 makerNoteOffset,
1555 offsetDelta,
1556 minOffset,
1557 maxOffset,
1558 tcCanonMakerNote);
1559
1560 return;
1561
1562 }
1563
1564 // Minolta MakerNote.
1565
1566 if (fExif->fMake.StartsWith ("MINOLTA" ) ||
1567 fExif->fMake.StartsWith ("KONICA MINOLTA"))
1568 {
1569
1570 ParseMakerNoteIFD (host,
1571 stream,
1572 makerNoteCount,
1573 makerNoteOffset,
1574 offsetDelta,
1575 minOffset,
1576 maxOffset,
1577 tcMinoltaMakerNote);
1578
1579 return;
1580
1581 }
1582
1583 // Sony MakerNote.
1584
1585 if (fExif->fMake.StartsWith ("SONY"))
1586 {
1587
1588 ParseMakerNoteIFD (host,
1589 stream,
1590 makerNoteCount,
1591 makerNoteOffset,
1592 offsetDelta,
1593 minOffset,
1594 maxOffset,
1595 tcSonyMakerNote);
1596
1597 return;
1598
1599 }
1600
1601 // Kodak MakerNote.
1602
1603 if (fExif->fMake.StartsWith ("EASTMAN KODAK"))
1604 {
1605
1606 ParseMakerNoteIFD (host,
1607 stream,
1608 makerNoteCount,
1609 makerNoteOffset,
1610 offsetDelta,
1611 minOffset,
1612 maxOffset,
1613 tcKodakMakerNote);
1614
1615 return;
1616
1617 }
1618
1619 // Mamiya MakerNote.
1620
1621 if (fExif->fMake.StartsWith ("Mamiya"))
1622 {
1623
1624 ParseMakerNoteIFD (host,
1625 stream,
1626 makerNoteCount,
1627 makerNoteOffset,
1628 offsetDelta,
1629 minOffset,
1630 maxOffset,
1631 tcMamiyaMakerNote);
1632
1633 // Mamiya uses a MakerNote chain.
1634
1635 while (fMakerNoteNextIFD)
1636 {
1637
1638 ParseMakerNoteIFD (host,
1639 stream,
1640 makerNoteCount,
1641 offsetDelta + fMakerNoteNextIFD,
1642 offsetDelta,
1643 minOffset,
1644 maxOffset,
1645 tcMamiyaMakerNote);
1646
1647 }
1648
1649 return;
1650
1651 }
1652
1653 // Nikon MakerNote without header.
1654
1655 if (fExif->fMake.StartsWith ("Hasselblad"))
1656 {
1657
1658 ParseMakerNoteIFD (host,
1659 stream,
1660 makerNoteCount,
1661 makerNoteOffset,
1662 offsetDelta,
1663 minOffset,
1664 maxOffset,
1665 tcHasselbladMakerNote);
1666
1667 return;
1668
1669 }
1670
1671 // Samsung MakerNote.
1672
1673 if (fExif->fMake.StartsWith ("Samsung"))
1674 {
1675
1676 ParseMakerNoteIFD (host,
1677 stream,
1678 makerNoteCount,
1679 makerNoteOffset,
1680 makerNoteOffset,
1681 minOffset,
1682 maxOffset,
1683 tcSamsungMakerNote);
1684
1685 return;
1686
1687 }
1688
1689 // Casio MakerNote.
1690
1691 if (fExif->fMake.StartsWith ("CASIO COMPUTER") &&
1692 memcmp (firstBytes, "QVC\000\000\000", 6) == 0)
1693 {
1694
1695 ParseMakerNoteIFD (host,
1696 stream,
1697 makerNoteCount - 6,
1698 makerNoteOffset + 6,
1699 makerNoteOffset,
1700 minOffset,
1701 maxOffset,
1702 tcCasioMakerNote);
1703
1704 return;
1705
1706 }
1707
1708 }
1709
1710 /*****************************************************************************/
1711
1712 void dng_info::ParseSonyPrivateData (dng_host & /* host */,
1713 dng_stream & /* stream */,
1714 uint64 /* count */,
1715 uint64 /* oldOffset */,
1716 uint64 /* newOffset */)
1717 {
1718
1719 // Sony private data is encrypted, sorry.
1720
1721 }
1722
1723 /*****************************************************************************/
1724
1725 void dng_info::ParseDNGPrivateData (dng_host &host,
1726 dng_stream &stream)
1727 {
1728
1729 if (fShared->fDNGPrivateDataCount < 2)
1730 {
1731 return;
1732 }
1733
1734 // DNG private data should always start with a null-terminated
1735 // company name, to define the format of the private data.
1736
1737 dng_string privateName;
1738
1739 {
1740
1741 char buffer [64];
1742
1743 stream.SetReadPosition (fShared->fDNGPrivateDataOffset);
1744
1745 uint32 readLength = Min_uint32 (fShared->fDNGPrivateDataCount,
1746 sizeof (buffer) - 1);
1747
1748 stream.Get (buffer, readLength);
1749
1750 buffer [readLength] = 0;
1751
1752 privateName.Set (buffer);
1753
1754 }
1755
1756 // Pentax is storing their MakerNote in the DNGPrivateData data.
1757
1758 if (privateName.StartsWith ("PENTAX" ) ||
1759 privateName.StartsWith ("SAMSUNG"))
1760 {
1761
1762 #if qDNGValidate
1763
1764 if (gVerbose)
1765 {
1766 printf ("Parsing Pentax/Samsung DNGPrivateData\n\n");
1767 }
1768
1769 #endif
1770
1771 stream.SetReadPosition (fShared->fDNGPrivateDataOffset + 8);
1772
1773 bool bigEndian = stream.BigEndian ();
1774
1775 uint16 endianMark = stream.Get_uint16 ();
1776
1777 if (endianMark == byteOrderMM)
1778 {
1779 bigEndian = true;
1780 }
1781
1782 else if (endianMark == byteOrderII)
1783 {
1784 bigEndian = false;
1785 }
1786
1787 TempBigEndian temp_endian (stream, bigEndian);
1788
1789 ParseMakerNoteIFD (host,
1790 stream,
1791 fShared->fDNGPrivateDataCount - 10,
1792 fShared->fDNGPrivateDataOffset + 10,
1793 fShared->fDNGPrivateDataOffset,
1794 fShared->fDNGPrivateDataOffset,
1795 fShared->fDNGPrivateDataOffset + fShared->fDNGPrivateDataCount,
1796 tcPentaxMakerNote);
1797
1798 return;
1799
1800 }
1801
1802 // Stop parsing if this is not an Adobe format block.
1803
1804 if (!privateName.Matches ("Adobe"))
1805 {
1806 return;
1807 }
1808
1809 TempBigEndian temp_order (stream);
1810
1811 uint32 section_offset = 6;
1812
1813 while (SafeUint32Add (section_offset, 8) < fShared->fDNGPrivateDataCount)
1814 {
1815
1816 stream.SetReadPosition (SafeUint64Add (fShared->fDNGPrivateDataOffset,
1817 section_offset));
1818
1819 uint32 section_key = stream.Get_uint32 ();
1820 uint32 section_count = stream.Get_uint32 ();
1821
1822 if (section_key == DNG_CHAR4 ('M','a','k','N') && section_count > 6)
1823 {
1824
1825 #if qDNGValidate
1826
1827 if (gVerbose)
1828 {
1829 printf ("Found MakerNote inside DNGPrivateData\n\n");
1830 }
1831
1832 #endif
1833
1834 uint16 order_mark = stream.Get_uint16 ();
1835 int64 old_offset = stream.Get_uint32 ();
1836
1837 uint32 tempSize = SafeUint32Sub (section_count, 6);
1838
1839 AutoPtr<dng_memory_block> tempBlock (host.Allocate (tempSize));
1840
1841 uint64 positionInOriginalFile = stream.PositionInOriginalFile();
1842
1843 stream.Get (tempBlock->Buffer (), tempSize);
1844
1845 dng_stream tempStream (tempBlock->Buffer (),
1846 tempSize,
1847 positionInOriginalFile);
1848
1849 tempStream.SetBigEndian (order_mark == byteOrderMM);
1850
1851 ParseMakerNote (host,
1852 tempStream,
1853 tempSize,
1854 0,
1855 0 - old_offset,
1856 0,
1857 tempSize);
1858
1859 }
1860
1861 else if (section_key == DNG_CHAR4 ('S','R','2',' ') && section_count > 6)
1862 {
1863
1864 #if qDNGValidate
1865
1866 if (gVerbose)
1867 {
1868 printf ("Found Sony private data inside DNGPrivateData\n\n");
1869 }
1870
1871 #endif
1872
1873 uint16 order_mark = stream.Get_uint16 ();
1874 uint64 old_offset = stream.Get_uint32 ();
1875
1876 uint64 new_offset = fShared->fDNGPrivateDataOffset + section_offset + 14;
1877
1878 TempBigEndian sr2_order (stream, order_mark == byteOrderMM);
1879
1880 ParseSonyPrivateData (host,
1881 stream,
1882 section_count - 6,
1883 old_offset,
1884 new_offset);
1885
1886 }
1887
1888 else if (section_key == DNG_CHAR4 ('R','A','F',' ') && section_count > 4)
1889 {
1890
1891 #if qDNGValidate
1892
1893 if (gVerbose)
1894 {
1895 printf ("Found Fuji RAF tags inside DNGPrivateData\n\n");
1896 }
1897
1898 #endif
1899
1900 uint16 order_mark = stream.Get_uint16 ();
1901
1902 uint32 tagCount = stream.Get_uint32 ();
1903
1904 uint64 tagOffset = stream.Position ();
1905
1906 if (tagCount)
1907 {
1908
1909 TempBigEndian raf_order (stream, order_mark == byteOrderMM);
1910
1911 ParseTag (host,
1912 stream,
1913 fExif.Get (),
1914 fShared.Get (),
1915 NULL,
1916 tcFujiRAF,
1917 tcFujiHeader,
1918 ttUndefined,
1919 tagCount,
1920 tagOffset,
1921 0);
1922
1923 stream.SetReadPosition (SafeUint64Add (tagOffset, tagCount));
1924
1925 }
1926
1927 tagCount = stream.Get_uint32 ();
1928
1929 tagOffset = stream.Position ();
1930
1931 if (tagCount)
1932 {
1933
1934 TempBigEndian raf_order (stream, order_mark == byteOrderMM);
1935
1936 ParseTag (host,
1937 stream,
1938 fExif.Get (),
1939 fShared.Get (),
1940 NULL,
1941 tcFujiRAF,
1942 tcFujiRawInfo1,
1943 ttUndefined,
1944 tagCount,
1945 tagOffset,
1946 0);
1947
1948 stream.SetReadPosition (SafeUint64Add (tagOffset, tagCount));
1949
1950 }
1951
1952 tagCount = stream.Get_uint32 ();
1953
1954 tagOffset = stream.Position ();
1955
1956 if (tagCount)
1957 {
1958
1959 TempBigEndian raf_order (stream, order_mark == byteOrderMM);
1960
1961 ParseTag (host,
1962 stream,
1963 fExif.Get (),
1964 fShared.Get (),
1965 NULL,
1966 tcFujiRAF,
1967 tcFujiRawInfo2,
1968 ttUndefined,
1969 tagCount,
1970 tagOffset,
1971 0);
1972
1973 stream.SetReadPosition (SafeUint64Add (tagOffset, tagCount));
1974
1975 }
1976
1977 }
1978
1979 else if (section_key == DNG_CHAR4 ('C','n','t','x') && section_count > 4)
1980 {
1981
1982 #if qDNGValidate
1983
1984 if (gVerbose)
1985 {
1986 printf ("Found Contax Raw header inside DNGPrivateData\n\n");
1987 }
1988
1989 #endif
1990
1991 uint16 order_mark = stream.Get_uint16 ();
1992
1993 uint32 tagCount = stream.Get_uint32 ();
1994
1995 uint64 tagOffset = stream.Position ();
1996
1997 if (tagCount)
1998 {
1999
2000 TempBigEndian contax_order (stream, order_mark == byteOrderMM);
2001
2002 ParseTag (host,
2003 stream,
2004 fExif.Get (),
2005 fShared.Get (),
2006 NULL,
2007 tcContaxRAW,
2008 tcContaxHeader,
2009 ttUndefined,
2010 tagCount,
2011 tagOffset,
2012 0);
2013
2014 }
2015
2016 }
2017
2018 else if (section_key == DNG_CHAR4 ('C','R','W',' ') && section_count > 4)
2019 {
2020
2021 #if qDNGValidate
2022
2023 if (gVerbose)
2024 {
2025 printf ("Found Canon CRW tags inside DNGPrivateData\n\n");
2026 }
2027
2028 #endif
2029
2030 uint16 order_mark = stream.Get_uint16 ();
2031 uint32 entries = stream.Get_uint16 ();
2032
2033 uint64 crwTagStart = stream.Position ();
2034
2035 for (uint32 parsePass = 1; parsePass <= 2; parsePass++)
2036 {
2037
2038 stream.SetReadPosition (crwTagStart);
2039
2040 for (uint32 index = 0; index < entries; index++)
2041 {
2042
2043 uint32 tagCode = stream.Get_uint16 ();
2044
2045 uint32 tagCount = stream.Get_uint32 ();
2046
2047 uint64 tagOffset = stream.Position ();
2048
2049 // We need to grab the model id tag first, and then all the
2050 // other tags.
2051
2052 if ((parsePass == 1) == (tagCode == 0x5834))
2053 {
2054
2055 TempBigEndian tag_order (stream, order_mark == byteOrderMM);
2056
2057 ParseTag (host,
2058 stream,
2059 fExif.Get (),
2060 fShared.Get (),
2061 NULL,
2062 tcCanonCRW,
2063 tagCode,
2064 ttUndefined,
2065 tagCount,
2066 tagOffset,
2067 0);
2068
2069 }
2070
2071 stream.SetReadPosition (tagOffset + tagCount);
2072
2073 }
2074
2075 }
2076
2077 }
2078
2079 else if (section_count > 4)
2080 {
2081
2082 uint32 parentCode = 0;
2083
2084 bool code32 = false;
2085 bool hasType = true;
2086
2087 switch (section_key)
2088 {
2089
2090 case DNG_CHAR4 ('M','R','W',' '):
2091 {
2092 parentCode = tcMinoltaMRW;
2093 code32 = true;
2094 hasType = false;
2095 break;
2096 }
2097
2098 case DNG_CHAR4 ('P','a','n','o'):
2099 {
2100 parentCode = tcPanasonicRAW;
2101 break;
2102 }
2103
2104 case DNG_CHAR4 ('L','e','a','f'):
2105 {
2106 parentCode = tcLeafMOS;
2107 break;
2108 }
2109
2110 case DNG_CHAR4 ('K','o','d','a'):
2111 {
2112 parentCode = tcKodakDCRPrivateIFD;
2113 break;
2114 }
2115
2116 case DNG_CHAR4 ('K','D','C',' '):
2117 {
2118 parentCode = tcKodakKDCPrivateIFD;
2119 break;
2120 }
2121
2122 default:
2123 break;
2124
2125 }
2126
2127 if (parentCode)
2128 {
2129
2130 #if qDNGValidate
2131
2132 if (gVerbose)
2133 {
2134 printf ("Found %s tags inside DNGPrivateData\n\n",
2135 LookupParentCode (parentCode));
2136 }
2137
2138 #endif
2139
2140 uint16 order_mark = stream.Get_uint16 ();
2141 uint32 entries = stream.Get_uint16 ();
2142
2143 for (uint32 index = 0; index < entries; index++)
2144 {
2145
2146 uint32 tagCode = code32 ? stream.Get_uint32 ()
2147 : stream.Get_uint16 ();
2148
2149 uint32 tagType = hasType ? stream.Get_uint16 ()
2150 : ttUndefined;
2151
2152 uint32 tagCount = stream.Get_uint32 ();
2153
2154 uint32 tagSize = SafeUint32Mult (tagCount, TagTypeSize (tagType));
2155
2156 uint64 tagOffset = stream.Position ();
2157
2158 TempBigEndian tag_order (stream, order_mark == byteOrderMM);
2159
2160 ParseTag (host,
2161 stream,
2162 fExif.Get (),
2163 fShared.Get (),
2164 NULL,
2165 parentCode,
2166 tagCode,
2167 tagType,
2168 tagCount,
2169 tagOffset,
2170 0);
2171
2172 stream.SetReadPosition (SafeUint64Add (tagOffset, tagSize));
2173
2174 }
2175
2176 }
2177
2178 }
2179
2180 section_offset = SafeUint32Add (section_offset, 8);
2181 section_offset = SafeUint32Add (section_offset, section_count);
2182
2183 if (section_offset & 1)
2184 {
2185 section_offset = SafeUint32Add (section_offset, 1);
2186 }
2187
2188 }
2189
2190 }
2191
2192 /*****************************************************************************/
2193
2194 void dng_info::Parse (dng_host &host,
2195 dng_stream &stream)
2196 {
2197
2198 fTIFFBlockOffset = stream.Position ();
2199
2200 fTIFFBlockOriginalOffset = stream.PositionInOriginalFile ();
2201
2202 // Check byte order indicator.
2203
2204 uint16 byteOrder = stream.Get_uint16 ();
2205
2206 if (byteOrder == byteOrderII)
2207 {
2208
2209 fBigEndian = false;
2210
2211 #if qDNGValidate
2212
2213 if (gVerbose)
2214 {
2215 printf ("\nUses little-endian byte order\n");
2216 }
2217
2218 #endif
2219
2220 stream.SetLittleEndian ();
2221
2222 }
2223
2224 else if (byteOrder == byteOrderMM)
2225 {
2226
2227 fBigEndian = true;
2228
2229 #if qDNGValidate
2230
2231 if (gVerbose)
2232 {
2233 printf ("\nUses big-endian byte order\n");
2234 }
2235
2236 #endif
2237
2238 stream.SetBigEndian ();
2239
2240 }
2241
2242 else
2243 {
2244
2245 #if qDNGValidate
2246
2247 ReportError ("Unknown byte order");
2248
2249 #endif
2250
2251 ThrowBadFormat ();
2252
2253 }
2254
2255 // Check "magic number" indicator.
2256
2257 fMagic = stream.Get_uint16 ();
2258
2259 #if qDNGValidate
2260
2261 if (gVerbose)
2262 {
2263 printf ("Magic number = %u\n\n", (unsigned) fMagic);
2264 }
2265
2266 #endif
2267
2268 ValidateMagic ();
2269
2270 // Validate BigTIFF header, if any.
2271
2272 if (fMagic == magicBigTIFF)
2273 {
2274
2275 uint16 byteSize = stream.Get_uint16 ();
2276 uint16 zeroPad = stream.Get_uint16 ();
2277
2278 if (byteSize != 8 || zeroPad != 0)
2279 {
2280
2281 #if qDNGValidate
2282
2283 ReportError ("Invalid BigTIFF header");
2284
2285 #endif
2286
2287 ThrowBadFormat ();
2288
2289 }
2290
2291 }
2292
2293 // Parse IFD 0.
2294
2295 uint64 next_offset = (fMagic == magicBigTIFF) ? stream.Get_uint64 ()
2296 : stream.Get_uint32 ();
2297
2298 fExif.Reset (host.Make_dng_exif ());
2299
2300 fShared.Reset (host.Make_dng_shared ());
2301
2302 fIFD.push_back (host.Make_dng_ifd ());
2303
2304 ParseIFD (host,
2305 stream,
2306 fExif.Get (),
2307 fShared.Get (),
2308 fIFD [0],
2309 fTIFFBlockOffset + next_offset,
2310 fTIFFBlockOffset,
2311 0);
2312
2313 next_offset = fIFD [0]->fNextIFD;
2314
2315 // Parse chained IFDs.
2316
2317 while (next_offset)
2318 {
2319
2320 if (next_offset >= stream.Length ())
2321 {
2322
2323 #if qDNGValidate
2324
2325 {
2326
2327 ReportWarning ("Chained IFD offset past end of stream");
2328
2329 }
2330
2331 #endif
2332
2333 break;
2334
2335 }
2336
2337 // Some TIFF file writers forget about the next IFD offset, so
2338 // validate the IFD at that offset before parsing it.
2339
2340 if (!ValidateIFD (stream,
2341 fTIFFBlockOffset + next_offset,
2342 fTIFFBlockOffset))
2343 {
2344
2345 #if qDNGValidate
2346
2347 {
2348
2349 ReportWarning ("Chained IFD is not valid");
2350
2351 }
2352
2353 #endif
2354
2355 break;
2356
2357 }
2358
2359 if (ChainedIFDCount () == kMaxChainedIFDs)
2360 {
2361
2362 #if qDNGValidate
2363
2364 {
2365
2366 ReportWarning ("Chained IFD count exceeds DNG SDK parsing limit");
2367
2368 }
2369
2370 #endif
2371
2372 break;
2373
2374 }
2375
2376 fChainedIFD.push_back (host.Make_dng_ifd ());
2377
2378 fChainedSubIFD.push_back (std::vector <dng_ifd *> ());
2379
2380 ParseIFD (host,
2381 stream,
2382 NULL,
2383 NULL,
2384 fChainedIFD [ChainedIFDCount () - 1],
2385 fTIFFBlockOffset + next_offset,
2386 fTIFFBlockOffset,
2387 tcFirstChainedIFD + ChainedIFDCount () - 1);
2388
2389 next_offset = fChainedIFD [ChainedIFDCount () - 1]->fNextIFD;
2390
2391 }
2392
2393 // Parse SubIFDs.
2394
2395 uint32 searchedIFDs = 0;
2396
2397 bool tooManySubIFDs = false;
2398
2399 while (searchedIFDs < IFDCount () && !tooManySubIFDs)
2400 {
2401
2402 uint32 searchLimit = IFDCount ();
2403
2404 for (uint32 searchIndex = searchedIFDs;
2405 searchIndex < searchLimit && !tooManySubIFDs;
2406 searchIndex++)
2407 {
2408
2409 for (uint32 subIndex = 0;
2410 subIndex < fIFD [searchIndex]->fSubIFDsCount;
2411 subIndex++)
2412 {
2413
2414 if (IFDCount () == kMaxSubIFDs + 1)
2415 {
2416
2417 tooManySubIFDs = true;
2418
2419 break;
2420
2421 }
2422
2423 uint32 subIFDType = fIFD [searchIndex]->fSubIFDsType;
2424
2425 stream.SetReadPosition (fIFD [searchIndex]->fSubIFDsOffset +
2426 subIndex * TagTypeSize (subIFDType));
2427
2428 uint64 sub_ifd_offset = stream.TagValue_uint64 (subIFDType);
2429
2430 fIFD.push_back (host.Make_dng_ifd ());
2431
2432 ParseIFD (host,
2433 stream,
2434 fExif.Get (),
2435 fShared.Get (),
2436 fIFD [IFDCount () - 1],
2437 fTIFFBlockOffset + sub_ifd_offset,
2438 fTIFFBlockOffset,
2439 tcFirstSubIFD + IFDCount () - 2);
2440
2441 }
2442
2443 searchedIFDs = searchLimit;
2444
2445 }
2446
2447 }
2448
2449 #if qDNGValidate
2450
2451 {
2452
2453 if (tooManySubIFDs)
2454 {
2455
2456 ReportWarning ("SubIFD count exceeds DNG SDK parsing limit");
2457
2458 }
2459
2460 }
2461
2462 #endif
2463
2464 // Parse SubIFDs in Chained IFDs. Don't currently need to make this a
2465 // recursive search.
2466
2467 for (uint32 chainedIndex = 0;
2468 chainedIndex < ChainedIFDCount ();
2469 chainedIndex++)
2470 {
2471
2472 for (uint32 subIndex = 0;
2473 subIndex < fChainedIFD [chainedIndex]->fSubIFDsCount;
2474 subIndex++)
2475 {
2476
2477 if (subIndex == kMaxSubIFDs)
2478 {
2479
2480 #if qDNGValidate
2481
2482 ReportWarning ("Chained SubIFD count exceeds DNG SDK parsing limit");
2483
2484 #endif
2485
2486 break;
2487
2488 }
2489
2490 uint32 subIFDType = fChainedIFD [chainedIndex]->fSubIFDsType;
2491
2492 stream.SetReadPosition (fChainedIFD [chainedIndex]->fSubIFDsOffset +
2493 subIndex * TagTypeSize (subIFDType));
2494
2495 uint64 sub_ifd_offset = stream.TagValue_uint64 (subIFDType);
2496
2497 fChainedSubIFD [chainedIndex].push_back (host.Make_dng_ifd ());
2498
2499 ParseIFD (host,
2500 stream,
2501 fExif.Get (),
2502 fShared.Get (),
2503 fChainedSubIFD [chainedIndex] [subIndex],
2504 fTIFFBlockOffset + sub_ifd_offset,
2505 fTIFFBlockOffset,
2506 tcFirstSubIFD + subIndex);
2507
2508 }
2509
2510 }
2511
2512 // Parse EXIF IFD.
2513
2514 if (fShared->fExifIFD)
2515 {
2516
2517 ParseIFD (host,
2518 stream,
2519 fExif.Get (),
2520 fShared.Get (),
2521 NULL,
2522 fTIFFBlockOffset + fShared->fExifIFD,
2523 fTIFFBlockOffset,
2524 tcExifIFD);
2525
2526 }
2527
2528 // Parse GPS IFD.
2529
2530 if (fShared->fGPSInfo)
2531 {
2532
2533 ParseIFD (host,
2534 stream,
2535 fExif.Get (),
2536 fShared.Get (),
2537 NULL,
2538 fTIFFBlockOffset + fShared->fGPSInfo,
2539 fTIFFBlockOffset,
2540 tcGPSInfo);
2541
2542 }
2543
2544 // Parse Interoperability IFD.
2545
2546 if (fShared->fInteroperabilityIFD)
2547 {
2548
2549 // Some Kodak KDC files have bogus Interoperability IFDs, so
2550 // validate the IFD before trying to parse it.
2551
2552 if (ValidateIFD (stream,
2553 fTIFFBlockOffset + fShared->fInteroperabilityIFD,
2554 fTIFFBlockOffset))
2555 {
2556
2557 ParseIFD (host,
2558 stream,
2559 fExif.Get (),
2560 fShared.Get (),
2561 NULL,
2562 fTIFFBlockOffset + fShared->fInteroperabilityIFD,
2563 fTIFFBlockOffset,
2564 tcInteroperabilityIFD);
2565
2566 }
2567
2568 #if qDNGValidate
2569
2570 else
2571 {
2572
2573 ReportWarning ("The Interoperability IFD is not a valid IFD");
2574
2575 }
2576
2577 #endif
2578
2579 }
2580
2581 // Parse Kodak DCR Private IFD.
2582
2583 if (fShared->fKodakDCRPrivateIFD)
2584 {
2585
2586 ParseIFD (host,
2587 stream,
2588 fExif.Get (),
2589 fShared.Get (),
2590 NULL,
2591 fTIFFBlockOffset + fShared->fKodakDCRPrivateIFD,
2592 fTIFFBlockOffset,
2593 tcKodakDCRPrivateIFD);
2594
2595 }
2596
2597 // Parse Kodak KDC Private IFD.
2598
2599 if (fShared->fKodakKDCPrivateIFD)
2600 {
2601
2602 ParseIFD (host,
2603 stream,
2604 fExif.Get (),
2605 fShared.Get (),
2606 NULL,
2607 fTIFFBlockOffset + fShared->fKodakKDCPrivateIFD,
2608 fTIFFBlockOffset,
2609 tcKodakKDCPrivateIFD);
2610
2611 }
2612
2613 // Parse MakerNote tag.
2614
2615 if (fShared->fMakerNoteCount)
2616 {
2617
2618 ParseMakerNote (host,
2619 stream,
2620 (uint32) (fTIFFBlockOffset + fShared->fMakerNoteCount),
2621 fShared->fMakerNoteOffset,
2622 fTIFFBlockOffset,
2623 0,
2624 stream.Length ());
2625
2626 }
2627
2628 // Parse DNGPrivateData tag.
2629
2630 if (fShared->fDNGPrivateDataCount &&
2631 fShared->fDNGVersion)
2632 {
2633
2634 ParseDNGPrivateData (host, stream);
2635
2636 }
2637
2638 #if qDNGValidate
2639
2640 // If we are running dng_validate on stand-alone camera profile file,
2641 // complete the validation of the profile.
2642
2643 if (fMagic == magicExtendedProfile)
2644 {
2645
2646 dng_camera_profile_info &profileInfo = fShared->fCameraProfile;
2647
2648 dng_camera_profile profile;
2649
2650 profile.Parse (stream, profileInfo);
2651
2652 if (profileInfo.fColorPlanes < 3 || !profile.IsValid (profileInfo.fColorPlanes))
2653 {
2654
2655 ReportError ("Invalid camera profile file");
2656
2657 }
2658
2659 }
2660
2661 #endif
2662
2663 }
2664
2665 /*****************************************************************************/
2666
2667 void dng_info::PostParse (dng_host &host)
2668 {
2669
2670 uint32 index;
2671
2672 fExif->PostParse (host, *fShared.Get ());
2673
2674 fShared->PostParse (host, *fExif.Get ());
2675
2676 for (index = 0; index < IFDCount (); index++)
2677 {
2678
2679 fIFD [index]->PostParse ();
2680
2681 }
2682
2683 for (index = 0; index < ChainedIFDCount (); index++)
2684 {
2685
2686 fChainedIFD [index]->PostParse ();
2687
2688 }
2689
2690 for (size_t i = 0; i < fChainedSubIFD.size (); i++)
2691 {
2692
2693 std::vector <dng_ifd *> &chain = fChainedSubIFD [i];
2694
2695 for (size_t j = 0; j < chain.size (); j++)
2696 {
2697
2698 if (chain [j])
2699 {
2700 chain [j]->PostParse ();
2701 }
2702
2703 }
2704
2705 }
2706
2707 if (fShared->fDNGVersion != 0)
2708 {
2709
2710 // Find main IFD.
2711
2712 fMainIndex = -1;
2713
2714 for (index = 0; index < IFDCount (); index++)
2715 {
2716
2717 if (fIFD [index]->fUsesNewSubFileType &&
2718 fIFD [index]->fNewSubFileType == sfMainImage)
2719 {
2720
2721 if (fMainIndex == -1)
2722 {
2723
2724 fMainIndex = index;
2725
2726 }
2727
2728 #if qDNGValidate
2729
2730 else
2731 {
2732
2733 ReportError ("Multiple IFDs marked as main image");
2734
2735 }
2736
2737 #endif
2738
2739 }
2740
2741 else if (fIFD [index]->fNewSubFileType == sfPreviewImage ||
2742 fIFD [index]->fNewSubFileType == sfAltPreviewImage)
2743 {
2744
2745 // Fill in default color space for DNG previews if not included.
2746
2747 if (fIFD [index]->fPreviewInfo.fColorSpace == previewColorSpace_MaxEnum)
2748 {
2749
2750 if (fIFD [index]->fSamplesPerPixel == 1)
2751 {
2752
2753 fIFD [index]->fPreviewInfo.fColorSpace = previewColorSpace_GrayGamma22;
2754
2755 }
2756
2757 else
2758 {
2759
2760 fIFD [index]->fPreviewInfo.fColorSpace = previewColorSpace_sRGB;
2761
2762 }
2763
2764 }
2765
2766 }
2767
2768 }
2769
2770 // Deal with lossless JPEG bug in early DNG versions.
2771
2772 if (fShared->fDNGVersion < dngVersion_1_1_0_0)
2773 {
2774
2775 if (fMainIndex != -1)
2776 {
2777
2778 fIFD [fMainIndex]->fLosslessJPEGBug16 = true;
2779
2780 }
2781
2782 }
2783
2784 // Find mask index.
2785
2786 for (index = 0; index < IFDCount (); index++)
2787 {
2788
2789 if (fIFD [index]->fNewSubFileType == sfTransparencyMask)
2790 {
2791
2792 if (fMaskIndex == -1)
2793 {
2794
2795 fMaskIndex = index;
2796
2797 }
2798
2799 #if qDNGValidate
2800
2801 else
2802 {
2803
2804 ReportError ("Multiple IFDs marked as transparency mask image");
2805
2806 }
2807
2808 #endif
2809
2810 }
2811
2812 }
2813
2814 // Find depth index.
2815
2816 for (index = 0; index < IFDCount (); index++)
2817 {
2818
2819 if (fIFD [index]->fNewSubFileType == sfDepthMap)
2820 {
2821
2822 if (fDepthIndex == -1)
2823 {
2824
2825 fDepthIndex = index;
2826
2827 }
2828
2829 #if qDNGValidate
2830
2831 else
2832 {
2833
2834 ReportError ("Multiple IFDs marked as depth map image");
2835
2836 }
2837
2838 #endif
2839
2840 }
2841
2842 }
2843
2844 // Find enhanced ifd index.
2845
2846 for (index = 0; index < IFDCount (); index++)
2847 {
2848
2849 if (fIFD [index]->fNewSubFileType == sfEnhancedImage)
2850 {
2851
2852 if (fEnhancedIndex == -1)
2853 {
2854
2855 fEnhancedIndex = index;
2856
2857 }
2858
2859 #if qDNGValidate
2860
2861 else
2862 {
2863
2864 ReportError ("Multiple IFDs marked as enhanced image");
2865
2866 }
2867
2868 #endif
2869
2870 }
2871
2872 }
2873
2874 // Find semantic mask ifd indices.
2875
2876 for (index = 0; index < IFDCount (); index++)
2877 {
2878
2879 if (fIFD [index]->fNewSubFileType == sfSemanticMask)
2880 {
2881
2882 fSemanticMaskIndices.push_back (index);
2883
2884 }
2885
2886 }
2887
2888 // Warn about Chained IFDs.
2889
2890 #if qDNGValidate
2891
2892 if (ChainedIFDCount () > 0)
2893 {
2894
2895 ReportWarning ("This file has Chained IFDs, which will be ignored by DNG readers");
2896
2897 }
2898
2899 #endif
2900
2901 }
2902
2903 }
2904
2905 /*****************************************************************************/
2906
2907 bool dng_info::IsValidDNG ()
2908 {
2909
2910 // Check shared info.
2911
2912 if (!fShared->IsValidDNG ())
2913 {
2914
2915 return false;
2916
2917 }
2918
2919 // Check TIFF magic number.
2920
2921 if (fMagic != magicTIFF && fMagic != magicBigTIFF)
2922 {
2923
2924 #if qDNGValidate
2925
2926 ReportError ("Invalid TIFF magic number");
2927
2928 #endif
2929
2930 return false;
2931
2932 }
2933
2934 // Make sure we have a main image IFD.
2935
2936 if (fMainIndex == -1)
2937 {
2938
2939 #if qDNGValidate
2940
2941 ReportError ("Unable to find main image IFD");
2942
2943 #endif
2944
2945 return false;
2946
2947 }
2948
2949 // Make sure is each IFD is valid.
2950
2951 for (uint32 index = 0; index < IFDCount (); index++)
2952 {
2953
2954 uint32 parentCode = (index == 0 ? 0 : tcFirstSubIFD + index - 1);
2955
2956 if (!fIFD [index]->IsValidDNG (*fShared.Get (),
2957 parentCode))
2958 {
2959
2960 // Only errors in the main and transparency mask IFDs are fatal to parsing.
2961
2962 if (index == (uint32) fMainIndex ||
2963 index == (uint32) fMaskIndex)
2964 {
2965
2966 return false;
2967
2968 }
2969
2970 // Also errors to depth map...
2971
2972 if (index == (uint32) fDepthIndex)
2973 {
2974
2975 return false;
2976
2977 }
2978
2979 // Also errors to enhanced image...
2980
2981 if (index == (uint32) fEnhancedIndex)
2982 {
2983
2984 return false;
2985
2986 }
2987
2988 // For now, treat errors in semantic mask images as non-fatal.
2989
2990 }
2991
2992 }
2993
2994 return true;
2995
2996 }
2997
2998 /*****************************************************************************/