Merge remote-tracking branch 'ry/v0.10'
[platform/upstream/nodejs.git] / src / node_crypto_bio.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #include "openssl/bio.h"
23 #include <assert.h>
24
25 namespace node {
26
27 class NodeBIO {
28  public:
29   static inline BIO_METHOD* GetMethod() {
30     return &method_;
31   }
32
33   static int New(BIO* bio);
34   static int Free(BIO* bio);
35   static int Read(BIO* bio, char* out, int len);
36   static int Write(BIO* bio, const char* data, int len);
37   static int Puts(BIO* bio, const char* str);
38   static int Gets(BIO* bio, char* out, int size);
39   static long Ctrl(BIO* bio, int cmd, long num, void* ptr);
40
41  protected:
42   static const size_t kBufferLength = 16 * 1024;
43
44   class Buffer {
45    public:
46     Buffer() : read_pos_(0), write_pos_(0), next_(NULL) {
47     }
48
49     size_t read_pos_;
50     size_t write_pos_;
51     Buffer* next_;
52     char data_[kBufferLength];
53   };
54
55   NodeBIO() : length_(0), read_head_(&head_), write_head_(&head_) {
56     // Loop head
57     head_.next_ = &head_;
58   }
59
60   ~NodeBIO();
61
62   // Read `len` bytes maximum into `out`, return actual number of read bytes
63   size_t Read(char* out, size_t size);
64
65   // Find first appearance of `delim` in buffer or `limit` if `delim`
66   // wasn't found.
67   size_t IndexOf(char delim, size_t limit);
68
69   // Discard all available data
70   void Reset();
71
72   // Put `len` bytes from `data` into buffer
73   void Write(const char* data, size_t size);
74
75   // Return size of buffer in bytes
76   size_t inline Length() {
77     return length_;
78   }
79
80   static inline NodeBIO* FromBIO(BIO* bio) {
81     assert(bio->ptr != NULL);
82     return static_cast<NodeBIO*>(bio->ptr);
83   }
84
85   size_t length_;
86   Buffer head_;
87   Buffer* read_head_;
88   Buffer* write_head_;
89
90   static BIO_METHOD method_;
91 };
92
93 } // namespace node