Back to index

tetex-bin  3.0
XRef.h
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // XRef.h
00004 //
00005 // Copyright 1996-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #ifndef XREF_H
00010 #define XREF_H
00011 
00012 #include <aconf.h>
00013 
00014 #ifdef USE_GCC_PRAGMAS
00015 #pragma interface
00016 #endif
00017 
00018 #include "gtypes.h"
00019 #include "Object.h"
00020 
00021 class Dict;
00022 class Stream;
00023 class Parser;
00024 class ObjectStream;
00025 
00026 //------------------------------------------------------------------------
00027 // XRef
00028 //------------------------------------------------------------------------
00029 
00030 enum XRefEntryType {
00031   xrefEntryFree,
00032   xrefEntryUncompressed,
00033   xrefEntryCompressed
00034 };
00035 
00036 struct XRefEntry {
00037   Guint offset;
00038   int gen;
00039   XRefEntryType type;
00040 };
00041 
00042 class XRef {
00043 public:
00044 
00045   // Constructor.  Read xref table from stream.
00046   XRef(BaseStream *strA, GString *ownerPassword, GString *userPassword);
00047 
00048   // Destructor.
00049   ~XRef();
00050 
00051   // Is xref table valid?
00052   GBool isOk() { return ok; }
00053 
00054   // Get the error code (if isOk() returns false).
00055   int getErrorCode() { return errCode; }
00056 
00057   // Is the file encrypted?
00058 #ifndef NO_DECRYPTION
00059   GBool isEncrypted() { return encrypted; }
00060 #else
00061   GBool isEncrypted() { return gFalse; }
00062 #endif
00063 
00064   // Check various permissions.
00065   GBool okToPrint(GBool ignoreOwnerPW = gFalse);
00066   GBool okToChange(GBool ignoreOwnerPW = gFalse);
00067   GBool okToCopy(GBool ignoreOwnerPW = gFalse);
00068   GBool okToAddNotes(GBool ignoreOwnerPW = gFalse);
00069 
00070   // Get catalog object.
00071   Object *getCatalog(Object *obj) { return fetch(rootNum, rootGen, obj); }
00072 
00073   // Fetch an indirect reference.
00074   Object *fetch(int num, int gen, Object *obj);
00075 
00076   // Return the document's Info dictionary (if any).
00077   Object *getDocInfo(Object *obj);
00078   Object *getDocInfoNF(Object *obj);
00079 
00080   // Return the number of objects in the xref table.
00081   int getNumObjects() { return size; }
00082 
00083   // Return the offset of the last xref table.
00084   Guint getLastXRefPos() { return lastXRefPos; }
00085 
00086   // Return the catalog object reference.
00087   int getRootNum() { return rootNum; }
00088   int getRootGen() { return rootGen; }
00089 
00090   // Get end position for a stream in a damaged file.
00091   // Returns false if unknown or file is not damaged.
00092   GBool getStreamEnd(Guint streamStart, Guint *streamEnd);
00093 
00094   // Direct access.
00095   int getSize() { return size; }
00096   XRefEntry *getEntry(int i) { return &entries[i]; }
00097   Object *getTrailerDict() { return &trailerDict; }
00098 
00099 private:
00100 
00101   BaseStream *str;          // input stream
00102   Guint start;                     // offset in file (to allow for garbage
00103                             //   at beginning of file)
00104   XRefEntry *entries;              // xref entries
00105   int size;                 // size of <entries> array
00106   int rootNum, rootGen;            // catalog dict
00107   GBool ok;                 // true if xref table is valid
00108   int errCode;                     // error code (if <ok> is false)
00109   Object trailerDict;              // trailer dictionary
00110   Guint lastXRefPos;        // offset of last xref table
00111   Guint *streamEnds;        // 'endstream' positions - only used in
00112                             //   damaged files
00113   int streamEndsLen;        // number of valid entries in streamEnds
00114   ObjectStream *objStr;            // cached object stream
00115 #ifndef NO_DECRYPTION
00116   GBool encrypted;          // true if file is encrypted
00117   int encVersion;           // encryption algorithm
00118   int encRevision;          // security handler revision
00119   int keyLength;            // length of key, in bytes
00120   int permFlags;            // permission bits
00121   Guchar fileKey[16];              // file decryption key
00122   GBool ownerPasswordOk;    // true if owner password is correct
00123 #endif
00124 
00125   Guint getStartXref();
00126   GBool readXRef(Guint *pos);
00127   GBool readXRefTable(Parser *parser, Guint *pos);
00128   GBool readXRefStreamSection(Stream *xrefStr, int *w, int first, int n);
00129   GBool readXRefStream(Stream *xrefStr, Guint *pos);
00130   GBool constructXRef();
00131   GBool checkEncrypted(GString *ownerPassword, GString *userPassword);
00132   Guint strToUnsigned(char *s);
00133 };
00134 
00135 #endif