Skip to content

Commit 76a7f8f

Browse files
authored
Merge pull request #74 from mirage/add-casts-int32
Add {to,of}_int32 with a documentation about the semantic of value
2 parents 1b1545c + d771bc0 commit 76a7f8f

8 files changed

Lines changed: 42 additions & 46 deletions

File tree

src-c/checkseum.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ module Make (F : FOREIGN) (D : DESC) = struct
212212
let equal a b = Optint.equal a b
213213
let default = D.default
214214
let unsafe_digest_bytes a o l v = F.unsafe_bytes v a o l
215+
let to_int32 = Optint.to_unsigned_int32
216+
let of_int32 = Optint.of_unsigned_int32
215217

216218
let unsafe_digest_string a o l v =
217219
F.unsafe_bytes v (Bytes.unsafe_of_string a) o l
@@ -248,6 +250,8 @@ module type S = sig
248250
val unsafe_digest_string : String.t -> int -> int -> t -> t
249251
val digest_bigstring : bigstring -> int -> int -> t -> t
250252
val unsafe_digest_bigstring : bigstring -> int -> int -> t -> t
253+
val to_int32 : t -> int32
254+
val of_int32 : int32 -> t
251255
end
252256

253257
module Adler32 : S =

src-ocaml/checkseum.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module type S = sig
1313
val unsafe_digest_string : String.t -> int -> int -> t -> t
1414
val digest_bigstring : bigstring -> int -> int -> t -> t
1515
val unsafe_digest_bigstring : bigstring -> int -> int -> t -> t
16+
val to_int32 : t -> int32
17+
val of_int32 : int32 -> t
1618
end
1719

1820
module Adler32 : S = Gin_adler32

src-ocaml/gin_adler32.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,5 @@ let unsafe_digest_bigstring a o l v =
133133
digest ~get:Bigarray.Array1.unsafe_get a o l v
134134

135135
let digest_bigstring a o l v = digest ~get:Bigarray.Array1.get a o l v
136+
let to_int32 = Optint.to_unsigned_int32
137+
let of_int32 = Optint.of_unsigned_int32

src-ocaml/gin_crc24.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ let digest_string a o l v = crc24 ~get:String.get a o l v
3535
let unsafe_digest_string a o l v = crc24 ~get:String.unsafe_get a o l v
3636
let digest_bytes a o l v = crc24 ~get:Bytes.get a o l v
3737
let unsafe_digest_bytes a o l v = crc24 ~get:Bytes.unsafe_get a o l v
38+
let to_int32 = Optint.to_unsigned_int32
39+
let of_int32 = Optint.of_unsigned_int32

src-ocaml/gin_crc32.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,5 @@ let digest_string a o l v = crc32 ~get:String.get a o l v
303303
let unsafe_digest_string a o l v = crc32 ~get:String.unsafe_get a o l v
304304
let digest_bytes a o l v = crc32 ~get:Bytes.get a o l v
305305
let unsafe_digest_bytes a o l v = crc32 ~get:Bytes.unsafe_get a o l v
306+
let to_int32 = Optint.to_unsigned_int32
307+
let of_int32 = Optint.of_unsigned_int32

src-ocaml/gin_crc32c.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,5 @@ let digest_string a o l v = crc32c ~get:String.get a o l v
303303
let unsafe_digest_string a o l v = crc32c ~get:String.unsafe_get a o l v
304304
let digest_bytes a o l v = crc32c ~get:Bytes.get a o l v
305305
let unsafe_digest_bytes a o l v = crc32c ~get:Bytes.unsafe_get a o l v
306+
let to_int32 = Optint.to_unsigned_int32
307+
let of_int32 = Optint.of_unsigned_int32

src/checkseum.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ module type S = sig
3535
val unsafe_digest_bigstring : bigstring -> int -> int -> t -> t
3636
(** [unsafe_digest_bigstring msg off len t] is the same as {!digest_bigstring}
3737
without bound-checking. *)
38+
39+
val to_int32 : t -> int32
40+
(** [to_int32 crc] casts [crc] to an {b unsigned} [int32] value. We precise
41+
{i unsigned} because every bits are significant for the checksum (even the
42+
sign bit). We interpret the given [int32] as a value where the sign bit
43+
does not have any signification (hence the {i unsigned}). *)
44+
45+
val of_int32 : int32 -> t
46+
(** [of_int32 crc] casts the given unsigned [int32] value to {!t}. For a
47+
checksum, every bits (including the sign bit) has a signification. The
48+
user must interpret the given [int32] as an unsigned [int32] (eg.
49+
regardless the sign bit which has another signification for the checksum). *)
3850
end
3951

