GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: string_bytes.h Lines: 7 8 87.5 %
Date: 2022-05-31 04:15:47 Branches: 1 2 50.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_STRING_BYTES_H_
23
#define SRC_STRING_BYTES_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
// Decodes a v8::Local<v8::String> or Buffer to a raw char*
28
29
#include "v8.h"
30
#include "env-inl.h"
31
32
#include <string>
33
34
namespace node {
35
36
class StringBytes {
37
 public:
38
  class InlineDecoder : public MaybeStackBuffer<char> {
39
   public:
40
1622
    inline v8::Maybe<bool> Decode(Environment* env,
41
                                  v8::Local<v8::String> string,
42
                                  enum encoding enc) {
43
      size_t storage;
44
3244
      if (!StringBytes::StorageSize(env->isolate(), string, enc).To(&storage))
45
        return v8::Nothing<bool>();
46
1622
      AllocateSufficientStorage(storage);
47
      const size_t length =
48
1622
          StringBytes::Write(env->isolate(), out(), storage, string, enc);
49
50
      // No zero terminator is included when using this method.
51
1622
      SetLength(length);
52
1622
      return v8::Just(true);
53
    }
54
55
1622
    inline size_t size() const { return length(); }
56
  };
57
58
  // Fast, but can be 2 bytes oversized for Base64, and
59
  // as much as triple UTF-8 strings <= 65536 chars in length
60
  static v8::Maybe<size_t> StorageSize(v8::Isolate* isolate,
61
                                       v8::Local<v8::Value> val,
62
                                       enum encoding enc);
63
64
  // Precise byte count, but slightly slower for Base64 and
65
  // very much slower for UTF-8
66
  static v8::Maybe<size_t> Size(v8::Isolate* isolate,
67
                                v8::Local<v8::Value> val,
68
                                enum encoding enc);
69
70
  // Write the bytes from the string or buffer into the char*
71
  // returns the number of bytes written, which will always be
72
  // <= buflen.  Use StorageSize/Size first to know how much
73
  // memory to allocate.
74
  static size_t Write(v8::Isolate* isolate,
75
                      char* buf,
76
                      size_t buflen,
77
                      v8::Local<v8::Value> val,
78
                      enum encoding enc,
79
                      int* chars_written = nullptr);
80
81
  // Take the bytes in the src, and turn it into a Buffer or String.
82
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
83
                                          const char* buf,
84
                                          size_t buflen,
85
                                          enum encoding encoding,
86
                                          v8::Local<v8::Value>* error);
87
88
  // Warning: This reverses endianness on BE platforms, even though the
89
  // signature using uint16_t implies that it should not.
90
  // However, the brokenness is already public API and can't therefore
91
  // be changed easily.
92
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
93
                                          const uint16_t* buf,
94
                                          size_t buflen,
95
                                          v8::Local<v8::Value>* error);
96
97
  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
98
                                          const char* buf,
99
                                          enum encoding encoding,
100
                                          v8::Local<v8::Value>* error);
101
102
  static size_t hex_encode(const char* src,
103
                           size_t slen,
104
                           char* dst,
105
                           size_t dlen);
106
107
  static std::string hex_encode(const char* src, size_t slen);
108
109
 private:
110
  static size_t WriteUCS2(v8::Isolate* isolate,
111
                          char* buf,
112
                          size_t buflen,
113
                          v8::Local<v8::String> str,
114
                          int flags,
115
                          size_t* chars_written);
116
};
117
118
}  // namespace node
119
120
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
121
122
#endif  // SRC_STRING_BYTES_H_