Dune Core Modules (2.9.0)

b64enc.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5
6#ifndef DUNE_GRID_IO_FILE_VTK_B64ENC_HH
7#define DUNE_GRID_IO_FILE_VTK_B64ENC_HH
8
9#include <assert.h>
10
11namespace Dune {
12
23 const char base64table[] =
24 {
25 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
26 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
27 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
28 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
29 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
30 };
31
33 struct b64chunk
34 {
35 using size_type = unsigned char;
36 size_type size;
37 char txt[3];
38
39 void reset()
40 {
41 size = 0;
42 txt[0] = txt[1] = txt[2] = 0;
43 }
44
45 int read(const char* t, size_type s)
46 {
47 size = s>=3 ? 3 : s;
48 txt[0] = s>0 ? t[0] : 0;
49 txt[1] = s>1 ? t[1] : 0;
50 txt[2] = s>2 ? t[2] : 0;
51 return size;
52 }
53
54 void put(const char c)
55 {
56 assert (size < 3);
57 txt[size++] = c;
58 }
59
60 void write(char* t)
61 {
62 const unsigned A = (txt[0] & 0b1111'1100) >> 2;
63 const unsigned B = (txt[0] & 0b0000'0011) << 4 | (txt[1] & 0b1111'0000) >> 4;
64 const unsigned C = (txt[1] & 0b0000'1111) << 2 | (txt[2] & 0b1100'0000) >> 6;
65 const unsigned D = txt[2] & 0b0011'1111;
66 t[0] = size>0 ? base64table[A] : '=';
67 t[1] = size>0 ? base64table[B] : '=';
68 t[2] = size>1 ? base64table[C] : '=';
69 t[3] = size>2 ? base64table[D] : '=';
70 size = 0;
71 }
72 };
73
76} // namespace Dune
77
78#endif // DUNE_GRID_IO_FILE_VTK_B64ENC_HH
Dune namespace.
Definition: alignedallocator.hh:13
const char base64table[]
endoing table
Definition: b64enc.hh:23
struct representing the three byte text as well as the four 6 bit chunks
Definition: b64enc.hh:34
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)