Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrek committed Jun 10, 2024
1 parent 50e5b46 commit ff6018b
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 126 deletions.
6 changes: 3 additions & 3 deletions src/utils/cdk/file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
let to_string name =
Unix2.tryopen_read_bin name (fun chan ->
let buf_size = 1024 in
let buf = String.create buf_size in
let buf = Bytes.create buf_size in
let rec iter buf nb_read =
let buf_size = String.length buf in
let buf_size = Bytes.length buf in
let to_read = min (buf_size - nb_read) 8192 in
let tmp = input chan buf nb_read to_read in
if tmp = 0 then
String.sub buf 0 nb_read
Bytes.sub_string buf 0 nb_read
else
let nb_read = nb_read + tmp in
let buf =
Expand Down
8 changes: 4 additions & 4 deletions src/utils/cdk/string2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ let of_char c = String.make 1 c


let resize s newlen =
let len = String.length s in
if len > newlen then String.sub s 0 newlen
let len = Bytes.length s in
if len > newlen then Bytes.sub s 0 newlen
else
let b = Bytes.create newlen in
String.blit s 0 b 0 len;
Bytes.unsafe_to_string b
Bytes.blit s 0 b 0 len;
b

let is_space c = c = ' ' || c = '\n' || c = '\r' || c = '\t'

Expand Down
2 changes: 1 addition & 1 deletion src/utils/cdk/string2.mli
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ val subcontains : string -> string -> bool
val of_char : char -> string
(*d [of_char c] returns the string containing one [c]. *)

val resize : string -> int -> string
val resize : bytes -> int -> bytes
(*d [resize s len] returns a string of length [len] starting with [s]. *)

val init : int -> (int -> char) -> string
Expand Down
34 changes: 17 additions & 17 deletions src/utils/cdk/zlib2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ let zlib_version_num () =
end

let grow_buffer s =
let s' = String.create (2 * String.length s) in
String.blit s 0 s' 0 (String.length s);
let s' = Bytes.create (2 * Bytes.length s) in
Bytes.blit s 0 s' 0 (Bytes.length s);
s'

let compress_string ?(level = 6) inbuf =
let zs = deflate_init level true in
let rec compr inpos outbuf outpos =
let inavail = String.length inbuf - inpos in
let outavail = String.length outbuf - outpos in
let outavail = Bytes.length outbuf - outpos in
if outavail = 0
then compr inpos (grow_buffer outbuf) outpos
else begin
let (finished, used_in, used_out) =
deflate zs inbuf inpos inavail outbuf outpos outavail
deflate_string zs inbuf inpos inavail outbuf outpos outavail
(if inavail = 0 then Z_FINISH else Z_NO_FLUSH) in
if finished then
String.sub outbuf 0 (outpos + used_out)
Bytes.sub_string outbuf 0 (outpos + used_out)
else
compr (inpos + used_in) outbuf (outpos + used_out)
end in
let res = compr 0 (String.create (String.length inbuf)) 0 in
let res = compr 0 (Bytes.create (String.length inbuf)) 0 in
deflate_end zs;
res

Expand All @@ -42,20 +42,20 @@ let gzip_string ?(level = 6) inbuf =
let out_crc = ref Int32.zero in
let rec compr inpos outbuf outpos =
let inavail = String.length inbuf - inpos in
let outavail = String.length outbuf - outpos in
let outavail = Bytes.length outbuf - outpos in
if outavail = 0
then compr inpos (grow_buffer outbuf) outpos
else begin
let (finished, used_in, used_out) =
deflate zs inbuf inpos inavail outbuf outpos outavail
deflate_string zs inbuf inpos inavail outbuf outpos outavail
(if inavail = 0 then Z_FINISH else Z_NO_FLUSH) in
out_crc := update_crc !out_crc inbuf inpos used_in;
out_crc := update_crc_string !out_crc inbuf inpos used_in;
if finished then
String.sub outbuf 0 (outpos + used_out)
Bytes.sub_string outbuf 0 (outpos + used_out)
else
compr (inpos + used_in) outbuf (outpos + used_out)
end in
let res = compr 0 (String.create (String.length inbuf)) 0 in
let res = compr 0 (Bytes.create (String.length inbuf)) 0 in
deflate_end zs;
let buf = Buffer.create (18 + String.length res) in
let write_int wbuf n =
Expand Down Expand Up @@ -85,18 +85,18 @@ let uncompress_string2 inbuf =
let zs = inflate_init true in
let rec uncompr inpos outbuf outpos =
let inavail = String.length inbuf - inpos in
let outavail = String.length outbuf - outpos in
let outavail = Bytes.length outbuf - outpos in
if outavail = 0
then uncompr inpos (grow_buffer outbuf) outpos
else begin
let (finished, used_in, used_out) =
inflate zs inbuf inpos inavail outbuf outpos outavail Z_SYNC_FLUSH in
inflate_string zs inbuf inpos inavail outbuf outpos outavail Z_SYNC_FLUSH in
if finished then
String.sub outbuf 0 (outpos + used_out)
Bytes.sub_string outbuf 0 (outpos + used_out)
else
uncompr (inpos + used_in) outbuf (outpos + used_out)
end in
let res = uncompr 0 (String.create (2 * String.length inbuf)) 0 in
let res = uncompr 0 (Bytes.create (2 * String.length inbuf)) 0 in
inflate_end zs;
res

Expand All @@ -105,12 +105,12 @@ let uncompress_string s =
let pos = ref 0 in
let len = String.length s in
uncompress ~header: true (fun b ->
let n = min (String.length b) (len - !pos) in
let n = min (Bytes.length b) (len - !pos) in
if n < 1 then 0 else begin
String.blit s !pos b 0 n;
pos := !pos + n;
n end
) (fun s len -> Buffer.add_string buf (String.sub s 0 len));
) (fun s len -> Buffer.add_string buf (Bytes.sub_string s 0 len));
Buffer.contents buf


