1 //========================================================================
2 //
3 // XRef.h
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 #ifndef XREF_H
10 #define XREF_H
11 
12 #include <aconf.h>
13 
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
17 
18 #include "gtypes.h"
19 #include "gfile.h"
20 #include "Object.h"
21 
22 class Dict;
23 class Stream;
24 class Parser;
25 class ObjectStream;
26 class XRefPosSet;
27 
28 //------------------------------------------------------------------------
29 // XRef
30 //------------------------------------------------------------------------
31 
32 enum XRefEntryType {
33   xrefEntryFree,
34   xrefEntryUncompressed,
35   xrefEntryCompressed
36 };
37 
38 struct XRefEntry {
39   GFileOffset offset;
40   int gen;
41   XRefEntryType type;
42 };
43 
44 struct XRefCacheEntry {
45   int num;
46   int gen;
47   Object obj;
48 };
49 
50 #define xrefCacheSize 16
51 
52 #define objStrCacheSize 4
53 
54 class XRef {
55 public:
56 
57   // Constructor.  Read xref table from stream.
58   XRef(BaseStream *strA, GBool repair);
59 
60   // Destructor.
61   ~XRef();
62 
63   // Is xref table valid?
isOk()64   GBool isOk() { return ok; }
65 
66   // Get the error code (if isOk() returns false).
getErrorCode()67   int getErrorCode() { return errCode; }
68 
69   // Set the encryption parameters.
70   void setEncryption(int permFlagsA, GBool ownerPasswordOkA,
71 		     Guchar *fileKeyA, int keyLengthA, int encVersionA,
72 		     CryptAlgorithm encAlgorithmA);
73 
74   // Is the file encrypted?
isEncrypted()75   GBool isEncrypted() { return encrypted; }
76 
77   // Check various permissions.
78   GBool okToPrint(GBool ignoreOwnerPW = gFalse);
79   GBool okToChange(GBool ignoreOwnerPW = gFalse);
80   GBool okToCopy(GBool ignoreOwnerPW = gFalse);
81   GBool okToAddNotes(GBool ignoreOwnerPW = gFalse);
getPermFlags()82   int getPermFlags() { return permFlags; }
83 
84   // Get catalog object.
getCatalog(Object * obj)85   Object *getCatalog(Object *obj) { return fetch(rootNum, rootGen, obj); }
86 
87   // Fetch an indirect reference.
88   Object *fetch(int num, int gen, Object *obj, int recursion = 0);
89 
90   // Return the document's Info dictionary (if any).
91   Object *getDocInfo(Object *obj);
92   Object *getDocInfoNF(Object *obj);
93 
94   // Return the number of objects in the xref table.
getNumObjects()95   int getNumObjects() { return last + 1; }
96 
97   // Return the offset of the last xref table.
getLastXRefPos()98   GFileOffset getLastXRefPos() { return lastXRefPos; }
99 
100   // Return the catalog object reference.
getRootNum()101   int getRootNum() { return rootNum; }
getRootGen()102   int getRootGen() { return rootGen; }
103 
104   // Get end position for a stream in a damaged file.
105   // Returns false if unknown or file is not damaged.
106   GBool getStreamEnd(GFileOffset streamStart, GFileOffset *streamEnd);
107 
108   // Direct access.
getSize()109   int getSize() { return size; }
getEntry(int i)110   XRefEntry *getEntry(int i) { return &entries[i]; }
getTrailerDict()111   Object *getTrailerDict() { return &trailerDict; }
112 
113 private:
114 
115   BaseStream *str;		// input stream
116   GFileOffset start;		// offset in file (to allow for garbage
117 				//   at beginning of file)
118   XRefEntry *entries;		// xref entries
119   int size;			// size of <entries> array
120   int last;			// last used index in <entries>
121   int rootNum, rootGen;		// catalog dict
122   GBool ok;			// true if xref table is valid
123   int errCode;			// error code (if <ok> is false)
124   Object trailerDict;		// trailer dictionary
125   GFileOffset lastXRefPos;	// offset of last xref table
126   GFileOffset *streamEnds;	// 'endstream' positions - only used in
127 				//   damaged files
128   int streamEndsLen;		// number of valid entries in streamEnds
129   ObjectStream *		// cached object streams
130     objStrs[objStrCacheSize];
131   GBool encrypted;		// true if file is encrypted
132   int permFlags;		// permission bits
133   GBool ownerPasswordOk;	// true if owner password is correct
134   Guchar fileKey[32];		// file decryption key
135   int keyLength;		// length of key, in bytes
136   int encVersion;		// encryption version
137   CryptAlgorithm encAlgorithm;	// encryption algorithm
138   XRefCacheEntry		// cache of recently accessed objects
139     cache[xrefCacheSize];
140 
141   GFileOffset getStartXref();
142   GBool readXRef(GFileOffset *pos, XRefPosSet *posSet);
143   GBool readXRefTable(GFileOffset *pos, int offset, XRefPosSet *posSet);
144   GBool readXRefStreamSection(Stream *xrefStr, int *w, int first, int n);
145   GBool readXRefStream(Stream *xrefStr, GFileOffset *pos);
146   GBool constructXRef();
147   ObjectStream *getObjectStream(int objStrNum);
148   GFileOffset strToFileOffset(char *s);
149 };
150 
151 #endif
152