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,73 @@ 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+ Isolate* isolate = env->isolate ();
398+
399+ if (!(args[0 ]->IsArrayBuffer () || args[0 ]->IsSharedArrayBuffer () ||
400+ args[0 ]->IsArrayBufferView ())) {
401+ return node::THROW_ERR_INVALID_ARG_TYPE (
402+ isolate,
403+ " The \" input\" argument must be an instance of SharedArrayBuffer, "
404+ " ArrayBuffer or ArrayBufferView." );
405+ }
406+
407+ static constexpr int kXUserDefined = 28 ; // Last one, see encoding.js
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+ uint16_t * dst = node::UncheckedMalloc<uint16_t >(length);
429+ if (dst == nullptr ) {
430+ isolate->ThrowException (node::ERR_MEMORY_ALLOCATION_FAILED (isolate));
431+ return v8::MaybeLocal<Value>();
432+ }
433+
434+ if (encoding == kXUserDefined ) {
435+ // x-user-defined
436+ for (size_t i = 0 ; i < length; i++) {
437+ dst[i] = data[i] >= 0x80 ? data[i] + 0xf700 : data[i];
438+ }
439+ } else {
440+ bool has_fatal = args[2 ]->IsTrue ();
441+
442+ const uint16_t * table = tSingleByteEncodings[encoding];
443+ for (size_t i = 0 ; i < length; i++) dst[i] = table[data[i]];
444+
445+ const char16_t * dst16 = reinterpret_cast <char16_t *>(dst);
446+ if (has_fatal && fSingleByteEncodings [encoding] &&
447+ simdutf::find (dst16, dst16 + length, 0xfffd ) != dst16 + length) {
448+ free (dst);
449+ return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA (
450+ isolate, " The encoded data was not valid for this encoding" );
451+ }
452+ }
453+
454+ Local<Value> ret;
455+ if (StringBytes::Raw (isolate, dst, length).ToLocal (&ret)) {
456+ args.GetReturnValue ().Set (ret);
457+ }
458+ }
459+
392460void BindingData::ToASCII (const FunctionCallbackInfo<Value>& args) {
393461 Environment* env = Environment::GetCurrent (args);
394462 CHECK_GE (args.Length (), 1 );
@@ -421,6 +489,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
421489 SetMethod (isolate, target, " encodeInto" , EncodeInto);
422490 SetMethodNoSideEffect (isolate, target, " encodeUtf8String" , EncodeUtf8String);
423491 SetMethodNoSideEffect (isolate, target, " decodeUTF8" , DecodeUTF8);
492+ SetMethodNoSideEffect (isolate, target, " decodeSingleByte" , DecodeSingleByte);
424493 SetMethodNoSideEffect (isolate, target, " toASCII" , ToASCII);
425494 SetMethodNoSideEffect (isolate, target, " toUnicode" , ToUnicode);
426495}
@@ -438,6 +507,7 @@ void BindingData::RegisterTimerExternalReferences(
438507 registry->Register (EncodeInto);
439508 registry->Register (EncodeUtf8String);
440509 registry->Register (DecodeUTF8);
510+ registry->Register (DecodeSingleByte);
441511 registry->Register (ToASCII);
442512 registry->Register (ToUnicode);
443513}
0 commit comments