package decompress
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=f91e6978beff3fcb61440d32f7c99c99f1e8654b4fb18408741d36035373ac60
sha512=c3f402404f76075e6f692ea36e701134a5d833824d5d1166365c6c81fb18b309270bf288ce4c118ac44fd0366d9b6eea0a6309255678d8e1bd2bbfa7ba843461
doc/decompress.zl/Zl/Higher/index.html
Module Zl.HigherSource
val compress :
?level:int ->
?dynamic:bool ->
w:De.Lz77.window ->
q:De.Queue.t ->
refill:(bigstring -> int) ->
flush:(bigstring -> int -> unit) ->
bigstring ->
bigstring ->
unitcompress ?level ?dynamic ~w ~q ~refill ~flush i o is Zlib.compress (with ~header:true) provided by camlzip package.
wis the window used by LZ77 compression algorithm.qis shared-queue between compression algorithm and DEFLATE encoder.iis input buffer.ois output buffer.
When compress wants more input, it calls refill with i. The client returns how many bytes he wrote into i. If he returns 0, he signals end of input.
When compress has written output buffer, it calls flush with o and how many bytes it wrote. Bytes into o must be copied and they will be lost at the next call to flush.
A simple example of how to use such interface is:
let deflate_string ?(level= 4) str =
let i = De.bigstring_create De.io_buffer_size in
let o = De.bigstring_create De.io_buffer_size in
let w = De.Lz77.make_window ~bits:15 in
let q = De.Queue.create 0x1000 in
let r = Buffer.create 0x1000 in
let p = ref 0 in
let refill buf =
let len = min (String.length str - !p) De.io_buffer_size in
Bigstringaf.blit_from_string str ~src_off:!p buf ~dst_off:0 ~len ;
p := !p + len ; len in
let flush buf len =
let str = Bigstringaf.substring buf ~off:0 ~len in
Buffer.add_string r str in
Zl.Higher.compress ~level ~dynamic:true
~w ~q ~refill ~flush i o ;
Buffer.contents rAs De.Higher.compress, several choices was made in this code and decompress don't want to be responsible of them. It's why such function exists only as example when lengths of buffers (such as i, o or q) changes the speed/compression ratio/memory consumption.
val uncompress :
allocate:(int -> window) ->
refill:(bigstring -> int) ->
flush:(bigstring -> int -> unit) ->
bigstring ->
bigstring ->
(unit, [> `Msg of string ]) resultuncompress ~allocate ~refill ~flush i o is Zlib.uncompress (with ~header:true) provided by camlzip package.
allocateis the allocator of window used by LZ77 uncompression algorithmiis input buffer.ois output buffer.
When uncompress wants more input, it calls refill with i. The client returns how many bytes he wrote into i. If he returns 0, he signals end of input.
When uncompress has written output buffer, it calls flush with o and how many bytes it wrote. Bytes into o must be copied and they will be lost at the next call to flush.
A simple example of how to use such interface is:
let inflate_string str =
let i = De.bigstring_create De.io_buffer_size in
let o = De.bigstring_create De.io_buffer_size in
let allocate bits = De.make_window ~bits in
let r = Buffer.create 0x1000 in
let p = ref 0 in
let refill buf =
let len = min (String.length str - !p) De.io_buffer_size in
Bigstringaf.blit_from_string str ~src_off:!p buf ~dst_off:0 ~len ;
p := !p + len ; len in
let flush buf len =
let str = Bigstringaf.substring buf ~off:0 ~len in
Buffer.add_string r str in
match Zl.Higher.uncompress ~allocate ~refill ~flush i o with
| Ok () -> Ok (Buffer.contents r)
| Error _ as err -> errAs you can see, several allocations appear. As long as you want to uncompress several contents for example, you can re-use the same window instead of an allocation of one per uncompression. Then, the throughput is mostly limited by i and o (even bigger, even faster but it requires memories). decompress don't want to be responsible about these choices, it's why such function exists only as an example.