Сжатие данных без потерь
作者
module CDeflate
using Libdl
export deflate, inflate, byte_shuffle, byte_unshuffle
const source_dir=@__DIR__
const library=joinpath(@__DIR__,"libpure_deflate."*Libdl.dlext)
function build_library()
source=joinpath(source_dir,"pure_deflate.c")
if !isfile(library) || mtime(library)<max(mtime(source),mtime(joinpath(source_dir,"pure_deflate.h")))
compiler=get(ENV,"CC","gcc")
flags=Sys.iswindows() ? String[] : ["-fPIC"]
run(Cmd(vcat([compiler,"-std=c99","-O2","-shared"],flags,[source,"-o",library])))
end
library
end
build_library()
const handle=Libdl.dlopen(library)
const encode_ptr=Libdl.dlsym(handle,:pd_deflate)
const decode_ptr=Libdl.dlsym(handle,:pd_inflate)
const bound_ptr=Libdl.dlsym(handle,:pd_deflate_bound)
const shuffle_ptr=Libdl.dlsym(handle,:pd_shuffle8)
const unshuffle_ptr=Libdl.dlsym(handle,:pd_unshuffle8)
function reorder(raw::Vector{UInt8},inverse::Bool)
result=similar(raw)
status=ccall(inverse ? unshuffle_ptr : shuffle_ptr,Cint,
(Ptr{UInt8},Ptr{UInt8},Csize_t),raw,result,length(raw))
status==0 || error("C byte shuffle: status=$status")
result
end
byte_shuffle(raw::Vector{UInt8})=reorder(raw,false)
byte_unshuffle(raw::Vector{UInt8})=reorder(raw,true)
function deflate(raw::Vector{UInt8};shuffle=false)
input=shuffle ? byte_shuffle(raw) : raw
cap=ccall(bound_ptr,Csize_t,(Csize_t,),length(input))
cap>0 || error("DEFLATE size overflow")
output=Vector{UInt8}(undef,cap)
# UInt64 storage provides alignment for the uint32_t-based C workspace.
workspace=Vector{UInt64}(undef,4096)
used=Ref{Csize_t}(0)
status=ccall(encode_ptr,Cint,
(Ptr{UInt8},Csize_t,Ptr{UInt8},Csize_t,Ref{Csize_t},Ptr{UInt64}),
input,length(input),output,cap,used,workspace)
status==0 || error("C DEFLATE: status=$status")
resize!(output,used[])
end
function inflate(input::Vector{UInt8},expected::Integer;shuffle=false)
expected>=0 || error("negative output size")
output=Vector{UInt8}(undef,expected)
workspace=Vector{UInt64}(undef,268) # 2144 bytes, default PD_WINDOW ABI
used=Ref{Csize_t}(0)
status=ccall(decode_ptr,Cint,
(Ptr{UInt8},Csize_t,Ptr{UInt8},Csize_t,Ref{Csize_t},Ptr{UInt64}),
input,length(input),output,length(output),used,workspace)
status==0 || error("C INFLATE: status=$status")
used[]==expected || error("unexpected decoded length")
shuffle ? byte_unshuffle(output) : output
end
end