Implementation:Ollama Ollama Llama Base64
| Knowledge Sources | |
|---|---|
| Domains | Encoding, Utilities |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Header-only Base64 encoding and decoding utility library used for binary data conversion within the llama.cpp common library.
Description
Provides a base64 class with static template methods for encoding binary data to Base64 strings and decoding Base64 strings back to binary. Supports multiple alphabet variants (standard and URL-safe) via an alphabet enum, and configurable decoding behavior (moderate vs. loose). Throws base64_error (derived from std::runtime_error) on invalid input during decoding. The library is public domain (Unlicense).
Usage
Use this when encoding or decoding binary data (such as image data for multimodal models) to and from Base64 format within the llama.cpp ecosystem.
Code Reference
Source Location
- Repository: Ollama
- File: llama/llama.cpp/common/base64.hpp
- Lines: 1-392
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 alphabet = alphabet::standard);
static std::string encode(const std::string& str, alphabet alphabet = alphabet::standard);
static std::string encode(const char* buffer, std::size_t size, alphabet alphabet = 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 alphabet = alphabet::auto_,
decoding_behavior behavior = decoding_behavior::moderate);
static std::string decode(const std::string& str,
alphabet alphabet = alphabet::auto_,
decoding_behavior behavior = decoding_behavior::moderate);
static std::size_t required_encode_size(std::size_t size);
static std::size_t required_decode_size(std::size_t size);
};
Import
#include "base64.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| in_begin | Input_iterator | Yes | Iterator to the beginning of source data |
| in_end | Input_iterator | Yes | Iterator to the end of source data |
| alphabet | base64::alphabet | No | Which Base64 alphabet to use (default: standard for encode, auto for decode) |
| behavior | base64::decoding_behavior | No | How to handle padding/errors during decoding |
Outputs
| Name | Type | Description |
|---|---|---|
| result | std::string | Encoded or decoded string result |
| out | Output_iterator | Iterator past the last written element (template version) |
Usage Examples
#include "base64.hpp"
// Encode a string to Base64
std::string encoded = base64::encode("Hello, World!");
// Decode a Base64 string
std::string decoded = base64::decode(encoded);
// URL-safe encoding
std::string url_safe = base64::encode("binary\xFFdata", base64::alphabet::url_filename_safe);
// Check required buffer size
std::size_t needed = base64::required_encode_size(input.size());