open Printf let iterations = 10;; (*========================================================================================*) let get_triple str inoff = (*1*) let bitoff = (inoff land 7) (*2*) and bitind = (inoff lsr 3) (*3*) in (if bitoff < 6 (*4*) then ((int_of_char (str.[bitind])) lsr (5-bitoff)) (*5*) else ((int_of_char (str.[bitind])) lsl (bitoff-5)) lor (*6*) ((int_of_char (str.[bitind+1])) lsr (13-bitoff))) land 7 ;; (*7*) let put_bytetriple str index bytetriple = (*8*) (str.[index] <- char_of_int ((bytetriple lsr 16) land 255) ; (*9*) str.[index+1] <- char_of_int ((bytetriple lsr 8) land 255); (*10*) str.[index+2] <- char_of_int (bytetriple land 255));; (*11*) let get_needed_bytes shifts = (*12*) if shifts <3 then 0 (*13*) else if shifts <6 then 1 (*14*) else 2;; (*15*) let make_result list0 endpiece shifts = (*16*) let endpiecesize = get_needed_bytes shifts (*17*) and triplebytesize = ((List.length list0) * 3) (*18*) in let outstr = String.create (triplebytesize + endpiecesize) (*19*) in ((match endpiecesize with (*20*) 0 -> () (*21*) | 1 -> outstr.[triplebytesize] <- (*22*) char_of_int ((endpiece lsr ((shifts*3)-8)) land 255) (*23*) | 2 -> (outstr.[triplebytesize] <- (*24*) char_of_int ((endpiece lsr ((shifts*3)-8)) land 255); (*25*) outstr.[triplebytesize+1] <- (*26*) char_of_int ((endpiece lsr ((shifts*3)-16)) land 255))); (*27*) let rec put_list_in_string list index = (*28*) match list with (*29*) bytetriple::rest -> (put_bytetriple outstr index bytetriple; (*30*) put_list_in_string rest (index-3)) (*31*) | [] -> outstr (*32*) in (put_list_in_string list0 (triplebytesize-3), (triplebytesize*8)+(shifts*3)));;(*33*) let drop_0xx str len = (*34*) let rec do_drop_0xx str len inoff shiftreg shifts acc = (*35*) let ninoff = inoff+3 (*36*) in if ninoff>len then (*37*) make_result acc shiftreg shifts (*38*) else let triple = get_triple str inoff (*39*) in if triple >= 4 (*40*) then if shifts == 7 (*41*) then do_drop_0xx str len ninoff 0 0 (((shiftreg lsl 3) lor triple)::acc) (*42*) else do_drop_0xx str len ninoff ((shiftreg lsl 3) lor triple) (shifts+1) acc (*43*) else do_drop_0xx str len ninoff shiftreg shifts acc (*44*) in do_drop_0xx str len 0 0 0 [];; (*45*) (*====================================================================================*) let get_file fname = let handle = open_in fname in let len = (in_channel_length handle); in let s = String.create len in (really_input handle s 0 len; s);; let rec iter n f = if n>1 then (f(); iter (n-1) f) else f();; let main fname = let str = get_file fname in let start = Sys.time() in match iter iterations (function() -> drop_0xx str ((String.length str)*8)) with (outs,bitsize) -> let stop = Sys.time () in let time = (stop -. start) in let outfile = open_out (fname ^ ".ocaml") in (printf "%.3f" time; output_string outfile outs; flush stdout) ;; let () = let w = (Array.get Sys.argv 1) in main w;;