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-2023 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 "OSstream.H"
39 #include "SHA1.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 namespace Detail
118 {
119 
120 /*---------------------------------------------------------------------------*\
121  Class Detail::OSHA1streamAllocator Declaration
122 \*---------------------------------------------------------------------------*/
123 
124 //- An allocator for holding Foam::osha1stream
125 class OSHA1streamAllocator
126 {
127 protected:
128 
129  // Protected Data
130 
131  //- The output stream
133 
134 
135  // Constructors
136 
137  //- Default construct
138  OSHA1streamAllocator() = default;
139 };
140 
141 } // End namespace Detail
142 
143 
144 /*---------------------------------------------------------------------------*\
145  Class OSHA1stream Declaration
146 \*---------------------------------------------------------------------------*/
147 
148 //- The output stream for calculating SHA1 digests
149 class OSHA1stream
150 :
152  public OSstream
153 {
154  typedef Detail::OSHA1streamAllocator allocator_type;
156 public:
157 
158  //- No copy construct
159  OSHA1stream(const OSHA1stream&) = delete;
160 
161  //- No copy assignment
162  void operator=(const OSHA1stream&) = delete;
163 
164 
165  // Constructors
166 
167  //- Construct with an empty digest
168  explicit OSHA1stream
169  (
170  IOstreamOption streamOpt = IOstreamOption()
171  )
172  :
173  allocator_type(),
174  OSstream(stream_, "sha1", streamOpt.format(), streamOpt.version())
175  {}
177 
178  // Member Functions
179 
180  //- Full access to the sha1
181  SHA1& sha1() noexcept { return stream_.sha1(); }
182 
183  //- Return SHA1::Digest for the data processed until now
184  SHA1Digest digest() { return stream_.digest(); }
185 
186  //- Clear the SHA1 calculation
187  void reset() { stream_.reset(); }
188 
189 
190  // Write Functions
191 
192  //- Add (unquoted) string contents.
193  // Ensures that SHA1 of C-string or C++-string content are identical.
194  virtual Ostream& write(const std::string& str) override
195  {
196  return writeQuoted(str, false); // Unquoted!
197  }
198 
199 
200  // Housekeeping
201 
202  //- Deprecated(2017-07) clear the SHA1 calculation
203  // \deprecated(2017-07) - use reset() method
204  void rewind()
205  {
206  stream_.sha1().clear();
207  }
208 
209 
210  // Additional constructors and methods (as per v2012 and earlier)
211  #ifdef Foam_IOstream_extras
212 
213  //- Construct with an empty digest, using given format
215  :
217  {}
218 
219  #endif /* Foam_IOstream_extras */
220 };
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
224 
225 } // End namespace Foam
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 
229 #endif
230 
231 // ************************************************************************* //
OSHA1streamAllocator()=default
Default construct.
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
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:226
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:56
A simple container for options an IOstream can normally have.
The output stream for calculating SHA1 digests.
Definition: OSHA1stream.H:176
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:249
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.
SHA1 & sha1() noexcept
Full access to the sha1.
Definition: OSHA1stream.H:216
const direction noexcept
Definition: Scalar.H:258
OSHA1stream(const OSHA1stream &)=delete
No copy construct.
An allocator for holding Foam::osha1stream.
Definition: OSHA1stream.H:146
virtual Ostream & write(const std::string &str) override
Add (unquoted) string contents.
Definition: OSHA1stream.H:236
Foam::osha1stream stream_
The output stream.
Definition: OSHA1stream.H:155
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:221
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