diff options
Diffstat (limited to 'WireGuard/WireGuard/ZipArchive')
-rw-r--r-- | WireGuard/WireGuard/ZipArchive/ZipArchive.swift | 31 | ||||
-rw-r--r-- | WireGuard/WireGuard/ZipArchive/ZipExporter.swift | 8 | ||||
-rw-r--r-- | WireGuard/WireGuard/ZipArchive/ZipImporter.swift | 12 |
3 files changed, 21 insertions, 30 deletions
diff --git a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift index 900341e..12cfb69 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift @@ -9,7 +9,7 @@ enum ZipArchiveError: WireGuardAppError { case badArchive func alertText() -> (String, String)? { - switch (self) { + switch self { case .cantOpenInputZipFile: return ("Unable to read zip archive", "The zip archive could not be read.") case .cantOpenOutputZipFileForWriting: @@ -49,15 +49,11 @@ class ZipArchive { defer { unzClose(zipFile) } - guard (unzGoToFirstFile(zipFile) == UNZ_OK) else { - throw ZipArchiveError.badArchive - } + guard unzGoToFirstFile(zipFile) == UNZ_OK else { throw ZipArchiveError.badArchive } var resultOfGoToNextFile: Int32 repeat { - guard (unzOpenCurrentFile(zipFile) == UNZ_OK) else { - throw ZipArchiveError.badArchive - } + guard unzOpenCurrentFile(zipFile) == UNZ_OK else { throw ZipArchiveError.badArchive } let bufferSize = 16384 // 16 KiB var fileNameBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufferSize) @@ -68,38 +64,33 @@ class ZipArchive { dataBuffer.deallocate() } - guard (unzGetCurrentFileInfo64(zipFile, nil, fileNameBuffer, UInt(bufferSize), nil, 0, nil, 0) == UNZ_OK) else { - throw ZipArchiveError.badArchive - } + guard unzGetCurrentFileInfo64(zipFile, nil, fileNameBuffer, UInt(bufferSize), nil, 0, nil, 0) == UNZ_OK else { throw ZipArchiveError.badArchive } let lastChar = String(cString: fileNameBuffer).suffix(1) let isDirectory = (lastChar == "/" || lastChar == "\\") let fileURL = URL(fileURLWithFileSystemRepresentation: fileNameBuffer, isDirectory: isDirectory, relativeTo: nil) - if (!isDirectory && requiredFileExtensions.contains(fileURL.pathExtension)) { + if !isDirectory && requiredFileExtensions.contains(fileURL.pathExtension) { var unzippedData = Data() var bytesRead: Int32 = 0 repeat { bytesRead = unzReadCurrentFile(zipFile, dataBuffer, UInt32(bufferSize)) - if (bytesRead > 0) { + if bytesRead > 0 { let dataRead = dataBuffer.withMemoryRebound(to: UInt8.self, capacity: bufferSize) { - (buf: UnsafeMutablePointer<UInt8>) -> Data in - return Data(bytes: buf, count: Int(bytesRead)) + return Data(bytes: $0, count: Int(bytesRead)) } unzippedData.append(dataRead) } - } while (bytesRead > 0) + } while bytesRead > 0 results.append((fileBaseName: fileURL.deletingPathExtension().lastPathComponent, contents: unzippedData)) } - guard (unzCloseCurrentFile(zipFile) == UNZ_OK) else { - throw ZipArchiveError.badArchive - } + guard unzCloseCurrentFile(zipFile) == UNZ_OK else { throw ZipArchiveError.badArchive } resultOfGoToNextFile = unzGoToNextFile(zipFile) - } while (resultOfGoToNextFile == UNZ_OK) + } while resultOfGoToNextFile == UNZ_OK - if (resultOfGoToNextFile == UNZ_END_OF_LIST_OF_FILE) { + if resultOfGoToNextFile == UNZ_END_OF_LIST_OF_FILE { return results } else { throw ZipArchiveError.badArchive diff --git a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift index f875e1b..b0e6b15 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift @@ -7,7 +7,7 @@ enum ZipExporterError: WireGuardAppError { case noTunnelsToExport func alertText() -> (String, String)? { - switch (self) { + switch self { case .noTunnelsToExport: return ("Nothing to export", "There are no tunnels to export") } @@ -18,7 +18,7 @@ class ZipExporter { static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to url: URL, completion: @escaping (WireGuardAppError?) -> Void) { - guard (!tunnelConfigurations.isEmpty) else { + guard !tunnelConfigurations.isEmpty else { completion(ZipExporterError.noTunnelsToExport) return } @@ -28,14 +28,14 @@ class ZipExporter { for tunnelConfiguration in tunnelConfigurations { if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) { let name = tunnelConfiguration.interface.name - if (name.isEmpty || name == lastTunnelName) { continue } + if name.isEmpty || name == lastTunnelName { continue } inputsToArchiver.append((fileName: "\(name).conf", contents: contents)) lastTunnelName = name } } do { try ZipArchive.archive(inputs: inputsToArchiver, to: url) - } catch (let error as WireGuardAppError) { + } catch let error as WireGuardAppError { DispatchQueue.main.async { completion(error) } return } catch { diff --git a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift index 194dc0e..e2767f2 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift @@ -7,7 +7,7 @@ enum ZipImporterError: WireGuardAppError { case noTunnelsInZipArchive func alertText() -> (String, String)? { - switch (self) { + switch self { case .noTunnelsInZipArchive: return ("No tunnels in zip archive", "No .conf tunnel files were found inside the zip archive.") } @@ -23,17 +23,17 @@ class ZipImporter { for (index, unarchivedFile) in unarchivedFiles.enumerated().reversed() { let fileBaseName = unarchivedFile.fileBaseName let trimmedName = fileBaseName.trimmingCharacters(in: .whitespacesAndNewlines) - if (!trimmedName.isEmpty) { + if !trimmedName.isEmpty { unarchivedFiles[index].fileBaseName = trimmedName } else { unarchivedFiles.remove(at: index) } } - if (unarchivedFiles.isEmpty) { + if unarchivedFiles.isEmpty { throw ZipImporterError.noTunnelsInZipArchive } - } catch (let error as WireGuardAppError) { + } catch let error as WireGuardAppError { DispatchQueue.main.async { completion(.failure(error)) } return } catch { @@ -41,9 +41,9 @@ class ZipImporter { } unarchivedFiles.sort { $0.fileBaseName < $1.fileBaseName } - var configs = Array<TunnelConfiguration?>(repeating: nil, count: unarchivedFiles.count) + var configs: [TunnelConfiguration?] = Array(repeating: nil, count: unarchivedFiles.count) for (index, file) in unarchivedFiles.enumerated() { - if (index > 0 && file == unarchivedFiles[index - 1]) { + if index > 0 && file == unarchivedFiles[index - 1] { continue } guard let fileContents = String(data: file.contents, encoding: .utf8) else { |