Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 1 | /* Types.h -- Basic types |
| 2 | 2010-03-11 : Igor Pavlov : Public domain */ |
| 3 | |
| 4 | #ifndef __7Z_TYPES_H |
| 5 | #define __7Z_TYPES_H |
| 6 | |
| 7 | #include <stddef.h> |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | #include <stdbool.h> |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 10 | |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 11 | |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 12 | #define SZ_OK 0 |
| 13 | |
| 14 | #define SZ_ERROR_DATA 1 |
| 15 | #define SZ_ERROR_MEM 2 |
| 16 | #define SZ_ERROR_CRC 3 |
| 17 | #define SZ_ERROR_UNSUPPORTED 4 |
| 18 | #define SZ_ERROR_PARAM 5 |
| 19 | #define SZ_ERROR_INPUT_EOF 6 |
| 20 | #define SZ_ERROR_OUTPUT_EOF 7 |
| 21 | #define SZ_ERROR_READ 8 |
| 22 | #define SZ_ERROR_WRITE 9 |
| 23 | #define SZ_ERROR_PROGRESS 10 |
| 24 | #define SZ_ERROR_FAIL 11 |
| 25 | #define SZ_ERROR_THREAD 12 |
| 26 | |
| 27 | #define SZ_ERROR_ARCHIVE 16 |
| 28 | #define SZ_ERROR_NO_ARCHIVE 17 |
| 29 | |
| 30 | typedef int SRes; |
Alexandru Gagniuc | e20f27a | 2014-01-27 16:16:47 -0600 | [diff] [blame] | 31 | typedef int WRes; /* This was DWORD for _WIN32. That's uint32_t */ |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 32 | |
| 33 | #ifndef RINOK |
| 34 | #define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; } |
| 35 | #endif |
| 36 | |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 37 | /* The following interfaces use first parameter as pointer to structure */ |
| 38 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 39 | struct IByteIn |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 40 | { |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 41 | uint8_t (*Read)(void *p); /* reads one byte, returns 0 in case of EOF or error */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 42 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 43 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 44 | struct IByteOut |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 45 | { |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 46 | void (*Write)(void *p, uint8_t b); |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 47 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 48 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 49 | struct ISeqInStream |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 50 | { |
| 51 | SRes (*Read)(void *p, void *buf, size_t *size); |
| 52 | /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. |
| 53 | (output(*size) < input(*size)) is allowed */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 54 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 55 | |
| 56 | /* it can return SZ_ERROR_INPUT_EOF */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 57 | SRes SeqInStream_Read(struct ISeqInStream *stream, void *buf, size_t size); |
| 58 | SRes SeqInStream_Read2(struct ISeqInStream *stream, void *buf, size_t size, SRes errorType); |
| 59 | SRes SeqInStream_ReadByte(struct ISeqInStream *stream, uint8_t *buf); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 60 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 61 | struct ISeqOutStream |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 62 | { |
| 63 | size_t (*Write)(void *p, const void *buf, size_t size); |
| 64 | /* Returns: result - the number of actually written bytes. |
| 65 | (result < size) means error */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 66 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 67 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 68 | enum ESzSeek |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 69 | { |
| 70 | SZ_SEEK_SET = 0, |
| 71 | SZ_SEEK_CUR = 1, |
| 72 | SZ_SEEK_END = 2 |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 73 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 74 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 75 | struct ISeekInStream |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 76 | { |
| 77 | SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 78 | SRes (*Seek)(void *p, int64_t *pos, enum ESzSeek origin); |
| 79 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 80 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 81 | struct ILookInStream |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 82 | { |
| 83 | SRes (*Look)(void *p, const void **buf, size_t *size); |
| 84 | /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. |
| 85 | (output(*size) > input(*size)) is not allowed |
| 86 | (output(*size) < input(*size)) is allowed */ |
| 87 | SRes (*Skip)(void *p, size_t offset); |
| 88 | /* offset must be <= output(*size) of Look */ |
| 89 | |
| 90 | SRes (*Read)(void *p, void *buf, size_t *size); |
| 91 | /* reads directly (without buffer). It's same as ISeqInStream::Read */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 92 | SRes (*Seek)(void *p, int64_t *pos, enum ESzSeek origin); |
| 93 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 94 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 95 | SRes LookInStream_LookRead(struct ILookInStream *stream, void *buf, size_t *size); |
| 96 | SRes LookInStream_SeekTo(struct ILookInStream *stream, uint64_t offset); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 97 | |
| 98 | /* reads via ILookInStream::Read */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 99 | SRes LookInStream_Read2(struct ILookInStream *stream, void *buf, size_t size, SRes errorType); |
| 100 | SRes LookInStream_Read(struct ILookInStream *stream, void *buf, size_t size); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 101 | |
| 102 | #define LookToRead_BUF_SIZE (1 << 14) |
| 103 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 104 | struct CLookToRead |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 105 | { |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 106 | struct ILookInStream s; |
| 107 | struct ISeekInStream *realStream; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 108 | size_t pos; |
| 109 | size_t size; |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 110 | uint8_t buf[LookToRead_BUF_SIZE]; |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 111 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 112 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 113 | void LookToRead_CreateVTable(struct CLookToRead *p, int lookahead); |
| 114 | void LookToRead_Init(struct CLookToRead *p); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 115 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 116 | struct CSecToLook |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 117 | { |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 118 | struct ISeqInStream s; |
| 119 | struct ILookInStream *realStream; |
| 120 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 121 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 122 | void SecToLook_CreateVTable(struct CSecToLook *p); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 123 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 124 | struct CSecToRead |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 125 | { |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 126 | struct ISeqInStream s; |
| 127 | struct ILookInStream *realStream; |
| 128 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 129 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 130 | void SecToRead_CreateVTable(struct CSecToRead *p); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 131 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 132 | struct ICompressProgress |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 133 | { |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 134 | SRes (*Progress)(void *p, uint64_t inSize, uint64_t outSize); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 135 | /* Returns: result. (result != SZ_OK) means break. |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 136 | Value (uint64_t)(int64_t)-1 for size means unknown value. */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 137 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 138 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 139 | struct ISzAlloc |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 140 | { |
| 141 | void *(*Alloc)(void *p, size_t size); |
| 142 | void (*Free)(void *p, void *address); /* address can be 0 */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 143 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 144 | |
| 145 | #define IAlloc_Alloc(p, size) (p)->Alloc((p), size) |
| 146 | #define IAlloc_Free(p, a) (p)->Free((p), a) |
| 147 | |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 148 | #endif |