Coverage Report

Created: 2026-02-26 07:15

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