#import "zip" #import "file_linux" // The platform zlib supplies raw DEFLATE for ZIP method 8. This native // adapter is separate from the portable ZIP directory and checksum rules. zlib :: #system_library "z"; ZStream :: struct { next_in: *u8 avail_in: u32 #if #defined(_WIN32) { total_in: u32 } else { total_in: usize } next_out: *u8 avail_out: u32 #if #defined(_WIN32) { total_out: u32 } else { total_out: usize } msg: *u8 state: *void zalloc: *void zfree: *void opaque: *void data_type: s32 #if #defined(_WIN32) { adler: u32 reserved: u32 } else { adler: usize reserved: usize } } ZlibVersion :: () -> *u8 #foreign zlib "zlibVersion"; ZlibInflateInit :: (stream: *ZStream, window_bits: s32, version: *u8, stream_size: s32) -> s32 #foreign zlib "inflateInit2_"; ZlibInflate :: (stream: *ZStream, flush: s32) -> s32 #foreign zlib "inflate"; ZlibInflateEnd :: (stream: *ZStream) -> s32 #foreign zlib "inflateEnd"; #program_export ExtractZipEntry :: (archive: []u8, entry: ZipEntry, output: []u8) -> bool { if !entry.found || entry.size != cast(u64)output.count || entry.data_start > cast(u64)archive.count || entry.compressed_size > cast(u64)archive.count - entry.data_start { return false } if entry.method == 0 { return CopyStoredZipEntry(archive, entry, output) } if entry.method != 8 || entry.compressed_size == 0 || entry.compressed_size > cast(u64)0xffffffff || entry.size > cast(u64)0xffffffff { return false } stream: ZStream spare: u8 = 0 stream.next_in = *archive[entry.data_start] stream.avail_in = cast(u32)entry.compressed_size stream.next_out = *spare if output.count > 0 { stream.next_out = *output[0] } stream.avail_out = cast(u32)output.count if output.count == 0 { stream.avail_out = 1 } if ZlibInflateInit(*stream, -15, ZlibVersion(), cast(s32)size_of(ZStream)) != 0 { return false } status: s32 = ZlibInflate(*stream, 4) valid: bool = status == 1 && cast(u64)stream.total_in == entry.compressed_size && cast(u64)stream.total_out == entry.size if ZlibInflateEnd(*stream) != 0 { valid = false } return valid && ZipCRC32(output) == entry.crc32 } ZipExtractWriteAll :: (file: FileHandle, bytes: []u8, offset: u64) -> bool { written: s64 = 0 while written < bytes.count { result := WriteAt(file, bytes[written:bytes.count], cast(s64)offset + written) if result <= 0 { return false } written += result } return true } // Inflate a classic ZIP entry into a caller-owned file without holding the // uncompressed payload in memory. The caller publishes or removes that file. #program_export ExtractZipEntryToFile :: (archive: []u8, entry: ZipEntry, file: FileHandle) -> bool { if !file.valid || !entry.found || entry.data_start > cast(u64)archive.count || entry.compressed_size > cast(u64)archive.count - entry.data_start || entry.compressed_size > cast(u64)0xffffffff || entry.size > cast(u64)0xffffffff { return false } crc: u32 = cast(u32)0xffffffff written: u64 = 0 if entry.method == 0 { if entry.compressed_size != entry.size { return false } while written < entry.size { count := entry.size - written if count > 65536 { count = 65536 } chunk := archive[entry.data_start + written: entry.data_start + written + count] if !ZipExtractWriteAll(file, chunk, written) { return false } crc = ZipCRC32Update(crc, chunk) written += count } return (crc ^ cast(u32)0xffffffff) == entry.crc32 } if entry.method != 8 || entry.compressed_size == 0 { return false } stream: ZStream = ZStream.{} stream.next_in = *archive[entry.data_start] stream.avail_in = cast(u32)entry.compressed_size if ZlibInflateInit(*stream, -15, ZlibVersion(), cast(s32)size_of(ZStream)) != 0 { return false } output: [16384]u8 valid: bool = true while true { before_in := stream.total_in before_out := stream.total_out stream.next_out = *output[0] stream.avail_out = 16384 status := ZlibInflate(*stream, 0) count: u32 = 16384 - stream.avail_out if cast(u64)count > entry.size - written { valid = false break } if count > 0 { chunk := output[0:cast(s32)count] if !ZipExtractWriteAll(file, chunk, written) { valid = false break } crc = ZipCRC32Update(crc, chunk) written += cast(u64)count } if status == 1 { break } if status != 0 || (stream.total_in == before_in && stream.total_out == before_out) { valid = false break } } if ZlibInflateEnd(*stream) != 0 { valid = false } return valid && cast(u64)stream.total_in == entry.compressed_size && cast(u64)stream.total_out == entry.size && written == entry.size && (crc ^ cast(u32)0xffffffff) == entry.crc32 }