Skip to content

Commit

Permalink
Merge pull request #94 from bricklife/feature/uncompressed_size_valid…
Browse files Browse the repository at this point in the history
…ation

Add fwrite() and uncompressed_size validation
  • Loading branch information
marmelroy committed Sep 20, 2017
2 parents 37433c9 + 1ea5e96 commit 63cf422
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,17 @@ public class Zip {
unzCloseCurrentFile(zip)
ret = unzGoToNextFile(zip)
}

var writeBytes: UInt64 = 0
var filePointer: UnsafeMutablePointer<FILE>?
filePointer = fopen(fullPath, "wb")
while filePointer != nil {
let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize)
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 {
break
Expand All @@ -204,6 +209,9 @@ public class Zip {
if crc_ret == UNZ_CRCERROR {
throw ZipError.unzipFail
}
guard writeBytes == fileInfo.uncompressed_size else {
throw ZipError.unzipFail
}

//Set file permissions from current fileInfo
if fileInfo.external_fa != 0 {
Expand Down

0 comments on commit 63cf422

Please sign in to comment.