Commit 16e9e93e authored by Ansgar Burchardt's avatar Ansgar Burchardt

Merge branch 'bugfix/base64-encoder' into 'master'

fix base64 encoder

See merge request !295

(cherry picked from commit 689caf34)

6c9b134f fix base64 encoder
parent 0e809960
Pipeline #9796 passed with stage
in 6 minutes and 59 seconds
......@@ -27,53 +27,48 @@ namespace Dune {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
/** @brief struct with three bytes of text */
struct b64txt
/** @brief struct representing the three byte text as well as the four 6 bit chunks */
struct b64chunk
{
typedef unsigned char size_type;
using size_type = unsigned char;
size_type size;
char txt[3];
void reset()
{
size = 0;
txt[0] = txt[1] = txt[2] = 0;
}
int read(const char* t, size_type s)
{
size = s>=3 ? 3 : s;
txt[2] = s>0 ? t[0] : 0;
txt[0] = s>0 ? t[0] : 0;
txt[1] = s>1 ? t[1] : 0;
txt[0] = s>2 ? t[2] : 0;
txt[2] = s>2 ? t[2] : 0;
return size;
}
void put(const char c)
{
assert (size < 3);
txt[2-size++] = c;
txt[size++] = c;
}
};
/** struct with four six bit chunks */
struct b64data
{
typedef unsigned char size_type;
size_type size;
unsigned A : 6;
unsigned B : 6;
unsigned C : 6;
unsigned D : 6;
void write(char* t)
{
t[3] = size>2 ? base64table[A] : '=';
t[2] = size>1 ? base64table[B] : '=';
t[1] = size>0 ? base64table[C] : '=';
t[0] = size>0 ? base64table[D] : '=';
const unsigned A = (txt[0] & 0b1111'1100) >> 2;
const unsigned B = (txt[0] & 0b0000'0011) << 4 | (txt[1] & 0b1111'0000) >> 4;
const unsigned C = (txt[1] & 0b0000'1111) << 2 | (txt[2] & 0b1100'0000) >> 6;
const unsigned D = txt[2] & 0b0011'1111;
t[0] = size>0 ? base64table[A] : '=';
t[1] = size>0 ? base64table[B] : '=';
t[2] = size>1 ? base64table[C] : '=';
t[3] = size>2 ? base64table[D] : '=';
size = 0;
}
};
/** @brief union representing the three byte text as well as the four 6 bit chunks */
union b64chunk
{
b64txt txt;
b64data data;
};
/** @} */
} // namespace Dune
......
......@@ -26,7 +26,7 @@ namespace Dune {
: s(s_)
{
// reset chunk
chunk.txt.read(0,0);
chunk.reset();
}
//! encode a data item
......@@ -42,10 +42,10 @@ namespace Dune {
char* p = reinterpret_cast<char*>(&data);
for (size_t len = sizeof(X); len > 0; len--,p++)
{
chunk.txt.put(*p);
if (chunk.txt.size == 3)
chunk.put(*p);
if (chunk.size == 3)
{
chunk.data.write(obuf);
chunk.write(obuf);
s.write(obuf,4);
}
}
......@@ -61,9 +61,9 @@ namespace Dune {
*/
void flush()
{
if (chunk.txt.size > 0)
if (chunk.size > 0)
{
chunk.data.write(obuf);
chunk.write(obuf);
s.write(obuf,4);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment