1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #include "node_buffer.h"
23 #include "allocated_buffer-inl.h"
24 #include "node.h"
25 #include "node_blob.h"
26 #include "node_errors.h"
27 #include "node_internals.h"
28
29 #include "env-inl.h"
30 #include "string_bytes.h"
31 #include "string_search.h"
32 #include "util-inl.h"
33 #include "v8.h"
34
35 #include <cstring>
36 #include <climits>
37
38 #define THROW_AND_RETURN_UNLESS_BUFFER(env, obj) \
39 THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument") \
40
41 #define THROW_AND_RETURN_IF_OOB(r) \
42 do { \
43 if ((r).IsNothing()) return; \
44 if (!(r).FromJust()) \
45 return node::THROW_ERR_OUT_OF_RANGE(env, "Index out of range"); \
46 } while (0) \
47
48 namespace node {
49 namespace Buffer {
50
51 using v8::ArrayBuffer;
52 using v8::ArrayBufferView;
53 using v8::BackingStore;
54 using v8::Context;
55 using v8::EscapableHandleScope;
56 using v8::FunctionCallbackInfo;
57 using v8::Global;
58 using v8::HandleScope;
59 using v8::Int32;
60 using v8::Integer;
61 using v8::Isolate;
62 using v8::Just;
63 using v8::Local;
64 using v8::Maybe;
65 using v8::MaybeLocal;
66 using v8::Nothing;
67 using v8::Number;
68 using v8::Object;
69 using v8::String;
70 using v8::Uint32;
71 using v8::Uint32Array;
72 using v8::Uint8Array;
73 using v8::Value;
74
75 namespace {
76
77 class CallbackInfo {
78 public:
79 static inline Local<ArrayBuffer> CreateTrackedArrayBuffer(
80 Environment* env,
81 char* data,
82 size_t length,
83 FreeCallback callback,
84 void* hint);
85
86 CallbackInfo(const CallbackInfo&) = delete;
87 CallbackInfo& operator=(const CallbackInfo&) = delete;
88
89 private:
90 static void CleanupHook(void* data);
91 inline void OnBackingStoreFree();
92 inline void CallAndResetCallback();
93 inline CallbackInfo(Environment* env,
94 FreeCallback callback,
95 char* data,
96 void* hint);
97 Global<ArrayBuffer> persistent_;
98 Mutex mutex_; // Protects callback_.
99 FreeCallback callback_;
100 char* const data_;
101 void* const hint_;
102 Environment* const env_;
103 };
104
105
CreateTrackedArrayBuffer(Environment * env,char * data,size_t length,FreeCallback callback,void * hint)106 Local<ArrayBuffer> CallbackInfo::CreateTrackedArrayBuffer(
107 Environment* env,
108 char* data,
109 size_t length,
110 FreeCallback callback,
111 void* hint) {
112 CHECK_NOT_NULL(callback);
113 CHECK_IMPLIES(data == nullptr, length == 0);
114
115 CallbackInfo* self = new CallbackInfo(env, callback, data, hint);
116 std::unique_ptr<BackingStore> bs =
117 ArrayBuffer::NewBackingStore(data, length, [](void*, size_t, void* arg) {
118 static_cast<CallbackInfo*>(arg)->OnBackingStoreFree();
119 }, self);
120 Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
121
122 // V8 simply ignores the BackingStore deleter callback if data == nullptr,
123 // but our API contract requires it being called.
124 if (data == nullptr) {
125 ab->Detach();
126 self->OnBackingStoreFree(); // This calls `callback` asynchronously.
127 } else {
128 // Store the ArrayBuffer so that we can detach it later.
129 self->persistent_.Reset(env->isolate(), ab);
130 self->persistent_.SetWeak();
131 }
132
133 return ab;
134 }
135
136
CallbackInfo(Environment * env,FreeCallback callback,char * data,void * hint)137 CallbackInfo::CallbackInfo(Environment* env,
138 FreeCallback callback,
139 char* data,
140 void* hint)
141 : callback_(callback),
142 data_(data),
143 hint_(hint),
144 env_(env) {
145 env->AddCleanupHook(CleanupHook, this);
146 env->isolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
147 }
148
CleanupHook(void * data)149 void CallbackInfo::CleanupHook(void* data) {
150 CallbackInfo* self = static_cast<CallbackInfo*>(data);
151
152 {
153 HandleScope handle_scope(self->env_->isolate());
154 Local<ArrayBuffer> ab = self->persistent_.Get(self->env_->isolate());
155 if (!ab.IsEmpty() && ab->IsDetachable()) {
156 ab->Detach();
157 self->persistent_.Reset();
158 }
159 }
160
161 // Call the callback in this case, but don't delete `this` yet because the
162 // BackingStore deleter callback will do so later.
163 self->CallAndResetCallback();
164 }
165
CallAndResetCallback()166 void CallbackInfo::CallAndResetCallback() {
167 FreeCallback callback;
168 {
169 Mutex::ScopedLock lock(mutex_);
170 callback = callback_;
171 callback_ = nullptr;
172 }
173 if (callback != nullptr) {
174 // Clean up all Environment-related state and run the callback.
175 env_->RemoveCleanupHook(CleanupHook, this);
176 int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
177 env_->isolate()->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
178
179 callback(data_, hint_);
180 }
181 }
182
OnBackingStoreFree()183 void CallbackInfo::OnBackingStoreFree() {
184 // This method should always release the memory for `this`.
185 std::unique_ptr<CallbackInfo> self { this };
186 Mutex::ScopedLock lock(mutex_);
187 // If callback_ == nullptr, that means that the callback has already run from
188 // the cleanup hook, and there is nothing left to do here besides to clean
189 // up the memory involved. In particular, the underlying `Environment` may
190 // be gone at this point, so don’t attempt to call SetImmediateThreadsafe().
191 if (callback_ == nullptr) return;
192
193 env_->SetImmediateThreadsafe([self = std::move(self)](Environment* env) {
194 CHECK_EQ(self->env_, env); // Consistency check.
195
196 self->CallAndResetCallback();
197 });
198 }
199
200
201 // Parse index for external array data. An empty Maybe indicates
202 // a pending exception. `false` indicates that the index is out-of-bounds.
ParseArrayIndex(Environment * env,Local<Value> arg,size_t def,size_t * ret)203 inline MUST_USE_RESULT Maybe<bool> ParseArrayIndex(Environment* env,
204 Local<Value> arg,
205 size_t def,
206 size_t* ret) {
207 if (arg->IsUndefined()) {
208 *ret = def;
209 return Just(true);
210 }
211
212 int64_t tmp_i;
213 if (!arg->IntegerValue(env->context()).To(&tmp_i))
214 return Nothing<bool>();
215
216 if (tmp_i < 0)
217 return Just(false);
218
219 // Check that the result fits in a size_t.
220 const uint64_t kSizeMax = static_cast<uint64_t>(static_cast<size_t>(-1));
221 // coverity[pointless_expression]
222 if (static_cast<uint64_t>(tmp_i) > kSizeMax)
223 return Just(false);
224
225 *ret = static_cast<size_t>(tmp_i);
226 return Just(true);
227 }
228
229 } // anonymous namespace
230
231 // Buffer methods
232
HasInstance(Local<Value> val)233 bool HasInstance(Local<Value> val) {
234 return val->IsArrayBufferView();
235 }
236
237
HasInstance(Local<Object> obj)238 bool HasInstance(Local<Object> obj) {
239 return obj->IsArrayBufferView();
240 }
241
242
Data(Local<Value> val)243 char* Data(Local<Value> val) {
244 CHECK(val->IsArrayBufferView());
245 Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
246 return static_cast<char*>(ui->Buffer()->GetBackingStore()->Data()) +
247 ui->ByteOffset();
248 }
249
250
Data(Local<Object> obj)251 char* Data(Local<Object> obj) {
252 return Data(obj.As<Value>());
253 }
254
255
Length(Local<Value> val)256 size_t Length(Local<Value> val) {
257 CHECK(val->IsArrayBufferView());
258 Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
259 return ui->ByteLength();
260 }
261
262
Length(Local<Object> obj)263 size_t Length(Local<Object> obj) {
264 CHECK(obj->IsArrayBufferView());
265 Local<ArrayBufferView> ui = obj.As<ArrayBufferView>();
266 return ui->ByteLength();
267 }
268
269
New(Environment * env,Local<ArrayBuffer> ab,size_t byte_offset,size_t length)270 MaybeLocal<Uint8Array> New(Environment* env,
271 Local<ArrayBuffer> ab,
272 size_t byte_offset,
273 size_t length) {
274 CHECK(!env->buffer_prototype_object().IsEmpty());
275 Local<Uint8Array> ui = Uint8Array::New(ab, byte_offset, length);
276 Maybe<bool> mb =
277 ui->SetPrototype(env->context(), env->buffer_prototype_object());
278 if (mb.IsNothing())
279 return MaybeLocal<Uint8Array>();
280 return ui;
281 }
282
New(Isolate * isolate,Local<ArrayBuffer> ab,size_t byte_offset,size_t length)283 MaybeLocal<Uint8Array> New(Isolate* isolate,
284 Local<ArrayBuffer> ab,
285 size_t byte_offset,
286 size_t length) {
287 Environment* env = Environment::GetCurrent(isolate);
288 if (env == nullptr) {
289 THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
290 return MaybeLocal<Uint8Array>();
291 }
292 return New(env, ab, byte_offset, length);
293 }
294
295
New(Isolate * isolate,Local<String> string,enum encoding enc)296 MaybeLocal<Object> New(Isolate* isolate,
297 Local<String> string,
298 enum encoding enc) {
299 EscapableHandleScope scope(isolate);
300
301 size_t length;
302 if (!StringBytes::Size(isolate, string, enc).To(&length))
303 return Local<Object>();
304 size_t actual = 0;
305 std::unique_ptr<BackingStore> store;
306
307 if (length > 0) {
308 store = ArrayBuffer::NewBackingStore(isolate, length);
309
310 if (UNLIKELY(!store)) {
311 THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate);
312 return Local<Object>();
313 }
314
315 actual = StringBytes::Write(
316 isolate,
317 static_cast<char*>(store->Data()),
318 length,
319 string,
320 enc);
321 CHECK(actual <= length);
322
323 if (LIKELY(actual > 0)) {
324 if (actual < length)
325 store = BackingStore::Reallocate(isolate, std::move(store), actual);
326 Local<ArrayBuffer> buf = ArrayBuffer::New(isolate, std::move(store));
327 Local<Object> obj;
328 if (UNLIKELY(!New(isolate, buf, 0, actual).ToLocal(&obj)))
329 return MaybeLocal<Object>();
330 return scope.Escape(obj);
331 }
332 }
333
334 return scope.EscapeMaybe(New(isolate, 0));
335 }
336
337
New(Isolate * isolate,size_t length)338 MaybeLocal<Object> New(Isolate* isolate, size_t length) {
339 EscapableHandleScope handle_scope(isolate);
340 Local<Object> obj;
341 Environment* env = Environment::GetCurrent(isolate);
342 if (env == nullptr) {
343 THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
344 return MaybeLocal<Object>();
345 }
346 if (Buffer::New(env, length).ToLocal(&obj))
347 return handle_scope.Escape(obj);
348 return Local<Object>();
349 }
350
351
New(Environment * env,size_t length)352 MaybeLocal<Object> New(Environment* env, size_t length) {
353 EscapableHandleScope scope(env->isolate());
354
355 // V8 currently only allows a maximum Typed Array index of max Smi.
356 if (length > kMaxLength) {
357 env->isolate()->ThrowException(ERR_BUFFER_TOO_LARGE(env->isolate()));
358 return Local<Object>();
359 }
360
361 return scope.EscapeMaybe(
362 AllocatedBuffer::AllocateManaged(env, length).ToBuffer());
363 }
364
365
Copy(Isolate * isolate,const char * data,size_t length)366 MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) {
367 EscapableHandleScope handle_scope(isolate);
368 Environment* env = Environment::GetCurrent(isolate);
369 if (env == nullptr) {
370 THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
371 return MaybeLocal<Object>();
372 }
373 Local<Object> obj;
374 if (Buffer::Copy(env, data, length).ToLocal(&obj))
375 return handle_scope.Escape(obj);
376 return Local<Object>();
377 }
378
379
Copy(Environment * env,const char * data,size_t length)380 MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
381 EscapableHandleScope scope(env->isolate());
382
383 // V8 currently only allows a maximum Typed Array index of max Smi.
384 if (length > kMaxLength) {
385 env->isolate()->ThrowException(ERR_BUFFER_TOO_LARGE(env->isolate()));
386 return Local<Object>();
387 }
388
389 AllocatedBuffer ret = AllocatedBuffer::AllocateManaged(env, length);
390 if (length > 0) {
391 memcpy(ret.data(), data, length);
392 }
393
394 return scope.EscapeMaybe(ret.ToBuffer());
395 }
396
397
New(Isolate * isolate,char * data,size_t length,FreeCallback callback,void * hint)398 MaybeLocal<Object> New(Isolate* isolate,
399 char* data,
400 size_t length,
401 FreeCallback callback,
402 void* hint) {
403 EscapableHandleScope handle_scope(isolate);
404 Environment* env = Environment::GetCurrent(isolate);
405 if (env == nullptr) {
406 callback(data, hint);
407 THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
408 return MaybeLocal<Object>();
409 }
410 return handle_scope.EscapeMaybe(
411 Buffer::New(env, data, length, callback, hint));
412 }
413
414
New(Environment * env,char * data,size_t length,FreeCallback callback,void * hint)415 MaybeLocal<Object> New(Environment* env,
416 char* data,
417 size_t length,
418 FreeCallback callback,
419 void* hint) {
420 EscapableHandleScope scope(env->isolate());
421
422 if (length > kMaxLength) {
423 env->isolate()->ThrowException(ERR_BUFFER_TOO_LARGE(env->isolate()));
424 callback(data, hint);
425 return Local<Object>();
426 }
427
428 Local<ArrayBuffer> ab =
429 CallbackInfo::CreateTrackedArrayBuffer(env, data, length, callback, hint);
430 if (ab->SetPrivate(env->context(),
431 env->untransferable_object_private_symbol(),
432 True(env->isolate())).IsNothing()) {
433 return Local<Object>();
434 }
435 MaybeLocal<Uint8Array> maybe_ui = Buffer::New(env, ab, 0, length);
436
437 Local<Uint8Array> ui;
438 if (!maybe_ui.ToLocal(&ui))
439 return MaybeLocal<Object>();
440
441 return scope.Escape(ui);
442 }
443
444 // Warning: This function needs `data` to be allocated with malloc() and not
445 // necessarily isolate's ArrayBuffer::Allocator.
New(Isolate * isolate,char * data,size_t length)446 MaybeLocal<Object> New(Isolate* isolate, char* data, size_t length) {
447 EscapableHandleScope handle_scope(isolate);
448 Environment* env = Environment::GetCurrent(isolate);
449 if (env == nullptr) {
450 free(data);
451 THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
452 return MaybeLocal<Object>();
453 }
454 Local<Object> obj;
455 if (Buffer::New(env, data, length).ToLocal(&obj))
456 return handle_scope.Escape(obj);
457 return Local<Object>();
458 }
459
460 // The contract for this function is that `data` is allocated with malloc()
461 // and not necessarily isolate's ArrayBuffer::Allocator.
New(Environment * env,char * data,size_t length)462 MaybeLocal<Object> New(Environment* env,
463 char* data,
464 size_t length) {
465 if (length > 0) {
466 CHECK_NOT_NULL(data);
467 CHECK(length <= kMaxLength);
468 }
469
470 auto free_callback = [](char* data, void* hint) { free(data); };
471 return New(env, data, length, free_callback, nullptr);
472 }
473
474 namespace {
475
CreateFromString(const FunctionCallbackInfo<Value> & args)476 void CreateFromString(const FunctionCallbackInfo<Value>& args) {
477 CHECK(args[0]->IsString());
478 CHECK(args[1]->IsInt32());
479
480 enum encoding enc = static_cast<enum encoding>(args[1].As<Int32>()->Value());
481 Local<Object> buf;
482 if (New(args.GetIsolate(), args[0].As<String>(), enc).ToLocal(&buf))
483 args.GetReturnValue().Set(buf);
484 }
485
486
487 template <encoding encoding>
StringSlice(const FunctionCallbackInfo<Value> & args)488 void StringSlice(const FunctionCallbackInfo<Value>& args) {
489 Environment* env = Environment::GetCurrent(args);
490 Isolate* isolate = env->isolate();
491
492 THROW_AND_RETURN_UNLESS_BUFFER(env, args.This());
493 ArrayBufferViewContents<char> buffer(args.This());
494
495 if (buffer.length() == 0)
496 return args.GetReturnValue().SetEmptyString();
497
498 size_t start = 0;
499 size_t end = 0;
500 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[0], 0, &start));
501 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[1], buffer.length(), &end));
502 if (end < start) end = start;
503 THROW_AND_RETURN_IF_OOB(Just(end <= buffer.length()));
504 size_t length = end - start;
505
506 Local<Value> error;
507 MaybeLocal<Value> maybe_ret =
508 StringBytes::Encode(isolate,
509 buffer.data() + start,
510 length,
511 encoding,
512 &error);
513 Local<Value> ret;
514 if (!maybe_ret.ToLocal(&ret)) {
515 CHECK(!error.IsEmpty());
516 isolate->ThrowException(error);
517 return;
518 }
519 args.GetReturnValue().Set(ret);
520 }
521
522
523 // bytesCopied = copy(buffer, target[, targetStart][, sourceStart][, sourceEnd])
Copy(const FunctionCallbackInfo<Value> & args)524 void Copy(const FunctionCallbackInfo<Value> &args) {
525 Environment* env = Environment::GetCurrent(args);
526
527 THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
528 THROW_AND_RETURN_UNLESS_BUFFER(env, args[1]);
529 ArrayBufferViewContents<char> source(args[0]);
530 Local<Object> target_obj = args[1].As<Object>();
531 SPREAD_BUFFER_ARG(target_obj, target);
532
533 size_t target_start = 0;
534 size_t source_start = 0;
535 size_t source_end = 0;
536
537 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[2], 0, &target_start));
538 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[3], 0, &source_start));
539 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[4], source.length(),
540 &source_end));
541
542 // Copy 0 bytes; we're done
543 if (target_start >= target_length || source_start >= source_end)
544 return args.GetReturnValue().Set(0);
545
546 if (source_start > source.length())
547 return THROW_ERR_OUT_OF_RANGE(
548 env, "The value of \"sourceStart\" is out of range.");
549
550 if (source_end - source_start > target_length - target_start)
551 source_end = source_start + target_length - target_start;
552
553 uint32_t to_copy = std::min(
554 std::min(source_end - source_start, target_length - target_start),
555 source.length() - source_start);
556
557 memmove(target_data + target_start, source.data() + source_start, to_copy);
558 args.GetReturnValue().Set(to_copy);
559 }
560
561
Fill(const FunctionCallbackInfo<Value> & args)562 void Fill(const FunctionCallbackInfo<Value>& args) {
563 Environment* env = Environment::GetCurrent(args);
564 Local<Context> ctx = env->context();
565
566 THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
567 SPREAD_BUFFER_ARG(args[0], ts_obj);
568
569 size_t start = 0;
570 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[2], 0, &start));
571 size_t end;
572 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[3], 0, &end));
573
574 size_t fill_length = end - start;
575 Local<String> str_obj;
576 size_t str_length;
577 enum encoding enc;
578
579 // OOB Check. Throw the error in JS.
580 if (start > end || fill_length + start > ts_obj_length)
581 return args.GetReturnValue().Set(-2);
582
583 // First check if Buffer has been passed.
584 if (Buffer::HasInstance(args[1])) {
585 SPREAD_BUFFER_ARG(args[1], fill_obj);
586 str_length = fill_obj_length;
587 memcpy(
588 ts_obj_data + start, fill_obj_data, std::min(str_length, fill_length));
589 goto start_fill;
590 }
591
592 // Then coerce everything that's not a string.
593 if (!args[1]->IsString()) {
594 uint32_t val;
595 if (!args[1]->Uint32Value(ctx).To(&val)) return;
596 int value = val & 255;
597 memset(ts_obj_data + start, value, fill_length);
598 return;
599 }
600
601 str_obj = args[1]->ToString(env->context()).ToLocalChecked();
602 enc = ParseEncoding(env->isolate(), args[4], UTF8);
603
604 // Can't use StringBytes::Write() in all cases. For example if attempting
605 // to write a two byte character into a one byte Buffer.
606 if (enc == UTF8) {
607 str_length = str_obj->Utf8Length(env->isolate());
608 node::Utf8Value str(env->isolate(), args[1]);
609 memcpy(ts_obj_data + start, *str, std::min(str_length, fill_length));
610
611 } else if (enc == UCS2) {
612 str_length = str_obj->Length() * sizeof(uint16_t);
613 node::TwoByteValue str(env->isolate(), args[1]);
614 if (IsBigEndian())
615 SwapBytes16(reinterpret_cast<char*>(&str[0]), str_length);
616
617 memcpy(ts_obj_data + start, *str, std::min(str_length, fill_length));
618
619 } else {
620 // Write initial String to Buffer, then use that memory to copy remainder
621 // of string. Correct the string length for cases like HEX where less than
622 // the total string length is written.
623 str_length = StringBytes::Write(env->isolate(),
624 ts_obj_data + start,
625 fill_length,
626 str_obj,
627 enc,
628 nullptr);
629 }
630
631 start_fill:
632
633 if (str_length >= fill_length)
634 return;
635
636 // If str_length is zero, then either an empty buffer was provided, or Write()
637 // indicated that no bytes could be written. If no bytes could be written,
638 // then return -1 because the fill value is invalid. This will trigger a throw
639 // in JavaScript. Silently failing should be avoided because it can lead to
640 // buffers with unexpected contents.
641 if (str_length == 0)
642 return args.GetReturnValue().Set(-1);
643
644 size_t in_there = str_length;
645 char* ptr = ts_obj_data + start + str_length;
646
647 while (in_there < fill_length - in_there) {
648 memcpy(ptr, ts_obj_data + start, in_there);
649 ptr += in_there;
650 in_there *= 2;
651 }
652
653 if (in_there < fill_length) {
654 memcpy(ptr, ts_obj_data + start, fill_length - in_there);
655 }
656 }
657
658
659 template <encoding encoding>
StringWrite(const FunctionCallbackInfo<Value> & args)660 void StringWrite(const FunctionCallbackInfo<Value>& args) {
661 Environment* env = Environment::GetCurrent(args);
662
663 THROW_AND_RETURN_UNLESS_BUFFER(env, args.This());
664 SPREAD_BUFFER_ARG(args.This(), ts_obj);
665
666 THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "argument");
667
668 Local<String> str = args[0]->ToString(env->context()).ToLocalChecked();
669
670 size_t offset = 0;
671 size_t max_length = 0;
672
673 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[1], 0, &offset));
674 if (offset > ts_obj_length) {
675 return node::THROW_ERR_BUFFER_OUT_OF_BOUNDS(
676 env, "\"offset\" is outside of buffer bounds");
677 }
678
679 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[2], ts_obj_length - offset,
680 &max_length));
681
682 max_length = std::min(ts_obj_length - offset, max_length);
683
684 if (max_length == 0)
685 return args.GetReturnValue().Set(0);
686
687 uint32_t written = StringBytes::Write(env->isolate(),
688 ts_obj_data + offset,
689 max_length,
690 str,
691 encoding,
692 nullptr);
693 args.GetReturnValue().Set(written);
694 }
695
ByteLengthUtf8(const FunctionCallbackInfo<Value> & args)696 void ByteLengthUtf8(const FunctionCallbackInfo<Value> &args) {
697 Environment* env = Environment::GetCurrent(args);
698 CHECK(args[0]->IsString());
699
700 // Fast case: avoid StringBytes on UTF8 string. Jump to v8.
701 args.GetReturnValue().Set(args[0].As<String>()->Utf8Length(env->isolate()));
702 }
703
704 // Normalize val to be an integer in the range of [1, -1] since
705 // implementations of memcmp() can vary by platform.
normalizeCompareVal(int val,size_t a_length,size_t b_length)706 static int normalizeCompareVal(int val, size_t a_length, size_t b_length) {
707 if (val == 0) {
708 if (a_length > b_length)
709 return 1;
710 else if (a_length < b_length)
711 return -1;
712 } else {
713 if (val > 0)
714 return 1;
715 else
716 return -1;
717 }
718 return val;
719 }
720
CompareOffset(const FunctionCallbackInfo<Value> & args)721 void CompareOffset(const FunctionCallbackInfo<Value> &args) {
722 Environment* env = Environment::GetCurrent(args);
723
724 THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
725 THROW_AND_RETURN_UNLESS_BUFFER(env, args[1]);
726 ArrayBufferViewContents<char> source(args[0]);
727 ArrayBufferViewContents<char> target(args[1]);
728
729 size_t target_start = 0;
730 size_t source_start = 0;
731 size_t source_end = 0;
732 size_t target_end = 0;
733
734 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[2], 0, &target_start));
735 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[3], 0, &source_start));
736 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[4], target.length(),
737 &target_end));
738 THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[5], source.length(),
739 &source_end));
740
741 if (source_start > source.length())
742 return THROW_ERR_OUT_OF_RANGE(
743 env, "The value of \"sourceStart\" is out of range.");
744 if (target_start > target.length())
745 return THROW_ERR_OUT_OF_RANGE(
746 env, "The value of \"targetStart\" is out of range.");
747
748 CHECK_LE(source_start, source_end);
749 CHECK_LE(target_start, target_end);
750
751 size_t to_cmp =
752 std::min(std::min(source_end - source_start, target_end - target_start),
753 source.length() - source_start);
754
755 int val = normalizeCompareVal(to_cmp > 0 ?
756 memcmp(source.data() + source_start,
757 target.data() + target_start,
758 to_cmp) : 0,
759 source_end - source_start,
760 target_end - target_start);
761
762 args.GetReturnValue().Set(val);
763 }
764
Compare(const FunctionCallbackInfo<Value> & args)765 void Compare(const FunctionCallbackInfo<Value> &args) {
766 Environment* env = Environment::GetCurrent(args);
767
768 THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
769 THROW_AND_RETURN_UNLESS_BUFFER(env, args[1]);
770 ArrayBufferViewContents<char> a(args[0]);
771 ArrayBufferViewContents<char> b(args[1]);
772
773 size_t cmp_length = std::min(a.length(), b.length());
774
775 int val = normalizeCompareVal(cmp_length > 0 ?
776 memcmp(a.data(), b.data(), cmp_length) : 0,
777 a.length(), b.length());
778 args.GetReturnValue().Set(val);
779 }
780
781
782 // Computes the offset for starting an indexOf or lastIndexOf search.
783 // Returns either a valid offset in [0...<length - 1>], ie inside the Buffer,
784 // or -1 to signal that there is no possible match.
IndexOfOffset(size_t length,int64_t offset_i64,int64_t needle_length,bool is_forward)785 int64_t IndexOfOffset(size_t length,
786 int64_t offset_i64,
787 int64_t needle_length,
788 bool is_forward) {
789 int64_t length_i64 = static_cast<int64_t>(length);
790 if (offset_i64 < 0) {
791 if (offset_i64 + length_i64 >= 0) {
792 // Negative offsets count backwards from the end of the buffer.
793 return length_i64 + offset_i64;
794 } else if (is_forward || needle_length == 0) {
795 // indexOf from before the start of the buffer: search the whole buffer.
796 return 0;
797 } else {
798 // lastIndexOf from before the start of the buffer: no match.
799 return -1;
800 }
801 } else {
802 if (offset_i64 + needle_length <= length_i64) {
803 // Valid positive offset.
804 return offset_i64;
805 } else if (needle_length == 0) {
806 // Out of buffer bounds, but empty needle: point to end of buffer.
807 return length_i64;
808 } else if (is_forward) {
809 // indexOf from past the end of the buffer: no match.
810 return -1;
811 } else {
812 // lastIndexOf from past the end of the buffer: search the whole buffer.
813 return length_i64 - 1;
814 }
815 }
816 }
817
IndexOfString(const FunctionCallbackInfo<Value> & args)818 void IndexOfString(const FunctionCallbackInfo<Value>& args) {
819 Environment* env = Environment::GetCurrent(args);
820 Isolate* isolate = env->isolate();
821
822 CHECK(args[1]->IsString());
823 CHECK(args[2]->IsNumber());
824 CHECK(args[3]->IsInt32());
825 CHECK(args[4]->IsBoolean());
826
827 enum encoding enc = static_cast<enum encoding>(args[3].As<Int32>()->Value());
828
829 THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
830 ArrayBufferViewContents<char> buffer(args[0]);
831
832 Local<String> needle = args[1].As<String>();
833 int64_t offset_i64 = args[2].As<Integer>()->Value();
834 bool is_forward = args[4]->IsTrue();
835
836 const char* haystack = buffer.data();
837 // Round down to the nearest multiple of 2 in case of UCS2.
838 const size_t haystack_length = (enc == UCS2) ?
839 buffer.length() &~ 1 : buffer.length(); // NOLINT(whitespace/operators)
840
841 size_t needle_length;
842 if (!StringBytes::Size(isolate, needle, enc).To(&needle_length)) return;
843
844 int64_t opt_offset = IndexOfOffset(haystack_length,
845 offset_i64,
846 needle_length,
847 is_forward);
848
849 if (needle_length == 0) {
850 // Match String#indexOf() and String#lastIndexOf() behavior.
851 args.GetReturnValue().Set(static_cast<double>(opt_offset));
852 return;
853 }
854
855 if (haystack_length == 0) {
856 return args.GetReturnValue().Set(-1);
857 }
858
859 if (opt_offset <= -1) {
860 return args.GetReturnValue().Set(-1);
861 }
862 size_t offset = static_cast<size_t>(opt_offset);
863 CHECK_LT(offset, haystack_length);
864 if ((is_forward && needle_length + offset > haystack_length) ||
865 needle_length > haystack_length) {
866 return args.GetReturnValue().Set(-1);
867 }
868
869 size_t result = haystack_length;
870
871 if (enc == UCS2) {
872 String::Value needle_value(isolate, needle);
873 if (*needle_value == nullptr)
874 return args.GetReturnValue().Set(-1);
875
876 if (haystack_length < 2 || needle_value.length() < 1) {
877 return args.GetReturnValue().Set(-1);
878 }
879
880 if (IsBigEndian()) {
881 StringBytes::InlineDecoder decoder;
882 if (decoder.Decode(env, needle, enc).IsNothing()) return;
883 const uint16_t* decoded_string =
884 reinterpret_cast<const uint16_t*>(decoder.out());
885
886 if (decoded_string == nullptr)
887 return args.GetReturnValue().Set(-1);
888
889 result = SearchString(reinterpret_cast<const uint16_t*>(haystack),
890 haystack_length / 2,
891 decoded_string,
892 decoder.size() / 2,
893 offset / 2,
894 is_forward);
895 } else {
896 result = SearchString(reinterpret_cast<const uint16_t*>(haystack),
897 haystack_length / 2,
898 reinterpret_cast<const uint16_t*>(*needle_value),
899 needle_value.length(),
900 offset / 2,
901 is_forward);
902 }
903 result *= 2;
904 } else if (enc == UTF8) {
905 String::Utf8Value needle_value(isolate, needle);
906 if (*needle_value == nullptr)
907 return args.GetReturnValue().Set(-1);
908
909 result = SearchString(reinterpret_cast<const uint8_t*>(haystack),
910 haystack_length,
911 reinterpret_cast<const uint8_t*>(*needle_value),
912 needle_length,
913 offset,
914 is_forward);
915 } else if (enc == LATIN1) {
916 uint8_t* needle_data = node::UncheckedMalloc<uint8_t>(needle_length);
917 if (needle_data == nullptr) {
918 return args.GetReturnValue().Set(-1);
919 }
920 needle->WriteOneByte(
921 isolate, needle_data, 0, needle_length, String::NO_NULL_TERMINATION);
922
923 result = SearchString(reinterpret_cast<const uint8_t*>(haystack),
924 haystack_length,
925 needle_data,
926 needle_length,
927 offset,
928 is_forward);
929 free(needle_data);
930 }
931
932 args.GetReturnValue().Set(
933 result == haystack_length ? -1 : static_cast<int>(result));
934 }
935
IndexOfBuffer(const FunctionCallbackInfo<Value> & args)936 void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
937 CHECK(args[1]->IsObject());
938 CHECK(args[2]->IsNumber());
939 CHECK(args[3]->IsInt32());
940 CHECK(args[4]->IsBoolean());
941
942 enum encoding enc = static_cast<enum encoding>(args[3].As<Int32>()->Value());
943
944 THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]);
945 THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[1]);
946 ArrayBufferViewContents<char> haystack_contents(args[0]);
947 ArrayBufferViewContents<char> needle_contents(args[1]);
948 int64_t offset_i64 = args[2].As<Integer>()->Value();
949 bool is_forward = args[4]->IsTrue();
950
951 const char* haystack = haystack_contents.data();
952 const size_t haystack_length = haystack_contents.length();
953 const char* needle = needle_contents.data();
954 const size_t needle_length = needle_contents.length();
955
956 int64_t opt_offset = IndexOfOffset(haystack_length,
957 offset_i64,
958 needle_length,
959 is_forward);
960
961 if (needle_length == 0) {
962 // Match String#indexOf() and String#lastIndexOf() behavior.
963 args.GetReturnValue().Set(static_cast<double>(opt_offset));
964 return;
965 }
966
967 if (haystack_length == 0) {
968 return args.GetReturnValue().Set(-1);
969 }
970
971 if (opt_offset <= -1) {
972 return args.GetReturnValue().Set(-1);
973 }
974 size_t offset = static_cast<size_t>(opt_offset);
975 CHECK_LT(offset, haystack_length);
976 if ((is_forward && needle_length + offset > haystack_length) ||
977 needle_length > haystack_length) {
978 return args.GetReturnValue().Set(-1);
979 }
980
981 size_t result = haystack_length;
982
983 if (enc == UCS2) {
984 if (haystack_length < 2 || needle_length < 2) {
985 return args.GetReturnValue().Set(-1);
986 }
987 result = SearchString(
988 reinterpret_cast<const uint16_t*>(haystack),
989 haystack_length / 2,
990 reinterpret_cast<const uint16_t*>(needle),
991 needle_length / 2,
992 offset / 2,
993 is_forward);
994 result *= 2;
995 } else {
996 result = SearchString(
997 reinterpret_cast<const uint8_t*>(haystack),
998 haystack_length,
999 reinterpret_cast<const uint8_t*>(needle),
1000 needle_length,
1001 offset,
1002 is_forward);
1003 }
1004
1005 args.GetReturnValue().Set(
1006 result == haystack_length ? -1 : static_cast<int>(result));
1007 }
1008
IndexOfNumber(const FunctionCallbackInfo<Value> & args)1009 void IndexOfNumber(const FunctionCallbackInfo<Value>& args) {
1010 CHECK(args[1]->IsUint32());
1011 CHECK(args[2]->IsNumber());
1012 CHECK(args[3]->IsBoolean());
1013
1014 THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]);
1015 ArrayBufferViewContents<char> buffer(args[0]);
1016
1017 uint32_t needle = args[1].As<Uint32>()->Value();
1018 int64_t offset_i64 = args[2].As<Integer>()->Value();
1019 bool is_forward = args[3]->IsTrue();
1020
1021 int64_t opt_offset =
1022 IndexOfOffset(buffer.length(), offset_i64, 1, is_forward);
1023 if (opt_offset <= -1 || buffer.length() == 0) {
1024 return args.GetReturnValue().Set(-1);
1025 }
1026 size_t offset = static_cast<size_t>(opt_offset);
1027 CHECK_LT(offset, buffer.length());
1028
1029 const void* ptr;
1030 if (is_forward) {
1031 ptr = memchr(buffer.data() + offset, needle, buffer.length() - offset);
1032 } else {
1033 ptr = node::stringsearch::MemrchrFill(buffer.data(), needle, offset + 1);
1034 }
1035 const char* ptr_char = static_cast<const char*>(ptr);
1036 args.GetReturnValue().Set(ptr ? static_cast<int>(ptr_char - buffer.data())
1037 : -1);
1038 }
1039
1040
Swap16(const FunctionCallbackInfo<Value> & args)1041 void Swap16(const FunctionCallbackInfo<Value>& args) {
1042 Environment* env = Environment::GetCurrent(args);
1043 THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
1044 SPREAD_BUFFER_ARG(args[0], ts_obj);
1045 SwapBytes16(ts_obj_data, ts_obj_length);
1046 args.GetReturnValue().Set(args[0]);
1047 }
1048
1049
Swap32(const FunctionCallbackInfo<Value> & args)1050 void Swap32(const FunctionCallbackInfo<Value>& args) {
1051 Environment* env = Environment::GetCurrent(args);
1052 THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
1053 SPREAD_BUFFER_ARG(args[0], ts_obj);
1054 SwapBytes32(ts_obj_data, ts_obj_length);
1055 args.GetReturnValue().Set(args[0]);
1056 }
1057
1058
Swap64(const FunctionCallbackInfo<Value> & args)1059 void Swap64(const FunctionCallbackInfo<Value>& args) {
1060 Environment* env = Environment::GetCurrent(args);
1061 THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]);
1062 SPREAD_BUFFER_ARG(args[0], ts_obj);
1063 SwapBytes64(ts_obj_data, ts_obj_length);
1064 args.GetReturnValue().Set(args[0]);
1065 }
1066
1067
1068 // Encode a single string to a UTF-8 Uint8Array (not Buffer).
1069 // Used in TextEncoder.prototype.encode.
EncodeUtf8String(const FunctionCallbackInfo<Value> & args)1070 static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
1071 Environment* env = Environment::GetCurrent(args);
1072 Isolate* isolate = env->isolate();
1073 CHECK_GE(args.Length(), 1);
1074 CHECK(args[0]->IsString());
1075
1076 Local<String> str = args[0].As<String>();
1077 size_t length = str->Utf8Length(isolate);
1078 AllocatedBuffer buf = AllocatedBuffer::AllocateManaged(env, length);
1079 str->WriteUtf8(isolate,
1080 buf.data(),
1081 -1, // We are certain that `data` is sufficiently large
1082 nullptr,
1083 String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
1084 auto array = Uint8Array::New(buf.ToArrayBuffer(), 0, length);
1085 args.GetReturnValue().Set(array);
1086 }
1087
1088
EncodeInto(const FunctionCallbackInfo<Value> & args)1089 static void EncodeInto(const FunctionCallbackInfo<Value>& args) {
1090 Environment* env = Environment::GetCurrent(args);
1091 Isolate* isolate = env->isolate();
1092 CHECK_GE(args.Length(), 3);
1093 CHECK(args[0]->IsString());
1094 CHECK(args[1]->IsUint8Array());
1095 CHECK(args[2]->IsUint32Array());
1096
1097 Local<String> source = args[0].As<String>();
1098
1099 Local<Uint8Array> dest = args[1].As<Uint8Array>();
1100 Local<ArrayBuffer> buf = dest->Buffer();
1101 char* write_result =
1102 static_cast<char*>(buf->GetBackingStore()->Data()) + dest->ByteOffset();
1103 size_t dest_length = dest->ByteLength();
1104
1105 // results = [ read, written ]
1106 Local<Uint32Array> result_arr = args[2].As<Uint32Array>();
1107 uint32_t* results = reinterpret_cast<uint32_t*>(
1108 static_cast<char*>(result_arr->Buffer()->GetBackingStore()->Data()) +
1109 result_arr->ByteOffset());
1110
1111 int nchars;
1112 int written = source->WriteUtf8(
1113 isolate,
1114 write_result,
1115 dest_length,
1116 &nchars,
1117 String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
1118 results[0] = nchars;
1119 results[1] = written;
1120 }
1121
1122
SetBufferPrototype(const FunctionCallbackInfo<Value> & args)1123 void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) {
1124 Environment* env = Environment::GetCurrent(args);
1125
1126 CHECK(args[0]->IsObject());
1127 Local<Object> proto = args[0].As<Object>();
1128 env->set_buffer_prototype_object(proto);
1129 }
1130
1131
Initialize(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)1132 void Initialize(Local<Object> target,
1133 Local<Value> unused,
1134 Local<Context> context,
1135 void* priv) {
1136 Environment* env = Environment::GetCurrent(context);
1137
1138 env->SetMethod(target, "setBufferPrototype", SetBufferPrototype);
1139 env->SetMethodNoSideEffect(target, "createFromString", CreateFromString);
1140
1141 env->SetMethodNoSideEffect(target, "byteLengthUtf8", ByteLengthUtf8);
1142 env->SetMethod(target, "copy", Copy);
1143 env->SetMethodNoSideEffect(target, "compare", Compare);
1144 env->SetMethodNoSideEffect(target, "compareOffset", CompareOffset);
1145 env->SetMethod(target, "fill", Fill);
1146 env->SetMethodNoSideEffect(target, "indexOfBuffer", IndexOfBuffer);
1147 env->SetMethodNoSideEffect(target, "indexOfNumber", IndexOfNumber);
1148 env->SetMethodNoSideEffect(target, "indexOfString", IndexOfString);
1149
1150 env->SetMethod(target, "swap16", Swap16);
1151 env->SetMethod(target, "swap32", Swap32);
1152 env->SetMethod(target, "swap64", Swap64);
1153
1154 env->SetMethod(target, "encodeInto", EncodeInto);
1155 env->SetMethodNoSideEffect(target, "encodeUtf8String", EncodeUtf8String);
1156
1157 target->Set(env->context(),
1158 FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"),
1159 Number::New(env->isolate(), kMaxLength)).Check();
1160
1161 target->Set(env->context(),
1162 FIXED_ONE_BYTE_STRING(env->isolate(), "kStringMaxLength"),
1163 Integer::New(env->isolate(), String::kMaxLength)).Check();
1164
1165 env->SetMethodNoSideEffect(target, "asciiSlice", StringSlice<ASCII>);
1166 env->SetMethodNoSideEffect(target, "base64Slice", StringSlice<BASE64>);
1167 env->SetMethodNoSideEffect(target, "base64urlSlice", StringSlice<BASE64URL>);
1168 env->SetMethodNoSideEffect(target, "latin1Slice", StringSlice<LATIN1>);
1169 env->SetMethodNoSideEffect(target, "hexSlice", StringSlice<HEX>);
1170 env->SetMethodNoSideEffect(target, "ucs2Slice", StringSlice<UCS2>);
1171 env->SetMethodNoSideEffect(target, "utf8Slice", StringSlice<UTF8>);
1172
1173 env->SetMethod(target, "asciiWrite", StringWrite<ASCII>);
1174 env->SetMethod(target, "base64Write", StringWrite<BASE64>);
1175 env->SetMethod(target, "base64urlWrite", StringWrite<BASE64URL>);
1176 env->SetMethod(target, "latin1Write", StringWrite<LATIN1>);
1177 env->SetMethod(target, "hexWrite", StringWrite<HEX>);
1178 env->SetMethod(target, "ucs2Write", StringWrite<UCS2>);
1179 env->SetMethod(target, "utf8Write", StringWrite<UTF8>);
1180
1181 Blob::Initialize(env, target);
1182
1183 // It can be a nullptr when running inside an isolate where we
1184 // do not own the ArrayBuffer allocator.
1185 if (NodeArrayBufferAllocator* allocator =
1186 env->isolate_data()->node_allocator()) {
1187 uint32_t* zero_fill_field = allocator->zero_fill_field();
1188 std::unique_ptr<BackingStore> backing =
1189 ArrayBuffer::NewBackingStore(zero_fill_field,
1190 sizeof(*zero_fill_field),
1191 [](void*, size_t, void*){},
1192 nullptr);
1193 Local<ArrayBuffer> array_buffer =
1194 ArrayBuffer::New(env->isolate(), std::move(backing));
1195 array_buffer->SetPrivate(
1196 env->context(),
1197 env->untransferable_object_private_symbol(),
1198 True(env->isolate())).Check();
1199 CHECK(target
1200 ->Set(env->context(),
1201 FIXED_ONE_BYTE_STRING(env->isolate(), "zeroFill"),
1202 Uint32Array::New(array_buffer, 0, 1))
1203 .FromJust());
1204 }
1205 }
1206
1207 } // anonymous namespace
1208 } // namespace Buffer
1209 } // namespace node
1210
1211 NODE_MODULE_CONTEXT_AWARE_INTERNAL(buffer, node::Buffer::Initialize)
1212