summaryrefslogtreecommitdiff
path: root/lib/buffer.js
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2011-05-15 21:25:11 -0400
committerRyan Dahl <ry@tinyclouds.org>2011-05-15 18:39:07 -0700
commite505a1215c5e1243c8790bf7d23c96c27900fff7 (patch)
tree12293f586a0ddb4362410943b9ec4f79a25b6c29 /lib/buffer.js
parent91bd144d2ca38843bd35f2d7b06030cd730a03ff (diff)
downloadnode-e505a1215c5e1243c8790bf7d23c96c27900fff7.tar.gz
Add reading/writing of floats and doubles from/to buffers
Code for readIEEE754/writeIEEE754 is from jspack: http://code.google.com/p/jspack/
Diffstat (limited to 'lib/buffer.js')
-rw-r--r--lib/buffer.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index 5df6f915f..b47dd715a 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -20,6 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var SlowBuffer = process.binding('buffer').SlowBuffer;
+var IEEE754 = require('buffer_ieee754');
var assert = require('assert');
@@ -684,6 +685,43 @@ Buffer.prototype.readInt32 = function(offset, endian) {
};
+Buffer.prototype.readFloat = function(offset, endian) {
+ var buffer = this;
+
+ assert.ok(endian !== undefined && endian !== null,
+ 'missing endian');
+
+ assert.ok(endian == 'big' || endian == 'little',
+ 'bad endian value');
+
+ assert.ok(offset !== undefined && offset !== null,
+ 'missing offset');
+
+ assert.ok(offset + 3 < buffer.length,
+ 'Trying to read beyond buffer length');
+
+ return IEEE754.readIEEE754(buffer, offset, endian, 23, 4);
+};
+
+Buffer.prototype.readDouble = function(offset, endian) {
+ var buffer = this;
+
+ assert.ok(endian !== undefined && endian !== null,
+ 'missing endian');
+
+ assert.ok(endian == 'big' || endian == 'little',
+ 'bad endian value');
+
+ assert.ok(offset !== undefined && offset !== null,
+ 'missing offset');
+
+ assert.ok(offset + 7 < buffer.length,
+ 'Trying to read beyond buffer length');
+
+ return IEEE754.readIEEE754(buffer, offset, endian, 52, 8);
+};
+
+
/*
* We have to make sure that the value is a valid integer. This means that it is
* non-negative. It has no fractional component and that it does not exceed the
@@ -843,6 +881,17 @@ function verifsint(value, max, min) {
assert.ok(Math.floor(value) === value, 'value has a fractional component');
}
+
+function verifIEEE754(value, max, min) {
+ assert.ok(typeof (value) == 'number',
+ 'cannot write a non-number as a number');
+
+ assert.ok(value <= max, 'value larger than maximum allowed value');
+
+ assert.ok(value >= min, 'value smaller than minimum allowed value');
+}
+
+
Buffer.prototype.writeInt8 = function(value, offset, endian) {
var buffer = this;
@@ -924,3 +973,49 @@ Buffer.prototype.writeInt32 = function(value, offset, endian) {
buffer.writeUInt32(0xffffffff + value + 1, offset, endian);
}
};
+
+
+Buffer.prototype.writeFloat = function(value, offset, endian) {
+ var buffer = this;
+
+ assert.ok(value !== undefined && value !== null,
+ 'missing value');
+
+ assert.ok(endian !== undefined && endian !== null,
+ 'missing endian');
+
+ assert.ok(endian == 'big' || endian == 'little',
+ 'bad endian value');
+
+ assert.ok(offset !== undefined && offset !== null,
+ 'missing offset');
+
+ assert.ok(offset + 3 < buffer.length,
+ 'Trying to read beyond buffer length');
+
+ verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38);
+ IEEE754.writeIEEE754(buffer, value, offset, endian, 23, 4);
+};
+
+
+Buffer.prototype.writeDouble = function(value, offset, endian) {
+ var buffer = this;
+
+ assert.ok(value !== undefined && value !== null,
+ 'missing value');
+
+ assert.ok(endian !== undefined && endian !== null,
+ 'missing endian');
+
+ assert.ok(endian == 'big' || endian == 'little',
+ 'bad endian value');
+
+ assert.ok(offset !== undefined && offset !== null,
+ 'missing offset');
+
+ assert.ok(offset + 7 < buffer.length,
+ 'Trying to read beyond buffer length');
+
+ verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308);
+ IEEE754.writeIEEE754(buffer, value, offset, endian, 52, 8);
+};