Debian Codegraph @

plugins/wcx/zip/src/fparchive/abunzoutstm.pas

raw
1 (* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is TurboPower Abbrevia
15 *
16 * The Initial Developer of the Original Code is Craig Peterson
17 *
18 * Portions created by the Initial Developer are Copyright (C) 2011
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Craig Peterson <[email protected]>
23 *
24 * ***** END LICENSE BLOCK ***** *)
25
26 {*********************************************************}
27 {* ABBREVIA: AbUnzOutStm.pas *}
28 {*********************************************************}
29 {* ABBREVIA: UnZip output stream; progress and CRC32 *}
30 {*********************************************************}
31
32 unit AbUnzOutStm;
33
34 {$I AbDefine.inc}
35
36 interface
37
38 uses
39 SysUtils, Classes, AbArcTyp;
40
41 type
42 // Fixed-length read-only stream, limits reads to the range between
43 // the input stream's starting position and a specified size. Seek/Position
44 // are adjusted to be 0 based.
45 TAbUnzipSubsetStream = class( TStream )
46 private
47 FStream : TStream;
48 FStartPos: Int64;
49 FCurPos: Int64;
50 FEndPos: Int64;
51
52 public
53 constructor Create(aStream: TStream; aStreamSize: Int64);
54
55 function Read(var Buffer; Count: Longint): Longint; override;
56 function Write(const Buffer; Count: Longint): Longint; override;
57 function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
58 end;
59
60
61 // Write-only output stream, computes CRC32 and calls progress event
62 TAbUnzipOutputStream = class( TStream )
63 private
64 FBytesWritten : Int64;
65 FCRC32 : UInt32;
66 FCurrentProgress : Byte;
67 FStream : TStream;
68 FUncompressedSize : Int64;
69 FOnProgress : TAbProgressEvent;
70
71 function GetCRC32 : LongInt;
72
73 public
74 constructor Create(aStream : TStream);
75
76 function Read(var Buffer; Count: Longint): Longint; override;
77 function Write(const Buffer; Count: Longint): Longint; override;
78 function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
79
80 property CRC32 : LongInt
81 read GetCRC32;
82 property Stream : TStream
83 read FStream
84 write FStream;
85 property UncompressedSize : Int64
86 read FUncompressedSize
87 write FUncompressedSize;
88 property OnProgress : TAbProgressEvent
89 read FOnProgress
90 write FOnProgress;
91 end;
92
93
94 implementation
95
96 uses
97 Math, AbExcept, AbUtils, DCcrc32;
98
99 { TAbUnzipSubsetStream implementation ====================================== }
100
101 { -------------------------------------------------------------------------- }
102 constructor TAbUnzipSubsetStream.Create(aStream: TStream; aStreamSize: Int64);
103 begin
104 inherited Create;
105 FStream := aStream;
106 FStartPos := FStream.Position;
107 FCurPos := FStartPos;
108 FEndPos := FStartPos + aStreamSize;
109 end;
110 { -------------------------------------------------------------------------- }
111 function TAbUnzipSubsetStream.Read(var Buffer; Count: Longint): Longint;
112 begin
113 if Count > FEndPos - FCurPos then
114 Count := FEndPos - FCurPos;
115 if Count > 0 then begin
116 Result := FStream.Read(Buffer, Count);
117 Inc(FCurPos, Result);
118 end
119 else
120 Result := 0;
121 end;
122 { -------------------------------------------------------------------------- }
123 function TAbUnzipSubsetStream.Write(const Buffer; Count: Longint): Longint;
124 begin
125 raise EAbException.Create('TAbUnzipSubsetStream.Write not supported');
126 end;
127 { -------------------------------------------------------------------------- }
128 function TAbUnzipSubsetStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
129 var
130 OldPos: Int64;
131 begin
132 OldPos := FCurPos;
133 case Origin of
134 soBeginning: FCurPos := FStartPos + Offset;
135 soCurrent: FCurPos := FCurPos + Offset;
136 soEnd: FCurPos := FEndPos + Offset;
137 end;
138 if FCurPos < FStartPos then
139 FCurPos := FStartPos;
140 if FCurPos > FEndPos then
141 FCurPos := FEndPos;
142 if OldPos <> FCurPos then
143 FStream.Position := FCurPos;
144 Result := FCurPos - FStartPos;
145 end;
146 { -------------------------------------------------------------------------- }
147
148
149 { TAbUnzipOutputStream implementation ====================================== }
150
151 { -------------------------------------------------------------------------- }
152 constructor TAbUnzipOutputStream.Create(aStream: TStream);
153 begin
154 inherited Create;
155 FStream := aStream;
156 end;
157 { -------------------------------------------------------------------------- }
158 function TAbUnzipOutputStream.Read(var Buffer; Count: Integer): Longint;
159 begin
160 raise EAbException.Create('TAbUnzipOutputStream.Read not supported');
161 end;
162 { -------------------------------------------------------------------------- }
163 function TAbUnzipOutputStream.Write(const Buffer; Count: Longint): Longint;
164 var
165 Abort : Boolean;
166 NewProgress : Byte;
167 begin
168 Result := FStream.Write(Buffer, Count);
169
170 FCRC32 := crc32_16bytes(@Buffer, Count, FCRC32);
171
172 Inc( FBytesWritten, Result );
173 if Assigned( FOnProgress ) then begin
174 Abort := False;
175 NewProgress := AbPercentage(FBytesWritten, FUncompressedSize);
176 if (NewProgress <> FCurrentProgress) then begin
177 FOnProgress( NewProgress, Abort );
178 FCurrentProgress := NewProgress;
179 end;
180 if Abort then
181 raise EAbUserAbort.Create;
182 end;
183 end;
184 { -------------------------------------------------------------------------- }
185 function TAbUnzipOutputStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
186 begin
187 Result := FStream.Seek(Offset, Origin);
188 end;
189 { -------------------------------------------------------------------------- }
190 function TAbUnzipOutputStream.GetCRC32: LongInt;
191 begin
192 Result := LongInt(FCRC32);
193 end;
194
195 end.
196