From aefb8ff9efe06b6a596fac8140daea605ff149ff Mon Sep 17 00:00:00 2001 From: m-holger Date: Sun, 8 Jan 2023 16:08:27 +0000 Subject: [PATCH] Refactor QPDFWriter::writePad --- include/qpdf/QPDFWriter.hh | 4 ++-- libqpdf/QPDFWriter.cc | 27 +++++++++------------------ 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/include/qpdf/QPDFWriter.hh b/include/qpdf/QPDFWriter.hh index 56771e18..af6c15be 100644 --- a/include/qpdf/QPDFWriter.hh +++ b/include/qpdf/QPDFWriter.hh @@ -557,7 +557,7 @@ class QPDFWriter void writeBuffer(std::shared_ptr&); void writeStringQDF(std::string const& str); void writeStringNoQDF(std::string const& str); - void writePad(int nspaces); + void writePad(size_t nspaces); void assignCompressedObjectNumbers(QPDFObjGen const& og); void enqueueObject(QPDFObjectHandle object); void writeObjectStreamOffsets( @@ -676,7 +676,7 @@ class QPDFWriter qpdf_offset_t hint_length, bool skip_compression, int linearization_pass); - int calculateXrefStreamPadding(qpdf_offset_t xref_bytes); + size_t calculateXrefStreamPadding(qpdf_offset_t xref_bytes); // When filtering subsections, push additional pipelines to the // stack. When ready to switch, activate the pipeline stack. When diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 383a6886..3d7de821 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -1010,11 +1010,9 @@ QPDFWriter::writeStringNoQDF(std::string const& str) } void -QPDFWriter::writePad(int nspaces) +QPDFWriter::writePad(size_t nspaces) { - for (int i = 0; i < nspaces; ++i) { - writeString(" "); - } + writeString(std::string(nspaces, ' ')); } Pipeline* @@ -1321,13 +1319,8 @@ QPDFWriter::writeTrailer( writeString(" /Prev "); qpdf_offset_t pos = this->m->pipeline->getCount(); writeString(std::to_string(prev)); - int nspaces = - QIntC::to_int(pos - this->m->pipeline->getCount() + 21); - if (nspaces < 0) { - throw std::logic_error( - "QPDFWriter: no padding required in trailer"); - } - writePad(nspaces); + writePad(QIntC::to_size( + pos - this->m->pipeline->getCount() + 21)); } } else { unparseChild(trailer.getKey(key), 1, 0); @@ -2783,7 +2776,7 @@ QPDFWriter::writeXRefStream( return space_before_zero; } -int +size_t QPDFWriter::calculateXrefStreamPadding(qpdf_offset_t xref_bytes) { // This routine is called right after a linearization first pass @@ -2794,7 +2787,7 @@ QPDFWriter::calculateXrefStreamPadding(qpdf_offset_t xref_bytes) // input by 6 bytes plus 5 bytes per 16K, and then we'll add 10 // extra bytes for number length increases. - return QIntC::to_int(16 + (5 * ((xref_bytes + 16383) / 16384))); + return QIntC::to_size(16 + (5 * ((xref_bytes + 16383) / 16384))); } void @@ -3029,9 +3022,7 @@ QPDFWriter::writeLinearized() writeString(" >>"); closeObject(lindict_id); static int const pad = 200; - int spaces = QIntC::to_int(pos - this->m->pipeline->getCount() + pad); - qpdf_assert_debug(spaces >= 0); - writePad(spaces); + writePad(QIntC::to_size(pos - this->m->pipeline->getCount() + pad)); writeString("\n"); // If the user supplied any additional header text, write it @@ -3082,7 +3073,7 @@ QPDFWriter::writeLinearized() } else { // Pad so that the next object starts at the same // place as in pass 1. - writePad(QIntC::to_int(first_xref_end - endpos)); + writePad(QIntC::to_size(first_xref_end - endpos)); if (this->m->pipeline->getCount() != first_xref_end) { throw std::logic_error( @@ -3164,7 +3155,7 @@ QPDFWriter::writeLinearized() second_xref_end = this->m->pipeline->getCount(); } else { // Make the file size the same. - writePad(QIntC::to_int( + writePad(QIntC::to_size( second_xref_end + hint_length - 1 - this->m->pipeline->getCount())); writeString("\n");