[Pkg-javascript-commits] [node-sha.js] 06/237: refactor util functions out
Bastien Roucariès
rouca at moszumanska.debian.org
Fri May 5 09:02:50 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-sha.js.
commit 283f1923bdf0bfe48167f240062071cdd2340f78
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date: Wed Dec 25 23:16:37 2013 +0700
refactor util functions out
---
test/reverse.js | 6 ++----
test/write.js | 19 +++++++++++++---
util.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 85 insertions(+), 7 deletions(-)
diff --git a/test/reverse.js b/test/reverse.js
index 1185d9f..b143c65 100644
--- a/test/reverse.js
+++ b/test/reverse.js
@@ -1,7 +1,5 @@
-
-
-var reverseByteOrder = require('../').reverseByteOrder
-var Uint32toHex = require('../').Uint32toHex
+var reverseByteOrder = require('../util').reverseByteOrder
+var Uint32toHex = require('../util').Uint32toHex
var tape = require('tape')
diff --git a/test/write.js b/test/write.js
index 43d41d1..37581e8 100644
--- a/test/write.js
+++ b/test/write.js
@@ -1,6 +1,7 @@
var tape = require('tape')
-var write = require('../').write
+var write = require('../util').write
+var hexpp = require('../hexpp')
tape('supports bigendian', function (t) {
@@ -10,9 +11,21 @@ tape('supports bigendian', function (t) {
write(actual, 'hello there.', 'ascii', 0, 0, 12)
- console.log(actual.buffer)
- console.log(expected.buffer)
+ console.log(hexpp(actual.buffer))
+ console.log(hexpp(expected.buffer))
t.deepEqual(expected, actual)
t.end()
})
+
+tape('several writes', function (t) {
+ var actual = new Uint8Array(9)
+ write(actual, 'foo', 'ascii', 0, 0, 3)
+ write(actual, 'bar', 'ascii', 3, 0, 3)
+ write(actual, 'baz', 'ascii', 6, 0, 3)
+ var expected = new Uint8Array([0,0,0, 0,0,0, 0,0,0])
+ console.log(hexpp(actual.buffer, {bigendian: true}))
+// console.log(hexpp(expected.buffer))
+ //t.deepEqual(expected, actual)
+ t.end()
+})
diff --git a/util.js b/util.js
new file mode 100644
index 0000000..f08af10
--- /dev/null
+++ b/util.js
@@ -0,0 +1,67 @@
+exports.write = write
+exports.reverseByteOrder = reverseByteOrder
+exports.toHex = toHex
+exports.zeroFill = zeroFill
+exports.Uint32toHex = Uint32toHex
+
+//write a string into an array, in either big or little endian mode.
+//you can create a Uint32 view on bytes with typed arrays,
+//new Uint32Array(uint8array.buffer)
+//but unfortunately, it's littleendian.
+//(as far as I can tell, am offline currently,
+// will look up docs when connected)
+
+function write (buffer, string, enc, start, from, to, LE) {
+
+ if(enc !== 'ascii')
+ throw new Error('only ascii is supported, for now')
+
+ var l = (to - from)
+
+ for( var i = 0; i < l; i++) {
+ //iterate in bigendian order.
+ var j = start + i
+ var byte = (j&0xfffffffc)|(LE ? j%4 : 3 - j%4)
+ console.log('byte', byte, string[i + from], string.charCodeAt(i + from).toString(16))
+ buffer[byte] = string.charCodeAt(i + from)
+ }
+
+}
+
+function toHex (buf, groups) {
+ buf = buf.buffer || buf
+ var s = ''
+ for(var i = 0; i < buf.byteLength; i++)
+ s += ((buf[i]>>4).toString(16)) + ((buf[i]&0xf).toString(16)) + (groups-1==i%groups ? ' ' : '')
+ return s
+}
+
+
+function reverseByteOrder(n) {
+ return (
+ ((n << 24) & 0xff000000)
+ | ((n << 8) & 0x00ff0000)
+ | ((n >> 8) & 0x0000ff00)
+ | ((n >> 24) & 0x000000ff)
+ )
+}
+
+//always fill to the end!
+function zeroFill(buf, from) {
+ for(var i = from; i < buf.byteLength; i++)
+ buf[i] = 0
+}
+
+
+function Uint32toHex (n) {
+ var s = (n & 0x0f).toString(16)
+ s = ((n >>= 4) & 0x0f).toString(16) + s
+ s = ((n >>= 4) & 0x0f).toString(16) + s
+ s = ((n >>= 4) & 0x0f).toString(16) + s
+ s = ((n >>= 4) & 0x0f).toString(16) + s
+ s = ((n >>= 4) & 0x0f).toString(16) + s
+ s = ((n >>= 4) & 0x0f).toString(16) + s
+ s = ((n >>= 4) & 0x0f).toString(16) + s
+ return s
+}
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-sha.js.git
More information about the Pkg-javascript-commits
mailing list