11#include " encoding_binding.h"
2+ #include " encoding_singlebyte.h"
23#include " ada.h"
34#include " env-inl.h"
45#include " node_errors.h"
@@ -379,6 +380,62 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
379380 }
380381}
381382
383+ void BindingData::DecodeSingleByte (const FunctionCallbackInfo<Value>& args) {
384+ Environment* env = Environment::GetCurrent (args);
385+
386+ CHECK_GE (args.Length (), 2 );
387+
388+ if (!(args[0 ]->IsArrayBuffer () || args[0 ]->IsSharedArrayBuffer () ||
389+ args[0 ]->IsArrayBufferView ())) {
390+ return node::THROW_ERR_INVALID_ARG_TYPE (
391+ env->isolate (),
392+ " The \" list\" argument must be an instance of SharedArrayBuffer, "
393+ " ArrayBuffer or ArrayBufferView." );
394+ }
395+
396+ CHECK (args[1 ]->IsInt32 ());
397+ const int encoding = args[1 ].As <v8::Int32>()->Value ();
398+ CHECK (encoding >= 0 && encoding < 29 );
399+
400+ ArrayBufferViewContents<uint8_t > buffer (args[0 ]);
401+ const uint8_t * data = buffer.data ();
402+ size_t length = buffer.length ();
403+
404+ if (length == 0 ) return args.GetReturnValue ().SetEmptyString ();
405+
406+ if (simdutf::validate_ascii (reinterpret_cast <const char *>(data), length)) {
407+ Local<Value> ret;
408+ if (StringBytes::Encode (env->isolate (), reinterpret_cast <const char *>(data), length, LATIN1).ToLocal (&ret)) {
409+ args.GetReturnValue ().Set (ret);
410+ }
411+ return ;
412+ }
413+
414+ uint16_t * dst = node::UncheckedMalloc<uint16_t >(length);
415+
416+ if (encoding == 28 ) {
417+ // x-user-defined
418+ for (size_t i = 0 ; i < length; i++) dst[i] = data[i] >= 0x80 ? data[i] + 0xf700 : data[i];
419+ } else {
420+ bool has_fatal = args[2 ]->IsTrue ();
421+
422+ const uint16_t * table = tSingleByteEncodings[encoding];
423+ for (size_t i = 0 ; i < length; i++) dst[i] = table[data[i]];
424+
425+ if (has_fatal && fSingleByteEncodings [encoding] &&
426+ simdutf::find (reinterpret_cast <char16_t *>(dst), reinterpret_cast <char16_t *>(dst) + length, 0xfffd ) != reinterpret_cast <char16_t *>(dst) + length
427+ ) {
428+ return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA (
429+ env->isolate (), " The encoded data was not valid for this encoding" );
430+ }
431+ }
432+
433+ Local<Value> ret;
434+ if (StringBytes::Raw (env->isolate (), dst, length).ToLocal (&ret)) {
435+ args.GetReturnValue ().Set (ret);
436+ }
437+ }
438+
382439void BindingData::ToASCII (const FunctionCallbackInfo<Value>& args) {
383440 Environment* env = Environment::GetCurrent (args);
384441 CHECK_GE (args.Length (), 1 );
@@ -411,6 +468,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
411468 SetMethod (isolate, target, " encodeInto" , EncodeInto);
412469 SetMethodNoSideEffect (isolate, target, " encodeUtf8String" , EncodeUtf8String);
413470 SetMethodNoSideEffect (isolate, target, " decodeUTF8" , DecodeUTF8);
471+ SetMethodNoSideEffect (isolate, target, " decodeSingleByte" , DecodeSingleByte);
414472 SetMethodNoSideEffect (isolate, target, " toASCII" , ToASCII);
415473 SetMethodNoSideEffect (isolate, target, " toUnicode" , ToUnicode);
416474}
@@ -428,6 +486,7 @@ void BindingData::RegisterTimerExternalReferences(
428486 registry->Register (EncodeInto);
429487 registry->Register (EncodeUtf8String);
430488 registry->Register (DecodeUTF8);
489+ registry->Register (DecodeSingleByte);
431490 registry->Register (ToASCII);
432491 registry->Register (ToUnicode);
433492}
0 commit comments