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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
|
{
This file is part of the Free Component Library (FCL)
Copyright (c) 1999-2000 by the Free Pascal development team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program 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.
**********************************************************************}
{****************************************************************************}
{* TStream *}
{****************************************************************************}
procedure TStream.ReadNotImplemented;
begin
raise EStreamError.CreateFmt(SStreamNoReading, [ClassName]) at get_caller_addr(get_frame);
end;
procedure TStream.WriteNotImplemented;
begin
raise EStreamError.CreateFmt(SStreamNoWriting, [ClassName]) at get_caller_addr(get_frame);
end;
function TStream.Read(var Buffer; Count: Longint): Longint;
begin
ReadNotImplemented;
Result := 0;
end;
function TStream.Write(const Buffer; Count: Longint): Longint;
begin
WriteNotImplemented;
Result := 0;
end;
function TStream.GetPosition: Int64;
begin
Result:=Seek(0,soCurrent);
end;
procedure TStream.SetPosition(const Pos: Int64);
begin
Seek(pos,soBeginning);
end;
procedure TStream.SetSize64(const NewSize: Int64);
begin
// Required because can't use overloaded functions in properties
SetSize(NewSize);
end;
function TStream.GetSize: Int64;
var
p : int64;
begin
p:=Seek(0,soCurrent);
GetSize:=Seek(0,soEnd);
Seek(p,soBeginning);
end;
procedure TStream.SetSize(NewSize: Longint);
begin
// We do nothing. Pipe streams don't support this
// As wel as possible read-ony streams !!
end;
procedure TStream.SetSize(const NewSize: Int64);
begin
// Backwards compatibility that calls the longint SetSize
if (NewSize<Low(longint)) or
(NewSize>High(longint)) then
raise ERangeError.Create(SRangeError);
SetSize(longint(NewSize));
end;
function TStream.Seek(Offset: Longint; Origin: Word): Longint;
type
TSeek64 = function(const offset:Int64;Origin:TSeekorigin):Int64 of object;
var
CurrSeek,
TStreamSeek : TSeek64;
CurrClass : TClass;
begin
// Redirect calls to 64bit Seek, but we can't call the 64bit Seek
// from TStream, because then we end up in an infinite loop
CurrSeek:=nil;
CurrClass:=Classtype;
while (CurrClass<>nil) and
(CurrClass<>TStream) do
CurrClass:=CurrClass.Classparent;
if CurrClass<>nil then
begin
CurrSeek:=@Self.Seek;
TStreamSeek:=@TStream(@CurrClass).Seek;
if TMethod(TStreamSeek).Code=TMethod(CurrSeek).Code then
CurrSeek:=nil;
end;
if CurrSeek<>nil then
Result:=Seek(Int64(offset),TSeekOrigin(origin))
else
raise EStreamError.CreateFmt(SSeekNotImplemented,[ClassName]);
end;
procedure TStream.Discard(const Count: Int64);
const
CSmallSize =255;
CLargeMaxBuffer =32*1024; // 32 KiB
var
Buffer: array[1..CSmallSize] of Byte;
begin
if Count=0 then
Exit;
if Count<=SizeOf(Buffer) then
ReadBuffer(Buffer,Count)
else
DiscardLarge(Count,CLargeMaxBuffer);
end;
procedure TStream.DiscardLarge(Count: int64; const MaxBufferSize: Longint);
var
Buffer: array of Byte;
begin
if Count=0 then
Exit;
if Count>MaxBufferSize then
SetLength(Buffer,MaxBufferSize)
else
SetLength(Buffer,Count);
while (Count>=Length(Buffer)) do
begin
ReadBuffer(Buffer[0],Length(Buffer));
Dec(Count,Length(Buffer));
end;
if Count>0 then
ReadBuffer(Buffer[0],Count);
end;
procedure TStream.InvalidSeek;
begin
raise EStreamError.CreateFmt(SStreamInvalidSeek, [ClassName]) at get_caller_addr(get_frame);
end;
procedure TStream.FakeSeekForward(Offset: Int64; const Origin: TSeekOrigin; const Pos: Int64);
var
Buffer: Pointer;
BufferSize, i: LongInt;
begin
if Origin=soBeginning then
Dec(Offset,Pos);
if (Offset<0) or (Origin=soEnd) then
InvalidSeek;
if Offset>0 then
Discard(Offset);
end;
function TStream.Seek(const Offset: Int64; Origin: TSeekorigin): Int64;
begin
// Backwards compatibility that calls the longint Seek
if (Offset<Low(longint)) or
(Offset>High(longint)) then
raise ERangeError.Create(SRangeError);
Result:=Seek(longint(Offset),ord(Origin));
end;
procedure TStream.ReadBuffer(var Buffer; Count: Longint);
begin
if Read(Buffer,Count)<Count then
Raise EReadError.Create(SReadError);
end;
procedure TStream.WriteBuffer(const Buffer; Count: Longint);
begin
if Write(Buffer,Count)<Count then
Raise EWriteError.Create(SWriteError);
end;
function TStream.CopyFrom(Source: TStream; Count: Int64): Int64;
var
Buffer: Pointer;
BufferSize, i: LongInt;
const
MaxSize = $20000;
begin
Result:=0;
if Count=0 then
Source.Position:=0; // This WILL fail for non-seekable streams...
BufferSize:=MaxSize;
if (Count>0) and (Count<BufferSize) then
BufferSize:=Count; // do not allocate more than needed
GetMem(Buffer,BufferSize);
try
if Count=0 then
repeat
i:=Source.Read(buffer^,BufferSize);
if i>0 then
WriteBuffer(buffer^,i);
Inc(Result,i);
until i<BufferSize
else
while Count>0 do
begin
if Count>BufferSize then
i:=BufferSize
else
i:=Count;
Source.ReadBuffer(buffer^,i);
WriteBuffer(buffer^,i);
Dec(count,i);
Inc(Result,i);
end;
finally
FreeMem(Buffer);
end;
end;
function TStream.ReadComponent(Instance: TComponent): TComponent;
var
Reader: TReader;
begin
Reader := TReader.Create(Self, 4096);
try
Result := Reader.ReadRootComponent(Instance);
finally
Reader.Free;
end;
end;
function TStream.ReadComponentRes(Instance: TComponent): TComponent;
begin
ReadResHeader;
Result := ReadComponent(Instance);
end;
procedure TStream.WriteComponent(Instance: TComponent);
begin
WriteDescendent(Instance, nil);
end;
procedure TStream.WriteComponentRes(const ResName: string; Instance: TComponent);
begin
WriteDescendentRes(ResName, Instance, nil);
end;
procedure TStream.WriteDescendent(Instance, Ancestor: TComponent);
var
Driver : TAbstractObjectWriter;
Writer : TWriter;
begin
Driver := TBinaryObjectWriter.Create(Self, 4096);
Try
Writer := TWriter.Create(Driver);
Try
Writer.WriteDescendent(Instance, Ancestor);
Finally
Writer.Destroy;
end;
Finally
Driver.Free;
end;
end;
procedure TStream.WriteDescendentRes(const ResName: string; Instance, Ancestor: TComponent);
var
FixupInfo: Integer;
begin
{ Write a resource header }
WriteResourceHeader(ResName, FixupInfo);
{ Write the instance itself }
WriteDescendent(Instance, Ancestor);
{ Insert the correct resource size into the resource header }
FixupResourceHeader(FixupInfo);
end;
procedure TStream.WriteResourceHeader(const ResName: string; {!!!: out} var FixupInfo: Integer);
var
ResType, Flags : word;
begin
ResType:=NtoLE(word($000A));
Flags:=NtoLE(word($1030));
{ Note: This is a Windows 16 bit resource }
{ Numeric resource type }
WriteByte($ff);
{ Application defined data }
WriteWord(ResType);
{ write the name as asciiz }
WriteBuffer(ResName[1],length(ResName));
WriteByte(0);
{ Movable, Pure and Discardable }
WriteWord(Flags);
{ Placeholder for the resource size }
WriteDWord(0);
{ Return current stream position so that the resource size can be
inserted later }
FixupInfo := Position;
end;
procedure TStream.FixupResourceHeader(FixupInfo: Integer);
var
ResSize,TmpResSize : Integer;
begin
ResSize := Position - FixupInfo;
TmpResSize := NtoLE(longword(ResSize));
{ Insert the correct resource size into the placeholder written by
WriteResourceHeader }
Position := FixupInfo - 4;
WriteDWord(TmpResSize);
{ Seek back to the end of the resource }
Position := FixupInfo + ResSize;
end;
procedure TStream.ReadResHeader;
var
ResType, Flags : word;
begin
try
{ Note: This is a Windows 16 bit resource }
{ application specific resource ? }
if ReadByte<>$ff then
raise EInvalidImage.Create(SInvalidImage);
ResType:=LEtoN(ReadWord);
if ResType<>$000a then
raise EInvalidImage.Create(SInvalidImage);
{ read name }
while ReadByte<>0 do
;
{ check the access specifier }
Flags:=LEtoN(ReadWord);
if Flags<>$1030 then
raise EInvalidImage.Create(SInvalidImage);
{ ignore the size }
ReadDWord;
except
on EInvalidImage do
raise;
else
raise EInvalidImage.create(SInvalidImage);
end;
end;
function TStream.ReadByte : Byte;
var
b : Byte;
begin
ReadBuffer(b,1);
ReadByte:=b;
end;
function TStream.ReadWord : Word;
var
w : Word;
begin
ReadBuffer(w,2);
ReadWord:=w;
end;
function TStream.ReadDWord : Cardinal;
var
d : Cardinal;
begin
ReadBuffer(d,4);
ReadDWord:=d;
end;
function TStream.ReadQWord: QWord;
var
q: QWord;
begin
ReadBuffer(q,8);
ReadQWord:=q;
end;
Function TStream.ReadAnsiString : String;
Var
TheSize : Longint;
P : PByte ;
begin
ReadBuffer (TheSize,SizeOf(TheSize));
SetLength(Result,TheSize);
// Illegal typecast if no AnsiStrings defined.
if TheSize>0 then
begin
ReadBuffer (Pointer(Result)^,TheSize);
P:=Pointer(Result)+TheSize;
p^:=0;
end;
end;
Procedure TStream.WriteAnsiString (const S : String);
Var L : Longint;
begin
L:=Length(S);
WriteBuffer (L,SizeOf(L));
WriteBuffer (Pointer(S)^,L);
end;
procedure TStream.WriteByte(b : Byte);
begin
WriteBuffer(b,1);
end;
procedure TStream.WriteWord(w : Word);
begin
WriteBuffer(w,2);
end;
procedure TStream.WriteDWord(d : Cardinal);
begin
WriteBuffer(d,4);
end;
procedure TStream.WriteQWord(q: QWord);
begin
WriteBuffer(q,8);
end;
{****************************************************************************}
{* THandleStream *}
{****************************************************************************}
Constructor THandleStream.Create(AHandle: THandle);
begin
Inherited Create;
FHandle:=AHandle;
end;
function THandleStream.Read(var Buffer; Count: Longint): Longint;
begin
Result:=FileRead(FHandle,Buffer,Count);
If Result=-1 then Result:=0;
end;
function THandleStream.Write(const Buffer; Count: Longint): Longint;
begin
Result:=FileWrite (FHandle,Buffer,Count);
If Result=-1 then Result:=0;
end;
Procedure THandleStream.SetSize(NewSize: Longint);
begin
SetSize(Int64(NewSize));
end;
Procedure THandleStream.SetSize(const NewSize: Int64);
begin
FileTruncate(FHandle,NewSize);
end;
function THandleStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
Result:=FileSeek(FHandle,Offset,ord(Origin));
end;
{****************************************************************************}
{* TFileStream *}
{****************************************************************************}
constructor TFileStream.Create(const AFileName: string; Mode: Word);
begin
Create(AFileName,Mode,438);
end;
constructor TFileStream.Create(const AFileName: string; Mode: Word; Rights: Cardinal);
begin
FFileName:=AFileName;
If (Mode and fmCreate) > 0 then
FHandle:=FileCreate(AFileName,Mode,Rights)
else
FHAndle:=FileOpen(AFileName,Mode);
If (THandle(FHandle)=feInvalidHandle) then
If Mode=fmcreate then
raise EFCreateError.createfmt(SFCreateError,[AFileName])
else
raise EFOpenError.Createfmt(SFOpenError,[AFilename]);
end;
destructor TFileStream.Destroy;
begin
FileClose(FHandle);
end;
{****************************************************************************}
{* TCustomMemoryStream *}
{****************************************************************************}
procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; ASize: PtrInt);
begin
FMemory:=Ptr;
FSize:=ASize;
end;
function TCustomMemoryStream.GetSize: Int64;
begin
Result:=FSize;
end;
function TCustomMemoryStream.GetPosition: Int64;
begin
Result:=FPosition;
end;
function TCustomMemoryStream.Read(var Buffer; Count: LongInt): LongInt;
begin
Result:=0;
If (FSize>0) and (FPosition<Fsize) and (FPosition>=0) then
begin
Result:=Count;
If (Result>(FSize-FPosition)) then
Result:=(FSize-FPosition);
Move ((FMemory+FPosition)^,Buffer,Result);
FPosition:=Fposition+Result;
end;
end;
function TCustomMemoryStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
Case Word(Origin) of
soFromBeginning : FPosition:=Offset;
soFromEnd : FPosition:=FSize+Offset;
soFromCurrent : FPosition:=FPosition+Offset;
end;
Result:=FPosition;
{$IFDEF DEBUG}
if Result < 0 then
raise Exception.Create('TCustomMemoryStream');
{$ENDIF}
end;
procedure TCustomMemoryStream.SaveToStream(Stream: TStream);
begin
if FSize>0 then Stream.WriteBuffer (FMemory^,FSize);
end;
procedure TCustomMemoryStream.SaveToFile(const FileName: string);
Var S : TFileStream;
begin
S:=TFileStream.Create (FileName,fmCreate);
Try
SaveToStream(S);
finally
S.free;
end;
end;
{****************************************************************************}
{* TMemoryStream *}
{****************************************************************************}
Const TMSGrow = 4096; { Use 4k blocks. }
procedure TMemoryStream.SetCapacity(NewCapacity: PtrInt);
begin
SetPointer (Realloc(NewCapacity),Fsize);
FCapacity:=NewCapacity;
end;
function TMemoryStream.Realloc(var NewCapacity: PtrInt): Pointer;
begin
If NewCapacity<0 Then
NewCapacity:=0
else
begin
// if growing, grow at least a quarter
if (NewCapacity>FCapacity) and (NewCapacity < (5*FCapacity) div 4) then
NewCapacity := (5*FCapacity) div 4;
// round off to block size.
NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1);
end;
// Only now check !
If NewCapacity=FCapacity then
Result:=FMemory
else
begin
Result:=Reallocmem(FMemory,Newcapacity);
If (Result=Nil) and (Newcapacity>0) then
Raise EStreamError.Create(SMemoryStreamError);
end;
end;
destructor TMemoryStream.Destroy;
begin
Clear;
Inherited Destroy;
end;
procedure TMemoryStream.Clear;
begin
FSize:=0;
FPosition:=0;
SetCapacity (0);
end;
procedure TMemoryStream.LoadFromStream(Stream: TStream);
begin
Stream.Position:=0;
SetSize(Stream.Size);
If FSize>0 then Stream.ReadBuffer(FMemory^,FSize);
end;
procedure TMemoryStream.LoadFromFile(const FileName: string);
Var S : TFileStream;
begin
S:=TFileStream.Create (FileName,fmOpenRead or fmShareDenyWrite);
Try
LoadFromStream(S);
finally
S.free;
end;
end;
procedure TMemoryStream.SetSize({$ifdef CPU64}const{$endif CPU64} NewSize: PtrInt);
begin
SetCapacity (NewSize);
FSize:=NewSize;
IF FPosition>FSize then
FPosition:=FSize;
end;
function TMemoryStream.Write(const Buffer; Count: LongInt): LongInt;
Var NewPos : PtrInt;
begin
If (Count=0) or (FPosition<0) then
exit(0);
NewPos:=FPosition+Count;
If NewPos>Fsize then
begin
IF NewPos>FCapacity then
SetCapacity (NewPos);
FSize:=Newpos;
end;
System.Move (Buffer,(FMemory+FPosition)^,Count);
FPosition:=NewPos;
Result:=Count;
end;
{****************************************************************************}
{* TBytesStream *}
{****************************************************************************}
constructor TBytesStream.Create(const ABytes: TBytes);
begin
inherited Create;
FBytes:=ABytes;
SetPointer(Pointer(FBytes),Length(FBytes));
FCapacity:=Length(FBytes);
end;
function TBytesStream.Realloc(var NewCapacity: PtrInt): Pointer;
begin
// adapt TMemoryStream code to use with dynamic array
if NewCapacity<0 Then
NewCapacity:=0
else
begin
if (NewCapacity>Capacity) and (NewCapacity < (5*Capacity) div 4) then
NewCapacity := (5*Capacity) div 4;
NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1);
end;
if NewCapacity=Capacity then
Result:=Pointer(FBytes)
else
begin
SetLength(FBytes,Newcapacity);
Result:=Pointer(FBytes);
if (Result=nil) and (Newcapacity>0) then
raise EStreamError.Create(SMemoryStreamError);
end;
end;
{****************************************************************************}
{* TStringStream *}
{****************************************************************************}
function TStringStream.GetSize: Int64;
begin
Result:=Length(FDataString);
end;
function TStringStream.GetPosition: Int64;
begin
Result:=FPosition;
end;
procedure TStringStream.SetSize(NewSize: Longint);
begin
Setlength(FDataString,NewSize);
If FPosition>NewSize then FPosition:=NewSize;
end;
constructor TStringStream.Create(const AString: string);
begin
Inherited create;
FDataString:=AString;
end;
function TStringStream.Read(var Buffer; Count: Longint): Longint;
begin
Result:=Length(FDataString)-FPosition;
If Result>Count then Result:=Count;
// This supposes FDataString to be of type AnsiString !
Move (Pchar(FDataString)[FPosition],Buffer,Result);
FPosition:=FPosition+Result;
end;
function TStringStream.ReadString(Count: Longint): string;
Var NewLen : Longint;
begin
NewLen:=Length(FDataString)-FPosition;
If NewLen>Count then NewLen:=Count;
SetLength(Result,NewLen);
Read (Pointer(Result)^,NewLen);
end;
function TStringStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
Case Origin of
soFromBeginning : FPosition:=Offset;
soFromEnd : FPosition:=Length(FDataString)+Offset;
soFromCurrent : FPosition:=FPosition+Offset;
end;
If FPosition>Length(FDataString) then
FPosition:=Length(FDataString)
else If FPosition<0 then
FPosition:=0;
Result:=FPosition;
end;
function TStringStream.Write(const Buffer; Count: Longint): Longint;
begin
Result:=Count;
SetSize(FPosition+Count);
// This supposes that FDataString is of type AnsiString)
Move (Buffer,PChar(FDataString)[Fposition],Count);
FPosition:=FPosition+Count;
end;
procedure TStringStream.WriteString(const AString: string);
begin
Write (PChar(Astring)[0],Length(AString));
end;
{****************************************************************************}
{* TResourceStream *}
{****************************************************************************}
{$ifdef UNICODE}
procedure TResourceStream.Initialize(Instance: TFPResourceHMODULE; Name, ResType: PWideChar; NameIsID: Boolean);
begin
Res:=FindResource(Instance, Name, ResType);
if Res=0 then
if NameIsID then
raise EResNotFound.CreateFmt(SResNotFound,[IntToStr(PtrInt(Name))])
else
raise EResNotFound.CreateFmt(SResNotFound,[Name]);
Handle:=LoadResource(Instance,Res);
if Handle=0 then
if NameIsID then
raise EResNotFound.CreateFmt(SResNotFound,[IntToStr(PtrInt(Name))])
else
raise EResNotFound.CreateFmt(SResNotFound,[Name]);
SetPointer(LockResource(Handle),SizeOfResource(Instance,Res));
end;
constructor TResourceStream.Create(Instance: TFPResourceHMODULE; const ResName: WideString; ResType: PWideChar);
begin
inherited create;
Initialize(Instance,PWideChar(ResName),ResType,False);
end;
constructor TResourceStream.CreateFromID(Instance: TFPResourceHMODULE; ResID: Integer; ResType: PWideChar);
begin
inherited create;
Initialize(Instance,PWideChar(ResID),ResType,True);
end;
{$else UNICODE}
procedure TResourceStream.Initialize(Instance: TFPResourceHMODULE; Name, ResType: PChar; NameIsID: Boolean);
begin
Res:=FindResource(Instance, Name, ResType);
if Res=0 then
if NameIsID then
raise EResNotFound.CreateFmt(SResNotFound,[IntToStr(PtrInt(Name))])
else
raise EResNotFound.CreateFmt(SResNotFound,[Name]);
Handle:=LoadResource(Instance,Res);
if Handle=0 then
if NameIsID then
raise EResNotFound.CreateFmt(SResNotFound,[IntToStr(PtrInt(Name))])
else
raise EResNotFound.CreateFmt(SResNotFound,[Name]);
SetPointer(LockResource(Handle),SizeOfResource(Instance,Res));
end;
constructor TResourceStream.Create(Instance: TFPResourceHMODULE; const ResName: string; ResType: PChar);
begin
inherited create;
Initialize(Instance,pchar(ResName),ResType,False);
end;
constructor TResourceStream.CreateFromID(Instance: TFPResourceHMODULE; ResID: Integer; ResType: PChar);
begin
inherited create;
Initialize(Instance,pchar(PtrInt(ResID)),ResType,True);
end;
{$endif UNICODE}
destructor TResourceStream.Destroy;
begin
UnlockResource(Handle);
FreeResource(Handle);
inherited destroy;
end;
{****************************************************************************}
{* TOwnerStream *}
{****************************************************************************}
constructor TOwnerStream.Create(ASource: TStream);
begin
FSource:=ASource;
end;
destructor TOwnerStream.Destroy;
begin
If FOwner then
FreeAndNil(FSource);
inherited Destroy;
end;
{****************************************************************************}
{* TStreamAdapter *}
{****************************************************************************}
constructor TStreamAdapter.Create(Stream: TStream; Ownership: TStreamOwnership = soReference);
begin
inherited Create;
FStream:=Stream;
FOwnership:=Ownership;
m_bReverted:=false; // mantis 15003
// http://www.tech-archive.net/Archive/German/microsoft.public.de.vc/2005-08/msg00791.html
// http://code.google.com/p/ddab-lib/wiki/TPJIStreamWrapper
end;
destructor TStreamAdapter.Destroy;
begin
if StreamOwnership=soOwned then
FreeAndNil(FStream);
inherited Destroy;
end;
{$warnings off}
function TStreamAdapter.Read(pv: Pointer; cb: DWORD; pcbRead: PDWORD): HResult; stdcall;
var
readcount: Longint;
begin
if m_bReverted then
begin
Result := STG_E_REVERTED;
Exit;
end;
if pv = nil then
begin
Result := STG_E_INVALIDPOINTER;
Exit;
end;
readcount := FStream.Read(pv^, cb);
if pcbRead <> nil then pcbRead^ := readcount;
Result := S_OK;
end;
function TStreamAdapter.Write(pv: Pointer; cb: DWORD; pcbWritten: PDWORD): HResult; stdcall;
var
writecount: Longint;
begin
if m_bReverted then
begin
Result := STG_E_REVERTED;
Exit;
end;
if pv = nil then
begin
Result := STG_E_INVALIDPOINTER;
Exit;
end;
writecount := FStream.Write(pv^, cb);
if pcbWritten <> nil then pcbWritten^ := writecount;
Result := S_OK;
end;
function TStreamAdapter.Seek(dlibMove: Largeint; dwOrigin: Longint; out libNewPosition: Largeint): HResult; stdcall;
var
newpos: Int64;
begin
if m_bReverted then
begin
Result := STG_E_REVERTED;
Exit;
end;
case dwOrigin of
STREAM_SEEK_SET: newpos := FStream.Seek(dlibMove, soBeginning);
STREAM_SEEK_CUR: newpos := FStream.Seek(dlibMove, soCurrent);
STREAM_SEEK_END: newpos := FStream.Seek(dlibMove, soEnd);
else
begin
Result := STG_E_INVALIDFUNCTION;
Exit;
end;
end;
if @libNewPosition <> nil then
libNewPosition := newpos;
Result := S_OK;
end;
function TStreamAdapter.SetSize(libNewSize: Largeint): HResult; stdcall;
begin
if m_bReverted then
begin
Result := STG_E_REVERTED;
Exit;
end;
if libNewSize<0 then
begin
Result := STG_E_INVALIDFUNCTION;
Exit;
end;
try
FStream.Size := libNewSize;
Result := S_OK;
except
// TODO: return different error value according to exception like STG_E_MEDIUMFULL
Result := E_FAIL;
end;
end;
function TStreamAdapter.CopyTo(stm: IStream; cb: Largeint; out cbRead: Largeint; out cbWritten: Largeint): HResult; stdcall;
var
sz: dword;
buffer : array[0..1023] of byte;
begin
if m_bReverted then
begin
Result := STG_E_REVERTED;
Exit;
end;
// the method is similar to TStream.CopyFrom => use CopyFrom implementation
cbWritten := 0;
cbRead := 0;
while cb > 0 do
begin
if (cb > sizeof(buffer)) then
sz := sizeof(Buffer)
else
sz := cb;
sz := FStream.Read(buffer, sz);
inc(cbRead, sz);
stm.Write(@buffer[0], sz, @sz);
inc(cbWritten, sz);
if sz = 0 then
begin
Result := E_FAIL;
Exit;
end;
dec(cb, sz);
end;
Result := S_OK;
end;
function TStreamAdapter.Commit(grfCommitFlags: Longint): HResult; stdcall;
begin
if m_bReverted then
Result := STG_E_REVERTED
else
Result := S_OK;
end;
function TStreamAdapter.Revert: HResult; stdcall;
begin
m_bReverted := True;
Result := S_OK;
end;
function TStreamAdapter.LockRegion(libOffset: Largeint; cb: Largeint; dwLockType: Longint): HResult; stdcall;
begin
Result := STG_E_INVALIDFUNCTION;
end;
function TStreamAdapter.UnlockRegion(libOffset: Largeint; cb: Largeint; dwLockType: Longint): HResult; stdcall;
begin
Result := STG_E_INVALIDFUNCTION;
end;
function TStreamAdapter.Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult; stdcall;
begin
if m_bReverted then
begin
Result := STG_E_REVERTED;
Exit;
end;
if grfStatFlag in [STATFLAG_DEFAULT,STATFLAG_NOOPEN,STATFLAG_NONAME] then
begin
if @statstg <> nil then
begin
fillchar(statstg, sizeof(TStatStg),#0);
{ //TODO handle pwcsName
if grfStatFlag = STATFLAG_DEFAULT then
runerror(217) //Result :={$ifdef windows} STG_E_INVALIDFLAG{$else}E_INVALID_FLAG{$endif}
}
statstg.dwType := STGTY_STREAM;
statstg.cbSize := FStream.Size;
statstg.grfLocksSupported := LOCK_WRITE;
end;
Result := S_OK;
end else
Result := STG_E_INVALIDFLAG
end;
function TStreamAdapter.Clone(out stm: IStream): HResult; stdcall;
begin
if m_bReverted then
begin
Result := STG_E_REVERTED;
Exit;
end;
// don't raise an exception here return error value that function is not implemented
// to implement this we need a clone method for TStream class
Result := STG_E_UNIMPLEMENTEDFUNCTION;
end;
constructor TProxyStream.Create(const Stream: IStream);
begin
FStream := Stream;
end;
function TProxyStream.Read(var Buffer; Count: Longint): Longint;
begin
Check(FStream.Read(@Buffer, Count, @Result));
end;
function TProxyStream. Seek(const Offset: int64; Origin: TSeekOrigin): int64;
begin
Check(FStream.Seek(Offset, ord(Origin), result));
end;
function TProxyStream.Write(const Buffer; Count: Longint): Longint;
begin
Check(FStream.Write(@Buffer, Count, @Result));
end;
function TProxyStream.GetIStream: IStream;
begin
Result := FStream;
end;
procedure TProxyStream.Check(err:integer);
var e : EInOutError;
begin
e:= EInOutError.Create('Proxystream.Check');
e.Errorcode:=err;
raise e;
end;
{$warnings on}
|