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
|
/*
Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
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; version 2 of the License.
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; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301 USA.
*/
/* yaSSL buffer header implements input/output buffers to simulate streaming
* with SSL types and sockets
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h> // memcpy
#include "runtime.hpp"
#include "buffer.hpp"
#include "yassl_types.hpp"
namespace yaSSL {
/* return 0 on check success, always true for NoCheck policy */
int NoCheck::check(uint, uint)
{
return 0;
}
/* return 0 on check success */
int Check::check(uint i, uint max)
{
if (i < max)
return 0;
return -1;
}
/* input_buffer operates like a smart c style array with a checking option,
* meant to be read from through [] with AUTO index or read().
* Should only write to at/near construction with assign() or raw (e.g., recv)
* followed by add_size with the number of elements added by raw write.
*
* Not using vector because need checked []access, offset, and the ability to
* write to the buffer bulk wise and have the correct size
*/
input_buffer::input_buffer()
: size_(0), current_(0), buffer_(0), end_(0), error_(0), zero_(0)
{}
input_buffer::input_buffer(uint s)
: size_(0), current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s),
error_(0), zero_(0)
{}
// with assign
input_buffer::input_buffer(uint s, const byte* t, uint len)
: size_(0), current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s),
error_(0), zero_(0)
{
assign(t, len);
}
input_buffer::~input_buffer()
{
ysArrayDelete(buffer_);
}
// users can pass defualt zero length buffer and then allocate
void input_buffer::allocate(uint s)
{
if (error_ == 0) {
buffer_ = NEW_YS byte[s];
end_ = buffer_ + s;
}
}
// for passing to raw writing functions at beginning, then use add_size
byte* input_buffer::get_buffer() const
{
return buffer_;
}
// after a raw write user can set NEW_YS size
// if you know the size before the write use assign()
void input_buffer::add_size(uint i)
{
if (error_ == 0 && check(size_ + i-1, get_capacity()) == 0)
size_ += i;
else
error_ = -1;
}
uint input_buffer::get_capacity() const
{
if (error_ == 0)
return end_ - buffer_;
return 0;
}
uint input_buffer::get_current() const
{
if (error_ == 0)
return current_;
return 0;
}
uint input_buffer::get_size() const
{
if (error_ == 0)
return size_;
return 0;
}
uint input_buffer::get_remaining() const
{
if (error_ == 0)
return size_ - current_;
return 0;
}
int input_buffer::get_error() const
{
return error_;
}
void input_buffer::set_error()
{
error_ = -1;
}
void input_buffer::set_current(uint i)
{
if (error_ == 0 && check(i ? i - 1 : 0, size_) == 0)
current_ = i;
else
error_ = -1;
}
// read only access through [], advance current
// user passes in AUTO index for ease of use
const byte& input_buffer::operator[](uint i)
{
if (error_ == 0 && check(current_, size_) == 0)
return buffer_[current_++];
error_ = -1;
return zero_;
}
// end of input test
bool input_buffer::eof()
{
if (error_ != 0)
return true;
return current_ >= size_;
}
// peek ahead
byte input_buffer::peek()
{
if (error_ == 0 && check(current_, size_) == 0)
return buffer_[current_];
error_ = -1;
return 0;
}
// write function, should use at/near construction
void input_buffer::assign(const byte* t, uint s)
{
if (t && error_ == 0 && check(current_, get_capacity()) == 0) {
add_size(s);
if (error_ == 0) {
memcpy(&buffer_[current_], t, s);
return; // success
}
}
error_ = -1;
}
// use read to query input, adjusts current
void input_buffer::read(byte* dst, uint length)
{
if (dst && error_ == 0 && check(current_ + length - 1, size_) == 0) {
memcpy(dst, &buffer_[current_], length);
current_ += length;
} else {
error_ = -1;
}
}
/* output_buffer operates like a smart c style array with a checking option.
* Meant to be written to through [] with AUTO index or write().
* Size (current) counter increases when written to. Can be constructed with
* zero length buffer but be sure to allocate before first use.
* Don't use add write for a couple bytes, use [] instead, way less overhead.
*
* Not using vector because need checked []access and the ability to
* write to the buffer bulk wise and retain correct size
*/
output_buffer::output_buffer()
: current_(0), buffer_(0), end_(0)
{}
// with allocate
output_buffer::output_buffer(uint s)
: current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s)
{}
// with assign
output_buffer::output_buffer(uint s, const byte* t, uint len)
: current_(0), buffer_(NEW_YS byte[s]), end_(buffer_+ s)
{
write(t, len);
}
output_buffer::~output_buffer()
{
ysArrayDelete(buffer_);
}
uint output_buffer::get_size() const
{
return current_;
}
uint output_buffer::get_capacity() const
{
return (uint) (end_ - buffer_);
}
void output_buffer::set_current(uint c)
{
check(c, get_capacity());
current_ = c;
}
// users can pass defualt zero length buffer and then allocate
void output_buffer::allocate(uint s)
{
buffer_ = NEW_YS byte[s]; end_ = buffer_ + s;
}
// for passing to reading functions when finished
const byte* output_buffer::get_buffer() const
{
return buffer_;
}
// allow write access through [], update current
// user passes in AUTO as index for ease of use
byte& output_buffer::operator[](uint i)
{
check(current_, get_capacity());
return buffer_[current_++];
}
// end of output test
bool output_buffer::eof()
{
return current_ >= get_capacity();
}
void output_buffer::write(const byte* t, uint s)
{
check(current_ + s - 1, get_capacity());
memcpy(&buffer_[current_], t, s);
current_ += s;
}
} // naemspace
|