defmodule EBML do @moduledoc """ Functions to decode EBML elements RFC: https://datatracker.ietf.org/doc/html/rfc8794 """ import EBML.Spec import EBML.Parser import EBML.Vint @spec decode(binary, list) :: [{atom, binary}] def decode(bytes, acc \\ []) def decode(<<>>, acc), do: acc def decode(bytes, acc) do with {id, bytes} <- decode_name(bytes), {data_size, bytes} <- decode_vint(bytes), {data, bytes} <- bytes_part(bytes, data_size) do type = typeof(id) data = parse(type, data) decode(bytes, [{id, data} | acc]) end end @spec decode_name(binary) :: {atom, binary} def decode_name(<> = bytes) do vint_width = get_vint_width(byte) <> = bytes {keyof(vint), tail} end @spec decode_vint(binary) :: {non_neg_integer, binary} def decode_vint(<> = bytes) do vint_width = get_vint_width(byte) <> = bytes {get_vint_data(vint, vint_width), tail} end defp bytes_part(bytes, data_size) do <> = bytes {bytes, tail} end end