-
Notifications
You must be signed in to change notification settings - Fork 140
/
classesLib.pas
1314 lines (1166 loc) · 29 KB
/
classesLib.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Copyright (C) 2002-2020 Massimo Melina (www.rejetto.com)
This file is part of HFS ~ HTTP File Server.
HFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
HFS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HFS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
{$INCLUDE defs.inc }
unit classesLib;
interface
uses
iniFiles, types, hslib, strUtils, sysUtils, classes, math, system.Generics.Collections,
OverbyteIcsWSocket, OverbyteIcshttpProt;
type
TantiDos = class
protected
accepted: boolean;
Paddress: string;
public
constructor create;
destructor Destroy; override;
function accept(conn:ThttpConn; address:string=''):boolean;
end;
TfastStringAppend = class
protected
buff: string;
n: integer;
public
function length():integer;
function reset():string;
function get():string;
function append(s:string):integer;
end;
PcachedIcon = ^TcachedIcon;
TcachedIcon = record
data: string;
idx: integer;
time: Tdatetime;
end;
TiconsCache = class
n: integer;
icons: array of TcachedIcon;
function get(data:string):PcachedIcon;
procedure put(data:string; idx:integer; time:Tdatetime);
procedure clear();
procedure purge(olderThan:Tdatetime);
function idxOf(data:string):integer;
end;
TusersInVFS = class
protected
users: TstringDynArray;
pwds: array of TstringDynArray;
public
procedure reset();
procedure track(usr, pwd:string); overload;
procedure drop(usr, pwd:string); overload;
function match(usr, pwd:string):boolean; overload;
function empty():boolean;
end;
TarchiveStream = class(Tstream)
protected
pos, cachedTotal: int64;
cur: integer;
procedure invalidate();
procedure calculate(); virtual; abstract;
function getTotal():int64;
public
flist: array of record
src, // full path of the file on the disk
dst: string; // full path of the file in the archive
firstByte, // offset of the file inside the archive
mtime,
size: int64;
data: Tobject; // extra data
end;
onDestroy: TNotifyEvent;
constructor create;
destructor Destroy; override;
function addFile(src:string; dst:string=''; data:Tobject=NIL):boolean; virtual;
function contains(src:string):boolean;
function count():integer;
procedure reset(); virtual;
property totalSize:int64 read getTotal;
property current:integer read cur;
end; // TarchiveStream
TtarStreamWhere = (TW_HEADER, TW_FILE, TW_PAD);
TtarStream = class(TarchiveStream)
protected
fs: TFileStream;
block: TStringStream;
lastSeekFake: int64;
where: TtarStreamWhere;
function fsInit():boolean;
procedure headerInit(); // fill block with header
procedure padInit(full:boolean=FALSE); // fill block with pad
function headerLengthForFilename(ufn:string):integer;
procedure calculate(); override;
public
fileNamesOEM: boolean;
constructor create;
destructor Destroy; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin=soBeginning): Int64; override;
procedure reset(); override;
end; // TtarStream
Thasher = class(TstringList)
procedure loadFrom(path:string);
function getHashFor(fn:string):string;
end;
Tint2int = Tdictionary<integer,integer>;
Tstr2str = Tdictionary<string,string>;
Tstr2pointer = Tdictionary<string,pointer>;
TstringToIntHash = class(ThashedStringList)
constructor create;
function getInt(s:string):integer;
function getIntByIdx(idx:integer):integer;
function incInt(s:string):integer;
procedure setInt(s:string; int:integer);
end;
PtplSection = ^TtplSection;
TtplSection = record
name, txt: string;
nolog, public, noList, cache: boolean;
ts: Tdatetime;
end;
Ttpl = class
protected
src: string;
lastExt, // cache for getTxtByExt()
last: record section:string; idx:integer; end; // cache for getIdx()
strTable: THashedStringList;
fOver: Ttpl;
sections: Tstr2pointer;
function getTxt(section:string):string;
function newSection(section:string):PtplSection;
procedure fromString(txt:string);
procedure setOver(v:Ttpl);
public
onChange: TNotifyEvent;
constructor create(txt:string=''; over:Ttpl=NIL);
destructor Destroy; override;
property txt[section:string]:string read getTxt; default;
property fullText:string read src write fromString;
property over:Ttpl read fOver write setOver;
function sectionExist(section:string):boolean;
function getTxtByExt(fileExt:string):string;
function getSection(section:string; inherit:boolean=TRUE):PtplSection;
function getSections():TStringDynArray;
procedure appendString(txt:string);
function getStrByID(id:string):string;
function me():Ttpl;
end; // Ttpl
TcachedTplObj = class
ts: Tdatetime;
tpl: Ttpl;
end;
TcachedTpls = class(THashedStringList)
public
function getTplFor(fn:string):Ttpl;
destructor Destroy; override;
end; // TcachedTpls
TperIp = class // for every different address, we have an object of this class. These objects are never freed until hfs is closed.
public
limiter: TspeedLimiter;
customizedLimiter: boolean;
constructor create();
destructor Destroy; override;
end;
ThttpClient = class(TSslHttpCli)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
class function createURL(url:string):ThttpClient;
end;
Ttlv = class
protected
cur, bound: integer;
whole, lastRaw: ansistring;
stack: array of integer;
stackTop: integer;
public
procedure parse(data:ansistring);
function pop(var value:string; var raw:ansiString):integer;
function down():boolean;
function up():boolean;
function getTotal():integer;
function getCursor():integer;
function getPerc():real;
function isOver():boolean;
function getTheRest():ansistring;
end;
const TLV_UTF8_FLAG = $1000000;
implementation
uses
utilLib, main, windows, dateUtils, forms;
const folderConcurrents: integer = 0;
const MAX_CONCURRENTS = 3;
const ip2availability: Tdictionary<string,Tdatetime> = NIL;
constructor TantiDos.create();
begin
accepted:=FALSE;
end;
function TantiDos.accept(conn:ThttpConn; address:string=''):boolean;
procedure reject();
resourcestring
MSG_ANTIDOS_REPLY = 'Please wait, server busy';
begin
conn.reply.mode:=HRM_OVERLOAD;
conn.addHeader(ansistring('Refresh: '+intToStr(1+random(2)))); // random for less collisions
conn.reply.body:=UTF8Encode(MSG_ANTIDOS_REPLY);
end;
begin
if address= '' then
address:=conn.address;
if ip2availability = NIL then
ip2availability:=Tdictionary<string,Tdatetime>.create();
try
if ip2availability[address] > now() then // this specific address has to wait?
begin
reject();
exit(FALSE);
end;
except
end;
if folderConcurrents >= MAX_CONCURRENTS then // max number of concurrent folder loading, others are postponed
begin
reject();
exit(FALSE);
end;
inc(folderConcurrents);
Paddress:=address;
ip2availability.AddOrSetValue(address, now()+1/HOURS);
accepted:=TRUE;
Result:=TRUE;
end;
destructor TantiDos.Destroy;
var
pair: Tpair<string,Tdatetime>;
t: Tdatetime;
begin
if not accepted then
exit;
t:=now();
if folderConcurrents = MAX_CONCURRENTS then // serving multiple addresses at max capacity, let's give a grace period for others
ip2availability[Paddress]:=t + 1/SECONDS
else
ip2availability.Remove(Paddress);
dec(folderConcurrents);
// purge leftovers
for pair in ip2availability do
if pair.Value < t then
ip2availability.Remove(pair.Key);
end;
class function ThttpClient.createURL(url:string):ThttpClient;
begin
if startsText('https://', url)
and not httpsCanWork() then
exit(NIL);
result:=ThttpClient.Create(NIL);
result.URL:=url;
end;
constructor ThttpClient.create(AOwner: TComponent);
begin
inherited;
followRelocation:=TRUE;
agent:=HFS_HTTP_AGENT;
SslContext := TSslContext.Create(NIL);
end; // create
destructor ThttpClient.Destroy;
begin
SslContext.free;
SslContext:=NIl;
inherited destroy;
end;
constructor TperIp.create();
begin
limiter:=TspeedLimiter.create();
srv.limiters.add(limiter);
end;
destructor TperIp.Destroy;
begin
srv.limiters.remove(limiter);
limiter.free;
end;
//////////// TcachedTpls
destructor TcachedTpls.Destroy;
var
i: integer;
begin
for i:=0 to count-1 do
objects[i].free;
end;
function TcachedTpls.getTplFor(fn:string):Ttpl;
var
i: integer;
o: TcachedTplObj;
s: string;
begin
fn:=trim(lowercase(fn));
i:=indexOf(fn);
if i >= 0 then
o:=objects[i] as TcachedTplObj
else
begin
o:=TcachedTplObj.create();
if addObject(fn, o) > 100 then
delete(0);
end;
result:=o.tpl;
if getMtime(fn) = o.ts then exit;
o.ts:=getMtime(fn);
s:=loadTextFile(fn);
if o.tpl = NIL then
begin
result:=Ttpl.create();
o.tpl:=result;
end;
o.tpl.fromString(s);
end; // getTplFor
//////////// TusersInVFS
function TusersInVFS.empty():boolean;
begin result:= users = NIL end;
procedure TusersInVFS.reset();
begin
users:=NIL;
pwds:=NIL;
end; // reset
procedure TusersInVFS.track(usr, pwd: string);
var
i: integer;
begin
if usr = '' then exit;
i:=idxOf(usr, users);
if i < 0 then i:=addString(usr, users);
if i >= length(pwds) then setLength(pwds, i+1);
addString(pwd, pwds[i]);
end; // track
procedure TusersInVFS.drop(usr, pwd: string);
var
i, j: integer;
begin
i:=idxOf(usr, users);
if i < 0 then exit;
j:=AnsiIndexStr(pwd, pwds[i]);
if j < 0 then exit;
removeString(pwds[i], j);
if assigned(pwds[i]) then exit;
// this username does not exist with any password
removeString(users, i);
while i+1 < length(pwds) do
begin
pwds[i]:=pwds[i+1];
inc(i);
end;
setLength(pwds, i);
end; // drop
function TusersInVFS.match(usr, pwd:string):boolean;
var
i: integer;
begin
result:=FALSE;
i:=idxOf(usr, users);
if i < 0 then exit;
result:= 0 <= AnsiIndexStr(pwd, pwds[i]);
end; // match
//////////// TiconsCache
function TiconsCache.idxOf(data:string):integer;
var
b, e, c: integer;
begin
result:=0;
if n = 0 then exit;
// binary search
b:=0;
e:=n-1;
repeat
result:=(b+e) div 2;
c:=compareStr(data, icons[result].data);
if c = 0 then exit;
if c < 0 then e:=result-1;
if c > 0 then b:=result+1;
until b > e;
result:=b;
end; // idxOf
function TiconsCache.get(data:string):PcachedIcon;
var
i: integer;
begin
result:=NIL;
i:=idxOf(data);
if (i >= 0) and (i < n) and (icons[i].data = data) then
result:=@icons[i];
end; // get
procedure TiconsCache.put(data:string; idx:integer; time:Tdatetime);
var
i, w: integer;
begin
if length(icons) <= n then setlength(icons, n+50);
w:=idxOf(data);
for i:=n downto w+1 do icons[i]:=icons[i-1]; // shift
icons[w].data:=data;
icons[w].idx:=idx;
icons[w].time:=time;
inc(n);
end; // put
procedure TiconsCache.clear();
begin
icons:=NIL;
n:=0;
end; // clear
procedure TiconsCache.purge(olderThan:Tdatetime);
var
i, m: integer;
begin
exit;
m:=0;
for i:=0 to n-1 do
if icons[i].time < olderThan then dec(n) // this does not shorten the loop
else
begin
if m < i then icons[m]:=icons[i];
inc(m);
end;
end; // purge
//////////// TfastStringAppend
function TfastStringAppend.length():integer;
begin result:=n end;
function TfastStringAppend.get():string;
begin
setlength(buff, n);
result:=buff;
end; // get
function TfastStringAppend.reset():string;
begin
result:=get();
buff:='';
n:=0;
end; // reset
function TfastStringAppend.append(s:string):integer;
var
ls, lb: integer;
begin
ls:=system.length(s);
lb:=system.length(buff);
if n+ls > lb then setlength(buff, lb+ls+20000);
moveChars(s[1], buff[n+1], ls);
inc(n, ls);
result:=n;
end; // append
//////////// TarchiveStream
function TarchiveStream.getTotal():int64;
begin
if cachedTotal < 0 then calculate();
result:=cachedTotal;
end; // getTotal
function TarchiveStream.contains(src:string):boolean;
var
i: integer;
begin
for i:=0 to Length(flist)-1 do
if flist[i].src = src then
exit(TRUE);
result:=FALSE;
end;
function TarchiveStream.addFile(src:string; dst:string=''; data:Tobject=NIL):boolean;
function getMtime(fh:Thandle):int64;
var
ctime, atime, mtime: Tfiletime;
st: TSystemTime;
begin
getFileTime(fh, @ctime, @atime, @mtime);
fileTimeToSystemTime(mtime, st);
result:=dateTimeToUnix(SystemTimeToDateTime(st));
end; // getMtime
var
i, fh: integer;
begin
result:=FALSE;
fh:=fileopen(src, fmOpenRead+fmShareDenyNone);
if fh = -1 then exit;
result:=TRUE;
if dst = '' then
dst:=extractFileName(src);
i:=length(flist);
setLength(flist, i+1);
flist[i].src:=src;
flist[i].dst:=dst;
flist[i].data:=data;
flist[i].size:=sizeOfFile(fh);
flist[i].mtime:=getMtime(fh);
flist[i].firstByte:=-1;
fileClose(fh);
invalidate();
end; // addFile
procedure TarchiveStream.invalidate();
begin cachedTotal:=-1 end;
constructor TarchiveStream.create;
begin
inherited;
reset();
end; // create
destructor TarchiveStream.destroy;
begin
if assigned(onDestroy) then onDestroy(self);
inherited;
end; // destroy
procedure TarchiveStream.reset();
begin
flist:=NIL;
cur:=0;
pos:=0;
invalidate();
end; // reset
function TarchiveStream.count():integer;
begin result:=length(flist) end;
//////////// TtarStream
constructor TtarStream.create;
begin
block:=TStringStream.create('');
lastSeekFake:=-1;
where:=TW_HEADER;
fileNamesOEM:=FALSE;
inherited;
end; // create
destructor TtarStream.destroy;
begin
freeAndNIL(fs);
inherited;
end; // destroy
procedure TtarStream.reset();
begin
inherited;
block.size:=0;
end; // reset
function TtarStream.fsInit():boolean;
begin
if assigned(fs) and (fs.FileName = flist[cur].src) then
exit(TRUE);
result:=FALSE;
try
freeAndNIL(fs);
fs:=TfileStream.Create(flist[cur].src, fmOpenRead+fmShareDenyWrite);
result:=TRUE;
except
fs:=NIL;
end;
end; // fsInit
procedure TtarStream.headerInit();
function num(i:int64; fieldLength:integer):ansistring;
const
CHARS : array [0..7] of ansichar = '01234567';
var
d: integer;
begin
d:=fieldLength-1;
result:=ansistring(dupeString('0', d))+#0;
while d > 0 do
begin
result[d]:=CHARS[i and 7];
dec(d);
i:=i shr 3;
if i = 0 then break;
end;
end; // num
function str(s:ansistring; fieldLength:integer; fill:ansistring=#0):ansistring;
begin
setLength(s, min(length(s), fieldLength-1));
result:=s+ansistring( dupeString(fill, fieldLength-length(s)) );
end; // str
function sum(s:ansistring):integer;
var
i: integer;
begin
result:=0;
for i:=1 to length(s) do
inc(result, ord(s[i]));
end; // sum
procedure applyChecksum(var s:ansistring);
var
chk: ansistring;
begin
chk:=num(sum(s), 7)+' ';
chk[7]:=#0;
move(chk[1], s[100+24+12+12+1], length(chk));
end; // applyChecksum
const
FAKE_CHECKSUM = ' ';
USTAR = 'ustar'#0'00';
PERM = '0100777'#0'0000000'#0'0000000'#0; // file mode, uid, gid
var
fn, s, pre: ansistring;
ufn: string;
begin
ufn:=replaceStr(flist[cur].dst,'\','/');
if fileNamesOEM then
fn:=strToOem(ufn)
else
fn:=UTF8encode(ufn);
pre:='';
if length(fn) >= 100 then
begin
pre:=str('././@LongLink', 100)+PERM
+num(length(fn)+1, 12)+num(flist[cur].mtime, 12)
+FAKE_CHECKSUM+'L';
pre:=str(pre, 256)+str(#0+USTAR,256);
applyChecksum(pre);
pre:=pre+str(fn, 512);
end;
s:=str(fn, 100)+PERM
+num(flist[cur].size, 12) // file size
+num(flist[cur].mtime, 12) // mtime
+FAKE_CHECKSUM
+'0'+str('', 100) // link properties
+USTAR;
applyChecksum(s);
s:=str(s, 512); // pad
block.Size:=0;
block.WriteString(pre+s);
block.seek(0, soBeginning);
end; // headerInit
function TtarStream.write(const Buffer; Count: Longint): Longint;
begin raise EWriteError.Create('write unsupproted') end;
function gap512(i:int64):word; inline;
begin
result:=i and 511;
if result > 0 then
result:=512-result;
end; // gap512
procedure TtarStream.padInit(full:boolean=FALSE);
begin
block.Size:=0;
block.WriteString(dupeString(#0, if_(full,512,gap512(pos)) ));
block.Seek(0, soBeginning);
end; // padInit
function TtarStream.headerLengthForFilename(ufn:string):integer;
var
fn: ansistring;
begin
if fileNamesOEM then
fn:=strToOem(ufn)
else
fn:=UTF8encode(ufn);
result:=length(fn);
result:=512*if_(result<100, 1, 3+result div 512);
end; // headerLengthForFilename
procedure TtarStream.calculate();
var
pos: int64;
i: integer;
begin
pos:=0;
for i:=0 to length(flist)-1 do
with flist[i] do
begin
firstByte:=pos;
inc(pos, size+headerLengthForFilename(dst));
inc(pos, gap512(pos));
end;
inc(pos, 512); // last empty block
cachedTotal:=pos;
end; // calculate
function TtarStream.seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
function left():int64;
begin result:=offset-pos end;
procedure fineSeek(s:Tstream);
begin inc(pos, s.seek(left(), soBeginning)) end;
function skipMoreThan(size:int64):boolean;
begin
result:=left() > size;
if result then inc(pos, size);
end;
var
bak: int64;
prevCur: integer;
begin
{ The lastSeekFake trick is a way to fastly manage a sequence of
seek(0,soCurrent); seek(0,soEnd); seek(0,soBeginning);
such sequence called very often, while it is used to just read
the size of the stream, no real seeking requirement.
}
bak:=lastSeekFake;
lastSeekFake:=-1;
if (origin = soCurrent) and (offset <> 0) then
seek(pos+offset, soBeginning);
if origin = soEnd then
if offset < 0 then
seek(totalSize+offset, soBeginning)
else
begin
lastSeekFake:=pos;
pos:=totalsize;
end;
result:=pos;
if origin <> soBeginning then exit;
if bak >= 0 then
begin
pos:=bak;
exit;
end;
// here starts the normal seeking algo
prevCur:=cur;
cur:=0; // flist index
pos:=0; // current position in the file
block.size:=0;
while (left() > 0) and (cur < length(flist)) do
begin
// are we seeking inside this header?
if not skipMoreThan(headerLengthForFilename(flist[cur].dst)) then
begin
if (prevCur <> cur) or (where <> TW_HEADER) or eos(block) then
headerInit();
fineSeek(block);
where:=TW_HEADER;
break;
end;
// are we seeking inside this file?
if not skipMoreThan(flist[cur].size) then
begin
if not fsInit() then
raise Exception.Create('TtarStream.seek: cannot open '+flist[cur].src);
fineSeek(fs);
where:=TW_FILE;
break;
end;
// are we seeking inside this pad?
if not skipMoreThan(gap512(pos)) then
begin
padInit();
fineSeek(block);
where:=TW_PAD;
break;
end;
inc(cur);
end;//while
if left() > 0 then
begin
padInit(TRUE);
fineSeek(block);
end;
result:=pos;
end; // seek
function TtarStream.read(var Buffer; Count: Longint): Longint;
var
p: Pbyte;
procedure goForth(d: int64);
begin
dec(count, d);
inc(pos, d);
inc(p, d);
end; // goForth
procedure goRead(s:Tstream);
begin goForth( s.read(p^, count) ) end;
var
i, posBak: int64;
n: integer;
begin
posBak:=pos;
p:=@buffer;
n:=length(flist);
while (count > 0) and (cur < n) do
case where of
TW_HEADER:
begin
if block.size = 0 then
headerInit();
goRead(block);
if not eos(block) then continue;
where:=TW_FILE;
freeAndNIL(fs); // in case the same files appear twice in a row, we must be sure to reinitialize the reader stream
block.size:=0;
end;
TW_FILE:
begin
fsInit();
if assigned(fs) then
goRead(fs);
{ We reserved a fixed space for this file in the archive, but the file
may not exist anymore, or its size may be shorter than expected,
so we can't rely on eos(fs) to know if we are done in this section.
Lets calculate how far we are from the theoretical end of the file,
and decide after it.
}
i:=headerLengthForFilename(flist[cur].dst);
i:=flist[cur].firstByte+i+flist[cur].size-pos;
if count >= i then
where:=TW_PAD;
// In case the file is shorter, we pad the rest with NUL bytes
i:=min(count, max(0,i));
fillChar(p^,i,0);
goForth(i);
end;
TW_PAD:
begin
if block.size = 0 then padInit();
goRead(block);
if not eos(block) then continue;
where:=TW_HEADER;
block.size:=0;
inc(cur);
end;
end;//case
// last empty block
if count > 0 then
begin
padInit(TRUE);
goRead(block);
end;
result:=pos-posBak;
end; // read
//////////// Thasher
procedure Thasher.loadFrom(path:string);
var
sr: TsearchRec;
s, l, h: string;
begin
if path='' then exit;
path:=includeTrailingPathDelimiter(lowercase(path));
if findFirst(path+'*.md5', faAnyFile-faDirectory, sr) <> 0 then exit;
repeat
s:=loadTextfile(path+sr.name);
while s > '' do
begin
l:=chopline(s);
h:=trim(chop('*',l));
if h = '' then break;
if l = '' then
// assume it is referring to the filename without the extention
l:=copy(sr.name, 1, length(sr.name)-4);
add(path+lowercase(l)+'='+h);
end;
until findnext(sr) <> 0;
sysutils.findClose(sr);
end; // loadFrom
function Thasher.getHashFor(fn:string):string;
begin
try result:=values[lowercase(fn)]
except result:='' end
end;
//////////// TstringToIntHash
constructor TstringToIntHash.create;
begin
inherited create;
sorted:=TRUE;
duplicates:=dupIgnore;
end; // create
function TstringToIntHash.getIntByIdx(idx:integer):integer;
begin if idx < 0 then result:=0 else result:=integer(objects[idx]) end;
function TstringToIntHash.getInt(s:string):integer;
begin result:=getIntByIdx(indexOf(s)) end;
procedure TstringToIntHash.setInt(s:string; int:integer);
begin
beginUpdate();
objects[add(s)]:=Tobject(int);
endUpdate();
end; // setInt
function TstringToIntHash.incInt(s:string):integer;
var
i: integer;
begin
beginUpdate();
i:=add(s);
result:=integer(objects[i]);
inc(result);
objects[i]:=Tobject(result);
endUpdate();
end; // autoupdatedFiles_getCounter
//////////// Ttpl
constructor Ttpl.create(txt:string=''; over:Ttpl=NIL);
begin
sections:=Tstr2pointer.Create();
fullText:=txt;
self.over:=over;
end;
destructor Ttpl.destroy;
begin
fullText:=''; // this will cause the disposing
inherited;
end; // destroy
function Ttpl.getStrByID(id:string):string;