{"id":16109874,"url":"https://github.com/clausklein/lzma-sdk","last_synced_at":"2025-06-13T08:37:05.966Z","repository":{"id":151753858,"uuid":"369159943","full_name":"ClausKlein/lzma-sdk","owner":"ClausKlein","description":null,"archived":false,"fork":false,"pushed_at":"2021-05-21T13:06:54.000Z","size":177,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-02-12T11:13:38.576Z","etag":null,"topics":["c","clang-tidy","cmake","lzma"],"latest_commit_sha":null,"homepage":"http://www.7-zip.org/sdk.html","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ClausKlein.png","metadata":{"files":{"readme":"README.rst","changelog":"history.txt","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-20T09:54:08.000Z","updated_at":"2024-05-27T10:37:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"c090cbd8-6c5d-426a-b7a3-a9a70151bc2d","html_url":"https://github.com/ClausKlein/lzma-sdk","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClausKlein%2Flzma-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClausKlein%2Flzma-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClausKlein%2Flzma-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClausKlein%2Flzma-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ClausKlein","download_url":"https://codeload.github.com/ClausKlein/lzma-sdk/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247438090,"owners_count":20938862,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","clang-tidy","cmake","lzma"],"created_at":"2024-10-09T19:34:47.476Z","updated_at":"2025-04-06T05:22:46.816Z","avatar_url":"https://github.com/ClausKlein.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"----------------\r\nLZMA compression\r\n----------------\r\n\r\nVersion: 9.20\r\n~~~~~~~~~~~~~\r\n\r\nThis file describes LZMA encoding and decoding functions written in C language.\r\n\r\nLZMA is an improved version of famous LZ77 compression algorithm.\r\nIt was improved in way of maximum increasing of compression ratio,\r\nkeeping high decompression speed and low memory requirements for\r\ndecompressing.\r\n\r\nAlso you can look source code for LZMA encoding and decoding:\r\n\r\n - CMakeLists.txt\r\n - Util/Lzma/LzmaUtil.c\r\n - Util/Lzma/GNUmakefile\r\n\r\n\r\nLZMA compressed file format\r\n---------------------------\r\n\r\nOffset Size Description::\r\n\r\n  0     1   Special LZMA properties (lc,lp, pb in encoded form)\r\n  1     4   Dictionary size (little endian)\r\n  5     8   Uncompressed size (little endian). -1 means unknown size\r\n 13         Compressed data\r\n\r\n\r\nANSI-C LZMA Decoder\r\n~~~~~~~~~~~~~~~~~~~\r\n\r\nPlease note that interfaces for ANSI-C code were changed in LZMA SDK 4.58.\r\nIf you want to use old interfaces you can download previous version of LZMA SDK\r\nfrom sourceforge.net site.\r\n\r\nTo use ANSI-C LZMA Decoder you need the following files:\r\n\r\n1) LzmaDec.h + LzmaDec.c + Types.h + Precomp.h + Compiler.h\r\n\r\nLook example code:\r\n\r\n  Util/Lzma/LzmaUtil.c\r\n\r\n\r\nMemory requirements for LZMA decoding\r\n-------------------------------------\r\n\r\nStack usage of LZMA decoding function for local variables is not\r\nlarger than 200-400 bytes.\r\n\r\nLZMA Decoder uses dictionary buffer and internal state structure.\r\nInternal state structure consumes::\r\n\r\n  state_size = (4 + (1.5 \u003c\u003c (lc + lp))) KB\r\n\r\nby default (lc=3, lp=0), state_size = 16 KB.\r\n\r\n\r\nHow To decompress data\r\n----------------------\r\n\r\nLZMA Decoder (ANSI-C version) now supports 2 interfaces:\r\n1) Single-call Decompressing\r\n2) Multi-call State Decompressing (zlib-like interface)\r\n\r\nYou must use external allocator:\r\nExample::\r\n\r\n  void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); }\r\n  void SzFree(void *p, void *address) { p = p; free(address); }\r\n  ISzAlloc alloc = { SzAlloc, SzFree };\r\n\r\nYou can use p = p; operator to disable compiler warnings.\r\n\r\n\r\nSingle-call Decompressing\r\n-------------------------\r\n\r\nWhen to use: RAM-\u003eRAM decompressing\r\nCompile files: LzmaDec.h + LzmaDec.c + Types.h\r\nCompile defines: no defines\r\n\r\nMemory Requirements:\r\n  - Input buffer: compressed size\r\n  - Output buffer: uncompressed size\r\n  - LZMA Internal Structures: state_size (16 KB for default settings)\r\n\r\nInterface::\r\n\r\n  int LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,\r\n      const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,\r\n      ELzmaStatus *status, ISzAlloc *alloc);\r\n  In:\r\n    dest     - output data\r\n    destLen  - output data size\r\n    src      - input data\r\n    srcLen   - input data size\r\n    propData - LZMA properties  (5 bytes)\r\n    propSize - size of propData buffer (5 bytes)\r\n    finishMode - It has meaning only if the decoding reaches output limit (*destLen).\r\n         LZMA_FINISH_ANY - Decode just destLen bytes.\r\n         LZMA_FINISH_END - Stream must be finished after (*destLen).\r\n                           You can use LZMA_FINISH_END, when you know that\r\n                           current output buffer covers last bytes of stream.\r\n    alloc    - Memory allocator.\r\n\r\n  Out:\r\n    destLen  - processed output size\r\n    srcLen   - processed input size\r\n\r\n  Output:\r\n    SZ_OK\r\n      status:\r\n        LZMA_STATUS_FINISHED_WITH_MARK\r\n        LZMA_STATUS_NOT_FINISHED\r\n        LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK\r\n    SZ_ERROR_DATA - Data error\r\n    SZ_ERROR_MEM  - Memory allocation error\r\n    SZ_ERROR_UNSUPPORTED - Unsupported properties\r\n    SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).\r\n\r\n  If LZMA decoder sees end_marker before reaching output limit, it returns OK result,\r\n  and output value of destLen will be less than output buffer size limit.\r\n\r\n  You can use multiple checks to test data integrity after full decompression:\r\n    1) Check Result and \"status\" variable.\r\n    2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.\r\n    3) Check that output(srcLen) = compressedSize, if you know real compressedSize.\r\n       You must use correct finish mode in that case. */\r\n\r\n\r\nMulti-call State Decompressing (zlib-like interface)\r\n----------------------------------------------------\r\n\r\nWhen to use: file-\u003efile decompressing\r\nCompile files: LzmaDec.h + LzmaDec.c + Types.h\r\n\r\nMemory Requirements:\r\n - Buffer for input stream: any size (for example, 16 KB)\r\n - Buffer for output stream: any size (for example, 16 KB)\r\n - LZMA Internal Structures: state_size (16 KB for default settings)\r\n - LZMA dictionary (dictionary size is encoded in LZMA properties header)\r\n\r\n1) read LZMA properties (5 bytes) and uncompressed size (8 bytes, little-endian) to header::\r\n\r\n    unsigned char header[LZMA_PROPS_SIZE + 8];\r\n    ReadFile(inFile, header, sizeof(header)\r\n\r\n2) Allocate CLzmaDec structures (state + dictionary) using LZMA properties::\r\n\r\n    CLzmaDec state;\r\n    LzmaDec_Constr(\u0026state);\r\n    res = LzmaDec_Allocate(\u0026state, header, LZMA_PROPS_SIZE, \u0026g_Alloc);\r\n    if (res != SZ_OK)\r\n      return res;\r\n\r\n3) Init LzmaDec structure before any new LZMA stream. And call LzmaDec_DecodeToBuf in loop::\r\n\r\n    LzmaDec_Init(\u0026state);\r\n    for (;;)\r\n    {\r\n      ...\r\n      int res = LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,\r\n          const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode);\r\n      ...\r\n    }\r\n\r\n\r\n4) Free all allocated structures::\r\n\r\n    LzmaDec_Free(\u0026state, \u0026g_Alloc);\r\n\r\nLook example code:\r\n\r\n  Util/Lzma/LzmaUtil.c\r\n\r\n\r\nHow To compress data\r\n--------------------\r\n\r\nCompile files:\r\n - Types.h\r\n - LzmaEnc.h\r\n - LzmaEnc.c\r\n - LzFind.h\r\n - LzFind.c\r\n - LzHash.h\r\n\r\nMemory Requirements:\r\n\r\n  - (dictSize * 11.5 + 6 MB) + state_size\r\n\r\nLzma Encoder can use two memory allocators:\r\n\r\n1) alloc - for small arrays.\r\n2) allocBig - for big arrays.\r\n\r\nFor example, you can use Large RAM Pages (2 MB) in allocBig allocator for\r\nbetter compression speed. Note that Windows has bad implementation for\r\nLarge RAM Pages.\r\n\r\nIt's OK to use same allocator for alloc and allocBig.\r\n\r\n\r\nSingle-call Compression with callbacks\r\n--------------------------------------\r\n\r\nLook example code:\r\n\r\n Util/Lzma/LzmaUtil.c\r\n\r\nWhen to use: file-\u003efile compressing\r\n\r\n1) you must implement callback structures for interfaces::\r\n\r\n    ISeqInStream\r\n    ISeqOutStream\r\n    ICompressProgress\r\n    ISzAlloc\r\n\r\n    static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); }\r\n    static void SzFree(void *p, void *address) {  p = p; MyFree(address); }\r\n    static ISzAlloc g_Alloc = { SzAlloc, SzFree };\r\n\r\n    CFileSeqInStream inStream;\r\n    CFileSeqOutStream outStream;\r\n\r\n    inStream.funcTable.Read = MyRead;\r\n    inStream.file = inFile;\r\n    outStream.funcTable.Write = MyWrite;\r\n    outStream.file = outFile;\r\n\r\n\r\n2) Create CLzmaEncHandle object::\r\n\r\n    CLzmaEncHandle enc;\r\n\r\n    enc = LzmaEnc_Create(\u0026g_Alloc);\r\n    if (enc == 0)\r\n      return SZ_ERROR_MEM;\r\n\r\n\r\n3) initialize CLzmaEncProps properties::\r\n\r\n    LzmaEncProps_Init(\u0026props);\r\n\r\n    Then you can change some properties in that structure.\r\n\r\n4) Send LZMA properties to LZMA Encoder::\r\n\r\n    res = LzmaEnc_SetProps(enc, \u0026props);\r\n\r\n5) Write encoded properties to header::\r\n\r\n      Byte header[LZMA_PROPS_SIZE + 8];\r\n      size_t headerSize = LZMA_PROPS_SIZE;\r\n      UInt64 fileSize;\r\n      int i;\r\n\r\n      res = LzmaEnc_WriteProperties(enc, header, \u0026headerSize);\r\n      fileSize = MyGetFileLength(inFile);\r\n      for (i = 0; i \u003c 8; i++)\r\n        header[headerSize++] = (Byte)(fileSize \u003e\u003e (8 * i));\r\n      MyWriteFileAndCheck(outFile, header, headerSize)\r\n\r\n6) Call encoding function::\r\n\r\n      res = LzmaEnc_Encode(enc, \u0026outStream.funcTable, \u0026inStream.funcTable,\r\n        NULL, \u0026g_Alloc, \u0026g_Alloc);\r\n\r\n7) Destroy LZMA Encoder Object::\r\n\r\n    LzmaEnc_Destroy(enc, \u0026g_Alloc, \u0026g_Alloc);\r\n\r\n\r\nIf callback function return some error code, LzmaEnc_Encode also returns that code\r\nor it can return the code like SZ_ERROR_READ, SZ_ERROR_WRITE or SZ_ERROR_PROGRESS.\r\n\r\n\r\nSingle-call RAM-\u003eRAM Compression\r\n--------------------------------\r\n\r\nSingle-call RAM-\u003eRAM Compression is similar to Compression with callbacks,\r\nbut you provide pointers to buffers instead of pointers to stream callbacks::\r\n\r\n  SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,\r\n    const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,\r\n    ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);\r\n\r\nReturn code::\r\n\r\n  SZ_OK               - OK\r\n  SZ_ERROR_MEM        - Memory allocation error\r\n  SZ_ERROR_PARAM      - Incorrect paramater\r\n  SZ_ERROR_OUTPUT_EOF - output buffer overflow\r\n  SZ_ERROR_THREAD     - errors in multithreading functions (only for Mt version)\r\n\r\n\r\n\r\nDefines\r\n-------\r\n\r\n_LZMA_SIZE_OPT - Enable some optimizations in LZMA Decoder to get smaller executable code.\r\n\r\n_LZMA_PROB32   - It can increase the speed on some 32-bit CPUs, but memory usage for\r\n                 some structures will be doubled in that case.\r\n\r\n_LZMA_UINT32_IS_ULONG  - Define it if int is 16-bit on your compiler and long is 32-bit.\r\n\r\n_LZMA_NO_SYSTEM_SIZE_T  - Define it if you don't want to use size_t type.\r\n\r\n\r\n_7ZIP_PPMD_SUPPPORT - Define it if you don't want to support PPMD method in AMSI-C .7z decoder.\r\n\r\n\r\nC++ LZMA Encoder/Decoder\r\n~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nC++ LZMA code use COM-like interfaces. So if you want to use it,\r\nyou can study basics of COM/OLE.\r\nC++ LZMA code is just wrapper over ANSI-C code.\r\n\r\n\r\nC++ Notes\r\n~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nIf you use some C++ code folders in 7-Zip (for example, C++ code for .7z handling),\r\nyou must check that you correctly work with \"new\" operator.\r\n7-Zip can be compiled with MSVC 6.0 that doesn't throw \"exception\" from \"new\" operator.\r\nSo 7-Zip uses \"CPP\\Common\\NewHandler.cpp\" that redefines \"new\" operator::\r\n\r\n    operator new(size_t size)\r\n    {\r\n      void *p = ::malloc(size);\r\n      if (p == 0)\r\n        throw CNewException();\r\n      return p;\r\n    }\r\n\r\nIf you use MSCV that throws exception for \"new\" operator, you can compile without\r\n\"NewHandler.cpp\". So standard exception will be used. Actually some code of\r\n7-Zip catches any exception in internal code and converts it to HRESULT code.\r\nSo you don't need to catch CNewException, if you call COM interfaces of 7-Zip.\r\n\r\n---\r\n\r\n - http://www.7-zip.org\r\n - http://www.7-zip.org/sdk.html\r\n - http://www.7-zip.org/support.html\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclausklein%2Flzma-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclausklein%2Flzma-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclausklein%2Flzma-sdk/lists"}