Path: blob/master/dep/rapidyaml/include/c4/base64.hpp
4261 views
#ifndef _C4_BASE64_HPP_1#define _C4_BASE64_HPP_23/** @file base64.hpp encoding/decoding for base64.4* @see https://en.wikipedia.org/wiki/Base645* @see https://www.base64encode.org/6* */78#include "c4/charconv.hpp"9#include "c4/blob.hpp"1011namespace c4 {121314/** check that the given buffer is a valid base64 encoding15* @see https://en.wikipedia.org/wiki/Base64 */16C4CORE_EXPORT bool base64_valid(csubstr encoded);171819/** base64-encode binary data.20* @param encoded [out] output buffer for encoded data21* @param data [in] the input buffer with the binary data22*23* @return the number of bytes needed to return the output (ie the24* required size for @p encoded). No writes occur beyond the end of25* the output buffer, so it is safe to do a speculative call where the26* encoded buffer is empty, or maybe too small. The caller should27* ensure that the returned size is smaller than the size of the28* encoded buffer.29*30* @note the result depends on endianness. If transfer between31* little/big endian systems is desired, the caller should normalize32* @p data before encoding.33*34* @see https://en.wikipedia.org/wiki/Base64 */35C4CORE_EXPORT size_t base64_encode(substr encoded, cblob data);363738/** decode the base64 encoding in the given buffer39* @param encoded [in] the encoded base6440* @param data [out] the output buffer41*42* @return the number of bytes needed to return the output (ie the43* required size for @p data). No writes occur beyond the end of the44* output buffer, so it is safe to do a speculative call where the45* data buffer is empty, or maybe too small. The caller should ensure46* that the returned size is smaller than the size of the data buffer.47*48* @note the result depends on endianness. If transfer between49* little/big endian systems is desired, the caller should normalize50* @p data after decoding.51*52* @see https://en.wikipedia.org/wiki/Base64 */53C4CORE_EXPORT size_t base64_decode(csubstr encoded, blob data);545556namespace fmt {5758template<typename CharOrConstChar>59struct base64_wrapper_60{61blob_<CharOrConstChar> data;62base64_wrapper_() : data() {}63base64_wrapper_(blob_<CharOrConstChar> blob) : data(blob) {}64};65/** a tag type to mark a payload as base64-encoded */66using const_base64_wrapper = base64_wrapper_<cbyte>;67/** a tag type to mark a payload to be encoded as base64 */68using base64_wrapper = base64_wrapper_<byte>;697071/** mark a variable to be written in base64 format */72template<class ...Args>73C4_ALWAYS_INLINE const_base64_wrapper cbase64(Args const& C4_RESTRICT ...args)74{75return const_base64_wrapper(cblob(args...));76}77/** mark a csubstr to be written in base64 format */78C4_ALWAYS_INLINE const_base64_wrapper cbase64(csubstr s)79{80return const_base64_wrapper(cblob(s.str, s.len));81}82/** mark a variable to be written in base64 format */83template<class ...Args>84C4_ALWAYS_INLINE const_base64_wrapper base64(Args const& C4_RESTRICT ...args)85{86return const_base64_wrapper(cblob(args...));87}88/** mark a csubstr to be written in base64 format */89C4_ALWAYS_INLINE const_base64_wrapper base64(csubstr s)90{91return const_base64_wrapper(cblob(s.str, s.len));92}9394/** mark a variable to be read in base64 format */95template<class ...Args>96C4_ALWAYS_INLINE base64_wrapper base64(Args &... args)97{98return base64_wrapper(blob(args...));99}100/** mark a variable to be read in base64 format */101C4_ALWAYS_INLINE base64_wrapper base64(substr s)102{103return base64_wrapper(blob(s.str, s.len));104}105106} // namespace fmt107108109/** write a variable in base64 format */110inline size_t to_chars(substr buf, fmt::const_base64_wrapper b)111{112return base64_encode(buf, b.data);113}114115/** read a variable in base64 format */116inline size_t from_chars(csubstr buf, fmt::base64_wrapper *b)117{118return base64_decode(buf, b->data);119}120121} // namespace c4122123#endif /* _C4_BASE64_HPP_ */124125126