An erlang implementation of Bittorrent’s bencode format. See: http://bittorrent.org/beps/bep_0003.html
- There is no depth limit.
- No error handing. Invalid input will cause a crash.
This library can be built with rebar3: $ rebar3 compile
or you can include it in your projects as a dependency in rebar.config:
{deps, [%% ... Other dependencies
{bencode, {git, "https://github.com/hyperimpose/bencode.git", {branch, "master"}}}]}.
There are two functions exported by bencode
: decode/1 and encode/1.
%% Encode an erlang term:
%%
%% This function will return the following bencoded binary:
%% <<"d4:testi123ee">>
bencode:encode(#{<<"test" => 123>>}).
%% Decode an erlang term:
%%
%% This function will return an erlang dictionary:
%% #{<<"test" => 123>>}
bencode:decode(<<"d4:testi123ee">>).
%% See the next section for the supported terms and their equivalents.
The bencoded values map to Erlang terms as follows:
Bencode | Erlang |
---|---|
dictionaries | map() #{} (If there are duplicate keys the last one is kept) |
lists | list() [] (Including strings/iolists etc.) |
integers | integer() |
strings | binary() |
Atoms or other terms are not supported.
This implementation of bencode is licensed under the BSD 3-Clause “New” or “Revised” License. A copy of this license is included in the file COPYING.