11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#ifndef SRC_STRING_BYTES_H_
231cb0ef41Sopenharmony_ci#define SRC_STRING_BYTES_H_
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci// Decodes a v8::Local<v8::String> or Buffer to a raw char*
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci#include "v8.h"
301cb0ef41Sopenharmony_ci#include "env-inl.h"
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci#include <string>
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cinamespace node {
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciclass StringBytes {
371cb0ef41Sopenharmony_ci public:
381cb0ef41Sopenharmony_ci  class InlineDecoder : public MaybeStackBuffer<char> {
391cb0ef41Sopenharmony_ci   public:
401cb0ef41Sopenharmony_ci    inline v8::Maybe<bool> Decode(Environment* env,
411cb0ef41Sopenharmony_ci                                  v8::Local<v8::String> string,
421cb0ef41Sopenharmony_ci                                  enum encoding enc) {
431cb0ef41Sopenharmony_ci      size_t storage;
441cb0ef41Sopenharmony_ci      if (!StringBytes::StorageSize(env->isolate(), string, enc).To(&storage))
451cb0ef41Sopenharmony_ci        return v8::Nothing<bool>();
461cb0ef41Sopenharmony_ci      AllocateSufficientStorage(storage);
471cb0ef41Sopenharmony_ci      const size_t length =
481cb0ef41Sopenharmony_ci          StringBytes::Write(env->isolate(), out(), storage, string, enc);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci      // No zero terminator is included when using this method.
511cb0ef41Sopenharmony_ci      SetLength(length);
521cb0ef41Sopenharmony_ci      return v8::Just(true);
531cb0ef41Sopenharmony_ci    }
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    inline size_t size() const { return length(); }
561cb0ef41Sopenharmony_ci  };
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  // Fast, but can be 2 bytes oversized for Base64, and
591cb0ef41Sopenharmony_ci  // as much as triple UTF-8 strings <= 65536 chars in length
601cb0ef41Sopenharmony_ci  static v8::Maybe<size_t> StorageSize(v8::Isolate* isolate,
611cb0ef41Sopenharmony_ci                                       v8::Local<v8::Value> val,
621cb0ef41Sopenharmony_ci                                       enum encoding enc);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  // Precise byte count, but slightly slower for Base64 and
651cb0ef41Sopenharmony_ci  // very much slower for UTF-8
661cb0ef41Sopenharmony_ci  static v8::Maybe<size_t> Size(v8::Isolate* isolate,
671cb0ef41Sopenharmony_ci                                v8::Local<v8::Value> val,
681cb0ef41Sopenharmony_ci                                enum encoding enc);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  // Write the bytes from the string or buffer into the char*
711cb0ef41Sopenharmony_ci  // returns the number of bytes written, which will always be
721cb0ef41Sopenharmony_ci  // <= buflen.  Use StorageSize/Size first to know how much
731cb0ef41Sopenharmony_ci  // memory to allocate.
741cb0ef41Sopenharmony_ci  static size_t Write(v8::Isolate* isolate,
751cb0ef41Sopenharmony_ci                      char* buf,
761cb0ef41Sopenharmony_ci                      size_t buflen,
771cb0ef41Sopenharmony_ci                      v8::Local<v8::Value> val,
781cb0ef41Sopenharmony_ci                      enum encoding enc);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  // Take the bytes in the src, and turn it into a Buffer or String.
811cb0ef41Sopenharmony_ci  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
821cb0ef41Sopenharmony_ci                                          const char* buf,
831cb0ef41Sopenharmony_ci                                          size_t buflen,
841cb0ef41Sopenharmony_ci                                          enum encoding encoding,
851cb0ef41Sopenharmony_ci                                          v8::Local<v8::Value>* error);
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  // Warning: This reverses endianness on BE platforms, even though the
881cb0ef41Sopenharmony_ci  // signature using uint16_t implies that it should not.
891cb0ef41Sopenharmony_ci  // However, the brokenness is already public API and can't therefore
901cb0ef41Sopenharmony_ci  // be changed easily.
911cb0ef41Sopenharmony_ci  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
921cb0ef41Sopenharmony_ci                                          const uint16_t* buf,
931cb0ef41Sopenharmony_ci                                          size_t buflen,
941cb0ef41Sopenharmony_ci                                          v8::Local<v8::Value>* error);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
971cb0ef41Sopenharmony_ci                                          const char* buf,
981cb0ef41Sopenharmony_ci                                          enum encoding encoding,
991cb0ef41Sopenharmony_ci                                          v8::Local<v8::Value>* error);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  static size_t hex_encode(const char* src,
1021cb0ef41Sopenharmony_ci                           size_t slen,
1031cb0ef41Sopenharmony_ci                           char* dst,
1041cb0ef41Sopenharmony_ci                           size_t dlen);
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  static std::string hex_encode(const char* src, size_t slen);
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci private:
1091cb0ef41Sopenharmony_ci  static size_t WriteUCS2(v8::Isolate* isolate,
1101cb0ef41Sopenharmony_ci                          char* buf,
1111cb0ef41Sopenharmony_ci                          size_t buflen,
1121cb0ef41Sopenharmony_ci                          v8::Local<v8::String> str,
1131cb0ef41Sopenharmony_ci                          int flags);
1141cb0ef41Sopenharmony_ci};
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci}  // namespace node
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci#endif  // SRC_STRING_BYTES_H_
121