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