mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
buffer: add buffer.isUtf8 for utf8 validation
PR-URL: https://github.com/nodejs/node/pull/45947 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
1 parent
4063cdcef6
commit
16ee02f2eb
7 files changed
+131
-1
No files matched your search
@@ -1223,6 +1223,20 @@ static void EncodeInto(const FunctionCallbackInfo<Value>& args) {
|
||||
results[1] = written;
|
||||
}
|
||||
|
||||
static void IsUtf8(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
CHECK_EQ(args.Length(), 1);
|
||||
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
|
||||
args[0]->IsSharedArrayBuffer());
|
||||
ArrayBufferViewContents<char> abv(args[0]);
|
||||
|
||||
if (abv.WasDetached()) {
|
||||
return node::THROW_ERR_INVALID_STATE(
|
||||
env, "Cannot validate on a detached buffer");
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(simdutf::validate_utf8(abv.data(), abv.length()));
|
||||
}
|
||||
|
||||
void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
@@ -1358,6 +1372,8 @@ void Initialize(Local<Object> target,
|
||||
SetMethod(context, target, "encodeInto", EncodeInto);
|
||||
SetMethodNoSideEffect(context, target, "encodeUtf8String", EncodeUtf8String);
|
||||
|
||||
SetMethodNoSideEffect(context, target, "isUtf8", IsUtf8);
|
||||
|
||||
target
|
||||
->Set(context,
|
||||
FIXED_ONE_BYTE_STRING(isolate, "kMaxLength"),
|
||||
@@ -1413,6 +1429,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
||||
registry->Register(EncodeInto);
|
||||
registry->Register(EncodeUtf8String);
|
||||
|
||||
registry->Register(IsUtf8);
|
||||
|
||||
registry->Register(StringSlice<ASCII>);
|
||||
registry->Register(StringSlice<BASE64>);
|
||||
registry->Register(StringSlice<BASE64URL>);
|
||||
|
||||
@@ -68,6 +68,7 @@ void OOMErrorHandler(const char* location, bool is_heap_oom);
|
||||
V(ERR_INVALID_ARG_TYPE, TypeError) \
|
||||
V(ERR_INVALID_OBJECT_DEFINE_PROPERTY, TypeError) \
|
||||
V(ERR_INVALID_MODULE, Error) \
|
||||
V(ERR_INVALID_STATE, Error) \
|
||||
V(ERR_INVALID_THIS, TypeError) \
|
||||
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
|
||||
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
|
||||
|
||||
@@ -555,6 +555,7 @@ void ArrayBufferViewContents<T, S>::ReadValue(v8::Local<v8::Value> buf) {
|
||||
auto ab = buf.As<v8::ArrayBuffer>();
|
||||
length_ = ab->ByteLength();
|
||||
data_ = static_cast<T*>(ab->Data());
|
||||
was_detached_ = ab->WasDetached();
|
||||
} else {
|
||||
CHECK(buf->IsSharedArrayBuffer());
|
||||
auto sab = buf.As<v8::SharedArrayBuffer>();
|
||||
|
||||
@@ -510,6 +510,7 @@ class ArrayBufferViewContents {
|
||||
inline void Read(v8::Local<v8::ArrayBufferView> abv);
|
||||
inline void ReadValue(v8::Local<v8::Value> buf);
|
||||
|
||||
inline bool WasDetached() const { return was_detached_; }
|
||||
inline const T* data() const { return data_; }
|
||||
inline size_t length() const { return length_; }
|
||||
|
||||
@@ -524,6 +525,7 @@ class ArrayBufferViewContents {
|
||||
T stack_storage_[kStackStorageSize];
|
||||
T* data_ = nullptr;
|
||||
size_t length_ = 0;
|
||||
bool was_detached_ = false;
|
||||
};
|
||||
|
||||
class Utf8Value : public MaybeStackBuffer<char> {
|
||||
|
||||
Reference in new issue
Block a user