package decompress
Install
    
    dune-project
 Dependency
Authors
Maintainers
Sources
sha256=73183dc1186ab6cf1ca641146f6948f2fae6a69729ec0a1e62943385f9895077
    
    
  sha512=31508b4ae16f6850fd86391f4bb3f950bba12ac45398c8ddb1b6e74f6a96f6a150b81377d6fd49146f1f0789c43a761d709d1d73a782d9c0df0d00a3c9663c89
    
    
  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.