Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Katütata! #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions lib/binary_kata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ defmodule BinaryKata do
Should return `true` when given parameter start with UTF8 Byte-Order-Mark, otherwise `false`.
@see https://en.wikipedia.org/wiki/Byte_order_mark
"""
def has_utf8_bom?(_), do: raise "TODO: Implement me!"
def has_utf8_bom?(<<0xef,0xbb,0xbf,_ :: binary>>), do: true
def has_utf8_bom?(_), do: false

@doc """
Remove a UTF8 BOM if exists.
"""
def remove_utf8_bom(_), do: raise "TODO: Implement me!"
def remove_utf8_bom(<<0xef,0xbb,0xbf,rest :: binary>>), do: rest
def remove_utf8_bom(data) when is_binary(data), do: data

@doc """
Add a UTF8 BOM if not exists.
"""
def add_utf8_bom(_), do: raise "TODO: Implement me!"
def add_utf8_bom(<<0xef,0xbb,0xbf,rest :: binary>> = data), do: data
def add_utf8_bom(data), do: <<0xef,0xbb,0xbf,data :: binary>>

@doc """
Detecting types of images by their first bytes / magic numbers.
Expand All @@ -23,7 +26,12 @@ defmodule BinaryKata do
@see https://en.wikipedia.org/wiki/Portable_Network_Graphics
@see https://en.wikipedia.org/wiki/GIF
"""
def image_type!(_), do: raise "TODO: Implement me!"
def image_type!(<<"GIF87a",_ :: binary>>), do: :gif
def image_type!(<<"GIF89a", _ :: binary>>), do: :gif
def image_type!(<<0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a, _ :: binary>>), do: :png
def image_type!(<<0xff,0xd8, _ :: binary>>), do: :jfif
def image_type!(<<0xff,0xd9, _ :: binary>>), do: :jfif
def image_type!(_), do: :unknown

@doc """
Get the width and height from a GIF image.
Expand All @@ -32,15 +40,41 @@ defmodule BinaryKata do
`width` will be little-endian in byte 7 and 8.
`height` will be little-endian in byte 9 and 10.
"""
def gif_dimensions!(_), do: raise "TODO: Implement me!"
def gif_dimensions!(<<header :: binary-size(6),_ :: binary>>) when header != <<"GIF87a">> and header != <<"GIF89a">>, do: :error
def gif_dimensions!(<<_ :: binary-size(6), width :: little-integer-size(16), height :: little-integer-size(16), _ :: binary>>), do: {width, height}

@doc """
Parsing Payload of a ARP packet. Padding will be omitted.

Internet Protocol (IPv4) over Ethernet ARP packet
octet offset 0 1
0 Hardware type (HTYPE)
2 Protocol type (PTYPE)
4 Hardware address length (HLEN) | Protocol address length (PLEN)
6 Operation (OPER)
8 Sender hardware address (SHA) (first 2 bytes)
10 (next 2 bytes)
12 (last 2 bytes)
14 Sender protocol address (SPA) (first 2 bytes)
16 (last 2 bytes)
18 Target hardware address (THA) (first 2 bytes)
20 (next 2 bytes)
22 (last 2 bytes)
24 Target protocol address (TPA) (first 2 bytes)
26 (last 2 bytes)

@see https://en.wikipedia.org/wiki/Address_Resolution_Protocol
"""
def parse_arp_packet_ipv4!(_) do
raise "TODO: Implement me!"
def parse_arp_packet_ipv4!(<<_ :: binary-size(6), operation :: binary-size(2), sender_address :: integer-size(48), sender_ip :: binary-size(4), target_adress :: integer-size(48), target_ip :: binary-size(4), _ :: binary>>) do
operation = case operation do
<<0,1>> -> :request
<<0,2>> -> :response
end

<<a,b,c,d>> = sender_ip
<<e,f,g,h>> = target_ip

{operation, sender_address, {a,b,c,d}, target_adress, {e,f,g,h}}
end

# Helper for `parse_arp_packet_ipv4!`
Expand Down
2 changes: 1 addition & 1 deletion test/binary_kata_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule BinaryKataTest do
use ExUnit.Case
use ExUnit.Case, async: false
doctest BinaryKata

test "has_utf8_bom?" do
Expand Down