Digest-BLAKE2

 view release on metacpan or  search on metacpan

stringencoders/modp_b64.h  view on Meta::CPAN

 *      // bad characters
 *      return false;
 *    }
 * }
 * // foo is filled out now
 * \endcode
 */
#define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)

#include "extern_c_end.h"

#ifdef __cplusplus
#include <cstring>
#include <string>

namespace modp {
    /** \brief b64 encode a cstr with len
     *
     * \param[in] s the input string to encode
     * \param[in] len the length of the input string
     * \return a newly allocated b64 string.  Empty if failed.
     */
    inline std::string b64_encode(const char* s, size_t len)
    {
        std::string x(modp_b64_encode_len(len), '\0');
        size_t d = modp_b64_encode(const_cast<char*>(x.data()), s,
                                   static_cast<int>(len));
        if (d == (size_t)-1) {
            x.clear();
        } else {
            x.erase(d, std::string::npos);
        }
        return x;
    }

    /** \brief b64 encode a cstr
     *
     * \param[in] s the input string to encode
     * \return a newly allocated b64 string.  Empty if failed.
     */
    inline std::string b64_encode(const char* s)
    {
        return b64_encode(s, static_cast<int>(strlen(s)));
    }

    /** \brief b64 encode a const std::string
     *
     * \param[in] s the input string to encode
     * \return a newly allocated b64 string.  Empty if failed.
     */
    inline std::string b64_encode(const std::string& s)
    {
        return b64_encode(s.data(), s.size());
    }

    /**
     * base 64 encode a string (self-modifing)
     *
     * This function is for C++ only (duh)
     *
     * \param[in,out] s the string to be decoded
     * \return a reference to the input string
     */
    inline std::string& b64_encode(std::string& s)
    {
        std::string x(b64_encode(s.data(), s.size()));
        s.swap(x);
        return s;
    }

    inline std::string b64_decode(const char* src, size_t len)
    {
        std::string x(modp_b64_decode_len(len)+1, '\0');
        size_t d = modp_b64_decode(const_cast<char*>(x.data()), src,
                                static_cast<int>(len));
        if (d == (size_t)-1) {
            x.clear();
        } else {
            x.erase(d, std::string::npos);
        }
        return x;
    }

    inline std::string b64_decode(const char* src)
    {
        return b64_decode(src, strlen(src));
    }

    /**
     * base 64 decode a string (self-modifing)
     * On failure, the string is empty.
     *
     * This function is for C++ only (duh)
     *
     * \param[in,out] s the string to be decoded
     * \return a reference to the input string
     */
    inline std::string& b64_decode(std::string& s)
    {
        std::string x(b64_decode(s.data(), s.size()));
        s.swap(x);
        return s;
    }

    inline std::string b64_decode(const std::string& s)
    {
        return b64_decode(s.data(), s.size());
    }

}

#endif /* __cplusplus */

#endif /* MODP_B64 */



( run in 0.928 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )