@ldapjs/asn1
Advanced tools
Comparing version 2.0.0-rc.1 to 2.0.0-rc.2
@@ -155,3 +155,3 @@ 'use strict' | ||
* in the buffer from the current offset. The current offset must be at a | ||
* byte who's value is equal to the ASN.1 enumeration tag. | ||
* byte whose value is equal to the ASN.1 enumeration tag. | ||
* | ||
@@ -174,3 +174,3 @@ * @throws When there is an error reading the tag. | ||
* in the buffer from the current offset. The current offset must be at a | ||
* byte who's value is equal to the ASN.1 integer tag. | ||
* byte whose value is equal to the ASN.1 integer tag. | ||
* | ||
@@ -283,2 +283,49 @@ * @throws When there is an error reading the tag. | ||
/** | ||
* Get a new {@link Buffer} instance that represents the full set of bytes | ||
* for a BER representation of a specified tag. For example, this is useful | ||
* when construction objects from an incoming LDAP message and the object | ||
* constructor can read a BER representation of itself to create a new | ||
* instance, e.g. when reading the filter section of a "search request" | ||
* message. | ||
* | ||
* @param {number} tag The expected tag that starts the TLV series of bytes. | ||
* @param {boolean} [advanceOffset=true] Indicates if the instance's internal | ||
* offset should be advanced or not after reading the buffer. | ||
* | ||
* @returns {Buffer|null} If there is a problem reading the buffer, e.g. | ||
* the number of bytes indicated by the length do not exist in the value, then | ||
* `null` will be returned. Otherwise, a new {@link Buffer} of bytes that | ||
* represents a full TLV. | ||
*/ | ||
readRawBuffer (tag, advanceOffset = true) { | ||
if (Number.isInteger(tag) === false) { | ||
throw Error('must specify an integer tag') | ||
} | ||
const foundTag = this.peek() | ||
if (foundTag !== tag) { | ||
const expected = tag.toString(16).padStart(2, '0') | ||
const found = foundTag.toString(16).padStart(2, '0') | ||
throw Error(`Expected 0x${expected}: got 0x${found}`) | ||
} | ||
const currentOffset = this.#offset | ||
const valueOffset = this.readLength(currentOffset + 1) | ||
if (valueOffset === null) { return null } | ||
const bufferLength = this.length | ||
// Buffer.subarray is not inclusive, so we need to add 2. | ||
const endPos = currentOffset + bufferLength + 2 | ||
if (endPos > this.buffer.byteLength) { | ||
return null | ||
} | ||
const buffer = this.buffer.subarray(currentOffset, endPos) | ||
if (advanceOffset === true) { | ||
this.setOffset(currentOffset + (bufferLength + 2)) | ||
} | ||
return buffer | ||
} | ||
/** | ||
* At the current buffer offset, read the next tag as a sequence tag, and | ||
@@ -290,4 +337,4 @@ * advance the offset to the position of the tag of the first item in the | ||
* | ||
* @returns {number} The read sequence tag value. Should match the function | ||
* input parameter value. | ||
* @returns {number|null} The read sequence tag value. Should match the | ||
* function input parameter value. | ||
* | ||
@@ -320,3 +367,3 @@ * @throws If the `tag` does not match or if there is an error reading | ||
* @param {boolean} [asBuffer=false] When true, the raw buffer will be | ||
* returned. Otherwise a native string. | ||
* returned. Otherwise, a native string. | ||
* | ||
@@ -424,6 +471,6 @@ * @returns {string | Buffer | null} Will return `null` if the buffer is | ||
/** | ||
* Given a buffer that represents an ingeter TLV, parse it and return it | ||
* Given a buffer that represents an integer TLV, parse it and return it | ||
* as a decimal value. This accounts for signedness. | ||
* | ||
* @param {Buffer} | ||
* @param {Buffer} integerBuffer | ||
* | ||
@@ -430,0 +477,0 @@ * @returns {number} |
@@ -274,2 +274,72 @@ 'use strict' | ||
tap.test('readRawBuffer', t => { | ||
t.test('requires number tag', async t => { | ||
const reader = new BerReader(Buffer.from([])) | ||
t.throws( | ||
() => reader.readRawBuffer(), | ||
Error('must specify an integer tag') | ||
) | ||
}) | ||
t.test('throws if tag does not match', async t => { | ||
const reader = new BerReader(Buffer.from([0x04, 0x00])) | ||
t.throws( | ||
() => reader.readRawBuffer(0x05), | ||
Error('Expected 0x05: got 0x04') | ||
) | ||
}) | ||
t.test('reads empty string buffer', async t => { | ||
const buffer = Buffer.from([0x04, 0x00]) | ||
const reader = new BerReader(buffer) | ||
const readBuffer = reader.readRawBuffer(0x04) | ||
t.equal(buffer.compare(readBuffer), 0) | ||
t.equal(reader.offset, 2) | ||
}) | ||
t.test('returns null for no value byte', async t => { | ||
const reader = new BerReader(Buffer.from([0x04])) | ||
const buffer = reader.readRawBuffer(0x04) | ||
t.equal(buffer, null) | ||
t.equal(reader.offset, 0) | ||
}) | ||
t.test('returns null if value length exceeds buffer length', async t => { | ||
const reader = new BerReader(Buffer.from([0x04, 0x01])) | ||
const buffer = reader.readRawBuffer(0x04) | ||
t.equal(buffer, null) | ||
t.equal(reader.offset, 0) | ||
}) | ||
t.test('return only next buffer', async t => { | ||
const buffer = Buffer.from([ | ||
0x04, 0x03, 0x66, 0x6f, 0x6f, | ||
0x04, 0x03, 0x62, 0x61, 0x72, | ||
0x04, 0x03, 0x62, 0x61, 0x7a | ||
]) | ||
const reader = new BerReader(buffer) | ||
reader.readString() | ||
const readBuffer = reader.readRawBuffer(0x04) | ||
t.equal(reader.offset, 10) | ||
t.equal(readBuffer.compare(buffer.subarray(5, 10)), 0) | ||
}) | ||
t.test('does not advance offset', async t => { | ||
const buffer = Buffer.from([ | ||
0x04, 0x03, 0x66, 0x6f, 0x6f, | ||
0x04, 0x03, 0x62, 0x61, 0x72, | ||
0x04, 0x03, 0x62, 0x61, 0x7a | ||
]) | ||
const reader = new BerReader(buffer) | ||
reader.readString() | ||
const readBuffer = reader.readRawBuffer(0x04, false) | ||
t.equal(reader.offset, 5) | ||
t.equal(readBuffer.compare(buffer.subarray(5, 10)), 0) | ||
}) | ||
t.end() | ||
}) | ||
tap.test('readSequence', t => { | ||
@@ -276,0 +346,0 @@ t.test('throws for tag mismatch', async t => { |
@@ -11,3 +11,3 @@ { | ||
"description": "Contains parsers and serializers for ASN.1 (currently BER only)", | ||
"version": "2.0.0-rc.1", | ||
"version": "2.0.0-rc.2", | ||
"repository": { | ||
@@ -14,0 +14,0 @@ "type": "git", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
73209
2033
0