Coverage Report

Created: 2026-05-30 06:25

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.67k
    gfree(fileData);
30
6.67k
  }
31
10.8k
}
32
33
6.66k
char *FoFiBase::readFile(char *fileName, int *fileLen) {
34
6.66k
  FILE *f;
35
6.66k
  char *buf;
36
6.66k
  int n;
37
38
6.66k
  if (!(f = fopen(fileName, "rb"))) {
39
0
    return NULL;
40
0
  }
41
6.66k
  fseek(f, 0, SEEK_END);
42
6.66k
  n = (int)ftell(f);
43
6.66k
  if (n < 0) {
44
0
    fclose(f);
45
0
    return NULL;
46
0
  }
47
6.66k
  fseek(f, 0, SEEK_SET);
48
6.66k
  buf = (char *)gmalloc(n);
49
6.66k
  if ((int)fread(buf, 1, n, f) != n) {
50
0
    gfree(buf);
51
0
    fclose(f);
52
0
    return NULL;
53
0
  }
54
6.66k
  fclose(f);
55
6.66k
  *fileLen = n;
56
6.66k
  return buf;
57
6.66k
}
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
201M
int FoFiBase::getU8(int pos, GBool *ok) {
74
201M
  if (pos < 0 || pos >= len) {
75
159M
    *ok = gFalse;
76
159M
    return 0;
77
159M
  }
78
42.7M
  return file[pos];
79
201M
}
80
81
22.3k
int FoFiBase::getS16BE(int pos, GBool *ok) {
82
22.3k
  int x;
83
84
22.3k
  if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) {
85
15
    *ok = gFalse;
86
15
    return 0;
87
15
  }
88
22.3k
  x = file[pos];
89
22.3k
  x = (x << 8) + file[pos+1];
90
22.3k
  if (x & 0x8000) {
91
7.36k
    x |= ~0xffff;
92
7.36k
  }
93
22.3k
  return x;
94
22.3k
}
95
96
9.30M
int FoFiBase::getU16BE(int pos, GBool *ok) {
97
9.30M
  int x;
98
99
9.30M
  if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) {
100
713k
    *ok = gFalse;
101
713k
    return 0;
102
713k
  }
103
8.59M
  x = file[pos];
104
8.59M
  x = (x << 8) + file[pos+1];
105
8.59M
  return x;
106
9.30M
}
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
3.86M
Guint FoFiBase::getU32BE(int pos, GBool *ok) {
126
3.86M
  Guint x;
127
128
3.86M
  if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) {
129
357k
    *ok = gFalse;
130
357k
    return 0;
131
357k
  }
132
3.50M
  x = file[pos];
133
3.50M
  x = (x << 8) + file[pos+1];
134
3.50M
  x = (x << 8) + file[pos+2];
135
3.50M
  x = (x << 8) + file[pos+3];
136
3.50M
  return x;
137
3.86M
}
138
139
9
Guint FoFiBase::getU32LE(int pos, GBool *ok) {
140
9
  Guint x;
141
142
9
  if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) {
143
0
    *ok = gFalse;
144
0
    return 0;
145
0
  }
146
9
  x = file[pos+3];
147
9
  x = (x << 8) + file[pos+2];
148
9
  x = (x << 8) + file[pos+1];
149
9
  x = (x << 8) + file[pos];
150
9
  return x;
151
9
}
152
153
1.01M
Guint FoFiBase::getUVarBE(int pos, int size, GBool *ok) {
154
1.01M
  Guint x;
155
1.01M
  int i;
156
157
1.01M
  if (pos < 0 || pos > INT_MAX - size || pos + size > len) {
158
3.11k
    *ok = gFalse;
159
3.11k
    return 0;
160
3.11k
  }
161
1.00M
  x = 0;
162
4.44M
  for (i = 0; i < size; ++i) {
163
3.44M
    x = (x << 8) + file[pos + i];
164
3.44M
  }
165
1.00M
  return x;
166
1.01M
}
167
168
514k
GBool FoFiBase::checkRegion(int pos, int size) {
169
514k
  return pos >= 0 &&
170
514k
         size >= 0 &&
171
514k
         size <= INT_MAX - pos &&
172
481k
         pos + size <= len;
173
514k
}