4 changes: 2 additions & 2 deletions src/utils/lib/bitv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,11 @@ let all_ones v =

let to_string v =
let n = v.length in
let s = String.make n '0' in
let s = Bytes.make n '0' in
for i = 0 to n - 1 do
if unsafe_get v i then s.[i] <- '1'
done;
s
Bytes.unsafe_to_string s

let print fmt v = Format.pp_print_string fmt (to_string v)

Expand Down
80 changes: 40 additions & 40 deletions src/utils/lib/md4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Base16 = struct
else Char.chr (Char.code '0' + x)

let to_string hash_length s =
let p = String.create (hash_length * 2) in
let p = Bytes.create (hash_length * 2) in
for i = 0 to hash_length - 1 do
let c = s.[i] in
let n = int_of_char c in
Expand All @@ -54,15 +54,15 @@ module Base16 = struct
p.[2 * i] <- hexa_digit i0;
p.[2 * i+1] <- hexa_digit i1;
done;
p
Bytes.unsafe_to_string p

let hexa_digit_case upper x =
if x >= 10 then Char.chr (Char.code (
if upper then 'A' else 'a')+ x - 10)
else Char.chr (Char.code '0' + x)

let to_string_case upper hash_length s =
let p = String.create (hash_length * 2) in
let p = Bytes.create (hash_length * 2) in
for i = 0 to hash_length - 1 do
let c = s.[i] in
let n = int_of_char c in
Expand All @@ -71,7 +71,7 @@ module Base16 = struct
p.[2 * i] <- hexa_digit_case upper i0;
p.[2 * i+1] <- hexa_digit_case upper i1;
done;
p
Bytes.unsafe_to_string p

let digit_hexa c =
let i = int_of_char c in
Expand All @@ -82,13 +82,13 @@ module Base16 = struct

let of_string hash_length s =
assert (String.length s = hash_length*2);
let p = String.create hash_length in
let p = Bytes.create hash_length in
for i = 0 to hash_length - 1 do
let c0 = s.[2*i] in
let c1 = s.[2*i+1] in
p.[i] <- char_of_int ((16 * digit_hexa c0) + digit_hexa c1);
done;
p
Bytes.unsafe_to_string p

end

Expand Down Expand Up @@ -154,7 +154,7 @@ module Base32 = struct
let to_string_case upper hash_length s =
assert (String.length s = hash_length);
let len = (hash_length * 8 + 4)/5 in
let r = String.create len in
let r = Bytes.create len in
for i = 0 to len - 1 do
let pos = i * 5 in
let byte = pos / 8 in
Expand All @@ -171,7 +171,7 @@ module Base32 = struct
let c = (x lsr (11 - bit)) land 0x1f in
r.[i] <- char_of_int5 upper c
done;
r
Bytes.unsafe_to_string r

end

Expand Down Expand Up @@ -265,7 +265,7 @@ module type Digest = sig

