7876fa6c5807473a69ad69e9c181fcd14d499abd
[swftools.git] / pdf2swf / xpdf / XRef.h
1 //========================================================================
2 //
3 // XRef.h
4 //
5 // Copyright 1996-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef XREF_H
10 #define XREF_H
11
12 #ifdef __GNUC__
13 #pragma interface
14 #endif
15
16 #include "gtypes.h"
17 #include "Object.h"
18
19 class Dict;
20 class Stream;
21
22 //------------------------------------------------------------------------
23 // XRef
24 //------------------------------------------------------------------------
25
26 struct XRefEntry {
27   Guint offset;
28   int gen;
29   GBool used;
30 };
31
32 class XRef {
33 public:
34
35   // Constructor.  Read xref table from stream.
36   XRef(BaseStream *strA, GString *ownerPassword, GString *userPassword);
37
38   // Destructor.
39   ~XRef();
40
41   // Is xref table valid?
42   GBool isOk() { return ok; }
43
44   // Get the error code (if isOk() returns false).
45   int getErrorCode() { return errCode; }
46
47   // Is the file encrypted?
48 #ifndef NO_DECRYPTION
49   GBool isEncrypted() { return encrypted; }
50 #else
51   GBool isEncrypted() { return gFalse; }
52 #endif
53
54   // Check various permissions.
55   GBool okToPrint(GBool ignoreOwnerPW = gFalse);
56   GBool okToChange(GBool ignoreOwnerPW = gFalse);
57   GBool okToCopy(GBool ignoreOwnerPW = gFalse);
58   GBool okToAddNotes(GBool ignoreOwnerPW = gFalse);
59
60   // Get catalog object.
61   Object *getCatalog(Object *obj) { return fetch(rootNum, rootGen, obj); }
62
63   // Fetch an indirect reference.
64   Object *fetch(int num, int gen, Object *obj);
65
66   // Return the document's Info dictionary (if any).
67   Object *getDocInfo(Object *obj);
68   Object *getDocInfoNF(Object *obj);
69
70   // Return the number of objects in the xref table.
71   int getNumObjects() { return size; }
72
73   // Return the offset of the last xref table.
74   Guint getLastXRefPos() { return lastXRefPos; }
75
76   // Return the catalog object reference.
77   int getRootNum() { return rootNum; }
78   int getRootGen() { return rootGen; }
79
80   // Get end position for a stream in a damaged file.
81   // Returns false if unknown or file is not damaged.
82   GBool getStreamEnd(Guint streamStart, Guint *streamEnd);
83
84 private:
85
86   BaseStream *str;              // input stream
87   Guint start;                  // offset in file (to allow for garbage
88                                 //   at beginning of file)
89   XRefEntry *entries;           // xref entries
90   int size;                     // size of <entries> array
91   int rootNum, rootGen;         // catalog dict
92   GBool ok;                     // true if xref table is valid
93   int errCode;                  // error code (if <ok> is false)
94   Object trailerDict;           // trailer dictionary
95   Guint lastXRefPos;            // offset of last xref table
96   Guint *streamEnds;            // 'endstream' positions - only used in
97                                 //   damaged files
98   int streamEndsLen;            // number of valid entries in streamEnds
99 #ifndef NO_DECRYPTION
100   GBool encrypted;              // true if file is encrypted
101   int encVersion;               // encryption algorithm
102   int encRevision;              // security handler revision
103   int keyLength;                // length of key, in bytes
104   int permFlags;                // permission bits
105   Guchar fileKey[16];           // file decryption key
106   GBool ownerPasswordOk;        // true if owner password is correct
107 #endif
108
109   Guint readTrailer();
110   GBool readXRef(Guint *pos);
111   GBool constructXRef();
112   GBool checkEncrypted(GString *ownerPassword, GString *userPassword);
113   Guint strToUnsigned(char *s);
114 };
115
116 #endif