33 lines
691 B
Elixir
33 lines
691 B
Elixir
defmodule Exdns.DnsPacket.Binary do
|
|
@moduledoc false
|
|
|
|
def take_binary(bin, offset, len) do
|
|
if byte_size(bin) < offset + len do
|
|
{:error, :truncated}
|
|
else
|
|
<<_::binary-size(offset), chunk::binary-size(len), _::binary>> = bin
|
|
{:ok, {chunk, offset + len}}
|
|
end
|
|
end
|
|
|
|
def take_u16(bin, offset) do
|
|
case bin do
|
|
<<_::binary-size(offset), value::16, _::binary>> ->
|
|
{:ok, {value, offset + 2}}
|
|
|
|
_ ->
|
|
{:error, :truncated}
|
|
end
|
|
end
|
|
|
|
def take_u32(bin, offset) do
|
|
case bin do
|
|
<<_::binary-size(offset), value::32, _::binary>> ->
|
|
{:ok, {value, offset + 4}}
|
|
|
|
_ ->
|
|
{:error, :truncated}
|
|
end
|
|
end
|
|
end
|