### File-to-File LZMA Compression Setup Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Prepare input and output stream structures for file-to-file compression. Implement callback functions for reading and writing. ```c CFileSeqInStream inStream; CFileSeqOutStream outStream; inStream.funcTable.Read = MyRead; inStream.file = inFile; outStream.funcTable.Write = MyWrite; outStream.file = outFile; ``` -------------------------------- ### LZMA Decoder Memory Allocator Example Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Defines custom memory allocation functions (SzAlloc and SzFree) for the LZMA decoder. These functions are required for memory management during decompression. ```c void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); } void SzFree(void *p, void *address) { p = p; free(address); } ISzAlloc alloc = { SzAlloc, SzFree }; ``` -------------------------------- ### Create 7z Archive with 7-Zip Source: https://github.com/ip7z/7zip/blob/main/DOC/7zC.txt Use 7z.exe or 7za.exe to create .7z archives. For archives with many files requiring fast extraction, consider using partly-solid archives with a specified solid block size. ```bash 7z.exe a archive.7z *.htm -r -mx -m0fb=255 ``` ```bash 7za.exe a archive.7z *.htm -ms=512K -r -mx -m0fb=255 -m0d=512K ``` -------------------------------- ### Initialize 7z Archive Database Source: https://github.com/ip7z/7zip/blob/main/DOC/7zC.txt Before opening an archive, initialize CRC structures and the 7z archive database using CrcGenerateTable() and SzArEx_Init(). ```c CrcGenerateTable(); SzArEx_Init(&db); ``` -------------------------------- ### Compile with GCC Source: https://github.com/ip7z/7zip/blob/main/DOC/readme.txt Compile 7-Zip using GCC without assembler. Navigate to the appropriate directory and use the specified makefile. ```makefile cd CPP/7zip/Bundles/Alone2 make -j -f ../../cmpl_gcc.mak ``` -------------------------------- ### Compile with CLANG Source: https://github.com/ip7z/7zip/blob/main/DOC/readme.txt Compile 7-Zip using CLANG without assembler. Ensure you are in the correct directory before running the make command. ```makefile make -j -f ../../cmpl_clang.mak ``` -------------------------------- ### Basic GCC Compilation Source: https://github.com/ip7z/7zip/blob/main/DOC/readme.txt The fundamental command to compile 7-Zip using the GCC toolchain via the makefile.gcc. ```makefile make -j -f makefile.gcc ``` -------------------------------- ### Compile for x86-64 with Asm Assembler Source: https://github.com/ip7z/7zip/blob/main/DOC/readme.txt Compile 7-Zip for x86-64 architecture utilizing the Asm assembler. This command should be executed from the appropriate directory. ```makefile make -j -f ../../cmpl_gcc_x64.mak ``` -------------------------------- ### Compile for arm64 with Assembler Source: https://github.com/ip7z/7zip/blob/main/DOC/readme.txt Compile 7-Zip for the arm64 architecture, enabling assembler support. The command requires the correct makefile path. ```makefile make -j -f ../../cmpl_gcc_arm64.mak ``` -------------------------------- ### Open 7z Archive with Custom Allocators Source: https://github.com/ip7z/7zip/blob/main/DOC/7zC.txt Open a 7z archive using SzArEx_Open, providing the archive stream and custom memory allocation functions for the main and temporary pools. ```c SzArEx_Open(&db, inStream, &allocMain, &allocTemp); ``` -------------------------------- ### Initialize LZMA Encoder Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Create and initialize an LZMA encoder object. Handle potential memory allocation errors during creation. ```c CLzmaEncHandle enc; enc = LzmaEnc_Create(&g_Alloc); if (enc == 0) return SZ_ERROR_MEM; ``` -------------------------------- ### Compile for arm64 on macOS Source: https://github.com/ip7z/7zip/blob/main/DOC/readme.txt Compile 7-Zip specifically for arm64 on macOS. This uses a dedicated makefile for the platform. ```makefile make -j -f ../../cmpl_mac_arm64.mak ``` -------------------------------- ### Compile with GCC and UASM Assembler Source: https://github.com/ip7z/7zip/blob/main/DOC/readme.txt Compile 7-Zip using GCC with UASM assembler for x86-64. This involves setting specific make variables to point to the UASM executable. ```makefile UASM="$PWD/GccUnixR/uasm" cd "7zip-src/CPP/7zip/Bundles/Alone2" make -f makefile.gcc -j IS_X64=1 USE_ASM=1 MY_ASM="$UASM" ``` -------------------------------- ### LZMA Decoder API (ANSI-C) Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Details on using the ANSI-C LZMA Decoder, including required files, memory considerations, and the two primary interfaces for decompression. ```APIDOC ## LZMA Decoder (ANSI-C) ### Description This section details the ANSI-C LZMA Decoder, which provides interfaces for both single-call and multi-call decompression. It outlines the necessary files, memory requirements, and the specific function signatures. ### Required Files - LzmaDec.h - LzmaDec.c - 7zTypes.h - Precomp.h (optional, for precompiled headers) - Compiler.h (optional, for compiler-specific definitions) ### Memory Requirements - **Stack Usage**: Not larger than 200-400 bytes for local variables. - **LZMA Internal Structures**: `state_size = (4 + (1.5 << (lc + lp))) KB`. By default (lc=3, lp=0), `state_size = 16 KB`. - **Dictionary Buffer**: Size depends on the dictionary size encoded in the LZMA properties header. ### External Allocator An external memory allocator must be provided: ```c void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); } void SzFree(void *p, void *address) { p = p; free(address); } ISzAlloc alloc = { SzAlloc, SzFree }; ``` ### Interface 1: Single-call Decompressing #### Description Suitable for RAM-to-RAM decompressing. Requires input and output buffers sized according to compressed and uncompressed data, respectively. #### Compile Files LzmaDec.h + LzmaDec.c + 7zTypes.h #### Compile Defines No specific defines required. #### Memory Requirements - **Input Buffer**: Compressed size. - **Output Buffer**: Uncompressed size. - **LZMA Internal Structures**: `state_size` (16 KB for default settings). #### Interface Signature ```c int LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc); ``` #### Parameters - **dest** (Byte *) - Output data buffer. - **destLen** (SizeT *) - Pointer to the size of the output data buffer. On output, it will contain the processed output size. - **src** (const Byte *) - Input data buffer. - **srcLen** (SizeT *) - Pointer to the size of the input data buffer. On output, it will contain the processed input size. - **propData** (const Byte *) - LZMA properties (typically 5 bytes). - **propSize** (unsigned) - Size of the `propData` buffer (must be 5 bytes). - **finishMode** (ELzmaFinishMode) - Controls stream finishing behavior when output buffer limit is reached. Options: `LZMA_FINISH_ANY`, `LZMA_FINISH_END`. - **status** (ELzmaStatus *) - Pointer to receive the decoding status. - **alloc** (ISzAlloc *) - Pointer to the memory allocator structure. #### Output Status Codes - **SZ_OK**: Success. - **status** can be `LZMA_STATUS_FINISHED_WITH_MARK`, `LZMA_STATUS_NOT_FINISHED`, or `LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK`. - **SZ_ERROR_DATA**: Data integrity error. - **SZ_ERROR_MEM**: Memory allocation error. - **SZ_ERROR_UNSUPPORTED**: Unsupported properties. - **SZ_ERROR_INPUT_EOF**: More input bytes are required. #### Integrity Checks After full decompression, verify: 1. Result code and `status` variable. 2. `destLen` equals the known uncompressed size. 3. `srcLen` equals the known compressed size (if using correct `finishMode`). ### Interface 2: Multi-call State Decompressing (zlib-like) #### Description Suitable for file-to-file decompressing. Uses a zlib-like interface for incremental processing. #### Compile Files LzmaDec.h + LzmaDec.c + 7zTypes.h #### Memory Requirements - **Input Stream Buffer**: Any size (e.g., 16 KB). - **Output Stream Buffer**: Any size (e.g., 16 KB). - **LZMA Internal Structures**: `state_size` (16 KB for default settings). - **LZMA Dictionary**: Size is encoded in the LZMA properties header. #### Initialization Steps 1. Read LZMA properties (5 bytes) and uncompressed size (8 bytes, little-endian) into a header buffer. ```c unsigned char header[LZMA_PROPS_SIZE + 8]; ReadFile(inFile, header, sizeof(header)); ``` 2. Allocate `CLzmaDec` structures (state + dictionary) using the LZMA properties. ```c CLzmaDec state; LzmaDec_Constr(&state); res = LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc); if (res != SZ_OK) // Handle allocation error ``` ``` -------------------------------- ### Set LZMA Encoder Properties Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Initialize LZMA encoder properties and optionally modify them before sending them to the encoder. ```c LzmaEncProps_Init(&props); Then you can change some properties in that structure. ``` ```c res = LzmaEnc_SetProps(enc, &props); ``` -------------------------------- ### 7z Decoder Test Application Usage Source: https://github.com/ip7z/7zip/blob/main/DOC/7zC.txt The 7zDec test application supports 'e' for extracting, 'l' for listing, and 't' for testing archive integrity. Provide the command and archive name as arguments. ```bash 7zDec l archive.7z ``` ```bash 7zDec e archive.7z ``` -------------------------------- ### Multi-call LZMA Decompression Initialization Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Initializes the LZMA decoder structure for multi-call decompression. This is typically used for file-to-file decompression and requires reading LZMA properties and uncompressed size from a header. ```c unsigned char header[LZMA_PROPS_SIZE + 8]; ReadFile(inFile, header, sizeof(header)) CLzmaDec state; LzmaDec_Constr(&state); res = LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc); if (res != SZ_OK) ``` -------------------------------- ### Initialize and Decode LZMA Stream Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Initialize the LzmaDec structure before processing an LZMA stream and decode data in a loop. Ensure to free allocated structures after use. ```c LzmaDec_Init(&state); for (;;) { ... int res = LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode); ... } ``` ```c LzmaDec_Free(&state, &g_Alloc); ``` -------------------------------- ### Write LZMA Encoded Properties and File Size Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Encode LZMA properties and append the file size to a header buffer. Write this header to the output file. ```c Byte header[LZMA_PROPS_SIZE + 8]; size_t headerSize = LZMA_PROPS_SIZE; UInt64 fileSize; int i; res = LzmaEnc_WriteProperties(enc, header, &headerSize); fileSize = MyGetFileLength(inFile); for (i = 0; i < 8; i++) header[headerSize++] = (Byte)(fileSize >> (8 * i)); MyWriteFileAndCheck(outFile, header, headerSize) ``` -------------------------------- ### Custom Memory Allocators for LZMA Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Define custom memory allocation functions for LZMA, separating small and large array allocations. It's permissible to use the same allocator for both. ```c static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); } static void SzFree(void *p, void *address) { p = p; MyFree(address); } static ISzAlloc g_Alloc = { SzAlloc, SzFree }; ``` -------------------------------- ### Free 7z Archive Database Memory Source: https://github.com/ip7z/7zip/blob/main/DOC/7zC.txt After processing the archive, free the allocated memory for the database using SzArEx_Free and the main pool's free function. ```c SzArEx_Free(&db, allocImp.Free); ``` -------------------------------- ### RAM-to-RAM LZMA Compression Function Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Compress data directly from a source buffer to a destination buffer using LZMA. This function handles properties encoding and optional progress callbacks. ```c SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); ``` -------------------------------- ### Perform LZMA Encoding Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Execute the LZMA encoding process using the provided stream callbacks and memory allocators. This function can return various error codes. ```c res = LzmaEnc_Encode(enc, &outStream.funcTable, &inStream.funcTable, NULL, &g_Alloc, &g_Alloc); ``` -------------------------------- ### Single-call LZMA Decompression Interface Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Interface for single-call LZMA decompression, suitable for RAM-to-RAM decompression. Requires LzmaDec.h, LzmaDec.c, and 7zTypes.h. Ensure correct input and output buffer sizes and LZMA properties. ```c int LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc); ``` -------------------------------- ### Extract File from 7z Archive Source: https://github.com/ip7z/7zip/blob/main/DOC/7zC.txt The SzAr_Extract function decompresses a specific file from the archive. It utilizes provided memory allocators and can leverage previous decompression results for caching solid blocks. ```c SZ_RESULT SzAr_Extract( CArchiveDatabaseEx *db, ILookInStream *inStream, UInt32 fileIndex, /* index of file */ UInt32 *blockIndex, /* index of solid block */ Byte **outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */ size_t *outBufferSize, /* buffer size for output buffer */ size_t *offset, /* offset of stream for required file in *outBuffer */ size_t *outSizeProcessed, /* size of file in *outBuffer */ ISzAlloc *allocMain, ISzAlloc *allocTemp); ``` -------------------------------- ### Redefined 'new' operator in 7-Zip (pre-VS 2010) Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt This C++ code shows how 7-Zip redefines the 'new' operator using 'malloc' and a custom exception 'CNewException' when compiled with older MSVC compilers (prior to VS 2010). ```cpp operator new(size_t size) { void *p = ::malloc(size); if (!p) throw CNewException(); return p; } ``` -------------------------------- ### Destroy LZMA Encoder Source: https://github.com/ip7z/7zip/blob/main/DOC/lzma.txt Clean up and release resources associated with the LZMA encoder object using the specified memory allocators. ```c LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.