diff options
author | Tor Andersson <tor@ghostscript.com> | 2005-01-11 07:19:08 +0100 |
---|---|---|
committer | Tor Andersson <tor@ghostscript.com> | 2005-01-11 07:19:08 +0100 |
commit | 4a6def1aebcb61599f9d975e92079aad5bb423c6 (patch) | |
tree | 65d7ae5520822cc10b79ef5d421672f5b7d6f435 /apps/pdfdebug.c | |
parent | 15db9438f7a852643bb2b5e81e67de4eb805b173 (diff) | |
download | mupdf-4a6def1aebcb61599f9d975e92079aad5bb423c6.tar.xz |
rename apps directory and reorganize macpdf.app
Diffstat (limited to 'apps/pdfdebug.c')
-rw-r--r-- | apps/pdfdebug.c | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/apps/pdfdebug.c b/apps/pdfdebug.c new file mode 100644 index 00000000..2a18ade7 --- /dev/null +++ b/apps/pdfdebug.c @@ -0,0 +1,188 @@ +#include <fitz.h> +#include <mupdf.h> + +static char *password = ""; +static int dodecode = 0; +static int doprintxref = 0; + +void usage() +{ + fprintf(stderr, "usage: pdfdebug [-dxs] [-u password] file.pdf [oid ...]\n"); + exit(1); +} + +/* + * Debug-print stream contents + */ + +static int safecol = 0; + +void printsafe(unsigned char *buf, int n) +{ + int i; + for (i = 0; i < n; i++) { + if (buf[i] == '\r' || buf[i] == '\n') { + printf("\n"); + safecol = 0; + } + else if (buf[i] < 32 || buf[i] > 126) { + printf("."); + safecol ++; + } + else { + printf("%c", buf[i]); + safecol ++; + } + if (safecol == 79) { + printf("\n"); + safecol = 0; + } + } +} + +void decodestream(pdf_xref *xref, int oid, int gid) +{ + fz_error *error; + unsigned char buf[512]; + + safecol = 0; + + error = pdf_openstream(xref, oid, gid); + if (error) fz_abort(error); + + while (1) + { + int n = fz_read(xref->stream, buf, sizeof buf); + if (n == 0) + break; + if (n < 0) + fz_abort(fz_ferror(xref->stream)); + printsafe(buf, n); + } + + pdf_closestream(xref); +} + +void copystream(pdf_xref *xref, int oid, int gid) +{ + fz_error *error; + unsigned char buf[512]; + + safecol = 0; + + error = pdf_openrawstream(xref, oid, gid); + if (error) fz_abort(error); + + while (1) + { + int n = fz_read(xref->stream, buf, sizeof buf); + if (n == 0) + break; + if (n < 0) + fz_abort(fz_ferror(xref->stream)); + printsafe(buf, n); + } + + pdf_closestream(xref); +} + +void printobject(pdf_xref *xref, int oid, int gid) +{ + fz_error *error; + fz_obj *obj; + + error = pdf_loadobject(&obj, xref, oid, gid); + if (error) fz_abort(error); + + printf("%d %d obj\n", oid, gid); + fz_debugobj(obj); + printf("\n"); + + if (xref->table[oid].stmofs) { + printf("stream\n"); + if (dodecode) + decodestream(xref, oid, gid); + else + copystream(xref, oid, gid); + printf("endstream\n"); + } + + printf("endobj\n"); + + fz_dropobj(obj); +} + +int main(int argc, char **argv) +{ + fz_error *error; + char *filename; + pdf_xref *xref; + int c; + + while ((c = getopt(argc, argv, "drxopu:")) != -1) + { + switch (c) + { + case 'd': + dodecode ++; + break; + case 'x': + doprintxref ++; + break; + case 'u': + password = optarg; + break; + default: + usage(); + } + } + + if (argc - optind == 0) + usage(); + + filename = argv[optind++]; + + error = pdf_newxref(&xref); + if (error) + fz_abort(error); + + error = pdf_loadxref(xref, filename); + if (error) + { + fz_warn("trying to repair"); + error = pdf_repairxref(xref, filename); + if (error) + fz_abort(error); + } + + error = pdf_decryptxref(xref); + if (error) + fz_abort(error); + + if (xref->crypt) + { + error = pdf_setpassword(xref->crypt, password); + if (error) fz_abort(error); + } + + if (optind == argc) + { + printf("trailer\n"); + fz_debugobj(xref->trailer); + printf("\n"); + } + + for ( ; optind < argc; optind++) + { + printobject(xref, atoi(argv[optind]), 0); + printf("\n"); + } + + if (doprintxref) + pdf_debugxref(xref); + + pdf_closexref(xref); + + return 0; +} + |