base64Layer.C
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) 2016-2017 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "base64Layer.H"
29
30// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31
33//- The characters used for base-64
34static const unsigned char base64Chars[64] =
35{
36 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
37 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
38 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
39 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
40 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
41 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
42 'w', 'x', 'y', 'z', '0', '1', '2', '3',
43 '4', '5', '6', '7', '8', '9', '+', '/'
44};
46
47
48// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
49
50std::size_t Foam::base64Layer::encodedLength(std::size_t n)
51{
52 return 4 * ((n / 3) + (n % 3 ? 1 : 0));
53}
54
55
56// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
57
58inline unsigned char Foam::base64Layer::encode0() const noexcept
59{
60 // Top 6 bits of char0
61 return base64Chars[((group_[0] & 0xFC) >> 2)];
62}
63
64inline unsigned char Foam::base64Layer::encode1() const noexcept
65{
66 // Bottom 2 bits of char0, Top 4 bits of char1
67 return base64Chars[((group_[0] & 0x03) << 4) | ((group_[1] & 0xF0) >> 4)];
68}
69
70inline unsigned char Foam::base64Layer::encode2() const noexcept
71{
72 // Bottom 4 bits of char1, Top 2 bits of char2
73 return base64Chars[((group_[1] & 0x0F) << 2) | ((group_[2] & 0xC0) >> 6)];
74}
75
76inline unsigned char Foam::base64Layer::encode3() const noexcept
77{
78 // Bottom 6 bits of char2
79 return base64Chars[(group_[2] & 0x3F)];
80}
81
82
84{
85 group_[groupLen_++] = static_cast<unsigned char>(c);
86 if (groupLen_ == 3)
87 {
88 unsigned char out[4];
89
90 out[0] = encode0();
91 out[1] = encode1();
92 out[2] = encode2();
93 out[3] = encode3();
94 os_.write(reinterpret_cast<char*>(out), 4);
95
96 groupLen_ = 0;
97 }
98
99 dirty_ = true;
100}
101
102
103// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
104
106:
107 os_(os),
108 group_(),
109 groupLen_(0),
110 dirty_(false)
111{}
112
113
114// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
115
117{
118 close();
119}
120
121
122// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123
124void Foam::base64Layer::write(const char* s, std::streamsize n)
125{
126 for (std::streamsize i=0; i < n; ++i)
127 {
128 add(s[i]);
129 }
130}
131
132
134{
135 groupLen_ = 0;
136 dirty_ = false;
137}
138
139
141{
142 if (!dirty_)
143 {
144 return false;
145 }
146
147 unsigned char out[4];
148 if (groupLen_ == 1)
149 {
150 group_[1] = 0;
151
152 out[0] = encode0();
153 out[1] = encode1();
154 out[2] = '=';
155 out[3] = '=';
156 os_.write(reinterpret_cast<char*>(out), 4);
157 }
158 else if (groupLen_ == 2)
159 {
160 group_[2] = 0;
161
162 out[0] = encode0();
163 out[1] = encode1();
164 out[2] = encode2();
165 out[3] = '=';
166 os_.write(reinterpret_cast<char*>(out), 4);
167 }
168
169 // group-length == 0 (no content)
170 // group-length == 3 is not possible, already reset in add()
171
172 groupLen_ = 0;
173 dirty_ = false;
174
175 return true;
176}
177
178
179// ************************************************************************* //
label n
An output filter layer to write base-64 encoded content.
Definition: base64Layer.H:56
bool close()
End the encoding sequence, padding the final characters with '='.
Definition: base64Layer.C:140
~base64Layer()
Destructor. Performs close()
Definition: base64Layer.C:116
static std::size_t encodedLength(std::size_t n)
The encoded length has 4 bytes out for every 3 bytes in.
Definition: base64Layer.C:50
void reset()
Restart a new encoding sequence.
Definition: base64Layer.C:133
virtual bool write()
Write the output fields.
Sums a given list of (at least two or more) fields and outputs the result into a new field,...
Definition: add.H:161
OBJstream os(runTime.globalPath()/outputName)
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))
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
const direction noexcept
Definition: Scalar.H:223