buffer.cpp 5.78 KB
Newer Older
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
/* buffer.cpp                               
 *
 * Copyright (C) 2003 Sawtooth Consulting Ltd.
 *
 * This file is part of yaSSL.
 *
 * yaSSL 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 2 of the License, or
 * (at your option) any later version.
 *
 * yaSSL 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */


/* yaSSL buffer header implements input/output buffers to simulate streaming
 * with SSL types and sockets
 */

#include "buffer.hpp"
#include "yassl_types.hpp"

namespace yaSSL {



// Checking Policy should implement a check function that tests whether the
// index is within the size limit of the array

void Check::check(uint i, uint limit) 
{ 
    assert(i < limit);
}


void NoCheck::check(uint, uint) 
{
}


/* 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) 
{}


input_buffer::input_buffer(uint s) 
64
    : size_(0), current_(0), buffer_(new (ys) byte[s]), end_(buffer_ + s)
65 66 67 68 69
{}


// with assign
input_buffer::input_buffer(uint s, const byte* t, uint len) 
70
    : size_(0), current_(0), buffer_(new (ys) byte[s]), end_(buffer_ + s) 
71 72 73 74 75 76 77
{ 
    assign(t, len); 
}


input_buffer::~input_buffer() 
{ 
78
    ysArrayDelete(buffer_); 
79 80 81 82 83 84 85
}


// users can pass defualt zero length buffer and then allocate
void input_buffer::allocate(uint s) 
{ 
    assert(!buffer_);       // find realloc error
86
    buffer_ = new (ys) byte[s];
87 88 89 90 91 92 93 94 95 96 97
    end_ = buffer_ + s; 
}


// for passing to raw writing functions at beginning, then use add_size
byte* input_buffer::get_buffer() const 
{ 
    return buffer_; 
}


98
// after a raw write user can set new (ys) size
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
// if you know the size before the write use assign()
void input_buffer::add_size(uint i) 
{ 
    check(size_ + i-1, get_capacity()); 
    size_ += i; 
}


uint input_buffer::get_capacity()  const 
{ 
    return end_ - buffer_; 
}


uint input_buffer::get_current()   const 
{ 
    return current_; 
}


uint input_buffer::get_size()      const 
{ 
    return size_; 
}


uint input_buffer::get_remaining() const 
{ 
    return size_ - current_; 
}


void input_buffer::set_current(uint i) 
{
    if (i)
        check(i - 1, size_); 
    current_ = i; 
}


// read only access through [], advance current
// user passes in AUTO index for ease of use
const byte& input_buffer::operator[](uint i) 
{
    assert (i == AUTO);
    check(current_, size_);
    return buffer_[current_++];
}


// end of input test
bool input_buffer::eof() 
{ 
    return current_ >= size_; 
}


// peek ahead
byte input_buffer::peek() const
{
    return buffer_[current_];
}


// write function, should use at/near construction
void input_buffer::assign(const byte* t, uint s)
{
    check(current_, get_capacity());
    add_size(s);
    memcpy(&buffer_[current_], t, s);
}


// use read to query input, adjusts current
void input_buffer::read(byte* dst, uint length)
{
    check(current_ + length - 1, size_);
    memcpy(dst, &buffer_[current_], length);
    current_ += length;
}



/* 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) 
200
    : current_(0), buffer_(new (ys) byte[s]), end_(buffer_ + s) 
201 202 203 204 205
{}


// with assign
output_buffer::output_buffer(uint s, const byte* t, uint len) 
206
    : current_(0), buffer_(new (ys) byte[s]), end_(buffer_+ s) 
207 208 209 210 211 212 213
{ 
    write(t, len); 
}


output_buffer::~output_buffer() 
{ 
214
    ysArrayDelete(buffer_); 
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
}


uint output_buffer::get_size() const 
{ 
    return current_; 
}


uint output_buffer::get_capacity() const 
{ 
    return 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) 
{ 
    assert(!buffer_);   // find realloc error
241
    buffer_ = new (ys) byte[s]; end_ = buffer_ + s; 
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
}


// 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) 
{
    assert(i == AUTO);
    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