GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_crypto_bio.h Lines: 28 28 100.0 %
Date: 2019-09-17 22:33:17 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.h"
30
#include "util.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 either for writing data from Node to OpenSSL,
38
// or for reading data back, but not both.
39
// The structure is only accessed, and owned by, the OpenSSL BIOPointer
40
// (a.k.a. std::unique_ptr<BIO>).
41
68141
class NodeBIO : public MemoryRetainer {
42
 public:
43
  ~NodeBIO() override;
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 contiguous block of reserved data and the size available
84
  // for future writes. Call Commit() once the write is complete.
85
  char* PeekWritable(size_t* size);
86
87
  // Specify how much data was written into the block returned by
88
  // PeekWritable().
89
  void Commit(size_t size);
90
91
92
  // Return size of buffer in bytes
93
4317886
  inline size_t Length() const {
94
4317886
    return length_;
95
  }
96
97
43641
  inline void set_eof_return(int num) {
98
43641
    eof_return_ = num;
99
43641
  }
100
101
10187
  inline int eof_return() {
102
10187
    return eof_return_;
103
  }
104
105
11433
  inline void set_initial(size_t initial) {
106
11433
    initial_ = initial;
107
11433
  }
108
109
  static NodeBIO* FromBIO(BIO* bio);
110
111
4
  void MemoryInfo(MemoryTracker* tracker) const override {
112
4
    tracker->TrackFieldWithSize("buffer", length_, "NodeBIO::Buffer");
113
4
  }
114
115
4
  SET_MEMORY_INFO_NAME(NodeBIO)
116
4
  SET_SELF_SIZE(NodeBIO)
117
118
 private:
119
  static int New(BIO* bio);
120
  static int Free(BIO* bio);
121
  static int Read(BIO* bio, char* out, int len);
122
  static int Write(BIO* bio, const char* data, int len);
123
  static int Puts(BIO* bio, const char* str);
124
  static int Gets(BIO* bio, char* out, int size);
125
  static long Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
126
                   void* ptr);
127
128
  static const BIO_METHOD* GetMethod();
129
130
  // Enough to handle the most of the client hellos
131
  static const size_t kInitialBufferLength = 1024;
132
  static const size_t kThroughputBufferLength = 16384;
133
134
  class Buffer {
135
   public:
136
57891
    Buffer(Environment* env, size_t len) : env_(env),
137
                                           read_pos_(0),
138
                                           write_pos_(0),
139
                                           len_(len),
140
57891
                                           next_(nullptr) {
141
57891
      data_ = new char[len];
142
57891
      if (env_ != nullptr)
143
14250
        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
144
57891
    }
145
146
55070
    ~Buffer() {
147
55070
      delete[] data_;
148
55070
      if (env_ != nullptr) {
149
11429
        const int64_t len = static_cast<int64_t>(len_);
150
11429
        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len);
151
      }
152
55070
    }
153
154
    Environment* env_;
155
    size_t read_pos_;
156
    size_t write_pos_;
157
    size_t len_;
158
    Buffer* next_;
159
    char* data_;
160
  };
161
162
  Environment* env_ = nullptr;
163
  size_t initial_ = kInitialBufferLength;
164
  size_t length_ = 0;
165
  int eof_return_ = -1;
166
  Buffer* read_head_ = nullptr;
167
  Buffer* write_head_ = nullptr;
168
169
  friend void node::crypto::InitCryptoOnce();
170
};
171
172
}  // namespace crypto
173
}  // namespace node
174
175
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
176
177
#endif  // SRC_NODE_CRYPTO_BIO_H_