Debian Codegraph @

plugins/wcx/zip/src/fparchive/abbitbkt.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
17 * TurboPower Software
18 *
19 * Portions created by the Initial Developer are Copyright (C) 1997-2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * ***** END LICENSE BLOCK ***** *)
25
26 {*********************************************************}
27 {* ABBREVIA: AbBitBkt.pas *}
28 {*********************************************************}
29 {* ABBREVIA: Bit bucket memory stream class *}
30 {*********************************************************}
31
32 unit AbBitBkt;
33
34 {$I AbDefine.inc}
35
36 interface
37
38 uses
39 Classes,
40 AbUtils;
41
42 type
43 TAbBitBucketStream = class(TStream)
44 private
45 FBuffer : {$IFDEF UNICODE}PByte{$ELSE}PAnsiChar{$ENDIF};
46 FBufSize : longint;
47 FBufPosn : longint;
48 FPosn : Int64;
49 FSize : Int64;
50 FTail : Int64;
51 protected
52 public
53 constructor Create(aBufSize : cardinal);
54 destructor Destroy; override;
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
59 procedure ForceSize(aSize : Int64);
60 end;
61
62 implementation
63
64 uses
65 Math, SysUtils, AbExcept;
66
67 {Notes: The buffer is a circular queue without a head pointer; FTail
68 is where data is next going to be written and it wraps
69 indescriminately. The buffer can never be empty--it is always
70 full (initially it is full of binary zeros.
71 The class is designed to act as a bit bucket for the test
72 feature of Abbrevia's zip code; it is not intended as a
73 complete class with many possible applications. It is designed
74 to be written to in a steady progression with some reading
75 back in the recently written stream (the buffer size details
76 how far back the Seek method will work). Seeking outside this
77 buffer will result in exceptions being generated.
78 For testing deflated files, the buffer size should be 32KB,
79 for imploded files, either 8KB or 4KB. The Create constructor
80 limits the buffer size to these values.}
81
82 {===TAbBitBucketStream===============================================}
83 constructor TAbBitBucketStream.Create(aBufSize : cardinal);
84 begin
85 inherited Create;
86 if (aBufSize <> 4096) and
87 (aBufSize <> 8192) and
88 (aBufSize <> 32768) then
89 FBufSize := 32768
90 else
91 FBufSize := aBufSize;
92 {add a 1KB leeway}
93 inc(FBufSize, 1024);
94 GetMem(FBuffer, FBufSize);
95 end;
96 {--------}
97 destructor TAbBitBucketStream.Destroy;
98 begin
99 if (FBuffer <> nil) then
100 FreeMem(FBuffer, FBufSize);
101 inherited Destroy;
102 end;
103 {--------}
104 procedure TAbBitBucketStream.ForceSize(aSize : Int64);
105 begin
106 FSize := aSize;
107 end;
108 {--------}
109 function TAbBitBucketStream.Read(var Buffer; Count : Longint) : Longint;
110 var
111 Chunk2Size : Int64;
112 Chunk1Size : Int64;
113 OutBuffer : PByte;
114 begin
115 OutBuffer := @Buffer;
116 {we cannot read more bytes than there is buffer}
117 if (Count > FBufSize) then
118 raise EAbBBSReadTooManyBytes.Create(Count);
119 {calculate the size of the chunks}
120 if (FBufPosn <= FTail) then begin
121 Chunk1Size := FTail - FBufPosn;
122 if (Chunk1Size > Count) then
123 Chunk1Size := Count;
124 Chunk2Size := 0;
125 end
126 else begin
127 Chunk1Size := FBufSize - FBufPosn;
128 if (Chunk1Size > Count) then begin
129 Chunk1Size := Count;
130 Chunk2Size := 0;
131 end
132 else begin
133 Chunk2Size := FTail;
134 if (Chunk2Size > (Count - Chunk1Size)) then
135 Chunk2Size := Count - Chunk1Size;
136 end
137 end;
138 {we cannot read more bytes than there are available}
139 if (Count > (Chunk1Size + Chunk2Size)) then
140 raise EAbBBSReadTooManyBytes.Create(Count);
141 {perform the read}
142 if (Chunk1Size > 0) then begin
143 Move(FBuffer[FBufPosn], OutBuffer^, Chunk1Size);
144 inc(FBufPosn, Chunk1Size);
145 inc(FPosn, Chunk1Size);
146 end;
147 if (Chunk2Size > 0) then begin
148 {we've wrapped}
149 Move(FBuffer[0], (OutBuffer + Chunk1Size)^, Chunk2Size);
150 FBufPosn := Chunk2Size;
151 inc(FPosn, Chunk2Size);
152 end;
153 Result := Count;
154 end;
155 {--------}
156 function TAbBitBucketStream.Write(const Buffer; Count : Longint) : Longint;
157 var
158 Chunk2Size : Int64;
159 Chunk1Size : Int64;
160 InBuffer : PByte;
161 Overage : longint;
162 begin
163 Result := Count;
164 InBuffer := @Buffer;
165 {we cannot write more bytes than there is buffer}
166 while Count > FBufSize do begin
167 Overage := Min(FBufSize, Count - FBufSize);
168 Write(InBuffer^, Overage);
169 Inc(PtrInt(InBuffer), Overage);
170 Dec(Count, Overage);
171 end;
172 {calculate the size of the chunks}
173 Chunk1Size := FBufSize - FTail;
174 if (Chunk1Size > Count) then begin
175 Chunk1Size := Count;
176 Chunk2Size := 0;
177 end
178 else begin
179 Chunk2Size := Count - Chunk1Size;
180 end;
181 {write the first chunk}
182 if (Chunk1Size > 0) then begin
183 Move(InBuffer^, FBuffer[FTail], Chunk1Size);
184 inc(FTail, Chunk1Size);
185 end;
186 {if the second chunk size is not zero, write the second chunk; note
187 that we have wrapped}
188 if (Chunk2Size > 0) then begin
189 Move((InBuffer + Chunk1Size)^, FBuffer[0], Chunk2Size);
190 FTail := Chunk2Size;
191 end;
192 {the stream size and position have changed}
193 inc(FSize, Count);
194 FPosn := FSize;
195 FBufPosn := FTail;
196 end;
197 {--------}
198 function TAbBitBucketStream.Seek(const Offset : Int64; Origin : TSeekOrigin): Int64;
199 var
200 Posn : Int64;
201 BytesBack : longint;
202 begin
203 {calculate the new position}
204 case Origin of
205 soBeginning :
206 Posn := Offset;
207 soCurrent :
208 Posn := FPosn + Offset;
209 soEnd :
210 if (Offset = 0) then begin
211 {special case: position at end of stream}
212 FBufPosn := FTail;
213 FPosn := FSize;
214 Result := FSize;
215 Exit;
216 end
217 else begin
218 Posn := FSize + Offset;
219 end;
220 else
221 raise EAbBBSInvalidOrigin.Create;
222 end;
223 {calculate whether the new position is within the buffer; if not,
224 raise exception}
225 if (Posn > FSize) or
226 (Posn <= (FSize - FBufSize)) then
227 raise EAbBBSSeekOutsideBuffer.Create;
228 {set the internal fields for the new position}
229 FPosn := Posn;
230 BytesBack := FSize - Posn;
231 if (BytesBack <= FTail) then
232 FBufPosn := FTail - BytesBack
233 else
234 FBufPosn := longint(FTail) + FBufSize - BytesBack;
235 {return the new position}
236 Result := Posn;
237 end;
238 {====================================================================}
239
240
241 end.