forked from fancycode/lzma-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-buffer.h
367 lines (312 loc) · 8.55 KB
/
common-buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
*
* @copyright Copyright (c) 2019 Joachim Bauch <mail@joachim-bauch.de>
*
* @license GNU GPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "7zTypes.h"
class OutputBuffer {
public:
OutputBuffer();
~OutputBuffer();
ISeqOutStream *stream() { return &stream_.vt; };
const uint8_t *data() const { return data_; }
size_t size() const { return position_; }
private:
static const size_t kInitialSize = 128;
typedef struct {
ISeqOutStream vt;
OutputBuffer *buffer;
} OutputBufferStream;
static size_t _Write(const ISeqOutStream *pp, const void *data, size_t size);
size_t Write(const void *data, size_t size);
OutputBufferStream stream_;
uint8_t *data_ = nullptr;
size_t size_ = 0;
size_t position_ = 0;
};
OutputBuffer::OutputBuffer() {
stream_.vt.Write = &OutputBuffer::_Write;
stream_.buffer = this;
}
OutputBuffer::~OutputBuffer() {
free(data_);
}
// static
size_t OutputBuffer::_Write(const ISeqOutStream *p, const void *data,
size_t size) {
OutputBufferStream *stream = CONTAINER_FROM_VTBL(p, OutputBufferStream, vt);
return stream->buffer->Write(data, size);
}
size_t OutputBuffer::Write(const void *data, size_t size) {
size_t available = size_ - position_;
if (available < size) {
if (!size_) {
size_ = kInitialSize;
}
while (size_ - position_ < size) {
size_ *= 2;
}
uint8_t *tmp = static_cast<uint8_t*>(malloc(size_));
assert(tmp);
if (data_) {
memcpy(tmp, data_, position_);
free(data_);
}
data_ = tmp;
}
memcpy(data_ + position_, data, size);
position_ += size;
return size;
}
class OutputByteBuffer {
public:
OutputByteBuffer();
~OutputByteBuffer();
IByteOut *stream() { return &stream_.vt; };
const uint8_t *data() const { return data_; }
size_t size() const { return position_; }
private:
static const size_t kInitialSize = 128;
typedef struct {
IByteOut vt;
OutputByteBuffer *buffer;
} ByteOutStream;
static void _Write(const IByteOut *pp, Byte b);
void Write(Byte b);
ByteOutStream stream_;
uint8_t *data_ = nullptr;
size_t size_ = 0;
size_t position_ = 0;
};
OutputByteBuffer::OutputByteBuffer() {
stream_.vt.Write = &OutputByteBuffer::_Write;
stream_.buffer = this;
}
OutputByteBuffer::~OutputByteBuffer() {
free(data_);
}
// static
void OutputByteBuffer::_Write(const IByteOut *p, Byte b) {
ByteOutStream *stream = CONTAINER_FROM_VTBL(p, ByteOutStream, vt);
stream->buffer->Write(b);
}
void OutputByteBuffer::Write(Byte b) {
if (size_ == position_) {
if (!size_) {
size_ = kInitialSize;
} else {
size_ *= 2;
}
uint8_t *tmp = static_cast<uint8_t*>(malloc(size_));
assert(tmp);
if (data_) {
memcpy(tmp, data_, position_);
free(data_);
}
data_ = tmp;
}
data_[position_++] = b;
}
class InputBuffer {
public:
InputBuffer(const uint8_t *data, size_t size);
ISeqInStream *stream() { return &stream_.vt; };
private:
typedef struct {
ISeqInStream vt;
InputBuffer *buffer;
} InputBufferStream;
static SRes _Read(const ISeqInStream *p, void *data, size_t *size);
SRes Read(void *data, size_t *size);
InputBufferStream stream_;
const uint8_t *data_;
size_t size_;
};
InputBuffer::InputBuffer(const uint8_t *data, size_t size)
: data_(data), size_(size) {
stream_.vt.Read = &InputBuffer::_Read;
stream_.buffer = this;
}
// static
SRes InputBuffer::_Read(const ISeqInStream *p, void *data, size_t *size) {
InputBufferStream *stream = CONTAINER_FROM_VTBL(p, InputBufferStream, vt);
return stream->buffer->Read(data, size);
}
SRes InputBuffer::Read(void *data, size_t *size) {
if (size_ < *size) {
*size = size_;
}
if (*size > 0) {
memcpy(data, data_, *size);
size_ -= *size;
data_ += *size;
}
return SZ_OK;
}
class InputByteBuffer {
public:
InputByteBuffer(const uint8_t *data, size_t size);
IByteIn *stream() { return &stream_.vt; };
private:
typedef struct {
IByteIn vt;
InputByteBuffer *buffer;
} ByteInStream;
static Byte _Read(const IByteIn *p);
Byte Read();
ByteInStream stream_;
const uint8_t *data_;
size_t size_;
size_t position_ = 0;
};
InputByteBuffer::InputByteBuffer(const uint8_t *data, size_t size)
: data_(data), size_(size) {
stream_.vt.Read = &InputByteBuffer::_Read;
stream_.buffer = this;
}
// static
Byte InputByteBuffer::_Read(const IByteIn *p) {
ByteInStream *stream = CONTAINER_FROM_VTBL(p, ByteInStream, vt);
return stream->buffer->Read();
}
Byte InputByteBuffer::Read() {
if (size_ == position_) {
return 0;
}
return data_[position_++];
}
class InputLookBuffer {
public:
InputLookBuffer(const uint8_t *data, size_t size);
ILookInStream *stream() { return &stream_.vt; };
private:
typedef struct {
ILookInStream vt;
InputLookBuffer *buffer;
} InputLookBufferStream;
static SRes _Look(const ILookInStream *p, const void **data, size_t *size);
SRes Look(const void **data, size_t *size);
static SRes _Skip(const ILookInStream *p, size_t offset);
SRes Skip(size_t offset);
static SRes _Read(const ILookInStream *p, void *data, size_t *size);
SRes Read(void *data, size_t *size);
static SRes _Seek(const ILookInStream *p, Int64 *pos, ESzSeek origin);
SRes Seek(Int64 *pos, ESzSeek origin);
InputLookBufferStream stream_;
const uint8_t *data_;
size_t size_;
size_t position_ = 0;
};
InputLookBuffer::InputLookBuffer(const uint8_t *data, size_t size)
: data_(data), size_(size) {
stream_.vt.Look = &InputLookBuffer::_Look;
stream_.vt.Skip = &InputLookBuffer::_Skip;
stream_.vt.Read = &InputLookBuffer::_Read;
stream_.vt.Seek = &InputLookBuffer::_Seek;
stream_.buffer = this;
}
// static
SRes InputLookBuffer::_Look(const ILookInStream *p, const void **data,
size_t *size) {
InputLookBufferStream *stream =
CONTAINER_FROM_VTBL(p, InputLookBufferStream, vt);
return stream->buffer->Look(data, size);
}
SRes InputLookBuffer::Look(const void **data, size_t *size) {
size_t available = size_ - position_;
if (available < *size) {
*size = available;
}
*data = data_ + position_;
return SZ_OK;
}
// static
SRes InputLookBuffer::_Skip(const ILookInStream *p, size_t offset) {
InputLookBufferStream *stream =
CONTAINER_FROM_VTBL(p, InputLookBufferStream, vt);
return stream->buffer->Skip(offset);
}
SRes InputLookBuffer::Skip(size_t offset) {
size_t available = size_ - position_;
if (offset > available) {
offset = available;
}
position_ += offset;
return SZ_OK;
}
// static
SRes InputLookBuffer::_Read(const ILookInStream *p, void *data, size_t *size) {
InputLookBufferStream *stream =
CONTAINER_FROM_VTBL(p, InputLookBufferStream, vt);
return stream->buffer->Read(data, size);
}
SRes InputLookBuffer::Read(void *data, size_t *size) {
size_t available = size_ - position_;
if (available < *size) {
*size = available;
}
if (*size > 0) {
memcpy(data, data_ + position_, *size);
position_ += *size;
}
return SZ_OK;
}
// static
SRes InputLookBuffer::_Seek(const ILookInStream *p, Int64 *pos,
ESzSeek origin) {
InputLookBufferStream *stream =
CONTAINER_FROM_VTBL(p, InputLookBufferStream, vt);
return stream->buffer->Seek(pos, origin);
}
SRes InputLookBuffer::Seek(Int64 *pos, ESzSeek origin) {
switch (origin) {
case SZ_SEEK_SET:
if (*pos > size_) {
*pos = size_;
} else if (*pos < 0) {
*pos = 0;
}
position_ = *pos;
break;
case SZ_SEEK_CUR:
if (*pos > size_ || position_ + *pos > size_) {
*pos = size_;
} else if (position_ < -*pos) {
*pos = 0;
}
position_ += *pos;
break;
case SZ_SEEK_END:
if (*pos >= 0) {
*pos = size_;
} else if (-(*pos) > size_) {
*pos = 0;
} else {
*pos = size_ + *pos;
}
position_ = *pos;
break;
default:
return SZ_ERROR_PARAM;
}
return SZ_OK;
}