diff --git a/include/qpdf/InputSource.hh b/include/qpdf/InputSource.hh index 7a5e8ec8..94817155 100644 --- a/include/qpdf/InputSource.hh +++ b/include/qpdf/InputSource.hh @@ -78,7 +78,7 @@ class QPDF_DLL_CLASS InputSource // semantically equivalent to seek(-1, SEEK_CUR) but is much more efficient. virtual void unreadCh(char ch) = 0; - // The following methods are for use by QPDFTokenizer + // The following methods are for internal use by qpdf only. inline qpdf_offset_t fastTell(); inline bool fastRead(char&); inline void fastUnread(bool); @@ -111,57 +111,4 @@ class QPDF_DLL_CLASS InputSource qpdf_offset_t buf_start = 0; }; -inline void -InputSource::loadBuffer() -{ - this->buf_idx = 0; - this->buf_len = qpdf_offset_t(read(this->buffer, this->buf_size)); - // NB read sets last_offset - this->buf_start = this->last_offset; -} - -inline qpdf_offset_t -InputSource::fastTell() -{ - if (this->buf_len == 0) { - loadBuffer(); - } else { - auto curr = tell(); - if (curr < this->buf_start || curr >= (this->buf_start + this->buf_len)) { - loadBuffer(); - } else { - this->last_offset = curr; - this->buf_idx = curr - this->buf_start; - } - } - return this->last_offset; -} - -inline bool -InputSource::fastRead(char& ch) -{ - // Before calling fastRead, fastTell must be called to prepare the buffer. Once reading is - // complete, fastUnread must be called to set the correct file position. - if (this->buf_idx < this->buf_len) { - ch = this->buffer[this->buf_idx]; - ++(this->buf_idx); - ++(this->last_offset); - return true; - - } else if (this->buf_len == 0) { - return false; - } else { - seek(this->buf_start + this->buf_len, SEEK_SET); - fastTell(); - return fastRead(ch); - } -} - -inline void -InputSource::fastUnread(bool back) -{ - this->last_offset -= back ? 1 : 0; - seek(this->last_offset, SEEK_SET); -} - #endif // QPDF_INPUTSOURCE_HH diff --git a/libqpdf/InputSource.cc b/libqpdf/InputSource.cc index 9389c2f5..12778522 100644 --- a/libqpdf/InputSource.cc +++ b/libqpdf/InputSource.cc @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/libqpdf/QPDFTokenizer.cc b/libqpdf/QPDFTokenizer.cc index 7f7c6d9e..9eba21bd 100644 --- a/libqpdf/QPDFTokenizer.cc +++ b/libqpdf/QPDFTokenizer.cc @@ -3,6 +3,7 @@ // DO NOT USE ctype -- it is locale dependent for some things, and it's not worth the risk of // including it in case it may accidentally be used. +#include #include #include #include diff --git a/libqpdf/qpdf/InputSource_private.hh b/libqpdf/qpdf/InputSource_private.hh new file mode 100644 index 00000000..a81efe40 --- /dev/null +++ b/libqpdf/qpdf/InputSource_private.hh @@ -0,0 +1,59 @@ +#ifndef QPDF_INPUTSOURCE_PRIVATE_HH +#define QPDF_INPUTSOURCE_PRIVATE_HH + +#include + +inline void +InputSource::loadBuffer() +{ + buf_idx = 0; + buf_len = qpdf_offset_t(read(buffer, buf_size)); + // NB read sets last_offset + buf_start = last_offset; +} + +inline qpdf_offset_t +InputSource::fastTell() +{ + if (buf_len == 0) { + loadBuffer(); + } else { + auto curr = tell(); + if (curr < buf_start || curr >= (buf_start + buf_len)) { + loadBuffer(); + } else { + last_offset = curr; + buf_idx = curr - buf_start; + } + } + return last_offset; +} + +inline bool +InputSource::fastRead(char& ch) +{ + // Before calling fastRead, fastTell must be called to prepare the buffer. Once reading is + // complete, fastUnread must be called to set the correct file position. + if (buf_idx < buf_len) { + ch = buffer[buf_idx]; + ++(buf_idx); + ++(last_offset); + return true; + + } else if (buf_len == 0) { + return false; + } else { + seek(buf_start + buf_len, SEEK_SET); + fastTell(); + return fastRead(ch); + } +} + +inline void +InputSource::fastUnread(bool back) +{ + last_offset -= back ? 1 : 0; + seek(last_offset, SEEK_SET); +} + +#endif // QPDF_INPUTSOURCE_PRIVATE_HH