1 #ifndef SRC_NODE_CRYPTO_BIO_H_
2 #define SRC_NODE_CRYPTO_BIO_H_
4 #include "openssl/bio.h"
15 NodeBIO() : env_(nullptr),
16 initial_(kInitialBufferLength),
19 write_head_(nullptr) {
26 void AssignEnvironment(Environment* env);
28 // Move read head to next buffer if needed
29 void TryMoveReadHead();
31 // Allocate new buffer for write if needed
32 void TryAllocateForWrite(size_t hint);
34 // Read `len` bytes maximum into `out`, return actual number of read bytes
35 size_t Read(char* out, size_t size);
37 // Memory optimization:
38 // Deallocate children of write head's child if they're empty
41 // Return pointer to internal data and amount of
42 // contiguous data available to read
43 char* Peek(size_t* size);
45 // Return pointers and sizes of multiple internal data chunks available for
47 size_t PeekMultiple(char** out, size_t* size, size_t* count);
49 // Find first appearance of `delim` in buffer or `limit` if `delim`
51 size_t IndexOf(char delim, size_t limit);
53 // Discard all available data
56 // Put `len` bytes from `data` into buffer
57 void Write(const char* data, size_t size);
59 // Return pointer to internal data and amount of
60 // contiguous data available for future writes
61 char* PeekWritable(size_t* size);
63 // Commit reserved data
64 void Commit(size_t size);
67 // Return size of buffer in bytes
68 inline size_t Length() const {
72 inline void set_initial(size_t initial) {
76 static inline NodeBIO* FromBIO(BIO* bio) {
77 CHECK_NE(bio->ptr, nullptr);
78 return static_cast<NodeBIO*>(bio->ptr);
82 static int New(BIO* bio);
83 static int Free(BIO* bio);
84 static int Read(BIO* bio, char* out, int len);
85 static int Write(BIO* bio, const char* data, int len);
86 static int Puts(BIO* bio, const char* str);
87 static int Gets(BIO* bio, char* out, int size);
88 static long Ctrl(BIO* bio, int cmd, long num, void* ptr);
90 // Enough to handle the most of the client hellos
91 static const size_t kInitialBufferLength = 1024;
92 static const size_t kThroughputBufferLength = 16384;
94 static const BIO_METHOD method;
98 Buffer(Environment* env, size_t len) : env_(env),
103 data_ = new char[len];
105 env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
110 if (env_ != nullptr) {
111 const int64_t len = static_cast<int64_t>(len_);
112 env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len);
133 #endif // SRC_NODE_CRYPTO_BIO_H_