Commit 1ea5e961 authored by Shinichiro Oba's avatar Shinichiro Oba

Add fwrite() and uncompressed_size validation

parent 4a15a786
...@@ -180,12 +180,17 @@ public class Zip { ...@@ -180,12 +180,17 @@ public class Zip {
unzCloseCurrentFile(zip) unzCloseCurrentFile(zip)
ret = unzGoToNextFile(zip) ret = unzGoToNextFile(zip)
} }
var writeBytes: UInt64 = 0
var filePointer: UnsafeMutablePointer<FILE>? var filePointer: UnsafeMutablePointer<FILE>?
filePointer = fopen(fullPath, "wb") filePointer = fopen(fullPath, "wb")
while filePointer != nil { while filePointer != nil {
let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize) let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize)
if readBytes > 0 { if readBytes > 0 {
fwrite(buffer, Int(readBytes), 1, filePointer) guard fwrite(buffer, Int(readBytes), 1, filePointer) == 1 else {
throw ZipError.unzipFail
}
writeBytes += UInt64(readBytes)
} }
else { else {
break break
...@@ -197,6 +202,9 @@ public class Zip { ...@@ -197,6 +202,9 @@ public class Zip {
if crc_ret == UNZ_CRCERROR { if crc_ret == UNZ_CRCERROR {
throw ZipError.unzipFail throw ZipError.unzipFail
} }
guard writeBytes == fileInfo.uncompressed_size else {
throw ZipError.unzipFail
}
//Set file permissions from current fileInfo //Set file permissions from current fileInfo
if fileInfo.external_fa != 0 { if fileInfo.external_fa != 0 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment