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
|
#include "pdftool.h"
char *basename = nil;
pdf_xref *xref = nil;
pdf_pagetree *pagetree = nil;
static void (*cleanup)(void) = nil;
void closexref(void);
void die(fz_error error)
{
fz_catch(error, "aborting");
if (cleanup)
cleanup();
closexref();
exit(1);
}
void setcleanup(void (*func)(void))
{
cleanup = func;
}
void openxref(char *filename, char *password, int dieonbadpass)
{
fz_error error;
fz_obj *obj;
int okay;
basename = strrchr(filename, '/');
if (!basename)
basename = filename;
else
basename++;
error = pdf_newxref(&xref);
if (error)
die(error);
error = pdf_loadxref(xref, filename);
if (error)
{
fz_catch(error, "trying to repair");
error = pdf_repairxref(xref, filename);
if (error)
die(error);
}
error = pdf_decryptxref(xref);
if (error)
die(error);
if (xref->crypt)
{
okay = pdf_setpassword(xref->crypt, password);
if (!okay && !dieonbadpass)
fz_warn("invalid password, attempting to continue.");
else if (!okay && dieonbadpass)
die(fz_throw("invalid password"));
}
obj = fz_dictgets(xref->trailer, "Root");
xref->root = fz_resolveindirect(obj);
if (xref->root)
fz_keepobj(xref->root);
obj = fz_dictgets(xref->trailer, "Info");
xref->info = fz_resolveindirect(obj);
if (xref->info)
fz_keepobj(xref->info);
}
void closexref(void)
{
if (cleanup)
cleanup();
if (pagetree)
{
pdf_droppagetree(pagetree);
pagetree = nil;
}
if (xref)
{
pdf_closexref(xref);
xref = nil;
}
basename = nil;
}
void loadpagetree(void)
{
fz_error error;
error = pdf_loadpagetree(&pagetree, xref);
if (error)
die(error);
}
|