Skip to content

Commit

Permalink
fix: Parse private sequences if they have an undefined length.
Browse files Browse the repository at this point in the history
Fixes a bug where private sequences would attempt to be parsed as items.
Also, add a test to verify that private sequences with explicit lengths are skipped.
  • Loading branch information
yagni committed Sep 26, 2022
1 parent eb51626 commit 24cc195
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/readDicomElementImplicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ export default function readDicomElementImplicit (byteStream, untilTag, vrCallba
return element;
}

if (isSequence(element, byteStream, vrCallback) && !isPrivateTag(element.tag)) {
// always parse sequences with undefined lengths, since there's no other way to know how long they are.
if (isSequence(element, byteStream, vrCallback) && (!isPrivateTag(element.tag) || element.hadUndefinedLength)) {
// parse the sequence
readSequenceItemsImplicit(byteStream, element);

if (isPrivateTag(element.tag)) {
element.items = undefined;
}

return element;
}

Expand Down
63 changes: 63 additions & 0 deletions test/readDicomElementImplicit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,67 @@ describe('readDicomElementImplicit', () => {
expect(element.length).to.equal(11);
});

it('private sequence with explicit length is skipped', () => {
// Arrange
// (0009,0006) length: 26
const bytes = [0x09, 0x00, 0x06, 0x00, 0x1a, 0x00, 0x00, 0x00,
// (fffe,e000) length: undefined
0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff,
// (0008,0018) length: 2 'B'
0x08, 0x00, 0x18, 0x00, 0x02, 0x00, 0x00, 0x00, 0x42, 0x20,
// (fffe,e00d) length: 0
0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00,
// (0008,0100) length: 2 'A'
0x08, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x41, 0x20,
];

const byteStream = new ByteStream(littleEndianByteArrayParser, convertToByteArray(bytes));

// Act
const element = readDicomElementImplicit(byteStream);

// Assert
expect(element.tag).to.equal('x00090006');
expect(element.items).to.equal(undefined);
expect(element.length).to.equal(26);

// Read the next element
const nextElement = readDicomElementImplicit(byteStream);
expect(nextElement.tag).to.equal('x00080100');
expect(nextElement.length).to.equal(2);
});

it('private sequence with implicit length is skipped', () => {
// Arrange
// (0009,0006) length: undefined
const bytes = [0x09, 0x00, 0x06, 0x00, 0xff, 0xff, 0xff, 0xff,
// (fffe,e000) length: undefined
0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff,
// (0008,0018) length: 4 'ABC '
0x08, 0x00, 0x18, 0x00, 0x04, 0x00, 0x00, 0x00, 0x41, 0x42, 0x43, 0x20,
// (fffe,e00d) length: 0
0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00,
// (fffe,e0dd) length: 0
0xfe, 0xff, 0xdd, 0xe0, 0x00, 0x00, 0x00, 0x00,
// (0008,0100) length: 2 'A'
0x08, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x41, 0x20,
];

const byteStream = new ByteStream(littleEndianByteArrayParser, convertToByteArray(bytes));

// Act
const element = readDicomElementImplicit(byteStream);

// Assert
expect(element.tag).to.equal('x00090006');
expect(element.items).to.equal(undefined);
expect(element.length).to.equal(28);

// Read the next element
const nextElement = readDicomElementImplicit(byteStream);
expect(nextElement.tag).to.equal('x00080100');
expect(nextElement.length).to.equal(2);
});


});

0 comments on commit 24cc195

Please sign in to comment.