diff --git a/browser.js b/browser.js index b96d7be..0424500 100644 --- a/browser.js +++ b/browser.js @@ -466,6 +466,74 @@ function readInt32LE (buffer, offset) { return view.getInt32(offset, true) } +function writeDoubleBE (buffer, value, offset) { + if (offset === undefined) offset = 0 + + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) + view.setFloat64(offset, value, false) + + return offset + 8 +} + +function writeFloatBE (buffer, value, offset) { + if (offset === undefined) offset = 0 + + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) + view.setFloat32(offset, value, false) + + return offset + 4 +} + +function writeUInt32BE (buffer, value, offset) { + if (offset === undefined) offset = 0 + + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) + view.setUint32(offset, value, false) + + return offset + 4 +} + +function writeInt32BE (buffer, value, offset) { + if (offset === undefined) offset = 0 + + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) + view.setInt32(offset, value, false) + + return offset + 4 +} + +function readDoubleBE (buffer, offset) { + if (offset === undefined) offset = 0 + + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) + + return view.getFloat64(offset, false) +} + +function readFloatBE (buffer, offset) { + if (offset === undefined) offset = 0 + + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) + + return view.getFloat32(offset, false) +} + +function readUInt32BE (buffer, offset) { + if (offset === undefined) offset = 0 + + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) + + return view.getUint32(offset, false) +} + +function readInt32BE (buffer, offset) { + if (offset === undefined) offset = 0 + + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) + + return view.getInt32(offset, false) +} + module.exports = exports = { isBuffer, isEncoding, @@ -495,5 +563,13 @@ module.exports = exports = { readDoubleLE, readFloatLE, readUInt32LE, - readInt32LE + readInt32LE, + writeDoubleBE, + writeFloatBE, + writeUInt32BE, + writeInt32BE, + readDoubleBE, + readFloatBE, + readUInt32BE, + readInt32BE } diff --git a/index.js b/index.js index a751d6b..5c807d8 100644 --- a/index.js +++ b/index.js @@ -115,6 +115,38 @@ function readInt32LE (buffer, offset) { return toBuffer(buffer).readInt32LE(offset) } +function writeDoubleBE (buffer, value, offset) { + return toBuffer(buffer).writeDoubleBE(value, offset) +} + +function writeFloatBE (buffer, value, offset) { + return toBuffer(buffer).writeFloatBE(value, offset) +} + +function writeUInt32BE (buffer, value, offset) { + return toBuffer(buffer).writeUInt32BE(value, offset) +} + +function writeInt32BE (buffer, value, offset) { + return toBuffer(buffer).writeInt32BE(value, offset) +} + +function readDoubleBE (buffer, offset) { + return toBuffer(buffer).readDoubleBE(offset) +} + +function readFloatBE (buffer, offset) { + return toBuffer(buffer).readFloatBE(offset) +} + +function readUInt32BE (buffer, offset) { + return toBuffer(buffer).readUInt32BE(offset) +} + +function readInt32BE (buffer, offset) { + return toBuffer(buffer).readInt32BE(offset) +} + module.exports = { isBuffer, isEncoding, @@ -144,5 +176,14 @@ module.exports = { readDoubleLE, readFloatLE, readUInt32LE, - readInt32LE + readInt32LE, + writeDoubleBE, + writeFloatBE, + writeUInt32BE, + writeInt32BE, + readDoubleBE, + readFloatBE, + readUInt32BE, + readInt32BE + } diff --git a/test/browser.mjs b/test/browser.mjs index a7cfdb8..6171fe4 100644 --- a/test/browser.mjs +++ b/test/browser.mjs @@ -406,3 +406,192 @@ test('readInt32LE', (t) => { t.is(actual, expected) }) }) + +test('writeFloatBE', (t) => { + t.test('offset 0', (t) => { + const buffer = b.alloc(4) + const updatedOffset = b.writeFloatBE(buffer, 123.5) + + t.is(updatedOffset, 4) + t.is(new DataView(buffer.buffer).getFloat32(0, false), 123.5) + }) + + t.test('other offset', (t) => { + const buffer = b.alloc(8) + const updatedOffset = b.writeFloatBE(buffer, 123.5, 4) + + t.is(updatedOffset, 8) + t.is(new DataView(buffer.buffer).getFloat32(4, false), 123.5) + }) + + t.test('sub-buffer', (t) => { + const buffer = b.alloc(8) + const sub = buffer.subarray(2) + + b.writeFloatBE(sub, 123.5) + + t.is(new DataView(buffer.buffer).getFloat32(2, false), 123.5) + }) +}) + +test('writeUInt32BE', (t) => { + t.test('offset 0', (t) => { + const buffer = b.alloc(4) + const updatedOffset = b.writeUInt32BE(buffer, 123) + + t.is(updatedOffset, 4) + t.is(new DataView(buffer.buffer).getUint32(0, false), 123) + }) + + t.test('other offset', (t) => { + const buffer = b.alloc(8) + const updatedOffset = b.writeUInt32BE(buffer, 123, 4) + + t.is(updatedOffset, 8) + t.is(new DataView(buffer.buffer).getUint32(4, false), 123) + }) + + t.test('sub-buffer', (t) => { + const buffer = b.alloc(8) + const sub = buffer.subarray(2) + + b.writeUInt32BE(sub, 123) + + t.is(new DataView(buffer.buffer).getUint32(2, false), 123) + }) +}) + +test('writeInt32BE', (t) => { + t.test('offset 0', (t) => { + const buffer = b.alloc(4) + const updatedOffset = b.writeInt32BE(buffer, 123) + + t.is(updatedOffset, 4) + t.is(new DataView(buffer.buffer).getInt32(0, false), 123) + }) + + t.test('other offset', (t) => { + const buffer = b.alloc(8) + const updatedOffset = b.writeInt32BE(buffer, 123, 4) + + t.is(updatedOffset, 8) + t.is(new DataView(buffer.buffer).getInt32(4, false), 123) + }) + + t.test('sub-buffer', (t) => { + const buffer = b.alloc(8) + const sub = buffer.subarray(2) + + b.writeInt32BE(sub, 123) + + t.is(new DataView(buffer.buffer).getInt32(2, false), 123) + }) +}) + +test('readDoubleBE', (t) => { + const expected = 5.447603722011605e-270 + + t.test('offset 0', (t) => { + const buffer = new Uint8Array([8, 7, 6, 5, 4, 3, 2, 1]) + + const actual = b.readDoubleBE(buffer) + t.is(actual, expected) + }) + + t.test('other offset', (t) => { + const buffer = new Uint8Array( + [...new Array(8).fill(0), ...[8, 7, 6, 5, 4, 3, 2, 1]] + ) + + const actual = b.readDoubleBE(buffer, 8) + t.is(actual, expected) + }) + + t.test('sub-buffer', (t) => { + const buffer = new Uint8Array( + [...new Array(4).fill(0), ...[8, 7, 6, 5, 4, 3, 2, 1]] + ) + const sub = buffer.subarray(4) + + const actual = b.readDoubleBE(sub) + t.is(actual, expected) + }) +}) + +test('readFloatBE', (t) => { + const expected = 1.539989614439558e-36 + + t.test('offset 0', (t) => { + const buffer = new Uint8Array([4, 3, 2, 1]) + + const actual = b.readFloatBE(buffer) + t.is(actual, expected) + }) + + t.test('other offset', (t) => { + const buffer = new Uint8Array([0, 0, 0, 0, 4, 3, 2, 1]) + + const actual = b.readFloatBE(buffer, 4) + t.is(actual, expected) + }) + + t.test('sub-buffer', (t) => { + const buffer = new Uint8Array([0, 0, 4, 3, 2, 1]) + const sub = buffer.subarray(2) + + const actual = b.readFloatBE(sub) + t.is(actual, expected) + }) +}) + +test('readUInt32BE', (t) => { + const expected = '78563412' + + t.test('Offset 0', (t) => { + const buffer = new Uint8Array([0x78, 0x56, 0x34, 0x12]) + + const actual = b.readUInt32BE(buffer).toString(16) + t.is(actual, expected) + }) + + t.test('other offset', (t) => { + const buffer = new Uint8Array([0, 0, 0, 0, 0x78, 0x56, 0x34, 0x12]) + + const actual = b.readUInt32BE(buffer, 4).toString(16) + t.is(actual, expected) + }) + + t.test('sub-buffer', (t) => { + const buffer = new Uint8Array([0, 0, 0x78, 0x56, 0x34, 0x12]) + const sub = buffer.subarray(2) + + const actual = b.readUInt32BE(sub).toString(16) + t.is(actual, expected) + }) +}) + +test('readInt32BE', (t) => { + const expected = 83886080 + + t.test('Offset 0', (t) => { + const buffer = new Uint8Array([5, 0, 0, 0]) + + const actual = b.readInt32BE(buffer) + t.is(actual, expected) + }) + + t.test('other offset', (t) => { + const buffer = new Uint8Array([0, 0, 0, 0, 5, 0, 0, 0]) + + const actual = b.readInt32BE(buffer, 4) + t.is(actual, expected) + }) + + t.test('sub-buffer', (t) => { + const buffer = new Uint8Array([0, 0, 5, 0, 0, 0]) + const sub = buffer.subarray(2) + + const actual = b.readInt32BE(sub) + t.is(actual, expected) + }) +}) diff --git a/test/node.mjs b/test/node.mjs index 0d5de88..cb37f67 100644 --- a/test/node.mjs +++ b/test/node.mjs @@ -158,6 +158,216 @@ test('readInt32LE', (t) => { }) }) +test('writeDoubleBE', (t) => { + const value = 123.456 + + const expectedBuffer = Buffer.from('405edd2f1a9fbe77', 'hex') + + t.test('from buffer', (t) => { + const buffer = Buffer.alloc(8) + const bufferOffset = b.writeDoubleBE(buffer, value, 0) + t.is(bufferOffset, 8) + t.alike(buffer, expectedBuffer) + }) + + t.test('from array', (t) => { + const array = new Uint8Array(8) + const arrayOffset = b.writeDoubleBE(array, value, 0) + t.is(arrayOffset, 8) + t.alike(Buffer.from(array), expectedBuffer) + }) +}) + +test('writeFloatBE', (t) => { + const value = 0xcafebabe + + const expectedBuffer = Buffer.from('4f4afebb', 'hex') + + t.test('from buffer', (t) => { + const buffer = Buffer.alloc(4) + const bufferOffset = b.writeFloatBE(buffer, value, 0) + t.is(bufferOffset, 4) + t.alike(buffer, expectedBuffer) + }) + + t.test('from array', (t) => { + const array = new Uint8Array(4) + const arrayOffset = b.writeFloatBE(array, value, 0) + t.is(arrayOffset, 4) + t.alike(Buffer.from(array), expectedBuffer) + }) +}) + +test('writeUInt32BE', (t) => { + const value = 0xfeedface + + const expectedBuffer = Buffer.from('feedface', 'hex') + + t.test('from buffer', (t) => { + const buffer = Buffer.alloc(4) + const bufferOffset = b.writeUInt32BE(buffer, value, 0) + t.is(bufferOffset, 4) + t.alike(buffer, expectedBuffer) + }) + + t.test('from array', (t) => { + const array = new Uint8Array(4) + const arrayOffset = b.writeUInt32BE(array, value, 0) + t.is(arrayOffset, 4) + t.alike(Buffer.from(array), expectedBuffer) + }) +}) + +test('writeInt32BE', (t) => { + const value = 0x05060708 + + const expectedBuffer = Buffer.from('05060708', 'hex') + + t.test('from buffer', (t) => { + const buffer = Buffer.alloc(4) + const bufferOffset = b.writeInt32BE(buffer, value, 0) + t.is(bufferOffset, 4) + t.alike(buffer, expectedBuffer) + }) + + t.test('from array', (t) => { + const array = new Uint8Array(4) + const arrayOffset = b.writeInt32BE(array, value, 0) + t.is(arrayOffset, 4) + t.alike(Buffer.from(array), expectedBuffer) + }) +}) + +test('writeInt32BE - top bit set', (t) => { + const minus1 = -1 + const minusMax = -1 * 0x80000000 + const minusMaxSub1 = -1 * 0x7fffffff + + const expectedBuffer = Buffer.from('ffffffff', 'hex') + const expectedMax = Buffer.from('80000000', 'hex') + const expectedMaxSub1 = Buffer.from('80000001', 'hex') + + t.test('from buffer', (t) => { + const buffer = Buffer.alloc(4) + + t.is(b.writeInt32BE(buffer, minus1, 0), 4) + t.alike(buffer, expectedBuffer) + + t.is(b.writeInt32BE(buffer, minusMaxSub1, 0), 4) + t.alike(buffer, expectedMaxSub1) + + t.is(b.writeInt32BE(buffer, minusMax, 0), 4) + t.alike(buffer, expectedMax) + }) + + t.test('from array', (t) => { + const array = new Uint8Array(4) + + t.is(b.writeInt32BE(array, minus1, 0), 4) + t.alike(Buffer.from(array), expectedBuffer) + + t.is(b.writeInt32BE(array, minusMaxSub1, 0), 4) + t.alike(Buffer.from(array), expectedMaxSub1) + + t.is(b.writeInt32BE(array, minusMax, 0), 4) + t.alike(Buffer.from(array), expectedMax) + }) +}) + +test('readDoubleBE', (t) => { + t.test('from buffer', (t) => { + const buffer = Buffer.from([8, 7, 6, 5, 4, 3, 2, 1]) + + const actual = b.readDoubleBE(buffer) + const expected = 5.447603722011605e-270 + t.is(actual, expected) + }) + + t.test('from uint8Array', (t) => { + const buffer = new Uint8Array([8, 7, 6, 5, 4, 3, 2, 1]) + + const actual = b.readDoubleBE(buffer) + const expected = 5.447603722011605e-270 + t.is(actual, expected) + }) +}) + +test('readFloatBE', (t) => { + t.test('from buffer', (t) => { + const buffer = Buffer.from([4, 3, 2, 1]) + + const actual = b.readFloatBE(buffer) + const expected = 1.539989614439558e-36 + t.is(actual, expected) + }) + + t.test('from uint8Array', (t) => { + const buffer = new Uint8Array([4, 3, 2, 1]) + + const actual = b.readFloatBE(buffer) + const expected = 1.539989614439558e-36 + t.is(actual, expected) + }) +}) + +test('readUInt32BE', (t) => { + t.test('from buffer', (t) => { + const buffer = Buffer.from([0x78, 0x56, 0x34, 0x12]) + + const actual = b.readUInt32BE(buffer) + const expected = 0x78563412 + t.is(actual, expected) + }) + + t.test('from uint8Array', (t) => { + const buffer = new Uint8Array([0x78, 0x56, 0x34, 0x12]) + + const actual = b.readUInt32BE(buffer) + const expected = 0x78563412 + t.is(actual, expected) + }) +}) + +test('readInt32BE', (t) => { + t.test('from buffer', (t) => { + const buffer = Buffer.from([5, 0, 0, 0]) + + const actual = b.readInt32BE(buffer) + const expected = 83886080 + t.is(actual, expected) + }) + + t.test('from uint8Array', (t) => { + const buffer = new Uint8Array([5, 0, 0, 0]) + + const actual = b.readInt32BE(buffer) + const expected = 83886080 + t.is(actual, expected) + }) +}) + +test('readInt32BE - top bit set', (t) => { + t.test('from buffer', (t) => { + const bufferMinus1 = Buffer.from([0xff, 0xff, 0xff, 0xff]) + const bufferMax = Buffer.from([0x80, 0, 0, 0]) + const bufferMaxSub1 = Buffer.from([0x80, 0, 0, 1]) + + t.is(b.readInt32BE(bufferMinus1), -1) + t.is(b.readInt32BE(bufferMaxSub1), -1 * 0x7fffffff) + t.is(b.readInt32BE(bufferMax), -1 * 0x80000000) + }) + + t.test('from array', (t) => { + const bufferMinus1 = new Uint8Array([0xff, 0xff, 0xff, 0xff]) + const bufferMax = new Uint8Array([0x80, 0, 0, 0]) + const bufferMaxSub1 = new Uint8Array([0x80, 0, 0, 1]) + + t.is(b.readInt32BE(bufferMinus1), -1) + t.is(b.readInt32BE(bufferMaxSub1), -1 * 0x7fffffff) + t.is(b.readInt32BE(bufferMax), -1 * 0x80000000) + }) +}) + test('browser.js and index.js export same functions', async (t) => { const { default: browser } = await import('../browser.js')