Torque3D Documentation / _generateds / zipStatFilter.h

zipStatFilter.h

Engine/source/core/util/zip/zipStatFilter.h

More...

Classes:

class

Helper class for tracking CRC and uncompressed size.

Namespaces:

namespace

Namespace for the zip code.

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/filterStream.h"
 25#include "core/crc.h"
 26#include "core/util/zip/centralDir.h"
 27
 28// [tom, 1/29/2007] ZipStatFilter allows us to track CRC and uncompressed size
 29// on the fly. This is necessary when dealing with compressed files as the
 30// CRC must be of the uncompressed data.
 31//
 32// The alternative would be to compress the files when updating the zip file,
 33// but this could potentially cause ZipArchive::closeArchive() to take a long
 34// time to complete. With compression done on the fly the time consuming code
 35// is pushed out to when writing to files, which is significantly easier to
 36// do asynchronously.
 37
 38#ifndef _ZIPSTATFILTER_H_
 39#define _ZIPSTATFILTER_H_
 40
 41//-----------------------------------------------------------------------------
 42/// \brief Namespace for the zip code.
 43///
 44/// @see Zip::ZipArchive, \ref zip_group "Zip Code Module"
 45//-----------------------------------------------------------------------------
 46namespace Zip
 47{
 48
 49/// @addtogroup zipint_group Zip Code Internals
 50/// 
 51/// The zip code internals are mostly undocumented, but should be fairly
 52/// obvious to anyone who is familiar with the zip file format.
 53
 54/// @ingroup zip_group
 55// @{
 56
 57//-----------------------------------------------------------------------------
 58/// \brief Helper class for tracking CRC and uncompressed size
 59/// 
 60/// ZipStatFilter allows us to track CRC and uncompressed size
 61/// on the fly. This is necessary when dealing with compressed files as the
 62/// CRC must be of the uncompressed data.
 63/// 
 64/// ZipStatFilter is mostly intended for internal use by the zip code.
 65/// However, it can be useful when reading zips sequentially using the
 66/// stream interface to provide CRC checking.
 67/// 
 68/// <b>Example</b>
 69/// 
 70/// @code
 71/// // It's assumed that you would use proper error checking and that
 72/// // zip is a valid pointer to a ZipArchive and otherStream is a pointer
 73/// // to a valid stream.
 74/// Zip::ZipArchive *zip;
 75/// Stream *otherStream;
 76/// 
 77/// // We need the real central directory to compare the CRC32
 78/// Zip::CentralDir *realCD = zip->findFileInfo("file.txt");
 79/// Stream *stream = zip->openFile("file.txt", ZipArchive::Read);
 80/// 
 81/// Zip::CentralDir fakeCD;
 82/// Zip::ZipStatFilter zsf(&fakeCD);
 83/// 
 84/// zsf.attachStream(stream);
 85/// 
 86/// // ... read <i>entire</i> file sequentially using zsf instead of stream
 87/// otherStream->copyFrom(&zsf);
 88/// 
 89/// zsf.detachStream();
 90/// 
 91/// // fakeCD.mCRC32 now contains the CRC32 of the stream
 92/// if(fakeCD.mCRC32 != realCD->mCRC32)
 93/// {
 94///    // ... handle CRC failure ...
 95/// }
 96/// 
 97/// zip->closeFile(stream);
 98/// @endcode
 99/// 
100/// A more complete example of this may be found in the code for the
101/// ZipArchive::extractFile() method in zipArchive.cc 
102/// 
103//-----------------------------------------------------------------------------
104class ZipStatFilter : public FilterStream
105{
106   typedef FilterStream Parent;
107
108protected:
109   Stream *mStream;
110
111   CentralDir *mCD;
112
113   virtual bool _write(const U32 numBytes, const void *buffer)
114   {
115      if(! mStream->write(numBytes, buffer))
116         return false;
117
118      mCD->mUncompressedSize += numBytes;
119      mCD->mCRC32 = CRC::calculateCRC(buffer, numBytes, mCD->mCRC32);
120
121      return true;
122   }
123
124   virtual bool _read(const U32 numBytes, void *buffer)
125   {
126      if(! mStream->read(numBytes, buffer))
127         return false;
128
129      mCD->mUncompressedSize += numBytes;
130      mCD->mCRC32 = CRC::calculateCRC(buffer, numBytes, mCD->mCRC32);
131
132      return true;
133   }
134
135public:
136   ZipStatFilter() : mStream(NULL), mCD(NULL) {}
137   ZipStatFilter(CentralDir *cd) : mStream(NULL), mCD(cd) {}
138   virtual ~ZipStatFilter()
139   {
140      detachStream();
141   }
142
143   virtual bool attachStream(Stream *stream)
144   {
145      if(mCD == NULL)
146         return false;
147
148      mStream = stream;
149      mCD->mUncompressedSize = 0;
150      mCD->mCRC32 = CRC::INITIAL_CRC_VALUE;
151      return true;
152   }
153
154   virtual void detachStream()
155   {
156      if(mStream == NULL)
157         return;
158
159      // Post condition the CRC
160      mCD->mCRC32 ^= CRC::CRC_POSTCOND_VALUE;
161      mStream = NULL;
162   }
163
164   virtual Stream *getStream()                     { return mStream; }
165
166   void setCentralDir(CentralDir *cd)              { mCD = cd; }
167   CentralDir *getCentralDir()                     { return mCD; }
168};
169
170// @}
171
172} // end namespace Zip
173
174#endif // _ZIPSTATFILTER_H_
175