Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve comment about magic word not present. #318

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const (
)

var (
// ErrorMagicWord indicates that the magic word was not found in the correct
// location in the DICOM.
ErrorMagicWord = errors.New("error, DICM magic word not found in correct location")
// ErrorMetaElementGroupLength indicates that the MetaElementGroupLength
// was not found where expected in the metadata.
ErrorMetaElementGroupLength = errors.New("MetaElementGroupLength tag not found where expected")
Expand Down Expand Up @@ -281,8 +278,8 @@ func SkipPixelData() ParseOption {
// a PixelData element will be added to the dataset with the
// PixelDataInfo.IntentionallyUnprocessed = true, and the raw bytes of the
// entire PixelData element stored in PixelDataInfo.UnprocessedValueData.
//
// In the future, we may be able to extend this functionality to support
//
// In the future, we may be able to extend this functionality to support
// on-demand processing of elements elsewhere in the library.
func SkipProcessingPixelDataValue() ParseOption {
return func(set *parseOptSet) {
Expand Down
5 changes: 4 additions & 1 deletion read.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
)

var (
// ErrorMagicWord indicates that the magic word was not found in the correct
// location in the DICOM.
ErrorMagicWord = errors.New("error, DICM magic word not found in correct location")
// ErrorOWRequiresEvenVL indicates that an element with VR=OW had a not even
// value length which is not allowed.
ErrorOWRequiresEvenVL = errors.New("vr of OW requires even value length")
Expand Down Expand Up @@ -160,7 +163,7 @@ func (r *reader) readHeader() ([]*Element, error) {
return nil, err
}
if string(data[128:]) != magicWord {
return nil, nil
return nil, ErrorMagicWord
Copy link
Owner

@suyashkumar suyashkumar May 29, 2024

Choose a reason for hiding this comment

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

I think the reason for this was the note at L157: namely, if the magic word isn't found we try to parse the Dataset without the preamble: #59. This was reported by folks who had files like this in production! Perhaps we should make the user pass an explicit parse option to trigger this behavior if we think there's harm in doing this as a default.

Of course, parsing without metadata can cause all sorts of problems which may eventually result in an error anyway.

Copy link
Owner

Choose a reason for hiding this comment

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

@wkoszek do y'all recall if this fix actually lead to y'all successfully parsing DICOMs without the header?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I reverted the commit, but I updated the comment because it's rather unusual to see a function returning nil, nil like that and the comment was a bit hidden up top. Will update PR title

Copy link
Owner

Choose a reason for hiding this comment

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

Agreed, thanks for the update!

}

err = r.rawReader.Skip(128 + 4) // skip preamble + magic word
Expand Down
8 changes: 8 additions & 0 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,14 @@ func headerWithFileMetaInformationGroupLength() (*headerData, error) {
return headerData, nil
}

func TestReadHeader_InvalidMagicWord(t *testing.T) {
notDICOMReader := bufio.NewReader(bytes.NewReader(make([]byte, 128+4)))
r := &reader{rawReader: dicomio.NewReader(notDICOMReader, binary.LittleEndian, dicomio.LimitReadUntilEOF)}
if _, err := r.readHeader(); err != ErrorMagicWord {
t.Fatalf("Expected %v error, got %v", ErrorMagicWord, err)
}
}

func TestReadHeader_TryAllowErrorMetaElementGroupLength(t *testing.T) {
opts := parseOptSet{allowMissingMetaElementGroupLength: true}

Expand Down
Loading