diff options
Diffstat (limited to 'libqpdf/InputSource.cc')
-rw-r--r-- | libqpdf/InputSource.cc | 104 |
1 files changed, 43 insertions, 61 deletions
diff --git a/libqpdf/InputSource.cc b/libqpdf/InputSource.cc index 3520ebf2..56e941eb 100644 --- a/libqpdf/InputSource.cc +++ b/libqpdf/InputSource.cc @@ -1,10 +1,10 @@ #include <qpdf/InputSource.hh> -#include <string.h> -#include <stdexcept> -#include <qpdf/QTC.hh> #include <qpdf/PointerHolder.hh> #include <qpdf/QIntC.hh> +#include <qpdf/QTC.hh> +#include <stdexcept> +#include <string.h> InputSource::Members::Members() { @@ -45,17 +45,15 @@ InputSource::readLine(size_t max_line_length) qpdf_offset_t eol = this->findAndSkipNextEOL(); this->last_offset = offset; size_t line_length = QIntC::to_size(eol - offset); - if (line_length < max_line_length) - { + if (line_length < max_line_length) { buf[line_length] = '\0'; } return std::string(buf); } bool -InputSource::findFirst(char const* start_chars, - qpdf_offset_t offset, size_t len, - Finder& finder) +InputSource::findFirst( + char const* start_chars, qpdf_offset_t offset, size_t len, Finder& finder) { // Basic approach: search for the first character of start_chars // starting from offset but not going past len (if len != 0). Once @@ -74,8 +72,7 @@ InputSource::findFirst(char const* start_chars, // To enable us to guarantee null-termination, save an extra byte // so that buf[size] is valid memory. size_t size = sizeof(buf) - 1; - if ((strlen(start_chars) < 1) || (strlen(start_chars) > size)) - { + if ((strlen(start_chars) < 1) || (strlen(start_chars) > size)) { throw std::logic_error( "InputSource::findSource called with" " too small or too large of a character sequence"); @@ -90,8 +87,7 @@ InputSource::findFirst(char const* start_chars, // that will cause return on the next pass. Eventually we will // either be out of range or hit EOF, either of which forces us to // return. - while (true) - { + while (true) { // Do we need to read more data? Pretend size = 5, buf starts // at 0, and start_chars has 3 characters. buf[5] is valid and // null. If p == 2, start_chars could be buf[2] through @@ -99,12 +95,12 @@ InputSource::findFirst(char const* start_chars, // If p points to buf[size], since strlen(start_chars) is // always >= 1, this overflow test will be correct for that // case regardless of start_chars. - if ((p == 0) || ((p + strlen(start_chars)) > (buf + bytes_read))) - { - if (p) - { - QTC::TC("libtests", "InputSource read next block", - ((p == buf + bytes_read) ? 0 : 1)); + if ((p == 0) || ((p + strlen(start_chars)) > (buf + bytes_read))) { + if (p) { + QTC::TC( + "libtests", + "InputSource read next block", + ((p == buf + bytes_read) ? 0 : 1)); buf_offset += (p - buf); } this->seek(buf_offset, SEEK_SET); @@ -113,10 +109,11 @@ InputSource::findFirst(char const* start_chars, // we could guarantee null termination as an extra // protection against overrun when using string functions. bytes_read = this->read(buf, size); - if (bytes_read < strlen(start_chars)) - { - QTC::TC("libtests", "InputSource find EOF", - bytes_read == 0 ? 0 : 1); + if (bytes_read < strlen(start_chars)) { + QTC::TC( + "libtests", + "InputSource find EOF", + bytes_read == 0 ? 0 : 1); return false; } memset(buf + bytes_read, '\0', 1 + (size - bytes_read)); @@ -124,29 +121,24 @@ InputSource::findFirst(char const* start_chars, } // Search for the first character. - if ((p = static_cast<char*>( - memchr(p, start_chars[0], - bytes_read - QIntC::to_size(p - buf)))) != 0) - { - if (p == buf) - { + if ((p = static_cast<char*>(memchr( + p, start_chars[0], bytes_read - QIntC::to_size(p - buf)))) != + 0) { + if (p == buf) { QTC::TC("libtests", "InputSource found match at buf[0]"); } // Found first letter. - if (len != 0) - { + if (len != 0) { // Make sure it's in range. size_t p_relative_offset = QIntC::to_size((p - buf) + (buf_offset - offset)); - if (p_relative_offset >= len) - { + if (p_relative_offset >= len) { // out of range QTC::TC("libtests", "InputSource out of range"); return false; } } - if ((p + strlen(start_chars)) > (buf + bytes_read)) - { + if ((p + strlen(start_chars)) > (buf + bytes_read)) { // If there are not enough bytes left in the file for // start_chars, we will detect this on the next pass // as EOF and return. @@ -157,30 +149,26 @@ InputSource::findFirst(char const* start_chars, // See if p points to a sequence matching start_chars. We // already checked above to make sure we are not going to // overrun memory. - if (strncmp(p, start_chars, strlen(start_chars)) == 0) - { + if (strncmp(p, start_chars, strlen(start_chars)) == 0) { // Call finder.check() with the input source // positioned to the point of the match. this->seek(buf_offset + (p - buf), SEEK_SET); - if (finder.check()) - { + if (finder.check()) { return true; + } else { + QTC::TC( + "libtests", + "InputSource start_chars matched but not check"); } - else - { - QTC::TC("libtests", "InputSource start_chars matched but not check"); - } - } - else - { - QTC::TC("libtests", "InputSource first char matched but not string"); + } else { + QTC::TC( + "libtests", + "InputSource first char matched but not string"); } // This occurrence of the first character wasn't a match. // Skip over it and keep searching. ++p; - } - else - { + } else { // Trigger reading the next block p = buf + bytes_read; } @@ -189,30 +177,24 @@ InputSource::findFirst(char const* start_chars, } bool -InputSource::findLast(char const* start_chars, - qpdf_offset_t offset, size_t len, - Finder& finder) +InputSource::findLast( + char const* start_chars, qpdf_offset_t offset, size_t len, Finder& finder) { bool found = false; qpdf_offset_t after_found_offset = 0; qpdf_offset_t cur_offset = offset; size_t cur_len = len; - while (this->findFirst(start_chars, cur_offset, cur_len, finder)) - { - if (found) - { + while (this->findFirst(start_chars, cur_offset, cur_len, finder)) { + if (found) { QTC::TC("libtests", "InputSource findLast found more than one"); - } - else - { + } else { found = true; } after_found_offset = this->tell(); cur_offset = after_found_offset; cur_len = len - QIntC::to_size((cur_offset - offset)); } - if (found) - { + if (found) { this->seek(after_found_offset, SEEK_SET); } return found; |