OSHA1stream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9  Copyright (C) 2019-2024 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::OSHA1stream
29 
30 Description
31  An output stream for calculating SHA1 digests.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Foam_OSHA1stream_H
36 #define Foam_OSHA1stream_H
37 
38 #include "SHA1.H"
39 #include "OSstream.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 /*---------------------------------------------------------------------------*\
47  Class osha1stream Declaration
48 \*---------------------------------------------------------------------------*/
49 
50 //- A basic output stream for calculating SHA1 digests
51 class osha1stream
52 :
53  virtual public std::ios,
54  public std::ostream
55 {
56  //- A streambuf class for calculating SHA1 digests
57  class sha1buf : public std::streambuf
58  {
59  //- This does all the work and has its own buffering
60  SHA1 sha1_;
61 
62  protected:
63 
64  //- Output overflow handling - append to SHA1
65  virtual int overflow(int_type c = traits_type::eof())
66  {
67  if (c != traits_type::eof()) sha1_.append(c);
68  return c;
69  }
70 
71  //- Put sequence of characters - append to SHA1
72  virtual std::streamsize xsputn(const char* s, std::streamsize n)
73  {
74  if (n) sha1_.append(s, n);
75  return n;
76  }
77 
78  public:
79 
80  //- Default construct
81  sha1buf() = default;
82 
83  //- Full access to the sha1
84  SHA1& sha1() noexcept { return sha1_; }
85  };
86 
87 
88  // Private Data
89 
90  //- Reference to the underlying buffer
91  sha1buf buf_;
92 
93 public:
94 
95  // Constructors
96 
97  //- Default construct
98  osha1stream() : std::ostream(&buf_) {}
99 
100 
101  // Member Functions
102 
103  //- This hides both signatures of std::basic_ios::rdbuf()
104  sha1buf* rdbuf() { return &buf_; }
105 
106  //- Full access to the sha1
107  SHA1& sha1() noexcept { return buf_.sha1(); }
108 
109  //- Return SHA1::Digest for the data processed until now
110  SHA1Digest digest() { return buf_.sha1().digest(); }
111 
112  //- Clear the SHA1 calculation
113  void reset() { buf_.sha1().clear(); }
114 };
115 
116 
117 /*---------------------------------------------------------------------------*\
118  Class OSHA1stream Declaration
119 \*---------------------------------------------------------------------------*/
120 
121 //- The output stream for calculating SHA1 digests
123 :
124  public Foam::Detail::StreamAllocator<Foam::osha1stream>,
125  public Foam::OSstream
126 {
127  typedef
129  allocator_type;
130 
131 public:
133  //- No copy construct
134  OSHA1stream(const OSHA1stream&) = delete;
135 
136  //- No copy assignment
137  void operator=(const OSHA1stream&) = delete;
138 
139 
140  // Constructors
141 
142  //- Construct with an empty digest
143  explicit OSHA1stream
144  (
145  IOstreamOption streamOpt = IOstreamOption()
146  )
147  :
148  allocator_type(),
149  OSstream(stream_, "sha1", streamOpt.format(), streamOpt.version())
150  {}
151 
152 
153  // Member Functions
154 
155  //- Full access to the sha1
156  SHA1& sha1() noexcept { return stream_.sha1(); }
157 
158  //- Return SHA1::Digest for the data processed until now
159  SHA1Digest digest() { return stream_.digest(); }
160 
161  //- Clear the SHA1 calculation
162  void reset() { stream_.reset(); }
163 
164 
165  // Write Functions
166 
167  //- Add (unquoted) string contents.
168  // Ensures that SHA1 of C-string or C++-string content are identical.
169  virtual Ostream& write(const std::string& str) override
170  {
171  return writeQuoted(str, false); // Unquoted!
172  }
173 
174 
175  // Housekeeping
176 
177  //- Deprecated(2017-07) clear the SHA1 calculation
178  // \deprecated(2017-07) - use reset() method
179  void rewind()
180  {
181  stream_.sha1().clear();
182  }
183 
184 
185  // Additional constructors and methods (as per v2012 and earlier)
186  #ifdef Foam_IOstream_extras
187 
188  //- Construct with an empty digest, using given format
190  :
192  {}
193 
194  #endif /* Foam_IOstream_extras */
195 };
196 
197 
198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199 
200 } // End namespace Foam
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 #endif
206 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
A wrapper to hold a std::stream type for OpenFOAM wrapped streams. This is necessary since the OpenFO...
Definition: IOstream.H:600
SHA1 & sha1() noexcept
Full access to the sha1.
Definition: OSHA1stream.H:122
void clear() noexcept
Reset the hashed data before appending more.
Definition: SHA1.C:314
void reset()
Clear the SHA1 calculation.
Definition: OSHA1stream.H:195
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition: OSstream.C:101
A basic output stream for calculating SHA1 digests.
Definition: OSHA1stream.H:46
The SHA1 message digest.
Definition: SHA1Digest.H:57
A simple container for options an IOstream can normally have.
The output stream for calculating SHA1 digests.
Definition: OSHA1stream.H:143
SHA1Digest digest()
Return SHA1::Digest for the data processed until now.
Definition: OSHA1stream.H:127
void rewind()
Deprecated(2017-07) clear the SHA1 calculation.
Definition: OSHA1stream.H:218
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
void operator=(const OSHA1stream &)=delete
No copy assignment.
Foam::osha1stream stream_
The std::stream.
Definition: IOstream.H:609
SHA1 & sha1() noexcept
Full access to the sha1.
Definition: OSHA1stream.H:185
const direction noexcept
Definition: scalarImpl.H:255
OSHA1stream(const OSHA1stream &)=delete
No copy construct.
virtual Ostream & write(const std::string &str) override
Add (unquoted) string contents.
Definition: OSHA1stream.H:205
const dimensionedScalar c
Speed of light in a vacuum.
void append(char c)
Append single character.
Definition: SHA1I.H:46
versionNumber version() const noexcept
Get the stream version.
SHA1Digest digest()
Return SHA1::Digest for the data processed until now.
Definition: OSHA1stream.H:190
streamFormat
Data format (ascii | binary)
label n
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:56
void reset()
Clear the SHA1 calculation.
Definition: OSHA1stream.H:132
osha1stream()
Default construct.
Definition: OSHA1stream.H:109
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Ostream(const Ostream &)=default
Copy construct.
streamFormat format() const noexcept
Get the current stream format.
Namespace for OpenFOAM.
sha1buf * rdbuf()
This hides both signatures of std::basic_ios::rdbuf()
Definition: OSHA1stream.H:117