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-2022 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
58  :
59  public std::streambuf
60  {
61  //- This does all the work and has its own buffering
62  SHA1 sha1_;
63 
64  protected:
65 
66  //- Handle overflow
67  virtual int overflow(int c = EOF)
68  {
69  if (c != EOF) sha1_.append(c);
70  return c;
71  }
72 
73  //- Put sequence of characters
74  virtual std::streamsize xsputn(const char* s, std::streamsize n)
75  {
76  if (n) sha1_.append(s, n);
77  return n;
78  }
79 
80  public:
81 
82  //- Default construct
83  sha1buf() = default;
84 
85  //- Full access to the sha1
86  SHA1& sha1() noexcept
87  {
88  return sha1_;
89  }
90  };
91 
92 
93  // Private Data
94 
95  //- Reference to the underlying buffer
96  sha1buf buf_;
97 
98 public:
99 
100  // Constructors
101 
102  //- Default construct
103  osha1stream()
104  :
105  std::ostream(&buf_)
106  {}
107 
108 
109  // Member Functions
110 
111  //- This hides both signatures of std::basic_ios::rdbuf()
112  sha1buf* rdbuf()
113  {
114  return &buf_;
115  }
116 
117  //- Full access to the sha1
118  SHA1& sha1() noexcept
119  {
120  return buf_.sha1();
121  }
122 };
123 
124 
125 namespace Detail
126 {
127 
128 /*---------------------------------------------------------------------------*\
129  Class Detail::OSHA1streamAllocator Declaration
130 \*---------------------------------------------------------------------------*/
131 
132 //- Allocator for an osha1stream
133 class OSHA1streamAllocator
134 {
135 protected:
136 
137  // Protected Data
138 
139  typedef osha1stream stream_type;
140 
141  //- The output stream
143 
144 
145  // Constructors
146 
147  //- Default construct
148  OSHA1streamAllocator() = default;
149 
151 public:
152 
153  // Member Functions
154 
155  //- Full access to the sha1
157  {
158  return stream_.sha1();
159  }
160 
162  //- Return SHA1::Digest for the data processed until now
164  {
165  return stream_.sha1().digest();
166  }
167 
168 
169  //- Clear the SHA1 calculation
170  void reset()
171  {
172  return stream_.sha1().clear();
173  }
174 };
175 
176 } // End namespace Detail
177 
178 
179 /*---------------------------------------------------------------------------*\
180  Class OSHA1stream Declaration
181 \*---------------------------------------------------------------------------*/
182 
183 //- The output stream for calculating SHA1 digests
184 class OSHA1stream
185 :
187  public OSstream
188 {
189  typedef Detail::OSHA1streamAllocator allocator_type;
190 
191  // Private Member Functions
192 
193  //- No copy construct
194  OSHA1stream(const OSHA1stream&) = delete;
195 
196  //- No copy assignment
197  void operator=(const OSHA1stream&) = delete;
198 
199 public:
200 
201  // Constructors
202 
203  //- Construct with an empty digest
204  explicit OSHA1stream
205  (
206  IOstreamOption streamOpt = IOstreamOption()
207  )
208  :
209  allocator_type(),
210  OSstream(stream_, "sha1", streamOpt.format(), streamOpt.version())
211  {}
212 
214  // Write Functions
215 
216  //- Add (unquoted) string contents.
217  // Ensures that SHA1 of C-string or C++-string content are identical.
218  virtual Ostream& write(const string& str)
219  {
220  return writeQuoted(str, false); // Unquoted!
221  }
222 
223 
224  // Housekeeping
225 
226  //- Deprecated(2017-07) clear the SHA1 calculation
227  // \deprecated(2017-07) - use reset() method
228  void rewind()
229  {
230  sha1().clear();
231  }
232 
233 
234  // Additional constructors and methods (as per v2012 and earlier)
235  #ifdef Foam_IOstream_extras
236 
237  //- Construct with an empty digest, using given format
238  explicit OSHA1stream(IOstreamOption::streamFormat fmt)
239  :
241  {}
242 
243  #endif /* Foam_IOstream_extras */
244 };
245 
246 
247 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 
249 } // End namespace Foam
250 
251 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252 
253 #endif
254 
255 // ************************************************************************* //
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:133
void clear() noexcept
Reset the hashed data before appending more.
Definition: SHA1.C:314
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:213
void rewind()
Deprecated(2017-07) clear the SHA1 calculation.
Definition: OSHA1stream.H:269
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
void reset()
Clear the SHA1 calculation.
Definition: OSHA1stream.H:197
virtual Ostream & write(const string &str)
Add (unquoted) string contents.
Definition: OSHA1stream.H:256
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:112
SHA1Digest digest()
Return SHA1::Digest for the data processed until now.
Definition: OSHA1stream.H:188
Allocator for an osha1stream.
Definition: OSHA1stream.H:150
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.
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
osha1stream()
Default construct.
Definition: OSHA1stream.H:114
stream_type stream_
The output stream.
Definition: OSHA1stream.H:161
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))
streamFormat format() const noexcept
Get the current stream format.
SHA1Digest digest() const
Calculate digest from current data.
Definition: SHA1I.H:92
Namespace for OpenFOAM.
SHA1 & sha1() noexcept
Full access to the sha1.
Definition: OSHA1stream.H:179
sha1buf * rdbuf()
This hides both signatures of std::basic_ios::rdbuf()
Definition: OSHA1stream.H:125