Coverage Report

Created: 2025-08-29 06:21

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