summaryrefslogtreecommitdiff
path: root/Build/source/libs/xpdf/xpdf-src/xpdf/PDFDoc.cc
blob: a6663ea4222aeb7a587f3af09a6f122bd7ed029e (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//========================================================================
//
// PDFDoc.cc
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================

#include <aconf.h>

#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#ifdef _WIN32
#  include <windows.h>
#endif
#include "gmempp.h"
#include "GString.h"
#include "gfile.h"
#include "config.h"
#include "GlobalParams.h"
#include "Page.h"
#include "Catalog.h"
#include "Stream.h"
#include "XRef.h"
#include "Link.h"
#include "OutputDev.h"
#include "Error.h"
#include "ErrorCodes.h"
#include "Lexer.h"
#include "Parser.h"
#include "SecurityHandler.h"
#include "UTF8.h"
#ifndef DISABLE_OUTLINE
#include "Outline.h"
#endif
#include "OptionalContent.h"
#include "PDFDoc.h"

//------------------------------------------------------------------------

#define headerSearchSize 1024	// read this many bytes at beginning of
				//   file to look for '%PDF'

// Avoid sharing files with child processes on Windows, where sharing
// can cause problems.
#ifdef _WIN32
#  define fopenReadMode "rbN"
#  define wfopenReadMode L"rbN"
#else
#  define fopenReadMode "rb"
#endif

//------------------------------------------------------------------------
// PDFDoc
//------------------------------------------------------------------------

PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
	       GString *userPassword, PDFCore *coreA) {
  Object obj;
  GString *fileName1, *fileName2;
#ifdef _WIN32
  int n, i;
#endif

  init(coreA);

  fileName = fileNameA;
#ifdef _WIN32
  n = fileName->getLength();
  fileNameU = (wchar_t *)gmallocn(n + 1, sizeof(wchar_t));
  for (i = 0; i < n; ++i) {
    fileNameU[i] = (wchar_t)(fileName->getChar(i) & 0xff);
  }
  fileNameU[n] = L'\0';
#endif

  fileName1 = fileName;

  // try to open file
  fileName2 = NULL;
#ifdef VMS
  if (!(file = fopen(fileName1->getCString(), fopenReadMode, "ctx=stm"))) {
    error(errIO, -1, "Couldn't open file '{0:t}'", fileName1);
    errCode = errOpenFile;
    return;
  }
#else
  if (!(file = fopen(fileName1->getCString(), fopenReadMode))) {
    fileName2 = fileName->copy();
    fileName2->lowerCase();
    if (!(file = fopen(fileName2->getCString(), fopenReadMode))) {
      fileName2->upperCase();
      if (!(file = fopen(fileName2->getCString(), fopenReadMode))) {
	error(errIO, -1, "Couldn't open file '{0:t}'", fileName);
	delete fileName2;
	errCode = errOpenFile;
	return;
      }
    }
    delete fileName2;
  }
#endif

  // create stream
  obj.initNull();
  str = new FileStream(file, 0, gFalse, 0, &obj);

  ok = setup(ownerPassword, userPassword);
}

#if 0
#ifdef _WIN32
PDFDoc::PDFDoc(wchar_t *fileNameA, int fileNameLen, GString *ownerPassword,
	       GString *userPassword, PDFCore *coreA) {
  OSVERSIONINFO version;
  Object obj;
  int i;

  init(coreA);

  // handle a Windows shortcut
  wchar_t wPath[winMaxLongPath + 1];
  int n = fileNameLen < winMaxLongPath ? fileNameLen : winMaxLongPath;
  memcpy(wPath, fileNameA, n * sizeof(wchar_t));
  wPath[n] = L'\0';
  readWindowsShortcut(wPath, winMaxLongPath + 1);
  int wPathLen = (int)wcslen(wPath);

  // save both Unicode and 8-bit copies of the file name
  fileName = new GString();
  fileNameU = (wchar_t *)gmallocn(wPathLen + 1, sizeof(wchar_t));
  memcpy(fileNameU, wPath, (wPathLen + 1) * sizeof(wchar_t));
  for (i = 0; i < wPathLen; ++i) {
    fileName->append((char)fileNameA[i]);
  }

  // try to open file
  // NB: _wfopen is only available in NT
  version.dwOSVersionInfoSize = sizeof(version);
  GetVersionEx(&version);
  if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) {
    file = _wfopen(fileNameU, wfopenReadMode);
  } else {
    file = fopen(fileName->getCString(), fopenReadMode);
  }
  if (!file) {
    error(errIO, -1, "Couldn't open file '{0:t}'", fileName);
    errCode = errOpenFile;
    return;
  }

  // create stream
  obj.initNull();
  str = new FileStream(file, 0, gFalse, 0, &obj);

  ok = setup(ownerPassword, userPassword);
}
#endif
#endif /* 0 */

