2012-07-21 09:37:14 +00:00
|
|
|
#include <qpdf/BufferInputSource.hh>
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2012-07-21 09:37:14 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdexcept>
|
2013-11-29 15:20:50 +00:00
|
|
|
#include <algorithm>
|
2019-08-27 14:46:06 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <sstream>
|
2012-07-21 09:37:14 +00:00
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
BufferInputSource::Members::Members(bool own_memory,
|
|
|
|
std::string const& description,
|
|
|
|
Buffer* buf) :
|
2012-07-21 09:37:14 +00:00
|
|
|
own_memory(own_memory),
|
|
|
|
description(description),
|
|
|
|
buf(buf),
|
2019-08-27 14:46:06 +00:00
|
|
|
cur_offset(0),
|
|
|
|
max_offset(buf ? QIntC::to_offset(buf->getSize()) : 0)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
BufferInputSource::Members::~Members()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferInputSource::BufferInputSource(std::string const& description,
|
|
|
|
Buffer* buf, bool own_memory) :
|
|
|
|
m(new Members(own_memory, description, buf))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-21 13:00:06 +00:00
|
|
|
BufferInputSource::BufferInputSource(std::string const& description,
|
|
|
|
std::string const& contents) :
|
2019-06-22 01:32:47 +00:00
|
|
|
m(new Members(true, description, 0))
|
2012-07-21 13:00:06 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->buf = new Buffer(contents.length());
|
2019-08-27 14:46:06 +00:00
|
|
|
this->m->max_offset = QIntC::to_offset(this->m->buf->getSize());
|
2019-06-22 01:32:47 +00:00
|
|
|
unsigned char* bp = this->m->buf->getBuffer();
|
2013-02-24 02:46:21 +00:00
|
|
|
memcpy(bp, contents.c_str(), contents.length());
|
2012-07-21 13:00:06 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 09:37:14 +00:00
|
|
|
BufferInputSource::~BufferInputSource()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->own_memory)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
delete this->m->buf;
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qpdf_offset_t
|
|
|
|
BufferInputSource::findAndSkipNextEOL()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->cur_offset < 0)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
|
|
|
throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
|
|
|
|
}
|
2019-08-27 14:46:06 +00:00
|
|
|
qpdf_offset_t end_pos = this->m->max_offset;
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->cur_offset >= end_pos)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
|
|
|
this->last_offset = end_pos;
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->cur_offset = end_pos;
|
2012-07-21 09:37:14 +00:00
|
|
|
return end_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
qpdf_offset_t result = 0;
|
2019-06-22 01:32:47 +00:00
|
|
|
unsigned char const* buffer = this->m->buf->getBuffer();
|
2020-06-25 07:50:49 +00:00
|
|
|
unsigned char const* end = buffer + end_pos;
|
|
|
|
unsigned char const* p = buffer + this->m->cur_offset;
|
2012-07-21 09:37:14 +00:00
|
|
|
|
2020-06-25 07:50:49 +00:00
|
|
|
while ((p < end) && !((*p == '\r') || (*p == '\n')))
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
if (p < end)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
|
|
|
result = p - buffer;
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->cur_offset = result + 1;
|
2012-07-21 09:37:14 +00:00
|
|
|
++p;
|
2019-06-22 01:32:47 +00:00
|
|
|
while ((this->m->cur_offset < end_pos) &&
|
2012-07-21 09:37:14 +00:00
|
|
|
((*p == '\r') || (*p == '\n')))
|
|
|
|
{
|
|
|
|
++p;
|
2019-06-22 01:32:47 +00:00
|
|
|
++this->m->cur_offset;
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->cur_offset = end_pos;
|
2012-07-21 09:37:14 +00:00
|
|
|
result = end_pos;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const&
|
|
|
|
BufferInputSource::getName() const
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
return this->m->description;
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qpdf_offset_t
|
|
|
|
BufferInputSource::tell()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
return this->m->cur_offset;
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BufferInputSource::seek(qpdf_offset_t offset, int whence)
|
|
|
|
{
|
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_SET:
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->cur_offset = offset;
|
2012-07-21 09:37:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
2020-10-22 09:44:48 +00:00
|
|
|
QIntC::range_check(this->m->max_offset, offset);
|
2019-08-27 14:46:06 +00:00
|
|
|
this->m->cur_offset = this->m->max_offset + offset;
|
2012-07-21 09:37:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
2020-10-22 09:44:48 +00:00
|
|
|
QIntC::range_check(this->m->cur_offset, offset);
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->cur_offset += offset;
|
2012-07-21 09:37:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: invalid argument to BufferInputSource::seek");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->cur_offset < 0)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
|
|
|
throw std::runtime_error(
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->description + ": seek before beginning of buffer");
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BufferInputSource::rewind()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->cur_offset = 0;
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
BufferInputSource::read(char* buffer, size_t length)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->cur_offset < 0)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
|
|
|
throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
|
|
|
|
}
|
2019-08-27 14:46:06 +00:00
|
|
|
qpdf_offset_t end_pos = this->m->max_offset;
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->cur_offset >= end_pos)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
|
|
|
this->last_offset = end_pos;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
this->last_offset = this->m->cur_offset;
|
2013-02-24 02:46:21 +00:00
|
|
|
size_t len = std::min(
|
2019-06-22 01:32:47 +00:00
|
|
|
QIntC::to_size(end_pos - this->m->cur_offset), length);
|
|
|
|
memcpy(buffer, this->m->buf->getBuffer() + this->m->cur_offset, len);
|
|
|
|
this->m->cur_offset += QIntC::to_offset(len);
|
2012-07-21 09:37:14 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BufferInputSource::unreadCh(char ch)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->cur_offset > 0)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
--this->m->cur_offset;
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
}
|