Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow an offset as the third argument to the "reinterpret" functions

  • Loading branch information...
commit 0ec559a7b694264928e104e6358bea5071cc0749 1 parent de2315c
@TooTallNate authored
Showing with 16 additions and 7 deletions.
  1. +8 −4 lib/ref.js
  2. +8 −3 src/binding.cc
View
12 lib/ref.js
@@ -747,6 +747,7 @@ exports.writePointer = function writePointer (buf, offset, ptr) {
*
* @param {Buffer} buffer A Buffer instance to base the returned Buffer off of.
* @param {Number} size The `length` property of the returned Buffer.
+ * @param {Number} offset The offset of the Buffer to begin from.
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and the requested _size_.
* @api private
*/
@@ -762,12 +763,13 @@ exports._reinterpret = exports.reinterpret
*
* @param {Buffer} buffer A Buffer instance to base the returned Buffer off of.
* @param {Number} size The `length` property of the returned Buffer.
+ * @param {Number} offset The offset of the Buffer to begin from.
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and the requested _size_.
*/
-exports.reinterpret = function reinterpret (buffer, size) {
+exports.reinterpret = function reinterpret (buffer, size, offset) {
debug('reinterpreting buffer to "%d" bytes', size)
- var rtn = exports._reinterpret(buffer, size)
+ var rtn = exports._reinterpret(buffer, size, offset || 0)
exports._attach(rtn, buffer)
return rtn
}
@@ -779,6 +781,7 @@ exports.reinterpret = function reinterpret (buffer, size) {
*
* @param {Buffer} buffer A Buffer instance to base the returned Buffer off of.
* @param {Number} size The number of sequential, aligned `NULL` bytes that are required to terminate the buffer.
+ * @param {Number} offset The offset of the Buffer to begin from.
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and a variable `length` that is terminated by _size_ NUL bytes.
* @api private
*/
@@ -804,12 +807,13 @@ exports._reinterpretUntilZeros = exports.reinterpretUntilZeros
*
* @param {Buffer} buffer A Buffer instance to base the returned Buffer off of.
* @param {Number} size The number of sequential, aligned `NULL` bytes are required to terminate the buffer.
+ * @param {Number} offset The offset of the Buffer to begin from.
* @return {Buffer} A new Buffer instance with the same memory address as _buffer_, and a variable `length` that is terminated by _size_ NUL bytes.
*/
-exports.reinterpretUntilZeros = function reinterpretUntilZeros (buffer, size) {
+exports.reinterpretUntilZeros = function reinterpretUntilZeros (buffer, size, offset) {
debug('reinterpreting buffer to until "%d" NULL (0) bytes are found', size)
- var rtn = exports._reinterpretUntilZeros(buffer, size)
+ var rtn = exports._reinterpretUntilZeros(buffer, size, offset || 0)
exports._attach(rtn, buffer)
return rtn
}
View
11 src/binding.cc
@@ -467,6 +467,7 @@ Handle<Value> ReadCString(const Arguments& args) {
*
* args[0] - Buffer - the "buf" Buffer instance to read the address from
* args[1] - Number - the size in bytes that the returned Buffer should be
+ * args[2] - Number - the offset from the "buf" buffer's address to read from
*/
Handle<Value> ReinterpretBuffer(const Arguments& args) {
@@ -478,14 +479,16 @@ Handle<Value> ReinterpretBuffer(const Arguments& args) {
String::New("reinterpret: Buffer instance expected")));
}
- char *ptr = Buffer::Data(buf.As<Object>());
- size_t size = args[1]->Uint32Value();
+ int64_t offset = args[2]->IntegerValue();
+ char *ptr = Buffer::Data(buf.As<Object>()) + offset;
if (ptr == NULL) {
return ThrowException(Exception::Error(
String::New("reinterpret: Cannot reinterpret from NULL pointer")));
}
+ size_t size = args[1]->Uint32Value();
+
Buffer *rtn = Buffer::New(ptr, size, read_pointer_cb, NULL);
return scope.Close(rtn->handle_);
@@ -498,6 +501,7 @@ Handle<Value> ReinterpretBuffer(const Arguments& args) {
*
* args[0] - Buffer - the "buf" Buffer instance to read the address from
* args[1] - Number - the number of sequential 0-byte values that need to be read
+ * args[2] - Number - the offset from the "buf" buffer's address to read from
*/
Handle<Value> ReinterpretBufferUntilZeros(const Arguments& args) {
@@ -509,7 +513,8 @@ Handle<Value> ReinterpretBufferUntilZeros(const Arguments& args) {
String::New("reinterpretUntilZeros: Buffer instance expected")));
}
- char *ptr = Buffer::Data(buf.As<Object>());
+ int64_t offset = args[2]->IntegerValue();
+ char *ptr = Buffer::Data(buf.As<Object>()) + offset;
if (ptr == NULL) {
return ThrowException(Exception::Error(
Please sign in to comment.
Something went wrong with that request. Please try again.