bec487a114756674f828afd31ff071b2b7f0fc16
[swftools.git] / pdf2swf / xpdf / XRef.h
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 "Object.h"
20
21 class Dict;
22 class Stream;
23 class Parser;
24 class ObjectStream;
25
26 //------------------------------------------------------------------------
27 // XRef
28 //------------------------------------------------------------------------
29
30 enum XRefEntryType {
31   xrefEntryFree,
32   xrefEntryUncompressed,
33   xrefEntryCompressed
34 };
35
36 struct XRefEntry {
37   Guint offset;
38   int gen;
39   XRefEntryType type;
40 };
41
42 class XRef {
43 public:
44
45   // Constructor.  Read xref table from stream.
46   XRef(BaseStream *strA, GString *ownerPassword, GString *userPassword);
47
48   // Destructor.
49   ~XRef();
50
51   // Is xref table valid?
52   GBool isOk() { return ok; }
53
54   // Get the error code (if isOk() returns false).
55   int getErrorCode() { return errCode; }
56
57   // Is the file encrypted?
58 #ifndef NO_DECRYPTION
59   GBool isEncrypted() { return encrypted; }
60 #else
61   GBool isEncrypted() { return gFalse; }
62 #endif
63
64   // Check various permissions.
65   GBool okToPrint(GBool ignoreOwnerPW = gFalse);
66   GBool okToChange(GBool ignoreOwnerPW = gFalse);
67   GBool okToCopy(GBool ignoreOwnerPW = gFalse);
68   GBool okToAddNotes(GBool ignoreOwnerPW = gFalse);
69
70   // Get catalog object.
71   Object *getCatalog(Object *obj) { return fetch(rootNum, rootGen, obj); }
72
73   // Fetch an indirect reference.
74   Object *fetch(int num, int gen, Object *obj);
75
76   // Return the document's Info dictionary (if any).
77   Object *getDocInfo(Object *obj);
78   Object *getDocInfoNF(Object *obj);
79
80   // Return the number of objects in the xref table.
81   int getNumObjects() { return size; }
82
83   // Return the offset of the last xref table.
84   Guint getLastXRefPos() { return lastXRefPos; }
85
86   // Return the catalog object reference.
87   int getRootNum() { return rootNum; }
88   int getRootGen() { return rootGen; }
89
90   // Get end position for a stream in a damaged file.
91   // Returns false if unknown or file is not damaged.
92   GBool getStreamEnd(Guint streamStart, Guint *streamEnd);
93
94   // Direct access.
95   int getSize() { return size; }
96   XRefEntry *getEntry(int i) { return &entries[i]; }
97   Object *getTrailerDict() { return &trailerDict; }
98
99 private:
100
101   BaseStream *str;              // input stream
102   Guint start;                  // offset in file (to allow for garbage
103                                 //   at beginning of file)
104   XRefEntry *entries;           // xref entries
105   int size;                     // size of <entries> array
106   int rootNum, rootGen;         // catalog dict
107   GBool ok;                     // true if xref table is valid
108   int errCode;                  // error code (if <ok> is false)
109   Object trailerDict;           // trailer dictionary
110   Guint lastXRefPos;            // offset of last xref table
111   Guint *streamEnds;            // 'endstream' positions - only used in
112                                 //   damaged files
113   int streamEndsLen;            // number of valid entries in streamEnds
114   ObjectStream *objStr;         // cached object stream
115 #ifndef NO_DECRYPTION
116   GBool encrypted;              // true if file is encrypted
117   int encVersion;               // encryption algorithm
118   int encRevision;              // security handler revision
119   int keyLength;                // length of key, in bytes
120   int permFlags;                // permission bits
121   Guchar fileKey[16];           // file decryption key
122   GBool ownerPasswordOk;        // true if owner password is correct
123 #endif
124
125   Guint getStartXref();
126   GBool readXRef(Guint *pos);
127   GBool readXRefTable(Parser *parser, Guint *pos);
128   GBool readXRefStreamSection(Stream *xrefStr, int *w, int first, int n);
129   GBool readXRefStream(Stream *xrefStr, Guint *pos);
130   GBool constructXRef();
131   GBool checkEncrypted(GString *ownerPassword, GString *userPassword);
132   Guint strToUnsigned(char *s);
133 };
134
135 #endif