Skip to content

Commit

Permalink
Try more debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Sep 29, 2024
1 parent 64de55f commit 5650dae
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Sources/Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class Zip {
let zip = unzOpen64(path)
defer { unzClose(zip) }
if unzGoToFirstFile(zip) != UNZ_OK {
print("XXXXXXXXXXX - 1")
throw ZipError.unzipFail
}
repeat {
Expand All @@ -81,12 +82,14 @@ public class Zip {
ret = unzOpenCurrentFile(zip)
}
if ret != UNZ_OK {
print("XXXXXXXXXXX - 2")
throw ZipError.unzipFail
}
var fileInfo = unz_file_info64()
ret = unzGetCurrentFileInfo64(zip, &fileInfo, nil, 0, nil, 0, nil, 0)
if ret != UNZ_OK {
unzCloseCurrentFile(zip)
print("XXXXXXXXXXX - 3")
throw ZipError.unzipFail
}
currentPosition += Double(fileInfo.compressed_size)
Expand All @@ -99,12 +102,8 @@ public class Zip {

var pathString = String(cString: fileName)

#if os(Windows)
// Colons are not allowed in Windows file names.
pathString = pathString.replacingOccurrences(of: ":", with: "_")
#endif

guard !pathString.isEmpty else {
print("XXXXXXXXXXX - 4")
throw ZipError.unzipFail
}

Expand All @@ -114,10 +113,11 @@ public class Zip {
pathString = pathString.replacingOccurrences(of: "\\", with: "/")
}

let fullPath = destination.appendingPathComponent(pathString).standardizedFileURL.path
let fullPath = destination.appendingPathComponent(pathString).standardizedFileURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }
// `.standardizedFileURL` removes any `..` to move a level up.
// If we then check that the `fullPath` starts with the destination directory we know we are not extracting "outside" the destination.
guard fullPath.starts(with: destination.standardizedFileURL.path) else {
print("XXXXXXXXXXX - 5")
throw ZipError.unzipFail
}

Expand All @@ -140,6 +140,7 @@ public class Zip {
let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize)
guard readBytes > 0 else { break }
guard fwrite(buffer, Int(readBytes), 1, filePointer) == 1 else {
print("XXXXXXXXXXX - 6")
throw ZipError.unzipFail
}
writeBytes += UInt64(readBytes)
Expand All @@ -148,9 +149,11 @@ public class Zip {
if let filePointer { fclose(filePointer) }

if unzCloseCurrentFile(zip) == UNZ_CRCERROR {
print("XXXXXXXXXXX - 7")
throw ZipError.unzipFail
}
guard writeBytes == fileInfo.uncompressed_size else {
print("XXXXXXXXXXX - 8")
throw ZipError.unzipFail
}

Expand Down

0 comments on commit 5650dae

Please sign in to comment.