• 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 #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:
Decode(Environment * env,v8::Local<v8::String> string,enum encoding enc)40     inline v8::Maybe<bool> Decode(Environment* env,
41                                   v8::Local<v8::String> string,
42                                   enum encoding enc) {
43       size_t storage;
44       if (!StringBytes::StorageSize(env->isolate(), string, enc).To(&storage))
45         return v8::Nothing<bool>();
46       AllocateSufficientStorage(storage);
47       const size_t length =
48           StringBytes::Write(env->isolate(), out(), storage, string, enc);
49 
50       // No zero terminator is included when using this method.
51       SetLength(length);
52       return v8::Just(true);
53     }
54 
size()55     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 
80   // Take the bytes in the src, and turn it into a Buffer or String.
81   static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
82                                           const char* buf,
83                                           size_t buflen,
84                                           enum encoding encoding,
85                                           v8::Local<v8::Value>* error);
86 
87   // Warning: This reverses endianness on BE platforms, even though the
88   // signature using uint16_t implies that it should not.
89   // However, the brokenness is already public API and can't therefore
90   // be changed easily.
91   static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
92                                           const uint16_t* buf,
93                                           size_t buflen,
94                                           v8::Local<v8::Value>* error);
95 
96   static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
97                                           const char* buf,
98                                           enum encoding encoding,
99                                           v8::Local<v8::Value>* error);
100 
101   static size_t hex_encode(const char* src,
102                            size_t slen,
103                            char* dst,
104                            size_t dlen);
105 
106   static std::string hex_encode(const char* src, size_t slen);
107 
108  private:
109   static size_t WriteUCS2(v8::Isolate* isolate,
110                           char* buf,
111                           size_t buflen,
112                           v8::Local<v8::String> str,
113                           int flags);
114 };
115 
116 }  // namespace node
117 
118 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
119 
120 #endif  // SRC_STRING_BYTES_H_
121