Skip to content

Commit

Permalink
Sophisticated debug
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Sep 8, 2024
1 parent e2e6fe5 commit 61b9d18
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Sources/Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class Zip {
let zip = unzOpen64(path)
defer { unzClose(zip) }
if unzGoToFirstFile(zip) != UNZ_OK {
print("XXXXXXXXXXXXXXXXXXXXXXXX - 1")
throw ZipError.unzipFail
}
repeat {
Expand All @@ -80,13 +81,15 @@ public class Zip {
ret = unzOpenCurrentFile(zip)
}
if ret != UNZ_OK {
print("XXXXXXXXXXXXXXXXXXXXXXXX - 2")
throw ZipError.unzipFail
}
var fileInfo = unz_file_info64()
memset(&fileInfo, 0, MemoryLayout<unz_file_info>.size)
ret = unzGetCurrentFileInfo64(zip, &fileInfo, nil, 0, nil, 0, nil, 0)
if ret != UNZ_OK {
unzCloseCurrentFile(zip)
print("XXXXXXXXXXXXXXXXXXXXXXXX - 3")
throw ZipError.unzipFail
}
currentPosition += Double(fileInfo.compressed_size)
Expand All @@ -99,6 +102,7 @@ public class Zip {

var pathString = String(cString: fileName)
guard !pathString.isEmpty else {
print("XXXXXXXXXXXXXXXXXXXXXXXX - 4")
throw ZipError.unzipFail
}

Expand All @@ -116,6 +120,7 @@ public class Zip {
// `.standardized` 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("XXXXXXXXXXXXXXXXXXXXXXXX - 5")
throw ZipError.unzipFail
}

Expand Down Expand Up @@ -152,6 +157,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("XXXXXXXXXXXXXXXXXXXXXXXX - 6")
throw ZipError.unzipFail
}
writeBytes += UInt64(readBytes)
Expand All @@ -161,9 +167,11 @@ public class Zip {

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

Expand Down Expand Up @@ -288,9 +296,19 @@ public class Zip {
throw ZipError.zipFail
}
if let password, let fileName {
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil, UInt16(Z_DEFLATED), compression.minizipCompression, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password, 0)
zipOpenNewFileInZip3(
zip, fileName, &zipInfo,
nil, 0, nil, 0, nil,
UInt16(Z_DEFLATED), compression.minizipCompression, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
password, 0
)
} else if let fileName {
zipOpenNewFileInZip3(zip, fileName, &zipInfo, nil, 0, nil, 0, nil, UInt16(Z_DEFLATED), compression.minizipCompression, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, nil, 0)
zipOpenNewFileInZip3(
zip, fileName, &zipInfo,
nil, 0, nil, 0, nil,
UInt16(Z_DEFLATED), compression.minizipCompression, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
nil, 0
)
} else {
throw ZipError.zipFail
}
Expand Down

0 comments on commit 61b9d18

Please sign in to comment.