• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 7z.h -- 7z interface
2 2013-01-18 : Igor Pavlov : Public domain */
3 
4 #ifndef __7Z_H
5 #define __7Z_H
6 
7 #include "7zTypes.h"
8 
9 EXTERN_C_BEGIN
10 
11 #define k7zStartHeaderSize 0x20
12 #define k7zSignatureSize 6
13 
14 extern Byte k7zSignature[k7zSignatureSize];
15 
16 typedef struct
17 {
18   const Byte *Data;
19   size_t Size;
20 } CSzData;
21 
22 /* CSzCoderInfo & CSzFolder support only default methods */
23 
24 typedef struct
25 {
26   size_t PropsOffset;
27   UInt32 MethodID;
28   Byte NumInStreams;
29   Byte NumOutStreams;
30   Byte PropsSize;
31 } CSzCoderInfo;
32 
33 typedef struct
34 {
35   UInt32 InIndex;
36   UInt32 OutIndex;
37 } CSzBindPair;
38 
39 #define SZ_NUM_CODERS_IN_FOLDER_MAX 4
40 #define SZ_NUM_BINDS_IN_FOLDER_MAX 3
41 #define SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX 4
42 #define SZ_NUM_CODERS_OUT_STREAMS_IN_FOLDER_MAX 4
43 
44 typedef struct
45 {
46   UInt32 NumCoders;
47   UInt32 NumBindPairs;
48   UInt32 NumPackStreams;
49   UInt32 MainOutStream;
50   UInt32 PackStreams[SZ_NUM_PACK_STREAMS_IN_FOLDER_MAX];
51   CSzBindPair BindPairs[SZ_NUM_BINDS_IN_FOLDER_MAX];
52   CSzCoderInfo Coders[SZ_NUM_CODERS_IN_FOLDER_MAX];
53   UInt64 CodersUnpackSizes[SZ_NUM_CODERS_OUT_STREAMS_IN_FOLDER_MAX];
54 } CSzFolder;
55 
56 /*
57 typedef struct
58 {
59   size_t CodersDataOffset;
60   size_t UnpackSizeDataOffset;
61   // UInt32 StartCoderUnpackSizesIndex;
62   UInt32 StartPackStreamIndex;
63   // UInt32 IndexOfMainOutStream;
64 } CSzFolder2;
65 */
66 
67 SRes SzGetNextFolderItem(CSzFolder *f, CSzData *sd, CSzData *sdSizes);
68 
69 typedef struct
70 {
71   UInt32 Low;
72   UInt32 High;
73 } CNtfsFileTime;
74 
75 typedef struct
76 {
77   Byte *Defs; /* MSB 0 bit numbering */
78   UInt32 *Vals;
79 } CSzBitUi32s;
80 
81 typedef struct
82 {
83   Byte *Defs; /* MSB 0 bit numbering */
84   // UInt64 *Vals;
85   CNtfsFileTime *Vals;
86 } CSzBitUi64s;
87 
88 #define SzBitArray_Check(p, i) (((p)[(i) >> 3] & (0x80 >> ((i) & 7))) != 0)
89 
90 #define SzBitWithVals_Check(p, i) ((p)->Defs && ((p)->Defs[(i) >> 3] & (0x80 >> ((i) & 7))) != 0)
91 
92 typedef struct
93 {
94   UInt32 NumPackStreams;
95   UInt32 NumFolders;
96 
97   UInt64 *PackPositions; // NumPackStreams + 1
98   CSzBitUi32s FolderCRCs;
99 
100   size_t *FoCodersOffsets;
101   size_t *FoSizesOffsets;
102   // UInt32 StartCoderUnpackSizesIndex;
103   UInt32 *FoStartPackStreamIndex;
104 
105   // CSzFolder2 *Folders;  // +1 item for sum values
106   Byte *CodersData;
107   Byte *UnpackSizesData;
108   size_t UnpackSizesDataSize;
109   // UInt64 *CoderUnpackSizes;
110 } CSzAr;
111 
112 
113 SRes SzAr_DecodeFolder(const CSzAr *p, UInt32 folderIndex,
114     ILookInStream *stream, UInt64 startPos,
115     Byte *outBuffer, size_t outSize,
116     ISzAlloc *allocMain);
117 
118 /*
119   SzExtract extracts file from archive
120 
121   *outBuffer must be 0 before first call for each new archive.
122 
123   Extracting cache:
124     If you need to decompress more than one file, you can send
125     these values from previous call:
126       *blockIndex,
127       *outBuffer,
128       *outBufferSize
129     You can consider "*outBuffer" as cache of solid block. If your archive is solid,
130     it will increase decompression speed.
131 
132     If you use external function, you can declare these 3 cache variables
133     (blockIndex, outBuffer, outBufferSize) as static in that external function.
134 
135     Free *outBuffer and set *outBuffer to 0, if you want to flush cache.
136 */
137 
138 typedef struct
139 {
140   CSzAr db;
141 
142   UInt64 startPosAfterHeader;
143   UInt64 dataPos;
144 
145   UInt32 NumFiles;
146 
147   UInt64 *UnpackPositions;
148   // Byte *IsEmptyFiles;
149   Byte *IsDirs;
150   CSzBitUi32s CRCs;
151 
152   CSzBitUi32s Attribs;
153   // CSzBitUi32s Parents;
154   CSzBitUi64s MTime;
155   CSzBitUi64s CTime;
156 
157   // UInt32 *FolderStartPackStreamIndex;
158   UInt32 *FolderStartFileIndex; // + 1
159   UInt32 *FileIndexToFolderIndexMap;
160 
161   size_t *FileNameOffsets; /* in 2-byte steps */
162   Byte *FileNames;  /* UTF-16-LE */
163 } CSzArEx;
164 
165 #define SzArEx_IsDir(p, i) (SzBitArray_Check((p)->IsDirs, i))
166 
167 #define SzArEx_GetFileSize(p, i) ((p)->UnpackPositions[(i) + 1] - (p)->UnpackPositions[i])
168 
169 void SzArEx_Init(CSzArEx *p);
170 void SzArEx_Free(CSzArEx *p, ISzAlloc *alloc);
171 UInt64 SzArEx_GetFolderStreamPos(const CSzArEx *p, UInt32 folderIndex, UInt32 indexInFolder);
172 int SzArEx_GetFolderFullPackSize(const CSzArEx *p, UInt32 folderIndex, UInt64 *resSize);
173 
174 /*
175 if dest == NULL, the return value specifies the required size of the buffer,
176   in 16-bit characters, including the null-terminating character.
177 if dest != NULL, the return value specifies the number of 16-bit characters that
178   are written to the dest, including the null-terminating character. */
179 
180 size_t SzArEx_GetFileNameUtf16(const CSzArEx *p, size_t fileIndex, UInt16 *dest);
181 
182 /*
183 size_t SzArEx_GetFullNameLen(const CSzArEx *p, size_t fileIndex);
184 UInt16 *SzArEx_GetFullNameUtf16_Back(const CSzArEx *p, size_t fileIndex, UInt16 *dest);
185 */
186 
187 SRes SzArEx_Extract(
188     const CSzArEx *db,
189     ILookInStream *inStream,
190     UInt32 fileIndex,         /* index of file */
191     UInt32 *blockIndex,       /* index of solid block */
192     Byte **outBuffer,         /* pointer to pointer to output buffer (allocated with allocMain) */
193     size_t *outBufferSize,    /* buffer size for output buffer */
194     size_t *offset,           /* offset of stream for required file in *outBuffer */
195     size_t *outSizeProcessed, /* size of file in *outBuffer */
196     ISzAlloc *allocMain,
197     ISzAlloc *allocTemp);
198 
199 
200 /*
201 SzArEx_Open Errors:
202 SZ_ERROR_NO_ARCHIVE
203 SZ_ERROR_ARCHIVE
204 SZ_ERROR_UNSUPPORTED
205 SZ_ERROR_MEM
206 SZ_ERROR_CRC
207 SZ_ERROR_INPUT_EOF
208 SZ_ERROR_FAIL
209 */
210 
211 SRes SzArEx_Open(CSzArEx *p, ILookInStream *inStream,
212     ISzAlloc *allocMain, ISzAlloc *allocTemp);
213 
214 EXTERN_C_END
215 
216 #endif
217