fileHeader.cpp
Engine/source/core/util/zip/fileHeader.cpp
Namespaces:
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "core/frameAllocator.h" 25#include "core/stream/stream.h" 26 27#include "core/util/zip/fileHeader.h" 28#include "core/util/zip/compressor.h" 29 30#include "core/util/safeDelete.h" 31 32#include "core/resizeStream.h" 33#include "core/strings/stringFunctions.h" 34#include "core/frameAllocator.h" 35 36namespace Zip 37{ 38 39//----------------------------------------------------------------------------- 40// Constructor/Destructor 41//----------------------------------------------------------------------------- 42 43FileHeader::FileHeader() 44{ 45 mHeaderSig = mFileHeaderSignature; 46 47 mExtractVer = 20; 48 mFlags = 0; 49 mCompressMethod = Stored; 50 51 mModTime = 0; 52 mModDate = 0; 53 54 mCRC32 = 0; 55 56 mCompressedSize = 0; 57 mUncompressedSize = 0; 58 59 mFilename = ""; 60} 61 62FileHeader::~FileHeader() 63{ 64 for(S32 i = 0;i < mExtraFields.size();i++) 65 { 66 SAFE_DELETE(mExtraFields[i]); 67 } 68} 69 70//----------------------------------------------------------------------------- 71// Protected Methods 72//----------------------------------------------------------------------------- 73 74bool FileHeader::readExtraFields(Stream *stream, U16 efLen) 75{ 76 bool ret = true; 77 78 U32 pos = stream->getPosition(); 79 U32 end = pos + efLen; 80 81 while(stream->getPosition() < end) 82 { 83 U16 fieldSig, fieldSize; 84 85 ret = false; 86 87 ret |= stream->read(&fieldSig); 88 ret |= stream->read(&fieldSize); 89 if(! ret) 90 break; 91 92 pos = stream->getPosition(); 93 94 ExtraField *ef = ExtraField::create(fieldSig); 95 if(ef) 96 { 97 ret |= ef->read(stream); 98 99 if(! ret) 100 delete ef; 101 else 102 mExtraFields.push_back(ef); 103 } 104 105 stream->setPosition(pos + fieldSize); 106 } 107 108 return ret; 109} 110 111//----------------------------------------------------------------------------- 112// Public Methods 113//----------------------------------------------------------------------------- 114 115bool FileHeader::read(Stream *stream) 116{ 117 stream->read(&mHeaderSig); 118 if(mHeaderSig != mFileHeaderSignature) 119 return false; 120 121 stream->read(&mExtractVer); 122 stream->read(&mFlags); 123 stream->read(&mCompressMethod); 124 stream->read(&mModTime); 125 stream->read(&mModDate); 126 stream->read(&mCRC32); 127 stream->read(&mCompressedSize); 128 stream->read(&mUncompressedSize); 129 130 U16 fnLen, efLen; 131 stream->read(&fnLen); 132 stream->read(&efLen); 133 134 char *fn = new char[fnLen + 1]; 135 stream->read(fnLen, fn); 136 fn[fnLen] = 0; 137 mFilename = fn; 138 SAFE_DELETE_ARRAY(fn); 139 140 141 return readExtraFields(stream, efLen); 142} 143 144bool FileHeader::write(Stream *stream) 145{ 146 mHeaderSig = mFileHeaderSignature; 147 148 stream->write(mHeaderSig); 149 150 stream->write(mExtractVer); 151 stream->write(mFlags); 152 stream->write(mCompressMethod); 153 stream->write(mModTime); 154 stream->write(mModDate); 155 stream->write(mCRC32); 156 stream->write(mCompressedSize); 157 stream->write(mUncompressedSize); 158 159 U16 fnLen = mFilename.length(), 160 efLen = 0; 161 stream->write(fnLen); 162 stream->write(efLen); 163 164 if(fnLen) 165 stream->write(fnLen, mFilename); 166 167 // FIXME [tom, 1/23/2007] Write extra fields here 168 169 return true; 170} 171 172//----------------------------------------------------------------------------- 173 174ExtraField *FileHeader::findExtraField(U16 id) 175{ 176 for(S32 i = 0;i < mExtraFields.size();++i) 177 { 178 if(mExtraFields[i]->getID() == id) 179 return mExtraFields[i]; 180 } 181 182 return NULL; 183} 184 185//----------------------------------------------------------------------------- 186 187void FileHeader::setFilename(String filename) 188{ 189 mFilename = filename; 190} 191 192} // end namespace Zip 193