www.digitalmars.com

D Programming Language 2.0

Last update Thu May 12 16:55:02 2011

std.base64

Encoding / Decoding Base64 format.

Implemented according to RFC 4648 - The Base16.

Example:
 ubyte[] data = [0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e];
 Base64.encode(data);        //-> "FPucA9l+"
 Base64.decode("FPucA9l+");  //-> [0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e]
Support Range interface using Encoder / Decoder.

Example:
 // Create MIME Base64 with CRLF, per line 76.
 foreach (encoded; Base64.encoder(f.byChunk(57))) {
     mime64.put(encoded);
     mime64.put("\r\n");
 }

License:
Boost License 1.0.

Authors:
Masahiro Nakagawa, Daniel Murphy (Single value Encoder and Decoder)

Source:
std/base64.d

alias Base64;
The Base64

alias Base64URL;
The "URL and Filename safe" Base64

template Base64Impl(char Map62th,char Map63th,char Padding = '=')
Core implementation for Base64 format.

Example:
 alias Base64Impl!('+', '/')                   Base64;    // The Base64 format(Already defined).
 alias Base64Impl!('!', '=', Base64.NoPadding) Base64Re;  // non-standard Base64 format for Regular expression

NOTE:
encoded-string doesn't have padding character if set Padding parameter to NoPadding.

size_t encodeLength(in size_t sourceLength);
Calculates the minimum length for encoding.

Parameters:
size_t sourceLength the length of source array.

Returns:
the calculated length using sourceLength.

template encode(R1,R2) if (isArray!(R1) && is(ElementType!(R1) : ubyte) && is(R2 == char[]))
template encode(R1,R2) if (!isArray!(R1) && isInputRange!(R1) && is(ElementType!(R1) : ubyte) && hasLength!(R1) && is(R2 == char[]))
Encodes source into buffer.

Parameters:
source an InputRange to encode.
range a buffer to store encoded result.

Returns:
the encoded string that slices buffer.

template encode(R1,R2) if (isArray!(R1) && is(ElementType!(R1) : ubyte) && !is(R2 == char[]))
template encode(R1,R2) if (!isArray!(R1) && isInputRange!(R1) && is(ElementType!(R1) : ubyte) && hasLength!(R1) && !is(R2 == char[]) && isOutputRange!(R2,char))
Encodes source into range.

Parameters:
source an InputRange to encode.
range an OutputRange to put encoded result.

Returns:
the number of calling put.

size_t encode(in R1 source, R2 range);
Encodes source into range.

Parameters:
R1 source an InputRange to encode.
R2 range an OutputRange to put encoded result.

Returns:
the number of calling put.

template encode(Range) if (isArray!(Range) && is(ElementType!(Range) : ubyte))
template encode(Range) if (!isArray!(Range) && isInputRange!(Range) && is(ElementType!(Range) : ubyte) && hasLength!(Range))
Encodes source to new buffer.

Shortcut to encode(source, buffer) function.

template Encoder(Range) if (isInputRange!(Range) && (is(ElementType!(Range) : const(ubyte)[]) || is(ElementType!(Range) : const(char)[])))
Range that encodes chunk data at a time.

template Encoder(Range) if (isInputRange!(Range) && is(ElementType!(Range) : ubyte))
Range that encodes single character at a time.

template encoder(Range) if (isInputRange!(Range))
Iterates through an InputRange at a time by using Encoder.

Default Encoder encodes chunk data.

Example:
 foreach (encoded; Base64.encoder(f.byLine())) {
     ... use encoded line ...
 }
In addition, You can use Encoder that returns encoded single character. This Encoder performs Range-based and lazy encoding.

Example:
 // The ElementType of data is not aggregation type
 foreach (encoded; Base64.encoder(data)) {
     ... use encoded character ...
 }

Parameters:
range an InputRange to iterate.

Returns:
a Encoder object instantiated and initialized according to the arguments.

Encoder!(Range) encoder(Range range);
Iterates through an InputRange at a time by using Encoder.

Default Encoder encodes chunk data.

Example:
 foreach (encoded; Base64.encoder(f.byLine())) {
     ... use encoded line ...
 }
