kodi
NptZip.h
1 /*****************************************************************
2 |
3 | Neptune - Zip Support
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
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.
18 |
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.
29 |
30  ****************************************************************/
31 
32 #ifndef _NPT_ZIP_H_
33 #define _NPT_ZIP_H_
34 
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptConfig.h"
39 #include "NptStreams.h"
40 #include "NptArray.h"
41 #include "NptFile.h"
42 
43 /*----------------------------------------------------------------------
44 | class references
45 +---------------------------------------------------------------------*/
46 class NPT_ZipInflateState;
47 class NPT_ZipDeflateState;
48 
49 /*----------------------------------------------------------------------
50 | NPT_ZipFile
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;
55 
57 {
58 public:
59  // types
60  class Entry {
61  public:
62  Entry(const unsigned char* data, NPT_Size data_available);
63  NPT_String m_Name;
64  NPT_UInt16 m_Flags;
65  NPT_UInt16 m_CompressionMethod;
66  NPT_UInt32 m_Crc32;
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;
74  };
75 
76  // class methods
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);
79 
80  // accessors
81  NPT_Array<Entry>& GetEntries() { return m_Entries; }
82 
83 private:
84  // constructor
85  NPT_ZipFile();
86 
87  // members
88  NPT_Array<Entry> m_Entries;
89 };
90 
91 /*----------------------------------------------------------------------
92 | NPT_Zip
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;
98 class NPT_Zip
99 {
100 public:
101  // class methods
102  static NPT_Result MapError(int err);
103 
107  typedef enum {
108  ZLIB,
109  GZIP
110  } Format;
111 
115  static NPT_Result Deflate(const NPT_DataBuffer& in,
116  NPT_DataBuffer& out,
117  int compression_level = NPT_ZIP_COMPRESSION_LEVEL_DEFAULT,
118  Format format = ZLIB);
119 
123  static NPT_Result Inflate(const NPT_DataBuffer& in,
124  NPT_DataBuffer& out,
125  bool raw = false);
126 
130  static NPT_Result Deflate(NPT_File& in,
131  NPT_File& out,
132  int compression_level = NPT_ZIP_COMPRESSION_LEVEL_DEFAULT,
133  Format format = GZIP);
134 
135 };
136 
137 /*----------------------------------------------------------------------
138 | NPT_ZipInflatingInputStream
139 +---------------------------------------------------------------------*/
141 {
142 public:
143  NPT_ZipInflatingInputStream(NPT_InputStreamReference& source, bool raw = false);
144  ~NPT_ZipInflatingInputStream() override;
145 
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;
154 
155 private:
156  NPT_InputStreamReference m_Source;
157  NPT_Position m_Position;
158  NPT_ZipInflateState* m_State;
159  NPT_DataBuffer m_Buffer;
160 };
161 
162 /*----------------------------------------------------------------------
163 | NPT_ZipInflatingOutputStream
164 +---------------------------------------------------------------------*/
165 
166 /*----------------------------------------------------------------------
167 | NPT_ZipDeflatingInputStream
168 +---------------------------------------------------------------------*/
170 {
171 public:
173  int compression_level = NPT_ZIP_COMPRESSION_LEVEL_DEFAULT,
174  NPT_Zip::Format format = NPT_Zip::ZLIB);
175  ~NPT_ZipDeflatingInputStream() override;
176 
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;
185 
186 private:
187  NPT_InputStreamReference m_Source;
188  NPT_Position m_Position;
189  bool m_Eos;
190  NPT_ZipDeflateState* m_State;
191  NPT_DataBuffer m_Buffer;
192 };
193 
194 /*----------------------------------------------------------------------
195 | NPT_ZipDeflatingOutputStream
196 +---------------------------------------------------------------------*/
197 /*class NPT_ZipDeflatingOutputStream : public NPT_OutputStream
198 {
199 public:
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();
204 
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);
211 
212 private:
213  NPT_OutputStreamReference m_Output;
214  NPT_Position m_Position;
215  bool m_Eos;
216  NPT_ZipDeflateState* m_State;
217  NPT_DataBuffer m_Buffer;
218 }; */
219 
220 #endif // _NPT_ZIP_H_
Definition: NptStreams.h:60
Definition: NptZip.h:56
Definition: NptArray.h:54
Definition: NptZip.h:60
Definition: NptZip.h:98
Definition: NptZip.h:140
Definition: NptZip.h:169
Definition: NptDataBuffer.h:44
Definition: NptFile.h:153
Definition: NptStrings.h:57
Format
Compressed data format.
Definition: NptZip.h:107