Digest-BLAKE2

 view release on metacpan or  search on metacpan

stringencoders/modp_b64w.h  view on Meta::CPAN

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

#include "extern_c_end.h"

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

namespace modp {

    /** \brief b64w 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 b64w string.  Empty if failed.
     */
    inline std::string b64w_encode(const char* s, size_t len)
    {
        std::string x(modp_b64w_encode_len(len), '\0');
        size_t d = modp_b64w_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 b64w encode a cstr
     *
     * \param[in] s the input string to encode
     * \return a newly allocated b64w string.  Empty if failed.
     */
    inline std::string b64w_encode(const char* s)
    {
        return b64w_encode(s, static_cast<int>(strlen(s)));
    }

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

    /** \brief self-modifing b64w encode
     *
     * web-safe base 64 decode a string (self-modifing)
     * On failure, the string is empty.
     *
     * \param[in,out] s the string to be decoded
     * \return a reference to the input string
     */
    inline std::string& b64w_encode(std::string& s)
    {
        std::string x(b64w_encode(s.data(), s.size()));
        s.swap(x);
        return s;
    }

    inline std::string b64w_decode(const char* src, size_t len)
    {
        std::string x(modp_b64w_decode_len(len)+1, '\0');
        size_t d = modp_b64w_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 b64w_decode(const char* src)
    {
        return b64w_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& b64w_decode(std::string& s)
    {
        std::string x(b64w_decode(s.data(), s.size()));
        s.swap(x);
        return s;
    }

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

#endif /* __cplusplus */

#endif /* MODP_B64W */



( run in 0.896 second using v1.01-cache-2.11-cpan-39bf76dae61 )