diff --git a/config/Makefile.in b/config/Makefile.in index 19b136c2..d7d29ef5 100644 --- a/config/Makefile.in +++ b/config/Makefile.in @@ -432,10 +432,11 @@ FASTTRACK_SRCS= \ $(SRC_FASTTRACK)/fasttrackClients.mlt \ $(SRC_FASTTRACK)/fasttrackHandler.ml \ $(SRC_FASTTRACK)/fasttrackServers.ml \ - $(SRC_FASTTRACK)/fasttrackPandora.ml \ $(SRC_FASTTRACK)/fasttrackInteractive.mlt \ $(SRC_FASTTRACK)/fasttrackMain.mlt + #$(SRC_FASTTRACK)/fasttrackPandora.ml \ + $(BITSTRING)/bitstring_persistent.cmo: $(BITSTRING)/bitstring_persistent.ml $(BITSTRING)/bitstring_persistent.cmi build/bitstring.cma $(OCAMLC) -I $(BITSTRING) -I +camlp4 camlp4lib.cma -c $< diff --git a/src/daemon/common/commonComplexOptions.ml b/src/daemon/common/commonComplexOptions.ml index b5088d52..0986c3ce 100644 --- a/src/daemon/common/commonComplexOptions.ml +++ b/src/daemon/common/commonComplexOptions.ml @@ -1156,9 +1156,9 @@ let backup_tar archive files = t_devminor = 0; t_prefix = ""; t_gnu = None;} in - let s = String.create size in + let s = Bytes.create size in Pervasives.really_input ic s 0 size; - header, s) in + header, Bytes.unsafe_to_string s) in Tar.output otar header s with | e -> diff --git a/src/daemon/common/commonDownloads.ml b/src/daemon/common/commonDownloads.ml index be9aa3a6..1c266ad3 100644 --- a/src/daemon/common/commonDownloads.ml +++ b/src/daemon/common/commonDownloads.ml @@ -87,7 +87,7 @@ module Make(M: sig in let final_pos = Unix32.seek64 (file_fd file) d.download_pos Unix.SEEK_SET in *) - Unix32.write (file_fd file) d.download_pos b.buf b.pos b.len; + Unix32.write_bytes (file_fd file) d.download_pos b.buf b.pos b.len; (* end; *) (* lprintf "DIFF %d/%d\n" nread b.len; *) d.download_pos <- d.download_pos ++ (Int64.of_int b.len); diff --git a/src/daemon/common/commonFile.ml b/src/daemon/common/commonFile.ml index 769a1749..46778767 100644 --- a/src/daemon/common/commonFile.ml +++ b/src/daemon/common/commonFile.ml @@ -975,7 +975,7 @@ let recover_bytes file = if pos = max then iter_file_out file_pos segments else - if s.[pos] = '\000' then + if Bytes.get s pos = '\000' then iter_string_out file_pos (pos+1) max segments else let begin_pos = file_pos -- (Int64.of_int (max - pos)) in @@ -995,7 +995,7 @@ let recover_bytes file = if pos = max then iter_file_in file_pos begin_pos segments else - if s.[pos] = '\000' then + if Bytes.get s pos = '\000' then let end_pos = file_pos -- (Int64.of_int (max - pos)) in (* lprintf " 0 byte at %Ld\n" end_pos; *) iter_string_out file_pos (pos+1) max diff --git a/src/daemon/common/commonMultimedia.ml b/src/daemon/common/commonMultimedia.ml index e68bc310..37aa2ae9 100644 --- a/src/daemon/common/commonMultimedia.ml +++ b/src/daemon/common/commonMultimedia.ml @@ -41,10 +41,7 @@ let input_int ic = let i1 = input_int16 ic in i0 lor (i1 lsl 16) -let input_string4 ic = - let s = String.create 4 in - really_input ic s 0 4; - s +let input_string4 ic = really_input_string ic 4 let print_string4 v s = lprintf "%s :" v; @@ -230,7 +227,7 @@ let rec page_seek ic s pos = else begin really_input ic s 0 4; seek_in ic (pos_in ic - 3); - if s = "OggS" + if Bytes.unsafe_to_string s = "OggS" then seek_in ic (pos_in ic + 3) else page_seek ic s pos end @@ -278,12 +275,10 @@ let rec next_ogg_stream ic ogg_infos str stream_number = lprintf "Stream Serial Number: %0.f\n" stream_number; *) seek_in ic (pos+24); - let content_type = String.create 1 in - really_input ic content_type 0 1; + let content_type = really_input_string ic 1 in let content_type = int_of_char content_type.[0] in seek_in ic (pos+25); - let stream_type = String.create 8 in - really_input ic stream_type 0 8; + let stream_type = really_input_string ic 8 in let stream_type = normalize_stream_type stream_type content_type in incr stream_number; let pos = pos_in ic in @@ -299,8 +294,7 @@ let rec next_ogg_stream ic ogg_infos str stream_number = | OGG_THEORA_STREAM -> get_ogg_theora_info ic ogg_infos str stream_number and get_ogg_video_info ic ogg_infos str sizeof_packet stream_number = - let s = String.create sizeof_packet in - really_input ic s 0 sizeof_packet; + let s = really_input_string ic sizeof_packet in let codec = String.lowercase (String.sub s 0 4) in let time_unit = read64 (String.sub s 8 8) in let video_width = @@ -326,8 +320,7 @@ and get_ogg_video_info ic ogg_infos str sizeof_packet stream_number = next_ogg_stream ic ogg_infos str stream_number and get_ogg_audio_info ic ogg_infos str sizeof_packet stream_number = - let s = String.create sizeof_packet in - really_input ic s 0 sizeof_packet; + let s = really_input_string ic sizeof_packet in let codec = get_audio_codec (String.sub s 0 4) in let sample_per_unit = read64 (String.sub s 16 8) in let channels = @@ -359,8 +352,7 @@ and get_ogg_audio_info ic ogg_infos str sizeof_packet stream_number = and get_ogg_vorbis_info ic ogg_infos str stream_number = seek_in ic (pos_in ic - 2); (* ogm sets 8 octets in the common header as vorbis uses 6 octects for 'vorbis' *) - let s = String.create 22 in - really_input ic s 0 22; + let s = really_input_string ic 22 in let version = read32 (String.sub s 0 4) in let audio_channels = int_of_char s.[4] in let sample_rate = read32 (String.sub s 5 4) in @@ -389,8 +381,7 @@ and get_ogg_vorbis_info ic ogg_infos str stream_number = and get_ogg_theora_info ic ogg_infos str stream_number = seek_in ic (pos_in ic - 2); (* ogm sets 8 octets in the common header as theora uses 6 octects for 'theora' *) - let s = String.create 34 in - really_input ic s 0 34; + let s = really_input_string ic 34 in let vmaj = int_of_char s.[0] in let vmin = int_of_char s.[1] in let vrev = int_of_char s.[2] in @@ -451,7 +442,7 @@ and get_ogg_index_info ic ogg_infos str stream_number = let search_info_ogg ic = let stream_number = ref 0 in - let str = String.create 4 in + let str = Bytes.create 4 in let ogg_infos = ref [] in (* make sure the current reading position is at the file beginning *) seek_in ic 0; diff --git a/src/daemon/common/commonSwarming.ml b/src/daemon/common/commonSwarming.ml index 4a417c2a..a81a51bf 100644 --- a/src/daemon/common/commonSwarming.ml +++ b/src/daemon/common/commonSwarming.ml @@ -2792,7 +2792,7 @@ let range_range r = (r.range_begin, r.range_end) let received up file_begin str string_begin string_len = assert (string_begin >= 0); assert (string_len >= 0); - assert (string_begin + string_len <= Bytes.length str); + assert (string_begin + string_len <= String.length str); let t = up.up_t in let s = t.t_s in @@ -2909,7 +2909,7 @@ let received up file_begin str string_begin string_len = (Printexc2.to_string e) r.range_begin file_end (file_best_name t.t_file)); - file_write_bytes tprim.t_file + file_write tprim.t_file r.range_begin str string_pos string_length; range_received (Some t) r r.range_begin file_end; diff --git a/src/daemon/common/commonUploads.ml b/src/daemon/common/commonUploads.ml index d63fc1fc..a1b5f538 100644 --- a/src/daemon/common/commonUploads.ml +++ b/src/daemon/common/commonUploads.ml @@ -290,7 +290,7 @@ let shared_tree = new_shared_dir "" let md4_of_list md4s = let len = List.length md4s in - let s = String.create (len * 16) in + let s = Bytes.create (len * 16) in let rec iter list i = match list with [] -> () @@ -300,7 +300,7 @@ let md4_of_list md4s = iter tail (i+16) in iter md4s 0; - Md4.string s + Md4.string @@ Bytes.unsafe_to_string s let rec tiger_of_array array pos block = if block = 1 then @@ -312,11 +312,11 @@ let rec tiger_of_array array pos block = else let d1 = tiger_of_array array pos (block/2) in let d2 = tiger_of_array array (pos+block/2) (block/2) in - let s = String.create (1 + Tiger.length * 2) in + let s = Bytes.create (1 + Tiger.length * 2) in s.[0] <- '\001'; String.blit (TigerTree.direct_to_string d1) 0 s 1 Tiger.length; String.blit (TigerTree.direct_to_string d2) 0 s (1+Tiger.length) Tiger.length; - let t = Tiger.string s in + let t = Tiger.string @@ Bytes.unsafe_to_string s in let t = TigerTree.direct_of_string (Tiger.direct_to_string t) in t @@ -345,11 +345,11 @@ let rec tiger_pos2 nblocks = pos, list let tiger_node d1 d2 = - let s = String.create (1 + Tiger.length * 2) in + let s = Bytes.create (1 + Tiger.length * 2) in s.[0] <- '\001'; String.blit (TigerTree.direct_to_string d1) 0 s 1 Tiger.length; String.blit (TigerTree.direct_to_string d2) 0 s (1+Tiger.length) Tiger.length; - let t = Tiger.string s in + let t = Tiger.string @@ Bytes.unsafe_to_string s in let t = TigerTree.direct_of_string (Tiger.direct_to_string t) in t @@ -386,12 +386,12 @@ let rec fill_tiger_tree s list = let flatten_tiger_array array = let len = Array.length array in - let s = String.create ( len * TigerTree.length) in + let s = Bytes.create ( len * TigerTree.length) in for i = 0 to len - 1 do String.blit (TigerTree.direct_to_string array.(i)) 0 s (i * TigerTree.length) TigerTree.length done; - s + Bytes.unsafe_to_string s let unflatten_tiger_array s = let len = String.length s / TigerTree.length in @@ -504,9 +504,9 @@ computation ??? *) let file_size = Unix32.getsize64 fd in let len64 = min 307200L file_size in let len = Int64.to_int len64 in - let s = String.create len in + let s = Bytes.create len in Unix32.read fd zero s 0 len; - Md5Ext.string s + Md5Ext.string @@ Bytes.unsafe_to_string s with e -> current_job := None; raise e diff --git a/src/daemon/common/commonWeb.ml b/src/daemon/common/commonWeb.ml index d109d115..4cae772c 100755 --- a/src/daemon/common/commonWeb.ml +++ b/src/daemon/common/commonWeb.ml @@ -265,7 +265,7 @@ let _ = while true do let nread = Unix.read pipe_out buffer 0 buffersize in if nread = 0 then raise End_of_file; - Buffer.add_substring output buffer 0 nread + Buffer.add_subbytes output buffer 0 nread done with | End_of_file -> () diff --git a/src/daemon/common/giftDecoding.ml b/src/daemon/common/giftDecoding.ml index 8e9fe8af..c5434795 100755 --- a/src/daemon/common/giftDecoding.ml +++ b/src/daemon/common/giftDecoding.ml @@ -11,11 +11,11 @@ let gui_cut_messages f sock nread = try let rec iter pos len = if len = 0 then raise Not_found; - if b.buf.[pos] = ';' && ( + if Bytes.get b.buf pos = ';' && ( pos = b.pos || - (pos > b.pos && b.buf.[pos-1] <> '\\')) then begin + (pos > b.pos && Bytes.get b.buf (pos-1) <> '\\')) then begin let len = pos - b.pos+1 in - let s = String.sub b.buf b.pos len in + let s = Bytes.sub_string b.buf b.pos len in buf_used b len; f s; iter b.pos b.len diff --git a/src/daemon/common/guiDecoding.ml b/src/daemon/common/guiDecoding.ml index 4fc5af22..60e2b431 100644 --- a/src/daemon/common/guiDecoding.ml +++ b/src/daemon/common/guiDecoding.ml @@ -42,10 +42,10 @@ let gui_cut_messages f sock nread = let b = buf sock in try while b.len >= 4 do - let msg_len = get_int b.buf b.pos in + let msg_len = get_int (Bytes.unsafe_to_string b.buf) b.pos in if b.len >= 4 + msg_len then begin - let s = String.sub b.buf (b.pos+4) msg_len in + let s = Bytes.sub_string b.buf (b.pos+4) msg_len in buf_used b (msg_len + 4); let opcode = get_int16 s 0 in (f opcode s : unit) diff --git a/src/daemon/common/guiEncoding.ml b/src/daemon/common/guiEncoding.ml index eddfbd39..b3f7788e 100644 --- a/src/daemon/common/guiEncoding.ml +++ b/src/daemon/common/guiEncoding.ml @@ -44,10 +44,10 @@ let gui_send writer sock t = Buffer.reset buf; buf_int buf 0; writer buf t; - let s = Buffer.contents buf in - let len = String.length s - 4 in + let s = Buffer.to_bytes buf in + let len = Bytes.length s - 4 in str_int s 0 len; - write_string sock s; + write_bytes sock s with UnsupportedGuiMessage -> () (*************** diff --git a/src/daemon/driver/driverCommands.ml b/src/daemon/driver/driverCommands.ml index 731789bc..7cfbfc3e 100644 --- a/src/daemon/driver/driverCommands.ml +++ b/src/daemon/driver/driverCommands.ml @@ -1389,12 +1389,12 @@ let _ = (* can't close pipe_out in the already forked+executed process... *) let output = Buffer.create 1024 in let buffersize = 1024 in - let buffer = String.create buffersize in + let buffer = Bytes.create buffersize in (try while true do let nread = Unix.read pipe_out buffer 0 buffersize in if nread = 0 then raise End_of_file; - Buffer.add_substring output buffer 0 nread + Buffer.add_subbytes output buffer 0 nread done with | End_of_file -> () @@ -3893,7 +3893,7 @@ let _ = let num = int_of_string arg in let file = file_find num in let swarmer = CommonSwarming.file_swarmer file in - let prio = CommonSwarming.get_swarmer_block_priorities swarmer in + let prio = Bytes.unsafe_to_string @@ CommonSwarming.get_swarmer_block_priorities swarmer in let downloaded = CommonSwarming.get_swarmer_block_verified swarmer in pr "\\"; pr "priorities: "; diff --git a/src/daemon/driver/driverControlers.ml b/src/daemon/driver/driverControlers.ml index 59f71f86..1499ae1f 100644 --- a/src/daemon/driver/driverControlers.ml +++ b/src/daemon/driver/driverControlers.ml @@ -464,7 +464,7 @@ let user_reader o telnet sock nread = let b = TcpBufferedSocket.buf sock in let rec iter () = if b.len > 0 then - let c = b.buf.[b.pos] in + let c = Bytes.get b.buf b.pos in buf_used b 1; (* lprintf "char %d\n" (int_of_char c); *) if c = '\255' && not telnet.telnet_iac then begin @@ -766,9 +766,7 @@ let read_theme_page page = let theme_page = get_theme_page page in Unix2.tryopen_read theme_page (fun file -> let size = (Unix.stat theme_page).Unix.st_size in - let s = String.make size ' ' in - really_input file s 0 size; - s) + really_input_string file size) let http_add_gen_header r = add_reply_header r "Server" ("MLdonkey/"^Autoconf.current_version); @@ -929,8 +927,8 @@ let send_preview r file fd size filename exten = add_reply_header r "Content-Disposition" (Printf.sprintf "inline;filename=\"%s\"" (Filename.basename filename)); - let s = String.create 200000 in - set_max_output_buffer r.sock (String.length s); + let s = Bytes.create 200000 in + set_max_output_buffer r.sock (Bytes.length s); set_rtimeout r.sock 10000.; let rec stream_file file pos sock = let max = (max_refill sock) - 1 in diff --git a/src/networks/bittorrent/bTClients.ml b/src/networks/bittorrent/bTClients.ml index 5ecfbdc5..3ec28cc3 100644 --- a/src/networks/bittorrent/bTClients.ml +++ b/src/networks/bittorrent/bTClients.ml @@ -114,6 +114,7 @@ let string_of_event = function Better create single global udp socket and use it for all tracker requests and distinguish trackers by txn? FIXME? *) + let talk_to_udp_tracker host port args file t need_sources = let interact ip = let socket = create (Ip.to_inet_addr !!client_bind_addr) 0 (fun sock event -> @@ -137,10 +138,10 @@ let talk_to_udp_tracker host port args file t need_sources = BasicSocket.set_rtimeout (sock socket) 60.; let txn = Random.int32 Int32.max_int in (* lprintf_nl "udpt txn %ld for %s" txn host; *) - write socket false (connect_request txn) ip port; + write socket false (Bytes.unsafe_of_string @@ connect_request txn) ip port; set_reader begin fun () -> let p = read socket in - let conn = connect_response p.udp_content txn in + let conn = connect_response (Bytes.unsafe_to_string p.udp_content) txn in (* lprintf_nl "udpt connection_id %Ld for %s" conn host; *) let txn = Random.int32 Int32.max_int in (* lprintf_nl "udpt txn' %ld for host %s" txn host; *) @@ -159,7 +160,7 @@ let talk_to_udp_tracker host port args file t need_sources = ~numwant:(if need_sources then try Int32.of_string (List.assoc "numwant" args) with _ -> -1l else 0l) (int_of_string (List.assoc "port" args)) in - write socket false req ip port; + write socket false (Bytes.unsafe_of_string req) ip port; set_reader (fun () -> let p = read socket in @@ -169,7 +170,7 @@ let talk_to_udp_tracker host port args file t need_sources = t.tracker_min_interval <- 600; if need_sources then t.tracker_last_clients_num <- 0; - let (interval,clients) = announce_response p.udp_content txn in + let (interval,clients) = announce_response (Bytes.unsafe_to_string p.udp_content) txn in if !verbose_msg_servers then lprintf_nl "udpt got interval %ld clients %d for host %s" interval (List.length clients) host; if interval > 0l then @@ -529,7 +530,7 @@ let is_bit_set s n = let set_bit s n = let i = n lsr 3 in - s.[i] <- Char.unsafe_chr (Char.code s.[i] lor bits.(n land 7)) + s.[i] <- Char.unsafe_chr (Char.code (Bytes.get s i) lor bits.(n land 7)) (* Official client seems to use max_range_request 5 and max_range_len 2^14 *) (* How much requests in the 'pipeline' *) @@ -537,10 +538,10 @@ let max_range_requests = 5 (* How much bytes we can request in one Piece *) let reserved () = - let s = String.make 8 '\x00' in + let s = Bytes.make 8 '\x00' in s.[7] <- (match !bt_dht with None -> '\x00' | Some _ -> '\x01'); s.[5] <- '\x10'; (* TODO bep9, bep10, notify clients about extended*) - s + Bytes.unsafe_to_string s (** handshake *) let send_init client_uid file_id sock = @@ -579,20 +580,20 @@ let send_bitfield c = lprintf_nl "Sending completed verified bitmap"; let nchunks = Array.length c.client_file.file_chunks in let len = (nchunks+7)/8 in - let s = String.make len '\000' in + let s = Bytes.make len '\000' in for i = 0 to nchunks - 1 do set_bit s i done; - s + Bytes.unsafe_to_string s | Some swarmer -> let bitmap = CommonSwarming.chunks_verified_bitmap swarmer in if !verbose_download then lprintf_nl "Sending verified bitmap: [%s]" (VB.to_string bitmap); let len = (VB.length bitmap + 7)/8 in - let s = String.make len '\000' in + let s = Bytes.make len '\000' in VB.iteri (fun i c -> if c = VB.State_verified then set_bit s i) bitmap; - s + Bytes.unsafe_to_string s )) let counter = ref 0 @@ -1920,7 +1921,7 @@ let rec iter_upload sock c = end in (* lprintf "sending piece\n"; *) - send_client c (Piece (num, pos, upload_buffer, 0, len)); + send_client c (Piece (num, pos, Bytes.unsafe_to_string upload_buffer, 0, len)); iter_upload sock c with exn -> if !verbose then diff --git a/src/networks/bittorrent/bTOptions.ml b/src/networks/bittorrent/bTOptions.ml index 4915e583..d38fa190 100644 --- a/src/networks/bittorrent/bTOptions.ml +++ b/src/networks/bittorrent/bTOptions.ml @@ -36,13 +36,13 @@ let client_port = define_option bittorrent_section ["client_port"] *) let generate_client_uid = let client_uid_from_version = "-ML" ^ Autoconf.current_version ^ "-" in - let client_uid_random_tail = String.create (20 - (String.length client_uid_from_version)) in - let sl_client_uid_random_tail = String.length client_uid_random_tail in + let client_uid_random_tail = Bytes.create (20 - (String.length client_uid_from_version)) in + let sl_client_uid_random_tail = Bytes.length client_uid_random_tail in if sl_client_uid_random_tail > 0 then for i = 0 to sl_client_uid_random_tail - 1 do client_uid_random_tail.[i] <- char_of_int (Random.int 256) done; - client_uid_from_version ^ client_uid_random_tail + client_uid_from_version ^ Bytes.unsafe_to_string client_uid_random_tail let client_uid = define_option bittorrent_section ["client_uid"] "The UID of this client" diff --git a/src/networks/bittorrent/bTProtocol.ml b/src/networks/bittorrent/bTProtocol.ml index 6c91ef72..be610410 100644 --- a/src/networks/bittorrent/bTProtocol.ml +++ b/src/networks/bittorrent/bTProtocol.ml @@ -329,9 +329,9 @@ module TcpMessages = struct | DHT_Port n -> buf_int8 buf 9; buf_int16 buf n | Extended (n,msg) -> buf_int8 buf 20; buf_int8 buf n; Buffer.add_string buf msg end; - let s = Buffer.contents buf in - str_int s 0 (String.length s - 4); - s + let s = Buffer.to_bytes buf in + str_int s 0 (Bytes.length s - 4); + Bytes.unsafe_to_string s end (*************************************************************************) @@ -456,7 +456,7 @@ let bt_handler parse_fun handler c sock = then drop the connection) *) if b.len >= 20 then begin - let payload = String.sub b.buf b.pos 20 in + let payload = Bytes.sub_string b.buf b.pos 20 in let p = parse_fun (-1) payload in buf_used b 20; c.client_received_peer_id <- true; @@ -477,13 +477,13 @@ let bt_handler parse_fun handler c sock = raise (Wait_for_more "after_peer_id"); end; while b.len >= 4 do - let msg_len = get_int b.buf b.pos in + let msg_len = get_int (Bytes.unsafe_to_string b.buf) b.pos in if msg_len < 0 then begin let (ip,port) = (TcpBufferedSocket.peer_addr sock) in lprintf_nl "BT: Unknown message from %s:%d dropped!! peerid:%b data_len:%i msg_len:%i software: %s" (Ip.to_string ip) port c.client_received_peer_id b.len msg_len (brand_to_string c.client_brand); - dump (String.sub b.buf b.pos (min b.len 30)); + dump (Bytes.sub_string b.buf b.pos (min b.len 30)); buf_used b b.len; close sock Closed_by_user; end @@ -494,7 +494,7 @@ let bt_handler parse_fun handler c sock = let (ip,port) = (TcpBufferedSocket.peer_addr sock) in lprintf_nl "btprotocol.bt_handler: closed connection from %s:%d because of too much data!! data_len:%i msg_len:%i software: %s" (Ip.to_string ip) port b.len msg_len (brand_to_string c.client_brand); - dump (String.sub b.buf b.pos (min b.len 30)); + dump (Bytes.sub_string b.buf b.pos (min b.len 30)); buf_used b b.len; close sock Closed_by_user end @@ -503,9 +503,9 @@ let bt_handler parse_fun handler c sock = buf_used b 4; (* lprintf "Message complete: %d\n" msg_len; *) if msg_len > 0 then - let opcode = get_int8 b.buf b.pos in + let opcode = get_int8 (Bytes.unsafe_to_string b.buf) b.pos in (* FIXME sub *) - let payload = String.sub b.buf (b.pos+1) (msg_len-1) in + let payload = Bytes.sub_string b.buf (b.pos+1) (msg_len-1) in buf_used b msg_len; (* lprintf "Opcode %d\n" opcode; *) try @@ -543,7 +543,7 @@ let handlers info gconn = match gconn.gconn_handler with | BTHeader h -> (* dump (String.sub b.buf b.pos (min b.len 100)); *) - let slen = get_int8 b.buf b.pos in + let slen = get_int8 (Bytes.unsafe_to_string b.buf) b.pos in if slen + 29 <= b.len then begin (* get proto and file_id from handshake, @@ -552,14 +552,14 @@ let handlers info gconn = *) (* let proto = String.sub b.buf (b.pos+1) slen in *) let file_id = Sha1.direct_of_string - (String.sub b.buf (b.pos+9+slen) 20) in - let proto,pos = get_string8 b.buf b.pos in - let rbits = (String.sub b.buf (b.pos+pos) 8) in + (Bytes.sub_string b.buf (b.pos+9+slen) 20) in + let proto,pos = get_string8 (Bytes.unsafe_to_string b.buf) b.pos in + let rbits = (Bytes.sub_string b.buf (b.pos+pos) 8) in buf_used b (slen+29); h gconn sock (proto, rbits, file_id); end else - if (String.sub b.buf b.pos (min b.len 100)) = "NATCHECK_HANDSHAKE" then + if (Bytes.sub_string b.buf b.pos (min b.len 100)) = "NATCHECK_HANDSHAKE" then write_string sock (Printf.sprintf "azureus_rand_%d" !azureus_porttest_random) else if (TcpBufferedSocket.closed sock) then let (ip,port) = (TcpBufferedSocket.peer_addr sock) in diff --git a/src/networks/bittorrent/bTTorrent.ml b/src/networks/bittorrent/bTTorrent.ml index 26ff2096..97c6b167 100644 --- a/src/networks/bittorrent/bTTorrent.ml +++ b/src/networks/bittorrent/bTTorrent.ml @@ -328,11 +328,12 @@ let decode_torrent s = let encode_torrent torrent = let npieces = Array.length torrent.torrent_pieces in - let pieces = String.create (20 * npieces) in + let pieces = Bytes.create (20 * npieces) in for i = 0 to npieces - 1 do String.blit (Sha1.direct_to_string torrent.torrent_pieces.(i)) 0 pieces (i*20) 20 done; + let pieces = Bytes.unsafe_to_string pieces in let encode_file (filename, size) = Dictionary [ diff --git a/src/networks/bittorrent/bT_DHT.ml b/src/networks/bittorrent/bT_DHT.ml index 71569ed2..dab49460 100644 --- a/src/networks/bittorrent/bT_DHT.ml +++ b/src/networks/bittorrent/bT_DHT.ml @@ -125,7 +125,7 @@ let send sock stats (ip,port as addr) txnmsg = if !debug then lprintf_nl "KRPC to %s : %S" (show_addr addr) s; stats_add stats `Sent 1; stats_add stats `SentBytes (String.length s); - write sock false s ip port + write sock false (Bytes.unsafe_of_string s) ip port type stats_key = [ `Timeout | `Sent | `SentBytes | `Recv | `RecvBytes | `Decoded | `Handled | `NoTxn ] type t = @@ -197,16 +197,16 @@ let create port enabler bw_control answer : t = let addr = (Ip.of_inet_addr inet_addr, port) in let ret = ref None in try - stats_add stats `RecvBytes (String.length p.udp_content); + stats_add stats `RecvBytes (Bytes.length p.udp_content); stats_add stats `Recv 1; - let r = decode_exn p.udp_content in + let r = decode_exn @@ Bytes.unsafe_to_string p.udp_content in stats_add stats `Decoded 1; ret := Some r; handle addr r; stats_add stats `Handled 1; with exn -> let version = match !ret with Some (_,Some s,_) -> sprintf " client %S" s | _ -> "" in - if !verb then lprintf_nl ~exn "handle packet from %s%s : %S" (show_addr addr) version p.udp_content; + if !verb then lprintf_nl ~exn "handle packet from %s%s : %S" (show_addr addr) version (Bytes.unsafe_to_string p.udp_content); let error txn code str = send socket stats addr (txn,(Error (Int64.of_int code,str))) in match exn,!ret with | Malformed_packet x, Some (txn, _, _) @@ -308,20 +308,20 @@ let make_peer (ip,port) = assert (port <= 0xffff); let (a,b,c,d) = Ip.to_ints ip in let e = port lsr 8 and f = port land 0xff in - let s = String.create 6 in + let s = Bytes.create 6 in let set i c = s.[i] <- char_of_int c in set 0 a; set 1 b; set 2 c; set 3 d; set 4 e; set 5 f; - s + Bytes.unsafe_to_string s let make_nodes nodes = - let s = String.create (26 * List.length nodes) in + let s = Bytes.create (26 * List.length nodes) in let i = ref 0 in List.iter (fun (id,addr) -> - String.blit (H.direct_to_string id) 0 s (!i*26) 20; - String.blit (make_peer addr) 0 s (!i*26+20) 6; + Bytes.blit_string (H.direct_to_string id) 0 s (!i*26) 20; + Bytes.blit_string (make_peer addr) 0 s (!i*26+20) 6; incr i ) nodes; - s + Bytes.unsafe_to_string s let parse_response_exn q dict = let get k = List.assoc k dict in diff --git a/src/networks/bittorrent/kademlia.ml b/src/networks/bittorrent/kademlia.ml index c6d901f3..017b5bd6 100644 --- a/src/networks/bittorrent/kademlia.ml +++ b/src/networks/bittorrent/kademlia.ml @@ -72,14 +72,14 @@ let cmp id1 id2 = let inside node hash = not (cmp hash node.lo = LT || cmp hash node.hi = GT) let middle = - let s = String.make 20 (Char.chr 0xFF) in + let s = Bytes.make 20 (Char.chr 0xFF) in s.[0] <- Char.chr 0x7F; - H.direct_of_string s + H.direct_of_string @@ Bytes.unsafe_to_string s let middle' = - let s = String.make 20 (Char.chr 0x00) in + let s = Bytes.make 20 (Char.chr 0x00) in s.[0] <- Char.chr 0x80; - H.direct_of_string s + H.direct_of_string @@ Bytes.unsafe_to_string s let last = H.direct_of_string (String.make 20 (Char.chr 0xFF)) @@ -95,16 +95,16 @@ let big_int_of_hash h = !n let hash_of_big_int n = - let s = String.create H.length in + let s = Bytes.create H.length in let n = ref n in let div = big_int_of_int 256 in - for i = String.length s - 1 downto 0 do + for i = Bytes.length s - 1 downto 0 do let (d,m) = quomod_big_int !n div in s.[i] <- Char.chr (int_of_big_int m); n := d done; assert (eq_big_int zero_big_int !n); - H.direct_of_string s + H.direct_of_string @@ Bytes.unsafe_to_string s let big_int_2 = big_int_of_int 2 (* hash <-> number *) diff --git a/src/networks/direct_connect/dcClients.ml b/src/networks/direct_connect/dcClients.ml index ccd70c52..4d350e6a 100644 --- a/src/networks/direct_connect/dcClients.ml +++ b/src/networks/direct_connect/dcClients.ml @@ -1321,16 +1321,16 @@ let client_downloaded c sock nread = (* TODO check tth while loading, abort if e let downloaded = if c.client_preread_bytes_left > 0 then begin (* if precheck not yet done *) let check_bytes = min nread c.client_preread_bytes_left in (* which is smaller... *) - let check_buffer = String.create check_bytes in + let check_buffer = Bytes.create check_bytes in Unix32.read (file_fd file) (c.client_pos -- (Int64.of_int c.client_preread_bytes_left)) check_buffer 0 check_bytes; - let str2 = String.sub b.buf b.pos check_bytes in - if (String.compare check_buffer str2) = 0 then begin (* if downloaded is ok *) + let str2 = Bytes.sub b.buf b.pos check_bytes in + if (Bytes.compare check_buffer str2) = 0 then begin (* if downloaded is ok *) c.client_preread_bytes_left <- c.client_preread_bytes_left - check_bytes; if c.client_preread_bytes_left = 0 then begin (* if checked all preread bytes *) let downloaded = b.len - check_bytes in if downloaded > 0 then begin (* check if buffer has bytes to write to file *) - Unix32.write (file_fd file) c.client_pos b.buf (b.pos+check_bytes) downloaded + Unix32.write_bytes (file_fd file) c.client_pos b.buf (b.pos+check_bytes) downloaded end; Int64.of_int downloaded end else Int64.zero @@ -1342,7 +1342,7 @@ let client_downloaded c sock nread = (* TODO check tth while loading, abort if e Int64.zero end end else begin (* precheck done, normal flow *) - Unix32.write (file_fd file) c.client_pos b.buf b.pos b.len; + Unix32.write_bytes (file_fd file) c.client_pos b.buf b.pos b.len; Int64.of_int b.len end in @@ -1370,7 +1370,7 @@ let client_downloaded c sock nread = (* TODO check tth while loading, abort if e | DcDownloadList filelist_fd -> (* downloading file list *) let b = TcpBufferedSocket.buf sock in let len = Int64.of_int b.len in - Unix32.write filelist_fd c.client_pos b.buf b.pos b.len; + Unix32.write_bytes filelist_fd c.client_pos b.buf b.pos b.len; c.client_pos <- c.client_pos ++ len; (match c.client_user with | Some u -> u.user_downloaded <- u.user_downloaded ++ len @@ -1456,7 +1456,7 @@ let udp_send ip port m = Buffer.reset buf; dc_write buf m; Buffer.add_char buf '|'; - let s = Buffer.contents buf in + let s = Buffer.to_bytes buf in (match !dc_udp_sock with | Some sock -> (*if !verbose_udp || !verbose_msg_clients then lprintf_nl "UDP Send: (%s)" s;*) @@ -1473,9 +1473,9 @@ let udp_handler sock event = UdpSocket.read_packets sock (fun p -> (try let pbuf = p.UdpSocket.udp_content in - let len = String.length pbuf in + let len = Bytes.length pbuf in if len > 0 then - udp_parse pbuf sock + udp_parse (Bytes.unsafe_to_string pbuf) sock with e -> () ) ) | _ -> () diff --git a/src/networks/direct_connect/dcProtocol.ml b/src/networks/direct_connect/dcProtocol.ml index 9e408b45..cc0ab678 100644 --- a/src/networks/direct_connect/dcProtocol.ml +++ b/src/networks/direct_connect/dcProtocol.ml @@ -1122,10 +1122,10 @@ let dc_handler_server f sock nread = (try let rec iter nread = if nread > 0 then begin - let pos = String.index_from b.buf b.pos '|' in + let pos = Bytes.index_from b.buf b.pos '|' in if pos < (b.pos + b.len) then begin let len = pos - b.pos in - let s = String.sub b.buf b.pos len in + let s = Bytes.sub_string b.buf b.pos len in buf_used b (len+1); begin try f (dc_parse true s) sock @@ -1150,10 +1150,10 @@ let dc_handler_client c fm nm dm sock nread = (* fm = (read_first_message false) | Some c when c.client_receiving <> Int64.zero -> (* if we are downloading from client ...*) dm c sock nread | _ -> (* or message is a new connection ... *) - let pos = String.index_from b.buf b.pos '|' in + let pos = Bytes.index_from b.buf b.pos '|' in if pos < (b.pos + b.len) then begin let len = pos - b.pos in - let s = String.sub b.buf b.pos len in + let s = Bytes.sub_string b.buf b.pos len in let msg = dc_parse false s in buf_used b (len+1); begin try diff --git a/src/networks/direct_connect/dcShared.ml b/src/networks/direct_connect/dcShared.ml index 716577e2..4506245d 100644 --- a/src/networks/direct_connect/dcShared.ml +++ b/src/networks/direct_connect/dcShared.ml @@ -98,9 +98,9 @@ let file_to_che3_to_string filename = let rec read pos = let rlen = int64_min_int (flen -- pos) slen in let npos = Int64.add pos (Int64.of_int rlen) in - let str = String.create slen in + let str = Bytes.create slen in Unix32.read file_fd pos str 0 rlen; - Buffer.add_string buf str; + Buffer.add_bytes buf str; if npos < flen then read npos in read Int64.zero; @@ -146,12 +146,12 @@ let file_to_bz2_to_buffer filename = getchar () in getchar ();*) let rec decompress () = - let str = String.create 4096 in - let n = Bzip2.input ic str 0 (String.length str) in + let str = Bytes.create 4096 in + let n = Bzip2.input ic str 0 (Bytes.length str) in if n = 0 then () else begin (*let ss = (String.sub str 0 n) in*) - Buffer.add_string buf (String.sub str 0 n); + Buffer.add_string buf (Bytes.sub_string str 0 n); (*lprintf_nl "(%s)" ss;*) decompress () end @@ -180,7 +180,7 @@ let buffer_to_bz2_to_file buf filename = else slen in let npos = pos + len in - let str = Buffer.sub buf pos len in + let str = Bytes.unsafe_of_string @@ Buffer.sub buf pos len in Bzip2.output oc str 0 len; if npos < blen then compress npos in compress 0; diff --git a/src/networks/donkey/donkeyClient.ml b/src/networks/donkey/donkeyClient.ml index 8d439a9b..ee8ad202 100644 --- a/src/networks/donkey/donkeyClient.ml +++ b/src/networks/donkey/donkeyClient.ml @@ -1603,7 +1603,7 @@ is checked for the file. if !verbose_download then lprintf_nl "Complete compressed block received!"; - let s = String.create comp.comp_len in + let s = Bytes.create comp.comp_len in let rec iter list = match list with [] -> 0 @@ -1615,6 +1615,7 @@ is checked for the file. in let pos = iter comp.comp_blocs in assert (pos = comp.comp_len); + let s = Bytes.unsafe_to_string s in let s = Zlib2.uncompress_string2 s in if !verbose_download then lprintf_nl "Decompressed: %d/%d" (String.length s) comp.comp_len; diff --git a/src/networks/donkey/donkeyFiles.ml b/src/networks/donkey/donkeyFiles.ml index b9b2af84..422e66e6 100644 --- a/src/networks/donkey/donkeyFiles.ml +++ b/src/networks/donkey/donkeyFiles.ml @@ -98,7 +98,7 @@ module NewUpload = struct impl.impl_shared_uploaded <- impl.impl_shared_uploaded ++ uploaded); - write_string sock upload_buffer; + write_bytes sock upload_buffer; check_end_upload c sock with | End_of_file -> lprintf_nl "Can not send file %s to %s, file removed?" diff --git a/src/networks/donkey/donkeyGlobals.ml b/src/networks/donkey/donkeyGlobals.ml index fee7f679..420cf609 100644 --- a/src/networks/donkey/donkeyGlobals.ml +++ b/src/networks/donkey/donkeyGlobals.ml @@ -298,7 +298,7 @@ let md4_of_array md4s = Array.iteri (fun i v -> String.blit (Md4.direct_to_string v) 0 s (i*16) 16 ) md4s; - Md4.string s + Md4.string @@ Bytes.unsafe_to_string s (* compute the name used to save the file *) diff --git a/src/networks/donkey/donkeyImport.ml b/src/networks/donkey/donkeyImport.ml index f337d181..1ade5eba 100644 --- a/src/networks/donkey/donkeyImport.ml +++ b/src/networks/donkey/donkeyImport.ml @@ -37,7 +37,7 @@ let dump_file filename = let n = input ic s 0 20 in lprintf "pos = %d\n" !pos; if n = 0 then raise Exit; - dump (String.sub s 0 n); + dump (Bytes.sub_string s 0 n); pos := !pos + n; done with End_of_file | Exit -> ()) diff --git a/src/networks/donkey/donkeyInteractive.ml b/src/networks/donkey/donkeyInteractive.ml index fbe412ac..43a9b4d9 100644 --- a/src/networks/donkey/donkeyInteractive.ml +++ b/src/networks/donkey/donkeyInteractive.ml @@ -467,9 +467,8 @@ let import_config dirname = let broadcast msg = let s = msg ^ "\n" in - let len = String.length s in List.iter (fun sock -> - TcpBufferedSocket.write sock s 0 len + TcpBufferedSocket.write_string sock s ) !user_socks (* diff --git a/src/networks/donkey/donkeyMftp.ml b/src/networks/donkey/donkeyMftp.ml index 0f3d3d64..7ab1d364 100644 --- a/src/networks/donkey/donkeyMftp.ml +++ b/src/networks/donkey/donkeyMftp.ml @@ -122,13 +122,7 @@ let read_request ic = assert (c = 227); let len32 = read_uint64_32 ic in let len = Int64.to_int len32 in - let s = String.create len in - really_input ic s 0 len; - (* - lprintf "read_request %d [%s]" len (String.escaped s); -lprint_newline (); - *) - s + really_input_string ic len let output_request oc s = output_char oc (char_of_int 227); diff --git a/src/networks/donkey/donkeyOvernetImport.ml b/src/networks/donkey/donkeyOvernetImport.ml index ff21f58f..76cbb377 100644 --- a/src/networks/donkey/donkeyOvernetImport.ml +++ b/src/networks/donkey/donkeyOvernetImport.ml @@ -24,7 +24,7 @@ open LittleEndian let dump_file filename = Unix2.tryopen_read_bin filename (fun ic -> - let s = String.create 20 in + let s = Bytes.create 20 in try lprintf "file: %s\n" filename; let pos = ref 0 in @@ -32,7 +32,7 @@ let dump_file filename = let n = input ic s 0 20 in lprintf "pos = %d\n" !pos; if n = 0 then raise Exit; - dump (String.sub s 0 n); + dump (Bytes.sub_string s 0 n); pos := !pos + n; done with End_of_file -> ()) diff --git a/src/networks/donkey/donkeyPandora.ml b/src/networks/donkey/donkeyPandora.ml index 19934ac2..a48e38df 100644 --- a/src/networks/donkey/donkeyPandora.ml +++ b/src/networks/donkey/donkeyPandora.ml @@ -172,7 +172,7 @@ let client_parse c opcode s = if comp.comp_len = comp.comp_total then begin lprintf "Compressed bloc received !!!!!!\n"; - let s = String.create comp.comp_len in + let s = Bytes.create comp.comp_len in let rec iter list = match list with [] -> 0 @@ -183,6 +183,7 @@ let client_parse c opcode s = pos + len in let pos = iter comp.comp_blocs in + let s = Bytes.unsafe_to_string s in assert (pos = comp.comp_len); let s = Zlib2.uncompress_string2 s in lprintf "Decompressed: %d/%d\n" (String.length s) comp.comp_len; diff --git a/src/networks/donkey/donkeyProtoCom.ml b/src/networks/donkey/donkeyProtoCom.ml index bc03edf7..1f68a98b 100644 --- a/src/networks/donkey/donkeyProtoCom.ml +++ b/src/networks/donkey/donkeyProtoCom.ml @@ -42,11 +42,11 @@ let client_msg_to_string emule_version msg = buf_int8 buf 0; buf_int buf 0; let magic = DonkeyProtoClient.write emule_version buf msg in - let s = Buffer.contents buf in - let len = String.length s - 5 in + let s = Buffer.to_bytes buf in + let len = Bytes.length s - 5 in s.[0] <- char_of_int magic; str_int s 1 len; - s + Bytes.unsafe_to_string s let server_msg_to_string msg = Buffer.reset buf; @@ -60,10 +60,10 @@ let server_msg_to_string msg = lprint_newline (); end; - let s = Buffer.contents buf in - let len = String.length s - 5 in + let s = Buffer.to_bytes buf in + let len = Bytes.length s - 5 in str_int s 1 len; - s + Bytes.unsafe_to_string s let server_send sock m = (* @@ -101,11 +101,11 @@ let client_handler2 c ff f = None -> emule_proto (); | Some c -> c.client_emule_proto in - let opcode = get_uint8 b.buf b.pos in - let msg_len = get_int b.buf (b.pos+1) in + let opcode = get_uint8 (Bytes.unsafe_to_string b.buf) b.pos in + let msg_len = get_int (Bytes.unsafe_to_string b.buf) (b.pos+1) in if b.len >= 5 + msg_len then begin - let s = String.sub b.buf (b.pos+5) msg_len in + let s = Bytes.sub_string b.buf (b.pos+5) msg_len in buf_used b (msg_len + 5); let t = M.parse emule_version opcode s in (* M.print t; @@ -124,11 +124,11 @@ let cut_messages parse f sock nread = let b = TcpBufferedSocket.buf sock in try while b.len >= 5 do - let opcode = get_uint8 b.buf b.pos in - let msg_len = get_int b.buf (b.pos+1) in + let opcode = get_uint8 (Bytes.unsafe_to_string b.buf) b.pos in + let msg_len = get_int (Bytes.unsafe_to_string b.buf) (b.pos+1) in if b.len >= 5 + msg_len then begin - let s = String.sub b.buf (b.pos+5) msg_len in + let s = Bytes.sub_string b.buf (b.pos+5) msg_len in buf_used b (msg_len + 5); let t = parse opcode s in f t sock @@ -148,7 +148,7 @@ let really_udp_send t ip port msg isping = try Buffer.reset buf; DonkeyProtoUdp.write buf msg; - let s = Buffer.contents buf in + let s = Buffer.to_bytes buf in UdpSocket.write t isping s ip port with e -> lprintf_nl "Exception %s in udp_send" (Printexc2.to_string e) @@ -163,10 +163,10 @@ let udp_handler f sock event = UdpSocket.read_packets sock (fun p -> try let pbuf = p.UdpSocket.udp_content in - let len = String.length pbuf in + let len = Bytes.length pbuf in if len > 0 then - let t = M.parse (int_of_char pbuf.[0]) - (String.sub pbuf 1 (len-1)) in + let t = M.parse (int_of_char (Bytes.get pbuf 0)) + (Bytes.sub_string pbuf 1 (len-1)) in (* M.print t; *) f t p with e -> () @@ -178,7 +178,7 @@ let udp_basic_handler f sock event = UdpSocket.READ_DONE -> UdpSocket.read_packets sock (fun p -> try - let pbuf = p.UdpSocket.udp_content in + let pbuf = Bytes.unsafe_to_string p.UdpSocket.udp_content in let len = String.length pbuf in if len = 0 || int_of_char pbuf.[0] <> DonkeyOpenProtocol.udp_magic then begin @@ -198,7 +198,7 @@ let udp_basic_handler f sock event = let new_string msg s = - let len = String.length s - 5 in + let len = Bytes.length s - 5 in str_int s 1 len let empty_string = "" @@ -315,9 +315,9 @@ let server_send_share compressed sock msg = ( make_tagged_server compressed (Some sock) msg ) 0 max_len in - let s = Buffer.contents buf in + let s = Buffer.to_bytes buf in str_int s 0 nfiles; - let s = String.sub s 0 prev_len in + let s = Bytes.sub_string s 0 prev_len in if !verbose_share || !verbose then lprintf_nl "Sending %d share%s to server %s:%d%s" nfiles (Printf2.print_plural_s nfiles) (Ip.to_string (peer_ip sock)) (peer_port sock) @@ -337,7 +337,7 @@ let server_send_share compressed sock msg = buf_int buf 0; buf_int8 buf 21; (* ShareReq *) Buffer.add_string buf s_c; - Buffer.contents buf + Buffer.to_bytes buf end else begin @@ -345,12 +345,12 @@ let server_send_share compressed sock msg = buf_int buf 0; buf_int8 buf 21; (* ShareReq *) Buffer.add_string buf s; - Buffer.contents buf + Buffer.to_bytes buf end in - let len = String.length s - 5 in + let len = Bytes.length s - 5 in str_int s 1 len; - write_string sock s + write_bytes sock s let client_send_files sock msg = let max_len = !!client_buffer_size - 100 - @@ -363,12 +363,11 @@ let client_send_files sock msg = let nfiles, prev_len = DonkeyProtoClient.ViewFilesReply.write_files_max buf ( make_tagged (Some sock) msg) 0 max_len in - let s = Buffer.contents buf in - let s = String.sub s 0 prev_len in - let len = String.length s - 5 in + let s = Bytes.unsafe_of_string @@ Buffer.sub buf 0 prev_len in + let len = Bytes.length s - 5 in str_int s 1 len; str_int s 6 nfiles; - write_string sock s + write_bytes sock s let client_send_dir sock dir files = let max_len = !!client_buffer_size - 100 - @@ -383,13 +382,12 @@ let client_send_dir sock dir files = let nfiles, prev_len = DonkeyProtoClient.ViewFilesReply.write_files_max buf ( make_tagged (Some sock) files) 0 max_len in - let s = Buffer.contents buf in - let s = String.sub s 0 prev_len in - let len = String.length s - 5 in begin + let s = Bytes.unsafe_of_string @@ Buffer.sub buf 0 prev_len in + let len = Bytes.length s - 5 in begin str_int s 1 len; str_int s (pos-4) nfiles; end; - write_string sock s + write_bytes sock s let udp_server_send s t = udp_send (get_udp_sock ()) s.server_ip (s.server_port+4) t diff --git a/src/networks/donkey/donkeyProtoCom.mli b/src/networks/donkey/donkeyProtoCom.mli index 7f8312bc..63e6e030 100644 --- a/src/networks/donkey/donkeyProtoCom.mli +++ b/src/networks/donkey/donkeyProtoCom.mli @@ -75,7 +75,7 @@ val client_send_files : val client_send_dir : TcpBufferedSocket.t -> string -> file list -> unit -val new_string : DonkeyProtoClient.t -> string -> unit +val new_string : DonkeyProtoClient.t -> bytes -> unit val tag_file : file -> CommonTypes.tag list diff --git a/src/networks/donkey/donkeyProtoKademlia.ml b/src/networks/donkey/donkeyProtoKademlia.ml index a8da9b8c..b0cb041c 100644 --- a/src/networks/donkey/donkeyProtoKademlia.ml +++ b/src/networks/donkey/donkeyProtoKademlia.ml @@ -84,7 +84,7 @@ module P = struct ss.[14] <- s.[pos+13]; ss.[15] <- s.[pos+12]; - Md4.direct_of_string ss + Md4.direct_of_string @@ Bytes.unsafe_to_string ss let buf_md4 buf s = let s = Md4.direct_to_string s in @@ -112,7 +112,7 @@ module P = struct ss.[14] <- s.[pos+13]; ss.[15] <- s.[pos+12]; - Buffer.add_string buf ss + Buffer.add_bytes buf ss (* Strange: why was the IP format changed for Kademlia ? *) @@ -458,7 +458,7 @@ module P = struct end; *) - UdpSocket.write sock ping s ip port + UdpSocket.write sock ping (Bytes.unsafe_of_string s) ip port with | MessageNotImplemented -> () | e -> lprintf_nl "Exception %s in udp_send" (Printexc2.to_string e) @@ -476,7 +476,7 @@ module P = struct Ip.of_inet_addr inet, port | _ -> assert false in - let t = parse_message ip port pbuf in + let t = parse_message ip port (Bytes.unsafe_to_string pbuf) in let is_not_banned ip = match !Ip.banned (ip, None) with None -> true @@ -491,7 +491,7 @@ module P = struct begin lprintf_nl "Error %s in udp_handler, dump of packet:" (Printexc2.to_string e); - dump p.UdpSocket.udp_content; + dump (Bytes.unsafe_to_string p.UdpSocket.udp_content); lprint_newline () end ); diff --git a/src/networks/donkey/donkeyProtoOvernet.ml b/src/networks/donkey/donkeyProtoOvernet.ml index 6157c45d..3ff18328 100644 --- a/src/networks/donkey/donkeyProtoOvernet.ml +++ b/src/networks/donkey/donkeyProtoOvernet.ml @@ -387,7 +387,7 @@ module Proto = struct UdpSocket.READ_DONE -> UdpSocket.read_packets sock (fun p -> try - let pbuf = p.UdpSocket.udp_content in + let pbuf = Bytes.unsafe_to_string p.UdpSocket.udp_content in let len = String.length pbuf in if len < 2 || int_of_char pbuf.[0] <> 227 then @@ -420,7 +420,7 @@ module Proto = struct if !verbose_unknown_messages then begin lprintf_nl "Error %s in udp_handler, dump of packet:" (Printexc2.to_string e); - dump p.UdpSocket.udp_content; + dump (Bytes.unsafe_to_string p.UdpSocket.udp_content); lprint_newline () end ); @@ -447,7 +447,7 @@ module Proto = struct lprintf_nl "UDP to %s:%d op 0x%02X len %d type %s" (Ip.to_string ip) port (get_uint8 s 1) (String.length s) (message_to_string msg); end; - UdpSocket.write sock ping s ip port + UdpSocket.write sock ping (Bytes.unsafe_of_string s) ip port with e -> lprintf_nl "Exception %s in udp_send" (Printexc2.to_string e) diff --git a/src/networks/fasttrack/fasttrackNetwork.ml b/src/networks/fasttrack/fasttrackNetwork.ml index b792e58c..405dacfa 100644 --- a/src/networks/fasttrack/fasttrackNetwork.ml +++ b/src/networks/fasttrack/fasttrackNetwork.ml @@ -78,7 +78,7 @@ type file_uid = Md5Ext.t type file_uri = string external create_cipher : unit -> cipher = "ml_create_cipher" -external apply_cipher : cipher -> string -> int -> int -> unit +external apply_cipher : cipher -> bytes -> int -> int -> unit = "ml_apply_cipher" external init_cipher : cipher -> unit = "ml_init_cipher" external set_cipher : cipher -> int32 -> int -> unit = "ml_set_cipher" @@ -87,9 +87,9 @@ external get_cipher_from_packet : string -> int -> cipher -> unit external xor_ciphers : cipher -> cipher -> unit = "ml_xor_ciphers" external xor_ciphers2 : cipher -> cipher -> unit = "ml_xor_ciphers2" -external cipher_packet_set : cipher -> string -> int -> unit +external cipher_packet_set : cipher -> bytes -> int -> unit = "ml_cipher_packet_set" -external cipher_packet_set_xored : cipher -> string -> int -> cipher -> unit +external cipher_packet_set_xored : cipher -> bytes -> int -> cipher -> unit = "ml_cipher_packet_set_xored" external cipher_free : cipher -> unit = "ml_cipher_free" external cipher_enc_type : cipher -> int = "ml_cipher_enc_type" diff --git a/src/networks/fasttrack/fasttrackProto.ml b/src/networks/fasttrack/fasttrackProto.ml index 71057f5b..5d88d006 100644 --- a/src/networks/fasttrack/fasttrackProto.ml +++ b/src/networks/fasttrack/fasttrackProto.ml @@ -65,8 +65,9 @@ let ip_to_string ip = Ip.to_string ip let crypt_and_send sock out_cipher str = if !verbose_msg_raw || monitored sock then lprintf "crypt_and_send: to send [%s]\n" (String.escaped str); - let str = String.copy str in - apply_cipher out_cipher str 0 (String.length str); + let str = Bytes.of_string str in + apply_cipher out_cipher str 0 (Bytes.length str); + let str = Bytes.unsafe_to_string str in if !verbose_msg_raw || monitored sock then lprintf "crypt_and_send: [%s] sent\n" (String.escaped str); write_string sock str @@ -1800,7 +1801,7 @@ module UdpMessages = struct try let s = write msg in - UdpSocket.write t ping s ip port + UdpSocket.write t ping (Bytes.unsafe_of_string s) ip port with e -> lprintf "FT: Exception %s in udp_send\n" (Printexc2.to_string e) @@ -1888,13 +1889,13 @@ let check_primitives () = let cipher = create_cipher () in set_cipher cipher 123456789l 0x29; init_cipher cipher; - let s = String.make 12 '0' in + let s = Bytes.make 12 '0' in cipher_packet_set cipher s 0; - assert (s = "\007\091\205\021\110\233\135\1870000"); + assert (Bytes.to_string s = "\007\091\205\021\110\233\135\1870000"); (* lprintf "cipher_packet_set s = \"%s\"\n" (String.escaped s); *) - let s = "123456789abcdefghijklm\233\234\235" in - apply_cipher cipher s 0 (String.length s); - assert (s = "\016\210\245\241\144Ug\028Z\229\1928\176\167\192\008\139\019\018Z\1937\226\250i"); + let s = Bytes.of_string "123456789abcdefghijklm\233\234\235" in + apply_cipher cipher s 0 (Bytes.length s); + assert (Bytes.to_string s = "\016\210\245\241\144Ug\028Z\229\1928\176\167\192\008\139\019\018Z\1937\226\250i"); (* lprintf "apply_cipher s = \"%s\"\n" (String.escaped s); *) cipher_free cipher; with _ -> diff --git a/src/networks/fasttrack/fasttrackProtocol.ml b/src/networks/fasttrack/fasttrackProtocol.ml index fe64101f..a0ceb94d 100644 --- a/src/networks/fasttrack/fasttrackProtocol.ml +++ b/src/networks/fasttrack/fasttrackProtocol.ml @@ -87,13 +87,13 @@ let rec iter len n = s.[len] <- char_of_int (0x80 lor (Int64.to_int (Int64.logand n int64_7f))); s else - let s = String.create (len+1) in + let s = Bytes.create (len+1) in s.[len] <- char_of_int (Int64.to_int n); s let buf_dynint b data = let data = Int64.logand bits32 data in - Buffer.add_string b (iter 0 data) + Buffer.add_bytes b (iter 0 data) let buf_dynint b data = let data = Int64.logand bits32 data in @@ -116,7 +116,7 @@ let buf_dynint b data = (Int64.logand !data int64_7f))); data := Int64.shift_right_logical !data 7; done; - Buffer.add_string b (String.sub buf 0 len) + Buffer.add_string b (Bytes.sub_string buf 0 len) let dynint v = let b = Buffer.create 10 in diff --git a/src/networks/fasttrack/fasttrackServers.ml b/src/networks/fasttrack/fasttrackServers.ml index 556f11c0..04208ecd 100644 --- a/src/networks/fasttrack/fasttrackServers.ml +++ b/src/networks/fasttrack/fasttrackServers.ml @@ -92,12 +92,12 @@ let server_parse_after s gconn sock = let rec iter () = let len = b.len in if len > 0 then - let size = TcpMessages.packet_size ciphers b.buf b.pos b.len in + let size = TcpMessages.packet_size ciphers (Bytes.unsafe_to_string b.buf) b.pos b.len in match size with None -> () | Some size -> if len >= size then - let msg = String.sub b.buf b.pos size in + let msg = Bytes.sub_string b.buf b.pos size in buf_used b size; let addr, t = TcpMessages.parse ciphers msg in FasttrackHandler.server_msg_handler sock s addr t; @@ -129,13 +129,13 @@ let server_parse_netname s gconn sock = let start_pos = b.pos in let end_pos = start_pos + len in let buf = b.buf in - let net = String.sub buf start_pos len in + let net = Bytes.sub_string buf start_pos len in if !verbose_msg_raw then lprintf "net:[%s]\n" (String.escaped net); let rec iter pos = if pos < end_pos then - if buf.[pos] = '\000' then begin - let netname = String.sub buf start_pos (pos-start_pos) in + if Bytes.get buf pos = '\000' then begin + let netname = Bytes.sub_string buf start_pos (pos-start_pos) in if !verbose_msg_raw then lprintf "netname: [%s]\n" (String.escaped netname); buf_used b (pos-start_pos+1); @@ -159,7 +159,7 @@ let server_parse_cipher s gconn sock = | Some ciphers -> if !verbose_msg_raw then lprintf "Cipher received from server\n"; - get_cipher_from_packet b.buf b.pos ciphers.in_cipher; + get_cipher_from_packet (Bytes.unsafe_to_string b.buf) b.pos ciphers.in_cipher; init_cipher ciphers.in_cipher; xor_ciphers ciphers.out_cipher ciphers.in_cipher; @@ -241,7 +241,7 @@ let connect_server h = }; set_cipher out_cipher (client_cipher_seed ()) 0x29; - let s = String.create 12 in + let s = Bytes.create 12 in (match !connection_header_hook with None -> @@ -253,6 +253,8 @@ let connect_server h = cipher_packet_set out_cipher s 4; + let s = Bytes.unsafe_to_string s in + if !verbose_msg_raw then begin lprintf "SENDING %s\n" (String.escaped s); AnyEndian.dump s; diff --git a/src/networks/fileTP/fileTPFTP.ml b/src/networks/fileTP/fileTPFTP.ml index f5ba897b..08763f2b 100644 --- a/src/networks/fileTP/fileTPFTP.ml +++ b/src/networks/fileTP/fileTPFTP.ml @@ -79,7 +79,7 @@ end_pos !counter_pos b.len to_read; let old_downloaded = CommonSwarming.downloaded swarmer in - CommonSwarming.received up !counter_pos b.buf b.pos to_read_int; + CommonSwarming.received up !counter_pos (Bytes.unsafe_to_string b.buf) b.pos to_read_int; let new_downloaded = CommonSwarming.downloaded swarmer in c.client_total_downloaded <- c.client_total_downloaded ++ (new_downloaded -- old_downloaded); @@ -183,15 +183,15 @@ let ftp_send_range_request c (x,y) sock d = TcpBufferedSocket.set_reader sock (fun sock nread -> let b = TcpBufferedSocket.buf sock in if !verbose then - AnyEndian.dump_hex (String.sub b.buf b.pos b.len); + AnyEndian.dump_hex (Bytes.sub_string b.buf b.pos b.len); let rec iter i = if i < b.len then - if b.buf.[b.pos + i] = '\n' then begin - let slen = if i > 0 && b.buf.[b.pos + i - 1] = '\r' + if Bytes.get b.buf (b.pos + i) = '\n' then begin + let slen = if i > 0 && Bytes.get b.buf (b.pos + i - 1) = '\r' then i - 1 else i in - let line = String.sub b.buf b.pos slen in + let line = Bytes.sub_string b.buf b.pos slen in if !verbose then lprintf_nl "SRR LINE [%s]" line; buf_used b (i+1); if slen > 3 then begin @@ -382,16 +382,16 @@ let ftp_check_size file url start_download_file = TcpBufferedSocket.set_reader sock (fun sock nread -> let b = TcpBufferedSocket.buf sock in if !verbose then - AnyEndian.dump_hex (String.sub b.buf b.pos b.len); + AnyEndian.dump_hex (Bytes.sub_string b.buf b.pos b.len); let rec iter i = if i < b.len then - if b.buf.[b.pos + i] = '\n' then begin - let slen = if i > 0 && b.buf.[b.pos + i - 1] = '\r' + if Bytes.get b.buf (b.pos + i) = '\n' then begin + let slen = if i > 0 && Bytes.get b.buf (b.pos + i - 1) = '\r' then i - 1 else i in - let line = String.sub b.buf b.pos slen in + let line = Bytes.sub_string b.buf b.pos slen in if !verbose then lprintf_nl "CS LINE [%s]" line; buf_used b (i+1); if slen > 3 then begin diff --git a/src/networks/fileTP/fileTPHTTP.ml b/src/networks/fileTP/fileTPHTTP.ml index 62f3ac18..c4130f8c 100644 --- a/src/networks/fileTP/fileTPHTTP.ml +++ b/src/networks/fileTP/fileTPHTTP.ml @@ -264,7 +264,7 @@ let rec client_parse_header c gconn sock header = let old_downloaded = CommonSwarming.downloaded swarmer in - CommonSwarming.received up !counter_pos b.buf b.pos to_read_int; + CommonSwarming.received up !counter_pos (Bytes.unsafe_to_string b.buf) b.pos to_read_int; let new_downloaded = CommonSwarming.downloaded swarmer in c.client_total_downloaded <- c.client_total_downloaded ++ (new_downloaded -- old_downloaded); diff --git a/src/networks/fileTP/fileTPProtocol.ml b/src/networks/fileTP/fileTPProtocol.ml index 0cc46b8c..8879948c 100644 --- a/src/networks/fileTP/fileTPProtocol.ml +++ b/src/networks/fileTP/fileTPProtocol.ml @@ -48,12 +48,12 @@ let handlers info gconn = let begin_pos = b.pos in let rec iter i n_read = if i < end_pos then - if b.buf.[i] = '\r' then + if Bytes.get b.buf i = '\r' then iter (i+1) n_read else - if b.buf.[i] = '\n' then + if Bytes.get b.buf i = '\n' then if n_read then begin - let header = String.sub b.buf b.pos (i - b.pos) in + let header = Bytes.sub_string b.buf b.pos (i - b.pos) in (* if info then begin lprintf "HEADER : "; dump header; lprint_newline (); diff --git a/src/networks/fileTP/fileTPSSH.ml b/src/networks/fileTP/fileTPSSH.ml index a08184b5..6b93c992 100644 --- a/src/networks/fileTP/fileTPSSH.ml +++ b/src/networks/fileTP/fileTPSSH.ml @@ -182,14 +182,14 @@ let ssh_check_size file url start_download_file = close sock s); TcpBufferedSocket.set_reader sock (fun sock nread -> let b = TcpBufferedSocket.buf sock in - lprintf "SSH reader %d [%s]\n" nread (String.escaped (String.sub b.buf b.pos b.len)); + lprintf "SSH reader %d [%s]\n" nread (String.escaped (Bytes.sub_string b.buf b.pos b.len)); let rec iter i = if i < b.len then - if b.buf.[b.pos + i] = '\n' then begin - let slen = if i > 0 && b.buf.[b.pos + i - 1] = '\r' then + if Bytes.get b.buf (b.pos + i) = '\n' then begin + let slen = if i > 0 && Bytes.get b.buf (b.pos + i - 1) = '\r' then i - 1 else i in - let line = String.sub b.buf b.pos slen in + let line = Bytes.sub_string b.buf b.pos slen in lprintf "SSH LINE [%s]\n" line; buf_used b (i+1); if String2.starts_with line "[SIZE " then begin @@ -245,7 +245,7 @@ let ssh_connect token c f = if b.len >= elen then begin segment := SegmentX (file_num, pos, len, elen, - String.sub b.buf b.pos elen); + Bytes.sub_string b.buf b.pos elen); buf_used b elen; iter0 0 end @@ -255,11 +255,11 @@ let ssh_connect token c f = and iter0 i = if i < b.len then - if b.buf.[b.pos + i] = '\n' then begin - let slen = if i > 0 && b.buf.[b.pos + i - 1] = '\r' then + if Bytes.get b.buf (b.pos + i) = '\n' then begin + let slen = if i > 0 && Bytes.get b.buf (b.pos + i - 1) = '\r' then i - 1 else i in - let line = String.sub b.buf b.pos slen in + let line = Bytes.sub_string b.buf b.pos slen in (* lprintf "SSH LINE [%s]\n" line; *) buf_used b (i+1); diff --git a/src/networks/gnutella/gnutellaClients.ml b/src/networks/gnutella/gnutellaClients.ml index 733b1ab7..ab440993 100644 --- a/src/networks/gnutella/gnutellaClients.ml +++ b/src/networks/gnutella/gnutellaClients.ml @@ -215,7 +215,7 @@ save it on disk in the next version. *) end; let buf = Buffer.create 100 in let read_ttr counter_pos b to_read_int = - Buffer.add_substring buf b.buf b.pos to_read_int + Buffer.add_subbytes buf b.buf b.pos to_read_int in let read_more () = if !verbose_msg_clients then @@ -266,7 +266,7 @@ and read_some d c counter_pos b to_read_int = begin try CommonSwarming.received up - counter_pos b.buf b.pos to_read_int; + counter_pos (Bytes.unsafe_to_string b.buf) b.pos to_read_int; with e -> lprintf "FT: Exception %s in CommonSwarming.received\n" (Printexc2.to_string e); diff --git a/src/networks/gnutella/gnutellaFunctions.ml b/src/networks/gnutella/gnutellaFunctions.ml index ff83b287..09e6390c 100644 --- a/src/networks/gnutella/gnutellaFunctions.ml +++ b/src/networks/gnutella/gnutellaFunctions.ml @@ -87,7 +87,7 @@ let default_handler gconn sock = let b = buf sock in if !verbose then lprintf "HttpReader: Handler not found for [%s]\n" - (String.escaped (String.sub b.buf b.pos b.len)); + (String.escaped (Bytes.sub_string b.buf b.pos b.len)); close sock (Closed_for_error "not recognized"); failwith "Reply is not in the correct protocol" | Some f -> f gconn sock @@ -104,12 +104,12 @@ let handlers info gconn = let b = TcpBufferedSocket.buf sock in if monitored sock || !verbose_msg_raw then lprintf "iter_read %s :%d/%d\n%s\n" (Ip.to_string (peer_ip sock)) nread b.len - (String.escaped (String.sub b.buf b.pos b.len)); + (String.escaped (Bytes.sub_string b.buf b.pos b.len)); if b.len > 0 then match gconn.gconn_handler with | HttpReader (n, hs, default) -> if b.len >= n then - let head = String.sub b.buf b.pos n in + let head = Bytes.sub_string b.buf b.pos n in (try let rec iter hs = match hs with @@ -133,9 +133,9 @@ let handlers info gconn = | (proto, h) :: tail -> if String2.starts_with head proto then begin - let i = find_header_end b.buf b.pos b.len in + let i = find_header_end (Bytes.unsafe_to_string b.buf) b.pos b.len in if i <> 0 then - let header = String.sub b.buf b.pos (i - b.pos) in + let header = Bytes.sub_string b.buf b.pos (i - b.pos) in let first_line, headers = match Http_client.split_header header with [] -> "", [] @@ -197,17 +197,17 @@ let handlers info gconn = | CipherReader (cipher, h) -> if monitored sock || !verbose_msg_raw then lprintf "CipherReader %d: [%s]\n" nread - (String.escaped (String.sub b.buf b.pos b.len)); + (String.escaped (Bytes.sub_string b.buf b.pos b.len)); if nread > 0 then begin (* AnyEndian.dump_sub b.buf (b.pos + b.len - nread) nread; *) apply_cipher cipher b.buf (b.pos + b.len - nread) nread; log (LogReceive(peer_ip sock, peer_port sock, - String.sub b.buf (b.pos + b.len - nread) nread)); + Bytes.sub_string b.buf (b.pos + b.len - nread) nread)); if monitored sock || !verbose_msg_raw then lprintf " deciphered: [%s]\n" - (String.escaped (String.sub b.buf b.pos b.len)); + (String.escaped (Bytes.sub_string b.buf b.pos b.len)); end; let len = b.len in (try diff --git a/src/networks/gnutella/gnutellaMain.ml b/src/networks/gnutella/gnutellaMain.ml index f7e07e83..7d252f7c 100644 --- a/src/networks/gnutella/gnutellaMain.ml +++ b/src/networks/gnutella/gnutellaMain.ml @@ -74,7 +74,7 @@ let udp_handler sock event = in (* lprintf "Gnutella: calling udp_client_handler %s:%d\n" (Ip.to_string ip) port; *) - let buf = p.UdpSocket.udp_content in + let buf = Bytes.unsafe_to_string p.UdpSocket.udp_content in GnutellaHandler.udp_client_handler ip port buf with e -> lprintf "Error %s in udp_handler\n" diff --git a/src/networks/gnutella/gnutellaTypes.ml b/src/networks/gnutella/gnutellaTypes.ml index c9ee3fbe..8eda3461 100644 --- a/src/networks/gnutella/gnutellaTypes.ml +++ b/src/networks/gnutella/gnutellaTypes.ml @@ -100,7 +100,7 @@ and client = { and upload_client = { uc_sock : TcpBufferedSocket.t; uc_partial : bool; - uc_reader : (int64 -> string -> int -> int -> unit); + uc_reader : (int64 -> bytes -> int -> int -> unit); mutable uc_chunk_pos : int64; uc_chunk_len : int64; uc_chunk_end : int64; diff --git a/src/utils/cdk/bzip2.ml b/src/utils/cdk/bzip2.ml index 07ca4192..89b4e1dd 100644 --- a/src/utils/cdk/bzip2.ml +++ b/src/utils/cdk/bzip2.ml @@ -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; @@ -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; @@ -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 @@ -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 @@ -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) @@ -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; @@ -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; @@ -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) -> @@ -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 = @@ -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; diff --git a/src/utils/cdk/bzip2.mli b/src/utils/cdk/bzip2.mli index 40ddf405..01969973 100644 --- a/src/utils/cdk/bzip2.mli +++ b/src/utils/cdk/bzip2.mli @@ -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. @@ -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]. *) @@ -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]. diff --git a/src/utils/cdk/bzlib.ml b/src/utils/cdk/bzlib.ml index ff17e9ec..459e4ac1 100644 --- a/src/utils/cdk/bzlib.ml +++ b/src/utils/cdk/bzlib.ml @@ -23,7 +23,7 @@ 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" @@ -31,7 +31,7 @@ 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" diff --git a/src/utils/cdk/bzlib.mli b/src/utils/cdk/bzlib.mli index 74f6d09f..12945b71 100644 --- a/src/utils/cdk/bzlib.mli +++ b/src/utils/cdk/bzlib.mli @@ -10,7 +10,7 @@ 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" @@ -18,7 +18,7 @@ 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" diff --git a/src/utils/cdk/file.ml b/src/utils/cdk/file.ml index c25e50d1..b63ce211 100644 --- a/src/utils/cdk/file.ml +++ b/src/utils/cdk/file.ml @@ -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 = diff --git a/src/utils/cdk/filename2.ml b/src/utils/cdk/filename2.ml index 38032f2f..3b3e49f1 100644 --- a/src/utils/cdk/filename2.ml +++ b/src/utils/cdk/filename2.ml @@ -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 '/' @@ -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 = diff --git a/src/utils/cdk/genlex2.ml b/src/utils/cdk/genlex2.ml index de90afe4..dc211dc0 100644 --- a/src/utils/cdk/genlex2.ml +++ b/src/utils/cdk/genlex2.ml @@ -28,7 +28,7 @@ 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 @@ -36,16 +36,16 @@ 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 *) diff --git a/src/utils/cdk/gzip.ml b/src/utils/cdk/gzip.ml index d170263b..239ece75 100644 --- a/src/utils/cdk/gzip.ml +++ b/src/utils/cdk/gzip.ml @@ -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 *) @@ -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 @@ -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"; @@ -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 diff --git a/src/utils/cdk/string2.ml b/src/utils/cdk/string2.ml index f51028a7..84f20643 100644 --- a/src/utils/cdk/string2.ml +++ b/src/utils/cdk/string2.ml @@ -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' diff --git a/src/utils/cdk/string2.mli b/src/utils/cdk/string2.mli index b363716c..854c5b1c 100644 --- a/src/utils/cdk/string2.mli +++ b/src/utils/cdk/string2.mli @@ -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 diff --git a/src/utils/cdk/tar.mlcpp b/src/utils/cdk/tar.mlcpp index 2c2f2b6a..193a5486 100644 --- a/src/utils/cdk/tar.mlcpp +++ b/src/utils/cdk/tar.mlcpp @@ -205,9 +205,13 @@ let read_oldgnu_header header = t_realsize = extract_int32 header 483 12; } +let really_input_string t size = + let buf = Bytes.create size in + t.chan#really_input buf 0 size; + Bytes.unsafe_to_string buf + let read_gnu_header t = - let buf = String.create blocksize in - t.chan#really_input buf 0 blocksize; + let buf = really_input_string t blocksize in { t_atime = extract_int32 buf 0 12; t_ctime = extract_int32 buf 12 12; t_offset = extract_int32 buf 24 12; @@ -215,9 +219,8 @@ let read_gnu_header t = } let read_header t = - align_at_header t; - let buf = String.create blocksize in - t.chan#really_input buf 0 blocksize; + align_at_header t; + let buf = really_input_string t blocksize in if buf = empty_block then raise End_of_file; let head1 = { t_name = c_string buf 0; t_mode = extract_num buf 100 8; @@ -264,14 +267,11 @@ let read_body t = let header = get_header t in t.last_header <- None; if header.t_size = 0 then "" - else let buf = String.create header.t_size in - t.chan#really_input buf 0 header.t_size; + else + let buf = really_input_string t header.t_size in let align = blocksize - (header.t_size mod blocksize) in - if align <> blocksize then begin - let leftover = String.create blocksize in - t.chan#really_input leftover 0 align - end; - buf + if align <> blocksize then ignore (really_input_string t align : string); + buf let read_entry t = let head = read_header t in @@ -390,7 +390,7 @@ let write_gnu_header t buf = let output t head body = let size = String.length body in - let buf = String.copy empty_block in + let buf = Bytes.of_string empty_block in write_str buf 0 100 head.t_name; write_num8 buf 100 head.t_mode; write_num8 buf 108 head.t_uid; @@ -407,23 +407,23 @@ let output t head body = write_str buf 345 155 head.t_prefix; if head.t_format = OLDGNU_FORMAT then write_oldgnu_header head buf; - let chksum = compute_chksum buf in + let chksum = compute_chksum @@ Bytes.unsafe_to_string buf in write_padded_num buf 148 chksum; t.ochan#output buf 0 blocksize; - if head.t_format = GNU_FORMAT && isdigit buf.[156] then begin - let buf2 = String.copy empty_block in + if head.t_format = GNU_FORMAT && isdigit @@ Bytes.get buf 156 then begin + let buf2 = Bytes.of_string empty_block in write_gnu_header head buf2; t.ochan#output buf2 0 blocksize end; if size > 0 then begin - t.ochan#output body 0 size; + t.ochan#output (Bytes.unsafe_of_string body) 0 size; let align = blocksize - (size mod blocksize) in if align > 0 && align < blocksize then - t.ochan#output empty_block 0 align + t.ochan#output (Bytes.unsafe_of_string empty_block) 0 align end let flush t = - t.ochan#output empty_block 0 blocksize; + t.ochan#output (Bytes.unsafe_of_string empty_block) 0 blocksize; t.ochan#flush () let close_out t = diff --git a/src/utils/cdk/zlib2.ml b/src/utils/cdk/zlib2.ml index d2d25abf..5e7d1170 100644 --- a/src/utils/cdk/zlib2.ml +++ b/src/utils/cdk/zlib2.ml @@ -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 @@ -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 = @@ -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 @@ -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 diff --git a/src/utils/lib/bitv.ml b/src/utils/lib/bitv.ml index 707a06d3..2f82fda4 100644 --- a/src/utils/lib/bitv.ml +++ b/src/utils/lib/bitv.ml @@ -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) diff --git a/src/utils/lib/magiclib.ml b/src/utils/lib/magiclib.ml index b8e19b2b..392b712f 100644 --- a/src/utils/lib/magiclib.ml +++ b/src/utils/lib/magiclib.ml @@ -69,13 +69,13 @@ let escape_colon s = let n = ref 0 in for i = 0 to len - 1 do if s.[i] = ':' then incr n done; if !n = 0 then s else - let s' = String.create (len + !n) in + let s' = Bytes.create (len + !n) in let j = ref 0 in for i = 0 to len - 1 do if s.[i] = ':' then (s'.[!j] <- '\\'; incr j); s'.[!j] <- s.[i]; incr j done; - s' + Bytes.unsafe_to_string s' (* Concatenate the filenames with ":". If ":" is present in a filename, escape it. *) diff --git a/src/utils/lib/md4.ml b/src/utils/lib/md4.ml index 40bb0282..146af068 100644 --- a/src/utils/lib/md4.ml +++ b/src/utils/lib/md4.ml @@ -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 @@ -54,7 +54,7 @@ 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 ( @@ -62,7 +62,7 @@ module Base16 = struct 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 @@ -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 @@ -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 @@ -103,32 +103,32 @@ module Base32 = struct 'A' .. 'Z' -> int_of_char n - 65 | 'a' .. 'z' -> int_of_char n - 97 | _ -> (int_of_char n+26)-50 - + let of_string hash_length r = let len = String.length r in assert (len = (hash_length * 8 + 4)/5); - let s = String.make hash_length '\000' in + let s = Bytes.make hash_length '\000' in for i = 0 to len - 1 do let pos = i * 5 in let byte = pos / 8 in let bit = pos mod 8 in let c = int5_of_char r.[i] in - if bit < 3 then + if bit < 3 then let x = c lsl (3-bit) in - s.[byte] <- char_of_int (int_of_char s.[byte] lor x); + s.[byte] <- char_of_int (int_of_char (Bytes.get s byte) lor x); else let x = (c lsr (bit - 3)) land 0xff in - s.[byte] <- char_of_int (int_of_char s.[byte] lor x); + s.[byte] <- char_of_int (int_of_char (Bytes.get s byte) lor x); if byte+1 < hash_length then let y = (c lsl (11 - bit)) land 0xff in - s.[byte+1] <- char_of_int (int_of_char s.[byte+1] lor y); + s.[byte+1] <- char_of_int (int_of_char (Bytes.get s (byte+1)) lor y); done; - s - + Bytes.unsafe_to_string s + let to_string 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 @@ -145,16 +145,16 @@ module Base32 = struct let c = (x lsr (11 - bit)) land 0x1f in r.[i] <- char_of_int5 c done; - r + Bytes.unsafe_to_string r let char_of_int5 upper n = char_of_int (if n < 26 then (if upper then 65 else 97)+n else 50+(n-26)) - + 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 @@ -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 @@ -181,7 +181,7 @@ module Base6427 = struct let _ = assert (String.length base64tbl = 64) let to_string _ hashbin = - let hash64 = String.create 30 in + let hash64 = Bytes.create 30 in let hashbin n = int_of_char hashbin.[n] in hash64.[0] <- '='; let j = ref 1 in @@ -198,16 +198,17 @@ module Base6427 = struct done done; hash64.[!j-1] <- '='; - String.sub hash64 0 !j + Bytes.sub_string hash64 0 !j - let base64tbl_inv = String.create 126 - let _ = + let base64tbl_inv = + let table = Bytes.create 126 in for i = 0 to 63 do - base64tbl_inv.[int_of_char base64tbl.[i]] <- char_of_int i - done + table.[int_of_char base64tbl.[i]] <- char_of_int i + done; + Bytes.unsafe_to_string table let of_string _ hash64 = - let hashbin = String.make 20 '\000' in + let hashbin = Bytes.make 20 '\000' in let hash64 n = let c = hash64.[n] in int_of_char base64tbl_inv.[int_of_char c] @@ -233,7 +234,7 @@ module Base6427 = struct hashbin.[!j+1] <- char_of_int ((!tmp lsr 8) land 0xff); j := !j + 2; done; - hashbin + Bytes.unsafe_to_string hashbin let to_string_case _ = to_string end @@ -291,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 @@ -314,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 @@ -329,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.make hash_length '\x00' 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 @@ -397,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 @@ -409,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 @@ -421,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 @@ -464,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 = @@ -481,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 = @@ -530,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" diff --git a/src/utils/lib/md4.mli b/src/utils/lib/md4.mli index 64cee459..b15de4d9 100644 --- a/src/utils/lib/md4.mli +++ b/src/utils/lib/md4.mli @@ -80,4 +80,4 @@ module Md5Ext : Digest module Base16 : Base module Base32 : Base module Base6427 : Base - \ No newline at end of file + diff --git a/src/utils/lib/misc.ml b/src/utils/lib/misc.ml index b670e1e4..bfa7293b 100644 --- a/src/utils/lib/misc.ml +++ b/src/utils/lib/misc.ml @@ -92,13 +92,13 @@ let zip_create zipfile files = let gz_extract filename = let file = ref "" in try - let buffer = String.create 4096 in + let buffer = Bytes.create 4096 in let file_out = Filename2.temp_file "arch_" ".tmp" in file := file_out; Unix2.tryopen_read_gzip filename (fun ic -> Unix2.tryopen_write_bin file_out (fun oc -> let rec decompress () = - let n = Gzip.input ic buffer 0 (String.length buffer) in + let n = Gzip.input ic buffer 0 (Bytes.length buffer) in if n = 0 then () else begin diff --git a/src/utils/lib/misc2.mlcpp b/src/utils/lib/misc2.mlcpp index 9ba2f447..6f60f623 100644 --- a/src/utils/lib/misc2.mlcpp +++ b/src/utils/lib/misc2.mlcpp @@ -26,14 +26,14 @@ let bz2_extract filename = else begin let file = ref "" in try - let buffer = String.create 4096 in + let buffer = Bytes.create 4096 in let file_out = Filename2.temp_file "arch_" ".tmp" in file := file_out; let ic = Bzip2.open_in filename in let oc = open_out_bin file_out in let rec decompress () = - let n = Bzip2.input ic buffer 0 (String.length buffer) in + let n = Bzip2.input ic buffer 0 (Bytes.length buffer) in if n = 0 then () else begin diff --git a/src/utils/lib/options.ml4 b/src/utils/lib/options.ml4 index 823372bf..c18137ff 100644 --- a/src/utils/lib/options.ml4 +++ b/src/utils/lib/options.ml4 @@ -208,7 +208,7 @@ and parse_option = parser | [< 'Int i >] -> IntValue i | [< 'Float f >] -> FloatValue f | [< 'Kwd "@"; 'Int i; v = parse_once_value i >] -> OnceValue v -| [< 'Char c >] -> StringValue (let s = String.create 1 in s.[0] <- c; s) +| [< 'Char c >] -> StringValue (String.make 1 c) | [< 'Kwd "["; v = parse_list [] >] -> List v | [< 'Kwd "("; v = parse_list [] >] -> List v diff --git a/src/utils/lib/unix32.ml b/src/utils/lib/unix32.ml index 044ba7c9..6ba7e089 100644 --- a/src/utils/lib/unix32.ml +++ b/src/utils/lib/unix32.ml @@ -327,13 +327,13 @@ module FDCache = struct check_destroyed t2; let buffer_len = 128 * 1024 in let buffer_len64 = Int64.of_int buffer_len in - let buffer = String.make buffer_len '\001' in + let buffer = Bytes.make buffer_len '\001' in let rec iter remaining pos1 pos2 = let len64 = min remaining buffer_len64 in let len = Int64.to_int len64 in if len > 0 then begin read t1 pos1 buffer 0 len; - write t2 pos2 buffer 0 len; + write t2 pos2 (Bytes.unsafe_to_string buffer) 0 len; iter (remaining -- len64) (pos1 ++ len64) (pos2 ++ len64) end in @@ -363,7 +363,7 @@ module type File = sig val mtime64 : t -> float val exists : t -> bool val remove : t -> unit - val read : t -> int64 -> string -> int -> int -> unit + val read : t -> int64 -> bytes -> int -> int -> unit val write : t -> int64 -> string -> int -> int -> unit val destroy : t -> unit val is_closed : t -> bool @@ -1216,6 +1216,8 @@ let write file file_pos string string_pos len = | Destroyed -> failwith "Unix32.write on destroyed FD" else lprintf_nl "Unix32.write: error, invalid argument len = 0" + +let write_bytes f fpos b bpos len = write f fpos (Bytes.unsafe_to_string b) bpos len let buffer = Buffer.create 65000 @@ -1349,12 +1351,12 @@ let copy_chunk t1 t2 pos1 pos2 len = flush_fd t1; flush_fd t2; let buffer_size = 128 * 1024 in - let buffer = String.make buffer_size '\001' in + let buffer = Bytes.make buffer_size '\001' in let rec iter remaining pos1 pos2 = let len = mini remaining buffer_size in if len > 0 then begin read t1 pos1 buffer 0 len; - write t2 pos2 buffer 0 len; + write t2 pos2 (Bytes.unsafe_to_string buffer) 0 len; let len64 = Int64.of_int len in iter (remaining - len) (pos1 ++ len64) (pos2 ++ len64) end diff --git a/src/utils/lib/unix32.mli b/src/utils/lib/unix32.mli index 026362db..2145f18b 100644 --- a/src/utils/lib/unix32.mli +++ b/src/utils/lib/unix32.mli @@ -55,10 +55,11 @@ val flush_fd : t -> unit val buffered_write : t -> int64 -> string -> int -> int -> unit val buffered_write_copy : t -> int64 -> string -> int -> int -> unit val write : t -> int64 -> string -> int -> int -> unit +val write_bytes : t -> int64 -> bytes -> int -> int -> unit val max_buffered : int64 ref val remove : t -> unit -val read : t -> int64 -> string -> int -> int -> unit +val read : t -> int64 -> bytes -> int -> int -> unit (*val allocate_chunk : t -> int64 -> int -> unit*) val copy_chunk : t -> t -> int64 -> int64 -> int -> unit diff --git a/src/utils/lib/url.ml b/src/utils/lib/url.ml index 03ff7e6c..b7bdb6da 100644 --- a/src/utils/lib/url.ml +++ b/src/utils/lib/url.ml @@ -36,7 +36,7 @@ type url = { let encode s = let pos = ref 0 in let len = String.length s in - let res = String.create (3*len) in + let res = Bytes.create (3*len) in let hexa_digit x = if x >= 10 then Char.chr (Char.code 'A' + x - 10) else Char.chr (Char.code '0' + x) in @@ -50,7 +50,7 @@ let encode s = res.[!pos+2] <- hexa_digit (Char.code c mod 16); pos := !pos + 3 done; - String.sub res 0 !pos + Bytes.sub_string res 0 !pos (** decodes a sting according RFC 1738 or x-www-form-urlencoded ('+' with ' ') @@ -304,4 +304,4 @@ open Options let option = define_option_class "URL" (fun v -> of_string (value_to_string v)) - (fun url -> string_to_value (to_string url)) \ No newline at end of file + (fun url -> string_to_value (to_string url)) diff --git a/src/utils/mp3tagui/mp3_info.ml b/src/utils/mp3tagui/mp3_info.ml index 6d64df9a..0cc2ad99 100644 --- a/src/utils/mp3tagui/mp3_info.ml +++ b/src/utils/mp3tagui/mp3_info.ml @@ -78,8 +78,7 @@ let get_xing_header ic header = then if mode <> 3 then 32 else 17 else if mode <> 3 then 17 else 9 in seek_in ic (pos_in ic + offset); - let buf = String.create 4 in - really_input ic buf 0 4; + let buf = really_input_string ic 4 in if buf <> "Xing" then raise Not_found; let flags = read_i4 ic in (* 3 = FRAMES_FLAG | BYTES_FLAG *) @@ -90,12 +89,13 @@ let get_xing_header ic header = let for_channel ic = seek_in ic 0; - let buf = String.create 4 in + let buf = Bytes.create 4 in really_input ic buf 0 4; - while not (check_head buf) do - String.blit buf 1 buf 0 3; + while not (check_head @@ Bytes.unsafe_to_string buf) do + Bytes.blit buf 1 buf 0 3; buf.[3] <- input_char ic done; + let buf = Bytes.unsafe_to_string buf in let header = (Char.code buf.[1] lsl 16) lor (Char.code buf.[2] lsl 8) lor diff --git a/src/utils/mp3tagui/mp3_tag.ml b/src/utils/mp3tagui/mp3_tag.ml index f5dd6bbc..c19abecd 100644 --- a/src/utils/mp3tagui/mp3_tag.ml +++ b/src/utils/mp3tagui/mp3_tag.ml @@ -46,9 +46,7 @@ module Id3v1 = let res = if len < 128 then false else begin seek_in ic (len - 128); - let buffer = String.create 3 in - really_input ic buffer 0 3; - buffer = "TAG" + really_input_string ic 3 = "TAG" end in close_in ic; res @@ -57,10 +55,7 @@ let read_channel ic = let len = in_channel_length ic in if len < 128 then raise Not_found; seek_in ic (len - 128); - let readstring len = - let buf = String.create len in - really_input ic buf 0 len; - Mp3_misc.chop_whitespace buf 0 in + let readstring len = Mp3_misc.chop_whitespace (really_input_string ic len) 0 in if readstring 3 <> "TAG" then raise Not_found; let title = readstring 30 in let artist = readstring 30 in @@ -150,7 +145,7 @@ module Id3v2 = struct for i = 0 to len - 1 do buff.[i] <- Char.chr (input_byte ic) done; - buff + Bytes.unsafe_to_string buff let input_int4 ic = let b4 = input_byte ic in let b3 = input_byte ic in @@ -188,8 +183,7 @@ module Id3v2 = struct let read_channel ic = try - let header = String.create 10 in - really_input ic header 0 10; + let header = really_input_string ic 10 in if not (valid_header header) then raise Not_found; let len = length_header header in let startpos = pos_in ic in @@ -280,16 +274,15 @@ module Id3v2 = struct let ic = open_in_bin filename in try begin try - let header = String.create 10 in - really_input ic header 0 10; + let header = really_input_string ic 10 in if not (valid_header header) then raise Not_found; seek_in ic (pos_in ic + length_header header) with Not_found | End_of_file -> seek_in ic 0 end; - let buffer = String.create 4096 in + let buffer = Bytes.create 4096 in let rec copy_file () = - let n = input ic buffer 0 (String.length buffer) in + let n = input ic buffer 0 (Bytes.length buffer) in if n = 0 then () else begin output oc buffer 0 n; copy_file () end in copy_file (); close_in ic diff --git a/src/utils/net/base64.ml b/src/utils/net/base64.ml index d3d30420..6f788345 100644 --- a/src/utils/net/base64.ml +++ b/src/utils/net/base64.ml @@ -57,7 +57,7 @@ let encode_with_options b64 equal s pos len linelen crlf = in (* l_t': length of the result with CRLF or LF characters *) - let t = String.make l_t' equal in + let t = Bytes.make l_t' equal in let j = ref 0 in let q = ref 0 in for k = 0 to len / 3 - 1 do @@ -75,10 +75,10 @@ let encode_with_options b64 equal s pos len linelen crlf = (Char.code (String.unsafe_get s (p+2))) in (* Obviously, 'bits' is a 24 bit entity (i.e. bits < 2**24) *) assert(!j + 3 < l_t'); - String.unsafe_set t !j (Array.unsafe_get b64 ( bits lsr 18)); - String.unsafe_set t (!j+1) (Array.unsafe_get b64 ((bits lsr 12) land 63)); - String.unsafe_set t (!j+2) (Array.unsafe_get b64 ((bits lsr 6) land 63)); - String.unsafe_set t (!j+3) (Array.unsafe_get b64 ( bits land 63)); + Bytes.unsafe_set t !j (Array.unsafe_get b64 ( bits lsr 18)); + Bytes.unsafe_set t (!j+1) (Array.unsafe_get b64 ((bits lsr 12) land 63)); + Bytes.unsafe_set t (!j+2) (Array.unsafe_get b64 ((bits lsr 6) land 63)); + Bytes.unsafe_set t (!j+3) (Array.unsafe_get b64 ( bits land 63)); j := !j + 4; if linelen > 3 then begin q := !q + 4; @@ -135,7 +135,7 @@ let encode_with_options b64 equal s pos len linelen crlf = end; end; - t ;; + Bytes.unsafe_to_string t @@ -207,7 +207,7 @@ let decode_substring t ~pos ~len ~url_variant:p_url ~accept_spaces:p_spaces = in let l_s = (l_t / 4) * 3 - pad_chars in (* sic! *) - let s = String.create l_s in + let s = Bytes.create l_s in let decode_char c = match c with @@ -248,9 +248,9 @@ let decode_substring t ~pos ~len ~url_variant:p_url ~accept_spaces:p_spaces = let x0 = (n0 lsl 2) lor (n1 lsr 4) in let x1 = ((n1 lsl 4) land 0xf0) lor (n2 lsr 2) in let x2 = ((n2 lsl 6) land 0xc0) lor n3 in - String.unsafe_set s q (Char.chr x0); - String.unsafe_set s (q+1) (Char.chr x1); - String.unsafe_set s (q+2) (Char.chr x2); + Bytes.unsafe_set s q (Char.chr x0); + Bytes.unsafe_set s (q+1) (Char.chr x1); + Bytes.unsafe_set s (q+2) (Char.chr x2); done; end else begin @@ -269,9 +269,9 @@ let decode_substring t ~pos ~len ~url_variant:p_url ~accept_spaces:p_spaces = let x0 = (n0 lsl 2) lor (n1 lsr 4) in let x1 = ((n1 lsl 4) land 0xf0) lor (n2 lsr 2) in let x2 = ((n2 lsl 6) land 0xc0) lor n3 in - String.unsafe_set s q (Char.chr x0); - String.unsafe_set s (q+1) (Char.chr x1); - String.unsafe_set s (q+2) (Char.chr x2); + Bytes.unsafe_set s q (Char.chr x0); + Bytes.unsafe_set s (q+1) (Char.chr x1); + Bytes.unsafe_set s (q+2) (Char.chr x2); done; cursor := pos + l_t - 4; end; @@ -316,7 +316,7 @@ let decode_substring t ~pos ~len ~url_variant:p_url ~accept_spaces:p_spaces = end; - s ;; + Bytes.unsafe_to_string s ;; diff --git a/src/utils/net/cobs.ml b/src/utils/net/cobs.ml index 6f6fd619..6441343c 100644 --- a/src/utils/net/cobs.ml +++ b/src/utils/net/cobs.ml @@ -98,16 +98,16 @@ let encodeData pdest psrc srclen = let decode psrc = let srclen = String.length psrc in let dstlen = calcDecodedLength psrc srclen in - let pdest = String.create dstlen in + let pdest = Bytes.create dstlen in decodeData pdest psrc srclen; - pdest + Bytes.unsafe_to_string pdest let encode psrc = let srclen = String.length psrc in let dstlen = calcEncodedLength psrc srclen in - let pdest = String.create dstlen in + let pdest = Bytes.create dstlen in encodeData pdest psrc srclen; - pdest + Bytes.unsafe_to_string pdest (* ggep: @@ -352,17 +352,17 @@ let write buf list = String.make 1 (char_of_int up) else if up land 0xffff = up then - let s = String.create 2 in + let s = Bytes.create 2 in LittleEndian.str_int16 s 0 up; - s + Bytes.unsafe_to_string s else if up land 0xffffff = up then - let s = String.create 3 in + let s = Bytes.create 3 in LittleEndian.str_int24 s 0 up; - s + Bytes.unsafe_to_string s else - let s = String.create 4 in + let s = Bytes.create 4 in LittleEndian.str_int s 0 up; - s + Bytes.unsafe_to_string s in GGEP.GGEP ("DU", s) ) list @@ -384,4 +384,4 @@ let print list = | GGEP_DU_uptime up -> lprintf " Uptime %d seconds\n" up ) list - \ No newline at end of file + diff --git a/src/utils/net/http_server.ml b/src/utils/net/http_server.ml index 4cd2c5b6..e8adad69 100644 --- a/src/utils/net/http_server.ml +++ b/src/utils/net/http_server.ml @@ -64,7 +64,7 @@ let decode64 s = | _ -> failwith "not a base64 string" in let len = String.length s in let len_res = len * 3 / 4 in - let res = String.create len_res in + let res = Bytes.create len_res in for i=0 to len/4 - 1 do let i1 = 4*i and i2 = 3*i in let v1 = (val64 s.[i1]) lsl 18 in @@ -80,7 +80,7 @@ let decode64 s = if s.[len-1] = '=' then if s.[len-2] = '=' then 2 else 1 else 0 in - String.sub res 0 (len_res - nb_cut) + Bytes.sub_string res 0 (len_res - nb_cut) let debug = ref false @@ -793,17 +793,17 @@ let request_handler config sock nread = let rec iter i = let end_pos = b.pos + b.len in if i < end_pos then - if b.buf.[i] = '\n' && i <= end_pos - 2 then - let c = b.buf.[i+1] in + if Bytes.get b.buf i = '\n' && i <= end_pos - 2 then + let c = Bytes.get b.buf (i+1) in if c = '\n' then let len = i + 2 - b.pos in - let header = String.sub b.buf b.pos len in + let header = Bytes.sub_string b.buf b.pos len in buf_used b len; manage config sock header else - if c = '\r' && i <= end_pos - 3 && b.buf.[i+2] = '\n' then + if c = '\r' && i <= end_pos - 3 && Bytes.get b.buf (i+2) = '\n' then let len = i + 3 - b.pos in - let header = String.sub b.buf b.pos len in + let header = Bytes.sub_string b.buf b.pos len in buf_used b len; manage config sock header else diff --git a/src/utils/net/mailer.ml b/src/utils/net/mailer.ml index f22bf0c2..7de49172 100644 --- a/src/utils/net/mailer.ml +++ b/src/utils/net/mailer.ml @@ -144,11 +144,11 @@ let canon_addr s = let string_xor s1 s2 = assert (String.length s1 = String.length s2); - let s = String.create (String.length s1) in - for i = 0 to String.length s - 1 do + let s = Bytes.create (String.length s1) in + for i = 0 to Bytes.length s - 1 do s.[i] <- Char.chr (Char.code s1.[i] lxor Char.code s2.[i]); done; - s + Bytes.unsafe_to_string s (* HMAC-MD5, RFC 2104 *) let hmac_md5 = @@ -157,8 +157,9 @@ let hmac_md5 = let md5 s = Md5.direct_to_string (Md5.string s) in fun secret challenge -> let secret = if String.length secret > 64 then md5 secret else secret in - let k = String.make 64 '\x00' in + let k = Bytes.make 64 '\x00' in String.blit secret 0 k 0 (String.length secret); + let k = Bytes.unsafe_to_string k in md5 (string_xor k opad ^ md5 (string_xor k ipad ^ challenge)) let sendmail smtp_server smtp_port new_style mail = diff --git a/src/utils/net/tcpBufferedSocket.ml b/src/utils/net/tcpBufferedSocket.ml index 6f8b8767..1e042df4 100644 --- a/src/utils/net/tcpBufferedSocket.ml +++ b/src/utils/net/tcpBufferedSocket.ml @@ -1618,7 +1618,8 @@ let write t s pos len = _to_deflate t; buf_add t wbuf s pos len -let write_string t s = write t (Bytes.of_string s) 0 (String.length s) +let write_bytes t s = write t s 0 (Bytes.length s) +let write_string t s = write_bytes t (Bytes.unsafe_of_string s) (*************************************************************************) (* *) diff --git a/src/utils/net/tcpBufferedSocket.mli b/src/utils/net/tcpBufferedSocket.mli index 3a2ffed4..5e45a0c5 100644 --- a/src/utils/net/tcpBufferedSocket.mli +++ b/src/utils/net/tcpBufferedSocket.mli @@ -67,6 +67,7 @@ val set_refill : t -> (t -> unit) -> unit val set_rtimer : t -> (t -> unit) -> unit val write: t -> bytes -> int -> int -> unit val write_string: t -> string -> unit +val write_bytes: t -> bytes -> unit val connect: token -> string -> Unix.inet_addr -> int -> handler -> t val close : t -> BasicSocket.close_reason -> unit val closed : t -> bool