MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buffer.hpp
1 /*
2  Copyright (c) 2000, 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 defines input and output buffers to simulate streaming
21  * with SSL types and sockets
22  */
23 
24 #ifndef yaSSL_BUFFER_HPP
25 #define yaSSL_BUFFER_HPP
26 
27 #include <assert.h> // assert
28 #include "yassl_types.hpp" // ysDelete
29 #include "memory.hpp" // mySTL::auto_ptr
30 #include STL_ALGORITHM_FILE
31 
32 
33 namespace STL = STL_NAMESPACE;
34 
35 
36 #ifdef _MSC_VER
37  // disable truncated debug symbols
38  #pragma warning(disable:4786)
39 #endif
40 
41 
42 namespace yaSSL {
43 
44 typedef unsigned char byte;
45 typedef unsigned int uint;
46 const uint AUTO = 0xFEEDBEEF;
47 
48 
49 
50 struct NoCheck {
51  void check(uint, uint);
52 };
53 
54 /* input_buffer operates like a smart c style array with a checking option,
55  * meant to be read from through [] with AUTO index or read().
56  * Should only write to at/near construction with assign() or raw (e.g., recv)
57  * followed by add_size with the number of elements added by raw write.
58  *
59  * Not using vector because need checked []access, offset, and the ability to
60  * write to the buffer bulk wise and have the correct size
61  */
62 
63 class input_buffer : public NoCheck {
64  uint size_; // number of elements in buffer
65  uint current_; // current offset position in buffer
66  byte* buffer_; // storage for buffer
67  byte* end_; // end of storage marker
68 public:
69  input_buffer();
70 
71  explicit input_buffer(uint s);
72 
73  // with assign
74  input_buffer(uint s, const byte* t, uint len);
75 
76  ~input_buffer();
77 
78  // users can pass defualt zero length buffer and then allocate
79  void allocate(uint s);
80 
81  // for passing to raw writing functions at beginning, then use add_size
82  byte* get_buffer() const;
83 
84  // after a raw write user can set new size
85  // if you know the size before the write use assign()
86  void add_size(uint i);
87 
88  uint get_capacity() const;
89 
90  uint get_current() const;
91 
92  uint get_size() const;
93 
94  uint get_remaining() const;
95 
96  void set_current(uint i);
97 
98  // read only access through [], advance current
99  // user passes in AUTO index for ease of use
100  const byte& operator[](uint i);
101 
102  // end of input test
103  bool eof();
104 
105  // peek ahead
106  byte peek() const;
107 
108  // write function, should use at/near construction
109  void assign(const byte* t, uint s);
110 
111  // use read to query input, adjusts current
112  void read(byte* dst, uint length);
113 
114 private:
115  input_buffer(const input_buffer&); // hide copy
116  input_buffer& operator=(const input_buffer&); // and assign
117 };
118 
119 
120 /* output_buffer operates like a smart c style array with a checking option.
121  * Meant to be written to through [] with AUTO index or write().
122  * Size (current) counter increases when written to. Can be constructed with
123  * zero length buffer but be sure to allocate before first use.
124  * Don't use add write for a couple bytes, use [] instead, way less overhead.
125  *
126  * Not using vector because need checked []access and the ability to
127  * write to the buffer bulk wise and retain correct size
128  */
129 class output_buffer : public NoCheck {
130  uint current_; // current offset and elements in buffer
131  byte* buffer_; // storage for buffer
132  byte* end_; // end of storage marker
133 public:
134  // default
135  output_buffer();
136 
137  // with allocate
138  explicit output_buffer(uint s);
139 
140  // with assign
141  output_buffer(uint s, const byte* t, uint len);
142 
143  ~output_buffer();
144 
145  uint get_size() const;
146 
147  uint get_capacity() const;
148 
149  void set_current(uint c);
150 
151  // users can pass defualt zero length buffer and then allocate
152  void allocate(uint s);
153 
154  // for passing to reading functions when finished
155  const byte* get_buffer() const;
156 
157  // allow write access through [], update current
158  // user passes in AUTO as index for ease of use
159  byte& operator[](uint i);
160 
161  // end of output test
162  bool eof();
163 
164  void write(const byte* t, uint s);
165 
166 private:
167  output_buffer(const output_buffer&); // hide copy
168  output_buffer& operator=(const output_buffer&); // and assign
169 };
170 
171 
172 
173 
174 // turn delete an incomplete type into comipler error instead of warning
175 template <typename T>
176 inline void checked_delete(T* p)
177 {
178  typedef char complete_type[sizeof(T) ? 1 : -1];
179  (void)sizeof(complete_type);
180  ysDelete(p);
181 }
182 
183 
184 // checked delete functor increases effeciency, no indirection on function call
185 // sets pointer to zero so safe for std conatiners
187 {
188  template <typename T>
189  void operator()(T*& p) const
190  {
191  T* tmp = 0;
192  STL::swap(tmp, p);
193  checked_delete(tmp);
194  }
195 };
196 
197 
198 
199 } // naemspace
200 
201 #endif // yaSSL_BUUFER_HPP