1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
#include <fitz.h>
#include <mupdf.h>
void usage()
{
fprintf(stderr,
"usage: pdfclean [options] infile.pdf outfile.pdf\n"
" -r\treconstruct broken xref table\n"
" -g\tgarbage collect unused objects\n"
" -x\texpand compressed streams\n"
" -d -\tset user password for decryption\n"
" -e\tencrypt outfile\n"
" -u -\tset user password for encryption\n"
" -o -\tset owner password\n"
" -p -\tset permissions\n"
" -n -\tkey length in bits: 40 <= n <= 128\n"
);
exit(1);
}
void expandstreams(pdf_xref *xref)
{
fz_error *error;
fz_obj *stmobj;
int stmofs;
fz_buffer *buf;
fz_obj *stmlen;
int i, gen;
for (i = 0; i < xref->size; i++)
{
if (xref->table[i].type == 'n')
{
gen = xref->table[i].gen;
error = pdf_loadobject0(&stmobj, xref, i, gen, &stmofs);
if (error) fz_abort(error);
if (stmofs != -1)
{
error = pdf_readstream0(&buf, xref, stmobj, i, gen, stmofs);
if (error) fz_abort(error);
fz_dictdels(stmobj, "Filter");
fz_dictdels(stmobj, "DecodeParms");
error = fz_newint(&stmlen, buf->wp - buf->rp);
if (error) fz_abort(error);
error = fz_dictputs(stmobj, "Length", stmlen);
if (error) fz_abort(error);
fz_dropobj(stmlen);
error = pdf_saveobject(xref, i, gen, stmobj);
if (error) fz_abort(error);
error = pdf_savestream(xref, i, gen, buf);
if (error) fz_abort(error);
}
}
}
}
int main(int argc, char **argv)
{
fz_error *error;
char *infile;
char *outfile;
pdf_xref *xref;
int c;
int doencrypt = 0;
int dorepair = 0;
int doexpand = 0;
int dogc = 0;
char *userpw = "";
char *ownerpw = "";
int perms = -4; /* 0xfffffffc */
int keylen = 40;
char *password = "";
while ((c = getopt(argc, argv, "rgxd:eu:o:p:n:")) != -1)
{
switch (c)
{
case 'r': ++ dorepair; break;
case 'x': ++ doexpand; break;
case 'g': ++ dogc; break;
case 'e': ++ doencrypt; break;
case 'u': userpw = optarg; break;
case 'o': ownerpw = optarg; break;
case 'p': perms = atoi(optarg); break;
case 'n': keylen = atoi(optarg); break;
case 'd': password = optarg; break;
default: usage();
}
}
if (argc - optind < 2)
usage();
infile = argv[optind++];
outfile = argv[optind++];
error = pdf_newxref(&xref);
if (error)
fz_abort(error);
if (dorepair)
error = pdf_repairxref(xref, infile);
else
error = pdf_openxref(xref, infile);
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 (doexpand)
expandstreams(xref);
printf("saving %s...\n", outfile);
error = pdf_savepdf(xref, outfile);
if (error)
fz_abort(error);
pdf_closexref(xref);
return 0;
}
|