Skip to content

Commit 68473d9

Browse files
Remove static functions readOrThrow and seekOrThrow.
1 parent 44542a9 commit 68473d9

File tree

3 files changed

+51
-77
lines changed

3 files changed

+51
-77
lines changed

src/image.cpp

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,6 @@ namespace {
138138
// *****************************************************************************
139139
// class member definitions
140140
namespace Exiv2 {
141-
// BasicIo::read() with error checking
142-
static void readOrThrow(BasicIo& iIo, byte* buf, long rcount, ErrorCode err) {
143-
iIo.readOrThrow(buf, rcount, err);
144-
}
145-
146-
// BasicIo::seek() with error checking
147-
static void seekOrThrow(BasicIo& iIo, long offset, BasicIo::Position pos, ErrorCode err) {
148-
iIo.seekOrThrow(offset, pos, err);
149-
}
150-
151141
Image::Image(int imageType, uint16_t supportedMetadata, BasicIo::UniquePtr io)
152142
: io_(std::move(io)),
153143
pixelWidth_(0),
@@ -339,8 +329,8 @@ namespace Exiv2 {
339329

340330
do {
341331
// Read top of directory
342-
seekOrThrow(io, start, BasicIo::beg, kerCorruptedMetadata);
343-
readOrThrow(io, dir.data(), 2, kerCorruptedMetadata);
332+
io.seekOrThrow(start, BasicIo::beg, kerCorruptedMetadata);
333+
io.readOrThrow(dir.data(), 2, kerCorruptedMetadata);
344334
uint16_t dirLength = byteSwap2(dir,0,bSwap);
345335
// Prevent infinite loops. (GHSA-m479-7frc-gqqg)
346336
enforce(dirLength > 0, kerCorruptedMetadata);
@@ -366,7 +356,7 @@ namespace Exiv2 {
366356
}
367357
bFirst = false;
368358

369-
readOrThrow(io, dir.data(), 12, kerCorruptedMetadata);
359+
io.readOrThrow(dir.data(), 12, kerCorruptedMetadata);
370360
uint16_t tag = byteSwap2(dir,0,bSwap);
371361
uint16_t type = byteSwap2(dir,2,bSwap);
372362
uint32_t count = byteSwap4(dir,4,bSwap);
@@ -417,9 +407,9 @@ namespace Exiv2 {
417407

418408
if ( bOffsetIsPointer ) { // read into buffer
419409
const long restore = io.tell(); // save
420-
seekOrThrow(io, offset, BasicIo::beg, kerCorruptedMetadata); // position
421-
readOrThrow(io, buf.data(), static_cast<long>(count_x_size), kerCorruptedMetadata); // read
422-
seekOrThrow(io, restore, BasicIo::beg, kerCorruptedMetadata); // restore
410+
io.seekOrThrow(offset, BasicIo::beg, kerCorruptedMetadata); // position
411+
io.readOrThrow(buf.data(), static_cast<long>(count_x_size), kerCorruptedMetadata); // read
412+
io.seekOrThrow(restore, BasicIo::beg, kerCorruptedMetadata); // restore
423413
}
424414

425415
if ( bPrint ) {
@@ -461,7 +451,7 @@ namespace Exiv2 {
461451
const long restore = io.tell();
462452
offset = byteSwap4(buf,k*size,bSwap);
463453
printIFDStructure(io,out,option,offset,bSwap,c,depth);
464-
seekOrThrow(io, restore, BasicIo::beg, kerCorruptedMetadata);
454+
io.seekOrThrow(restore, BasicIo::beg, kerCorruptedMetadata);
465455
}
466456
} else if ( option == kpsRecursive && tag == 0x83bb /* IPTCNAA */ ) {
467457
if (count > 0) {
@@ -470,11 +460,11 @@ namespace Exiv2 {
470460
}
471461

472462
const long restore = io.tell();
473-
seekOrThrow(io, offset, BasicIo::beg, kerCorruptedMetadata); // position
463+
io.seekOrThrow(offset, BasicIo::beg, kerCorruptedMetadata); // position
474464
std::vector<byte> bytes(count) ; // allocate memory
475465
// TODO: once we have C++11 use bytes.data()
476-
readOrThrow(io, &bytes[0], count, kerCorruptedMetadata);
477-
seekOrThrow(io, restore, BasicIo::beg, kerCorruptedMetadata);
466+
io.readOrThrow(&bytes[0], count, kerCorruptedMetadata);
467+
io.seekOrThrow(restore, BasicIo::beg, kerCorruptedMetadata);
478468
// TODO: once we have C++11 use bytes.data()
479469
IptcData::printStructure(out, makeSliceUntil(&bytes[0], count), depth);
480470
}
@@ -484,23 +474,23 @@ namespace Exiv2 {
484474
uint32_t jump= 10 ;
485475
byte bytes[20] ;
486476
const auto chars = reinterpret_cast<const char*>(&bytes[0]);
487-
seekOrThrow(io, offset, BasicIo::beg, kerCorruptedMetadata); // position
488-
readOrThrow(io, bytes, jump, kerCorruptedMetadata) ; // read
477+
io.seekOrThrow(offset, BasicIo::beg, kerCorruptedMetadata); // position
478+
io.readOrThrow(bytes, jump, kerCorruptedMetadata) ; // read
489479
bytes[jump]=0 ;
490480
if ( ::strcmp("Nikon",chars) == 0 ) {
491481
// tag is an embedded tiff
492482
const long byteslen = count-jump;
493483
DataBuf bytes(byteslen); // allocate a buffer
494-
readOrThrow(io, bytes.data(), byteslen, kerCorruptedMetadata); // read
484+
io.readOrThrow(bytes.data(), byteslen, kerCorruptedMetadata); // read
495485
MemIo memIo(bytes.c_data(), byteslen) ; // create a file
496486
printTiffStructure(memIo,out,option,depth);
497487
} else {
498488
// tag is an IFD
499-
seekOrThrow(io, 0, BasicIo::beg, kerCorruptedMetadata); // position
489+
io.seekOrThrow(0, BasicIo::beg, kerCorruptedMetadata); // position
500490
printIFDStructure(io,out,option,offset,bSwap,c,depth);
501491
}
502492

503-
seekOrThrow(io, restore, BasicIo::beg, kerCorruptedMetadata); // restore
493+
io.seekOrThrow(restore, BasicIo::beg, kerCorruptedMetadata); // restore
504494
}
505495
}
506496

@@ -513,7 +503,7 @@ namespace Exiv2 {
513503
}
514504
}
515505
if ( start ) {
516-
readOrThrow(io, dir.data(), 4, kerCorruptedMetadata);
506+
io.readOrThrow(dir.data(), 4, kerCorruptedMetadata);
517507
start = byteSwap4(dir,0,bSwap);
518508
}
519509
} while (start) ;
@@ -533,7 +523,7 @@ namespace Exiv2 {
533523
DataBuf dir(dirSize);
534524

535525
// read header (we already know for certain that we have a Tiff file)
536-
readOrThrow(io, dir.data(), 8, kerCorruptedMetadata);
526+
io.readOrThrow(dir.data(), 8, kerCorruptedMetadata);
537527
char c = static_cast<char>(dir.read_uint8(0));
538528
bool bSwap = ( c == 'M' && isLittleEndianPlatform() )
539529
|| ( c == 'I' && isBigEndianPlatform() )

src/jpgimage.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ namespace Exiv2 {
9494
constexpr uint16_t Photoshop::iptc_ = 0x0404;
9595
constexpr uint16_t Photoshop::preview_ = 0x040c;
9696

97-
// BasicIo::read() with error checking
98-
static void readOrThrow(BasicIo& iIo, byte* buf, long rcount, ErrorCode err) {
99-
iIo.readOrThrow(buf, rcount, err);
100-
}
101-
102-
// BasicIo::seek() with error checking
103-
static void seekOrThrow(BasicIo& iIo, long offset, BasicIo::Position pos, ErrorCode err) {
104-
iIo.seekOrThrow(offset, pos, err);
105-
}
106-
10797
static inline bool inRange(int lo,int value, int hi)
10898
{
10999
return lo<=value && value <= hi;
@@ -386,7 +376,7 @@ namespace Exiv2 {
386376
byte sizebuf[2];
387377
uint16_t size = 0;
388378
if (markerHasLength(marker)) {
389-
readOrThrow(*io_, sizebuf, 2, kerFailedToReadImageData);
379+
io_->readOrThrow(sizebuf, 2, kerFailedToReadImageData);
390380
size = getUShort(sizebuf, bigEndian);
391381
// `size` is the size of the segment, including the 2-byte size field
392382
// that we just read.
@@ -396,7 +386,7 @@ namespace Exiv2 {
396386
// Read the rest of the segment.
397387
DataBuf buf(size);
398388
if (size > 0) {
399-
readOrThrow(*io_, buf.data(2), size - 2, kerFailedToReadImageData);
389+
io_->readOrThrow(buf.data(2), size - 2, kerFailedToReadImageData);
400390
buf.copyBytes(0, sizebuf, 2);
401391
}
402392

@@ -613,7 +603,7 @@ namespace Exiv2 {
613603
byte sizebuf[2];
614604
uint16_t size = 0;
615605
if (markerHasLength(marker)) {
616-
readOrThrow(*io_, sizebuf, 2, kerFailedToReadImageData);
606+
io_->readOrThrow(sizebuf, 2, kerFailedToReadImageData);
617607
size = getUShort(sizebuf, bigEndian);
618608
// `size` is the size of the segment, including the 2-byte size field
619609
// that we just read.
@@ -624,7 +614,7 @@ namespace Exiv2 {
624614
DataBuf buf(size);
625615
if (size > 0) {
626616
assert(size >= 2); // enforced above
627-
readOrThrow(*io_, buf.data(2), size - 2, kerFailedToReadImageData);
617+
io_->readOrThrow(buf.data(2), size - 2, kerFailedToReadImageData);
628618
buf.copyBytes(0, sizebuf, 2);
629619
}
630620

@@ -844,16 +834,16 @@ namespace Exiv2 {
844834
#ifdef EXIV2_DEBUG_MESSAGES
845835
std::cout << start << ":" << length << std::endl;
846836
#endif
847-
seekOrThrow(*io_, start, BasicIo::beg, kerFailedToReadImageData);
837+
io_->seekOrThrow(start, BasicIo::beg, kerFailedToReadImageData);
848838
DataBuf buf(length);
849-
readOrThrow(*io_, buf.data(), buf.size(), kerFailedToReadImageData);
839+
io_->readOrThrow(buf.data(), buf.size(), kerFailedToReadImageData);
850840
tempIo->write(buf.c_data(), buf.size());
851841
}
852842
}
853843

854-
seekOrThrow(*io_, 0, BasicIo::beg, kerFailedToReadImageData);
844+
io_->seekOrThrow(0, BasicIo::beg, kerFailedToReadImageData);
855845
io_->transfer(*tempIo); // may throw
856-
seekOrThrow(*io_, 0, BasicIo::beg, kerFailedToReadImageData);
846+
io_->seekOrThrow(0, BasicIo::beg, kerFailedToReadImageData);
857847
readMetadata();
858848
}
859849
} // JpegBase::printStructure
@@ -920,7 +910,7 @@ namespace Exiv2 {
920910
byte sizebuf[2];
921911
uint16_t size = 0;
922912
if (markerHasLength(marker)) {
923-
readOrThrow(*io_, sizebuf, 2, kerFailedToReadImageData);
913+
io_->readOrThrow(sizebuf, 2, kerFailedToReadImageData);
924914
size = getUShort(sizebuf, bigEndian);
925915
// `size` is the size of the segment, including the 2-byte size field
926916
// that we just read.
@@ -931,7 +921,7 @@ namespace Exiv2 {
931921
DataBuf buf(size);
932922
if (size > 0) {
933923
assert(size >= 2); // enforced above
934-
readOrThrow(*io_, buf.data(2), size - 2, kerFailedToReadImageData);
924+
io_->readOrThrow(buf.data(2), size - 2, kerFailedToReadImageData);
935925
buf.copyBytes(0, sizebuf, 2);
936926
}
937927

@@ -1021,7 +1011,7 @@ namespace Exiv2 {
10211011
if (!comment_.empty())
10221012
++search;
10231013

1024-
seekOrThrow(*io_, seek, BasicIo::beg, kerNoImageInInputData);
1014+
io_->seekOrThrow(seek, BasicIo::beg, kerNoImageInInputData);
10251015
count = 0;
10261016
marker = advanceToMarker(kerNoImageInInputData);
10271017

@@ -1034,7 +1024,7 @@ namespace Exiv2 {
10341024
byte sizebuf[2];
10351025
uint16_t size = 0;
10361026
if (markerHasLength(marker)) {
1037-
readOrThrow(*io_, sizebuf, 2, kerFailedToReadImageData);
1027+
io_->readOrThrow(sizebuf, 2, kerFailedToReadImageData);
10381028
size = getUShort(sizebuf, bigEndian);
10391029
// `size` is the size of the segment, including the 2-byte size field
10401030
// that we just read.
@@ -1045,7 +1035,7 @@ namespace Exiv2 {
10451035
DataBuf buf(size);
10461036
if (size > 0) {
10471037
assert(size >= 2); // enforced above
1048-
readOrThrow(*io_, buf.data(2), size - 2, kerFailedToReadImageData);
1038+
io_->readOrThrow(buf.data(2), size - 2, kerFailedToReadImageData);
10491039
buf.copyBytes(0, sizebuf, 2);
10501040
}
10511041

src/webpimage.cpp

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@
5555
namespace Exiv2 {
5656
using namespace Exiv2::Internal;
5757

58-
// This static function is a temporary fix in v0.27. In the next version,
59-
// it will be added as a method of BasicIo.
60-
static void readOrThrow(BasicIo& iIo, byte* buf, long rcount, ErrorCode err) {
61-
iIo.readOrThrow(buf, rcount, err);
62-
}
63-
6458
WebPImage::WebPImage(BasicIo::UniquePtr io)
6559
: Image(ImageType::webp, mdNone, std::move(io))
6660
{
@@ -138,7 +132,7 @@ namespace Exiv2 {
138132
DataBuf chunkId(WEBP_TAG_SIZE+1);
139133
chunkId.write_uint8(WEBP_TAG_SIZE, '\0');
140134

141-
readOrThrow(*io_, data, WEBP_TAG_SIZE * 3, Exiv2::kerCorruptedMetadata);
135+
io_->readOrThrow(data, WEBP_TAG_SIZE * 3, Exiv2::kerCorruptedMetadata);
142136
uint64_t filesize = Exiv2::getULong(data + WEBP_TAG_SIZE, littleEndian);
143137

144138
/* Set up header */
@@ -178,19 +172,19 @@ namespace Exiv2 {
178172
case we have any exif or xmp data, also check
179173
for any chunks with alpha frame/layer set */
180174
while (!io_->eof() && static_cast<uint64_t>(io_->tell()) < filesize) {
181-
readOrThrow(*io_, chunkId.data(), WEBP_TAG_SIZE, Exiv2::kerCorruptedMetadata);
182-
readOrThrow(*io_, size_buff, WEBP_TAG_SIZE, Exiv2::kerCorruptedMetadata);
175+
io_->readOrThrow(chunkId.data(), WEBP_TAG_SIZE, Exiv2::kerCorruptedMetadata);
176+
io_->readOrThrow(size_buff, WEBP_TAG_SIZE, Exiv2::kerCorruptedMetadata);
183177
const uint32_t size_u32 = Exiv2::getULong(size_buff, littleEndian);
184178

185179
// Check that `size_u32` is safe to cast to `long`.
186180
enforce(size_u32 <= static_cast<size_t>(std::numeric_limits<unsigned int>::max()),
187181
Exiv2::kerCorruptedMetadata);
188182
const long size = static_cast<long>(size_u32);
189183
DataBuf payload(size);
190-
readOrThrow(*io_, payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
184+
io_->readOrThrow(payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
191185
if ( payload.size() % 2 ) {
192186
byte c = 0;
193-
readOrThrow(*io_, &c, 1, Exiv2::kerCorruptedMetadata);
187+
io_->readOrThrow(&c, 1, Exiv2::kerCorruptedMetadata);
194188
}
195189

196190
/* Chunk with information about features
@@ -315,8 +309,8 @@ namespace Exiv2 {
315309

316310
io_->seek(12, BasicIo::beg);
317311
while (!io_->eof() && static_cast<uint64_t>(io_->tell()) < filesize) {
318-
readOrThrow(*io_, chunkId.data(), 4, Exiv2::kerCorruptedMetadata);
319-
readOrThrow(*io_, size_buff, 4, Exiv2::kerCorruptedMetadata);
312+
io_->readOrThrow(chunkId.data(), 4, Exiv2::kerCorruptedMetadata);
313+
io_->readOrThrow(size_buff, 4, Exiv2::kerCorruptedMetadata);
320314

321315
const uint32_t size_u32 = Exiv2::getULong(size_buff, littleEndian);
322316

@@ -326,7 +320,7 @@ namespace Exiv2 {
326320
const long size = static_cast<long>(size_u32);
327321

328322
DataBuf payload(size);
329-
readOrThrow(*io_, payload.data(), size, Exiv2::kerCorruptedMetadata);
323+
io_->readOrThrow(payload.data(), size, Exiv2::kerCorruptedMetadata);
330324
if ( io_->tell() % 2 ) io_->seek(+1,BasicIo::cur); // skip pad
331325

332326
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8X)) {
@@ -518,7 +512,7 @@ namespace Exiv2 {
518512
DataBuf chunkId(5);
519513
chunkId.write_uint8(4, '\0');
520514

521-
readOrThrow(*io_, data, WEBP_TAG_SIZE * 3, Exiv2::kerCorruptedMetadata);
515+
io_->readOrThrow(data, WEBP_TAG_SIZE * 3, Exiv2::kerCorruptedMetadata);
522516

523517
const uint32_t filesize_u32 =
524518
Safe::add(Exiv2::getULong(data + WEBP_TAG_SIZE, littleEndian), 8U);
@@ -544,8 +538,8 @@ namespace Exiv2 {
544538

545539
chunkId.write_uint8(4, '\0');
546540
while (!io_->eof() && io_->tell() < filesize) {
547-
readOrThrow(*io_, chunkId.data(), WEBP_TAG_SIZE, Exiv2::kerCorruptedMetadata);
548-
readOrThrow(*io_, size_buff, WEBP_TAG_SIZE, Exiv2::kerCorruptedMetadata);
541+
io_->readOrThrow(chunkId.data(), WEBP_TAG_SIZE, Exiv2::kerCorruptedMetadata);
542+
io_->readOrThrow(size_buff, WEBP_TAG_SIZE, Exiv2::kerCorruptedMetadata);
549543

550544
const uint32_t size_u32 = Exiv2::getULong(size_buff, littleEndian);
551545

@@ -566,7 +560,7 @@ namespace Exiv2 {
566560
has_canvas_data = true;
567561
byte size_buf[WEBP_TAG_SIZE];
568562

569-
readOrThrow(*io_, payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
563+
io_->readOrThrow(payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
570564

571565
// Fetch width
572566
memcpy(&size_buf, payload.c_data(4), 3);
@@ -581,7 +575,7 @@ namespace Exiv2 {
581575
enforce(size >= 10, Exiv2::kerCorruptedMetadata);
582576

583577
has_canvas_data = true;
584-
readOrThrow(*io_, payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
578+
io_->readOrThrow(payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
585579
byte size_buf[WEBP_TAG_SIZE];
586580

587581
// Fetch width""
@@ -602,7 +596,7 @@ namespace Exiv2 {
602596
byte size_buf_w[2];
603597
byte size_buf_h[3];
604598

605-
readOrThrow(*io_, payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
599+
io_->readOrThrow(payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
606600

607601
// Fetch width
608602
memcpy(&size_buf_w, payload.c_data(1), 2);
@@ -620,7 +614,7 @@ namespace Exiv2 {
620614
has_canvas_data = true;
621615
byte size_buf[WEBP_TAG_SIZE];
622616

623-
readOrThrow(*io_, payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
617+
io_->readOrThrow(payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
624618

625619
// Fetch width
626620
memcpy(&size_buf, payload.c_data(6), 3);
@@ -632,10 +626,10 @@ namespace Exiv2 {
632626
size_buf[3] = 0;
633627
pixelHeight_ = Exiv2::getULong(size_buf, littleEndian) + 1;
634628
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ICCP)) {
635-
readOrThrow(*io_, payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
629+
io_->readOrThrow(payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
636630
this->setIccProfile(std::move(payload));
637631
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_EXIF)) {
638-
readOrThrow(*io_, payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
632+
io_->readOrThrow(payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
639633

640634
byte size_buff2[2];
641635
// 4 meaningful bytes + 2 padding bytes
@@ -713,7 +707,7 @@ namespace Exiv2 {
713707
exifData_.clear();
714708
}
715709
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_XMP)) {
716-
readOrThrow(*io_, payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
710+
io_->readOrThrow(payload.data(), payload.size(), Exiv2::kerCorruptedMetadata);
717711
xmpPacket_.assign(payload.c_str(), payload.size());
718712
if (!xmpPacket_.empty() && XmpParser::decode(xmpData_, xmpPacket_)) {
719713
#ifndef SUPPRESS_WARNINGS
@@ -756,9 +750,9 @@ namespace Exiv2 {
756750
byte webp[len];
757751
byte data[len];
758752
byte riff[len];
759-
readOrThrow(iIo, riff, len, Exiv2::kerCorruptedMetadata);
760-
readOrThrow(iIo, data, len, Exiv2::kerCorruptedMetadata);
761-
readOrThrow(iIo, webp, len, Exiv2::kerCorruptedMetadata);
753+
iIo.readOrThrow(riff, len, Exiv2::kerCorruptedMetadata);
754+
iIo.readOrThrow(data, len, Exiv2::kerCorruptedMetadata);
755+
iIo.readOrThrow(webp, len, Exiv2::kerCorruptedMetadata);
762756
bool matched_riff = (memcmp(riff, RiffImageId, len) == 0);
763757
bool matched_webp = (memcmp(webp, WebPImageId, len) == 0);
764758
iIo.seek(-12, BasicIo::cur);

0 commit comments

Comments
 (0)