| 1 |
/* |
| 2 |
Copyright 1996-2017 Han The Thanh, <[email protected]> |
| 3 |
|
| 4 |
This file is part of pdfTeX. |
| 5 |
|
| 6 |
pdfTeX is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
pdfTeX is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License along |
| 17 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
#include <w2c/config.h> |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <stddef.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
#include <ctype.h> |
| 26 |
|
| 27 |
#include <aconf.h> |
| 28 |
#include <GString.h> |
| 29 |
#include <gmem.h> |
| 30 |
#include <gfile.h> |
| 31 |
#include <assert.h> |
| 32 |
|
| 33 |
#include "Object.h" |
| 34 |
#include "Stream.h" |
| 35 |
#include "Lexer.h" |
| 36 |
#include "Parser.h" |
| 37 |
#include "Array.h" |
| 38 |
#include "Dict.h" |
| 39 |
#include "XRef.h" |
| 40 |
#include "Catalog.h" |
| 41 |
#include "Page.h" |
| 42 |
#include "GfxFont.h" |
| 43 |
#include "PDFDoc.h" |
| 44 |
#include "GlobalParams.h" |
| 45 |
#include "Error.h" |
| 46 |
|
| 47 |
static XRef *xref = 0; |
| 48 |
|
| 49 |
int main(int argc, char *argv[]) |
| 50 |
{ |
| 51 |
char *p, buf[1024]; |
| 52 |
PDFDoc *doc; |
| 53 |
GString *fileName; |
| 54 |
Stream *s; |
| 55 |
Object srcStream, srcName, catalogDict; |
| 56 |
FILE *outfile; |
| 57 |
char *outname; |
| 58 |
int objnum = 0, objgen = 0; |
| 59 |
bool extract_xref_table = false; |
| 60 |
int c; |
| 61 |
fprintf(stderr, "pdftosrc version %s\n", xpdfVersion); |
| 62 |
if (argc < 2) { |
| 63 |
fprintf(stderr, |
| 64 |
"Usage: pdftosrc <PDF-file> [<stream-object-number>]\n"); |
| 65 |
exit(1); |
| 66 |
} |
| 67 |
fileName = new GString(argv[1]); |
| 68 |
globalParams = new GlobalParams(); |
| 69 |
doc = new PDFDoc(fileName); |
| 70 |
if (!doc->isOk()) { |
| 71 |
fprintf(stderr, "Invalid PDF file\n"); |
| 72 |
exit(1); |
| 73 |
} |
| 74 |
if (argc >= 3) { |
| 75 |
objnum = atoi(argv[2]); |
| 76 |
if (argc >= 4) |
| 77 |
objgen = atoi(argv[3]); |
| 78 |
} |
| 79 |
xref = doc->getXRef(); |
| 80 |
catalogDict.initNull(); |
| 81 |
xref->getCatalog(&catalogDict); |
| 82 |
if (!catalogDict.isDict("Catalog")) { |
| 83 |
fprintf(stderr, "No Catalog found\n"); |
| 84 |
exit(1); |
| 85 |
} |
| 86 |
srcStream.initNull(); |
| 87 |
if (objnum == 0) { |
| 88 |
catalogDict.dictLookup("SourceObject", &srcStream); |
| 89 |
static char const_SourceFile[] = "SourceFile"; |
| 90 |
if (!srcStream.isStream(const_SourceFile)) { |
| 91 |
fprintf(stderr, "No SourceObject found\n"); |
| 92 |
exit(1); |
| 93 |
} |
| 94 |
srcName.initNull(); |
| 95 |
srcStream.getStream()->getDict()->lookup("SourceName", &srcName); |
| 96 |
if (!srcName.isString()) { |
| 97 |
fprintf(stderr, "No SourceName found\n"); |
| 98 |
exit(1); |
| 99 |
} |
| 100 |
outname = srcName.getString()->getCString(); |
| 101 |
// We cannot free srcName, as objname shares its string. |
| 102 |
// srcName.free(); |
| 103 |
} else if (objnum > 0) { |
| 104 |
xref->fetch(objnum, objgen, &srcStream); |
| 105 |
if (!srcStream.isStream()) { |
| 106 |
fprintf(stderr, "Not a Stream object\n"); |
| 107 |
exit(1); |
| 108 |
} |
| 109 |
sprintf(buf, "%s", fileName->getCString()); |
| 110 |
if ((p = strrchr(buf, '.')) == 0) |
| 111 |
p = strchr(buf, 0); |
| 112 |
if (objgen == 0) |
| 113 |
sprintf(p, ".%i", objnum); |
| 114 |
else |
| 115 |
sprintf(p, ".%i+%i", objnum, objgen); |
| 116 |
outname = buf; |
| 117 |
} else { // objnum < 0 means we are extracting the XRef table |
| 118 |
extract_xref_table = true; |
| 119 |
sprintf(buf, "%s", fileName->getCString()); |
| 120 |
if ((p = strrchr(buf, '.')) == 0) |
| 121 |
p = strchr(buf, 0); |
| 122 |
sprintf(p, ".xref"); |
| 123 |
outname = buf; |
| 124 |
} |
| 125 |
if (!(outfile = fopen(outname, "wb"))) { |
| 126 |
fprintf(stderr, "Cannot open file \"%s\" for writing\n", outname); |
| 127 |
exit(1); |
| 128 |
} |
| 129 |
if (extract_xref_table) { |
| 130 |
int size = xref->getNumObjects(); |
| 131 |
int i; |
| 132 |
for (i = 0; i < size; i++) { |
| 133 |
if (xref->getEntry(i)->offset == 0xffffffff) |
| 134 |
break; |
| 135 |
} |
| 136 |
size = i; |
| 137 |
fprintf(outfile, "xref\n"); |
| 138 |
fprintf(outfile, "0 %i\n", size); |
| 139 |
for (i = 0; i < size; i++) { |
| 140 |
XRefEntry *e = xref->getEntry(i); |
| 141 |
if (e->type != xrefEntryCompressed) |
| 142 |
fprintf(outfile, "%.10lu %.5i %s\n", |
| 143 |
(long unsigned) e->offset, e->gen, |
| 144 |
(e->type == xrefEntryFree ? "f" : "n")); |
| 145 |
else { // e->offset is the object number of the object stream |
| 146 |
Stream *str; |
| 147 |
Lexer *lexer; |
| 148 |
Parser *parser; |
| 149 |
Object objStr, obj1, obj2; |
| 150 |
int nObjects, first, n; |
| 151 |
int localOffset = 0; |
| 152 |
Guint firstOffset; |
| 153 |
|
| 154 |
assert(xref->fetch(e->offset, 0, &objStr)->isStream()); |
| 155 |
nObjects = objStr.streamGetDict()->lookup("N", &obj1)->getInt(); |
| 156 |
obj1.free(); |
| 157 |
first = objStr.streamGetDict()->lookup("First", &obj1)->getInt(); |
| 158 |
obj1.free(); |
| 159 |
firstOffset = objStr.getStream()->getBaseStream()->getStart() + first; |
| 160 |
|
| 161 |
// parse the header: object numbers and offsets |
| 162 |
objStr.streamReset(); |
| 163 |
obj1.initNull(); |
| 164 |
str = new EmbedStream(objStr.getStream(), &obj1, gTrue, first); |
| 165 |
lexer = new Lexer(xref, str); |
| 166 |
parser = new Parser(xref, lexer, gFalse); |
| 167 |
for (n = 0; n < nObjects; ++n) { |
| 168 |
parser->getObj(&obj1); |
| 169 |
parser->getObj(&obj2); |
| 170 |
if (n == e->gen) |
| 171 |
localOffset = obj2.getInt(); |
| 172 |
obj1.free(); |
| 173 |
obj2.free(); |
| 174 |
} |
| 175 |
#if defined(XPDF304) |
| 176 |
while (str->getChar() != EOF) ; |
| 177 |
#else /* xpdf 4.00 */ |
| 178 |
lexer->skipToEOF(); |
| 179 |
#endif |
| 180 |
delete parser; |
| 181 |
objStr.free(); |
| 182 |
|
| 183 |
fprintf(outfile, "%.10lu 00000 n\n", |
| 184 |
(long unsigned)(firstOffset + localOffset)); |
| 185 |
} |
| 186 |
} |
| 187 |
} else { |
| 188 |
s = srcStream.getStream(); |
| 189 |
s->reset(); |
| 190 |
while ((c = s->getChar()) != EOF) |
| 191 |
fputc(c, outfile); |
| 192 |
srcStream.free(); |
| 193 |
} |
| 194 |
if (objnum == 0) |
| 195 |
fprintf(stderr, "Source file extracted to %s\n", outname); |
| 196 |
else if (objnum > 0) |
| 197 |
fprintf(stderr, "Stream object extracted to %s\n", outname); |
| 198 |
else |
| 199 |
fprintf(stderr, "Cross-reference table extracted to %s\n", outname); |
| 200 |
fclose(outfile); |
| 201 |
catalogDict.free(); |
| 202 |
delete doc; |
| 203 |
delete globalParams; |
| 204 |
} |