1*4882a593SmuzhiyunLZMA SDK 9.20 2*4882a593Smuzhiyun------------- 3*4882a593Smuzhiyun 4*4882a593SmuzhiyunLZMA SDK provides the documentation, samples, header files, libraries, 5*4882a593Smuzhiyunand tools you need to develop applications that use LZMA compression. 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunLZMA is default and general compression method of 7z format 8*4882a593Smuzhiyunin 7-Zip compression program (www.7-zip.org). LZMA provides high 9*4882a593Smuzhiyuncompression ratio and very fast decompression. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunLZMA is an improved version of famous LZ77 compression algorithm. 12*4882a593SmuzhiyunIt was improved in way of maximum increasing of compression ratio, 13*4882a593Smuzhiyunkeeping high decompression speed and low memory requirements for 14*4882a593Smuzhiyundecompressing. 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunLICENSE 19*4882a593Smuzhiyun------- 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunLZMA SDK is written and placed in the public domain by Igor Pavlov. 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunSome code in LZMA SDK is based on public domain code from another developers: 24*4882a593Smuzhiyun 1) PPMd var.H (2001): Dmitry Shkarin 25*4882a593Smuzhiyun 2) SHA-256: Wei Dai (Crypto++ library) 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun 28*4882a593SmuzhiyunLZMA SDK Contents 29*4882a593Smuzhiyun----------------- 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunLZMA SDK includes: 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun - ANSI-C/C++/C#/Java source code for LZMA compressing and decompressing 34*4882a593Smuzhiyun - Compiled file->file LZMA compressing/decompressing program for Windows system 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunUNIX/Linux version 38*4882a593Smuzhiyun------------------ 39*4882a593SmuzhiyunTo compile C++ version of file->file LZMA encoding, go to directory 40*4882a593SmuzhiyunCPP/7zip/Bundles/LzmaCon 41*4882a593Smuzhiyunand call make to recompile it: 42*4882a593Smuzhiyun make -f makefile.gcc clean all 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunIn some UNIX/Linux versions you must compile LZMA with static libraries. 45*4882a593SmuzhiyunTo compile with static libraries, you can use 46*4882a593SmuzhiyunLIB = -lm -static 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun 49*4882a593SmuzhiyunFiles 50*4882a593Smuzhiyun--------------------- 51*4882a593Smuzhiyunlzma.txt - LZMA SDK description (this file) 52*4882a593Smuzhiyun7zFormat.txt - 7z Format description 53*4882a593Smuzhiyun7zC.txt - 7z ANSI-C Decoder description 54*4882a593Smuzhiyunmethods.txt - Compression method IDs for .7z 55*4882a593Smuzhiyunlzma.exe - Compiled file->file LZMA encoder/decoder for Windows 56*4882a593Smuzhiyun7zr.exe - 7-Zip with 7z/lzma/xz support. 57*4882a593Smuzhiyunhistory.txt - history of the LZMA SDK 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun 60*4882a593SmuzhiyunSource code structure 61*4882a593Smuzhiyun--------------------- 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunC/ - C files 64*4882a593Smuzhiyun 7zCrc*.* - CRC code 65*4882a593Smuzhiyun Alloc.* - Memory allocation functions 66*4882a593Smuzhiyun Bra*.* - Filters for x86, IA-64, ARM, ARM-Thumb, PowerPC and SPARC code 67*4882a593Smuzhiyun LzFind.* - Match finder for LZ (LZMA) encoders 68*4882a593Smuzhiyun LzFindMt.* - Match finder for LZ (LZMA) encoders for multithreading encoding 69*4882a593Smuzhiyun LzHash.h - Additional file for LZ match finder 70*4882a593Smuzhiyun LzmaDec.* - LZMA decoding 71*4882a593Smuzhiyun LzmaEnc.* - LZMA encoding 72*4882a593Smuzhiyun LzmaLib.* - LZMA Library for DLL calling 73*4882a593Smuzhiyun Types.h - Basic types for another .c files 74*4882a593Smuzhiyun Threads.* - The code for multithreading. 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun LzmaLib - LZMA Library (.DLL for Windows) 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun LzmaUtil - LZMA Utility (file->file LZMA encoder/decoder). 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun Archive - files related to archiving 81*4882a593Smuzhiyun 7z - 7z ANSI-C Decoder 82*4882a593Smuzhiyun 83*4882a593SmuzhiyunCPP/ -- CPP files 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun Common - common files for C++ projects 86*4882a593Smuzhiyun Windows - common files for Windows related code 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun 7zip - files related to 7-Zip Project 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun Common - common files for 7-Zip 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun Compress - files related to compression/decompression 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun Archive - files related to archiving 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun Common - common files for archive handling 97*4882a593Smuzhiyun 7z - 7z C++ Encoder/Decoder 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun Bundles - Modules that are bundles of other modules 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun Alone7z - 7zr.exe: Standalone version of 7z.exe that supports only 7z/LZMA/BCJ/BCJ2 102*4882a593Smuzhiyun LzmaCon - lzma.exe: LZMA compression/decompression 103*4882a593Smuzhiyun Format7zR - 7zr.dll: Reduced version of 7za.dll: extracting/compressing to 7z/LZMA/BCJ/BCJ2 104*4882a593Smuzhiyun Format7zExtractR - 7zxr.dll: Reduced version of 7zxa.dll: extracting from 7z/LZMA/BCJ/BCJ2. 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun UI - User Interface files 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun Client7z - Test application for 7za.dll, 7zr.dll, 7zxr.dll 109*4882a593Smuzhiyun Common - Common UI files 110*4882a593Smuzhiyun Console - Code for console archiver 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun 114*4882a593SmuzhiyunCS/ - C# files 115*4882a593Smuzhiyun 7zip 116*4882a593Smuzhiyun Common - some common files for 7-Zip 117*4882a593Smuzhiyun Compress - files related to compression/decompression 118*4882a593Smuzhiyun LZ - files related to LZ (Lempel-Ziv) compression algorithm 119*4882a593Smuzhiyun LZMA - LZMA compression/decompression 120*4882a593Smuzhiyun LzmaAlone - file->file LZMA compression/decompression 121*4882a593Smuzhiyun RangeCoder - Range Coder (special code of compression/decompression) 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunJava/ - Java files 124*4882a593Smuzhiyun SevenZip 125*4882a593Smuzhiyun Compression - files related to compression/decompression 126*4882a593Smuzhiyun LZ - files related to LZ (Lempel-Ziv) compression algorithm 127*4882a593Smuzhiyun LZMA - LZMA compression/decompression 128*4882a593Smuzhiyun RangeCoder - Range Coder (special code of compression/decompression) 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun 131*4882a593SmuzhiyunC/C++ source code of LZMA SDK is part of 7-Zip project. 132*4882a593Smuzhiyun7-Zip source code can be downloaded from 7-Zip's SourceForge page: 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun http://sourceforge.net/projects/sevenzip/ 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun 138*4882a593SmuzhiyunLZMA features 139*4882a593Smuzhiyun------------- 140*4882a593Smuzhiyun - Variable dictionary size (up to 1 GB) 141*4882a593Smuzhiyun - Estimated compressing speed: about 2 MB/s on 2 GHz CPU 142*4882a593Smuzhiyun - Estimated decompressing speed: 143*4882a593Smuzhiyun - 20-30 MB/s on 2 GHz Core 2 or AMD Athlon 64 144*4882a593Smuzhiyun - 1-2 MB/s on 200 MHz ARM, MIPS, PowerPC or other simple RISC 145*4882a593Smuzhiyun - Small memory requirements for decompressing (16 KB + DictionarySize) 146*4882a593Smuzhiyun - Small code size for decompressing: 5-8 KB 147*4882a593Smuzhiyun 148*4882a593SmuzhiyunLZMA decoder uses only integer operations and can be 149*4882a593Smuzhiyunimplemented in any modern 32-bit CPU (or on 16-bit CPU with some conditions). 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunSome critical operations that affect the speed of LZMA decompression: 152*4882a593Smuzhiyun 1) 32*16 bit integer multiply 153*4882a593Smuzhiyun 2) Misspredicted branches (penalty mostly depends from pipeline length) 154*4882a593Smuzhiyun 3) 32-bit shift and arithmetic operations 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunThe speed of LZMA decompressing mostly depends from CPU speed. 157*4882a593SmuzhiyunMemory speed has no big meaning. But if your CPU has small data cache, 158*4882a593Smuzhiyunoverall weight of memory speed will slightly increase. 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun 161*4882a593SmuzhiyunHow To Use 162*4882a593Smuzhiyun---------- 163*4882a593Smuzhiyun 164*4882a593SmuzhiyunUsing LZMA encoder/decoder executable 165*4882a593Smuzhiyun-------------------------------------- 166*4882a593Smuzhiyun 167*4882a593SmuzhiyunUsage: LZMA <e|d> inputFile outputFile [<switches>...] 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun e: encode file 170*4882a593Smuzhiyun 171*4882a593Smuzhiyun d: decode file 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun b: Benchmark. There are two tests: compressing and decompressing 174*4882a593Smuzhiyun with LZMA method. Benchmark shows rating in MIPS (million 175*4882a593Smuzhiyun instructions per second). Rating value is calculated from 176*4882a593Smuzhiyun measured speed and it is normalized with Intel's Core 2 results. 177*4882a593Smuzhiyun Also Benchmark checks possible hardware errors (RAM 178*4882a593Smuzhiyun errors in most cases). Benchmark uses these settings: 179*4882a593Smuzhiyun (-a1, -d21, -fb32, -mfbt4). You can change only -d parameter. 180*4882a593Smuzhiyun Also you can change the number of iterations. Example for 30 iterations: 181*4882a593Smuzhiyun LZMA b 30 182*4882a593Smuzhiyun Default number of iterations is 10. 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun<Switches> 185*4882a593Smuzhiyun 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun -a{N}: set compression mode 0 = fast, 1 = normal 188*4882a593Smuzhiyun default: 1 (normal) 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun d{N}: Sets Dictionary size - [0, 30], default: 23 (8MB) 191*4882a593Smuzhiyun The maximum value for dictionary size is 1 GB = 2^30 bytes. 192*4882a593Smuzhiyun Dictionary size is calculated as DictionarySize = 2^N bytes. 193*4882a593Smuzhiyun For decompressing file compressed by LZMA method with dictionary 194*4882a593Smuzhiyun size D = 2^N you need about D bytes of memory (RAM). 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun -fb{N}: set number of fast bytes - [5, 273], default: 128 197*4882a593Smuzhiyun Usually big number gives a little bit better compression ratio 198*4882a593Smuzhiyun and slower compression process. 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun -lc{N}: set number of literal context bits - [0, 8], default: 3 201*4882a593Smuzhiyun Sometimes lc=4 gives gain for big files. 202*4882a593Smuzhiyun 203*4882a593Smuzhiyun -lp{N}: set number of literal pos bits - [0, 4], default: 0 204*4882a593Smuzhiyun lp switch is intended for periodical data when period is 205*4882a593Smuzhiyun equal 2^N. For example, for 32-bit (4 bytes) 206*4882a593Smuzhiyun periodical data you can use lp=2. Often it's better to set lc0, 207*4882a593Smuzhiyun if you change lp switch. 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun -pb{N}: set number of pos bits - [0, 4], default: 2 210*4882a593Smuzhiyun pb switch is intended for periodical data 211*4882a593Smuzhiyun when period is equal 2^N. 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun -mf{MF_ID}: set Match Finder. Default: bt4. 214*4882a593Smuzhiyun Algorithms from hc* group doesn't provide good compression 215*4882a593Smuzhiyun ratio, but they often works pretty fast in combination with 216*4882a593Smuzhiyun fast mode (-a0). 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun Memory requirements depend from dictionary size 219*4882a593Smuzhiyun (parameter "d" in table below). 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun MF_ID Memory Description 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun bt2 d * 9.5 + 4MB Binary Tree with 2 bytes hashing. 224*4882a593Smuzhiyun bt3 d * 11.5 + 4MB Binary Tree with 3 bytes hashing. 225*4882a593Smuzhiyun bt4 d * 11.5 + 4MB Binary Tree with 4 bytes hashing. 226*4882a593Smuzhiyun hc4 d * 7.5 + 4MB Hash Chain with 4 bytes hashing. 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun -eos: write End Of Stream marker. By default LZMA doesn't write 229*4882a593Smuzhiyun eos marker, since LZMA decoder knows uncompressed size 230*4882a593Smuzhiyun stored in .lzma file header. 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun -si: Read data from stdin (it will write End Of Stream marker). 233*4882a593Smuzhiyun -so: Write data to stdout 234*4882a593Smuzhiyun 235*4882a593Smuzhiyun 236*4882a593SmuzhiyunExamples: 237*4882a593Smuzhiyun 238*4882a593Smuzhiyun1) LZMA e file.bin file.lzma -d16 -lc0 239*4882a593Smuzhiyun 240*4882a593Smuzhiyuncompresses file.bin to file.lzma with 64 KB dictionary (2^16=64K) 241*4882a593Smuzhiyunand 0 literal context bits. -lc0 allows to reduce memory requirements 242*4882a593Smuzhiyunfor decompression. 243*4882a593Smuzhiyun 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun2) LZMA e file.bin file.lzma -lc0 -lp2 246*4882a593Smuzhiyun 247*4882a593Smuzhiyuncompresses file.bin to file.lzma with settings suitable 248*4882a593Smuzhiyunfor 32-bit periodical data (for example, ARM or MIPS code). 249*4882a593Smuzhiyun 250*4882a593Smuzhiyun3) LZMA d file.lzma file.bin 251*4882a593Smuzhiyun 252*4882a593Smuzhiyundecompresses file.lzma to file.bin. 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun 255*4882a593SmuzhiyunCompression ratio hints 256*4882a593Smuzhiyun----------------------- 257*4882a593Smuzhiyun 258*4882a593SmuzhiyunRecommendations 259*4882a593Smuzhiyun--------------- 260*4882a593Smuzhiyun 261*4882a593SmuzhiyunTo increase the compression ratio for LZMA compressing it's desirable 262*4882a593Smuzhiyunto have aligned data (if it's possible) and also it's desirable to locate 263*4882a593Smuzhiyundata in such order, where code is grouped in one place and data is 264*4882a593Smuzhiyungrouped in other place (it's better than such mixing: code, data, code, 265*4882a593Smuzhiyundata, ...). 266*4882a593Smuzhiyun 267*4882a593Smuzhiyun 268*4882a593SmuzhiyunFilters 269*4882a593Smuzhiyun------- 270*4882a593SmuzhiyunYou can increase the compression ratio for some data types, using 271*4882a593Smuzhiyunspecial filters before compressing. For example, it's possible to 272*4882a593Smuzhiyunincrease the compression ratio on 5-10% for code for those CPU ISAs: 273*4882a593Smuzhiyunx86, IA-64, ARM, ARM-Thumb, PowerPC, SPARC. 274*4882a593Smuzhiyun 275*4882a593SmuzhiyunYou can find C source code of such filters in C/Bra*.* files 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunYou can check the compression ratio gain of these filters with such 278*4882a593Smuzhiyun7-Zip commands (example for ARM code): 279*4882a593SmuzhiyunNo filter: 280*4882a593Smuzhiyun 7z a a1.7z a.bin -m0=lzma 281*4882a593Smuzhiyun 282*4882a593SmuzhiyunWith filter for little-endian ARM code: 283*4882a593Smuzhiyun 7z a a2.7z a.bin -m0=arm -m1=lzma 284*4882a593Smuzhiyun 285*4882a593SmuzhiyunIt works in such manner: 286*4882a593SmuzhiyunCompressing = Filter_encoding + LZMA_encoding 287*4882a593SmuzhiyunDecompressing = LZMA_decoding + Filter_decoding 288*4882a593Smuzhiyun 289*4882a593SmuzhiyunCompressing and decompressing speed of such filters is very high, 290*4882a593Smuzhiyunso it will not increase decompressing time too much. 291*4882a593SmuzhiyunMoreover, it reduces decompression time for LZMA_decoding, 292*4882a593Smuzhiyunsince compression ratio with filtering is higher. 293*4882a593Smuzhiyun 294*4882a593SmuzhiyunThese filters convert CALL (calling procedure) instructions 295*4882a593Smuzhiyunfrom relative offsets to absolute addresses, so such data becomes more 296*4882a593Smuzhiyuncompressible. 297*4882a593Smuzhiyun 298*4882a593SmuzhiyunFor some ISAs (for example, for MIPS) it's impossible to get gain from such filter. 299*4882a593Smuzhiyun 300*4882a593Smuzhiyun 301*4882a593SmuzhiyunLZMA compressed file format 302*4882a593Smuzhiyun--------------------------- 303*4882a593SmuzhiyunOffset Size Description 304*4882a593Smuzhiyun 0 1 Special LZMA properties (lc,lp, pb in encoded form) 305*4882a593Smuzhiyun 1 4 Dictionary size (little endian) 306*4882a593Smuzhiyun 5 8 Uncompressed size (little endian). -1 means unknown size 307*4882a593Smuzhiyun 13 Compressed data 308*4882a593Smuzhiyun 309*4882a593Smuzhiyun 310*4882a593SmuzhiyunANSI-C LZMA Decoder 311*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 312*4882a593Smuzhiyun 313*4882a593SmuzhiyunPlease note that interfaces for ANSI-C code were changed in LZMA SDK 4.58. 314*4882a593SmuzhiyunIf you want to use old interfaces you can download previous version of LZMA SDK 315*4882a593Smuzhiyunfrom sourceforge.net site. 316*4882a593Smuzhiyun 317*4882a593SmuzhiyunTo use ANSI-C LZMA Decoder you need the following files: 318*4882a593Smuzhiyun1) LzmaDec.h + LzmaDec.c + Types.h 319*4882a593SmuzhiyunLzmaUtil/LzmaUtil.c is example application that uses these files. 320*4882a593Smuzhiyun 321*4882a593Smuzhiyun 322*4882a593SmuzhiyunMemory requirements for LZMA decoding 323*4882a593Smuzhiyun------------------------------------- 324*4882a593Smuzhiyun 325*4882a593SmuzhiyunStack usage of LZMA decoding function for local variables is not 326*4882a593Smuzhiyunlarger than 200-400 bytes. 327*4882a593Smuzhiyun 328*4882a593SmuzhiyunLZMA Decoder uses dictionary buffer and internal state structure. 329*4882a593SmuzhiyunInternal state structure consumes 330*4882a593Smuzhiyun state_size = (4 + (1.5 << (lc + lp))) KB 331*4882a593Smuzhiyunby default (lc=3, lp=0), state_size = 16 KB. 332*4882a593Smuzhiyun 333*4882a593Smuzhiyun 334*4882a593SmuzhiyunHow To decompress data 335*4882a593Smuzhiyun---------------------- 336*4882a593Smuzhiyun 337*4882a593SmuzhiyunLZMA Decoder (ANSI-C version) now supports 2 interfaces: 338*4882a593Smuzhiyun1) Single-call Decompressing 339*4882a593Smuzhiyun2) Multi-call State Decompressing (zlib-like interface) 340*4882a593Smuzhiyun 341*4882a593SmuzhiyunYou must use external allocator: 342*4882a593SmuzhiyunExample: 343*4882a593Smuzhiyunvoid *SzAlloc(void *p, size_t size) { p = p; return malloc(size); } 344*4882a593Smuzhiyunvoid SzFree(void *p, void *address) { p = p; free(address); } 345*4882a593SmuzhiyunISzAlloc alloc = { SzAlloc, SzFree }; 346*4882a593Smuzhiyun 347*4882a593SmuzhiyunYou can use p = p; operator to disable compiler warnings. 348*4882a593Smuzhiyun 349*4882a593Smuzhiyun 350*4882a593SmuzhiyunSingle-call Decompressing 351*4882a593Smuzhiyun------------------------- 352*4882a593SmuzhiyunWhen to use: RAM->RAM decompressing 353*4882a593SmuzhiyunCompile files: LzmaDec.h + LzmaDec.c + Types.h 354*4882a593SmuzhiyunCompile defines: no defines 355*4882a593SmuzhiyunMemory Requirements: 356*4882a593Smuzhiyun - Input buffer: compressed size 357*4882a593Smuzhiyun - Output buffer: uncompressed size 358*4882a593Smuzhiyun - LZMA Internal Structures: state_size (16 KB for default settings) 359*4882a593Smuzhiyun 360*4882a593SmuzhiyunInterface: 361*4882a593Smuzhiyun int LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, 362*4882a593Smuzhiyun const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, 363*4882a593Smuzhiyun ELzmaStatus *status, ISzAlloc *alloc); 364*4882a593Smuzhiyun In: 365*4882a593Smuzhiyun dest - output data 366*4882a593Smuzhiyun destLen - output data size 367*4882a593Smuzhiyun src - input data 368*4882a593Smuzhiyun srcLen - input data size 369*4882a593Smuzhiyun propData - LZMA properties (5 bytes) 370*4882a593Smuzhiyun propSize - size of propData buffer (5 bytes) 371*4882a593Smuzhiyun finishMode - It has meaning only if the decoding reaches output limit (*destLen). 372*4882a593Smuzhiyun LZMA_FINISH_ANY - Decode just destLen bytes. 373*4882a593Smuzhiyun LZMA_FINISH_END - Stream must be finished after (*destLen). 374*4882a593Smuzhiyun You can use LZMA_FINISH_END, when you know that 375*4882a593Smuzhiyun current output buffer covers last bytes of stream. 376*4882a593Smuzhiyun alloc - Memory allocator. 377*4882a593Smuzhiyun 378*4882a593Smuzhiyun Out: 379*4882a593Smuzhiyun destLen - processed output size 380*4882a593Smuzhiyun srcLen - processed input size 381*4882a593Smuzhiyun 382*4882a593Smuzhiyun Output: 383*4882a593Smuzhiyun SZ_OK 384*4882a593Smuzhiyun status: 385*4882a593Smuzhiyun LZMA_STATUS_FINISHED_WITH_MARK 386*4882a593Smuzhiyun LZMA_STATUS_NOT_FINISHED 387*4882a593Smuzhiyun LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 388*4882a593Smuzhiyun SZ_ERROR_DATA - Data error 389*4882a593Smuzhiyun SZ_ERROR_MEM - Memory allocation error 390*4882a593Smuzhiyun SZ_ERROR_UNSUPPORTED - Unsupported properties 391*4882a593Smuzhiyun SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). 392*4882a593Smuzhiyun 393*4882a593Smuzhiyun If LZMA decoder sees end_marker before reaching output limit, it returns OK result, 394*4882a593Smuzhiyun and output value of destLen will be less than output buffer size limit. 395*4882a593Smuzhiyun 396*4882a593Smuzhiyun You can use multiple checks to test data integrity after full decompression: 397*4882a593Smuzhiyun 1) Check Result and "status" variable. 398*4882a593Smuzhiyun 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize. 399*4882a593Smuzhiyun 3) Check that output(srcLen) = compressedSize, if you know real compressedSize. 400*4882a593Smuzhiyun You must use correct finish mode in that case. */ 401*4882a593Smuzhiyun 402*4882a593Smuzhiyun 403*4882a593SmuzhiyunMulti-call State Decompressing (zlib-like interface) 404*4882a593Smuzhiyun---------------------------------------------------- 405*4882a593Smuzhiyun 406*4882a593SmuzhiyunWhen to use: file->file decompressing 407*4882a593SmuzhiyunCompile files: LzmaDec.h + LzmaDec.c + Types.h 408*4882a593Smuzhiyun 409*4882a593SmuzhiyunMemory Requirements: 410*4882a593Smuzhiyun - Buffer for input stream: any size (for example, 16 KB) 411*4882a593Smuzhiyun - Buffer for output stream: any size (for example, 16 KB) 412*4882a593Smuzhiyun - LZMA Internal Structures: state_size (16 KB for default settings) 413*4882a593Smuzhiyun - LZMA dictionary (dictionary size is encoded in LZMA properties header) 414*4882a593Smuzhiyun 415*4882a593Smuzhiyun1) read LZMA properties (5 bytes) and uncompressed size (8 bytes, little-endian) to header: 416*4882a593Smuzhiyun unsigned char header[LZMA_PROPS_SIZE + 8]; 417*4882a593Smuzhiyun ReadFile(inFile, header, sizeof(header) 418*4882a593Smuzhiyun 419*4882a593Smuzhiyun2) Allocate CLzmaDec structures (state + dictionary) using LZMA properties 420*4882a593Smuzhiyun 421*4882a593Smuzhiyun CLzmaDec state; 422*4882a593Smuzhiyun LzmaDec_Constr(&state); 423*4882a593Smuzhiyun res = LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc); 424*4882a593Smuzhiyun if (res != SZ_OK) 425*4882a593Smuzhiyun return res; 426*4882a593Smuzhiyun 427*4882a593Smuzhiyun3) Init LzmaDec structure before any new LZMA stream. And call LzmaDec_DecodeToBuf in loop 428*4882a593Smuzhiyun 429*4882a593Smuzhiyun LzmaDec_Init(&state); 430*4882a593Smuzhiyun for (;;) 431*4882a593Smuzhiyun { 432*4882a593Smuzhiyun ... 433*4882a593Smuzhiyun int res = LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, 434*4882a593Smuzhiyun const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode); 435*4882a593Smuzhiyun ... 436*4882a593Smuzhiyun } 437*4882a593Smuzhiyun 438*4882a593Smuzhiyun 439*4882a593Smuzhiyun4) Free all allocated structures 440*4882a593Smuzhiyun LzmaDec_Free(&state, &g_Alloc); 441*4882a593Smuzhiyun 442*4882a593SmuzhiyunFor full code example, look at C/LzmaUtil/LzmaUtil.c code. 443*4882a593Smuzhiyun 444*4882a593Smuzhiyun 445*4882a593SmuzhiyunHow To compress data 446*4882a593Smuzhiyun-------------------- 447*4882a593Smuzhiyun 448*4882a593SmuzhiyunCompile files: LzmaEnc.h + LzmaEnc.c + Types.h + 449*4882a593SmuzhiyunLzFind.c + LzFind.h + LzFindMt.c + LzFindMt.h + LzHash.h 450*4882a593Smuzhiyun 451*4882a593SmuzhiyunMemory Requirements: 452*4882a593Smuzhiyun - (dictSize * 11.5 + 6 MB) + state_size 453*4882a593Smuzhiyun 454*4882a593SmuzhiyunLzma Encoder can use two memory allocators: 455*4882a593Smuzhiyun1) alloc - for small arrays. 456*4882a593Smuzhiyun2) allocBig - for big arrays. 457*4882a593Smuzhiyun 458*4882a593SmuzhiyunFor example, you can use Large RAM Pages (2 MB) in allocBig allocator for 459*4882a593Smuzhiyunbetter compression speed. Note that Windows has bad implementation for 460*4882a593SmuzhiyunLarge RAM Pages. 461*4882a593SmuzhiyunIt's OK to use same allocator for alloc and allocBig. 462*4882a593Smuzhiyun 463*4882a593Smuzhiyun 464*4882a593SmuzhiyunSingle-call Compression with callbacks 465*4882a593Smuzhiyun-------------------------------------- 466*4882a593Smuzhiyun 467*4882a593SmuzhiyunCheck C/LzmaUtil/LzmaUtil.c as example, 468*4882a593Smuzhiyun 469*4882a593SmuzhiyunWhen to use: file->file decompressing 470*4882a593Smuzhiyun 471*4882a593Smuzhiyun1) you must implement callback structures for interfaces: 472*4882a593SmuzhiyunISeqInStream 473*4882a593SmuzhiyunISeqOutStream 474*4882a593SmuzhiyunICompressProgress 475*4882a593SmuzhiyunISzAlloc 476*4882a593Smuzhiyun 477*4882a593Smuzhiyunstatic void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); } 478*4882a593Smuzhiyunstatic void SzFree(void *p, void *address) { p = p; MyFree(address); } 479*4882a593Smuzhiyunstatic ISzAlloc g_Alloc = { SzAlloc, SzFree }; 480*4882a593Smuzhiyun 481*4882a593Smuzhiyun CFileSeqInStream inStream; 482*4882a593Smuzhiyun CFileSeqOutStream outStream; 483*4882a593Smuzhiyun 484*4882a593Smuzhiyun inStream.funcTable.Read = MyRead; 485*4882a593Smuzhiyun inStream.file = inFile; 486*4882a593Smuzhiyun outStream.funcTable.Write = MyWrite; 487*4882a593Smuzhiyun outStream.file = outFile; 488*4882a593Smuzhiyun 489*4882a593Smuzhiyun 490*4882a593Smuzhiyun2) Create CLzmaEncHandle object; 491*4882a593Smuzhiyun 492*4882a593Smuzhiyun CLzmaEncHandle enc; 493*4882a593Smuzhiyun 494*4882a593Smuzhiyun enc = LzmaEnc_Create(&g_Alloc); 495*4882a593Smuzhiyun if (enc == 0) 496*4882a593Smuzhiyun return SZ_ERROR_MEM; 497*4882a593Smuzhiyun 498*4882a593Smuzhiyun 499*4882a593Smuzhiyun3) initialize CLzmaEncProps properties; 500*4882a593Smuzhiyun 501*4882a593Smuzhiyun LzmaEncProps_Init(&props); 502*4882a593Smuzhiyun 503*4882a593Smuzhiyun Then you can change some properties in that structure. 504*4882a593Smuzhiyun 505*4882a593Smuzhiyun4) Send LZMA properties to LZMA Encoder 506*4882a593Smuzhiyun 507*4882a593Smuzhiyun res = LzmaEnc_SetProps(enc, &props); 508*4882a593Smuzhiyun 509*4882a593Smuzhiyun5) Write encoded properties to header 510*4882a593Smuzhiyun 511*4882a593Smuzhiyun Byte header[LZMA_PROPS_SIZE + 8]; 512*4882a593Smuzhiyun size_t headerSize = LZMA_PROPS_SIZE; 513*4882a593Smuzhiyun UInt64 fileSize; 514*4882a593Smuzhiyun int i; 515*4882a593Smuzhiyun 516*4882a593Smuzhiyun res = LzmaEnc_WriteProperties(enc, header, &headerSize); 517*4882a593Smuzhiyun fileSize = MyGetFileLength(inFile); 518*4882a593Smuzhiyun for (i = 0; i < 8; i++) 519*4882a593Smuzhiyun header[headerSize++] = (Byte)(fileSize >> (8 * i)); 520*4882a593Smuzhiyun MyWriteFileAndCheck(outFile, header, headerSize) 521*4882a593Smuzhiyun 522*4882a593Smuzhiyun6) Call encoding function: 523*4882a593Smuzhiyun res = LzmaEnc_Encode(enc, &outStream.funcTable, &inStream.funcTable, 524*4882a593Smuzhiyun NULL, &g_Alloc, &g_Alloc); 525*4882a593Smuzhiyun 526*4882a593Smuzhiyun7) Destroy LZMA Encoder Object 527*4882a593Smuzhiyun LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc); 528*4882a593Smuzhiyun 529*4882a593Smuzhiyun 530*4882a593SmuzhiyunIf callback function return some error code, LzmaEnc_Encode also returns that code 531*4882a593Smuzhiyunor it can return the code like SZ_ERROR_READ, SZ_ERROR_WRITE or SZ_ERROR_PROGRESS. 532*4882a593Smuzhiyun 533*4882a593Smuzhiyun 534*4882a593SmuzhiyunSingle-call RAM->RAM Compression 535*4882a593Smuzhiyun-------------------------------- 536*4882a593Smuzhiyun 537*4882a593SmuzhiyunSingle-call RAM->RAM Compression is similar to Compression with callbacks, 538*4882a593Smuzhiyunbut you provide pointers to buffers instead of pointers to stream callbacks: 539*4882a593Smuzhiyun 540*4882a593SmuzhiyunHRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 541*4882a593Smuzhiyun CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, 542*4882a593Smuzhiyun ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); 543*4882a593Smuzhiyun 544*4882a593SmuzhiyunReturn code: 545*4882a593Smuzhiyun SZ_OK - OK 546*4882a593Smuzhiyun SZ_ERROR_MEM - Memory allocation error 547*4882a593Smuzhiyun SZ_ERROR_PARAM - Incorrect paramater 548*4882a593Smuzhiyun SZ_ERROR_OUTPUT_EOF - output buffer overflow 549*4882a593Smuzhiyun SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) 550*4882a593Smuzhiyun 551*4882a593Smuzhiyun 552*4882a593Smuzhiyun 553*4882a593SmuzhiyunDefines 554*4882a593Smuzhiyun------- 555*4882a593Smuzhiyun 556*4882a593Smuzhiyun_LZMA_SIZE_OPT - Enable some optimizations in LZMA Decoder to get smaller executable code. 557*4882a593Smuzhiyun 558*4882a593Smuzhiyun_LZMA_PROB32 - It can increase the speed on some 32-bit CPUs, but memory usage for 559*4882a593Smuzhiyun some structures will be doubled in that case. 560*4882a593Smuzhiyun 561*4882a593Smuzhiyun_LZMA_UINT32_IS_ULONG - Define it if int is 16-bit on your compiler and long is 32-bit. 562*4882a593Smuzhiyun 563*4882a593Smuzhiyun_LZMA_NO_SYSTEM_SIZE_T - Define it if you don't want to use size_t type. 564*4882a593Smuzhiyun 565*4882a593Smuzhiyun 566*4882a593Smuzhiyun_7ZIP_PPMD_SUPPPORT - Define it if you don't want to support PPMD method in AMSI-C .7z decoder. 567*4882a593Smuzhiyun 568*4882a593Smuzhiyun 569*4882a593SmuzhiyunC++ LZMA Encoder/Decoder 570*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 571*4882a593SmuzhiyunC++ LZMA code use COM-like interfaces. So if you want to use it, 572*4882a593Smuzhiyunyou can study basics of COM/OLE. 573*4882a593SmuzhiyunC++ LZMA code is just wrapper over ANSI-C code. 574*4882a593Smuzhiyun 575*4882a593Smuzhiyun 576*4882a593SmuzhiyunC++ Notes 577*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 578*4882a593SmuzhiyunIf you use some C++ code folders in 7-Zip (for example, C++ code for .7z handling), 579*4882a593Smuzhiyunyou must check that you correctly work with "new" operator. 580*4882a593Smuzhiyun7-Zip can be compiled with MSVC 6.0 that doesn't throw "exception" from "new" operator. 581*4882a593SmuzhiyunSo 7-Zip uses "CPP\Common\NewHandler.cpp" that redefines "new" operator: 582*4882a593Smuzhiyunoperator new(size_t size) 583*4882a593Smuzhiyun{ 584*4882a593Smuzhiyun void *p = ::malloc(size); 585*4882a593Smuzhiyun if (p == 0) 586*4882a593Smuzhiyun throw CNewException(); 587*4882a593Smuzhiyun return p; 588*4882a593Smuzhiyun} 589*4882a593SmuzhiyunIf you use MSCV that throws exception for "new" operator, you can compile without 590*4882a593Smuzhiyun"NewHandler.cpp". So standard exception will be used. Actually some code of 591*4882a593Smuzhiyun7-Zip catches any exception in internal code and converts it to HRESULT code. 592*4882a593SmuzhiyunSo you don't need to catch CNewException, if you call COM interfaces of 7-Zip. 593*4882a593Smuzhiyun 594*4882a593Smuzhiyun--- 595*4882a593Smuzhiyun 596*4882a593Smuzhiyunhttp://www.7-zip.org 597*4882a593Smuzhiyunhttp://www.7-zip.org/sdk.html 598*4882a593Smuzhiyunhttp://www.7-zip.org/support.html 599