/* Xz.h - Xz interface 2021-04-01 : Igor Pavlov : Public domain */ #ifndef __XZ_H #define __XZ_H #include "Sha256.h" EXTERN_C_BEGIN #define XZ_ID_Subblock … #define XZ_ID_Delta … #define XZ_ID_X86 … #define XZ_ID_PPC … #define XZ_ID_IA64 … #define XZ_ID_ARM … #define XZ_ID_ARMT … #define XZ_ID_SPARC … #define XZ_ID_LZMA2 … unsigned Xz_ReadVarInt(const Byte *p, size_t maxSize, UInt64 *value); unsigned Xz_WriteVarInt(Byte *buf, UInt64 v); /* ---------- xz block ---------- */ #define XZ_BLOCK_HEADER_SIZE_MAX … #define XZ_NUM_FILTERS_MAX … #define XZ_BF_NUM_FILTERS_MASK … #define XZ_BF_PACK_SIZE … #define XZ_BF_UNPACK_SIZE … #define XZ_FILTER_PROPS_SIZE_MAX … CXzFilter; CXzBlock; #define XzBlock_GetNumFilters(p) … #define XzBlock_HasPackSize(p) … #define XzBlock_HasUnpackSize(p) … #define XzBlock_HasUnsupportedFlags(p) … SRes XzBlock_Parse(CXzBlock *p, const Byte *header); SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, BoolInt *isIndex, UInt32 *headerSizeRes); /* ---------- xz stream ---------- */ #define XZ_SIG_SIZE … #define XZ_FOOTER_SIG_SIZE … extern const Byte XZ_SIG[XZ_SIG_SIZE]; /* extern const Byte XZ_FOOTER_SIG[XZ_FOOTER_SIG_SIZE]; */ #define XZ_FOOTER_SIG_0 … #define XZ_FOOTER_SIG_1 … #define XZ_STREAM_FLAGS_SIZE … #define XZ_STREAM_CRC_SIZE … #define XZ_STREAM_HEADER_SIZE … #define XZ_STREAM_FOOTER_SIZE … #define XZ_CHECK_MASK … #define XZ_CHECK_NO … #define XZ_CHECK_CRC32 … #define XZ_CHECK_CRC64 … #define XZ_CHECK_SHA256 … CXzCheck; void XzCheck_Init(CXzCheck *p, unsigned mode); void XzCheck_Update(CXzCheck *p, const void *data, size_t size); int XzCheck_Final(CXzCheck *p, Byte *digest); CXzStreamFlags; #define XzFlags_IsSupported(f) … #define XzFlags_GetCheckType(f) … #define XzFlags_HasDataCrc32(f) … unsigned XzFlags_GetCheckSize(CXzStreamFlags f); SRes Xz_ParseHeader(CXzStreamFlags *p, const Byte *buf); SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream); CXzBlockSizes; CXzStream; void Xz_Construct(CXzStream *p); void Xz_Free(CXzStream *p, ISzAllocPtr alloc); #define XZ_SIZE_OVERFLOW … UInt64 Xz_GetUnpackSize(const CXzStream *p); UInt64 Xz_GetPackSize(const CXzStream *p); CXzs; void Xzs_Construct(CXzs *p); void Xzs_Free(CXzs *p, ISzAllocPtr alloc); SRes Xzs_ReadBackward(CXzs *p, ILookInStream *inStream, Int64 *startOffset, ICompressProgress *progress, ISzAllocPtr alloc); UInt64 Xzs_GetNumBlocks(const CXzs *p); UInt64 Xzs_GetUnpackSize(const CXzs *p); // ECoderStatus values are identical to ELzmaStatus values of LZMA2 decoder ECoderStatus; // ECoderFinishMode values are identical to ELzmaFinishMode ECoderFinishMode; IStateCoder; #define MIXCODER_NUM_FILTERS_MAX … CMixCoder; EXzState; CXzUnpacker; /* alloc : aligned for cache line allocation is better */ void XzUnpacker_Construct(CXzUnpacker *p, ISzAllocPtr alloc); void XzUnpacker_Init(CXzUnpacker *p); void XzUnpacker_SetOutBuf(CXzUnpacker *p, Byte *outBuf, size_t outBufSize); void XzUnpacker_Free(CXzUnpacker *p); /* XzUnpacker The sequence for decoding functions: { XzUnpacker_Construct() [Decoding_Calls] XzUnpacker_Free() } [Decoding_Calls] There are 3 types of interfaces for [Decoding_Calls] calls: Interface-1 : Partial output buffers: { XzUnpacker_Init() for() { XzUnpacker_Code(); } XzUnpacker_IsStreamWasFinished() } Interface-2 : Direct output buffer: Use it, if you know exact size of decoded data, and you need whole xz unpacked data in one output buffer. xz unpacker doesn't allocate additional buffer for lzma2 dictionary in that mode. { XzUnpacker_Init() XzUnpacker_SetOutBufMode(); // to set output buffer and size for() { XzUnpacker_Code(); // (dest = NULL) in XzUnpacker_Code() } XzUnpacker_IsStreamWasFinished() } Interface-3 : Direct output buffer : One call full decoding It unpacks whole input buffer to output buffer in one call. It uses Interface-2 internally. { XzUnpacker_CodeFull() XzUnpacker_IsStreamWasFinished() } */ /* finishMode: It has meaning only if the decoding reaches output limit (*destLen). CODER_FINISH_ANY - use smallest number of input bytes CODER_FINISH_END - read EndOfStream marker after decoding Returns: SZ_OK status: CODER_STATUS_NOT_FINISHED, CODER_STATUS_NEEDS_MORE_INPUT - the decoder can return it in two cases: 1) it needs more input data to finish current xz stream 2) xz stream was finished successfully. But the decoder supports multiple concatented xz streams. So it expects more input data for new xz streams. Call XzUnpacker_IsStreamWasFinished() to check that latest xz stream was finished successfully. SZ_ERROR_MEM - Memory allocation error SZ_ERROR_DATA - Data error SZ_ERROR_UNSUPPORTED - Unsupported method or method properties SZ_ERROR_CRC - CRC error // SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). SZ_ERROR_NO_ARCHIVE - the error with xz Stream Header with one of the following reasons: - xz Stream Signature failure - CRC32 of xz Stream Header is failed - The size of Stream padding is not multiple of four bytes. It's possible to get that error, if xz stream was finished and the stream contains some another data. In that case you can call XzUnpacker_GetExtraSize() function to get real size of xz stream. */ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, int srcFinished, ECoderFinishMode finishMode, ECoderStatus *status); SRes XzUnpacker_CodeFull(CXzUnpacker *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ECoderFinishMode finishMode, ECoderStatus *status); /* If you decode full xz stream(s), then you can call XzUnpacker_IsStreamWasFinished() after successful XzUnpacker_CodeFull() or after last call of XzUnpacker_Code(). */ BoolInt XzUnpacker_IsStreamWasFinished(const CXzUnpacker *p); /* XzUnpacker_GetExtraSize() returns then number of unconfirmed bytes, if it's in (XZ_STATE_STREAM_HEADER) state or in (XZ_STATE_STREAM_PADDING) state. These bytes can be some data after xz archive, or it can be start of new xz stream. Call XzUnpacker_GetExtraSize() after XzUnpacker_Code() function to detect real size of xz stream in two cases, if XzUnpacker_Code() returns: res == SZ_OK && status == CODER_STATUS_NEEDS_MORE_INPUT res == SZ_ERROR_NO_ARCHIVE */ UInt64 XzUnpacker_GetExtraSize(const CXzUnpacker *p); /* for random block decoding: XzUnpacker_Init(); set CXzUnpacker::streamFlags XzUnpacker_PrepareToRandomBlockDecoding() loop { XzUnpacker_Code() XzUnpacker_IsBlockFinished() } */ void XzUnpacker_PrepareToRandomBlockDecoding(CXzUnpacker *p); BoolInt XzUnpacker_IsBlockFinished(const CXzUnpacker *p); #define XzUnpacker_GetPackSizeForIndex(p) … /* ---- Single-Thread and Multi-Thread xz Decoding with Input/Output Streams ---- */ /* if (CXzDecMtProps::numThreads > 1), the decoder can try to use Multi-Threading. The decoder analyses xz block header, and if there are pack size and unpack size values stored in xz block header, the decoder reads compressed data of block to internal buffers, and then it can start parallel decoding, if there are another blocks. The decoder can switch back to Single-Thread decoding after some conditions. The sequence of calls for xz decoding with in/out Streams: { XzDecMt_Create() XzDecMtProps_Init(XzDecMtProps) to set default values of properties // then you can change some XzDecMtProps parameters with required values // here you can set the number of threads and (memUseMax) - the maximum Memory usage for multithreading decoding. for() { XzDecMt_Decode() // one call per one file } XzDecMt_Destroy() } */ CXzDecMtProps; void XzDecMtProps_Init(CXzDecMtProps *p); CXzDecMtHandle; /* alloc : XzDecMt uses CAlignOffsetAlloc internally for addresses allocated by (alloc). allocMid : for big allocations, aligned allocation is better */ CXzDecMtHandle XzDecMt_Create(ISzAllocPtr alloc, ISzAllocPtr allocMid); void XzDecMt_Destroy(CXzDecMtHandle p); CXzStatInfo; void XzStatInfo_Clear(CXzStatInfo *p); /* XzDecMt_Decode() SRes: it's combined decoding result. It also is equal to stat->CombinedRes. SZ_OK - no error check also output value in (stat->DataAfterEnd) that can show additional possible error SZ_ERROR_MEM - Memory allocation error SZ_ERROR_NO_ARCHIVE - is not xz archive SZ_ERROR_ARCHIVE - Headers error SZ_ERROR_DATA - Data Error SZ_ERROR_UNSUPPORTED - Unsupported method or method properties SZ_ERROR_CRC - CRC Error SZ_ERROR_INPUT_EOF - it needs more input data SZ_ERROR_WRITE - ISeqOutStream error (SZ_ERROR_READ) - ISeqInStream errors (SZ_ERROR_PROGRESS) - ICompressProgress errors // SZ_ERROR_THREAD - error in multi-threading functions MY_SRes_HRESULT_FROM_WRes(WRes_error) - error in multi-threading function */ SRes XzDecMt_Decode(CXzDecMtHandle p, const CXzDecMtProps *props, const UInt64 *outDataSize, // NULL means undefined int finishMode, // 0 - partial unpacking is allowed, 1 - xz stream(s) must be finished ISeqOutStream *outStream, // Byte *outBuf, size_t *outBufSize, ISeqInStream *inStream, // const Byte *inData, size_t inDataSize, CXzStatInfo *stat, // out: decoding results and statistics int *isMT, // out: 0 means that ST (Single-Thread) version was used // 1 means that MT (Multi-Thread) version was used ICompressProgress *progress); EXTERN_C_END #endif