In addition, You can use Encoder that returns encoded single character. This Encoder performs Range-based and lazy encoding.

Example:
 // The ElementType of data is not aggregation type
 foreach (encoded; Base64.encoder(data)) {
     ... use encoded character ...
 }

Parameters:
Range range an InputRange to iterate.

Returns:
a Encoder object instantiated and initialized according to the arguments.

size_t decodeLength(in size_t sourceLength);
Calculates the minimum length for decoding.

Parameters:
size_t sourceLength the length of source array.

Returns:
calculated length using sourceLength.

template decode(R1,R2) if (isArray!(R1) && is(ElementType!(R1) : dchar) && is(R2 == ubyte[]) && isOutputRange!(R2,ubyte))
template decode(R1,R2) if (!isArray!(R1) && isInputRange!(R1) && is(ElementType!(R1) : dchar) && hasLength!(R1) && is(R2 == ubyte[]) && isOutputRange!(R2,ubyte))
Decodes source into buffer.

Parameters:
source an InputRange to decode.
buffer a buffer to store decoded result.

Returns:
the decoded string that slices buffer.

Throws:
an Exception if source has character outside base-alphabet.

template decode(R1,R2) if (isArray!(R1) && is(ElementType!(R1) : dchar) && !is(R2 == ubyte[]) && isOutputRange!(R2,ubyte))
template decode(R1,R2) if (!isArray!(R1) && isInputRange!(R1) && is(ElementType!(R1) : dchar) && hasLength!(R1) && !is(R2 == ubyte[]) && isOutputRange!(R2,ubyte))
Decodes source into range.

Parameters:
source an InputRange to decode.
range an OutputRange to put decoded result

Returns:
the number of calling put.

Throws:
an Exception if source has character outside base-alphabet.

size_t decode(in R1 source, R2 range);
Decodes source into range.

Parameters:
R1 source an InputRange to decode.
R2 range an OutputRange to put decoded result

Returns:
the number of calling put.

Throws:
an Exception if source has character outside base-alphabet.

template decode(Range) if (isArray!(Range) && is(ElementType!(Range) : dchar))
template decode(Range) if (!isArray!(Range) && isInputRange!(Range) && is(ElementType!(Range) : dchar) && hasLength!(Range))
Decodes source into new buffer.

Shortcut to decode(source, buffer) function.

template Decoder(Range) if (isInputRange!(Range) && (is(ElementType!(Range) : const(char)[]) || is(ElementType!(Range) : const(ubyte)[])))
Range that decodes chunk data at a time.

template Decoder(Range) if (isInputRange!(Range) && is(ElementType!(Range) : char))
Range that decodes single character at a time.

template decoder(Range) if (isInputRange!(Range))
Iterates through an InputRange at a time by using Decoder.

Default Decoder decodes chunk data.

Example:
 foreach (decoded; Base64.decoder(f.byLine())) {
     ... use decoded line ...
 }
In addition, You can use Decoder that returns decoded single character. This Decoder performs Range-based and lazy decoding.

Example:
 auto encoded = Base64.encoder(cast(ubyte[])"0123456789");
 foreach (n; map!q{a - '0'}(Base64.decoder(encoded))) {
     ... do something with n ...
 }

NOTE:
If you use ByChunk, chunk-size should be the multiple of 4. Decoder can't judge a encode-boundary.

Parameters:
range an InputRange to iterate.

Returns:
a Decoder object instantiated and initialized according to the arguments.

Decoder!(Range) decoder(Range range);
Iterates through an InputRange at a time by using Decoder.

Default Decoder decodes chunk data.

Example:
 foreach (decoded; Base64.decoder(f.byLine())) {
     ... use decoded line ...
 }
In addition, You can use Decoder that returns decoded single character. This Decoder performs Range-based and lazy decoding.

Example:
 auto encoded = Base64.encoder(cast(ubyte[])"0123456789");
 foreach (n; map!q{a - '0'}(Base64.decoder(encoded))) {
     ... do something with n ...
 }

NOTE:
If you use ByChunk, chunk-size should be the multiple of 4. Decoder can't judge a encode-boundary.

Parameters:
Range range an InputRange to iterate.

Returns:
a Decoder object instantiated and initialized according to the arguments.