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