PureLZMA library

Overview

This is a purebasic 4.6x user library.

It provides two functions to compress/uncompress data using the LZMA algorithm.
Optional RC4 based encryption is supported.
It uses unmodified code from the LZMA SDK v4.62.

Functions

PureLZMA_Compress(*Buffer, *BufferLen [, EncryptionKey.s])
Compress a buffer with LZMA.

Parameters :

Input :

*Buffer : pointer to the input buffer
*BufferLen : pointer to a variable containing the input buffer length

Optional :
EncryptionKey : encryption key ; if EncryptionKey <> "", the compressed data is encrypted [RC4 based]

Ouput :

*BufferLen : pointer to a variable containing the ouput (compressed) buffer length

Returned value :

Positive value : pointer to the output (compressed) buffer
Zero or negative value : error code.

Error codes :

-1 : Memory allocation error
-2 : Output buffer overflow or buffer not compressable
-3 : Other compression error
PureLZMA_UnCompress(*Buffer, *BufferLen [, EncryptionKey.s])
Uncompress a buffer with LZMA.

Parameters :

Input :

*Buffer : pointer to the input buffer
*BufferLen : pointer to a variable containing the input buffer length

Optional
EncryptionKey : encryption key [if data was encrypted]

Ouput :

*BufferLen : pointer to a variable containing the ouput (uncompressed) buffer length

Returned value :

Positive value : pointer to the output (uncompressed) buffer
Zero or negative value : error code.

Error codes :

0 : Invalid data [not compressed by PureLZMA_Compress()] or wrong key
-1 : Memory allocation error
-2 : Input buffer insufficient
-3, -4 : Other compression error
PureLZMA_FreeBuffer(*Memory)
Free memory allocated by PureLZMA functions PureLZMA_Compress() or PureLZMA_UnCompress().
PureLZMA_SetOptions(Opt_Level, DictSize, lc, lp, pb, fb, NumThreads)
Set LZMA compression options (by default, PureLZMA_Compress() uses the default LZMA settings).
Use before calling PureLZMA_Compress().

Available options (from the LZMA SDK) :

Note : the LZMA Encoder will use default values for any parameter,
if it is :
-1 for any from: level, loc, lp, pb, fb, numThreads
0 for dictSize


level - the compression level ( 0 <= level <= 9 ).

level dictSize algo fb
0: 16 KB 0 32
1: 64 KB 0 32
2: 256 KB 0 32
3: 1 MB 0 32
4: 4 MB 0 32
5: 16 MB 1 32
6: 32 MB 1 32
7+: 64 MB 1 64

The default value for "level" is 5.
algo = 0 means fast method
algo = 1 means normal method

dictSize - the dictionary size in bytes.

The maximum value is 128 MB = (1 << 27) bytes
The default value is 16 MB = (1 << 24) bytes.
It's recommended to use a dictionary that is larger than 4 KB and
that can be calculated as (1 << N) or (3 << N) sizes.

lc - the number of literal context bits (high bits of previous literal).

It can be in the range from 0 to 8. The default value is 3.
Sometimes lc = 4 gives the gain for big files.

lp - the number of literal pos bits (low bits of current position for literals).

It can be in the range from 0 to 4. The default value is 0.
The lp switch is intended for periodical data when the period is equal to 2 ^ lp.
For example, for 32-bit (4 bytes) periodical data you can use lp = 2.
Often it's better to set lc = 0, if you change lp switch.

pb - the number of pos bits (low bits of current position).

It can be in the range from 0 to 4. The default value is 2.
The pb switch is intended for periodical data when the period is equal 2^pb.

fb - the word size (the number of fast bytes).

It can be in the range from 5 to 273. The default value is 32.
Usually, a big number gives a little bit better compression ratio
and a slower compression process.

numThreads - the number of threads (1 or 2).

The default value is 2.
Fast mode (algo = 0) can use only 1 thread.