// Copyright (c) 2010-2023, Lawrence Livermore National Security, LLC. Produced // at the Lawrence Livermore National Laboratory. All Rights reserved. See files // LICENSE and NOTICE for details. LLNL-CODE-806117. // // This file is part of the MFEM library. For more information and source code // availability visit https://mfem.org. // // MFEM is free software; you can redistribute it and/or modify it under the // terms of the BSD-3 license. We welcome feedback and contributions, see file // CONTRIBUTING.md for details. #include "binaryio.hpp" #include "error.hpp" namespace mfem { namespace bin_io { static const char b64str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; void WriteBase64(std::ostream &out, const void *bytes, size_t nbytes) { const unsigned char *in = static_cast(bytes); const unsigned char *end = in + nbytes; while (end - in >= 3) { out << b64str[in[0] >> 2]; out << b64str[((in[0] & 0x03) << 4) | (in[1] >> 4)]; out << b64str[((in[1] & 0x0f) << 2) | (in[2] >> 6)]; out << b64str[in[2] & 0x3f]; in += 3; } if (end - in > 0) // Padding { out << b64str[in[0] >> 2]; if (end - in == 1) { out << b64str[(in[0] & 0x03) << 4]; out << '='; } else // end - in == 2 { out << b64str[((in[0] & 0x03) << 4) | (in[1] >> 4)]; out << b64str[(in[1] & 0x0f) << 2]; } out << '='; } } static const unsigned char b64table[] = { 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,62, 255,62, 255,63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255,255,255,0, 255,255, 255,0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255,255,255,255,63, 255,26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 }; void DecodeBase64(const char *src, size_t len, std::vector &buf) { const unsigned char *in = (const unsigned char *)src; buf.clear(); size_t count = 0; for (size_t i=0; i