Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrek committed Jun 9, 2024
1 parent 1e2d673 commit 618e4fa
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 75 deletions.
34 changes: 17 additions & 17 deletions src/utils/cdk/bzip2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let buffer_size = 1024

type in_channel =
{ in_chan: Pervasives.in_channel;
in_buffer: string;
in_buffer: bytes;
mutable in_pos: int;
mutable in_avail: int;
mutable in_eof: bool;
Expand All @@ -16,7 +16,7 @@ type in_channel =

let open_in_chan ic =
{ in_chan = ic;
in_buffer = String.create buffer_size;
in_buffer = Bytes.create buffer_size;
in_pos = 0;
in_avail = 0;
in_eof = false;
Expand All @@ -32,12 +32,12 @@ let open_in filename =
let read_byte iz =
if iz.in_avail = 0 then begin
let n = Pervasives.input iz.in_chan iz.in_buffer 0
(String.length iz.in_buffer) in
(Bytes.length iz.in_buffer) in
if n = 0 then raise End_of_file;
iz.in_pos <- 0;
iz.in_avail <- n
end;
let c = iz.in_buffer.[iz.in_pos] in
let c = Bytes.get iz.in_buffer iz.in_pos in
iz.in_pos <- iz.in_pos + 1;
iz.in_avail <- iz.in_avail - 1;
Char.code c
Expand All @@ -53,19 +53,19 @@ let read_int32 iz =
(Int32.shift_left (Int32.of_int b4) 24)))

let rec input iz buf pos len =
if pos < 0 || len < 0 || pos + len > String.length buf then
if pos < 0 || len < 0 || pos + len > Bytes.length buf then
invalid_arg "Bzip2.input";
if iz.in_eof then 0 else begin
if iz.in_avail = 0 then begin
let n = Pervasives.input iz.in_chan iz.in_buffer 0
(String.length iz.in_buffer) in
(Bytes.length iz.in_buffer) in
if n = 0 then raise(Error("truncated file"));
iz.in_pos <- 0;
iz.in_avail <- n
end;
let (finished, used_in, used_out) =
try
Bzlib.decompress iz.in_stream iz.in_buffer iz.in_pos iz.in_avail
Bzlib.decompress iz.in_stream (Bytes.unsafe_to_string iz.in_buffer) iz.in_pos iz.in_avail
buf pos len
with Bzlib.Error(_, e) ->
raise(Error(Bzlib.string_of_error e)) in
Expand All @@ -88,10 +88,10 @@ let rec really_input iz buf pos len =
really_input iz buf (pos + n) (len - n)
end

let char_buffer = String.create 1
let char_buffer = Bytes.create 1

let input_char iz =
if input iz char_buffer 0 1 = 0 then raise End_of_file else char_buffer.[0]
if input iz char_buffer 0 1 = 0 then raise End_of_file else Bytes.get char_buffer 0

let input_byte iz =
Char.code (input_char iz)
Expand All @@ -106,7 +106,7 @@ let close_in iz =