PDFDoc::PDFDoc(char *fileNameA, GString *ownerPassword,
	       GString *userPassword, PDFCore *coreA) {
#if 0
#ifdef _WIN32
  OSVERSIONINFO version;
#endif
#endif /* 0 */
  Object obj;
#if 0
#ifdef _WIN32
  Unicode u;
  int i, j;
#endif
#endif /* 0 */
  init(coreA);

  fileName = new GString(fileNameA);

#if defined(_WIN32)
#if 0
  wchar_t wPath[winMaxLongPath + 1];
  i = 0;
  j = 0;
  while (j < winMaxLongPath && getUTF8(fileName, &i, &u)) {
    wPath[j++] = (wchar_t)u;
  }
  wPath[j] = L'\0';
  readWindowsShortcut(wPath, winMaxLongPath + 1);
  int wPathLen = (int)wcslen(wPath);

  fileNameU = (wchar_t *)gmallocn(wPathLen + 1, sizeof(wchar_t));
  memcpy(fileNameU, wPath, (wPathLen + 1) * sizeof(wchar_t));

  // NB: _wfopen is only available in NT
  version.dwOSVersionInfoSize = sizeof(version);
  GetVersionEx(&version);
  if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) {
    file = _wfopen(fileNameU, wfopenReadMode);
  } else {
#endif /* 0 */
    file = fopen(fileName->getCString(), fopenReadMode);
#if 0
  }
#endif /* 0 */
#elif defined(VMS)
  file = fopen(fileName->getCString(), fopenReadMode, "ctx=stm");
#else
  file = fopen(fileName->getCString(), fopenReadMode);
#endif

  if (!file) {
    error(errIO, -1, "Couldn't open file '{0:t}'", fileName);
    errCode = errOpenFile;
    return;
  }

  // create stream
  obj.initNull();
  str = new FileStream(file, 0, gFalse, 0, &obj);

  ok = setup(ownerPassword, userPassword);
}

PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
	       GString *userPassword, PDFCore *coreA) {
#ifdef _WIN32
  int n, i;
#endif

  init(coreA);

  if (strA->getFileName()) {
    fileName = strA->getFileName()->copy();
#ifdef _WIN32
    n = fileName->getLength();
    fileNameU = (wchar_t *)gmallocn(n + 1, sizeof(wchar_t));
    for (i = 0; i < n; ++i) {
      fileNameU[i] = (wchar_t)(fileName->getChar(i) & 0xff);
    }
    fileNameU[n] = L'\0';
#endif
  } else {
    fileName = NULL;
#ifdef _WIN32
    fileNameU = NULL;
#endif
  }
  str = strA;
  ok = setup(ownerPassword, userPassword);
}

void PDFDoc::init(PDFCore *coreA) {
  ok = gFalse;
  errCode = errNone;
  core = coreA;
  file = NULL;
  str = NULL;
  xref = NULL;
  catalog = NULL;
#ifndef DISABLE_OUTLINE
  outline = NULL;
#endif
  optContent = NULL;
}

GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {

  str->reset();

  // check header
  checkHeader();

  // read the xref and catalog
  if (!PDFDoc::setup2(ownerPassword, userPassword, gFalse)) {
    if (errCode == errDamaged || errCode == errBadCatalog) {
      // try repairing the xref table
      error(errSyntaxWarning, -1,
	    "PDF file is damaged - attempting to reconstruct xref table...");
      if (!PDFDoc::setup2(ownerPassword, userPassword, gTrue)) {
	return gFalse;
      }
    } else {
      return gFalse;
    }
  }

#ifndef DISABLE_OUTLINE
  // read outline
  outline = new Outline(catalog->getOutline(), xref);
#endif

  // read the optional content info
  optContent = new OptionalContent(this);


  // done
  return gTrue;
}

GBool PDFDoc::setup2(GString *ownerPassword, GString *userPassword,
		     GBool repairXRef) {
  // read xref table
  xref = new XRef(str, repairXRef);
  if (!xref->isOk()) {
    error(errSyntaxError, -1, "Couldn't read xref table");
    errCode = xref->getErrorCode();
    delete xref;
    xref = NULL;
    return gFalse;
  }

  // check for encryption
  if (!checkEncryption(ownerPassword, userPassword)) {
    errCode = errEncrypted;
    delete xref;
    xref = NULL;
    return gFalse;
  }

  // read catalog
  catalog = new Catalog(this);
  if (!catalog->isOk()) {
    error(errSyntaxError, -1, "Couldn't read page catalog");
    errCode = errBadCatalog;
    delete catalog;
    catalog = NULL;
    delete xref;
    xref = NULL;
    return gFalse;
  }

  return gTrue;
}

PDFDoc::~PDFDoc() {
  if (optContent) {
    delete optContent;
  }
#ifndef DISABLE_OUTLINE
  if (outline) {
    delete outline;
  }
#endif
  if (catalog) {
    delete catalog;
  }
  if (xref) {
    delete xref;
  }
  if (str) {
    delete str;
  }
  if (file) {
    fclose(file);
  }
  if (fileName) {
    delete fileName;
  }
#ifdef _WIN32
  if (fileNameU) {
    gfree(fileNameU);
  }
#endif
}

// Check for a PDF header on this stream.  Skip past some garbage
// if necessary.
void PDFDoc::checkHeader() {
  char hdrBuf[headerSearchSize+1];
  char *p;
  int i;

  pdfVersion = 0;
  memset(hdrBuf, 0, headerSearchSize + 1);
  str->getBlock(hdrBuf, headerSearchSize);
  for (i = 0; i < headerSearchSize - 5; ++i) {
    if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
      break;
    }
  }
  if (i >= headerSearchSize - 5) {
    error(errSyntaxWarning, -1, "May not be a PDF file (continuing anyway)");
    return;
  }
  str->moveStart(i);
  if (!(p = strtok(&hdrBuf[i+5], " \t\n\r"))) {
    error(errSyntaxWarning, -1, "May not be a PDF file (continuing anyway)");
    return;
  }
  pdfVersion = atof(p);
  if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
      pdfVersion > supportedPDFVersionNum + 0.0001) {
    error(errSyntaxWarning, -1,
	  "PDF version {0:s} -- xpdf supports version {1:s} (continuing anyway)",
	  p, supportedPDFVersionStr);
  }
}

GBool PDFDoc::checkEncryption(GString *ownerPassword, GString *userPassword) {
  Object encrypt;
  GBool encrypted;
  SecurityHandler *secHdlr;
  GBool ret;

  xref->getTrailerDict()->dictLookup("Encrypt", &encrypt);
  if ((encrypted = encrypt.isDict())) {
    if ((secHdlr = SecurityHandler::make(this, &encrypt))) {
      if (secHdlr->isUnencrypted()) {
	// no encryption
	ret = gTrue;
      } else if (secHdlr->checkEncryption(ownerPassword, userPassword)) {
	// authorization succeeded
       	xref->setEncryption(secHdlr->getPermissionFlags(),
			    secHdlr->getOwnerPasswordOk(),
			    secHdlr->getFileKey(),
			    secHdlr->getFileKeyLength(),
			    secHdlr->getEncVersion(),
			    secHdlr->getEncAlgorithm());
	ret = gTrue;
      } else {
	// authorization failed
	ret = gFalse;
      }
      delete secHdlr;
    } else {
      // couldn't find the matching security handler
      ret = gFalse;
    }
  } else {
    // document is not encrypted
    ret = gTrue;
  }
  encrypt.free();
  return ret;
}

