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,66 @@ 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+ return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA (
443+ env->isolate (), " The encoded data was not valid for this encoding" );
444+ }
445+ }
446+
447+ Local<Value> ret;
448+ if (StringBytes::Raw (env->isolate (), dst, length).ToLocal (&ret)) {
449+ args.GetReturnValue ().Set (ret);
450+ }
451+ }
452+
392453void BindingData::ToASCII (const FunctionCallbackInfo<Value>& args) {
393454 Environment* env = Environment::GetCurrent (args);
394455 CHECK_GE (args.Length (), 1 );
@@ -421,6 +482,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
421482 SetMethod (isolate, target, " encodeInto" , EncodeInto);
422483 SetMethodNoSideEffect (isolate, target, " encodeUtf8String" , EncodeUtf8String);
423484 SetMethodNoSideEffect (isolate, target, " decodeUTF8" , DecodeUTF8);
485+ SetMethodNoSideEffect (isolate, target, " decodeSingleByte" , DecodeSingleByte);
424486 SetMethodNoSideEffect (isolate, target, " toASCII" , ToASCII);
425487 SetMethodNoSideEffect (isolate, target, " toUnicode" , ToUnicode);
426488}
@@ -438,6 +500,7 @@ void BindingData::RegisterTimerExternalReferences(
438500 registry->Register (EncodeInto);
439501 registry->Register (EncodeUtf8String);
440502 registry->Register (DecodeUTF8);
503+ registry->Register (DecodeSingleByte);
441504 registry->Register (ToASCII);
442505 registry->Register (ToUnicode);
443506}
0 commit comments