Commit 63cf4227 authored by Roy Marmelstein's avatar Roy Marmelstein Committed by GitHub

Merge pull request #94 from bricklife/feature/uncompressed_size_validation

Add fwrite() and uncompressed_size validation
parents 37433c91 1ea5e961
...@@ -187,12 +187,17 @@ public class Zip { ...@@ -187,12 +187,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
...@@ -204,6 +209,9 @@ public class Zip { ...@@ -204,6 +209,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