Skip to content

Commit 08a68c3

Browse files
Merge pull request #2251 from kevinbackhouse/basicio-ftell
Change return type of BasicIo::tell() to size_t
2 parents 5e4eab0 + ad4e13b commit 08a68c3

File tree

14 files changed

+129
-147
lines changed

14 files changed

+129
-147
lines changed

include/exiv2/basicio.hpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,9 @@ class EXIV2API BasicIo {
198198
//@{
199199
/*!
200200
@brief Get the current IO position.
201-
@return Offset from the start of IO if successful;<BR>
202-
-1 if failure;
201+
@return Offset from the start of IO
203202
*/
204-
[[nodiscard]] virtual long tell() const = 0;
203+
[[nodiscard]] virtual size_t tell() const = 0;
205204
/*!
206205
@brief Get the current size of the IO source in bytes.
207206
@return Size of the IO source in bytes;<BR>
@@ -439,10 +438,9 @@ class EXIV2API FileIo : public BasicIo {
439438
//@{
440439
/*!
441440
@brief Get the current file position.
442-
@return Offset from the start of the file if successful;<BR>
443-
-1 if failure;
441+
@return Offset from the start of the file
444442
*/
445-
[[nodiscard]] long tell() const override;
443+
[[nodiscard]] size_t tell() const override;
446444
/*!
447445
@brief Flush any buffered writes and get the current file size
448446
in bytes.
@@ -624,7 +622,7 @@ class EXIV2API MemIo : public BasicIo {
624622
@brief Get the current IO position.
625623
@return Offset from the start of the memory block
626624
*/
627-
[[nodiscard]] long tell() const override;
625+
[[nodiscard]] size_t tell() const override;
628626
/*!
629627
@brief Get the current memory buffer size in bytes.
630628
@return Size of the in memory data in bytes;<BR>
@@ -874,7 +872,7 @@ class EXIV2API RemoteIo : public BasicIo {
874872
@brief Get the current IO position.
875873
@return Offset from the start of the memory block
876874
*/
877-
[[nodiscard]] long tell() const override;
875+
[[nodiscard]] size_t tell() const override;
878876
/*!
879877
@brief Get the current memory buffer size in bytes.
880878
@return Size of the in memory data in bytes;<BR>

include/exiv2/bmffimage.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ class EXIV2API BmffImage : public Image {
122122
@return address of next box
123123
@warning This function should only be called by readMetadata()
124124
*/
125-
long boxHandler(std::ostream& out, Exiv2::PrintStructureOption option, long pbox_end, int depth);
125+
uint64_t boxHandler(std::ostream& out, Exiv2::PrintStructureOption option, uint64_t pbox_end, int depth);
126126
[[nodiscard]] static std::string indent(int i) {
127127
return std::string(2 * i, ' ');
128128
}
129129

130130
uint32_t fileType_{0};
131-
std::set<uint64_t> visits_;
131+
std::set<size_t> visits_;
132132
uint64_t visits_max_{0};
133133
uint16_t unknownID_{0xffff};
134134
uint16_t exifID_{0xffff};
@@ -140,7 +140,7 @@ class EXIV2API BmffImage : public Image {
140140
/*!
141141
@brief box utilities
142142
*/
143-
static std::string toAscii(long n);
143+
static std::string toAscii(uint32_t n);
144144
std::string boxName(uint32_t box);
145145
static bool superBox(uint32_t box);
146146
static bool fullBox(uint32_t box);

include/exiv2/webpimage.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ class EXIV2API WebPImage : public Image {
6363

6464
private:
6565
void doWriteMetadata(BasicIo& outIo);
66-
//! @name NOT Implemented
67-
//@{
68-
static long getHeaderOffset(const byte* data, size_t data_size, const byte* header, size_t header_size);
66+
67+
//! Finds the offset of header in data. Returns std::string::npos if the header isn't found.
68+
static size_t getHeaderOffset(const byte* data, size_t data_size, const byte* header, size_t header_size);
69+
6970
static bool equalsWebPTag(Exiv2::DataBuf& buf, const char* str);
70-
void debugPrintHex(byte* data, long size);
71-
void decodeChunks(long filesize);
71+
void debugPrintHex(byte* data, size_t size);
72+
void decodeChunks(uint32_t filesize);
7273
void inject_VP8X(BasicIo& iIo, bool has_xmp, bool has_exif, bool has_alpha, bool has_icc, int width, int height);
7374
/* Misc. */
7475
static constexpr byte WEBP_PAD_ODD = 0;

src/basicio.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,10 @@ int FileIo::seek(int64_t offset, Position pos) {
458458
#endif
459459
}
460460

461-
long FileIo::tell() const {
462-
return std::ftell(p_->fp_);
461+
size_t FileIo::tell() const {
462+
const long pos = std::ftell(p_->fp_);
463+
enforce(pos >= 0, ErrorCode::kerInputDataReadFailed);
464+
return static_cast<size_t>(pos);
463465
}
464466

465467
size_t FileIo::size() const {
@@ -786,8 +788,8 @@ int MemIo::munmap() {
786788
return 0;
787789
}
788790

789-
long MemIo::tell() const {
790-
return static_cast<long>(p_->idx_);
791+
size_t MemIo::tell() const {
792+
return p_->idx_;
791793
}
792794

793795
size_t MemIo::size() const {
@@ -1341,8 +1343,8 @@ int RemoteIo::munmap() {
13411343
return 0;
13421344
}
13431345

1344-
long RemoteIo::tell() const {
1345-
return static_cast<long>(p_->idx_);
1346+
size_t RemoteIo::tell() const {
1347+
return p_->idx_;
13461348
}
13471349

13481350
size_t RemoteIo::size() const {

src/bmffimage.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ BmffImage::BmffImage(BasicIo::UniquePtr io, bool /* create */) :
7474
Image(ImageType::bmff, mdExif | mdIptc | mdXmp, std::move(io)) {
7575
} // BmffImage::BmffImage
7676

77-
std::string BmffImage::toAscii(long n) {
77+
std::string BmffImage::toAscii(uint32_t n) {
7878
const auto p = reinterpret_cast<const char*>(&n);
7979
std::string result;
8080
for (int i = 0; i < 4; i++) {
@@ -152,9 +152,9 @@ std::string BmffImage::uuidName(Exiv2::DataBuf& uuid) {
152152
return result;
153153
}
154154

155-
long BmffImage::boxHandler(std::ostream& out /* = std::cout*/, Exiv2::PrintStructureOption option /* = kpsNone */,
156-
long pbox_end, int depth) {
157-
long address = io_->tell();
155+
uint64_t BmffImage::boxHandler(std::ostream& out /* = std::cout*/, Exiv2::PrintStructureOption option /* = kpsNone */,
156+
uint64_t pbox_end, int depth) {
157+
const size_t address = io_->tell();
158158
// never visit a box twice!
159159
if (depth == 0)
160160
visits_.clear();
@@ -198,22 +198,22 @@ long BmffImage::boxHandler(std::ostream& out /* = std::cout*/, Exiv2::PrintStruc
198198
}
199199

200200
// read data in box and restore file position
201-
long restore = io_->tell();
201+
const size_t restore = io_->tell();
202202
enforce(box_length >= hdrsize, Exiv2::ErrorCode::kerCorruptedMetadata);
203-
enforce(box_length - hdrsize <= static_cast<uint64_t>(pbox_end - restore), Exiv2::ErrorCode::kerCorruptedMetadata);
203+
enforce(box_length - hdrsize <= pbox_end - restore, Exiv2::ErrorCode::kerCorruptedMetadata);
204204

205-
const auto buffer_size = static_cast<size_t>(box_length - hdrsize);
205+
const auto buffer_size = box_length - hdrsize;
206206
if (skipBox(box_type)) {
207207
if (bTrace) {
208208
out << std::endl;
209209
}
210210
// The enforce() above checks that restore + buffer_size won't
211211
// exceed pbox_end, and by implication, won't exceed LONG_MAX
212-
return restore + static_cast<long>(buffer_size);
212+
return restore + buffer_size;
213213
}
214214

215-
DataBuf data(buffer_size);
216-
const long box_end = restore + static_cast<long>(data.size());
215+
DataBuf data(static_cast<size_t>(buffer_size));
216+
const size_t box_end = restore + data.size();
217217
io_->read(data.data(), data.size());
218218
io_->seek(restore, BasicIo::beg);
219219

@@ -478,26 +478,25 @@ long BmffImage::boxHandler(std::ostream& out /* = std::cout*/, Exiv2::PrintStruc
478478
void BmffImage::parseTiff(uint32_t root_tag, uint64_t length, uint64_t start) {
479479
enforce(start <= io_->size(), ErrorCode::kerCorruptedMetadata);
480480
enforce(length <= io_->size() - start, ErrorCode::kerCorruptedMetadata);
481-
enforce(start <= std::numeric_limits<uint64_t>::max(), ErrorCode::kerCorruptedMetadata);
482-
enforce(length <= std::numeric_limits<uint64_t>::max(), ErrorCode::kerCorruptedMetadata);
481+
enforce(start <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max()), ErrorCode::kerCorruptedMetadata);
482+
enforce(length <= std::numeric_limits<size_t>::max(), ErrorCode::kerCorruptedMetadata);
483483

484484
// read and parse exif data
485-
long restore = io_->tell();
485+
const size_t restore = io_->tell();
486486
DataBuf exif(static_cast<size_t>(length));
487-
io_->seek(static_cast<long>(start), BasicIo::beg);
487+
io_->seek(static_cast<int64_t>(start), BasicIo::beg);
488488
if (exif.size() > 8 && io_->read(exif.data(), exif.size()) == exif.size()) {
489489
// hunt for "II" or "MM"
490-
long eof = 0xffffffff; // impossible value for punt
491-
long punt = eof;
490+
const size_t eof = std::numeric_limits<size_t>::max(); // impossible value for punt
491+
size_t punt = eof;
492492
for (size_t i = 0; i < exif.size() - 8 && punt == eof; i += 2) {
493493
if (exif.read_uint8(i) == exif.read_uint8(i + 1))
494494
if (exif.read_uint8(i) == 'I' || exif.read_uint8(i) == 'M')
495-
punt = static_cast<long>(i);
495+
punt = i;
496496
}
497497
if (punt != eof) {
498-
Internal::TiffParserWorker::decode(exifData(), iptcData(), xmpData(), exif.c_data(punt),
499-
static_cast<uint32_t>(exif.size() - punt), root_tag,
500-
Internal::TiffMapping::findDecoder);
498+
Internal::TiffParserWorker::decode(exifData(), iptcData(), xmpData(), exif.c_data(punt), exif.size() - punt,
499+
root_tag, Internal::TiffMapping::findDecoder);
501500
}
502501
}
503502
io_->seek(restore, BasicIo::beg);
@@ -506,7 +505,7 @@ void BmffImage::parseTiff(uint32_t root_tag, uint64_t length, uint64_t start) {
506505
void BmffImage::parseTiff(uint32_t root_tag, uint64_t length) {
507506
if (length > 8) {
508507
enforce(length - 8 <= io_->size() - io_->tell(), ErrorCode::kerCorruptedMetadata);
509-
enforce(length - 8 <= std::numeric_limits<uint64_t>::max(), ErrorCode::kerCorruptedMetadata);
508+
enforce(length - 8 <= std::numeric_limits<size_t>::max(), ErrorCode::kerCorruptedMetadata);
510509
DataBuf data(static_cast<size_t>(length - 8u));
511510
const size_t bufRead = io_->read(data.data(), data.size());
512511

@@ -524,8 +523,8 @@ void BmffImage::parseXmp(uint64_t length, uint64_t start) {
524523
enforce(start <= io_->size(), ErrorCode::kerCorruptedMetadata);
525524
enforce(length <= io_->size() - start, ErrorCode::kerCorruptedMetadata);
526525

527-
long restore = io_->tell();
528-
io_->seek(static_cast<long>(start), BasicIo::beg);
526+
const size_t restore = io_->tell();
527+
io_->seek(static_cast<int64_t>(start), BasicIo::beg);
529528

530529
auto lengthSizeT = static_cast<size_t>(length);
531530
DataBuf xmp(lengthSizeT + 1);
@@ -547,9 +546,8 @@ void BmffImage::parseXmp(uint64_t length, uint64_t start) {
547546
void BmffImage::parseCr3Preview(DataBuf& data, std::ostream& out, bool bTrace, uint8_t version, size_t width_offset,
548547
size_t height_offset, size_t size_offset, size_t relative_position) {
549548
// Derived from https://github.com/lclevy/canon_cr3
550-
long here = io_->tell();
551-
enforce(here >= 0 && here <= std::numeric_limits<long>::max() - static_cast<long>(relative_position),
552-
ErrorCode::kerCorruptedMetadata);
549+
const size_t here = io_->tell();
550+
enforce(here <= std::numeric_limits<size_t>::max() - relative_position, ErrorCode::kerCorruptedMetadata);
553551
NativePreview nativePreview;
554552
nativePreview.position_ = here + relative_position;
555553
nativePreview.width_ = data.read_uint16(width_offset, endian_);
@@ -600,8 +598,8 @@ void BmffImage::readMetadata() {
600598
exifID_ = unknownID_;
601599
xmpID_ = unknownID_;
602600

603-
long address = 0;
604-
const auto file_end = static_cast<long>(io_->size());
601+
uint64_t address = 0;
602+
const auto file_end = io_->size();
605603
while (address < file_end) {
606604
io_->seek(address, BasicIo::beg);
607605
address = boxHandler(std::cout, kpsNone, file_end, 0);
@@ -635,8 +633,8 @@ void BmffImage::printStructure(std::ostream& out, Exiv2::PrintStructureOption op
635633
openOrThrow();
636634
IoCloser closer(*io_);
637635

638-
long address = 0;
639-
const auto file_end = static_cast<long>(io_->size());
636+
uint64_t address = 0;
637+
const auto file_end = io_->size();
640638
while (address < file_end) {
641639
io_->seek(address, BasicIo::beg);
642640
address = boxHandler(out, option, file_end, depth);
@@ -682,7 +680,7 @@ bool isBmffType(BasicIo& iIo, bool advance) {
682680
bool const is_video = (buf[8] == 'q' && buf[9] == 't' && buf[10] == ' ' && buf[11] == ' ');
683681
bool matched = is_jxl || (is_ftyp && !is_video);
684682
if (!advance || !matched) {
685-
iIo.seek(static_cast<long>(0), BasicIo::beg);
683+
iIo.seek(0, BasicIo::beg);
686684
}
687685
return matched;
688686
}

src/epsimage.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "config.h"
1212

1313
#include "basicio.hpp"
14+
#include "enforce.hpp"
1415
#include "epsimage.hpp"
1516
#include "error.hpp"
1617
#include "futils.hpp"
@@ -101,13 +102,8 @@ void writeTemp(BasicIo& tempIo, const std::string& data) {
101102

102103
//! Get the current write position of temp file, taking care of errors
103104
uint32_t posTemp(const BasicIo& tempIo) {
104-
const long pos = tempIo.tell();
105-
if (pos == -1) {
106-
#ifndef SUPPRESS_WARNINGS
107-
EXV_WARNING << "Internal error while determining current write position in temporary file.\n";
108-
#endif
109-
throw Error(ErrorCode::kerImageWriteFailed);
110-
}
105+
const size_t pos = tempIo.tell();
106+
enforce(pos <= std::numeric_limits<uint32_t>::max(), ErrorCode::kerImageWriteFailed);
111107
return static_cast<uint32_t>(pos);
112108
}
113109

@@ -1167,7 +1163,7 @@ bool isEpsType(BasicIo& iIo, bool advance) {
11671163
bufSize = i.size();
11681164
}
11691165
}
1170-
const long restore = iIo.tell(); // save
1166+
const size_t restore = iIo.tell(); // save
11711167
DataBuf buf = iIo.read(bufSize);
11721168
if (iIo.error() || buf.size() != bufSize) {
11731169
iIo.seek(restore, BasicIo::beg);

src/image.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static bool typeValid(uint16_t type) {
287287
return type >= 1 && type <= 13;
288288
}
289289

290-
static std::set<long> visits; // #547
290+
static std::set<size_t> visits; // #547
291291

292292
void Image::printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStructureOption option, size_t start,
293293
bool bSwap, char c, int depth) {
@@ -370,7 +370,7 @@ void Image::printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStruct
370370
const bool bOffsetIsPointer = count_x_size > 4;
371371

372372
if (bOffsetIsPointer) { // read into buffer
373-
const long restore = io.tell(); // save
373+
const size_t restore = io.tell(); // save
374374
io.seekOrThrow(offset, BasicIo::beg, ErrorCode::kerCorruptedMetadata); // position
375375
io.readOrThrow(buf.data(), count_x_size, ErrorCode::kerCorruptedMetadata); // read
376376
io.seekOrThrow(restore, BasicIo::beg, ErrorCode::kerCorruptedMetadata); // restore
@@ -410,7 +410,7 @@ void Image::printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStruct
410410

411411
if (option == kpsRecursive && (tag == 0x8769 /* ExifTag */ || tag == 0x014a /*SubIFDs*/ || type == tiffIfd)) {
412412
for (size_t k = 0; k < count; k++) {
413-
const long restore = io.tell();
413+
const size_t restore = io.tell();
414414
offset = byteSwap4(buf, k * size, bSwap);
415415
printIFDStructure(io, out, option, offset, bSwap, c, depth);
416416
io.seekOrThrow(restore, BasicIo::beg, ErrorCode::kerCorruptedMetadata);
@@ -421,7 +421,7 @@ void Image::printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStruct
421421
throw Error(ErrorCode::kerCorruptedMetadata);
422422
}
423423

424-
const long restore = io.tell();
424+
const size_t restore = io.tell();
425425
io.seekOrThrow(offset, BasicIo::beg, ErrorCode::kerCorruptedMetadata); // position
426426
std::vector<byte> bytes(count); // allocate memory
427427
// TODO: once we have C++11 use bytes.data()
@@ -431,7 +431,7 @@ void Image::printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStruct
431431
IptcData::printStructure(out, makeSliceUntil(bytes.data(), count), depth);
432432
}
433433
} else if (option == kpsRecursive && tag == 0x927c /* MakerNote */ && count > 10) {
434-
const long restore = io.tell(); // save
434+
const size_t restore = io.tell(); // save
435435

436436
uint32_t jump = 10;
437437
byte bytes[20];

0 commit comments

Comments
 (0)