blob: ce98ab5e4f4548d1d4c5379d0ac2e058bbe9bc44 [file] [log] [blame]
Stefan Reinauer2e200cd2012-10-30 14:02:45 -07001/* Types.h -- Basic types
22010-03-11 : Igor Pavlov : Public domain */
3
4#ifndef __7Z_TYPES_H
5#define __7Z_TYPES_H
6
7#include <stddef.h>
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -06008#include <stdint.h>
9#include <stdbool.h>
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070010
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070011
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070012#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
30typedef int SRes;
Alexandru Gagniuce20f27a2014-01-27 16:16:47 -060031typedef int WRes; /* This was DWORD for _WIN32. That's uint32_t */
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070032
33#ifndef RINOK
34#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
35#endif
36
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070037/* The following interfaces use first parameter as pointer to structure */
38
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060039struct IByteIn
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070040{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060041 uint8_t (*Read)(void *p); /* reads one byte, returns 0 in case of EOF or error */
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060042};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070043
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060044struct IByteOut
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070045{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -060046 void (*Write)(void *p, uint8_t b);
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060047};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070048
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060049struct ISeqInStream
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070050{
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 Gagniuc9ad52fe2014-01-27 20:57:54 -060054};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070055
56/* it can return SZ_ERROR_INPUT_EOF */
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060057SRes SeqInStream_Read(struct ISeqInStream *stream, void *buf, size_t size);
58SRes SeqInStream_Read2(struct ISeqInStream *stream, void *buf, size_t size, SRes errorType);
59SRes SeqInStream_ReadByte(struct ISeqInStream *stream, uint8_t *buf);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070060
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060061struct ISeqOutStream
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070062{
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 Gagniuc9ad52fe2014-01-27 20:57:54 -060066};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070067
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060068enum ESzSeek
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070069{
70 SZ_SEEK_SET = 0,
71 SZ_SEEK_CUR = 1,
72 SZ_SEEK_END = 2
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060073};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070074
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060075struct ISeekInStream
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070076{
77 SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060078 SRes (*Seek)(void *p, int64_t *pos, enum ESzSeek origin);
79};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070080
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060081struct ILookInStream
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070082{
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 Gagniuc9ad52fe2014-01-27 20:57:54 -060092 SRes (*Seek)(void *p, int64_t *pos, enum ESzSeek origin);
93};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070094
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060095SRes LookInStream_LookRead(struct ILookInStream *stream, void *buf, size_t *size);
96SRes LookInStream_SeekTo(struct ILookInStream *stream, uint64_t offset);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -070097
98/* reads via ILookInStream::Read */
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -060099SRes LookInStream_Read2(struct ILookInStream *stream, void *buf, size_t size, SRes errorType);
100SRes LookInStream_Read(struct ILookInStream *stream, void *buf, size_t size);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700101
102#define LookToRead_BUF_SIZE (1 << 14)
103
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600104struct CLookToRead
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700105{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600106 struct ILookInStream s;
107 struct ISeekInStream *realStream;
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700108 size_t pos;
109 size_t size;
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600110 uint8_t buf[LookToRead_BUF_SIZE];
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600111};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700112
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600113void LookToRead_CreateVTable(struct CLookToRead *p, int lookahead);
114void LookToRead_Init(struct CLookToRead *p);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700115
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600116struct CSecToLook
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700117{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600118 struct ISeqInStream s;
119 struct ILookInStream *realStream;
120};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700121
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600122void SecToLook_CreateVTable(struct CSecToLook *p);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700123
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600124struct CSecToRead
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700125{
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600126 struct ISeqInStream s;
127 struct ILookInStream *realStream;
128};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700129
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600130void SecToRead_CreateVTable(struct CSecToRead *p);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700131
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600132struct ICompressProgress
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700133{
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600134 SRes (*Progress)(void *p, uint64_t inSize, uint64_t outSize);
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700135 /* Returns: result. (result != SZ_OK) means break.
Alexandru Gagniuc91e9f272014-01-26 22:55:01 -0600136 Value (uint64_t)(int64_t)-1 for size means unknown value. */
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600137};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700138
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600139struct ISzAlloc
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700140{
141 void *(*Alloc)(void *p, size_t size);
142 void (*Free)(void *p, void *address); /* address can be 0 */
Alexandru Gagniuc9ad52fe2014-01-27 20:57:54 -0600143};
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700144
145#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
146#define IAlloc_Free(p, a) (p)->Free((p), a)
147
Stefan Reinauer2e200cd2012-10-30 14:02:45 -0700148#endif