diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2011-04-08 14:21:38 +0200 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2011-04-08 14:21:38 +0200 |
commit | 5526e501b4447d6cbc6ed868a3c182dba22c1f5d (patch) | |
tree | ec3a18e6d0f94b06363e04ef7eed9585cd092e4e | |
parent | 94e774a8bd2891f36885ae710efd42caebd855ed (diff) | |
download | mupdf-5526e501b4447d6cbc6ed868a3c182dba22c1f5d.tar.xz |
pdf: add pdf_from_ucs2 to encode a unicode string in pdfdocencoding.
For use by SumatraPDF to check passwords.
-rw-r--r-- | pdf/mupdf.h | 1 | ||||
-rw-r--r-- | pdf/pdf_parse.c | 47 |
2 files changed, 46 insertions, 2 deletions
diff --git a/pdf/mupdf.h b/pdf/mupdf.h index 1c08345a..80942a80 100644 --- a/pdf/mupdf.h +++ b/pdf/mupdf.h @@ -39,6 +39,7 @@ fz_matrix pdf_to_matrix(fz_obj *array); char *pdf_to_utf8(fz_obj *src); unsigned short *pdf_to_ucs2(fz_obj *src); fz_obj *pdf_to_utf8_name(fz_obj *src); +char *pdf_from_ucs2(unsigned short *str); /* * Encryption diff --git a/pdf/pdf_parse.c b/pdf/pdf_parse.c index 9f9d33b1..31a2bd0c 100644 --- a/pdf/pdf_parse.c +++ b/pdf/pdf_parse.c @@ -1,7 +1,8 @@ #include "fitz.h" #include "mupdf.h" -fz_rect pdf_to_rect(fz_obj *array) +fz_rect +pdf_to_rect(fz_obj *array) { fz_rect r; float a = fz_to_real(fz_array_get(array, 0)); @@ -15,7 +16,8 @@ fz_rect pdf_to_rect(fz_obj *array) return r; } -fz_matrix pdf_to_matrix(fz_obj *array) +fz_matrix +pdf_to_matrix(fz_obj *array) { fz_matrix m; m.a = fz_to_real(fz_array_get(array, 0)); @@ -27,6 +29,7 @@ fz_matrix pdf_to_matrix(fz_obj *array) return m; } +/* Convert Unicode/PdfDocEncoding string into utf-8 */ char * pdf_to_utf8(fz_obj *src) { @@ -72,6 +75,7 @@ pdf_to_utf8(fz_obj *src) return dst; } +/* Convert Unicode/PdfDocEncoding string into ucs-2 */ unsigned short * pdf_to_ucs2(fz_obj *src) { @@ -98,6 +102,45 @@ pdf_to_ucs2(fz_obj *src) return dst; } +/* Convert UCS-2 string into PdfDocEncoding for authentication */ +char * +pdf_from_ucs2(unsigned short *src) +{ + int i, j, len; + char *docstr; + + len = 0; + while (src[len]) + len++; + + docstr = fz_malloc(len + 1); + + for (i = 0; i < len; i++) + { + /* shortcut: check if the character has the same code point in both encodings */ + if (0 < src[i] && src[i] < 256 && pdf_doc_encoding[src[i]] == src[i]) { + docstr[i] = src[i]; + continue; + } + + /* search through pdf_docencoding for the character's code point */ + for (j = 0; j < 256; j++) + if (pdf_doc_encoding[j] == src[i]) + break; + docstr[i] = j; + + /* fail, if a character can't be encoded */ + if (!docstr[i]) + { + fz_free(docstr); + return NULL; + } + } + docstr[len] = '\0'; + + return docstr; +} + fz_obj * pdf_to_utf8_name(fz_obj *src) { |