i hope the node casts are correct here.
[AROS-Contrib.git] / arospdf / xpdf / PDFDoc.cc
blob2c65c006329d956a0776cf8652b64ab92ea0cc9b
1 //========================================================================
2 //
3 // PDFDoc.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #include <aconf.h>
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stddef.h>
18 #include <string.h>
19 #ifdef WIN32
20 # include <windows.h>
21 #endif
22 #include "GString.h"
23 #include "config.h"
24 #include "GlobalParams.h"
25 #include "Page.h"
26 #include "Catalog.h"
27 #include "Stream.h"
28 #include "XRef.h"
29 #include "Link.h"
30 #include "OutputDev.h"
31 #include "Error.h"
32 #include "ErrorCodes.h"
33 #include "Lexer.h"
34 #include "Parser.h"
35 #include "SecurityHandler.h"
36 #ifndef DISABLE_OUTLINE
37 #include "Outline.h"
38 #endif
39 #include "PDFDoc.h"
41 //------------------------------------------------------------------------
43 #define headerSearchSize 1024 // read this many bytes at beginning of
44 // file to look for '%PDF'
46 //------------------------------------------------------------------------
47 // PDFDoc
48 //------------------------------------------------------------------------
50 PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
51 GString *userPassword, void *guiDataA) {
52 xObject obj;
53 GString *fileName1, *fileName2;
55 ok = gFalse;
56 errCode = errNone;
58 guiData = guiDataA;
60 file = NULL;
61 str = NULL;
62 xref = NULL;
63 catalog = NULL;
64 #ifndef DISABLE_OUTLINE
65 outline = NULL;
66 #endif
68 fileName = fileNameA;
69 fileName1 = fileName;
72 // try to open file
73 fileName2 = NULL;
74 #ifdef VMS
75 if (!(file = fopen(fileName1->getCString(), "rb", "ctx=stm"))) {
76 error(-1, "Couldn't open file '%s'", fileName1->getCString());
77 errCode = errOpenFile;
78 return;
80 #else
81 if (!(file = fopen(fileName1->getCString(), "rb"))) {
82 fileName2 = fileName->copy();
83 fileName2->lowerCase();
84 if (!(file = fopen(fileName2->getCString(), "rb"))) {
85 fileName2->upperCase();
86 if (!(file = fopen(fileName2->getCString(), "rb"))) {
87 error(-1, "Couldn't open file '%s'", fileName->getCString());
88 delete fileName2;
89 errCode = errOpenFile;
90 return;
93 delete fileName2;
95 #endif
97 // create stream
98 obj.initNull();
99 str = new FileStream(file, 0, gFalse, 0, &obj);
101 ok = setup(ownerPassword, userPassword);
104 #ifdef WIN32
105 PDFDoc::PDFDoc(wchar_t *fileNameA, int fileNameLen, GString *ownerPassword,
106 GString *userPassword, void *guiDataA) {
107 OSVERSIONINFO version;
108 wchar_t fileName2[_MAX_PATH + 1];
109 xObject obj;
110 int i;
112 ok = gFalse;
113 errCode = errNone;
115 guiData = guiDataA;
117 file = NULL;
118 str = NULL;
119 xref = NULL;
120 catalog = NULL;
121 #ifndef DISABLE_OUTLINE
122 outline = NULL;
123 #endif
125 //~ file name should be stored in Unicode (?)
126 fileName = new GString();
127 for (i = 0; i < fileNameLen; ++i) {
128 fileName->append((char)fileNameA[i]);
131 // zero-terminate the file name string
132 for (i = 0; i < fileNameLen && i < _MAX_PATH; ++i) {
133 fileName2[i] = fileNameA[i];
135 fileName2[i] = 0;
137 // try to open file
138 // NB: _wfopen is only available in NT
139 version.dwOSVersionInfoSize = sizeof(version);
140 GetVersionEx(&version);
141 if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) {
142 file = _wfopen(fileName2, L"rb");
143 } else {
144 file = fopen(fileName->getCString(), "rb");
146 if (!file) {
147 error(-1, "Couldn't open file '%s'", fileName->getCString());
148 errCode = errOpenFile;
149 return;
152 // create stream
153 obj.initNull();
154 str = new FileStream(file, 0, gFalse, 0, &obj);
156 ok = setup(ownerPassword, userPassword);
158 #endif
160 PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
161 GString *userPassword, void *guiDataA) {
162 ok = gFalse;
163 errCode = errNone;
164 guiData = guiDataA;
165 if (strA->getFileName()) {
166 fileName = strA->getFileName()->copy();
167 } else {
168 fileName = NULL;
170 file = NULL;
171 str = strA;
172 xref = NULL;
173 catalog = NULL;
174 #ifndef DISABLE_OUTLINE
175 outline = NULL;
176 #endif
177 ok = setup(ownerPassword, userPassword);
180 GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
181 str->reset();
183 // check header
184 checkHeader();
186 // read xref table
187 xref = new XRef(str);
188 if (!xref->isOk()) {
189 error(-1, "Couldn't read xref table");
190 errCode = xref->getErrorCode();
191 return gFalse;
194 // check for encryption
195 if (!checkEncryption(ownerPassword, userPassword)) {
196 errCode = errEncrypted;
197 return gFalse;
200 // read catalog
201 catalog = new Catalog(xref);
202 if (!catalog->isOk()) {
203 error(-1, "Couldn't read page catalog");
204 errCode = errBadCatalog;
205 return gFalse;
208 #ifndef DISABLE_OUTLINE
209 // read outline
210 outline = new Outline(catalog->getOutline(), xref);
211 #endif
213 // done
214 return gTrue;
217 PDFDoc::~PDFDoc() {
218 #ifndef DISABLE_OUTLINE
219 if (outline) {
220 delete outline;
222 #endif
223 if (catalog) {
224 delete catalog;
226 if (xref) {
227 delete xref;
229 if (str) {
230 delete str;
232 if (file) {
233 fclose(file);
235 if (fileName) {
236 delete fileName;
240 // Check for a PDF header on this stream. Skip past some garbage
241 // if necessary.
242 void PDFDoc::checkHeader() {
243 char hdrBuf[headerSearchSize+1];
244 char *p;
245 int i;
247 pdfVersion = 0;
248 for (i = 0; i < headerSearchSize; ++i) {
249 hdrBuf[i] = str->getChar();
251 hdrBuf[headerSearchSize] = '\0';
252 for (i = 0; i < headerSearchSize - 5; ++i) {
253 if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
254 break;
257 if (i >= headerSearchSize - 5) {
258 error(-1, "May not be a PDF file (continuing anyway)");
259 return;
261 str->moveStart(i);
262 if (!(p = strtok(&hdrBuf[i+5], " \t\n\r"))) {
263 error(-1, "May not be a PDF file (continuing anyway)");
264 return;
266 pdfVersion = atof(p);
267 if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
268 pdfVersion > supportedPDFVersionNum + 0.0001) {
269 error(-1, "PDF version %s -- xpdf supports version %s"
270 " (continuing anyway)", p, supportedPDFVersionStr);
274 GBool PDFDoc::checkEncryption(GString *ownerPassword, GString *userPassword) {
275 xObject encrypt;
276 GBool encrypted;
277 SecurityHandler *secHdlr;
278 GBool ret;
280 xref->getTrailerDict()->dictLookup("Encrypt", &encrypt);
281 if ((encrypted = encrypt.isDict())) {
282 if ((secHdlr = SecurityHandler::make(this, &encrypt))) {
283 if (secHdlr->checkEncryption(ownerPassword, userPassword)) {
284 // authorization succeeded
285 xref->setEncryption(secHdlr->getPermissionFlags(),
286 secHdlr->getOwnerPasswordOk(),
287 secHdlr->getFileKey(),
288 secHdlr->getFileKeyLength(),
289 secHdlr->getEncVersion(),
290 secHdlr->getEncAlgorithm());
291 ret = gTrue;
292 } else {
293 // authorization failed
294 ret = gFalse;
296 delete secHdlr;
297 } else {
298 // couldn't find the matching security handler
299 ret = gFalse;
301 } else {
302 // document is not encrypted
303 ret = gTrue;
305 encrypt.free();
306 return ret;
309 void PDFDoc::displayPage(OutputDev *out, int page,
310 double hDPI, double vDPI, int rotate,
311 GBool useMediaBox, GBool crop, GBool printing,
312 GBool (*abortCheckCbk)(void *data),
313 void *abortCheckCbkData) {
314 if (globalParams->getPrintCommands()) {
315 printf("***** page %d *****\n", page);
317 catalog->getPage(page)->display(out, hDPI, vDPI,
318 rotate, useMediaBox, crop, printing, catalog,
319 abortCheckCbk, abortCheckCbkData);
322 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
323 double hDPI, double vDPI, int rotate,
324 GBool useMediaBox, GBool crop, GBool printing,
325 GBool (*abortCheckCbk)(void *data),
326 void *abortCheckCbkData) {
327 int page;
329 for (page = firstPage; page <= lastPage; ++page) {
330 displayPage(out, page, hDPI, vDPI, rotate, useMediaBox, crop, printing,
331 abortCheckCbk, abortCheckCbkData);
335 void PDFDoc::displayPageSlice(OutputDev *out, int page,
336 double hDPI, double vDPI, int rotate,
337 GBool useMediaBox, GBool crop, GBool printing,
338 int sliceX, int sliceY, int sliceW, int sliceH,
339 GBool (*abortCheckCbk)(void *data),
340 void *abortCheckCbkData) {
341 catalog->getPage(page)->displaySlice(out, hDPI, vDPI,
342 rotate, useMediaBox, crop,
343 sliceX, sliceY, sliceW, sliceH,
344 printing, catalog,
345 abortCheckCbk, abortCheckCbkData);
348 Links *PDFDoc::getLinks(int page) {
349 return catalog->getPage(page)->getLinks(catalog);
352 void PDFDoc::processLinks(OutputDev *out, int page) {
353 catalog->getPage(page)->processLinks(out, catalog);
356 GBool PDFDoc::isLinearized() {
357 Parser *parser;
358 xObject obj1, obj2, obj3, obj4, obj5;
359 GBool lin;
361 lin = gFalse;
362 obj1.initNull();
363 parser = new Parser(xref,
364 new Lexer(xref,
365 str->makeSubStream(str->getStart(), gFalse, 0, &obj1)),
366 gTrue);
367 parser->getObj(&obj1);
368 parser->getObj(&obj2);
369 parser->getObj(&obj3);
370 parser->getObj(&obj4);
371 if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
372 obj4.isDict()) {
373 obj4.dictLookup("Linearized", &obj5);
374 if (obj5.isNum() && obj5.getNum() > 0) {
375 lin = gTrue;
377 obj5.free();
379 obj4.free();
380 obj3.free();
381 obj2.free();
382 obj1.free();
383 delete parser;
384 return lin;
387 GBool PDFDoc::saveAs(GString *name) {
388 FILE *f;
389 int c;
391 if (!(f = fopen(name->getCString(), "wb"))) {
392 error(-1, "Couldn't open file '%s'", name->getCString());
393 return gFalse;
395 str->reset();
396 while ((c = str->getChar()) != EOF) {
397 fputc(c, f);
399 str->close();
400 fclose(f);
401 return gTrue;