| 1 |
unit BDecode; |
| 2 |
|
| 3 |
{====================================================================== |
| 4 |
BDecode.pas -- BitTorrent BDecoding Routines |
| 5 |
Original Coding by Knowbuddy, 2003-03-19 |
| 6 |
======================================================================} |
| 7 |
|
| 8 |
interface |
| 9 |
|
| 10 |
uses |
| 11 |
SysUtils, Classes, Hashes, Contnrs; |
| 12 |
|
| 13 |
type |
| 14 |
TISType = (tisString = 0, tisInt); |
| 15 |
TIntString = class(TObject) |
| 16 |
public |
| 17 |
StringPart: String; |
| 18 |
IntPart: Int64; |
| 19 |
ISType: TISType; |
| 20 |
end; |
| 21 |
function bdecodeStream(s: TStream): TObject; |
| 22 |
function bdecodeInt64(s: TStream): TIntString; |
| 23 |
function bdecodeHash(s: TStream): TObjectHash; |
| 24 |
function bdecodeString(s: TStream; i: Integer = 0): TIntString; |
| 25 |
function bdecodeList(s: TStream): TObjectList; |
| 26 |
function bin2hex(s: String; m: Integer = 999): String; |
| 27 |
|
| 28 |
var |
| 29 |
hexchars: array[0..15] of Char = '0123456789abcdef'; |
| 30 |
|
| 31 |
|
| 32 |
implementation |
| 33 |
|
| 34 |
function bin2hex(s: String; m: Integer = 999): String; |
| 35 |
var |
| 36 |
i, j, k, l : Integer; |
| 37 |
r : array of Char; |
| 38 |
begin |
| 39 |
l := Length(s); |
| 40 |
if(m < l) then l := m; |
| 41 |
SetLength(r,l * 2); |
| 42 |
for i := 1 to l do begin |
| 43 |
j := Ord(s[i]); |
| 44 |
k := (i - 1) * 2; |
| 45 |
r[k] := hexchars[j div 16]; |
| 46 |
r[k+1] := hexchars[j mod 16]; |
| 47 |
end; |
| 48 |
bin2hex := String(r); |
| 49 |
end; |
| 50 |
|
| 51 |
function bdecodeStream(s: TStream): TObject; |
| 52 |
var |
| 53 |
r: TObject; |
| 54 |
c: Char; |
| 55 |
n: Integer; |
| 56 |
begin |
| 57 |
n := s.Read(c, 1); |
| 58 |
if(n > 0) then begin |
| 59 |
case c of |
| 60 |
'd' : r:= bdecodeHash(s); |
| 61 |
'l' : r:= bdecodeList(s); |
| 62 |
'i' : r:= bdecodeInt64(s); |
| 63 |
'0'..'9' : r:= bdecodeString(s,StrToInt(c)); |
| 64 |
else r := nil; |
| 65 |
end; |
| 66 |
end else begin |
| 67 |
r := nil; |
| 68 |
end; |
| 69 |
bdecodeStream := r; |
| 70 |
end; |
| 71 |
|
| 72 |
function bdecodeHash(s: TStream): TObjectHash; |
| 73 |
var |
| 74 |
r: TObjectHash; |
| 75 |
o: TObject; |
| 76 |
n, st: Integer; |
| 77 |
c: Char; |
| 78 |
k, l: TIntString; |
| 79 |
begin |
| 80 |
r := TObjectHash.Create(); |
| 81 |
n := s.Read(c, 1); |
| 82 |
while((n > 0) and (c <> 'e') and (c >= '0') and (c <= '9')) do begin |
| 83 |
n := StrToInt(c); |
| 84 |
k := bdecodeString(s, n); |
| 85 |
if(k <> nil) then begin |
| 86 |
st := s.Position; |
| 87 |
o := bdecodeStream(s); |
| 88 |
if((o <> nil) and (k.StringPart <> '')) then r[k.StringPart] := o; |
| 89 |
if(k.StringPart = 'pieces') then begin |
| 90 |
k.StringPart:='pieces'; |
| 91 |
end; |
| 92 |
if(k.StringPart = 'info') then begin |
| 93 |
l := TIntString.Create(); |
| 94 |
l.IntPart := st; |
| 95 |
r['_info_start'] := l; |
| 96 |
l := TIntString.Create(); |
| 97 |
l.IntPart := s.Position - st; |
| 98 |
r['_info_length'] := l; |
| 99 |
end; |
| 100 |
end; |
| 101 |
n := s.Read(c, 1); |
| 102 |
|
| 103 |
end; |
| 104 |
|
| 105 |
if ((c < '0') or (c > '9')) and (c <> 'e') then |
| 106 |
bdecodeHash:= nil |
| 107 |
else bdecodeHash := r; |
| 108 |
end; |
| 109 |
|
| 110 |
function bdecodeList(s: TStream): TObjectList; |
| 111 |
var |
| 112 |
r: TObjectList; |
| 113 |
o: TObject; |
| 114 |
n: Integer; |
| 115 |
c: Char; |
| 116 |
begin |
| 117 |
r := TObjectList.Create(); |
| 118 |
n := s.Read(c, 1); |
| 119 |
while((n > 0) and (c <> 'e')) do begin |
| 120 |
s.Seek(-1, soFromCurrent); |
| 121 |
o := bdecodeStream(s); |
| 122 |
if(o <> nil) then r.Add(o); |
| 123 |
n := s.Read(c, 1); |
| 124 |
end; |
| 125 |
bdecodeList := r; |
| 126 |
end; |
| 127 |
|
| 128 |
function bdecodeString(s: TStream; i: Integer = 0): TIntString; |
| 129 |
var |
| 130 |
r: TIntString; |
| 131 |
t: String; |
| 132 |
c: Char; |
| 133 |
n: Integer; |
| 134 |
begin |
| 135 |
c := '0'; |
| 136 |
n := s.Read(c, 1); |
| 137 |
while((n > 0) and (c >= '0') and (c <= '9')) do begin |
| 138 |
i := (i * 10) + StrToInt(c); |
| 139 |
n := s.Read(c, 1); |
| 140 |
end; |
| 141 |
SetLength(t, i); |
| 142 |
n:=s.Read(PChar(t)^, i); |
| 143 |
r := TIntString.Create(); |
| 144 |
r.StringPart := t; |
| 145 |
r.ISType := tisString; |
| 146 |
|
| 147 |
bdecodeString := r; |
| 148 |
end; |
| 149 |
|
| 150 |
function bdecodeInt64(s: TStream): TIntString; |
| 151 |
var |
| 152 |
r: TIntString; |
| 153 |
i: Int64; |
| 154 |
c: Char; |
| 155 |
n: Integer; |
| 156 |
neg: boolean; |
| 157 |
|
| 158 |
begin |
| 159 |
i := 0; |
| 160 |
c := '0'; |
| 161 |
neg:= false; |
| 162 |
repeat |
| 163 |
if c='-' then neg:= true else i := (i * 10) + StrToInt(c); |
| 164 |
n := s.Read(c, 1); |
| 165 |
until not ((n > 0) and (((c >= '0')and(c <= '9'))or(c = '-'))); |
| 166 |
|
| 167 |
if neg then i:=-i; |
| 168 |
|
| 169 |
r := TIntString.Create(); |
| 170 |
r.IntPart := i; |
| 171 |
r.ISType := tisInt; |
| 172 |
bdecodeInt64 := r; |
| 173 |
end; |
| 174 |
|
| 175 |
end. |