11#include " encoding_binding.h"
22#include " ada.h"
3+ #include " encoding_singlebyte.h"
34#include " env-inl.h"
45#include " node_errors.h"
56#include " node_external_reference.h"
@@ -389,6 +390,67 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
389390 }
390391}
391392
393+ void BindingData::DecodeSingleByte (const FunctionCallbackInfo<Value>& args) {
394+ Environment* env = Environment::GetCurrent (args);
395+
396+ CHECK_GE (args.Length (), 2 );
397+
398+ if (!(args[0 ]->IsArrayBuffer () || args[0 ]->IsSharedArrayBuffer () ||
399+ args[0 ]->IsArrayBufferView ())) {
400+ return node::THROW_ERR_INVALID_ARG_TYPE (
401+ env->isolate (),
402+ " The \" input\" argument must be an instance of SharedArrayBuffer, "
403+ " ArrayBuffer or ArrayBufferView." );
404+ }
405+
406+ CHECK (args[1 ]->IsInt32 ());
407+ const int encoding = args[1 ].As <v8::Int32>()->Value ();
408+ CHECK (encoding >= 0 && encoding < 29 );
409+
410+ ArrayBufferViewContents<uint8_t > buffer (args[0 ]);
411+ const uint8_t * data = buffer.data ();
412+ size_t length = buffer.length ();
413+
414+ if (length == 0 ) return args.GetReturnValue ().SetEmptyString ();
415+
416+ const char * dataChar = reinterpret_cast <const char *>(data);
417+ if (!simdutf::validate_ascii_with_errors (dataChar, length).error ) {
418+ Local<Value> ret;
419+ if (StringBytes::Encode (env->isolate (), dataChar, length, LATIN1)
420+ .ToLocal (&ret)) {
421+ args.GetReturnValue ().Set (ret);
422+ }
423+ return ;
424+ }
425+
426+ uint16_t * dst = node::UncheckedMalloc<uint16_t >(length);
427+
428+ if (encoding == 28 ) {
429+ // x-user-defined
430+ for (size_t i = 0 ; i < length; i++) {
431+ dst[i] = data[i] >= 0x80 ? data[i] + 0xf700 : data[i];
432+ }
433+ } else {
434+ bool has_fatal = args[2 ]->IsTrue ();
435+
436+ const uint16_t * table = tSingleByteEncodings[encoding];
437+ for (size_t i = 0 ; i < length; i++) dst[i] = table[data[i]];
438+
439+ const char16_t * dst16 = reinterpret_cast <char16_t *>(dst);
440+ if (has_fatal && fSingleByteEncodings [encoding] &&
441+ simdutf::find (dst16, dst16 + length, 0xfffd ) != dst16 + length) {
442+ free (dst);
443+ return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA (
444+ env->isolate (), " The encoded data was not valid for this encoding" );
445+ }
446+ }
447+
448+ Local<Value> ret;
449+ if (StringBytes::Raw (env->isolate (), dst, length).ToLocal (&ret)) {
450+ args.GetReturnValue ().Set (ret);
451+ }
452+ }
453+
392454void BindingData::ToASCII (const FunctionCallbackInfo<Value>& args) {
393455 Environment* env = Environment::GetCurrent (args);
394456 CHECK_GE (args.Length (), 1 );
@@ -421,6 +483,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
421483 SetMethod (isolate, target, " encodeInto" , EncodeInto);
422484 SetMethodNoSideEffect (isolate, target, " encodeUtf8String" , EncodeUtf8String);
423485 SetMethodNoSideEffect (isolate, target, " decodeUTF8" , DecodeUTF8);
486+ SetMethodNoSideEffect (isolate, target, " decodeSingleByte" , DecodeSingleByte);
424487 SetMethodNoSideEffect (isolate, target, " toASCII" , ToASCII);
425488 SetMethodNoSideEffect (isolate, target, " toUnicode" , ToUnicode);
426489}
@@ -438,6 +501,7 @@ void BindingData::RegisterTimerExternalReferences(
438501 registry->Register (EncodeInto);
439502 registry->Register (EncodeUtf8String);
440503 registry->Register (DecodeUTF8);
504+ registry->Register (DecodeSingleByte);
441505 registry->Register (ToASCII);
442506 registry->Register (ToUnicode);
443507}
0 commit comments