| OLD | NEW |
| 1 // Copyright 2014 The Chromium OS Authors. All rights reserved. | 1 // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VOLUME_ARCHIVE_LIBARCHIVE_H_ | 5 #ifndef VOLUME_ARCHIVE_MINIZIP_H_ |
| 6 #define VOLUME_ARCHIVE_LIBARCHIVE_H_ | 6 #define VOLUME_ARCHIVE_MINIZIP_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "third_party/zlib/contrib/minizip/unzip.h" | 10 #include "third_party/zlib/contrib/minizip/unzip.h" |
| 11 #include "third_party/zlib/contrib/minizip/zip.h" | 11 #include "third_party/zlib/contrib/minizip/zip.h" |
| 12 | 12 |
| 13 #include "volume_archive.h" | 13 #include "volume_archive.h" |
| 14 | 14 |
| 15 // A namespace with constants used by VolumeArchiveLibarchive. | 15 // A namespace with constants used by VolumeArchiveMinizip. |
| 16 namespace volume_archive_constants { | 16 namespace volume_archive_constants { |
| 17 | 17 |
| 18 const char kArchiveReadNewError[] = "Could not allocate archive."; | 18 const char kArchiveReadNewError[] = "Could not allocate archive."; |
| 19 const char kFileNotFound[] = "File not found for read data request."; | 19 const char kFileNotFound[] = "File not found for read data request."; |
| 20 const char kVolumeReaderError[] = "VolumeReader failed to retrieve data."; | 20 const char kVolumeReaderError[] = "VolumeReader failed to retrieve data."; |
| 21 const char kArchiveOpenError[] = "Failed to open archive."; | 21 const char kArchiveOpenError[] = "Failed to open archive."; |
| 22 const char kArchiveNextHeaderError[] = | 22 const char kArchiveNextHeaderError[] = |
| 23 "Failed to open current file in archive."; | 23 "Failed to open current file in archive."; |
| 24 const char kArchiveReadDataError[] = "Failed to read archive data."; | 24 const char kArchiveReadDataError[] = "Failed to read archive data."; |
| 25 const char kArchiveReadFreeError[] = "Failed to close archive."; | 25 const char kArchiveReadFreeError[] = "Failed to close archive."; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 42 | 42 |
| 43 // Maximum length of filename in zip archive. | 43 // Maximum length of filename in zip archive. |
| 44 const int kZipMaxPath = 256; | 44 const int kZipMaxPath = 256; |
| 45 | 45 |
| 46 // The size of the static cache. We need at least 64KB to cache whole | 46 // The size of the static cache. We need at least 64KB to cache whole |
| 47 // 'end of central directory' data. | 47 // 'end of central directory' data. |
| 48 const int64_t kStaticCacheSize = 128 * 1024; | 48 const int64_t kStaticCacheSize = 128 * 1024; |
| 49 | 49 |
| 50 } // namespace volume_archive_constants | 50 } // namespace volume_archive_constants |
| 51 | 51 |
| 52 class VolumeArchiveLibarchive; | 52 class VolumeArchiveMinizip; |
| 53 | 53 |
| 54 // A namespace with custom functions passed to minizip. | 54 // A namespace with custom functions passed to minizip. |
| 55 namespace volume_archive_functions { | 55 namespace volume_archive_functions { |
| 56 | 56 |
| 57 int64_t DynamicCache(VolumeArchiveLibarchive* archive, int64_t unz_size); | 57 int64_t DynamicCache(VolumeArchiveMinizip* archive, int64_t unz_size); |
| 58 | 58 |
| 59 uLong CustomArchiveRead(void* archive, void* stream, void* buf, uLong size); | 59 uLong CustomArchiveRead(void* archive, void* stream, void* buf, uLong size); |
| 60 | 60 |
| 61 // Returns the offset from the beginning of the data. | 61 // Returns the offset from the beginning of the data. |
| 62 long CustomArchiveTell(void* archive, void* stream); | 62 long CustomArchiveTell(void* archive, void* stream); |
| 63 | 63 |
| 64 // Moves the current offset to the specified position. | 64 // Moves the current offset to the specified position. |
| 65 long CustomArchiveSeek(void* archive, | 65 long CustomArchiveSeek(void* archive, |
| 66 void* stream, | 66 void* stream, |
| 67 uLong offset, | 67 uLong offset, |
| 68 int origin); | 68 int origin); |
| 69 | 69 |
| 70 } // compressor_archive_functions | 70 } // compressor_archive_functions |
| 71 | 71 |
| 72 | 72 |
| 73 class VolumeArchiveLibarchive; | 73 class VolumeArchiveMinizip; |
| 74 | 74 |
| 75 // Defines an implementation of VolumeArchive that wraps all libarchive | 75 // Defines an implementation of VolumeArchive that wraps all minizip |
| 76 // operations. | 76 // operations. |
| 77 class VolumeArchiveLibarchive : public VolumeArchive { | 77 class VolumeArchiveMinizip : public VolumeArchive { |
| 78 public: | 78 public: |
| 79 explicit VolumeArchiveLibarchive(VolumeReader* reader); | 79 explicit VolumeArchiveMinizip(VolumeReader* reader); |
| 80 | 80 |
| 81 virtual ~VolumeArchiveLibarchive(); | 81 virtual ~VolumeArchiveMinizip(); |
| 82 | 82 |
| 83 // See volume_archive_interface.h. | 83 // See volume_archive_interface.h. |
| 84 virtual bool Init(const std::string& encoding); | 84 virtual bool Init(const std::string& encoding); |
| 85 | 85 |
| 86 // See volume_archive_interface.h. | 86 // See volume_archive_interface.h. |
| 87 virtual VolumeArchive::Result GetCurrentFileInfo(std::string* path_name, | 87 virtual VolumeArchive::Result GetCurrentFileInfo(std::string* path_name, |
| 88 int64_t* size, | 88 int64_t* size, |
| 89 bool* is_directory, | 89 bool* is_directory, |
| 90 time_t* modification_time); | 90 time_t* modification_time); |
| 91 | 91 |
| 92 virtual VolumeArchive::Result GoToNextFile(); | 92 virtual VolumeArchive::Result GoToNextFile(); |
| 93 | 93 |
| 94 // See volume_archive_interface.h. | 94 // See volume_archive_interface.h. |
| 95 virtual bool SeekHeader(const std::string& path_name); | 95 virtual bool SeekHeader(const std::string& path_name); |
| 96 | 96 |
| 97 // See volume_archive_interface.h. | 97 // See volume_archive_interface.h. |
| 98 virtual int64_t ReadData(int64_t offset, int64_t length, const char** buffer); | 98 virtual int64_t ReadData(int64_t offset, int64_t length, const char** buffer); |
| 99 | 99 |
| 100 // See volume_archive_interface.h. | 100 // See volume_archive_interface.h. |
| 101 virtual void MaybeDecompressAhead(); | 101 virtual void MaybeDecompressAhead(); |
| 102 | 102 |
| 103 // See volume_archive_interface.h. | 103 // See volume_archive_interface.h. |
| 104 virtual bool Cleanup(); | 104 virtual bool Cleanup(); |
| 105 | 105 |
| 106 int64_t reader_data_size() const { return reader_data_size_; } | 106 int64_t reader_data_size() const { return reader_data_size_; } |
| 107 | 107 |
| 108 // Custom functions need to access private variables of | 108 // Custom functions need to access private variables of |
| 109 // CompressorArchiveLibarchive frequently. | 109 // CompressorArchiveMinizip frequently. |
| 110 friend int64_t volume_archive_functions::DynamicCache( | 110 friend int64_t volume_archive_functions::DynamicCache( |
| 111 VolumeArchiveLibarchive* va, int64_t unz_size); | 111 VolumeArchiveMinizip* va, int64_t unz_size); |
| 112 | 112 |
| 113 friend uLong volume_archive_functions::CustomArchiveRead( | 113 friend uLong volume_archive_functions::CustomArchiveRead( |
| 114 void* archive, void* stream, void* buf, uLong size); | 114 void* archive, void* stream, void* buf, uLong size); |
| 115 | 115 |
| 116 friend long volume_archive_functions::CustomArchiveTell( | 116 friend long volume_archive_functions::CustomArchiveTell( |
| 117 void* archive, void* stream); | 117 void* archive, void* stream); |
| 118 | 118 |
| 119 friend long volume_archive_functions::CustomArchiveSeek( | 119 friend long volume_archive_functions::CustomArchiveSeek( |
| 120 void* archive, void* stream, uLong offset, int origin); | 120 void* archive, void* stream, uLong offset, int origin); |
| 121 | 121 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 // The offset from which static_cache_ has the data of the archive. | 155 // The offset from which static_cache_ has the data of the archive. |
| 156 int64_t static_cache_offset_; | 156 int64_t static_cache_offset_; |
| 157 | 157 |
| 158 // The size of the data in static_cache_. The End Of Central Directory header | 158 // The size of the data in static_cache_. The End Of Central Directory header |
| 159 // is guaranteed to be in the last 64(global comment) + 1(other fields) of the | 159 // is guaranteed to be in the last 64(global comment) + 1(other fields) of the |
| 160 // file. This cache is used to store the header. | 160 // file. This cache is used to store the header. |
| 161 int64_t static_cache_size_; | 161 int64_t static_cache_size_; |
| 162 | 162 |
| 163 // The data offset, which will be offset + length after last read | 163 // The data offset, which will be offset + length after last read |
| 164 // operation, where offset and length are method parameters for | 164 // operation, where offset and length are method parameters for |
| 165 // VolumeArchiveLibarchive::ReadData. Data offset is used to improve | 165 // VolumeArchiveMinizip::ReadData. Data offset is used to improve |
| 166 // performance for consecutive calls to VolumeArchiveLibarchive::ReadData. | 166 // performance for consecutive calls to VolumeArchiveMinizip::ReadData. |
| 167 // | 167 // |
| 168 // Intead of starting the read from the beginning for every | 168 // Intead of starting the read from the beginning for every |
| 169 // VolumeArchiveLibarchive::ReadData, the next call will start | 169 // VolumeArchiveMinizip::ReadData, the next call will start |
| 170 // from last_read_data_offset_ in case the offset parameter of | 170 // from last_read_data_offset_ in case the offset parameter of |
| 171 // VolumeArchiveLibarchive::ReadData has the same value as | 171 // VolumeArchiveMinizip::ReadData has the same value as |
| 172 // last_read_data_offset_. This avoids decompressing again the bytes at | 172 // last_read_data_offset_. This avoids decompressing again the bytes at |
| 173 // the begninning of the file, which is the average case scenario. | 173 // the begninning of the file, which is the average case scenario. |
| 174 // But in case the offset parameter is different than last_read_data_offset_, | 174 // But in case the offset parameter is different than last_read_data_offset_, |
| 175 // then dummy_buffer_ will be used to ignore unused bytes. | 175 // then dummy_buffer_ will be used to ignore unused bytes. |
| 176 int64_t last_read_data_offset_; | 176 int64_t last_read_data_offset_; |
| 177 | 177 |
| 178 // The length of the last VolumeArchiveLibarchive::ReadData. Used for | 178 // The length of the last VolumeArchiveMinizip::ReadData. Used for |
| 179 // decompress ahead. | 179 // decompress ahead. |
| 180 int64_t last_read_data_length_; | 180 int64_t last_read_data_length_; |
| 181 | 181 |
| 182 // Dummy buffer for unused data read using VolumeArchiveLibarchive::ReadData. | 182 // Dummy buffer for unused data read using VolumeArchiveMinizip::ReadData. |
| 183 // Sometimes VolumeArchiveLibarchive::ReadData can require reading from | 183 // Sometimes VolumeArchiveMinizip::ReadData can require reading from |
| 184 // offsets different from last_read_data_offset_. In this case some bytes | 184 // offsets different from last_read_data_offset_. In this case some bytes |
| 185 // must be skipped. Because seeking is not possible inside compressed files, | 185 // must be skipped. Because seeking is not possible inside compressed files, |
| 186 // the bytes will be discarded using this buffer. | 186 // the bytes will be discarded using this buffer. |
| 187 char dummy_buffer_[volume_archive_constants::kDummyBufferSize]; | 187 char dummy_buffer_[volume_archive_constants::kDummyBufferSize]; |
| 188 | 188 |
| 189 // The address where the decompressed data starting from | 189 // The address where the decompressed data starting from |
| 190 // decompressed_offset_ is stored. It should point to a valid location | 190 // decompressed_offset_ is stored. It should point to a valid location |
| 191 // inside decompressed_data_buffer_. Necesssary in order to NOT throw | 191 // inside decompressed_data_buffer_. Necesssary in order to NOT throw |
| 192 // away unused decompressed bytes as throwing them away would mean in some | 192 // away unused decompressed bytes as throwing them away would mean in some |
| 193 // situations restarting decompressing the file from the beginning. | 193 // situations restarting decompressing the file from the beginning. |
| 194 char* decompressed_data_; | 194 char* decompressed_data_; |
| 195 | 195 |
| 196 // The actual buffer that contains the decompressed data. | 196 // The actual buffer that contains the decompressed data. |
| 197 char decompressed_data_buffer_ | 197 char decompressed_data_buffer_ |
| 198 [volume_archive_constants::kDecompressBufferSize]; | 198 [volume_archive_constants::kDecompressBufferSize]; |
| 199 | 199 |
| 200 // The size of valid data starting from decompressed_data_ that is stored | 200 // The size of valid data starting from decompressed_data_ that is stored |
| 201 // inside decompressed_data_buffer_. | 201 // inside decompressed_data_buffer_. |
| 202 int64_t decompressed_data_size_; | 202 int64_t decompressed_data_size_; |
| 203 | 203 |
| 204 // True if VolumeArchiveLibarchive::DecompressData failed. | 204 // True if VolumeArchiveMinizip::DecompressData failed. |
| 205 bool decompressed_error_; | 205 bool decompressed_error_; |
| 206 }; | 206 }; |
| 207 | 207 |
| 208 #endif // VOLUME_ARCHIVE_LIBARCHIVE_H_ | 208 #endif // VOLUME_ARCHIVE_MINIZIP_H_ |
| OLD | NEW |