Coverage Report

Created: 2026-07-04 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/fofi/FoFiBase.cc
Line
Count
Source
1
//========================================================================
2
//
3
// FoFiBase.cc
4
//
5
// Copyright 1999-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdio.h>
12
#include <limits.h>
13
#include "gmem.h"
14
#include "gmempp.h"
15
#include "FoFiBase.h"
16
17
//------------------------------------------------------------------------
18
// FoFiBase
19
//------------------------------------------------------------------------
20
21
17.5k
FoFiBase::FoFiBase(char *fileA, int lenA, GBool freeFileDataA) {
22
17.5k
  fileData = file = (Guchar *)fileA;
23
17.5k
  len = lenA;
24
17.5k
  freeFileData = freeFileDataA;
25
17.5k
}
26
27
17.5k
FoFiBase::~FoFiBase() {
28
17.5k
  if (freeFileData) {
29
10.9k
    gfree(fileData);
30
10.9k
  }
31
17.5k
}
32
33
9.37k
char *FoFiBase::readFile(char *fileName, int *fileLen) {
34
9.37k
  FILE *f;
35
9.37k
  char *buf;
36
9.37k
  int n;
37
38
9.37k
  if (!(f = fopen(fileName, "rb"))) {
39
0
    return NULL;
40
0
  }
41
9.37k
  fseek(f, 0, SEEK_END);
42
9.37k
  n = (int)ftell(f);
43
9.37k
  if (n < 0) {
44
0
    fclose(f);
45
0
    return NULL;
46
0
  }
47
9.37k
  fseek(f, 0, SEEK_SET);
48
9.37k
  buf = (char *)gmalloc(n);
49
9.37k
  if ((int)fread(buf, 1, n, f) != n) {
50
0
    gfree(buf);
51
0
    fclose(f);
52
0
    return NULL;
53
0
  }
54
9.37k
  fclose(f);
55
9.37k
  *fileLen = n;
56
9.37k
  return buf;
57
9.37k
}
58
59
0
int FoFiBase::getS8(int pos, GBool *ok) {
60
0
  int x;
61
62
0
  if (pos < 0 || pos >= len) {
63
0
    *ok = gFalse;
64
0
    return 0;
65
0
  }
66
0
  x = file[pos];
67
0
  if (x & 0x80) {
68
0
    x |= ~0xff;
69
0
  }
70
0
  return x;
71
0
}
72
73
378M
int FoFiBase::getU8(int pos, GBool *ok) {
74
378M
  if (pos < 0 || pos >= len) {
75
286M
    *ok = gFalse;
76
286M
    return 0;
77
286M
  }
78
92.0M
  return file[pos];
79
378M
}
80
81
31.0k
int FoFiBase::getS16BE(int pos, GBool *ok) {
82
31.0k
  int x;
83
84
31.0k
  if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) {
85
15
    *ok = gFalse;
86
15
    return 0;
87
15
  }
88
31.0k
  x = file[pos];
89
31.0k
  x = (x << 8) + file[pos+1];
90
31.0k
  if (x & 0x8000) {
91
9.93k
    x |= ~0xffff;
92
9.93k
  }
93
31.0k
  return x;
94
31.0k
}
95
96
10.5M
int FoFiBase::getU16BE(int pos, GBool *ok) {
97
10.5M
  int x;
98
99
10.5M
  if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) {
100
761k
    *ok = gFalse;
101
761k
    return 0;
102
761k
  }
103
9.77M
  x = file[pos];
104
9.77M
  x = (x << 8) + file[pos+1];
105
9.77M
  return x;
106
10.5M
}
107
108
0
int FoFiBase::getS32BE(int pos, GBool *ok) {
109
0
  int x;
110
111
0
  if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) {
112
0
    *ok = gFalse;
113
0
    return 0;
114
0
  }
115
0
  x = file[pos];
116
0
  x = (x << 8) + file[pos+1];
117
0
  x = (x << 8) + file[pos+2];
118
0
  x = (x << 8) + file[pos+3];
119
0
  if (x & 0x80000000) {
120
0
    x |= ~0xffffffff;
121
0
  }
122
0
  return x;
123
0
}
124
125
6.15M
Guint FoFiBase::getU32BE(int pos, GBool *ok) {
126
6.15M
  Guint x;
127
128
6.15M
  if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) {
129
376k
    *ok = gFalse;
130
376k
    return 0;
131
376k
  }
132
5.78M
  x = file[pos];
133
5.78M
  x = (x << 8) + file[pos+1];
134
5.78M
  x = (x << 8) + file[pos+2];
135
5.78M
  x = (x << 8) + file[pos+3];
136
5.78M
  return x;
137
6.15M
}
138
139
1.61k
Guint FoFiBase::getU32LE(int pos, GBool *ok) {
140
1.61k
  Guint x;
141
142
1.61k
  if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) {
143
0
    *ok = gFalse;
144
0
    return 0;
145
0
  }
146
1.61k
  x = file[pos+3];
147
1.61k
  x = (x << 8) + file[pos+2];
148
1.61k
  x = (x << 8) + file[pos+1];
149
1.61k
  x = (x << 8) + file[pos];
150
1.61k
  return x;
151
1.61k
}
152
153
1.34M
Guint FoFiBase::getUVarBE(int pos, int size, GBool *ok) {
154
1.34M
  Guint x;
155
1.34M
  int i;
156
157
1.34M
  if (pos < 0 || pos > INT_MAX - size || pos + size > len) {
158
10.2k
    *ok = gFalse;
159
10.2k
    return 0;
160
10.2k
  }
161
1.33M
  x = 0;
162
5.84M
  for (i = 0; i < size; ++i) {
163
4.50M
    x = (x << 8) + file[pos + i];
164
4.50M
  }
165
1.33M
  return x;
166
1.34M
}
167
168
844k
GBool FoFiBase::checkRegion(int pos, int size) {
169
844k
  return pos >= 0 &&
170
844k
         size >= 0 &&
171
844k
         size <= INT_MAX - pos &&
172
798k
         pos + size <= len;
173
844k
}