Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library · std.bytes

    Bytes.fromBase64

    Decodes Base64 text into Bytes.

    fromBase64(s: string) -> Result<Bytes, string>
    Module
    std.bytes
    Receiver
    Not applicable
    Minimum arity
    1
    Maximum arity
    1

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    Import std.bytes in language mode 5.21.

    The current compiler selects std/bytes.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Source evidence

    Receiver

    Parameter constraints

    Bytes.toBase64 uses the RFC 4648 standard alphabet with plus and slash. Its output has no whitespace or line wrapping.

    A final group containing one byte ends with two equals signs. A final group containing two bytes ends with one equals sign.

    Bytes.fromBase64 accepts complete groups of four characters. Padding appears at the end of the final group and all unused bits are zero.

    Missing or misplaced padding, nonzero unused bits, whitespace, non-ASCII characters, minus signs, and underscores return Err with a string.

    Empty bytes encode as empty text and empty text decodes to Ok with empty Bytes. Invalid text never returns a partial byte sequence.

    The input is evaluated once and is unchanged. Encoding then decoding recovers the input bytes exactly without any external effects.

    Result

    The declared result type is Result<Bytes, string>.

    Effects

    Failures

    Result and effects

    Link

    Decode canonical padded Base64 using the standard alphabet. Invalid alphabet, padding, or unused bits return Err.

    Resolved contracts

    Link

    Related entries

    Link

    std.bytes

    Encode and validate padded Base64

    Link
    Ruletopaz.stdlib.contract.bytes-base64.rule

    Encode and validate padded Base64

    Bytes.toBase64 uses the RFC 4648 standard alphabet with plus and slash. Its output has no whitespace or line wrapping.

    A final group containing one byte ends with two equals signs. A final group containing two bytes ends with one equals sign.

    Bytes.fromBase64 accepts complete groups of four characters. Padding appears at the end of the final group and all unused bits are zero.

    Missing or misplaced padding, nonzero unused bits, whitespace, non-ASCII characters, minus signs, and underscores return Err with a string.

    Empty bytes encode as empty text and empty text decodes to Ok with empty Bytes. Invalid text never returns a partial byte sequence.

    The input is evaluated once and is unchanged. Encoding then decoding recovers the input bytes exactly without any external effects.

    Why

    A precise alphabet and padding rule let independent implementations exchange the same bytes. Rejecting nonzero unused bits prevents several texts from representing one accepted encoding.

    Source evidence

    Use standard Base64 vectors

    Link
    main.tpzUse standard Base64 vectorsOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. for text in (["", "f", "fo", "foo"]) {
    5. io.writeLine(text.encodeUtf8().toBase64())
    6. }
    7. for text in (["", "Zg==", "Zm8=", "Zm9v"]) {
    8. io.writeLine(test.assertOk(Bytes.fromBase64(text)).toHex())
    9. }
    Expected outputexit 0
    
    Zg==
    Zm8=
    Zm9v
    
    66
    666f
    666f6f
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. Zg==
    2. Zm8=
    3. Zm9v
    4. 66
    5. 666f
    6. 666f6f
    Source evidence

    Reject padding and alphabet errors

    Link
    main.tpzReject padding and alphabet errorsOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.io
    3. for text in (["Zg", "Zh==", "Zg== ", "_w==", "Zg==Zg==", "=g=="]) {
    4. match (Bytes.fromBase64(text)) {
    5. case Err(_) => io.writeLine("invalid Base64")
    6. case Ok(_) => io.writeLine("accepted")
    7. }
    8. }
    Expected outputexit 0
    invalid Base64
    invalid Base64
    invalid Base64
    invalid Base64
    invalid Base64
    invalid Base64
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. invalid Base64
    2. invalid Base64
    3. invalid Base64
    4. invalid Base64
    5. invalid Base64
    6. invalid Base64
    Source evidence

    Menu

    Download