2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-23 03:18:59 +00:00
qpdf/libqpdf/BufferInputSource.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
3.3 KiB
C++
Raw Normal View History

#include <qpdf/BufferInputSource.hh>
#include <qpdf/QIntC.hh>
#include <algorithm>
2023-05-20 11:22:32 +00:00
#include <cstring>
#include <sstream>
2022-08-25 13:56:52 +00:00
BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) :
own_memory(own_memory),
description(description),
buf(buf),
cur_offset(0),
max_offset(buf ? QIntC::to_offset(buf->getSize()) : 0)
{
}
BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) :
2022-08-25 13:56:52 +00:00
own_memory(true),
description(description),
buf(new Buffer(contents.length())),
cur_offset(0),
max_offset(QIntC::to_offset(buf->getSize()))
{
2022-08-25 13:56:52 +00:00
memcpy(buf->getBuffer(), contents.c_str(), contents.length());
}
BufferInputSource::~BufferInputSource()
{
2022-08-25 13:56:52 +00:00
if (this->own_memory) {
delete this->buf;
}
}
qpdf_offset_t
BufferInputSource::findAndSkipNextEOL()
{
2022-08-25 13:56:52 +00:00
if (this->cur_offset < 0) {
throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
}
2022-08-25 13:56:52 +00:00
qpdf_offset_t end_pos = this->max_offset;
if (this->cur_offset >= end_pos) {
this->last_offset = end_pos;
2022-08-25 13:56:52 +00:00
this->cur_offset = end_pos;
return end_pos;
}
qpdf_offset_t result = 0;
2022-08-25 13:56:52 +00:00
unsigned char const* buffer = this->buf->getBuffer();
unsigned char const* end = buffer + end_pos;
2022-08-25 13:56:52 +00:00
unsigned char const* p = buffer + this->cur_offset;
while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
++p;
}
if (p < end) {
result = p - buffer;
2022-08-25 13:56:52 +00:00
this->cur_offset = result + 1;
++p;
2022-08-25 13:56:52 +00:00
while ((this->cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) {
++p;
2022-08-25 13:56:52 +00:00
++this->cur_offset;
}
} else {
2022-08-25 13:56:52 +00:00
this->cur_offset = end_pos;
result = end_pos;
}
return result;
}
std::string const&
BufferInputSource::getName() const
{
2022-08-25 13:56:52 +00:00
return this->description;
}
qpdf_offset_t
BufferInputSource::tell()
{
2022-08-25 13:56:52 +00:00
return this->cur_offset;
}
void
BufferInputSource::seek(qpdf_offset_t offset, int whence)
{
switch (whence) {
case SEEK_SET:
2022-08-25 13:56:52 +00:00
this->cur_offset = offset;
break;
case SEEK_END:
2022-08-25 13:56:52 +00:00
QIntC::range_check(this->max_offset, offset);
this->cur_offset = this->max_offset + offset;
break;
case SEEK_CUR:
2022-08-25 13:56:52 +00:00
QIntC::range_check(this->cur_offset, offset);
this->cur_offset += offset;
break;
default:
throw std::logic_error("INTERNAL ERROR: invalid argument to BufferInputSource::seek");
break;
}
2022-08-25 13:56:52 +00:00
if (this->cur_offset < 0) {
throw std::runtime_error(this->description + ": seek before beginning of buffer");
}
}
void
BufferInputSource::rewind()
{
2022-08-25 13:56:52 +00:00
this->cur_offset = 0;
}
size_t
BufferInputSource::read(char* buffer, size_t length)
{
2022-08-25 13:56:52 +00:00
if (this->cur_offset < 0) {
throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
}
2022-08-25 13:56:52 +00:00
qpdf_offset_t end_pos = this->max_offset;
if (this->cur_offset >= end_pos) {
this->last_offset = end_pos;
return 0;
}
2022-08-25 13:56:52 +00:00
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)
{
2022-08-25 13:56:52 +00:00
if (this->cur_offset > 0) {
--this->cur_offset;
}
}