summaryrefslogtreecommitdiff
path: root/test/pdfdebug.c
blob: ddc10dff19f97a17580c55b8dd40d47d2f97675a (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <fitz.h>
#include <mupdf.h>

static char *password = "";
static int dodecode = 0;
static int dorepair = 0;
static int doprintxref = 0;

void usage()
{
	fprintf(stderr, "usage: pdfdebug [-drx] [-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, fz_obj *stream, int oid, int gid, int ofs)
{
	fz_error *error;
	unsigned char buf[512];

	safecol = 0;

	error = pdf_openstream0(xref, stream, oid, gid, ofs);
	if (error) fz_abort(error);

	while (1)
	{
		int n = fz_read(xref->file, buf, sizeof buf);
		if (n == 0)
			break;
		if (n < 0)
			fz_abort(fz_ferror(xref->file));
		printsafe(buf, n);
	}

	pdf_closestream(xref);
}

void copystream(pdf_xref *xref, fz_obj *stream, int ofs)
{
	fz_error *error;
	unsigned char buf[512];
	fz_filter *filter;
	fz_obj *obj;
	int len;

	safecol = 0;

	obj = fz_dictgets(stream, "Length");
	error = pdf_resolve(&obj, xref);
	if (error) fz_abort(error);
	len = fz_toint(obj);
	fz_dropobj(obj);

	error = fz_newnullfilter(&filter, len);
	if (error) fz_abort(error);

	fz_seek(xref->file, ofs);

	error = fz_pushfilter(xref->file, filter);
	if (error) fz_abort(error);

	while (1)
	{
		int n = fz_read(xref->file, buf, sizeof buf);
		if (n == 0)
			break;
		if (n < 0)
			fz_abort(fz_ferror(xref->file));
		printsafe(buf, n);
	}

	fz_popfilter(xref->file);
}

void printobject(pdf_xref *xref, int oid, int gid)
{
	fz_error *error;
	int stmofs;
	fz_obj *obj;

	error = pdf_loadobject0(&obj, xref, oid, gid, &stmofs);
	if (error) fz_abort(error);

	printf("%d %d obj\n", oid, gid);
	fz_debugobj(obj);
	printf("\n");
	if (stmofs != -1) {
		printf("stream\n");
		if (dodecode)
			decodestream(xref, obj, oid, gid, stmofs);
		else
			copystream(xref, obj, stmofs);
		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 'r':
			dorepair ++;
			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);

	if (dorepair)
		error = pdf_repairxref(xref, filename);
	else
		error = pdf_openxref(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 (doprintxref)
		pdf_debugxref(xref);

	if (optind == argc)
	{
		printf("trailer\n");
		fz_debugobj(xref->trailer);
		printf("\n");
	}

	for ( ; optind < argc; optind++)
	{
		printobject(xref, atoi(argv[optind]), 0);
		printf("\n");
	}

	pdf_closexref(xref);

	return 0;
}