Debian Codegraph @

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

raw
1 (* ***** BEGIN LICENSE BLOCK *****
2 * Simple interface to lzma library
3 *
4 * Copyright (C) 2014-2023 Alexander Koblov ([email protected])
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * ***** END LICENSE BLOCK ***** *)
26
27 {********************************************************************}
28 {* ABBREVIA: AbXz.pas *}
29 {********************************************************************}
30 {* ABBREVIA: TXzDecompressionStream, TXzDecompressionStream classes *}
31 {********************************************************************}
32
33 unit AbXz;
34
35 {$mode delphi}
36 {$packrecords c}
37
38 interface
39
40 uses
41 Classes, SysUtils, CTypes;
42
43 type
44 TLzmaStreamRec = record
45 next_in: pbyte; (**< Pointer to the next input byte. *)
46 avail_in: csize_t; (**< Number of available input bytes in next_in. *)
47 total_in: cuint64; (**< Total number of bytes read by liblzma. *)
48
49 next_out: pbyte; (**< Pointer to the next output position. *)
50 avail_out: csize_t; (**< Amount of free space in next_out. *)
51 total_out: cuint64; (**< Total number of bytes written by liblzma. *)
52
53 (**
54 * \brief Custom memory allocation functions
55 *
56 * In most cases this is NULL which makes liblzma use
57 * the standard malloc() and free().
58 *)
59 allocator: pointer;
60
61 (** Internal state is not visible to applications. *)
62 internal: pointer;
63
64 (*
65 * Reserved space to allow possible future extensions without
66 * breaking the ABI. Excluding the initialization of this structure,
67 * you should not touch these, because the names of these variables
68 * may change.
69 *)
70 reserved_ptr1: pointer;
71 reserved_ptr2: pointer;
72 reserved_ptr3: pointer;
73 reserved_ptr4: pointer;
74 reserved_int1: cuint64;
75 reserved_int2: cuint64;
76 reserved_int3: csize_t;
77 reserved_int4: csize_t;
78 reserved_enum1: cuint32;
79 reserved_enum2: cuint32;
80 end;
81
82 type
83
84 { TXzCustomStream }
85
86 TXzCustomStream = class(TOwnerStream)
87 protected
88 FLzmaRec: TLzmaStreamRec;
89 FBuffer: array[Word] of Byte;
90 public
91 constructor Create(AStream: TStream);
92 destructor Destroy; override;
93 end;
94
95 { TXzCompressionStream }
96
97 TXzCompressionStream = class(TXzCustomStream)
98 private
99 procedure FlushBuffer;
100 function Check(Return: cint): cint;
101 public
102 constructor Create(ATarget: TStream; ALevel: Integer);
103 destructor Destroy; override;
104 function Write(const Buffer; Count: Longint): Longint; override;
105 function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
106 end;
107
108 { TXzDecompressionStream }
109
110 TXzDecompressionStream = class(TXzCustomStream)
111 private
112 function Check(Return: cint): cint;
113 public
114 constructor Create(ASource: TStream);
115 function Read(var Buffer; Count: Longint): Longint; override;
116 function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
117 end;
118
119 ELzmaError = class(Exception);
120 ELzmaCompressionError = class(ELzmaError);
121 ELzmaDecompressionError = class(ELzmaError);
122
123 implementation
124
125 uses
126 DynLibs, RtlConsts;
127
128 const
129 // Lzma return codes
130 LZMA_OK = 0;
131 LZMA_STREAM_END = 1;
132 LZMA_NO_CHECK = 2;
133 LZMA_UNSUPPORTED_CHECK = 3;
134 LZMA_GET_CHECK = 4;
135 LZMA_MEM_ERROR = 5;
136 LZMA_MEMLIMIT_ERROR = 6;
137 LZMA_FORMAT_ERROR = 7;
138 LZMA_OPTIONS_ERROR = 8;
139 LZMA_DATA_ERROR = 9;
140 LZMA_BUF_ERROR = 10;
141 LZMA_PROG_ERROR = 11;
142
143 const
144 // Lzma actions
145 LZMA_RUN = 0;
146 LZMA_SYNC_FLUSH = 1;
147 LZMA_FULL_FLUSH = 2;
148 LZMA_FINISH = 3;
149
150 const
151 // Type of the integrity check (Check ID)
152 LZMA_CHECK_CRC64 = 4;
153
154 const
155 // Decoding flags
156 LZMA_TELL_UNSUPPORTED_CHECK = $02;
157 LZMA_CONCATENATED = $08;
158
159 const
160 liblzma = {$IF DEFINED(MSWINDOWS)}
161 'liblzma.dll'
162 {$ELSEIF DEFINED(DARWIN)}
163 'liblzma.dylib'
164 {$ELSEIF DEFINED(UNIX)}
165 'liblzma.so.5'
166 {$IFEND};
167
168 var
169 hLzma: TLibHandle = NilHandle;
170
171 var
172 lzma_stream_decoder: function(var strm: TLzmaStreamRec; memlimit: cuint64; flags: cuint32): cint; cdecl;
173 lzma_easy_encoder: function(var strm: TLzmaStreamRec; preset: cuint32; check: cint): cint; cdecl;
174 lzma_code: function(var strm: TLzmaStreamRec; action: cint): cint; cdecl;
175 lzma_end: procedure(var strm: TLzmaStreamRec); cdecl;
176
177 procedure LzmaLoadLibrary;
178 begin
179 if hLzma <> NilHandle then Exit;
180
181 hLzma := LoadLibrary(liblzma);
182 if hLzma = NilHandle then
183 raise ELzmaError.Create('Lzma shared library not found');
184
185 @lzma_stream_decoder := GetProcAddress(hLzma, 'lzma_stream_decoder');
186 @lzma_easy_encoder := GetProcAddress(hLzma, 'lzma_easy_encoder');
187 @lzma_code := GetProcAddress(hLzma, 'lzma_code');
188 @lzma_end := GetProcAddress(hLzma, 'lzma_end');
189 end;
190
191 constructor TXzCustomStream.Create(AStream: TStream);
192 begin
193 LzmaLoadLibrary;
194 inherited Create(AStream);
195 end;
196
197 destructor TXzCustomStream.Destroy;
198 begin
199 if (@lzma_end <> nil) then lzma_end(FLzmaRec);
200 inherited Destroy;
201 end;
202
203 { TXzCompressionStream }
204
205 function TXzCompressionStream.Check(Return: cint): cint;
206 var
207 Message: String;
208 begin
209 Result:= Return;
210 if not (Return in [LZMA_OK, LZMA_STREAM_END]) then
211 begin
212 case Return of
213 LZMA_MEM_ERROR:
214 Message:= 'Memory allocation failed';
215 LZMA_OPTIONS_ERROR:
216 Message:= 'Specified preset is not supported';
217 LZMA_UNSUPPORTED_CHECK:
218 Message:= 'Specified integrity check is not supported';
219 LZMA_FORMAT_ERROR:
220 Message:= 'The input is not in the .xz format';
221 LZMA_DATA_ERROR:
222 Message:= 'File size limits exceeded';
223 else
224 Message:= Format('Unknown error, possibly a bug (error code %d)', [Return]);
225 end;
226 raise ELzmaCompressionError.Create(Message);
227 end;
228 end;
229
230 constructor TXzCompressionStream.Create(ATarget: TStream; ALevel: Integer);
231 begin
232 inherited Create(ATarget);
233 FLzmaRec.next_out:= FBuffer;
234 FLzmaRec.avail_out:= SizeOf(FBuffer);
235 Check(lzma_easy_encoder(FLzmaRec, ALevel, LZMA_CHECK_CRC64));
236 end;
237
238 function TXzCompressionStream.Write(const Buffer; Count: Longint): Longint;
239 begin
240 FLzmaRec.avail_in:= Count;
241 FLzmaRec.next_in:= @Buffer;
242 while FLzmaRec.avail_in > 0 do
243 begin
244 Check(lzma_code(FLzmaRec, LZMA_RUN));
245 if FLzmaRec.avail_out = 0 then FlushBuffer;
246 end;
247 Result:= Count;
248 end;
249
250 function TXzCompressionStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
251 begin
252 if (Offset = 0) and (Origin = soCurrent) then
253 Result:= FLzmaRec.total_in
254 else if (Origin = soBeginning) and (FLzmaRec.total_in = Offset) then
255 Result:= Offset
256 else begin
257 raise ELzmaCompressionError.CreateFmt(SStreamInvalidSeek, [ClassName]);
258 end;
259 end;
260
261 procedure TXzCompressionStream.FlushBuffer;
262 begin
263 FLzmaRec.next_out:= FBuffer;
264 FLzmaRec.avail_out:= SizeOf(FBuffer);
265 FSource.WriteBuffer(FBuffer, SizeOf(FBuffer));
266 end;
267
268 destructor TXzCompressionStream.Destroy;
269 var
270 State: cint;
271 begin
272 try
273 repeat
274 if FLzmaRec.avail_out = 0 then FlushBuffer;
275 State:= Check(lzma_code(FLzmaRec, LZMA_FINISH));
276 until State = LZMA_STREAM_END;
277
278 if FLzmaRec.avail_out < SizeOf(FBuffer) then
279 begin
280 FSource.WriteBuffer(FBuffer, SizeOf(FBuffer) - FLzmaRec.avail_out);
281 end;
282 finally
283 inherited Destroy;
284 end;
285 end;
286
287 { TXzDecompressionStream }
288
289 function TXzDecompressionStream.Check(Return: cint): cint;
290 var
291 Message: String;
292 begin
293 Result:= Return;
294 if not (Return in [LZMA_OK, LZMA_STREAM_END]) then
295 begin
296 case Return of
297 LZMA_MEM_ERROR:
298 Message:= 'Memory allocation failed';
299 LZMA_OPTIONS_ERROR:
300 Message:= 'Unsupported decompressor flags';
301 LZMA_FORMAT_ERROR:
302 Message:= 'The input is not in the .xz format';
303 LZMA_DATA_ERROR:
304 Message:= 'Compressed file is corrupt';
305 LZMA_BUF_ERROR:
306 Message:= 'Compressed file is truncated or otherwise corrupt';
307 else
308 Message:= Format('Unknown error, possibly a bug (error code %d)', [Return]);
309 end;
310 raise ELzmaDecompressionError.Create(Message);
311 end;
312 end;
313
314 constructor TXzDecompressionStream.Create(ASource: TStream);
315 const
316 flags = LZMA_TELL_UNSUPPORTED_CHECK or LZMA_CONCATENATED;
317 var
318 memory_limit: cuint64 = High(cuint64);
319 begin
320 inherited Create(ASource);
321 Check(lzma_stream_decoder(FLzmaRec, memory_limit, flags));
322 end;
323
324 function TXzDecompressionStream.Read(var Buffer; Count: Longint): Longint;
325 var
326 State: cint;
327 Action: cint = LZMA_RUN;
328 begin
329 FLzmaRec.avail_out:= Count;
330 FLzmaRec.next_out:= @Buffer;
331 while FLzmaRec.avail_out > 0 do
332 begin
333 if FLzmaRec.avail_in = 0 then
334 begin
335 FLzmaRec.next_in:= FBuffer;
336 FLzmaRec.avail_in:= FSource.Read(FBuffer, SizeOf(FBuffer));
337 if FLzmaRec.avail_in = 0 then Action:= LZMA_FINISH;
338 end;
339 State:= Check(lzma_code(FLzmaRec, Action));
340 if State = LZMA_STREAM_END then Break;
341 end;
342 Result:= Count - FLzmaRec.avail_out;
343 end;
344
345 function TXzDecompressionStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
346 begin
347 if (Offset >= 0) and (Origin = soCurrent) then
348 begin
349 if (Offset > 0) then Discard(Offset);
350 Result:= FLzmaRec.total_out;
351 end
352 else if (Origin = soBeginning) and (FLzmaRec.total_out = Offset) then
353 Result:= Offset
354 else begin
355 raise ELzmaDecompressionError.CreateFmt(SStreamInvalidSeek, [ClassName]);
356 end;
357 end;
358
359 end.
360