val string : string -> t
(* val file : string -> t *)
val create : unit -> t
(* val create : unit -> t *)
val direct_of_string : string -> t
val direct_to_string : t -> string
val random : unit -> t
Expand All @@ -292,12 +292,12 @@ module Make(M: sig
val hash_name : string

(* [unsafe_string digest string string_len] *)
val unsafe_string : string -> string -> int -> unit
val unsafe_string : bytes -> string -> int -> unit

(* [unsafe_file digest filename filesize] *)
val unsafe_file : string -> string -> int64 -> unit
val unsafe_file : bytes -> string -> int64 -> unit
(* [unsafe_string digest file_fd offset len] *)
val digest_subfile : string -> Unix.file_descr -> int64 -> int64 -> unit
val digest_subfile : bytes -> Unix.file_descr -> int64 -> int64 -> unit

module Base : Base
end) = struct
Expand All @@ -315,13 +315,13 @@ module Make(M: sig

let string s =
let len = String.length s in
let digest = String.create hash_length in
let digest = Bytes.create hash_length in
unsafe_string digest s len;
digest
Bytes.unsafe_to_string digest

let to_bits s =
let len = String.length s in
let digest = String.create (8*len) in
let digest = Bytes.create (8*len) in
for i = 0 to len-1 do
let c = int_of_char s.[i] in
for j = 7 downto 0 do
Expand All @@ -330,39 +330,39 @@ module Make(M: sig

done
done;
digest
Bytes.unsafe_to_string digest

external xor_c : t -> t -> t -> unit = "md4_xor" "noalloc"
external xor_c : t -> t -> bytes -> unit = "md4_xor" "noalloc"

let xor m1 m2 =
let m3 = String.create hash_length in
let m3 = Bytes.create hash_length in
xor_c m1 m2 m3;
m3
Bytes.unsafe_to_string m3

let file s =
let digest = String.create hash_length in
let digest = Bytes.create hash_length in
let file_size = Unix32.getsize s in
unsafe_file digest s file_size;
digest
Bytes.unsafe_to_string digest

let digest_subfile fd pos len =
let digest = String.create hash_length in
let digest = Bytes.create hash_length in
Unix32.apply_on_chunk fd pos len
(fun fd pos ->
digest_subfile digest fd pos len);
digest
Bytes.unsafe_to_string digest

let create () = String.create hash_length
(* let create () = String.create hash_length *)

let direct_to_string s = s
let direct_of_string s = s

let random () =
let s = create () in
let s = Bytes.create hash_length in
for i = 0 to hash_length - 1 do
s.[i] <- char_of_int (Random.int 256)
done;
s
Bytes.unsafe_to_string s

let of_string = Base.of_string hash_length
let to_string = Base.to_string hash_length
Expand Down Expand Up @@ -398,9 +398,9 @@ module Md4 = Make(struct
let hash_length = 16
let hash_name = "Md4"

external unsafe_string : string -> string -> int -> unit = "md4_unsafe_string"
external unsafe_file : string -> string -> int64 -> unit = "md4_unsafe_file"
external digest_subfile : string -> Unix.file_descr -> int64 -> int64 -> unit =
external unsafe_string : bytes -> string -> int -> unit = "md4_unsafe_string"
external unsafe_file : bytes -> string -> int64 -> unit = "md4_unsafe_file"
external digest_subfile : bytes -> Unix.file_descr -> int64 -> int64 -> unit =
"md4_unsafe64_fd"

module Base = Base16
Expand All @@ -410,9 +410,9 @@ module Md5 = Make(struct
let hash_length = 16
let hash_name = "Md5"

external unsafe_string : string -> string -> int -> unit = "md5_unsafe_string"
external unsafe_file : string -> string -> int64 -> unit = "md5_unsafe_file"
external digest_subfile : string -> Unix.file_descr -> int64 -> int64 -> unit =
external unsafe_string : bytes -> string -> int -> unit = "md5_unsafe_string"
external unsafe_file : bytes -> string -> int64 -> unit = "md5_unsafe_file"
external digest_subfile : bytes -> Unix.file_descr -> int64 -> int64 -> unit =
"md5_unsafe64_fd"

module Base = Base16
Expand All @@ -422,9 +422,9 @@ module PreSha1 = Make(struct
let hash_length = 20
let hash_name = "Sha1"

external unsafe_string : string -> string -> int -> unit = "sha1_unsafe_string"
external unsafe_file : string -> string -> int64 -> unit = "sha1_unsafe_file"
external digest_subfile : string -> Unix.file_descr -> int64 -> int64 -> unit =
external unsafe_string : bytes -> string -> int -> unit = "sha1_unsafe_string"
external unsafe_file : bytes -> string -> int64 -> unit = "sha1_unsafe_file"
external digest_subfile : bytes -> Unix.file_descr -> int64 -> int64 -> unit =
"sha1_unsafe64_fd"

module Base = Base32
Expand Down Expand Up @@ -465,7 +465,7 @@ module Tiger = Make(struct
let hash_length = 24
let hash_name = "Tiger"

external unsafe_string : string -> string -> int -> unit =
external unsafe_string : bytes -> string -> int -> unit =
"tiger_unsafe_string"

let unsafe_file digest filename =
Expand All @@ -482,8 +482,8 @@ module PreTigerTree = Make(struct
let hash_length = 24
let hash_name = "TigerTree"

external unsafe_string : string -> string -> int -> unit = "tigertree_unsafe_string"
external digest_subfile : string -> Unix.file_descr -> int64 -> int64 -> unit =
external unsafe_string : bytes -> string -> int -> unit = "tigertree_unsafe_string"
external digest_subfile : bytes -> Unix.file_descr -> int64 -> int64 -> unit =
"tigertree_unsafe64_fd"

let unsafe_file digest filename file_size =
Expand Down Expand Up @@ -531,10 +531,10 @@ module PreMd5Ext = Make(struct
let hash_length = 20
let hash_name = "Md5Ext"

external unsafe_string : string -> string -> int -> unit =
external unsafe_string : bytes -> string -> int -> unit =
"fst_hash_string_ml"

external unsafe_file : string -> string -> int64 -> unit = "fst_hash_file_ml"
external unsafe_file : bytes -> string -> int64 -> unit = "fst_hash_file_ml"
let digest_subfile _ _ _ _ =
failwith "Md5Ext.digest_subfile not implemented"

Expand Down
Loading

0 comments on commit ff6018b

Please sign in to comment.