simple_base64

RFC 4648 Compliant Base64 Encoding for Eiffel

RFC 4648 v1.0.0 MIT

Overview

simple_base64 is a lightweight, RFC 4648 compliant Base64 encoding and decoding library for Eiffel. It provides both standard Base64 and URL-safe Base64URL encoding variants, with full Design by Contract support.

Part of the simple_* ecosystem of focused, single-purpose Eiffel libraries.

Quick Start

Installation

Add to your ECF file:

<library name="simple_base64" location="$SIMPLE_BASE64/simple_base64.ecf"/>

Set environment variable:

set SIMPLE_BASE64=D:\path\to\simple_base64

Basic Usage

local
    encoder: SIMPLE_BASE64
    encoded, decoded: STRING
do
    create encoder.make

    -- Encode a string
    encoded := encoder.encode ("Hello, World!")
    -- Result: "SGVsbG8sIFdvcmxkIQ=="

    -- Decode back
    decoded := encoder.decode (encoded)
    -- Result: "Hello, World!"
end

URL-Safe Encoding

-- URL-safe variant (no padding, uses - and _ instead of + and /)
url_safe := encoder.encode_url ("data with special chars")

-- Decode URL-safe
decoded := encoder.decode_url (url_safe)

MIME Encoding (RFC 2045)

-- MIME encoding with line wrapping at 76 characters
mime_encoded := encoder.encode_mime (long_content)

-- Lenient decode handles whitespace and line breaks
decoded := encoder.decode_lenient (mime_encoded)

Data URIs (RFC 2397)

-- Create a data URI
uri := encoder.to_data_uri ("Hello!", "text/plain")
-- Result: "data:text/plain;base64,SGVsbG8h"

-- Extract data from a data URI
data := encoder.from_data_uri (uri)
-- Result: "Hello!"

-- Extract the mediatype
mediatype := encoder.data_uri_mediatype (uri)
-- Result: "text/plain"

Features

Standard Base64

RFC 4648 compliant encoding with proper padding using the standard alphabet (A-Z, a-z, 0-9, +, /).

URL-Safe Base64URL

URL and filename safe variant using - and _ characters. Perfect for JWTs, URLs, and filenames.

Validation

Built-in validation functions to check if strings are valid Base64 or Base64URL encoded.

Design by Contract

Full preconditions, postconditions, and loop variants for correctness verification.

Byte-Level Access

Direct byte array encoding/decoding for binary data handling.

Format Conversion

Convert between standard and URL-safe formats with to_url_safe and to_standard.

MIME Encoding

RFC 2045 compliant encoding with line wrapping at 76 characters. Lenient decoder handles whitespace.

Data URI Support

RFC 2397 data URIs for embedding data inline. Create, parse, and extract mediatypes.

API Summary

Feature Description
encode (a_input) Encode string to standard Base64
encode_url (a_input) Encode to URL-safe Base64URL (no padding)
encode_bytes (a_bytes) Encode byte array to Base64
decode (a_input) Decode Base64 string
decode_bytes (a_input) Decode to byte array
is_valid_base64 (a_string) Validate standard Base64
is_valid_base64_url (a_string) Validate URL-safe Base64URL
to_url_safe (a_base64) Convert standard to URL-safe format
to_standard (a_base64url) Convert URL-safe to standard format
encode_mime (a_input) Encode with MIME line wrapping (76 chars)
decode_lenient (a_input) Decode ignoring whitespace/line breaks
to_data_uri (a_data, a_mediatype) Create data URI from content
from_data_uri (a_uri) Extract and decode data from data URI
data_uri_mediatype (a_uri) Extract mediatype from data URI
is_data_uri (a_input) Check if string is a data URI

View complete API reference →

Testing

The library includes a comprehensive test suite with 44 tests covering:

-- Run tests
ec.exe -batch -config simple_base64.ecf -target simple_base64_tests -tests