[poppler] glib/poppler-document.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Jan 17 12:41:48 UTC 2022


 glib/poppler-document.cc |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

New commits:
commit 90d1dd7bae03b9af063554f240d0f684912755c0
Author: Nelson Benítez León <nbenitezl at gmail.com>
Date:   Sat Jan 8 13:24:41 2022 -0400

    glib: try with utf8 password if latin1 fails
    
    when opening encrypted files in below functions.
    
    poppler_document_new_from_file():
    poppler_document_new_from_data():
    poppler_document_new_from_bytes():
    poppler_document_new_from_stream():
    poppler_document_new_from_fd():
    
    Poppler-glib converts password to latin1 before opening,
    but as shown in issue #824 there could be files encrypted
    with a UTF8 password, and other pdf viewers handle this
    well, so let's do the same and try with UTF-8 if latin1
    fails. Note: we originally receive the password in UTF-8
    from GTK.
    
    Thanks @aacid for clue about how this was fixed in Okular side.
    
    Fixes issue #824

diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index 6a226919..7c275454 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -214,11 +214,26 @@ PopplerDocument *poppler_document_new_from_file(const char *uri, const char *pas
     length = MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, length);
 
     newDoc = new PDFDoc(filenameW, length, password_g, password_g);
+    if (!newDoc->isOk() && newDoc->getErrorCode() == errEncrypted && password) {
+        /* Try again with original password (which comes from GTK in UTF8) Issue #824 */
+        delete newDoc;
+        delete password_g;
+        password_g = new GooString(password);
+        newDoc = new PDFDoc(filenameW, length, password_g, password_g);
+    }
     delete[] filenameW;
 #else
     GooString *filename_g;
     filename_g = new GooString(filename);
     newDoc = new PDFDoc(filename_g, password_g, password_g);
+    if (!newDoc->isOk() && newDoc->getErrorCode() == errEncrypted && password) {
+        /* Try again with original password (which comes from GTK in UTF8) Issue #824 */
+        filename_g = filename_g->copy();
+        delete newDoc;
+        delete password_g;
+        password_g = new GooString(password);
+        newDoc = new PDFDoc(filename_g, password_g, password_g);
+    }
 #endif
     g_free(filename);
 
@@ -260,6 +275,14 @@ PopplerDocument *poppler_document_new_from_data(char *data, int length, const ch
 
     password_g = poppler_password_to_latin1(password);
     newDoc = new PDFDoc(str, password_g, password_g);
+    if (!newDoc->isOk() && newDoc->getErrorCode() == errEncrypted && password) {
+        /* Try again with original password (which comes from GTK in UTF8) Issue #824 */
+        str = dynamic_cast<MemStream *>(str->copy());
+        delete newDoc;
+        delete password_g;
+        password_g = new GooString(password);
+        newDoc = new PDFDoc(str, password_g, password_g);
+    }
     delete password_g;
 
     return _poppler_document_new_from_pdfdoc(std::move(initer), newDoc, error);
@@ -308,6 +331,14 @@ PopplerDocument *poppler_document_new_from_bytes(GBytes *bytes, const char *pass
 
     password_g = poppler_password_to_latin1(password);
     newDoc = new PDFDoc(str, password_g, password_g);
+    if (!newDoc->isOk() && newDoc->getErrorCode() == errEncrypted && password) {
+        /* Try again with original password (which comes from GTK in UTF8) Issue #824 */
+        str = dynamic_cast<BytesStream *>(str->copy());
+        delete newDoc;
+        delete password_g;
+        password_g = new GooString(password);
+        newDoc = new PDFDoc(str, password_g, password_g);
+    }
     delete password_g;
 
     return _poppler_document_new_from_pdfdoc(std::move(initer), newDoc, error);
@@ -368,6 +399,14 @@ PopplerDocument *poppler_document_new_from_stream(GInputStream *stream, goffset
 
     password_g = poppler_password_to_latin1(password);
     newDoc = new PDFDoc(str, password_g, password_g);
+    if (!newDoc->isOk() && newDoc->getErrorCode() == errEncrypted && password) {
+        /* Try again with original password (which comes from GTK in UTF8) Issue #824 */
+        str = str->copy();
+        delete newDoc;
+        delete password_g;
+        password_g = new GooString(password);
+        newDoc = new PDFDoc(str, password_g, password_g);
+    }
     delete password_g;
 
     return _poppler_document_new_from_pdfdoc(std::move(initer), newDoc, error);
@@ -488,6 +527,14 @@ PopplerDocument *poppler_document_new_from_fd(int fd, const char *password, GErr
 
     password_g = poppler_password_to_latin1(password);
     newDoc = new PDFDoc(stream, password_g, password_g);
+    if (!newDoc->isOk() && newDoc->getErrorCode() == errEncrypted && password) {
+        /* Try again with original password (which comes from GTK in UTF8) Issue #824 */
+        stream = stream->copy();
+        delete newDoc;
+        delete password_g;
+        password_g = new GooString(password);
+        newDoc = new PDFDoc(stream, password_g, password_g);
+    }
     delete password_g;
 
     return _poppler_document_new_from_pdfdoc(std::move(initer), newDoc, error);


More information about the poppler mailing list