Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ggml org Llama cpp Base64

From Leeroopedia
Knowledge Sources
Domains Encoding, Utilities
Last Updated 2026-02-15 00:00 GMT

Overview

Header-only Base64 encoding and decoding implementation supporting both standard and URL-safe alphabets.

Description

This file provides a public domain (Unlicense) Base64 utility class within the llama.cpp common library. The base64 class offers static template methods for encoding and decoding that work with arbitrary iterators. It supports two alphabets: standard (A-Z, a-z, 0-9, +, /) and url_filename_safe (replacing + and / with - and _). Decoding supports auto-detection of the alphabet and two behaviors: moderate (ignores remaining bits for unpadded input) and loose (stops at padding characters). The base64_error class extends std::runtime_error for encoding/decoding errors.

Usage

Used for encoding/decoding binary data (e.g., images in multimodal requests) to/from Base64 strings in the server's API handling and other components that need Base64 support.

Code Reference

Source Location

Signature

class base64_error : public std::runtime_error {
public:
    using std::runtime_error::runtime_error;
};

class base64 {
public:
    enum class alphabet { auto_, standard, url_filename_safe };
    enum class decoding_behavior { moderate, loose };

    template<typename Input_iterator, typename Output_iterator>
    static Output_iterator encode(Input_iterator in_begin, Input_iterator in_end,
                                  Output_iterator out,
                                  alphabet alpha = alphabet::standard);

    template<typename Input_iterator, typename Output_iterator>
    static Output_iterator decode(Input_iterator in_begin, Input_iterator in_end,
                                  Output_iterator out,
                                  alphabet alpha = alphabet::auto_,
                                  decoding_behavior behavior = decoding_behavior::moderate);

    static std::string encode(const std::string & input);
    static std::string decode(const std::string & input);
};

Import

#include "base64.hpp"

I/O Contract

Inputs

Name Type Required Description
in_begin Input_iterator Yes Iterator to the beginning of the input data
in_end Input_iterator Yes Iterator to the end of the input data
alpha alphabet No Which Base64 alphabet to use (default: standard for encode, auto_ for decode)
behavior decoding_behavior No How to handle unpadded input (default: moderate)

Outputs

Name Type Description
out Output_iterator Iterator past the last element written
return value std::string Encoded or decoded string (for the string overloads)

Usage Examples

#include "base64.hpp"

// Encode a string to Base64
std::string encoded = base64::encode("Hello, World!");
// Result: "SGVsbG8sIFdvcmxkIQ=="

// Decode a Base64 string
std::string decoded = base64::decode("SGVsbG8sIFdvcmxkIQ==");
// Result: "Hello, World!"

// Use URL-safe alphabet
std::string input = "data with +/= chars";
std::string url_safe;
base64::encode(input.begin(), input.end(), std::back_inserter(url_safe),
               base64::alphabet::url_filename_safe);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment