Base1 v0.1.0 Base1 View Source

This module encodes and decodes Base1.

Overview

According to the author of Base1:

  Base1 encodes arbitrary binary data as a string of repeated "A" characters.
  With analogy to the unary numeral system, the binary data is encoded in the length of the string.

Base1 is designed to be relatively inefficient, but accurate.

There are three main ways to encode and decode data as Base1 in this module:

Resources

Link to this section Summary

Types

A Base1 encoded binary

Functions

Decodes a Base1 string as binary

Decodes a Base1 string as binary

Decodes a Base1 string length

Decodes a Base1 string length

Decodes a Base1 string length encoded as a string

Decodes a Base1 string length encoded as a string

Encodes a binary as a Base1 string

Encodes a binary as a Base1 string

Encodes a binary as a Base1 length

Encodes a binary as a Base1 length string

Link to this section Types

Link to this type base1_binary() View Source
base1_binary() :: <<_::8>>

A Base1 encoded binary.

Link to this section Functions

Decodes a Base1 string as binary.

Raises an ArgumentError if the input string is not a Base1 binary.

Examples

iex> Base1.decode!("A")
<<0>>

iex> Base1.decode!("AA")
<<1>>

iex> Base1.decode!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
"b"
Link to this function decode(string) View Source
decode(base1_binary()) :: {:ok, binary()} | :error

Decodes a Base1 string as binary.

Returns :error if the input string is not a Base1 binary.

Examples

iex> Base1.decode("A")
{:ok, <<0>>}

iex> Base1.decode("AA")
{:ok, <<1>>}

iex> Base1.decode("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
{:ok, "b"}

iex> Base1.decode("AAAAAAHHH BLACK BEAST OF")
:error
Link to this function decode_length!(number) View Source
decode_length!(non_neg_integer()) :: binary()

Decodes a Base1 string length.

Raises an ArgumentError if the length is not a non-negative integer.

Examples

iex> Base1.decode_length!(1)
<<0>>

iex> Base1.decode_length!(2)
<<1>>

iex> Base1.decode_length!(381115146191751804757868696628784997)
"Hello Cleveland"

iex> Base1.decode_length!(9479543125109159158650175321189934146175361938985120077868441490465756706997907027887945065)
"It is probably better to encode length"
Link to this function decode_length(number) View Source
decode_length(non_neg_integer()) :: {:ok, binary()} | :error

Decodes a Base1 string length.

Returns :error if the length is not a non-negative integer.

Examples

iex> Base1.decode_length(1)
{:ok, <<0>>}

iex> Base1.decode_length(2)
{:ok, <<1>>}

iex> Base1.decode_length(381115146191751804757868696628784997)
{:ok, "Hello Cleveland"}

iex> Base1.decode_length(9479543125109159158650175321189934146175361938985120077868441490465756706997907027887945065)
{:ok, "It is probably better to encode length"}

iex> Base1.decode_length(-256)
:error
Link to this function decode_length_bin!(data) View Source
decode_length_bin!(binary()) :: binary()

Decodes a Base1 string length encoded as a string.

Raises an ArgumentError if the length is not a non-negative integer string.

Examples

iex> Base1.decode_length_bin!("1")
<<0>>

iex> Base1.decode_length_bin!("2")
<<1>>

iex> Base1.decode_length_bin!("381115146191751804757868696628784997")
"Hello Cleveland"

iex> Base1.decode_length_bin!("9479543125109159158650175321189934146175361938985120077868441490465756706997907027887945065")
"It is probably better to encode length"
Link to this function decode_length_bin(data) View Source
decode_length_bin(binary()) :: binary()

Decodes a Base1 string length encoded as a string.

:error is returned if the length is not a non-negative integer string.

Examples

iex> Base1.decode_length_bin("1")
{:ok, <<0>>}

iex> Base1.decode_length_bin("2")
{:ok, <<1>>}

iex> Base1.decode_length_bin("381115146191751804757868696628784997")
{:ok, "Hello Cleveland"}

iex> Base1.decode_length_bin("9479543125109159158650175321189934146175361938985120077868441490465756706997907027887945065")
{:ok, "It is probably better to encode length"}

iex> Base1.decode_length_bin("cheddar cheese please")
:error

Encodes a binary as a Base1 string.

Examples

iex> Base1.encode!(<<1>>)
"AA"

iex> Base1.encode!("b")
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

iex> Base1.encode!(<<>>)
<<>>
Link to this function encode(data) View Source
encode(binary()) :: {:ok, base1_binary()} | :error

Encodes a binary as a Base1 string.

Examples

iex> Base1.encode(<<1>>)
{:ok, "AA"}

iex> Base1.encode("b")
{:ok, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}

iex> Base1.encode(<<>>)
{:ok, <<>>}
Link to this function encode_length(data) View Source
encode_length(binary()) :: non_neg_integer()

Encodes a binary as a Base1 length.

Examples

Base1.encode_length(<<0>>
1

Base1.encode_length(<<1>>)
2

Base1.encode_length("hi")
26986

Base1.encode_length(<<0x03, 0xC0>>)
1217
Link to this function encode_length_bin(data) View Source
encode_length_bin(binary()) :: binary()

Encodes a binary as a Base1 length string.

Examples

Base1.encode_length_bin(<<0>>
"1"

Base1.encode_length_bin(<<1>>)
"2"

Base1.encode_length_bin("hi")
"26986"

Base1.encode_length_bin(<<0x03, 0xC0>>)
"1217"