1 /* 7zTypes.h -- Basic types
2 2024-01-24 : Igor Pavlov : Public domain */
4 #ifndef ZIP7_7Z_TYPES_H
5 #define ZIP7_7Z_TYPES_H
8 /* #include <windows.h> */
15 #ifndef EXTERN_C_BEGIN
17 #define EXTERN_C_BEGIN extern "C" {
18 #define EXTERN_C_END }
20 #define EXTERN_C_BEGIN
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
42 #define SZ_ERROR_ARCHIVE 16
43 #define SZ_ERROR_NO_ARCHIVE 17
50 #define MY_ALIGN(n) __declspec(align(n))
58 #define MY_ALIGN(n) alignas(n)
60 #define MY_ALIGN(n) __attribute__ ((aligned(n)))
66 /* typedef DWORD WRes; */
67 typedef unsigned WRes;
68 #define MY_SRes_HRESULT_FROM_WRes(x) HRESULT_FROM_WIN32(x)
70 // #define MY_HRES_ERROR_INTERNAL_ERROR MY_SRes_HRESULT_FROM_WRes(ERROR_INTERNAL_ERROR)
74 // #define ENV_HAVE_LSTAT
77 // (FACILITY_ERRNO = 0x800) is 7zip's FACILITY constant to represent (errno) errors in HRESULT
78 #define MY_FACILITY_ERRNO 0x800
79 #define MY_FACILITY_WIN32 7
80 #define MY_FACILITY_WRes MY_FACILITY_ERRNO
82 #define MY_HRESULT_FROM_errno_CONST_ERROR(x) ((HRESULT)( \
83 ( (HRESULT)(x) & 0x0000FFFF) \
84 | (MY_FACILITY_WRes << 16) \
85 | (HRESULT)0x80000000 ))
87 #define MY_SRes_HRESULT_FROM_WRes(x) \
88 ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : MY_HRESULT_FROM_errno_CONST_ERROR(x))
90 // we call macro HRESULT_FROM_WIN32 for system errors (WRes) that are (errno)
91 #define HRESULT_FROM_WIN32(x) MY_SRes_HRESULT_FROM_WRes(x)
94 #define ERROR_FILE_NOT_FOUND 2L
95 #define ERROR_ACCESS_DENIED 5L
96 #define ERROR_NO_MORE_FILES 18L
97 #define ERROR_LOCK_VIOLATION 33L
98 #define ERROR_FILE_EXISTS 80L
99 #define ERROR_DISK_FULL 112L
100 #define ERROR_NEGATIVE_SEEK 131L
101 #define ERROR_ALREADY_EXISTS 183L
102 #define ERROR_DIRECTORY 267L
103 #define ERROR_TOO_MANY_POSTS 298L
105 #define ERROR_INTERNAL_ERROR 1359L
106 #define ERROR_INVALID_REPARSE_DATA 4392L
107 #define ERROR_REPARSE_TAG_INVALID 4393L
108 #define ERROR_REPARSE_TAG_MISMATCH 4394L
111 // we use errno equivalents for some WIN32 errors:
113 #define ERROR_INVALID_PARAMETER EINVAL
114 #define ERROR_INVALID_FUNCTION EINVAL
115 #define ERROR_ALREADY_EXISTS EEXIST
116 #define ERROR_FILE_EXISTS EEXIST
117 #define ERROR_PATH_NOT_FOUND ENOENT
118 #define ERROR_FILE_NOT_FOUND ENOENT
119 #define ERROR_DISK_FULL ENOSPC
120 // #define ERROR_INVALID_HANDLE EBADF
122 // we use FACILITY_WIN32 for errors that has no errno equivalent
123 // Too many posts were made to a semaphore.
124 #define ERROR_TOO_MANY_POSTS ((HRESULT)0x8007012AL)
125 #define ERROR_INVALID_REPARSE_DATA ((HRESULT)0x80071128L)
126 #define ERROR_REPARSE_TAG_INVALID ((HRESULT)0x80071129L)
128 // if (MY_FACILITY_WRes != FACILITY_WIN32),
129 // we use FACILITY_WIN32 for COM errors:
130 #define E_OUTOFMEMORY ((HRESULT)0x8007000EL)
131 #define E_INVALIDARG ((HRESULT)0x80070057L)
132 #define MY_E_ERROR_NEGATIVE_SEEK ((HRESULT)0x80070083L)
135 // we can use FACILITY_ERRNO for some COM errors, that have errno equivalents:
136 #define E_OUTOFMEMORY MY_HRESULT_FROM_errno_CONST_ERROR(ENOMEM)
137 #define E_INVALIDARG MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL)
138 #define MY_E_ERROR_NEGATIVE_SEEK MY_HRESULT_FROM_errno_CONST_ERROR(EINVAL)
141 #define TEXT(quote) quote
143 #define FILE_ATTRIBUTE_READONLY 0x0001
144 #define FILE_ATTRIBUTE_HIDDEN 0x0002
145 #define FILE_ATTRIBUTE_SYSTEM 0x0004
146 #define FILE_ATTRIBUTE_DIRECTORY 0x0010
147 #define FILE_ATTRIBUTE_ARCHIVE 0x0020
148 #define FILE_ATTRIBUTE_DEVICE 0x0040
149 #define FILE_ATTRIBUTE_NORMAL 0x0080
150 #define FILE_ATTRIBUTE_TEMPORARY 0x0100
151 #define FILE_ATTRIBUTE_SPARSE_FILE 0x0200
152 #define FILE_ATTRIBUTE_REPARSE_POINT 0x0400
153 #define FILE_ATTRIBUTE_COMPRESSED 0x0800
154 #define FILE_ATTRIBUTE_OFFLINE 0x1000
155 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x2000
156 #define FILE_ATTRIBUTE_ENCRYPTED 0x4000
158 #define FILE_ATTRIBUTE_UNIX_EXTENSION 0x8000 /* trick for Unix */
164 #define RINOK(x) { const int _result_ = (x); if (_result_ != 0) return _result_; }
168 #define RINOK_WRes(x) { const WRes _result_ = (x); if (_result_ != 0) return _result_; }
171 typedef unsigned char Byte;
173 typedef unsigned short UInt16;
175 #ifdef Z7_DECL_Int32_AS_long
177 typedef unsigned long UInt32;
180 typedef unsigned int UInt32;
188 typedef unsigned int UINT;
189 typedef UInt32 UINT32;
190 typedef INT32 LONG; // LONG, ULONG and DWORD must be 32-bit for _WIN32 compatibility
191 typedef UINT32 ULONG;
194 typedef UINT32 DWORD;
200 typedef void *LPVOID;
201 // typedef void VOID;
202 // typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
203 // gcc / clang on Unix : sizeof(long==sizeof(void*) in 32 or 64 bits)
204 typedef long INT_PTR;
205 typedef unsigned long UINT_PTR;
206 typedef long LONG_PTR;
207 typedef unsigned long DWORD_PTR;
209 typedef size_t SIZE_T;
214 #define MY_HRES_ERROR_INTERNAL_ERROR ((HRESULT)0x8007054FL)
217 #ifdef Z7_DECL_Int64_AS_long
220 typedef unsigned long UInt64;
224 #if (defined(_MSC_VER) || defined(__BORLANDC__)) && !defined(__clang__)
225 typedef __int64 Int64;
226 typedef unsigned __int64 UInt64;
228 #if defined(__clang__) || defined(__GNUC__)
230 typedef int64_t Int64;
231 typedef uint64_t UInt64;
233 typedef long long int Int64;
234 typedef unsigned long long int UInt64;
235 // #define UINT64_CONST(n) n ## ULL
241 #define UINT64_CONST(n) n
244 #ifdef Z7_DECL_SizeT_AS_unsigned_int
245 typedef unsigned int SizeT;
247 typedef size_t SizeT;
251 #if (defined(_MSC_VER) && _MSC_VER <= 1200)
252 typedef size_t MY_uintptr_t;
255 typedef uintptr_t MY_uintptr_t;
260 /* typedef BoolInt Bool; */
266 #define Z7_STDCALL __stdcall
274 #define Z7_NO_INLINE __declspec(noinline)
279 #define Z7_FORCE_INLINE __forceinline
281 #define Z7_CDECL __cdecl
282 #define Z7_FASTCALL __fastcall
286 #if (defined(__GNUC__) && (__GNUC__ >= 4)) \
287 || (defined(__clang__) && (__clang_major__ >= 4)) \
288 || defined(__INTEL_COMPILER) \
290 #define Z7_NO_INLINE __attribute__((noinline))
291 #define Z7_FORCE_INLINE __attribute__((always_inline)) inline
294 #define Z7_FORCE_INLINE
299 #if defined(_M_IX86) \
301 // #define Z7_FASTCALL __attribute__((fastcall))
302 // #define Z7_FASTCALL __attribute__((cdecl))
304 #elif defined(MY_CPU_AMD64)
305 // #define Z7_FASTCALL __attribute__((ms_abi))
314 /* The following interfaces use first parameter as pointer to structure */
316 // #define Z7_C_IFACE_CONST_QUAL
317 #define Z7_C_IFACE_CONST_QUAL const
319 #define Z7_C_IFACE_DECL(a) \
321 typedef Z7_C_IFACE_CONST_QUAL struct a ## _ * a ## Ptr; \
322 typedef struct a ## _ a; \
326 Z7_C_IFACE_DECL (IByteIn)
328 Byte (*Read)(IByteInPtr p); /* reads one byte, returns 0 in case of EOF or error */
330 #define IByteIn_Read(p) (p)->Read(p)
333 Z7_C_IFACE_DECL (IByteOut)
335 void (*Write)(IByteOutPtr p, Byte b);
337 #define IByteOut_Write(p, b) (p)->Write(p, b)
340 Z7_C_IFACE_DECL (ISeqInStream)
342 SRes (*Read)(ISeqInStreamPtr p, void *buf, size_t *size);
343 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
344 (output(*size) < input(*size)) is allowed */
346 #define ISeqInStream_Read(p, buf, size) (p)->Read(p, buf, size)
348 /* try to read as much as avail in stream and limited by (*processedSize) */
349 SRes SeqInStream_ReadMax(ISeqInStreamPtr stream, void *buf, size_t *processedSize);
350 /* it can return SZ_ERROR_INPUT_EOF */
351 // SRes SeqInStream_Read(ISeqInStreamPtr stream, void *buf, size_t size);
352 // SRes SeqInStream_Read2(ISeqInStreamPtr stream, void *buf, size_t size, SRes errorType);
353 SRes SeqInStream_ReadByte(ISeqInStreamPtr stream, Byte *buf);
356 Z7_C_IFACE_DECL (ISeqOutStream)
358 size_t (*Write)(ISeqOutStreamPtr p, const void *buf, size_t size);
359 /* Returns: result - the number of actually written bytes.
360 (result < size) means error */
362 #define ISeqOutStream_Write(p, buf, size) (p)->Write(p, buf, size)
372 Z7_C_IFACE_DECL (ISeekInStream)
374 SRes (*Read)(ISeekInStreamPtr p, void *buf, size_t *size); /* same as ISeqInStream::Read */
375 SRes (*Seek)(ISeekInStreamPtr p, Int64 *pos, ESzSeek origin);
377 #define ISeekInStream_Read(p, buf, size) (p)->Read(p, buf, size)
378 #define ISeekInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin)
381 Z7_C_IFACE_DECL (ILookInStream)
383 SRes (*Look)(ILookInStreamPtr p, const void **buf, size_t *size);
384 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
385 (output(*size) > input(*size)) is not allowed
386 (output(*size) < input(*size)) is allowed */
387 SRes (*Skip)(ILookInStreamPtr p, size_t offset);
388 /* offset must be <= output(*size) of Look */
389 SRes (*Read)(ILookInStreamPtr p, void *buf, size_t *size);
390 /* reads directly (without buffer). It's same as ISeqInStream::Read */
391 SRes (*Seek)(ILookInStreamPtr p, Int64 *pos, ESzSeek origin);
394 #define ILookInStream_Look(p, buf, size) (p)->Look(p, buf, size)
395 #define ILookInStream_Skip(p, offset) (p)->Skip(p, offset)
396 #define ILookInStream_Read(p, buf, size) (p)->Read(p, buf, size)
397 #define ILookInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin)
400 SRes LookInStream_LookRead(ILookInStreamPtr stream, void *buf, size_t *size);
401 SRes LookInStream_SeekTo(ILookInStreamPtr stream, UInt64 offset);
403 /* reads via ILookInStream::Read */
404 SRes LookInStream_Read2(ILookInStreamPtr stream, void *buf, size_t size, SRes errorType);
405 SRes LookInStream_Read(ILookInStreamPtr stream, void *buf, size_t size);
411 ISeekInStreamPtr realStream;
414 size_t size; /* it's data size */
416 /* the following variables must be set outside */
421 void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead);
423 #define LookToRead2_INIT(p) { (p)->pos = (p)->size = 0; }
429 ILookInStreamPtr realStream;
432 void SecToLook_CreateVTable(CSecToLook *p);
439 ILookInStreamPtr realStream;
442 void SecToRead_CreateVTable(CSecToRead *p);
445 Z7_C_IFACE_DECL (ICompressProgress)
447 SRes (*Progress)(ICompressProgressPtr p, UInt64 inSize, UInt64 outSize);
448 /* Returns: result. (result != SZ_OK) means break.
449 Value (UInt64)(Int64)-1 for size means unknown value. */
452 #define ICompressProgress_Progress(p, inSize, outSize) (p)->Progress(p, inSize, outSize)
456 typedef struct ISzAlloc ISzAlloc;
457 typedef const ISzAlloc * ISzAllocPtr;
461 void *(*Alloc)(ISzAllocPtr p, size_t size);
462 void (*Free)(ISzAllocPtr p, void *address); /* address can be 0 */
465 #define ISzAlloc_Alloc(p, size) (p)->Alloc(p, size)
466 #define ISzAlloc_Free(p, a) (p)->Free(p, a)
469 #define IAlloc_Alloc(p, size) ISzAlloc_Alloc(p, size)
470 #define IAlloc_Free(p, a) ISzAlloc_Free(p, a)
478 #define MY_offsetof(type, m) offsetof(type, m)
480 #define MY_offsetof(type, m) FIELD_OFFSET(type, m)
483 #define MY_offsetof(type, m) ((size_t)&(((type *)0)->m))
489 #ifndef Z7_container_of
492 #define Z7_container_of(ptr, type, m) container_of(ptr, type, m)
493 #define Z7_container_of(ptr, type, m) CONTAINING_RECORD(ptr, type, m)
494 #define Z7_container_of(ptr, type, m) ((type *)((char *)(ptr) - offsetof(type, m)))
495 #define Z7_container_of(ptr, type, m) (&((type *)0)->m == (ptr), ((type *)(((char *)(ptr)) - MY_offsetof(type, m))))
499 GCC shows warning: "perhaps the 'offsetof' macro was used incorrectly"
500 GCC 3.4.4 : classes with constructor
501 GCC 4.8.1 : classes with non-public variable members"
504 #define Z7_container_of(ptr, type, m) \
505 ((type *)(void *)((char *)(void *) \
506 (1 ? (ptr) : &((type *)NULL)->m) - MY_offsetof(type, m)))
508 #define Z7_container_of_CONST(ptr, type, m) \
509 ((const type *)(const void *)((const char *)(const void *) \
510 (1 ? (ptr) : &((type *)NULL)->m) - MY_offsetof(type, m)))
513 #define Z7_container_of_NON_CONST_FROM_CONST(ptr, type, m) \
514 ((type *)(void *)(const void *)((const char *)(const void *) \
515 (1 ? (ptr) : &((type *)NULL)->m) - MY_offsetof(type, m)))
520 #define Z7_CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) ((type *)(void *)(ptr))
522 // #define Z7_CONTAINER_FROM_VTBL(ptr, type, m) Z7_CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m)
523 #define Z7_CONTAINER_FROM_VTBL(ptr, type, m) Z7_container_of(ptr, type, m)
524 // #define Z7_CONTAINER_FROM_VTBL(ptr, type, m) Z7_container_of_NON_CONST_FROM_CONST(ptr, type, m)
526 #define Z7_CONTAINER_FROM_VTBL_CONST(ptr, type, m) Z7_container_of_CONST(ptr, type, m)
528 #define Z7_CONTAINER_FROM_VTBL_CLS(ptr, type, m) Z7_CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m)
530 #define Z7_CONTAINER_FROM_VTBL_CLS(ptr, type, m) Z7_CONTAINER_FROM_VTBL(ptr, type, m)
532 #if defined (__clang__) || defined(__GNUC__)
533 #define Z7_DIAGNOSTIC_IGNORE_BEGIN_CAST_QUAL \
534 _Pragma("GCC diagnostic push") \
535 _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
536 #define Z7_DIAGNOSTIC_IGNORE_END_CAST_QUAL \
537 _Pragma("GCC diagnostic pop")
539 #define Z7_DIAGNOSTIC_IGNORE_BEGIN_CAST_QUAL
540 #define Z7_DIAGNOSTIC_IGNORE_END_CAST_QUAL
543 #define Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR(ptr, type, m, p) \
544 Z7_DIAGNOSTIC_IGNORE_BEGIN_CAST_QUAL \
545 type *p = Z7_CONTAINER_FROM_VTBL(ptr, type, m); \
546 Z7_DIAGNOSTIC_IGNORE_END_CAST_QUAL
548 #define Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(type) \
549 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR(pp, type, vt, p)
552 // #define ZIP7_DECLARE_HANDLE(name) typedef void *name;
553 #define Z7_DECLARE_HANDLE(name) struct name##_dummy{int unused;}; typedef struct name##_dummy *name;
556 #define Z7_memset_0_ARRAY(a) memset((a), 0, sizeof(a))
558 #ifndef Z7_ARRAY_SIZE
559 #define Z7_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
565 #define CHAR_PATH_SEPARATOR '\\'
566 #define WCHAR_PATH_SEPARATOR L'\\'
567 #define STRING_PATH_SEPARATOR "\\"
568 #define WSTRING_PATH_SEPARATOR L"\\"
572 #define CHAR_PATH_SEPARATOR '/'
573 #define WCHAR_PATH_SEPARATOR L'/'
574 #define STRING_PATH_SEPARATOR "/"
575 #define WSTRING_PATH_SEPARATOR L"/"
579 #define k_PropVar_TimePrec_0 0
580 #define k_PropVar_TimePrec_Unix 1
581 #define k_PropVar_TimePrec_DOS 2
582 #define k_PropVar_TimePrec_HighPrec 3
583 #define k_PropVar_TimePrec_Base 16
584 #define k_PropVar_TimePrec_100ns (k_PropVar_TimePrec_Base + 7)
585 #define k_PropVar_TimePrec_1ns (k_PropVar_TimePrec_Base + 9)