-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
serializer.ex
48 lines (40 loc) · 1.11 KB
/
serializer.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
defmodule UBootEnv.Serializer do
@moduledoc """
Encode and decode a U-Boot environment data
The U-Boot environment data looks like this:
* `"<key>=<value>\\0"` for each key-value pair
* `"\\0"` an empty key-value pair to terminate the list.
This looks like `"\\0\\0"` when viewing the file in a hex editor.
"""
@doc """
Encode a list of key value pairs into their binary form.
"""
@spec encode(map()) :: binary()
def encode(kv) when is_map(kv) do
[Enum.map(kv, &kv_to_encoded/1), <<0>>] |> IO.iodata_to_binary()
end
defp kv_to_encoded({k, v}), do: [k, ?=, v, 0]
@doc """
Decode a U-Boot environment binary data to a map of key/value pairs
"""
@spec decode(binary()) :: map()
def decode(data) do
data
|> decode_contents()
|> Map.new()
end
defp decode_contents(contents, acc \\ []) do
case :binary.split(contents, <<0>>) do
["" | _rest] ->
acc
[kv, rest] ->
decode_contents(rest, [parse_kv(kv) | acc])
end
end
defp parse_kv(kv) do
case :binary.split(kv, "=") do
[k, v] -> {k, v}
[k] -> {k, ""}
end
end
end