GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_crypto_bio.h Lines: 28 28 100.0 %
Date: 2019-01-07 12:15:22 Branches: 5 6 83.3 %

Line Branch Exec Source
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
#ifndef SRC_NODE_CRYPTO_BIO_H_
23
#define SRC_NODE_CRYPTO_BIO_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "node_crypto.h"
28
#include "openssl/bio.h"
29
#include "env-inl.h"
30
#include "util-inl.h"
31
#include "v8.h"
32
33
namespace node {
34
namespace crypto {
35
36
// This class represents buffers for OpenSSL I/O, implemented as a singly-linked
37
// list of chunks. It can be used both for writing data from Node to OpenSSL
38
// and back, but only one direction per instance.
39
// The structure is only accessed, and owned by, the OpenSSL BIOPointer
40
// (a.k.a. std::unique_ptr<BIO>).
41
52496
class NodeBIO : public MemoryRetainer {
42
 public:
43
  ~NodeBIO();
44
45
  static BIOPointer New(Environment* env = nullptr);
46
47
  // NewFixed takes a copy of `len` bytes from `data` and returns a BIO that,
48
  // when read from, returns those bytes followed by EOF.
49
  static BIOPointer NewFixed(const char* data, size_t len,
50
                             Environment* env = nullptr);
51
52
  // Move read head to next buffer if needed
53
  void TryMoveReadHead();
54
55
  // Allocate new buffer for write if needed
56
  void TryAllocateForWrite(size_t hint);
57
58
  // Read `len` bytes maximum into `out`, return actual number of read bytes
59
  size_t Read(char* out, size_t size);
60
61
  // Memory optimization:
62
  // Deallocate children of write head's child if they're empty
63
  void FreeEmpty();
64
65
  // Return pointer to internal data and amount of
66
  // contiguous data available to read
67
  char* Peek(size_t* size);
68
69
  // Return pointers and sizes of multiple internal data chunks available for
70
  // reading
71
  size_t PeekMultiple(char** out, size_t* size, size_t* count);
72
73
  // Find first appearance of `delim` in buffer or `limit` if `delim`
74
  // wasn't found.
75
  size_t IndexOf(char delim, size_t limit);
76
77
  // Discard all available data
78
  void Reset();
79
80
  // Put `len` bytes from `data` into buffer
81
  void Write(const char* data, size_t size);
82
83
  // Return pointer to internal data and amount of
84
  // contiguous data available for future writes
85
  char* PeekWritable(size_t* size);
86
87
  // Commit reserved data
88
  void Commit(size_t size);
89
90
91
  // Return size of buffer in bytes
92
2902305
  inline size_t Length() const {
93
2902305
    return length_;
94
  }
95
96
29406
  inline void set_eof_return(int num) {
97
29406
    eof_return_ = num;
98
29406
  }
99
100
5844
  inline int eof_return() {
101
5844
    return eof_return_;
102
  }
103
104
11036
  inline void set_initial(size_t initial) {
105
11036
    initial_ = initial;
106
11036
  }
107
108
  static NodeBIO* FromBIO(BIO* bio);
109
110
4
  void MemoryInfo(MemoryTracker* tracker) const override {
111
4
    tracker->TrackFieldWithSize("buffer", length_, "NodeBIO::Buffer");
112
4
  }
113
114
4
  SET_MEMORY_INFO_NAME(NodeBIO)
115
4
  SET_SELF_SIZE(NodeBIO)
116
117
 private:
118
  static int New(BIO* bio);
119
  static int Free(BIO* bio);
120
  static int Read(BIO* bio, char* out, int len);
121
  static int Write(BIO* bio, const char* data, int len);
122
  static int Puts(BIO* bio, const char* str);
123
  static int Gets(BIO* bio, char* out, int size);
124
  static long Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
125
                   void* ptr);
126
127
  static const BIO_METHOD* GetMethod();
128
129
  // Enough to handle the most of the client hellos
130
  static const size_t kInitialBufferLength = 1024;
131
  static const size_t kThroughputBufferLength = 16384;
132
133
  class Buffer {
134
   public:
135
35311
    Buffer(Environment* env, size_t len) : env_(env),
136
                                           read_pos_(0),
137
                                           write_pos_(0),
138
                                           len_(len),
139
35311
                                           next_(nullptr) {
140
35311
      data_ = new char[len];
141
35311
      if (env_ != nullptr)
142
5905
        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
143
35311
    }
144
145
35290
    ~Buffer() {
146
35290
      delete[] data_;
147
35290
      if (env_ != nullptr) {
148
5884
        const int64_t len = static_cast<int64_t>(len_);
149
5884
        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len);
150
      }
151
35290
    }
152
153
    Environment* env_;
154
    size_t read_pos_;
155
    size_t write_pos_;
156
    size_t len_;
157
    Buffer* next_;
158
    char* data_;
159
  };
160
161
  Environment* env_ = nullptr;
162
  size_t initial_ = kInitialBufferLength;
163
  size_t length_ = 0;
164
  int eof_return_ = -1;
165
  Buffer* read_head_ = nullptr;
166
  Buffer* write_head_ = nullptr;
167
168
  friend void node::crypto::InitCryptoOnce();
169
};
170
171
}  // namespace crypto
172
}  // namespace node
173
174
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
175
176
#endif  // SRC_NODE_CRYPTO_BIO_H_