type out_channel =
{ out_chan: Pervasives.out_channel;
out_buffer: string;
out_buffer: bytes;
mutable out_pos: int;
mutable out_avail: int;
out_stream: Bzlib.stream;
Expand All @@ -115,7 +115,7 @@ type out_channel =
let open_out_chan ?(level = 6) oc =
if level < 1 || level > 9 then invalid_arg "Bzip2.open_out: bad level";
{ out_chan = oc;
out_buffer = String.create buffer_size;
out_buffer = Bytes.create buffer_size;
out_pos = 0;
out_avail = buffer_size;
out_stream = Bzlib.compress_init level 0 0;
Expand All @@ -125,18 +125,18 @@ let open_out ?(level = 6) filename =
open_out_chan ~level (Pervasives.open_out_bin filename)

let rec output oz buf pos len =
if pos < 0 || len < 0 || pos + len > String.length buf then
if pos < 0 || len < 0 || pos + len > Bytes.length buf then
invalid_arg "Bzlib2.output";
(* If output buffer is full, flush it *)
if oz.out_avail = 0 then begin
(* Printf.printf "Flushing out_avail\n"; *)
Pervasives.output oz.out_chan oz.out_buffer 0 oz.out_pos;
oz.out_pos <- 0;
oz.out_avail <- String.length oz.out_buffer
oz.out_avail <- Bytes.length oz.out_buffer
end;
let (_, used_in, used_out) =
try
Bzlib.compress oz.out_stream buf pos len
Bzlib.compress oz.out_stream (Bytes.unsafe_to_string buf) pos len
oz.out_buffer oz.out_pos oz.out_avail
Bzlib.BZ_RUN
with Bzlib.Error(_, e) ->
Expand All @@ -147,7 +147,7 @@ let rec output oz buf pos len =
if used_in < len then output oz buf (pos + used_in) (len - used_in)

let output_char oz c =
char_buffer.[0] <- c;
Bytes.set char_buffer 0 c;
output oz char_buffer 0 1

let output_byte oz b =
Expand All @@ -159,10 +159,10 @@ let flush oz =
if oz.out_avail = 0 then begin
Pervasives.output oz.out_chan oz.out_buffer 0 oz.out_pos;
oz.out_pos <- 0;
oz.out_avail <- String.length oz.out_buffer
oz.out_avail <- Bytes.length oz.out_buffer
end;
let (finished, _, used_out) =
Bzlib.compress oz.out_stream oz.out_buffer 0 0
Bzlib.compress oz.out_stream (Bytes.unsafe_to_string oz.out_buffer) 0 0
oz.out_buffer oz.out_pos oz.out_avail
Bzlib.BZ_FINISH in
oz.out_pos <- oz.out_pos + used_out;
Expand Down
10 changes: 5 additions & 5 deletions src/utils/cdk/bzip2.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ val input_byte: in_channel -> int
(* Same as [Bzip2.input_char], but return the 8-bit integer representing
the character.
Raise [End_of_file] if no more compressed data is available. *)
val input: in_channel -> string -> int -> int -> int
val input: in_channel -> bytes -> int -> int -> int
(* [input ic buf pos len] uncompresses up to [len] characters
from the given channel [ic],
storing them in string [buf], starting at character number [pos].
storing them in buffer [buf], starting at character number [pos].
It returns the actual number of characters read, between 0 and
[len] (inclusive).
A return value of 0 means that the end of file was reached.
Expand All @@ -38,10 +38,10 @@ val input: in_channel -> string -> int -> int -> int
exactly [len] characters.)
Exception [Invalid_argument "Bzip2.input"] is raised if
[pos] and [len] do not designate a valid substring of [buf]. *)
val really_input: in_channel -> string -> int -> int -> unit
val really_input: in_channel -> bytes -> int -> int -> unit
(* [really_input ic buf pos len] uncompresses [len] characters
from the given channel, storing them in
string [buf], starting at character number [pos].
buffer [buf], starting at character number [pos].
Raise [End_of_file] if fewer than [len] characters can be read.
Raise [Invalid_argument "Bzip2.input"] if
[pos] and [len] do not designate a valid substring of [buf]. *)
Expand Down Expand Up @@ -82,7 +82,7 @@ val output_char: out_channel -> char -> unit
val output_byte: out_channel -> int -> unit
(* Same as [Bzip2.output_char], but the output character is given
by its code. The given integer is taken modulo 256. *)
val output: out_channel -> string -> int -> int -> unit
val output: out_channel -> bytes -> int -> int -> unit
(* [output oc buf pos len] compresses and writes [len] characters
from string [buf], starting at offset [pos], and writes the
compressed data to the channel [oc].
Expand Down
4 changes: 2 additions & 2 deletions src/utils/cdk/bzlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ type action = BZ_RUN | BZ_FLUSH | BZ_FINISH

external compress_init: int -> int -> int -> stream = "camlzip_bzCompressInit"
external compress:
stream -> string -> int -> int -> string -> int -> int -> action
stream -> string -> int -> int -> bytes -> int -> int -> action
-> bool * int * int
= "camlzip_bzCompress_bytecode" "camlzip_bzCompress"
external compress_end: stream -> unit = "camlzip_bzCompressEnd"


external decompress_init: int -> bool -> stream = "camlzip_bzDecompressInit"
external decompress:
stream -> string -> int -> int -> string -> int -> int -> bool * int * int
stream -> string -> int -> int -> bytes -> int -> int -> bool * int * int
= "camlzip_bzDecompress_bytecode" "camlzip_bzDecompress"
external decompress_end: stream -> unit = "camlzip_bzDecompressEnd"

Expand Down
4 changes: 2 additions & 2 deletions src/utils/cdk/bzlib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ type action = BZ_RUN | BZ_FLUSH | BZ_FINISH

external compress_init: int -> int -> int -> stream = "camlzip_bzCompressInit"
external compress:
stream -> string -> int -> int -> string -> int -> int -> action
stream -> string -> int -> int -> bytes -> int -> int -> action
-> bool * int * int
= "camlzip_bzCompress_bytecode" "camlzip_bzCompress"
external compress_end: stream -> unit = "camlzip_bzCompressEnd"


external decompress_init: int -> bool -> stream = "camlzip_bzDecompressInit"
external decompress:
stream -> string -> int -> int -> string -> int -> int -> bool * int * int
stream -> string -> int -> int -> bytes -> int -> int -> bool * int * int
= "camlzip_bzDecompress_bytecode" "camlzip_bzDecompress"
external decompress_end: stream -> unit = "camlzip_bzDecompressEnd"

Expand Down
21 changes: 11 additions & 10 deletions src/utils/cdk/filename2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ let to_string filename =
List.fold_left (fun file f -> f file) filename !to_strings

let path_of_filename filename =
let filename = String.copy filename in
let len = String.length filename in
let filename = Bytes.of_string filename in
for i = 0 to len - 1 do
if filename.[i] = '\\' then filename.[i] <- '/';
if Bytes.get filename i = '\\' then Bytes.set filename i '/';
done;
let filename =
if len > 2 && filename.[1] = ':' &&
match filename.[0] with
if len > 2 && Bytes.get filename 1 = ':' &&
match Bytes.get filename 0 with
'a' .. 'z' | 'A' .. 'Z' -> true
| _ -> false then
Printf.sprintf "%s/%s" (String.sub filename 0 2)
(String.sub filename 2 (len-2))
else filename
Printf.sprintf "%s/%s" (Bytes.sub_string filename 0 2)
(Bytes.sub_string filename 2 (len-2))
else Bytes.unsafe_to_string filename
in
split_simplify filename '/'

Expand All @@ -142,11 +142,12 @@ let filesystem_compliant name fstype namemax =
(* replace all illegal characters with a valid one.
assumes all filesystems accept '_'s in filenames *)
let escape_chars p filename =
let s = String.copy filename in
let s = Bytes.of_string filename in
for i = 0 to String.length filename - 1 do
if p s.[i] then s.[i] <- '_'
if p (Bytes.get s i) then Bytes.set s i '_'
done;
s in
Bytes.unsafe_to_string s
in

(* remove all illegal characters at the beginning of filename *)
let trim_left p filename =
Expand Down
12 changes: 6 additions & 6 deletions src/utils/cdk/genlex2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ type token =

(* The string buffering machinery *)

let initial_buffer = String.create 32
let initial_buffer = Bytes.create 32

let buffer = ref initial_buffer
let bufpos = ref 0

let reset_buffer () = buffer := initial_buffer; bufpos := 0

let store c =
if !bufpos >= String.length !buffer then
if !bufpos >= Bytes.length !buffer then
begin
let newbuffer = String.create (2 * !bufpos) in
String.blit !buffer 0 newbuffer 0 !bufpos; buffer := newbuffer
let newbuffer = Bytes.create (2 * !bufpos) in
Bytes.blit !buffer 0 newbuffer 0 !bufpos; buffer := newbuffer
end;
String.set !buffer !bufpos c;
Bytes.set !buffer !bufpos c;
incr bufpos

let get_string () =
let s = String.sub !buffer 0 !bufpos in buffer := initial_buffer; s
let s = Bytes.sub_string !buffer 0 !bufpos in buffer := initial_buffer; s

(* The lexer *)

Expand Down
8 changes: 4 additions & 4 deletions src/utils/cdk/gzip.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type in_channel =
in_stream: Zlib.stream;
mutable in_size: int32;
mutable in_crc: int32;
char_buffer: string }
char_buffer: bytes }

let open_in ic =
(* Superficial parsing of header *)
Expand Down Expand Up @@ -73,7 +73,7 @@ let open_in ic =
in_stream = Zlib.inflate_init false;
in_size = Int32.zero;
in_crc = Int32.zero;
char_buffer = String.create 1 }
char_buffer = Bytes.create 1 }

let open_in_file filename =
let ic = Pervasives.open_in_bin filename in
Expand Down Expand Up @@ -176,7 +176,7 @@ type 'a out_channel =
out_stream: Zlib.stream;
mutable out_size: int32;
mutable out_crc: int32;
char_buffer: string }
char_buffer: bytes }

let open_out ?(level = 6) oc =
if level < 1 || level > 9 then invalid_arg "Gzip_stream.open_output: bad level";
Expand All @@ -195,7 +195,7 @@ let open_out ?(level = 6) oc =
out_stream = Zlib.deflate_init level false;
out_size = Int32.zero;
out_crc = Int32.zero;
char_buffer = String.create 1 }
char_buffer = Bytes.create 1 }

let open_out_file ?level filename =
let oc = Pervasives.open_out_bin filename in
Expand Down
Loading

0 comments on commit 618e4fa

Please sign in to comment.