package mrmime

  1. Overview
  2. Docs

Source file enclosure.ml

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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
[@@@warning "-32"]

module type V = sig
  type t

  val pp : t Fmt.t
  val sentinel : t
  val weight : t -> int
  val merge : t -> t -> t option
  val physically_equal : t -> t -> bool
end

module RBQ (V : V) = struct
  module Queue = Ke.Fke.Weighted

  type t = {a: V.t array; c: int; m: int; q: (int, Bigarray_compat.int_elt) Queue.t}
  (* XXX(dinosaure): [ke] is limited to [Bigarray_compat.kind]. We make an [array]
     which will contain values and [q] will contain index of them. Length of [a]
     is length of [q]. By this way, length is a power of two and [a] follows
     same assertions (see [mask]) as [Ke].

     [c] will be the cursor in [a]. [m] is the capacity. It's a good example of
     [ke] with something else than [Bigarray_compat.kind]. *)

  let make capacity =
    let q, capacity = Queue.create ~capacity Bigarray_compat.Int in
    { a= Array.make capacity V.sentinel
    ; c= 0
    ; m= capacity
    ; q }

  let pp ppf t =
    let a = Array.make (Queue.length t.q) V.sentinel in
    let x = ref 0 in
    Queue.iter (fun i -> a.(!x) <- t.a.(i) ; incr x) t.q ;
    Fmt.pf ppf "{ @[<hov>a = %a;@ \
                         c = %d;@ \
                         m = %d;@ \
                         q = %a;@] }"
      Fmt.(Dump.array V.pp) a
      t.c t.m
      (Queue.dump Fmt.int) t.q

  let available t = Queue.available t.q
  let is_empty t = Queue.is_empty t.q

  let[@inline always] mask x t = x land (t.m - 1)

  let push t v =
    let i = mask t.c t in
    match Queue.push t.q i with
    | Some q ->
      t.a.(i) <- v ;
      Ok { t with c= succ t.c; q; }
    | None -> Error t

  let shift_exn t =
    let i, q = Queue.pop_exn t.q in
    (t.a.(i), { t with q })

  let cons t v =
    let i = mask t.c t in
    match Queue.cons t.q i with
    | Some q ->
      t.a.(i) <- v ;
      Ok { t with c= succ t.c; q; }
    | None -> Error t

  exception Full

  let cons_exn t v =
    match cons t v with
    | Ok t -> t
    | Error _ -> raise Full

  let weight t =
    Queue.fold (fun a i -> a + V.weight t.a.(i)) 0 t.q

  let to_list t =
    let res = ref [] in
    Queue.rev_iter (fun i -> res := t.a.(i) :: !res) t.q ;
    !res
end

let pp_chr =
  Fmt.using (function '\032' .. '\126' as x -> x | _ -> '.') Fmt.char

let pp_scalar : type buffer.
    get:(buffer -> int -> char) -> length:(buffer -> int) -> buffer Fmt.t =
 fun ~get ~length ppf b ->
  let l = length b in
  for i = 0 to l / 16 do
    Fmt.pf ppf "%08x: " (i * 16) ;
    let j = ref 0 in
    while !j < 16 do
      if (i * 16) + !j < l then
        Fmt.pf ppf "%02x" (Char.code @@ get b ((i * 16) + !j))
      else Fmt.pf ppf "  " ;
      if !j mod 2 <> 0 then Fmt.pf ppf " " ;
      incr j
    done ;
    Fmt.pf ppf "  " ;
    j := 0 ;
    while !j < 16 do
      if (i * 16) + !j < l then Fmt.pf ppf "%a" pp_chr (get b ((i * 16) + !j))
      else Fmt.pf ppf " " ;
      incr j
    done ;

    Fmt.pf ppf "@,"
  done

module RBA = Ke.Fke.Weighted

module Buffer = struct
  type t =
    | Bigstring of Bigstringaf.t
    | String of string
    | Bytes of bytes

  let pp ppf = function
    | Bigstring x -> pp_scalar ~length:Bigstringaf.length ~get:Bigstringaf.get ppf x
    | String x -> pp_scalar ~length:String.length ~get:String.get ppf x
    | Bytes x -> pp_scalar ~length:Bytes.length ~get:Bytes.get ppf x

  let weight = function
    | Bigstring x -> Bigstringaf.length x
    | String x -> String.length x
    | Bytes x -> Bytes.length x

  let sub buffer off len = match buffer with
    | Bigstring x -> Bigstring (Bigstringaf.sub x ~off ~len)
    | String x -> String (String.sub x off len)
    | Bytes x -> Bytes (Bytes.sub x off len)
end

