MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
binaryio.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#include "binaryio.hpp"
13#include "error.hpp"
14
15namespace mfem
16{
17namespace bin_io
18{
19
20static const char b64str[]
21 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
22 "abcdefghijklmnopqrstuvwxyz"
23 "0123456789+/";
24
25void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes)
26{
27 const unsigned char *in = static_cast<const unsigned char *>(bytes);
28 const unsigned char *end = in + nbytes;
29 while (end - in >= 3)
30 {
32 out << b64str[((in[0] & 0x03) << 4) | (in[1] >> 4)];
33 out << b64str[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
34 out << b64str[in[2] & 0x3f];
35 in += 3;
36 }
37 if (end - in > 0) // Padding
38 {
40 if (end - in == 1)
41 {
42 out << b64str[(in[0] & 0x03) << 4];
43 out << '=';
44 }
45 else // end - in == 2
46 {
47 out << b64str[((in[0] & 0x03) << 4) | (in[1] >> 4)];
48 out << b64str[(in[1] & 0x0f) << 2];
49 }
50 out << '=';
51 }
52}
53
54static const unsigned char b64table[] =
55{
56 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
57 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
58 255,255,255,255,255,255,255,255,255,255,255,62, 255,62, 255,63,
59 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255,255,255,0, 255,255,
60 255,0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
61 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255,255,255,255,63,
62 255,26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
63 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255,255,255,255,255,
64 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
65 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
66 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
67 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
68 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
69 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
70 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
71 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
72};
73
74void DecodeBase64(const char *src, size_t len, std::vector<char> &buf)
75{
76 const unsigned char *in = (const unsigned char *)src;
77 buf.clear();
78 size_t count = 0;
79 for (size_t i=0; i<len; ++i) { if (b64table[in[i]] != 255) { ++count; } }
80 if (count % 4 != 0) { return; }
81 buf.resize(3*len/4);
82 unsigned char *out = (unsigned char *)buf.data();
83 count = 0;
84 int pad = 0;
85 unsigned char c[4];
86 for (size_t i=0; i<len; ++i)
87 {
88 unsigned char t = b64table[in[i]];
89 if (t == 255) { continue; }
90 if (in[i] == '=') { ++pad; }
91 c[count++] = t;
92 if (count == 4)
93 {
94 *out++ = (c[0] << 2) | (c[1] >> 4);
95 if (pad <= 1) { *out++ = (c[1] << 4) | (c[2] >> 2); }
96 if (pad == 0) { *out++ = (c[2] << 6) | c[3]; }
97 count = pad = 0;
98 }
99 }
100 buf.resize(out - (unsigned char *)buf.data());
101}
102
103size_t NumBase64Chars(size_t nbytes) { return ((4*nbytes/3) + 3) & ~3; }
104
105} // namespace mfem::bin_io
106} // namespace mfem
void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes)
Given a buffer bytes of length nbytes, encode the data in base-64 format, and write the encoded data ...
Definition binaryio.cpp:25
void DecodeBase64(const char *src, size_t len, std::vector< char > &buf)
Decode len base-64 encoded characters in the buffer src, and store the resulting decoded data in buf....
Definition binaryio.cpp:74
size_t NumBase64Chars(size_t nbytes)
Return the number of characters needed to encode nbytes in base-64.
Definition binaryio.cpp:103
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition globals.hpp:66
RefCoord t[3]