void PDFDoc::displayPage(OutputDev *out, int page,
			 double hDPI, double vDPI, int rotate,
			 GBool useMediaBox, GBool crop, GBool printing,
			 GBool (*abortCheckCbk)(void *data),
			 void *abortCheckCbkData) {
  if (globalParams->getPrintCommands()) {
    printf("***** page %d *****\n", page);
  }
  catalog->getPage(page)->display(out, hDPI, vDPI,
				  rotate, useMediaBox, crop, printing,
				  abortCheckCbk, abortCheckCbkData);
}

void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
			  double hDPI, double vDPI, int rotate,
			  GBool useMediaBox, GBool crop, GBool printing,
			  GBool (*abortCheckCbk)(void *data),
			  void *abortCheckCbkData) {
  int page;

  for (page = firstPage; page <= lastPage; ++page) {
    if (globalParams->getPrintStatusInfo()) {
      fflush(stderr);
      printf("[processing page %d]\n", page);
      fflush(stdout);
    }
    displayPage(out, page, hDPI, vDPI, rotate, useMediaBox, crop, printing,
		abortCheckCbk, abortCheckCbkData);
    catalog->doneWithPage(page);
  }
}

void PDFDoc::displayPageSlice(OutputDev *out, int page,
			      double hDPI, double vDPI, int rotate,
			      GBool useMediaBox, GBool crop, GBool printing,
			      int sliceX, int sliceY, int sliceW, int sliceH,
			      GBool (*abortCheckCbk)(void *data),
			      void *abortCheckCbkData) {
  catalog->getPage(page)->displaySlice(out, hDPI, vDPI,
				       rotate, useMediaBox, crop,
				       sliceX, sliceY, sliceW, sliceH,
				       printing,
				       abortCheckCbk, abortCheckCbkData);
}

Links *PDFDoc::getLinks(int page) {
  return catalog->getPage(page)->getLinks();
}

void PDFDoc::processLinks(OutputDev *out, int page) {
  catalog->getPage(page)->processLinks(out);
}

#ifndef DISABLE_OUTLINE
int PDFDoc::getOutlineTargetPage(OutlineItem *outlineItem) {
  LinkAction *action;
  LinkActionKind kind;
  LinkDest *dest;
  GString *namedDest;
  Ref pageRef;
  int pg;

  if (outlineItem->pageNum >= 0) {
    return outlineItem->pageNum;
  }
  if (!(action = outlineItem->getAction())) {
    outlineItem->pageNum = 0;
    return 0;
  }
  kind = action->getKind();
  if (kind != actionGoTo) {
    outlineItem->pageNum = 0;
    return 0;
  }
  if ((dest = ((LinkGoTo *)action)->getDest())) {
    dest = dest->copy();
  } else if ((namedDest = ((LinkGoTo *)action)->getNamedDest())) {
    dest = findDest(namedDest);
  }
  pg = 0;
  if (dest) {
    if (dest->isPageRef()) {
      pageRef = dest->getPageRef();
      pg = findPage(pageRef.num, pageRef.gen);
    } else {
      pg = dest->getPageNum();
    }
    delete dest;
  }
  outlineItem->pageNum = pg;
  return pg;
}
#endif

