1818#include " pngimage.hpp"
1919#include " tiffimage.hpp"
2020#include " types.hpp"
21+ #include " utils.hpp"
2122
2223#include < array>
2324#include < iostream>
@@ -171,14 +172,8 @@ static bool tEXtToDataBuf(const byte* bytes, long length, DataBuf& result) {
171172 return true ;
172173}
173174
174- std::string upper (const std::string& str) {
175- std::string result;
176- transform (str.begin (), str.end (), std::back_inserter (result), toupper);
177- return result;
178- }
179-
180175std::string::size_type findi (const std::string& str, const std::string& substr) {
181- return upper ( str) .find (upper ( substr) );
176+ return str.find (substr);
182177}
183178
184179void PngImage::printStructure (std::ostream& out, PrintStructureOption option, int depth) {
@@ -194,14 +189,14 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, in
194189 chType[4 ] = 0 ;
195190
196191 if (option == kpsBasic || option == kpsXMP || option == kpsIccProfile || option == kpsRecursive) {
197- constexpr auto xmpKey = " XML:com.adobe.xmp" ;
198- constexpr auto exifKey = " Raw profile type exif" ;
199- constexpr auto app1Key = " Raw profile type APP1" ;
200- constexpr auto iptcKey = " Raw profile type iptc" ;
201- constexpr auto iccKey = " icc" ;
202- constexpr auto softKey = " Software" ;
203- constexpr auto commKey = " Comment" ;
204- constexpr auto descKey = " Description" ;
192+ const auto xmpKey = upper ( " XML:com.adobe.xmp" ) ;
193+ const auto exifKey = upper ( " Raw profile type exif" ) ;
194+ const auto app1Key = upper ( " Raw profile type APP1" ) ;
195+ const auto iptcKey = upper ( " Raw profile type iptc" ) ;
196+ const auto iccKey = upper ( " icc" ) ;
197+ const auto softKey = upper ( " Software" ) ;
198+ const auto commKey = upper ( " Comment" ) ;
199+ const auto descKey = upper ( " Description" ) ;
205200
206201 bool bPrint = option == kpsBasic || option == kpsRecursive;
207202 if (bPrint) {
@@ -234,8 +229,10 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, in
234229 }
235230
236231 DataBuf buff (dataOffset);
237- bufRead = io_->read (buff.data (), dataOffset);
238- enforce (bufRead == dataOffset, ErrorCode::kerFailedToReadImageData);
232+ if (dataOffset > 0 ) {
233+ bufRead = io_->read (buff.data (), dataOffset);
234+ enforce (bufRead == dataOffset, ErrorCode::kerFailedToReadImageData);
235+ }
239236 io_->seek (restore, BasicIo::beg);
240237
241238 // format output
@@ -273,15 +270,14 @@ void PngImage::printStructure(std::ostream& out, PrintStructureOption option, in
273270 bool eXIf = std::strcmp (chType, " eXIf" ) == 0 ;
274271
275272 // for XMP, ICC etc: read and format data
276- // / \todo inside findi we are transforming the dataString to uppercase. Therefore we are transforming it
277- // / several times when we could do it just once and reuse it.
278- bool bXMP = option == kpsXMP && findi (dataString, xmpKey) == 0 ;
279- bool bICC = option == kpsIccProfile && findi (dataString, iccKey) == 0 ;
280- bool bExif = option == kpsRecursive && (findi (dataString, exifKey) == 0 || findi (dataString, app1Key) == 0 );
281- bool bIptc = option == kpsRecursive && findi (dataString, iptcKey) == 0 ;
282- bool bSoft = option == kpsRecursive && findi (dataString, softKey) == 0 ;
283- bool bComm = option == kpsRecursive && findi (dataString, commKey) == 0 ;
284- bool bDesc = option == kpsRecursive && findi (dataString, descKey) == 0 ;
273+ const auto dataStringU = upper (dataString);
274+ bool bXMP = option == kpsXMP && findi (dataStringU, xmpKey) == 0 ;
275+ bool bICC = option == kpsIccProfile && findi (dataStringU, iccKey) == 0 ;
276+ bool bExif = option == kpsRecursive && (findi (dataStringU, exifKey) == 0 || findi (dataStringU, app1Key) == 0 );
277+ bool bIptc = option == kpsRecursive && findi (dataStringU, iptcKey) == 0 ;
278+ bool bSoft = option == kpsRecursive && findi (dataStringU, softKey) == 0 ;
279+ bool bComm = option == kpsRecursive && findi (dataStringU, commKey) == 0 ;
280+ bool bDesc = option == kpsRecursive && findi (dataStringU, descKey) == 0 ;
285281 bool bDump = bXMP || bICC || bExif || bIptc || bSoft || bComm || bDesc || eXIf;
286282
287283 if (bDump) {
@@ -426,7 +422,9 @@ void PngImage::readMetadata() {
426422 if (chunkType == " IEND" || chunkType == " IHDR" || chunkType == " tEXt" || chunkType == " zTXt" ||
427423 chunkType == " eXIf" || chunkType == " iTXt" || chunkType == " iCCP" ) {
428424 DataBuf chunkData (chunkLength);
429- readChunk (chunkData, *io_); // Extract chunk data.
425+ if (chunkLength > 0 ) {
426+ readChunk (chunkData, *io_); // Extract chunk data.
427+ }
430428
431429 if (chunkType == " IEND" ) {
432430 return ; // Last chunk found: we stop parsing.
0 commit comments