7zTypes.h 9.0 KB

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