Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:05:22

0001 // Copyright Joyent, Inc. and other Node contributors.
0002 //
0003 // Permission is hereby granted, free of charge, to any person obtaining a
0004 // copy of this software and associated documentation files (the
0005 // "Software"), to deal in the Software without restriction, including
0006 // without limitation the rights to use, copy, modify, merge, publish,
0007 // distribute, sublicense, and/or sell copies of the Software, and to permit
0008 // persons to whom the Software is furnished to do so, subject to the
0009 // following conditions:
0010 //
0011 // The above copyright notice and this permission notice shall be included
0012 // in all copies or substantial portions of the Software.
0013 //
0014 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0015 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0016 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
0017 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
0018 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0019 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0020 // USE OR OTHER DEALINGS IN THE SOFTWARE.
0021 
0022 #ifndef SRC_NODE_BUFFER_H_
0023 #define SRC_NODE_BUFFER_H_
0024 
0025 #include "node.h"
0026 #include "v8.h"
0027 
0028 namespace node {
0029 
0030 namespace Buffer {
0031 
0032 static const size_t kMaxLength = v8::Uint8Array::kMaxLength;
0033 
0034 typedef void (*FreeCallback)(char* data, void* hint);
0035 
0036 NODE_EXTERN bool HasInstance(v8::Local<v8::Value> val);
0037 NODE_EXTERN bool HasInstance(v8::Local<v8::Object> val);
0038 NODE_EXTERN char* Data(v8::Local<v8::Value> val);
0039 NODE_EXTERN char* Data(v8::Local<v8::Object> val);
0040 NODE_EXTERN size_t Length(v8::Local<v8::Value> val);
0041 NODE_EXTERN size_t Length(v8::Local<v8::Object> val);
0042 
0043 // public constructor - data is copied
0044 NODE_EXTERN v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate,
0045                                             const char* data,
0046                                             size_t len);
0047 
0048 // public constructor
0049 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate, size_t length);
0050 
0051 // public constructor from string
0052 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
0053                                            v8::Local<v8::String> string,
0054                                            enum encoding enc = UTF8);
0055 
0056 // public constructor - data is used, callback is passed data on object gc
0057 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
0058                                            char* data,
0059                                            size_t length,
0060                                            FreeCallback callback,
0061                                            void* hint);
0062 
0063 // public constructor - data is used.
0064 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
0065                                            char* data,
0066                                            size_t len);
0067 
0068 // Creates a Buffer instance over an existing ArrayBuffer.
0069 NODE_EXTERN v8::MaybeLocal<v8::Uint8Array> New(v8::Isolate* isolate,
0070                                                v8::Local<v8::ArrayBuffer> ab,
0071                                                size_t byte_offset,
0072                                                size_t length);
0073 
0074 // This is verbose to be explicit with inline commenting
0075 static inline bool IsWithinBounds(size_t off, size_t len, size_t max) {
0076   // Asking to seek too far into the buffer
0077   // check to avoid wrapping in subsequent subtraction
0078   if (off > max)
0079     return false;
0080 
0081   // Asking for more than is left over in the buffer
0082   if (max - off < len)
0083     return false;
0084 
0085   // Otherwise we're in bounds
0086   return true;
0087 }
0088 
0089 }  // namespace Buffer
0090 }  // namespace node
0091 
0092 #endif  // SRC_NODE_BUFFER_H_