1 /* 7zTypes.h -- Basic types
2 2021-12-25 : Igor Pavlov : Public domain */
3 
4 #ifndef __7Z_TYPES_H
5 #define __7Z_TYPES_H
6 
7 #ifdef _WIN32
8 /* #include <windows.h> */
9 #else
10 #include <errno.h>
11 #endif
12 
13 #include <stddef.h>
14 
15 #ifndef EXTERN_C_BEGIN
16 #ifdef __cplusplus
17 #define EXTERN_C_BEGIN extern "C" {
18 #define EXTERN_C_END }
19 #else
20 #define EXTERN_C_BEGIN
21 #define EXTERN_C_END
22 #endif
23 #endif
24 
25 EXTERN_C_BEGIN
26 
27 #define SZ_OK 0
28 
29 #define SZ_ERROR_DATA 1
30 #define SZ_ERROR_MEM 2
31 #define SZ_ERROR_CRC 3
32 #define SZ_ERROR_UNSUPPORTED 4
33 #define SZ_ERROR_PARAM 5
34 #define SZ_ERROR_INPUT_EOF 6
35 #define SZ_ERROR_OUTPUT_EOF 7
36 #define SZ_ERROR_READ 8
37 #define SZ_ERROR_WRITE 9
38 #define SZ_ERROR_PROGRESS 10
39 #define SZ_ERROR_FAIL 11
40 #define SZ_ERROR_THREAD 12
41 
42 #define SZ_ERROR_ARCHIVE 16
43 #define SZ_ERROR_NO_ARCHIVE 17
44 
45 typedef int SRes;
46 
47 
48 #ifdef _MSC_VER
49   #if _MSC_VER > 1200
50     #define MY_ALIGN(n) __declspec(align(n))
51   #else
52     #define MY_ALIGN(n)
53   #endif
54 #else
55   #define MY_ALIGN(n) __attribute__ ((aligned(n)))
56 #endif
57 
58 
59 #ifdef _WIN32
60 
61 /* typedef DWORD WRes; */
62 typedef unsigned WRes;
63 #define MY_SRes_HRESULT_FROM_WRes(x) HRESULT_FROM_WIN32(x)
64 
65 // #define MY_HRES_ERROR__INTERNAL_ERROR  MY_SRes_HRESULT_FROM_WRes(ERROR_INTERNAL_ERROR)
66 
67 #else // _WIN32
68 
69 // #define ENV_HAVE_LSTAT
70 typedef int WRes;
71 
72 // (FACILITY_ERRNO = 0x800) is 7zip's FACILITY constant to represent (errno) errors in HRESULT
73 #define MY__FACILITY_ERRNO  0x800
74 #define MY__FACILITY_WIN32  7
75 #define MY__FACILITY__WRes  MY__FACILITY_ERRNO
76 
77 #define MY_HRESULT_FROM_errno_CONST_ERROR(x) ((HRESULT)( \
78           ( (HRESULT)(x) & 0x0000FFFF) \
79           | (MY__FACILITY__WRes << 16)  \
80           | (HRESULT)0x80000000 ))
81 
82 #define MY_SRes_HRESULT_FROM_WRes(x) \
83   ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : MY_HRESULT_FROM_errno_CONST_ERROR(x))
84 
85 // we call macro HRESULT_FROM_WIN32 for system errors (WRes) that are (errno)
86 #define HRESULT_FROM_WIN32(x) MY_SRes_HRESULT_FROM_WRes(x)
87 
88 /*
89 #define ERROR_FILE_NOT_FOUND             2L
90 #define ERROR_ACCESS_DENIED              5L
91 #define ERROR_NO_MORE_FILES              18L
92 #define ERROR_LOCK_VIOLATION             33L
93 #define ERROR_FILE_EXISTS                80L
94 #define ERROR_DISK_FULL                  112L
95 #define ERROR_NEGATIVE_SEEK              131L
96 #define ERROR_ALREADY_EXISTS             183L
97 #define ERROR_DIRECTORY                  267L
98 #define ERROR_TOO_MANY_POSTS             298L
99 
100 #define ERROR_INTERNAL_ERROR             1359L
101 #define ERROR_INVALID_REPARSE_DATA       4392L
102 #define ERROR_REPARSE_TAG_INVALID        4393L
103 #define ERROR_REPARSE_TAG_MISMATCH       4394L
104 */
105 
106 // we use errno equivalents for some WIN32 errors:
107 
108 #define ERROR_INVALID_PARAMETER     EINVAL
109 #define ERROR_INVALID_FUNCTION      EINVAL
110 #define ERROR_ALREADY_EXISTS        EEXIST
111 #define ERROR_FILE_EXISTS           EEXIST
112 #define ERROR_PATH_NOT_FOUND        ENOENT
113 #define ERROR_FILE_NOT_FOUND        ENOENT
114 #define ERROR_DISK_FULL             ENOSPC
115 // #define ERROR_INVALID_HANDLE        EBADF
116 
117 // we use FACILITY_WIN32 for errors that has no errno equivalent
118 // Too many posts were made to a semaphore.
119 #define ERROR_TOO_MANY_POSTS        ((HRESULT)0x8007012AL)
120 #define ERROR_INVALID_REPARSE_DATA  ((HRESULT)0x80071128L)
121 #define ERROR_REPARSE_TAG_INVALID   ((HRESULT)0x80071129L)
122 
123 // if (MY__FACILITY__WRes != FACILITY_WIN32),
124 // we use FACILITY_WIN32 for COM errors:
125 #define E_OUTOFMEMORY               ((HRESULT)0x8007000EL)
126 #define E_INVALIDARG                ((HRESULT)0x80070057L)
127 #define MY__E_ERROR_NEGATIVE_SEEK   ((HRESULT)0x80070083L)
128 
129 /*
130 // we can use FACILITY_ERRNO for some COM errors, that have errno equivalents:
131 #define E_OUTOFMEMORY             MY_HRESULT_FROM_errno_CONST_ERROR(ENOMEM)
132 #define E_INVALIDARG              MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL)
133 #define MY__E_ERROR_NEGATIVE_SEEK MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL)
134 */
135 
136 // gcc / clang : (sizeof(long) == sizeof(void*)) in 32/64 bits
137 typedef          long INT_PTR;
138 typedef unsigned long UINT_PTR;
139 
140 #define TEXT(quote) quote
141 
142 #define FILE_ATTRIBUTE_READONLY       0x0001
143 #define FILE_ATTRIBUTE_HIDDEN         0x0002
144 #define FILE_ATTRIBUTE_SYSTEM         0x0004
145 #define FILE_ATTRIBUTE_DIRECTORY      0x0010
146 #define FILE_ATTRIBUTE_ARCHIVE        0x0020
147 #define FILE_ATTRIBUTE_DEVICE         0x0040
148 #define FILE_ATTRIBUTE_NORMAL         0x0080
149 #define FILE_ATTRIBUTE_TEMPORARY      0x0100
150 #define FILE_ATTRIBUTE_SPARSE_FILE    0x0200
151 #define FILE_ATTRIBUTE_REPARSE_POINT  0x0400
152 #define FILE_ATTRIBUTE_COMPRESSED     0x0800
153 #define FILE_ATTRIBUTE_OFFLINE        0x1000
154 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x2000
155 #define FILE_ATTRIBUTE_ENCRYPTED      0x4000
156 
157 #define FILE_ATTRIBUTE_UNIX_EXTENSION 0x8000   /* trick for Unix */
158 
159 #endif
160 
161 
162 #ifndef RINOK
163 #define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
164 #endif
165 
166 #ifndef RINOK_WRes
167 #define RINOK_WRes(x) { WRes __result__ = (x); if (__result__ != 0) return __result__; }
168 #endif
169 
170 typedef unsigned char Byte;
171 typedef short Int16;
172 typedef unsigned short UInt16;
173 
174 #ifdef _LZMA_UINT32_IS_ULONG
175 typedef long Int32;
176 typedef unsigned long UInt32;
177 #else
178 typedef int Int32;
179 typedef unsigned int UInt32;
180 #endif
181 
182 
183 #ifndef _WIN32
184 
185 typedef int INT;
186 typedef Int32 INT32;
187 typedef unsigned int UINT;
188 typedef UInt32 UINT32;
189 typedef INT32 LONG;   // LONG, ULONG and DWORD must be 32-bit for _WIN32 compatibility
190 typedef UINT32 ULONG;
191 
192 #undef DWORD
193 typedef UINT32 DWORD;
194 
195 #define VOID void
196 
197 #define HRESULT LONG
198 
199 typedef void *LPVOID;
200 // typedef void VOID;
201 // typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
202 // gcc / clang on Unix  : sizeof(long==sizeof(void*) in 32 or 64 bits)
203 typedef          long  INT_PTR;
204 typedef unsigned long  UINT_PTR;
205 typedef          long  LONG_PTR;
206 typedef unsigned long  DWORD_PTR;
207 
208 typedef size_t SIZE_T;
209 
210 #endif //  _WIN32
211 
212 
213 #define MY_HRES_ERROR__INTERNAL_ERROR  ((HRESULT)0x8007054FL)
214 
215 
216 #ifdef _SZ_NO_INT_64
217 
218 /* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
219    NOTES: Some code will work incorrectly in that case! */
220 
221 typedef long Int64;
222 typedef unsigned long UInt64;
223 
224 #else
225 
226 #if defined(_MSC_VER) || defined(__BORLANDC__)
227 typedef __int64 Int64;
228 typedef unsigned __int64 UInt64;
229 #define UINT64_CONST(n) n
230 #else
231 typedef long long int Int64;
232 typedef unsigned long long int UInt64;
233 #define UINT64_CONST(n) n ## ULL
234 #endif
235 
236 #endif
237 
238 #ifdef _LZMA_NO_SYSTEM_SIZE_T
239 typedef UInt32 SizeT;
240 #else
241 typedef size_t SizeT;
242 #endif
243 
244 typedef int BoolInt;
245 /* typedef BoolInt Bool; */
246 #define True 1
247 #define False 0
248 
249 
250 #ifdef _WIN32
251 #define MY_STD_CALL __stdcall
252 #else
253 #define MY_STD_CALL
254 #endif
255 
256 #ifdef _MSC_VER
257 
258 #if _MSC_VER >= 1300
259 #define MY_NO_INLINE __declspec(noinline)
260 #else
261 #define MY_NO_INLINE
262 #endif
263 
264 #define MY_FORCE_INLINE __forceinline
265 
266 #define MY_CDECL __cdecl
267 #define MY_FAST_CALL __fastcall
268 
269 #else //  _MSC_VER
270 
271 #if (defined(__GNUC__) && (__GNUC__ >= 4)) \
272     || (defined(__clang__) && (__clang_major__ >= 4)) \
273     || defined(__INTEL_COMPILER) \
274     || defined(__xlC__)
275 #define MY_NO_INLINE __attribute__((noinline))
276 // #define MY_FORCE_INLINE __attribute__((always_inline)) inline
277 #else
278 #define MY_NO_INLINE
279 #endif
280 
281 #define MY_FORCE_INLINE
282 
283 
284 #define MY_CDECL
285 
286 #if  defined(_M_IX86) \
287   || defined(__i386__)
288 // #define MY_FAST_CALL __attribute__((fastcall))
289 // #define MY_FAST_CALL __attribute__((cdecl))
290 #define MY_FAST_CALL
291 #elif defined(MY_CPU_AMD64)
292 // #define MY_FAST_CALL __attribute__((ms_abi))
293 #define MY_FAST_CALL
294 #else
295 #define MY_FAST_CALL
296 #endif
297 
298 #endif //  _MSC_VER
299 
300 
301 /* The following interfaces use first parameter as pointer to structure */
302 
303 typedef struct IByteIn IByteIn;
304 struct IByteIn
305 {
306   Byte (*Read)(const IByteIn *p); /* reads one byte, returns 0 in case of EOF or error */
307 };
308 #define IByteIn_Read(p) (p)->Read(p)
309 
310 
311 typedef struct IByteOut IByteOut;
312 struct IByteOut
313 {
314   void (*Write)(const IByteOut *p, Byte b);
315 };
316 #define IByteOut_Write(p, b) (p)->Write(p, b)
317 
318 
319 typedef struct ISeqInStream ISeqInStream;
320 struct ISeqInStream
321 {
322   SRes (*Read)(const ISeqInStream *p, void *buf, size_t *size);
323     /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
324        (output(*size) < input(*size)) is allowed */
325 };
326 #define ISeqInStream_Read(p, buf, size) (p)->Read(p, buf, size)
327 
328 /* it can return SZ_ERROR_INPUT_EOF */
329 SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size);
330 SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType);
331 SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf);
332 
333 
334 typedef struct ISeqOutStream ISeqOutStream;
335 struct ISeqOutStream
336 {
337   size_t (*Write)(const ISeqOutStream *p, const void *buf, size_t size);
338     /* Returns: result - the number of actually written bytes.
339        (result < size) means error */
340 };
341 #define ISeqOutStream_Write(p, buf, size) (p)->Write(p, buf, size)
342 
343 typedef enum
344 {
345   SZ_SEEK_SET = 0,
346   SZ_SEEK_CUR = 1,
347   SZ_SEEK_END = 2
348 } ESzSeek;
349 
350 
351 typedef struct ISeekInStream ISeekInStream;
352 struct ISeekInStream
353 {
354   SRes (*Read)(const ISeekInStream *p, void *buf, size_t *size);  /* same as ISeqInStream::Read */
355   SRes (*Seek)(const ISeekInStream *p, Int64 *pos, ESzSeek origin);
356 };
357 #define ISeekInStream_Read(p, buf, size)   (p)->Read(p, buf, size)
358 #define ISeekInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin)
359 
360 
361 typedef struct ILookInStream ILookInStream;
362 struct ILookInStream
363 {
364   SRes (*Look)(const ILookInStream *p, const void **buf, size_t *size);
365     /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
366        (output(*size) > input(*size)) is not allowed
367        (output(*size) < input(*size)) is allowed */
368   SRes (*Skip)(const ILookInStream *p, size_t offset);
369     /* offset must be <= output(*size) of Look */
370 
371   SRes (*Read)(const ILookInStream *p, void *buf, size_t *size);
372     /* reads directly (without buffer). It's same as ISeqInStream::Read */
373   SRes (*Seek)(const ILookInStream *p, Int64 *pos, ESzSeek origin);
374 };
375 
376 #define ILookInStream_Look(p, buf, size)   (p)->Look(p, buf, size)
377 #define ILookInStream_Skip(p, offset)      (p)->Skip(p, offset)
378 #define ILookInStream_Read(p, buf, size)   (p)->Read(p, buf, size)
379 #define ILookInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin)
380 
381 
382 SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size);
383 SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset);
384 
385 /* reads via ILookInStream::Read */
386 SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType);
387 SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size);
388 
389 
390 
391 typedef struct
392 {
393   ILookInStream vt;
394   const ISeekInStream *realStream;
395 
396   size_t pos;
397   size_t size; /* it's data size */
398 
399   /* the following variables must be set outside */
400   Byte *buf;
401   size_t bufSize;
402 } CLookToRead2;
403 
404 void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead);
405 
406 #define LookToRead2_Init(p) { (p)->pos = (p)->size = 0; }
407 
408 
409 typedef struct
410 {
411   ISeqInStream vt;
412   const ILookInStream *realStream;
413 } CSecToLook;
414 
415 void SecToLook_CreateVTable(CSecToLook *p);
416 
417 
418 
419 typedef struct
420 {
421   ISeqInStream vt;
422   const ILookInStream *realStream;
423 } CSecToRead;
424 
425 void SecToRead_CreateVTable(CSecToRead *p);
426 
427 
428 typedef struct ICompressProgress ICompressProgress;
429 
430 struct ICompressProgress
431 {
432   SRes (*Progress)(const ICompressProgress *p, UInt64 inSize, UInt64 outSize);
433     /* Returns: result. (result != SZ_OK) means break.
434        Value (UInt64)(Int64)-1 for size means unknown value. */
435 };
436 #define ICompressProgress_Progress(p, inSize, outSize) (p)->Progress(p, inSize, outSize)
437 
438 
439 
440 typedef struct ISzAlloc ISzAlloc;
441 typedef const ISzAlloc * ISzAllocPtr;
442 
443 struct ISzAlloc
444 {
445   void *(*Alloc)(ISzAllocPtr p, size_t size);
446   void (*Free)(ISzAllocPtr p, void *address); /* address can be 0 */
447 };
448 
449 #define ISzAlloc_Alloc(p, size) (p)->Alloc(p, size)
450 #define ISzAlloc_Free(p, a) (p)->Free(p, a)
451 
452 /* deprecated */
453 #define IAlloc_Alloc(p, size) ISzAlloc_Alloc(p, size)
454 #define IAlloc_Free(p, a) ISzAlloc_Free(p, a)
455 
456 
457 
458 
459 
460 #ifndef MY_offsetof
461   #ifdef offsetof
462     #define MY_offsetof(type, m) offsetof(type, m)
463     /*
464     #define MY_offsetof(type, m) FIELD_OFFSET(type, m)
465     */
466   #else
467     #define MY_offsetof(type, m) ((size_t)&(((type *)0)->m))
468   #endif
469 #endif
470 
471 
472 
473 #ifndef MY_container_of
474 
475 /*
476 #define MY_container_of(ptr, type, m) container_of(ptr, type, m)
477 #define MY_container_of(ptr, type, m) CONTAINING_RECORD(ptr, type, m)
478 #define MY_container_of(ptr, type, m) ((type *)((char *)(ptr) - offsetof(type, m)))
479 #define MY_container_of(ptr, type, m) (&((type *)0)->m == (ptr), ((type *)(((char *)(ptr)) - MY_offsetof(type, m))))
480 */
481 
482 /*
483   GCC shows warning: "perhaps the 'offsetof' macro was used incorrectly"
484     GCC 3.4.4 : classes with constructor
485     GCC 4.8.1 : classes with non-public variable members"
486 */
487 
488 #define MY_container_of(ptr, type, m) ((type *)(void *)((char *)(void *)(1 ? (ptr) : &((type *)0)->m) - MY_offsetof(type, m)))
489 
490 #endif
491 
492 #define CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) ((type *)(void *)(ptr))
493 
494 /*
495 #define CONTAINER_FROM_VTBL(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m)
496 */
497 #define CONTAINER_FROM_VTBL(ptr, type, m) MY_container_of(ptr, type, m)
498 
499 #define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m)
500 /*
501 #define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL(ptr, type, m)
502 */
503 
504 
505 #define MY_memset_0_ARRAY(a) memset((a), 0, sizeof(a))
506 
507 #ifdef _WIN32
508 
509 #define CHAR_PATH_SEPARATOR '\\'
510 #define WCHAR_PATH_SEPARATOR L'\\'
511 #define STRING_PATH_SEPARATOR "\\"
512 #define WSTRING_PATH_SEPARATOR L"\\"
513 
514 #else
515 
516 #define CHAR_PATH_SEPARATOR '/'
517 #define WCHAR_PATH_SEPARATOR L'/'
518 #define STRING_PATH_SEPARATOR "/"
519 #define WSTRING_PATH_SEPARATOR L"/"
520 
521 #endif
522 
523 EXTERN_C_END
524 
525 #endif
526