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"
@@ -391,6 +392,73 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
391392 }
392393}
393394
395+ void BindingData::DecodeSingleByte (const FunctionCallbackInfo<Value>& args) {
396+ Environment* env = Environment::GetCurrent (args);
397+
398+ CHECK_GE (args.Length (), 2 );
399+ Isolate* isolate = env->isolate ();
400+
401+ if (!(args[0 ]->IsArrayBuffer () || args[0 ]->IsSharedArrayBuffer () ||
402+ args[0 ]->IsArrayBufferView ())) {
403+ return node::THROW_ERR_INVALID_ARG_TYPE (
404+ isolate,
405+ " The \" input\" argument must be an instance of SharedArrayBuffer, "
406+ " ArrayBuffer or ArrayBufferView." );
407+ }
408+
409+ CHECK (args[1 ]->IsInt32 ());
410+ const int encoding = args[1 ].As <v8::Int32>()->Value ();
411+ CHECK (encoding >= 0 && encoding <= kXUserDefined );
412+
413+ ArrayBufferViewContents<uint8_t > buffer (args[0 ]);
414+ const uint8_t * data = buffer.data ();
415+ size_t length = buffer.length ();
416+
417+ if (length == 0 ) return args.GetReturnValue ().SetEmptyString ();
418+
419+ const char * dataChar = reinterpret_cast <const char *>(data);
420+ if (!simdutf::validate_ascii_with_errors (dataChar, length).error ) {
421+ Local<Value> ret;
422+ if (StringBytes::Encode (isolate, dataChar, length, LATIN1).ToLocal (&ret)) {
423+ args.GetReturnValue ().Set (ret);
424+ }
425+ return ;
426+ }
427+
428+ if (length > static_cast <size_t >(v8::String::kMaxLength )) {
429+ isolate->ThrowException (ERR_STRING_TOO_LONG (isolate));
430+ return ;
431+ }
432+
433+ uint16_t * dst = node::UncheckedMalloc<uint16_t >(length);
434+ if (dst == nullptr ) return node::THROW_ERR_MEMORY_ALLOCATION_FAILED (isolate);
435+
436+ if (encoding == kXUserDefined ) {
437+ // x-user-defined
438+ for (size_t i = 0 ; i < length; i++) {
439+ dst[i] = data[i] >= 0x80 ? data[i] + 0xf700 : data[i];
440+ }
441+ } else {
442+ bool has_fatal = args[2 ]->IsTrue ();
443+
444+ const uint16_t * table = tSingleByteEncodings[encoding];
445+ for (size_t i = 0 ; i < length; i++) dst[i] = table[data[i]];
446+
447+ const char16_t * dst16 = reinterpret_cast <char16_t *>(dst);
448+ if (has_fatal && fSingleByteEncodings [encoding] &&
449+ simdutf::find (dst16, dst16 + length, 0xfffd ) != dst16 + length) {
450+ free (dst);
451+ return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA (
452+ isolate, " The encoded data was not valid for this encoding" );
453+ }
454+ }
455+
456+ Local<Value> ret;
457+ if (StringBytes::Raw (isolate, dst, length).ToLocal (&ret)) {
458+ args.GetReturnValue ().Set (ret);
459+ }
460+ }
461+
394462void BindingData::ToASCII (const FunctionCallbackInfo<Value>& args) {
395463 Environment* env = Environment::GetCurrent (args);
396464 CHECK_GE (args.Length (), 1 );
@@ -423,6 +491,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
423491 SetMethod (isolate, target, " encodeInto" , EncodeInto);
424492 SetMethodNoSideEffect (isolate, target, " encodeUtf8String" , EncodeUtf8String);
425493 SetMethodNoSideEffect (isolate, target, " decodeUTF8" , DecodeUTF8);
494+ SetMethodNoSideEffect (isolate, target, " decodeSingleByte" , DecodeSingleByte);
426495 SetMethodNoSideEffect (isolate, target, " toASCII" , ToASCII);
427496 SetMethodNoSideEffect (isolate, target, " toUnicode" , ToUnicode);
428497}
@@ -440,6 +509,7 @@ void BindingData::RegisterTimerExternalReferences(
440509 registry->Register (EncodeInto);
441510 registry->Register (EncodeUtf8String);
442511 registry->Register (DecodeUTF8);
512+ registry->Register (DecodeSingleByte);
443513 registry->Register (ToASCII);
444514 registry->Register (ToUnicode);
445515}
0 commit comments