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