From 4ff9e9aaf6758af6d2602cc19d0f11293300a878 Mon Sep 17 00:00:00 2001 From: niamtokik Date: Tue, 30 Mar 2021 13:05:45 +0000 Subject: [PATCH] encoding part to test --- lib/dotzip/central_directory_header.ex | 80 +++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/lib/dotzip/central_directory_header.ex b/lib/dotzip/central_directory_header.ex index 00c227c..143fc71 100644 --- a/lib/dotzip/central_directory_header.ex +++ b/lib/dotzip/central_directory_header.ex @@ -48,67 +48,127 @@ defmodule Dotzip.CentralDirectoryHeader do {:ok, Map.put(data, :last_modification_time, last_modification), rest} end + defp encode_last_modification_time({:ok, %{ :last_modification_time => last_modification_time } = data, buffer}) do + {:ok, data, << buffer::bitstring, last_modification_time::little-size(16) >>} + end + defp last_modification_date({:ok, data, << last_modification_date::little-size(16), rest::bitstring >>}) do {:ok, Map.put(data, :last_modification_date, last_modification_date), rest} end + defp encode_last_modification_date({:ok, %{ :last_modification_date => last_modification_date } = data, buffer}) do + {:ok, data, << buffer::bitstring, last_modification_date::little-size(16) >>} + end + defp crc32({:ok, data, << crc32::binary-size(4), rest::bitstring >>}) do - {:ok, Map.put(data, :crc32, crc32), rest} + {:ok, Map.put(data, :crc32, crc32), rest} + end + + defp encode_crc32({:ok, %{ :crc32 => crc32 } = data, buffer }) do + {:ok, data, << buffer::bitstring, crc32::binary-size(4) >>} end defp compressed_size({:ok, data, << compressed_size::little-size(32), rest::bitstring >>}) do {:ok, Map.put(data, :compressed_size, compressed_size), rest} end + defp encode_compressed_size({:ok, %{ :compressed_size => compressed_size } = data, buffer}) do + {:ok, data, << buffer::bitstring, compressed_size::little-size(32) >>} + end + defp uncompressed_size({:ok, data, << uncompressed_size::little-size(32), rest::bitstring >>}) do {:ok, Map.put(data, :uncompressed_size, uncompressed_size), rest} end - + + defp encode_uncompressed_size({:ok, %{ :uncompressed_size => uncompressed_size } = data, buffer}) do + {:ok, data, <> } + end + defp file_name_length({:ok, data, << file_name_length::little-size(16), rest::bitstring >>}) do {:ok, Map.put(data, :file_name_length, file_name_length), rest } end + defp encode_file_name_length({:ok, %{ :file_name_length => file_name_length } = data, buffer }) do + {:ok, data, <> } + end + defp extra_field_length({:ok, data, << extra_field_length::little-size(16), rest::bitstring >>}) do {:ok, Map.put(data, :extra_field_length, extra_field_length), rest} end + defp encode_extra_field_length({:ok, %{ :extra_field_length => extra_field_length } = data, buffer}) do + {:ok, data, <>} + end + defp file_comment_length({:ok, data, << file_comment_length::little-size(16), rest::bitstring >>}) do {:ok, Map.put(data, :file_comment_length, file_comment_length), rest} end + defp encode_file_comment_length({:ok, %{ :file_comment_length => file_comment_length } = data, buffer}) do + {:ok, data, <>} + end + defp disk_number_start({:ok, data, << disk_number_start::little-size(16), rest::bitstring >>}) do {:ok, Map.put(data, :disk_number_start, disk_number_start), rest} end + defp encode_disk_number_start({:ok, %{ :disk_number_start => disk_number_start } = data, buffer}) do + {:ok, data, <>} + end + defp internal_file_attributes({:ok, data, << internal_file_attributes::binary-size(2), rest::bitstring >>}) do {:ok, Map.put(data, :internal_file_attributes, internal_file_attributes), rest} end + defp encode_internal_file_attributes({:ok, %{ :internal_file_attributes => internal_file_attributes } = data, buffer}) do + {:ok, data, <>} + end + defp external_file_attributes({:ok, data, << external_file_attributes::binary-size(4), rest::bitstring >>}) do {:ok, Map.put(data, :external_file_attributes, external_file_attributes), rest} end + defp encode_external_file_attributes({:ok, %{ :external_file_attributes => external_file_attributes } = data, buffer}) do + {:ok, data, <>} + end + defp relative_offset({:ok, data, << relative_offset::little-size(32), rest::bitstring >>}) do {:ok, Map.put(data, :relative_offset, relative_offset), rest} end + defp encode_relative_offset({:ok, %{ :relative_offset => relative_offset } = data, buffer}) do + {:ok, data, <>} + end + defp file_name({:ok, data, rest}) do %{ :file_name_length => file_name_length } = data <> = rest {:ok, Map.put(data, :file_name, file_name), r} end + defp encode_file_name({:ok, %{ :file_name => file_name, :file_name_length => file_name_length } = data, buffer}) do + {:ok, data, << buffer::bitstring, file_name::binary-size(file_name_length) >>} + end + defp extra_field({:ok, data, rest}) do %{ :extra_field_length => extra_field_length } = data <> = rest {:ok, Map.put(data, :extra_field, extra_field), r} end + defp encode_extra_field({:ok, %{ :extra_field => extra_field, :extra_field_length => extra_field_length } = data, buffer}) do + {:ok, data, <>} + end + defp file_comment({:ok, data, rest}) do %{ :file_comment_length => file_comment_length } = data <> = rest {:ok, Map.put(data, :file_comment, file_comment), r} end + + defp encode_file_comment({:ok, %{ :file_comment => file_comment, :file_comment_length => file_comment_length} = data, buffer}) do + {:ok, data, <>} + end def decode(data) when is_bitstring(data) do {:ok, central_directory_header, rest} = signature(data) @@ -139,6 +199,22 @@ defmodule Dotzip.CentralDirectoryHeader do |> encode_version_needed() |> encode_purpose_flag() |> encode_compression_method() + |> encode_last_modification_time() + |> encode_last_modification_date() + |> encode_crc32() + |> encode_compressed_size() + |> encode_uncompressed_size() + |> encode_file_name_length() + |> encode_extra_field_length() + |> encode_file_comment_length() + |> encode_disk_number_start() + |> encode_internal_file_attributes() + |> encode_external_file_attributes() + |> encode_relative_offset() + |> encode_file_name() + |> encode_extra_field() + |> encode_file_comment() + end end