2012-12-25 19:38:18 +00:00
|
|
|
#include <qpdf/OffsetInputSource.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2019-08-27 14:58:20 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
2012-12-25 19:38:18 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
OffsetInputSource::OffsetInputSource(
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<InputSource> proxied, qpdf_offset_t global_offset) :
|
2012-12-25 19:38:18 +00:00
|
|
|
proxied(proxied),
|
|
|
|
global_offset(global_offset)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (global_offset < 0) {
|
2019-08-27 14:58:20 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"OffsetInputSource constructed with negative offset");
|
|
|
|
}
|
|
|
|
this->max_safe_offset =
|
|
|
|
std::numeric_limits<qpdf_offset_t>::max() - global_offset;
|
2012-12-25 19:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OffsetInputSource::~OffsetInputSource()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
qpdf_offset_t
|
|
|
|
OffsetInputSource::findAndSkipNextEOL()
|
|
|
|
{
|
|
|
|
return this->proxied->findAndSkipNextEOL() - this->global_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const&
|
|
|
|
OffsetInputSource::getName() const
|
|
|
|
{
|
|
|
|
return this->proxied->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
qpdf_offset_t
|
|
|
|
OffsetInputSource::tell()
|
|
|
|
{
|
|
|
|
return this->proxied->tell() - this->global_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OffsetInputSource::seek(qpdf_offset_t offset, int whence)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (whence == SEEK_SET) {
|
|
|
|
if (offset > this->max_safe_offset) {
|
2019-08-27 14:58:20 +00:00
|
|
|
std::ostringstream msg;
|
2020-10-21 19:29:28 +00:00
|
|
|
msg.imbue(std::locale::classic());
|
2022-04-02 21:14:10 +00:00
|
|
|
msg << "seeking to " << offset << " offset by " << global_offset
|
2019-08-27 14:58:20 +00:00
|
|
|
<< " would cause an overflow of the offset type";
|
|
|
|
throw std::range_error(msg.str());
|
|
|
|
}
|
2012-12-25 19:38:18 +00:00
|
|
|
this->proxied->seek(offset + global_offset, whence);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2012-12-25 19:38:18 +00:00
|
|
|
this->proxied->seek(offset, whence);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (tell() < 0) {
|
2019-08-27 14:58:20 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"offset input source: seek before beginning of file");
|
|
|
|
}
|
2012-12-25 19:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OffsetInputSource::rewind()
|
|
|
|
{
|
|
|
|
seek(0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
OffsetInputSource::read(char* buffer, size_t length)
|
|
|
|
{
|
2013-12-14 20:06:18 +00:00
|
|
|
size_t result = this->proxied->read(buffer, length);
|
|
|
|
this->setLastOffset(this->proxied->getLastOffset() - global_offset);
|
|
|
|
return result;
|
2012-12-25 19:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OffsetInputSource::unreadCh(char ch)
|
|
|
|
{
|
|
|
|
this->proxied->unreadCh(ch);
|
|
|
|
}
|