Source file checkseum.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
type ba =
  (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t

type st = Bytes.t
type off = int
type len = int
type optint = Optint.t

module type FOREIGN = sig
  type t

  val unsafe_bytes : t -> st -> off -> len -> t
  val unsafe_bigstring : t -> ba -> off -> len -> t
end

module type DESC = sig
  val default : optint
end

module Adler32_foreign_64 = struct
  type t = int

  external unsafe_bytes :
    (t[@untagged]) ->
    st ->
    (off[@untagged]) ->
    (len[@untagged]) ->
    (t[@untagged])
    = "caml_checkseum_adler32_st" "caml_checkseum_adler32_st_untagged"
    [@@noalloc]

  external unsafe_bigstring :
    (t[@untagged]) ->
    ba ->
    (off[@untagged]) ->
    (len[@untagged]) ->
    (t[@untagged])
    = "caml_checkseum_adler32_ba" "caml_checkseum_adler32_ba_untagged"
    [@@noalloc]
end

module Adler32_foreign_32 = struct
  type t = int32

  external unsafe_bytes :
    (t[@unboxed]) -> st -> (off[@untagged]) -> (len[@untagged]) -> (t[@unboxed])
    = "caml_checkseum_adler32_st" "caml_checkseum_adler32_st_untagged"
    [@@noalloc]

  external unsafe_bigstring :
    (t[@unboxed]) -> ba -> (off[@untagged]) -> (len[@untagged]) -> (t[@unboxed])
    = "caml_checkseum_adler32_ba" "caml_checkseum_adler32_ba_untagged"
    [@@noalloc]
end

module Adler32_foreign : FOREIGN with type t = optint = struct
  let impl : (module FOREIGN with type t = Optint.t) =
    match Optint.is_immediate with
    | Optint.Conditional.True ->
        (module Adler32_foreign_64 : FOREIGN with type t = Optint.t)
    | Optint.Conditional.False ->
        (module Adler32_foreign_32 : FOREIGN with type t = Optint.t)

  include (val impl : FOREIGN with type t = Optint.t)
end

module Crc32_foreign_64 = struct
  type t = int

  external unsafe_bytes :
    (t[@untagged]) ->
    st ->
    (off[@untagged]) ->
    (len[@untagged]) ->
    (t[@untagged])
    = "caml_checkseum_crc32_st" "caml_checkseum_crc32_st_untagged"
    [@@noalloc]

  external unsafe_bigstring :
    (t[@untagged]) ->
    ba ->
    (off[@untagged]) ->
    (len[@untagged]) ->
    (t[@untagged])
    = "caml_checkseum_crc32_ba" "caml_checkseum_crc32_ba_untagged"
    [@@noalloc]
end

module Crc32_foreign_32 = struct
  type t = int32

  external unsafe_bytes :
    (t[@unboxed]) -> st -> (off[@untagged]) -> (len[@untagged]) -> (t[@unboxed])
    = "caml_checkseum_crc32_st" "caml_checkseum_crc32_st_untagged"
    [@@noalloc]

  external unsafe_bigstring :
    (t[@unboxed]) -> ba -> (off[@untagged]) -> (len[@untagged]) -> (t[@unboxed])
    = "caml_checkseum_crc32_ba" "caml_checkseum_crc32_ba_untagged"
    [@@noalloc]
end

module Crc32_foreign : FOREIGN with type t = optint = struct
  let impl : (module FOREIGN with type t = Optint.t) =
    match Optint.is_immediate with
    | Optint.Conditional.True ->
        (module Crc32_foreign_64 : FOREIGN with type t = Optint.t)
    | Optint.Conditional.False ->
        (module Crc32_foreign_32 : FOREIGN with type t = Optint.t)

  include (val impl : FOREIGN with type t = Optint.t)
end

module Crc32c_foreign_64 = struct
  type t = int

  external unsafe_bytes :
    (t[@untagged]) ->
    st ->
    (off[@untagged]) ->
    (len[@untagged]) ->
    (t[@untagged])
    = "caml_checkseum_crc32c_st" "caml_checkseum_crc32c_st_untagged"
    [@@noalloc]

  external unsafe_bigstring :
    (t[@untagged]) ->
    ba ->
    (off[@untagged]) ->
    (len[@untagged]) ->
    (t[@untagged])
    = "caml_checkseum_crc32c_ba" "caml_checkseum_crc32c_ba_untagged"
    [@@noalloc]
end

module Crc32c_foreign_32 = struct
  type t = int32

  external unsafe_bytes :
    (t[@unboxed]) -> st -> (off[@untagged]) -> (len[@untagged]) -> (t[@unboxed])
    = "caml_checkseum_crc32c_st" "caml_checkseum_crc32c_st_untagged"
    [@@noalloc]

  external unsafe_bigstring :
    (t[@unboxed]) -> ba -> (off[@untagged]) -> (len[@untagged]) -> (t[@unboxed])
    = "caml_checkseum_crc32c_ba" "caml_checkseum_crc32c_ba_untagged"
    [@@noalloc]
end

module Crc32c_foreign : FOREIGN with type t = optint = struct
  let impl : (module FOREIGN with type t = Optint.t) =
    match Optint.is_immediate with
    | Optint.Conditional.True ->
        (module Crc32c_foreign_64 : FOREIGN with type t = Optint.t)
    | Optint.Conditional.False ->
        (module Crc32c_foreign_32 : FOREIGN with type t = Optint.t)

  include (val impl : FOREIGN with type t = Optint.t)
end

module Crc24_foreign_64 = struct
  type t = int

  external unsafe_bytes :
    (t[@untagged]) ->
    st ->
    (off[@untagged]) ->
    (len[@untagged]) ->
    (t[@untagged])
    = "caml_checkseum_crc24_st" "caml_checkseum_crc24_st_untagged"
    [@@noalloc]

  external unsafe_bigstring :
    (t[@untagged]) ->
    ba ->
    (off[@untagged]) ->
    (len[@untagged]) ->
    (t[@untagged])
    = "caml_checkseum_crc24_ba" "caml_checkseum_crc24_ba_untagged"
    [@@noalloc]
end

module Crc24_foreign_32 = struct
  type t = int32

  external unsafe_bytes :
    (t[@unboxed]) -> st -> (off[@untagged]) -> (len[@untagged]) -> (t[@unboxed])
    = "caml_checkseum_crc24_st" "caml_checkseum_crc24_st_untagged"
    [@@noalloc]

  external unsafe_bigstring :
    (t[@unboxed]) -> ba -> (off[@untagged]) -> (len[@untagged]) -> (t[@unboxed])
    = "caml_checkseum_crc24_ba" "caml_checkseum_crc24_ba_untagged"
    [@@noalloc]
end

module Crc24_foreign : FOREIGN with type t = optint = struct
  let impl : (module FOREIGN with type t = Optint.t) =
    match Optint.is_immediate with
    | Optint.Conditional.True ->
        (module Crc24_foreign_64 : FOREIGN with type t = Optint.t)
    | Optint.Conditional.False ->
        (module Crc24_foreign_32 : FOREIGN with type t = Optint.t)

  include (val impl : FOREIGN with type t = Optint.t)
end

module Make (F : FOREIGN) (D : DESC) = struct
  type t = optint

  let pp ppf v = Optint.pp ppf v
  let equal a b = Optint.equal a b
  let default = D.default
  let unsafe_digest_bytes a o l v = F.unsafe_bytes v a o l
  let to_int32 = Optint.to_unsigned_int32
  let of_int32 = Optint.of_unsigned_int32

  let unsafe_digest_string a o l v =
    F.unsafe_bytes v (Bytes.unsafe_of_string a) o l

  let unsafe_digest_bigstring a o l v = F.unsafe_bigstring v a o l

  let digest_bytes a o l v =
    if o < 0 || l < 0 || o > Bytes.length a - l
    then invalid_arg "index out of bounds" ;
    unsafe_digest_bytes a o l v

  let digest_string a o l v =
    if o < 0 || l < 0 || o > String.length a - l
    then invalid_arg "index out of bounds" ;
    unsafe_digest_string a o l v

  let digest_bigstring a o l v =
    if o < 0 || l < 0 || o > Bigarray.Array1.dim a - l
    then invalid_arg "index out of bounds" ;
    unsafe_digest_bigstring a o l v
end

type bigstring = ba

module type S = sig
  type t = optint

  val pp : Format.formatter -> t -> unit
  val equal : t -> t -> bool
  val default : t
  val digest_bytes : Bytes.t -> int -> int -> t -> t
  val unsafe_digest_bytes : Bytes.t -> int -> int -> t -> t
  val digest_string : String.t -> int -> int -> t -> t
  val unsafe_digest_string : String.t -> int -> int -> t -> t
  val digest_bigstring : bigstring -> int -> int -> t -> t
  val unsafe_digest_bigstring : bigstring -> int -> int -> t -> t
  val to_int32 : t -> int32
  val of_int32 : int32 -> t
end

module Adler32 : S =
  Make
    (Adler32_foreign)
    (struct
      let default = Optint.one
    end)

module Crc32 : S =
  Make
    (Crc32_foreign)
    (struct
      let default = Optint.zero
    end)

module Crc32c : S =
  Make
    (Crc32c_foreign)
    (struct
      let default = Optint.zero
    end)

module Crc24 : S =
  Make
    (Crc24_foreign)
    (struct
      let default = Optint.of_int 0xb704ce
    end)