@@ -27,11 +27,11 @@ public static class ZlibModule
2727 public const string __doc__ = @"The functions in this module allow compression and decompression using the
2828zlib library, which is based on GNU zip.
2929
30- adler32(string [, start ]) -- Compute an Adler-32 checksum.
31- compress(string [, level]) -- Compress string , with compression level in 1-9.
30+ adler32(data [, value ]) -- Compute an Adler-32 checksum.
31+ compress(data [, level]) -- Compress data , with compression level 1-9.
3232compressobj([level]) -- Return a compressor object.
33- crc32(string [, start ]) -- Compute a CRC-32 checksum.
34- decompress(string ,[wbits],[bufsize]) -- Decompresses a compressed string .
33+ crc32(data [, value ]) -- Compute a CRC-32 checksum.
34+ decompress(data ,[wbits],[bufsize]) -- Decompresses compressed data .
3535decompressobj([wbits]) -- Return a decompressor object.
3636
3737'wbits' is window buffer size.
@@ -69,34 +69,32 @@ public static class ZlibModule
6969
7070 internal const int DEFAULTALLOC = 16 * 1024 ;
7171
72- [ Documentation ( @"adler32(string [, start ]) -- Compute an Adler-32 checksum of string .
72+ [ Documentation ( @"adler32(data [, value ]) -- Compute an Adler-32 checksum of data .
7373
7474An optional starting value can be specified. The returned checksum is
7575a signed integer." ) ]
76- public static int adler32 ( [ BytesLike ] IList < byte > data , long baseValue = 1L )
76+ public static int adler32 ( [ NotNull ] IBufferProtocol data , long value = 1L )
7777 {
78- return ( int ) Adler32 . GetAdler32Checksum ( baseValue , data . ToArray ( ) , 0 , data . Count ) ;
78+ using var buffer = data . GetBuffer ( ) ;
79+ return ( int ) Adler32 . GetAdler32Checksum ( value , buffer . AsUnsafeArray ( ) ?? buffer . ToArray ( ) , 0 , buffer . NumBytes ( ) ) ;
7980 }
8081
81- [ Documentation ( @"crc32(string [, start ]) -- Compute a CRC-32 checksum of string .
82+ [ Documentation ( @"crc32(data [, value ]) -- Compute a CRC-32 checksum of data .
8283
8384An optional starting value can be specified. The returned checksum is
8485a signed integer." ) ]
85- public static BigInteger crc32 ( [ BytesLike ] IList < byte > data , long baseValue = 0L )
86- {
87- if ( baseValue < int . MinValue || baseValue > uint . MaxValue )
88- throw new ArgumentOutOfRangeException ( nameof ( baseValue ) ) ;
89-
90- return IronPython . Modules . PythonBinaryAscii . crc32 ( data , 0 , data . Count , ( uint ) baseValue ) ;
91- }
86+ public static object crc32 ( [ NotNull ] IBufferProtocol data , uint value = 0 )
87+ // TODO: [PythonIndex(overflow=mask)] uint value = 0
88+ => IronPython . Modules . PythonBinaryAscii . crc32 ( data , value ) ;
9289
93- [ Documentation ( @"compress(string [, level]) -- Returned compressed string .
90+ [ Documentation ( @"compress(data [, level]) -- Returns a bytes object containing compressed data .
9491
9592Optional arg level is the compression level, in 1-9." ) ]
96- public static Bytes compress ( [ BytesLike ] IList < byte > data ,
93+ public static Bytes compress ( [ NotNull ] IBufferProtocol data ,
9794 int level = Z_DEFAULT_COMPRESSION )
9895 {
99- byte [ ] input = data . ToArray ( ) ;
96+ using var buffer = data . GetBuffer ( ) ;
97+ byte [ ] input = buffer . AsUnsafeArray ( ) ?? buffer . ToArray ( ) ;
10098 byte [ ] output = new byte [ input . Length + input . Length / 1000 + 12 + 1 ] ;
10199
102100 ZStream zst = new ZStream ( ) ;
@@ -154,15 +152,16 @@ public static Compress compressobj(
154152 return new Compress ( level , method , wbits , memlevel , strategy ) ;
155153 }
156154
157- [ Documentation ( @"decompress(string [, wbits[, bufsize]]) -- Return decompressed string .
155+ [ Documentation ( @"decompress(data [, wbits[, bufsize]]) -- Returns a bytes object containing the uncompressed data .
158156
159157Optional arg wbits is the window buffer size. Optional arg bufsize is
160158the initial output buffer size." ) ]
161- public static Bytes decompress ( [ BytesLike ] IList < byte > data ,
159+ public static Bytes decompress ( [ NotNull ] IBufferProtocol data ,
162160 int wbits = MAX_WBITS ,
163161 int bufsize = DEFAULTALLOC )
164162 {
165- var bytes = Decompress ( data . ToArray ( ) , wbits , bufsize ) ;
163+ using var buffer = data . GetBuffer ( ) ;
164+ var bytes = Decompress ( buffer . AsUnsafeArray ( ) ?? buffer . ToArray ( ) , wbits , bufsize ) ;
166165 return Bytes . Make ( bytes ) ;
167166 }
168167
0 commit comments