/src/xpdf-4.04/fofi/FoFiBase.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //======================================================================== |
2 | | // |
3 | | // FoFiBase.cc |
4 | | // |
5 | | // Copyright 1999-2003 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #ifdef USE_GCC_PRAGMAS |
12 | | #pragma implementation |
13 | | #endif |
14 | | |
15 | | #include <stdio.h> |
16 | | #include <limits.h> |
17 | | #include "gmem.h" |
18 | | #include "gmempp.h" |
19 | | #include "FoFiBase.h" |
20 | | |
21 | | //------------------------------------------------------------------------ |
22 | | // FoFiBase |
23 | | //------------------------------------------------------------------------ |
24 | | |
25 | 0 | FoFiBase::FoFiBase(char *fileA, int lenA, GBool freeFileDataA) { |
26 | 0 | fileData = file = (Guchar *)fileA; |
27 | 0 | len = lenA; |
28 | 0 | freeFileData = freeFileDataA; |
29 | 0 | } |
30 | | |
31 | 0 | FoFiBase::~FoFiBase() { |
32 | 0 | if (freeFileData) { |
33 | 0 | gfree(fileData); |
34 | 0 | } |
35 | 0 | } |
36 | | |
37 | 0 | char *FoFiBase::readFile(char *fileName, int *fileLen) { |
38 | 0 | FILE *f; |
39 | 0 | char *buf; |
40 | 0 | int n; |
41 | |
|
42 | 0 | if (!(f = fopen(fileName, "rb"))) { |
43 | 0 | return NULL; |
44 | 0 | } |
45 | 0 | fseek(f, 0, SEEK_END); |
46 | 0 | n = (int)ftell(f); |
47 | 0 | if (n < 0) { |
48 | 0 | fclose(f); |
49 | 0 | return NULL; |
50 | 0 | } |
51 | 0 | fseek(f, 0, SEEK_SET); |
52 | 0 | buf = (char *)gmalloc(n); |
53 | 0 | if ((int)fread(buf, 1, n, f) != n) { |
54 | 0 | gfree(buf); |
55 | 0 | fclose(f); |
56 | 0 | return NULL; |
57 | 0 | } |
58 | 0 | fclose(f); |
59 | 0 | *fileLen = n; |
60 | 0 | return buf; |
61 | 0 | } |
62 | | |
63 | 0 | int FoFiBase::getS8(int pos, GBool *ok) { |
64 | 0 | int x; |
65 | |
|
66 | 0 | if (pos < 0 || pos >= len) { |
67 | 0 | *ok = gFalse; |
68 | 0 | return 0; |
69 | 0 | } |
70 | 0 | x = file[pos]; |
71 | 0 | if (x & 0x80) { |
72 | 0 | x |= ~0xff; |
73 | 0 | } |
74 | 0 | return x; |
75 | 0 | } |
76 | | |
77 | 0 | int FoFiBase::getU8(int pos, GBool *ok) { |
78 | 0 | if (pos < 0 || pos >= len) { |
79 | 0 | *ok = gFalse; |
80 | 0 | return 0; |
81 | 0 | } |
82 | 0 | return file[pos]; |
83 | 0 | } |
84 | | |
85 | 0 | int FoFiBase::getS16BE(int pos, GBool *ok) { |
86 | 0 | int x; |
87 | |
|
88 | 0 | if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) { |
89 | 0 | *ok = gFalse; |
90 | 0 | return 0; |
91 | 0 | } |
92 | 0 | x = file[pos]; |
93 | 0 | x = (x << 8) + file[pos+1]; |
94 | 0 | if (x & 0x8000) { |
95 | 0 | x |= ~0xffff; |
96 | 0 | } |
97 | 0 | return x; |
98 | 0 | } |
99 | | |
100 | 0 | int FoFiBase::getU16BE(int pos, GBool *ok) { |
101 | 0 | int x; |
102 | |
|
103 | 0 | if (pos < 0 || pos > INT_MAX - 1 || pos+1 >= len) { |
104 | 0 | *ok = gFalse; |
105 | 0 | return 0; |
106 | 0 | } |
107 | 0 | x = file[pos]; |
108 | 0 | x = (x << 8) + file[pos+1]; |
109 | 0 | return x; |
110 | 0 | } |
111 | | |
112 | 0 | int FoFiBase::getS32BE(int pos, GBool *ok) { |
113 | 0 | int x; |
114 | |
|
115 | 0 | if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) { |
116 | 0 | *ok = gFalse; |
117 | 0 | return 0; |
118 | 0 | } |
119 | 0 | x = file[pos]; |
120 | 0 | x = (x << 8) + file[pos+1]; |
121 | 0 | x = (x << 8) + file[pos+2]; |
122 | 0 | x = (x << 8) + file[pos+3]; |
123 | 0 | if (x & 0x80000000) { |
124 | 0 | x |= ~0xffffffff; |
125 | 0 | } |
126 | 0 | return x; |
127 | 0 | } |
128 | | |
129 | 0 | Guint FoFiBase::getU32BE(int pos, GBool *ok) { |
130 | 0 | Guint x; |
131 | |
|
132 | 0 | if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) { |
133 | 0 | *ok = gFalse; |
134 | 0 | return 0; |
135 | 0 | } |
136 | 0 | x = file[pos]; |
137 | 0 | x = (x << 8) + file[pos+1]; |
138 | 0 | x = (x << 8) + file[pos+2]; |
139 | 0 | x = (x << 8) + file[pos+3]; |
140 | 0 | return x; |
141 | 0 | } |
142 | | |
143 | 0 | Guint FoFiBase::getU32LE(int pos, GBool *ok) { |
144 | 0 | Guint x; |
145 | |
|
146 | 0 | if (pos < 0 || pos > INT_MAX - 3 || pos+3 >= len) { |
147 | 0 | *ok = gFalse; |
148 | 0 | return 0; |
149 | 0 | } |
150 | 0 | x = file[pos+3]; |
151 | 0 | x = (x << 8) + file[pos+2]; |
152 | 0 | x = (x << 8) + file[pos+1]; |
153 | 0 | x = (x << 8) + file[pos]; |
154 | 0 | return x; |
155 | 0 | } |
156 | | |
157 | 0 | Guint FoFiBase::getUVarBE(int pos, int size, GBool *ok) { |
158 | 0 | Guint x; |
159 | 0 | int i; |
160 | |
|
161 | 0 | if (pos < 0 || pos > INT_MAX - size || pos + size > len) { |
162 | 0 | *ok = gFalse; |
163 | 0 | return 0; |
164 | 0 | } |
165 | 0 | x = 0; |
166 | 0 | for (i = 0; i < size; ++i) { |
167 | 0 | x = (x << 8) + file[pos + i]; |
168 | 0 | } |
169 | 0 | return x; |
170 | 0 | } |
171 | | |
172 | 0 | GBool FoFiBase::checkRegion(int pos, int size) { |
173 | 0 | return pos >= 0 && |
174 | 0 | size >= 0 && |
175 | 0 | size <= INT_MAX - pos && |
176 | 0 | pos + size <= len; |
177 | 0 | } |