Fix
[ryzomcore.git] / nel / 3rdparty / seven_zip / 7zStream.c
blob6b5aa1621dd141987f2ceb2980fca89431678ce8
1 /* 7zStream.c -- 7z Stream functions
2 2017-04-03 : Igor Pavlov : Public domain */
4 #include "Precomp.h"
6 #include <string.h>
8 #include "7zTypes.h"
10 SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType)
12 while (size != 0)
14 size_t processed = size;
15 RINOK(ISeqInStream_Read(stream, buf, &processed));
16 if (processed == 0)
17 return errorType;
18 buf = (void *)((Byte *)buf + processed);
19 size -= processed;
21 return SZ_OK;
24 SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size)
26 return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
29 SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf)
31 size_t processed = 1;
32 RINOK(ISeqInStream_Read(stream, buf, &processed));
33 return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
38 SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset)
40 Int64 t = offset;
41 return ILookInStream_Seek(stream, &t, SZ_SEEK_SET);
44 SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size)
46 const void *lookBuf;
47 if (*size == 0)
48 return SZ_OK;
49 RINOK(ILookInStream_Look(stream, &lookBuf, size));
50 memcpy(buf, lookBuf, *size);
51 return ILookInStream_Skip(stream, *size);
54 SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType)
56 while (size != 0)
58 size_t processed = size;
59 RINOK(ILookInStream_Read(stream, buf, &processed));
60 if (processed == 0)
61 return errorType;
62 buf = (void *)((Byte *)buf + processed);
63 size -= processed;
65 return SZ_OK;
68 SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size)
70 return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
75 #define GET_LookToRead2 CLookToRead2 *p = CONTAINER_FROM_VTBL(pp, CLookToRead2, vt);
77 static SRes LookToRead2_Look_Lookahead(const ILookInStream *pp, const void **buf, size_t *size)
79 SRes res = SZ_OK;
80 GET_LookToRead2
81 size_t size2 = p->size - p->pos;
82 if (size2 == 0 && *size != 0)
84 p->pos = 0;
85 p->size = 0;
86 size2 = p->bufSize;
87 res = ISeekInStream_Read(p->realStream, p->buf, &size2);
88 p->size = size2;
90 if (*size > size2)
91 *size = size2;
92 *buf = p->buf + p->pos;
93 return res;
96 static SRes LookToRead2_Look_Exact(const ILookInStream *pp, const void **buf, size_t *size)
98 SRes res = SZ_OK;
99 GET_LookToRead2
100 size_t size2 = p->size - p->pos;
101 if (size2 == 0 && *size != 0)
103 p->pos = 0;
104 p->size = 0;
105 if (*size > p->bufSize)
106 *size = p->bufSize;
107 res = ISeekInStream_Read(p->realStream, p->buf, size);
108 size2 = p->size = *size;
110 if (*size > size2)
111 *size = size2;
112 *buf = p->buf + p->pos;
113 return res;
116 static SRes LookToRead2_Skip(const ILookInStream *pp, size_t offset)
118 GET_LookToRead2
119 p->pos += offset;
120 return SZ_OK;
123 static SRes LookToRead2_Read(const ILookInStream *pp, void *buf, size_t *size)
125 GET_LookToRead2
126 size_t rem = p->size - p->pos;
127 if (rem == 0)
128 return ISeekInStream_Read(p->realStream, buf, size);
129 if (rem > *size)
130 rem = *size;
131 memcpy(buf, p->buf + p->pos, rem);
132 p->pos += rem;
133 *size = rem;
134 return SZ_OK;
137 static SRes LookToRead2_Seek(const ILookInStream *pp, Int64 *pos, ESzSeek origin)
139 GET_LookToRead2
140 p->pos = p->size = 0;
141 return ISeekInStream_Seek(p->realStream, pos, origin);
144 void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead)
146 p->vt.Look = lookahead ?
147 LookToRead2_Look_Lookahead :
148 LookToRead2_Look_Exact;
149 p->vt.Skip = LookToRead2_Skip;
150 p->vt.Read = LookToRead2_Read;
151 p->vt.Seek = LookToRead2_Seek;
156 static SRes SecToLook_Read(const ISeqInStream *pp, void *buf, size_t *size)
158 CSecToLook *p = CONTAINER_FROM_VTBL(pp, CSecToLook, vt);
159 return LookInStream_LookRead(p->realStream, buf, size);
162 void SecToLook_CreateVTable(CSecToLook *p)
164 p->vt.Read = SecToLook_Read;
167 static SRes SecToRead_Read(const ISeqInStream *pp, void *buf, size_t *size)
169 CSecToRead *p = CONTAINER_FROM_VTBL(pp, CSecToRead, vt);
170 return ILookInStream_Read(p->realStream, buf, size);
173 void SecToRead_CreateVTable(CSecToRead *p)
175 p->vt.Read = SecToRead_Read;