Skip to content

Commit

Permalink
Merge pull request #26 from talex5/safe-string
Browse files Browse the repository at this point in the history
Add support for OCaml 4.06
  • Loading branch information
djs55 authored Nov 14, 2017
2 parents 3afdc07 + 9ea5469 commit 980ec32
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
7 changes: 5 additions & 2 deletions sha1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*)

type ctx
type buf = (int, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
type buf = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
type t

external init: unit -> ctx = "stub_sha1_init"
Expand Down Expand Up @@ -58,7 +58,7 @@ let buffer buf =

let channel chan len =
let ctx = init ()
and buf = String.create blksize in
and buf = Bytes.create blksize in

let left = ref len and eof = ref false in
while (!left == -1 || !left > 0) && not !eof
Expand All @@ -68,7 +68,10 @@ let channel chan len =
if readed = 0 then
eof := true
else (
let buf = Bytes.unsafe_to_string buf in
unsafe_update_substring ctx buf 0 readed;
(* [unsafe_update_substring] does not hold on to [buf],
so we can mutate it again now *)
if !left <> -1 then left := !left - readed
)
done;
Expand Down
7 changes: 5 additions & 2 deletions sha256.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ let buffer buf =

let channel chan len =
let ctx = init ()
and buf = String.create blksize in
and buf = Bytes.create blksize in

let left = ref len and eof = ref false in
while (!left == -1 || !left > 0) && not !eof
Expand All @@ -69,7 +69,10 @@ let channel chan len =
if readed = 0 then
eof := true
else (
unsafe_update_substring ctx buf 0 readed;
let buf = Bytes.unsafe_to_string buf in
unsafe_update_substring ctx buf 0 readed;
(* [unsafe_update_substring] does not hold on to [buf],
so we can mutate it again now *)
if !left <> -1 then left := !left - readed
)
done;
Expand Down
7 changes: 5 additions & 2 deletions sha512.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ let buffer buf =

let channel chan len =
let ctx = init ()
and buf = String.create blksize in
and buf = Bytes.create blksize in

let left = ref len and eof = ref false in
while (!left == -1 || !left > 0) && not !eof
Expand All @@ -67,7 +67,10 @@ let channel chan len =
if readed = 0 then
eof := true
else (
unsafe_update_substring ctx buf 0 readed;
let buf = Bytes.unsafe_to_string buf in
unsafe_update_substring ctx buf 0 readed;
(* [unsafe_update_substring] does not hold on to [buf],
so we can mutate it again now *)
if !left <> -1 then left := !left - readed
)
done;
Expand Down

0 comments on commit 980ec32

Please sign in to comment.