1
1
#include " encoding_binding.h"
2
2
#include " ada.h"
3
3
#include " env-inl.h"
4
+ #include " node_buffer.h"
4
5
#include " node_errors.h"
5
6
#include " node_external_reference.h"
6
7
#include " simdutf.h"
@@ -226,6 +227,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
226
227
SetMethodNoSideEffect (isolate, target, " decodeUTF8" , DecodeUTF8);
227
228
SetMethodNoSideEffect (isolate, target, " toASCII" , ToASCII);
228
229
SetMethodNoSideEffect (isolate, target, " toUnicode" , ToUnicode);
230
+ SetMethodNoSideEffect (isolate, target, " decodeLatin1" , DecodeLatin1);
229
231
}
230
232
231
233
void BindingData::CreatePerContextProperties (Local<Object> target,
@@ -243,6 +245,44 @@ void BindingData::RegisterTimerExternalReferences(
243
245
registry->Register (DecodeUTF8);
244
246
registry->Register (ToASCII);
245
247
registry->Register (ToUnicode);
248
+ registry->Register (DecodeLatin1);
249
+ }
250
+
251
+ void BindingData::DecodeLatin1 (const FunctionCallbackInfo<Value>& args) {
252
+ Environment* env = Environment::GetCurrent (args);
253
+
254
+ CHECK_GE (args.Length (), 1 );
255
+ if (!(args[0 ]->IsArrayBuffer () || args[0 ]->IsSharedArrayBuffer () ||
256
+ args[0 ]->IsArrayBufferView ())) {
257
+ return node::THROW_ERR_INVALID_ARG_TYPE (
258
+ env->isolate (),
259
+ " The \" input\" argument must be an instance of ArrayBuffer, "
260
+ " SharedArrayBuffer, or ArrayBufferView." );
261
+ }
262
+
263
+ ArrayBufferViewContents<uint8_t > buffer (args[0 ]);
264
+ const uint8_t * data = buffer.data ();
265
+ size_t length = buffer.length ();
266
+
267
+ if (length == 0 ) {
268
+ return args.GetReturnValue ().SetEmptyString ();
269
+ }
270
+
271
+ std::string result (length * 2 , ' \0 ' );
272
+
273
+ size_t written = simdutf::convert_latin1_to_utf8 (
274
+ reinterpret_cast <const char *>(data), length, &result[0 ]);
275
+
276
+ if (written == 0 ) {
277
+ return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA (
278
+ env->isolate (), " The encoded data was not valid for encoding latin1" );
279
+ }
280
+
281
+ result.resize (written);
282
+
283
+ Local<Object> buffer_result =
284
+ node::Buffer::Copy (env, result.c_str (), result.length ()).ToLocalChecked ();
285
+ args.GetReturnValue ().Set (buffer_result);
246
286
}
247
287
248
288
} // namespace encoding_binding
0 commit comments