📄 mzlib.pas
字号:
Z_PARTIAL_FLUSH = 1;
Z_SYNC_FLUSH = 2;
Z_FULL_FLUSH = 3;
Z_FINISH = 4;
// Return codes for the compression/decompression functions. Negative
// values are errors, positive values are used for special but normal events.
Z_OK = 0;
Z_STREAM_END = 1;
Z_NEED_DICT = 2;
Z_ERRNO = -1;
Z_STREAM_ERROR = -2;
Z_DATA_ERROR = -3;
Z_MEM_ERROR = -4;
Z_BUF_ERROR = -5;
Z_VERSION_ERROR = -6;
// compression levels
Z_DEFAULT_COMPRESSION = -1;
Z_NO_COMPRESSION = 0;
Z_BEST_SPEED = 1;
Z_BEST_COMPRESSION = 9;
// compression strategy, see DeflateInit2 below for details
Z_DEFAULT_STRATEGY = 0;
Z_FILTERED = 1;
Z_HUFFMAN_ONLY = 2;
// possible values of the DataType field
Z_BINARY = 0;
Z_ASCII = 1;
Z_UNKNOWN = 2;
// the Deflate compression imMethod (the only one supported in this Version)
Z_DEFLATED = 8;
// three kinds of block type
STORED_BLOCK = 0;
STATIC_TREES = 1;
DYN_TREES = 2;
// minimum and maximum match lengths
MIN_MATCH = 3;
MAX_MATCH = 258;
// preset dictionary flag in zlib header
PRESET_DICT = $20;
ZLIB_VERSION: String[10] = '1.1.2';
ERROR_BASE = Z_NEED_DICT;
ErrorMessages: array[0..9] of String = (
SNeedDict, // Z_NEED_DICT 2
SStreamEnd, // Z_STREAM_END 1
'', // Z_OK 0
SFileError, // Z_ERRNO -1
SStreamError, // Z_STREAM_ERROR -2
SDataError, // Z_DATA_ERROR -3
SInsufficientMemory, // Z_MEM_ERROR -4
SBufferError, // Z_BUF_ERROR -5
SIncompatibleVersion, // Z_VERSION_ERROR -6
''
);
function zError(Error: Integer): String;
function CRC32(CRC: Cardinal; Buffer: PByte; Len: Cardinal): Cardinal;
//----------------- deflation support ----------------------------------------------------------------------------------
function DeflateInit(var ZState: TZState; Level: Integer): Integer;
function DeflateInit_(ZState: PZState; Level: Integer; const Version: String; StreamSize: Integer): Integer;
function Deflate(var ZState: TZState; Flush: Integer): Integer;
function DeflateEnd(var ZState: TZState): Integer;
// The following functions are needed only in some special applications.
function DeflateInit2(var ZState: TZState; Level: Integer; Method: Byte; AWindowBits: Integer; MemLevel: Integer;
Strategy: Integer): Integer;
function DeflateSetDictionary(var ZState: TZState; Dictionary: PByte; DictLength: Cardinal): Integer;
function DeflateCopy(Dest: PZState; Source: PZState): Integer;
function DeflateReset(var ZState: TZState): Integer;
function DeflateParams(var ZState: TZState; Level: Integer; Strategy: Integer): Integer;
const
LENGTH_CODES = 29; // number of length codes, not counting the special END_BLOCK code
LITERALS = 256; // number of literal bytes 0..255
L_CODES = (LITERALS + 1 + LENGTH_CODES);
// number of literal or length codes, including the END_BLOCK code
D_CODES = 30; // number of distance codes
BL_CODES = 19; // number of codes used to transfer the bit lengths
HEAP_SIZE = (2 * L_CODES + 1); // maximum heap size
MAX_BITS = 15; // all codes must not exceed MAX_BITS bits
// stream status
INIT_STATE = 42;
BUSY_STATE = 113;
FINISH_STATE = 666;
type
// data structure describing a single value and its code string
PTreeEntry = ^TTreeEntry;
TTreeEntry = record
fc: record
case Byte of
0:
(Frequency: Word); // frequency count
1:
(Code: Word); // bit string
end;
dl: record
case Byte of
0:
(dad: Word); // father node in Huffman tree
1:
(Len: Word); // length of bit string
end;
end;
TLiteralTree = array[0..HEAP_SIZE - 1] of TTreeEntry; // literal and length tree
TDistanceTree = array[0..2 * D_CODES] of TTreeEntry; // distance tree
THuffmanTree = array[0..2 * BL_CODES] of TTreeEntry; // Huffman tree for bit lengths
PTree = ^TTree;
TTree = array[0..(MaxInt div SizeOf(TTreeEntry)) - 1] of TTreeEntry; // generic tree type
PStaticTreeDescriptor = ^TStaticTreeDescriptor;
TStaticTreeDescriptor = record
StaticTree: PTree; // static tree or nil
ExtraBits: PIntegerArray; // extra bits for each code or nil
ExtraBase: Integer; // base index for ExtraBits
Elements: Integer; // max number of elements in the tree
MaxLength: Integer; // max bit length for the codes
end;
PTreeDescriptor = ^TTreeDescriptor;
TTreeDescriptor = record
DynamicTree: PTree;
MaxCode: Integer; // largest code with non zero frequency
StaticDescriptor: PStaticTreeDescriptor; // the corresponding static tree
end;
PDeflateState = ^TDeflateState;
TDeflateState = record
ZState: PZState; // pointer back to this zlib stream
Status: Integer; // as the name implies
PendingBuffer: PByteArray; // output still pending
PendingBufferSize: Integer;
PendingOutput: PByte; // next pending byte to output to the stream
Pending: Integer; // nb of bytes in the pending buffer
NoHeader: Integer; // suppress zlib header and Adler32
DataType: Byte; // UNKNOWN, BINARY or ASCII
imMethod: Byte; // ibmStored (for zip only) or DEFLATED
LastFlush: Integer; // Value of flush param for previous deflate call
WindowSize: Cardinal; // LZ77 window size (32K by default)
WindowBits: Cardinal; // log2(WindowSize) (8..16)
WindowMask: Cardinal; // WindowSize - 1
// Sliding window. Input bytes are read into the second half of the window,
// and move to the first half later to keep a dictionary of at least WSize
// bytes. With this organization, matches are limited to a distance of
// WSize - MAX_MATCH bytes, but this ensures that IO is always
// performed with a length multiple of the block Size. Also, it limits
// the window Size to 64K, which is quite useful on MSDOS.
// To do: use the user input buffer as sliding window.
Window: PByteArray;
// Actual size of Window: 2 * WSize, except when the user input buffer
// is directly used as sliding window.
CurrentWindowSize: Integer;
// Link to older string with same hash index. to limit the size of this
// array to 64K, this link is maintained only for the last 32K strings.
// An index in this array is thus a window index modulo 32K.
Previous: PWordArray;
Head: PWordArray; // heads of the hash chains or nil
InsertHash: Cardinal; // hash index of string to be inserted
HashSize: Cardinal; // number of elements in hash table
HashBits: Cardinal; // log2(HashSize)
HashMask: Cardinal; // HashSize - 1
// Number of bits by which InsertHash must be shifted at each input step.
// It must be such that after MIN_MATCH steps, the oldest byte no longer
// takes part in the hash key, that is:
// HashShift * MIN_MATCH >= HashBits
HashShift: Cardinal;
// Window position at the beginning of the current output block. Gets
// negative when the window is moved backwards.
BlockStart: Integer;
MatchLength: Cardinal; // length of best match
PreviousMatch: Cardinal; // previous match
MatchAvailable: Boolean; // set if previous match exists
StringStart: Cardinal; // start of string to insert
MatchStart: Cardinal; // start of matching string
Lookahead: Cardinal; // number of valid bytes ahead in window
// Length of the best match at previous step. Matches not greater than this
// are discarded. This is used in the lazy match evaluation.
PreviousLength: Cardinal;
// To speed up deflation hash chains are never searched beyond this
// Length. A higher limit improves compression ratio but degrades the speed.
MaxChainLength: Cardinal;
Level: Integer; // compression level (1..9)
Strategy: Integer; // favor or force Huffman coding
GoodMatch: Cardinal; // use a faster search when the previous match is longer than this
NiceMatch: Cardinal; // stop searching when current match exceeds this
LiteralTree: TLiteralTree; // literal and length tree
DistanceTree: TDistanceTree; // distance tree
BitLengthTree: THuffmanTree; // Huffman tree for bit lengths
LiteralDescriptor: TTreeDescriptor; // Descriptor for literal tree
DistanceDescriptor: TTreeDescriptor; // Descriptor for distance tree
BitLengthDescriptor: TTreeDescriptor; // Descriptor for bit length tree
BitLengthCounts: array[0..MAX_BITS] of Word; // number of codes at each bit length for an optimal tree
Heap: array[0..2 * L_CODES] of Integer; // heap used to build the Huffman trees
HeapLength: Integer; // number of elements in the heap
HeapMaximum: Integer; // element of largest frequency
// The sons of Heap[N] are Heap[2 * N] and Heap[2 * N + 1]. Heap[0] is not used.
// The same heap array is used to build all trees.
Depth: array[0..2 * L_CODES] of Byte; // depth of each subtree used as tie breaker for trees of equal frequency
LiteralBuffer: PByteArray; // buffer for literals or lengths
// Size of match buffer for literals/lengths. There are 4 reasons for limiting LiteralBufferSize to 64K:
// - frequencies can be kept in 16 bit counters
// - If compression is not successful for the first block, all input
// data is still in the window so we can still emit a stored block even
// when input comes from standard input. This can also be done for
// all blocks if LiteralBufferSize is not greater than 32K.
// - if compression is not successful for a file smaller than 64K, we can
// even emit a stored file instead of a stored block (saving 5 bytes).
// This is applicable only for zip (not gzip or zlib).
// - creating new Huffman trees less frequently may not provide fast
// adaptation to changes in the input data statistics. (Take for
// example a binary file with poorly compressible code followed by
// a highly compressible string table.) Smaller buffer sizes give
// fast adaptation but have of course the overhead of transmitting
// trees more frequently.
// - I can't count above 4
LiteralBufferSize: Cardinal;
LastLiteral: Cardinal; // running index in LiteralBuffer
// Buffer for distances. To simplify the code, DistanceBuffer and LiteralBuffer have
// the same number of elements. To use different lengths, an extra flag array would be necessary.
DistanceBuffer: PWordArray;
OptimalLength: Integer; // bit length of current block with optimal trees
StaticLength: Integer; // bit length of current block with static trees
CompressedLength: Integer; // total bit length of compressed file
Matches: Cardinal; // number of string matches in current block
LastEOBLength: Integer; // bit length of EOB code for last block
BitsBuffer: Word; // Output buffer. Bits are inserted starting at the bottom (least significant bits).
ValidBits: Integer; // Number of valid bits in BitsBuffer. All Bits above the last valid bit are always zero.
case Byte of
0:
// Attempt to find a better match only when the current match is strictly smaller than this value.
// This mechanism is used only for compression levels >= 4.
(MaxLazyMatch: Cardinal);
1:
// Insert new strings in the hash table only if the match Length is not greater than this length. This saves
// time but degrades compression. MaxInsertLength is used only for compression levels <= 3.
(MaxInsertLength: Cardinal);
end;
//----------------- inflation support ----------------------------------------------------------------------------------
function InflateInit(var Z: TZState): Integer;
function InflateInit_(var Z: TZState; const Version: String; StreamSize: Integer): Integer;
function InflateInit2_(var Z: TZState; W: Integer; const Version: String; StreamSize: Integer): Integer;
function InflateInit2(var Z: TZState; AWindowBits: Integer): Integer;
function InflateEnd(var Z: TZState): Integer;
function InflateReset(var Z: TZState): Integer;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -