MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buffer.cpp
1 /*
2  Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 
20 /* yaSSL buffer header implements input/output buffers to simulate streaming
21  * with SSL types and sockets
22  */
23 
24 
25 // First include (the generated) my_config.h, to get correct platform defines.
26 #include "my_config.h"
27 #include <string.h> // memcpy
28 #include "runtime.hpp"
29 #include "buffer.hpp"
30 #include "yassl_types.hpp"
31 
32 namespace yaSSL {
33 
34 
35 
36 
37 void NoCheck::check(uint, uint)
38 {
39 }
40 
41 
42 /* input_buffer operates like a smart c style array with a checking option,
43  * meant to be read from through [] with AUTO index or read().
44  * Should only write to at/near construction with assign() or raw (e.g., recv)
45  * followed by add_size with the number of elements added by raw write.
46  *
47  * Not using vector because need checked []access, offset, and the ability to
48  * write to the buffer bulk wise and have the correct size
49  */
50 
51 
52 input_buffer::input_buffer()
53  : size_(0), current_(0), buffer_(0), end_(0)
54 {}
55 
56 
57 input_buffer::input_buffer(uint s)
58  : size_(0), current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s)
59 {}
60 
61 
62 // with assign
63 input_buffer::input_buffer(uint s, const byte* t, uint len)
64  : size_(0), current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s)
65 {
66  assign(t, len);
67 }
68 
69 
70 input_buffer::~input_buffer()
71 {
72  ysArrayDelete(buffer_);
73 }
74 
75 
76 // users can pass defualt zero length buffer and then allocate
77 void input_buffer::allocate(uint s)
78 {
79  buffer_ = NEW_YS byte[s];
80  end_ = buffer_ + s;
81 }
82 
83 
84 // for passing to raw writing functions at beginning, then use add_size
85 byte* input_buffer::get_buffer() const
86 {
87  return buffer_;
88 }
89 
90 
91 // after a raw write user can set NEW_YS size
92 // if you know the size before the write use assign()
93 void input_buffer::add_size(uint i)
94 {
95  check(size_ + i-1, get_capacity());
96  size_ += i;
97 }
98 
99 
100 uint input_buffer::get_capacity() const
101 {
102  return (uint) (end_ - buffer_);
103 }
104 
105 
106 uint input_buffer::get_current() const
107 {
108  return current_;
109 }
110 
111 
112 uint input_buffer::get_size() const
113 {
114  return size_;
115 }
116 
117 
118 uint input_buffer::get_remaining() const
119 {
120  return size_ - current_;
121 }
122 
123 
124 void input_buffer::set_current(uint i)
125 {
126  if (i)
127  check(i - 1, size_);
128  current_ = i;
129 }
130 
131 
132 // read only access through [], advance current
133 // user passes in AUTO index for ease of use
134 const byte& input_buffer::operator[](uint i)
135 {
136  check(current_, size_);
137  return buffer_[current_++];
138 }
139 
140 
141 // end of input test
142 bool input_buffer::eof()
143 {
144  return current_ >= size_;
145 }
146 
147 
148 // peek ahead
149 byte input_buffer::peek() const
150 {
151  return buffer_[current_];
152 }
153 
154 
155 // write function, should use at/near construction
156 void input_buffer::assign(const byte* t, uint s)
157 {
158  check(current_, get_capacity());
159  add_size(s);
160  memcpy(&buffer_[current_], t, s);
161 }
162 
163 
164 // use read to query input, adjusts current
165 void input_buffer::read(byte* dst, uint length)
166 {
167  check(current_ + length - 1, size_);
168  memcpy(dst, &buffer_[current_], length);
169  current_ += length;
170 }
171 
172 
173 
174 /* output_buffer operates like a smart c style array with a checking option.
175  * Meant to be written to through [] with AUTO index or write().
176  * Size (current) counter increases when written to. Can be constructed with
177  * zero length buffer but be sure to allocate before first use.
178  * Don't use add write for a couple bytes, use [] instead, way less overhead.
179  *
180  * Not using vector because need checked []access and the ability to
181  * write to the buffer bulk wise and retain correct size
182  */
183 
184 
185 output_buffer::output_buffer()
186  : current_(0), buffer_(0), end_(0)
187 {}
188 
189 
190 // with allocate
191 output_buffer::output_buffer(uint s)
192  : current_(0), buffer_(NEW_YS byte[s]), end_(buffer_ + s)
193 {}
194 
195 
196 // with assign
197 output_buffer::output_buffer(uint s, const byte* t, uint len)
198  : current_(0), buffer_(NEW_YS byte[s]), end_(buffer_+ s)
199 {
200  write(t, len);
201 }
202 
203 
204 output_buffer::~output_buffer()
205 {
206  ysArrayDelete(buffer_);
207 }
208 
209 
210 uint output_buffer::get_size() const
211 {
212  return current_;
213 }
214 
215 
216 uint output_buffer::get_capacity() const
217 {
218  return (uint) (end_ - buffer_);
219 }
220 
221 
222 void output_buffer::set_current(uint c)
223 {
224  check(c, get_capacity());
225  current_ = c;
226 }
227 
228 
229 // users can pass defualt zero length buffer and then allocate
230 void output_buffer::allocate(uint s)
231 {
232  buffer_ = NEW_YS byte[s]; end_ = buffer_ + s;
233 }
234 
235 
236 // for passing to reading functions when finished
237 const byte* output_buffer::get_buffer() const
238 {
239  return buffer_;
240 }
241 
242 
243 // allow write access through [], update current
244 // user passes in AUTO as index for ease of use
245 byte& output_buffer::operator[](uint i)
246 {
247  check(current_, get_capacity());
248  return buffer_[current_++];
249 }
250 
251 
252 // end of output test
253 bool output_buffer::eof()
254 {
255  return current_ >= get_capacity();
256 }
257 
258 
259 void output_buffer::write(const byte* t, uint s)
260 {
261  check(current_ + s - 1, get_capacity());
262  memcpy(&buffer_[current_], t, s);
263  current_ += s;
264 }
265 
266 
267 
268 } // naemspace
269