diff options
-rw-r--r-- | apps/pdfclean.c | 101 | ||||
-rw-r--r-- | mupdf/pdf_crypt.c | 105 |
2 files changed, 10 insertions, 196 deletions
diff --git a/apps/pdfclean.c b/apps/pdfclean.c index 0f52046b..77da8f9b 100644 --- a/apps/pdfclean.c +++ b/apps/pdfclean.c @@ -9,8 +9,6 @@ #include "pdftool.h" -static fz_obj *id = NULL; - static pdf_crypt *outcrypt = NULL; static FILE *out = NULL; @@ -18,7 +16,6 @@ static char *uselist = NULL; static int *ofslist = NULL; static int *genlist = NULL; -static int doencrypt = 0; static int dogarbage = 0; static int doexpand = 0; @@ -127,15 +124,8 @@ static void copystream(fz_obj *obj, int oid, int gen) if (error) die(error); - if (doencrypt) - pdf_cryptbuffer(outcrypt, buf, oid, gen); - fprintf(out, "%d %d obj\n", oid, gen); - if (doencrypt) - pdf_cryptobj(outcrypt, obj, oid, gen); fz_fprintobj(out, obj, !doexpand); - if (doencrypt) - pdf_cryptobj(outcrypt, obj, oid, gen); fprintf(out, "stream\n"); fwrite(buf->rp, 1, buf->wp - buf->rp, out); fprintf(out, "endstream\nendobj\n\n"); @@ -153,9 +143,6 @@ static void expandstream(fz_obj *obj, int oid, int gen) if (error) die(error); - if (doencrypt) - pdf_cryptbuffer(outcrypt, buf, oid, gen); - fz_copydict(&newdict, obj); fz_dictdels(newdict, "Filter"); fz_dictdels(newdict, "DecodeParms"); @@ -165,11 +152,7 @@ static void expandstream(fz_obj *obj, int oid, int gen) fz_dropobj(newlen); fprintf(out, "%d %d obj\n", oid, gen); - if (doencrypt) - pdf_cryptobj(outcrypt, obj, oid, gen); fz_fprintobj(out, newdict, !doexpand); - if (doencrypt) - pdf_cryptobj(outcrypt, obj, oid, gen); fprintf(out, "stream\n"); fwrite(buf->rp, 1, buf->wp - buf->rp, out); fprintf(out, "endstream\nendobj\n\n"); @@ -211,11 +194,7 @@ static void saveobject(int oid, int gen) if (!xref->table[oid].stmofs) { fprintf(out, "%d %d obj\n", oid, gen); - if (doencrypt) - pdf_cryptobj(outcrypt, obj, oid, gen); fz_fprintobj(out, obj, !doexpand); - if (doencrypt) - pdf_cryptobj(outcrypt, obj, oid, gen); fprintf(out, "endobj\n\n"); } else @@ -239,8 +218,8 @@ static void savexref(void) startxref = ftell(out); - fprintf(out, "xref\n0 %d\n", xref->len + doencrypt); - for (oid = 0; oid < xref->len + doencrypt; oid++) + fprintf(out, "xref\n0 %d\n", xref->len); + for (oid = 0; oid < xref->len; oid++) { if (uselist[oid]) fprintf(out, "%010d %05d n \n", ofslist[oid], genlist[oid]); @@ -263,14 +242,9 @@ static void savexref(void) if (obj) fz_dictputs(trailer, "Root", obj); - if (doencrypt) - { - fz_newindirect(&obj, xref->len, 0, xref); - fz_dictputs(trailer, "Encrypt", obj); - fz_dropobj(obj); - } - - fz_dictputs(trailer, "ID", id); + obj = fz_dictgets(xref->trailer, "ID"); + if (obj) + fz_dictputs(trailer, "ID", obj); fprintf(out, "trailer\n"); fz_fprintobj(out, trailer, !doexpand); @@ -283,14 +257,9 @@ static void cleanusage(void) { fprintf(stderr, "usage: pdfclean [options] input.pdf [outfile.pdf]\n" - " -d -\tpassword for decryption\n" + " -p -\tpassword for decryption\n" " -g \tgarbage collect unused objects\n" - " -x \texpand compressed streams\n" - " -e \tencrypt output\n" - " -u -\tset user password for encryption\n" - " -o -\tset owner password\n" - " -p -\tset permissions (combine letters 'pmca')\n" - " -n -\tkey length in bits: 40 <= n <= 128\n"); + " -x \texpand compressed streams\n"); exit(1); } @@ -298,37 +267,17 @@ int main(int argc, char **argv) { char *infile; char *outfile = "out.pdf"; - char *userpw = ""; - char *ownerpw = ""; - unsigned perms = 0xfffff0c0; /* nothing allowed */ - int keylen = 40; char *password = ""; fz_error error; int c, oid; int lastfree; - while ((c = fz_getopt(argc, argv, "d:egn:o:p:u:x")) != -1) + while ((c = fz_getopt(argc, argv, "gxp:")) != -1) { switch (c) { - case 'p': - /* see TABLE 3.15 User access permissions */ - perms = 0xfffff0c0; - if (strchr(fz_optarg, 'p')) /* print */ - perms |= (1 << 2) | (1 << 11); - if (strchr(fz_optarg, 'm')) /* modify */ - perms |= (1 << 3) | (1 << 10); - if (strchr(fz_optarg, 'c')) /* copy */ - perms |= (1 << 4) | (1 << 9); - if (strchr(fz_optarg, 'a')) /* annotate / forms */ - perms |= (1 << 5) | (1 << 8); - break; - case 'd': password = fz_optarg; break; - case 'e': doencrypt ++; break; + case 'p': password = fz_optarg; break; case 'g': dogarbage ++; break; - case 'n': keylen = atoi(fz_optarg); break; - case 'o': ownerpw = fz_optarg; break; - case 'u': userpw = fz_optarg; break; case 'x': doexpand ++; break; default: cleanusage(); break; } @@ -343,25 +292,6 @@ int main(int argc, char **argv) openxref(infile, password, 0); - id = fz_dictgets(xref->trailer, "ID"); - if (!id) - { - error = fz_packobj(&id, xref, "[(ABCDEFGHIJKLMNOP)(ABCDEFGHIJKLMNOP)]"); - if (error) - die(error); - } - else - { - fz_keepobj(id); - } - - if (doencrypt) - { - error = pdf_newencrypt(&outcrypt, userpw, ownerpw, perms, keylen, id); - if (error) - die(error); - } - out = fopen(outfile, "wb"); if (!out) die(fz_throw("cannot open output file '%s'", outfile)); @@ -410,20 +340,9 @@ int main(int argc, char **argv) } } - /* add new encryption dictionary to xref */ - if (doencrypt) - { - ofslist[xref->len] = ftell(out); - genlist[xref->len] = 0; - uselist[xref->len] = 1; - fprintf(out, "%d %d obj\n", xref->len, 0); - fz_fprintobj(out, outcrypt->encrypt, !doexpand); - fprintf(out, "endobj\n\n"); - } - /* construct linked list of free object slots */ lastfree = 0; - for (oid = 0; oid < xref->len + doencrypt; oid++) + for (oid = 0; oid < xref->len; oid++) { if (!uselist[oid]) { diff --git a/mupdf/pdf_crypt.c b/mupdf/pdf_crypt.c index b9f57527..35da028d 100644 --- a/mupdf/pdf_crypt.c +++ b/mupdf/pdf_crypt.c @@ -358,49 +358,6 @@ createkey(pdf_crypt *crypt, unsigned char *userpw, int pwlen) } /* - * Algorithm 3.3 Computing the O value - */ -static void -createowner(pdf_crypt *crypt, unsigned char *userpw, int userpwlen, unsigned char *ownerpw, int ownerpwlen) -{ - unsigned char buf[32]; - unsigned char key[16]; - fz_arc4 arc4; - fz_md5 md5; - - /* Step 1 + 2 */ - if (ownerpwlen == 0) - { - ownerpw = userpw; - ownerpwlen = userpwlen; - } - padpassword(buf, ownerpw, ownerpwlen); - fz_md5init(&md5); - fz_md5update(&md5, buf, 32); - fz_md5final(&md5, key); - - /* Step 3 (rev 3 or later) */ - if (crypt->r >= 3) - voodoo50(key, crypt->len); - - /* Step 4 */ - fz_arc4init(&arc4, key, crypt->len); - - /* Step 5 */ - padpassword(buf, userpw, ownerpwlen); - - /* Step 6 */ - fz_arc4encrypt(&arc4, buf, buf, 32); - - /* Step 7 (rev 3 or later) */ - if (crypt->r >= 3) - voodoo19(buf, 32, key, crypt->len); - - /* Step 8 */ - memcpy(crypt->o, buf, 32); -} - -/* * Algorithm 3.4 Computing the U value (rev 2) * Algorithm 3.5 Computing the U value (rev 3 or later) */ @@ -445,55 +402,6 @@ createuser(pdf_crypt *crypt, unsigned char *userpw, int pwlen) } } -/* - * Create crypt object for encrypting, given passwords, - * permissions, and file ID - */ -fz_error -pdf_newencrypt(pdf_crypt **cp, char *userpw, char *ownerpw, int p, int n, fz_obj *id) -{ - fz_error error; - pdf_crypt *crypt; - - crypt = fz_malloc(sizeof(pdf_crypt)); - if (!crypt) - return fz_rethrow(-1, "out of memory: crypt struct"); - - crypt->encrypt = nil; - crypt->id = fz_keepobj(fz_arrayget(id, 0)); - crypt->p = p; - crypt->len = MIN(MAX(n / 8, 5), 16); - crypt->keylen = MIN(crypt->len + 5, 16); - crypt->r = crypt->len == 5 ? 2 : 3; - - createowner(crypt, - (unsigned char *) userpw, strlen(userpw), - (unsigned char *) ownerpw, strlen(ownerpw)); - createuser(crypt, - (unsigned char *) userpw, strlen(userpw)); - - error = fz_packobj(&crypt->encrypt, nil, - "<< /Filter /Standard " - "/V %i /R %i " - "/O %# /U %# " - "/P %i " - "/Length %i >>", - crypt->r == 2 ? 1 : 2, - crypt->r, - crypt->o, 32, - crypt->u, 32, - crypt->p, - crypt->len * 8); - if (error) - { - pdf_freecrypt(crypt); - return fz_rethrow(error, "cannot create encryption dictionary"); - } - - *cp = crypt; - return fz_okay; -} - int pdf_setpassword(pdf_crypt *crypt, char *pw) { @@ -635,19 +543,6 @@ pdf_cryptobj(pdf_crypt *crypt, fz_obj *obj, int oid, int gen) } /* - * De/encrypt the contents of a buffer - */ -void -pdf_cryptbuffer(pdf_crypt *crypt, fz_buffer *buf, int oid, int gen) -{ - fz_arc4 arc4; - unsigned char key[16]; - createobjkey(crypt, oid, gen, key); - fz_arc4init(&arc4, key, crypt->keylen); - fz_arc4encrypt(&arc4, buf->rp, buf->rp, buf->wp - buf->rp); -} - -/* * Create filter suitable for de/encrypting a stream */ fz_error |