Skip to content

Commit 115efec

Browse files
Forgive not having Exif header in PNG text chunk
* Forgive not having Exif header in PNG text chunk * Fix accessor * Find TIFF marker instead of Exif header for legacy PNG * Make clang-format happy * Apply suggestions from code review Co-authored-by: Kevin Backhouse <[email protected]> * APP1 Exif identifier is padded with zero * Make clang-format happy --------- Co-authored-by: Kevin Backhouse <[email protected]>
1 parent e499392 commit 115efec

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/pngchunk_int.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,17 @@ void PngChunk::parseChunkContent(Image* pImage, const byte* key, size_t keySize,
188188
DataBuf exifData = readRawProfile(arr, false);
189189
size_t length = exifData.size();
190190

191-
if (length >= 6) { // length should have at least the size of exifHeader
192-
// Find the position of Exif header in bytes array.
193-
const std::array<byte, 6> exifHeader{0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
191+
if (length >= 4) { // length should have at least the size of TIFF header
192+
// Find the position of TIFF header in bytes array.
193+
// Forgives the absence of the expected Exif\0 APP1 prefix.
194+
const std::array<byte, 4> tiffHeaderLE{0x49, 0x49, 0x2A, 0x00}; // "II*\0"
195+
const std::array<byte, 4> tiffHeaderBE{0x4D, 0x4D, 0x00, 0x2A}; // "MM\0*"
194196
size_t pos = std::numeric_limits<size_t>::max();
195197

196198
/// \todo Find substring inside an string
197-
for (size_t i = 0; i < length - exifHeader.size(); i++) {
198-
if (exifData.cmpBytes(i, exifHeader.data(), exifHeader.size()) == 0) {
199+
for (size_t i = 0; i < length - tiffHeaderLE.size(); i++) {
200+
if (0 == exifData.cmpBytes(i, tiffHeaderLE.data(), tiffHeaderLE.size()) ||
201+
0 == exifData.cmpBytes(i, tiffHeaderBE.data(), tiffHeaderBE.size())) {
199202
pos = i;
200203
break;
201204
}
@@ -205,9 +208,8 @@ void PngChunk::parseChunkContent(Image* pImage, const byte* key, size_t keySize,
205208

206209
if (pos != std::numeric_limits<size_t>::max()) {
207210
#ifdef EXIV2_DEBUG_MESSAGES
208-
std::cout << "Exiv2::PngChunk::parseChunkContent: Exif header found at position " << pos << "\n";
211+
std::cout << "Exiv2::PngChunk::parseChunkContent: TIFF header found at position " << pos << "\n";
209212
#endif
210-
pos = pos + sizeof(exifHeader);
211213
ByteOrder bo = TiffParser::decode(pImage->exifData(), pImage->iptcData(), pImage->xmpData(),
212214
exifData.c_data(pos), length - pos);
213215
pImage->setByteOrder(bo);

src/pngimage.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,14 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, si
322322
#endif
323323
if (!parsedBuf.empty()) {
324324
if (bExif) {
325+
// check for expected "Exif\0\0" APP1 identifier, punt otherwise
326+
size_t offset = 0;
327+
std::array<byte, 6> exifId{0x45, 0x78, 0x69, 0x66, 0x00, 0x00}; // "Exif\0\0"
328+
if (0 == parsedBuf.cmpBytes(0, exifId.data(), exifId.size())) {
329+
offset = 6;
330+
}
325331
// create memio object with the data, then print the structure
326-
MemIo p(parsedBuf.c_data(6), parsedBuf.size() - 6);
332+
MemIo p(parsedBuf.c_data(offset), parsedBuf.size() - offset);
327333
printTiffStructure(p, out, option, depth + 1);
328334
}
329335
if (bIptc) {

0 commit comments

Comments
 (0)