00001 // $Id: zstream.h 1282 2006-06-09 09:46:49Z alex $ 00002 /* @@tag:xara-cn-tp@@ THIRD PARTY COPYRIGHT */ 00003 // This class contains all the streaming file zipping code 00004 00005 #ifndef INC_ZSTREAM 00006 #define INC_ZSTREAM 00007 00008 #include <fstream> 00009 //#include "ccfile.h" // FilePos - in camtypes.h [AUTOMATICALLY REMOVED] 00010 00011 #include "zutil.h" 00012 #include "zdeflate.h" 00013 #include "zinflate.h" 00014 00015 // This is what the original code is based upon. Converted to C++. 00016 00017 /* interface of the 'zlib' general purpose compression library 00018 00019 version 0.92 May 3rd, 1995. 00020 00021 Copyright (C) 1995 Jean-loup Gailly and Mark Adler 00022 00023 This software is provided 'as-is', without any express or implied 00024 warranty. In no event will the authors be held liable for any damages 00025 arising from the use of this software. 00026 00027 Permission is granted to anyone to use this software for any purpose, 00028 including commercial applications, and to alter it and redistribute it 00029 freely, subject to the following restrictions: 00030 00031 1. The origin of this software must not be misrepresented; you must not 00032 claim that you wrote the original software. If you use this software 00033 in a product, an acknowledgment in the product documentation would be 00034 appreciated but is not required. 00035 2. Altered source versions must be plainly marked as such, and must not be 00036 misrepresented as being the original software. 00037 3. This notice may not be removed or altered from any source distribution. 00038 00039 Jean-loup Gailly Mark Adler 00040 gzip@prep.ai.mit.edu madler@cco.caltech.edu 00041 */ 00042 00043 // Some useful bits and pieces 00044 00045 // These are from zconf.h 00046 00047 /* 00048 The library does not install any signal handler. It is recommended to 00049 add at least a handler for SIGSEGV when decompressing; the library checks 00050 the consistency of the input data whenever possible but may go nuts 00051 for some forms of corrupted input. 00052 */ 00053 00054 /* 00055 The 'zlib' compression library provides in-memory compression and 00056 decompression functions, including integrity checks of the uncompressed 00057 data. This version of the library supports only one compression method 00058 (deflation) but other algorithms may be added later and will have the same 00059 stream interface. 00060 00061 For compression the application must provide the output buffer and 00062 may optionally provide the input buffer for optimization. For decompression, 00063 the application must provide the input buffer and may optionally provide 00064 the output buffer for optimization. 00065 00066 Compression can be done in a single step if the buffers are large 00067 enough (for example if an input file is mmap'ed), or can be done by 00068 repeated calls of the compression function. In the latter case, the 00069 application must provide more input and/or consume the output 00070 (providing more output space) before each call. 00071 */ 00072 00073 /* 00074 The application must update next_in and avail_in when avail_in has 00075 dropped to zero. It must update next_out and avail_out when avail_out 00076 has dropped to zero. The application must initialize zalloc, zfree and 00077 opaque before calling the init function. All other fields are set by the 00078 compression library and must not be updated by the application. 00079 00080 The opaque value provided by the application will be passed as the first 00081 parameter for calls of zalloc and zfree. This can be useful for custom 00082 memory management. The compression library attaches no meaning to the 00083 opaque value. 00084 00085 zalloc must return NULL if there is not enough memory for the object. 00086 On 16-bit systems, the functions zalloc and zfree must be able to allocate 00087 exactly 65536 bytes, but will not be required to allocate more than this 00088 if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, 00089 pointers returned by zalloc for objects of exactly 65536 bytes *must* 00090 have their offset normalized to zero. The default allocation function 00091 provided by this library ensures this (see zutil.c). To reduce memory 00092 requirements and avoid any allocation of 64K objects, at the expense of 00093 compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). 00094 00095 The fields total_in and total_out can be used for statistics or 00096 progress reports. After compression, total_in holds the total size of 00097 the uncompressed data and may be saved for use in the decompressor 00098 (particularly if the decompressor wants to decompress everything in 00099 a single step). 00100 */ 00101 00102 typedef void* (*alloc_func)(void *opaque, uInt items, uInt size); 00103 typedef void (*free_func)(void *opaque, void* address); 00104 00105 //class InflateState; 00106 //class DeflateState; 00107 00108 typedef ZStream FAR *z_streamp; 00109 00110 00111 00112 /******************************************************************************************** 00113 00114 > class ZStream : public CCObject 00115 00116 Author: Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com> Humprhys 00117 Created: 23/5/95 00118 Purpose: A file stream compressor and decompressor. 00119 Allows the CCDiskFile to offer compression of files. At present just 00120 used to compress the native file format. 00121 00122 ********************************************************************************************/ 00123 00124 class ZStream : public CC_CLASS_MEMDUMP 00125 { 00126 // Give my name in memory dumps 00127 // If you need a different sort of decare, replace this one. 00128 // See CCObject in the help file for info about DECLARE types 00129 CC_DECLARE_MEMDUMP(ZStream); 00130 00131 public: 00132 ZStream(); 00133 ~ZStream(); 00134 00135 //Init(); 00136 00137 public: 00138 // The application can compare zlib_version and ZLIB_VERSION for consistency. 00139 // If the first character differs, the library code actually used is 00140 // not compatible with the zlib.h header file used by the application. 00141 char *zlib_version; 00142 00143 // ZStream object 00144 Byte *next_in; /* next input byte */ 00145 uInt avail_in; /* number of bytes available at next_in */ 00146 uLong total_in; /* total nb of input bytes read so far */ 00147 00148 Byte *next_out; /* next output byte should be put there */ 00149 uInt avail_out; /* remaining free space at next_out */ 00150 uLong total_out; /* total nb of bytes output so far */ 00151 00152 char *msg; /* last error message, NULL if no error */ 00153 InflateState *In_state; /* not visible by applications */ 00154 DeflateState *De_state; /* not visible by applications */ 00155 00156 alloc_func zalloc; /* used to allocate the internal state */ 00157 free_func zfree; /* used to free the internal state */ 00158 void *opaque; /* private data object passed to zalloc and zfree */ 00159 00160 Byte data_type; /* best guess about the data type: ascii or binary */ 00161 uLong adler; /* adler32 value of the uncompressed data */ 00162 uLong reserved; /* reserved for future use */ 00163 protected: 00164 00165 }; 00166 00167 00168 00169 /******************************************************************************************** 00170 00171 > class GZipStream : public CCObject 00172 00173 Author: Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com> Humprhys 00174 Created: 23/5/95 00175 Purpose: A file stream compressor and decompressor. 00176 Allows the CCDiskFile to offer compression of files. At present just 00177 used to compress the native file format. 00178 00179 Note: z_stream == to our ZStream 00180 gz_stream == to our GZipStream 00181 00182 ********************************************************************************************/ 00183 00184 class GZipStream : public CC_CLASS_MEMDUMP 00185 { 00186 // Give my name in memory dumps 00187 // If you need a different sort of decare, replace this one. 00188 // See CCObject in the help file for info about DECLARE types 00189 CC_DECLARE_MEMDUMP(GZipStream); 00190 00191 public: 00192 GZipStream(); 00193 ~GZipStream(); 00194 00195 //Init(); 00196 00197 public: 00198 ZStream stream; // The stream that we will be using 00199 INT32 z_err; // error code for last stream operation 00200 INT32 z_eof; // set if end of input file 00201 iostream *file; // Underlying implementation is an fstream 00202 Byte *inbuf; // input buffer 00203 Byte *outbuf; // output buffer 00204 uLong crc; // crc32 of uncompressed data 00205 char *msg; // error message 00206 TCHAR *path; // path name for debugging only 00207 INT32 transparent; // 1 if input file is not a .gz file 00208 char mode; // 'w' or 'r' 00209 00210 // Peek variables 00211 BOOL Peek; // flag to say if we have peeked at all 00212 char PeekedValue;// if peek is true then use this as the next value 00213 INT32 PeekStatus; // the value returned from the peek 00214 00215 FilePos InitialPos; // position in the file that we start at 00216 00217 BOOL Inited; // Flag to say whether Init has been called. 00218 00219 protected: 00220 }; 00221 00222 00223 00224 //class ZipDeflate; 00225 //class ZipInflate; 00226 00227 00228 /******************************************************************************************** 00229 00230 > class GZipFile : public CCObject 00231 00232 Author: Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com> Humprhys 00233 Created: 23/5/95 00234 Purpose: A file stream compressor and decompressor. 00235 Allows the CCDiskFile to offer compression of files. At present just 00236 used to compress the native file format. 00237 00238 ********************************************************************************************/ 00239 00240 class GZipFile : public CC_CLASS_MEMDUMP 00241 { 00242 // Give my name in memory dumps 00243 // If you need a different sort of decare, replace this one. 00244 // See CCObject in the help file for info about DECLARE types 00245 CC_DECLARE_MEMDUMP(GZipFile); 00246 00247 public: 00248 GZipFile(); 00249 ~GZipFile(); 00250 00251 //Init(); 00252 00253 protected: 00254 // The classes we use for the compression/decompression 00255 ZipDeflate *deflate; 00256 ZipInflate *inflate; 00257 00258 public: 00259 // Function to find out the current file version of the stream 00260 static double GetStreamVersionNo(); 00261 00262 // Useful function to try and claim all the memory for the compression system 00263 GZipStream *gz_init(iostream *pFileStream, TCHAR *mode, BOOL Header = FALSE); 00264 00265 // Useful stream functions for opening/closing,reading and writing 00266 GZipStream *gz_open(iostream *pFileStream, TCHAR *mode, TCHAR *path); 00267 00268 // The form of open if we have already called init 00269 INT32 gz_open(GZipStream *s); 00270 00271 INT32 gzpeek(GZipStream *s, char *data); 00272 00273 INT32 gzread(GZipStream *s, char *data, unsigned len); 00274 00275 INT32 gzwrite(GZipStream *s, const char *data, unsigned len); 00276 00277 INT32 gzflush(GZipStream *s, INT32 flush); 00278 00279 INT32 gzclose(GZipStream *s); 00280 00281 //char* gzerror(GZipStream *s, INT32 *errnum); 00282 00283 INT32 destroy(GZipStream *file); 00284 00285 00286 // Checksum functions 00287 00288 static uLong crc32(uLong crc, Bytef *buf, uInt len); 00289 static uLong adler32(uLong adler, const Bytef *buf, uInt len); 00290 static uLongf* get_crc_table(); 00291 00292 // These are wrappers around the memory allocation functions 00293 static void* zcalloc (void *opaque, unsigned items, unsigned size); 00294 static void zcfree(void *opaque, void *ptr); 00295 00296 protected: 00297 void putLong(iostream* file, uLong x); 00298 uLong getLong(Byte *buf); 00299 00300 public: 00301 FilePos GetCurrentFilePos(GZipStream *s); 00302 }; 00303 00304 #endif // INC_ZSTREAM