1 /* 7zTypes.h -- Basic types 2 2018-08-04 : Igor Pavlov : Public domain */ 3 4 #ifndef __7Z_TYPES_H 5 #define __7Z_TYPES_H 6 7 #ifdef _WIN32 8 /* #include <windows.h> */ 9 #endif 10 11 #include <stddef.h> 12 13 #ifndef EXTERN_C_BEGIN 14 #ifdef __cplusplus 15 #define EXTERN_C_BEGIN extern "C" { 16 #define EXTERN_C_END } 17 #else 18 #define EXTERN_C_BEGIN 19 #define EXTERN_C_END 20 #endif 21 #endif 22 23 EXTERN_C_BEGIN 24 25 #define SZ_OK 0 26 27 #define SZ_ERROR_DATA 1 28 #define SZ_ERROR_MEM 2 29 #define SZ_ERROR_CRC 3 30 #define SZ_ERROR_UNSUPPORTED 4 31 #define SZ_ERROR_PARAM 5 32 #define SZ_ERROR_INPUT_EOF 6 33 #define SZ_ERROR_OUTPUT_EOF 7 34 #define SZ_ERROR_READ 8 35 #define SZ_ERROR_WRITE 9 36 #define SZ_ERROR_PROGRESS 10 37 #define SZ_ERROR_FAIL 11 38 #define SZ_ERROR_THREAD 12 39 40 #define SZ_ERROR_ARCHIVE 16 41 #define SZ_ERROR_NO_ARCHIVE 17 42 43 typedef int SRes; 44 45 46 #ifdef _WIN32 47 48 /* typedef DWORD WRes; */ 49 typedef unsigned WRes; 50 #define MY_SRes_HRESULT_FROM_WRes(x) HRESULT_FROM_WIN32(x) 51 52 #else 53 54 typedef int WRes; 55 #define MY__FACILITY_WIN32 7 56 #define MY__FACILITY__WRes MY__FACILITY_WIN32 57 #define MY_SRes_HRESULT_FROM_WRes(x) ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : ((HRESULT) (((x) & 0x0000FFFF) | (MY__FACILITY__WRes << 16) | 0x80000000))) 58 59 #endif 60 61 62 #ifndef RINOK 63 #define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; } 64 #endif 65 66 typedef unsigned char Byte; 67 typedef short Int16; 68 typedef unsigned short UInt16; 69 70 #ifdef _LZMA_UINT32_IS_ULONG 71 typedef long Int32; 72 typedef unsigned long UInt32; 73 #else 74 typedef int Int32; 75 typedef unsigned int UInt32; 76 #endif 77 78 #ifdef _SZ_NO_INT_64 79 80 /* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers. 81 NOTES: Some code will work incorrectly in that case! */ 82 83 typedef long Int64; 84 typedef unsigned long UInt64; 85 86 #else 87 88 #if defined(_MSC_VER) || defined(__BORLANDC__) 89 typedef __int64 Int64; 90 typedef unsigned __int64 UInt64; 91 #define UINT64_CONST(n) n 92 #else 93 typedef long long int Int64; 94 typedef unsigned long long int UInt64; 95 #define UINT64_CONST(n) n ## ULL 96 #endif 97 98 #endif 99 100 #ifdef _LZMA_NO_SYSTEM_SIZE_T 101 typedef UInt32 SizeT; 102 #else 103 typedef size_t SizeT; 104 #endif 105 106 typedef int BoolInt; 107 /* typedef BoolInt Bool; */ 108 #define True 1 109 #define False 0 110 111 112 #ifdef _WIN32 113 #define MY_STD_CALL __stdcall 114 #else 115 #define MY_STD_CALL 116 #endif 117 118 #ifdef _MSC_VER 119 120 #if _MSC_VER >= 1300 121 #define MY_NO_INLINE __declspec(noinline) 122 #else 123 #define MY_NO_INLINE 124 #endif 125 126 #define MY_FORCE_INLINE __forceinline 127 128 #define MY_CDECL __cdecl 129 #define MY_FAST_CALL __fastcall 130 131 #else 132 133 #define MY_NO_INLINE 134 #define MY_FORCE_INLINE 135 #define MY_CDECL 136 #define MY_FAST_CALL 137 138 /* inline keyword : for C++ / C99 */ 139 140 /* GCC, clang: */ 141 /* 142 #if defined (__GNUC__) && (__GNUC__ >= 4) 143 #define MY_FORCE_INLINE __attribute__((always_inline)) 144 #define MY_NO_INLINE __attribute__((noinline)) 145 #endif 146 */ 147 148 #endif 149 150 151 /* The following interfaces use first parameter as pointer to structure */ 152 153 typedef struct IByteIn IByteIn; 154 struct IByteIn 155 { 156 Byte (*Read)(const IByteIn *p); /* reads one byte, returns 0 in case of EOF or error */ 157 }; 158 #define IByteIn_Read(p) (p)->Read(p) 159 160 161 typedef struct IByteOut IByteOut; 162 struct IByteOut 163 { 164 void (*Write)(const IByteOut *p, Byte b); 165 }; 166 #define IByteOut_Write(p, b) (p)->Write(p, b) 167 168 169 typedef struct ISeqInStream ISeqInStream; 170 struct ISeqInStream 171 { 172 SRes (*Read)(const ISeqInStream *p, void *buf, size_t *size); 173 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. 174 (output(*size) < input(*size)) is allowed */ 175 }; 176 #define ISeqInStream_Read(p, buf, size) (p)->Read(p, buf, size) 177 178 /* it can return SZ_ERROR_INPUT_EOF */ 179 SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size); 180 SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType); 181 SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf); 182 183 184 typedef struct ISeqOutStream ISeqOutStream; 185 struct ISeqOutStream 186 { 187 size_t (*Write)(const ISeqOutStream *p, const void *buf, size_t size); 188 /* Returns: result - the number of actually written bytes. 189 (result < size) means error */ 190 }; 191 #define ISeqOutStream_Write(p, buf, size) (p)->Write(p, buf, size) 192 193 typedef enum 194 { 195 SZ_SEEK_SET = 0, 196 SZ_SEEK_CUR = 1, 197 SZ_SEEK_END = 2 198 } ESzSeek; 199 200 201 typedef struct ISeekInStream ISeekInStream; 202 struct ISeekInStream 203 { 204 SRes (*Read)(const ISeekInStream *p, void *buf, size_t *size); /* same as ISeqInStream::Read */ 205 SRes (*Seek)(const ISeekInStream *p, Int64 *pos, ESzSeek origin); 206 }; 207 #define ISeekInStream_Read(p, buf, size) (p)->Read(p, buf, size) 208 #define ISeekInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) 209 210 211 typedef struct ILookInStream ILookInStream; 212 struct ILookInStream 213 { 214 SRes (*Look)(const ILookInStream *p, const void **buf, size_t *size); 215 /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. 216 (output(*size) > input(*size)) is not allowed 217 (output(*size) < input(*size)) is allowed */ 218 SRes (*Skip)(const ILookInStream *p, size_t offset); 219 /* offset must be <= output(*size) of Look */ 220 221 SRes (*Read)(const ILookInStream *p, void *buf, size_t *size); 222 /* reads directly (without buffer). It's same as ISeqInStream::Read */ 223 SRes (*Seek)(const ILookInStream *p, Int64 *pos, ESzSeek origin); 224 }; 225 226 #define ILookInStream_Look(p, buf, size) (p)->Look(p, buf, size) 227 #define ILookInStream_Skip(p, offset) (p)->Skip(p, offset) 228 #define ILookInStream_Read(p, buf, size) (p)->Read(p, buf, size) 229 #define ILookInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) 230 231 232 SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size); 233 SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset); 234 235 /* reads via ILookInStream::Read */ 236 SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType); 237 SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size); 238 239 240 241 typedef struct 242 { 243 ILookInStream vt; 244 const ISeekInStream *realStream; 245 246 size_t pos; 247 size_t size; /* it's data size */ 248 249 /* the following variables must be set outside */ 250 Byte *buf; 251 size_t bufSize; 252 } CLookToRead2; 253 254 void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead); 255 256 #define LookToRead2_Init(p) { (p)->pos = (p)->size = 0; } 257 258 259 typedef struct 260 { 261 ISeqInStream vt; 262 const ILookInStream *realStream; 263 } CSecToLook; 264 265 void SecToLook_CreateVTable(CSecToLook *p); 266 267 268 269 typedef struct 270 { 271 ISeqInStream vt; 272 const ILookInStream *realStream; 273 } CSecToRead; 274 275 void SecToRead_CreateVTable(CSecToRead *p); 276 277 278 typedef struct ICompressProgress ICompressProgress; 279 280 struct ICompressProgress 281 { 282 SRes (*Progress)(const ICompressProgress *p, UInt64 inSize, UInt64 outSize); 283 /* Returns: result. (result != SZ_OK) means break. 284 Value (UInt64)(Int64)-1 for size means unknown value. */ 285 }; 286 #define ICompressProgress_Progress(p, inSize, outSize) (p)->Progress(p, inSize, outSize) 287 288 289 290 typedef struct ISzAlloc ISzAlloc; 291 typedef const ISzAlloc * ISzAllocPtr; 292 293 struct ISzAlloc 294 { 295 void *(*Alloc)(ISzAllocPtr p, size_t size); 296 void (*Free)(ISzAllocPtr p, void *address); /* address can be 0 */ 297 }; 298 299 #define ISzAlloc_Alloc(p, size) (p)->Alloc(p, size) 300 #define ISzAlloc_Free(p, a) (p)->Free(p, a) 301 302 /* deprecated */ 303 #define IAlloc_Alloc(p, size) ISzAlloc_Alloc(p, size) 304 #define IAlloc_Free(p, a) ISzAlloc_Free(p, a) 305 306 307 308 309 310 #ifndef MY_offsetof 311 #ifdef offsetof 312 #define MY_offsetof(type, m) offsetof(type, m) 313 /* 314 #define MY_offsetof(type, m) FIELD_OFFSET(type, m) 315 */ 316 #else 317 #define MY_offsetof(type, m) ((size_t)&(((type *)0)->m)) 318 #endif 319 #endif 320 321 322 323 #ifndef MY_container_of 324 325 /* 326 #define MY_container_of(ptr, type, m) container_of(ptr, type, m) 327 #define MY_container_of(ptr, type, m) CONTAINING_RECORD(ptr, type, m) 328 #define MY_container_of(ptr, type, m) ((type *)((char *)(ptr) - offsetof(type, m))) 329 #define MY_container_of(ptr, type, m) (&((type *)0)->m == (ptr), ((type *)(((char *)(ptr)) - MY_offsetof(type, m)))) 330 */ 331 332 /* 333 GCC shows warning: "perhaps the 'offsetof' macro was used incorrectly" 334 GCC 3.4.4 : classes with constructor 335 GCC 4.8.1 : classes with non-public variable members" 336 */ 337 338 #define MY_container_of(ptr, type, m) ((type *)((char *)(1 ? (ptr) : &((type *)0)->m) - MY_offsetof(type, m))) 339 340 341 #endif 342 343 #define CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) ((type *)(ptr)) 344 345 /* 346 #define CONTAINER_FROM_VTBL(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) 347 */ 348 #define CONTAINER_FROM_VTBL(ptr, type, m) MY_container_of(ptr, type, m) 349 350 #define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) 351 /* 352 #define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL(ptr, type, m) 353 */ 354 355 356 357 #ifdef _WIN32 358 359 #define CHAR_PATH_SEPARATOR '\\' 360 #define WCHAR_PATH_SEPARATOR L'\\' 361 #define STRING_PATH_SEPARATOR "\\" 362 #define WSTRING_PATH_SEPARATOR L"\\" 363 364 #else 365 366 #define CHAR_PATH_SEPARATOR '/' 367 #define WCHAR_PATH_SEPARATOR L'/' 368 #define STRING_PATH_SEPARATOR "/" 369 #define WSTRING_PATH_SEPARATOR L"/" 370 371 #endif 372 373 EXTERN_C_END 374 375 #endif 376