<commit_before>/*
 * OpenSSL BIO_f_base64() wrapper.
 *
 * author: <NAME> <<EMAIL>>
 */

#ifndef BENG_PROXY_SSL_BASE64_HXX
#define BENG_PROXY_SSL_BASE64_HXX

#include "MemBio.hxx"
#include "Unique.hxx"
#include "Error.hxx"

#include <string>

#include <string.h>

/**
 * Call a function that writes into a memory BIO and return the BIO
 * memory as Base64-encoded #AllocatedString instance.
 */
template<typename W>
static inline AllocatedString<>
BioWriterToBase64String(W &&writer)
{
    return BioWriterToString([&writer](BIO &bio){
            UniqueBIO b64(BIO_new(BIO_f_base64()));
            if (!b64)
                throw SslError("BIO_new() failed");

            BIO_push(b64.get(), &bio);
            BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);
            writer(*b64);
            BIO_flush(b64.get());
        });
}

static inline AllocatedString<>
Base64(ConstBuffer<void> data)
{
    return BioWriterToBase64String([data](BIO &bio){
            BIO_write(&bio, data.data, data.size);<commit_msg>ssl/Base64: explicitly ignore BIO_flush() result<commit_after>/*
 * OpenSSL BIO_f_base64() wrapper.
 *
 * author: <NAME> <<EMAIL>>
 */

#ifndef BENG_PROXY_SSL_BASE64_HXX
#define BENG_PROXY_SSL_BASE64_HXX

#include "MemBio.hxx"
#include "Unique.hxx"
#include "Error.hxx"

#include <string>

#include <string.h>

/**
 * Call a function that writes into a memory BIO and return the BIO
 * memory as Base64-encoded #AllocatedString instance.
 */
template<typename W>
static inline AllocatedString<>
BioWriterToBase64String(W &&writer)
{
    return BioWriterToString([&writer](BIO &bio){
            UniqueBIO b64(BIO_new(BIO_f_base64()));
            if (!b64)
                throw SslError("BIO_new() failed");

            BIO_push(b64.get(), &bio);
            BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);
            writer(*b64);
            (void)BIO_flush(b64.get());
        });
}

static inline AllocatedString<>
Base64(ConstBuffer<void> data)
{
    return BioWriterToBase64String([data](BIO &bio){
            BIO_write(&bio, data.data, data.size);
{'fraction_non_alphanumeric': 0.1337181337181337, 'fraction_numerical': 0.029822029822029823, 'mean_word_length': 3.463519313304721, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5587415', 'n_tokens_mistral': 777, 'n_tokens_neox': 711, 'n_words': 149}