| 1 | // LAF Base Library |
| 2 | // Copyright (c) 2022 Igara Studio S.A. |
| 3 | // Copyright (c) 2015-2016 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef BASE_BASE64_H_INCLUDED |
| 9 | #define BASE_BASE64_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "base/buffer.h" |
| 13 | |
| 14 | #include <string> |
| 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | void encode_base64(const char* input, size_t n, std::string& output); |
| 19 | void decode_base64(const char* input, size_t n, buffer& output); |
| 20 | |
| 21 | inline void encode_base64(const buffer& input, std::string& output) { |
| 22 | if (!input.empty()) |
| 23 | encode_base64((const char*)&input[0], input.size(), output); |
| 24 | } |
| 25 | |
| 26 | inline std::string encode_base64(const buffer& input) { |
| 27 | std::string output; |
| 28 | if (!input.empty()) |
| 29 | encode_base64((const char*)&input[0], input.size(), output); |
| 30 | return output; |
| 31 | } |
| 32 | |
| 33 | inline std::string encode_base64(const std::string& input) { |
| 34 | std::string output; |
| 35 | if (!input.empty()) |
| 36 | encode_base64((const char*)input.c_str(), input.size(), output); |
| 37 | return output; |
| 38 | } |
| 39 | |
| 40 | inline void decode_base64(const std::string& input, buffer& output) { |
| 41 | if (!input.empty()) |
| 42 | decode_base64(input.c_str(), input.size(), output); |
| 43 | } |
| 44 | |
| 45 | inline buffer decode_base64(const std::string& input) { |
| 46 | buffer output; |
| 47 | if (!input.empty()) |
| 48 | decode_base64(input.c_str(), input.size(), output); |
| 49 | return output; |
| 50 | } |
| 51 | |
| 52 | inline std::string decode_base64s(const std::string& input) { |
| 53 | if (input.empty()) |
| 54 | return std::string(); |
| 55 | buffer tmp; |
| 56 | decode_base64(input.c_str(), input.size(), tmp); |
| 57 | return std::string((const char*)&tmp[0], tmp.size()); |
| 58 | } |
| 59 | |
| 60 | inline buffer decode_base64(const buffer& input) { |
| 61 | buffer output; |
| 62 | if (!input.empty()) |
| 63 | decode_base64((const char*)&input[0], input.size(), output); |
| 64 | return output; |
| 65 | } |
| 66 | |
| 67 | } // namespace base |
| 68 | |
| 69 | #endif |
| 70 | |