• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 Google Inc. All rights reserved.
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 "common.h"
26 
27 namespace devtools_ijar {
28 
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.
zipattr_is_dir(u4 attr)32 inline bool zipattr_is_dir(u4 attr) { return (attr & 0x10) != 0; }
33 
34 // Convert a Unix file mode to a ZIP file attribute
mode_to_zipattr(mode_t m)35 inline u4 mode_to_zipattr(mode_t m) {
36   return (((u4) m) << 16) + ((m & S_IFDIR) != 0 ? 0x10 : 0);
37 }
38 
39 // Convert a ZIP file attribute to a Unix file mode
zipattr_to_mode(u4 attr)40 inline mode_t zipattr_to_mode(u4 attr) {
41   return ((mode_t) ((attr >> 16) & 0xffff));
42 }
43 
44 //
45 // Class interface for building ZIP files
46 //
47 class ZipBuilder {
48  public:
~ZipBuilder()49   virtual ~ZipBuilder() {}
50 
51   // Returns the text for the last error, or null on no last error.
52   virtual const char* GetError() = 0;
53 
54   // Add a new file to the ZIP, the file will have path "filename"
55   // and external attributes "attr". This function returns a pointer
56   // to a memory buffer to write the data of the file into. This buffer
57   // is owned by ZipBuilder and should not be free'd by the caller. The
58   // file length is then specified when the files is finished written
59   // using the FinishFile(size_t) function.
60   // On failure, returns NULL and GetError() will return an non-empty message.
61   virtual u1* NewFile(const char* filename, const u4 attr) = 0;
62 
63   // Finish writing a file and specify its length. After calling this method
64   // one should not reuse the pointer given by NewFile. The file can be
65   // compressed using the deflate algorithm by setting `compress` to true.
66   // By default, CRC32 are not computed as java tooling doesn't care, but
67   // computing it can be activated by setting `compute_crc` to true.
68   // On failure, returns -1 and GetError() will return an non-empty message.
69   virtual int FinishFile(size_t filelength,
70                          bool compress = false,
71                          bool compute_crc = false) = 0;
72 
73   // Write an empty file, it is equivalent to:
74   //   NewFile(filename, 0);
75   //   FinishFile(0);
76   // On failure, returns -1 and GetError() will return an non-empty message.
77   virtual int WriteEmptyFile(const char* filename) = 0;
78 
79   // Finish writing the ZIP file. This method can be called only once
80   // (subsequent calls will do nothing) and none of
81   // NewFile/FinishFile/WriteEmptyFile should be called after calling Finish. If
82   // this method was not called when the object is destroyed, it will be called.
83   // It is here as a convenience to get information on the final generated ZIP
84   // file.
85   // On failure, returns -1 and GetError() will return an non-empty message.
86   virtual int Finish() = 0;
87 
88   // Get the current size of the ZIP file. This size will not be matching the
89   // final ZIP file until Finish() has been called because Finish() is actually
90   // writing the central directory of the ZIP File.
91   virtual size_t GetSize() = 0;
92 
93   // Returns the current number of files stored in the ZIP.
94   virtual int GetNumberFiles() = 0;
95 
96   // Create a new ZipBuilder writing the file zip_file and the size of the
97   // output will be at most estimated_size. Use ZipBuilder::EstimateSize() or
98   // ZipExtractor::CalculateOuputLength() to have an estimated_size depending on
99   // a list of file to store.
100   // On failure, returns NULL. Refer to errno for error code.
101   static ZipBuilder* Create(const char* zip_file, u8 estimated_size);
102 
103   // Estimate the maximum size of the ZIP files containing files in the "files"
104   // null-terminated array.
105   // Returns 0 on error.
106   static u8 EstimateSize(char **files);
107 };
108 
109 //
110 // An abstract class to process data from a ZipExtractor.
111 // Derive from this class if you wish to process data from a ZipExtractor.
112 //
113 class ZipExtractorProcessor {
114  public:
~ZipExtractorProcessor()115   virtual ~ZipExtractorProcessor() {}
116 
117   // Tells whether to skip or process the file "filename". "attr" is the
118   // external file attributes and can be converted to unix mode using the
119   // zipattr_to_mode() function. This method is suppoed to returns true
120   // if the file should be processed and false if it should be skipped.
121   virtual bool Accept(const char* filename, const u4 attr) = 0;
122 
123   // Process a file accepted by Accept. The file "filename" has external
124   // attributes "attr" and length "size". The file content is accessible
125   // in the buffer pointed by "data".
126   virtual void Process(const char* filename, const u4 attr,
127                        const u1* data, const size_t size) = 0;
128 };
129 
130 //
131 // Class interface for reading ZIP files
132 //
133 class ZipExtractor {
134  public:
~ZipExtractor()135   virtual ~ZipExtractor() {}
136 
137   // Returns the text for the last error, or null on no last error.
138   virtual const char* GetError() = 0;
139 
140   // Process the next files, returns false if the end of ZIP file has been
141   // reached. The processor provided by the Create method will be called
142   // if a file is encountered. If false is returned, check the return value
143   // of GetError() for potential errors.
144   virtual bool ProcessNext() = 0;
145 
146   // Process the all files, returns -1 on error (GetError() will be populated
147   // on error).
148   virtual int ProcessAll();
149 
150   // Reset the file pointer to the beginning.
151   virtual void Reset() = 0;
152 
153   // Return the size of the ZIP file.
154   virtual size_t GetSize() = 0;
155 
156   // Return the size of the resulting zip file by keeping only file
157   // accepted by the processor and storing them uncompressed. This
158   // method can be used to create a ZipBuilder for storing a subset
159   // of the input files.
160   // On error, 0 is returned and GetError() returns a non-empty message.
161   virtual u8 CalculateOutputLength() = 0;
162 
163   // Create a ZipExtractor that extract the zip file "filename" and process
164   // it with "processor".
165   // On error, a null pointer is returned and the value of errno should be
166   // checked.
167   static ZipExtractor* Create(const char* filename,
168                               ZipExtractorProcessor *processor);
169 };
170 
171 }  // namespace devtools_ijar
172 
173 #endif  // INCLUDED_THIRD_PARTY_IJAR_ZIP_H
174