Skip to content
Merged
Prev Previous commit
Next Next commit
add ignoreBOM and fatal support to decodeLatin1
  • Loading branch information
Mert Can Altin
Mert Can Altin committed Oct 5, 2024
commit b7420b55210a745f68bef130fc707d28ff8e3eee
3 changes: 2 additions & 1 deletion lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,14 @@ function makeTextDecoderICU() {
validateDecoder(this);

this[kUTF8FastPath] &&= !(options?.stream);
this[kLatin1FastPath] &&= !(options?.stream);

if (this[kUTF8FastPath]) {
return decodeUTF8(input, this[kIgnoreBOM], this[kFatal]);
}

if (this[kLatin1FastPath]) {
Comment thread
anonrig marked this conversation as resolved.
return decodeLatin1(input);
return decodeLatin1(input, this[kIgnoreBOM], this[kFatal]);
}

this.#prepareConverter();
Expand Down
10 changes: 9 additions & 1 deletion src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,18 @@ void BindingData::DecodeLatin1(const FunctionCallbackInfo<Value>& args) {
"SharedArrayBuffer, or ArrayBufferView.");
}

bool ignore_bom = args[1]->IsTrue();
bool has_fatal = args[2]->IsTrue();

ArrayBufferViewContents<uint8_t> buffer(args[0]);
const uint8_t* data = buffer.data();
size_t length = buffer.length();

if (ignore_bom && length > 0 && data[0] == 0xFEFF) {
data++;
length--;
}

if (length == 0) {
return args.GetReturnValue().SetEmptyString();
}
Expand All @@ -273,7 +281,7 @@ void BindingData::DecodeLatin1(const FunctionCallbackInfo<Value>& args) {
size_t written = simdutf::convert_latin1_to_utf8(
reinterpret_cast<const char*>(data), length, &result[0]);
Comment thread
mertcanaltin marked this conversation as resolved.
Outdated

if (written == 0) {
if (has_fatal && written == 0) {
return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
env->isolate(), "The encoded data was not valid for encoding latin1");
}
Expand Down