00001 /* 00002 Copyright (C) 2002-2005 The Pentagram team 00003 00004 This program is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU General Public License 00006 as published by the Free Software Foundation; either version 2 00007 of the License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include "pent_include.h" 00020 00021 #include "FlexFile.h" 00022 #include "IDataSource.h" 00023 00024 DEFINE_RUNTIME_CLASSTYPE_CODE(FlexFile,ArchiveFile); 00025 00026 00027 00028 FlexFile::FlexFile(IDataSource* ds_) 00029 { 00030 ds = ds_; 00031 count = 0; 00032 valid = isFlexFile(ds); 00033 00034 if (valid) { 00035 ds->seek(0x54); 00036 count = ds->read4(); 00037 } 00038 } 00039 00040 FlexFile::~FlexFile() 00041 { 00042 delete ds; 00043 } 00044 00045 //static 00046 bool FlexFile::isFlexFile(IDataSource* ds) 00047 { 00048 ds->seek(0); 00049 int i; 00050 char buf[0x52]; 00051 ds->read(buf, 0x52); 00052 00053 for (i=0; i < 0x52; ++i) 00054 { 00055 if (buf[i] == 0x1A) break; 00056 } 00057 00058 if (i < 0x52) 00059 { 00060 for (++i; i < 0x52; ++i) 00061 { 00062 if (buf[i] != 0x1A) return false; 00063 } 00064 return true; 00065 } 00066 return false; 00067 } 00068 00069 uint32 FlexFile::getOffset(uint32 index) 00070 { 00071 ds->seek(0x80 + 8*index); 00072 return ds->read4(); 00073 } 00074 00075 uint8* FlexFile::getObject(uint32 index, uint32* sizep) 00076 { 00077 if (index >= count) return 0; 00078 00079 uint32 size = getSize(index); 00080 if (size == 0) return 0; 00081 00082 uint8 *object = new uint8[size]; 00083 uint32 offset = getOffset(index); 00084 00085 ds->seek(offset); 00086 ds->read(object, size); 00087 00088 if (sizep) *sizep = size; 00089 00090 return object; 00091 } 00092 00093 uint32 FlexFile::getSize(uint32 index) 00094 { 00095 if (index >= count) return 0; 00096 00097 ds->seek(0x84 + 8*index); 00098 uint32 length = ds->read4(); 00099 00100 return length; 00101 } 00102 00103 bool FlexFile::nameToIndex(const std::string& name, uint32& index) 00104 { 00105 return extractIndexFromName(name, index); 00106 }