diff options
author | Oliver Sander <oliver.sander@tu-dresden.de> | 2023-10-15 20:54:16 +0200 |
---|---|---|
committer | Oliver Sander <oliver.sander@tu-dresden.de> | 2023-10-16 21:07:52 +0200 |
commit | 28e70bc0fafe1499b107d5965f1e8c7a59b4c4a5 (patch) | |
tree | 50ab7a48989f1a30eabd2f66b7d499d34a17f66f | |
parent | c89b933f3bd235c6f95b420fdcfe5ca7df900c09 (diff) |
Move the method sanitizedName from GooString to PDFDoc
Because it is only used by the PDFDoc class, and it does not seem
to be generic enough for a GooString method.
Also, this patch modifies the method to return the string as a
std::string object by value, rather than as a pointer to a
heap-allocated GooString object. This saves one heap allocation.
-rw-r--r-- | goo/GooString.cc | 17 | ||||
-rw-r--r-- | goo/GooString.h | 5 | ||||
-rw-r--r-- | poppler/PDFDoc.cc | 25 | ||||
-rw-r--r-- | poppler/PDFDoc.h | 5 |
4 files changed, 24 insertions, 28 deletions
diff --git a/goo/GooString.cc b/goo/GooString.cc index 56b79c50..7d20dbf8 100644 --- a/goo/GooString.cc +++ b/goo/GooString.cc @@ -635,20 +635,3 @@ bool GooString::endsWith(const char *suffix) const { return endsWith(toStr(), suffix); } - -GooString *GooString::sanitizedName() const -{ - auto *name = new GooString(); - - for (const auto c : *this) { - if (c <= (char)0x20 || c >= (char)0x7f || c == ' ' || c == '(' || c == ')' || c == '<' || c == '>' || c == '[' || c == ']' || c == '{' || c == '}' || c == '/' || c == '%' || c == '#') { - char buf[8]; - sprintf(buf, "#%02x", c & 0xff); - name->append(buf); - } else { - name->append(c); - } - } - - return name; -} diff --git a/goo/GooString.h b/goo/GooString.h index 5af12d0c..14fcdc1a 100644 --- a/goo/GooString.h +++ b/goo/GooString.h @@ -256,11 +256,6 @@ public: bool hasJustUnicodeMarker() const { return size() == 2 && hasUnicodeMarker(); } POPPLER_PRIVATE_EXPORT void prependUnicodeMarker(); - - // Sanitizes the string so that it does - // not contain any ( ) < > [ ] { } / % - // The caller owns the return value - POPPLER_PRIVATE_EXPORT GooString *sanitizedName() const; }; #endif diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index c55a2035..e4b8dd09 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -1205,6 +1205,23 @@ void PDFDoc::saveCompleteRewrite(OutStream *outStr) delete uxref; } +std::string PDFDoc::sanitizedName(const std::string &name) +{ + std::string sanitizedName; + + for (const auto c : name) { + if (c <= (char)0x20 || c >= (char)0x7f || c == ' ' || c == '(' || c == ')' || c == '<' || c == '>' || c == '[' || c == ']' || c == '{' || c == '}' || c == '/' || c == '%' || c == '#') { + char buf[8]; + sprintf(buf, "#%02x", c & 0xff); + sanitizedName.append(buf); + } else { + sanitizedName.push_back(c); + } + } + + return sanitizedName; +} + void PDFDoc::writeDictionary(Dict *dict, OutStream *outStr, XRef *xRef, unsigned int numOffset, unsigned char *fileKey, CryptAlgorithm encAlgorithm, int keyLength, Ref ref, std::set<Dict *> *alreadyWrittenDicts) { bool deleteSet = false; @@ -1226,9 +1243,7 @@ void PDFDoc::writeDictionary(Dict *dict, OutStream *outStr, XRef *xRef, unsigned outStr->printf("<<"); for (int i = 0; i < dict->getLength(); i++) { GooString keyName(dict->getKey(i)); - GooString *keyNameToPrint = keyName.sanitizedName(); - outStr->printf("/%s ", keyNameToPrint->c_str()); - delete keyNameToPrint; + outStr->printf("/%s ", sanitizedName(keyName.toStr()).c_str()); Object obj1 = dict->getValNF(i).copy(); writeObject(&obj1, outStr, xRef, numOffset, fileKey, encAlgorithm, keyLength, ref, alreadyWrittenDicts); } @@ -1376,9 +1391,7 @@ void PDFDoc::writeObject(Object *obj, OutStream *outStr, XRef *xRef, unsigned in } case objName: { GooString name(obj->getName()); - GooString *nameToPrint = name.sanitizedName(); - outStr->printf("/%s ", nameToPrint->c_str()); - delete nameToPrint; + outStr->printf("/%s ", sanitizedName(name.toStr()).c_str()); break; } case objNull: diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index c4ab6705..9a732be3 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -347,6 +347,11 @@ private: // insert referenced objects in XRef bool markDictionary(Dict *dict, XRef *xRef, XRef *countRef, unsigned int numOffset, int oldRefNum, int newRefNum, std::set<Dict *> *alreadyMarkedDicts); bool markObject(Object *obj, XRef *xRef, XRef *countRef, unsigned int numOffset, int oldRefNum, int newRefNum, std::set<Dict *> *alreadyMarkedDicts = nullptr); + + // Sanitizes the string so that it does + // not contain any ( ) < > [ ] { } / % + static std::string sanitizedName(const std::string &name); + static void writeDictionary(Dict *dict, OutStream *outStr, XRef *xRef, unsigned int numOffset, unsigned char *fileKey, CryptAlgorithm encAlgorithm, int keyLength, Ref ref, std::set<Dict *> *alreadyWrittenDicts); // Write object header to current file stream and return its offset |