Coverage Report

Created: 2026-10-17 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
11.9k
FoFiBase::FoFiBase(char *fileA, int lenA, GBool freeFileDataA) {
22
11.9k
  fileData = file = (Guchar *)fileA;
23
11.9k
  len = lenA;
24
11.9k
  freeFileData = freeFileDataA;
25
11.9k
}
26
27
11.9k
FoFiBase::~FoFiBase() {
28
11.9k
  if (freeFileData) {
29
8.31k
    gfree(fileData);
30
8.31k
  }
31
11.9k
}
32
33
8.31k
char *FoFiBase::readFile(char *fileName, int *fileLen) {
34
8.31k
  FILE *f;
35
8.31k
  char *buf;
36
8.31k
  int n;
37
38
8.31k
  if (!(f = fopen(fileName, "rb"))) {
39
0
    return NULL;
40
0
  }
41
8.31k
  fseek(f, 0, SEEK_END);
42
8.31k
  n = (int)ftell(f);
43
8.31k
  if (n < 0) {
44
0
    fclose(f);
45
0
    return NULL;
46
0
  }
47
8.31k
  fseek(f, 0, SEEK_SET);
48
8.31k
  buf = (char *)gmalloc(n);
49
8.31k
  if ((int)fread(buf, 1, n, f) != n) {
50
0
    gfree(buf);
51
0
    fclose(f);
52
0
    return NULL;
53
0
  }
54
8.31k
  fclose(f);
55
8.31k
  *fileLen = n;
56
8.31k
  return buf;
57
8.31k
}
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
686M
int FoFiBase::getU8(int pos, GBool *ok) {
74
686M
  if (pos < 0 || pos >= len) {
75
538M
    *ok = gFalse;
76
538M
    return 0;
77
538M
  }
78
148M
  return file[pos];
79
686M
}
80
81
32.1k
int FoFiBase::getS16BE(int pos, GBool *ok) {
82
32.1k
  int x;
83
84
32.1k
  if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) {
85
30
    *ok = gFalse;
86
30
    return 0;
87
30
  }
88
32.1k
  x = file[pos];
89
32.1k
  x = (x << 8) + file[pos+1];
90
32.1k
  if (x & 0x8000) {
91
9.89k
    x |= ~0xffff;
92
9.89k
  }
93
32.1k
  return x;
94
32.1k
}
95
96
13.4M
int FoFiBase::getU16BE(int pos, GBool *ok) {
97
13.4M
  int x;
98
99
13.4M
  if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) {
100
1.45M
    *ok = gFalse;
101
1.45M
    return 0;
102
1.45M
  }
103
12.0M
  x = file[pos];
104
12.0M
  x = (x << 8) + file[pos+1];
105
12.0M
  return x;
106
13.4M
}
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
9.70M
Guint FoFiBase::getU32BE(int pos, GBool *ok) {
126
9.70M
  Guint x;
127
128
9.70M
  if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) {
129
1.14M
    *ok = gFalse;
130
1.14M
    return 0;
131
1.14M
  }
132
8.55M
  x = file[pos];
133
8.55M
  x = (x << 8) + file[pos+1];
134
8.55M
  x = (x << 8) + file[pos+2];
135
8.55M
  x = (x << 8) + file[pos+3];
136
8.55M
  return x;
137
9.70M
}
138
139
0
Guint FoFiBase::getU32LE(int pos, GBool *ok) {
140
0
  Guint x;
141
142
0
  if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) {
143
0
    *ok = gFalse;
144
0
    return 0;
145
0
  }
146
0
  x = file[pos+3];
147
0
  x = (x << 8) + file[pos+2];
148
0
  x = (x << 8) + file[pos+1];
149
0
  x = (x << 8) + file[pos];
150
0
  return x;
151
0
}
152
153
2.60M
Guint FoFiBase::getUVarBE(int pos, int size, GBool *ok) {
154
2.60M
  Guint x;
155
2.60M
  int i;
156
157
2.60M
  if (pos < 0 || pos > INT_MAX - size || pos + size > len) {
158
40.3k
    *ok = gFalse;
159
40.3k
    return 0;
160
40.3k
  }
161
2.56M
  x = 0;
162
14.5M
  for (i = 0; i < size; ++i) {
163
11.9M
    x = (x << 8) + file[pos + i];
164
11.9M
  }
165
2.56M
  return x;
166
2.60M
}
167
168
1.34M
GBool FoFiBase::checkRegion(int pos, int size) {
169
1.34M
  return pos >= 0 &&
170
1.34M
         size >= 0 &&
171
1.34M
         size <= INT_MAX - pos &&
172
1.28M
         pos + size <= len;
173
1.34M
}