parse ADCGET list, prepare to answer
[mldonkey.git] / src / networks / direct_connect / dcKey.ml
blobabdf9ef5ce4d592bbb1c278c12495aa205b86257
1 (* Compute the key from $Lock xxxxx yyyyy| using 'gen_key xxxxx' *)
4 (*************************************************************************)
5 (* the key is quite easily :) computed from the lock *)
6 (* key[x]= ns(lock[x]^lock[x-1]) *)
7 (* ns is a nibble swap (switch the upper 4 bits with the lower 4 bits) *)
8 (* exception: *)
9 (* key[0] is a bit different *)
10 (* let's name A and B the 2 last bytes of the lock *)
11 (* key[0]= ns(lock[0]^A^B^0x05) ; 0x05 is a kind of magic nibble *)
12 (*************************************************************************)
16 $Lock 17lM=.U=*@Q&HvoG2=HJcLH:-,Q=5R=xMvRo-ET4;nMZYxnP,P_&oHFmc%B Pk=.l>r?nZz-VKmlf&z|
18 $Key 5/%DCN096%/(181)(18)(7)1(183)(134)q(166)(17)w(230)(227)(145)(130)W(240)W (146)(242)@'q(16)(215)(198)(128)v(246)TS(179)B(211)/%DCN036%/(134)(17)(6)(240)U2q0(18)a(227)(199)(199)(240)(151)(148)r(224)(178)(224)dv|
22 let char_is_extra ch buf =
23 match ch with
24 | 0 -> Printf.bprintf buf "/%%DCN000%%/"
25 | 5 -> Printf.bprintf buf "/%%DCN005%%/"
26 | 36 -> Printf.bprintf buf "/%%DCN036%%/"
27 | 96 -> Printf.bprintf buf "/%%DCN096%%/"
28 | 124 -> Printf.bprintf buf "/%%DCN124%%/"
29 | 126 -> Printf.bprintf buf "/%%DCN126%%/"
30 | _ -> Buffer.add_char buf (char_of_int ch)
32 let get s pos = int_of_char s.[pos]
34 let calculate_key s =
35 let buf = Buffer.create 100 in
36 let len = String.length s in
38 (* first byte *)
40 let u = (get s 0) land 255 in (* u=(((unsigned int)(lck->str[0]))&255); *)
41 let l = (get s (len-1)) land 255 in (* l=(((unsigned int)(lck->str[lck->len-1]))&255); *)
42 let o = (get s (len-2)) land 255 in (* o=(((unsigned int)(lck->str[lck->len-2]))&255); *)
44 let u = u lxor l lxor o lxor 0x05 in (* u=u^l^o^0x05; *)
46 let v = (((u lsl 8) lor u) lsr 4) land 255 in (* v=(((u<<8)|u)>>4)&255; *)
48 char_is_extra v buf;
49 (* match v with
50 | 0 | 5 -> Printf.bprintf buf "/%%DCN%03d%%/" v
51 | 36 -> Printf.bprintf buf "/%%DCN036%%/"
52 | 96 -> Printf.bprintf buf "/%%DCN096%%/"
53 | _ -> Buffer.add_char buf (char_of_int v) *)
54 for i = 1 to len - 1 do
55 let u = (get s i) land 255 in
56 let l = (get s (i-1)) land 255 in
57 let u = u lxor l in
58 let v = (((u lsl 8) lor u) lsr 4) land 255 in (* v=(((u<<8)|u)>>4)&255; *)
59 char_is_extra v buf;
60 done;
61 Buffer.contents buf
63 let char_percent = int_of_char '%'
64 let char_z = int_of_char 'z'
66 let create_key = "MLDonkey"
67 (* let len = 80 + Random.int 15 in
68 let key = String.create len in
69 for i = 0 to len - 1 do
70 key.[i] <- char_of_int (char_percent + Random.int (char_z - char_percent))
71 done;
72 key *)