MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
binaryio.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, 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 
15 namespace mfem
16 {
17 namespace bin_io
18 {
19 
20 static const char *b64str
21  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
22  "abcdefghijklmnopqrstuvwxyz"
23  "0123456789+/";
24 
25 void 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  {
31  out << b64str[in[0] >> 2];
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  {
39  out << b64str[in[0] >> 2];
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 
54 } // namespace mfem::bin_io
55 } // namespace mfem
void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes)
Definition: binaryio.cpp:25
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