2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Revert removal of unreadCh change for performance

Turns out unreadCh is much more efficient than seek(-1, SEEK_CUR).
Update comments and code to reflect this.
This commit is contained in:
Jay Berkenbilt 2020-10-27 10:51:41 -04:00
parent 81d2c548dc
commit bcea54fcaa
8 changed files with 24 additions and 49 deletions

View File

@ -81,15 +81,6 @@
disables passing -rpath to the linker when building shared
libraries with libtool. Fixes #422.
2020-10-18 Jay Berkenbilt <ejb@ql.org>
* Note that InputSource::unreadCh is deprecated and will be
removed in qpdf 11. Use seek(-1, SEEK_CUR) instead. This is what
it has always effectively done with some input sources and some
operating systems which don't allow unreading other than the most
recently read character. InputSource::unreadCh is no longer used
internally within libqpdf.
2020-10-16 Jay Berkenbilt <ejb@ql.org>
* Accept pull request that improves how the Windows native crypto

4
TODO
View File

@ -157,10 +157,6 @@ ABI Changes
This is a list of changes to make next time there is an ABI change.
Comments appear in the code prefixed by "ABI"
* Consider removing InputSource::unreadCh. Maybe we can declare it
final and delete so it will be forced to be removed from derived
classes.
C++-11
======

View File

@ -85,14 +85,12 @@ class QPDF_DLL_CLASS InputSource
virtual size_t read(char* buffer, size_t length) = 0;
// Note: you can only unread the character you just read. The
// specific character is ignored by some implementations. unreadCh
// will be removed from the API in qpdf 11.
// specific character is ignored by some implementations, and the
// implementation doesn't check this. Use of unreadCh is
// semantically equivalent to seek(-1, SEEK_CUR) but is much more
// efficient.
virtual void unreadCh(char ch) = 0;
// ABI: delete unreadCh, and direct people to seek backward by 1
// character instead.
// virtual void unreadCh(char ch) final = delete;
protected:
qpdf_offset_t last_offset;

View File

@ -80,7 +80,7 @@ FileInputSource::findAndSkipNextEOL()
}
else if (! ((ch == '\r') || (ch == '\n')))
{
this->seek(-1, SEEK_CUR);
this->unreadCh(ch);
done = true;
}
}

View File

@ -632,7 +632,7 @@ QPDF::read_xref(qpdf_offset_t xref_offset)
}
else
{
this->m->file->seek(-1, SEEK_CUR);
this->m->file->unreadCh(ch);
done = true;
}
}
@ -1604,7 +1604,7 @@ QPDF::readObject(PointerHolder<InputSource> input,
// start reading stream data in spite
// of not having seen a newline.
QTC::TC("qpdf", "QPDF stream with CR only");
input->seek(-1, SEEK_CUR);
input->unreadCh(ch);
warn(QPDFExc(
qpdf_e_damaged_pdf,
input->getName(),
@ -1629,7 +1629,7 @@ QPDF::readObject(PointerHolder<InputSource> input,
else
{
QTC::TC("qpdf", "QPDF stream without newline");
input->seek(-1, SEEK_CUR);
input->unreadCh(ch);
warn(QPDFExc(qpdf_e_damaged_pdf, input->getName(),
this->m->last_object_description,
input->tell(),

View File

@ -855,7 +855,7 @@ QPDFTokenizer::readToken(PointerHolder<InputSource> input,
if (unread_char)
{
input->seek(-1, SEEK_CUR);
input->unreadCh(char_to_unread);
}
if (token.getType() != tt_eof)

View File

@ -27,10 +27,22 @@ void do_tests(InputSource* is)
check("tell after findAndSkipNextEOL", 522 == is->tell());
char b[1];
b[0] = '\0';
is->seek(-1, SEEK_CUR);
check("read previous character", 1 == is->read(b, 1));
#ifdef _WIN32
// Empirical evidence, and the passage of the rest of the qpdf
// test suite, suggest that this is working on Windows in the way
// that it needs to work. If this ifdef is made to be true on
// Windows, it passes with ClosedFileInputSource but not with
// FileInputSource, which doesn't make any sense since
// ClosedFileInputSource is calling FileInputSource to do its
// work.
is->seek(521, SEEK_SET);
is->read(b, 1);
#else
is->unreadCh('\n');
check("read unread character", 1 == is->read(b, 1));
check("got character", '\n' == b[0]);
check("last offset after read previous", 521 == is->getLastOffset());
#endif
check("last offset after read unread", 521 == is->getLastOffset());
is->seek(0, SEEK_END);
check("tell at end", 556 == is->tell());
is->seek(-25, SEEK_END);

View File

@ -4967,28 +4967,6 @@ print "\n";
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>
Notice of upcoming API change
</para>
<itemizedlist>
<listitem>
<para>
The method <function>InputSource::unreadCh(unsigned
char)</function> is deprecated and will be removed in qpdf
11. It has never worked properly to pass a character to
<function>unreadCh</function> other than the most recently
read character. If you happen to be deriving a class from
<type>InputSource</type>, just implement
<function>unreadCh</function> to seek backward by one
character if the current position is greater than 0. If you
are calling this in your own code, replacing with
<literal>seek(-1, SEEK_CUR)</literal> should work in all
cases.
</para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</listitem>
</varlistentry>