| 1 | /* | 
|---|
| 2 | ----------------------------------------------------------------------------- | 
|---|
| 3 | This source file is part of OGRE | 
|---|
| 4 | (Object-oriented Graphics Rendering Engine) | 
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (c) 2000-2013 Torus Knot Software Ltd | 
|---|
| 8 |  | 
|---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy | 
|---|
| 10 | of this software and associated documentation files (the "Software"), to deal | 
|---|
| 11 | in the Software without restriction, including without limitation the rights | 
|---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|---|
| 13 | copies of the Software, and to permit persons to whom the Software is | 
|---|
| 14 | furnished to do so, subject to the following conditions: | 
|---|
| 15 |  | 
|---|
| 16 | The above copyright notice and this permission notice shall be included in | 
|---|
| 17 | all copies or substantial portions of the Software. | 
|---|
| 18 |  | 
|---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
|---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
|---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
|---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
|---|
| 25 | THE SOFTWARE. | 
|---|
| 26 | ----------------------------------------------------------------------------- | 
|---|
| 27 | */ | 
|---|
| 28 | #ifndef __Zip_H__ | 
|---|
| 29 | #define __Zip_H__ | 
|---|
| 30 |  | 
|---|
| 31 | #include "OgrePrerequisites.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "OgreArchive.h" | 
|---|
| 34 | #include "OgreArchiveFactory.h" | 
|---|
| 35 | #if OGRE_THREAD_SUPPORT | 
|---|
| 36 | #include "Threading/OgreThreadHeaders.h" | 
|---|
| 37 | #endif | 
|---|
| 38 | #include "OgreHeaderPrefix.h" | 
|---|
| 39 |  | 
|---|
| 40 | // Forward declaration for zziplib to avoid header file dependency. | 
|---|
| 41 | typedef struct zzip_dir         ZZIP_DIR; | 
|---|
| 42 | typedef struct zzip_file        ZZIP_FILE; | 
|---|
| 43 | typedef union _zzip_plugin_io zzip_plugin_io_handlers; | 
|---|
| 44 |  | 
|---|
| 45 | namespace Ogre { | 
|---|
| 46 |  | 
|---|
| 47 |         /** \addtogroup Core | 
|---|
| 48 |         *  @{ | 
|---|
| 49 |         */ | 
|---|
| 50 |         /** \addtogroup Resources | 
|---|
| 51 |         *  @{ | 
|---|
| 52 |         */ | 
|---|
| 53 |         /** Specialisation of the Archive class to allow reading of files from a zip | 
|---|
| 54 |         format source archive. | 
|---|
| 55 |     @remarks | 
|---|
| 56 |         This archive format supports all archives compressed in the standard | 
|---|
| 57 |         zip format, including iD pk3 files. | 
|---|
| 58 |     */ | 
|---|
| 59 |     class _OgreExport ZipArchive : public Archive  | 
|---|
| 60 |     { | 
|---|
| 61 |     protected: | 
|---|
| 62 |         /// Handle to root zip file | 
|---|
| 63 |         ZZIP_DIR* mZzipDir; | 
|---|
| 64 |         /// Handle any errors from zzip | 
|---|
| 65 |         void checkZzipError(int zzipError, const String& operation) const; | 
|---|
| 66 |         /// File list (since zziplib seems to only allow scanning of dir tree once) | 
|---|
| 67 |         FileInfoList mFileList; | 
|---|
| 68 |         /// A pointer to file io alternative implementation  | 
|---|
| 69 |         zzip_plugin_io_handlers* mPluginIo; | 
|---|
| 70 |  | 
|---|
| 71 |         OGRE_AUTO_MUTEX; | 
|---|
| 72 |     public: | 
|---|
| 73 |         ZipArchive(const String& name, const String& archType, zzip_plugin_io_handlers* pluginIo = NULL); | 
|---|
| 74 |         ~ZipArchive(); | 
|---|
| 75 |         /// @copydoc Archive::isCaseSensitive | 
|---|
| 76 |         bool isCaseSensitive(void) const { return false; } | 
|---|
| 77 |  | 
|---|
| 78 |         /// @copydoc Archive::load | 
|---|
| 79 |         void load(); | 
|---|
| 80 |         /// @copydoc Archive::unload | 
|---|
| 81 |         void unload(); | 
|---|
| 82 |  | 
|---|
| 83 |         /// @copydoc Archive::open | 
|---|
| 84 |         DataStreamPtr open(const String& filename, bool readOnly = true) const; | 
|---|
| 85 |  | 
|---|
| 86 |                 /// @copydoc Archive::create | 
|---|
| 87 |                 DataStreamPtr create(const String& filename) const; | 
|---|
| 88 |  | 
|---|
| 89 |                 /// @copydoc Archive::remove | 
|---|
| 90 |                 void remove(const String& filename) const; | 
|---|
| 91 |  | 
|---|
| 92 |         /// @copydoc Archive::list | 
|---|
| 93 |         StringVectorPtr list(bool recursive = true, bool dirs = false); | 
|---|
| 94 |  | 
|---|
| 95 |         /// @copydoc Archive::listFileInfo | 
|---|
| 96 |         FileInfoListPtr listFileInfo(bool recursive = true, bool dirs = false); | 
|---|
| 97 |  | 
|---|
| 98 |         /// @copydoc Archive::find | 
|---|
| 99 |         StringVectorPtr find(const String& pattern, bool recursive = true, | 
|---|
| 100 |             bool dirs = false); | 
|---|
| 101 |  | 
|---|
| 102 |         /// @copydoc Archive::findFileInfo | 
|---|
| 103 |         FileInfoListPtr findFileInfo(const String& pattern, bool recursive = true, | 
|---|
| 104 |             bool dirs = false) const; | 
|---|
| 105 |  | 
|---|
| 106 |         /// @copydoc Archive::exists | 
|---|
| 107 |         bool exists(const String& filename); | 
|---|
| 108 |  | 
|---|
| 109 |                 /// @copydoc Archive::getModifiedTime | 
|---|
| 110 |                 time_t getModifiedTime(const String& filename); | 
|---|
| 111 |     }; | 
|---|
| 112 |  | 
|---|
| 113 |     /** Specialisation of ArchiveFactory for Zip files. */ | 
|---|
| 114 |     class _OgrePrivate ZipArchiveFactory : public ArchiveFactory | 
|---|
| 115 |     { | 
|---|
| 116 |     public: | 
|---|
| 117 |         virtual ~ZipArchiveFactory() {} | 
|---|
| 118 |         /// @copydoc FactoryObj::getType | 
|---|
| 119 |         const String& getType(void) const; | 
|---|
| 120 |         /// @copydoc FactoryObj::createInstance | 
|---|
| 121 |         Archive *createInstance( const String& name, bool readOnly )  | 
|---|
| 122 |         { | 
|---|
| 123 |                         if(!readOnly) | 
|---|
| 124 |                                 return NULL; | 
|---|
| 125 |  | 
|---|
| 126 |             return OGRE_NEW ZipArchive(name, "Zip"); | 
|---|
| 127 |         } | 
|---|
| 128 |         /// @copydoc FactoryObj::destroyInstance | 
|---|
| 129 |         void destroyInstance( Archive* ptr) { OGRE_DELETE ptr; } | 
|---|
| 130 |     }; | 
|---|
| 131 |  | 
|---|
| 132 |     /** Specialisation of ZipArchiveFactory for embedded Zip files. */ | 
|---|
| 133 |     class _OgreExport EmbeddedZipArchiveFactory : public ZipArchiveFactory | 
|---|
| 134 |     { | 
|---|
| 135 |     protected: | 
|---|
| 136 |         /// A static pointer to file io alternative implementation for the embedded files | 
|---|
| 137 |         static zzip_plugin_io_handlers* mPluginIo;  | 
|---|
| 138 |     public: | 
|---|
| 139 |         EmbeddedZipArchiveFactory(); | 
|---|
| 140 |         virtual ~EmbeddedZipArchiveFactory(); | 
|---|
| 141 |         /// @copydoc FactoryObj::getType | 
|---|
| 142 |         const String& getType(void) const; | 
|---|
| 143 |         /// @copydoc FactoryObj::createInstance | 
|---|
| 144 |         Archive *createInstance( const String& name, bool readOnly )  | 
|---|
| 145 |         { | 
|---|
| 146 |             ZipArchive * resZipArchive = OGRE_NEW ZipArchive(name, "EmbeddedZip", mPluginIo); | 
|---|
| 147 |             return resZipArchive; | 
|---|
| 148 |         } | 
|---|
| 149 |          | 
|---|
| 150 |         /** a function type to decrypt embedded zip file | 
|---|
| 151 |         @param pos pos in file | 
|---|
| 152 |         @param buf current buffer to decrypt | 
|---|
| 153 |         @param len - length of buffer | 
|---|
| 154 |         @return success | 
|---|
| 155 |         */   | 
|---|
| 156 |         typedef bool (*DecryptEmbeddedZipFileFunc)(size_t pos, void* buf, size_t len); | 
|---|
| 157 |  | 
|---|
| 158 |         /// Add an embedded file to the embedded file list | 
|---|
| 159 |         static void addEmbbeddedFile(const String& name, const uint8 * fileData,  | 
|---|
| 160 |                         size_t fileSize, DecryptEmbeddedZipFileFunc decryptFunc); | 
|---|
| 161 |  | 
|---|
| 162 |         /// Remove an embedded file to the embedded file list | 
|---|
| 163 |         static void removeEmbbeddedFile(const String& name); | 
|---|
| 164 |  | 
|---|
| 165 |     }; | 
|---|
| 166 |  | 
|---|
| 167 |     /** Specialisation of DataStream to handle streaming data from zip archives. */ | 
|---|
| 168 |     class _OgrePrivate ZipDataStream : public DataStream | 
|---|
| 169 |     { | 
|---|
| 170 |     protected: | 
|---|
| 171 |         ZZIP_FILE* mZzipFile; | 
|---|
| 172 |                 /// We need caching because sometimes serializers step back in data stream and zziplib behaves slow | 
|---|
| 173 |                 StaticCache<2 * OGRE_STREAM_TEMP_SIZE> mCache; | 
|---|
| 174 |     public: | 
|---|
| 175 |         /// Unnamed constructor | 
|---|
| 176 |         ZipDataStream(ZZIP_FILE* zzipFile, size_t uncompressedSize); | 
|---|
| 177 |         /// Constructor for creating named streams | 
|---|
| 178 |         ZipDataStream(const String& name, ZZIP_FILE* zzipFile, size_t uncompressedSize); | 
|---|
| 179 |                 ~ZipDataStream(); | 
|---|
| 180 |         /// @copydoc DataStream::read | 
|---|
| 181 |         size_t read(void* buf, size_t count); | 
|---|
| 182 |                 /// @copydoc DataStream::write | 
|---|
| 183 |                 size_t write(void* buf, size_t count); | 
|---|
| 184 |         /// @copydoc DataStream::skip | 
|---|
| 185 |         void skip(long count); | 
|---|
| 186 |         /// @copydoc DataStream::seek | 
|---|
| 187 |         void seek( size_t pos ); | 
|---|
| 188 |         /// @copydoc DataStream::seek | 
|---|
| 189 |         size_t tell(void) const; | 
|---|
| 190 |         /// @copydoc DataStream::eof | 
|---|
| 191 |         bool eof(void) const; | 
|---|
| 192 |         /// @copydoc DataStream::close | 
|---|
| 193 |         void close(void); | 
|---|
| 194 |  | 
|---|
| 195 |  | 
|---|
| 196 |     }; | 
|---|
| 197 |  | 
|---|
| 198 |         /** @} */ | 
|---|
| 199 |         /** @} */ | 
|---|
| 200 |  | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | #include "OgreHeaderSuffix.h" | 
|---|
| 204 |  | 
|---|
| 205 | #endif | 
|---|