1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef SRC_NODE_CRYPTO_BIO_H_
23 #define SRC_NODE_CRYPTO_BIO_H_
25 #include "openssl/bio.h"
32 static inline BIO_METHOD* GetMethod() {
36 NodeBIO() : length_(0), read_head_(&head_), write_head_(&head_) {
43 static int New(BIO* bio);
44 static int Free(BIO* bio);
45 static int Read(BIO* bio, char* out, int len);
46 static int Write(BIO* bio, const char* data, int len);
47 static int Puts(BIO* bio, const char* str);
48 static int Gets(BIO* bio, char* out, int size);
49 static long Ctrl(BIO* bio, int cmd, long num, void* ptr);
51 // Allocate new buffer for write if needed
52 void TryAllocateForWrite();
54 // Read `len` bytes maximum into `out`, return actual number of read bytes
55 size_t Read(char* out, size_t size);
57 // Memory optimization:
58 // Deallocate children of write head's child if they're empty
61 // Return pointer to internal data and amount of
62 // contiguous data available to read
63 char* Peek(size_t* size);
65 // Find first appearance of `delim` in buffer or `limit` if `delim`
67 size_t IndexOf(char delim, size_t limit);
69 // Discard all available data
72 // Put `len` bytes from `data` into buffer
73 void Write(const char* data, size_t size);
75 // Return pointer to internal data and amount of
76 // contiguous data available for future writes
77 char* PeekWritable(size_t* size);
79 // Commit reserved data
80 void Commit(size_t size);
82 // Return size of buffer in bytes
83 size_t inline Length() {
87 static inline NodeBIO* FromBIO(BIO* bio) {
88 assert(bio->ptr != NULL);
89 return static_cast<NodeBIO*>(bio->ptr);
93 // NOTE: Size is maximum TLS frame length, this is required if we want
94 // to fit whole ClientHello into one Buffer of NodeBIO.
95 static const size_t kBufferLength = 16 * 1024 + 5;
99 Buffer() : read_pos_(0), write_pos_(0), next_(NULL) {
105 char data_[kBufferLength];
113 static BIO_METHOD method_;
118 #endif // SRC_NODE_CRYPTO_BIO_H_