From fa7be46432f3364b8e0fbb61b38547b3d1bd3e05 Mon Sep 17 00:00:00 2001 From: Robert Lechowicz Date: Tue, 23 Jan 2024 12:39:48 +0100 Subject: [PATCH] Fix issue when ZIP64 extra field has different size than expected from Central directory file header --- Sources/ZIPFoundation/Entry+ZIP64.swift | 7 +++++-- .../ZIPFoundationTests/ZIPFoundationEntryTests+ZIP64.swift | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/ZIPFoundation/Entry+ZIP64.swift b/Sources/ZIPFoundation/Entry+ZIP64.swift index dec18688..c8188756 100644 --- a/Sources/ZIPFoundation/Entry+ZIP64.swift +++ b/Sources/ZIPFoundation/Entry+ZIP64.swift @@ -104,7 +104,10 @@ extension Entry.ZIP64ExtendedInformation { init?(data: Data, fields: [Field]) { let headerLength = 4 - guard fields.reduce(0, { $0 + $1.size }) + headerLength == data.count else { return nil } + let tmpDataSize:UInt16 = data.scanValue(start: 2) + guard Int(tmpDataSize) + headerLength == data.count else { + return nil + } var readOffset = headerLength func value(of field: Field) throws -> T where T: BinaryInteger { if fields.contains(field) { @@ -120,7 +123,7 @@ extension Entry.ZIP64ExtendedInformation { } } do { - dataSize = data.scanValue(start: 2) + dataSize = tmpDataSize uncompressedSize = try value(of: .uncompressedSize) compressedSize = try value(of: .compressedSize) relativeOffsetOfLocalHeader = try value(of: .relativeOffsetOfLocalHeader) diff --git a/Tests/ZIPFoundationTests/ZIPFoundationEntryTests+ZIP64.swift b/Tests/ZIPFoundationTests/ZIPFoundationEntryTests+ZIP64.swift index ea0e7a52..71abc26d 100644 --- a/Tests/ZIPFoundationTests/ZIPFoundationEntryTests+ZIP64.swift +++ b/Tests/ZIPFoundationTests/ZIPFoundationEntryTests+ZIP64.swift @@ -132,7 +132,7 @@ extension ZIPFoundationTests { let invalidExtraField2 = ZIP64ExtendedInformation(data: Data(extraFieldBytesMissingByte), fields: [.compressedSize, .uncompressedSize]) XCTAssertNil(invalidExtraField2) - let extraFieldBytesWithWrongFields: [UInt8] = [0x01, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x00, 0x00, + let extraFieldBytesWithWrongFields: [UInt8] = [0x01, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] let invalidExtraField3 = ZIP64ExtendedInformation(data: Data(extraFieldBytesWithWrongFields),