]> git.cworth.org Git - vogl/blob - src/voglcore/lzma_7zStream.cpp
Initial vogl checkin
[vogl] / src / voglcore / lzma_7zStream.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 /* 7zStream.c -- 7z Stream functions
28 2008-11-23 : Igor Pavlov : Public domain */
29 #include "vogl_core.h"
30 #include <string.h>
31
32 #include "lzma_Types.h"
33
34 namespace vogl
35 {
36
37     SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType)
38     {
39         while (size != 0)
40         {
41             size_t processed = size;
42             RINOK(stream->Read(stream, buf, &processed));
43             if (processed == 0)
44                 return errorType;
45             buf = (void *)((Byte *)buf + processed);
46             size -= processed;
47         }
48         return SZ_OK;
49     }
50
51     SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size)
52     {
53         return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
54     }
55
56     SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf)
57     {
58         size_t processed = 1;
59         RINOK(stream->Read(stream, buf, &processed));
60         return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
61     }
62
63     SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset)
64     {
65         Int64 t = offset;
66         return stream->Seek(stream, &t, SZ_SEEK_SET);
67     }
68
69     SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size)
70     {
71         void *lookBuf;
72         if (*size == 0)
73             return SZ_OK;
74         RINOK(stream->Look(stream, &lookBuf, size));
75         memcpy(buf, lookBuf, *size);
76         return stream->Skip(stream, *size);
77     }
78
79     SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType)
80     {
81         while (size != 0)
82         {
83             size_t processed = size;
84             RINOK(stream->Read(stream, buf, &processed));
85             if (processed == 0)
86                 return errorType;
87             buf = (void *)((Byte *)buf + processed);
88             size -= processed;
89         }
90         return SZ_OK;
91     }
92
93     SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size)
94     {
95         return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
96     }
97
98     static SRes LookToRead_Look_Lookahead(void *pp, void **buf, size_t *size)
99     {
100         SRes res = SZ_OK;
101         CLookToRead *p = (CLookToRead *)pp;
102         size_t size2 = p->size - p->pos;
103         if (size2 == 0 && *size > 0)
104         {
105             p->pos = 0;
106             size2 = LookToRead_BUF_SIZE;
107             res = p->realStream->Read(p->realStream, p->buf, &size2);
108             p->size = size2;
109         }
110         if (size2 < *size)
111             *size = size2;
112         *buf = p->buf + p->pos;
113         return res;
114     }
115
116     static SRes LookToRead_Look_Exact(void *pp, void **buf, size_t *size)
117     {
118         SRes res = SZ_OK;
119         CLookToRead *p = (CLookToRead *)pp;
120         size_t size2 = p->size - p->pos;
121         if (size2 == 0 && *size > 0)
122         {
123             p->pos = 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;
128         }
129         if (size2 < *size)
130             *size = size2;
131         *buf = p->buf + p->pos;
132         return res;
133     }
134
135     static SRes LookToRead_Skip(void *pp, size_t offset)
136     {
137         CLookToRead *p = (CLookToRead *)pp;
138         p->pos += offset;
139         return SZ_OK;
140     }
141
142     static SRes LookToRead_Read(void *pp, void *buf, size_t *size)
143     {
144         CLookToRead *p = (CLookToRead *)pp;
145         size_t rem = p->size - p->pos;
146         if (rem == 0)
147             return p->realStream->Read(p->realStream, buf, size);
148         if (rem > *size)
149             rem = *size;
150         memcpy(buf, p->buf + p->pos, rem);
151         p->pos += rem;
152         *size = rem;
153         return SZ_OK;
154     }
155
156     static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin)
157     {
158         CLookToRead *p = (CLookToRead *)pp;
159         p->pos = p->size = 0;
160         return p->realStream->Seek(p->realStream, pos, origin);
161     }
162
163     void LookToRead_CreateVTable(CLookToRead *p, int lookahead)
164     {
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;
169     }
170
171     void LookToRead_Init(CLookToRead *p)
172     {
173         p->pos = p->size = 0;
174     }
175
176     static SRes SecToLook_Read(void *pp, void *buf, size_t *size)
177     {
178         CSecToLook *p = (CSecToLook *)pp;
179         return LookInStream_LookRead(p->realStream, buf, size);
180     }
181
182     void SecToLook_CreateVTable(CSecToLook *p)
183     {
184         p->s.Read = SecToLook_Read;
185     }
186
187     static SRes SecToRead_Read(void *pp, void *buf, size_t *size)
188     {
189         CSecToRead *p = (CSecToRead *)pp;
190         return p->realStream->Read(p->realStream, buf, size);
191     }
192
193     void SecToRead_CreateVTable(CSecToRead *p)
194     {
195         p->s.Read = SecToRead_Read;
196     }
197 }