| 1 |
{ |
| 2 |
Copyright 2013-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
|
| 15 |
Original copyright of gzio.pas (part of FPC RTL, |
| 16 |
licensed on LGPL with static linking exception, |
| 17 |
exactly the same license as Castle Game Engine): |
| 18 |
|
| 19 |
Pascal unit based on gzio.c -- IO on .gz files |
| 20 |
Copyright (C) 1995-1998 Jean-loup Gailly. |
| 21 |
|
| 22 |
Define NO_DEFLATE to compile this file without the compression code |
| 23 |
|
| 24 |
Pascal tranlastion based on code contributed by Francisco Javier Crespo |
| 25 |
Copyright (C) 1998 by Jacques Nomssi Nzali |
| 26 |
For conditions of distribution and use, see copyright notice in readme.txt |
| 27 |
|
| 28 |
---------------------------------------------------------------------------- |
| 29 |
} |
| 30 |
|
| 31 |
{ Rework of packages/paszlib/src/gzio.pas from FPC RTL |
| 32 |
to allow reading/writing gzip from/to Pascal streams (TStream) |
| 33 |
instead of only normal files. |
| 34 |
|
| 35 |
Together with CastleInternalZStream, this allows to compress/decompress by gzip |
| 36 |
entirely in memory, without any temporary files underneath. |
| 37 |
|
| 38 |
(Yes, the original gzio.pas code looks quite old-school. |
| 39 |
It is a C code translated literally to an old Pascal, without any |
| 40 |
modern features like classes.) |
| 41 |
|
| 42 |
@exclude } |
| 43 |
unit CastleInternalGzio; |
| 44 |
|
| 45 |
{$I castleconf.inc} |
| 46 |
|
| 47 |
{$ifndef FPC} |
| 48 |
{$message fatal 'This internal unit is only for FPC, not Delphi.'} |
| 49 |
{$endif} |
| 50 |
|
| 51 |
interface |
| 52 |
|
| 53 |
uses CTypes, zbase, crc, zdeflate, zinflate, Classes; |
| 54 |
|
| 55 |
type gzFile = pointer; |
| 56 |
type z_off_t = CInt32; |
| 57 |
|
| 58 |
function gzopen (const Stream: TStream; const WriteMode: boolean) : gzFile; |
| 59 |
function gzread (f:gzFile; buf:pointer; len:cardinal) : integer; |
| 60 |
function gzgetc (f:gzfile) : integer; |
| 61 |
function gzgets (f:gzfile; buf:Pchar; len:integer) : Pchar; |
| 62 |
|
| 63 |
{$ifndef NO_DEFLATE} |
| 64 |
function gzwrite (f:gzFile; buf:pointer; len:cardinal) : integer; |
| 65 |
function gzputc (f:gzfile; c:char) : integer; |
| 66 |
function gzputs (f:gzfile; s:Pchar) : integer; |
| 67 |
function gzflush (f:gzFile; flush:integer) : integer; |
| 68 |
{$endif} |
| 69 |
|
| 70 |
function gzerror (f:gzfile) : string; |
| 71 |
function gzseek (f:gzfile; offset:z_off_t; whence:integer) : z_off_t; |
| 72 |
function gztell (f:gzfile) : z_off_t; |
| 73 |
function gzclose (f:gzFile) : integer; |
| 74 |
function gzsetparams (f:gzfile; level:integer; strategy:integer) : integer; |
| 75 |
function gzrewind (f:gzFile) : integer; |
| 76 |
function gzeof (f:gzfile) : boolean; |
| 77 |
|
| 78 |
const |
| 79 |
SEEK_SET {: z_off_t} = 0; { seek from beginning of file } |
| 80 |
SEEK_CUR {: z_off_t} = 1; { seek from current position } |
| 81 |
SEEK_END {: z_off_t} = 2; |
| 82 |
|
| 83 |
implementation |
| 84 |
|
| 85 |
uses SysUtils; |
| 86 |
|
| 87 |
const |
| 88 |
Z_EOF = -1; { same value as in STDIO.H } |
| 89 |
Z_BUFSIZE = 16384; |
| 90 |
{ Z_PRINTF_BUFSIZE = 4096; } |
| 91 |
|
| 92 |
|
| 93 |
gz_magic : array[0..1] of byte = ($1F, $8B); { gzip magic header } |
| 94 |
|
| 95 |
{ gzip flag byte } |
| 96 |
|
| 97 |
//ASCII_FLAG = $01; { bit 0 set: file probably ascii text } |
| 98 |
HEAD_CRC = $02; { bit 1 set: header CRC present } |
| 99 |
EXTRA_FIELD = $04; { bit 2 set: extra field present } |
| 100 |
ORIG_NAME = $08; { bit 3 set: original file name present } |
| 101 |
COMMENT = $10; { bit 4 set: file comment present } |
| 102 |
RESERVED = $E0; { bits 5..7: reserved } |
| 103 |
|
| 104 |
type gz_stream = record |
| 105 |
stream : z_stream; |
| 106 |
z_err : integer; { error code for last stream operation } |
| 107 |
z_eof : boolean; { set if end of input file } |
| 108 |
gzStream : TStream; { .gz file } |
| 109 |
inbuf : Pbyte; { input buffer } |
| 110 |
outbuf : Pbyte; { output buffer } |
| 111 |
crc : cardinal; { crc32 of uncompressed data } |
| 112 |
msg : string[79]; { error message - limit 79 chars } |
| 113 |
transparent : boolean; { true if input file is not a .gz file } |
| 114 |
WriteMode : boolean; { false means that we're in read mode } |
| 115 |
startpos : CInt32; { start of compressed data in file (header skipped) } |
| 116 |
end; |
| 117 |
|
| 118 |
type gz_streamp = ^gz_stream; |
| 119 |
|
| 120 |
function destroy (var s:gz_streamp) : integer; forward; |
| 121 |
procedure check_header(s:gz_streamp); forward; |
| 122 |
|
| 123 |
|
| 124 |
{ GZOPEN ==================================================================== |
| 125 |
|
| 126 |
Opens a gzip (.gz) file for reading or writing. |
| 127 |
|
| 128 |
gzopen can be used to open a file which is not in gzip format; in this |
| 129 |
case, gzread will directly read from the file without decompression. |
| 130 |
|
| 131 |
gzopen raises exception if the file could not be opened |
| 132 |
or if there was insufficient memory to allocate the (de)compression state |
| 133 |
(zlib error is Z_MEM_ERROR). |
| 134 |
|
| 135 |
============================================================================} |
| 136 |
|
| 137 |
function gzopen(const Stream: TStream; const WriteMode:boolean) : gzFile; |
| 138 |
|
| 139 |
var |
| 140 |
err : integer; |
| 141 |
level : integer; { compression level } |
| 142 |
strategy : integer; { compression strategy } |
| 143 |
s : gz_streamp; |
| 144 |
{$IFNDEF NO_DEFLATE} |
| 145 |
gzheader : array [0..9] of byte; |
| 146 |
{$ENDIF} |
| 147 |
begin |
| 148 |
GetMem (s,sizeof(gz_stream)); |
| 149 |
|
| 150 |
level := Z_DEFAULT_COMPRESSION; |
| 151 |
strategy := Z_DEFAULT_STRATEGY; |
| 152 |
|
| 153 |
s^.stream.next_in := nil; |
| 154 |
s^.stream.next_out := nil; |
| 155 |
s^.stream.avail_in := 0; |
| 156 |
s^.stream.avail_out := 0; |
| 157 |
s^.z_err := Z_OK; |
| 158 |
s^.z_eof := false; |
| 159 |
s^.inbuf := nil; |
| 160 |
s^.outbuf := nil; |
| 161 |
s^.crc := crc32(0, nil, 0); |
| 162 |
s^.msg := ''; |
| 163 |
s^.transparent := false; |
| 164 |
s^.gzStream := Stream; |
| 165 |
s^.WriteMode := WriteMode; |
| 166 |
|
| 167 |
if WriteMode then begin |
| 168 |
{$IFDEF NO_DEFLATE} |
| 169 |
err := Z_STREAM_ERROR; |
| 170 |
{$ELSE} |
| 171 |
err := deflateInit2 (s^.stream, level, Z_DEFLATED, -MAX_WBITS, |
| 172 |
DEF_MEM_LEVEL, strategy); |
| 173 |
{ windowBits is passed < 0 to suppress zlib header } |
| 174 |
|
| 175 |
GetMem (s^.outbuf, Z_BUFSIZE); |
| 176 |
s^.stream.next_out := s^.outbuf; |
| 177 |
{$ENDIF} |
| 178 |
if (err <> Z_OK) or (s^.outbuf = nil) then begin |
| 179 |
destroy(s); |
| 180 |
raise Exception.Create('Error when opening gz stream'); |
| 181 |
end; |
| 182 |
end |
| 183 |
|
| 184 |
else begin |
| 185 |
GetMem (s^.inbuf, Z_BUFSIZE); |
| 186 |
s^.stream.next_in := s^.inbuf; |
| 187 |
|
| 188 |
err := inflateInit2_ (s^.stream, -MAX_WBITS, ZLIB_VERSION, sizeof(z_stream)); |
| 189 |
{ windowBits is passed < 0 to tell that there is no zlib header } |
| 190 |
|
| 191 |
if (err <> Z_OK) or (s^.inbuf = nil) then begin |
| 192 |
destroy(s); |
| 193 |
raise Exception.Create('Error when opening gz stream'); |
| 194 |
end; |
| 195 |
end; |
| 196 |
|
| 197 |
s^.stream.avail_out := Z_BUFSIZE; |
| 198 |
|
| 199 |
if WriteMode then begin { Write a very simple .gz header } |
| 200 |
{$IFNDEF NO_DEFLATE} |
| 201 |
gzheader [0] := gz_magic [0]; |
| 202 |
gzheader [1] := gz_magic [1]; |
| 203 |
gzheader [2] := Z_DEFLATED; { method } |
| 204 |
gzheader [3] := 0; { flags } |
| 205 |
gzheader [4] := 0; { time[0] } |
| 206 |
gzheader [5] := 0; { time[1] } |
| 207 |
gzheader [6] := 0; { time[2] } |
| 208 |
gzheader [7] := 0; { time[3] } |
| 209 |
gzheader [8] := 0; { xflags } |
| 210 |
gzheader [9] := {$ifdef UNIX} 3 {$else} 0 {$endif}; { OS code, see http://www.gzip.org/zlib/rfc-gzip.html#header-trailer } |
| 211 |
s^.gzStream.WriteBuffer(gzheader, 10); |
| 212 |
s^.startpos := CInt32(10); |
| 213 |
{$ENDIF} |
| 214 |
end |
| 215 |
else begin |
| 216 |
check_header(s); { skip the .gz header } |
| 217 |
s^.startpos := s^.gzStream.Position - s^.stream.avail_in; |
| 218 |
end; |
| 219 |
|
| 220 |
gzopen := gzFile(s); |
| 221 |
end; |
| 222 |
|
| 223 |
|
| 224 |
{ GZSETPARAMS =============================================================== |
| 225 |
|
| 226 |
Update the compression level and strategy. |
| 227 |
|
| 228 |
============================================================================} |
| 229 |
|
| 230 |
function gzsetparams (f:gzfile; level:integer; strategy:integer) : integer; |
| 231 |
|
| 232 |
var |
| 233 |
|
| 234 |
s : gz_streamp; |
| 235 |
written: integer; |
| 236 |
|
| 237 |
begin |
| 238 |
|
| 239 |
s := gz_streamp(f); |
| 240 |
|
| 241 |
if (s = nil) or not s^.WriteMode then begin |
| 242 |
gzsetparams := Z_STREAM_ERROR; |
| 243 |
exit; |
| 244 |
end; |
| 245 |
|
| 246 |
{ Make room to allow flushing } |
| 247 |
if (s^.stream.avail_out = 0) then begin |
| 248 |
s^.stream.next_out := s^.outbuf; |
| 249 |
written := s^.gzStream.Write(s^.outbuf^, Z_BUFSIZE); |
| 250 |
if (written <> Z_BUFSIZE) then s^.z_err := Z_ERRNO; |
| 251 |
s^.stream.avail_out := Z_BUFSIZE; |
| 252 |
end; |
| 253 |
|
| 254 |
gzsetparams := deflateParams (s^.stream, level, strategy); |
| 255 |
end; |
| 256 |
|
| 257 |
|
| 258 |
{ GET_BYTE ================================================================== |
| 259 |
|
| 260 |
Read a byte from a gz_stream. Updates next_in and avail_in. |
| 261 |
Returns EOF for end of file. |
| 262 |
IN assertion: the stream s has been sucessfully opened for reading. |
| 263 |
|
| 264 |
============================================================================} |
| 265 |
|
| 266 |
function get_byte (s:gz_streamp) : integer; |
| 267 |
|
| 268 |
begin |
| 269 |
if s^.z_eof then begin |
| 270 |
get_byte := Z_EOF; |
| 271 |
exit; |
| 272 |
end; |
| 273 |
|
| 274 |
if s^.stream.avail_in=0 then begin |
| 275 |
s^.stream.avail_in := s^.gzStream.Read(s^.inbuf^, Z_BUFSIZE); |
| 276 |
if s^.stream.avail_in=0 then begin |
| 277 |
s^.z_eof := true; |
| 278 |
get_byte := Z_EOF; |
| 279 |
exit; |
| 280 |
end; |
| 281 |
s^.stream.next_in := s^.inbuf; |
| 282 |
end; |
| 283 |
|
| 284 |
Dec(s^.stream.avail_in); |
| 285 |
get_byte := s^.stream.next_in^; |
| 286 |
Inc(s^.stream.next_in); |
| 287 |
end; |
| 288 |
|
| 289 |
|
| 290 |
{ GETLONG =================================================================== |
| 291 |
|
| 292 |
Reads a CInt32 in LSB order from the given gz_stream. |
| 293 |
|
| 294 |
============================================================================} |
| 295 |
{ |
| 296 |
function getLong (s:gz_streamp) : cardinal; |
| 297 |
var |
| 298 |
x : array [0..3] of byte; |
| 299 |
i : byte; |
| 300 |
c : integer; |
| 301 |
n1 : CInt32; |
| 302 |
n2 : CInt32; |
| 303 |
begin |
| 304 |
|
| 305 |
for i:=0 to 3 do begin |
| 306 |
c := get_byte(s); |
| 307 |
if (c = Z_EOF) then s^.z_err := Z_DATA_ERROR; |
| 308 |
x[i] := (c and $FF) |
| 309 |
end; |
| 310 |
n1 := (ush(x[3] shl 8)) or x[2]; |
| 311 |
n2 := (ush(x[1] shl 8)) or x[0]; |
| 312 |
getlong := (n1 shl 16) or n2; |
| 313 |
end; |
| 314 |
} |
| 315 |
function getLong(s : gz_streamp) : cardinal; |
| 316 |
var |
| 317 |
x : packed array [0..3] of byte; |
| 318 |
c : integer; |
| 319 |
begin |
| 320 |
{ x := cardinal(get_byte(s)); - you can't do this with TP, no unsigned CInt32 } |
| 321 |
{$ifdef ENDIAN_BIG} |
| 322 |
x[3] := Byte(get_byte(s)); |
| 323 |
x[2] := Byte(get_byte(s)); |
| 324 |
x[1] := Byte(get_byte(s)); |
| 325 |
c := get_byte(s); |
| 326 |
x[0] := Byte(c); |
| 327 |
{$else} |
| 328 |
x[0] := Byte(get_byte(s)); |
| 329 |
x[1] := Byte(get_byte(s)); |
| 330 |
x[2] := Byte(get_byte(s)); |
| 331 |
c := get_byte(s); |
| 332 |
x[3] := Byte(c); |
| 333 |
{$endif} |
| 334 |
if (c = Z_EOF) then |
| 335 |
s^.z_err := Z_DATA_ERROR; |
| 336 |
GetLong := cardinal(x); |
| 337 |
end; |
| 338 |
|
| 339 |
|
| 340 |
{ CHECK_HEADER ============================================================== |
| 341 |
|
| 342 |
Check the gzip header of a gz_stream opened for reading. |
| 343 |
Set the stream mode to transparent if the gzip magic header is not present. |
| 344 |
Set s^.err to Z_DATA_ERROR if the magic header is present but the rest of |
| 345 |
the header is incorrect. |
| 346 |
|
| 347 |
IN assertion: the stream s has already been created sucessfully; |
| 348 |
s^.stream.avail_in is zero for the first time, but may be non-zero |
| 349 |
for concatenated .gz files |
| 350 |
|
| 351 |
============================================================================} |
| 352 |
|
| 353 |
procedure check_header (s:gz_streamp); |
| 354 |
|
| 355 |
var |
| 356 |
|
| 357 |
method : integer; { method byte } |
| 358 |
flags : integer; { flags byte } |
| 359 |
len : cardinal; |
| 360 |
c : integer; |
| 361 |
|
| 362 |
begin |
| 363 |
|
| 364 |
{ Check the gzip magic header } |
| 365 |
for len := 0 to 1 do begin |
| 366 |
c := get_byte(s); |
| 367 |
if (c <> gz_magic[len]) then begin |
| 368 |
if (len <> 0) then begin |
| 369 |
Inc(s^.stream.avail_in); |
| 370 |
Dec(s^.stream.next_in); |
| 371 |
end; |
| 372 |
if (c <> Z_EOF) then begin |
| 373 |
Inc(s^.stream.avail_in); |
| 374 |
Dec(s^.stream.next_in); |
| 375 |
s^.transparent := TRUE; |
| 376 |
end; |
| 377 |
if (s^.stream.avail_in <> 0) then s^.z_err := Z_OK |
| 378 |
else s^.z_err := Z_STREAM_END; |
| 379 |
exit; |
| 380 |
end; |
| 381 |
end; |
| 382 |
|
| 383 |
method := get_byte(s); |
| 384 |
flags := get_byte(s); |
| 385 |
if (method <> Z_DEFLATED) or ((flags and RESERVED) <> 0) then begin |
| 386 |
s^.z_err := Z_DATA_ERROR; |
| 387 |
exit; |
| 388 |
end; |
| 389 |
|
| 390 |
for len := 0 to 5 do get_byte(s); { Discard time, xflags and OS code } |
| 391 |
|
| 392 |
if ((flags and EXTRA_FIELD) <> 0) then begin { skip the extra field } |
| 393 |
len := cardinal(get_byte(s)); |
| 394 |
len := len + (cardinal(get_byte(s)) shl 8); |
| 395 |
{ len is garbage if EOF but the loop below will quit anyway } |
| 396 |
while (len <> 0) and (get_byte(s) <> Z_EOF) do Dec(len); |
| 397 |
end; |
| 398 |
|
| 399 |
if ((flags and ORIG_NAME) <> 0) then begin { skip the original file name } |
| 400 |
repeat |
| 401 |
c := get_byte(s); |
| 402 |
until (c = 0) or (c = Z_EOF); |
| 403 |
end; |
| 404 |
|
| 405 |
if ((flags and COMMENT) <> 0) then begin { skip the .gz file comment } |
| 406 |
repeat |
| 407 |
c := get_byte(s); |
| 408 |
until (c = 0) or (c = Z_EOF); |
| 409 |
end; |
| 410 |
|
| 411 |
if ((flags and HEAD_CRC) <> 0) then begin { skip the header crc } |
| 412 |
get_byte(s); |
| 413 |
get_byte(s); |
| 414 |
end; |
| 415 |
|
| 416 |
if (s^.z_eof = true) then |
| 417 |
s^.z_err := Z_DATA_ERROR |
| 418 |
else |
| 419 |
s^.z_err := Z_OK; |
| 420 |
|
| 421 |
end; |
| 422 |
|
| 423 |
|
| 424 |
{ DESTROY =================================================================== |
| 425 |
|
| 426 |
Cleanup then free the given gz_stream. Return a zlib error code. |
| 427 |
Try freeing in the reverse order of allocations. |
| 428 |
|
| 429 |
============================================================================} |
| 430 |
|
| 431 |
function destroy (var s:gz_streamp) : integer; |
| 432 |
|
| 433 |
begin |
| 434 |
|
| 435 |
destroy := Z_OK; |
| 436 |
|
| 437 |
if not Assigned (s) then begin |
| 438 |
destroy := Z_STREAM_ERROR; |
| 439 |
exit; |
| 440 |
end; |
| 441 |
|
| 442 |
if (s^.stream.state <> nil) then begin |
| 443 |
if s^.WriteMode then |
| 444 |
begin |
| 445 |
{$IFDEF NO_DEFLATE} |
| 446 |
destroy := Z_STREAM_ERROR; |
| 447 |
{$ELSE} |
| 448 |
destroy := deflateEnd(s^.stream); |
| 449 |
{$ENDIF} |
| 450 |
end else |
| 451 |
begin |
| 452 |
destroy := inflateEnd(s^.stream); |
| 453 |
end; |
| 454 |
end; |
| 455 |
|
| 456 |
if (s^.z_err < 0) then destroy := s^.z_err; |
| 457 |
|
| 458 |
if Assigned (s^.inbuf) then |
| 459 |
FreeMem(s^.inbuf, Z_BUFSIZE); |
| 460 |
if Assigned (s^.outbuf) then |
| 461 |
FreeMem(s^.outbuf, Z_BUFSIZE); |
| 462 |
FreeMem(s, sizeof(gz_stream)); |
| 463 |
|
| 464 |
end; |
| 465 |
|
| 466 |
|
| 467 |
{ GZREAD ==================================================================== |
| 468 |
|
| 469 |
Reads the given number of uncompressed bytes from the compressed file. |
| 470 |
If the input file was not in gzip format, gzread copies the given number |
| 471 |
of bytes into the buffer. |
| 472 |
|
| 473 |
gzread returns the number of uncompressed bytes actually read |
| 474 |
(0 for end of file, -1 for error). |
| 475 |
|
| 476 |
============================================================================} |
| 477 |
|
| 478 |
function gzread (f:gzFile; buf:pointer; len:cardinal) : integer; |
| 479 |
|
| 480 |
var |
| 481 |
|
| 482 |
s : gz_streamp; |
| 483 |
start : Pbyte; |
| 484 |
n : cardinal; |
| 485 |
crclen : cardinal; { Buffer length to update CRC32 } |
| 486 |
filecrc : cardinal; { CRC32 stored in GZIP'ed file } |
| 487 |
filelen : cardinal; { Total lenght of uncompressed file } |
| 488 |
bytes : integer; { bytes actually read in I/O Read } |
| 489 |
total_in : cardinal; |
| 490 |
total_out : cardinal; |
| 491 |
begin |
| 492 |
|
| 493 |
s := gz_streamp(f); |
| 494 |
start := Pbyte(buf); { starting point for crc computation } |
| 495 |
|
| 496 |
if (s = nil) or s^.WriteMode then begin |
| 497 |
gzread := Z_STREAM_ERROR; |
| 498 |
exit; |
| 499 |
end; |
| 500 |
|
| 501 |
if (s^.z_err = Z_DATA_ERROR) or (s^.z_err = Z_ERRNO) then begin |
| 502 |
gzread := -1; |
| 503 |
exit; |
| 504 |
end; |
| 505 |
|
| 506 |
if (s^.z_err = Z_STREAM_END) then begin |
| 507 |
gzread := 0; { EOF } |
| 508 |
exit; |
| 509 |
end; |
| 510 |
|
| 511 |
s^.stream.next_out := Pbyte(buf); |
| 512 |
s^.stream.avail_out := len; |
| 513 |
|
| 514 |
while (s^.stream.avail_out <> 0) do begin |
| 515 |
|
| 516 |
if (s^.transparent = true) then begin |
| 517 |
{ Copy first the lookahead bytes: } |
| 518 |
n := s^.stream.avail_in; |
| 519 |
if (n > s^.stream.avail_out) then n := s^.stream.avail_out; |
| 520 |
if (n > 0) then begin |
| 521 |
move(s^.stream.next_in^,s^.stream.next_out^,n); |
| 522 |
inc (s^.stream.next_out, n); |
| 523 |
inc (s^.stream.next_in, n); |
| 524 |
dec (s^.stream.avail_out, n); |
| 525 |
dec (s^.stream.avail_in, n); |
| 526 |
end; |
| 527 |
if (s^.stream.avail_out > 0) then begin |
| 528 |
bytes := s^.gzStream.Read(s^.stream.next_out^, s^.stream.avail_out); |
| 529 |
dec (s^.stream.avail_out, cardinal(bytes)); |
| 530 |
end; |
| 531 |
dec (len, s^.stream.avail_out); |
| 532 |
inc (s^.stream.total_in, cardinal(len)); |
| 533 |
inc (s^.stream.total_out, cardinal(len)); |
| 534 |
gzread := integer(len); |
| 535 |
exit; |
| 536 |
end; { IF transparent } |
| 537 |
|
| 538 |
if (s^.stream.avail_in = 0) and (s^.z_eof = false) then |
| 539 |
begin |
| 540 |
s^.stream.avail_in := s^.gzStream.Read(s^.inbuf^, Z_BUFSIZE); |
| 541 |
if (s^.stream.avail_in = 0) then begin |
| 542 |
s^.z_eof := true; |
| 543 |
end; |
| 544 |
s^.stream.next_in := s^.inbuf; |
| 545 |
end; |
| 546 |
|
| 547 |
s^.z_err := inflate(s^.stream, Z_NO_FLUSH); |
| 548 |
|
| 549 |
if (s^.z_err = Z_STREAM_END) then begin |
| 550 |
crclen := 0; |
| 551 |
crclen:=s^.stream.next_out-start; |
| 552 |
{ Check CRC and original size } |
| 553 |
s^.crc := crc32(s^.crc, start, crclen); |
| 554 |
start := s^.stream.next_out; |
| 555 |
|
| 556 |
filecrc := getLong (s); |
| 557 |
filelen := getLong (s); |
| 558 |
|
| 559 |
if (s^.crc <> filecrc) or (s^.stream.total_out <> filelen) |
| 560 |
then s^.z_err := Z_DATA_ERROR |
| 561 |
else begin |
| 562 |
{ Check for concatenated .gz files: } |
| 563 |
check_header(s); |
| 564 |
if (s^.z_err = Z_OK) then begin |
| 565 |
total_in := s^.stream.total_in; |
| 566 |
total_out := s^.stream.total_out; |
| 567 |
|
| 568 |
inflateReset (s^.stream); |
| 569 |
s^.stream.total_in := total_in; |
| 570 |
s^.stream.total_out := total_out; |
| 571 |
s^.crc := crc32 (0, nil, 0); |
| 572 |
end; |
| 573 |
end; {IF-THEN-ELSE} |
| 574 |
end; |
| 575 |
|
| 576 |
if (s^.z_err <> Z_OK) or (s^.z_eof = true) then break; |
| 577 |
|
| 578 |
end; {WHILE} |
| 579 |
|
| 580 |
crclen:=s^.stream.next_out-start; |
| 581 |
s^.crc := crc32 (s^.crc, start, crclen); |
| 582 |
gzread := integer(len - s^.stream.avail_out); |
| 583 |
|
| 584 |
end; |
| 585 |
|
| 586 |
|
| 587 |
{ GZGETC ==================================================================== |
| 588 |
|
| 589 |
Reads one byte from the compressed file. |
| 590 |
gzgetc returns this byte or -1 in case of end of file or error. |
| 591 |
|
| 592 |
============================================================================} |
| 593 |
|
| 594 |
function gzgetc (f:gzfile) : integer; |
| 595 |
|
| 596 |
var c:byte; |
| 597 |
|
| 598 |
begin |
| 599 |
|
| 600 |
if (gzread (f,@c,1) = 1) then gzgetc := c else gzgetc := -1; |
| 601 |
|
| 602 |
end; |
| 603 |
|
| 604 |
|
| 605 |
{ GZGETS ==================================================================== |
| 606 |
|
| 607 |
Reads bytes from the compressed file until len-1 characters are read, |
| 608 |
or a newline character is read and transferred to buf, or an end-of-file |
| 609 |
condition is encountered. The string is then Null-terminated. |
| 610 |
|
| 611 |
gzgets returns buf, or nil in case of error. |
| 612 |
The current implementation is not optimized at all. |
| 613 |
|
| 614 |
============================================================================} |
| 615 |
|
| 616 |
function gzgets (f:gzfile; buf:Pchar; len:integer) : Pchar; |
| 617 |
|
| 618 |
var |
| 619 |
|
| 620 |
b : Pchar; { start of buffer } |
| 621 |
bytes : integer; { number of bytes read by gzread } |
| 622 |
gzchar : char; { char read by gzread } |
| 623 |
|
| 624 |
begin |
| 625 |
|
| 626 |
if (buf = nil) or (len <= 0) then begin |
| 627 |
gzgets := nil; |
| 628 |
exit; |
| 629 |
end; |
| 630 |
|
| 631 |
b := buf; |
| 632 |
repeat |
| 633 |
dec (len); |
| 634 |
bytes := gzread (f, buf, 1); |
| 635 |
gzchar := buf^; |
| 636 |
inc (buf); |
| 637 |
until (len = 0) or (bytes <> 1) or (gzchar = Chr(13)); |
| 638 |
|
| 639 |
buf^ := #0; |
| 640 |
if (b = buf) and (len > 0) then gzgets := nil else gzgets := b; |
| 641 |
|
| 642 |
end; |
| 643 |
|
| 644 |
|
| 645 |
{$IFNDEF NO_DEFLATE} |
| 646 |
|
| 647 |
{ GZWRITE =================================================================== |
| 648 |
|
| 649 |
Writes the given number of uncompressed bytes into the compressed file. |
| 650 |
gzwrite returns the number of uncompressed bytes actually written |
| 651 |
(0 in case of error). |
| 652 |
|
| 653 |
============================================================================} |
| 654 |
|
| 655 |
function gzwrite (f:gzfile; buf:pointer; len:cardinal) : integer; |
| 656 |
|
| 657 |
var |
| 658 |
|
| 659 |
s : gz_streamp; |
| 660 |
written : integer; |
| 661 |
|
| 662 |
begin |
| 663 |
|
| 664 |
s := gz_streamp(f); |
| 665 |
|
| 666 |
if (s = nil) or not s^.WriteMode then begin |
| 667 |
gzwrite := Z_STREAM_ERROR; |
| 668 |
exit; |
| 669 |
end; |
| 670 |
|
| 671 |
s^.stream.next_in := Pbyte(buf); |
| 672 |
s^.stream.avail_in := len; |
| 673 |
|
| 674 |
while (s^.stream.avail_in <> 0) do begin |
| 675 |
|
| 676 |
if (s^.stream.avail_out = 0) then begin |
| 677 |
s^.stream.next_out := s^.outbuf; |
| 678 |
written := s^.gzStream.Write(s^.outbuf^, Z_BUFSIZE); |
| 679 |
if (written <> Z_BUFSIZE) then begin |
| 680 |
s^.z_err := Z_ERRNO; |
| 681 |
break; |
| 682 |
end; |
| 683 |
s^.stream.avail_out := Z_BUFSIZE; |
| 684 |
end; |
| 685 |
|
| 686 |
s^.z_err := deflate(s^.stream, Z_NO_FLUSH); |
| 687 |
if (s^.z_err <> Z_OK) then break; |
| 688 |
|
| 689 |
end; {WHILE} |
| 690 |
|
| 691 |
{ Kambi: The test for "buf <> nil" below is needed, as caller |
| 692 |
may use buf=nil (with len=0) and it should be simply ignored |
| 693 |
(it should not reset the crc). |
| 694 |
Otherwise using TGZFileStream.WriteBuffer with nil data (0 length) |
| 695 |
would accidentally reset the crc, since crc32() call returns always |
| 696 |
0 for buf = nil. } |
| 697 |
if buf <> nil then |
| 698 |
s^.crc := crc32(s^.crc, buf, len); |
| 699 |
gzwrite := integer(len - s^.stream.avail_in); |
| 700 |
|
| 701 |
end; |
| 702 |
|
| 703 |
{ GZPUTC ==================================================================== |
| 704 |
|
| 705 |
Writes c, converted to an unsigned char, into the compressed file. |
| 706 |
gzputc returns the value that was written, or -1 in case of error. |
| 707 |
|
| 708 |
============================================================================} |
| 709 |
|
| 710 |
function gzputc (f:gzfile; c:char) : integer; |
| 711 |
begin |
| 712 |
if (gzwrite (f,@c,1) = 1) then |
| 713 |
{$IFDEF FPC} |
| 714 |
gzputc := integer(ord(c)) |
| 715 |
{$ELSE} |
| 716 |
gzputc := integer(c) |
| 717 |
{$ENDIF} |
| 718 |
else |
| 719 |
gzputc := -1; |
| 720 |
end; |
| 721 |
|
| 722 |
|
| 723 |
{ GZPUTS ==================================================================== |
| 724 |
|
| 725 |
Writes the given null-terminated string to the compressed file, excluding |
| 726 |
the terminating null character. |
| 727 |
gzputs returns the number of characters written, or -1 in case of error. |
| 728 |
|
| 729 |
============================================================================} |
| 730 |
|
| 731 |
function gzputs (f:gzfile; s:Pchar) : integer; |
| 732 |
begin |
| 733 |
gzputs := gzwrite (f, pointer(s), strlen(s)); |
| 734 |
end; |
| 735 |
|
| 736 |
|
| 737 |
{ DO_FLUSH ================================================================== |
| 738 |
|
| 739 |
Flushes all pending output into the compressed file. |
| 740 |
The parameter flush is as in the zdeflate() function. |
| 741 |
|
| 742 |
============================================================================} |
| 743 |
|
| 744 |
function do_flush (f:gzfile; flush:integer) : integer; |
| 745 |
var |
| 746 |
len : cardinal; |
| 747 |
done : boolean; |
| 748 |
s : gz_streamp; |
| 749 |
written : integer; |
| 750 |
begin |
| 751 |
done := false; |
| 752 |
s := gz_streamp(f); |
| 753 |
|
| 754 |
if (s = nil) or not s^.WriteMode then begin |
| 755 |
do_flush := Z_STREAM_ERROR; |
| 756 |
exit; |
| 757 |
end; |
| 758 |
|
| 759 |
s^.stream.avail_in := 0; { should be zero already anyway } |
| 760 |
|
| 761 |
while true do begin |
| 762 |
|
| 763 |
len := Z_BUFSIZE - s^.stream.avail_out; |
| 764 |
|
| 765 |
if (len <> 0) then begin |
| 766 |
written := s^.gzStream.Write(s^.outbuf^, len); |
| 767 |
if (written <> len) then begin |
| 768 |
s^.z_err := Z_ERRNO; |
| 769 |
do_flush := Z_ERRNO; |
| 770 |
exit; |
| 771 |
end; |
| 772 |
s^.stream.next_out := s^.outbuf; |
| 773 |
s^.stream.avail_out := Z_BUFSIZE; |
| 774 |
end; |
| 775 |
|
| 776 |
if (done = true) then break; |
| 777 |
s^.z_err := deflate(s^.stream, flush); |
| 778 |
|
| 779 |
{ Ignore the second of two consecutive flushes: } |
| 780 |
if (len = 0) and (s^.z_err = Z_BUF_ERROR) then s^.z_err := Z_OK; |
| 781 |
|
| 782 |
{ deflate has finished flushing only when it hasn't used up |
| 783 |
all the available space in the output buffer: } |
| 784 |
|
| 785 |
done := (s^.stream.avail_out <> 0) or (s^.z_err = Z_STREAM_END); |
| 786 |
if (s^.z_err <> Z_OK) and (s^.z_err <> Z_STREAM_END) then break; |
| 787 |
|
| 788 |
end; {WHILE} |
| 789 |
|
| 790 |
if (s^.z_err = Z_STREAM_END) then do_flush:=Z_OK else do_flush:=s^.z_err; |
| 791 |
end; |
| 792 |
|
| 793 |
{ GZFLUSH =================================================================== |
| 794 |
|
| 795 |
Flushes all pending output into the compressed file. |
| 796 |
The parameter flush is as in the zdeflate() function. |
| 797 |
|
| 798 |
The return value is the zlib error number (see function gzerror below). |
| 799 |
gzflush returns Z_OK if the flush parameter is Z_FINISH and all output |
| 800 |
could be flushed. |
| 801 |
|
| 802 |
gzflush should be called only when strictly necessary because it can |
| 803 |
degrade compression. |
| 804 |
|
| 805 |
============================================================================} |
| 806 |
|
| 807 |
function gzflush (f:gzfile; flush:integer) : integer; |
| 808 |
var |
| 809 |
err : integer; |
| 810 |
s : gz_streamp; |
| 811 |
begin |
| 812 |
s := gz_streamp(f); |
| 813 |
err := do_flush (f, flush); |
| 814 |
|
| 815 |
if (err <> 0) then begin |
| 816 |
gzflush := err; |
| 817 |
exit; |
| 818 |
end; |
| 819 |
|
| 820 |
if (s^.z_err = Z_STREAM_END) then gzflush := Z_OK else gzflush := s^.z_err; |
| 821 |
end; |
| 822 |
|
| 823 |
{$ENDIF} (* NO DEFLATE *) |
| 824 |
|
| 825 |
|
| 826 |
{ GZREWIND ================================================================== |
| 827 |
|
| 828 |
Rewinds input file. |
| 829 |
|
| 830 |
============================================================================} |
| 831 |
|
| 832 |
function gzrewind (f:gzFile) : integer; |
| 833 |
var |
| 834 |
s:gz_streamp; |
| 835 |
begin |
| 836 |
s := gz_streamp(f); |
| 837 |
|
| 838 |
if (s = nil) or s^.WriteMode then begin |
| 839 |
gzrewind := -1; |
| 840 |
exit; |
| 841 |
end; |
| 842 |
|
| 843 |
s^.z_err := Z_OK; |
| 844 |
s^.z_eof := false; |
| 845 |
s^.stream.avail_in := 0; |
| 846 |
s^.stream.next_in := s^.inbuf; |
| 847 |
|
| 848 |
if (s^.startpos = 0) then begin { not a compressed file } |
| 849 |
s^.gzStream.Position := 0; |
| 850 |
gzrewind := 0; |
| 851 |
exit; |
| 852 |
end; |
| 853 |
|
| 854 |
inflateReset(s^.stream); |
| 855 |
s^.gzStream.Position := s^.startpos; |
| 856 |
gzrewind := 0; |
| 857 |
end; |
| 858 |
|
| 859 |
|
| 860 |
{ GZSEEK ==================================================================== |
| 861 |
|
| 862 |
Sets the starting position for the next gzread or gzwrite on the given |
| 863 |
compressed file. The offset represents a number of bytes from the beginning |
| 864 |
of the uncompressed stream. |
| 865 |
|
| 866 |
gzseek returns the resulting offset, or -1 in case of error. |
| 867 |
SEEK_END is not implemented, returns error. |
| 868 |
In this version of the library, gzseek can be extremely slow. |
| 869 |
|
| 870 |
============================================================================} |
| 871 |
|
| 872 |
function gzseek (f:gzfile; offset:z_off_t; whence:integer) : z_off_t; |
| 873 |
var |
| 874 |
s : gz_streamp; |
| 875 |
size : cardinal; |
| 876 |
begin |
| 877 |
s := gz_streamp(f); |
| 878 |
|
| 879 |
if (s = nil) or (whence = SEEK_END) or (s^.z_err = Z_ERRNO) |
| 880 |
or (s^.z_err = Z_DATA_ERROR) then begin |
| 881 |
gzseek := z_off_t(-1); |
| 882 |
exit; |
| 883 |
end; |
| 884 |
|
| 885 |
if s^.WriteMode then begin |
| 886 |
{$IFDEF NO_DEFLATE} |
| 887 |
gzseek := z_off_t(-1); |
| 888 |
exit; |
| 889 |
{$ELSE} |
| 890 |
if (whence = SEEK_SET) then dec(offset, s^.stream.total_out); |
| 891 |
if (offset < 0) then begin; |
| 892 |
gzseek := z_off_t(-1); |
| 893 |
exit; |
| 894 |
end; |
| 895 |
|
| 896 |
{ At this point, offset is the number of zero bytes to write. } |
| 897 |
if s^.inbuf=nil then begin |
| 898 |
getmem(s^.inbuf,Z_BUFSIZE); |
| 899 |
fillchar(s^.inbuf^,Z_BUFSIZE,0); |
| 900 |
end; |
| 901 |
|
| 902 |
while (offset > 0) do begin |
| 903 |
size := Z_BUFSIZE; |
| 904 |
if (offset < Z_BUFSIZE) then size := cardinal(offset); |
| 905 |
|
| 906 |
size := gzwrite(f, s^.inbuf, size); |
| 907 |
if (size = 0) then begin |
| 908 |
gzseek := z_off_t(-1); |
| 909 |
exit; |
| 910 |
end; |
| 911 |
|
| 912 |
dec (offset,size); |
| 913 |
end; |
| 914 |
|
| 915 |
gzseek := z_off_t(s^.stream.total_in); |
| 916 |
exit; |
| 917 |
{$ENDIF} |
| 918 |
end; |
| 919 |
{ Rest of function is for reading only } |
| 920 |
|
| 921 |
{ compute absolute position } |
| 922 |
if (whence = SEEK_CUR) then inc (offset, s^.stream.total_out); |
| 923 |
if (offset < 0) then begin |
| 924 |
gzseek := z_off_t(-1); |
| 925 |
exit; |
| 926 |
end; |
| 927 |
|
| 928 |
if (s^.transparent = true) then begin |
| 929 |
s^.stream.avail_in := 0; |
| 930 |
s^.stream.next_in := s^.inbuf; |
| 931 |
s^.gzStream.Position := offset; |
| 932 |
s^.stream.total_in := cardinal(offset); |
| 933 |
s^.stream.total_out := cardinal(offset); |
| 934 |
gzseek := z_off_t(offset); |
| 935 |
exit; |
| 936 |
end; |
| 937 |
|
| 938 |
{ For a negative seek, rewind and use positive seek } |
| 939 |
if (cardinal(offset) >= s^.stream.total_out) |
| 940 |
then dec (offset, s^.stream.total_out) |
| 941 |
else if (gzrewind(f) <> 0) then begin |
| 942 |
gzseek := z_off_t(-1); |
| 943 |
exit; |
| 944 |
end; |
| 945 |
{ offset is now the number of bytes to skip. } |
| 946 |
|
| 947 |
if (offset <> 0) and (s^.outbuf = nil) |
| 948 |
then GetMem (s^.outbuf, Z_BUFSIZE); |
| 949 |
|
| 950 |
while (offset > 0) do begin |
| 951 |
size := Z_BUFSIZE; |
| 952 |
if (offset < Z_BUFSIZE) then size := integer(offset); |
| 953 |
|
| 954 |
size := gzread (f, s^.outbuf, size); |
| 955 |
if (size <= 0) then begin |
| 956 |
gzseek := z_off_t(-1); |
| 957 |
exit; |
| 958 |
end; |
| 959 |
dec(offset, size); |
| 960 |
end; |
| 961 |
|
| 962 |
gzseek := z_off_t(s^.stream.total_out); |
| 963 |
end; |
| 964 |
|
| 965 |
|
| 966 |
{ GZTELL ==================================================================== |
| 967 |
|
| 968 |
Returns the starting position for the next gzread or gzwrite on the |
| 969 |
given compressed file. This position represents a number of bytes in the |
| 970 |
uncompressed data stream. |
| 971 |
|
| 972 |
============================================================================} |
| 973 |
|
| 974 |
function gztell (f:gzfile) : z_off_t; |
| 975 |
begin |
| 976 |
gztell := gzseek (f, 0, SEEK_CUR); |
| 977 |
end; |
| 978 |
|
| 979 |
|
| 980 |
{ GZEOF ===================================================================== |
| 981 |
|
| 982 |
Returns TRUE when EOF has previously been detected reading the given |
| 983 |
input stream, otherwise FALSE. |
| 984 |
|
| 985 |
============================================================================} |
| 986 |
|
| 987 |
function gzeof (f:gzfile) : boolean; |
| 988 |
var |
| 989 |
s:gz_streamp; |
| 990 |
begin |
| 991 |
s := gz_streamp(f); |
| 992 |
|
| 993 |
if (s=nil) or s^.WriteMode then |
| 994 |
gzeof := false |
| 995 |
else |
| 996 |
gzeof := s^.z_eof; |
| 997 |
end; |
| 998 |
|
| 999 |
|
| 1000 |
{ PUTLONG =================================================================== |
| 1001 |
|
| 1002 |
Outputs a CInt32 in LSB order to the given file |
| 1003 |
|
| 1004 |
============================================================================} |
| 1005 |
|
| 1006 |
procedure putLong (var f:TStream; x:cardinal); |
| 1007 |
var |
| 1008 |
n : integer; |
| 1009 |
c : byte; |
| 1010 |
begin |
| 1011 |
for n:=0 to 3 do begin |
| 1012 |
c := x and $FF; |
| 1013 |
f.WriteBuffer(c, 1); |
| 1014 |
x := x shr 8; |
| 1015 |
end; |
| 1016 |
end; |
| 1017 |
|
| 1018 |
|
| 1019 |
{ GZCLOSE =================================================================== |
| 1020 |
|
| 1021 |
Flushes all pending output if necessary, closes the compressed file |
| 1022 |
and deallocates all the (de)compression state. |
| 1023 |
|
| 1024 |
The return value is the zlib error number (see function gzerror below). |
| 1025 |
|
| 1026 |
============================================================================} |
| 1027 |
|
| 1028 |
function gzclose (f:gzFile) : integer; |
| 1029 |
var |
| 1030 |
err : integer; |
| 1031 |
s : gz_streamp; |
| 1032 |
begin |
| 1033 |
s := gz_streamp(f); |
| 1034 |
if (s = nil) then begin |
| 1035 |
gzclose := Z_STREAM_ERROR; |
| 1036 |
exit; |
| 1037 |
end; |
| 1038 |
|
| 1039 |
if s^.WriteMode then begin |
| 1040 |
{$IFDEF NO_DEFLATE} |
| 1041 |
gzclose := Z_STREAM_ERROR; |
| 1042 |
exit; |
| 1043 |
{$ELSE} |
| 1044 |
err := do_flush (f, Z_FINISH); |
| 1045 |
if (err <> Z_OK) then begin |
| 1046 |
gzclose := destroy (gz_streamp(f)); |
| 1047 |
exit; |
| 1048 |
end; |
| 1049 |
|
| 1050 |
putLong (s^.gzStream, s^.crc); |
| 1051 |
putLong (s^.gzStream, s^.stream.total_in); |
| 1052 |
{$ENDIF} |
| 1053 |
end; |
| 1054 |
|
| 1055 |
gzclose := destroy (gz_streamp(f)); |
| 1056 |
end; |
| 1057 |
|
| 1058 |
|
| 1059 |
{ GZERROR =================================================================== |
| 1060 |
|
| 1061 |
Returns the error message for the last error which occured on the |
| 1062 |
given compressed file. errnum is set to zlib error number. If an |
| 1063 |
error occured in the file system and not in the compression library, |
| 1064 |
errnum is set to Z_ERRNO and the application may consult errno |
| 1065 |
to get the exact error code. |
| 1066 |
|
| 1067 |
============================================================================} |
| 1068 |
|
| 1069 |
function gzerror (f:gzfile) : string; |
| 1070 |
var |
| 1071 |
errnum:smallint; |
| 1072 |
m : string; |
| 1073 |
s : gz_streamp; |
| 1074 |
begin |
| 1075 |
s := gz_streamp(f); |
| 1076 |
if (s = nil) then begin |
| 1077 |
errnum := Z_STREAM_ERROR; |
| 1078 |
gzerror := zError(Z_STREAM_ERROR); |
| 1079 |
end; |
| 1080 |
|
| 1081 |
errnum := s^.z_err; |
| 1082 |
if (errnum = Z_OK) then begin |
| 1083 |
gzerror := zError(Z_OK); |
| 1084 |
exit; |
| 1085 |
end; |
| 1086 |
|
| 1087 |
m := s^.stream.msg; |
| 1088 |
if (errnum = Z_ERRNO) then m := ''; |
| 1089 |
if (m = '') then m := zError(s^.z_err); |
| 1090 |
|
| 1091 |
s^.msg := m; |
| 1092 |
gzerror := s^.msg; |
| 1093 |
end; |
| 1094 |
|
| 1095 |
end. |