C# Class Blast.Blast

Show file Open project: Nihlus/libwarcraft

Public Methods

Method Description
Blast ( Stream inputStream, Stream outputStream ) : System

Decompress input to output using the provided infun() and outfun() calls. On success, the return value of blast() is zero. If there is an error in the source data, i.e. it is not in the proper format, then a negative value is returned. If there is not enough input available or there is not enough output space, then a positive error is returned.

The input function is invoked: len = infun(how, &buf), where buf is set by infun() to point to the input buffer, and infun() returns the number of available bytes there. If infun() returns zero, then blast() returns with an input error. (blast() only asks for input if it needs it.) inhow is for use by the application to pass an input descriptor to infun(), if desired.

The output function is invoked: err = outfun(how, buf, len), where the bytes to be written are buf[0..len-1]. If err is not zero, then blast() returns with an output error. outfun() is always called with len <= 4096. outhow is for use by the application to pass an output descriptor to outfun(), if desired.

The return codes are:

2: ran out of input before completing decompression 1: output error before completing decompression 0: successful decompression -1: literal flag not zero or one -2: dictionary size not in 4..6 -3: distance is too far back

At the bottom of blast.c is an example program that uses blast() that can be compiled to produce a command-line decompression filter by defining TEST.

Decompress ( ) : void

Decode PKWare Compression Library stream.

Private Methods

Method Description
ConsumeByte ( ) : byte
CopyBufferSection ( int fromIndex, int copyCount ) : void
Decode ( HuffmanTable h ) : int

Decode a code from the stream using huffman table h. Return the symbol or a negative value if there is an error. If all of the lengths are zero, i.e. an empty code, or if the code is incomplete and an invalid code is received, then -9 is returned after reading MAXBITS bits.

Format notes:

The codes as stored in the compressed data are bit-reversed relative to a simple integer ordering of codes of the same lengths. Hence below the bits are pulled from the compressed data one at a time and used to build the code value reversed from what is in the stream in order to permit simple integer comparisons for decoding. The first code for the shortest length is all ones. Subsequent codes of the same length are simply integer decrements of the previous code. When moving up a length, a one bit is appended to the code. For a complete code, the last code of the longest length will be all zeros. To support this ordering, the bits pulled during decoding are inverted to apply the more "natural" ordering starting with all zeros and incrementing.

DecompressStream ( ) : void

Decode PKWare Compression Library stream. Format notes: - First byte is 0 if literals are uncoded or 1 if they are coded. Second byte is 4, 5, or 6 for the number of extra bits in the distance code. This is the base-2 logarithm of the dictionary size minus six. - Compressed data is a combination of literals and length/distance pairs terminated by an end code. Literals are either Huffman coded or uncoded bytes. A length/distance pair is a coded length followed by a coded distance to represent a string that occurs earlier in the uncompressed data that occurs again at the current location. - A bit preceding a literal or length/distance pair indicates which comes next, 0 for literals, 1 for length/distance. - If literals are uncoded, then the next eight bits are the literal, in the normal bit order in th stream, i.e. no bit-reversal is needed. Similarly, no bit reversal is needed for either the length extra bits or the distance extra bits. - Literal bytes are simply written to the output. A length/distance pair is an instruction to copy previously uncompressed bytes to the output. The copy is from distance bytes back in the output stream, copying for length bytes. - Distances pointing before the beginning of the output data are not permitted. - Overlapped copies, where the length is greater than the distance, are allowed and common. For example, a distance of one and a length of 518 simply copies the last byte 518 times. A distance of four and a length of twelve copies the last four bytes three times. A simple forward copy ignoring whether the length is greater than the distance or not implements this correctly.

DoReadBuffer ( ) : void
EnsureBufferSpace ( int required ) : void
FlushBits ( ) : void
FlushOutputBuffer ( ) : void
FlushOutputBufferSection ( int count ) : void
GetBits ( int need ) : int
IsInputRemaining ( ) : bool

Check for presence of more input without consuming it. May refill the input buffer.

WriteBuffer ( byte b ) : void

Method Details

Blast() public method

Decompress input to output using the provided infun() and outfun() calls. On success, the return value of blast() is zero. If there is an error in the source data, i.e. it is not in the proper format, then a negative value is returned. If there is not enough input available or there is not enough output space, then a positive error is returned.

The input function is invoked: len = infun(how, &buf), where buf is set by infun() to point to the input buffer, and infun() returns the number of available bytes there. If infun() returns zero, then blast() returns with an input error. (blast() only asks for input if it needs it.) inhow is for use by the application to pass an input descriptor to infun(), if desired.

The output function is invoked: err = outfun(how, buf, len), where the bytes to be written are buf[0..len-1]. If err is not zero, then blast() returns with an output error. outfun() is always called with len <= 4096. outhow is for use by the application to pass an output descriptor to outfun(), if desired.

The return codes are:

2: ran out of input before completing decompression 1: output error before completing decompression 0: successful decompression -1: literal flag not zero or one -2: dictionary size not in 4..6 -3: distance is too far back

At the bottom of blast.c is an example program that uses blast() that can be compiled to produce a command-line decompression filter by defining TEST.

public Blast ( Stream inputStream, Stream outputStream ) : System
inputStream Stream
outputStream Stream
return System

Decompress() public method

Decode PKWare Compression Library stream.
public Decompress ( ) : void
return void