Commit 8ded7ff5f395eff6d265f4339438b58d964d2389

Authored by m-holger
1 parent 32d14f31

Refactor `QPDF` type conversion helpers: relocate methods to `qpdf::Doc::Common`…

…, replace direct calls with `QIntC` equivalents, and clean up `QPDF.hh`.
examples/pdf-custom-filter.cc
  1 +
  2 +#include <qpdf/QIntC.hh>
1 3 #include <qpdf/QPDF.hh>
2 4 #include <qpdf/QPDFStreamFilter.hh>
3 5 #include <qpdf/QPDFWriter.hh>
... ...
include/qpdf/QPDF.hh
... ... @@ -37,7 +37,6 @@
37 37 #include <qpdf/Buffer.hh>
38 38 #include <qpdf/InputSource.hh>
39 39 #include <qpdf/PDFVersion.hh>
40   -#include <qpdf/QIntC.hh>
41 40 #include <qpdf/QPDFExc.hh>
42 41 #include <qpdf/QPDFObjGen.hh>
43 42 #include <qpdf/QPDFObjectHandle.hh>
... ... @@ -803,32 +802,6 @@ class QPDF
803 802 // JSON import
804 803 void importJSON(std::shared_ptr<InputSource>, bool must_be_complete);
805 804  
806   - // Type conversion helper methods
807   - template <typename T>
808   - static qpdf_offset_t
809   - toO(T const& i)
810   - {
811   - return QIntC::to_offset(i);
812   - }
813   - template <typename T>
814   - static size_t
815   - toS(T const& i)
816   - {
817   - return QIntC::to_size(i);
818   - }
819   - template <typename T>
820   - static int
821   - toI(T const& i)
822   - {
823   - return QIntC::to_int(i);
824   - }
825   - template <typename T>
826   - static unsigned long long
827   - toULL(T const& i)
828   - {
829   - return QIntC::to_ulonglong(i);
830   - }
831   -
832 805 class Members;
833 806  
834 807 // Keep all member variables inside the Members object, which we dynamically allocate. This
... ...
libqpdf/QPDF.cc
... ... @@ -761,7 +761,10 @@ QPDF::pipeStreamData(
761 761 auto buf = file->read(length, offset);
762 762 if (buf.size() != length) {
763 763 throw qpdf_for_warning.m->c.damagedPDF(
764   - *file, "", offset + toO(buf.size()), "unexpected EOF reading stream data");
  764 + *file,
  765 + "",
  766 + offset + QIntC::to_offset(buf.size()),
  767 + "unexpected EOF reading stream data");
765 768 }
766 769 pipeline->write(buf.data(), length);
767 770 attempted_finish = true;
... ...
libqpdf/QPDF_encryption.cc
... ... @@ -1010,7 +1010,7 @@ QPDF::decryptString(std::string&amp; str, QPDFObjGen og)
1010 1010 // Using std::shared_ptr guarantees that tmp will be freed even if rc4.process throws an
1011 1011 // exception.
1012 1012 auto tmp = QUtil::make_unique_cstr(str);
1013   - RC4 rc4(QUtil::unsigned_char_pointer(key), toI(key.length()));
  1013 + RC4 rc4(QUtil::unsigned_char_pointer(key), QIntC::to_int(key.length()));
1014 1014 auto data = QUtil::unsigned_char_pointer(tmp.get());
1015 1015 rc4.process(data, vlen, data);
1016 1016 str = std::string(tmp.get(), vlen);
... ...
libqpdf/QPDF_objects.cc
... ... @@ -1138,7 +1138,7 @@ QPDF::getObjectCount()
1138 1138 if (!m->obj_cache.empty()) {
1139 1139 og = (*(m->obj_cache.rbegin())).first;
1140 1140 }
1141   - return toS(og.getObj());
  1141 + return QIntC::to_size(og.getObj());
1142 1142 }
1143 1143  
1144 1144 std::vector<QPDFObjectHandle>
... ...
libqpdf/qpdf/QPDF_private.hh
... ... @@ -3,6 +3,7 @@
3 3  
4 4 #include <qpdf/QPDF.hh>
5 5  
  6 +#include <qpdf/QIntC.hh>
6 7 #include <qpdf/QPDFAcroFormDocumentHelper.hh>
7 8 #include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
8 9 #include <qpdf/QPDFLogger.hh>
... ... @@ -323,13 +324,39 @@ class QPDF::Doc
323 324 }
324 325  
325 326 protected:
  327 + // Type conversion helper methods
  328 + template <typename T>
  329 + static qpdf_offset_t
  330 + toO(T const& i)
  331 + {
  332 + return QIntC::to_offset(i);
  333 + }
  334 + template <typename T>
  335 + static size_t
  336 + toS(T const& i)
  337 + {
  338 + return QIntC::to_size(i);
  339 + }
  340 + template <typename T>
  341 + static int
  342 + toI(T const& i)
  343 + {
  344 + return QIntC::to_int(i);
  345 + }
  346 + template <typename T>
  347 + static unsigned long long
  348 + toULL(T const& i)
  349 + {
  350 + return QIntC::to_ulonglong(i);
  351 + }
  352 +
326 353 QPDF& qpdf;
327 354 QPDF::Members* m;
328 355  
329 356 qpdf::Doc::Config& cf;
330 357 QPDF::Doc::Pages& pages;
331 358 QPDF::Doc::Objects& objects;
332   - };
  359 + }; // class qpdf::Doc::Common
333 360  
334 361 Doc() = delete;
335 362 Doc(Doc const&) = delete;
... ...