1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 /* 7zStream.c -- 7z Stream functions
28 2008-11-23 : Igor Pavlov : Public domain */
29 #include "vogl_core.h"
32 #include "lzma_Types.h"
37 SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType)
41 size_t processed = size;
42 RINOK(stream->Read(stream, buf, &processed));
45 buf = (void *)((Byte *)buf + processed);
51 SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size)
53 return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
56 SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf)
59 RINOK(stream->Read(stream, buf, &processed));
60 return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
63 SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset)
66 return stream->Seek(stream, &t, SZ_SEEK_SET);
69 SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size)
74 RINOK(stream->Look(stream, &lookBuf, size));
75 memcpy(buf, lookBuf, *size);
76 return stream->Skip(stream, *size);
79 SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType)
83 size_t processed = size;
84 RINOK(stream->Read(stream, buf, &processed));
87 buf = (void *)((Byte *)buf + processed);
93 SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size)
95 return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
98 static SRes LookToRead_Look_Lookahead(void *pp, void **buf, size_t *size)
101 CLookToRead *p = (CLookToRead *)pp;
102 size_t size2 = p->size - p->pos;
103 if (size2 == 0 && *size > 0)
106 size2 = LookToRead_BUF_SIZE;
107 res = p->realStream->Read(p->realStream, p->buf, &size2);
112 *buf = p->buf + p->pos;
116 static SRes LookToRead_Look_Exact(void *pp, void **buf, size_t *size)
119 CLookToRead *p = (CLookToRead *)pp;
120 size_t size2 = p->size - p->pos;
121 if (size2 == 0 && *size > 0)
124 if (*size > LookToRead_BUF_SIZE)
125 *size = LookToRead_BUF_SIZE;
126 res = p->realStream->Read(p->realStream, p->buf, size);
127 size2 = p->size = *size;
131 *buf = p->buf + p->pos;
135 static SRes LookToRead_Skip(void *pp, size_t offset)
137 CLookToRead *p = (CLookToRead *)pp;
142 static SRes LookToRead_Read(void *pp, void *buf, size_t *size)
144 CLookToRead *p = (CLookToRead *)pp;
145 size_t rem = p->size - p->pos;
147 return p->realStream->Read(p->realStream, buf, size);
150 memcpy(buf, p->buf + p->pos, rem);
156 static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin)
158 CLookToRead *p = (CLookToRead *)pp;
159 p->pos = p->size = 0;
160 return p->realStream->Seek(p->realStream, pos, origin);
163 void LookToRead_CreateVTable(CLookToRead *p, int lookahead)
165 p->s.Look = lookahead ? LookToRead_Look_Lookahead : LookToRead_Look_Exact;
166 p->s.Skip = LookToRead_Skip;
167 p->s.Read = LookToRead_Read;
168 p->s.Seek = LookToRead_Seek;
171 void LookToRead_Init(CLookToRead *p)
173 p->pos = p->size = 0;
176 static SRes SecToLook_Read(void *pp, void *buf, size_t *size)
178 CSecToLook *p = (CSecToLook *)pp;
179 return LookInStream_LookRead(p->realStream, buf, size);
182 void SecToLook_CreateVTable(CSecToLook *p)
184 p->s.Read = SecToLook_Read;
187 static SRes SecToRead_Read(void *pp, void *buf, size_t *size)
189 CSecToRead *p = (CSecToRead *)pp;
190 return p->realStream->Read(p->realStream, buf, size);
193 void SecToRead_CreateVTable(CSecToRead *p)
195 p->s.Read = SecToRead_Read;