1 /* ZstdDec.h -- Zstd Decoder interfaces 2 2024-01-21 : Igor Pavlov : Public domain */ 3 4 #ifndef ZIP7_INC_ZSTD_DEC_H 5 #define ZIP7_INC_ZSTD_DEC_H 6 7 EXTERN_C_BEGIN 8 9 typedef struct CZstdDec CZstdDec; 10 typedef CZstdDec * CZstdDecHandle; 11 12 CZstdDecHandle ZstdDec_Create(ISzAllocPtr alloc_Small, ISzAllocPtr alloc_Big); 13 void ZstdDec_Destroy(CZstdDecHandle p); 14 15 typedef enum 16 { 17 ZSTD_STATUS_NOT_SPECIFIED, /* use main error code instead */ 18 ZSTD_STATUS_FINISHED_FRAME, /* data frame or skip frame was finished */ 19 ZSTD_STATUS_NOT_FINISHED, /* just finished non-empty block or unfinished RAW/RLE block */ 20 ZSTD_STATUS_NEEDS_MORE_INPUT, /* the callee needs more input bytes. It has more priority over ZSTD_STATUS_NOT_FINISHED */ 21 ZSTD_STATUS_OUT_REACHED /* is not finihed frame and ((outProcessed > outSize) || (outProcessed == outSize && unfinished RAW/RLE block) */ 22 } enum_ZstdStatus_Dummy; 23 24 #define ZstdDecState_DOES_NEED_MORE_INPUT_OR_FINISHED_FRAME(p) \ 25 ((p)->status & ZSTD_STATUS_FINISHED_FRAME) 26 /* 27 ((p)->status == ZSTD_STATUS_NEEDS_MORE_INPUT || \ 28 (p)->status == ZSTD_STATUS_FINISHED_FRAME) 29 */ 30 31 typedef Byte enum_ZstdStatus; 32 33 34 void ZstdDec_Init(CZstdDecHandle p); 35 36 typedef struct 37 { 38 UInt64 num_Blocks; 39 Byte descriptor_OR; 40 Byte descriptor_NOT_OR; 41 Byte are_ContentSize_Unknown; 42 Byte windowDescriptor_MAX; 43 44 // Byte are_ContentSize_Known; 45 // Byte are_SingleSegments; 46 // Byte are_WindowDescriptors; 47 Byte checksum_Defined; 48 // Byte are_Checksums; 49 // Byte are_Non_Checksums; 50 51 // Byte are_DictionaryId; 52 Byte are_DictionaryId_Different; 53 54 // Byte reserved[3]; 55 56 UInt32 checksum; // checksum of last data frame 57 /// UInt32 dictionaryId_Cur; 58 UInt32 dictionaryId; // if there are non-zero dictionary IDs, then it's first dictionaryId 59 60 UInt64 num_DataFrames; 61 UInt64 num_SkipFrames; 62 UInt64 skipFrames_Size; 63 UInt64 contentSize_Total; 64 UInt64 contentSize_MAX; 65 // UInt64 num_Checksums; 66 // UInt64 num_Non_Checksums; // frames without checksum 67 // UInt64 num_WindowDescriptors; 68 // UInt64 num_SingleSegments; 69 // UInt64 num_Frames_with_ContentSize; 70 // UInt64 num_Frames_without_ContentSize; 71 UInt64 windowSize_MAX; 72 UInt64 windowSize_Allocate_MAX; 73 // UInt64 num_DictionaryIds; 74 // UInt64 num_Blocks_forType[4]; 75 // UInt64 num_BlockBytes_forType[4]; 76 // UInt64 num_SingleSegments; 77 // UInt64 singleSegment_ContentSize_MAX; 78 } CZstdDecInfo; 79 80 #define ZstdDecInfo_CLEAR(p) { memset(p, 0, sizeof(*(p))); } 81 82 #define ZstdDecInfo_GET_NUM_FRAMES(p) ((p)->num_DataFrames + (p)->num_SkipFrames) 83 84 85 typedef struct CZstdDecState 86 { 87 enum_ZstdStatus status; // out 88 Byte disableHash; 89 // Byte mustBeFinished; 90 Byte outSize_Defined; 91 // Byte isAfterSizeMode; 92 // UInt64 inProcessed; 93 // SRes codeRes; 94 // Byte needWrite_IsStrong; 95 96 const Byte *inBuf; 97 size_t inPos; // in/out 98 size_t inLim; 99 100 const Byte *win; // out 101 size_t winPos; // out 102 size_t wrPos; // in/out 103 // size_t cycSize; // out : if (!outBuf_fromCaller) 104 size_t needWrite_Size; // out 105 106 Byte *outBuf_fromCaller; 107 size_t outBufSize_fromCaller; 108 /* (outBufSize_fromCaller >= full_uncompressed_size_of_all_frames) is required 109 for success decoding. 110 If outBufSize_fromCaller < full_uncompressed_size_of_all_frames), 111 decoding can give error message, because we decode per block basis. 112 */ 113 114 // size_t outStep; 115 UInt64 outSize; // total in all frames 116 UInt64 outProcessed; // out decoded in all frames (it can be >= outSize) 117 118 CZstdDecInfo info; 119 } CZstdDecState; 120 121 void ZstdDecState_Clear(CZstdDecState *p); 122 123 /* 124 ZstdDec_Decode() 125 return: 126 SZ_OK - no error 127 SZ_ERROR_DATA - Data Error 128 SZ_ERROR_MEM - Memory allocation error 129 SZ_ERROR_UNSUPPORTED - Unsupported method or method properties 130 SZ_ERROR_CRC - XXH hash Error 131 // SZ_ERROR_ARCHIVE - Headers error (not used now) 132 */ 133 SRes ZstdDec_Decode(CZstdDecHandle dec, CZstdDecState *p); 134 135 /* 136 ZstdDec_ReadUnusedFromInBuf(): 137 returns: the number of bytes that were read from InBuf 138 (*afterDecoding_tempPos) must be set to zero before first call of ZstdDec_ReadUnusedFromInBuf() 139 */ 140 size_t ZstdDec_ReadUnusedFromInBuf( 141 CZstdDecHandle dec, 142 size_t afterDecoding_tempPos, // in/out 143 void *data, size_t size); 144 145 typedef struct 146 { 147 SRes decode_SRes; // error code of data decoding 148 Byte is_NonFinishedFrame; // there is unfinished decoding for data frame or skip frame 149 Byte extraSize; 150 } CZstdDecResInfo; 151 152 /* 153 #define ZstdDecResInfo_CLEAR(p) \ 154 { (p)->decode_SRes = 0; \ 155 (p)->is_NonFinishedFrame; \ 156 (p)->extraSize = 0; \ 157 } 158 // memset(p, 0, sizeof(*p)); 159 */ 160 161 /* 162 additional error codes for CZstdDecResInfo::decode_SRes: 163 SZ_ERROR_NO_ARCHIVE - is not zstd stream (no frames) 164 SZ_ERROR_INPUT_EOF - need more data in input stream 165 */ 166 void ZstdDec_GetResInfo(const CZstdDec *dec, 167 const CZstdDecState *p, 168 SRes res, // it's result from ZstdDec_Decode() 169 CZstdDecResInfo *info); 170 171 EXTERN_C_END 172 173 #endif 174