def base64_map(val):
    """Map a value in the range 0 <= val < 64 to its base64 character."""
    assert val >= 0 and val < 64
    if val < 26:
        return chr(ord('A') + val)
    elif val < 52:
        return chr(ord('a') + val - 26)
    elif val < 62:
        return chr(ord('0') + val - 52)
    elif val == 62:
        return '+'
    elif val == 63:
        return '/'