1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
#include <qpdf/BufferInputSource.hh>
#include <qpdf/InputSource_private.hh>
#include <qpdf/Buffer.hh>
#include <qpdf/QIntC.hh>
#include <algorithm>
#include <cstring>
#include <sstream>
using namespace qpdf;
#ifndef QPDF_FUTURE
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) :
own_memory(true),
description(description),
buf(new Buffer(contents.length())),
cur_offset(0),
max_offset(QIntC::to_offset(buf->getSize()))
{
memcpy(buf->getBuffer(), contents.c_str(), contents.length());
}
BufferInputSource::~BufferInputSource()
{
if (own_memory) {
delete buf;
}
}
qpdf_offset_t
BufferInputSource::findAndSkipNextEOL()
{
util::internal_error_if(cur_offset < 0, "BufferInputSource offset < 0");
qpdf_offset_t end_pos = max_offset;
if (cur_offset >= end_pos) {
last_offset = end_pos;
cur_offset = end_pos;
return end_pos;
}
qpdf_offset_t result = 0;
unsigned char const* buffer = buf->getBuffer();
unsigned char const* end = buffer + end_pos;
unsigned char const* p = buffer + cur_offset;
while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
++p;
}
if (p < end) {
result = p - buffer;
cur_offset = result + 1;
++p;
while ((cur_offset < end_pos) && ((*p == '\r') || (*p == '\n'))) {
++p;
++cur_offset;
}
} else {
cur_offset = end_pos;
result = end_pos;
}
return result;
}
std::string const&
BufferInputSource::getName() const
{
return description;
}
qpdf_offset_t
BufferInputSource::tell()
{
return cur_offset;
}
void
BufferInputSource::seek(qpdf_offset_t offset, int whence)
{
switch (whence) {
case SEEK_SET:
cur_offset = offset;
break;
case SEEK_END:
QIntC::range_check(max_offset, offset);
cur_offset = max_offset + offset;
break;
default:
util::assertion(whence == SEEK_CUR, "invalid argument to BufferInputSource::seek");
QIntC::range_check(cur_offset, offset);
cur_offset += offset;
}
if (cur_offset < 0) {
throw std::runtime_error(description + ": seek before beginning of buffer");
}
}
void
BufferInputSource::rewind()
{
cur_offset = 0;
}
size_t
BufferInputSource::read(char* buffer, size_t length)
{
util::internal_error_if(cur_offset < 0, "BufferInputSource offset < 0");
qpdf_offset_t end_pos = max_offset;
if (cur_offset >= end_pos) {
last_offset = end_pos;
return 0;
}
last_offset = cur_offset;
size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length);
memcpy(buffer, buf->getBuffer() + cur_offset, len);
cur_offset += QIntC::to_offset(len);
return len;
}
void
BufferInputSource::unreadCh(char ch)
{
if (cur_offset > 0) {
--cur_offset;
}
}
#else
class BufferInputSource::Members
{
public:
Members(std::string const& description, Buffer* buf, bool own_memory) :
buf(own_memory ? buf : nullptr),
is(description,
buf && buf->getSize() > 0
? std::string_view(reinterpret_cast<const char*>(buf->getBuffer()), buf->getSize())
: std::string_view())
{
}
Members(std::string const& description, std::string const& str) :
content(str),
is(description, content)
{
}
~Members() = default;
std::unique_ptr<Buffer> buf{nullptr};
std::string content;
is::OffsetBuffer is;
};
BufferInputSource::BufferInputSource(std::string const& description, Buffer* buf, bool own_memory) :
m(std::make_unique<Members>(description, buf, own_memory))
{
}
BufferInputSource::BufferInputSource(std::string const& description, std::string const& contents) :
m(std::make_unique<Members>(description, contents))
{
}
BufferInputSource::~BufferInputSource() = default;
qpdf_offset_t
BufferInputSource::findAndSkipNextEOL()
{
auto result = m->is.findAndSkipNextEOL();
last_offset = m->is.getLastOffset();
return result;
}
std::string const&
BufferInputSource::getName() const
{
return m->is.getName();
}
qpdf_offset_t
BufferInputSource::tell()
{
return m->is.tell();
}
void
BufferInputSource::seek(qpdf_offset_t offset, int whence)
{
m->is.seek(offset, whence);
}
void
BufferInputSource::rewind()
{
m->is.rewind();
}
size_t
BufferInputSource::read(char* buffer, size_t length)
{
auto result = m->is.read(buffer, length);
last_offset = m->is.getLastOffset();
return result;
}
void
BufferInputSource::unreadCh(char ch)
{
m->is.unreadCh(ch);
}
#endif // QPDF_FUTURE
qpdf_offset_t
is::OffsetBuffer::findAndSkipNextEOL()
{
util::internal_error_if(pos < 0, "is::OffsetBuffer offset < 0");
auto end_pos = static_cast<qpdf_offset_t>(view_.size());
if (pos >= end_pos) {
last_offset = end_pos + global_offset;
pos = end_pos;
return end_pos + global_offset;
}
qpdf_offset_t result = 0;
auto buffer = view_.begin();
auto end = view_.end();
auto p = buffer + static_cast<std::ptrdiff_t>(pos);
while (p < end && !(*p == '\r' || *p == '\n')) {
++p;
}
if (p < end) {
result = p - buffer;
pos = result + 1;
++p;
while (pos < end_pos && (*p == '\r' || *p == '\n')) {
++p;
++pos;
}
} else {
pos = end_pos;
result = end_pos;
}
return result + global_offset;
}
void
is::OffsetBuffer::seek(qpdf_offset_t offset, int whence)
{
switch (whence) {
case SEEK_SET:
pos = offset - global_offset;
break;
case SEEK_END:
QIntC::range_check(static_cast<qpdf_offset_t>(view_.size()), offset);
pos = static_cast<qpdf_offset_t>(view_.size()) + offset;
break;
default:
util::assertion(whence == SEEK_CUR, "invalid argument to BufferInputSource::seek");
QIntC::range_check(pos, offset);
pos += offset;
}
if (pos < 0) {
throw std::runtime_error(description + ": seek before beginning of buffer");
}
}
size_t
is::OffsetBuffer::read(char* buffer, size_t length)
{
util::internal_error_if(pos < 0, "is::OffsetBuffer offset < 0");
auto end_pos = static_cast<qpdf_offset_t>(view_.size());
if (pos >= end_pos) {
last_offset = end_pos + global_offset;
return 0;
}
last_offset = pos + global_offset;
size_t len = std::min(QIntC::to_size(end_pos - pos), length);
memcpy(buffer, view_.data() + pos, len);
pos += QIntC::to_offset(len);
return len;
}
|