From adfd8254412276033a6890a2194e1f9fee82b5c7 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Wed, 11 Feb 2009 00:46:01 +0100 Subject: [PATCH] SHA1, dictionary improvements - added class OSHA1stream for a stream-based calculation method - dictionary gets digest() method - dictionaryEntry tweak: avoid trailing space after dictionary keyword --- applications/test/sha1/testSHA1.C | 58 ++++- .../db/IOstreams/hashes/OSHA1stream.H | 212 ++++++++++++++++++ src/OpenFOAM/db/dictionary/dictionary.C | 20 ++ src/OpenFOAM/db/dictionary/dictionary.H | 4 + .../dictionaryEntry/dictionaryEntryIO.C | 8 +- 5 files changed, 295 insertions(+), 7 deletions(-) create mode 100644 src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H diff --git a/applications/test/sha1/testSHA1.C b/applications/test/sha1/testSHA1.C index 4022237574f..603829fac42 100644 --- a/applications/test/sha1/testSHA1.C +++ b/applications/test/sha1/testSHA1.C @@ -30,8 +30,9 @@ Description \*---------------------------------------------------------------------------*/ -#include "SHA1.H" -#include "IOstreams.H" +#include "OSHA1stream.H" +#include "IStringStream.H" +#include "dictionary.H" using namespace Foam; @@ -66,7 +67,7 @@ int main(int argc, char * argv[]) Info<<"SHA1 digests are different\n"; } Info<<"lhs:" << sha << " rhs:" << shaDig << endl; - + // start over: sha.clear(); sha.append(str); @@ -80,6 +81,55 @@ int main(int argc, char * argv[]) Info<< "digest1: " << sha_A << nl; Info<< "digest2: " << sha << nl; - + // start over: + sha.clear(); + sha.append("\""); + sha.append(str); + sha.append("\""); + + Info<< "digest3: " << sha << nl; + + // try the output buffer interface + { + OSHA1stream os; + + os << str; + Info<< os.digest() << endl; + + os << str; + Info<< os.digest() << endl; + + os.rewind(); + os << "The quick brown fox jumps over the lazy dog"; + Info<< os.digest() << endl; + + } + + { + dictionary dict + ( + IStringStream + ( + "parent { Default_Boundary_Region { type zeroGradient; } }" + "inlet_1 { value inlet_1; }" + "inlet_2 { value inlet_2; }" + "inlet_3 { value inlet_3; }" + "\"inlet_.*\" { value XXX; }" + ) () + ); + + Info<< "dict:" << endl; + dict.write(Info, false); + + dictionary dict2(dict); + + OSHA1stream os; + dict.write(os, false); + Info<< os.digest() << endl; + + Info<< dict2.digest() << endl; + } + + return 0; } diff --git a/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H b/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H new file mode 100644 index 00000000000..c61da69d910 --- /dev/null +++ b/src/OpenFOAM/db/IOstreams/hashes/OSHA1stream.H @@ -0,0 +1,212 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Class + Foam::OSHA1stream + +Description + An output stream for calculating SHA1 digests. + +SourceFiles + OSHA1stream.C + +\*---------------------------------------------------------------------------*/ + +#ifndef OSHA1stream_H +#define OSHA1stream_H + +#include "OSstream.H" +#include "SHA1.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +class osha1stream; +class OSHA1stream; + +/*---------------------------------------------------------------------------*\ + Class sha1streambuf Declaration +\*---------------------------------------------------------------------------*/ + +//- A streambuf class for calculating SHA1 digests +class sha1streambuf +: + public std::streambuf +{ + // Private data + + //- This does all the work and has its own buffering + SHA1 sha1_; + + friend class osha1stream; + +public: + + // Constructors + + //- Construct null + sha1streambuf() + {} + + // Member Functions + + // Write + + //- Process unbuffered + virtual std::streamsize xsputn(const char* str, std::streamsize n) + { + sha1_.append(str, n); + return n; + } +}; + + +/*---------------------------------------------------------------------------*\ + Class osha1stream Declaration +\*---------------------------------------------------------------------------*/ + +//- A basic output stream for calculating SHA1 digests +class osha1stream +: + virtual public std::ios, + public std::ostream +{ + // Private data + + sha1streambuf sbuf_; + +public: + + // Constructors + + //- Construct null + osha1stream() + : + std::ostream(&sbuf_) + {} + + // Member Functions + + // Access + + //- This hides both signatures of std::basic_ios::rdbuf() + sha1streambuf* rdbuf() + { + return &sbuf_; + } + + //- Full access to the sha1 + SHA1& sha1() + { + return sbuf_.sha1_; + } + +}; + + +/*---------------------------------------------------------------------------*\ + Class OSHA1stream Declaration +\*---------------------------------------------------------------------------*/ + +//- The output stream for calculating SHA1 digests +class OSHA1stream +: + public OSstream +{ + + // Private Member Functions + + //- Disallow default bitwise copy construct + OSHA1stream(const OSHA1stream&); + + //- Disallow default bitwise assignment + void operator=(const OSHA1stream&); + +public: + + // Constructors + + //- Construct and set stream status + OSHA1stream + ( + streamFormat format=ASCII, + versionNumber version=currentVersion + ) + : + OSstream + ( + *(new osha1stream()), + "OSHA1stream.sinkFile_", + format, + version + ) + {} + + + // Destructor + + ~OSHA1stream() + { + delete &dynamic_cast<osha1stream&>(stream()); + } + + + // Member functions + + // Access + + //- Full access to the sha1 + Foam::SHA1& sha1() + { + return dynamic_cast<osha1stream&>(stream()).sha1(); + } + + //- Return SHA1::Digest for the data processed until now + Foam::SHA1::Digest digest() + { + return sha1().digest(); + } + + // Edit + + //- Clear the SHA1 calculation + void rewind() + { + sha1().clear(); + } + +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index 8acc6e71ff8..985aee472c8 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -28,6 +28,7 @@ License #include "primitiveEntry.H" #include "dictionaryEntry.H" #include "regExp.H" +#include "OSHA1stream.H" /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */ @@ -232,6 +233,25 @@ Foam::label Foam::dictionary::endLineNumber() const } +Foam::SHA1::Digest Foam::dictionary::digest() const +{ + OSHA1stream os; + + // process entries + for + ( + IDLList<entry>::const_iterator iter = begin(); + iter != end(); + ++iter + ) + { + os << *iter; + } + + return os.digest(); +} + + bool Foam::dictionary::found(const word& keyword, bool recursive) const { if (hashedEntries_.found(keyword)) diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 5316f053653..027909ba22e 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -60,6 +60,7 @@ SourceFiles #include "HashTable.H" #include "wordList.H" #include "className.H" +#include "SHA1.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -210,6 +211,9 @@ public: //- Return line number of last token in dictionary label endLineNumber() const; + //- Return the SHA1 digest of the dictionary contents + SHA1::Digest digest() const; + // Search and lookup diff --git a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntryIO.C b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntryIO.C index 46fd58242e1..4ed38340530 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntryIO.C @@ -45,7 +45,7 @@ Foam::dictionaryEntry::dictionaryEntry is.fatalCheck ( "dictionaryEntry::dictionaryEntry" - "(const dictionary& parentDict, Istream& is)" + "(const dictionary& parentDict, Istream&)" ); } @@ -65,7 +65,7 @@ Foam::dictionaryEntry::dictionaryEntry is.fatalCheck ( "dictionaryEntry::dictionaryEntry" - "(const keyType& keyword, const dictionary& parentDict, Istream& is)" + "(const keyType&, const dictionary& parentDict, Istream&)" ); } @@ -74,7 +74,9 @@ Foam::dictionaryEntry::dictionaryEntry void Foam::dictionaryEntry::write(Ostream& os) const { - os.writeKeyword(keyword()); + // write keyword with indent but without trailing spaces + os.indent(); + os.write(keyword()); dictionary::write(os); } -- GitLab