From: malaterre Date: Tue, 16 Nov 2004 02:04:00 +0000 (+0000) Subject: ENH: Apply first patch toward better string comparison when dealing with broken DICOM... X-Git-Tag: Version1.0.bp~604 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=609c6adb9848fef0dc437626b77c72a3fb68d33f;p=gdcm.git ENH: Apply first patch toward better string comparison when dealing with broken DICOM file. Essentially the string could be padded with a space instead of a null character as defined by standard --- diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt index 37e5ccd9..d53cbc71 100644 --- a/Testing/CMakeLists.txt +++ b/Testing/CMakeLists.txt @@ -12,6 +12,7 @@ SET(TEST_SOURCES TestHash.cxx TestTS.cxx TestVR.cxx + TestUtil.cxx TestDicomString.cxx ) diff --git a/Testing/TestUtil.cxx b/Testing/TestUtil.cxx new file mode 100644 index 00000000..fb9cb97c --- /dev/null +++ b/Testing/TestUtil.cxx @@ -0,0 +1,21 @@ +// This test should test everything in Util, since I didn't know any other +// way to test this class. + +#include "gdcm.h" + +int TestUtil(int , char * []) +{ + const char ref[] = "MONOCHROME1"; + std::string a = "MONOCHROME1"; + a += '\0'; + std::string b = "MONOCHROME1 "; + std::string c = gdcm::Util::DicomString("MONOCHROME1"); + std::string d = "MONOCHROME1"; + + if( !gdcm::Util::DicomStringEqual(a,ref) ) return 1; + if( !gdcm::Util::DicomStringEqual(b,ref) ) return 1; + if( !gdcm::Util::DicomStringEqual(c,ref) ) return 1; + if( gdcm::Util::DicomStringEqual(d,ref) ) return 1; + + return 0; +} diff --git a/src/gdcmUtil.cxx b/src/gdcmUtil.cxx index 9c096bea..30adaed2 100644 --- a/src/gdcmUtil.cxx +++ b/src/gdcmUtil.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmUtil.cxx,v $ Language: C++ - Date: $Date: 2004/11/14 00:53:10 $ - Version: $Revision: 1.62 $ + Date: $Date: 2004/11/16 02:04:00 $ + Version: $Revision: 1.63 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -197,27 +197,6 @@ std::string Util::GetName(std::string const & fullName) } } -/** - * \ingroup Util - * \brief Create a /DICOM/ string: - * It should a of even lenght (no odd length ever) - * It can contains as many \0 as you want. - * This function is similar to DicomString(const char*), - * except it doesn't take a lenght. - * It only pad with a null character if length is odd - */ -std::string Util::DicomString(const char* s) -{ - size_t l = strlen(s); - if( l%2 ) - { - l++; - } - std::string r(s, s+l); - assert( !(r.size() % 2) ); - return r; -} - /** * \ingroup Util * \brief Get the current date of the system in a dicom string @@ -252,11 +231,51 @@ std::string Util::GetCurrentTime() */ std::string Util::DicomString(const char* s, size_t l) { + std::string r(s, s+l); + assert( !(r.size() % 2) ); // == basically 'l' is even + return r; +} + +/** + * \ingroup Util + * \brief Create a /DICOM/ string: + * It should a of even lenght (no odd length ever) + * It can contains as many \0 as you want. + * This function is similar to DicomString(const char*), + * except it doesn't take a lenght. + * It only pad with a null character if length is odd + */ +std::string Util::DicomString(const char* s) +{ + size_t l = strlen(s); + if( l%2 ) + { + l++; + } std::string r(s, s+l); assert( !(r.size() % 2) ); return r; } +/** + * \ingroup Util + * \brief Safely compare two Dicom String: + * - Both string should be of even lenght + * - We allow padding of even lenght string by either a null + * character of a space + */ +bool Util::DicomStringEqual(const std::string& s1, const char *s2) +{ + // s2 is the string from the DICOM reference: 'MONOCHROME1' + std::string s1_even = s1; //Never directly change input parameter + std::string s2_even = DicomString( s2 ); + if( s1_even[s1_even.size()-1] == ' ') + { + s1_even[s1_even.size()-1] = '\0'; //replace space character by null + } + return s1_even == s2_even; +} + template std::ostream& binary_write(std::ostream& os, const T& val) { diff --git a/src/gdcmUtil.h b/src/gdcmUtil.h index 76436936..99cb2555 100644 --- a/src/gdcmUtil.h +++ b/src/gdcmUtil.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmUtil.h,v $ Language: C++ - Date: $Date: 2004/11/10 18:27:24 $ - Version: $Revision: 1.42 $ + Date: $Date: 2004/11/16 02:04:00 $ + Version: $Revision: 1.43 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -53,6 +53,7 @@ public: static std::string DicomString(const char* s, size_t l); static std::string DicomString(const char* s); + static bool DicomStringEqual(const std::string& s1, const char *s2); }; template