1370b324cSopenharmony_ci/* 7z.h -- 7z interface 2370b324cSopenharmony_ci2023-04-02 : Igor Pavlov : Public domain */ 3370b324cSopenharmony_ci 4370b324cSopenharmony_ci#ifndef ZIP7_INC_7Z_H 5370b324cSopenharmony_ci#define ZIP7_INC_7Z_H 6370b324cSopenharmony_ci 7370b324cSopenharmony_ci#include "7zTypes.h" 8370b324cSopenharmony_ci 9370b324cSopenharmony_ciEXTERN_C_BEGIN 10370b324cSopenharmony_ci 11370b324cSopenharmony_ci#define k7zStartHeaderSize 0x20 12370b324cSopenharmony_ci#define k7zSignatureSize 6 13370b324cSopenharmony_ci 14370b324cSopenharmony_ciextern const Byte k7zSignature[k7zSignatureSize]; 15370b324cSopenharmony_ci 16370b324cSopenharmony_citypedef struct 17370b324cSopenharmony_ci{ 18370b324cSopenharmony_ci const Byte *Data; 19370b324cSopenharmony_ci size_t Size; 20370b324cSopenharmony_ci} CSzData; 21370b324cSopenharmony_ci 22370b324cSopenharmony_ci/* CSzCoderInfo & CSzFolder support only default methods */ 23370b324cSopenharmony_ci 24370b324cSopenharmony_citypedef struct 25370b324cSopenharmony_ci{ 26370b324cSopenharmony_ci size_t PropsOffset; 27370b324cSopenharmony_ci UInt32 MethodID; 28370b324cSopenharmony_ci Byte NumStreams; 29370b324cSopenharmony_ci Byte PropsSize; 30370b324cSopenharmony_ci} CSzCoderInfo; 31370b324cSopenharmony_ci 32370b324cSopenharmony_citypedef struct 33370b324cSopenharmony_ci{ 34370b324cSopenharmony_ci UInt32 InIndex; 35370b324cSopenharmony_ci UInt32 OutIndex; 36370b324cSopenharmony_ci} CSzBond; 37370b324cSopenharmony_ci 38370b324cSopenharmony_ci#define SZ_NUM_CODERS_IN_FOLDER_MAX 4 39370b324cSopenharmony_ci#define SZ_NUM_BONDS_IN_FOLDER_MAX 3 40370b324cSopenharmony_ci#define SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX 4 41370b324cSopenharmony_ci 42370b324cSopenharmony_citypedef struct 43370b324cSopenharmony_ci{ 44370b324cSopenharmony_ci UInt32 NumCoders; 45370b324cSopenharmony_ci UInt32 NumBonds; 46370b324cSopenharmony_ci UInt32 NumPackStreams; 47370b324cSopenharmony_ci UInt32 UnpackStream; 48370b324cSopenharmony_ci UInt32 PackStreams[SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX]; 49370b324cSopenharmony_ci CSzBond Bonds[SZ_NUM_BONDS_IN_FOLDER_MAX]; 50370b324cSopenharmony_ci CSzCoderInfo Coders[SZ_NUM_CODERS_IN_FOLDER_MAX]; 51370b324cSopenharmony_ci} CSzFolder; 52370b324cSopenharmony_ci 53370b324cSopenharmony_ci 54370b324cSopenharmony_ciSRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd); 55370b324cSopenharmony_ci 56370b324cSopenharmony_citypedef struct 57370b324cSopenharmony_ci{ 58370b324cSopenharmony_ci UInt32 Low; 59370b324cSopenharmony_ci UInt32 High; 60370b324cSopenharmony_ci} CNtfsFileTime; 61370b324cSopenharmony_ci 62370b324cSopenharmony_citypedef struct 63370b324cSopenharmony_ci{ 64370b324cSopenharmony_ci Byte *Defs; /* MSB 0 bit numbering */ 65370b324cSopenharmony_ci UInt32 *Vals; 66370b324cSopenharmony_ci} CSzBitUi32s; 67370b324cSopenharmony_ci 68370b324cSopenharmony_citypedef struct 69370b324cSopenharmony_ci{ 70370b324cSopenharmony_ci Byte *Defs; /* MSB 0 bit numbering */ 71370b324cSopenharmony_ci // UInt64 *Vals; 72370b324cSopenharmony_ci CNtfsFileTime *Vals; 73370b324cSopenharmony_ci} CSzBitUi64s; 74370b324cSopenharmony_ci 75370b324cSopenharmony_ci#define SzBitArray_Check(p, i) (((p)[(i) >> 3] & (0x80 >> ((i) & 7))) != 0) 76370b324cSopenharmony_ci 77370b324cSopenharmony_ci#define SzBitWithVals_Check(p, i) ((p)->Defs && ((p)->Defs[(i) >> 3] & (0x80 >> ((i) & 7))) != 0) 78370b324cSopenharmony_ci 79370b324cSopenharmony_citypedef struct 80370b324cSopenharmony_ci{ 81370b324cSopenharmony_ci UInt32 NumPackStreams; 82370b324cSopenharmony_ci UInt32 NumFolders; 83370b324cSopenharmony_ci 84370b324cSopenharmony_ci UInt64 *PackPositions; // NumPackStreams + 1 85370b324cSopenharmony_ci CSzBitUi32s FolderCRCs; // NumFolders 86370b324cSopenharmony_ci 87370b324cSopenharmony_ci size_t *FoCodersOffsets; // NumFolders + 1 88370b324cSopenharmony_ci UInt32 *FoStartPackStreamIndex; // NumFolders + 1 89370b324cSopenharmony_ci UInt32 *FoToCoderUnpackSizes; // NumFolders + 1 90370b324cSopenharmony_ci Byte *FoToMainUnpackSizeIndex; // NumFolders 91370b324cSopenharmony_ci UInt64 *CoderUnpackSizes; // for all coders in all folders 92370b324cSopenharmony_ci 93370b324cSopenharmony_ci Byte *CodersData; 94370b324cSopenharmony_ci 95370b324cSopenharmony_ci UInt64 RangeLimit; 96370b324cSopenharmony_ci} CSzAr; 97370b324cSopenharmony_ci 98370b324cSopenharmony_ciUInt64 SzAr_GetFolderUnpackSize(const CSzAr *p, UInt32 folderIndex); 99370b324cSopenharmony_ci 100370b324cSopenharmony_ciSRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex, 101370b324cSopenharmony_ci ILookInStreamPtr stream, UInt64 startPos, 102370b324cSopenharmony_ci Byte *outBuffer, size_t outSize, 103370b324cSopenharmony_ci ISzAllocPtr allocMain); 104370b324cSopenharmony_ci 105370b324cSopenharmony_citypedef struct 106370b324cSopenharmony_ci{ 107370b324cSopenharmony_ci CSzAr db; 108370b324cSopenharmony_ci 109370b324cSopenharmony_ci UInt64 startPosAfterHeader; 110370b324cSopenharmony_ci UInt64 dataPos; 111370b324cSopenharmony_ci 112370b324cSopenharmony_ci UInt32 NumFiles; 113370b324cSopenharmony_ci 114370b324cSopenharmony_ci UInt64 *UnpackPositions; // NumFiles + 1 115370b324cSopenharmony_ci // Byte *IsEmptyFiles; 116370b324cSopenharmony_ci Byte *IsDirs; 117370b324cSopenharmony_ci CSzBitUi32s CRCs; 118370b324cSopenharmony_ci 119370b324cSopenharmony_ci CSzBitUi32s Attribs; 120370b324cSopenharmony_ci // CSzBitUi32s Parents; 121370b324cSopenharmony_ci CSzBitUi64s MTime; 122370b324cSopenharmony_ci CSzBitUi64s CTime; 123370b324cSopenharmony_ci 124370b324cSopenharmony_ci UInt32 *FolderToFile; // NumFolders + 1 125370b324cSopenharmony_ci UInt32 *FileToFolder; // NumFiles 126370b324cSopenharmony_ci 127370b324cSopenharmony_ci size_t *FileNameOffsets; /* in 2-byte steps */ 128370b324cSopenharmony_ci Byte *FileNames; /* UTF-16-LE */ 129370b324cSopenharmony_ci} CSzArEx; 130370b324cSopenharmony_ci 131370b324cSopenharmony_ci#define SzArEx_IsDir(p, i) (SzBitArray_Check((p)->IsDirs, i)) 132370b324cSopenharmony_ci 133370b324cSopenharmony_ci#define SzArEx_GetFileSize(p, i) ((p)->UnpackPositions[(i) + 1] - (p)->UnpackPositions[i]) 134370b324cSopenharmony_ci 135370b324cSopenharmony_civoid SzArEx_Init(CSzArEx *p); 136370b324cSopenharmony_civoid SzArEx_Free(CSzArEx *p, ISzAllocPtr alloc); 137370b324cSopenharmony_ciUInt64 SzArEx_GetFolderStreamPos(const CSzArEx *p, UInt32 folderIndex, UInt32 indexInFolder); 138370b324cSopenharmony_ciint SzArEx_GetFolderFullPackSize(const CSzArEx *p, UInt32 folderIndex, UInt64 *resSize); 139370b324cSopenharmony_ci 140370b324cSopenharmony_ci/* 141370b324cSopenharmony_ciif dest == NULL, the return value specifies the required size of the buffer, 142370b324cSopenharmony_ci in 16-bit characters, including the null-terminating character. 143370b324cSopenharmony_ciif dest != NULL, the return value specifies the number of 16-bit characters that 144370b324cSopenharmony_ci are written to the dest, including the null-terminating character. */ 145370b324cSopenharmony_ci 146370b324cSopenharmony_cisize_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest); 147370b324cSopenharmony_ci 148370b324cSopenharmony_ci/* 149370b324cSopenharmony_cisize_t SzArEx_GetFullNameLen(const CSzArEx *p, size_t fileIndex); 150370b324cSopenharmony_ciUInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16 *dest); 151370b324cSopenharmony_ci*/ 152370b324cSopenharmony_ci 153370b324cSopenharmony_ci 154370b324cSopenharmony_ci 155370b324cSopenharmony_ci/* 156370b324cSopenharmony_ci SzArEx_Extract extracts file from archive 157370b324cSopenharmony_ci 158370b324cSopenharmony_ci *outBuffer must be 0 before first call for each new archive. 159370b324cSopenharmony_ci 160370b324cSopenharmony_ci Extracting cache: 161370b324cSopenharmony_ci If you need to decompress more than one file, you can send 162370b324cSopenharmony_ci these values from previous call: 163370b324cSopenharmony_ci *blockIndex, 164370b324cSopenharmony_ci *outBuffer, 165370b324cSopenharmony_ci *outBufferSize 166370b324cSopenharmony_ci You can consider "*outBuffer" as cache of solid block. If your archive is solid, 167370b324cSopenharmony_ci it will increase decompression speed. 168370b324cSopenharmony_ci 169370b324cSopenharmony_ci If you use external function, you can declare these 3 cache variables 170370b324cSopenharmony_ci (blockIndex, outBuffer, outBufferSize) as static in that external function. 171370b324cSopenharmony_ci 172370b324cSopenharmony_ci Free *outBuffer and set *outBuffer to 0, if you want to flush cache. 173370b324cSopenharmony_ci*/ 174370b324cSopenharmony_ci 175370b324cSopenharmony_ciSRes SzArEx_Extract( 176370b324cSopenharmony_ci const CSzArEx *db, 177370b324cSopenharmony_ci ILookInStreamPtr inStream, 178370b324cSopenharmony_ci UInt32 fileIndex, /* index of file */ 179370b324cSopenharmony_ci UInt32 *blockIndex, /* index of solid block */ 180370b324cSopenharmony_ci Byte **outBuffer, /* pointer to pointer to output buffer (allocated with allocMain) */ 181370b324cSopenharmony_ci size_t *outBufferSize, /* buffer size for output buffer */ 182370b324cSopenharmony_ci size_t *offset, /* offset of stream for required file in *outBuffer */ 183370b324cSopenharmony_ci size_t *outSizeProcessed, /* size of file in *outBuffer */ 184370b324cSopenharmony_ci ISzAllocPtr allocMain, 185370b324cSopenharmony_ci ISzAllocPtr allocTemp); 186370b324cSopenharmony_ci 187370b324cSopenharmony_ci 188370b324cSopenharmony_ci/* 189370b324cSopenharmony_ciSzArEx_Open Errors: 190370b324cSopenharmony_ciSZ_ERROR_NO_ARCHIVE 191370b324cSopenharmony_ciSZ_ERROR_ARCHIVE 192370b324cSopenharmony_ciSZ_ERROR_UNSUPPORTED 193370b324cSopenharmony_ciSZ_ERROR_MEM 194370b324cSopenharmony_ciSZ_ERROR_CRC 195370b324cSopenharmony_ciSZ_ERROR_INPUT_EOF 196370b324cSopenharmony_ciSZ_ERROR_FAIL 197370b324cSopenharmony_ci*/ 198370b324cSopenharmony_ci 199370b324cSopenharmony_ciSRes SzArEx_Open(CSzArEx *p, ILookInStreamPtr inStream, 200370b324cSopenharmony_ci ISzAllocPtr allocMain, ISzAllocPtr allocTemp); 201370b324cSopenharmony_ci 202370b324cSopenharmony_ciEXTERN_C_END 203370b324cSopenharmony_ci 204370b324cSopenharmony_ci#endif 205