/* **************************************************************************** * * Copyright (c) Jeff Hardy 2007-2009. * * This source code is subject to terms and conditions of the Microsoft Public License. A * copy of the license can be found in the License.txt file at the root of this distribution. If * you cannot locate the Microsoft Public License, please send an email to * jdhardy@gmail.com. By using this source code in any fashion, you are agreeing to be bound * by the terms of the Microsoft Public License. * * You must not remove this notice, or any other, from this software. * * ***************************************************************************/usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingIronPython.Runtime;usingIronPython.Runtime.Types;usingComponentAce.Compression.Libs.ZLib;usingSystem.Runtime.InteropServices;[assembly: PythonModule("zlib", typeof(IronPython.Zlib.ZlibModule))]namespaceIronPython.Zlib{publicstaticclassZlibModule{publicconststringZLIB_VERSION="1.2.3";// just match the zlib version in Python 2.6internalconstintZ_OK=(int)ZLibResultCode.Z_OK;internalconstintZ_STREAM_END=(int)ZLibResultCode.Z_STREAM_END;internalconstintZ_NEED_DICT=(int)ZLibResultCode.Z_NEED_DICT;internalconstintZ_ERRNO=(int)ZLibResultCode.Z_ERRNO;internalconstintZ_STREAM_ERROR=(int)ZLibResultCode.Z_STREAM_ERROR;internalconstintZ_DATA_ERROR=(int)ZLibResultCode.Z_DATA_ERROR;internalconstintZ_MEM_ERROR=(int)ZLibResultCode.Z_MEM_ERROR;internalconstintZ_BUF_ERROR=(int)ZLibResultCode.Z_BUF_ERROR;internalconstintZ_VERSION_ERROR=(int)ZLibResultCode.Z_VERSION_ERROR;publicconstintZ_NO_FLUSH=(int)FlushStrategy.Z_NO_FLUSH;publicconstintZ_SYNC_FLUSH=(int)FlushStrategy.Z_SYNC_FLUSH;publicconstintZ_FULL_FLUSH=(int)FlushStrategy.Z_FULL_FLUSH;publicconstintZ_FINISH=(int)FlushStrategy.Z_FINISH;publicconstintZ_BEST_SPEED=ZLibCompressionLevel.Z_BEST_SPEED;publicconstintZ_BEST_COMPRESSION=ZLibCompressionLevel.Z_BEST_COMPRESSION;publicconstintZ_DEFAULT_COMPRESSION=ZLibCompressionLevel.Z_DEFAULT_COMPRESSION;publicconstintZ_FILTERED=(int)CompressionStrategy.Z_FILTERED;publicconstintZ_HUFFMAN_ONLY=(int)CompressionStrategy.Z_HUFFMAN_ONLY;publicconstintZ_DEFAULT_STRATEGY=(int)CompressionStrategy.Z_DEFAULT_STRATEGY;publicconstintDEFLATED=8;publicconstintDEF_MEM_LEVEL=8;publicconstintMAX_WBITS=ZLibUtil.MAX_WBITS;internalconstintDEFAULTALLOC=16*1024;#region adler32publicstaticintadler32(stringdata,[DefaultParameterValue(1L)]longbaseValue){returnadler32(Latin1.GetBytes(data),baseValue);}publicstaticintadler32(byte[]data,[DefaultParameterValue(1L)]longbaseValue){return(int)Adler32.GetAdler32Checksum(baseValue,data,0,data.Length);}#endregion#region crc32publicstaticintcrc32(stringdata,[DefaultParameterValue(0L)]longbaseValue){if(baseValue<int.MinValue||baseValue>uint.MaxValue)thrownewArgumentOutOfRangeException("baseValue");if(baseValue>=0&&baseValue<=uint.MaxValue)returnIronPython.Modules.PythonBinaryAscii.crc32(data,(uint)baseValue);elsereturnIronPython.Modules.PythonBinaryAscii.crc32(data,(int)baseValue);}publicstaticintcrc32(byte[]data,[DefaultParameterValue(0L)]longbaseValue){if(baseValue<int.MinValue||baseValue>uint.MaxValue)thrownewArgumentOutOfRangeException("baseValue");if(baseValue>=0&&baseValue<=uint.MaxValue)returnIronPython.Modules.PythonBinaryAscii.crc32(data,(uint)baseValue);elsereturnIronPython.Modules.PythonBinaryAscii.crc32(data,(int)baseValue);}#endregion#region compresspublicstaticstringcompress(PythonBufferdata){returncompress(data,Z_DEFAULT_COMPRESSION);}publicstaticstringcompress(PythonBufferdata,intlevel){returncompress(data.ToString(),level);}publicstaticstringcompress(stringdata){returncompress(data,Z_DEFAULT_COMPRESSION);}publicstaticstringcompress(stringdata,intlevel){byte[]input=Latin1.GetBytes(data);byte[]output=newbyte[input.Length+input.Length/1000+12+1];ZStreamzst=newZStream();zst.next_in=input;zst.avail_in=input.Length;zst.next_out=output;zst.avail_out=output.Length;interr=zst.deflateInit(level);if(err!=Z_OK){thrownewZStreamException();}err=zst.deflate(FlushStrategy.Z_FINISH);if(err!=Z_STREAM_END){zst.deflateEnd();thrownewZStreamException();}err=zst.deflateEnd();if(err==Z_OK)returnLatin1.GetString(output,0,(int)zst.total_out);elsethrownewZStreamException();}#endregion#region compressobjpublicstaticCompresscompressobj(){returncompressobj(Z_DEFAULT_COMPRESSION,DEFLATED,MAX_WBITS,DEF_MEM_LEVEL,Z_DEFAULT_STRATEGY);}publicstaticCompresscompressobj(intlevel){returnnewCompress(level,DEFLATED,MAX_WBITS,DEF_MEM_LEVEL,Z_DEFAULT_STRATEGY);}publicstaticCompresscompressobj(intlevel,intmethod){returnnewCompress(level,method,MAX_WBITS,DEF_MEM_LEVEL,Z_DEFAULT_STRATEGY);}publicstaticCompresscompressobj(intlevel,intmethod,intwbits){returnnewCompress(level,method,wbits,DEF_MEM_LEVEL,Z_DEFAULT_STRATEGY);}publicstaticCompresscompressobj(intlevel,intmethod,intwbits,intmemlevel){returnnewCompress(level,method,wbits,memlevel,Z_DEFAULT_STRATEGY);}publicstaticCompresscompressobj(intlevel,intmethod,intwbits,intmemlevel,intstrategy){returnnewCompress(level,method,wbits,memlevel,strategy);}#endregion#region decompresspublicstaticstringdecompress(stringdata){returndecompress(data,MAX_WBITS,DEFAULTALLOC);}publicstaticstringdecompress(stringdata,intwbits){returndecompress(data,wbits,DEFAULTALLOC);}publicstaticstringdecompress(stringdata,intwbits,intbufsize){byte[]input=Latin1.GetBytes(data);byte[]outputBuffer=newbyte[bufsize];byte[]output=newbyte[bufsize];intoutputOffset=0;ZStreamzst=newZStream();zst.next_in=input;zst.avail_in=input.Length;zst.next_out=outputBuffer;zst.avail_out=outputBuffer.Length;interr=zst.inflateInit(wbits);if(err!=Z_OK){thrownewZStreamException();}do{err=zst.inflate(FlushStrategy.Z_FINISH);if(err!=Z_STREAM_END){if(err==Z_BUF_ERROR&&zst.avail_out>0){zst.inflateEnd();thrownewZStreamException();}elseif(err==Z_OK||(err==Z_BUF_ERROR&&zst.avail_out==0)){// copy to the output and reset the bufferif(outputOffset+outputBuffer.Length>output.Length)Array.Resize(refoutput,output.Length*2);Array.Copy(outputBuffer,0,output,outputOffset,outputBuffer.Length);outputOffset+=outputBuffer.Length;zst.next_out=outputBuffer;zst.avail_out=outputBuffer.Length;zst.next_out_index=0;}else{zst.inflateEnd();thrownewZStreamException();}}}while(err!=Z_STREAM_END);err=zst.inflateEnd();if(err!=Z_OK){thrownewZStreamException();}if(outputOffset+outputBuffer.Length-zst.avail_out>output.Length)Array.Resize(refoutput,output.Length*2);Array.Copy(outputBuffer,0,output,outputOffset,outputBuffer.Length-zst.avail_out);outputOffset+=outputBuffer.Length-zst.avail_out;returnLatin1.GetString(output,0,outputOffset);}#endregion#region decompressobjpublicstaticDecompressdecompressobj(){returndecompressobj(MAX_WBITS);}publicstaticDecompressdecompressobj(intwbits){returnnewDecompress(wbits);}#endregionpublicstaticreadonlyPythonType/*!*/ZlibErrorType=DynamicHelpers.GetPythonTypeFromType(typeof(ZStreamException));publicstaticreadonlyPythonType/*!*/error=ZlibErrorType;internalstaticSystem.Text.EncodingLatin1=System.Text.Encoding.GetEncoding("iso-8859-1");}}