GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/node_crypto_bio.h Lines: 15 15 100.0 %
Date: 2016-11-30 Branches: 13 22 59.1 %

Line Branch Exec Source
1
#ifndef SRC_NODE_CRYPTO_BIO_H_
2
#define SRC_NODE_CRYPTO_BIO_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "openssl/bio.h"
7
#include "env.h"
8
#include "env-inl.h"
9
#include "util.h"
10
#include "util-inl.h"
11
#include "v8.h"
12
13
namespace node {
14
15
class NodeBIO {
16
 public:
17
  NodeBIO() : env_(nullptr),
18
              initial_(kInitialBufferLength),
19
              length_(0),
20
              read_head_(nullptr),
21
22009
              write_head_(nullptr) {
22
  }
23
24
  ~NodeBIO();
25
26
  static BIO* New();
27
28
  // NewFixed takes a copy of `len` bytes from `data` and returns a BIO that,
29
  // when read from, returns those bytes followed by EOF.
30
  static BIO* NewFixed(const char* data, size_t len);
31
32
  void AssignEnvironment(Environment* env);
33
34
  // Move read head to next buffer if needed
35
  void TryMoveReadHead();
36
37
  // Allocate new buffer for write if needed
38
  void TryAllocateForWrite(size_t hint);
39
40
  // Read `len` bytes maximum into `out`, return actual number of read bytes
41
  size_t Read(char* out, size_t size);
42
43
  // Memory optimization:
44
  // Deallocate children of write head's child if they're empty
45
  void FreeEmpty();
46
47
  // Return pointer to internal data and amount of
48
  // contiguous data available to read
49
  char* Peek(size_t* size);
50
51
  // Return pointers and sizes of multiple internal data chunks available for
52
  // reading
53
  size_t PeekMultiple(char** out, size_t* size, size_t* count);
54
55
  // Find first appearance of `delim` in buffer or `limit` if `delim`
56
  // wasn't found.
57
  size_t IndexOf(char delim, size_t limit);
58
59
  // Discard all available data
60
  void Reset();
61
62
  // Put `len` bytes from `data` into buffer
63
  void Write(const char* data, size_t size);
64
65
  // Return pointer to internal data and amount of
66
  // contiguous data available for future writes
67
  char* PeekWritable(size_t* size);
68
69
  // Commit reserved data
70
  void Commit(size_t size);
71
72
73
  // Return size of buffer in bytes
74
  inline size_t Length() const {
75
    return length_;
76
  }
77
78
  inline void set_initial(size_t initial) {
79
296
    initial_ = initial;
80
  }
81
82
  static inline NodeBIO* FromBIO(BIO* bio) {
83




572365
    CHECK_NE(bio->ptr, nullptr);
84
    return static_cast<NodeBIO*>(bio->ptr);
85
  }
86
87
 private:
88
  static int New(BIO* bio);
89
  static int Free(BIO* bio);
90
  static int Read(BIO* bio, char* out, int len);
91
  static int Write(BIO* bio, const char* data, int len);
92
  static int Puts(BIO* bio, const char* str);
93
  static int Gets(BIO* bio, char* out, int size);
94
  static long Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
95
                   void* ptr);
96
97
  // Enough to handle the most of the client hellos
98
  static const size_t kInitialBufferLength = 1024;
99
  static const size_t kThroughputBufferLength = 16384;
100
101
  static const BIO_METHOD method;
102
103
  class Buffer {
104
   public:
105
21759
    Buffer(Environment* env, size_t len) : env_(env),
106
                                           read_pos_(0),
107
                                           write_pos_(0),
108
                                           len_(len),
109
21759
                                           next_(nullptr) {
110
21759
      data_ = new char[len];
111
21759
      if (env_ != nullptr)
112
1566
        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
113
21759
    }
114
115
43460
    ~Buffer() {
116
21730
      delete[] data_;
117
21730
      if (env_ != nullptr) {
118
1541
        const int64_t len = static_cast<int64_t>(len_);
119
1541
        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len);
120
      }
121
21730
    }
122
123
    Environment* env_;
124
    size_t read_pos_;
125
    size_t write_pos_;
126
    size_t len_;
127
    Buffer* next_;
128
    char* data_;
129
  };
130
131
  Environment* env_;
132
  size_t initial_;
133
  size_t length_;
134
  Buffer* read_head_;
135
  Buffer* write_head_;
136
};
137
138
}  // namespace node
139
140
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
141
142
#endif  // SRC_NODE_CRYPTO_BIO_H_