summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/tlv.erl34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/tlv.erl b/src/tlv.erl
new file mode 100644
index 0000000..b1d3428
--- /dev/null
+++ b/src/tlv.erl
@@ -0,0 +1,34 @@
+%%% Copyright (c) 2015, NORDUnet A/S.
+%%% See LICENSE for licensing information.
+
+-module(tlv).
+
+-export([encode/2, decode/1]).
+
+-spec encode(binary(), binary()) -> binary().
+encode(Type, Value) ->
+ 4 = byte_size(Type), % FIXME: make Type a 4 octet binary
+ Len = byte_size(Value) + 8,
+ <<Len:32, Type/binary, Value/binary>>.
+
+-spec decode(binary()) -> {binary(), binary(), binary()}.
+decode(Data) ->
+ <<TotalLength:32, Rest1/binary>> = Data,
+ Length = TotalLength - 8,
+ <<Type:4/binary-unit:8, Rest2/binary>> = Rest1,
+ <<Value:Length/binary-unit:8, Rest3/binary>> = Rest2,
+ {Type, Value, Rest3}.
+
+%%%%%%%%%%%%%%%%%%%%
+
+-include_lib("eunit/include/eunit.hrl").
+-define(TESTDATA1, {<<"DATA">>, <<"some data">>}).
+-define(TESTDATA2, {<<"DAT2">>, <<"some other data">>}).
+
+the_test_() ->
+ {setup,
+ fun() -> [?TESTDATA1, ?TESTDATA2] end,
+ fun(_) -> ok end,
+ fun(TestVectors) ->
+ [?_assertEqual({T, V, <<>>}, decode(encode(T, V))) ||
+ {T, V} <- TestVectors] end}.