1 /* 7zTypes.h -- Basic types 2 2022-04-01 : 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 #define TEXT(quote) quote 137 138 #define FILE_ATTRIBUTE_READONLY 0x0001 139 #define FILE_ATTRIBUTE_HIDDEN 0x0002 140 #define FILE_ATTRIBUTE_SYSTEM 0x0004 141 #define FILE_ATTRIBUTE_DIRECTORY 0x0010 142 #define FILE_ATTRIBUTE_ARCHIVE 0x0020 143 #define FILE_ATTRIBUTE_DEVICE 0x0040 144 #define FILE_ATTRIBUTE_NORMAL 0x0080 145 #define FILE_ATTRIBUTE_TEMPORARY 0x0100 146 #define FILE_ATTRIBUTE_SPARSE_FILE 0x0200 147 #define FILE_ATTRIBUTE_REPARSE_POINT 0x0400 148 #define FILE_ATTRIBUTE_COMPRESSED 0x0800 149 #define FILE_ATTRIBUTE_OFFLINE 0x1000 150 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x2000 151 #define FILE_ATTRIBUTE_ENCRYPTED 0x4000 152 153 #define FILE_ATTRIBUTE_UNIX_EXTENSION 0x8000 /* trick for Unix */ 154 155 #endif 156 157 158 #ifndef RINOK 159 #define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; } 160 #endif 161 162 #ifndef RINOK_WRes 163 #define RINOK_WRes(x) { WRes __result__ = (x); if (__result__ != 0) return __result__; } 164 #endif 165 166 typedef unsigned char Byte; 167 typedef short Int16; 168 typedef unsigned short UInt16; 169 170 #ifdef _LZMA_UINT32_IS_ULONG 171 typedef long Int32; 172 typedef unsigned long UInt32; 173 #else 174 typedef int Int32; 175 typedef unsigned int UInt32; 176 #endif 177 178 179 #ifndef _WIN32 180 181 typedef int INT; 182 typedef Int32 INT32; 183 typedef unsigned int UINT; 184 typedef UInt32 UINT32; 185 typedef INT32 LONG; // LONG, ULONG and DWORD must be 32-bit for _WIN32 compatibility 186 typedef UINT32 ULONG; 187 188 #undef DWORD 189 typedef UINT32 DWORD; 190 191 #define VOID void 192 193 #define HRESULT LONG 194 195 typedef void *LPVOID; 196 // typedef void VOID; 197 // typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR; 198 // gcc / clang on Unix : sizeof(long==sizeof(void*) in 32 or 64 bits) 199 typedef long INT_PTR; 200 typedef unsigned long UINT_PTR; 201 typedef long LONG_PTR; 202 typedef unsigned long DWORD_PTR; 203 204 typedef size_t SIZE_T; 205 206 #endif // _WIN32 207 208 209 #define MY_HRES_ERROR__INTERNAL_ERROR ((HRESULT)0x8007054FL) 210 211 212 #ifdef _SZ_NO_INT_64 213 214 /* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers. 215 NOTES: Some code will work incorrectly in that case! */ 216 217 typedef long Int64; 218 typedef unsigned long UInt64; 219 220 #else 221 222 #if defined(_MSC_VER) || defined(__BORLANDC__) 223 typedef __int64 Int64; 224 typedef unsigned __int64 UInt64; 225 #define UINT64_CONST(n) n 226 #else 227 typedef long long int Int64; 228 typedef unsigned long long int UInt64; 229 #define UINT64_CONST(n) n ## ULL 230 #endif 231 232 #endif 233 234 #ifdef _LZMA_NO_SYSTEM_SIZE_T 235 typedef UInt32 SizeT; 236 #else 237 typedef size_t SizeT; 238 #endif 239 240 typedef int BoolInt; 241 /* typedef BoolInt Bool; */ 242 #define True 1 243 #define False 0 244 245 246 #ifdef _WIN32 247 #define MY_STD_CALL __stdcall 248 #else 249 #define MY_STD_CALL 250 #endif 251 252 #ifdef _MSC_VER 253 254 #if _MSC_VER >= 1300 255 #define MY_NO_INLINE __declspec(noinline) 256 #else 257 #define MY_NO_INLINE 258 #endif 259 260 #define MY_FORCE_INLINE __forceinline 261 262 #define MY_CDECL __cdecl 263 #define MY_FAST_CALL __fastcall 264 265 #else // _MSC_VER 266 267 #if (defined(__GNUC__) && (__GNUC__ >= 4)) \ 268 || (defined(__clang__) && (__clang_major__ >= 4)) \ 269 || defined(__INTEL_COMPILER) \ 270 || defined(__xlC__) 271 #define MY_NO_INLINE __attribute__((noinline)) 272 // #define MY_FORCE_INLINE __attribute__((always_inline)) inline 273 #else 274 #define MY_NO_INLINE 275 #endif 276 277 #define MY_FORCE_INLINE 278 279 280 #define MY_CDECL 281 282 #if defined(_M_IX86) \ 283 || defined(__i386__) 284 // #define MY_FAST_CALL __attribute__((fastcall)) 285 // #define MY_FAST_CALL __attribute__((cdecl)) 286 #define MY_FAST_CALL 287 #elif defined(MY_CPU_AMD64) 288 // #define MY_FAST_CALL __attribute__((ms_abi)) 289 #define MY_FAST_CALL 290 #else 291 #define MY_FAST_CALL 292 #endif 293 294 #endif // _MSC_VER 295 296 297 /* The following interfaces use first parameter as pointer to structure */ 298 299 typedef struct IByteIn IByteIn; 300 struct IByteIn 301 { 302 Byte (*Read)(const IByteIn *p); /* reads one byte, returns 0 in case of EOF or error */ 303 }; 304 #define IByteIn_Read(p) (p)->Read(p) 305 306 307 typedef struct IByteOut IByteOut; 308 struct IByteOut 309 { 310 void (*Write)(const IByteOut *p, Byte b); 311 }; 312 #define IByteOut_Write(p, b) (p)->Write(p, b) 313 314 315 typedef struct ISeqInStream ISeqInStream; 316 struct ISeqInStream 317 { 318 SRes (*Read)(const ISeqInStream *p, void *buf, size_t *size); 319 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. 320 (output(*size) < input(*size)) is allowed */ 321 }; 322 #define ISeqInStream_Read(p, buf, size) (p)->Read(p, buf, size) 323 324 /* it can return SZ_ERROR_INPUT_EOF */ 325 SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size); 326 SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType); 327 SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf); 328 329 330 typedef struct ISeqOutStream ISeqOutStream; 331 struct ISeqOutStream 332 { 333 size_t (*Write)(const ISeqOutStream *p, const void *buf, size_t size); 334 /* Returns: result - the number of actually written bytes. 335 (result < size) means error */ 336 }; 337 #define ISeqOutStream_Write(p, buf, size) (p)->Write(p, buf, size) 338 339 typedef enum 340 { 341 SZ_SEEK_SET = 0, 342 SZ_SEEK_CUR = 1, 343 SZ_SEEK_END = 2 344 } ESzSeek; 345 346 347 typedef struct ISeekInStream ISeekInStream; 348 struct ISeekInStream 349 { 350 SRes (*Read)(const ISeekInStream *p, void *buf, size_t *size); /* same as ISeqInStream::Read */ 351 SRes (*Seek)(const ISeekInStream *p, Int64 *pos, ESzSeek origin); 352 }; 353 #define ISeekInStream_Read(p, buf, size) (p)->Read(p, buf, size) 354 #define ISeekInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) 355 356 357 typedef struct ILookInStream ILookInStream; 358 struct ILookInStream 359 { 360 SRes (*Look)(const ILookInStream *p, const void **buf, size_t *size); 361 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. 362 (output(*size) > input(*size)) is not allowed 363 (output(*size) < input(*size)) is allowed */ 364 SRes (*Skip)(const ILookInStream *p, size_t offset); 365 /* offset must be <= output(*size) of Look */ 366 367 SRes (*Read)(const ILookInStream *p, void *buf, size_t *size); 368 /* reads directly (without buffer). It's same as ISeqInStream::Read */ 369 SRes (*Seek)(const ILookInStream *p, Int64 *pos, ESzSeek origin); 370 }; 371 372 #define ILookInStream_Look(p, buf, size) (p)->Look(p, buf, size) 373 #define ILookInStream_Skip(p, offset) (p)->Skip(p, offset) 374 #define ILookInStream_Read(p, buf, size) (p)->Read(p, buf, size) 375 #define ILookInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) 376 377 378 SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size); 379 SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset); 380 381 /* reads via ILookInStream::Read */ 382 SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType); 383 SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size); 384 385 386 387 typedef struct 388 { 389 ILookInStream vt; 390 const ISeekInStream *realStream; 391 392 size_t pos; 393 size_t size; /* it's data size */ 394 395 /* the following variables must be set outside */ 396 Byte *buf; 397 size_t bufSize; 398 } CLookToRead2; 399 400 void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead); 401 402 #define LookToRead2_Init(p) { (p)->pos = (p)->size = 0; } 403 404 405 typedef struct 406 { 407 ISeqInStream vt; 408 const ILookInStream *realStream; 409 } CSecToLook; 410 411 void SecToLook_CreateVTable(CSecToLook *p); 412 413 414 415 typedef struct 416 { 417 ISeqInStream vt; 418 const ILookInStream *realStream; 419 } CSecToRead; 420 421 void SecToRead_CreateVTable(CSecToRead *p); 422 423 424 typedef struct ICompressProgress ICompressProgress; 425 426 struct ICompressProgress 427 { 428 SRes (*Progress)(const ICompressProgress *p, UInt64 inSize, UInt64 outSize); 429 /* Returns: result. (result != SZ_OK) means break. 430 Value (UInt64)(Int64)-1 for size means unknown value. */ 431 }; 432 #define ICompressProgress_Progress(p, inSize, outSize) (p)->Progress(p, inSize, outSize) 433 434 435 436 typedef struct ISzAlloc ISzAlloc; 437 typedef const ISzAlloc * ISzAllocPtr; 438 439 struct ISzAlloc 440 { 441 void *(*Alloc)(ISzAllocPtr p, size_t size); 442 void (*Free)(ISzAllocPtr p, void *address); /* address can be 0 */ 443 }; 444 445 #define ISzAlloc_Alloc(p, size) (p)->Alloc(p, size) 446 #define ISzAlloc_Free(p, a) (p)->Free(p, a) 447 448 /* deprecated */ 449 #define IAlloc_Alloc(p, size) ISzAlloc_Alloc(p, size) 450 #define IAlloc_Free(p, a) ISzAlloc_Free(p, a) 451 452 453 454 455 456 #ifndef MY_offsetof 457 #ifdef offsetof 458 #define MY_offsetof(type, m) offsetof(type, m) 459 /* 460 #define MY_offsetof(type, m) FIELD_OFFSET(type, m) 461 */ 462 #else 463 #define MY_offsetof(type, m) ((size_t)&(((type *)0)->m)) 464 #endif 465 #endif 466 467 468 469 #ifndef MY_container_of 470 471 /* 472 #define MY_container_of(ptr, type, m) container_of(ptr, type, m) 473 #define MY_container_of(ptr, type, m) CONTAINING_RECORD(ptr, type, m) 474 #define MY_container_of(ptr, type, m) ((type *)((char *)(ptr) - offsetof(type, m))) 475 #define MY_container_of(ptr, type, m) (&((type *)0)->m == (ptr), ((type *)(((char *)(ptr)) - MY_offsetof(type, m)))) 476 */ 477 478 /* 479 GCC shows warning: "perhaps the 'offsetof' macro was used incorrectly" 480 GCC 3.4.4 : classes with constructor 481 GCC 4.8.1 : classes with non-public variable members" 482 */ 483 484 #define MY_container_of(ptr, type, m) ((type *)(void *)((char *)(void *)(1 ? (ptr) : &((type *)0)->m) - MY_offsetof(type, m))) 485 486 #endif 487 488 #define CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) ((type *)(void *)(ptr)) 489 490 /* 491 #define CONTAINER_FROM_VTBL(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) 492 */ 493 #define CONTAINER_FROM_VTBL(ptr, type, m) MY_container_of(ptr, type, m) 494 495 #define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) 496 /* 497 #define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL(ptr, type, m) 498 */ 499 500 501 #define MY_memset_0_ARRAY(a) memset((a), 0, sizeof(a)) 502 503 #ifdef _WIN32 504 505 #define CHAR_PATH_SEPARATOR '\\' 506 #define WCHAR_PATH_SEPARATOR L'\\' 507 #define STRING_PATH_SEPARATOR "\\" 508 #define WSTRING_PATH_SEPARATOR L"\\" 509 510 #else 511 512 #define CHAR_PATH_SEPARATOR '/' 513 #define WCHAR_PATH_SEPARATOR L'/' 514 #define STRING_PATH_SEPARATOR "/" 515 #define WSTRING_PATH_SEPARATOR L"/" 516 517 #endif 518 519 #define k_PropVar_TimePrec_0 0 520 #define k_PropVar_TimePrec_Unix 1 521 #define k_PropVar_TimePrec_DOS 2 522 #define k_PropVar_TimePrec_HighPrec 3 523 #define k_PropVar_TimePrec_Base 16 524 #define k_PropVar_TimePrec_100ns (k_PropVar_TimePrec_Base + 7) 525 #define k_PropVar_TimePrec_1ns (k_PropVar_TimePrec_Base + 9) 526 527 EXTERN_C_END 528 529 #endif 530