Skip to content

Commit 1d92c69

Browse files
committed
Use Decompress
Superseed mirage#145
1 parent 5d02d72 commit 1d92c69

File tree

10 files changed

+68
-9
lines changed

10 files changed

+68
-9
lines changed

git-mirage.opam

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ depends: [
2323
"alcotest" {test}
2424
"mtime" {test & >= "1.0.0"}
2525
"mirage-fs-unix" {test & >= "1.3.0"}
26-
"camlzip" {test & >= "1.07"}
2726
"nocrypto" {test & >= "0.5.4"}
2827
"io-page" {test & >= "1.6.1"}
2928
]

git-unix.opam

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ depends: [
1616
"logs"
1717
"git-http" {>= "1.10.0"}
1818
"conduit" {>= "0.8.4"}
19-
"camlzip" {>= "1.06"}
2019
"nocrypto" {>= "0.2.0"}
2120
"mtime" {>= "1.0.0"}
2221
"base-unix"

git.opam

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ depends: [
2020
"fmt"
2121
"hex"
2222
"astring"
23+
"decompress"
2324
"alcotest" {test}
24-
"camlzip" {test}
2525
"nocrypto" {test}
2626
"mtime" {test & >= "1.0.0"}
2727
]

src/git/git.mli

+4
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ module Inflate: sig
432432
module None: S
433433
(** No compression. *)
434434

435+
module M: S
436+
(** Default Zlib compression in pure OCaml, provided by
437+
[Decompress]. *)
438+
435439
(** Minimaal signature provided by Zlib. *)
436440
module type ZLIB = sig
437441
exception Error of string * string

src/git/inflate.ml

+56
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,62 @@ module None = struct
3333

3434
end
3535

36+
module M = struct
37+
38+
open Decompress
39+
40+
exception Deflate_error of Deflate.error
41+
42+
let deflate ?(level = 4) data =
43+
let input_buffer = Bytes.create 0xFFFF in
44+
let output_buffer = Bytes.create 0xFFFF in
45+
let pos = ref 0 in
46+
let res = Buffer.create (Cstruct.len data) in
47+
Deflate.bytes input_buffer output_buffer (fun input_buffer -> function
48+
| Some max ->
49+
let n = min max (min 0xFFFF (Cstruct.len data - !pos)) in
50+
Cstruct.blit_to_string data !pos input_buffer 0 n;
51+
pos := !pos + n;
52+
n
53+
| None ->
54+
let n = min 0xFFFF (Cstruct.len data - !pos) in
55+
Cstruct.blit_to_string data !pos input_buffer 0 n;
56+
pos := !pos + n;
57+
n
58+
) (fun output_buffer len ->
59+
Buffer.add_subbytes res output_buffer 0 len;
60+
0xFFFF)
61+
(Deflate.default ~proof:B.proof_bytes level)
62+
|> function
63+
| Ok _ -> Buffer.contents res |> Cstruct.of_string
64+
| Error exn -> raise (Deflate_error exn)
65+
66+
let inflate ?output_size (data:Mstruct.t) =
67+
let data = Mstruct.clone data in
68+
let input_buffer = Bytes.create 0xFFFF in
69+
let output_buffer = Bytes.create 0xFFFF in
70+
let window = Window.create ~proof:B.proof_bytes in
71+
let pos = ref 0 in
72+
let res = match output_size with
73+
| None -> Buffer.create (Mstruct.length data)
74+
| Some n -> Buffer.create n
75+
in
76+
Inflate.bytes input_buffer output_buffer (fun input_buffer ->
77+
let n = min 0xFFFF (Mstruct.length data - !pos) in
78+
let i = Mstruct.get_string data n in
79+
Bytes.blit i 0 input_buffer 0 n;
80+
pos := !pos + n;
81+
n
82+
) (fun output_buffer len ->
83+
Buffer.add_subbytes res output_buffer 0 len;
84+
0xFFFF)
85+
(Inflate.default window)
86+
|> function
87+
| Ok _ -> Some (Mstruct.of_string (Buffer.contents res))
88+
| Error _ -> None
89+
90+
end
91+
3692
module type ZLIB = sig
3793

3894
exception Error of string * string

src/git/inflate.mli

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module type S = sig
2020
end
2121

2222
module None: S
23+
module M: S
2324

2425
module type ZLIB = sig
2526
exception Error of string * string

src/git/jbuild

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
(library
44
((name git)
55
(public_name git)
6-
(libraries (cstruct fmt lwt mstruct uri ocamlgraph logs astring hex))))
6+
(libraries (cstruct fmt lwt mstruct uri ocamlgraph logs astring hex
7+
decompress))))

test/common/jbuild

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
(library
44
((name test)
55
(wrapped false)
6-
(libraries (git mtime mtime.clock.os alcotest nocrypto camlzip lwt.unix
7-
logs.fmt))))
6+
(libraries (git mtime mtime.clock.os alcotest nocrypto lwt.unix logs.fmt))))

test/git-mirage/test.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module M = struct
8181

8282
end
8383

84-
module S = FS(M)(SHA1_slow)(Git.Inflate.Make(Zlib))
84+
module S = FS(M)(SHA1_slow)(Git.Inflate.M)
8585

8686
let suite =
8787
{

test/git/test.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ open Git
1818
open Lwt.Infix
1919
open Test_common
2020

21-
(* To avoid depending on git-unix *)
22-
module Zlib = Git.Inflate.Make(Zlib)
21+
module Zlib = Git.Inflate.M
2322

23+
(* To avoid depending on git-unix *)
2424
module SHA1 = struct
2525

2626
let cstruct buf =

0 commit comments

Comments
 (0)