-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJclStreams.pas
More file actions
3129 lines (2800 loc) · 89.6 KB
/
Copy pathJclStreams.pas
File metadata and controls
3129 lines (2800 loc) · 89.6 KB
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
{**************************************************************************************************}
{ }
{ Project JEDI Code Library (JCL) }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
{ you may not use this file except in compliance with the License. You may obtain a copy of the }
{ License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF }
{ ANY KIND, either express or implied. See the License for the specific language governing rights }
{ and limitations under the License. }
{ }
{ The Original Code is JclStreams.pas. }
{ }
{ The Initial Developer of the Original Code is Robert Marquardt. Portions created by }
{ Robert Marquardt are Copyright (C) Robert Marquardt (robert_marquardt att gmx dott de) }
{ All rights reserved. }
{ }
{ Contributors: }
{ Florent Ouchet (outchy) }
{ Heinz Zastrau }
{ Andreas Schmidt }
{ }
{**************************************************************************************************}
{ }
{ Stream-related functions and classes }
{ }
{**************************************************************************************************}
{ }
{ Last modified: $Date:: $ }
{ Revision: $Rev:: $ }
{ Author: $Author:: $ }
{ }
{**************************************************************************************************}
unit JclStreams;
{$I jcl.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
{$IFDEF HAS_UNITSCOPE}
{$IFDEF MSWINDOWS}
Winapi.Windows,
{$ENDIF MSWINDOWS}
System.SysUtils, System.Classes,
System.Contnrs,
{$ELSE ~HAS_UNITSCOPE}
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF MSWINDOWS}
SysUtils, Classes,
Contnrs,
{$ENDIF ~HAS_UNITSCOPE}
{$IFDEF HAS_UNIT_LIBC}
Libc,
{$ENDIF HAS_UNIT_LIBC}
JclBase, JclStringConversions;
const
StreamDefaultBufferSize = 4096;
type
EJclStreamError = class(EJclError);
// abstraction layer to support Delphi 5 and C++Builder 5 streams
// 64 bit version of overloaded functions are introduced
TJclStream = class(TStream)
protected
procedure SetSize(NewSize: Longint); overload; override;
procedure SetSize(const NewSize: Int64); overload; override;
public
function Seek(Offset: Longint; Origin: Word): Longint; overload; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; override;
procedure LoadFromStream(Source: TStream; BufferSize: Longint = StreamDefaultBufferSize); virtual;
procedure LoadFromFile(const FileName: TFileName; BufferSize: Longint = StreamDefaultBufferSize); virtual;
procedure SaveToStream(Dest: TStream; BufferSize: Longint = StreamDefaultBufferSize); virtual;
procedure SaveToFile(const FileName: TFileName; BufferSize: Longint = StreamDefaultBufferSize); virtual;
end;
//=== VCL stream replacements ===
TJclHandleStream = class(TJclStream)
private
FHandle: THandle;
protected
procedure SetSize(const NewSize: Int64); override;
public
constructor Create(AHandle: THandle);
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
property Handle: THandle read FHandle;
end;
TJclFileStream = class(TJclHandleStream)
public
constructor Create(const FileName: TFileName; Mode: Word; Rights: Cardinal = $666);
destructor Destroy; override;
end;
{
TJclCustomMemoryStream = class(TJclStream)
end;
TJclMemoryStream = class(TJclCustomMemoryStream)
end;
TJclStringStream = class(TJclStream)
end;
TJclResourceStream = class(TJclCustomMemoryStream)
end;
}
//=== new stream ideas ===
TJclEmptyStream = class(TJclStream)
protected
procedure SetSize(const NewSize: Int64); override;
public
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
end;
TJclNullStream = class(TJclStream)
private
FPosition: Int64;
FSize: Int64;
protected
procedure SetSize(const NewSize: Int64); override;
public
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
end;
TJclRandomStream = class(TJclNullStream)
protected
function GetRandSeed: Longint; virtual;
procedure SetRandSeed(Seed: Longint); virtual;
public
function RandomData: Byte; virtual;
procedure Randomize; dynamic;
function Read(var Buffer; Count: Longint): Longint; override;
property RandSeed: Longint read GetRandSeed write SetRandSeed;
end;
TJclMultiplexStream = class(TJclStream)
private
FStreams: TList;
FReadStreamIndex: Integer;
function GetStream(Index: Integer): TStream;
function GetCount: Integer;
procedure SetStream(Index: Integer; const Value: TStream);
function GetReadStream: TStream;
procedure SetReadStream(const Value: TStream);
procedure SetReadStreamIndex(const Value: Integer);
protected
procedure SetSize(const NewSize: Int64); override;
public
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): Int64; override;
function Add(NewStream: TStream): Integer;
procedure Clear;
function Remove(AStream: TStream): Integer;
procedure Delete(const Index: Integer);
property Streams[Index: Integer]: TStream read GetStream write SetStream;
property ReadStreamIndex: Integer read FReadStreamIndex write SetReadStreamIndex;
property ReadStream: TStream read GetReadStream write SetReadStream;
property Count: Integer read GetCount;
end;
TJclStreamDecorator = class(TJclStream)
private
FAfterStreamChange: TNotifyEvent;
FBeforeStreamChange: TNotifyEvent;
FOwnsStream: Boolean;
FStream: TStream;
procedure SetStream(Value: TStream);
protected
procedure DoAfterStreamChange; virtual;
procedure DoBeforeStreamChange; virtual;
procedure SetSize(const NewSize: Int64); override;
public
constructor Create(AStream: TStream; AOwnsStream: Boolean = False);
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): Int64; override;
property AfterStreamChange: TNotifyEvent read FAfterStreamChange write FAfterStreamChange;
property BeforeStreamChange: TNotifyEvent read FBeforeStreamChange write FBeforeStreamChange;
property OwnsStream: Boolean read FOwnsStream write FOwnsStream;
property Stream: TStream read FStream write SetStream;
end;
TJclBufferedStream = class(TJclStreamDecorator)
protected
FBuffer: array of Byte;
FBufferCurrentSize: Longint;
FBufferMaxModifiedPos: Longint;
FBufferSize: Longint;
FBufferStart: Int64; // position of the first byte of the buffer in stream
FPosition: Int64; // current position in stream
function BufferHit: Boolean;
function GetCalcedSize: Int64; virtual;
function LoadBuffer: Boolean; virtual;
function ReadFromBuffer(var Buffer; Count, Start: Longint): Longint;
function WriteToBuffer(const Buffer; Count, Start: Longint): Longint;
protected
procedure DoAfterStreamChange; override;
procedure DoBeforeStreamChange; override;
procedure SetSize(const NewSize: Int64); override;
public
constructor Create(AStream: TStream; AOwnsStream: Boolean = False);
destructor Destroy; override;
procedure Flush; virtual;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
property BufferSize: Longint read FBufferSize write FBufferSize;
end;
TStreamNotifyEvent = procedure(Sender: TObject; Position: Int64; Size: Int64) of object;
TJclEventStream = class(TJclStreamDecorator)
private
FNotification: TStreamNotifyEvent;
procedure DoNotification;
protected
procedure DoBeforeStreamChange; override;
procedure DoAfterStreamChange; override;
procedure SetSize(const NewSize: Int64); override;
public
constructor Create(AStream: TStream; ANotification: TStreamNotifyEvent = nil;
AOwnsStream: Boolean = False);
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
property OnNotification: TStreamNotifyEvent read FNotification write FNotification;
end;
TJclEasyStream = class(TJclStreamDecorator)
public
function IsEqual(Stream: TStream): Boolean;
function ReadBoolean: Boolean;
function ReadChar: Char;
function ReadAnsiChar: AnsiChar;
function ReadWideChar: WideChar;
function ReadByte: Byte;
function ReadCurrency: Currency;
function ReadDateTime: TDateTime;
function ReadExtended: Extended;
function ReadDouble: Double;
function ReadInt64: Int64;
function ReadInteger: Integer;
function ReadCString: string; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function ReadCAnsiString: AnsiString;
function ReadCWideString: WideString;
function ReadShortString: string;
function ReadSingle: Single;
function ReadSizedString: string; {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
function ReadSizedAnsiString: AnsiString;
function ReadSizedWideString: WideString;
procedure WriteBoolean(Value: Boolean);
procedure WriteChar(Value: Char);
procedure WriteAnsiChar(Value: AnsiChar);
procedure WriteWideChar(Value: WideChar);
procedure WriteByte(Value: Byte);
procedure WriteCurrency(const Value: Currency);
procedure WriteDateTime(const Value: TDateTime);
procedure WriteExtended(const Value: Extended);
procedure WriteDouble(const Value: Double);
procedure WriteInt64(Value: Int64); overload;
procedure WriteInteger(Value: Integer); overload;
procedure WriteCString(const Value: string); {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
procedure WriteCAnsiString(const Value: AnsiString);
procedure WriteCWideString(const Value: WideString);
// use WriteCString
procedure WriteShortString(const Value: ShortString);
procedure WriteSingle(const Value: Single);
procedure WriteSizedString(const Value: string); {$IFDEF SUPPORTS_INLINE} inline; {$ENDIF}
procedure WriteSizedAnsiString(const Value: AnsiString);
procedure WriteSizedWideString(const Value: WideString);
end;
TJclScopedStream = class(TJclStream)
private
FParentStream: TStream;
FStartPos: Int64;
FCurrentPos: Int64;
FMaxSize: Int64;
protected
procedure SetSize(const NewSize: Int64); override;
public
// scopedstream starting at the current position of the ParentStream
// if MaxSize is positive or null, read and write operations cannot overrun this size or the ParentStream limitation
// if MaxSize is negative, read and write operations are unlimited (up to the ParentStream limitation)
constructor Create(AParentStream: TStream; const AMaxSize: Int64 = -1); overload;
constructor Create(AParentStream: TStream; const AStartPos, AMaxSize: Int64); overload;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
property ParentStream: TStream read FParentStream;
property StartPos: Int64 read FStartPos;
property MaxSize: Int64 read FMaxSize write FMaxSize;
end;
TJclStreamSeekEvent = function(Sender: TObject; const Offset: Int64;
Origin: TSeekOrigin): Int64 of object;
TJclStreamReadEvent = function(Sender: TObject; var Buffer; Count: Longint): Longint of object;
TJclStreamWriteEvent = function(Sender: TObject; const Buffer;Count: Longint): Longint of object;
TJclStreamSizeEvent = procedure(Sender: TObject; const NewSize: Int64) of object;
TJclDelegatedStream = class(TJclStream)
private
FOnSeek: TJclStreamSeekEvent;
FOnRead: TJclStreamReadEvent;
FOnWrite: TJclStreamWriteEvent;
FOnSize: TJclStreamSizeEvent;
protected
procedure SetSize(const NewSize: Int64); override;
public
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
property OnSeek: TJclStreamSeekEvent read FOnSeek write FOnSeek;
property OnRead: TJclStreamReadEvent read FOnRead write FOnRead;
property OnWrite: TJclStreamWriteEvent read FOnWrite write FOnWrite;
property OnSize: TJclStreamSizeEvent read FOnSize write FOnSize;
end;
// ancestor classes for streams with checksums and encrypted streams
// data are stored in sectors: each BufferSize-d buffer is followed by FSectorOverHead bytes
// containing the checksum. In case of an encrypted stream, there is no byte
// but sector is encrypted
// reusing some code from TJclBufferedStream
TJclSectoredStream = class(TJclBufferedStream)
protected
FSectorOverHead: Longint;
function FlatToSectored(const Position: Int64): Int64;
function SectoredToFlat(const Position: Int64): Int64;
function GetCalcedSize: Int64; override;
function LoadBuffer: Boolean; override;
procedure DoAfterStreamChange; override;
procedure AfterBlockRead; virtual; // override to check protection
procedure BeforeBlockWrite; virtual; // override to compute protection
procedure SetSize(const NewSize: Int64); override;
public
constructor Create(AStorageStream: TStream; AOwnsStream: Boolean = False;
ASectorOverHead: Longint = 0);
procedure Flush; override;
end;
TJclCRC16Stream = class(TJclSectoredStream)
protected
procedure AfterBlockRead; override;
procedure BeforeBlockWrite; override;
public
constructor Create(AStorageStream: TStream; AOwnsStream: Boolean = False);
end;
TJclCRC32Stream = class(TJclSectoredStream)
protected
procedure AfterBlockRead; override;
procedure BeforeBlockWrite; override;
public
constructor Create(AStorageStream: TStream; AOwnsStream: Boolean = False);
end;
{$IFDEF COMPILER7_UP}
{$DEFINE SIZE64}
{$ENDIF ~COMPILER7_UP}
{$IFDEF FPC}
{$DEFINE SIZE64}
{$ENDIF FPC}
TJclSplitStream = class(TJclStream)
private
FVolume: TStream;
FVolumeIndex: Integer;
FVolumeMaxSize: Int64;
FPosition: Int64;
FVolumePosition: Int64;
FForcePosition: Boolean;
protected
function GetVolume(Index: Integer): TStream; virtual; abstract;
function GetVolumeMaxSize(Index: Integer): Int64; virtual; abstract;
function GetSize: Int64; {$IFDEF SIZE64}override;{$ENDIF SIZE64}
procedure SetSize(const NewSize: Int64); override;
function InternalLoadVolume(Index: Integer): Boolean;
public
constructor Create(AForcePosition: Boolean = False);
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
property ForcePosition: Boolean read FForcePosition write FForcePosition;
end;
TJclVolumeEvent = function(Index: Integer): TStream of object;
TJclVolumeMaxSizeEvent = function(Index: Integer): Int64 of object;
TJclDynamicSplitStream = class(TJclSplitStream)
private
FOnVolume: TJclVolumeEvent;
FOnVolumeMaxSize: TJclVolumeMaxSizeEvent;
protected
function GetVolume(Index: Integer): TStream; override;
function GetVolumeMaxSize(Index: Integer): Int64; override;
public
property OnVolume: TJclVolumeEvent read FOnVolume write FOnVolume;
property OnVolumeMaxSize: TJclVolumeMaxSizeEvent read FOnVolumeMaxSize
write FOnVolumeMaxSize;
end;
TJclSplitVolume = class
public
MaxSize: Int64;
Stream: TStream;
OwnStream: Boolean;
end;
TJclStaticSplitStream = class(TJclSplitStream)
private
FVolumes: TObjectList;
function GetVolumeCount: Integer;
protected
function GetVolume(Index: Integer): TStream; override;
function GetVolumeMaxSize(Index: Integer): Int64; override;
public
constructor Create(AForcePosition: Boolean = False);
destructor Destroy; override;
function AddVolume(AStream: TStream; AMaxSize: Int64 = 0;
AOwnStream: Boolean = False): Integer;
property VolumeCount: Integer read GetVolumeCount;
property Volumes[Index: Integer]: TStream read GetVolume;
property VolumeMaxSizes[Index: Integer]: Int64 read GetVolumeMaxSize;
end;
TJclStringStream = class
protected
FStream: TStream;
FOwnStream: Boolean;
FBOM: array of Byte;
FBufferSize: SizeInt;
FStrPosition: Int64; // current position in characters
FStrBuffer: TUCS4Array; // buffer for read/write operations
FStrBufferPosition: Int64; // position of the first character of the read/write buffer
FStrBufferCurrentSize: Int64; // numbers of characters available in str buffer
FStrBufferModifiedSize: Int64; // numbers of characters modified in str buffer
FStrBufferStart: Int64; // position of the first byte of the read/write buffer in stream
FStrBufferNext: Int64; // position of the next character following the read/write buffer in stream
FStrPeekPosition: Int64; // current peek position in characters
FStrPeekBuffer: TUCS4Array; // buffer for peek operations
FStrPeekBufferPosition: Int64; // index of the first character of the peek buffer
FStrPeekBufferCurrentSize: SizeInt; // numbers of characters available in peek buffer
FStrPeekBufferStart: Int64; // position of the first byte of the peek buffer in stream
FStrPeekBufferNext: Int64; // position of the next character following the peek buffer in stream
function LoadBuffer: Boolean;
function LoadPeekBuffer: Boolean;
function InternalGetNextChar(S: TStream; out Ch: UCS4): Boolean; virtual; abstract;
function InternalGetNextBuffer(S: TStream; var Buffer: TUCS4Array; Start, Count: SizeInt): Longint; virtual;
function InternalSetNextChar(S: TStream; Ch: UCS4): Boolean; virtual; abstract;
function InternalSetNextBuffer(S: TStream; const Buffer: TUCS4Array; Start, Count: SizeInt): Longint; virtual;
procedure InvalidateBuffers;
public
constructor Create(AStream: TStream; AOwnsStream: Boolean = False); virtual;
destructor Destroy; override;
procedure Flush; virtual;
function ReadString(var Buffer: string; Start, Count: Longint): Longint; overload;
function ReadString(BufferSize: Longint = StreamDefaultBufferSize): string; overload;
function ReadAnsiString(var Buffer: AnsiString; Start, Count: Longint): Longint; overload;
function ReadAnsiString(BufferSize: Longint = StreamDefaultBufferSize): AnsiString; overload;
function ReadWideString(var Buffer: WideString; Start, Count: Longint): Longint; overload;
function ReadWideString(BufferSize: Longint = StreamDefaultBufferSize): WideString; overload;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; virtual;
function WriteString(const Buffer: string; Start, Count: Longint): Longint;
function WriteAnsiString(const Buffer: AnsiString; Start, Count: Longint): Longint;
function WriteWideString(const Buffer: WideString; Start, Count: Longint): Longint;
function PeekChar(out Buffer: Char): Boolean;
function PeekAnsiChar(out Buffer: AnsiChar): Boolean;
function PeekUCS4(out Buffer: UCS4): Boolean;
function PeekWideChar(out Buffer: WideChar): Boolean;
function ReadChar(out Buffer: Char): Boolean;
function ReadAnsiChar(out Buffer: AnsiChar): Boolean;
function ReadUCS4(out Buffer: UCS4): Boolean;
function ReadWideChar(out Buffer: WideChar): Boolean;
function WriteChar(Value: Char): Boolean;
function WriteAnsiChar(Value: AnsiChar): Boolean;
function WriteUCS4(Value: UCS4): Boolean;
function WriteWideChar(Value: WideChar): Boolean;
function SkipBOM: LongInt; virtual;
function WriteBOM: Longint; virtual;
property BufferSize: SizeInt read FBufferSize write FBufferSize;
property PeekPosition: Int64 read FStrPeekPosition;
property Position: Int64 read FStrPosition;
property Stream: TStream read FStream;
property OwnStream: Boolean read FOwnStream;
end;
TJclStringStreamClass = class of TJclStringStream;
TJclAnsiStream = class(TJclStringStream)
private
FCodePage: Word;
protected
function InternalGetNextChar(S: TStream; out Ch: UCS4): Boolean; override;
function InternalGetNextBuffer(S: TStream; var Buffer: TUCS4Array; Start, Count: SizeInt): Longint; override;
function InternalSetNextChar(S: TStream; Ch: UCS4): Boolean; override;
function InternalSetNextBuffer(S: TStream; const Buffer: TUCS4Array; Start, Count: SizeInt): Longint; override;
public
constructor Create(AStream: TStream; AOwnsStream: Boolean = False); override;
property CodePage: Word read FCodePage write FCodePage;
end;
TJclUTF8Stream = class(TJclStringStream)
protected
function InternalGetNextChar(S: TStream; out Ch: UCS4): Boolean; override;
function InternalGetNextBuffer(S: TStream; var Buffer: TUCS4Array; Start, Count: SizeInt): Longint; override;
function InternalSetNextChar(S: TStream; Ch: UCS4): Boolean; override;
function InternalSetNextBuffer(S: TStream; const Buffer: TUCS4Array; Start, Count: SizeInt): Longint; override;
public
constructor Create(AStream: TStream; AOwnsStream: Boolean = False); override;
end;
TJclUTF16Stream = class(TJclStringStream)
protected
function InternalGetNextChar(S: TStream; out Ch: UCS4): Boolean; override;
function InternalGetNextBuffer(S: TStream; var Buffer: TUCS4Array; Start, Count: SizeInt): Longint; override;
function InternalSetNextChar(S: TStream; Ch: UCS4): Boolean; override;
function InternalSetNextBuffer(S: TStream; const Buffer: TUCS4Array; Start, Count: SizeInt): Longint; override;
public
constructor Create(AStream: TStream; AOwnsStream: Boolean = False); override;
end;
TJclStringEncoding = (seAnsi, seUTF8, seUTF16, seAuto);
TJclAutoStream = class(TJclStringStream)
private
FCodePage: Word;
FEncoding: TJclStringEncoding;
procedure SetCodePage(Value: Word);
protected
function InternalGetNextChar(S: TStream; out Ch: UCS4): Boolean; override;
function InternalGetNextBuffer(S: TStream; var Buffer: TUCS4Array; Start, Count: SizeInt): Longint; override;
function InternalSetNextChar(S: TStream; Ch: UCS4): Boolean; override;
function InternalSetNextBuffer(S: TStream; const Buffer: TUCS4Array; Start, Count: SizeInt): Longint; override;
public
constructor Create(AStream: TStream; AOwnsStream: Boolean = False); override;
function SkipBOM: LongInt; override;
property CodePage: Word read FCodePage write SetCodePage;
property Encoding: TJclStringEncoding read FEncoding;
end;
// buffered copy of all available bytes from Source to Dest
// returns the number of bytes that were copied
function StreamCopy(Source: TStream; Dest: TStream; BufferSize: Longint = StreamDefaultBufferSize): Int64;
// buffered copy of all available characters from Source to Dest
// retuns the number of characters (in specified encoding) that were copied
function StringStreamCopy(Source, Dest: TJclStringStream; BufferLength: Longint = StreamDefaultBufferSize): Int64;
function AnsiStringStreamCopy(Source, Dest: TJclStringStream; BufferLength: Longint = StreamDefaultBufferSize): Int64;
function WideStringStreamCopy(Source, Dest: TJclStringStream; BufferLength: Longint = StreamDefaultBufferSize): Int64;
// compares 2 streams for differencies
function CompareStreams(A, B : TStream; BufferSize: Longint = StreamDefaultBufferSize): Boolean;
// compares 2 files for differencies (calling CompareStreams)
function CompareFiles(const FileA, FileB: TFileName; BufferSize: Longint = StreamDefaultBufferSize): Boolean;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JCL\source\common';
Extra: '';
Data: nil
);
{$ENDIF UNITVERSIONING}
implementation
uses
{$IFDEF HAS_UNITSCOPE}
System.Types,
{$ENDIF HAS_UNITSCOPE}
JclResources,
JclCharsets,
JclMath,
JclSysUtils;
function StreamCopy(Source: TStream; Dest: TStream; BufferSize: Longint): Int64;
var
Buffer: array of Byte;
ByteCount: Longint;
begin
Result := 0;
SetLength(Buffer, BufferSize);
repeat
ByteCount := Source.Read(Buffer[0], BufferSize);
Result := Result + ByteCount;
Dest.WriteBuffer(Buffer[0], ByteCount);
until ByteCount < BufferSize;
end;
function StringStreamCopy(Source, Dest: TJclStringStream; BufferLength: Longint): Int64;
var
Buffer: string;
CharCount: Longint;
begin
Result := 0;
SetLength(Buffer, BufferLength);
repeat
CharCount := Source.ReadString(Buffer, 1, BufferLength);
Result := Result + CharCount;
CharCount := Dest.WriteString(Buffer, 1, CharCount);
until CharCount = 0;
end;
function AnsiStringStreamCopy(Source, Dest: TJclStringStream; BufferLength: Longint): Int64;
var
Buffer: AnsiString;
CharCount: Longint;
begin
Result := 0;
SetLength(Buffer, BufferLength);
repeat
CharCount := Source.ReadAnsiString(Buffer, 1, BufferLength);
Result := Result + CharCount;
CharCount := Dest.WriteAnsiString(Buffer, 1, CharCount);
until CharCount = 0;
end;
function WideStringStreamCopy(Source, Dest: TJclStringStream; BufferLength: Longint): Int64;
var
Buffer: WideString;
CharCount: Longint;
begin
Result := 0;
SetLength(Buffer, BufferLength);
repeat
CharCount := Source.ReadWideString(Buffer, 1, BufferLength);
Result := Result + CharCount;
CharCount := Dest.WriteWideString(Buffer, 1, CharCount);
until CharCount = 0;
end;
function CompareStreams(A, B : TStream; BufferSize: Longint): Boolean;
var
BufferA, BufferB: array of Byte;
ByteCountA, ByteCountB: Longint;
begin
SetLength(BufferA, BufferSize);
try
SetLength(BufferB, BufferSize);
try
repeat
ByteCountA := A.Read(BufferA[0], BufferSize);
ByteCountB := B.Read(BufferB[0], BufferSize);
Result := (ByteCountA = ByteCountB);
Result := Result and CompareMem(BufferA, BufferB, ByteCountA);
until (ByteCountA <> BufferSize) or (ByteCountB <> BufferSize) or not Result;
finally
SetLength(BufferB, 0);
end;
finally
SetLength(BufferA, 0);
end;
end;
function CompareFiles(const FileA, FileB: TFileName; BufferSize: Longint): Boolean;
var
A, B: TStream;
begin
A := TFileStream.Create(FileA, fmOpenRead or fmShareDenyWrite);
try
B := TFileStream.Create(FileB, fmOpenRead or fmShareDenyWrite);
try
Result := CompareStreams(A, B, BufferSize);
finally
B.Free;
end;
finally
A.Free;
end;
end;
//=== { TJclStream } =========================================================
function TJclStream.Seek(Offset: Longint; Origin: Word): Longint;
var
Result64: Int64;
begin
case Origin of
soFromBeginning:
Result64 := Seek(Int64(Offset), soBeginning);
soFromCurrent:
Result64 := Seek(Int64(Offset), soCurrent);
soFromEnd:
Result64 := Seek(Int64(Offset), soEnd);
else
Result64 := -1;
end;
if (Result64 < 0) or (Result64 > High(Longint)) then
Result64 := -1;
Result := Result64;
end;
procedure TJclStream.LoadFromFile(const FileName: TFileName;
BufferSize: Integer);
var
FS: TStream;
begin
FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(FS, BufferSize);
finally
FS.Free;
end;
end;
procedure TJclStream.LoadFromStream(Source: TStream; BufferSize: Integer);
begin
StreamCopy(Source, Self, BufferSize);
end;
procedure TJclStream.SaveToFile(const FileName: TFileName; BufferSize: Integer);
var
FS: TStream;
begin
FS := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(FS, BufferSize);
finally
FS.Free;
end;
end;
procedure TJclStream.SaveToStream(Dest: TStream; BufferSize: Integer);
begin
StreamCopy(Self, Dest, BufferSize);
end;
function TJclStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
// override to customize
Result := -1;
end;
procedure TJclStream.SetSize(NewSize: Longint);
begin
SetSize(Int64(NewSize));
end;
procedure TJclStream.SetSize(const NewSize: Int64);
begin
// override to customize
end;
//=== { TJclHandleStream } ===================================================
constructor TJclHandleStream.Create(AHandle: THandle);
begin
inherited Create;
FHandle := AHandle;
end;
function TJclHandleStream.Read(var Buffer; Count: Longint): Longint;
begin
Result := 0;
{$IFDEF MSWINDOWS}
if (Count <= 0) or not ReadFile(Handle, Buffer, DWORD(Count), DWORD(Result), nil) then
Result := 0;
{$ENDIF MSWINDOWS}
{$IFDEF LINUX}
Result := __read(Handle, Buffer, Count);
{$ENDIF LINUX}
end;
function TJclHandleStream.Write(const Buffer; Count: Longint): Longint;
begin
Result := 0;
{$IFDEF MSWINDOWS}
if (Count <= 0) or not WriteFile(Handle, Buffer, DWORD(Count), DWORD(Result), nil) then
Result := 0;
{$ENDIF MSWINDOWS}
{$IFDEF LINUX}
Result := __write(Handle, Buffer, Count);
{$ENDIF LINUX}
end;
{$IFDEF MSWINDOWS}
function TJclHandleStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
const
INVALID_SET_FILE_POINTER = -1;
type
TLarge = record
case Boolean of
False:
(OffsetLo: Longint;
OffsetHi: Longint);
True:
(Offset64: Int64);
end;
var
Offs: TLarge;
begin
Offs.Offset64 := Offset;
Offs.OffsetLo := SetFilePointer(Handle, Offs.OffsetLo, @Offs.OffsetHi, Ord(Origin));
if (Offs.OffsetLo = INVALID_SET_FILE_POINTER) and (GetLastError <> NO_ERROR) then
Result := -1
else
Result := Offs.Offset64;
end;
{$ENDIF MSWINDOWS}
{$IFDEF LINUX}
function TJclHandleStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
const
SeekOrigins: array [TSeekOrigin] of Cardinal = ( SEEK_SET {soBeginning}, SEEK_CUR {soCurrent}, SEEK_END {soEnd} );
begin
Result := lseek(Handle, Offset, SeekOrigins[Origin]);
end;
{$ENDIF LINUX}
procedure TJclHandleStream.SetSize(const NewSize: Int64);
begin
Seek(NewSize, soBeginning);
{$IFDEF MSWINDOWS}
if not SetEndOfFile(Handle) then
RaiseLastOSError;
{$ENDIF MSWINDOWS}
{$IFDEF LINUX}
if ftruncate(Handle, Position) = -1 then
raise EJclStreamError.CreateRes(@RsStreamsSetSizeError);
{$ENDIF LINUX}
end;
//=== { TJclFileStream } =====================================================
constructor TJclFileStream.Create(const FileName: TFileName; Mode: Word; Rights: Cardinal);
var
H: THandle;
{$IFDEF LINUX}
const
INVALID_HANDLE_VALUE = -1;
{$ENDIF LINUX}
begin
if Mode = fmCreate then
begin
{$IFDEF LINUX}
H := open(PChar(FileName), O_CREAT or O_RDWR, Rights);
{$ENDIF LINUX}
{$IFDEF MSWINDOWS}
H := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE,
0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
{$ENDIF MSWINDOWS}
inherited Create(H);
if Handle = INVALID_HANDLE_VALUE then
raise EJclStreamError.CreateResFmt(@RsStreamsCreateError, [FileName]);
end
else
begin
H := THandle(FileOpen(FileName, Mode));
inherited Create(H);
if Handle = INVALID_HANDLE_VALUE then
raise EJclStreamError.CreateResFmt(@RsStreamsOpenError, [FileName]);
end;
end;
destructor TJclFileStream.Destroy;
begin
{$IFDEF MSWINDOWS}
if Handle <> INVALID_HANDLE_VALUE then
CloseHandle(Handle);
{$ENDIF MSWINDOWS}
{$IFDEF LINUX}
__close(Handle);
{$ENDIF LINUX}
inherited Destroy;
end;
//=== { TJclEmptyStream } ====================================================
// a stream which stays empty no matter what you do
// so it is a Unix /dev/null equivalent
procedure TJclEmptyStream.SetSize(const NewSize: Int64);
begin
// nothing
end;
function TJclEmptyStream.Read(var Buffer; Count: Longint): Longint;
begin
// you cannot read anything
Result := 0;
end;
function TJclEmptyStream.Write(const Buffer; Count: Longint): Longint;
begin
// you cannot write anything
Result := 0;
end;
function TJclEmptyStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
if Offset <> 0 then
// seeking to anywhere except the position 0 is an error
Result := -1
else
Result := 0;
end;
//=== { TJclNullStream } =====================================================
// a stream which only keeps position and size, but no data
// so it is a Unix /dev/zero equivalent (?)
procedure TJclNullStream.SetSize(const NewSize: Int64);
begin
if NewSize > 0 then
FSize := NewSize
else
FSize := 0;
if FPosition > FSize then
FPosition := FSize;
end;
function TJclNullStream.Read(var Buffer; Count: Longint): Longint;
begin
if Count < 0 then
Count := 0;
// FPosition > FSize is possible!
if FSize - FPosition < Count then
Count := FSize - FPosition;
// does not read if beyond EOF
if Count > 0 then
begin
ResetMemory(Buffer, Count);
FPosition := FPosition + Count;
end;
Result := Count;
end;
function TJclNullStream.Write(const Buffer; Count: Longint): Longint;
begin
if Count < 0 then
Count := 0;
FPosition := FPosition + Count;
// writing when FPosition > FSize is possible!
if FPosition > FSize then
FSize := FPosition;
Result := Count;
end;
function TJclNullStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
var
Rel: Int64;
begin
case Origin of
soBeginning:
Rel := 0;
soCurrent:
Rel := FPosition;
soEnd:
Rel := FSize;
else
// force Rel + Offset = -1 (code is never reached)
Rel := Offset - 1;
end;
if Rel + Offset >= 0 then
begin
// all non-negative destination positions including beyond EOF are valid
FPosition := Rel + Offset;
Result := FPosition;
end
else
Result := -1;
end;
//=== { TJclRandomStream } ===================================================
// A TJclNullStream decendant which returns random data when read