Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file oBus_util.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241(*
* oBus_util.ml
* ------------
* Copyright : (c) 2009, Jeremie Dimino <jeremie@dimino.org>
* Licence : BSD3
*
* This file is a part of obus, an ocaml implementation of D-Bus.
*)letsection=Lwt_log.Section.make"obus(util)"letrecassocx=function|[]->None|(k,v)::_whenk=x->Some(v)|_::l->assocxlletrecassqx=function|[]->None|(k,v)::_whenk==x->Some(v)|_::l->assqxlletrecfind_mapf=function|[]->None|x::l->matchfxwith|None->find_mapfl|y->yletfilter_mapfl=List.fold_right(funxacc->matchfxwith|None->acc|Some(v)->v::acc)l[]letpart_mapfl=List.fold_right(funx(success,failure)->matchfxwith|None->(success,x::failure)|Some(v)->(v::success,failure))l([],[])type('a,'b)either=|InLof'a|InRof'bletsplitfl=List.fold_right(funx(a,b)->matchfxwith|InLx->(x::a,b)|InRx->(a,x::b))l([],[])letmap_optionxf=matchxwith|Somex->Some(fx)|None->Noneletencode_charn=ifn<10thenchar_of_int(n+Char.code'0')elseifn<16thenchar_of_int(n+Char.code'a'-10)elseassertfalselethex_encodestr=letlen=String.lengthstrinlethex=Bytes.create(len*2)infori=0tolen-1doletn=Char.code(String.unsafe_getstri)inBytes.unsafe_sethex(i*2)(encode_char(nlsr4));Bytes.unsafe_sethex(i*2+1)(encode_char(nland15))done;Bytes.unsafe_to_stringhexletdecode_charch=matchchwith|'0'..'9'->Char.codech-Char.code'0'|'a'..'f'->Char.codech-Char.code'a'+10|'A'..'F'->Char.codech-Char.code'A'+10|_->raise(Invalid_argument"OBus_util.decode_char")lethex_decodehex=ifString.lengthhexmod2<>0thenraise(Invalid_argument"OBus_util.hex_decode");letlen=String.lengthhex/2inletstr=Bytes.createleninfori=0tolen-1doBytes.unsafe_setstri(char_of_int((decode_char(String.unsafe_gethex(i*2))lsl4)lor(decode_char(String.unsafe_gethex(i*2+1)))))done;Bytes.unsafe_to_stringstrlethomedir=lazy(tryLwt.return(Sys.getenv"HOME")withNot_found->let%lwtpwd=Lwt_unix.getpwuid(Unix.getuid())inLwt.returnpwd.Unix.pw_dir)letinit_pseudo=Lazy.from_funRandom.self_initletfill_pseudobufferposlen=ignore(Lwt_log.warning~section"using pseudo-random generator");Lazy.forceinit_pseudo;fori=postopos+len-1doBytes.unsafe_setbufferi(char_of_int(Random.int256))doneletfill_randombufferposlen=tryletic=open_in"/dev/urandom"inletn=inputicbufferposleninifn<lenthenfill_pseudobuffer(pos+n)(len-n);close_inicwithexn->ignore(Lwt_log.warning_f~exn~section"failed to get random data from /dev/urandom");fill_pseudobufferposlenletrandom_stringn=letstr=Bytes.createninfill_randomstr0n;Bytes.unsafe_to_stringstrletrandom_int32()=letr=random_string4inInt32.logor(Int32.logor(Int32.of_int(Char.coder.[0]))(Int32.shift_left(Int32.of_int(Char.coder.[1]))8))(Int32.logor(Int32.shift_left(Int32.of_int(Char.coder.[2]))16)(Int32.shift_left(Int32.of_int(Char.coder.[3]))24))letrandom_int()=Int32.to_int(random_int32())letrandom_int64()=Int64.logor(Int64.of_int32(random_int32()))(Int64.shift_left(Int64.of_int32(random_int32()))32)(* Compute the sha1 of a string.
Copied from uuidm by Daniel C. Bünzli, which can be found here:
http://erratique.ch/software/uuidm *)letsha_1s=letsha_1_pads=letlen=String.lengthsinletblen=8*leninletrem=lenmod64inletmlen=ifrem>55thenlen+128-remelselen+64-reminletm=Bytes.createmleninBytes.blit_strings0m0len;Bytes.fillmlen(mlen-len)'\x00';Bytes.setmlen'\x80';ifSys.word_size>32thenbeginBytes.setm(mlen-8)(Char.unsafe_chr(blenlsr56land0xFF));Bytes.setm(mlen-7)(Char.unsafe_chr(blenlsr48land0xFF));Bytes.setm(mlen-6)(Char.unsafe_chr(blenlsr40land0xFF));Bytes.setm(mlen-5)(Char.unsafe_chr(blenlsr32land0xFF));end;Bytes.setm(mlen-4)(Char.unsafe_chr(blenlsr24land0xFF));Bytes.setm(mlen-3)(Char.unsafe_chr(blenlsr16land0xFF));Bytes.setm(mlen-2)(Char.unsafe_chr(blenlsr8land0xFF));Bytes.setm(mlen-1)(Char.unsafe_chr(blenland0xFF));Bytes.unsafe_to_stringmin(* Operations on int32 *)let(&&&)=(land)inlet(lor)=Int32.logorinlet(lxor)=Int32.logxorinlet(land)=Int32.logandinlet(++)=Int32.addinletlnot=Int32.lognotinletsr=Int32.shift_rightinletsl=Int32.shift_leftinletclsnx=(slxn)lor(Int32.shift_right_logicalx(32-n))in(* Start *)letm=sha_1_padsinletw=Array.make160linleth0=ref0x67452301linleth1=ref0xEFCDAB89linleth2=ref0x98BADCFElinleth3=ref0x10325476linleth4=ref0xC3D2E1F0linleta=ref0linletb=ref0linletc=ref0linletd=ref0linlete=ref0linfori=0to((String.lengthm)/64)-1do(* For each block *)(* Fill w *)letbase=i*64inforj=0to15doletk=base+(j*4)inw.(j)<-sl(Int32.of_int(Char.codem.[k]))24lorsl(Int32.of_int(Char.codem.[k+1]))16lorsl(Int32.of_int(Char.codem.[k+2]))8lor(Int32.of_int(Char.codem.[k+3]))done;(* Loop *)a:=!h0;b:=!h1;c:=!h2;d:=!h3;e:=!h4;fort=0to79doletf,k=ift<=19then(!bland!c)lor((lnot!b)land!d),0x5A827999lelseift<=39then!blxor!clxor!d,0x6ED9EBA1lelseift<=59then(!bland!c)lor(!bland!d)lor(!cland!d),0x8F1BBCDClelse!blxor!clxor!d,0xCA62C1D6linlets=t&&&0xFinif(t>=16)thenbeginw.(s)<-cls1beginw.((s+13)&&&0xF)lxorw.((s+8)&&&0xF)lxorw.((s+2)&&&0xF)lxorw.(s)endend;lettemp=(cls5!a)++f++!e++w.(s)++kine:=!d;d:=!c;c:=cls30!b;b:=!a;a:=temp;done;(* Update *)h0:=!h0++!a;h1:=!h1++!b;h2:=!h2++!c;h3:=!h3++!d;h4:=!h4++!edone;leth=Bytes.create20inleti2shki=Bytes.seth(k)(Char.unsafe_chr((Int32.to_int(sri24))&&&0xFF));Bytes.seth(k+1)(Char.unsafe_chr((Int32.to_int(sri16))&&&0xFF));Bytes.seth(k+2)(Char.unsafe_chr((Int32.to_int(sri8))&&&0xFF));Bytes.seth(k+3)(Char.unsafe_chr((Int32.to_inti)&&&0xFF));ini2sh0!h0;i2sh4!h1;i2sh8!h2;i2sh12!h3;i2sh16!h4;Bytes.unsafe_to_stringh