Example
ubyte[] data = [0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e];const(char)[] encoded = Base64.encode(data);assert(encoded =="FPucA9l+");ubyte[] decoded = Base64.decode("FPucA9l+");assert(decoded == [0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e]);The range API is supported for both encoding and decoding:
Example
// Create MIME Base64 with CRLF, per line 76.File f = File("./text.txt","r");scope(exit) f.close();Appender!string mime64 = appender!string;foreach (encoded; Base64.encoder(f.byChunk(57))){ mime64.put(encoded); mime64.put("\r\n");}writeln(mime64.data);
Sourcestd/base64.d
Base64 = Base64Impl!('+', '/', '=');ubyte[] data = [0x83, 0xd7, 0x30, 0x7a, 0x01, 0x3f];writeln(Base64.encode(data));// "g9cwegE/"writeln(Base64.decode("g9cwegE/"));// data
Base64URL = Base64Impl!('-', '_', '=');ubyte[] data = [0x83, 0xd7, 0x30, 0x7a, 0x01, 0x3f];writeln(Base64URL.encode(data));// "g9cwegE_"writeln(Base64URL.decode("g9cwegE_"));// data
Base64URLNoPadding = Base64Impl!('-', '_', '\0');ubyte[] data = [0x83, 0xd7, 0x30, 0x7b, 0xef];writeln(Base64URLNoPadding.encode(data));// "g9cwe-8"writeln(Base64URLNoPadding.decode("g9cwe-8"));// data
Base64Impl(char Map62th, char Map63th, char Padding = '=')// Non-standard Base64 format for embedding in regular expressions.alias Base64Re =Base64Impl!('!', '=', Base64.NoPadding);
NOTEEncoded strings will not have any padding if thePadding parameter is set toNoPadding.
import std.string : representation;// pre-defined: alias Base64 = Base64Impl!('+', '/');ubyte[] emptyArr;writeln(Base64.encode(emptyArr));// ""writeln(Base64.encode("f".representation));// "Zg=="writeln(Base64.encode("foo".representation));// "Zm9v"alias Base64Re =Base64Impl!('!', '=', Base64.NoPadding);writeln(Base64Re.encode("f".representation));// "Zg"writeln(Base64Re.encode("foo".representation));// "Zm9v"
NoPadding;encodeLength(in size_tsourceLength);size_tsourceLength | Length of the source array. |
ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e];// Allocate a buffer large enough to hold the encoded string.auto buf =newchar[Base64.encodeLength(data.length)];Base64.encode(data, buf);writeln(buf);// "Gis8TV1u"
encode(R1, R2)(scope const R1source, return scope R2buffer)encode(R1, R2)(R1source, R2buffer)R1source | Theinput range to encode. |
R2buffer | Thechar[] buffer to store the encoded result. |
ubyte[6] data = [0x83, 0xd7, 0x30, 0x7a, 0x01, 0x3f];char[32]buffer;// much bigger than necessary// Just to be sure...auto encodedLength = Base64.encodeLength(data.length);assert(buffer.length >= encodedLength);// encode() returns a slice to the provided buffer.auto encoded = Base64.encode(data[],buffer[]);assert(encodedisbuffer[0 .. encodedLength]);writeln(encoded);// "g9cwegE/"
encode(E, R)(scope const(E)[]source, auto ref Rrange)encode(R1, R2)(R1source, auto ref R2range)const(E)[]source | Theinput range to encode. |
Rrange | Theoutput range to store the encoded result. |
import std.array : appender;auto output = appender!string();ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e];// This overload of encode() returns the number of calls to the output// range's put method.writeln(Base64.encode(data, output));// 8writeln(output.data);// "Gis8TV1u"
encode(Range)(Rangesource)encode(Range)(Rangesource)Rangesource | Theinput range to encode. |
ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e];writeln(Base64.encode(data));// "Gis8TV1u"
Encoder(Range) if (isInputRange!Range && (is(ElementType!Range : const(ubyte)[]) || is(ElementType!Range : const(char)[])));NoteThis struct is not intended to be created in user code directly; use theencoder function instead.
empty();front();popFront();save();Encoder(Range) if (isInputRange!Range && is(ElementType!Range : ubyte));NoteThis struct is not intended to be created in user code directly; use theencoder function instead.
empty() const;front();popFront();save();encoder(Range)(Rangerange)Rangerange | Aninput range over the data to be encoded. |
range is at least a forward range, otherwise it will be only an input range.Example This example encodes the input one line at a time.
File f = File("text.txt","r");scope(exit) f.close();uint line = 0;foreach (encoded; Base64.encoder(f.byLine())){ writeln(++line,". ", encoded);}
Example This example encodes the input data one byte at a time.
ubyte[] data =cast(ubyte[])"0123456789";// The ElementType of data is not aggregation typeforeach (encoded; Base64.encoder(data)){ writeln(encoded);}
decodeLength(in size_tsourceLength);size_tsourceLength | The length of the Base64 encoding. |
auto encoded ="Gis8TV1u";// Allocate a sufficiently large buffer to hold to decoded result.auto buffer =newubyte[Base64.decodeLength(encoded.length)];Base64.decode(encoded, buffer);writeln(buffer);// [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]
decode(R1, R2)(in R1source, return scope R2buffer)decode(R1, R2)(R1source, R2buffer)R1source | Theinput range to decode. |
R2buffer | The buffer to store decoded result. |
auto encoded ="Gis8TV1u";ubyte[32]buffer;// much bigger than necessary// Just to be sure...auto decodedLength = Base64.decodeLength(encoded.length);assert(buffer.length >= decodedLength);// decode() returns a slice of the given buffer.auto decoded = Base64.decode(encoded,buffer[]);assert(decodedisbuffer[0 .. decodedLength]);writeln(decoded);// [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]
decode(R1, R2)(in R1source, auto ref R2range)decode(R1, R2)(R1source, auto ref R2range)R1source | Theinput range to decode. |
R2range | Theoutput range to store the decoded result. |
struct OutputRange{ubyte[] result;void put(ubyte b) { result ~= b; }}OutputRange output;// This overload of decode() returns the number of calls to put().writeln(Base64.decode("Gis8TV1u", output));// 6writeln(output.result);// [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]
decode(Range)(Rangesource)decode(Range)(Rangesource)Rangesource | Theinput range to decode. |
auto data ="Gis8TV1u";writeln(Base64.decode(data));// [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]
Decoder(Range) if (isInputRange!Range && (is(ElementType!Range : const(char)[]) || is(ElementType!Range : const(ubyte)[])));NoteThis struct is not intended to be created in user code directly; use thedecoder function instead.
empty();front();popFront();save();Decoder(Range) if (isInputRange!Range && is(ElementType!Range : char));NoteThis struct is not intended to be created in user code directly; use thedecoder function instead.
empty() const;front();popFront();save();decoder(Range)(Rangerange)decoder()(const(char)[]range);Rangerange | Aninput range over the data to be decoded, or achar array. Will not acceptwchar[] nordchar[]. |
range is at least a forward range, otherwise it will be only an input range. If the input data contains characters not found in the base alphabet of the current Base64 encoding scheme, the returned range may throw aBase64Exception.Example This example shows decoding over a range of input data lines.
foreach (decoded; Base64.decoder(stdin.byLine())){ writeln(decoded);}This example shows decoding one byte at a time.
auto encoded = Base64.encoder(cast(ubyte[])"0123456789");foreach (n; map!q{a - '0'}(Base64.decoder(encoded))){ writeln(n);}
import std.algorithm.comparison : equal;string encoded ="VGhvdSBzaGFsdCBuZXZlciBjb250aW51ZSBhZnRlciBhc3NlcnRpbmcgbnVsbA==";assert(Base64.decoder(encoded) .equal("Thou shalt never continue after asserting null"));
Base64Exception:object.Exception;import std.exception : assertThrown;assertThrown!Base64Exception(Base64.decode("ab|c"));