• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if (__GNUC__ >= 8) && !defined(__clang__)
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wcast-function-type"
32 #endif
33 #include "v8.h"
34 #if (__GNUC__ >= 8) && !defined(__clang__)
35 #pragma GCC diagnostic pop
36 #endif
37 #include "env-inl.h"
38 
39 #include <string>
40 
41 namespace node {
42 
43 class StringBytes {
44  public:
45   class InlineDecoder : public MaybeStackBuffer<char> {
46    public:
Decode(Environment * env,v8::Local<v8::String> string,v8::Local<v8::Value> encoding,enum encoding _default)47     inline v8::Maybe<bool> Decode(Environment* env,
48                                   v8::Local<v8::String> string,
49                                   v8::Local<v8::Value> encoding,
50                                   enum encoding _default) {
51       enum encoding enc = ParseEncoding(env->isolate(), encoding, _default);
52       if (!StringBytes::IsValidString(string, enc)) {
53         env->ThrowTypeError("Bad input string");
54         return v8::Nothing<bool>();
55       }
56 
57       size_t storage;
58       if (!StringBytes::StorageSize(env->isolate(), string, enc).To(&storage))
59         return v8::Nothing<bool>();
60       AllocateSufficientStorage(storage);
61       const size_t length =
62           StringBytes::Write(env->isolate(), out(), storage, string, enc);
63 
64       // No zero terminator is included when using this method.
65       SetLength(length);
66       return v8::Just(true);
67     }
68 
size()69     inline size_t size() const { return length(); }
70   };
71 
72   // Does the string match the encoding? Quick but non-exhaustive.
73   // Example: a HEX string must have a length that's a multiple of two.
74   // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard...
75   static bool IsValidString(v8::Local<v8::String> string,
76                             enum encoding enc);
77 
78   // Fast, but can be 2 bytes oversized for Base64, and
79   // as much as triple UTF-8 strings <= 65536 chars in length
80   static v8::Maybe<size_t> StorageSize(v8::Isolate* isolate,
81                                        v8::Local<v8::Value> val,
82                                        enum encoding enc);
83 
84   // Precise byte count, but slightly slower for Base64 and
85   // very much slower for UTF-8
86   static v8::Maybe<size_t> Size(v8::Isolate* isolate,
87                                 v8::Local<v8::Value> val,
88                                 enum encoding enc);
89 
90   // Write the bytes from the string or buffer into the char*
91   // returns the number of bytes written, which will always be
92   // <= buflen.  Use StorageSize/Size first to know how much
93   // memory to allocate.
94   static size_t Write(v8::Isolate* isolate,
95                       char* buf,
96                       size_t buflen,
97                       v8::Local<v8::Value> val,
98                       enum encoding enc,
99                       int* chars_written = nullptr);
100 
101   // Take the bytes in the src, and turn it into a Buffer or String.
102   static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
103                                           const char* buf,
104                                           size_t buflen,
105                                           enum encoding encoding,
106                                           v8::Local<v8::Value>* error);
107 
108   // Warning: This reverses endianness on BE platforms, even though the
109   // signature using uint16_t implies that it should not.
110   // However, the brokenness is already public API and can't therefore
111   // be changed easily.
112   static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
113                                           const uint16_t* buf,
114                                           size_t buflen,
115                                           v8::Local<v8::Value>* error);
116 
117   static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
118                                           const char* buf,
119                                           enum encoding encoding,
120                                           v8::Local<v8::Value>* error);
121 
122   static size_t hex_encode(const char* src,
123                            size_t slen,
124                            char* dst,
125                            size_t dlen);
126 
127   static std::string hex_encode(const char* src, size_t slen);
128 
129  private:
130   static size_t WriteUCS2(v8::Isolate* isolate,
131                           char* buf,
132                           size_t buflen,
133                           v8::Local<v8::String> str,
134                           int flags,
135                           size_t* chars_written);
136 };
137 
138 }  // namespace node
139 
140 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
141 
142 #endif  // SRC_STRING_BYTES_H_
143