From 6df217d92249b9fe1644115cde794899b7847380 Mon Sep 17 00:00:00 2001 From: Feliks Borzik Date: Sun, 8 Jun 2025 14:53:04 +0100 Subject: [PATCH 1/2] zip64 values should be truncated only if they exceed allowed int size --- lib/zstream/protocol.ex | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/zstream/protocol.ex b/lib/zstream/protocol.ex index f5e8c8f..19ef122 100644 --- a/lib/zstream/protocol.ex +++ b/lib/zstream/protocol.ex @@ -163,11 +163,11 @@ defmodule Zstream.Protocol do # number of the disk with the start of the central directory zip64?(options, 0, 0xFFFF)::little-size(16), # total number of entries in the central directory on this disk - zip64?(options, entries_count, 0xFFFF)::little-size(16), + ceil_zip64?(options, entries_count, 0xFFFF)::little-size(16), # total number of entries in the central directory - zip64?(options, entries_count, 0xFFFF)::little-size(16), + ceil_zip64?(options, entries_count, 0xFFFF)::little-size(16), # size of the central directory - zip64?(options, size, 0xFFFFFFFF)::little-size(32), + ceil_zip64?(options, size, 0xFFFFFFFF)::little-size(32), # offset of start of central directory with respect to the starting disk number zip64?(options, offset, 0xFFFFFFFF)::little-size(32), # .ZIP file comment length @@ -211,4 +211,12 @@ defmodule Zstream.Protocol do normal end end + + defp ceil_zip64?(options, normal, max_value) do + if Keyword.fetch!(options, :zip64) && normal > max_value do + max_value + else + normal + end + end end From 84ef46277bf4ca6e654204522a580b08a10a4f58 Mon Sep 17 00:00:00 2001 From: Feliks Borzik Date: Tue, 10 Jun 2025 21:56:08 +0100 Subject: [PATCH 2/2] simplify end of central directory --- lib/zstream/protocol.ex | 22 +++++++--------------- lib/zstream/zip.ex | 3 +-- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/lib/zstream/protocol.ex b/lib/zstream/protocol.ex index 19ef122..c6dda15 100644 --- a/lib/zstream/protocol.ex +++ b/lib/zstream/protocol.ex @@ -154,22 +154,22 @@ defmodule Zstream.Protocol do end end - def end_of_central_directory(offset, size, entries_count, options) do + def end_of_central_directory(offset, size, entries_count) do << # end of central dir signature 0x06054B50::little-size(32), # number of this disk - zip64?(options, 0, 0xFFFF)::little-size(16), + 0::little-size(16), # number of the disk with the start of the central directory - zip64?(options, 0, 0xFFFF)::little-size(16), + 0::little-size(16), # total number of entries in the central directory on this disk - ceil_zip64?(options, entries_count, 0xFFFF)::little-size(16), + Enum.min([entries_count, 0xFFFF])::little-size(16), # total number of entries in the central directory - ceil_zip64?(options, entries_count, 0xFFFF)::little-size(16), + Enum.min([entries_count, 0xFFFF])::little-size(16), # size of the central directory - ceil_zip64?(options, size, 0xFFFFFFFF)::little-size(32), + Enum.min([size, 0xFFFFFFFF])::little-size(32), # offset of start of central directory with respect to the starting disk number - zip64?(options, offset, 0xFFFFFFFF)::little-size(32), + Enum.min([offset, 0xFFFFFFFF])::little-size(32), # .ZIP file comment length byte_size(@comment)::little-size(16), @comment @@ -211,12 +211,4 @@ defmodule Zstream.Protocol do normal end end - - defp ceil_zip64?(options, normal, max_value) do - if Keyword.fetch!(options, :zip64) && normal > max_value do - max_value - else - normal - end - end end diff --git a/lib/zstream/zip.ex b/lib/zstream/zip.ex index f221a4e..082434f 100644 --- a/lib/zstream/zip.ex +++ b/lib/zstream/zip.ex @@ -99,8 +99,7 @@ defmodule Zstream.Zip do Protocol.end_of_central_directory( state.offset, IO.iodata_length(central_directory_headers), - length(state.entries), - global_options + length(state.entries) ) {[