From 169cffd1d05db8d7c385b48a503a8ea80f1dcfb6 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 21 Mar 2024 17:39:05 -0400 Subject: [PATCH] add Util_zlib.decompress_slice --- imandrakit.opam | 1 - imandrakit.opam.template | 1 - src/core/util_zlib.ml | 10 +++++++--- src/core/util_zlib.mli | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/imandrakit.opam b/imandrakit.opam index 47938aa7..3f4cf343 100644 --- a/imandrakit.opam +++ b/imandrakit.opam @@ -47,7 +47,6 @@ build: [ ] ] dev-repo: "git+https://github.com/imandra-ai/imandrakit.git" - pin-depends: [ ["moonpool.dev" "git+https://github.com/c-cube/moonpool#main"] ] diff --git a/imandrakit.opam.template b/imandrakit.opam.template index 62831850..b7a4caa4 100644 --- a/imandrakit.opam.template +++ b/imandrakit.opam.template @@ -1,4 +1,3 @@ - pin-depends: [ ["moonpool.dev" "git+https://github.com/c-cube/moonpool#main"] ] diff --git a/src/core/util_zlib.ml b/src/core/util_zlib.ml index 9f5b4f6e..a10d9fc5 100644 --- a/src/core/util_zlib.ml +++ b/src/core/util_zlib.ml @@ -11,12 +11,14 @@ let compress ?(buf = Buffer.create 32) (s : bytes) : bytes = let compress_str ?buf s : string = compress ?buf (Bytes.unsafe_of_string s) |> Bytes.unsafe_to_string -let decompress ?(buf = Buffer.create 32) (s : bytes) : bytes = +let decompress_slice ?(buf = Buffer.create 32) (s : bytes) offset len : bytes = + if offset + len > Bytes.length s then invalid_arg "Util_zlib.decompress"; Buffer.clear buf; - let i = ref 0 in + let i = ref offset in + let last = offset + len in Zlib.uncompress ~header:false (fun bytes -> - let len = min (Bytes.length s - !i) (Bytes.length bytes) in + let len = min (last - !i) (Bytes.length bytes) in if len > 0 then ( Bytes.blit s !i bytes 0 len; i := !i + len @@ -25,5 +27,7 @@ let decompress ?(buf = Buffer.create 32) (s : bytes) : bytes = (fun bytes len -> Buffer.add_subbytes buf bytes 0 len); Buffer.contents buf |> Bytes.unsafe_of_string +let decompress ?buf s : bytes = decompress_slice ?buf s 0 (Bytes.length s) + let decompress_str ?buf s : string = decompress ?buf (Bytes.unsafe_of_string s) |> Bytes.unsafe_to_string diff --git a/src/core/util_zlib.mli b/src/core/util_zlib.mli index 4e7376bf..99ecea87 100644 --- a/src/core/util_zlib.mli +++ b/src/core/util_zlib.mli @@ -8,4 +8,5 @@ val compress_str : ?buf:Buffer.t -> string -> string val decompress : ?buf:Buffer.t -> bytes -> bytes (** Decompress the bytes. *) +val decompress_slice : ?buf:Buffer.t -> bytes -> int -> int -> bytes val decompress_str : ?buf:Buffer.t -> string -> string