1 /*****************************************************************
3 | Neptune - Zip Support
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
38 #include "NptConfig.h"
39 #include "NptStreams.h"
43 /*----------------------------------------------------------------------
45 +---------------------------------------------------------------------*/
46 class NPT_ZipInflateState
;
47 class NPT_ZipDeflateState
;
49 /*----------------------------------------------------------------------
51 +---------------------------------------------------------------------*/
52 const unsigned int NPT_ZIP_FILE_FLAG_ENCRYPTED
= 0x01;
53 const unsigned int NPT_ZIP_FILE_COMPRESSION_METHOD_NONE
= 0x00;
54 const unsigned int NPT_ZIP_FILE_COMPRESSION_METHOD_DEFLATE
= 0x08;
62 Entry(const unsigned char* data
, NPT_Size data_available
);
65 NPT_UInt16 m_CompressionMethod
;
67 NPT_LargeSize m_CompressedSize
;
68 NPT_LargeSize m_UncompressedSize
;
69 NPT_UInt16 m_DiskNumber
;
70 NPT_UInt16 m_InternalFileAttributes
;
71 NPT_UInt32 m_ExternalFileAttributes
;
72 NPT_Position m_RelativeOffset
;
73 NPT_UInt32 m_DirectoryEntrySize
;
77 static NPT_Result
Parse(NPT_InputStream
& stream
, NPT_ZipFile
*& file
);
78 static NPT_Result
GetInputStream(Entry
& entry
, NPT_InputStreamReference
& zip_stream
, NPT_InputStream
*& file_stream
);
81 NPT_Array
<Entry
>& GetEntries() { return m_Entries
; }
88 NPT_Array
<Entry
> m_Entries
;
91 /*----------------------------------------------------------------------
93 +---------------------------------------------------------------------*/
94 const int NPT_ZIP_COMPRESSION_LEVEL_DEFAULT
= -1;
95 const int NPT_ZIP_COMPRESSION_LEVEL_MIN
= 0;
96 const int NPT_ZIP_COMPRESSION_LEVEL_MAX
= 9;
97 const int NPT_ZIP_COMPRESSION_LEVEL_NONE
= 0;
102 static NPT_Result
MapError(int err
);
105 * Compressed data format
113 * Deflate (i.e compress) a buffer
115 static NPT_Result
Deflate(const NPT_DataBuffer
& in
,
117 int compression_level
= NPT_ZIP_COMPRESSION_LEVEL_DEFAULT
,
118 Format format
= ZLIB
);
121 * Inflate (i.e decompress) a buffer
123 static NPT_Result
Inflate(const NPT_DataBuffer
& in
,
128 * Deflate (i.e compress) a file
130 static NPT_Result
Deflate(NPT_File
& in
,
132 int compression_level
= NPT_ZIP_COMPRESSION_LEVEL_DEFAULT
,
133 Format format
= GZIP
);
137 /*----------------------------------------------------------------------
138 | NPT_ZipInflatingInputStream
139 +---------------------------------------------------------------------*/
140 class NPT_ZipInflatingInputStream
: public NPT_InputStream
143 NPT_ZipInflatingInputStream(NPT_InputStreamReference
& source
, bool raw
= false);
144 ~NPT_ZipInflatingInputStream() override
;
146 // NPT_InputStream methods
147 NPT_Result
Read(void* buffer
,
148 NPT_Size bytes_to_read
,
149 NPT_Size
* bytes_read
= NULL
) override
;
150 NPT_Result
Seek(NPT_Position offset
) override
;
151 NPT_Result
Tell(NPT_Position
& offset
) override
;
152 NPT_Result
GetSize(NPT_LargeSize
& size
) override
;
153 NPT_Result
GetAvailable(NPT_LargeSize
& available
) override
;
156 NPT_InputStreamReference m_Source
;
157 NPT_Position m_Position
;
158 NPT_ZipInflateState
* m_State
;
159 NPT_DataBuffer m_Buffer
;
162 /*----------------------------------------------------------------------
163 | NPT_ZipInflatingOutputStream
164 +---------------------------------------------------------------------*/
166 /*----------------------------------------------------------------------
167 | NPT_ZipDeflatingInputStream
168 +---------------------------------------------------------------------*/
169 class NPT_ZipDeflatingInputStream
: public NPT_InputStream
172 NPT_ZipDeflatingInputStream(NPT_InputStreamReference
& source
,
173 int compression_level
= NPT_ZIP_COMPRESSION_LEVEL_DEFAULT
,
174 NPT_Zip::Format format
= NPT_Zip::ZLIB
);
175 ~NPT_ZipDeflatingInputStream() override
;
177 // NPT_InputStream methods
178 NPT_Result
Read(void* buffer
,
179 NPT_Size bytes_to_read
,
180 NPT_Size
* bytes_read
= NULL
) override
;
181 NPT_Result
Seek(NPT_Position offset
) override
;
182 NPT_Result
Tell(NPT_Position
& offset
) override
;
183 NPT_Result
GetSize(NPT_LargeSize
& size
) override
;
184 NPT_Result
GetAvailable(NPT_LargeSize
& available
) override
;
187 NPT_InputStreamReference m_Source
;
188 NPT_Position m_Position
;
190 NPT_ZipDeflateState
* m_State
;
191 NPT_DataBuffer m_Buffer
;
194 /*----------------------------------------------------------------------
195 | NPT_ZipDeflatingOutputStream
196 +---------------------------------------------------------------------*/
197 /*class NPT_ZipDeflatingOutputStream : public NPT_OutputStream
200 NPT_ZipDeflatingOutputStream(NPT_OutputStreamReference& source,
201 int compression_level = NPT_ZIP_COMPRESSION_LEVEL_DEFAULT,
202 NPT_Zip::Format format = NPT_Zip::ZLIB);
203 NPT_ZipDeflatingOutputStream();
205 // NPT_OutputStream methods
206 virtual NPT_Result Write(void* buffer,
207 NPT_Size bytes_to_write,
208 NPT_Size* bytes_written = NULL);
209 virtual NPT_Result Seek(NPT_Position offset);
210 virtual NPT_Result Tell(NPT_Position& offset);
213 NPT_OutputStreamReference m_Output;
214 NPT_Position m_Position;
216 NPT_ZipDeflateState* m_State;
217 NPT_DataBuffer m_Buffer;
220 #endif // _NPT_ZIP_H_