Skip to content

Commit

Permalink
BUG: DCMTK reader wrongly rejects file with preamble
Browse files Browse the repository at this point in the history
A previous optimization didn't take into account the preamble
of a dicom file. This adds a check.

Fixes #4108.
  • Loading branch information
pieper authored and thewtex committed Mar 12, 2024
1 parent fc02910 commit b04aed2
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Modules/IO/DCMTK/src/itkDCMTKImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@
#include "dcmtk/dcmdata/dcrledrg.h"
#include "dcmtk/oflog/oflog.h"

namespace
{

/**
* Helper function to test for 128 byte dicom preamble
* @param file A stream to test if the file is dicom like
* @return true if the stream has a dicom preamble
*/
static bool
readPreambleDicom(std::ifstream & file)
{
char preamble[132] = "";

file.read(preamble, sizeof(preamble));

return (preamble[128] == 'D' && preamble[129] == 'I' && preamble[130] == 'C' && preamble[131] == 'M');
}

} // end anonymous namespace

namespace itk
{
/** Constructor */
Expand Down Expand Up @@ -220,6 +240,8 @@ DCMTKImageIO::CanReadFile(const char * filename)
#if !defined(__EMSCRIPTEN__)
{
std::ifstream file;

// look for a preamble
try
{
this->OpenFileForReading(file, filename);
Expand All @@ -228,9 +250,11 @@ DCMTKImageIO::CanReadFile(const char * filename)
{
return false;
}
const bool hasdicompreamble = readPreambleDicom(file);
file.seekg(0, std::ios::beg);
const bool hasdicomsig = readNoPreambleDicom(file);
file.close();
if (!hasdicomsig)
if (!hasdicomsig && !hasdicompreamble)
{
return false;
}
Expand Down

0 comments on commit b04aed2

Please sign in to comment.