Skip to content

Commit c544e3a

Browse files
committed
Use Decompress
Superseed mirage#145
1 parent 54e2418 commit c544e3a

File tree

9 files changed

+65
-8
lines changed

9 files changed

+65
-8
lines changed

git-mirage.opam

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ depends: [
2222
"git" {>= "1.10.0"}
2323
"alcotest" {test}
2424
"mirage-fs-unix" {test & >= "1.3.0"}
25-
"camlzip" {test & >= "1.07"}
2625
"nocrypto" {test & >= "0.5.4"}
2726
"io-page" {test & >= "1.6.1"}
2827
]

git-unix.opam

-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ depends: [
1515
"cmdliner"
1616
"git-http" {>= "1.10.0"}
1717
"conduit" {>= "0.8.4"}
18-
"camlzip" {>= "1.06"}
1918
"nocrypto" {>= "0.2.0"}
2019
"mtime"
2120
"base-unix"
2221
"alcotest" {test}
2322
"mirage-fs-unix" {test & >= "1.3.0"}
24-
"camlzip" {test & >= "1.07"}
2523
"nocrypto" {test & >= "0.5.4"}
2624
"io-page" {test & >= "1.6.1"}
2725
]

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
]
2727
available: [ocaml-version >= "4.02.3"]

src/git/git.mli

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

435+
module M: S
436+
(** Default Zlib compress, provided by [Decompress]. *)
437+
435438
(** Minimaal signature provided by Zlib. *)
436439
module type ZLIB = sig
437440
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

test/common/jbuild

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