diff --git a/Sources/TAR/TarContainer.swift b/Sources/TAR/TarContainer.swift index 8504677e..564a4a23 100644 --- a/Sources/TAR/TarContainer.swift +++ b/Sources/TAR/TarContainer.swift @@ -52,7 +52,7 @@ public class TarContainer: Container { } else if specialEntryType == .longName || specialEntryType == .longLinkName { return .gnu } - case .entryInfo(let info): + case .entryInfo(let info, _): // TODO: Probably this case (depending on how info.format is set) is already covered by the above. switch info.format { case .pax: @@ -175,25 +175,42 @@ public class TarContainer: Container { - Returns: Array of `TarEntry`. */ public static func open(container data: Data) throws -> [TarEntry] { - let infos = try info(container: data) + // TAR container should be at least 512 bytes long (when it contains only one header). + guard data.count >= 512 + else { throw TarError.tooSmallFileIsPassed } + + var parser = TarParser(data) var entries = [TarEntry]() - for entryInfo in infos { - if entryInfo.type == .directory { - var entry = TarEntry(info: entryInfo, data: nil) - entry.info.size = 0 - entries.append(entry) - } else { - let dataStartIndex = entryInfo.blockStartIndex + 512 - let dataEndIndex = dataStartIndex + entryInfo.size! - // Verify that data is not truncated. - // The data.startIndex inequality is strict since by this point at least one header (i.e. 512 bytes) - // has been processed. The data.endIndex inequality is strict since there can be a 1024 bytes-long EOF - // marker block which isn't included into any entry. - guard dataStartIndex > data.startIndex && dataEndIndex < data.endIndex - else { throw TarError.wrongField } - let entryData = data.subdata(in: dataStartIndex.. data.startIndex && dataEndIndex < data.endIndex + else { throw TarError.wrongField } + let entryData = data.subdata(in: dataStartIndex..