1 //========================================================================
5 // Copyright 1999-2003 Glyph & Cog, LLC
7 //========================================================================
9 //========================================================================
11 // Modified under the Poppler project - http://poppler.freedesktop.org
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
16 // Copyright (C) 2008 Ed Avis <eda@waniasset.com>
17 // Copyright (C) 2011 Jim Meyering <jim@meyering.net>
19 // To see a description of the changes please see the Changelog file that
20 // came with your tarball or type make ChangeLog if you are building from git
22 //========================================================================
26 #ifdef USE_GCC_PRAGMAS
27 #pragma implementation
33 #include "poppler/Error.h"
36 //------------------------------------------------------------------------
38 //------------------------------------------------------------------------
40 FoFiBase::FoFiBase(char *fileA
, int lenA
, GBool freeFileDataA
) {
41 fileData
= file
= (Guchar
*)fileA
;
43 freeFileData
= freeFileDataA
;
46 FoFiBase::~FoFiBase() {
52 char *FoFiBase::readFile(char *fileName
, int *fileLen
) {
57 if (!(f
= fopen(fileName
, "rb"))) {
58 error(errIO
, -1, "Cannot open '{0:s}'", fileName
);
61 if (fseek(f
, 0, SEEK_END
) != 0) {
62 error(errIO
, -1, "Cannot seek to end of '{0:s}'", fileName
);
68 error(errIO
, -1, "Cannot determine length of '{0:s}'", fileName
);
72 if (fseek(f
, 0, SEEK_SET
) != 0) {
73 error(errIO
, -1, "Cannot seek to start of '{0:s}'", fileName
);
77 buf
= (char *)gmalloc(n
);
78 if ((int)fread(buf
, 1, n
, f
) != n
) {
88 int FoFiBase::getS8(int pos
, GBool
*ok
) {
91 if (pos
< 0 || pos
>= len
) {
102 int FoFiBase::getU8(int pos
, GBool
*ok
) {
103 if (pos
< 0 || pos
>= len
) {
110 int FoFiBase::getS16BE(int pos
, GBool
*ok
) {
113 if (pos
< 0 || pos
+1 >= len
|| pos
> INT_MAX
- 1) {
118 x
= (x
<< 8) + file
[pos
+1];
125 int FoFiBase::getU16BE(int pos
, GBool
*ok
) {
128 if (pos
< 0 || pos
+1 >= len
|| pos
> INT_MAX
- 1) {
133 x
= (x
<< 8) + file
[pos
+1];
137 int FoFiBase::getS32BE(int pos
, GBool
*ok
) {
140 if (pos
< 0 || pos
+3 >= len
|| pos
> INT_MAX
- 3) {
145 x
= (x
<< 8) + file
[pos
+1];
146 x
= (x
<< 8) + file
[pos
+2];
147 x
= (x
<< 8) + file
[pos
+3];
148 if (x
& 0x80000000) {
154 Guint
FoFiBase::getU32BE(int pos
, GBool
*ok
) {
157 if (pos
< 0 || pos
+3 >= len
|| pos
> INT_MAX
- 3) {
162 x
= (x
<< 8) + file
[pos
+1];
163 x
= (x
<< 8) + file
[pos
+2];
164 x
= (x
<< 8) + file
[pos
+3];
168 Guint
FoFiBase::getU32LE(int pos
, GBool
*ok
) {
171 if (pos
< 0 || pos
+3 >= len
|| pos
> INT_MAX
- 3) {
176 x
= (x
<< 8) + file
[pos
+2];
177 x
= (x
<< 8) + file
[pos
+1];
178 x
= (x
<< 8) + file
[pos
];
182 Guint
FoFiBase::getUVarBE(int pos
, int size
, GBool
*ok
) {
186 if (pos
< 0 || pos
+ size
> len
|| pos
> INT_MAX
- size
) {
191 for (i
= 0; i
< size
; ++i
) {
192 x
= (x
<< 8) + file
[pos
+ i
];
197 GBool
FoFiBase::checkRegion(int pos
, int size
) {