Coverage Report

Created: 2023-09-25 06:41

/src/xpdf-4.04/xpdf/XRef.h
Line
Count
Source (jump to first uncovered line)
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
#if MULTITHREADED
22
#include "GMutex.h"
23
#endif
24
25
class Dict;
26
class Stream;
27
class Parser;
28
class ObjectStream;
29
class XRefPosSet;
30
31
//------------------------------------------------------------------------
32
// XRef
33
//------------------------------------------------------------------------
34
35
enum XRefEntryType {
36
  xrefEntryFree,
37
  xrefEntryUncompressed,
38
  xrefEntryCompressed
39
};
40
41
struct XRefEntry {
42
  GFileOffset offset;
43
  int gen;
44
  XRefEntryType type;
45
};
46
47
struct XRefCacheEntry {
48
  int num;
49
  int gen;
50
  Object obj;
51
};
52
53
279k
#define xrefCacheSize 16
54
55
18.5k
#define objStrCacheSize 128
56
0
#define objStrCacheTimeout 1000
57
58
class XRef {
59
public:
60
61
  // Constructor.  Read xref table from stream.
62
  XRef(BaseStream *strA, GBool repair);
63
64
  // Destructor.
65
  ~XRef();
66
67
  // Is xref table valid?
68
72
  GBool isOk() { return ok; }
69
70
  // Get the error code (if isOk() returns false).
71
69
  int getErrorCode() { return errCode; }
72
73
  // Was the xref constructed by the repair code?
74
  GBool isRepaired() { return repaired; }
75
76
  // Set the encryption parameters.
77
  void setEncryption(int permFlagsA, GBool ownerPasswordOkA,
78
         Guchar *fileKeyA, int keyLengthA, int encVersionA,
79
         CryptAlgorithm encAlgorithmA);
80
81
  // Is the file encrypted?
82
  GBool isEncrypted() { return encrypted; }
83
  GBool getEncryption(int *permFlagsA, GBool *ownerPasswordOkA,
84
          int *keyLengthA, int *encVersionA,
85
          CryptAlgorithm *encAlgorithmA);
86
87
  // Check various permissions.
88
  GBool okToPrint(GBool ignoreOwnerPW = gFalse);
89
  GBool okToChange(GBool ignoreOwnerPW = gFalse);
90
  GBool okToCopy(GBool ignoreOwnerPW = gFalse);
91
  GBool okToAddNotes(GBool ignoreOwnerPW = gFalse);
92
  int getPermFlags() { return permFlags; }
93
94
  // Get catalog object.
95
3
  Object *getCatalog(Object *obj) { return fetch(rootNum, rootGen, obj); }
96
97
  // Fetch an indirect reference.
98
  Object *fetch(int num, int gen, Object *obj, int recursion = 0);
99
100
  // Return the document's Info dictionary (if any).
101
  Object *getDocInfo(Object *obj);
102
  Object *getDocInfoNF(Object *obj);
103
104
  // Return the number of objects in the xref table.
105
0
  int getNumObjects() { return last + 1; }
106
107
  // Return the offset of the last xref table.
108
  GFileOffset getLastXRefPos() { return lastXRefPos; }
109
110
  // Return the offset of the 'startxref' at the end of the file.
111
  GFileOffset getLastStartxrefPos() { return lastStartxrefPos; }
112
113
  // Return the catalog object reference.
114
  int getRootNum() { return rootNum; }
115
  int getRootGen() { return rootGen; }
116
117
  // Get the xref table positions.
118
  int getNumXRefTables() { return xrefTablePosLen; }
119
  GFileOffset getXRefTablePos(int idx) { return xrefTablePos[idx]; }
120
121
  // Get end position for a stream in a damaged file.
122
  // Returns false if unknown or file is not damaged.
123
  GBool getStreamEnd(GFileOffset streamStart, GFileOffset *streamEnd);
124
125
  // Direct access.
126
  int getSize() { return size; }
127
  XRefEntry *getEntry(int i) { return &entries[i]; }
128
3
  Object *getTrailerDict() { return &trailerDict; }
129
130
private:
131
132
  BaseStream *str;    // input stream
133
  GFileOffset start;    // offset in file (to allow for garbage
134
        //   at beginning of file)
135
  XRefEntry *entries;   // xref entries
136
  int size;     // size of <entries> array
137
  int last;     // last used index in <entries>
138
  int rootNum, rootGen;   // catalog dict
139
  GBool ok;     // true if xref table is valid
140
  int errCode;      // error code (if <ok> is false)
141
  GBool repaired;   // set if the xref table was constructed by
142
        //   the repair code
143
  Object trailerDict;   // trailer dictionary
144
  GFileOffset lastXRefPos;  // offset of last xref table
145
  GFileOffset lastStartxrefPos; // offset of 'startxref' at end of file
146
  GFileOffset *xrefTablePos;  // positions of all xref tables
147
  int xrefTablePosLen;    // number of xref table positions
148
  GFileOffset *streamEnds;  // 'endstream' positions - only used in
149
        //   damaged files
150
  int streamEndsLen;    // number of valid entries in streamEnds
151
  ObjectStream *    // cached object streams
152
    objStrs[objStrCacheSize];
153
  int objStrCacheLength;  // number of valid entries in objStrs[]
154
  Guint       // time of last use for each obj stream
155
    objStrLastUse[objStrCacheSize];
156
  Guint objStrTime;   // current time for the obj stream cache
157
#if MULTITHREADED
158
  GMutex objStrsMutex;
159
#endif
160
  GBool encrypted;    // true if file is encrypted
161
  int permFlags;    // permission bits
162
  GBool ownerPasswordOk;  // true if owner password is correct
163
  Guchar fileKey[32];   // file decryption key
164
  int keyLength;    // length of key, in bytes
165
  int encVersion;   // encryption version
166
  CryptAlgorithm encAlgorithm;  // encryption algorithm
167
  XRefCacheEntry    // cache of recently accessed objects
168
    cache[xrefCacheSize];
169
#if MULTITHREADED
170
  GMutex cacheMutex;
171
#endif
172
173
  GFileOffset getStartXref();
174
  GBool readXRef(GFileOffset *pos, XRefPosSet *posSet, GBool hybrid);
175
  GBool readXRefTable(GFileOffset *pos, int offset, XRefPosSet *posSet);
176
  GBool readXRefStream(Stream *xrefStr, GFileOffset *pos, GBool hybrid);
177
  GBool readXRefStreamSection(Stream *xrefStr, int *w, int first, int n);
178
  GBool constructXRef();
179
  void constructTrailerDict(GFileOffset pos);
180
  void saveTrailerDict(Dict *dict, GBool isXRefStream);
181
  char *constructObjectEntry(char *p, GFileOffset pos, int *objNum);
182
  void constructObjectStreamEntries(Object *objStr, int objStrObjNum);
183
  GBool constructXRefEntry(int num, int gen, GFileOffset pos,
184
         XRefEntryType type);
185
  GBool getObjectStreamObject(int objStrNum, int objIdx,
186
            int objNum, Object *obj);
187
  ObjectStream *getObjectStream(int objStrNum);
188
  void cleanObjectStreamCache();
189
  GFileOffset strToFileOffset(char *s);
190
};
191
192
#endif