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