-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCachedStreams.pas
900 lines (805 loc) · 26.3 KB
/
CachedStreams.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
unit CachedStreams;
{******************************************************************************}
{ Copyright (c) Dmitry Mozulyov }
{ }
{ Permission is hereby granted, free of charge, to any person obtaining a copy }
{ of this software and associated documentation files (the "Software"), to deal}
{ in the Software without restriction, including without limitation the rights }
{ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell }
{ copies of the Software, and to permit persons to whom the Software is }
{ furnished to do so, subject to the following conditions: }
{ }
{ The above copyright notice and this permission notice shall be included in }
{ all copies or substantial portions of the Software. }
{ }
{ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR }
{ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, }
{ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE }
{ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER }
{ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,}
{ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN }
{ THE SOFTWARE. }
{ }
{ email: softforyou@inbox.ru }
{ skype: dimandevil }
{ repository: https://github.com/d-mozulyov/CachedBuffers }
{******************************************************************************}
// compiler directives
{$ifdef FPC}
{$MODE DELPHIUNICODE}
{$ASMMODE INTEL}
{$define INLINESUPPORT}
{$define INLINESUPPORTSIMPLE}
{$define OPERATORSUPPORT}
{$define STATICSUPPORT}
{$define GENERICSUPPORT}
{$define ANSISTRSUPPORT}
{$define SHORTSTRSUPPORT}
{$define WIDESTRSUPPORT}
{$ifdef MSWINDOWS}
{$define WIDESTRLENSHIFT}
{$endif}
{$define INTERNALCODEPAGE}
{$ifdef CPU386}
{$define CPUX86}
{$endif}
{$ifdef CPUX86_64}
{$define CPUX64}
{$endif}
{$if Defined(CPUARM) or Defined(UNIX)}
{$define POSIX}
{$ifend}
{$else}
{$if CompilerVersion >= 24}
{$LEGACYIFEND ON}
{$ifend}
{$if CompilerVersion >= 15}
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{$WARN SYMBOL_DEPRECATED OFF}
{$ifend}
{$if CompilerVersion >= 20}
{$define INLINESUPPORT}
{$ifend}
{$if CompilerVersion >= 17}
{$define INLINESUPPORTSIMPLE}
{$ifend}
{$if CompilerVersion >= 18}
{$define OPERATORSUPPORT}
{$ifend}
{$if CompilerVersion >= 18.5}
{$define STATICSUPPORT}
{$ifend}
{$if CompilerVersion >= 20}
{$define GENERICSUPPORT}
{$define SYSARRAYSUPPORT}
{$ifend}
{$if CompilerVersion < 23}
{$define CPUX86}
{$ifend}
{$if CompilerVersion >= 23}
{$define UNITSCOPENAMES}
{$define RETURNADDRESS}
{$ifend}
{$if CompilerVersion >= 21}
{$WEAKLINKRTTI ON}
{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}
{$define EXTENDEDRTTI}
{$ifend}
{$if CompilerVersion >= 33}
{$define MANAGEDRECORDS}
{$ifend}
{$if (not Defined(NEXTGEN)) or (CompilerVersion >= 31)}
{$define ANSISTRSUPPORT}
{$ifend}
{$ifNdef NEXTGEN}
{$define SHORTSTRSUPPORT}
{$endif}
{$if Defined(MSWINDOWS) or (Defined(MACOS) and not Defined(IOS))}
{$define WIDESTRSUPPORT}
{$ifend}
{$if Defined(MSWINDOWS) or (Defined(WIDESTRSUPPORT) and (CompilerVersion <= 21))}
{$define WIDESTRLENSHIFT}
{$ifend}
{$if Defined(ANSISTRSUPPORT) and (CompilerVersion >= 20)}
{$define INTERNALCODEPAGE}
{$ifend}
{$if Defined(NEXTGEN)}
{$POINTERMATH ON}
{$ifend}
{$endif}
{$U-}{$V+}{$B-}{$X+}{$T+}{$P+}{$H+}{$J-}{$Z1}{$A4}
{$O+}{$R-}{$I-}{$Q-}{$W-}
{$ifdef CPUX86}
{$if not Defined(NEXTGEN)}
{$define CPUX86ASM}
{$define CPUINTELASM}
{$ifend}
{$define CPUINTEL}
{$endif}
{$ifdef CPUX64}
{$if (not Defined(POSIX)) or Defined(FPC)}
{$define CPUX64ASM}
{$define CPUINTELASM}
{$ifend}
{$define CPUINTEL}
{$endif}
{$if Defined(CPUINTEL) and Defined(POSIX)}
{$ifdef CPUX86}
{$define POSIXINTEL32}
{$else}
{$define POSIXINTEL64}
{$endif}
{$ifend}
{$if Defined(CPUX64) or Defined(CPUARM64)}
{$define LARGEINT}
{$else}
{$define SMALLINT}
{$ifend}
{$ifdef KOL_MCK}
{$define KOL}
{$endif}
{$ifdef POSIX}
{$undef CPUX86ASM}
{$undef CPUX64ASM}
{$undef CPUINTELASM}
{$endif}
interface
uses {$ifdef UNITSCOPENAMES}System.Types{$else}Types{$endif},
{$ifdef MSWINDOWS}
{$ifdef UNITSCOPENAMES}
Winapi.Windows, Winapi.ActiveX,
{$else}
Windows, ActiveX,
{$endif}
{$endif}
{$ifdef KOL}
KOL, err,
{$else}
{$ifdef UNITSCOPENAMES}
System.SysUtils, System.Classes,
{$else}
SysUtils, Classes,
{$endif}
{$endif}
CachedBuffers;
type
// RTL types
{$ifdef FPC}
PUInt64 = ^UInt64;
PBoolean = ^Boolean;
PString = ^string;
{$else}
{$if CompilerVersion < 16}
UInt64 = Int64;
PUInt64 = ^UInt64;
{$ifend}
{$if CompilerVersion < 21}
NativeInt = Integer;
NativeUInt = Cardinal;
{$ifend}
{$if CompilerVersion < 22}
PNativeInt = ^NativeInt;
PNativeUInt = ^NativeUInt;
{$ifend}
PWord = ^Word;
{$endif}
{$if not Defined(FPC) and (CompilerVersion < 20)}
TDate = type TDateTime;
TTime = type TDateTime;
{$ifend}
PDate = ^TDate;
PTime = ^TTime;
{$if SizeOf(Extended) >= 10}
{$define EXTENDEDSUPPORT}
{$ifend}
TBytes = {$ifdef SYSARRAYSUPPORT}TArray<Byte>{$else}array of Byte{$endif};
PBytes = ^TBytes;
{$if Defined(NEXTGEN) and (CompilerVersion >= 31)}
AnsiChar = type System.UTF8Char;
PAnsiChar = ^AnsiChar;
AnsiString = type System.RawByteString;
PAnsiString = ^AnsiString;
{$ifend}
// CachedBuffers types
ECachedBuffer = CachedBuffers.ECachedBuffer;
TCachedBufferKind = CachedBuffers.TCachedBufferKind;
TCachedBufferCallback = CachedBuffers.TCachedBufferCallback;
TCachedBufferProgress = CachedBuffers.TCachedBufferProgress;
TCachedBufferMemory = CachedBuffers.TCachedBufferMemory;
PCachedBufferMemory = CachedBuffers.PCachedBufferMemory;
TCachedBuffer = CachedBuffers.TCachedBuffer;
TCachedReader = CachedBuffers.TCachedReader;
TCachedWriter = CachedBuffers.TCachedWriter;
TCachedReReader = CachedBuffers.TCachedReReader;
TCachedReWriter = CachedBuffers.TCachedReWriter;
TCachedFileReader = CachedBuffers.TCachedFileReader;
TCachedFileWriter = CachedBuffers.TCachedFileWriter;
TCachedMemoryReader = CachedBuffers.TCachedMemoryReader;
TCachedMemoryWriter = CachedBuffers.TCachedMemoryWriter;
TCachedResourceReader = CachedBuffers.TCachedResourceReader;
// Classes.TStream or KOL.PStream
StreamType = {$ifdef KOL}PStream{$else}TStream{$endif};
{ TCachedStreamReader class }
TCachedStreamReader = class(TCachedReader)
protected
FStream: StreamType;
FOwner: Boolean;
FOffset: Int64;
function InternalCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
public
constructor Create(const AStream: StreamType; const AOwner: Boolean = False; const ABufferSize: NativeUInt = 0);
destructor Destroy; override;
property Stream: StreamType read FStream;
property Owner: Boolean read FOwner write FOwner;
property Offset: Int64 read FOFfset;
end;
{ TCachedStreamWriter class }
TCachedStreamWriter = class(TCachedWriter)
protected
FStream: StreamType;
FOwner: Boolean;
FOffset: Int64;
function InternalCallback(const ASender: TCachedBuffer; AData: PByte; ASize: NativeUInt): NativeUInt;
public
constructor Create(const AStream: StreamType; const AOwner: Boolean = False; const ABufferSize: NativeUInt = 0);
destructor Destroy; override;
property Stream: StreamType read FStream;
property Owner: Boolean read FOwner write FOwner;
property Offset: Int64 read FOFfset;
end;
{ TCachedBufferStream class }
{$ifdef KOL}
PCachedBufferStream = ^TCachedBufferStream;
TCachedBufferStream = object(TStream)
{$else}
TCachedBufferStream = class(TStream)
{$endif}
protected
FKind: TCachedBufferKind;
FOwner: Boolean;
FCachedBuffer: TCachedBuffer;
{$ifNdef KOL}
protected
{$if Defined(FPC) or (CompilerVersion >= 15)}
function GetSize: Int64; override;
{$ifend}
procedure SetSize(NewSize: Longint); override;
procedure SetSize(const NewSize: Int64); override;
{$endif}
public
{$ifNdef KOL}
constructor Create(const ACachedBuffer: TCachedBuffer; const AOwner: Boolean = False);
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; overload; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; override;
{$endif}
destructor Destroy; {$ifdef KOL}virtual{$else}override{$endif};
property Kind: TCachedBufferKind read FKind;
property Owner: Boolean read FOwner;
property CachedBuffer: TCachedBuffer read FCachedBuffer;
end;
{ TCachedBufferAdapter }
{ Implements OLE IStream on TCachedBuffer }
{$if (not Defined(FPC)) and (CompilerVersion >= 29)}
{$define NEWISTREAM}
{$ifend}
TCachedBufferAdapter = class(TCachedObject, IStream)
protected
FKind: TCachedBufferKind;
FOwner: Boolean;
FCachedBuffer: TCachedBuffer;
public
constructor Create(const ACachedBuffer: TCachedBuffer; const AOwner: Boolean = False);
destructor Destroy; override;
{$if Defined(FPC)}
function Read(pv: Pointer; cb: DWORD; pcbRead: PDWORD): HRESULT; stdcall;
function Write(pv: Pointer; cb: DWORD; pcbWritten: PDWORD): HRESULT; stdcall;
function Seek(dlibMove: LargeUInt; dwOrigin: Longint; out libNewPosition: LargeUInt): HResult; stdcall;
function SetSize(libNewSize: LargeUInt): HRESULT; stdcall;
function CopyTo(stm: IStream; cb: LargeUInt; out cbRead: LargeUInt; out cbWritten: LargeUInt): HRESULT; stdcall;
function Commit(grfCommitFlags: Longint): HRESULT; stdcall;
function Revert: HRESULT; stdcall;
function LockRegion(libOffset: LargeUInt;cb: LargeUInt; dwLockType: Longint): HRESULT; stdcall;
function UnlockRegion(libOffset: LargeUInt; cb: LargeUInt; dwLockType: Longint): HRESULT; stdcall;
function Stat(out statstg: TStatStg; grfStatFlag: Longint): HRESULT; stdcall;
function Clone(out stm: IStream): HRESULT; stdcall;
{$elseif not Defined(NEWISTREAM)}
function Read(pv: Pointer; cb: Longint; pcbRead: PLongint): HResult; virtual; stdcall;
function Write(pv: Pointer; cb: Longint; pcbWritten: PLongint): HResult; virtual; stdcall;
function Seek(dlibMove: Largeint; dwOrigin: Longint; out libNewPosition: Largeint): HResult; virtual; stdcall;
function SetSize(libNewSize: Largeint): HResult; virtual; stdcall;
function CopyTo(stm: IStream; cb: Largeint; out cbRead: Largeint; out cbWritten: Largeint): HResult; virtual; stdcall;
function Commit(grfCommitFlags: Longint): HResult; virtual; stdcall;
function Revert: HResult; virtual; stdcall;
function LockRegion(libOffset: Largeint; cb: Largeint; dwLockType: Longint): HResult; virtual; stdcall;
function UnlockRegion(libOffset: Largeint; cb: Largeint; dwLockType: Longint): HResult; virtual; stdcall;
function Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult; virtual; stdcall;
function Clone(out stm: IStream): HResult; virtual; stdcall;
{$else .NEWISTREAM}
function Read(pv: Pointer; cb: FixedUInt; pcbRead: PFixedUInt): HResult; virtual; stdcall;
function Write(pv: Pointer; cb: FixedUInt; pcbWritten: PFixedUInt): HResult; virtual; stdcall;
function Seek(dlibMove: Largeint; dwOrigin: DWORD; out libNewPosition: LargeUInt): HResult; virtual; stdcall;
function SetSize(libNewSize: LargeUInt): HResult; virtual; stdcall;
function CopyTo(stm: IStream; cb: LargeUInt; out cbRead: LargeUInt; out cbWritten: LargeUInt): HResult; virtual; stdcall;
function Commit(grfCommitFlags: DWORD): HResult; virtual; stdcall;
function Revert: HResult; virtual; stdcall;
function LockRegion(libOffset: LargeUInt; cb: LargeUInt; dwLockType: DWORD): HResult; virtual; stdcall;
function UnlockRegion(libOffset: LargeUInt; cb: LargeUInt; dwLockType: DWORD): HResult; virtual; stdcall;
function Stat(out statstg: TStatStg; grfStatFlag: DWORD): HResult; virtual; stdcall;
function Clone(out stm: IStream): HResult; virtual; stdcall;
{$ifend}
property Kind: TCachedBufferKind read FKind;
property Owner: Boolean read FOwner;
property CachedBuffer: TCachedBuffer read FCachedBuffer;
end;
{$ifdef KOL}
function NewCachedBufferStream(const ACachedBuffer: TCachedBuffer;
const AOwner: Boolean = False): PCachedBufferStream;
{$endif}
implementation
{ TCachedStreamReader }
constructor TCachedStreamReader.Create(const AStream: StreamType;
const AOwner: Boolean; const ABufferSize: NativeUInt);
begin
FStream := AStream;
FOwner := AOwner;
FOffset := AStream.Position;
inherited Create(InternalCallback, ABufferSize);
end;
destructor TCachedStreamReader.Destroy;
begin
inherited;
if (FOwner) then FStream.Free;
end;
function TCachedStreamReader.InternalCallback(const ASender: TCachedBuffer;
AData: PByte; ASize: NativeUInt): NativeUInt;
var
S, R: Longint;
N: NativeUInt;
begin
Result := 0;
while (ASize <> 0) do
begin
S := ASize;
if (ASize > NativeUInt(High(Longint))) then S := High(Longint);
R := Stream.Read(AData^, S);
if (R > 0) then
begin
N := NativeUInt(R);
Inc(Result, N);
Inc(AData, N);
Dec(ASize, N);
end;
if (R <> S) then Break;
end;
end;
{ TCachedStreamWriter }
constructor TCachedStreamWriter.Create(const AStream: StreamType;
const AOwner: Boolean; const ABufferSize: NativeUInt);
begin
FStream := AStream;
FOwner := AOwner;
FOffset := AStream.Position;
inherited Create(InternalCallback, ABufferSize);
end;
destructor TCachedStreamWriter.Destroy;
begin
inherited;
if (FOwner) then FStream.Free;
end;
function TCachedStreamWriter.InternalCallback(const ASender: TCachedBuffer;
AData: PByte; ASize: NativeUInt): NativeUInt;
var
S, R: Longint;
N: NativeUInt;
begin
Result := 0;
while (ASize <> 0) do
begin
S := ASize;
if (ASize > NativeUInt(High(Longint))) then S := High(Longint);
R := Stream.Write(AData^, S);
if (R > 0) then
begin
N := NativeUInt(R);
Inc(Result, N);
Inc(AData, N);
Dec(ASize, N);
end;
if (R <> S) then Break;
end;
end;
{ TCachedBufferStream }
{$ifNdef KOL}
constructor TCachedBufferStream.Create(const ACachedBuffer: TCachedBuffer;
const AOwner: Boolean);
begin
inherited Create;
FKind := ACachedBuffer.Kind;
FOwner := AOwner;
FCachedBuffer := ACachedBuffer;
end;
{$else}
{$IFDEF STREAM_LARGE64}
{$UNDEF STREAM_COMPAT}
{$ENDIF}
procedure FillCachedBufferStreamMethods(Stream: PCachedBufferStream); forward;
function NewCachedBufferStream(const ACachedBuffer: TCachedBuffer;
const AOwner: Boolean = False): PCachedBufferStream;
begin
New(Result, Create);
Result.FKind := ACachedBuffer.Kind;
Result.FOwner := AOwner;
Result.FCachedBuffer := ACachedBuffer;
FillCachedBufferStreamMethods(Result);
end;
{$endif}
destructor TCachedBufferStream.Destroy;
begin
inherited;
if (FOwner) then FCachedBuffer.Free;
end;
function CachedBufferSeek(const ACachedBuffer: TCachedBuffer;
AOffset: Int64; AOrigin: Word): Int64;
const
soFromBeginning = 0;
soFromCurrent = 1;
var
LTemp: Int64;
LCount: NativeUInt;
begin
Result := ACachedBuffer.Position;
if (AOrigin = soFromBeginning) then
begin
LTemp := AOffset;
AOffset := AOffset - Result;
Result := LTemp{from beginning Offset};
end;
if (AOrigin <= soFromCurrent) and (AOffset = 0) then
Exit;
if (ACachedBuffer.Kind = cbReader) and (AOrigin <= soFromCurrent) and (AOffset > 0) then
begin
while (AOffset <> 0) do
begin
LCount := AOffset;
if (LCount > NativeUInt(High(NativeInt))) then LCount := NativeUInt(High(NativeInt));
AOffset := AOffset - Int64(LCount);
TCachedReader(ACachedBuffer).Skip(LCount);
end;
end else
raise ECachedBuffer.Create('Invalid seek operation');
end;
{$ifNdef KOL}
{$if Defined(FPC) or (CompilerVersion >= 15)}
function TCachedBufferStream.GetSize: Int64;
begin
if (CachedBuffer.Limited) then
Result := CachedBuffer.Limit
else
Result := CachedBuffer.Position;
end;
{$ifend}
procedure TCachedBufferStream.SetSize(NewSize: Longint);
begin
CachedBuffer.Limit := NewSize;
end;
procedure TCachedBufferStream.SetSize(const NewSize: Int64);
begin
CachedBuffer.Limit := NewSize;
end;
function TCachedBufferStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
Result := CachedBufferSeek(CachedBuffer, Offset, Origin);
end;
function TCachedBufferStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
Result := CachedBufferSeek(CachedBuffer, Offset, Ord(Origin));
end;
{$else .KOL}
function StreamGetSiz(Self: PCachedBufferStream): TStrmSize;
begin
Result := Self.CachedBuffer.Position;
end;
procedure StreamSetSiz(Self: PCachedBufferStream; {$IFNDEF STREAM_COMPAT}const{$ENDIF} NewSize: TStrmSize);
begin
Self.CachedBuffer.Limit := NewSize;
end;
function StreamSeek(Self: PCachedBufferStream; {$IFNDEF STREAM_COMPAT}const{$ENDIF} Offset: TStrmMove; Origin: TMoveMethod ): TStrmSize;
begin
Result := CachedBufferSeek(Self.CachedBuffer, Offset, Ord(Origin));
end;
{$endif}
{$ifNdef KOL}
function TCachedBufferStream.Read(var Buffer; Count: Longint): Longint;
{$else .KOL}
function StreamRead(Self: PCachedBufferStream; var Buffer; {$IFNDEF STREAM_COMPAT}const{$ENDIF} Count: TStrmSize): TStrmSize;
{$endif}
begin
if (Self.FKind = cbReader) then
begin
if (Count <= 0) then
begin
Result := 0;
end else
begin
Result := Count;
TCachedReader(Self.CachedBuffer).Read(Buffer, Result);
end;
end else
raise ECachedBuffer.Create('Invalid read operation');
end;
{$ifNdef KOL}
function TCachedBufferStream.Write(const Buffer; Count: Longint): Longint;
{$else .KOL}
function StreamWrite(Self: PCachedBufferStream; var Buffer; {$IFNDEF STREAM_COMPAT}const{$ENDIF} Count: TStrmSize): TStrmSize;
{$endif}
begin
if (Self.FKind = cbWriter) then
begin
if (Count <= 0) then
begin
Result := 0;
end else
begin
Result := Count;
TCachedWriter(Self.CachedBuffer).Write(Buffer, Result);
end;
end else
raise ECachedBuffer.Create('Invalid write operation');
end;
{$ifdef KOL}
procedure StreamNone(Strm: PStream);
begin
end;
procedure FillCachedBufferStreamMethods(Stream: PCachedBufferStream);
begin
with Stream.fMethods do
begin
fSeek := Pointer(@StreamSeek);
fGetSiz := Pointer(@StreamGetSiz);
fSetSiz := Pointer(@StreamSetSiz);
fRead := Pointer(@StreamRead);
fWrite := Pointer(@StreamWrite);
fClose := Pointer(@StreamNone);
end;
end;
{$endif}
{ TCachedBufferAdapter }
constructor TCachedBufferAdapter.Create(const ACachedBuffer: TCachedBuffer;
const AOwner: Boolean);
begin
inherited Create;
FKind := ACachedBuffer.Kind;
FOwner := AOwner;
FCachedBuffer := ACachedBuffer;
end;
destructor TCachedBufferAdapter.Destroy;
begin
inherited;
if (FOwner) then FCachedBuffer.Free;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.Read(pv: Pointer; cb: DWORD; pcbRead: PDWORD): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.Read(pv: Pointer; cb: Longint; pcbRead: PLongint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.Read(pv: Pointer; cb: FixedUInt; pcbRead: PFixedUInt): HResult;
{$ifend}
var
LNumRead: Longint;
begin
if (FKind = cbWriter) then
begin
Result := S_FALSE;
Exit;
end;
try
if (pv = nil) then
begin
Result := STG_E_INVALIDPOINTER;
Exit;
end;
if (cb <= 0) then
begin
LNumRead := 0;
end else
begin
LNumRead := cb;
TCachedReader(Self.CachedBuffer).Read(pv^, LNumRead);
end;
if (pcbRead <> nil) then pcbRead^ := LNumRead;
Result := S_OK;
except
Result := S_FALSE;
end;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.Write(pv: Pointer; cb: DWORD; pcbWritten: PDWORD): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.Write(pv: Pointer; cb: Longint; pcbWritten: PLongint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.Write(pv: Pointer; cb: FixedUInt; pcbWritten: PFixedUInt): HResult;
{$ifend}
var
LNumWritten: Longint;
begin
if (FKind = cbReader) then
begin
Result := STG_E_CANTSAVE;
Exit;
end;
try
if (pv = nil) then
begin
Result := STG_E_INVALIDPOINTER;
Exit;
end;
if (cb <= 0) then
begin
LNumWritten := 0;
end else
begin
LNumWritten := cb;
TCachedWriter(Self.CachedBuffer).Write(pv^, LNumWritten);
end;
if (pcbWritten <> nil) then pcbWritten^ := LNumWritten;
Result := S_OK;
except
Result := STG_E_CANTSAVE;
end;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.Seek(dlibMove: LargeUInt; dwOrigin: Longint; out libNewPosition: LargeUInt): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.Seek(dlibMove: Largeint; dwOrigin: Longint; out libNewPosition: Largeint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.Seek(dlibMove: Largeint; dwOrigin: DWORD; out libNewPosition: LargeUInt): HResult;
{$ifend}
var
LNewPos: Largeint;
begin
try
if (Integer(dwOrigin) < STREAM_SEEK_SET) or (dwOrigin > STREAM_SEEK_END) then
begin
Result := STG_E_INVALIDFUNCTION;
Exit;
end;
LNewPos := CachedBufferSeek(CachedBuffer, dlibMove, dwOrigin);
if (@libNewPosition <> nil) then libNewPosition := LNewPos;
Result := S_OK;
except
Result := STG_E_INVALIDPOINTER;
end;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.SetSize(libNewSize: LargeUInt): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.SetSize(libNewSize: Largeint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.SetSize(libNewSize: LargeUInt): HResult;
{$ifend}
begin
try
CachedBuffer.Limit := libNewSize;
Result := S_OK;
except
Result := E_UNEXPECTED;
end;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.CopyTo(stm: IStream; cb: LargeUInt; out cbRead: LargeUInt; out cbWritten: LargeUInt): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.CopyTo(stm: IStream; cb: Largeint; out cbRead: Largeint; out cbWritten: Largeint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.CopyTo(stm: IStream; cb: LargeUInt; out cbRead: LargeUInt; out cbWritten: LargeUInt): HResult;
{$ifend}
var
N: NativeInt;
W: Longint;
LBytesRead, LBytesWritten: Largeint;
begin
if (Kind = cbWriter) then
begin
Result := E_UNEXPECTED;
Exit;
end;
LBytesRead := 0;
LBytesWritten := 0;
try
while (cb > 0) do
begin
N := CachedBuffer.Margin;
if (N <= 0) then
begin
if (TCachedReader(CachedBuffer).Finishing) then Break
else
begin
CachedBuffer.Flush;
N := CachedBuffer.Margin;
if (N <= 0) then Break;
end;
end;
{$if SizeOf(NativeInt) > SizeOf(Longint)}
if (N > High(Longint)) then N := High(Longint);
{$ifend}
if (N > cb) then N := cb;
Inc(LBytesRead, N);
W := 0;
Result := stm.Write(CachedBuffer.Current, N, Pointer(@W));
Inc(LBytesWritten, W);
if (Result = S_OK) and (W <> N) then Result := E_FAIL;
if (Result <> S_OK) then Exit;
Inc(NativeInt(CachedBuffer.Current), N);
Dec(cb, N);
end;
if (@cbWritten <> nil) then cbWritten := LBytesWritten;
if (@cbRead <> nil) then cbRead := LBytesRead;
Result := S_OK;
except
Result := E_UNEXPECTED;
end;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.Commit(grfCommitFlags: Longint): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.Commit(grfCommitFlags: Longint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.Commit(grfCommitFlags: DWORD): HResult;
{$ifend}
begin
Result := S_OK;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.Revert: HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.Revert: HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.Revert: HResult;
{$ifend}
begin
Result := STG_E_REVERTED;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.LockRegion(libOffset: LargeUInt;cb: LargeUInt; dwLockType: Longint): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.LockRegion(libOffset: Largeint; cb: Largeint; dwLockType: Longint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.LockRegion(libOffset: LargeUInt; cb: LargeUInt; dwLockType: DWORD): HResult;
{$ifend}
begin
Result := STG_E_INVALIDFUNCTION;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.UnlockRegion(libOffset: LargeUInt; cb: LargeUInt; dwLockType: Longint): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.UnlockRegion(libOffset: Largeint; cb: Largeint; dwLockType: Longint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.UnlockRegion(libOffset: LargeUInt; cb: LargeUInt; dwLockType: DWORD): HResult;
{$ifend}
begin
Result := STG_E_INVALIDFUNCTION;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.Stat(out statstg: TStatStg; grfStatFlag: Longint): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.Stat(out statstg: TStatStg; grfStatFlag: DWORD): HResult;
{$ifend}
begin
Result := E_NOTIMPL;
end;
{$if Defined(FPC)}
function TCachedBufferAdapter.Clone(out stm: IStream): HRESULT;
{$elseif not Defined(NEWISTREAM)}
function TCachedBufferAdapter.Clone(out stm: IStream): HResult;
{$else .NEWISTREAM}
function TCachedBufferAdapter.Clone(out stm: IStream): HResult;
{$ifend}
begin
Result := E_NOTIMPL;
end;
end.