1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
let input_bigstring ic buf off len =
let tmp = Bytes.create len in
let res = input ic tmp 0 len in
Bigstringaf.blit_from_bytes tmp ~src_off:0 buf ~dst_off:off ~len:res;
res
let output_bigstring oc buf off len =
let res = Bigstringaf.substring buf ~off ~len in
output_string oc res
module N : sig
type encoder
type dst = [ `Channel of out_channel | `Buffer of Buffer.t | `Manual ]
type ret = [ `Flush of encoder | `End ]
val dst_rem : encoder -> int
val dst : encoder -> Zl.bigstring -> int -> int -> encoder
val encode : encoder -> ret
val encoder :
?level:int ->
i:Zl.bigstring ->
q:De.Queue.t ->
w:De.Lz77.window ->
source:int ->
H.bigstring ->
dst ->
Duff.hunk list ->
encoder
end = struct
type dst = [ `Channel of out_channel | `Buffer of Buffer.t | `Manual ]
type encoder = {
dst : dst;
src : Bigstringaf.t;
o : H.bigstring;
o_pos : int;
o_max : int;
h : H.N.encoder;
z : Zl.Def.encoder;
t : Zl.bigstring;
d : [ `Copy of int * int | `Insert of string | `End | `Await ] list;
}
type ret = [ `Flush of encoder | `End ]
let flush k e =
match e.dst with
| `Manual -> `Flush e
| `Channel oc ->
output_bigstring oc e.o 0 e.o_pos;
k { e with o_pos = 0 }
| `Buffer b ->
for i = 0 to e.o_pos - 1 do
Buffer.add_char b (Bigstringaf.get e.o i)
done;
k { e with o_pos = 0 }
let rec encode_z e =
match Zl.Def.encode e.z with
| `End z ->
let len = Bigstringaf.length e.o - Zl.Def.dst_rem z in
let z = Zl.Def.dst z De.bigstring_empty 0 0 in
if len > 0 then flush encode_z { e with z; o_pos = len } else `End
| `Flush z ->
let len = Bigstringaf.length e.o - Zl.Def.dst_rem z in
flush encode_z { e with z; o_pos = len }
| `Await z -> (
match e.d with
| [] ->
let z = Zl.Def.src z De.bigstring_empty 0 0 in
encode_z { e with z }
| d ->
H.N.dst e.h e.t 0 (De.bigstring_length e.t);
encode_h { e with z } d)
and encode_h e d =
let v, d = match d with v :: d -> v, d | [] -> `End, [] in
match H.N.encode e.h v, d with
| `Ok, [] ->
let len = Bigstringaf.length e.t - H.N.dst_rem e.h in
let z = Zl.Def.src e.z e.t 0 len in
encode_z { e with d; z }
| `Ok, d -> encode_h { e with d } d
| `Partial, d ->
let len = Bigstringaf.length e.t - H.N.dst_rem e.h in
let z = Zl.Def.src e.z e.t 0 len in
encode_z { e with d = `Await :: d; z }
let encode e = encode_z e
let encoder ?(level = 4) ~i ~q ~w ~source src dst hunks =
let o, o_pos, o_max =
match dst with
| `Manual -> De.bigstring_empty, 1, 0
| `Buffer _ | `Channel _ ->
De.bigstring_create H.io_buffer_size, 0, H.io_buffer_size - 1
in
let z = Zl.Def.encoder `Manual `Manual ~q ~w ~level in
let z = Zl.Def.dst z De.bigstring_empty 0 0 in
{
dst;
src;
o;
o_pos;
o_max;
t = i;
d =
List.map
(function
| Duff.Copy (off, len) -> `Copy (off, len)
| Duff.Insert (off, len) ->
`Insert (Bigstringaf.substring src ~off ~len))
hunks;
z;
h = H.N.encoder `Manual ~dst_len:(Bigstringaf.length src) ~src_len:source;
}
let dst_rem e = e.o_max - e.o_pos + 1
let dst e s j l =
let z = Zl.Def.dst e.z s j l in
{ e with z; o = s; o_pos = j; o_max = j + l - 1 }
end
module M : sig
type decoder
type src = [ `Channel of in_channel | `String of string | `Manual ]
type decode =
[ `Await of decoder
| `Header of int * int * decoder
| `End of decoder
| `Malformed of string ]
val src_len : decoder -> int
val dst_len : decoder -> int
val src_rem : decoder -> int
val dst_rem : decoder -> int
val src : decoder -> Zl.bigstring -> int -> int -> decoder
val dst : decoder -> H.bigstring -> int -> int -> decoder
val source : decoder -> H.bigstring -> decoder
val decode : decoder -> decode
val decoder :
?source:H.bigstring ->
o:Zl.bigstring ->
allocate:(int -> Zl.window) ->
src ->
decoder
end = struct
type src = [ `Channel of in_channel | `String of string | `Manual ]
type decoder = {
src : src;
dst : H.bigstring;
dst_len : int;
src_len : int;
i : Zl.bigstring;
i_pos : int;
i_len : int;
o : Zl.bigstring;
z : Zl.Inf.decoder;
h : H.M.decoder;
k : decoder -> decode;
}
and decode =
[ `Await of decoder
| `Header of int * int * decoder
| `End of decoder
| `Malformed of string ]
let refill k d =
match d.src with
| `String _ ->
let z = Zl.Inf.src d.z De.bigstring_empty 0 0 in
k { d with z }
| `Channel ic ->
let res = input_bigstring ic d.i 0 (De.bigstring_length d.i) in
let z = Zl.Inf.src d.z d.i 0 res in
k { d with z }
| `Manual -> `Await { d with k }
let rec decode d =
match H.M.decode d.h with
| `Header (src_len, dst_len) ->
`Header (src_len, dst_len, { d with src_len; dst_len; k = decode })
| `End -> `End { d with k = decode }
| `Malformed err -> `Malformed err
| `Await -> inflate { d with z = Zl.Inf.flush d.z }
and inflate d =
match Zl.Inf.decode d.z with
| `Await z ->
let dst_len = De.bigstring_length d.o - Zl.Inf.dst_rem z in
H.M.src d.h d.o 0 dst_len;
refill inflate { d with z }
| `End z ->
let dst_len = De.bigstring_length d.o - Zl.Inf.dst_rem z in
H.M.src d.h d.o 0 dst_len;
decode { d with z }
| `Flush z ->
let dst_len = De.bigstring_length d.o - Zl.Inf.dst_rem z in
H.M.src d.h d.o 0 dst_len;
decode { d with z }
| `Malformed err -> `Malformed err
let src d s j l =
let z = Zl.Inf.src d.z s j l in
{ d with z }
let dst d s j l =
H.M.dst d.h s j l;
d
let source d src =
H.M.source d.h src;
d
let dst_len d =
let dst_len = H.M.dst_len d.h in
assert (d.dst_len = dst_len);
dst_len
let src_len d =
let src_len = H.M.src_len d.h in
assert (d.src_len = src_len);
src_len
let dst_rem d = H.M.dst_rem d.h
let src_rem d = Zl.Inf.src_rem d.z
let decoder ?source ~o ~allocate src =
let decoder_z = Zl.Inf.decoder `Manual ~o ~allocate in
let decoder_h = H.M.decoder `Manual ?source in
let i, i_pos, i_len =
match src with
| `Manual -> De.bigstring_empty, 1, 0
| `String x ->
( Bigstringaf.of_string x ~off:0 ~len:(String.length x),
0,
String.length x - 1 )
| `Channel _ -> Bigstringaf.create De.io_buffer_size, 1, 0
in
{
src;
dst = De.bigstring_empty;
dst_len = 0;
src_len = 0;
i;
i_pos;
i_len;
o;
z = decoder_z;
h = decoder_h;
k = decode;
}
let decode d = d.k d
end