/src/lzma-fuzz/sdk/C/7zStream.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* 7zStream.c -- 7z Stream functions |
2 | | 2017-04-03 : Igor Pavlov : Public domain */ |
3 | | |
4 | | #include "Precomp.h" |
5 | | |
6 | | #include <string.h> |
7 | | |
8 | | #include "7zTypes.h" |
9 | | |
10 | | SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType) |
11 | 0 | { |
12 | 0 | while (size != 0) |
13 | 0 | { |
14 | 0 | size_t processed = size; |
15 | 0 | RINOK(ISeqInStream_Read(stream, buf, &processed)); |
16 | 0 | if (processed == 0) |
17 | 0 | return errorType; |
18 | 0 | buf = (void *)((Byte *)buf + processed); |
19 | 0 | size -= processed; |
20 | 0 | } |
21 | 0 | return SZ_OK; |
22 | 0 | } |
23 | | |
24 | | SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size) |
25 | 0 | { |
26 | 0 | return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); |
27 | 0 | } |
28 | | |
29 | | SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf) |
30 | 0 | { |
31 | 0 | size_t processed = 1; |
32 | 0 | RINOK(ISeqInStream_Read(stream, buf, &processed)); |
33 | 0 | return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF; |
34 | 0 | } |
35 | | |
36 | | |
37 | | |
38 | | SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset) |
39 | 14.4k | { |
40 | 14.4k | Int64 t = offset; |
41 | 14.4k | return ILookInStream_Seek(stream, &t, SZ_SEEK_SET); |
42 | 14.4k | } |
43 | | |
44 | | SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size) |
45 | 0 | { |
46 | 0 | const void *lookBuf; |
47 | 0 | if (*size == 0) |
48 | 0 | return SZ_OK; |
49 | 0 | RINOK(ILookInStream_Look(stream, &lookBuf, size)); |
50 | 0 | memcpy(buf, lookBuf, *size); |
51 | 0 | return ILookInStream_Skip(stream, *size); |
52 | 0 | } |
53 | | |
54 | | SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType) |
55 | 9.47k | { |
56 | 18.9k | while (size != 0) |
57 | 9.48k | { |
58 | 9.48k | size_t processed = size; |
59 | 9.48k | RINOK(ILookInStream_Read(stream, buf, &processed)); |
60 | 9.48k | if (processed == 0) |
61 | 9 | return errorType; |
62 | 9.47k | buf = (void *)((Byte *)buf + processed); |
63 | 9.47k | size -= processed; |
64 | 9.47k | } |
65 | 9.46k | return SZ_OK; |
66 | 9.47k | } |
67 | | |
68 | | SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size) |
69 | 4.57k | { |
70 | 4.57k | return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); |
71 | 4.57k | } |
72 | | |
73 | | |
74 | | |
75 | 0 | #define GET_LookToRead2 CLookToRead2 *p = CONTAINER_FROM_VTBL(pp, CLookToRead2, vt); |
76 | | |
77 | | static SRes LookToRead2_Look_Lookahead(const ILookInStream *pp, const void **buf, size_t *size) |
78 | 0 | { |
79 | 0 | SRes res = SZ_OK; |
80 | 0 | GET_LookToRead2 |
81 | 0 | size_t size2 = p->size - p->pos; |
82 | 0 | if (size2 == 0 && *size != 0) |
83 | 0 | { |
84 | 0 | p->pos = 0; |
85 | 0 | p->size = 0; |
86 | 0 | size2 = p->bufSize; |
87 | 0 | res = ISeekInStream_Read(p->realStream, p->buf, &size2); |
88 | 0 | p->size = size2; |
89 | 0 | } |
90 | 0 | if (*size > size2) |
91 | 0 | *size = size2; |
92 | 0 | *buf = p->buf + p->pos; |
93 | 0 | return res; |
94 | 0 | } |
95 | | |
96 | | static SRes LookToRead2_Look_Exact(const ILookInStream *pp, const void **buf, size_t *size) |
97 | 0 | { |
98 | 0 | SRes res = SZ_OK; |
99 | 0 | GET_LookToRead2 |
100 | 0 | size_t size2 = p->size - p->pos; |
101 | 0 | if (size2 == 0 && *size != 0) |
102 | 0 | { |
103 | 0 | p->pos = 0; |
104 | 0 | p->size = 0; |
105 | 0 | if (*size > p->bufSize) |
106 | 0 | *size = p->bufSize; |
107 | 0 | res = ISeekInStream_Read(p->realStream, p->buf, size); |
108 | 0 | size2 = p->size = *size; |
109 | 0 | } |
110 | 0 | if (*size > size2) |
111 | 0 | *size = size2; |
112 | 0 | *buf = p->buf + p->pos; |
113 | 0 | return res; |
114 | 0 | } |
115 | | |
116 | | static SRes LookToRead2_Skip(const ILookInStream *pp, size_t offset) |
117 | 0 | { |
118 | 0 | GET_LookToRead2 |
119 | 0 | p->pos += offset; |
120 | 0 | return SZ_OK; |
121 | 0 | } |
122 | | |
123 | | static SRes LookToRead2_Read(const ILookInStream *pp, void *buf, size_t *size) |
124 | 0 | { |
125 | 0 | GET_LookToRead2 |
126 | 0 | size_t rem = p->size - p->pos; |
127 | 0 | if (rem == 0) |
128 | 0 | return ISeekInStream_Read(p->realStream, buf, size); |
129 | 0 | if (rem > *size) |
130 | 0 | rem = *size; |
131 | 0 | memcpy(buf, p->buf + p->pos, rem); |
132 | 0 | p->pos += rem; |
133 | 0 | *size = rem; |
134 | 0 | return SZ_OK; |
135 | 0 | } |
136 | | |
137 | | static SRes LookToRead2_Seek(const ILookInStream *pp, Int64 *pos, ESzSeek origin) |
138 | 0 | { |
139 | 0 | GET_LookToRead2 |
140 | 0 | p->pos = p->size = 0; |
141 | 0 | return ISeekInStream_Seek(p->realStream, pos, origin); |
142 | 0 | } |
143 | | |
144 | | void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead) |
145 | 0 | { |
146 | 0 | p->vt.Look = lookahead ? |
147 | 0 | LookToRead2_Look_Lookahead : |
148 | 0 | LookToRead2_Look_Exact; |
149 | 0 | p->vt.Skip = LookToRead2_Skip; |
150 | 0 | p->vt.Read = LookToRead2_Read; |
151 | 0 | p->vt.Seek = LookToRead2_Seek; |
152 | 0 | } |
153 | | |
154 | | |
155 | | |
156 | | static SRes SecToLook_Read(const ISeqInStream *pp, void *buf, size_t *size) |
157 | 0 | { |
158 | 0 | CSecToLook *p = CONTAINER_FROM_VTBL(pp, CSecToLook, vt); |
159 | 0 | return LookInStream_LookRead(p->realStream, buf, size); |
160 | 0 | } |
161 | | |
162 | | void SecToLook_CreateVTable(CSecToLook *p) |
163 | 0 | { |
164 | 0 | p->vt.Read = SecToLook_Read; |
165 | 0 | } |
166 | | |
167 | | static SRes SecToRead_Read(const ISeqInStream *pp, void *buf, size_t *size) |
168 | 0 | { |
169 | 0 | CSecToRead *p = CONTAINER_FROM_VTBL(pp, CSecToRead, vt); |
170 | 0 | return ILookInStream_Read(p->realStream, buf, size); |
171 | 0 | } |
172 | | |
173 | | void SecToRead_CreateVTable(CSecToRead *p) |
174 | 0 | { |
175 | 0 | p->vt.Read = SecToRead_Read; |
176 | 0 | } |