11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci#ifndef SRC_NODE_BUFFER_H_ 231cb0ef41Sopenharmony_ci#define SRC_NODE_BUFFER_H_ 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci#include "node.h" 261cb0ef41Sopenharmony_ci#include "v8.h" 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cinamespace node { 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_cinamespace Buffer { 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_cistatic const size_t kMaxLength = v8::TypedArray::kMaxLength; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_citypedef void (*FreeCallback)(char* data, void* hint); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciNODE_EXTERN bool HasInstance(v8::Local<v8::Value> val); 371cb0ef41Sopenharmony_ciNODE_EXTERN bool HasInstance(v8::Local<v8::Object> val); 381cb0ef41Sopenharmony_ciNODE_EXTERN char* Data(v8::Local<v8::Value> val); 391cb0ef41Sopenharmony_ciNODE_EXTERN char* Data(v8::Local<v8::Object> val); 401cb0ef41Sopenharmony_ciNODE_EXTERN size_t Length(v8::Local<v8::Value> val); 411cb0ef41Sopenharmony_ciNODE_EXTERN size_t Length(v8::Local<v8::Object> val); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci// public constructor - data is copied 441cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate, 451cb0ef41Sopenharmony_ci const char* data, 461cb0ef41Sopenharmony_ci size_t len); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci// public constructor 491cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate, size_t length); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci// public constructor from string 521cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate, 531cb0ef41Sopenharmony_ci v8::Local<v8::String> string, 541cb0ef41Sopenharmony_ci enum encoding enc = UTF8); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci// public constructor - data is used, callback is passed data on object gc 571cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate, 581cb0ef41Sopenharmony_ci char* data, 591cb0ef41Sopenharmony_ci size_t length, 601cb0ef41Sopenharmony_ci FreeCallback callback, 611cb0ef41Sopenharmony_ci void* hint); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci// public constructor - data is used. 641cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate, 651cb0ef41Sopenharmony_ci char* data, 661cb0ef41Sopenharmony_ci size_t len); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci// Creates a Buffer instance over an existing ArrayBuffer. 691cb0ef41Sopenharmony_ciNODE_EXTERN v8::MaybeLocal<v8::Uint8Array> New(v8::Isolate* isolate, 701cb0ef41Sopenharmony_ci v8::Local<v8::ArrayBuffer> ab, 711cb0ef41Sopenharmony_ci size_t byte_offset, 721cb0ef41Sopenharmony_ci size_t length); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci// This is verbose to be explicit with inline commenting 751cb0ef41Sopenharmony_cistatic inline bool IsWithinBounds(size_t off, size_t len, size_t max) { 761cb0ef41Sopenharmony_ci // Asking to seek too far into the buffer 771cb0ef41Sopenharmony_ci // check to avoid wrapping in subsequent subtraction 781cb0ef41Sopenharmony_ci if (off > max) 791cb0ef41Sopenharmony_ci return false; 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci // Asking for more than is left over in the buffer 821cb0ef41Sopenharmony_ci if (max - off < len) 831cb0ef41Sopenharmony_ci return false; 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci // Otherwise we're in bounds 861cb0ef41Sopenharmony_ci return true; 871cb0ef41Sopenharmony_ci} 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci} // namespace Buffer 901cb0ef41Sopenharmony_ci} // namespace node 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci#endif // SRC_NODE_BUFFER_H_ 93