diff --git a/EvilDICOM/EvilDICOM/Core/IO/Reading/SequenceItemReader.cs b/EvilDICOM/EvilDICOM/Core/IO/Reading/SequenceItemReader.cs index 075850a8..200dfb24 100644 --- a/EvilDICOM/EvilDICOM/Core/IO/Reading/SequenceItemReader.cs +++ b/EvilDICOM/EvilDICOM/Core/IO/Reading/SequenceItemReader.cs @@ -52,13 +52,13 @@ public static void SkipItemLittleEndian(DICOMBinaryReader dr, TransferSyntax syn else { if (syntax == TransferSyntax.EXPLICIT_VR_LITTLE_ENDIAN) - while (!IsEndOfSequenceItemLittleEndian(dr)) + while (!IsEndOfSequenceItemLittleEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; DICOMElementReader.SkipElementExplicitLittleEndian(dr); } else - while (!IsEndOfSequenceItemLittleEndian(dr)) + while (!IsEndOfSequenceItemLittleEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; DICOMElementReader.SkipElementImplicitLittleEndian(dr); @@ -72,7 +72,7 @@ public static void SkipItemBigEndian(DICOMBinaryReader dr) if (length != -1) dr.Skip(length); else - while (!IsEndOfSequenceItemBigEndian(dr)) + while (!IsEndOfSequenceItemBigEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; DICOMElementReader.SkipElementExplicitBigEndian(dr); @@ -94,7 +94,7 @@ private static bool IsEndOfSequenceItemBigEndian(DICOMBinaryReader dr) private static DICOMObject ReadIndefiniteBigEndian(DICOMBinaryReader dr, TransferSyntax syntax, StringEncoding enc) { var elements = new List(); - while (!IsEndOfSequenceItemLittleEndian(dr)) + while (!IsEndOfSequenceItemLittleEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; elements.Add(DICOMElementReader.ReadElementExplicitBigEndian(dr, enc)); @@ -105,7 +105,7 @@ private static DICOMObject ReadIndefiniteBigEndian(DICOMBinaryReader dr, Transfe private static DICOMObject ReadIndefiniteLittleEndian(DICOMBinaryReader dr, TransferSyntax syntax, StringEncoding enc) { var elements = new List(); - while (!IsEndOfSequenceItemLittleEndian(dr)) + while (!IsEndOfSequenceItemLittleEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; if (syntax == TransferSyntax.EXPLICIT_VR_LITTLE_ENDIAN) diff --git a/EvilDICOM/EvilDICOM/Core/IO/Reading/SequenceReader.cs b/EvilDICOM/EvilDICOM/Core/IO/Reading/SequenceReader.cs index 10a388db..bf109d63 100644 --- a/EvilDICOM/EvilDICOM/Core/IO/Reading/SequenceReader.cs +++ b/EvilDICOM/EvilDICOM/Core/IO/Reading/SequenceReader.cs @@ -16,7 +16,7 @@ public class SequenceReader public static int ReadIndefiniteLengthLittleEndian(DICOMBinaryReader dr, TransferSyntax syntax) { var startingPos = dr.StreamPosition; - while (!IsEndOfSequenceLittleEndian(dr)) + while (!IsEndOfSequenceLittleEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; SequenceItemReader.SkipItemLittleEndian(dr, syntax); @@ -27,7 +27,7 @@ public static int ReadIndefiniteLengthLittleEndian(DICOMBinaryReader dr, Transfe public static int ReadIndefiniteLengthBigEndian(DICOMBinaryReader dr) { var startingPos = dr.StreamPosition; - while (!IsEndOfSequenceBigEndian(dr)) + while (!IsEndOfSequenceBigEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; SequenceItemReader.SkipItemBigEndian(dr);