Coverage Report

Created: 2022-03-21 07:20

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