diff options
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/Buffer.cc | 30 | ||||
-rw-r--r-- | libqpdf/QPDF.cc | 34 |
2 files changed, 49 insertions, 15 deletions
diff --git a/libqpdf/Buffer.cc b/libqpdf/Buffer.cc index 0c2dd958..71e219a1 100644 --- a/libqpdf/Buffer.cc +++ b/libqpdf/Buffer.cc @@ -4,17 +4,22 @@ Buffer::Buffer() { - init(0); + init(0, 0, true); } Buffer::Buffer(unsigned long size) { - init(size); + init(size, 0, true); +} + +Buffer::Buffer(unsigned char* buf, unsigned long size) +{ + init(size, buf, false); } Buffer::Buffer(Buffer const& rhs) { - init(0); + init(0, 0, true); copy(rhs); } @@ -31,10 +36,18 @@ Buffer::~Buffer() } void -Buffer::init(unsigned long size) +Buffer::init(unsigned long size, unsigned char* buf, bool own_memory) { + this->own_memory = own_memory; this->size = size; - this->buf = (size ? new unsigned char[size] : 0); + if (own_memory) + { + this->buf = (size ? new unsigned char[size] : 0); + } + else + { + this->buf = buf; + } } void @@ -43,7 +56,7 @@ Buffer::copy(Buffer const& rhs) if (this != &rhs) { this->destroy(); - this->init(rhs.size); + this->init(rhs.size, 0, true); if (this->size) { memcpy(this->buf, rhs.buf, this->size); @@ -54,7 +67,10 @@ Buffer::copy(Buffer const& rhs) void Buffer::destroy() { - delete [] this->buf; + if (this->own_memory) + { + delete [] this->buf; + } this->size = 0; this->buf = 0; } diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index bf9beac5..3ea0f813 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -159,7 +159,8 @@ QPDF::FileInputSource::unreadCh(char ch) } QPDF::BufferInputSource::BufferInputSource(std::string const& description, - Buffer* buf) : + Buffer* buf, bool own_memory) : + own_memory(own_memory), description(description), buf(buf), cur_offset(0) @@ -168,6 +169,10 @@ QPDF::BufferInputSource::BufferInputSource(std::string const& description, QPDF::BufferInputSource::~BufferInputSource() { + if (own_memory) + { + delete this->buf; + } } std::string const& @@ -192,7 +197,7 @@ QPDF::BufferInputSource::seek(off_t offset, int whence) break; case SEEK_END: - this->cur_offset = this->buf->getSize() - offset; + this->cur_offset = this->buf->getSize() + offset; break; case SEEK_CUR: @@ -306,11 +311,19 @@ QPDF::processFile(char const* filename, char const* password) FileInputSource* fi = new FileInputSource(); this->file = fi; fi->setFilename(filename); - if (password) - { - this->provided_password = password; - } - parse(); + parse(password); +} + +void +QPDF::processMemoryFile(char const* description, + char const* buf, size_t length, + char const* password) +{ + this->file = + new BufferInputSource(description, + new Buffer((unsigned char*)buf, length), + true); + parse(password); } void @@ -340,11 +353,16 @@ QPDF::getWarnings() } void -QPDF::parse() +QPDF::parse(char const* password) { static PCRE header_re("^%PDF-(1.\\d+)\\b"); static PCRE eof_re("(?s:startxref\\s+(\\d+)\\s+%%EOF\\b)"); + if (password) + { + this->provided_password = password; + } + std::string line = this->file->readLine(); PCRE::Match m1 = header_re.match(line.c_str()); if (m1) |