Refactor QPDFWriter::writePad

This commit is contained in:
m-holger 2023-01-08 16:08:27 +00:00 committed by Jay Berkenbilt
parent 8363657cf3
commit aefb8ff9ef
2 changed files with 11 additions and 20 deletions

View File

@ -557,7 +557,7 @@ class QPDFWriter
void writeBuffer(std::shared_ptr<Buffer>&); void writeBuffer(std::shared_ptr<Buffer>&);
void writeStringQDF(std::string const& str); void writeStringQDF(std::string const& str);
void writeStringNoQDF(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 assignCompressedObjectNumbers(QPDFObjGen const& og);
void enqueueObject(QPDFObjectHandle object); void enqueueObject(QPDFObjectHandle object);
void writeObjectStreamOffsets( void writeObjectStreamOffsets(
@ -676,7 +676,7 @@ class QPDFWriter
qpdf_offset_t hint_length, qpdf_offset_t hint_length,
bool skip_compression, bool skip_compression,
int linearization_pass); 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 // When filtering subsections, push additional pipelines to the
// stack. When ready to switch, activate the pipeline stack. When // stack. When ready to switch, activate the pipeline stack. When

View File

@ -1010,11 +1010,9 @@ QPDFWriter::writeStringNoQDF(std::string const& str)
} }
void void
QPDFWriter::writePad(int nspaces) QPDFWriter::writePad(size_t nspaces)
{ {
for (int i = 0; i < nspaces; ++i) { writeString(std::string(nspaces, ' '));
writeString(" ");
}
} }
Pipeline* Pipeline*
@ -1321,13 +1319,8 @@ QPDFWriter::writeTrailer(
writeString(" /Prev "); writeString(" /Prev ");
qpdf_offset_t pos = this->m->pipeline->getCount(); qpdf_offset_t pos = this->m->pipeline->getCount();
writeString(std::to_string(prev)); writeString(std::to_string(prev));
int nspaces = writePad(QIntC::to_size(
QIntC::to_int(pos - this->m->pipeline->getCount() + 21); pos - this->m->pipeline->getCount() + 21));
if (nspaces < 0) {
throw std::logic_error(
"QPDFWriter: no padding required in trailer");
}
writePad(nspaces);
} }
} else { } else {
unparseChild(trailer.getKey(key), 1, 0); unparseChild(trailer.getKey(key), 1, 0);
@ -2783,7 +2776,7 @@ QPDFWriter::writeXRefStream(
return space_before_zero; return space_before_zero;
} }
int size_t
QPDFWriter::calculateXrefStreamPadding(qpdf_offset_t xref_bytes) QPDFWriter::calculateXrefStreamPadding(qpdf_offset_t xref_bytes)
{ {
// This routine is called right after a linearization first pass // 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 // input by 6 bytes plus 5 bytes per 16K, and then we'll add 10
// extra bytes for number length increases. // 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 void
@ -3029,9 +3022,7 @@ QPDFWriter::writeLinearized()
writeString(" >>"); writeString(" >>");
closeObject(lindict_id); closeObject(lindict_id);
static int const pad = 200; static int const pad = 200;
int spaces = QIntC::to_int(pos - this->m->pipeline->getCount() + pad); writePad(QIntC::to_size(pos - this->m->pipeline->getCount() + pad));
qpdf_assert_debug(spaces >= 0);
writePad(spaces);
writeString("\n"); writeString("\n");
// If the user supplied any additional header text, write it // If the user supplied any additional header text, write it
@ -3082,7 +3073,7 @@ QPDFWriter::writeLinearized()
} else { } else {
// Pad so that the next object starts at the same // Pad so that the next object starts at the same
// place as in pass 1. // 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) { if (this->m->pipeline->getCount() != first_xref_end) {
throw std::logic_error( throw std::logic_error(
@ -3164,7 +3155,7 @@ QPDFWriter::writeLinearized()
second_xref_end = this->m->pipeline->getCount(); second_xref_end = this->m->pipeline->getCount();
} else { } else {
// Make the file size the same. // Make the file size the same.
writePad(QIntC::to_int( writePad(QIntC::to_size(
second_xref_end + hint_length - 1 - second_xref_end + hint_length - 1 -
this->m->pipeline->getCount())); this->m->pipeline->getCount()));
writeString("\n"); writeString("\n");