Source file index_pack.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
let src =
Logs.Src.create "git.idx-pack" ~doc:"Git internal lazy index-pack decoder"
module Log = (val Logs.src_log src : Logs.LOG)
module type LAZY = sig
module Hash : S.HASH
type error =
| Invalid_version of int32
| Invalid_index
| Expected_bigoffset_table
| Invalid_bigoffset_index of int
val pp_error : error Fmt.t
type t
val make : ?cache:int -> Cstruct.t -> (t, error) result
val find : t -> Hash.t -> (Checkseum.Crc32.t * int64) option
val mem : t -> Hash.t -> bool
val iter : t -> (Hash.t -> Checkseum.Crc32.t * int64 -> unit) -> unit
val fold : t -> (Hash.t -> Checkseum.Crc32.t * int64 -> 'a -> 'a) -> 'a -> 'a
val cardinal : t -> int
end
module Lazy (Hash : S.HASH) = struct
type error =
| Invalid_version of int32
| Invalid_index
| Expected_bigoffset_table
| Invalid_bigoffset_index of int
let pp_error ppf = function
| Invalid_header -> Fmt.pf ppf "(Invalid_header %s)" header
| Invalid_version version -> Fmt.pf ppf "(Invalid_version %ld)" version
| Invalid_index -> Fmt.pf ppf "Invalid_index"
| Expected_bigoffset_table -> Fmt.pf ppf "Expected_bigoffset_table"
| Invalid_bigoffset_index index ->
Fmt.pf ppf "(Invalid_bigoffset_index %d)" index
module Cache =
Lru.M.Make
(Hash)
(struct
type t = Checkseum.Crc32.t * int64
let weight _ = 1
end)
type t =
{ map: Cstruct.t
; number_of_hashes: int
; fanout_offset: int
; hashes_offset: int
; crcs_offset: int
; values_offset: int
; v64_offset: int option
; cache: Cache.t }
let has map off len =
if off < 0 || len < 0 || off + len > Cstruct.len map then
raise (Invalid_argument (Fmt.strf "%d:%d:%d" off len (Cstruct.len map)))
else true
let map =
Log.debug (fun l -> l "Checking the IDX header.") ;
if has map 0 4 then
if
Cstruct.get_char map 0 = '\255'
&& Cstruct.get_char map 1 = '\116'
&& Cstruct.get_char map 2 = '\079'
&& Cstruct.get_char map 3 = '\099'
then Ok ()
else Error (Invalid_header (Cstruct.to_string @@ Cstruct.sub map 0 4))
else Error Invalid_index
let check_version map =
Log.debug (fun l -> l "Checking the IDX version.") ;
if has map 4 4 then
if Cstruct.BE.get_uint32 map 4 = 2l then Ok ()
else Error (Invalid_version (Cstruct.BE.get_uint32 map 4))
else Error Invalid_index
let number_of_hashes map =
if has map 8 (256 * 4) then (
let n = Cstruct.BE.get_uint32 map (8 + (255 * 4)) in
Log.debug (fun l -> l "Current IDX file has %ld objects." n) ;
Ok (8, n) )
else Error Invalid_index
let bind v f = match v with Ok v -> f v | Error _ as e -> e
let ( >>= ) = bind
let ( *> ) u v = match u with Ok _ -> v | Error _ as e -> e
let make ?(cache = 1024) map =
check_header map *> check_version map *> number_of_hashes map
>>= fun (fanout_offset, number_of_hashes) ->
let number_of_hashes = Int32.to_int number_of_hashes in
let hashes_offset = 8 + (256 * 4) in
let crcs_offset = 8 + (256 * 4) + (number_of_hashes * 20) in
let values_offset =
8 + (256 * 4) + (number_of_hashes * 20) + (number_of_hashes * 4)
in
let v64_offset =
8
+ (256 * 4)
+ (number_of_hashes * 20)
+ (number_of_hashes * 4)
+ (number_of_hashes * 4)
in
let v64_offset =
if v64_offset + (Hash.digest_size * 2) = Cstruct.len map then None
else Some v64_offset
in
Ok
{ map
; number_of_hashes
; fanout_offset
; hashes_offset
; crcs_offset
; values_offset
; v64_offset
; cache= Cache.create ~random:true cache }
let cardinal {number_of_hashes; _} = number_of_hashes
exception Break
let compare buf off hash =
try
for i = 0 to Hash.digest_size - 1 do
if Cstruct.get_char buf (off + i) <> Cstruct.get_char hash i then
raise Break
done ;
true
with Break -> false
exception ReturnT
exception ReturnF
let lt buf off hash =
try
for i = 0 to Hash.digest_size - 1 do
let a = Cstruct.get_uint8 buf (off + i) in
let b = Cstruct.get_uint8 hash i in
if a > b then raise ReturnT else if a <> b then raise ReturnF
done ;
false
with
| ReturnT -> true
| ReturnF -> false
let binary_search buf hash =
let rec aux off len buf =
if len = Hash.digest_size then off / Hash.digest_size
else
let len' = len / (Hash.digest_size * 2) * Hash.digest_size in
let off' = off + len' in
if compare buf off' hash then off' / Hash.digest_size
else if lt buf off' hash then (aux [@tailcall]) off len' buf
else (aux [@tailcall]) off' (len - len') buf
in
aux 0 (Cstruct.len buf) buf
let fanout_idx t hash =
( match Cstruct.get_uint8 hash 0 with
| 0 ->
let n = Cstruct.BE.get_uint32 t.map t.fanout_offset in
if n = 0l then Error Invalid_index
else
Ok
(binary_search
(Cstruct.sub t.map t.hashes_offset
(Int32.to_int n * Hash.digest_size))
hash)
| idx ->
if
has t.map (t.fanout_offset + (4 * idx)) 4
&& has t.map (t.fanout_offset + (4 * (idx - 1))) 4
then
let off1 =
Int32.to_int
@@ Cstruct.BE.get_uint32 t.map (t.fanout_offset + (4 * idx))
in
let off0 =
Int32.to_int
@@ Cstruct.BE.get_uint32 t.map (t.fanout_offset + (4 * (idx - 1)))
in
if
has t.map
(t.hashes_offset + (off0 * Hash.digest_size))
((off1 - off0) * Hash.digest_size)
&& off1 - off0 > 0
then
Ok
( off0
+ binary_search
(Cstruct.sub t.map
(t.hashes_offset + (off0 * Hash.digest_size))
((off1 - off0) * Hash.digest_size))
hash )
else Error Invalid_index
else Error Invalid_index )
|> function
| Ok off ->
let hash' =
Cstruct.sub t.map
(t.hashes_offset + (off * Hash.digest_size))
Hash.digest_size
in
if Cstruct.equal hash hash' then Ok off else Error Invalid_index
| Error err -> Error err
let mem t hash =
match Cache.find hash t.cache with
| Some _ -> true
| None -> (
match fanout_idx t (Cstruct.of_string (Hash.to_raw_string hash)) with
| Ok _ -> true
| Error _ -> false )
let find t hash =
match Cache.find hash t.cache with
| Some (crc, offset) ->
Log.debug (fun l ->
l "Retrieving the information of %a from the cache." Hash.pp hash
) ;
Some (crc, offset)
| None -> (
match fanout_idx t (Cstruct.of_string (Hash.to_raw_string hash)) with
| Ok idx -> (
let crc = Cstruct.BE.get_uint32 t.map (t.crcs_offset + (idx * 4)) in
let off =
Cstruct.BE.get_uint32 t.map (t.values_offset + (idx * 4))
in
let off =
if Int32.equal 0l (Int32.logand off 0x80000000l) then
Ok (Int64.of_int32 off)
else
match t.v64_offset with
| Some v64_offset ->
Log.debug (fun l ->
l "Retrieving a big offset of %a." Hash.pp hash ) ;
let n = Int32.to_int (Int32.logand off 0x7FFFFFFFl) in
if has t.map (v64_offset + (n * 8)) 8 then
Ok (Cstruct.BE.get_uint64 t.map (v64_offset + (n * 8)))
else Error (Invalid_bigoffset_index n)
| None -> Error Expected_bigoffset_table
in
match off with
| Ok off ->
Cache.add hash (Crc32.of_int32 crc, off) t.cache ;
Some (Crc32.of_int32 crc, off)
| Error _ -> None )
| Error _ -> None )
let iter t f =
for i = 0 to t.number_of_hashes - 1 do
let hash =
Cstruct.sub t.map
(t.hashes_offset + (i * Hash.digest_size))
Hash.digest_size
in
let crc =
Crc32.of_int32 (Cstruct.BE.get_uint32 t.map (t.crcs_offset + (i * 4)))
in
let off = Cstruct.BE.get_uint32 t.map (t.values_offset + (i * 4)) in
let off =
if Int32.equal 0l (Int32.logand off 0x80000000l) then
Int64.of_int32 off
else
match t.v64_offset with
| Some v64_offset ->
let n = Int32.to_int (Int32.logand off 0x7FFFFFFFl) in
Cstruct.BE.get_uint64 t.map (v64_offset + (n * 8))
| None -> raise (Invalid_argument "Expected big offset table")
in
f (Hash.of_raw_string (Cstruct.to_string hash)) (crc, off)
done
let fold t f a =
let a = ref a in
iter t (fun k v -> a := f k v !a) ;
!a
end
module type DECODER = sig
module Hash : S.HASH
type error =
| Invalid_byte of int
| Invalid_version of Int32.t
| Invalid_index_of_bigoffset of int
| Expected_bigoffset_table
| Invalid_hash of Hash.t * Hash.t
val pp_error : error Fmt.t
type t
val pp : t Fmt.t
val make : unit -> t
val refill : int -> int -> t -> t
val eval :
Cstruct.t
-> t
-> [ `Await of t
| `End of t * Hash.t
| `Hash of t * (Hash.t * Checkseum.Crc32.t * int64)
| `Error of t * error ]
end
module Decoder (Hash : S.HASH) = struct
type error =
| Invalid_byte of int
| Invalid_version of Int32.t
| Invalid_index_of_bigoffset of int
| Expected_bigoffset_table
| Invalid_hash of Hash.t * Hash.t
let pp_error ppf = function
| Invalid_byte byte -> Fmt.pf ppf "(Invalid_byte %02x)" byte
| Invalid_version version -> Fmt.pf ppf "(Invalid_version %ld)" version
| Invalid_index_of_bigoffset idx ->
Fmt.pf ppf "(Invalid_index_of_bigoffset %d)" idx
| Expected_bigoffset_table -> Fmt.pf ppf "Expected_bigoffset_table"
| Invalid_hash (has, expect) ->
Fmt.pf ppf "(Invalid_hash (%a, %a))" Hash.pp has Hash.pp expect
type t =
{ i_off: int
; i_pos: int
; i_len: int
; fanout: Int32.t array
; hashes: Hash.t Queue.t
; crcs: Checkseum.Crc32.t Queue.t
; offsets: (Int32.t * bool) Queue.t
; hash: Hash.ctx
; state: state }
and k = Cstruct.t -> t -> res
and state =
| Fanout of k
| Hashes of k
| Crcs of k
| Offsets of k
| Hash of k
| Ret of int64 array option * Hash.t * Hash.t
| End of Hash.t
| Exception of error
and res =
| Wait of t
| Error of t * error
| Cont of t
| Result of t * (Hash.t * Checkseum.Crc32.t * Int64.t)
| Ok of t * Hash.t
let pp_state ppf = function
| Header _ -> Fmt.pf ppf "(Header #k)"
| Fanout _ -> Fmt.pf ppf "(Fanout #k)"
| Hashes _ -> Fmt.pf ppf "(Hashes #k)"
| Crcs _ -> Fmt.pf ppf "(Crcs #k)"
| Offsets _ -> Fmt.pf ppf "(Offsets #k)"
| Hash _ -> Fmt.pf ppf "(Hash #k)"
| End hash_pack -> Fmt.pf ppf "(End %a)" Hash.pp hash_pack
| Exception exn -> Fmt.pf ppf "(Exception %a)" pp_error exn
| Ret (boffs, hash_idx, hash_pack) ->
Fmt.pf ppf "(Ret (big offsets:%d, idx:%a, pack:%a))"
(Helper.Option.value ~default:0
(Helper.Option.map Array.length boffs))
Hash.pp hash_idx Hash.pp hash_pack
let pp ppf t =
Fmt.pf ppf
"{ @[<hov>i_off = %d;@ i_pos = %d;@ i_len = %d;@ fanout = #table;@ \
hashes = #queue;@ crcs = #queue;@ offsets = #queue;@ hash = #ctx;@ \
state = %a;@] }"
t.i_off t.i_pos t.i_len (Fmt.hvbox pp_state) t.state
let feed_cstruct h c = Hash.feed_bigstring h (Cstruct.to_bigarray c)
let await src t =
let hash = feed_cstruct t.hash (Cstruct.sub src t.i_off t.i_pos) in
Wait {t with hash}
let error t exn : res = Error ({t with state= Exception exn}, exn)
let ok t hash : res = Ok ({t with state= End hash}, hash)
let to_int32 b0 b1 b2 b3 =
let ( << ) = Int32.shift_left in
let ( || ) = Int32.logor in
Int32.of_int b0 << 24
|| Int32.of_int b1 << 16
|| Int32.of_int b2 << 8
|| Int32.of_int b3
let to_int64 b0 b1 b2 b3 b4 b5 b6 b7 =
let ( << ) = Int64.shift_left in
let ( || ) = Int64.logor in
Int64.of_int b0 << 56
|| Int64.of_int b1 << 48
|| Int64.of_int b2 << 40
|| Int64.of_int b3 << 32
|| Int64.of_int b4 << 24
|| Int64.of_int b5 << 16
|| Int64.of_int b6 << 8
|| Int64.of_int b7
let rec get_byte ~ctor k src t =
if t.i_len - t.i_pos > 0 then
let byte = Cstruct.get_uint8 src (t.i_off + t.i_pos) in
k byte src {t with i_pos= t.i_pos + 1}
else
await src
{t with state= ctor (fun src t -> (get_byte [@tailcall]) ~ctor k src t)}
let rec get_u32 ~ctor k src t =
if t.i_len - t.i_pos > 3 then
let num = Cstruct.BE.get_uint32 src (t.i_off + t.i_pos) in
k num src {t with i_pos= t.i_pos + 4}
else if t.i_len - t.i_pos > 0 then
( get_byte ~ctor
@@ fun byte0 ->
get_byte ~ctor
@@ fun byte1 ->
get_byte ~ctor
@@ fun byte2 ->
get_byte ~ctor
@@ fun byte3 src t -> k (to_int32 byte0 byte1 byte2 byte3) src t )
src t
else
await src
{ t with
state= Header (fun src t -> (get_u32 [@tailcall]) ~ctor k src t) }
let rec get_u64 ~ctor k src t =
if t.i_len - t.i_pos > 7 then
let num = Cstruct.BE.get_uint64 src (t.i_off + t.i_pos) in
k num src {t with i_pos= t.i_pos + 8}
else if t.i_len - t.i_pos > 0 then
( get_byte ~ctor
@@ fun byte0 ->
get_byte ~ctor
@@ fun byte1 ->
get_byte ~ctor
@@ fun byte2 ->
get_byte ~ctor
@@ fun byte3 ->
get_byte ~ctor
@@ fun byte4 ->
get_byte ~ctor
@@ fun byte5 ->
get_byte ~ctor
@@ fun byte6 ->
get_byte ~ctor
@@ fun byte7 src t ->
k (to_int64 byte0 byte1 byte2 byte3 byte4 byte5 byte6 byte7) src t )
src t
else
await src
{ t with
state= Offsets (fun src t -> (get_u64 [@tailcall]) k ~ctor src t) }
module KFanoutTable = struct let get_u32 = get_u32 ~ctor:(fun k -> Fanout k)
end
module KHashes = struct
let get_byte = get_byte ~ctor:(fun k -> Hashes k)
let get_hash k src t =
let res = Bytes.create Hash.digest_size in
let rec loop i src t =
if i = Hash.digest_size then k (Bytes.unsafe_to_string res) src t
else
get_byte
(fun byte src t ->
Bytes.set res i (Char.unsafe_chr byte) ;
(loop [@tailcall]) (i + 1) src t )
src t
in
loop 0 src t
end
module KCrcs = struct let get_u32 = get_u32 ~ctor:(fun k -> Crcs k) end
module KOffsets = struct
let get_u32_and_msb k src t =
get_u32
~ctor:(fun k -> Offsets k)
(fun u32 src t ->
k (u32, Int32.equal 0l (Int32.logand u32 0x80000000l)) src t )
src t
let get_u64 = get_u64 ~ctor:(fun k -> Offsets k)
end
module KHash = struct
let rec get_byte k src t =
if t.i_len - t.i_pos > 0 then
let byte = Cstruct.get_uint8 src (t.i_off + t.i_pos) in
k byte src {t with i_pos= t.i_pos + 1}
else
Wait {t with state= Hash (fun src t -> (get_byte [@tailcall]) k src t)}
let get_hash k src t =
let res = Bytes.create Hash.digest_size in
let rec loop i src t =
if i = Hash.digest_size then k (Bytes.unsafe_to_string res) src t
else
get_byte
(fun byte src t ->
Bytes.set res i (Char.unsafe_chr byte) ;
(loop [@tailcall]) (i + 1) src t )
src t
in
loop 0 src t
end
let rest ?boffsets (hash_idx, hash_pack) _ t =
match Queue.pop t.hashes, Queue.pop t.crcs, Queue.pop t.offsets with
| hash, crc, (offset, true) ->
Result
( {t with state= Ret (boffsets, hash_idx, hash_pack)}
, (hash, crc, Int64.of_int32 offset) )
| exception Queue.Empty -> ok t hash_pack
| hash, crc, (offset, false) -> (
match boffsets with
| None -> error t Expected_bigoffset_table
| Some arr ->
let idx = Int32.to_int (Int32.logand offset 0x7FFFFFFFl) in
if idx >= 0 && idx < Array.length arr then
Result
( {t with state= Ret (boffsets, hash_idx, hash_pack)}
, (hash, crc, arr.(idx)) )
else error t (Invalid_index_of_bigoffset idx) )
let hash ?boffsets src t =
let aux k src t =
let hash = feed_cstruct t.hash (Cstruct.sub src t.i_off t.i_pos) in
KHash.get_hash k src {t with hash}
in
( KHash.get_hash
@@ fun hash_pack ->
aux
@@ fun hash_idx src t ->
let produce = Hash.get t.hash in
let hash_idx = Hash.of_raw_string hash_idx in
let hash_pack = Hash.of_raw_string hash_pack in
if hash_idx <> produce then
error t (Invalid_hash (Hash.get t.hash, hash_idx))
else rest ?boffsets (hash_idx, hash_pack) src t )
src t
let rec boffsets arr idx max src t =
if idx >= max then hash ~boffsets:arr src t
else
KOffsets.get_u64
(fun offset src t ->
arr.(idx) <- offset ;
(boffsets [@tailcall]) arr (succ idx) max src t )
src t
let rec offsets idx boffs max src t =
if Int32.compare idx max >= 0 then
if boffs > 0 then boffsets (Array.make boffs 0L) 0 boffs src t
else hash src t
else
KOffsets.get_u32_and_msb
(fun (offset, msb) src t ->
Queue.add (offset, msb) t.offsets ;
(offsets [@tailcall]) (Int32.succ idx)
(if not msb then succ boffs else boffs)
max src t )
src t
let rec crcs idx max src t =
if Int32.compare idx max >= 0 then offsets 0l 0 max src t
else
KCrcs.get_u32
(fun crc src t ->
Queue.add (Crc32.of_int32 crc) t.crcs ;
(crcs [@tailcall]) (Int32.succ idx) max src t )
src t
let rec hashes idx max src t =
if Int32.compare idx max >= 0 then Cont {t with state= Crcs (crcs 0l max)}
else
KHashes.get_hash
(fun hash src t ->
let hash = Hash.of_raw_string hash in
Queue.add hash t.hashes ;
(hashes [@tailcall]) (Int32.succ idx) max src t )
src t
let rec fanout idx src t =
match idx with
| 256 ->
Cont
{t with state= Hashes (hashes 0l (Array.fold_left max 0l t.fanout))}
| _ ->
KFanoutTable.get_u32
(fun entry src t ->
t.fanout.(idx) <- entry ;
(fanout [@tailcall]) (idx + 1) src t )
src t
let src t =
( KHeader.check_byte '\255'
@@ KHeader.check_byte '\116'
@@ KHeader.check_byte '\079'
@@ KHeader.check_byte '\099'
@@ KHeader.get_u32
@@ fun version _ t ->
if version = 2l then Cont {t with state= Fanout (fanout 0)}
else error t (Invalid_version version) )
src t
let make () =
{ i_off= 0
; i_pos= 0
; i_len= 0
; fanout= Array.make 256 0l
; hashes= Queue.create ()
; crcs= Queue.create ()
; offsets= Queue.create ()
; hash= Hash.init ()
; state= Header header }
let refill off len t =
if t.i_len - t.i_pos = 0 then {t with i_off= off; i_len= len; i_pos= 0}
else
raise
(Invalid_argument
(Fmt.strf "I.refill: you lost something (pos: %d, len: %d)" t.i_pos
t.i_len))
let eval src t =
let eval0 t =
match t.state with
| Header k -> k src t
| Fanout k -> k src t
| Hashes k -> k src t
| Crcs k -> k src t
| Offsets k -> k src t
| Ret (boffsets, hash_idx, hash_pack) ->
rest ?boffsets (hash_idx, hash_pack) src t
| Hash k -> k src t
| End hash -> ok t hash
| Exception exn -> error t exn
in
let rec loop t =
match eval0 t with
| Cont t -> loop t
| Wait t -> `Await t
| Ok (t, hash) -> `End (t, hash)
| Result (t, hash) -> `Hash (t, hash)
| Error (t, exn) -> `Error (t, exn)
in
loop t
end
module type ENCODER = sig
module Hash : S.HASH
type error
val pp_error : error Fmt.t
type t
val pp : t Fmt.t
type 'a sequence = ('a -> unit) -> unit
val default : (Hash.t * (Checkseum.Crc32.t * int64)) sequence -> Hash.t -> t
val flush : int -> int -> t -> t
val used_out : t -> int
val eval : Cstruct.t -> t -> [`Flush of t | `End of t | `Error of t * error]
end
module Encoder (Hash : S.HASH) = struct
type error
let pp_error = Fmt.nop
module K = struct
type t = Hash.t
let get x i = (Hash.to_raw_string x).[i]
let compare = Hash.unsafe_compare
end
module Fanout = Fanout.Make (K)
type t =
{ o_off: int
; o_pos: int
; o_len: int
; write: int
; table: (Checkseum.Crc32.t * int64) Fanout.t
; boffsets: int64 array
; hash: Hash.ctx
; pack: Hash.t
; state: state }
and k = Cstruct.t -> t -> res
and state =
| Fanout of k
| Hashes of k
| Crcs of k
| Offsets of k
| BigOffsets of k
| Hash of k
| End
and res = Flush of t | Cont of t | Ok of t
let pp_state ppf = function
| Header _ -> Fmt.pf ppf "(Header #k)"
| Fanout _ -> Fmt.pf ppf "(Fanout #k)"
| Hashes _ -> Fmt.pf ppf "(Hashes #k)"
| Crcs _ -> Fmt.pf ppf "(Crcs #k)"
| Offsets _ -> Fmt.pf ppf "(Offsets #k)"
| BigOffsets _ -> Fmt.pf ppf "(BigOffsets #k)"
| Hash _ -> Fmt.pf ppf "(Hash #k)"
| End -> Fmt.pf ppf "End"
let pp ppf {o_off; o_pos; o_len; write; pack; state; _} =
Fmt.pf ppf
"{ @[<hov>o_off = %d;@ o_pos = %d;@ o_len = %d;@ write = %d;@ table = \
#table;@ boffsets = #table;@ hash = #ctx;@ pack = %a;@ state = %a;@] }"
o_off o_pos o_len write Hash.pp pack (Fmt.hvbox pp_state) state
let feed_cstruct h c = Hash.feed_bigstring h (Cstruct.to_bigarray c)
let flush dst t =
let hash = feed_cstruct t.hash (Cstruct.sub dst t.o_off t.o_pos) in
Flush {t with hash}
module Int32 = struct
include Int32
let ( >> ) = Int32.shift_right
let ( && ) = Int32.logand
let ( ! ) = Int32.to_int
end
let rec put_byte ~ctor chr k dst t =
if t.o_len - t.o_pos > 0 then (
Cstruct.set_char dst (t.o_off + t.o_pos) chr ;
k dst {t with o_pos= t.o_pos + 1; write= t.write + 1} )
else
flush dst
{ t with
state= ctor (fun dst t -> (put_byte [@tailcall]) ~ctor chr k dst t)
}
let rec put_int32 ~ctor integer k dst t =
if t.o_len - t.o_pos >= 4 then (
Cstruct.BE.set_uint32 dst (t.o_off + t.o_pos) integer ;
k dst {t with o_pos= t.o_pos + 4; write= t.write + 4} )
else if t.o_len - t.o_pos > 0 then
let i1 = Char.unsafe_chr @@ Int32.(!((integer && 0xFF000000l) >> 24)) in
let i2 = Char.unsafe_chr @@ Int32.(!((integer && 0x00FF0000l) >> 16)) in
let i3 = Char.unsafe_chr @@ Int32.(!((integer && 0x0000FF00l) >> 8)) in
let i4 = Char.unsafe_chr @@ Int32.(!(integer && 0x000000FFl)) in
( put_byte ~ctor i1
@@ put_byte ~ctor i2
@@ put_byte ~ctor i3
@@ put_byte ~ctor i4 k )
dst t
else
flush dst
{ t with
state=
ctor (fun dst t -> (put_int32 [@tailcall]) ~ctor integer k dst t)
}
module Int64 = struct
include Int64
let ( >> ) = Int64.shift_right_logical
let ( && ) = Int64.logand
let ( ! ) = Int64.to_int
end
module KFanout = struct let put_int32 = put_int32 ~ctor:(fun k -> Fanout k)
end
module KHashes = struct
let put_hash hash k dst t =
if t.o_len - t.o_pos >= Hash.digest_size then (
Cstruct.blit hash 0 dst (t.o_off + t.o_pos) Hash.digest_size ;
k dst
{ t with
o_pos= t.o_pos + Hash.digest_size
; write= t.write + Hash.digest_size } )
else
let rec loop rest dst t =
if rest = 0 then k dst t
else
let n = min rest (t.o_len - t.o_pos) in
if n = 0 then flush dst {t with state= Hashes (loop rest)}
else (
Cstruct.blit hash (Hash.digest_size - rest) dst
(t.o_off + t.o_pos) n ;
flush dst
{ t with
state= Hashes (loop (rest - n))
; o_pos= t.o_pos + n
; write= t.write + n } )
in
loop Hash.digest_size dst t
end
module KCrcs = struct let put_int32 = put_int32 ~ctor:(fun k -> Crcs k) end
module KOffsets = struct let put_int32 = put_int32 ~ctor:(fun k -> Offsets k)
end
module KBOffsets = struct
let put_byte = put_byte ~ctor:(fun k -> BigOffsets k)
let rec put_int64 integer k dst t =
if t.o_len - t.o_pos >= 8 then (
Cstruct.BE.set_uint64 dst (t.o_off + t.o_pos) integer ;
k dst {t with o_pos= t.o_pos + 8; write= t.write + 8} )
else if t.o_len - t.o_pos > 0 then
let i1 =
Char.unsafe_chr @@ Int64.(!((integer && 0xFF00000000000000L) >> 56))
in
let i2 =
Char.unsafe_chr @@ Int64.(!((integer && 0x00FF000000000000L) >> 48))
in
let i3 =
Char.unsafe_chr @@ Int64.(!((integer && 0x0000FF0000000000L) >> 40))
in
let i4 =
Char.unsafe_chr @@ Int64.(!((integer && 0x000000FF00000000L) >> 32))
in
let i5 =
Char.unsafe_chr @@ Int64.(!((integer && 0x00000000FF000000L) >> 24))
in
let i6 =
Char.unsafe_chr @@ Int64.(!((integer && 0x0000000000FF0000L) >> 16))
in
let i7 =
Char.unsafe_chr @@ Int64.(!((integer && 0x000000000000FF00L) >> 8))
in
let i8 =
Char.unsafe_chr @@ Int64.(!(integer && 0x00000000000000FFL))
in
( put_byte i1
@@ put_byte i2
@@ put_byte i3
@@ put_byte i4
@@ put_byte i5
@@ put_byte i6
@@ put_byte i7
@@ put_byte i8 k )
dst t
else flush dst {t with state= BigOffsets (put_int64 integer k)}
end
module KHash = struct
let put_hash ?(digest = true) hash k dst t =
if t.o_len - t.o_pos >= Hash.digest_size then (
Cstruct.blit_from_string hash 0 dst (t.o_off + t.o_pos)
Hash.digest_size ;
k dst
{ t with
o_pos= t.o_pos + Hash.digest_size
; write= t.write + Hash.digest_size } )
else
let rec loop rest dst t =
if rest = 0 then k dst t
else
let n = min rest (t.o_len - t.o_pos) in
if n = 0 then
let t = {t with state= Hash (loop rest)} in
if digest then flush dst t else Flush t
else (
Cstruct.blit_from_string hash (Hash.digest_size - rest) dst
(t.o_off + t.o_pos) n ;
let t =
{ t with
state= Hash (loop (rest - n))
; o_pos= t.o_pos + n
; write= t.write + n }
in
if digest then flush dst t else Flush t )
in
loop Hash.digest_size dst t
end
let ok t : res = Ok {t with state= End}
let is_big_offset integer = Int64.(integer >> 31) <> 0L
let hash dst t =
( KHash.put_hash (Hash.to_raw_string t.pack)
@@ fun dst t ->
let ctx = feed_cstruct t.hash (Cstruct.sub dst t.o_off t.o_pos) in
let hash = Hash.get ctx in
KHash.put_hash ~digest:false (Hash.to_raw_string hash)
(fun _ t -> ok t)
dst {t with hash= ctx} )
dst t
let rec boffsets idx dst t =
if idx = Array.length t.boffsets then Cont {t with state= Hash hash}
else KBOffsets.put_int64 t.boffsets.(idx) (boffsets (idx + 1)) dst t
let rec offsets idx idx_boffs dst t =
if idx = 256 then Cont {t with state= BigOffsets (boffsets 0)}
else
let rec aux acc idx_boffs dst t =
match acc with
| [] -> Cont {t with state= Offsets (offsets (idx + 1) idx_boffs)}
| (_, (_, off)) :: r ->
if is_big_offset off then
let integer = Int32.(0x40000000l && Int32.of_int idx_boffs) in
KOffsets.put_int32 integer (aux r (idx_boffs + 1)) dst t
else
KOffsets.put_int32 (Int64.to_int32 off) (aux r idx_boffs) dst t
in
aux (Fanout.get idx t.table) idx_boffs dst t
let rec crcs idx dst t =
if idx = 256 then Cont {t with state= Offsets (offsets 0 0)}
else
let rec aux acc dst t =
match acc with
| [] -> Cont {t with state= Hashes (crcs (idx + 1))}
| (_, (crc, _)) :: r ->
KCrcs.put_int32 (Crc32.to_int32 crc) (aux r) dst t
in
aux (Fanout.get idx t.table) dst t
let rec hashes idx dst t =
if idx = 256 then Cont {t with state= Crcs (crcs 0)}
else
let rec aux acc dst t =
match acc with
| [] -> Cont {t with state= Hashes (hashes (idx + 1))}
| (hash, _) :: r -> KHashes.put_hash hash (aux r) dst t
in
let lst =
Fanout.get idx t.table
|> List.map (fun (hash, crc32) ->
Cstruct.of_string (Hash.to_raw_string hash), crc32 )
in
aux lst dst t
let rec fanout idx acc dst t =
match idx with
| 256 -> Cont {t with state= Hashes (hashes 0)}
| n ->
let value = Int32.of_int (Fanout.length n t.table) in
KFanout.put_int32 (Int32.add value acc)
(fanout (idx + 1) (Int32.add value acc))
dst t
let dst t =
( KHeader.put_byte '\255'
@@ KHeader.put_byte '\116'
@@ KHeader.put_byte '\079'
@@ KHeader.put_byte '\099'
@@ KHeader.put_int32 2l
@@ fun _ t -> Cont {t with state= Fanout (fanout 0 0l)} )
dst t
let flush off len t = {t with o_off= off; o_len= len; o_pos= 0}
let used_out t = t.o_pos
let eval dst t =
let eval0 t =
match t.state with
| Header k -> k dst t
| Fanout k -> k dst t
| Hashes k -> k dst t
| Crcs k -> k dst t
| Offsets k -> k dst t
| BigOffsets k -> k dst t
| Hash k -> k dst t
| End -> ok t
in
let rec loop t =
match eval0 t with
| Cont t -> loop t
| Flush t -> `Flush t
| Ok t -> `End t
in
loop t
type 'a sequence = ('a -> unit) -> unit
let default : (Hash.t * (Checkseum.Crc32.t * int64)) sequence -> Hash.t -> t
=
fun seq hash ->
let boffsets = ref [] in
let table = Fanout.make () in
let f (hash, (crc, offset)) =
if is_big_offset offset then (
let idx = Int64.of_int (List.length !boffsets) in
boffsets := offset :: !boffsets ;
Fanout.bind hash (crc, idx) table )
else Fanout.bind hash (crc, offset) table
in
let () = seq f in
{ o_off= 0
; o_pos= 0
; o_len= 0
; write= 0
; table
; boffsets= Array.of_list (List.rev !boffsets)
; hash= Hash.init ()
; pack= hash
; state= Header header }
end