SIMPLE_BASE64

API Reference

Class Overview

SIMPLE_BASE64 provides RFC 4648 compliant Base64 encoding and decoding for Eiffel applications. It supports both standard Base64 and URL-safe Base64URL variants.

Creation

create encoder.make

Initialize the encoder. No parameters required.

Encoding Features

encode (a_input: STRING): STRING

encoded := encoder.encode ("Hello, World!")

Encode a string to standard Base64 with padding.

Contracts

require: input_not_void: a_input /= Void
ensure: result_not_void: Result /= Void
ensure: valid_base64: is_valid_base64 (Result)

encode_bytes (a_bytes: ARRAY [NATURAL_8]): STRING

encoded := encoder.encode_bytes (byte_array)

Encode a byte array to standard Base64. Useful for binary data.

Contracts

require: bytes_not_void: a_bytes /= Void
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = ((a_bytes.count + 2) // 3 * 4)

encode_url (a_input: STRING): STRING

url_safe := encoder.encode_url ("data+with/special")

Encode to URL-safe Base64URL without padding. Uses - and _ instead of + and /.

Contracts

require: input_not_void: a_input /= Void
ensure: result_not_void: Result /= Void
ensure: no_padding: not Result.has ('=')
ensure: url_safe: not Result.has ('+') and not Result.has ('/')

encode_url_with_padding (a_input: STRING): STRING

url_safe := encoder.encode_url_with_padding ("data")

Encode to URL-safe Base64URL with padding preserved.

Contracts

require: input_not_void: a_input /= Void
ensure: result_not_void: Result /= Void
ensure: url_safe: not Result.has ('+') and not Result.has ('/')

Decoding Features

decode (a_input: STRING): STRING

decoded := encoder.decode ("SGVsbG8sIFdvcmxkIQ==")

Decode a Base64 or Base64URL string back to the original string.

Contracts

require: input_not_void: a_input /= Void
require: valid_input: is_valid_base64 (a_input) or is_valid_base64_url (a_input)
ensure: result_not_void: Result /= Void

decode_bytes (a_input: STRING): ARRAY [NATURAL_8]

bytes := encoder.decode_bytes (encoded_string)

Decode Base64 to a byte array. Automatically handles both standard and URL-safe formats.

Contracts

require: input_not_void: a_input /= Void
require: valid_or_normalizable: is_valid_base64 (a_input) or is_valid_base64_url (a_input) or a_input.is_empty
ensure: result_not_void: Result /= Void

decode_url (a_input: STRING): STRING

decoded := encoder.decode_url (url_safe_string)

Decode a URL-safe Base64URL string. Alias for decode with clearer intent.

Contracts

require: input_not_void: a_input /= Void
ensure: result_not_void: Result /= Void

Validation Features

is_valid_base64 (a_string: STRING): BOOLEAN

if encoder.is_valid_base64 (input) then ...

Check if a string is valid standard Base64 (with proper length and characters).

Format: Length must be a multiple of 4. Characters must be A-Z, a-z, 0-9, +, /, or = (padding).

Contracts

require: string_not_void: a_string /= Void

is_valid_base64_url (a_string: STRING): BOOLEAN

if encoder.is_valid_base64_url (input) then ...

Check if a string is valid URL-safe Base64URL.

Characters must be A-Z, a-z, 0-9, -, _, or = (padding). Length does not need to be a multiple of 4.

Contracts

require: string_not_void: a_string /= Void

Conversion Features

to_url_safe (a_base64: STRING): STRING

url_safe := encoder.to_url_safe (standard_base64)

Convert standard Base64 to URL-safe format. Replaces + with - and / with _.

Contracts

require: input_not_void: a_base64 /= Void
ensure: result_not_void: Result /= Void
ensure: no_plus: not Result.has ('+')
ensure: no_slash: not Result.has ('/')

to_standard (a_base64url: STRING): STRING

standard := encoder.to_standard (url_safe_base64)

Convert URL-safe Base64URL to standard format. Replaces - with + and _ with /.

Contracts

require: input_not_void: a_base64url /= Void
ensure: result_not_void: Result /= Void
ensure: no_dash: not Result.has ('-')
ensure: no_underscore: not Result.has ('_')

Constants

Constant Value Description
Standard_alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ Standard Base64 alphabet (RFC 4648)
Url_safe_alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_ URL-safe Base64URL alphabet (RFC 4648)
Padding_char = Padding character

Class Invariants

invariant: standard_alphabet_64: Standard_alphabet.count = 64
invariant: url_safe_alphabet_64: Url_safe_alphabet.count = 64