diff --git a/libqpdf/BufferInputSource.cc b/libqpdf/BufferInputSource.cc index e124854..af2c6bc 100644 --- a/libqpdf/BufferInputSource.cc +++ b/libqpdf/BufferInputSource.cc @@ -151,7 +151,7 @@ is::OffsetBuffer::findAndSkipNextEOL_internal() if (cur_offset < 0) { throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); } - qpdf_offset_t end_pos = max_offset; + auto end_pos = static_cast(view_.size()); if (cur_offset >= end_pos) { last_offset = end_pos + global_offset; cur_offset = end_pos; @@ -159,9 +159,9 @@ is::OffsetBuffer::findAndSkipNextEOL_internal() } 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; + auto buffer = view_.begin(); + auto end = view_.end(); + auto p = buffer + cur_offset; while (p < end && !(*p == '\r' || *p == '\n')) { ++p; @@ -190,8 +190,8 @@ is::OffsetBuffer::seek_internal(qpdf_offset_t offset, int whence) break; case SEEK_END: - QIntC::range_check(max_offset, offset); - cur_offset = max_offset + offset; + QIntC::range_check(static_cast(view_.size()), offset); + cur_offset = static_cast(view_.size()) + offset; break; case SEEK_CUR: @@ -215,7 +215,7 @@ is::OffsetBuffer::read(char* buffer, size_t length) if (cur_offset < 0) { throw std::logic_error("INTERNAL ERROR: is::OffsetBuffer offset < 0"); } - qpdf_offset_t end_pos = max_offset; + auto end_pos = static_cast(view_.size()); if (cur_offset >= end_pos) { last_offset = end_pos + global_offset; return 0; @@ -223,7 +223,7 @@ is::OffsetBuffer::read(char* buffer, size_t length) last_offset = cur_offset + global_offset; size_t len = std::min(QIntC::to_size(end_pos - cur_offset), length); - memcpy(buffer, buf->getBuffer() + cur_offset, len); + memcpy(buffer, view_.data() + cur_offset, len); cur_offset += QIntC::to_offset(len); return len; } diff --git a/libqpdf/qpdf/InputSource_private.hh b/libqpdf/qpdf/InputSource_private.hh index 84ecc75..800530a 100644 --- a/libqpdf/qpdf/InputSource_private.hh +++ b/libqpdf/qpdf/InputSource_private.hh @@ -15,8 +15,11 @@ namespace qpdf::is public: OffsetBuffer(std::string const& description, Buffer* buf, qpdf_offset_t global_offset) : description(description), - buf(buf), - max_offset(buf ? static_cast(buf->getSize()) : 0), + view_( + buf && buf->getSize() + ? std::string_view( + reinterpret_cast(buf->getBuffer()), buf->getSize()) + : std::string_view()), global_offset(global_offset) { if (global_offset < 0) { @@ -76,9 +79,8 @@ namespace qpdf::is void seek_internal(qpdf_offset_t offset, int whence); std::string description; - Buffer* buf; qpdf_offset_t cur_offset{0}; - qpdf_offset_t max_offset; + std::string_view view_; qpdf_offset_t global_offset; };