diff options
author | Narayan Kamath | 2017-10-27 03:27:20 -0500 |
---|---|---|
committer | Gerrit Code Review | 2017-10-27 03:27:20 -0500 |
commit | d2ea4471bc06d6bf9c122c70aaa668fc2900613f (patch) | |
tree | e234f060b9115b4e937de1fb852c3c3e981be11c /libziparchive | |
parent | 661b3827a6717c2958f1fde232b0ebe797f5d5ff (diff) | |
parent | 485b3640a40a9afda9f49103bc83963d81230f70 (diff) | |
download | platform-system-core-d2ea4471bc06d6bf9c122c70aaa668fc2900613f.tar.gz platform-system-core-d2ea4471bc06d6bf9c122c70aaa668fc2900613f.tar.xz platform-system-core-d2ea4471bc06d6bf9c122c70aaa668fc2900613f.zip |
Merge "zip_archive: Make Inflate a public API."
Diffstat (limited to 'libziparchive')
-rw-r--r-- | libziparchive/include/ziparchive/zip_archive.h | 40 | ||||
-rw-r--r-- | libziparchive/zip_archive.cc | 60 |
2 files changed, 61 insertions, 39 deletions
diff --git a/libziparchive/include/ziparchive/zip_archive.h b/libziparchive/include/ziparchive/zip_archive.h index 73ae68d3f..dd463d1f7 100644 --- a/libziparchive/include/ziparchive/zip_archive.h +++ b/libziparchive/include/ziparchive/zip_archive.h | |||
@@ -230,4 +230,44 @@ int32_t ProcessZipEntryContents(ZipArchiveHandle handle, ZipEntry* entry, | |||
230 | ProcessZipEntryFunction func, void* cookie); | 230 | ProcessZipEntryFunction func, void* cookie); |
231 | #endif | 231 | #endif |
232 | 232 | ||
233 | namespace zip_archive { | ||
234 | |||
235 | class Writer { | ||
236 | public: | ||
237 | virtual bool Append(uint8_t* buf, size_t buf_size) = 0; | ||
238 | virtual ~Writer(); | ||
239 | |||
240 | protected: | ||
241 | Writer() = default; | ||
242 | |||
243 | private: | ||
244 | Writer(const Writer&) = delete; | ||
245 | void operator=(const Writer&) = delete; | ||
246 | }; | ||
247 | |||
248 | class Reader { | ||
249 | public: | ||
250 | virtual bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const = 0; | ||
251 | virtual ~Reader(); | ||
252 | |||
253 | protected: | ||
254 | Reader() = default; | ||
255 | |||
256 | private: | ||
257 | Reader(const Reader&) = delete; | ||
258 | void operator=(const Reader&) = delete; | ||
259 | }; | ||
260 | |||
261 | /* | ||
262 | * Inflates the first |compressed_length| bytes of |reader| to a given |writer|. | ||
263 | * |crc_out| is set to the CRC32 checksum of the uncompressed data. | ||
264 | * | ||
265 | * Returns 0 on success and negative values on failure, for example if |reader| | ||
266 | * cannot supply the right amount of data, or if the number of bytes written to | ||
267 | * data does not match |uncompressed_length|. | ||
268 | */ | ||
269 | int32_t Inflate(const Reader& reader, const uint32_t compressed_length, | ||
270 | const uint32_t uncompressed_length, Writer* writer, uint64_t* crc_out); | ||
271 | } // namespace zip_archive | ||
272 | |||
233 | #endif // LIBZIPARCHIVE_ZIPARCHIVE_H_ | 273 | #endif // LIBZIPARCHIVE_ZIPARCHIVE_H_ |
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc index b7d04c721..f27cd2021 100644 --- a/libziparchive/zip_archive.cc +++ b/libziparchive/zip_archive.cc | |||
@@ -725,22 +725,10 @@ int32_t Next(void* cookie, ZipEntry* data, ZipString* name) { | |||
725 | return kIterationEnd; | 725 | return kIterationEnd; |
726 | } | 726 | } |
727 | 727 | ||
728 | class Writer { | ||
729 | public: | ||
730 | virtual bool Append(uint8_t* buf, size_t buf_size) = 0; | ||
731 | virtual ~Writer() {} | ||
732 | |||
733 | protected: | ||
734 | Writer() = default; | ||
735 | |||
736 | private: | ||
737 | DISALLOW_COPY_AND_ASSIGN(Writer); | ||
738 | }; | ||
739 | |||
740 | // A Writer that writes data to a fixed size memory region. | 728 | // A Writer that writes data to a fixed size memory region. |
741 | // The size of the memory region must be equal to the total size of | 729 | // The size of the memory region must be equal to the total size of |
742 | // the data appended to it. | 730 | // the data appended to it. |
743 | class MemoryWriter : public Writer { | 731 | class MemoryWriter : public zip_archive::Writer { |
744 | public: | 732 | public: |
745 | MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {} | 733 | MemoryWriter(uint8_t* buf, size_t size) : Writer(), buf_(buf), size_(size), bytes_written_(0) {} |
746 | 734 | ||
@@ -764,7 +752,7 @@ class MemoryWriter : public Writer { | |||
764 | 752 | ||
765 | // A Writer that appends data to a file |fd| at its current position. | 753 | // A Writer that appends data to a file |fd| at its current position. |
766 | // The file will be truncated to the end of the written data. | 754 | // The file will be truncated to the end of the written data. |
767 | class FileWriter : public Writer { | 755 | class FileWriter : public zip_archive::Writer { |
768 | public: | 756 | public: |
769 | // Creates a FileWriter for |fd| and prepare to write |entry| to it, | 757 | // Creates a FileWriter for |fd| and prepare to write |entry| to it, |
770 | // guaranteeing that the file descriptor is valid and that there's enough | 758 | // guaranteeing that the file descriptor is valid and that there's enough |
@@ -848,19 +836,7 @@ class FileWriter : public Writer { | |||
848 | size_t total_bytes_written_; | 836 | size_t total_bytes_written_; |
849 | }; | 837 | }; |
850 | 838 | ||
851 | class Reader { | 839 | class EntryReader : public zip_archive::Reader { |
852 | public: | ||
853 | virtual bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const = 0; | ||
854 | virtual ~Reader() {} | ||
855 | |||
856 | protected: | ||
857 | Reader() = default; | ||
858 | |||
859 | private: | ||
860 | DISALLOW_COPY_AND_ASSIGN(Reader); | ||
861 | }; | ||
862 | |||
863 | class EntryReader : public Reader { | ||
864 | public: | 840 | public: |
865 | EntryReader(const MappedZipFile& zip_file, const ZipEntry* entry) | 841 | EntryReader(const MappedZipFile& zip_file, const ZipEntry* entry) |
866 | : Reader(), zip_file_(zip_file), entry_(entry) {} | 842 | : Reader(), zip_file_(zip_file), entry_(entry) {} |
@@ -884,9 +860,14 @@ static inline int zlib_inflateInit2(z_stream* stream, int window_bits) { | |||
884 | } | 860 | } |
885 | #pragma GCC diagnostic pop | 861 | #pragma GCC diagnostic pop |
886 | 862 | ||
887 | static int32_t InflateReaderToWriter(const Reader& reader, const uint32_t compressed_length, | 863 | namespace zip_archive { |
888 | const uint32_t uncompressed_length, Writer* writer, | 864 | |
889 | uint64_t* crc_out) { | 865 | // Moved out of line to avoid -Wweak-vtables. |
866 | Reader::~Reader() {} | ||
867 | Writer::~Writer() {} | ||
868 | |||
869 | int32_t Inflate(const Reader& reader, const uint32_t compressed_length, | ||
870 | const uint32_t uncompressed_length, Writer* writer, uint64_t* crc_out) { | ||
890 | const size_t kBufSize = 32768; | 871 | const size_t kBufSize = 32768; |
891 | std::vector<uint8_t> read_buf(kBufSize); | 872 | std::vector<uint8_t> read_buf(kBufSize); |
892 | std::vector<uint8_t> write_buf(kBufSize); | 873 | std::vector<uint8_t> write_buf(kBufSize); |
@@ -987,17 +968,18 @@ static int32_t InflateReaderToWriter(const Reader& reader, const uint32_t compre | |||
987 | 968 | ||
988 | return 0; | 969 | return 0; |
989 | } | 970 | } |
971 | } // namespace zip_archive | ||
990 | 972 | ||
991 | static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, | 973 | static int32_t InflateEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, |
992 | Writer* writer, uint64_t* crc_out) { | 974 | zip_archive::Writer* writer, uint64_t* crc_out) { |
993 | const EntryReader reader(mapped_zip, entry); | 975 | const EntryReader reader(mapped_zip, entry); |
994 | 976 | ||
995 | return InflateReaderToWriter(reader, entry->compressed_length, entry->uncompressed_length, writer, | 977 | return zip_archive::Inflate(reader, entry->compressed_length, entry->uncompressed_length, writer, |
996 | crc_out); | 978 | crc_out); |
997 | } | 979 | } |
998 | 980 | ||
999 | static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, Writer* writer, | 981 | static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entry, |
1000 | uint64_t* crc_out) { | 982 | zip_archive::Writer* writer, uint64_t* crc_out) { |
1001 | static const uint32_t kBufSize = 32768; | 983 | static const uint32_t kBufSize = 32768; |
1002 | std::vector<uint8_t> buf(kBufSize); | 984 | std::vector<uint8_t> buf(kBufSize); |
1003 | 985 | ||
@@ -1030,7 +1012,7 @@ static int32_t CopyEntryToWriter(MappedZipFile& mapped_zip, const ZipEntry* entr | |||
1030 | return 0; | 1012 | return 0; |
1031 | } | 1013 | } |
1032 | 1014 | ||
1033 | int32_t ExtractToWriter(ZipArchiveHandle handle, ZipEntry* entry, Writer* writer) { | 1015 | int32_t ExtractToWriter(ZipArchiveHandle handle, ZipEntry* entry, zip_archive::Writer* writer) { |
1034 | ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle); | 1016 | ZipArchive* archive = reinterpret_cast<ZipArchive*>(handle); |
1035 | const uint16_t method = entry->method; | 1017 | const uint16_t method = entry->method; |
1036 | 1018 | ||
@@ -1060,12 +1042,12 @@ int32_t ExtractToWriter(ZipArchiveHandle handle, ZipEntry* entry, Writer* writer | |||
1060 | } | 1042 | } |
1061 | 1043 | ||
1062 | int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, uint8_t* begin, uint32_t size) { | 1044 | int32_t ExtractToMemory(ZipArchiveHandle handle, ZipEntry* entry, uint8_t* begin, uint32_t size) { |
1063 | std::unique_ptr<Writer> writer(new MemoryWriter(begin, size)); | 1045 | std::unique_ptr<zip_archive::Writer> writer(new MemoryWriter(begin, size)); |
1064 | return ExtractToWriter(handle, entry, writer.get()); | 1046 | return ExtractToWriter(handle, entry, writer.get()); |
1065 | } | 1047 | } |
1066 | 1048 | ||
1067 | int32_t ExtractEntryToFile(ZipArchiveHandle handle, ZipEntry* entry, int fd) { | 1049 | int32_t ExtractEntryToFile(ZipArchiveHandle handle, ZipEntry* entry, int fd) { |
1068 | std::unique_ptr<Writer> writer(FileWriter::Create(fd, entry)); | 1050 | std::unique_ptr<zip_archive::Writer> writer(FileWriter::Create(fd, entry)); |
1069 | if (writer.get() == nullptr) { | 1051 | if (writer.get() == nullptr) { |
1070 | return kIoError; | 1052 | return kIoError; |
1071 | } | 1053 | } |
@@ -1098,7 +1080,7 @@ ZipString::ZipString(const char* entry_name) : name(reinterpret_cast<const uint8 | |||
1098 | } | 1080 | } |
1099 | 1081 | ||
1100 | #if !defined(_WIN32) | 1082 | #if !defined(_WIN32) |
1101 | class ProcessWriter : public Writer { | 1083 | class ProcessWriter : public zip_archive::Writer { |
1102 | public: | 1084 | public: |
1103 | ProcessWriter(ProcessZipEntryFunction func, void* cookie) | 1085 | ProcessWriter(ProcessZipEntryFunction func, void* cookie) |
1104 | : Writer(), proc_function_(func), cookie_(cookie) {} | 1086 | : Writer(), proc_function_(func), cookie_(cookie) {} |