LZMA SDK 4.01
-------------

LZMA SDK 4.01  Copyright (C) 1999-2004 Igor Pavlov  2004-02-15

LZMA SDK provides developers with documentation, source code,
and sample code necessary to write software that uses LZMA compression. 

LZMA is default and general compression method of 7z format
in 7-Zip program. LZMA provides high compression ratio and very 
fast decompression.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

LZMA SDK also can be available under a proprietary license for 
those who cannot use the GNU LGPL in their code. To request
such proprietary license or any additional consultations,
write to support@7-zip.org


LZMA SDK includes:

  - File->file LZMA compressing program (lzma.exe) with source code
  - ANSI-C compatible source code for LZMA decompressing
  
Source\7zip\Compress\LZMA_Alone contains source code of lzma.exe.
To compile lzma.exe you must also download 7-Zip sources from 
www.7-zip.org

LZMA decompression code was ported from original C++ sources to C.
Also it was simplified and optimized for code size. 
But it is fully compatible with LZMA from 7-Zip.


LZMA Decompression features
---------------------------
  - Variable dictionary size (up to 256 MB)
  - Estimated decompressing speed: 
      - 8-12 MB/s on 1000 MHz P3 or K7.
      - 500-1000 KB/s on 100 MHz ARM, MIPS, PowerPC or other simple RISC.
  - Small memory requirements for decompressing (8-32 KB + DictionarySize)
  - Small code size for decompressing: 2-8 KB

LZMA decoder uses only integer operations and can be 
implemented in any modern 32-bit CPU (or on 16-bit CPU with some conditions).

Some critical operations that affect to speed of LZMA decompression:
  1) 32*16 bit integer multiply.
  2) Misspredicted branches (penalty mostly depends from pipeline length). 
  3) 32-bit shift and arithmetic operations.

Speed of LZMA decompression mostly depends from CPU speed.
Memory speed has no big meaning. But if your CPU has small data cache, 
overall weight of memory speed will slightly increase.


How To Use
----------

Using LZMA encoder/decoder executable
--------------------------------------

Usage:  LZMA <e|d> inputFile outputFile [<switches>...]

  e: encode file

  d: decode file

<Switches>
  
  -d{N}:  set dictionary - [0,28], default: 23 (2^23 = 8MB)

  -fb{N}: set number of fast bytes - [5, 255], default: 128
          Usually big number gives a little bit better compression ratio 
          and slower compression process.

  -lc{N}: set number of literal context bits - [0, 8], default: 3
          Sometimes lc=4 gives gain for big files.

  -lp{N}: set number of literal pos bits - [0, 4], default: 0
          lp switch is intended for periodical data when period is 
          equal 2^value (where lp=value). 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{N}: set number of pos bits - [0, 4], default: 2
          pb switch is intended for periodical data 
          when period is equal 2^value (where lp=value).

  -eos:   write End Of Stream marker

Examples:

1) LZMA e file.bin file.lzma -d16 -lc0 

compresses file.bin to file.lzma with 64KB dictionary (2^16=64K)  
and 0 literal context bits. 
-lc0 allows to reduce memory requirements for decompression.


2) LZMA e file.bin file.lzma -lc0 -lp2

compresses file.bin to file.lzma with settings suitable 
for 32-bit periodical data (for example, ARM or MIPS code).

3) LZMA d file.lzma file.bin

decompresses file.lzma to file.bin.

LZMA compressed file format
---------------------------
Offset Size Description
  0     1   Special LZMA properties for compressed data
  1     4   Dictionary size (little endian)
  5     8   Uncompressed size (little endian). -1 means unknown size
 13         Compressed data


Memory requirements for LZMA decoding
------------------------------------

LZMA decoder doesn't allocate memory itself, so you must 
calculate required memory, allocate it and send it to LZMA.

Stack usage of LZMA function for local variables is not 
larger than 200 bytes.

Memory requirements for decompression depend 
from interface that you want to use:

  a) Memory to memory decompression:
    
    M1 = (inputSize + outputSize + lzmaInternalSize).

  b) Decompression with buffering:

    M2 = (inputBufferSize + outputBufferSize + dictionarySize + lzmaInternalSize)


LZMA SDK source code
--------------------