module IOVec = struct
  type t = {buffer: Buffer.t; off: int; len: int}

  let weight {len; _} = len

  let pp ppf t =
    Fmt.pf ppf "{ @[<hov>buffer= @[<hov>%a@];@ \
                         off= %d;@ len= %d;@] }"
      Buffer.pp t.buffer t.off t.len

  let sentinel =
    let deadbeef = "\222\173\190\239" in
    {buffer= Buffer.String deadbeef; off= 0; len= String.length deadbeef}

  let make buffer off len =
    {buffer; off; len}

  let length {len; _} = len

  let lengthv = List.fold_left (fun a x -> length x + a) 0

  let shift {buffer; off; len} n =
    assert (n <= len) ;
    {buffer; off= off + n; len= len - n}

  let split {buffer; off; len} n =
    assert (n <= len) ;
    ( {buffer= Buffer.sub buffer off n; off= 0; len= n}
    , {buffer= Buffer.sub buffer (off + n) (len - n); off= 0; len= len - n})

  let physically_equal a b =
    match a, b with
    | {buffer= Buffer.Bytes a; _}, {buffer= Buffer.Bytes b; _} -> a == b
    | {buffer= Buffer.Bigstring a; _}, {buffer= Buffer.Bigstring b; _} ->
      ( match Overlap.array1 a b with
        | Some (len, 0, 0) ->
          Bigstringaf.length a = len && Bigstringaf.length b = len
        | _ -> false )
    | _, _ -> false

  let merge a b =
    match a, b with
    | {buffer= Buffer.Bytes a'; _}, {buffer= Buffer.Bytes b'; _} ->
      assert (a' == b') ;
      if a.off + a.len = b.off
      then Some {buffer= Buffer.Bytes a'; off= a.off; len= a.len + b.len}
      else None
    | {buffer= Buffer.Bigstring a'; _}, {buffer= Buffer.Bigstring _; _} ->
      if a.off + a.len = b.off
      then Some {buffer= Buffer.Bigstring a'; off= a.off; len= a.len + b.len}
      else None
    | _, _ -> None
end

module RBS = RBQ (IOVec)

type emitter = IOVec.t list -> int

type encoder =
  { sched : RBS.t
  ; write : (char, Bigarray_compat.int8_unsigned_elt) RBA.t
  ; flush : (int * (int -> encoder -> unit)) Ke.Fke.t
  ; written : int
  ; received : int
  ; emitter : emitter }

let pp_flush ppf _ = Fmt.string ppf "#flush"

let pp ppf t =
  Fmt.pf ppf "{ @[<hov>sched= @[<hov>%a@];@ \
                       write= @[<hov>%a@];@ \
                       flush= @[<hov>%a@];@ \
                       written= %d;@ \
                       received= %d;@ \
                       emitter= #emitter;@] }"
    RBS.pp t.sched
    (RBA.pp pp_chr) t.write
    (Ke.Fke.pp pp_flush) t.flush
    t.written t.received

let is_empty t = RBS.is_empty t.sched

(* XXX(dinosaure): [sched] is a queue of [IOVec]. [write] is a
   ring-buffer/[Bigstringaf.t]. [flush] is a queue which can contain
   user-defined operation at a break point. [written] is how many bytes we
   sended to the user (afterwards a *flush* operation). [received] is how many
   bytes we received from the user.

   The goal is to have two ways to fill output:
   - an heavy way with [write_*] operations which will do internally a [blit].
   - a soft way with [shedule_*] operations which will store a pointer.

   The complexity is under [sched] where it stores pointer from user but pointer
   from [write] queue too. Indeed, [write_] operations did not do only a [blit]
   but then they store resulted/*blitted* [Bigstringaf.t] part to [sched].

   When we want to shift a part of [encoder], **all** buffers are stored in
   [sched]. So we need to shift [sched]. However, resulted [IOVec] can be
   physically a part of [write]. In this context, we need to shift [write]. *)

let create ~emitter len =
  let write, _ = RBA.create ~capacity:len Bigarray_compat.Char in
  { sched= RBS.make (len * 2)
  ; write
  ; flush = Ke.Fke.empty
  ; written= 0
  ; received= 0
  ; emitter }

let check iovec {write; _} =
  match iovec with
  | {IOVec.buffer= Buffer.Bigstring x; _} ->
    let buf = RBA.unsafe_bigarray write in
    ( match Overlap.array1 x buf with
    | Some (_, _, _) -> true
    | None -> false )
  | _ -> false

let shift_buffers written t =
  let rec go written acc t =
    match RBS.shift_exn t.sched with
    | iovec, shifted ->
      let len = IOVec.length iovec in
      if written > len
      then go (written - len) (iovec :: acc)
          { t with sched= shifted
                 ; write=
                     if check iovec t
                     then RBA.N.shift_exn t.write len
                     else t.write }
      else if written > 0
      then
        let last, rest = IOVec.split iovec written in
        ( List.rev (last :: acc)
        , { t with sched= RBS.cons_exn shifted rest
                 ; write=
                     if check iovec t
                     then RBA.N.shift_exn t.write (IOVec.length last)
                     else t.write })
      else (List.rev acc, t)
    | exception RBS.Queue.Empty -> (List.rev acc, t) in
  go written [] t

let shift_flushes written t =
  let rec go t =
    try
      let (threshold, f), flush = Ke.Fke.pop_exn t.flush in
      if compare (t.written + written - min_int) (threshold - min_int) >= 0
      then let () = f written {t with flush} in go {t with flush}
      else t
    with Ke.Fke.Empty -> t in
  go t

let shift n t =
  let lst, t = shift_buffers n t in
  ( lst
  , let t = shift_flushes (IOVec.lengthv lst) t in { t with written = t.written + n} )

let has t = RBS.weight t.sched

let drain drain t =
  let rec go rest t =
    match RBS.shift_exn t.sched with
    | iovec, shifted ->
        let len = IOVec.length iovec in
        if rest >= len then
          go (rest - len)
            { t with
              sched= shifted
            ; write=
                if check iovec t
                then RBA.N.shift_exn t.write len
                else t.write }
        else
          { t with
            sched= RBS.cons_exn shifted (IOVec.shift iovec rest)
          ; write=
              if check iovec t
              then RBA.N.shift_exn t.write rest
              else t.write }
    | exception RBS.Queue.Empty -> t in
  let t = go drain t in { t with written= t.written + drain }

let flush k t =
  let t = shift_flushes (has t) t in
  let n = t.emitter (RBS.to_list t.sched) in
  let t = drain n t in
  k { t with written= t.written + n }

let rec schedule k ~length ~buffer ?(off = 0) ?len v t =
  let len = match len with Some len -> len | None -> length v - off in
  match RBS.push t.sched (IOVec.make (buffer v) off len) with
  | Ok sched ->
    (* TODO: merge [Bigstringaf.t]. *)
    k { t with sched; received= t.received + len }
  | Error _ ->
      let max = RBS.available t.sched in
      let k t =
        (schedule [@tailcall]) k ~length ~buffer ~off:(off + max)
          ~len:(len - max) v t in
      schedule (flush k) ~length ~buffer ~off ~len:max v t

external identity : 'a -> 'a = "%identity"

let kschedule_string =
  let length = String.length in
  let buffer x = Buffer.String x in
  fun k t ?(off = 0) ?len v -> schedule k ~length ~buffer ~off ?len v t

let schedule_string = kschedule_string identity

let kschedule_bytes =
  let length = Bytes.length in
  let buffer x = Buffer.Bytes x in
  fun k t ?(off = 0) ?len v -> schedule k ~length ~buffer ~off ?len v t

let schedule_bytes = kschedule_bytes identity

let kschedule_bigstring =
  let length = Bigarray_compat.Array1.dim in
  let buffer x = Buffer.Bigstring x in
  fun k t ?(off = 0) ?len v -> schedule k ~length ~buffer ~off ?len v t

let schedule_bigstring = kschedule_bigstring identity

let schedule_flush f t = {t with flush= Ke.Fke.push t.flush (t.received, f)}

let kschedulev k l t =
  let rec go t = function
    | [] -> k t
    | (length, off, len, buffer) :: r ->
      schedule
        (fun t -> (go [@tailcall]) t r)
        ~length ?off ?len ~buffer:identity buffer t
  in go t l

let schedulev = kschedulev identity

let kschedulev_bigstring k l t =
  let rec go t = function
    | [] -> k t
    | buffer :: r ->
      kschedule_bigstring (fun t -> (go [@tailcall]) t r) t buffer
  in go t l

let schedulev_bigstring = kschedulev_bigstring identity

let rec kwrite k ~blit ~length ?(off = 0) ?len buffer t =
  let len = match len with Some len -> len | None -> length buffer - off in
  let available = RBA.available t.write in
  (* XXX(dinosaure): we can factorize the first and the second branch. *)
  if available >= len then
    let areas, write = RBA.N.push_exn t.write ~blit ~length ~off ~len buffer in
    kschedulev_bigstring k areas {t with write}
  else if available > 0 then
    let k t =
      (kwrite [@tailcall]) k ~blit ~length ~off:(off + available)
        ~len:(len - available) buffer t in
    let areas, write = RBA.N.push_exn t.write ~blit ~length ~off ~len:available buffer in
    kschedulev_bigstring (flush k) areas {t with write}
  else
    let k t = (kwrite [@tailcall]) k ~blit ~length ~off ~len buffer t in
    flush k t

let write = kwrite identity

let kwritev k l t =
  let rec go t = function
    | [] -> k t
    | (blit, length, off, len, buffer) :: r ->
        kwrite (fun t -> (go [@tailcall]) t r) ~blit ~length ?off ?len buffer t
  in go t l

let bigarray_blit_from_string src src_off dst dst_off len =
  Bigstringaf.blit_from_string src ~src_off dst ~dst_off ~len

let bigarray_blit_from_bytes src src_off dst dst_off len =
  Bigstringaf.blit_from_bytes src ~src_off dst ~dst_off ~len

let bigarray_blit src src_off dst dst_off len =
  Bigarray_compat.Array1.(blit (sub src src_off len) (sub dst dst_off len))

let bigarray_blit_to_bytes src src_off dst dst_off len =
  Bigstringaf.blit_to_bytes src ~src_off dst ~dst_off ~len

let kwrite_string =
  let length = String.length in
  let blit = bigarray_blit_from_string in
  fun k ?(off = 0) ?len a t -> kwrite k ~blit ~length ~off ?len a t

let write_string = kwrite_string identity

let kwrite_bytes =
  let length = Bytes.length in
  let blit = bigarray_blit_from_bytes in
  fun k ?(off = 0) ?len a t -> kwrite k ~blit ~length ~off ?len a t

let write_bytes = kwrite_bytes identity

let kwrite_bigstring =
  let length = Bigarray_compat.Array1.dim in
  let blit = bigarray_blit in
  fun k ?(off = 0) ?len a t -> kwrite k ~blit ~length ~off ?len a t

let write_bigstring = kwrite_bigstring identity

let kwrite_char =
  let length _ = assert false in
  let blit src src_off dst dst_off len =
    assert (src_off = 0) ;
    assert (len = 1) ;
    Bigstringaf.set dst dst_off src
  in
  fun k a t -> kwrite k ~length ~blit ~off:0 ~len:1 a t

let write_char = kwrite_char identity

let kwrite_uint8 =
  let length _ = assert false in
  let blit src src_off dst dst_off len =
    assert (src_off = 0) ;
    assert (len = 1) ;
    Bigstringaf.set dst dst_off (Char.unsafe_chr src)
  in
  fun k a t -> kwrite k ~length ~blit ~off:0 ~len:1 a t

let write_uint8 = kwrite_uint8 identity

module type S = sig
  val kwrite_uint16 : (encoder -> 'v) -> int -> encoder -> 'v
  val write_uint16 : int -> encoder -> encoder
  val kwrite_uint32 : (encoder -> 'v) -> int32 -> encoder -> 'v
  val write_uint32 : int32 -> encoder -> encoder
  val kwrite_uint64 : (encoder -> 'v) -> int64 -> encoder -> 'v
  val write_uint64 : int64 -> encoder -> encoder
end

module type ENDIAN = sig
  type t = Bigstringaf.t

  val set_int16 : t -> int -> int -> unit
  val set_int32 : t -> int -> int32 -> unit
  val set_int64 : t -> int -> int64 -> unit
end

module Make (X : ENDIAN) : S = struct
  let _length _ = assert false

  let kwrite_uint16 =
    let length = _length in
    let blit src src_off dst dst_off len =
      assert (src_off = 0) ;
      assert (len = 2) ;
      X.set_int16 dst dst_off src
    in
    fun k a t -> kwrite k ~length ~blit ~off:0 ~len:2 a t

  let write_uint16 = kwrite_uint16 identity

  let kwrite_uint32 =
    let length = _length in
    let blit src src_off dst dst_off len =
      assert (src_off = 0) ;
      assert (len = 4) ;
      X.set_int32 dst dst_off src
    in
    fun k a t -> kwrite k ~length ~blit ~off:0 ~len:4 a t

  let write_uint32 = kwrite_uint32 identity

  let kwrite_uint64 =
    let length = _length in
    let blit src src_off dst dst_off len =
      assert (src_off = 0) ;
      assert (len = 8) ;
      X.set_int64 dst dst_off src
    in
    fun k a t -> kwrite k ~length ~blit ~off:0 ~len:8 a t

  let write_uint64 = kwrite_uint64 identity
end

module LE' = struct
  type t = Bigstringaf.t

  let set_int16 = Bigstringaf.set_int16_le
  let set_int32 = Bigstringaf.set_int32_le
  let set_int64 = Bigstringaf.set_int64_le
end

module BE' = struct
  type t = Bigstringaf.t

  let set_int16 = Bigstringaf.set_int16_be
  let set_int32 = Bigstringaf.set_int32_be
  let set_int64 = Bigstringaf.set_int64_be
end

module LE = Make(LE')
module BE = Make(BE')
OCaml

Innovation. Community. Security.