2018-10-28 19:34:46 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2023-02-14 15:10:32 +00:00
|
|
|
// Copyright © 2018-2023 WireGuard LLC. All Rights Reserved.
|
2018-10-28 19:34:46 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2018-12-06 13:35:46 +00:00
|
|
|
enum ZipArchiveError: WireGuardAppError {
|
2018-10-28 19:34:46 +00:00
|
|
|
case cantOpenInputZipFile
|
2018-10-29 17:26:25 +00:00
|
|
|
case cantOpenOutputZipFileForWriting
|
2018-10-28 19:34:46 +00:00
|
|
|
case badArchive
|
2019-04-08 07:52:06 +00:00
|
|
|
case noTunnelsInZipArchive
|
2018-12-06 13:35:46 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
var alertText: AlertText {
|
2018-12-12 18:28:27 +00:00
|
|
|
switch self {
|
2018-12-06 13:35:46 +00:00
|
|
|
case .cantOpenInputZipFile:
|
2018-12-18 11:00:16 +00:00
|
|
|
return (tr("alertCantOpenInputZipFileTitle"), tr("alertCantOpenInputZipFileMessage"))
|
2018-12-06 13:35:46 +00:00
|
|
|
case .cantOpenOutputZipFileForWriting:
|
2018-12-18 11:00:16 +00:00
|
|
|
return (tr("alertCantOpenOutputZipFileForWritingTitle"), tr("alertCantOpenOutputZipFileForWritingMessage"))
|
2018-12-06 13:35:46 +00:00
|
|
|
case .badArchive:
|
2018-12-18 11:00:16 +00:00
|
|
|
return (tr("alertBadArchiveTitle"), tr("alertBadArchiveMessage"))
|
2019-04-08 07:52:06 +00:00
|
|
|
case .noTunnelsInZipArchive:
|
|
|
|
return (tr("alertNoTunnelsInImportedZipArchiveTitle"), tr("alertNoTunnelsInImportedZipArchiveMessage"))
|
2018-12-06 13:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 19:34:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 16:23:50 +00:00
|
|
|
enum ZipArchive {}
|
|
|
|
|
|
|
|
extension ZipArchive {
|
2018-10-28 19:34:46 +00:00
|
|
|
|
2018-10-29 17:26:25 +00:00
|
|
|
static func archive(inputs: [(fileName: String, contents: Data)], to destinationURL: URL) throws {
|
|
|
|
let destinationPath = destinationURL.path
|
|
|
|
guard let zipFile = zipOpen(destinationPath, APPEND_STATUS_CREATE) else {
|
|
|
|
throw ZipArchiveError.cantOpenOutputZipFileForWriting
|
|
|
|
}
|
|
|
|
for input in inputs {
|
|
|
|
let fileName = input.fileName
|
|
|
|
let contents = input.contents
|
|
|
|
zipOpenNewFileInZip(zipFile, fileName.cString(using: .utf8), nil, nil, 0, nil, 0, nil, Z_DEFLATED, Z_DEFAULT_COMPRESSION)
|
2020-11-26 16:23:50 +00:00
|
|
|
contents.withUnsafeBytes { rawBufferPointer -> Void in
|
|
|
|
zipWriteInFileInZip(zipFile, rawBufferPointer.baseAddress, UInt32(contents.count))
|
2018-10-29 17:26:25 +00:00
|
|
|
}
|
|
|
|
zipCloseFileInZip(zipFile)
|
|
|
|
}
|
|
|
|
zipClose(zipFile, nil)
|
|
|
|
}
|
|
|
|
|
2018-12-12 10:31:24 +00:00
|
|
|
static func unarchive(url: URL, requiredFileExtensions: [String]) throws -> [(fileBaseName: String, contents: Data)] {
|
2018-10-28 19:34:46 +00:00
|
|
|
|
2018-12-13 03:09:52 +00:00
|
|
|
var results = [(fileBaseName: String, contents: Data)]()
|
2020-12-02 14:08:45 +00:00
|
|
|
let requiredFileExtensionsLowercased = requiredFileExtensions.map { $0.lowercased() }
|
2018-10-28 19:34:46 +00:00
|
|
|
|
|
|
|
guard let zipFile = unzOpen64(url.path) else {
|
|
|
|
throw ZipArchiveError.cantOpenInputZipFile
|
|
|
|
}
|
|
|
|
defer {
|
|
|
|
unzClose(zipFile)
|
|
|
|
}
|
2018-12-12 18:28:27 +00:00
|
|
|
guard unzGoToFirstFile(zipFile) == UNZ_OK else { throw ZipArchiveError.badArchive }
|
2018-10-28 19:34:46 +00:00
|
|
|
|
|
|
|
var resultOfGoToNextFile: Int32
|
|
|
|
repeat {
|
2018-12-12 18:28:27 +00:00
|
|
|
guard unzOpenCurrentFile(zipFile) == UNZ_OK else { throw ZipArchiveError.badArchive }
|
2018-10-28 19:34:46 +00:00
|
|
|
|
2018-12-08 08:13:00 +00:00
|
|
|
let bufferSize = 16384 // 16 KiB
|
2020-12-02 14:08:45 +00:00
|
|
|
let fileNameBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufferSize)
|
|
|
|
let dataBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufferSize)
|
2018-10-28 19:34:46 +00:00
|
|
|
|
|
|
|
defer {
|
|
|
|
fileNameBuffer.deallocate()
|
|
|
|
dataBuffer.deallocate()
|
|
|
|
}
|
|
|
|
|
2018-12-12 18:28:27 +00:00
|
|
|
guard unzGetCurrentFileInfo64(zipFile, nil, fileNameBuffer, UInt(bufferSize), nil, 0, nil, 0) == UNZ_OK else { throw ZipArchiveError.badArchive }
|
2018-10-28 19:34:46 +00:00
|
|
|
|
2018-12-12 10:31:24 +00:00
|
|
|
let lastChar = String(cString: fileNameBuffer).suffix(1)
|
|
|
|
let isDirectory = (lastChar == "/" || lastChar == "\\")
|
|
|
|
let fileURL = URL(fileURLWithFileSystemRepresentation: fileNameBuffer, isDirectory: isDirectory, relativeTo: nil)
|
2018-10-28 19:34:46 +00:00
|
|
|
|
2019-02-09 13:54:08 +00:00
|
|
|
if !isDirectory && requiredFileExtensionsLowercased.contains(fileURL.pathExtension.lowercased()) {
|
2018-10-28 19:34:46 +00:00
|
|
|
var unzippedData = Data()
|
|
|
|
var bytesRead: Int32 = 0
|
|
|
|
repeat {
|
|
|
|
bytesRead = unzReadCurrentFile(zipFile, dataBuffer, UInt32(bufferSize))
|
2018-12-12 18:28:27 +00:00
|
|
|
if bytesRead > 0 {
|
2018-10-28 19:34:46 +00:00
|
|
|
let dataRead = dataBuffer.withMemoryRebound(to: UInt8.self, capacity: bufferSize) {
|
2018-12-12 18:28:27 +00:00
|
|
|
return Data(bytes: $0, count: Int(bytesRead))
|
2018-10-28 19:34:46 +00:00
|
|
|
}
|
|
|
|
unzippedData.append(dataRead)
|
|
|
|
}
|
2018-12-12 18:28:27 +00:00
|
|
|
} while bytesRead > 0
|
2018-12-12 10:31:24 +00:00
|
|
|
results.append((fileBaseName: fileURL.deletingPathExtension().lastPathComponent, contents: unzippedData))
|
2018-10-28 19:34:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 18:28:27 +00:00
|
|
|
guard unzCloseCurrentFile(zipFile) == UNZ_OK else { throw ZipArchiveError.badArchive }
|
2018-10-28 19:34:46 +00:00
|
|
|
|
|
|
|
resultOfGoToNextFile = unzGoToNextFile(zipFile)
|
2018-12-12 18:28:27 +00:00
|
|
|
} while resultOfGoToNextFile == UNZ_OK
|
2018-10-28 19:34:46 +00:00
|
|
|
|
2018-12-12 18:28:27 +00:00
|
|
|
if resultOfGoToNextFile == UNZ_END_OF_LIST_OF_FILE {
|
2018-10-28 19:34:46 +00:00
|
|
|
return results
|
|
|
|
} else {
|
|
|
|
throw ZipArchiveError.badArchive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|