Back to index

tetex-bin  3.0
FoFiBase.cc
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // FoFiBase.cc
00004 //
00005 // Copyright 1999-2003 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014 
00015 #include <stdio.h>
00016 #include "gmem.h"
00017 #include "FoFiBase.h"
00018 
00019 //------------------------------------------------------------------------
00020 // FoFiBase
00021 //------------------------------------------------------------------------
00022 
00023 FoFiBase::FoFiBase(char *fileA, int lenA, GBool freeFileDataA) {
00024   fileData = file = (Guchar *)fileA;
00025   len = lenA;
00026   freeFileData = freeFileDataA;
00027 }
00028 
00029 FoFiBase::~FoFiBase() {
00030   if (freeFileData) {
00031     gfree(fileData);
00032   }
00033 }
00034 
00035 char *FoFiBase::readFile(char *fileName, int *fileLen) {
00036   FILE *f;
00037   char *buf;
00038   int n;
00039 
00040   if (!(f = fopen(fileName, "rb"))) {
00041     return NULL;
00042   }
00043   fseek(f, 0, SEEK_END);
00044   n = (int)ftell(f);
00045   fseek(f, 0, SEEK_SET);
00046   buf = (char *)gmalloc(n);
00047   if ((int)fread(buf, 1, n, f) != n) {
00048     gfree(buf);
00049     fclose(f);
00050     return NULL;
00051   }
00052   fclose(f);
00053   *fileLen = n;
00054   return buf;
00055 }
00056 
00057 int FoFiBase::getS8(int pos, GBool *ok) {
00058   int x;
00059 
00060   if (pos < 0 || pos >= len) {
00061     *ok = gFalse;
00062     return 0;
00063   }
00064   x = file[pos];
00065   if (x & 0x80) {
00066     x |= ~0xff;
00067   }
00068   return x;
00069 }
00070 
00071 int FoFiBase::getU8(int pos, GBool *ok) {
00072   if (pos < 0 || pos >= len) {
00073     *ok = gFalse;
00074     return 0;
00075   }
00076   return file[pos];
00077 }
00078 
00079 int FoFiBase::getS16BE(int pos, GBool *ok) {
00080   int x;
00081 
00082   if (pos < 0 || pos+1 >= len) {
00083     *ok = gFalse;
00084     return 0;
00085   }
00086   x = file[pos];
00087   x = (x << 8) + file[pos+1];
00088   if (x & 0x8000) {
00089     x |= ~0xffff;
00090   }
00091   return x;
00092 }
00093 
00094 int FoFiBase::getU16BE(int pos, GBool *ok) {
00095   int x;
00096 
00097   if (pos < 0 || pos+1 >= len) {
00098     *ok = gFalse;
00099     return 0;
00100   }
00101   x = file[pos];
00102   x = (x << 8) + file[pos+1];
00103   return x;
00104 }
00105 
00106 int FoFiBase::getS32BE(int pos, GBool *ok) {
00107   int x;
00108 
00109   if (pos < 0 || pos+3 >= len) {
00110     *ok = gFalse;
00111     return 0;
00112   }
00113   x = file[pos];
00114   x = (x << 8) + file[pos+1];
00115   x = (x << 8) + file[pos+2];
00116   x = (x << 8) + file[pos+3];
00117   if (x & 0x80000000) {
00118     x |= ~0xffffffff;
00119   }
00120   return x;
00121 }
00122 
00123 Guint FoFiBase::getU32BE(int pos, GBool *ok) {
00124   Guint x;
00125 
00126   if (pos < 0 || pos+3 >= len) {
00127     *ok = gFalse;
00128     return 0;
00129   }
00130   x = file[pos];
00131   x = (x << 8) + file[pos+1];
00132   x = (x << 8) + file[pos+2];
00133   x = (x << 8) + file[pos+3];
00134   return x;
00135 }
00136 
00137 Guint FoFiBase::getUVarBE(int pos, int size, GBool *ok) {
00138   Guint x;
00139   int i;
00140 
00141   if (pos < 0 || pos + size > len) {
00142     *ok = gFalse;
00143     return 0;
00144   }
00145   x = 0;
00146   for (i = 0; i < size; ++i) {
00147     x = (x << 8) + file[pos + i];
00148   }
00149   return x;
00150 }
00151 
00152 GBool FoFiBase::checkRegion(int pos, int size) {
00153   return pos >= 0 &&
00154          pos + size >= pos &&
00155          pos + size <= len;
00156 }