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 #ifndef SRC_NODE_BUFFER_H_
23 #define SRC_NODE_BUFFER_H_
24 
25 #include "node.h"
26 #include "v8.h"
27 
28 namespace node {
29 
30 // TODO(addaleax): Remove this.
31 NODE_DEPRECATED("use command-line flags",
32                 extern bool zero_fill_all_buffers);
33 
34 namespace Buffer {
35 
36 static const unsigned int kMaxLength = v8::TypedArray::kMaxLength;
37 
38 typedef void (*FreeCallback)(char* data, void* hint);
39 
40 NODE_EXTERN bool HasInstance(v8::Local<v8::Value> val);
41 NODE_EXTERN bool HasInstance(v8::Local<v8::Object> val);
42 NODE_EXTERN char* Data(v8::Local<v8::Value> val);
43 NODE_EXTERN char* Data(v8::Local<v8::Object> val);
44 NODE_EXTERN size_t Length(v8::Local<v8::Value> val);
45 NODE_EXTERN size_t Length(v8::Local<v8::Object> val);
46 
47 // public constructor - data is copied
48 NODE_EXTERN v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate,
49                                             const char* data,
50                                             size_t len);
51 
52 // public constructor
53 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate, size_t length);
54 
55 // public constructor from string
56 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
57                                            v8::Local<v8::String> string,
58                                            enum encoding enc = UTF8);
59 
60 // public constructor - data is used, callback is passed data on object gc
61 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
62                                            char* data,
63                                            size_t length,
64                                            FreeCallback callback,
65                                            void* hint);
66 
67 // public constructor - data is used.
68 NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
69                                            char* data,
70                                            size_t len);
71 
72 // This is verbose to be explicit with inline commenting
IsWithinBounds(size_t off,size_t len,size_t max)73 static inline bool IsWithinBounds(size_t off, size_t len, size_t max) {
74   // Asking to seek too far into the buffer
75   // check to avoid wrapping in subsequent subtraction
76   if (off > max)
77     return false;
78 
79   // Asking for more than is left over in the buffer
80   if (max - off < len)
81     return false;
82 
83   // Otherwise we're in bounds
84   return true;
85 }
86 
87 }  // namespace Buffer
88 }  // namespace node
89 
90 #endif  // SRC_NODE_BUFFER_H_
91