2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-03 07:12:28 +00:00

Remove BufferInputSource::Members

This commit is contained in:
m-holger 2022-08-25 14:56:52 +01:00
parent d6a447b654
commit fc4feb6f1a
2 changed files with 44 additions and 66 deletions

View File

@ -54,26 +54,11 @@ class QPDF_DLL_CLASS BufferInputSource: public InputSource
virtual void unreadCh(char ch);
private:
class QPDF_DLL_PRIVATE Members
{
friend class BufferInputSource;
public:
QPDF_DLL
~Members() = default;
private:
Members(bool own_memory, std::string const& description, Buffer* buf);
Members(Members const&) = delete;
bool own_memory;
std::string description;
Buffer* buf;
qpdf_offset_t cur_offset;
qpdf_offset_t max_offset;
};
std::shared_ptr<Members> m;
bool own_memory;
std::string description;
Buffer* buf;
qpdf_offset_t cur_offset;
qpdf_offset_t max_offset;
};
#endif // QPDF_BUFFERINPUTSOURCE_HH

View File

@ -7,8 +7,8 @@
#include <stdexcept>
#include <string.h>
BufferInputSource::Members::Members(
bool own_memory, std::string const& description, Buffer* buf) :
BufferInputSource::BufferInputSource(
std::string const& description, Buffer* buf, bool own_memory) :
own_memory(own_memory),
description(description),
buf(buf),
@ -17,61 +17,55 @@ BufferInputSource::Members::Members(
{
}
BufferInputSource::BufferInputSource(
std::string const& description, Buffer* buf, bool own_memory) :
m(new Members(own_memory, description, buf))
{
}
BufferInputSource::BufferInputSource(
std::string const& description, std::string const& contents) :
m(new Members(true, description, nullptr))
own_memory(true),
description(description),
buf(new Buffer(contents.length())),
cur_offset(0),
max_offset(QIntC::to_offset(buf->getSize()))
{
this->m->buf = new Buffer(contents.length());
this->m->max_offset = QIntC::to_offset(this->m->buf->getSize());
unsigned char* bp = this->m->buf->getBuffer();
memcpy(bp, contents.c_str(), contents.length());
memcpy(buf->getBuffer(), contents.c_str(), contents.length());
}
BufferInputSource::~BufferInputSource()
{
if (this->m->own_memory) {
delete this->m->buf;
if (this->own_memory) {
delete this->buf;
}
}
qpdf_offset_t
BufferInputSource::findAndSkipNextEOL()
{
if (this->m->cur_offset < 0) {
if (this->cur_offset < 0) {
throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
}
qpdf_offset_t end_pos = this->m->max_offset;
if (this->m->cur_offset >= end_pos) {
qpdf_offset_t end_pos = this->max_offset;
if (this->cur_offset >= end_pos) {
this->last_offset = end_pos;
this->m->cur_offset = end_pos;
this->cur_offset = end_pos;
return end_pos;
}
qpdf_offset_t result = 0;
unsigned char const* buffer = this->m->buf->getBuffer();
unsigned char const* buffer = this->buf->getBuffer();
unsigned char const* end = buffer + end_pos;
unsigned char const* p = buffer + this->m->cur_offset;
unsigned char const* p = buffer + this->cur_offset;
while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
++p;
}
if (p < end) {
result = p - buffer;
this->m->cur_offset = result + 1;
this->cur_offset = result + 1;
++p;
while ((this->m->cur_offset < end_pos) &&
((*p == '\r') || (*p == '\n'))) {
while ((this->cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) {
++p;
++this->m->cur_offset;
++this->cur_offset;
}
} else {
this->m->cur_offset = end_pos;
this->cur_offset = end_pos;
result = end_pos;
}
return result;
@ -80,13 +74,13 @@ BufferInputSource::findAndSkipNextEOL()
std::string const&
BufferInputSource::getName() const
{
return this->m->description;
return this->description;
}
qpdf_offset_t
BufferInputSource::tell()
{
return this->m->cur_offset;
return this->cur_offset;
}
void
@ -94,17 +88,17 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence)
{
switch (whence) {
case SEEK_SET:
this->m->cur_offset = offset;
this->cur_offset = offset;
break;
case SEEK_END:
QIntC::range_check(this->m->max_offset, offset);
this->m->cur_offset = this->m->max_offset + offset;
QIntC::range_check(this->max_offset, offset);
this->cur_offset = this->max_offset + offset;
break;
case SEEK_CUR:
QIntC::range_check(this->m->cur_offset, offset);
this->m->cur_offset += offset;
QIntC::range_check(this->cur_offset, offset);
this->cur_offset += offset;
break;
default:
@ -113,42 +107,41 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence)
break;
}
if (this->m->cur_offset < 0) {
if (this->cur_offset < 0) {
throw std::runtime_error(
this->m->description + ": seek before beginning of buffer");
this->description + ": seek before beginning of buffer");
}
}
void
BufferInputSource::rewind()
{
this->m->cur_offset = 0;
this->cur_offset = 0;
}
size_t
BufferInputSource::read(char* buffer, size_t length)
{
if (this->m->cur_offset < 0) {
if (this->cur_offset < 0) {
throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
}
qpdf_offset_t end_pos = this->m->max_offset;
if (this->m->cur_offset >= end_pos) {
qpdf_offset_t end_pos = this->max_offset;
if (this->cur_offset >= end_pos) {
this->last_offset = end_pos;
return 0;
}
this->last_offset = this->m->cur_offset;
size_t len =
std::min(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);
this->last_offset = this->cur_offset;
size_t len = std::min(QIntC::to_size(end_pos - this->cur_offset), length);
memcpy(buffer, this->buf->getBuffer() + this->cur_offset, len);
this->cur_offset += QIntC::to_offset(len);
return len;
}
void
BufferInputSource::unreadCh(char ch)
{
if (this->m->cur_offset > 0) {
--this->m->cur_offset;
if (this->cur_offset > 0) {
--this->cur_offset;
}
}