private static bool IsPrivateSequenceBad(IByteSource source, bool isExplicitVR) { source.Mark(); try { source.GetUInt16(); // group source.GetUInt16(); // element source.GetUInt32(); // length source.GetUInt16(); // group source.GetUInt16(); // element var bytes = source.GetBytes(2); var vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); DicomVR dummy; if (DicomVR.TryParse(vr, out dummy)) { return(!isExplicitVR); } // unable to parse VR if (isExplicitVR) { return(true); } } finally { source.Rewind(); } return(false); }
private bool ParseItemSequenceTag(IByteSource source) { if (this.parseStage == ParseStage.Tag) { source.Mark(); if (!source.Require(8)) { this.result = DicomReaderResult.Suspended; return(false); } var group = source.GetUInt16(); var element = source.GetUInt16(); this._tag = new DicomTag(@group, element); if (this._tag != DicomTag.Item && this._tag != DicomTag.SequenceDelimitationItem) { // assume invalid sequence source.Rewind(); if (!this._implicit) { source.PopMilestone(); } this.observer.OnEndSequence(); if (this.badPrivateSequence) { this.isExplicitVR = !this.isExplicitVR; this.badPrivateSequence = false; } return(false); } this.length = source.GetUInt32(); if (this._tag == DicomTag.SequenceDelimitationItem) { // #64, in case explicit length has been specified despite occurrence of Sequence Delimitation Item if (source.HasReachedMilestone() && source.MilestonesCount > this.sequenceDepth) { this.ResetState(); return(true); } // end of sequence this.observer.OnEndSequence(); if (this.badPrivateSequence) { this.isExplicitVR = !this.isExplicitVR; this.badPrivateSequence = false; } this.ResetState(); return(false); } this.parseStage = ParseStage.Value; } return(true); }
private bool IsPrivateSequenceBad(IByteSource source) { source.Mark(); try { var group = source.GetUInt16(); var element = source.GetUInt16(); var tag = new DicomTag(group, element); var length = source.GetUInt32(); group = source.GetUInt16(); element = source.GetUInt16(); tag = new DicomTag(group, element); byte[] bytes = source.GetBytes(2); string vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); DicomVR dummy; if (DicomVR.TryParse(vr, out dummy)) { return(!_explicit); } // unable to parse VR if (_explicit) { return(true); } } finally { source.Rewind(); } return(false); }
private static bool IsPrivateSequenceBad(IByteSource source, bool isExplicitVR) { source.Mark(); try { // Skip "item" tags; continue skipping until length is non-zero (#223) while (new DicomTag(source.GetUInt16(), source.GetUInt16()) == DicomTag.Item // group, element & source.GetUInt32() == 0) // length (using & instead of && enforces RHS to be evaluated regardless of LHS) { } source.GetUInt16(); // group source.GetUInt16(); // element var bytes = source.GetBytes(2); var vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); DicomVR dummy; if (DicomVR.TryParse(vr, out dummy)) { return(!isExplicitVR); } // unable to parse VR if (isExplicitVR) { return(true); } } finally { source.Rewind(); } return(false); }
private static bool IsPrivateSequenceBad(IByteSource source, uint count, bool isExplicitVR) { source.Mark(); try { // Skip "item" tags; continue skipping until length is non-zero (#223) // Using & instead of && enforces RHS to be evaluated regardless of LHS uint length; while (source.GetUInt16() == DicomTag.Item.Group & source.GetUInt16() == DicomTag.Item.Element & (length = source.GetUInt32()) < uint.MaxValue) // Dummy condition to ensure that length is included in parsing { // Length non-zero, end skipping (#223) if (length > 0) { break; } // Handle scenario where last tag is private sequence with empty items (#487) count -= 8; if (count <= 0) { return(false); } } source.GetUInt16(); // group source.GetUInt16(); // element var bytes = source.GetBytes(2); var vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); DicomVR dummy; if (DicomVR.TryParse(vr, out dummy)) { return(!isExplicitVR); } // unable to parse VR if (isExplicitVR) { return(true); } } finally { source.Rewind(); } return(false); }
private bool ParseFragmentSequenceTag(IByteSource source) { if (_parseStage == ParseStage.Tag) { source.Mark(); if (!source.Require(8)) { _result = DicomReaderResult.Suspended; return(false); } var group = source.GetUInt16(); var element = source.GetUInt16(); var tag = new DicomTag(@group, element); if (tag != DicomTag.Item && tag != DicomTag.SequenceDelimitationItem) { throw new DicomReaderException($"Unexpected tag in DICOM fragment sequence: {tag}"); } _length = source.GetUInt32(); if (tag == DicomTag.SequenceDelimitationItem) { // end of fragment _observer.OnEndFragmentSequence(); _fragmentItem = 0; ResetState(); return(false); } _fragmentItem++; _parseStage = ParseStage.Value; } return(true); }
private void ParseFragmentSequence(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(8, ParseFragmentSequence, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); DicomTag tag = new DicomTag(group, element); if (tag != DicomTag.Item && tag != DicomTag.SequenceDelimitationItem) { throw new DicomReaderException("Unexpected tag in DICOM fragment sequence: {0}", tag); } _length = source.GetUInt32(); if (tag == DicomTag.SequenceDelimitationItem) { // end of fragment _observer.OnEndFragmentSequence(); _fragmentItem = 0; ResetState(); ParseDataset(source, PopState()); return; } _fragmentItem++; _state = ParseState.Value; } if (_state == ParseState.Value) { if (!source.Require(_length, ParseFragmentSequence, state)) { _result = DicomReaderResult.Suspended; return; } IByteBuffer buffer = source.GetBuffer(_length); if (_fragmentItem == 1) { buffer = EndianByteBuffer.Create(buffer, source.Endian, 4); } else { buffer = EndianByteBuffer.Create(buffer, source.Endian, _vr.UnitSize); } _observer.OnFragmentSequenceItem(source, buffer); _state = ParseState.Tag; } } } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
private void ParseDataset(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF && !source.HasReachedMilestone() && _result == DicomReaderResult.Processing) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); DicomPrivateCreator creator = null; if (group.IsOdd() && element > 0x00ff) { string pvt = null; uint card = (uint)(group << 16) + (uint)(element >> 8); if (_private.TryGetValue(card, out pvt)) { creator = Dictionary.GetPrivateCreator(pvt); } } _tag = new DicomTag(group, element, creator); _entry = Dictionary[_tag]; if (!_tag.IsPrivate && _entry != null && _entry.MaskTag == null) { _tag = _entry.Tag; // Use dictionary tag } if (_stop != null && _tag.CompareTo(_stop) >= 0) { _result = DicomReaderResult.Stopped; return; } _state = ParseState.VR; } while (_state == ParseState.VR) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { _vr = DicomVR.NONE; _state = ParseState.Length; break; } if (IsExplicitVR) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } byte[] bytes = source.GetBytes(2); string vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); if (!DicomVR.TryParse(vr, out _vr)) { // unable to parse VR _vr = DicomVR.UN; } } else { if (_entry != null) { if (_entry == DicomDictionary.UnknownTag) { _vr = DicomVR.UN; } else if (_entry.ValueRepresentations.Contains(DicomVR.OB) && _entry.ValueRepresentations.Contains(DicomVR.OW)) { _vr = DicomVR.OW; // ??? } else { _vr = _entry.ValueRepresentations.FirstOrDefault(); } } } if (_vr == null) { _vr = DicomVR.UN; } _state = ParseState.Length; if (_vr == DicomVR.UN) { if (_tag.Element == 0x0000) { // Group Length to UL _vr = DicomVR.UL; break; } else if (IsExplicitVR) { break; } } if (_tag.IsPrivate) { if (_tag.Element != 0x0000 && _tag.Element <= 0x00ff && _vr == DicomVR.UN) { _vr = DicomVR.LO; // force private creator to LO } } } while (_state == ParseState.Length) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); _state = ParseState.Value; break; } if (IsExplicitVR) { if (_vr.Is16bitLength) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt16(); } else { if (!source.Require(6, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } source.Skip(2); _length = source.GetUInt32(); } } else { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); // assume that undefined length in implicit dataset is SQ if (_length == UndefinedLength && _vr == DicomVR.UN) { _vr = DicomVR.SQ; } } _state = ParseState.Value; } if (_state == ParseState.Value) { // check dictionary for VR after reading length to handle 16-bit lengths // check before reading value to handle SQ elements var parsedVR = _vr; // check dictionary for VR after reading length to handle 16-bit lengths // check before reading value to handle SQ elements if (_vr == DicomVR.UN && IsExplicitVR) { var entry = Dictionary[_tag]; if (entry != null) { _vr = entry.ValueRepresentations.FirstOrDefault(); } if (_vr == null) { _vr = DicomVR.UN; } } if (_tag == DicomTag.ItemDelimitationItem) { // end of sequence item return; } while (_vr == DicomVR.SQ && _tag.IsPrivate) { if (!IsPrivateSequence(source)) { _vr = DicomVR.UN; break; } if (IsPrivateSequenceBad(source)) { _badPrivateSequence = true; _explicit = !_explicit; } break; } if (_vr == DicomVR.SQ) { // start of sequence _observer.OnBeginSequence(source, _tag, _length); _state = ParseState.Tag; if (_length != UndefinedLength) { _implicit = false; source.PushMilestone(_length); } else { _implicit = true; } PushState(state); var last = source.Position; ParseItemSequence(source, null); // Aeric Sylvan - https://github.com/rcd/fo-dicom/issues/62#issuecomment-46248073 // Fix reading of SQ with parsed VR of UN if (source.Position > last || _length == 0) { continue; } else { _state = ParseState.Value; _vr = parsedVR; } } if (_length == UndefinedLength) { _observer.OnBeginFragmentSequence(source, _tag, _vr); _state = ParseState.Tag; PushState(state); ParseFragmentSequence(source, null); continue; } if (!source.Require(_length, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } IByteBuffer buffer = source.GetBuffer(_length); if (!_vr.IsString) { buffer = EndianByteBuffer.Create(buffer, source.Endian, _vr.UnitSize); } _observer.OnElement(source, _tag, _vr, buffer); // parse private creator value and add to lookup table if (_tag.IsPrivate && _tag.Element != 0x0000 && _tag.Element <= 0x00ff) { var creator = DicomEncoding.Default.GetString(buffer.Data, 0, buffer.Data.Length).TrimEnd((char)DicomVR.LO.PaddingValue); var card = (uint)(_tag.Group << 16) + (uint)(_tag.Element); _private[card] = creator; } ResetState(); } } if (source.HasReachedMilestone()) { // end of explicit length sequence item source.PopMilestone(); return; } if (_result != DicomReaderResult.Processing) { return; } // end of processing _result = DicomReaderResult.Success; } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
private void ParseItemSequence(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF && !source.HasReachedMilestone()) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(8, ParseItemSequence, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); _tag = new DicomTag(group, element); if (_tag != DicomTag.Item && _tag != DicomTag.SequenceDelimitationItem) { // assume invalid sequence source.Rewind(); if (!_implicit) { source.PopMilestone(); } _observer.OnEndSequence(); if (_badPrivateSequence) { _explicit = !_explicit; _badPrivateSequence = false; } return; } _length = source.GetUInt32(); if (_tag == DicomTag.SequenceDelimitationItem) { // end of sequence _observer.OnEndSequence(); if (_badPrivateSequence) { _explicit = !_explicit; _badPrivateSequence = false; } ResetState(); return; } _state = ParseState.Value; } if (_state == ParseState.Value) { if (_length != UndefinedLength) { if (!source.Require(_length, ParseItemSequence, state)) { _result = DicomReaderResult.Suspended; return; } source.PushMilestone(_length); } _observer.OnBeginSequenceItem(source, _length); ResetState(); ParseDataset(source, state); ResetState(); _observer.OnEndSequenceItem(); continue; } } // end of explicit length sequence if (source.HasReachedMilestone()) { source.PopMilestone(); } _observer.OnEndSequence(); if (_badPrivateSequence) { _explicit = !_explicit; _badPrivateSequence = false; } } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
private static bool IsPrivateSequenceBad(IByteSource source, bool isExplicitVR) { source.Mark(); try { // Skip "item" tags; continue skipping until length is non-zero (#223) while (new DicomTag(source.GetUInt16(), source.GetUInt16()) == DicomTag.Item // group, element & source.GetUInt32() == 0) // length (using & instead of && enforces RHS to be evaluated regardless of LHS) { } source.GetUInt16(); // group source.GetUInt16(); // element var bytes = source.GetBytes(2); var vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); DicomVR dummy; if (DicomVR.TryParse(vr, out dummy)) return !isExplicitVR; // unable to parse VR if (isExplicitVR) return true; } finally { source.Rewind(); } return false; }
private void ParseFragmentSequence(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(8, ParseFragmentSequence, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); DicomTag tag = new DicomTag(group, element); if (tag != DicomTag.Item && tag != DicomTag.SequenceDelimitationItem) throw new DicomReaderException("Unexpected tag in DICOM fragment sequence: {0}", tag); _length = source.GetUInt32(); if (tag == DicomTag.SequenceDelimitationItem) { // end of fragment _observer.OnEndFragmentSequence(); _fragmentItem = 0; ResetState(); ParseDataset(source, PopState()); return; } _fragmentItem++; _state = ParseState.Value; } if (_state == ParseState.Value) { if (!source.Require(_length, ParseFragmentSequence, state)) { _result = DicomReaderResult.Suspended; return; } IByteBuffer buffer = source.GetBuffer(_length); if (_fragmentItem == 1) buffer = EndianByteBuffer.Create(buffer, source.Endian, 4); else buffer = EndianByteBuffer.Create(buffer, source.Endian, _vr.UnitSize); _observer.OnFragmentSequenceItem(source, buffer); _state = ParseState.Tag; } } } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
private void ParseDataset(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF && !source.HasReachedMilestone() && _result == DicomReaderResult.Processing) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); DicomPrivateCreator creator = null; if (group.IsOdd() && element > 0x00ff) { string pvt = null; uint card = (uint)(group << 16) + (uint)(element >> 8); if (_private.TryGetValue(card, out pvt)) creator = Dictionary.GetPrivateCreator(pvt); } _tag = new DicomTag(group, element, creator); if (_stop != null && _tag.CompareTo(_stop) >= 0) { _result = DicomReaderResult.Stopped; return; } _state = ParseState.VR; } while (_state == ParseState.VR) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { _vr = DicomVR.NONE; _state = ParseState.Length; break; } if (IsExplicitVR) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } byte[] bytes = source.GetBytes(2); string vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); try { _vr = DicomVR.Parse(vr); } catch { // unable to parse VR _vr = DicomVR.UN; } } else { DicomDictionaryEntry entry = Dictionary[_tag]; if (entry != null) { if (entry == DicomDictionary.UnknownTag) _vr = DicomVR.UN; else if (entry.ValueRepresentations.Contains(DicomVR.OB) && entry.ValueRepresentations.Contains(DicomVR.OW)) _vr = DicomVR.OW; // ??? else _vr = entry.ValueRepresentations.FirstOrDefault(); } } if (_vr == null) _vr = DicomVR.UN; _state = ParseState.Length; if (_vr == DicomVR.UN) { if (_tag.Element == 0x0000) { // Group Length to UL _vr = DicomVR.UL; break; } else if (IsExplicitVR) { break; } } if (_tag.IsPrivate) { if (_tag.Element != 0x0000 && _tag.Element <= 0x00ff && _vr == DicomVR.UN) _vr = DicomVR.LO; // force private creator to LO } } while (_state == ParseState.Length) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); _state = ParseState.Value; break; } if (IsExplicitVR) { if (_vr.Is16bitLength) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt16(); } else { if (!source.Require(6, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } source.Skip(2); _length = source.GetUInt32(); } } else { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); // assume that undefined length in implicit dataset is SQ if (_length == UndefinedLength && _vr == DicomVR.UN) _vr = DicomVR.SQ; } _state = ParseState.Value; } if (_state == ParseState.Value) { // check dictionary for VR after reading length to handle 16-bit lengths // check before reading value to handle SQ elements if (_vr == DicomVR.UN && IsExplicitVR) { var entry = Dictionary[_tag]; if (entry != null) _vr = entry.ValueRepresentations.FirstOrDefault(); if (_vr == null) _vr = DicomVR.UN; } if (_tag == DicomTag.ItemDelimitationItem) { // end of sequence item return; } while (_vr == DicomVR.SQ && _tag.IsPrivate) { if (!IsPrivateSequence(source)) { _vr = DicomVR.UN; break; } if (IsPrivateSequenceBad(source)) { _badPrivateSequence = true; _explicit = !_explicit; } break; } if (_vr == DicomVR.SQ) { // start of sequence _observer.OnBeginSequence(source, _tag, _length); _state = ParseState.Tag; if (_length != UndefinedLength) { _implicit = false; source.PushMilestone(_length); } else _implicit = true; PushState(state); ParseItemSequence(source, null); continue; } if (_length == UndefinedLength) { _observer.OnBeginFragmentSequence(source, _tag, _vr); _state = ParseState.Tag; PushState(state); ParseFragmentSequence(source, null); continue; } if (!source.Require(_length, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } IByteBuffer buffer = source.GetBuffer(_length); if (!_vr.IsString) buffer = EndianByteBuffer.Create(buffer, source.Endian, _vr.UnitSize); _observer.OnElement(source, _tag, _vr, buffer); // parse private creator value and add to lookup table if (_tag.IsPrivate && _tag.Element != 0x0000 && _tag.Element <= 0x00ff) { var creator = DicomEncoding.Default.GetString(buffer.Data, 0, buffer.Data.Length).TrimEnd((char)DicomVR.LO.PaddingValue); var card = (uint)(_tag.Group << 16) + (uint)(_tag.Element); _private[card] = creator; } ResetState(); } } if (source.HasReachedMilestone()) { // end of explicit length sequence item source.PopMilestone(); return; } if (_result != DicomReaderResult.Processing) return; // end of processing _result = DicomReaderResult.Success; } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
private void ParseItemSequence(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF && !source.HasReachedMilestone()) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(8, ParseItemSequence, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); _tag = new DicomTag(group, element); if (_tag != DicomTag.Item && _tag != DicomTag.SequenceDelimitationItem) { // assume invalid sequence source.Rewind(); if (!_implicit) source.PopMilestone(); _observer.OnEndSequence(); if (_badPrivateSequence) { _explicit = !_explicit; _badPrivateSequence = false; } return; } _length = source.GetUInt32(); if (_tag == DicomTag.SequenceDelimitationItem) { // end of sequence _observer.OnEndSequence(); if (_badPrivateSequence) { _explicit = !_explicit; _badPrivateSequence = false; } ResetState(); return; } _state = ParseState.Value; } if (_state == ParseState.Value) { if (_length != UndefinedLength) { if (!source.Require(_length, ParseItemSequence, state)) { _result = DicomReaderResult.Suspended; return; } source.PushMilestone(_length); } _observer.OnBeginSequenceItem(source, _length); ResetState(); ParseDataset(source, state); ResetState(); _observer.OnEndSequenceItem(); continue; } } // end of explicit length sequence if (source.HasReachedMilestone()) source.PopMilestone(); _observer.OnEndSequence(); if (_badPrivateSequence) { _explicit = !_explicit; _badPrivateSequence = false; } } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
private bool IsPrivateSequenceBad(IByteSource source) { source.Mark(); try { var group = source.GetUInt16(); var element = source.GetUInt16(); var tag = new DicomTag(group, element); var length = source.GetUInt32(); group = source.GetUInt16(); element = source.GetUInt16(); tag = new DicomTag(group, element); byte[] bytes = source.GetBytes(2); string vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); try { DicomVR.Parse(vr); return !_explicit; } catch { // unable to parse VR if (_explicit) return true; } } finally { source.Rewind(); } return false; }
private bool ParseLength(IByteSource source) { while (_parseStage == ParseStage.Length) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { if (!source.Require(4)) { _result = DicomReaderResult.Suspended; return(false); } _length = source.GetUInt32(); _parseStage = ParseStage.Value; break; } if (_isExplicitVR || _badPrivateSequence) { if (_vr == DicomVR.Implicit) { if (!source.Require(4)) { _result = DicomReaderResult.Suspended; return(false); } _length = source.GetUInt32(); // assume that undefined length in implicit VR element is SQ if (_length == _undefinedLength) { _vr = DicomVR.SQ; } } else if (_vr.Is16bitLength) { if (!source.Require(2)) { _result = DicomReaderResult.Suspended; return(false); } _length = source.GetUInt16(); } else { if (!source.Require(6)) { _result = DicomReaderResult.Suspended; return(false); } source.Skip(2); _length = source.GetUInt32(); // CP-246 (#177) handling // assume that Undefined Length in explicit datasets with VR UN are sequences // According to CP-246 the sequence shall be handled as ILE, but this will be handled later... // in the current code this needs to be restricted to privates if (_length == _undefinedLength && _vr == DicomVR.UN && _tag.IsPrivate) { _vr = DicomVR.SQ; } } } else { if (!source.Require(4)) { _result = DicomReaderResult.Suspended; return(false); } _length = source.GetUInt32(); // assume that undefined length in implicit dataset is SQ if (_length == _undefinedLength && _vr == DicomVR.UN) { _vr = DicomVR.SQ; } } _parseStage = ParseStage.Value; } return(true); }
private bool ParseItemSequenceTag(IByteSource source) { if (_parseStage == ParseStage.Tag) { source.Mark(); if (!source.Require(8)) { _result = DicomReaderResult.Suspended; return(false); } var group = source.GetUInt16(); var element = source.GetUInt16(); _tag = new DicomTag(@group, element); if (_tag != DicomTag.Item && _tag != DicomTag.SequenceDelimitationItem) { // assume invalid sequence source.Rewind(); if (!_implicit) { source.PopMilestone(); } _observer.OnEndSequence(); // #565 Only reset the badPrivate sequence if we're in the correct depth // This prevents prematurely resetting in case of sub-sequences contained in the bad private sequence if (_badPrivateSequence && _sequenceDepth == _badPrivateSequenceDepth) { _isExplicitVR = !_isExplicitVR; _badPrivateSequence = false; } return(false); } _length = source.GetUInt32(); if (_tag == DicomTag.SequenceDelimitationItem) { // #64, in case explicit length has been specified despite occurrence of Sequence Delimitation Item if (source.HasReachedMilestone() && source.MilestonesCount > _sequenceDepth) { ResetState(); return(true); } // end of sequence _observer.OnEndSequence(); // #565 Only reset the badPrivate sequence if we're in the correct depth // This prevents prematurely resetting in case of sub-sequences contained in the bad private sequence if (_badPrivateSequence && _sequenceDepth == _badPrivateSequenceDepth) { _isExplicitVR = !_isExplicitVR; _badPrivateSequence = false; } ResetState(); return(false); } _parseStage = ParseStage.Value; } return(true); }
private void ParseDataset(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF && !source.HasReachedMilestone()) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); DicomPrivateCreator creator = null; if (group.IsOdd() && element > 0x00ff) { string pvt = null; uint card = (uint)(group << 16) + (uint)(element >> 8); if (_private.TryGetValue(card, out pvt)) { creator = Dictionary.GetPrivateCreator(pvt); } } _tag = new DicomTag(group, element, creator); if (_stop != null && _tag.CompareTo(_stop) >= 0) { _result = DicomReaderResult.Stopped; return; } _state = ParseState.VR; } while (_state == ParseState.VR) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { _vr = DicomVR.NONE; _state = ParseState.Length; break; } if (IsExplicitVR) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } byte[] bytes = source.GetBytes(2); string vr = Encoding.ASCII.GetString(bytes); _vr = DicomVR.Parse(vr); } else { DicomDictionaryEntry entry = Dictionary[_tag]; if (entry != null) { if (entry.ValueRepresentations.Contains(DicomVR.OB) && entry.ValueRepresentations.Contains(DicomVR.OW)) { _vr = DicomVR.OW; // ??? } else { _vr = entry.ValueRepresentations.FirstOrDefault(); } } } if (_vr == null) { _vr = DicomVR.UN; } _state = ParseState.Length; if (_vr == DicomVR.UN) { if (_tag.Element == 0x0000) { // Group Length to UL _vr = DicomVR.UL; break; } if (_tag.Group.IsOdd()) { if (_tag.Element <= 0x00ff) { // Private Creator to LO _vr = DicomVR.LO; break; } else if (IsExplicitVR) { DicomDictionaryEntry entry = Dictionary[_tag]; if (entry != null) { _vr = entry.ValueRepresentations.FirstOrDefault(); } if (_vr == null) { _vr = DicomVR.UN; } break; } } } } while (_state == ParseState.Length) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); _state = ParseState.Value; break; } if (IsExplicitVR) { if (_vr.Is16bitLength) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt16(); } else { if (!source.Require(6, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } source.Skip(2); _length = source.GetUInt32(); } } else { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); } _state = ParseState.Value; } if (_state == ParseState.Value) { if (_tag == DicomTag.ItemDelimitationItem) { // end of sequence item ParseItemSequence(source, state); return; } if (_vr == DicomVR.SQ) { // start of sequence _observer.OnBeginSequence(source, _tag, _length); _state = ParseState.Tag; if (_length != UndefinedLength) { source.PushMilestone(_length); } PushState(state); ParseItemSequence(source, null); continue; } if (_length == UndefinedLength) { _observer.OnBeginFragmentSequence(source, _tag, _vr); _state = ParseState.Tag; PushState(state); ParseFragmentSequence(source, null); continue; } if (!source.Require(_length, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } IByteBuffer buffer = source.GetBuffer(_length); if (!_vr.IsString) { buffer = EndianByteBuffer.Create(buffer, source.Endian, _vr.UnitSize); } _observer.OnElement(source, _tag, _vr, buffer); ResetState(); } } if (source.HasReachedMilestone()) { // end of explicit length sequence item _observer.OnEndSequenceItem(); source.PopMilestone(); ParseItemSequence(source, state); return; } // end of processing _result = DicomReaderResult.Success; } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
private bool ParseFragmentSequenceTag(IByteSource source) { if (this.parseStage == ParseStage.Tag) { source.Mark(); if (!source.Require(8)) { this.result = DicomReaderResult.Suspended; return false; } var group = source.GetUInt16(); var element = source.GetUInt16(); DicomTag tag = new DicomTag(@group, element); if (tag != DicomTag.Item && tag != DicomTag.SequenceDelimitationItem) { throw new DicomReaderException("Unexpected tag in DICOM fragment sequence: {0}", tag); } this.length = source.GetUInt32(); if (tag == DicomTag.SequenceDelimitationItem) { // end of fragment this.observer.OnEndFragmentSequence(); this.fragmentItem = 0; this.ResetState(); return false; } this.fragmentItem++; this.parseStage = ParseStage.Value; } return true; }
private bool ParseItemSequenceTag(IByteSource source) { if (this.parseStage == ParseStage.Tag) { source.Mark(); if (!source.Require(8)) { this.result = DicomReaderResult.Suspended; return false; } var group = source.GetUInt16(); var element = source.GetUInt16(); this._tag = new DicomTag(@group, element); if (this._tag != DicomTag.Item && this._tag != DicomTag.SequenceDelimitationItem) { // assume invalid sequence source.Rewind(); if (!this._implicit) { source.PopMilestone(); } this.observer.OnEndSequence(); if (this.badPrivateSequence) { this.isExplicitVR = !this.isExplicitVR; this.badPrivateSequence = false; } return false; } this.length = source.GetUInt32(); if (this._tag == DicomTag.SequenceDelimitationItem) { // #64, in case explicit length has been specified despite occurrence of Sequence Delimitation Item if (source.HasReachedMilestone() && source.MilestonesCount > this.sequenceDepth) { this.ResetState(); return true; } // end of sequence this.observer.OnEndSequence(); if (this.badPrivateSequence) { this.isExplicitVR = !this.isExplicitVR; this.badPrivateSequence = false; } this.ResetState(); return false; } this.parseStage = ParseStage.Value; } return true; }
private bool ParseLength(IByteSource source) { while (this.parseStage == ParseStage.Length) { if (this._tag == DicomTag.Item || this._tag == DicomTag.ItemDelimitationItem || this._tag == DicomTag.SequenceDelimitationItem) { if (!source.Require(4)) { this.result = DicomReaderResult.Suspended; return false; } this.length = source.GetUInt32(); this.parseStage = ParseStage.Value; break; } if (this.isExplicitVR) { if (this._vr == DicomVR.Implicit) { if (!source.Require(4)) { this.result = DicomReaderResult.Suspended; return false; } this.length = source.GetUInt32(); // assume that undefined length in implicit VR element is SQ if (this.length == UndefinedLength) { this._vr = DicomVR.SQ; } } else if (this._vr.Is16bitLength) { if (!source.Require(2)) { this.result = DicomReaderResult.Suspended; return false; } this.length = source.GetUInt16(); } else { if (!source.Require(6)) { this.result = DicomReaderResult.Suspended; return false; } source.Skip(2); this.length = source.GetUInt32(); // CP-246 (#177) handling // assume that Undefined Length in explicit datasets with VR UN are sequences // According to CP-246 the sequence shall be handled as ILE, but this will be handled later... // in the current code this needs to be restricted to privates if (this.length == UndefinedLength && this._vr == DicomVR.UN && this._tag.IsPrivate) { this._vr = DicomVR.SQ; } } } else { if (!source.Require(4)) { this.result = DicomReaderResult.Suspended; return false; } this.length = source.GetUInt32(); // assume that undefined length in implicit dataset is SQ if (this.length == UndefinedLength && this._vr == DicomVR.UN) { this._vr = DicomVR.SQ; } } this.parseStage = ParseStage.Value; } return true; }
private static bool IsPrivateSequenceBad(IByteSource source, bool isExplicitVR) { source.Mark(); try { source.GetUInt16(); // group source.GetUInt16(); // element source.GetUInt32(); // length source.GetUInt16(); // group source.GetUInt16(); // element var bytes = source.GetBytes(2); var vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); DicomVR dummy; if (DicomVR.TryParse(vr, out dummy)) return !isExplicitVR; // unable to parse VR if (isExplicitVR) return true; } finally { source.Rewind(); } return false; }