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
|
/*
Copyright (c) 1996-2007 Han The Thanh, <thanh@pdftex.org>
This file is part of pdfTeX.
pdfTeX is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
pdfTeX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with pdfTeX; if not, write to the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
$Id: pdftosrc.cc 114 2007-05-23 18:23:49Z ms $
*/
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <aconf.h>
#include <assert.h>
#include <GString.h>
#include <gmem.h>
#include <gfile.h>
#include "Object.h"
#include "Stream.h"
#include "Array.h"
#include "Dict.h"
#include "XRef.h"
#include "Catalog.h"
#include "Page.h"
#include "GfxFont.h"
#include "PDFDoc.h"
#include "GlobalParams.h"
#include "Error.h"
static XRef *xref = 0;
int main(int argc, char *argv[])
{
char *p, buf[1024];
PDFDoc *doc;
GString *fileName;
Stream *s;
Object srcStream, srcName, catalogDict;
FILE *outfile;
char *outname;
int objnum = 0, objgen = 0;
bool extract_xref_table = false;
int c;
fprintf(stderr, "pdftosrc version %s\n", xpdfVersion);
if (argc < 2) {
fprintf(stderr,
"Usage: pdftosrc <PDF-file> [<stream-object-number>]\n");
exit(1);
}
fileName = new GString(argv[1]);
globalParams = new GlobalParams();
doc = new PDFDoc(fileName);
if (!doc->isOk()) {
fprintf(stderr, "Invalid PDF file\n");
exit(1);
}
if (argc >= 3) {
objnum = atoi(argv[2]);
if (argc >= 4)
objgen = atoi(argv[3]);
}
xref = doc->getXRef();
catalogDict.initNull();
xref->getCatalog(&catalogDict);
if (!catalogDict.isDict("Catalog")) {
fprintf(stderr, "No Catalog found\n");
exit(1);
}
srcStream.initNull();
if (objnum == 0) {
catalogDict.dictLookup("SourceObject", &srcStream);
if (!srcStream.isStream("SourceFile")) {
fprintf(stderr, "No SourceObject found\n");
exit(1);
}
srcName.initNull();
srcStream.getStream()->getDict()->lookup("SourceName", &srcName);
if (!srcName.isString()) {
fprintf(stderr, "No SourceName found\n");
exit(1);
}
outname = srcName.getString()->getCString();
// We cannot free srcName, as objname shares its string.
// srcName.free();
} else if (objnum > 0) {
xref->fetch(objnum, objgen, &srcStream);
if (!srcStream.isStream()) {
fprintf(stderr, "Not a Stream object\n");
exit(1);
}
sprintf(buf, "%s", fileName->getCString());
if ((p = strrchr(buf, '.')) == 0)
p = strchr(buf, 0);
if (objgen == 0)
sprintf(p, ".%i", objnum);
else
sprintf(p, ".%i+%i", objnum, objgen);
outname = buf;
} else { // objnum < 0 means we are extracting the XRef table
extract_xref_table = true;
sprintf(buf, "%s", fileName->getCString());
if ((p = strrchr(buf, '.')) == 0)
p = strchr(buf, 0);
sprintf(p, ".xref");
outname = buf;
}
if (!(outfile = fopen(outname, "wb"))) {
fprintf(stderr, "Cannot open file \"%s\" for writing\n", outname);
exit(1);
}
if (extract_xref_table) {
int size = xref->getSize();
int i;
for (i = 0; i < size; i++) {
if (xref->getEntry(i)->offset == 0xffffffff)
break;
}
size = i;
fprintf(outfile, "xref\n");
fprintf(outfile, "0 %i\n", size);
for (i = 0; i < size; i++) {
XRefEntry *e = xref->getEntry(i);
if (e->type != xrefEntryCompressed)
fprintf(outfile, "%.10lu %.5i %s\n",
(long unsigned) e->offset, e->gen,
(e->type == xrefEntryFree ? "f" : "n"));
else { // e->offset is the object number of the object stream
// e->gen is the local index inside that object stream
//int objStrOffset = xref->getEntry(e->offset)->offset;
Object tmpObj;
xref->fetch(i, e->gen, &tmpObj); // to ensure xref->objStr is set
ObjectStream *objStr = xref->getObjStr();
assert(objStr != NULL);
int *localOffsets = objStr->getOffsets();
assert(localOffsets != NULL);
// fprintf(outfile, "%0.10lu %i n\n",
// (long unsigned) (objStrOffset), e->gen);
fprintf(outfile, "%.10lu 00000 n\n",
(long unsigned) (objStr->getFirstOffset() +
localOffsets[e->gen]));
// (long unsigned) (objStrOffset + objStr->getStart() + localOffsets[e->gen]));
tmpObj.free();
}
}
} else {
s = srcStream.getStream();
s->reset();
while ((c = s->getChar()) != EOF)
fputc(c, outfile);
srcStream.free();
}
if (objnum == 0)
fprintf(stderr, "Source file extracted to %s\n", outname);
else if (objnum > 0)
fprintf(stderr, "Stream object extracted to %s\n", outname);
else
fprintf(stderr, "Cross-reference table extracted to %s\n", outname);
fclose(outfile);
catalogDict.free();
delete doc;
delete globalParams;
}
|