Skip to content

Commit

Permalink
fixes for parsing undefined length sequences with zero items
Browse files Browse the repository at this point in the history
  • Loading branch information
chafey committed Aug 4, 2015
1 parent d32d019 commit f63b408
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 54 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dicomParser",
"version": "1.1.0",
"version": "1.1.1",
"description": "Javascript parser for DICOM Part 10 data",
"main" : "dist/dicomParser.js",
"ignore": [
Expand Down
58 changes: 32 additions & 26 deletions dist/dicomParser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! dicom-parser - v1.1.0 - 2015-08-03 | (c) 2014 Chris Hafey | https://github.com/chafey/dicomParser */
/*! dicom-parser - v1.1.1 - 2015-08-03 | (c) 2014 Chris Hafey | https://github.com/chafey/dicomParser */
(function (root, factory) {

// node.js
Expand Down Expand Up @@ -1373,7 +1373,7 @@ var dicomParser = (function (dicomParser)
if(groupNumber === 0xfffe)
{
var elementNumber = byteStream.readUint16();
if(elementNumber === 0xe00d || elementNumber === 0xe0dd)
if(elementNumber === 0xe00d)
{
// NOTE: It would be better to also check for the length to be 0 as part of the check above
// but we will just log a warning for now
Expand Down Expand Up @@ -1782,6 +1782,13 @@ var dicomParser = (function (dicomParser)
var nextTag = dicomParser.readTag(byteStream);
byteStream.seek(-4);

// zero length sequence
if (element.hadUndefinedLength && nextTag === 'xfffee0dd') {
element.length = 0;
byteStream.seek(8);
return element;
}

if (nextTag === 'xfffee000') {
// parse the sequence
dicomParser.readSequenceItemsImplicit(byteStream, element);
Expand Down Expand Up @@ -1864,21 +1871,20 @@ var dicomParser = (function (dicomParser)
{
while(byteStream.position < byteStream.byteArray.length)
{
// end reading this sequence if the next tag is the sequence delimitation item
var nextTag = dicomParser.readTag(byteStream);
byteStream.seek(-4);
if (nextTag === 'xfffee0dd') {
// set the correct length
element.length = byteStream.position - element.dataOffset;
byteStream.seek(8);
return element;
}

var item = readSequenceItemExplicit(byteStream, warnings);
element.items.push(item);

// If this is the sequence delimitation item, return the offset of the next element
if(item.tag === 'xfffee0dd')
{
// sequence delimitation item, update attr data length and return
element.length = byteStream.position - element.dataOffset;
return;
}
}

// eof encountered - log a warning and set the length of the element based on the buffer size
byteStream.warnings.push('eof encountered before finding sequence delimitation item in sequence element of undefined length with tag ' + element.tag);
element.length = byteStream.byteArray.length - element.dataOffset;
element.length = byteStream.position - element.dataOffset;
}

function readSQElementKnownLengthExplicit(byteStream, element, warnings)
Expand Down Expand Up @@ -1943,6 +1949,7 @@ var dicomParser = (function (dicomParser)
// the end of this sequence item
if(element.tag === 'xfffee00d')
{
console.log('end of sequence item');
return new dicomParser.DataSet(byteStream.byteArrayParser, byteStream.byteArray, elements);
}
}
Expand Down Expand Up @@ -1973,20 +1980,19 @@ var dicomParser = (function (dicomParser)
{
while(byteStream.position < byteStream.byteArray.length)
{
var item = readSequenceItemImplicit(byteStream);
element.items.push(item);
// end reading this sequence if the next tag is the sequence delimitation item
var nextTag = dicomParser.readTag(byteStream);
byteStream.seek(-4);
if (nextTag === 'xfffee0dd') {
// set the correct length
element.length = byteStream.position - element.dataOffset;
byteStream.seek(8);
return element;
}

// If this is the sequence delimitation item, return the offset of the next element
if(item.tag === 'xfffee0dd')
{
// sequence delimitation item, update attr data length and return
element.length = byteStream.position - element.dataOffset;
return;
}
var item = readSequenceItemImplicit(byteStream);
element.items.push(item);
}

// eof encountered - log a warning and set the length of the element based on the buffer size
byteStream.warnings.push('eof encountered before finding sequence delimitation item in sequence of undefined length');
element.length = byteStream.byteArray.length - element.dataOffset;
}

Expand Down
14 changes: 13 additions & 1 deletion examples/dumpWithDataDictionary/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,17 @@ <h1>DICOM Dump with Data Dictionary</h1>
// and recursively call this function
var itemNumber = 0;
element.items.forEach(function (item) {
output.push('<li>Item #' + itemNumber++ + ' ' + item.tag + '</li>')
output.push('<li>Item #' + itemNumber++ + ' ' + item.tag);
var lengthText = " length=" + item.length;
if (item.hadUndefinedLength) {
lengthText += " <strong>(-1)</strong>";
}

if(showLength === true) {
text += lengthText + "; ";
output.push(lengthText);
}
output.push('</li>');
output.push('<ul>');
dumpDataSet(item.dataSet, output);
output.push('</ul>');
Expand Down Expand Up @@ -382,6 +392,8 @@ <h1>DICOM Dump with Data Dictionary</h1>
var elementHexStr = ("0000" + element.toString(16)).substr(-4);
text += "x" + groupHexStr + elementHexStr;
}
else if(vr === 'SQ') {
}
else {
// If it is some other length and we have no string
text += "<i>no display code for VR " + vr + " yet, sorry!</i>";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dicom-parser",
"version": "1.1.0",
"version": "1.1.1",
"description": "Javascript parser for DICOM Part 10 data",
"keywords": [
"DICOM",
Expand Down
2 changes: 1 addition & 1 deletion src/findItemDelimitationItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var dicomParser = (function (dicomParser)
if(groupNumber === 0xfffe)
{
var elementNumber = byteStream.readUint16();
if(elementNumber === 0xe00d || elementNumber === 0xe0dd)
if(elementNumber === 0xe00d)
{
// NOTE: It would be better to also check for the length to be 0 as part of the check above
// but we will just log a warning for now
Expand Down
7 changes: 7 additions & 0 deletions src/readDicomElementImplicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ var dicomParser = (function (dicomParser)
var nextTag = dicomParser.readTag(byteStream);
byteStream.seek(-4);

// zero length sequence
if (element.hadUndefinedLength && nextTag === 'xfffee0dd') {
element.length = 0;
byteStream.seek(8);
return element;
}

if (nextTag === 'xfffee000') {
// parse the sequence
dicomParser.readSequenceItemsImplicit(byteStream, element);
Expand Down
23 changes: 11 additions & 12 deletions src/readSequenceElementExplicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,20 @@ var dicomParser = (function (dicomParser)
{
while(byteStream.position < byteStream.byteArray.length)
{
// end reading this sequence if the next tag is the sequence delimitation item
var nextTag = dicomParser.readTag(byteStream);
byteStream.seek(-4);
if (nextTag === 'xfffee0dd') {
// set the correct length
element.length = byteStream.position - element.dataOffset;
byteStream.seek(8);
return element;
}

var item = readSequenceItemExplicit(byteStream, warnings);
element.items.push(item);

// If this is the sequence delimitation item, return the offset of the next element
if(item.tag === 'xfffee0dd')
{
// sequence delimitation item, update attr data length and return
element.length = byteStream.position - element.dataOffset;
return;
}
}

// eof encountered - log a warning and set the length of the element based on the buffer size
byteStream.warnings.push('eof encountered before finding sequence delimitation item in sequence element of undefined length with tag ' + element.tag);
element.length = byteStream.byteArray.length - element.dataOffset;
element.length = byteStream.position - element.dataOffset;
}

function readSQElementKnownLengthExplicit(byteStream, element, warnings)
Expand Down
24 changes: 12 additions & 12 deletions src/readSequenceElementImplicit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var dicomParser = (function (dicomParser)
// the end of this sequence item
if(element.tag === 'xfffee00d')
{
console.log('end of sequence item');

This comment has been minimized.

Copy link
@henryqdineen

henryqdineen Aug 4, 2015

Contributor

can we get rid of this console.log in a future release? thanks!

return new dicomParser.DataSet(byteStream.byteArrayParser, byteStream.byteArray, elements);
}
}
Expand Down Expand Up @@ -54,20 +55,19 @@ var dicomParser = (function (dicomParser)
{
while(byteStream.position < byteStream.byteArray.length)
{
var item = readSequenceItemImplicit(byteStream);
element.items.push(item);
// end reading this sequence if the next tag is the sequence delimitation item
var nextTag = dicomParser.readTag(byteStream);
byteStream.seek(-4);
if (nextTag === 'xfffee0dd') {
// set the correct length
element.length = byteStream.position - element.dataOffset;
byteStream.seek(8);
return element;
}

// If this is the sequence delimitation item, return the offset of the next element
if(item.tag === 'xfffee0dd')
{
// sequence delimitation item, update attr data length and return
element.length = byteStream.position - element.dataOffset;
return;
}
var item = readSequenceItemImplicit(byteStream);
element.items.push(item);
}

// eof encountered - log a warning and set the length of the element based on the buffer size
byteStream.warnings.push('eof encountered before finding sequence delimitation item in sequence of undefined length');
element.length = byteStream.byteArray.length - element.dataOffset;
}

Expand Down

1 comment on commit f63b408

@chafey
Copy link
Collaborator Author

@chafey chafey commented on f63b408 Aug 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and pushed, thanks

Please sign in to comment.