Damien Martin-Guillerez | bf6281d | 2015-11-19 16:41:33 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Damien Martin-Guillerez | 0844112 | 2015-05-28 11:12:31 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // zip.h -- .zip (.jar) file reading/writing routines. |
| 16 | // |
| 17 | // This file specifies the interface to use the ZIP implementation of ijar. |
| 18 | // |
| 19 | |
| 20 | #ifndef INCLUDED_THIRD_PARTY_IJAR_ZIP_H |
| 21 | #define INCLUDED_THIRD_PARTY_IJAR_ZIP_H |
| 22 | |
| 23 | #include <sys/stat.h> |
| 24 | |
| 25 | #include "third_party/ijar/common.h" |
| 26 | |
| 27 | namespace devtools_ijar { |
| 28 | |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 29 | // Tells if this is a directory entry from the mode. This method |
| 30 | // is safer than zipattr_to_mode(attr) & S_IFDIR because the unix |
| 31 | // mode might not be set in DOS zip files. |
| 32 | inline bool zipattr_is_dir(u4 attr) { return (attr & 0x10) != 0; } |
| 33 | |
Laszlo Csomor | 479e18d | 2016-12-02 15:11:08 +0000 | [diff] [blame^] | 34 | // Convert a ZIP file attribute to a Unix file permission mask. |
| 35 | inline mode_t zipattr_to_perm(u4 attr) { |
| 36 | return ((mode_t)((attr >> 16) & 0777)); |
Damien Martin-Guillerez | 0844112 | 2015-05-28 11:12:31 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // |
| 40 | // Class interface for building ZIP files |
| 41 | // |
| 42 | class ZipBuilder { |
| 43 | public: |
| 44 | virtual ~ZipBuilder() {} |
| 45 | |
| 46 | // Returns the text for the last error, or null on no last error. |
| 47 | virtual const char* GetError() = 0; |
| 48 | |
| 49 | // Add a new file to the ZIP, the file will have path "filename" |
| 50 | // and external attributes "attr". This function returns a pointer |
| 51 | // to a memory buffer to write the data of the file into. This buffer |
| 52 | // is owned by ZipBuilder and should not be free'd by the caller. The |
| 53 | // file length is then specified when the files is finished written |
| 54 | // using the FinishFile(size_t) function. |
| 55 | // On failure, returns NULL and GetError() will return an non-empty message. |
| 56 | virtual u1* NewFile(const char* filename, const u4 attr) = 0; |
| 57 | |
| 58 | // Finish writing a file and specify its length. After calling this method |
Damien Martin-Guillerez | 3a160e7 | 2015-08-31 12:22:14 +0000 | [diff] [blame] | 59 | // one should not reuse the pointer given by NewFile. The file can be |
| 60 | // compressed using the deflate algorithm by setting `compress` to true. |
Damien Martin-Guillerez | 4009d2c | 2015-09-11 14:44:53 +0000 | [diff] [blame] | 61 | // By default, CRC32 are not computed as java tooling doesn't care, but |
| 62 | // computing it can be activated by setting `compute_crc` to true. |
Damien Martin-Guillerez | 0844112 | 2015-05-28 11:12:31 +0000 | [diff] [blame] | 63 | // On failure, returns -1 and GetError() will return an non-empty message. |
Damien Martin-Guillerez | 4009d2c | 2015-09-11 14:44:53 +0000 | [diff] [blame] | 64 | virtual int FinishFile(size_t filelength, |
| 65 | bool compress = false, |
| 66 | bool compute_crc = false) = 0; |
Damien Martin-Guillerez | 0844112 | 2015-05-28 11:12:31 +0000 | [diff] [blame] | 67 | |
| 68 | // Write an empty file, it is equivalent to: |
| 69 | // NewFile(filename, 0); |
| 70 | // FinishFile(0); |
| 71 | // On failure, returns -1 and GetError() will return an non-empty message. |
| 72 | virtual int WriteEmptyFile(const char* filename) = 0; |
| 73 | |
| 74 | // Finish writing the ZIP file. This method can be called only once |
| 75 | // (subsequent calls will do nothing) and none of |
| 76 | // NewFile/FinishFile/WriteEmptyFile should be called after calling Finish. If |
| 77 | // this method was not called when the object is destroyed, it will be called. |
| 78 | // It is here as a convenience to get information on the final generated ZIP |
| 79 | // file. |
| 80 | // On failure, returns -1 and GetError() will return an non-empty message. |
| 81 | virtual int Finish() = 0; |
| 82 | |
| 83 | // Get the current size of the ZIP file. This size will not be matching the |
| 84 | // final ZIP file until Finish() has been called because Finish() is actually |
| 85 | // writing the central directory of the ZIP File. |
| 86 | virtual size_t GetSize() = 0; |
| 87 | |
| 88 | // Returns the current number of files stored in the ZIP. |
| 89 | virtual int GetNumberFiles() = 0; |
| 90 | |
| 91 | // Create a new ZipBuilder writing the file zip_file and the size of the |
| 92 | // output will be at most estimated_size. Use ZipBuilder::EstimateSize() or |
| 93 | // ZipExtractor::CalculateOuputLength() to have an estimated_size depending on |
| 94 | // a list of file to store. |
| 95 | // On failure, returns NULL. Refer to errno for error code. |
| 96 | static ZipBuilder* Create(const char* zip_file, u8 estimated_size); |
| 97 | |
| 98 | // Estimate the maximum size of the ZIP files containing files in the "files" |
| 99 | // null-terminated array. |
| 100 | // Returns 0 on error. |
Rumou Duan | a518f63 | 2016-09-21 21:59:01 +0000 | [diff] [blame] | 101 | static u8 EstimateSize(char const* const* files, char const* const* zip_paths, |
| 102 | int nb_entries); |
Damien Martin-Guillerez | 0844112 | 2015-05-28 11:12:31 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | // |
| 106 | // An abstract class to process data from a ZipExtractor. |
| 107 | // Derive from this class if you wish to process data from a ZipExtractor. |
| 108 | // |
| 109 | class ZipExtractorProcessor { |
| 110 | public: |
| 111 | virtual ~ZipExtractorProcessor() {} |
| 112 | |
| 113 | // Tells whether to skip or process the file "filename". "attr" is the |
| 114 | // external file attributes and can be converted to unix mode using the |
| 115 | // zipattr_to_mode() function. This method is suppoed to returns true |
| 116 | // if the file should be processed and false if it should be skipped. |
| 117 | virtual bool Accept(const char* filename, const u4 attr) = 0; |
| 118 | |
| 119 | // Process a file accepted by Accept. The file "filename" has external |
| 120 | // attributes "attr" and length "size". The file content is accessible |
| 121 | // in the buffer pointed by "data". |
| 122 | virtual void Process(const char* filename, const u4 attr, |
| 123 | const u1* data, const size_t size) = 0; |
| 124 | }; |
| 125 | |
| 126 | // |
| 127 | // Class interface for reading ZIP files |
| 128 | // |
| 129 | class ZipExtractor { |
| 130 | public: |
| 131 | virtual ~ZipExtractor() {} |
| 132 | |
| 133 | // Returns the text for the last error, or null on no last error. |
| 134 | virtual const char* GetError() = 0; |
| 135 | |
| 136 | // Process the next files, returns false if the end of ZIP file has been |
| 137 | // reached. The processor provided by the Create method will be called |
| 138 | // if a file is encountered. If false is returned, check the return value |
| 139 | // of GetError() for potential errors. |
| 140 | virtual bool ProcessNext() = 0; |
| 141 | |
| 142 | // Process the all files, returns -1 on error (GetError() will be populated |
| 143 | // on error). |
| 144 | virtual int ProcessAll(); |
| 145 | |
| 146 | // Reset the file pointer to the beginning. |
| 147 | virtual void Reset() = 0; |
| 148 | |
| 149 | // Return the size of the ZIP file. |
| 150 | virtual size_t GetSize() = 0; |
| 151 | |
| 152 | // Return the size of the resulting zip file by keeping only file |
| 153 | // accepted by the processor and storing them uncompressed. This |
| 154 | // method can be used to create a ZipBuilder for storing a subset |
| 155 | // of the input files. |
| 156 | // On error, 0 is returned and GetError() returns a non-empty message. |
| 157 | virtual u8 CalculateOutputLength() = 0; |
| 158 | |
| 159 | // Create a ZipExtractor that extract the zip file "filename" and process |
| 160 | // it with "processor". |
| 161 | // On error, a null pointer is returned and the value of errno should be |
| 162 | // checked. |
| 163 | static ZipExtractor* Create(const char* filename, |
| 164 | ZipExtractorProcessor *processor); |
| 165 | }; |
| 166 | |
| 167 | } // namespace devtools_ijar |
| 168 | |
| 169 | #endif // INCLUDED_THIRD_PARTY_IJAR_ZIP_H |