Coverage Report

Created: 2026-03-31 07:04

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
9.60k
FoFiBase::FoFiBase(char *fileA, int lenA, GBool freeFileDataA) {
22
9.60k
  fileData = file = (Guchar *)fileA;
23
9.60k
  len = lenA;
24
9.60k
  freeFileData = freeFileDataA;
25
9.60k
}
26
27
9.60k
FoFiBase::~FoFiBase() {
28
9.60k
  if (freeFileData) {
29
5.84k
    gfree(fileData);
30
5.84k
  }
31
9.60k
}
32
33
5.84k
char *FoFiBase::readFile(char *fileName, int *fileLen) {
34
5.84k
  FILE *f;
35
5.84k
  char *buf;
36
5.84k
  int n;
37
38
5.84k
  if (!(f = fopen(fileName, "rb"))) {
39
0
    return NULL;
40
0
  }
41
5.84k
  fseek(f, 0, SEEK_END);
42
5.84k
  n = (int)ftell(f);
43
5.84k
  if (n < 0) {
44
0
    fclose(f);
45
0
    return NULL;
46
0
  }
47
5.84k
  fseek(f, 0, SEEK_SET);
48
5.84k
  buf = (char *)gmalloc(n);
49
5.84k
  if ((int)fread(buf, 1, n, f) != n) {
50
0
    gfree(buf);
51
0
    fclose(f);
52
0
    return NULL;
53
0
  }
54
5.84k
  fclose(f);
55
5.84k
  *fileLen = n;
56
5.84k
  return buf;
57
5.84k
}
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
127M
int FoFiBase::getU8(int pos, GBool *ok) {
74
127M
  if (pos < 0 || pos >= len) {
75
114M
    *ok = gFalse;
76
114M
    return 0;
77
114M
  }
78
13.1M
  return file[pos];
79
127M
}
80
81
21.6k
int FoFiBase::getS16BE(int pos, GBool *ok) {
82
21.6k
  int x;
83
84
21.6k
  if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) {
85
15
    *ok = gFalse;
86
15
    return 0;
87
15
  }
88
21.6k
  x = file[pos];
89
21.6k
  x = (x << 8) + file[pos+1];
90
21.6k
  if (x & 0x8000) {
91
7.51k
    x |= ~0xffff;
92
7.51k
  }
93
21.6k
  return x;
94
21.6k
}
95
96
9.37M
int FoFiBase::getU16BE(int pos, GBool *ok) {
97
9.37M
  int x;
98
99
9.37M
  if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) {
100
886k
    *ok = gFalse;
101
886k
    return 0;
102
886k
  }
103
8.49M
  x = file[pos];
104
8.49M
  x = (x << 8) + file[pos+1];
105
8.49M
  return x;
106
9.37M
}
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.70M
Guint FoFiBase::getU32BE(int pos, GBool *ok) {
126
3.70M
  Guint x;
127
128
3.70M
  if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) {
129
447k
    *ok = gFalse;
130
447k
    return 0;
131
447k
  }
132
3.25M
  x = file[pos];
133
3.25M
  x = (x << 8) + file[pos+1];
134
3.25M
  x = (x << 8) + file[pos+2];
135
3.25M
  x = (x << 8) + file[pos+3];
136
3.25M
  return x;
137
3.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
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
143
    *ok = gFalse;
159
143
    return 0;
160
143
  }
161
54.7k
  x = 0;
162
146k
  for (i = 0; i < size; ++i) {
163
91.6k
    x = (x << 8) + file[pos + i];
164
91.6k
  }
165
54.7k
  return x;
166
54.9k
}
167
168
303k
GBool FoFiBase::checkRegion(int pos, int size) {
169
303k
  return pos >= 0 &&
170
303k
         size >= 0 &&
171
303k
         size <= INT_MAX - pos &&
172
284k
         pos + size <= len;
173
303k
}