Skip to content

Commit

Permalink
feat(tlv): handle zero-length TLVs
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Dec 17, 2023
1 parent e6bcfbc commit 26527ca
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/lib/type/TlvIo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ class TlvIo implements IoType {
const tagValue = this.#tagType.read(stream, context);
const lengthValue = this.#lengthType.read(stream, context);

// Skip reading values for zero-length TLVs
if (lengthValue === 0) {
return {
tag: tagValue,
length: lengthValue,
value: null,
};
}

const valueType = this.#valueCallback(tagValue, lengthValue);
const valueBytes = stream.readBytes(lengthValue);

Expand Down Expand Up @@ -97,11 +106,14 @@ class TlvIo implements IoType {
this.#lengthType.write(stream, value.length, context);
const expectedEndOffset = stream.offset + value.length;

const valueType = this.#valueCallback(value.tag, value.length);
if (valueType && valueType.write) {
valueType.write(stream, value.value, context);
} else {
stream.writeBytes(value.value);
// Skip writing values for zero-length TLVs
if (value.length !== 0) {
const valueType = this.#valueCallback(value.tag, value.length);
if (valueType && valueType.write) {
valueType.write(stream, value.value, context);
} else {
stream.writeBytes(value.value);
}
}

// Account for underruns and overruns
Expand Down

0 comments on commit 26527ca

Please sign in to comment.