4052
module Adler32 : S

test/test.ml

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,42 @@ let make ~name (module M : Checkseum.S) input expected =
77
fun () ->
88
Alcotest.check checkseum name
99
M.(digest_string input 0 (String.length input) default)
10-
expected )
10+
M.(of_int32 expected) )
1111

1212
let () =
1313
Alcotest.run "checkseum"
1414
[
1515
( "crc32c",
1616
[
17-
make ~name:"0" (module Checkseum.Crc32c) "" Optint.zero;
18-
make ~name:"1"
19-
(module Checkseum.Crc32c)
20-
"\x00"
21-
(Optint.of_unsigned_int32 0x527d5351l);
17+
make ~name:"0" (module Checkseum.Crc32c) "" 0l;
18+
make ~name:"1" (module Checkseum.Crc32c) "\x00" 0x527d5351l;
2219
make ~name:"2"
2320
(module Checkseum.Crc32c)
24-
"\xff\xff\xff\xff"
25-
(Optint.of_unsigned_int32 0xffffffffl);
26-
make ~name:"3"
27-
(module Checkseum.Crc32c)
28-
"123456789"
29-
(Optint.of_unsigned_int32 0xe3069283l);
21+
"\xff\xff\xff\xff" 0xffffffffl;
22+
make ~name:"3" (module Checkseum.Crc32c) "123456789" 0xe3069283l;
3023
make ~name:"4"
3124
(module Checkseum.Crc32c)
32-
"Thou hast made me, and shall thy work decay?"
33-
(Optint.of_unsigned_int32 0x866374c0l);
25+
"Thou hast made me, and shall thy work decay?" 0x866374c0l;
3426
] );
3527
( "crc32",
3628
[
37-
make ~name:"0" (module Checkseum.Crc32) "" Optint.zero;
38-
make ~name:"1"
39-
(module Checkseum.Crc32)
40-
"\x00"
41-
(Optint.of_unsigned_int32 0xd202ef8dl);
42-
make ~name:"2"
43-
(module Checkseum.Crc32)
44-
"\xff\xff\xff\xff"
45-
(Optint.of_unsigned_int32 0xffffffffl);
46-
make ~name:"3"
47-
(module Checkseum.Crc32)
48-
"123456789"
49-
(Optint.of_unsigned_int32 0xcbf43926l);
29+
make ~name:"0" (module Checkseum.Crc32) "" 0l;
30+
make ~name:"1" (module Checkseum.Crc32) "\x00" 0xd202ef8dl;
31+
make ~name:"2" (module Checkseum.Crc32) "\xff\xff\xff\xff" 0xffffffffl;
32+
make ~name:"3" (module Checkseum.Crc32) "123456789" 0xcbf43926l;
5033
make ~name:"4"
5134
(module Checkseum.Crc32)
52-
"Thou hast made me, and shall thy work decay?"
53-
(Optint.of_unsigned_int32 0xf1fabe1dl);
35+
"Thou hast made me, and shall thy work decay?" 0xf1fabe1dl;
5436
make ~name:"5"
5537
(module Checkseum.Crc32)
5638
(String.concat "%" (List.init 1000 (const "abcdef")))
57-
(Optint.of_unsigned_int32 0xadc436fl);
39+
0xadc436fl;
5840
] );
5941
( "crc24",
6042
[
61-
make ~name:"0"
62-
(module Checkseum.Crc24)
63-
""
64-
(Optint.of_unsigned_int32 0xb704cel);
65-
make ~name:"1"
66-
(module Checkseum.Crc24)
67-
"a"
68-
(Optint.of_unsigned_int32 0xf25713l);
69-
make ~name:"2"
70-
(module Checkseum.Crc24)
71-
"abc"
72-
(Optint.of_unsigned_int32 0xba1c7bl);
73-
make ~name:"3"
74-
(module Checkseum.Crc24)
75-
"message digest"
76-
(Optint.of_unsigned_int32 0xdbf0b6l);
43+
make ~name:"0" (module Checkseum.Crc24) "" 0xb704cel;
44+
make ~name:"1" (module Checkseum.Crc24) "a" 0xf25713l;
45+
make ~name:"2" (module Checkseum.Crc24) "abc" 0xba1c7bl;
46+
make ~name:"3" (module Checkseum.Crc24) "message digest" 0xdbf0b6l;
7747
] );
7848
]

0 commit comments

Comments
 (0)