Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 77ee634
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Mathias Buus | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# uint64be | ||
|
||
Encode / decode big endian unsigned 64 bit integers | ||
|
||
``` | ||
npm install uint64be | ||
``` | ||
|
||
## Usage | ||
|
||
``` js | ||
var uint64be = require('uint64be') | ||
|
||
var buf = uint64be.encode(42) // returns a 8 byte buffer with 42 encoded | ||
console.log(uint64be.decode(buf)) // returns 42 | ||
``` | ||
|
||
## API | ||
|
||
#### `buffer = uint64be.encode(num, [buffer], [offset])` | ||
|
||
Encode a number as a big endian 64 bit unsigned integer. | ||
Optionally you can pass a buffer + offset as the 2nd and 3rd argument | ||
and the number will be encoded into that buffer at the given offset. | ||
|
||
#### `num = uint64be.decode(buffer, [offset])` | ||
|
||
Decode a number from a buffer. | ||
|
||
#### `length = uint64be.encodingLength(num)` | ||
|
||
Always returns `8`. Added to comply with the standard encoding interface in node. | ||
Similarly `uint64be.encode.bytes` and `uint64be.decode.bytes` is also set to `8`. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var UINT_32_MAX = 0xffffffff | ||
|
||
exports.encodingLength = function () { | ||
return 8 | ||
} | ||
|
||
exports.encode = function (num, buf, offset) { | ||
if (!buf) buf = new Buffer(8) | ||
if (!offset) offset = 0 | ||
|
||
var top = Math.floor(num / UINT_32_MAX) | ||
var rem = num - top * UINT_32_MAX | ||
|
||
buf.writeUInt32BE(top, offset) | ||
buf.writeUInt32BE(rem, offset + 4) | ||
return buf | ||
} | ||
|
||
exports.decode = function (buf, offset) { | ||
if (!offset) offset = 0 | ||
|
||
if (!buf) buf = new Buffer(4) | ||
if (!offset) offset = 0 | ||
|
||
var top = buf.readUInt32BE(offset) | ||
var rem = buf.readUInt32BE(offset + 4) | ||
|
||
return top * UINT_32_MAX + rem | ||
} | ||
|
||
exports.encode.bytes = 8 | ||
exports.decode.bytes = 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "uint64be", | ||
"version": "0.0.0", | ||
"description": "Encode / decode big endian unsigned 64 bit integers", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"standard": "^5.0.0", | ||
"tape": "^4.0.2" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mafintosh/uint64be.git" | ||
}, | ||
"scripts": { | ||
"test": "standard && tape test.js" | ||
}, | ||
"author": "Mathias Buus (@mafintosh)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mafintosh/uint64be/issues" | ||
}, | ||
"homepage": "https://github.com/mafintosh/uint64be" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var uint64be = require('./') | ||
var tape = require('tape') | ||
|
||
tape('encode', function (t) { | ||
t.same(uint64be.encodingLength(42), 8) | ||
t.same(uint64be.encode(42), new Buffer([0, 0, 0, 0, 0, 0, 0, 42])) | ||
t.same(uint64be.encode(42424242424242), new Buffer([0x0, 0x0, 0x26, 0x95, 0xa9, 0xe6, 0x70, 0x47])) | ||
t.same(uint64be.encode(Number.MAX_SAFE_INTEGER), new Buffer([0x0, 0x20, 0x0, 0x0, 0x0, 0x1f, 0xff, 0xff])) | ||
t.same(uint64be.encode.bytes, 8) | ||
t.end() | ||
}) | ||
|
||
tape('decode', function (t) { | ||
t.same(uint64be.decode(new Buffer([0, 0, 0, 0, 0, 0, 0, 42])), 42) | ||
t.same(uint64be.decode(new Buffer([0x0, 0x0, 0x26, 0x95, 0xa9, 0xe6, 0x70, 0x47])), 42424242424242) | ||
t.same(uint64be.decode(new Buffer([0x0, 0x20, 0x0, 0x0, 0x0, 0x1f, 0xff, 0xff])), Number.MAX_SAFE_INTEGER) | ||
t.same(uint64be.decode.bytes, 8) | ||
t.end() | ||
}) |