diff options
Diffstat (limited to 'libziparchive/zip_archive_private.h')
-rw-r--r-- | libziparchive/zip_archive_private.h | 136 |
1 files changed, 96 insertions, 40 deletions
diff --git a/libziparchive/zip_archive_private.h b/libziparchive/zip_archive_private.h index 971db4f12..840f1afec 100644 --- a/libziparchive/zip_archive_private.h +++ b/libziparchive/zip_archive_private.h | |||
@@ -26,24 +26,83 @@ | |||
26 | 26 | ||
27 | #include <utils/FileMap.h> | 27 | #include <utils/FileMap.h> |
28 | #include <ziparchive/zip_archive.h> | 28 | #include <ziparchive/zip_archive.h> |
29 | #include "android-base/macros.h" | ||
30 | |||
31 | static const char* kErrorMessages[] = { | ||
32 | "Success", | ||
33 | "Iteration ended", | ||
34 | "Zlib error", | ||
35 | "Invalid file", | ||
36 | "Invalid handle", | ||
37 | "Duplicate entries in archive", | ||
38 | "Empty archive", | ||
39 | "Entry not found", | ||
40 | "Invalid offset", | ||
41 | "Inconsistent information", | ||
42 | "Invalid entry name", | ||
43 | "I/O error", | ||
44 | "File mapping failed", | ||
45 | }; | ||
46 | |||
47 | enum ErrorCodes : int32_t { | ||
48 | kIterationEnd = -1, | ||
49 | |||
50 | // We encountered a Zlib error when inflating a stream from this file. | ||
51 | // Usually indicates file corruption. | ||
52 | kZlibError = -2, | ||
53 | |||
54 | // The input file cannot be processed as a zip archive. Usually because | ||
55 | // it's too small, too large or does not have a valid signature. | ||
56 | kInvalidFile = -3, | ||
57 | |||
58 | // An invalid iteration / ziparchive handle was passed in as an input | ||
59 | // argument. | ||
60 | kInvalidHandle = -4, | ||
61 | |||
62 | // The zip archive contained two (or possibly more) entries with the same | ||
63 | // name. | ||
64 | kDuplicateEntry = -5, | ||
65 | |||
66 | // The zip archive contains no entries. | ||
67 | kEmptyArchive = -6, | ||
68 | |||
69 | // The specified entry was not found in the archive. | ||
70 | kEntryNotFound = -7, | ||
71 | |||
72 | // The zip archive contained an invalid local file header pointer. | ||
73 | kInvalidOffset = -8, | ||
74 | |||
75 | // The zip archive contained inconsistent entry information. This could | ||
76 | // be because the central directory & local file header did not agree, or | ||
77 | // if the actual uncompressed length or crc32 do not match their declared | ||
78 | // values. | ||
79 | kInconsistentInformation = -9, | ||
80 | |||
81 | // An invalid entry name was encountered. | ||
82 | kInvalidEntryName = -10, | ||
83 | |||
84 | // An I/O related system call (read, lseek, ftruncate, map) failed. | ||
85 | kIoError = -11, | ||
86 | |||
87 | // We were not able to mmap the central directory or entry contents. | ||
88 | kMmapFailed = -12, | ||
89 | |||
90 | kLastErrorCode = kMmapFailed, | ||
91 | }; | ||
29 | 92 | ||
30 | class MappedZipFile { | 93 | class MappedZipFile { |
31 | public: | 94 | public: |
32 | explicit MappedZipFile(const int fd) : | 95 | explicit MappedZipFile(const int fd) |
33 | has_fd_(true), | 96 | : has_fd_(true), fd_(fd), base_ptr_(nullptr), data_length_(0), read_pos_(0) {} |
34 | fd_(fd), | 97 | |
35 | base_ptr_(nullptr), | 98 | explicit MappedZipFile(void* address, size_t length) |
36 | data_length_(0), | 99 | : has_fd_(false), |
37 | read_pos_(0) {} | 100 | fd_(-1), |
38 | 101 | base_ptr_(address), | |
39 | explicit MappedZipFile(void* address, size_t length) : | 102 | data_length_(static_cast<off64_t>(length)), |
40 | has_fd_(false), | 103 | read_pos_(0) {} |
41 | fd_(-1), | 104 | |
42 | base_ptr_(address), | 105 | bool HasFd() const { return has_fd_; } |
43 | data_length_(static_cast<off64_t>(length)), | ||
44 | read_pos_(0) {} | ||
45 | |||
46 | bool HasFd() const {return has_fd_;} | ||
47 | 106 | ||
48 | int GetFileDescriptor() const; | 107 | int GetFileDescriptor() const; |
49 | 108 | ||
@@ -74,13 +133,11 @@ class MappedZipFile { | |||
74 | 133 | ||
75 | class CentralDirectory { | 134 | class CentralDirectory { |
76 | public: | 135 | public: |
77 | CentralDirectory(void) : | 136 | CentralDirectory(void) : base_ptr_(nullptr), length_(0) {} |
78 | base_ptr_(nullptr), | ||
79 | length_(0) {} | ||
80 | 137 | ||
81 | const uint8_t* GetBasePtr() const {return base_ptr_;} | 138 | const uint8_t* GetBasePtr() const { return base_ptr_; } |
82 | 139 | ||
83 | size_t GetMapLength() const {return length_;} | 140 | size_t GetMapLength() const { return length_; } |
84 | 141 | ||
85 | void Initialize(void* map_base_ptr, off64_t cd_start_offset, size_t cd_size); | 142 | void Initialize(void* map_base_ptr, off64_t cd_start_offset, size_t cd_size); |
86 | 143 | ||
@@ -109,25 +166,25 @@ struct ZipArchive { | |||
109 | uint32_t hash_table_size; | 166 | uint32_t hash_table_size; |
110 | ZipString* hash_table; | 167 | ZipString* hash_table; |
111 | 168 | ||
112 | ZipArchive(const int fd, bool assume_ownership) : | 169 | ZipArchive(const int fd, bool assume_ownership) |
113 | mapped_zip(fd), | 170 | : mapped_zip(fd), |
114 | close_file(assume_ownership), | 171 | close_file(assume_ownership), |
115 | directory_offset(0), | 172 | directory_offset(0), |
116 | central_directory(), | 173 | central_directory(), |
117 | directory_map(new android::FileMap()), | 174 | directory_map(new android::FileMap()), |
118 | num_entries(0), | 175 | num_entries(0), |
119 | hash_table_size(0), | 176 | hash_table_size(0), |
120 | hash_table(nullptr) {} | 177 | hash_table(nullptr) {} |
121 | 178 | ||
122 | ZipArchive(void* address, size_t length) : | 179 | ZipArchive(void* address, size_t length) |
123 | mapped_zip(address, length), | 180 | : mapped_zip(address, length), |
124 | close_file(false), | 181 | close_file(false), |
125 | directory_offset(0), | 182 | directory_offset(0), |
126 | central_directory(), | 183 | central_directory(), |
127 | directory_map(new android::FileMap()), | 184 | directory_map(new android::FileMap()), |
128 | num_entries(0), | 185 | num_entries(0), |
129 | hash_table_size(0), | 186 | hash_table_size(0), |
130 | hash_table(nullptr) {} | 187 | hash_table(nullptr) {} |
131 | 188 | ||
132 | ~ZipArchive() { | 189 | ~ZipArchive() { |
133 | if (close_file && mapped_zip.GetFileDescriptor() >= 0) { | 190 | if (close_file && mapped_zip.GetFileDescriptor() >= 0) { |
@@ -139,7 +196,6 @@ struct ZipArchive { | |||
139 | 196 | ||
140 | bool InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset, | 197 | bool InitializeCentralDirectory(const char* debug_file_name, off64_t cd_start_offset, |
141 | size_t cd_size); | 198 | size_t cd_size); |
142 | |||
143 | }; | 199 | }; |
144 | 200 | ||
145 | #endif // LIBZIPARCHIVE_ZIPARCHIVE_PRIVATE_H_ | 201 | #endif // LIBZIPARCHIVE_ZIPARCHIVE_PRIVATE_H_ |