Skip to content

Commit 44542a9

Browse files
Add readOrThrow and seekOrThrow to BasicIo.
1 parent c5101e0 commit 44542a9

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

include/exiv2/basicio.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "exiv2lib_export.h"
2828

2929
// included header files
30+
#include "error.hpp"
3031
#include "types.hpp"
3132

3233
// + standard includes
@@ -142,6 +143,17 @@ namespace Exiv2 {
142143
0 if failure;
143144
*/
144145
virtual long read(byte* buf, long rcount) = 0;
146+
/*!
147+
@brief Safe version of `read()` that checks for errors and throws
148+
an exception if the read was unsuccessful.
149+
@param buf Pointer to a block of memory into which the read data
150+
is stored. The memory block must be at least \em rcount bytes
151+
long.
152+
@param rcount Maximum number of bytes to read. Fewer bytes may be
153+
read if \em rcount bytes are not available.
154+
@param err Error code to use if an exception is thrown.
155+
*/
156+
void readOrThrow(byte* buf, long rcount, ErrorCode err);
145157
/*!
146158
@brief Read one byte from the IO source. Current IO position is
147159
advanced by one byte.
@@ -176,6 +188,19 @@ namespace Exiv2 {
176188
#else
177189
virtual int seek(long offset, Position pos) = 0;
178190
#endif
191+
/*!
192+
@brief Safe version of `seek()` that checks for errors and throws
193+
an exception if the seek was unsuccessful.
194+
@param offset Number of bytes to move the position relative
195+
to the starting position specified by \em pos
196+
@param pos Position from which the seek should start
197+
@param err Error code to use if an exception is thrown.
198+
*/
199+
#if defined(_MSC_VER)
200+
void seekOrThrow(int64_t offset, Position pos, ErrorCode err);
201+
#else
202+
void seekOrThrow(long offset, Position pos, ErrorCode err);
203+
#endif
179204

180205
/*!
181206
@brief Direct access to the IO data. For files, this is done by

src/basicio.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "futils.hpp"
2929
#include "types.hpp"
3030
#include "error.hpp"
31+
#include "enforce.hpp"
3132
#include "http.hpp"
3233
#include "properties.hpp"
3334
#include "image_int.hpp"
@@ -77,6 +78,21 @@ using nlink_t = short;
7778
// *****************************************************************************
7879
// class member definitions
7980
namespace Exiv2 {
81+
void BasicIo::readOrThrow(byte* buf, long rcount, ErrorCode err) {
82+
const long nread = read(buf, rcount);
83+
enforce(nread == rcount, err);
84+
enforce(!error(), err);
85+
}
86+
87+
#if defined(_MSC_VER)
88+
void BasicIo::seekOrThrow(int64_t offset, Position pos, ErrorCode err) {
89+
#else
90+
void BasicIo::seekOrThrow(long offset, Position pos, ErrorCode err) {
91+
#endif
92+
const int r = seek(offset, pos);
93+
enforce(r == 0, err);
94+
}
95+
8096
//! Internal Pimpl structure of class FileIo.
8197
class FileIo::Impl {
8298
public:

src/image.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,12 @@ namespace {
140140
namespace Exiv2 {
141141
// BasicIo::read() with error checking
142142
static void readOrThrow(BasicIo& iIo, byte* buf, long rcount, ErrorCode err) {
143-
const long nread = iIo.read(buf, rcount);
144-
enforce(nread == rcount, err);
145-
enforce(!iIo.error(), err);
143+
iIo.readOrThrow(buf, rcount, err);
146144
}
147145

148146
// BasicIo::seek() with error checking
149147
static void seekOrThrow(BasicIo& iIo, long offset, BasicIo::Position pos, ErrorCode err) {
150-
const int r = iIo.seek(offset, pos);
151-
enforce(r == 0, err);
148+
iIo.seekOrThrow(offset, pos, err);
152149
}
153150

154151
Image::Image(int imageType, uint16_t supportedMetadata, BasicIo::UniquePtr io)

src/jpgimage.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,12 @@ namespace Exiv2 {
9696

9797
// BasicIo::read() with error checking
9898
static void readOrThrow(BasicIo& iIo, byte* buf, long rcount, ErrorCode err) {
99-
const long nread = iIo.read(buf, rcount);
100-
enforce(nread == rcount, err);
101-
enforce(!iIo.error(), err);
99+
iIo.readOrThrow(buf, rcount, err);
102100
}
103101

104102
// BasicIo::seek() with error checking
105103
static void seekOrThrow(BasicIo& iIo, long offset, BasicIo::Position pos, ErrorCode err) {
106-
const int r = iIo.seek(offset, pos);
107-
enforce(r == 0, err);
104+
iIo.seekOrThrow(offset, pos, err);
108105
}
109106

110107
static inline bool inRange(int lo,int value, int hi)

src/webpimage.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ namespace Exiv2 {
5858
// This static function is a temporary fix in v0.27. In the next version,
5959
// it will be added as a method of BasicIo.
6060
static void readOrThrow(BasicIo& iIo, byte* buf, long rcount, ErrorCode err) {
61-
const long nread = iIo.read(buf, rcount);
62-
enforce(nread == rcount, err);
63-
enforce(!iIo.error(), err);
61+
iIo.readOrThrow(buf, rcount, err);
6462
}
6563

6664
WebPImage::WebPImage(BasicIo::UniquePtr io)

0 commit comments

Comments
 (0)