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:
- Using
encode/1
anddecode/1
ordecode!/1
to produce a sequence of repeated “A” characters. This does not scale. - Using
encode_length/1
anddecode_length!/1
ordecode_length/1
. This scales better, though is still relatively inefficient. It is much more compact, however and supports larger inputs. - Using
encode_length_bin/1
anddecode_length_bin!/1
ordecode_length_bin/1
. This scales better, but not as well as pure length encoding due to the string conversion. This exists mostly to provide a convenience for interfaces that need Base1 and expect Elixir binary input and output.
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
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"
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
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"
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
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"
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!(<<>>)
<<>>
Encodes a binary as a Base1 string.
Examples
iex> Base1.encode(<<1>>)
{:ok, "AA"}
iex> Base1.encode("b")
{:ok, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}
iex> Base1.encode(<<>>)
{:ok, <<>>}
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