Debian Codegraph @

plugins/wcx/torrent/src/TorrentFile.pas

raw
1 unit TorrentFile;
2
3 interface
4
5 uses
6 SysUtils, Contnrs, Hashes, Classes, BDecode;
7
8 type
9 TBitfield = array of boolean;
10
11 TTorrentPiece = class(TObject)
12 private
13 _Hash: String;
14 _HashBin: String;
15 _Valid: Boolean;
16 public
17 property Hash: String read _Hash;
18 property HashBin: String read _HashBin;
19 property Valid: Boolean read _Valid write _Valid;
20 constructor Create(Hash: String; HashBin:String; Valid: Boolean);
21 end;
22
23 TTorrentSubFile = class(TObject)
24 private
25 _Name: String;
26 _Path: String;
27 _Filename: String;
28 _Length: Int64;
29 _Offset: Int64;
30 _Left: Int64;
31 public
32 property Name: String read _Name write _Name;
33 property Path: String read _Path write _Path;
34 property Length: Int64 read _Length;
35 property Offset: Int64 read _Offset;
36 property Left: Int64 read _Left write _Left;
37 property Filename: String read _Filename write _Filename;
38 constructor Create(Name: String; Path: String; Length: Int64; Offset: Int64);
39 end;
40
41 TTorrentFile = class(TObject)
42 published
43 private
44 _Announce : String;
45 _Name : String;
46 _Comment : String;
47 _Length : Int64;
48 _CreationTime : Int32;
49 _Count : Integer;
50 _Err : TStringList;
51 _Tree : TObjectHash;
52 _SHA1Hash : String;
53 _HashBin : String;
54 _Multifile : Boolean;
55 _Files : TObjectList;
56 public
57 Pieces : array of TTorrentPiece;
58 PieceLength : Integer;
59 BackupTrackers : TStringList;
60
61 property Announce: String read _Announce write _Announce;
62 property Name: String read _Name write _Name;
63 property CreationTime: Int32 read _CreationTime write _CreationTime;
64 property Length: Int64 read _Length;
65 property Count: Integer read _Count;
66 property Tree: TObjectHash read _Tree;
67 property Errors: TStringList read _Err;
68 property Hash: String read _SHA1Hash;
69 property Comment: String read _Comment write _Comment;
70 property HashBin: String read _HashBin;
71 property Multifile: Boolean read _Multifile;
72 property Files: TObjectList read _Files write _Files;
73 procedure Clear();
74 function Load(Stream: TStream): Boolean;
75 procedure Save(Stream: TStream; Pieces : array of TTorrentPiece);
76 procedure Init(Announce, Name, Comment, HashBin:String; Length:Int64; Multifile:Boolean);
77 constructor Create();
78 destructor Destroy(); override;
79 end;
80
81 implementation
82
83 uses
84 DCDateTimeUtils, SHA1;
85
86 { TTorrentSubFile }
87
88 constructor TTorrentSubFile.Create(Name, Path: String; Length: Int64; Offset: Int64);
89 begin
90 _Name := Name;
91 _Path := Path;
92 _Length := Length;
93 _Offset := Offset;
94 _Left := Length;
95
96 inherited Create();
97 end;
98
99 procedure TTorrentFile.Clear();
100 var
101 i : Integer;
102 begin
103 _Announce := '';
104 _Name := '';
105 _SHA1Hash := '';
106 _Length := 0;
107 _Count := 0;
108 _Files.Clear();
109 _Tree.Clear();
110 _Err.Clear();
111 for i := Low(Pieces) to High(Pieces) do FreeAndNil(Pieces[i]);
112 SetLength(Pieces,0);
113 _Multifile := False;
114 end;
115
116 constructor TTorrentFile.Create();
117 begin
118 _Files := TObjectList.Create();
119 _Tree := TObjectHash.Create();
120 _Err := TStringList.Create();
121 BackupTrackers := TStringList.Create;
122 inherited Create();
123 end;
124
125 destructor TTorrentFile.Destroy();
126 begin
127 Clear();
128 FreeAndNil(_Files);
129 FreeAndNil(_Tree);
130 FreeAndNil(_Err);
131 FreeAndNil(BackupTrackers);
132 inherited;
133 end;
134
135 procedure TTorrentFile.Init(Announce, Name, Comment, HashBin:String; Length:Int64; Multifile:Boolean);
136 begin
137 _Announce := Announce;
138 _Name := Name;
139 _Comment := Comment;
140 _HashBin := HashBin;
141 _Length := Length;
142 _Multifile := Multifile;
143 _CreationTime := DateTimeToUnixFileTime(Now);
144 end;
145
146 function TTorrentFile.Load(Stream: TStream): Boolean;
147 var
148 info, thisfile: TObjectHash;
149 files, path, backup, backup2: TObjectList;
150 fp, fn: String;
151 i, j, pcount: Integer;
152 sz, fs, fo: Int64;
153 digest: TSHA1Digest;
154 r: Boolean;
155 o: TObject;
156 s:string;
157 begin
158 Clear();
159 r := False;
160 sz := 0;
161 try
162 o := bdecodeStream(Stream);
163 if(Assigned(o)) then begin
164 _Tree := o as TObjectHash;
165 if(_Tree.Exists('announce')) then begin
166 _Announce := (_Tree['announce'] as TIntString).StringPart;
167 end else begin
168 _Err.Add('Corrupt File: Missing "announce" segment');
169 end;
170 if(_Tree.Exists('announce-list')) then begin
171 backup := _Tree['announce-list'] as TObjectList;
172 for i := 0 to backup.Count - 1 do begin
173 backup2 := (backup[i] as TObjectList);
174 for j:=0 to backup2.Count -1 do BackupTrackers.Add((backup2[j] as TIntString).StringPart);
175 end;
176 end;
177 if(_Tree.Exists('comment')) then begin
178 _Comment := (_Tree['comment'] as TIntString).StringPart;
179 end;
180 if(_Tree.Exists('creation date')) then begin
181 _CreationTime := (_Tree['creation date'] as TIntString).IntPart;
182 end;
183 if(_Tree.Exists('info')) then begin
184 info := _Tree['info'] as TObjectHash;
185 if(info.Exists('name')) then begin
186 _Name := (info['name'] as TIntString).StringPart;
187 if copy(_Name,system.length(_Name)-7,8)='.torrent' then
188 _Name:=copy(_Name,0,system.length(_Name)-8);
189 end else begin
190 _Err.Add('Corrupt File: Missing "info.name" segment');
191 end;
192 if(info.Exists('piece length')) then begin
193 PieceLength := (info['piece length'] as TIntString).IntPart;
194 end else begin
195 _Err.Add('Corrupt File: Missing "info.piece length" segment');
196 end;
197 {
198 if(info.Exists('pieces')) then begin
199 fp := (info['pieces'] as TIntString).StringPart;
200 pcount := System.Length(fp) div 20;
201 SetLength(Pieces,pcount);
202 for i := 0 to pcount - 1 do begin
203 s:=copy(fp,(i * 20) + 1,20);
204 Pieces[i] := TTorrentPiece.Create(bin2hex(s), s, False);
205 end;
206 end else begin
207 _Err.Add('Corrupt File: Missing "info.pieces" segment');
208 end;
209 }
210 if(info.Exists('length')) then begin // single-file archive
211 sz := (info['length'] as TIntString).IntPart;
212 _Count := 1;
213 _Files.Add(TTorrentSubFile.Create(_Name,'',sz,Int64(0)));
214 end else begin
215 if(info.Exists('files')) then begin
216 _Multifile := True;
217 files := info['files'] as TObjectList;
218 for i := 0 to files.Count - 1 do begin
219 thisfile := files[i] as TObjectHash;
220 if(thisfile.Exists('length')) then begin
221 fs := (thisfile['length'] as TIntString).IntPart;
222 end else begin
223 fs := Int64(0);
224 _Err.Add('Corrupt File: files[' + IntToStr(i) + '] is missing a "length" segment');
225 end;
226 fp := '';
227 fn := '';
228 if(thisfile.Exists('path')) then begin
229 path := thisfile['path'] as TObjectList;
230 for j := 0 to path.Count - 2 do
231 fp := fp + (path[j] as TIntString).StringPart + PathDelim;
232 if(path.Count > 0) then fn := (path[path.Count - 1] as TIntString).StringPart;
233 end else begin
234 _Err.Add('Corrupt File: files[' + IntToStr(i) + '] is missing a "path" segment');
235 end;
236 _Files.Add(TTorrentSubFile.Create(fn,fp,fs,sz));
237 sz := sz + fs;
238 end;
239 _Count := _Files.Count;
240 end else begin
241 _Err.Add('Corrupt File: Missing both "info.length" and "info.files" segments (should have one or the other)');
242 end;
243 end;
244 if(_Tree.Exists('_info_start') and _Tree.Exists('_info_length')) then begin
245 fo := Stream.Position;
246 Stream.Seek((_Tree['_info_start'] as TIntString).IntPart,soFromBeginning);
247 fs := (_Tree['_info_length'] as TIntString).IntPart;
248 SetLength(fp,fs);
249 Stream.Read(PChar(fp)^,fs);
250 digest := SHA1String(fp);
251 _SHA1Hash := SHA1Print(digest);
252 SetLength(_HashBin, 20);
253 Move(digest[0], _HashBin[1], 20);
254 Stream.Seek(fo,soFromBeginning);
255 end;
256 end else begin
257 _Err.Add('Corrupt File: Missing "info" segment');
258 end;
259 _Length := sz;
260 r := True;
261 end else begin
262 _Err.Add('Error parsing file; does not appear to be valid bencoded metainfo');
263 end;
264 except
265 _Err.Add('Something bad happened while trying to load the file, probably corrupt metainfo');
266 end;
267 Result := r;
268 end;
269
270 procedure TTorrentFile.Save(Stream: TStream; Pieces : array of TTorrentPiece);
271 var i:integer;
272 s,s2:string;
273
274 procedure WStrm(s:string);
275 begin
276 Stream.WriteBuffer(s[1],system.length(s));
277 end;
278
279 procedure WStrg(s:string);
280 var t:String;
281 begin
282 t:=inttostr(system.length(s))+':'+s;
283 WStrm(t);
284 end;
285
286 procedure WInt(i:int64);
287 begin
288 WStrm('i'); WStrm(IntToStr(i)); WStrm('e');
289 end;
290
291 begin
292 WStrm('d');
293 WStrg('announce'); WStrg(Announce);
294 if BackupTrackers.Count > 0 then
295 begin
296 WStrg('announce-list');
297 WStrm('l');
298 // Primary Tracker
299 WStrm('l');
300 WStrg(Announce);
301 WStrm('e');
302 // Backup Tracker
303 for i:=0 to BackupTrackers.Count-1 do
304 if BackupTrackers[i] <> Announce then
305 begin
306 WStrm('l');
307 WStrg(BackupTrackers[i]);
308 WStrm('e');
309 end;
310 WStrm('e');
311 end;
312 if Comment <> '' then
313 begin
314 WStrg('comment');
315 WStrg(comment);
316 end;
317 if Date <> 0 then
318 begin
319 WStrg('creation date');
320 WInt(CreationTime);
321 end;
322 WStrg('info'); WStrm('d');
323
324 if Multifile then
325 begin
326 WStrg('files'); WStrm('l');
327
328 for i:=0 to Files.Count-1 do
329 with (Files[i] as TTorrentSubFile) do
330 begin
331 WStrm('d');
332 WStrg('length');
333 WInt(Length);
334 WStrg('path');
335 WStrm('l');
336
337 if Path <> '' then
338 begin
339 s:=path;
340 repeat
341 if pos('\',s) <> 0 then
342 begin
343 s2:=copy(s,1,pos('\',s)-1); WStrg(s2);
344 Delete(s,1,pos('\',s));
345 end;
346
347 if (pos('\',s)=0) and (s <>'') then WStrg(s);
348 until pos('\',s)=0;
349 end;
350 WStrg(Name);
351
352 WStrm('e');
353 WStrm('e');
354 end;
355
356 WStrm('e');
357 end
358 else
359 begin
360 WStrg('length');
361 WInt(Length);
362 end;
363
364 WStrg('name');
365 WStrg(Name);
366 WStrg('piece length');
367 WInt(PieceLength);
368
369 WStrg('pieces');
370 WStrm(IntToStr((high(pieces)+1)*20));
371 WStrm(':');
372 for i:=0 to high(pieces) do WStrm(pieces[i].HashBin);
373
374 WStrm('e');
375 WStrm('e');
376 end;
377
378 constructor TTorrentPiece.Create(Hash, HashBin: String; Valid: Boolean);
379 begin
380 _Hash := Hash;
381 _HashBin := HashBin;
382 _Valid := Valid;
383 inherited Create();
384 end;
385
386 end.