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_NODE_I18N_H_ 23 #define SRC_NODE_I18N_H_ 24 25 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 26 27 #if defined(NODE_HAVE_I18N_SUPPORT) 28 29 #include "base_object.h" 30 #include "env.h" 31 #include "util.h" 32 #include "v8.h" 33 34 #include <unicode/ucnv.h> 35 36 #include <string> 37 38 namespace node { 39 40 namespace i18n { 41 42 bool InitializeICUDirectory(const std::string& path); 43 44 enum idna_mode { 45 // Default mode for maximum compatibility. 46 IDNA_DEFAULT, 47 // Ignore all errors in IDNA conversion, if possible. 48 IDNA_LENIENT, 49 // Enforce STD3 rules (UseSTD3ASCIIRules) and DNS length restrictions 50 // (VerifyDnsLength). Corresponds to `beStrict` flag in the "domain to ASCII" 51 // algorithm. 52 IDNA_STRICT 53 }; 54 55 // Implements the WHATWG URL Standard "domain to ASCII" algorithm. 56 // https://url.spec.whatwg.org/#concept-domain-to-ascii 57 int32_t ToASCII(MaybeStackBuffer<char>* buf, 58 const char* input, 59 size_t length, 60 enum idna_mode mode = IDNA_DEFAULT); 61 62 // Implements the WHATWG URL Standard "domain to Unicode" algorithm. 63 // https://url.spec.whatwg.org/#concept-domain-to-unicode 64 int32_t ToUnicode(MaybeStackBuffer<char>* buf, 65 const char* input, 66 size_t length); 67 68 struct ConverterDeleter { operatorConverterDeleter69 void operator()(UConverter* pointer) const { ucnv_close(pointer); } 70 }; 71 using ConverterPointer = std::unique_ptr<UConverter, ConverterDeleter>; 72 73 class Converter { 74 public: 75 explicit Converter(const char* name, const char* sub = nullptr); 76 explicit Converter(UConverter* converter, const char* sub = nullptr); 77 conv()78 UConverter* conv() const { return conv_.get(); } 79 80 size_t max_char_size() const; 81 size_t min_char_size() const; 82 void reset(); 83 void set_subst_chars(const char* sub = nullptr); 84 85 private: 86 ConverterPointer conv_; 87 }; 88 89 class ConverterObject : public BaseObject, Converter { 90 public: 91 enum ConverterFlags { 92 CONVERTER_FLAGS_FLUSH = 0x1, 93 CONVERTER_FLAGS_FATAL = 0x2, 94 CONVERTER_FLAGS_IGNORE_BOM = 0x4, 95 CONVERTER_FLAGS_UNICODE = 0x8, 96 CONVERTER_FLAGS_BOM_SEEN = 0x10, 97 }; 98 99 static void Create(const v8::FunctionCallbackInfo<v8::Value>& args); 100 static void Decode(const v8::FunctionCallbackInfo<v8::Value>& args); 101 static void Has(const v8::FunctionCallbackInfo<v8::Value>& args); 102 103 SET_NO_MEMORY_INFO() 104 SET_MEMORY_INFO_NAME(ConverterObject) 105 SET_SELF_SIZE(ConverterObject) 106 107 protected: 108 ConverterObject(Environment* env, 109 v8::Local<v8::Object> wrap, 110 UConverter* converter, 111 int flags, 112 const char* sub = nullptr); 113 set_bom_seen(bool seen)114 void set_bom_seen(bool seen) { 115 if (seen) 116 flags_ |= CONVERTER_FLAGS_BOM_SEEN; 117 else 118 flags_ &= ~CONVERTER_FLAGS_BOM_SEEN; 119 } 120 bom_seen()121 bool bom_seen() const { 122 return (flags_ & CONVERTER_FLAGS_BOM_SEEN) == CONVERTER_FLAGS_BOM_SEEN; 123 } 124 unicode()125 bool unicode() const { 126 return (flags_ & CONVERTER_FLAGS_UNICODE) == CONVERTER_FLAGS_UNICODE; 127 } 128 ignore_bom()129 bool ignore_bom() const { 130 return (flags_ & CONVERTER_FLAGS_IGNORE_BOM) == CONVERTER_FLAGS_IGNORE_BOM; 131 } 132 133 private: 134 int flags_ = 0; 135 }; 136 137 } // namespace i18n 138 } // namespace node 139 140 #endif // NODE_HAVE_I18N_SUPPORT 141 142 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 143 144 #endif // SRC_NODE_I18N_H_ 145