Coverage Report

Created: 2026-08-13 06:56

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