LzmaDecode.h  - interface for LZMA decoding
LzmaDecode.c  - LZMA decoding
LzmaTest.c    - test application that decodes LZMA encoded file

How To decompress data
----------------------

1) Read first byte of properties for LZMA compressed stream, 
   check that it has correct value and calculate three 
   LZMA property variables:

  int lc, lp, pb;
  unsigned char prop0 = properties[0];
  if (prop0 >= (9*5*5))
  {
    sprintf(rs + strlen(rs), "\n properties error");
    return 1;
  }
  for (pb = 0; prop0 >= (9 * 5); 
    pb++, prop0 -= (9 * 5));
  for (lp = 0; prop0 >= 9; 
    lp++, prop0 -= 9);
  lc = prop0;

2) Calculate required amount for LZMA lzmaInternalSize:

  lzmaInternalSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp))) * 
     sizeof(CProb)

  LZMA_BASE_SIZE = 1846
  LZMA_LIT_SIZE = 768

  LZMA decoder uses array of CProb variables as internal structure.
  By default, CProb is (unsigned short)
  But you can define _LZMA_PROB32 to make it (unsigned int)
  It can increase speed on some 32-bit CPUs, but memory usage will 
  be doubled in that case.


  2b) If you use Decompression with buffering, add 100 bytes to 
      lzmaInternalSize:
     
      #ifdef _LZMA_OUT_READ
      lzmaInternalSize += 100;
      #endif

3) Allocate that memory with malloc or some other function:

  lzmaInternalData = malloc(lzmaInternalSize);


4) Decompress data:

  4a) If you use simple memory to memory decompression:

    int result = LzmaDecode(lzmaInternalData, lzmaInternalSize,
        lc, lp, pb,
        unsigned char *inStream, unsigned int inSize,
        unsigned char *outStream, unsigned int outSize, 
        &outSizeProcessed);

  4b) If you use Decompression with buffering

    4.1) Read dictionary size from properties

      unsigned int dictionarySize = 0;
      int i;
      for (i = 0; i < 4; i++)
        dictionarySize += (unsigned int)(b) << (i * 8);

    4.2) Allocate memory for dictionary

      unsigned char *dictionary = malloc(dictionarySize);

    4.3) Initialize LZMA decoder:

    LzmaDecoderInit((unsigned char *)lzmaInternalData, lzmaInternalSize,
        lc, lp, pb,
        dictionary, dictionarySize,
        &bo.ReadCallback);

    4.4) In loop call LzmaDecoderCode function:

    for (nowPos = 0; nowPos < outSize;)
    {
      unsigned int blockSize = outSize - nowPos;
      unsigned int kBlockSize = 0x10000;
      if (blockSize > kBlockSize)
        blockSize = kBlockSize;
      res = LzmaDecode((unsigned char *)lzmaInternalData, 
      ((unsigned char *)outStream) + nowPos, blockSize, &outSizeProcessed);
      if (res != 0)
      {
        printf("\nerror = %d\n", res);
        break;
      }
      nowPos += outSizeProcessed;
      if (outSizeProcessed == 0)
      {
        outSize = nowPos;
        break;
      }
    }


EXIT codes
-----------

LZMA decoder can return on of the following codes:

#define LZMA_RESULT_OK 0
#define LZMA_RESULT_DATA_ERROR 1
#define LZMA_RESULT_NOT_ENOUGH_MEM 2

If you use callback function for input data and you return some 
error code, LZMA Decoder also returns that code.



LZMA Defines
------------

_LZMA_IN_CB    - Use callback for input data

_LZMA_OUT_READ - Use read function for output data

_LZMA_LOC_OPT  - Enable local speed optimizations inside code

_LZMA_PROB32   - It can increase speed on some 32-bit CPUs, 
                 but memory usage will be doubled in that case

_LZMA_UINT32_IS_ULONG  - Define it if int is 16-bit on your compiler
                         and long is 32-bit.


NOTES
-----
1) please note that LzmaTest doesn't free allocated memory in some cases. 
But in your real applicaions you must free memory after decompression.

2) All numbers above were calculated for case when int is not more than 
  32-bit in your compiler. If in your compiler int is 64-bit or larger 
  probably lzma can require more memory for some structures.


---

WWW:    http://www.7-zip.org
E-mail: support@7-zip.org
