📄 zlibh.pas
字号:
{$IFDEF ZEXPORT_CDECL} cdecl; {$ENDIF}
{*
Decompresses the source buffer into the destination buffer. sourceLen is
the byte length of the source buffer. Upon entry, destLen is the total
size of the destination buffer, which must be large enough to hold the
entire uncompressed data. (The size of the uncompressed data must have
been saved previously by the compressor and transmitted to the decompressor
by some mechanism outside the scope of this compression library.)
Upon exit, destLen is the actual size of the compressed buffer.
This function can be used to decompress a whole file at once if the
input file is mmap'ed.
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
enough memory, Z_BUF_ERROR if there was not enough room in the output
buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
*}
(*
type
gzFile = voidp;
function gzopen(path: PChar; mode: PChar):gzFile;
{*
Opens a gzip (.gz) file for reading or writing. The mode parameter
is as in fopen ("rb" or "wb") but can also include a compression level
("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
Huffman only compression as in "wb1h", or 'R' for run-length encoding
as in "wb1R". (See the description of deflateInit2 for more information
about the strategy parameter.)
gzopen can be used to read a file which is not in gzip format; in this
case gzread will directly read from the file without decompression.
gzopen returns NULL if the file could not be opened or if there was
insufficient memory to allocate the (de)compression state; errno
can be checked to distinguish the two cases (if errno is zero, the
zlib error is Z_MEM_ERROR). *}
function gzdopen(fd: Integer; mode: PChar):gzFile;
{*
gzdopen() associates a gzFile with the file descriptor fd. File
descriptors are obtained from calls like open, dup, creat, pipe or
fileno (in the file has been previously opened with fopen).
The mode parameter is as in gzopen.
The next call of gzclose on the returned gzFile will also close the
file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
gzdopen returns NULL if there was insufficient memory to allocate
the (de)compression state.
*}
function gzsetparams(file_:gzFile; level: Integer; strategy: Integer): Integer;
{*
Dynamically update the compression level or strategy. See the description
of deflateInit2 for the meaning of these parameters.
gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
opened for writing.
*}
function gzread(file_:gzFile; buf:voidp; len:UnsignedInt): Integer;
{*
Reads the given number of uncompressed bytes from the compressed file.
If the input file was not in gzip format, gzread copies the given number
of bytes into the buffer.
gzread returns the number of uncompressed bytes actually read (0 for
end of file, -1 for error). *}
function gzwrite(file_:gzFile;
buf:voidpc;
len:UnsignedInt): Integer;
{*
Writes the given number of uncompressed bytes into the compressed file.
gzwrite returns the number of uncompressed bytes actually written
(0 in case of error).
*}
// function gzprintf(file_:gzFile; format: PChar, ...): Integer;
// No ellipsis in Delphi
{*
Converts, formats, and writes the args to the compressed file under
control of the format string, as in fprintf. gzprintf returns the number of
uncompressed bytes actually written (0 in case of error). The number of
uncompressed bytes written is limited to 4095. The caller should assure that
this limit is not exceeded. If it is exceeded, then gzprintf() will return
return an error (0) with nothing written. In this case, there may also be a
buffer overflow with unpredictable consequences, which is possible only if
zlib was compiled with the insecure functions sprintf() or vsprintf()
because the secure snprintf() or vsnprintf() functions were not available.
*}
function gzputs(file_:gzFile; s: PChar): Integer;
(*
Writes the given null-terminated string to the compressed file, excluding
the terminating null character.
gzputs returns the number of characters written, or -1 in case of error.
*}
function gzgets(file_:gzFile; buf: PChar; len: Integer): PChar;
{*
Reads bytes from the compressed file until len-1 characters are read, or
a newline character is read and transferred to buf, or an end-of-file
condition is encountered. The string is then terminated with a null
character.
gzgets returns buf, or Z_NULL in case of error.
*}
function gzputc(file_:gzFile; c: Integer): Integer;
{*
Writes c, converted to an unsigned char, into the compressed file.
gzputc returns the value that was written, or -1 in case of error.
*}
function gzgetc(file_:gzFile): Integer;
{*
Reads one byte from the compressed file. gzgetc returns this byte
or -1 in case of end of file or error.
*}
function gzungetc(c: Integer; file_:gzFile): Integer;
{*
Push one character back onto the stream to be read again later.
Only one character of push-back is allowed. gzungetc() returns the
character pushed, or -1 on failure. gzungetc() will fail if a
character has been pushed but not read yet, or if c is -1. The pushed
character will be discarded if the stream is repositioned with gzseek()
or gzrewind().
*}
function gzflush(file_:gzFile; flush: Integer): Integer;
{*
Flushes all pending output into the compressed file. The parameter
flush is as in the deflate() function. The return value is the zlib
error number (see function gzerror below). gzflush returns Z_OK if
the flush parameter is Z_FINISH and all output could be flushed.
gzflush should be called only when strictly necessary because it can
degrade compression.
*}
function gzseek(file_:gzFile;
offset:z_off_t;
whence: Integer):z_off_t;
{*
Sets the starting position for the next gzread or gzwrite on the
given compressed file. The offset represents a number of bytes in the
uncompressed data stream. The whence parameter is defined as in lseek(2);
the value SEEK_END is not supported.
If the file is opened for reading, this function is emulated but can be
extremely slow. If the file is opened for writing, only forward seeks are
supported; gzseek then compresses a sequence of zeroes up to the new
starting position.
gzseek returns the resulting offset location as measured in bytes from
the beginning of the uncompressed stream, or -1 in case of error, in
particular if the file is opened for writing and the new starting position
would be before the current position.
*}
function gzrewind(file_:gzFile): Integer;
{*
Rewinds the given file. This function is supported only for reading.
gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
*}
function gztell(file_:gzFile):z_off_t;
{*
Returns the starting position for the next gzread or gzwrite on the
given compressed file. This position represents a number of bytes in the
uncompressed data stream.
gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
*}
function gzeof(file_:gzFile): Integer;
{*
Returns 1 when EOF has previously been detected reading the given
input stream, otherwise zero.
*}
function gzclose(file_:gzFile): Integer;
{*
Flushes all pending output if necessary, closes the compressed file
and deallocates all the (de)compression state. The return value is the zlib
error number (see function gzerror below).
*}
function gzerror(file_:gzFile; var errnum: Integer): PChar;
{*
Returns the error message for the last error which occurred on the
given compressed file. errnum is set to zlib error number. If an
error occurred in the file system and not in the compression library,
errnum is set to Z_ERRNO and the application may consult errno
to get the exact error code.
*}
procedure gzclearerr(file_:gzFile);
{*
Clears the error and end-of-file flags for file. This is analogous to the
clearerr() function in stdio. This is useful for continuing to read a gzip
file that is being written concurrently.
*}
*)
{* checksum functions *}
{*
These functions are not related to compression but are exported
anyway because they might be useful in applications using the
compression library.
*}
{$EXTERNALSYM adler32}
function adler32(adler:uLong; {const} buf: PBytef; len:uInt):uLong;
{$IFDEF ZEXPORT_CDECL} cdecl; {$ENDIF}
(*
Update a running Adler-32 checksum with the bytes buf[0..len-1] and
return the updated checksum. If buf is NULL, this function returns
the required initial value for the checksum.
An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
much faster. Usage example:
uLong adler = adler32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
adler = adler32(adler, buffer, length);
}
if (adler != original_adler) error();
*)
{$EXTERNALSYM crc32}
function crc32 (crc:uLong; {const} buf: PBytef; len:uInt):uLong;
{$IFDEF ZEXPORT_CDECL} cdecl; {$ENDIF}
(*
Update a running crc with the bytes buf[0..len-1] and return the updated
crc. If buf is NULL, this function returns the required initial value
for the crc. Pre- and post-conditioning (one's complement) is performed
within this function so it shouldn't be done by the application.
Usage example:
uLong crc = crc32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
crc = crc32(crc, buffer, length);
}
if (crc != original_crc) error();
*)
{* various hacks, don't look :) *)
{* deflateInit and inflateInit are macros to allow checking the zlib version
* and the compiler's view of z_stream:
*}
{$EXTERNALSYM deflateInit_}
function deflateInit_(var strm:z_stream;
level: Integer;
{const} version: PChar;
stream_size: Integer): Integer;
{$IFDEF ZEXPORT_CDECL} cdecl; {$ENDIF}
{$EXTERNALSYM inflateInit_}
function inflateInit_(var strm:z_stream;
{const} version: PChar;
stream_size: Integer): Integer;
{$IFDEF ZEXPORT_CDECL} cdecl; {$ENDIF}
{$EXTERNALSYM deflateInit2_}
function deflateInit2_(var strm:z_stream;
level: Integer;
method: Integer;
windowBits: Integer;
memLevel: Integer;
strategy: Integer;
{const} version: PChar;
stream_size: Integer): Integer;
{$IFDEF ZEXPORT_CDECL} cdecl; {$ENDIF}
{$EXTERNALSYM inflateInit2_}
function inflateInit2_(var strm:z_stream;
windowBits: Integer;
{const} version: PChar;
stream_size: Integer): Integer;
{$IFDEF ZEXPORT_CDECL} cdecl; {$ENDIF}
{$EXTERNALSYM inflateBackInit_}
function inflateBackInit_(var strm:z_stream;
windowBits: Integer;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -