token-types
Advanced tools
Comparing version 0.9.4 to 0.10.0
@@ -82,2 +82,18 @@ /// <reference types="node" /> | ||
/** | ||
* 64-bit unsigned integer, Little Endian byte order | ||
*/ | ||
export declare const UINT64_LE: IToken<number>; | ||
/** | ||
* 64-bit signed integer, Little Endian byte order | ||
*/ | ||
export declare const INT64_LE: IToken<number>; | ||
/** | ||
* 64-bit unsigned integer, Big Endian byte order | ||
*/ | ||
export declare const UINT64_BE: IToken<number>; | ||
/** | ||
* 64-bit signed integer, Big Endian byte order | ||
*/ | ||
export declare const INT64_BE: IToken<number>; | ||
/** | ||
* Ignore a given number of bytes | ||
@@ -114,8 +130,33 @@ */ | ||
private static windows1252; | ||
private static decode(buffer, off, until); | ||
private static inRange(a, min, max); | ||
private static codePointToString(cp); | ||
private static singleByteDecoder(bite); | ||
private static decode; | ||
private static inRange; | ||
private static codePointToString; | ||
private static singleByteDecoder; | ||
constructor(len: number); | ||
get(buf: Buffer, off?: number): string; | ||
} | ||
/** | ||
* Best effort approach to write 64 but signed integer, little endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
export declare function writeIntLE(buf: Buffer, value: number, offset: number, byteLength: number): number; | ||
/** | ||
* Best effort approach to read up to 64 bit unsigned integer, big endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
export declare function readUIntBE(buf: Buffer, offset: number, byteLength: number): number; | ||
/** | ||
* Best effort approach to write up to 64 bit unsigned integer, big endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
export declare function writeUIntBE(buf: Buffer, value: number, offset: number, byteLength: number): number; | ||
/** | ||
* Best effort approach to read 64 but signed integer, big endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
export declare function readIntBE(buf: Buffer, offset: number, byteLength: number): number; | ||
/** | ||
* Best effort approach to write 64 but signed integer, big endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
export declare function writeIntBE(buf: Buffer, value: number, offset: number, byteLength: number): number; |
188
lib/index.js
@@ -284,2 +284,50 @@ "use strict"; | ||
/** | ||
* 64-bit unsigned integer, Little Endian byte order | ||
*/ | ||
exports.UINT64_LE = { | ||
len: 8, | ||
get: function (buf, off) { | ||
return readUIntLE(buf, off, this.len); | ||
}, | ||
put: function (b, o, v) { | ||
return writeUIntLE(b, v, o, this.len); | ||
} | ||
}; | ||
/** | ||
* 64-bit signed integer, Little Endian byte order | ||
*/ | ||
exports.INT64_LE = { | ||
len: 8, | ||
get: function (buf, off) { | ||
return readIntLE(buf, off, this.len); | ||
}, | ||
put: function (b, off, v) { | ||
return writeIntLE(b, v, off, this.len); | ||
} | ||
}; | ||
/** | ||
* 64-bit unsigned integer, Big Endian byte order | ||
*/ | ||
exports.UINT64_BE = { | ||
len: 8, | ||
get: function (buf, off) { | ||
return readUIntBE(buf, off, this.len); | ||
}, | ||
put: function (b, o, v) { | ||
return writeUIntBE(b, v, o, this.len); | ||
} | ||
}; | ||
/** | ||
* 64-bit signed integer, Big Endian byte order | ||
*/ | ||
exports.INT64_BE = { | ||
len: 8, | ||
get: function (buf, off) { | ||
return readIntBE(buf, off, this.len); | ||
}, | ||
put: function (b, off, v) { | ||
return writeIntBE(b, v, off, this.len); | ||
} | ||
}; | ||
/** | ||
* Ignore a given number of bytes | ||
@@ -378,1 +426,141 @@ */ | ||
exports.AnsiStringType = AnsiStringType; | ||
/** | ||
* Best effort approach to read up to 64 bit unsigned integer, little endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
function readUIntLE(buf, offset, byteLength) { | ||
offset = offset >>> 0; | ||
byteLength = byteLength >>> 0; | ||
var val = buf[offset]; | ||
var mul = 1; | ||
var i = 0; | ||
while (++i < byteLength && (mul *= 0x100)) { | ||
val += buf[offset + i] * mul; | ||
} | ||
return val; | ||
} | ||
/** | ||
* Best effort approach to write up to 64 bit unsigned integer, little endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
function writeUIntLE(buf, value, offset, byteLength) { | ||
value = +value; | ||
offset = offset >>> 0; | ||
byteLength = byteLength >>> 0; | ||
var mul = 1; | ||
var i = 0; | ||
buf[offset] = value & 0xFF; | ||
while (++i < byteLength && (mul *= 0x100)) { | ||
buf[offset + i] = (value / mul) & 0xFF; | ||
} | ||
return offset + byteLength; | ||
} | ||
/** | ||
* Best effort approach to read 64 but signed integer, little endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
function readIntLE(buf, offset, byteLength) { | ||
offset = offset >>> 0; | ||
byteLength = byteLength >>> 0; | ||
var val = buf[offset]; | ||
var mul = 1; | ||
var i = 0; | ||
while (++i < byteLength && (mul *= 0x100)) { | ||
val += buf[offset + i] * mul; | ||
} | ||
mul *= 0x80; | ||
if (val >= mul) | ||
val -= Math.pow(2, 8 * byteLength); | ||
return val; | ||
} | ||
/** | ||
* Best effort approach to write 64 but signed integer, little endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
function writeIntLE(buf, value, offset, byteLength) { | ||
value = +value; | ||
offset = offset >>> 0; | ||
var i = 0; | ||
var mul = 1; | ||
var sub = 0; | ||
buf[offset] = value & 0xFF; | ||
while (++i < byteLength && (mul *= 0x100)) { | ||
if (value < 0 && sub === 0 && buf[offset + i - 1] !== 0) { | ||
sub = 1; | ||
} | ||
buf[offset + i] = ((value / mul) >> 0) - sub & 0xFF; | ||
} | ||
return offset + byteLength; | ||
} | ||
exports.writeIntLE = writeIntLE; | ||
/** | ||
* Best effort approach to read up to 64 bit unsigned integer, big endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
function readUIntBE(buf, offset, byteLength) { | ||
offset = offset >>> 0; | ||
byteLength = byteLength >>> 0; | ||
var val = buf[offset + --byteLength]; | ||
var mul = 1; | ||
while (byteLength > 0 && (mul *= 0x100)) { | ||
val += buf[offset + --byteLength] * mul; | ||
} | ||
return val; | ||
} | ||
exports.readUIntBE = readUIntBE; | ||
/** | ||
* Best effort approach to write up to 64 bit unsigned integer, big endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
function writeUIntBE(buf, value, offset, byteLength) { | ||
value = +value; | ||
offset = offset >>> 0; | ||
byteLength = byteLength >>> 0; | ||
var i = byteLength - 1; | ||
var mul = 1; | ||
buf[offset + i] = value & 0xFF; | ||
while (--i >= 0 && (mul *= 0x100)) { | ||
buf[offset + i] = (value / mul) & 0xFF; | ||
} | ||
return offset + byteLength; | ||
} | ||
exports.writeUIntBE = writeUIntBE; | ||
/** | ||
* Best effort approach to read 64 but signed integer, big endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
function readIntBE(buf, offset, byteLength) { | ||
offset = offset >>> 0; | ||
byteLength = byteLength >>> 0; | ||
var i = byteLength; | ||
var mul = 1; | ||
var val = buf[offset + --i]; | ||
while (i > 0 && (mul *= 0x100)) { | ||
val += buf[offset + --i] * mul; | ||
} | ||
mul *= 0x80; | ||
if (val >= mul) | ||
val -= Math.pow(2, 8 * byteLength); | ||
return val; | ||
} | ||
exports.readIntBE = readIntBE; | ||
/** | ||
* Best effort approach to write 64 but signed integer, big endian. | ||
* Note that JavasScript is limited to 2^53 - 1 bit. | ||
*/ | ||
function writeIntBE(buf, value, offset, byteLength) { | ||
value = +value; | ||
offset = offset >>> 0; | ||
var i = byteLength - 1; | ||
var mul = 1; | ||
var sub = 0; | ||
buf[offset + i] = value & 0xFF; | ||
while (--i >= 0 && (mul *= 0x100)) { | ||
if (value < 0 && sub === 0 && buf[offset + i + 1] !== 0) { | ||
sub = 1; | ||
} | ||
buf[offset + i] = ((value / mul) >> 0) - sub & 0xFF; | ||
} | ||
return offset + byteLength; | ||
} | ||
exports.writeIntBE = writeIntBE; |
{ | ||
"name": "token-types", | ||
"version": "0.9.4", | ||
"version": "0.10.0", | ||
"description": "Common token types for decoding and encoding binairy values", | ||
@@ -33,15 +33,15 @@ "author": { | ||
"devDependencies": { | ||
"@types/chai": "^4.1.3", | ||
"@types/mocha": "^5.2.0", | ||
"@types/node": "^9.6.13", | ||
"@types/chai": "^4.1.4", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.7.1", | ||
"chai": "^4.1.2", | ||
"copyfiles": "^2.0.0", | ||
"coveralls": "^3.0.1", | ||
"mocha": "^5.1.1", | ||
"nyc": "^11.7.1", | ||
"ts-node": "^6.0.3", | ||
"coveralls": "^3.0.2", | ||
"mocha": "^5.2.0", | ||
"nyc": "^12.0.2", | ||
"ts-node": "^7.0.1", | ||
"tslint": "^5.10.0", | ||
"typescript": "^2.8.3" | ||
"typescript": "^3.0.1" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -18,2 +18,3 @@ [![Build Status](https://travis-ci.org/Borewit/token-types.svg?branch=master)](https://travis-ci.org/Borewit/token-types) | ||
* `UINT32_BE`, `UINT32_LE` | ||
* `UINT64_BE`, `UINT64_LE` * | ||
* `INT8` | ||
@@ -23,2 +24,3 @@ * `INT16_BE`, `INT16_LE` | ||
* `INT32_BE`, `INT32_LE` | ||
* `INT64_BE`, `UINT64_LE` * | ||
@@ -29,6 +31,3 @@ String types: | ||
One might notice that there is no support for 64-bit tokens, since JavaScript | ||
seems to limit value size to less than 2^64. Rather than wrapping up an | ||
additional math library to handle this, I wanted to stick with JavaScript | ||
primitives. Maybe this will change later if this becomes important. | ||
*) The 64-bit tokens are best effort based, since JavaScript limit value size to less than 2^64. | ||
@@ -35,0 +34,0 @@ [npm-url]: https://npmjs.org/package/token-types |
@@ -18,3 +18,4 @@ { | ||
"forin": false, | ||
"no-implicit-dependencies": [true, "dev"] | ||
"no-implicit-dependencies": [true, "dev"], | ||
"no-conditional-assignment": false | ||
}, | ||
@@ -21,0 +22,0 @@ "jsRules": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100023
8
749
39