NptZip.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - Zip Support
00004 |
00005 |   (c) 2007 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_ZIP_H_
00011 #define _NPT_ZIP_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptConfig.h"
00017 #include "NptStreams.h"
00018 
00019 /*----------------------------------------------------------------------
00020 |   class references
00021 +---------------------------------------------------------------------*/
00022 class NPT_ZipInflateState;
00023 class NPT_ZipDeflateState;
00024 
00025 /*----------------------------------------------------------------------
00026 |   NPT_Zip
00027 +---------------------------------------------------------------------*/
00028 const int NPT_ZIP_COMPRESSION_LEVEL_DEFAULT = -1;
00029 const int NPT_ZIP_COMPRESSION_LEVEL_MIN     = 0;
00030 const int NPT_ZIP_COMPRESSION_LEVEL_MAX     = 9;
00031 const int NPT_ZIP_COMPRESSION_LEVEL_NONE    = 0;
00032 class NPT_Zip 
00033 {
00034 public:
00035     // class methods
00036     static NPT_Result MapError(int err);
00037 
00041     typedef enum {
00042         ZLIB,
00043         GZIP
00044     } Format;
00045         
00049     static NPT_Result Deflate(const NPT_DataBuffer& in,
00050                               NPT_DataBuffer&       out,
00051                               int                   compression_level = NPT_ZIP_COMPRESSION_LEVEL_DEFAULT,
00052                               Format                format = ZLIB);
00053                               
00057     static NPT_Result Inflate(const NPT_DataBuffer& in,
00058                               NPT_DataBuffer&       out);                       
00059 };
00060 
00061 /*----------------------------------------------------------------------
00062 |   NPT_ZipInflatingInputStream
00063 +---------------------------------------------------------------------*/
00064 class NPT_ZipInflatingInputStream : public NPT_InputStream 
00065 {
00066 public:
00067     NPT_ZipInflatingInputStream(NPT_InputStreamReference& source);
00068    ~NPT_ZipInflatingInputStream();
00069    
00070     // NPT_InputStream methods
00071     virtual NPT_Result Read(void*     buffer, 
00072                             NPT_Size  bytes_to_read, 
00073                             NPT_Size* bytes_read = NULL);
00074     virtual NPT_Result Seek(NPT_Position offset);
00075     virtual NPT_Result Tell(NPT_Position& offset);
00076     virtual NPT_Result GetSize(NPT_Size& size);
00077     virtual NPT_Result GetAvailable(NPT_Size& available);
00078 
00079 private:
00080     NPT_InputStreamReference m_Source;
00081     NPT_Position             m_Position;
00082     NPT_ZipInflateState*     m_State;
00083     NPT_DataBuffer           m_Buffer;
00084 };
00085 
00086 /*----------------------------------------------------------------------
00087 |   NPT_ZipInflatingOutputStream
00088 +---------------------------------------------------------------------*/
00089 
00090 /*----------------------------------------------------------------------
00091 |   NPT_ZipDeflatingInputStream
00092 +---------------------------------------------------------------------*/
00093 class NPT_ZipDeflatingInputStream : public NPT_InputStream 
00094 {
00095 public:
00096     NPT_ZipDeflatingInputStream(NPT_InputStreamReference& source,
00097                                 int                       compression_level = NPT_ZIP_COMPRESSION_LEVEL_DEFAULT,
00098                                 NPT_Zip::Format           format = NPT_Zip::ZLIB);
00099    ~NPT_ZipDeflatingInputStream();
00100    
00101     // NPT_InputStream methods
00102     virtual NPT_Result Read(void*     buffer, 
00103                             NPT_Size  bytes_to_read, 
00104                             NPT_Size* bytes_read = NULL);
00105     virtual NPT_Result Seek(NPT_Position offset);
00106     virtual NPT_Result Tell(NPT_Position& offset);
00107     virtual NPT_Result GetSize(NPT_Size& size);
00108     virtual NPT_Result GetAvailable(NPT_Size& available);
00109 
00110 private:
00111     NPT_InputStreamReference m_Source;
00112     NPT_Position             m_Position;
00113     bool                     m_Eos;
00114     NPT_ZipDeflateState*     m_State;
00115     NPT_DataBuffer           m_Buffer;
00116 };
00117 
00118 /*----------------------------------------------------------------------
00119 |   NPT_ZipDeflatingInputStream
00120 +---------------------------------------------------------------------*/
00121 
00122 #endif // _NPT_ZIP_H_