Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
string_bytes.h
Go to the documentation of this file.
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 "node.h"
31 #include "env.h"
32 #include "env-inl.h"
33 #include "util.h"
34 
35 namespace node {
36 
37 class StringBytes {
38  public:
39  class InlineDecoder : public MaybeStackBuffer<char> {
40  public:
41  inline bool Decode(Environment* env,
42  v8::Local<v8::String> string,
43  v8::Local<v8::Value> encoding,
44  enum encoding _default) {
45  enum encoding enc = ParseEncoding(env->isolate(), encoding, _default);
46  if (!StringBytes::IsValidString(string, enc)) {
47  env->ThrowTypeError("Bad input string");
48  return false;
49  }
50 
51  const size_t storage = StringBytes::StorageSize(env->isolate(),
52  string,
53  enc);
54  AllocateSufficientStorage(storage);
55  const size_t length = StringBytes::Write(env->isolate(),
56  out(),
57  storage,
58  string,
59  enc);
60 
61  // No zero terminator is included when using this method.
62  SetLength(length);
63  return true;
64  }
65 
66  inline size_t size() const { return length(); }
67  };
68 
69  // Does the string match the encoding? Quick but non-exhaustive.
70  // Example: a HEX string must have a length that's a multiple of two.
71  // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard...
72  static bool IsValidString(v8::Local<v8::String> string,
73  enum encoding enc);
74 
75  // Fast, but can be 2 bytes oversized for Base64, and
76  // as much as triple UTF-8 strings <= 65536 chars in length
77  static size_t StorageSize(v8::Isolate* isolate,
78  v8::Local<v8::Value> val,
79  enum encoding enc);
80 
81  // Precise byte count, but slightly slower for Base64 and
82  // very much slower for UTF-8
83  static size_t Size(v8::Isolate* isolate,
84  v8::Local<v8::Value> val,
85  enum encoding enc);
86 
87  // If the string is external then assign external properties to data and len,
88  // then return true. If not return false.
89  static bool GetExternalParts(v8::Local<v8::Value> val,
90  const char** data,
91  size_t* len);
92 
93  // Write the bytes from the string or buffer into the char*
94  // returns the number of bytes written, which will always be
95  // <= buflen. Use StorageSize/Size first to know how much
96  // memory to allocate.
97  static size_t Write(v8::Isolate* isolate,
98  char* buf,
99  size_t buflen,
100  v8::Local<v8::Value> val,
101  enum encoding enc,
102  int* chars_written = nullptr);
103 
104  // Take the bytes in the src, and turn it into a Buffer or String.
105  // Don't call with encoding=UCS2.
106  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
107  const char* buf,
108  size_t buflen,
109  enum encoding encoding,
110  v8::Local<v8::Value>* error);
111 
112  // The input buffer should be in host endianness.
113  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
114  const uint16_t* buf,
115  size_t buflen,
116  v8::Local<v8::Value>* error);
117 
118  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
119  const char* buf,
120  enum encoding encoding,
121  v8::Local<v8::Value>* error);
122 
123  private:
124  static size_t WriteUCS2(char* buf,
125  size_t buflen,
126  v8::Local<v8::String> str,
127  int flags,
128  size_t* chars_written);
129 };
130 
131 } // namespace node
132 
133 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
134 
135 #endif // SRC_STRING_BYTES_H_
unsigned char * buf
Definition: cares_wrap.cc:483
int len
Definition: cares_wrap.cc:485
union node::cares_wrap::@8::CaresAsyncData::@0 data
encoding
Definition: node.h:322
enum encoding ParseEncoding(const char *encoding, enum encoding default_encoding)
Definition: node.cc:1485
NODE_DEPRECATED("Use ParseEncoding(isolate, ...)", inline enum encoding ParseEncoding(v8::Local< v8::Value > encoding_v, enum encoding default_encoding=LATIN1) { return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding);}) NODE_EXTERN void FatalException(v8 NODE_DEPRECATED("Use FatalException(isolate, ...)", inline void FatalException(const v8::TryCatch &try_catch) { return FatalException(v8::Isolate::GetCurrent(), try_catch);}) NODE_EXTERN v8 NODE_EXTERN v8::Local< v8::Value > Encode(v8::Isolate *isolate, const uint16_t *buf, size_t len)
Definition: node.h:350