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: 23 23 100.0 %
Date: 2017-06-14 Branches: 6 8 75.0 %

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 "openssl/bio.h"
28
#include "env.h"
29
#include "env-inl.h"
30
#include "util.h"
31
#include "util-inl.h"
32
#include "v8.h"
33
34
namespace node {
35
36
class NodeBIO {
37
 public:
38
46729
  NodeBIO() : env_(nullptr),
39
              initial_(kInitialBufferLength),
40
              length_(0),
41
              read_head_(nullptr),
42
46729
              write_head_(nullptr) {
43
46729
  }
44
45
  ~NodeBIO();
46
47
  static BIO* New();
48
49
  // NewFixed takes a copy of `len` bytes from `data` and returns a BIO that,
50
  // when read from, returns those bytes followed by EOF.
51
  static BIO* NewFixed(const char* data, size_t len);
52
53
  void AssignEnvironment(Environment* env);
54
55
  // Move read head to next buffer if needed
56
  void TryMoveReadHead();
57
58
  // Allocate new buffer for write if needed
59
  void TryAllocateForWrite(size_t hint);
60
61
  // Read `len` bytes maximum into `out`, return actual number of read bytes
62
  size_t Read(char* out, size_t size);
63
64
  // Memory optimization:
65
  // Deallocate children of write head's child if they're empty
66
  void FreeEmpty();
67
68
  // Return pointer to internal data and amount of
69
  // contiguous data available to read
70
  char* Peek(size_t* size);
71
72
  // Return pointers and sizes of multiple internal data chunks available for
73
  // reading
74
  size_t PeekMultiple(char** out, size_t* size, size_t* count);
75
76
  // Find first appearance of `delim` in buffer or `limit` if `delim`
77
  // wasn't found.
78
  size_t IndexOf(char delim, size_t limit);
79
80
  // Discard all available data
81
  void Reset();
82
83
  // Put `len` bytes from `data` into buffer
84
  void Write(const char* data, size_t size);
85
86
  // Return pointer to internal data and amount of
87
  // contiguous data available for future writes
88
  char* PeekWritable(size_t* size);
89
90
  // Commit reserved data
91
  void Commit(size_t size);
92
93
94
  // Return size of buffer in bytes
95
2450226
  inline size_t Length() const {
96
2450226
    return length_;
97
  }
98
99
364
  inline void set_initial(size_t initial) {
100
364
    initial_ = initial;
101
364
  }
102
103
705085
  static inline NodeBIO* FromBIO(BIO* bio) {
104
705085
    CHECK_NE(bio->ptr, nullptr);
105
705085
    return static_cast<NodeBIO*>(bio->ptr);
106
  }
107
108
 private:
109
  static int New(BIO* bio);
110
  static int Free(BIO* bio);
111
  static int Read(BIO* bio, char* out, int len);
112
  static int Write(BIO* bio, const char* data, int len);
113
  static int Puts(BIO* bio, const char* str);
114
  static int Gets(BIO* bio, char* out, int size);
115
  static long Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
116
                   void* ptr);
117
118
  // Enough to handle the most of the client hellos
119
  static const size_t kInitialBufferLength = 1024;
120
  static const size_t kThroughputBufferLength = 16384;
121
122
  static const BIO_METHOD method;
123
124
  class Buffer {
125
   public:
126
26413
    Buffer(Environment* env, size_t len) : env_(env),
127
                                           read_pos_(0),
128
                                           write_pos_(0),
129
                                           len_(len),
130
26413
                                           next_(nullptr) {
131
26413
      data_ = new char[len];
132
26413
      if (env_ != nullptr)
133
1876
        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
134
26413
    }
135
136
26380
    ~Buffer() {
137
26380
      delete[] data_;
138
26380
      if (env_ != nullptr) {
139
1849
        const int64_t len = static_cast<int64_t>(len_);
140
1849
        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len);
141
      }
142
26380
    }
143
144
    Environment* env_;
145
    size_t read_pos_;
146
    size_t write_pos_;
147
    size_t len_;
148
    Buffer* next_;
149
    char* data_;
150
  };
151
152
  Environment* env_;
153
  size_t initial_;
154
  size_t length_;
155
  Buffer* read_head_;
156
  Buffer* write_head_;
157
};
158
159
}  // namespace node
160
161
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
162
163
#endif  // SRC_NODE_CRYPTO_BIO_H_