poppler/Form.cc             |   14 +++++++-------
 poppler/Form.h              |    8 ++++----
 poppler/PDFDoc.cc           |    6 +++---
 poppler/PDFDoc.h            |    6 +++---
 poppler/SignatureHandler.cc |    8 ++++----
 poppler/SignatureHandler.h  |    4 ++--
 qt5/src/poppler-form.cc     |    5 ++---
 qt6/src/poppler-form.cc     |    7 +++----
 utils/pdfsig.cc             |    3 +--
 9 files changed, 29 insertions(+), 32 deletions(-)

New commits:
commit 8787103a43732440e1994c6c72d99a4717af8b95
Author: Sune Vuorela <s...@vuorela.dk>
Date:   Fri Mar 17 14:08:42 2023 +0100

    Switch some const char* to std string
    
    Also move the weirdness (empty password is nullptr) close to usage,
    rather than have it as far away as possible.

diff --git a/poppler/Form.cc b/poppler/Form.cc
index eadf3979..3d594598 100644
--- a/poppler/Form.cc
+++ b/poppler/Form.cc
@@ -602,11 +602,11 @@ static bool hashFileRange(FILE *f, SignatureHandler 
*handler, Goffset start, Gof
 }
 #endif
 
-bool FormWidgetSignature::signDocument(const char *saveFilename, const char 
*certNickname, const char *password, const GooString *reason, const GooString 
*location, const std::optional<GooString> &ownerPassword,
+bool FormWidgetSignature::signDocument(const std::string &saveFilename, const 
std::string &certNickname, const std::string &password, const GooString 
*reason, const GooString *location, const std::optional<GooString> 
&ownerPassword,
                                        const std::optional<GooString> 
&userPassword)
 {
 #ifdef ENABLE_NSS3
-    if (!certNickname) {
+    if (certNickname.empty()) {
         fprintf(stderr, "signDocument: Empty nickname\n");
         return false;
     }
@@ -636,7 +636,7 @@ bool FormWidgetSignature::signDocument(const char 
*saveFilename, const char *cer
     // Incremental save to avoid breaking any existing signatures
     const GooString fname(saveFilename);
     if (doc->saveAs(fname, writeForceIncremental) != errNone) {
-        fprintf(stderr, "signDocument: error saving to file \"%s\"\n", 
saveFilename);
+        fprintf(stderr, "signDocument: error saving to file \"%s\"\n", 
saveFilename.c_str());
         return false;
     }
 
@@ -648,7 +648,7 @@ bool FormWidgetSignature::signDocument(const char 
*saveFilename, const char *cer
 
     // Update byte range of signature in the saved PDF
     Goffset sigStart, sigEnd, fileSize;
-    FILE *file = openFile(saveFilename, "r+b");
+    FILE *file = openFile(saveFilename.c_str(), "r+b");
     if (!updateOffsets(file, objStart, objEnd, &sigStart, &sigEnd, &fileSize)) 
{
         fprintf(stderr, "signDocument: unable update byte range\n");
         fclose(file);
@@ -689,9 +689,9 @@ bool FormWidgetSignature::signDocument(const char 
*saveFilename, const char *cer
 #endif
 }
 
-bool FormWidgetSignature::signDocumentWithAppearance(const char *saveFilename, 
const char *certNickname, const char *password, const GooString *reason, const 
GooString *location, const std::optional<GooString> &ownerPassword,
-                                                     const 
std::optional<GooString> &userPassword, const GooString &signatureText, const 
GooString &signatureTextLeft, double fontSize, double leftFontSize,
-                                                     
std::unique_ptr<AnnotColor> &&fontColor, double borderWidth, 
std::unique_ptr<AnnotColor> &&borderColor, std::unique_ptr<AnnotColor> 
&&backgroundColor)
+bool FormWidgetSignature::signDocumentWithAppearance(const std::string 
&saveFilename, const std::string &certNickname, const std::string &password, 
const GooString *reason, const GooString *location,
+                                                     const 
std::optional<GooString> &ownerPassword, const std::optional<GooString> 
&userPassword, const GooString &signatureText, const GooString 
&signatureTextLeft, double fontSize,
+                                                     double leftFontSize, 
std::unique_ptr<AnnotColor> &&fontColor, double borderWidth, 
std::unique_ptr<AnnotColor> &&borderColor, std::unique_ptr<AnnotColor> 
&&backgroundColor)
 {
     // Set the appearance
     GooString *aux = getField()->getDefaultAppearance();
diff --git a/poppler/Form.h b/poppler/Form.h
index 3ac83d7f..2d0f0431 100644
--- a/poppler/Form.h
+++ b/poppler/Form.h
@@ -307,13 +307,13 @@ public:
     // field "ByteRange" in the dictionary "V".
     // Arguments reason and location are UTF-16 big endian strings with BOM. 
An empty string and nullptr are acceptable too.
     // Returns success.
-    bool signDocument(const char *filename, const char *certNickname, const 
char *password, const GooString *reason = nullptr, const GooString *location = 
nullptr, const std::optional<GooString> &ownerPassword = {},
+    bool signDocument(const std::string &filename, const std::string 
&certNickname, const std::string &password, const GooString *reason = nullptr, 
const GooString *location = nullptr, const std::optional<GooString> 
&ownerPassword = {},
                       const std::optional<GooString> &userPassword = {});
 
     // Same as above but adds text, font color, etc.
-    bool signDocumentWithAppearance(const char *filename, const char 
*certNickname, const char *password, const GooString *reason = nullptr, const 
GooString *location = nullptr, const std::optional<GooString> &ownerPassword = 
{},
-                                    const std::optional<GooString> 
&userPassword = {}, const GooString &signatureText = {}, const GooString 
&signatureTextLeft = {}, double fontSize = {}, double leftFontSize = {},
-                                    std::unique_ptr<AnnotColor> &&fontColor = 
{}, double borderWidth = {}, std::unique_ptr<AnnotColor> &&borderColor = {}, 
std::unique_ptr<AnnotColor> &&backgroundColor = {});
+    bool signDocumentWithAppearance(const std::string &filename, const 
std::string &certNickname, const std::string &password, const GooString *reason 
= nullptr, const GooString *location = nullptr,
+                                    const std::optional<GooString> 
&ownerPassword = {}, const std::optional<GooString> &userPassword = {}, const 
GooString &signatureText = {}, const GooString &signatureTextLeft = {}, double 
fontSize = {},
+                                    double leftFontSize = {}, 
std::unique_ptr<AnnotColor> &&fontColor = {}, double borderWidth = {}, 
std::unique_ptr<AnnotColor> &&borderColor = {}, std::unique_ptr<AnnotColor> 
&&backgroundColor = {});
 
     // checks the length encoding of the signature and returns the hex encoded 
signature
     // if the check passed (and the checked file size as output parameter in 
checkedFileSize)
diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index 7ed4311c..cec0fab3 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -2171,9 +2171,9 @@ static std::string findPdfFontNameToUseForSigning(Form 
*form)
     return {};
 }
 
-bool PDFDoc::sign(const char *saveFilename, const char *certNickname, const 
char *password, GooString *partialFieldName, int page, const PDFRectangle 
&rect, const GooString &signatureText, const GooString &signatureTextLeft,
-                  double fontSize, double leftFontSize, 
std::unique_ptr<AnnotColor> &&fontColor, double borderWidth, 
std::unique_ptr<AnnotColor> &&borderColor, std::unique_ptr<AnnotColor> 
&&backgroundColor, const GooString *reason,
-                  const GooString *location, const std::string &imagePath, 
const std::optional<GooString> &ownerPassword, const std::optional<GooString> 
&userPassword)
+bool PDFDoc::sign(const std::string &saveFilename, const std::string 
&certNickname, const std::string &password, GooString *partialFieldName, int 
page, const PDFRectangle &rect, const GooString &signatureText,
+                  const GooString &signatureTextLeft, double fontSize, double 
leftFontSize, std::unique_ptr<AnnotColor> &&fontColor, double borderWidth, 
std::unique_ptr<AnnotColor> &&borderColor,
+                  std::unique_ptr<AnnotColor> &&backgroundColor, const 
GooString *reason, const GooString *location, const std::string &imagePath, 
const std::optional<GooString> &ownerPassword, const std::optional<GooString> 
&userPassword)
 {
     ::Page *destPage = getPage(page);
     if (destPage == nullptr) {
diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h
index 01592e0c..a57ba789 100644
--- a/poppler/PDFDoc.h
+++ b/poppler/PDFDoc.h
@@ -491,9 +491,9 @@ public:
     // Arguments reason and location are UTF-16 big endian strings with BOM. 
An empty string and nullptr are acceptable too.
     // Argument imagePath is a background image (a path to a file).
     // sign() takes ownership of partialFieldName.
-    bool sign(const char *saveFilename, const char *certNickname, const char 
*password, GooString *partialFieldName, int page, const PDFRectangle &rect, 
const GooString &signatureText, const GooString &signatureTextLeft, double 
fontSize,
-              double leftFontSize, std::unique_ptr<AnnotColor> &&fontColor, 
double borderWidth, std::unique_ptr<AnnotColor> &&borderColor, 
std::unique_ptr<AnnotColor> &&backgroundColor, const GooString *reason = 
nullptr,
-              const GooString *location = nullptr, const std::string 
&imagePath = "", const std::optional<GooString> &ownerPassword = {}, const 
std::optional<GooString> &userPassword = {});
+    bool sign(const std::string &saveFilename, const std::string 
&certNickname, const std::string &password, GooString *partialFieldName, int 
page, const PDFRectangle &rect, const GooString &signatureText,
+              const GooString &signatureTextLeft, double fontSize, double 
leftFontSize, std::unique_ptr<AnnotColor> &&fontColor, double borderWidth, 
std::unique_ptr<AnnotColor> &&borderColor, std::unique_ptr<AnnotColor> 
&&backgroundColor,
+              const GooString *reason = nullptr, const GooString *location = 
nullptr, const std::string &imagePath = "", const std::optional<GooString> 
&ownerPassword = {}, const std::optional<GooString> &userPassword = {});
 
 private:
     // insert referenced objects in XRef
diff --git a/poppler/SignatureHandler.cc b/poppler/SignatureHandler.cc
index e28853e5..cf6ee95d 100644
--- a/poppler/SignatureHandler.cc
+++ b/poppler/SignatureHandler.cc
@@ -812,12 +812,12 @@ SignatureHandler::SignatureHandler(unsigned char *p7, int 
p7_length) : hash_cont
     }
 }
 
-SignatureHandler::SignatureHandler(const char *certNickname, HashAlgorithm 
digestAlgTag)
+SignatureHandler::SignatureHandler(const std::string &certNickname, 
HashAlgorithm digestAlgTag)
     : hash_length(digestLength(digestAlgTag)), digest_alg_tag(digestAlgTag), 
CMSitem(), hash_context(nullptr), CMSMessage(nullptr), CMSSignedData(nullptr), 
CMSSignerInfo(nullptr), signing_cert(nullptr)
 {
     setNSSDir({});
     CMSMessage = NSS_CMSMessage_Create(nullptr);
-    signing_cert = CERT_FindCertByNickname(CERT_GetDefaultCertDB(), 
certNickname);
+    signing_cert = CERT_FindCertByNickname(CERT_GetDefaultCertDB(), 
certNickname.c_str());
     
hash_context.reset(HASH_Create(HASH_GetHashTypeByOidTag(ConvertHashAlgorithmToNss(digestAlgTag))));
 }
 
@@ -1050,7 +1050,7 @@ CertificateValidationStatus 
SignatureHandler::validateCertificate(time_t validat
     return CERTIFICATE_GENERIC_ERROR;
 }
 
-std::unique_ptr<GooString> SignatureHandler::signDetached(const char 
*password) const
+std::unique_ptr<GooString> SignatureHandler::signDetached(const std::string 
&password) const
 {
     if (!hash_context) {
         return nullptr;
@@ -1198,7 +1198,7 @@ std::unique_ptr<GooString> 
SignatureHandler::signDetached(const char *password)
     cms_output.data = nullptr;
     cms_output.len = 0;
 
-    NSSCMSEncoderContext *cms_ecx = NSS_CMSEncoder_Start(cms_msg.get(), 
nullptr, nullptr, &cms_output, arena.get(), passwordCallback, const_cast<char 
*>(password), nullptr, nullptr, nullptr, nullptr);
+    NSSCMSEncoderContext *cms_ecx = NSS_CMSEncoder_Start(cms_msg.get(), 
nullptr, nullptr, &cms_output, arena.get(), passwordCallback, password.empty() 
? nullptr : const_cast<char *>(password.c_str()), nullptr, nullptr, nullptr, 
nullptr);
     if (!cms_ecx) {
         return nullptr;
     }
diff --git a/poppler/SignatureHandler.h b/poppler/SignatureHandler.h
index 9cf701f2..5a7399d1 100644
--- a/poppler/SignatureHandler.h
+++ b/poppler/SignatureHandler.h
@@ -47,7 +47,7 @@ class POPPLER_PRIVATE_EXPORT SignatureHandler
 {
 public:
     SignatureHandler(unsigned char *p7, int p7_length);
-    SignatureHandler(const char *certNickname, HashAlgorithm digestAlgTag);
+    SignatureHandler(const std::string &certNickName, HashAlgorithm 
digestAlgTag);
     ~SignatureHandler();
     time_t getSigningTime() const;
     std::string getSignerName() const;
@@ -60,7 +60,7 @@ public:
     CertificateValidationStatus validateCertificate(time_t validation_time, 
bool ocspRevocationCheck, bool useAIACertFetch);
     std::unique_ptr<X509CertificateInfo> getCertificateInfo() const;
     static std::vector<std::unique_ptr<X509CertificateInfo>> 
getAvailableSigningCertificates();
-    std::unique_ptr<GooString> signDetached(const char *password) const;
+    std::unique_ptr<GooString> signDetached(const std::string &password) const;
 
     // Initializes the NSS dir with the custom given directory
     // calling it with an empty string means use the default firefox db, 
/etc/pki/nssdb, ~/.pki/nssdb
diff --git a/qt5/src/poppler-form.cc b/qt5/src/poppler-form.cc
index 011eb3ea..b56e73d0 100644
--- a/qt5/src/poppler-form.cc
+++ b/qt5/src/poppler-form.cc
@@ -1114,9 +1114,8 @@ FormFieldSignature::SigningResult 
FormFieldSignature::sign(const QString &output
     const auto gSignatureText = 
std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureText()));
     const auto gSignatureLeftText = 
std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureLeftText()));
 
-    const bool success =
-            
fws->signDocumentWithAppearance(outputFileName.toUtf8().constData(), 
data.certNickname().toUtf8().constData(), data.password().toUtf8().constData(), 
reason.get(), location.get(), ownerPwd, userPwd, *gSignatureText,
-                                            *gSignatureLeftText, 
data.fontSize(), data.leftFontSize(), convertQColor(data.fontColor()), 
data.borderWidth(), convertQColor(data.borderColor()), 
convertQColor(data.backgroundColor()));
+    const bool success = 
fws->signDocumentWithAppearance(outputFileName.toStdString(), 
data.certNickname().toStdString(), data.password().toStdString(), reason.get(), 
location.get(), ownerPwd, userPwd, *gSignatureText, *gSignatureLeftText,
+                                                         data.fontSize(), 
data.leftFontSize(), convertQColor(data.fontColor()), data.borderWidth(), 
convertQColor(data.borderColor()), convertQColor(data.backgroundColor()));
 
     return success ? SigningSuccess : GenericSigningError;
 }
diff --git a/qt6/src/poppler-form.cc b/qt6/src/poppler-form.cc
index 86671971..05d183cb 100644
--- a/qt6/src/poppler-form.cc
+++ b/qt6/src/poppler-form.cc
@@ -781,7 +781,7 @@ bool CertificateInfo::checkPassword(const QString 
&password) const
 {
 #ifdef ENABLE_NSS3
     Q_D(const CertificateInfo);
-    SignatureHandler sigHandler(d->nick_name.toUtf8().constData(), 
HashAlgorithm::Sha256);
+    SignatureHandler sigHandler(d->nick_name.toStdString(), 
HashAlgorithm::Sha256);
     unsigned char buffer[5];
     memcpy(buffer, "test", 5);
     sigHandler.updateHash(buffer, 5);
@@ -1114,9 +1114,8 @@ FormFieldSignature::SigningResult 
FormFieldSignature::sign(const QString &output
     const auto gSignatureText = 
std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureText()));
     const auto gSignatureLeftText = 
std::unique_ptr<GooString>(QStringToUnicodeGooString(data.signatureLeftText()));
 
-    const bool success =
-            
fws->signDocumentWithAppearance(outputFileName.toUtf8().constData(), 
data.certNickname().toUtf8().constData(), data.password().toUtf8().constData(), 
reason.get(), location.get(), ownerPwd, userPwd, *gSignatureText,
-                                            *gSignatureLeftText, 
data.fontSize(), data.leftFontSize(), convertQColor(data.fontColor()), 
data.borderWidth(), convertQColor(data.borderColor()), 
convertQColor(data.backgroundColor()));
+    const bool success = 
fws->signDocumentWithAppearance(outputFileName.toStdString(), 
data.certNickname().toStdString(), data.password().toStdString(), reason.get(), 
location.get(), ownerPwd, userPwd, *gSignatureText, *gSignatureLeftText,
+                                                         data.fontSize(), 
data.leftFontSize(), convertQColor(data.fontColor()), data.borderWidth(), 
convertQColor(data.borderColor()), convertQColor(data.backgroundColor()));
 
     return success ? SigningSuccess : GenericSigningError;
 }
diff --git a/utils/pdfsig.cc b/utils/pdfsig.cc
index ee6d9881..c090f1dc 100644
--- a/utils/pdfsig.cc
+++ b/utils/pdfsig.cc
@@ -355,7 +355,6 @@ int main(int argc, char *argv[])
             return 2;
         }
 
-        const char *pw = (strlen(password) == 0) ? nullptr : password;
         const auto rs = std::unique_ptr<GooString>(reason.toStr().empty() ? 
nullptr : utf8ToUtf16WithBom(reason.toStr()));
 
         if (newSignatureFieldName.getLength() == 0) {
@@ -371,7 +370,7 @@ int main(int argc, char *argv[])
         }
 
         // We don't provide a way to customize the UI from pdfsig for now
-        const bool success = doc->sign(argv[2], certNickname, pw, 
newSignatureFieldName.copy(), /*page*/ 1,
+        const bool success = doc->sign(std::string { argv[2] }, std::string { 
certNickname }, std::string { password }, newSignatureFieldName.copy(), 
/*page*/ 1,
                                        /*rect */ { 0, 0, 0, 0 }, 
/*signatureText*/ {}, /*signatureTextLeft*/ {}, /*fontSize */ 0, 
/*leftFontSize*/ 0,
                                        /*fontColor*/ {}, /*borderWidth*/ 0, 
/*borderColor*/ {}, /*backgroundColor*/ {}, rs.get(), /* location */ nullptr, 
/* image path */ "", ownerPW, userPW);
         return success ? 0 : 3;

Reply via email to