Update V8 to version 4.7.53.
[chromium-blink-merge.git] / third_party / ijar / zip.h
blobc548cb3cdb08d6e527afd8a4b0f68667a680f96e
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.
15 // zip.h -- .zip (.jar) file reading/writing routines.
17 // This file specifies the interface to use the ZIP implementation of ijar.
20 #ifndef INCLUDED_THIRD_PARTY_IJAR_ZIP_H
21 #define INCLUDED_THIRD_PARTY_IJAR_ZIP_H
23 #include <sys/stat.h>
25 #include "third_party/ijar/common.h"
27 namespace devtools_ijar {
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; }
34 // Convert a Unix file mode to a ZIP file attribute
35 inline u4 mode_to_zipattr(mode_t m) {
36 return (((u4) m) << 16) + ((m & S_IFDIR) != 0 ? 0x10 : 0);
39 // Convert a ZIP file attribute to a Unix file mode
40 inline mode_t zipattr_to_mode(u4 attr) {
41 return ((mode_t) ((attr >> 16) & 0xffff));
45 // Class interface for building ZIP files
47 class ZipBuilder {
48 public:
49 virtual ~ZipBuilder() {}
51 // Returns the text for the last error, or null on no last error.
52 virtual const char* GetError() = 0;
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;
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 // On failure, returns -1 and GetError() will return an non-empty message.
67 virtual int FinishFile(size_t filelength, bool compress = false) = 0;
69 // Write an empty file, it is equivalent to:
70 // NewFile(filename, 0);
71 // FinishFile(0);
72 // On failure, returns -1 and GetError() will return an non-empty message.
73 virtual int WriteEmptyFile(const char* filename) = 0;
75 // Finish writing the ZIP file. This method can be called only once
76 // (subsequent calls will do nothing) and none of
77 // NewFile/FinishFile/WriteEmptyFile should be called after calling Finish. If
78 // this method was not called when the object is destroyed, it will be called.
79 // It is here as a convenience to get information on the final generated ZIP
80 // file.
81 // On failure, returns -1 and GetError() will return an non-empty message.
82 virtual int Finish() = 0;
84 // Get the current size of the ZIP file. This size will not be matching the
85 // final ZIP file until Finish() has been called because Finish() is actually
86 // writing the central directory of the ZIP File.
87 virtual size_t GetSize() = 0;
89 // Returns the current number of files stored in the ZIP.
90 virtual int GetNumberFiles() = 0;
92 // Create a new ZipBuilder writing the file zip_file and the size of the
93 // output will be at most estimated_size. Use ZipBuilder::EstimateSize() or
94 // ZipExtractor::CalculateOuputLength() to have an estimated_size depending on
95 // a list of file to store.
96 // On failure, returns NULL. Refer to errno for error code.
97 static ZipBuilder* Create(const char* zip_file, u8 estimated_size);
99 // Estimate the maximum size of the ZIP files containing files in the "files"
100 // null-terminated array.
101 // Returns 0 on error.
102 static u8 EstimateSize(char **files);
106 // An abstract class to process data from a ZipExtractor.
107 // Derive from this class if you wish to process data from a ZipExtractor.
109 class ZipExtractorProcessor {
110 public:
111 virtual ~ZipExtractorProcessor() {}
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;
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;
127 // Class interface for reading ZIP files
129 class ZipExtractor {
130 public:
131 virtual ~ZipExtractor() {}
133 // Returns the text for the last error, or null on no last error.
134 virtual const char* GetError() = 0;
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;
142 // Process the all files, returns -1 on error (GetError() will be populated
143 // on error).
144 virtual int ProcessAll();
146 // Reset the file pointer to the beginning.
147 virtual void Reset() = 0;
149 // Return the size of the ZIP file.
150 virtual size_t GetSize() = 0;
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;
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);
167 } // namespace devtools_ijar
169 #endif // INCLUDED_THIRD_PARTY_IJAR_ZIP_H