GBool PDFDoc::isLinearized() {
  Parser *parser;
  Object obj1, obj2, obj3, obj4, obj5;
  GBool lin;

  lin = gFalse;
  obj1.initNull();
  parser = new Parser(xref,
	     new Lexer(xref,
	       str->makeSubStream(str->getStart(), gFalse, 0, &obj1)),
	     gTrue);
  parser->getObj(&obj1);
  parser->getObj(&obj2);
  parser->getObj(&obj3);
  parser->getObj(&obj4);
  if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
      obj4.isDict()) {
    obj4.dictLookup("Linearized", &obj5);
    if (obj5.isNum() && obj5.getNum() > 0) {
      lin = gTrue;
    }
    obj5.free();
  }
  obj4.free();
  obj3.free();
  obj2.free();
  obj1.free();
  delete parser;
  return lin;
}

GBool PDFDoc::saveAs(GString *name) {
  FILE *f;
  char buf[4096];
  int n;

  if (!(f = fopen(name->getCString(), "wb"))) {
    error(errIO, -1, "Couldn't open file '{0:t}'", name);
    return gFalse;
  }
  str->reset();
  while ((n = str->getBlock(buf, sizeof(buf))) > 0) {
    fwrite(buf, 1, n, f);
  }
  str->close();
  fclose(f);
  return gTrue;
}

GBool PDFDoc::saveEmbeddedFile(int idx, const char *path) {
  FILE *f;
  GBool ret;

  if (!(f = fopen(path, "wb"))) {
    return gFalse;
  }
  ret = saveEmbeddedFile2(idx, f);
  fclose(f);
  return ret;
}

GBool PDFDoc::saveEmbeddedFileU(int idx, const char *path) {
  FILE *f;
  GBool ret;

  if (!(f = openFile(path, "wb"))) {
    return gFalse;
  }
  ret = saveEmbeddedFile2(idx, f);
  fclose(f);
  return ret;
}

#ifdef _WIN32
GBool PDFDoc::saveEmbeddedFile(int idx, const wchar_t *path, int pathLen) {
  FILE *f;
  OSVERSIONINFO version;
  wchar_t path2w[winMaxLongPath + 1];
  char path2c[MAX_PATH + 1];
  int i;
  GBool ret;

  // NB: _wfopen is only available in NT
#if 0
  version.dwOSVersionInfoSize = sizeof(version);
  GetVersionEx(&version);
  if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) {
    for (i = 0; i < pathLen && i < winMaxLongPath; ++i) {
      path2w[i] = path[i];
    }
    path2w[i] = 0;
    f = _wfopen(path2w, L"wb");
  } else {
#endif /* 0 */
    for (i = 0; i < pathLen && i < MAX_PATH; ++i) {
      path2c[i] = (char)path[i];
    }
    path2c[i] = 0;
    f = fopen(path2c, "wb");
#if 0
  }
#endif /* 0 */
  if (!f) {
    return gFalse;
  }
  ret = saveEmbeddedFile2(idx, f);
  fclose(f);
  return ret;
}
#endif

GBool PDFDoc::saveEmbeddedFile2(int idx, FILE *f) {
  Object strObj;
  char buf[4096];
  int n;

  if (!catalog->getEmbeddedFileStreamObj(idx, &strObj)) {
    return gFalse;
  }
  strObj.streamReset();
  while ((n = strObj.streamGetBlock(buf, sizeof(buf))) > 0) {
    fwrite(buf, 1, n, f);
  }
  strObj.streamClose();
  strObj.free();
  return gTrue;
}

char *PDFDoc::getEmbeddedFileMem(int idx, int *size) {
  Object strObj;
  char *buf;
  int bufSize, sizeInc, n;

  if (!catalog->getEmbeddedFileStreamObj(idx, &strObj)) {
    return NULL;
  }
  strObj.streamReset();
  bufSize = 0;
  buf = NULL;
  do {
    sizeInc = bufSize ? bufSize : 1024;
    if (bufSize > INT_MAX - sizeInc) {
      error(errIO, -1, "embedded file is too large");
      *size = 0;
      return NULL;
    }
    buf = (char *)grealloc(buf, bufSize + sizeInc);
    n = strObj.streamGetBlock(buf + bufSize, sizeInc);
    bufSize += n;
  } while (n == sizeInc);
  strObj.streamClose();
  strObj.free();
  *size = bufSize;
  return buf;
}