@@ -23,6 +23,18 @@ mutable struct ZStream
2323 reserved:: Culong
2424end
2525
26+ @assert typemax (Csize_t) ≥ typemax (Cuint)
27+
28+ function zalloc (:: Ptr{Cvoid} , items:: Cuint , size:: Cuint ):: Ptr{Cvoid}
29+ s, f = Base. Checked. mul_with_overflow (items, size)
30+ if f
31+ C_NULL
32+ else
33+ ccall (:jl_malloc , Ptr{Cvoid}, (Csize_t,), s% Csize_t)
34+ end
35+ end
36+ zfree (:: Ptr{Cvoid} , p:: Ptr{Cvoid} ) = ccall (:jl_free , Cvoid, (Ptr{Cvoid},), p)
37+
2638function ZStream ()
2739 ZStream (
2840 # input
@@ -32,7 +44,9 @@ function ZStream()
3244 # message and state
3345 C_NULL , C_NULL ,
3446 # memory allocation
35- C_NULL , C_NULL , C_NULL ,
47+ @cfunction (zalloc, Ptr{Cvoid}, (Ptr{Cvoid}, Cuint, Cuint)),
48+ @cfunction (zfree, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid})),
49+ C_NULL ,
3650 # data type, adler and reserved
3751 0 , 0 , 0 )
3852end
@@ -83,6 +97,11 @@ function deflate_end!(zstream::ZStream)
8397 return ccall ((:deflateEnd , libz), Cint, (Ref{ZStream},), zstream)
8498end
8599
100+ function compress_finalizer! (zstream:: ZStream )
101+ deflate_end! (zstream)
102+ nothing
103+ end
104+
86105function deflate! (zstream:: ZStream , flush:: Integer )
87106 return ccall ((:deflate , libz), Cint, (Ref{ZStream}, Cint), zstream, flush)
88107end
@@ -99,6 +118,11 @@ function inflate_end!(zstream::ZStream)
99118 return ccall ((:inflateEnd , libz), Cint, (Ref{ZStream},), zstream)
100119end
101120
121+ function decompress_finalizer! (zstream:: ZStream )
122+ inflate_end! (zstream)
123+ nothing
124+ end
125+
102126function inflate! (zstream:: ZStream , flush:: Integer )
103127 return ccall ((:inflate , libz), Cint, (Ref{ZStream}, Cint), zstream, flush)
104128end
0 commit comments