| ... | ... | @@ -5,11 +5,8 @@ |
| 5 | 5 | |
| 6 | 6 | const builtin = @import("builtin"); |
| 7 | 7 | const std = @import("std"); |
| 8 | | const testing = std.testing; |
| 9 | | |
| 10 | | pub const testutil = @import("zip/test.zig"); |
| 11 | | const File = testutil.File; |
| 12 | | const FileStore = testutil.FileStore; |
| 8 | const File = std.fs.File; |
| 9 | const is_le = builtin.target.cpu.arch.endian() == .little; |
| 13 | 10 | |
| 14 | 11 | pub const CompressionMethod = enum(u16) { |
| 15 | 12 | store = 0, |
| ... | ... | @@ -95,57 +92,71 @@ pub const EndRecord = extern struct { |
| 95 | 92 | central_directory_size: u32 align(1), |
| 96 | 93 | central_directory_offset: u32 align(1), |
| 97 | 94 | comment_len: u16 align(1), |
| 95 | |
| 98 | 96 | pub fn need_zip64(self: EndRecord) bool { |
| 99 | 97 | return isMaxInt(self.record_count_disk) or |
| 100 | 98 | isMaxInt(self.record_count_total) or |
| 101 | 99 | isMaxInt(self.central_directory_size) or |
| 102 | 100 | isMaxInt(self.central_directory_offset); |
| 103 | 101 | } |
| 104 | | }; |
| 105 | 102 | |
| 106 | | /// Find and return the end record for the given seekable zip stream. |
| 107 | | /// Note that `seekable_stream` must be an instance of `std.io.SeekableStream` and |
| 108 | | /// its context must also have a `.reader()` method that returns an instance of |
| 109 | | /// `std.io.Reader`. |
| 110 | | pub fn findEndRecord(seekable_stream: anytype, stream_len: u64) !EndRecord { |
| 111 | | var buf: [@sizeOf(EndRecord) + std.math.maxInt(u16)]u8 = undefined; |
| 112 | | const record_len_max = @min(stream_len, buf.len); |
| 113 | | var loaded_len: u32 = 0; |
| 114 | | |
| 115 | | var comment_len: u16 = 0; |
| 116 | | while (true) { |
| 117 | | const record_len: u32 = @as(u32, comment_len) + @sizeOf(EndRecord); |
| 118 | | if (record_len > record_len_max) |
| 119 | | return error.ZipNoEndRecord; |
| 120 | | |
| 121 | | if (record_len > loaded_len) { |
| 122 | | const new_loaded_len = @min(loaded_len + 300, record_len_max); |
| 123 | | const read_len = new_loaded_len - loaded_len; |
| 124 | | |
| 125 | | try seekable_stream.seekTo(stream_len - @as(u64, new_loaded_len)); |
| 126 | | const read_buf: []u8 = buf[buf.len - new_loaded_len ..][0..read_len]; |
| 127 | | const len = try seekable_stream.context.reader().readAll(read_buf); |
| 128 | | if (len != read_len) |
| 129 | | return error.ZipTruncated; |
| 130 | | loaded_len = new_loaded_len; |
| 131 | | } |
| 103 | pub const FindBufferError = error{ ZipNoEndRecord, ZipTruncated }; |
| 132 | 104 | |
| 133 | | const record_bytes = buf[buf.len - record_len ..][0..@sizeOf(EndRecord)]; |
| 134 | | if (std.mem.eql(u8, record_bytes[0..4], &end_record_sig) and |
| 135 | | std.mem.readInt(u16, record_bytes[20..22], .little) == comment_len) |
| 136 | | { |
| 137 | | const record: *align(1) EndRecord = @ptrCast(record_bytes.ptr); |
| 138 | | if (builtin.target.cpu.arch.endian() != .little) { |
| 139 | | std.mem.byteSwapAllFields(@TypeOf(record.*), record); |
| 105 | /// TODO audit this logic |
| 106 | pub fn findBuffer(buffer: []const u8) FindBufferError!EndRecord { |
| 107 | const pos = std.mem.lastIndexOf(u8, buffer, &end_record_sig) orelse return error.ZipNoEndRecord; |
| 108 | if (pos + @sizeOf(EndRecord) > buffer.len) return error.EndOfStream; |
| 109 | const record_ptr: *EndRecord = @ptrCast(buffer[pos..][0..@sizeOf(EndRecord)]); |
| 110 | var record = record_ptr.*; |
| 111 | if (!is_le) std.mem.byteSwapAllFields(EndRecord, &record); |
| 112 | return record; |
| 113 | } |
| 114 | |
| 115 | pub const FindFileError = File.GetEndPosError || File.SeekError || error{ |
| 116 | ZipNoEndRecord, |
| 117 | EndOfStream, |
| 118 | }; |
| 119 | |
| 120 | pub fn findFile(fr: *File.Reader) FindFileError!EndRecord { |
| 121 | const end_pos = try fr.getSize(); |
| 122 | |
| 123 | var buf: [@sizeOf(EndRecord) + std.math.maxInt(u16)]u8 = undefined; |
| 124 | const record_len_max = @min(end_pos, buf.len); |
| 125 | var loaded_len: u32 = 0; |
| 126 | var comment_len: u16 = 0; |
| 127 | while (true) { |
| 128 | const record_len: u32 = @as(u32, comment_len) + @sizeOf(EndRecord); |
| 129 | if (record_len > record_len_max) |
| 130 | return error.ZipNoEndRecord; |
| 131 | |
| 132 | if (record_len > loaded_len) { |
| 133 | const new_loaded_len = @min(loaded_len + 300, record_len_max); |
| 134 | const read_len = new_loaded_len - loaded_len; |
| 135 | |
| 136 | try fr.seekTo(end_pos - @as(u64, new_loaded_len)); |
| 137 | const read_buf: []u8 = buf[buf.len - new_loaded_len ..][0..read_len]; |
| 138 | var br = fr.interface().unbuffered(); |
| 139 | br.readSlice(read_buf) catch |err| switch (err) { |
| 140 | error.ReadFailed => return fr.err.?, |
| 141 | }; |
| 142 | loaded_len = new_loaded_len; |
| 143 | } |
| 144 | |
| 145 | const record_bytes = buf[buf.len - record_len ..][0..@sizeOf(EndRecord)]; |
| 146 | if (std.mem.eql(u8, record_bytes[0..4], &end_record_sig) and |
| 147 | std.mem.readInt(u16, record_bytes[20..22], .little) == comment_len) |
| 148 | { |
| 149 | const record: *align(1) EndRecord = @ptrCast(record_bytes.ptr); |
| 150 | if (!is_le) std.mem.byteSwapAllFields(EndRecord, record); |
| 151 | return record.*; |
| 140 | 152 | } |
| 141 | | return record.*; |
| 142 | | } |
| 143 | 153 | |
| 144 | | if (comment_len == std.math.maxInt(u16)) |
| 145 | | return error.ZipNoEndRecord; |
| 146 | | comment_len += 1; |
| 154 | if (comment_len == std.math.maxInt(u16)) |
| 155 | return error.ZipNoEndRecord; |
| 156 | comment_len += 1; |
| 157 | } |
| 147 | 158 | } |
| 148 | | } |
| 159 | }; |
| 149 | 160 | |
| 150 | 161 | /// Decompresses the given data from `reader` into `writer`. Stops early if more |
| 151 | 162 | /// than `uncompressed_size` bytes are processed and verifies that exactly that |
| ... | ... | @@ -248,319 +259,322 @@ fn readZip64FileExtents(comptime T: type, header: T, extents: *FileExtents, data |
| 248 | 259 | } |
| 249 | 260 | } |
| 250 | 261 | |
| 251 | | pub fn Iterator(comptime SeekableStream: type) type { |
| 252 | | return struct { |
| 253 | | stream: SeekableStream, |
| 254 | | |
| 255 | | cd_record_count: u64, |
| 256 | | cd_zip_offset: u64, |
| 257 | | cd_size: u64, |
| 262 | pub const Iterator = struct { |
| 263 | input: *File.Reader, |
| 258 | 264 | |
| 259 | | cd_record_index: u64 = 0, |
| 260 | | cd_record_offset: u64 = 0, |
| 265 | cd_record_count: u64, |
| 266 | cd_zip_offset: u64, |
| 267 | cd_size: u64, |
| 261 | 268 | |
| 262 | | const Self = @This(); |
| 269 | cd_record_index: u64 = 0, |
| 270 | cd_record_offset: u64 = 0, |
| 263 | 271 | |
| 264 | | pub fn init(stream: SeekableStream) !Self { |
| 265 | | const stream_len = try stream.getEndPos(); |
| 272 | pub fn init(input: *File.Reader) !Iterator { |
| 273 | const end_record = try EndRecord.findFile(input); |
| 266 | 274 | |
| 267 | | const end_record = try findEndRecord(stream, stream_len); |
| 275 | if (!isMaxInt(end_record.record_count_disk) and end_record.record_count_disk > end_record.record_count_total) |
| 276 | return error.ZipDiskRecordCountTooLarge; |
| 268 | 277 | |
| 269 | | if (!isMaxInt(end_record.record_count_disk) and end_record.record_count_disk > end_record.record_count_total) |
| 270 | | return error.ZipDiskRecordCountTooLarge; |
| 278 | if (end_record.disk_number != 0 or end_record.central_directory_disk_number != 0) |
| 279 | return error.ZipMultiDiskUnsupported; |
| 271 | 280 | |
| 272 | | if (end_record.disk_number != 0 or end_record.central_directory_disk_number != 0) |
| 273 | | return error.ZipMultiDiskUnsupported; |
| 274 | | |
| 275 | | { |
| 276 | | const counts_valid = !isMaxInt(end_record.record_count_disk) and !isMaxInt(end_record.record_count_total); |
| 277 | | if (counts_valid and end_record.record_count_disk != end_record.record_count_total) |
| 278 | | return error.ZipMultiDiskUnsupported; |
| 279 | | } |
| 280 | | |
| 281 | | var result = Self{ |
| 282 | | .stream = stream, |
| 283 | | .cd_record_count = end_record.record_count_total, |
| 284 | | .cd_zip_offset = end_record.central_directory_offset, |
| 285 | | .cd_size = end_record.central_directory_size, |
| 286 | | }; |
| 287 | | if (!end_record.need_zip64()) return result; |
| 288 | | |
| 289 | | const locator_end_offset: u64 = @as(u64, end_record.comment_len) + @sizeOf(EndRecord) + @sizeOf(EndLocator64); |
| 290 | | if (locator_end_offset > stream_len) |
| 291 | | return error.ZipTruncated; |
| 292 | | try stream.seekTo(stream_len - locator_end_offset); |
| 293 | | const locator = try stream.context.reader().readStructEndian(EndLocator64, .little); |
| 294 | | if (!std.mem.eql(u8, &locator.signature, &end_locator64_sig)) |
| 295 | | return error.ZipBadLocatorSig; |
| 296 | | if (locator.zip64_disk_count != 0) |
| 297 | | return error.ZipUnsupportedZip64DiskCount; |
| 298 | | if (locator.total_disk_count != 1) |
| 281 | { |
| 282 | const counts_valid = !isMaxInt(end_record.record_count_disk) and !isMaxInt(end_record.record_count_total); |
| 283 | if (counts_valid and end_record.record_count_disk != end_record.record_count_total) |
| 299 | 284 | return error.ZipMultiDiskUnsupported; |
| 285 | } |
| 300 | 286 | |
| 301 | | try stream.seekTo(locator.record_file_offset); |
| 302 | | |
| 303 | | const record64 = try stream.context.reader().readStructEndian(EndRecord64, .little); |
| 287 | var result: Iterator = .{ |
| 288 | .input = input, |
| 289 | .cd_record_count = end_record.record_count_total, |
| 290 | .cd_zip_offset = end_record.central_directory_offset, |
| 291 | .cd_size = end_record.central_directory_size, |
| 292 | }; |
| 293 | if (!end_record.need_zip64()) return result; |
| 304 | 294 | |
| 305 | | if (!std.mem.eql(u8, &record64.signature, &end_record64_sig)) |
| 306 | | return error.ZipBadEndRecord64Sig; |
| 295 | const locator_end_offset: u64 = @as(u64, end_record.comment_len) + @sizeOf(EndRecord) + @sizeOf(EndLocator64); |
| 296 | const stream_len = try input.getSize(); |
| 307 | 297 | |
| 308 | | if (record64.end_record_size < @sizeOf(EndRecord64) - 12) |
| 309 | | return error.ZipEndRecord64SizeTooSmall; |
| 310 | | if (record64.end_record_size > @sizeOf(EndRecord64) - 12) |
| 311 | | return error.ZipEndRecord64UnhandledExtraData; |
| 298 | if (locator_end_offset > stream_len) |
| 299 | return error.ZipTruncated; |
| 300 | try input.seekTo(stream_len - locator_end_offset); |
| 301 | var br = input.interface().unbuffered(); |
| 302 | const locator = br.readStructEndian(EndLocator64, .little) catch |err| switch (err) { |
| 303 | error.ReadFailed => return input.err.?, |
| 304 | }; |
| 305 | if (!std.mem.eql(u8, &locator.signature, &end_locator64_sig)) |
| 306 | return error.ZipBadLocatorSig; |
| 307 | if (locator.zip64_disk_count != 0) |
| 308 | return error.ZipUnsupportedZip64DiskCount; |
| 309 | if (locator.total_disk_count != 1) |
| 310 | return error.ZipMultiDiskUnsupported; |
| 312 | 311 | |
| 313 | | if (record64.version_needed_to_extract > 45) |
| 314 | | return error.ZipUnsupportedVersion; |
| 312 | try input.seekTo(locator.record_file_offset); |
| 315 | 313 | |
| 316 | | { |
| 317 | | const is_multidisk = record64.disk_number != 0 or |
| 318 | | record64.central_directory_disk_number != 0 or |
| 319 | | record64.record_count_disk != record64.record_count_total; |
| 320 | | if (is_multidisk) |
| 321 | | return error.ZipMultiDiskUnsupported; |
| 322 | | } |
| 314 | const record64 = br.readStructEndian(EndRecord64, .little) catch |err| switch (err) { |
| 315 | error.ReadFailed => return input.err.?, |
| 316 | }; |
| 323 | 317 | |
| 324 | | if (isMaxInt(end_record.record_count_total)) { |
| 325 | | result.cd_record_count = record64.record_count_total; |
| 326 | | } else if (end_record.record_count_total != record64.record_count_total) |
| 327 | | return error.Zip64RecordCountTotalMismatch; |
| 318 | if (!std.mem.eql(u8, &record64.signature, &end_record64_sig)) |
| 319 | return error.ZipBadEndRecord64Sig; |
| 328 | 320 | |
| 329 | | if (isMaxInt(end_record.central_directory_offset)) { |
| 330 | | result.cd_zip_offset = record64.central_directory_offset; |
| 331 | | } else if (end_record.central_directory_offset != record64.central_directory_offset) |
| 332 | | return error.Zip64CentralDirectoryOffsetMismatch; |
| 321 | if (record64.end_record_size < @sizeOf(EndRecord64) - 12) |
| 322 | return error.ZipEndRecord64SizeTooSmall; |
| 323 | if (record64.end_record_size > @sizeOf(EndRecord64) - 12) |
| 324 | return error.ZipEndRecord64UnhandledExtraData; |
| 333 | 325 | |
| 334 | | if (isMaxInt(end_record.central_directory_size)) { |
| 335 | | result.cd_size = record64.central_directory_size; |
| 336 | | } else if (end_record.central_directory_size != record64.central_directory_size) |
| 337 | | return error.Zip64CentralDirectorySizeMismatch; |
| 326 | if (record64.version_needed_to_extract > 45) |
| 327 | return error.ZipUnsupportedVersion; |
| 338 | 328 | |
| 339 | | return result; |
| 329 | { |
| 330 | const is_multidisk = record64.disk_number != 0 or |
| 331 | record64.central_directory_disk_number != 0 or |
| 332 | record64.record_count_disk != record64.record_count_total; |
| 333 | if (is_multidisk) |
| 334 | return error.ZipMultiDiskUnsupported; |
| 340 | 335 | } |
| 341 | 336 | |
| 342 | | pub fn next(self: *Self) !?Entry { |
| 343 | | if (self.cd_record_index == self.cd_record_count) { |
| 344 | | if (self.cd_record_offset != self.cd_size) |
| 345 | | return if (self.cd_size > self.cd_record_offset) |
| 346 | | error.ZipCdOversized |
| 347 | | else |
| 348 | | error.ZipCdUndersized; |
| 337 | if (isMaxInt(end_record.record_count_total)) { |
| 338 | result.cd_record_count = record64.record_count_total; |
| 339 | } else if (end_record.record_count_total != record64.record_count_total) |
| 340 | return error.Zip64RecordCountTotalMismatch; |
| 349 | 341 | |
| 350 | | return null; |
| 351 | | } |
| 342 | if (isMaxInt(end_record.central_directory_offset)) { |
| 343 | result.cd_zip_offset = record64.central_directory_offset; |
| 344 | } else if (end_record.central_directory_offset != record64.central_directory_offset) |
| 345 | return error.Zip64CentralDirectoryOffsetMismatch; |
| 352 | 346 | |
| 353 | | const header_zip_offset = self.cd_zip_offset + self.cd_record_offset; |
| 354 | | try self.stream.seekTo(header_zip_offset); |
| 355 | | const header = try self.stream.context.reader().readStructEndian(CentralDirectoryFileHeader, .little); |
| 356 | | if (!std.mem.eql(u8, &header.signature, &central_file_header_sig)) |
| 357 | | return error.ZipBadCdOffset; |
| 347 | if (isMaxInt(end_record.central_directory_size)) { |
| 348 | result.cd_size = record64.central_directory_size; |
| 349 | } else if (end_record.central_directory_size != record64.central_directory_size) |
| 350 | return error.Zip64CentralDirectorySizeMismatch; |
| 358 | 351 | |
| 359 | | self.cd_record_index += 1; |
| 360 | | self.cd_record_offset += @sizeOf(CentralDirectoryFileHeader) + header.filename_len + header.extra_len + header.comment_len; |
| 352 | return result; |
| 353 | } |
| 361 | 354 | |
| 362 | | // Note: checking the version_needed_to_extract doesn't seem to be helpful, i.e. the zip file |
| 363 | | // at https://github.com/ninja-build/ninja/releases/download/v1.12.0/ninja-linux.zip |
| 364 | | // has an undocumented version 788 but extracts just fine. |
| 355 | pub fn next(self: *Iterator) !?Entry { |
| 356 | if (self.cd_record_index == self.cd_record_count) { |
| 357 | if (self.cd_record_offset != self.cd_size) |
| 358 | return if (self.cd_size > self.cd_record_offset) |
| 359 | error.ZipCdOversized |
| 360 | else |
| 361 | error.ZipCdUndersized; |
| 365 | 362 | |
| 366 | | if (header.flags.encrypted) |
| 367 | | return error.ZipEncryptionUnsupported; |
| 368 | | // TODO: check/verify more flags |
| 369 | | if (header.disk_number != 0) |
| 370 | | return error.ZipMultiDiskUnsupported; |
| 363 | return null; |
| 364 | } |
| 371 | 365 | |
| 372 | | var extents: FileExtents = .{ |
| 373 | | .uncompressed_size = header.uncompressed_size, |
| 374 | | .compressed_size = header.compressed_size, |
| 375 | | .local_file_header_offset = header.local_file_header_offset, |
| 376 | | }; |
| 366 | const header_zip_offset = self.cd_zip_offset + self.cd_record_offset; |
| 367 | const input = self.input; |
| 368 | try input.seekTo(header_zip_offset); |
| 369 | var br = input.interface().unbuffered(); |
| 370 | const header = br.readStructEndian(CentralDirectoryFileHeader, .little) catch |err| switch (err) { |
| 371 | error.ReadFailed => return input.err.?, |
| 372 | }; |
| 373 | if (!std.mem.eql(u8, &header.signature, &central_file_header_sig)) |
| 374 | return error.ZipBadCdOffset; |
| 375 | |
| 376 | self.cd_record_index += 1; |
| 377 | self.cd_record_offset += @sizeOf(CentralDirectoryFileHeader) + header.filename_len + header.extra_len + header.comment_len; |
| 378 | |
| 379 | // Note: checking the version_needed_to_extract doesn't seem to be helpful, i.e. the zip file |
| 380 | // at https://github.com/ninja-build/ninja/releases/download/v1.12.0/ninja-linux.zip |
| 381 | // has an undocumented version 788 but extracts just fine. |
| 382 | |
| 383 | if (header.flags.encrypted) |
| 384 | return error.ZipEncryptionUnsupported; |
| 385 | // TODO: check/verify more flags |
| 386 | if (header.disk_number != 0) |
| 387 | return error.ZipMultiDiskUnsupported; |
| 388 | |
| 389 | var extents: FileExtents = .{ |
| 390 | .uncompressed_size = header.uncompressed_size, |
| 391 | .compressed_size = header.compressed_size, |
| 392 | .local_file_header_offset = header.local_file_header_offset, |
| 393 | }; |
| 377 | 394 | |
| 378 | | if (header.extra_len > 0) { |
| 379 | | var extra_buf: [std.math.maxInt(u16)]u8 = undefined; |
| 380 | | const extra = extra_buf[0..header.extra_len]; |
| 395 | if (header.extra_len > 0) { |
| 396 | var extra_buf: [std.math.maxInt(u16)]u8 = undefined; |
| 397 | const extra = extra_buf[0..header.extra_len]; |
| 381 | 398 | |
| 382 | | { |
| 383 | | try self.stream.seekTo(header_zip_offset + @sizeOf(CentralDirectoryFileHeader) + header.filename_len); |
| 384 | | const len = try self.stream.context.reader().readAll(extra); |
| 385 | | if (len != extra.len) |
| 386 | | return error.ZipTruncated; |
| 387 | | } |
| 399 | try input.seekTo(header_zip_offset + @sizeOf(CentralDirectoryFileHeader) + header.filename_len); |
| 400 | br.readSlice(extra) catch |err| switch (err) { |
| 401 | error.ReadFailed => return input.err.?, |
| 402 | }; |
| 388 | 403 | |
| 389 | | var extra_offset: usize = 0; |
| 390 | | while (extra_offset + 4 <= extra.len) { |
| 391 | | const header_id = std.mem.readInt(u16, extra[extra_offset..][0..2], .little); |
| 392 | | const data_size = std.mem.readInt(u16, extra[extra_offset..][2..4], .little); |
| 393 | | const end = extra_offset + 4 + data_size; |
| 394 | | if (end > extra.len) |
| 395 | | return error.ZipBadExtraFieldSize; |
| 396 | | const data = extra[extra_offset + 4 .. end]; |
| 397 | | switch (@as(ExtraHeader, @enumFromInt(header_id))) { |
| 398 | | .zip64_info => try readZip64FileExtents(CentralDirectoryFileHeader, header, &extents, data), |
| 399 | | else => {}, // ignore |
| 400 | | } |
| 401 | | extra_offset = end; |
| 404 | var extra_offset: usize = 0; |
| 405 | while (extra_offset + 4 <= extra.len) { |
| 406 | const header_id = std.mem.readInt(u16, extra[extra_offset..][0..2], .little); |
| 407 | const data_size = std.mem.readInt(u16, extra[extra_offset..][2..4], .little); |
| 408 | const end = extra_offset + 4 + data_size; |
| 409 | if (end > extra.len) |
| 410 | return error.ZipBadExtraFieldSize; |
| 411 | const data = extra[extra_offset + 4 .. end]; |
| 412 | switch (@as(ExtraHeader, @enumFromInt(header_id))) { |
| 413 | .zip64_info => try readZip64FileExtents(CentralDirectoryFileHeader, header, &extents, data), |
| 414 | else => {}, // ignore |
| 402 | 415 | } |
| 416 | extra_offset = end; |
| 403 | 417 | } |
| 404 | | |
| 405 | | return .{ |
| 406 | | .version_needed_to_extract = header.version_needed_to_extract, |
| 407 | | .flags = header.flags, |
| 408 | | .compression_method = header.compression_method, |
| 409 | | .last_modification_time = header.last_modification_time, |
| 410 | | .last_modification_date = header.last_modification_date, |
| 411 | | .header_zip_offset = header_zip_offset, |
| 412 | | .crc32 = header.crc32, |
| 413 | | .filename_len = header.filename_len, |
| 414 | | .compressed_size = extents.compressed_size, |
| 415 | | .uncompressed_size = extents.uncompressed_size, |
| 416 | | .file_offset = extents.local_file_header_offset, |
| 417 | | }; |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | | pub const Entry = struct { |
| 421 | | version_needed_to_extract: u16, |
| 422 | | flags: GeneralPurposeFlags, |
| 423 | | compression_method: CompressionMethod, |
| 424 | | last_modification_time: u16, |
| 425 | | last_modification_date: u16, |
| 426 | | header_zip_offset: u64, |
| 427 | | crc32: u32, |
| 428 | | filename_len: u32, |
| 429 | | compressed_size: u64, |
| 430 | | uncompressed_size: u64, |
| 431 | | file_offset: u64, |
| 432 | | |
| 433 | | pub fn extract( |
| 434 | | self: Entry, |
| 435 | | stream: SeekableStream, |
| 436 | | options: ExtractOptions, |
| 437 | | filename_buf: []u8, |
| 438 | | dest: std.fs.Dir, |
| 439 | | ) !u32 { |
| 440 | | if (filename_buf.len < self.filename_len) |
| 441 | | return error.ZipInsufficientBuffer; |
| 442 | | const filename = filename_buf[0..self.filename_len]; |
| 443 | | |
| 444 | | try stream.seekTo(self.header_zip_offset + @sizeOf(CentralDirectoryFileHeader)); |
| 445 | | |
| 446 | | { |
| 447 | | const len = try stream.context.reader().readAll(filename); |
| 448 | | if (len != filename.len) |
| 449 | | return error.ZipBadFileOffset; |
| 450 | | } |
| 420 | return .{ |
| 421 | .version_needed_to_extract = header.version_needed_to_extract, |
| 422 | .flags = header.flags, |
| 423 | .compression_method = header.compression_method, |
| 424 | .last_modification_time = header.last_modification_time, |
| 425 | .last_modification_date = header.last_modification_date, |
| 426 | .header_zip_offset = header_zip_offset, |
| 427 | .crc32 = header.crc32, |
| 428 | .filename_len = header.filename_len, |
| 429 | .compressed_size = extents.compressed_size, |
| 430 | .uncompressed_size = extents.uncompressed_size, |
| 431 | .file_offset = extents.local_file_header_offset, |
| 432 | }; |
| 433 | } |
| 451 | 434 | |
| 452 | | const local_data_header_offset: u64 = local_data_header_offset: { |
| 453 | | const local_header = blk: { |
| 454 | | try stream.seekTo(self.file_offset); |
| 455 | | break :blk try stream.context.reader().readStructEndian(LocalFileHeader, .little); |
| 456 | | }; |
| 457 | | if (!std.mem.eql(u8, &local_header.signature, &local_file_header_sig)) |
| 458 | | return error.ZipBadFileOffset; |
| 459 | | if (local_header.version_needed_to_extract != self.version_needed_to_extract) |
| 460 | | return error.ZipMismatchVersionNeeded; |
| 461 | | if (local_header.last_modification_time != self.last_modification_time) |
| 462 | | return error.ZipMismatchModTime; |
| 463 | | if (local_header.last_modification_date != self.last_modification_date) |
| 464 | | return error.ZipMismatchModDate; |
| 465 | | |
| 466 | | if (@as(u16, @bitCast(local_header.flags)) != @as(u16, @bitCast(self.flags))) |
| 467 | | return error.ZipMismatchFlags; |
| 468 | | if (local_header.crc32 != 0 and local_header.crc32 != self.crc32) |
| 469 | | return error.ZipMismatchCrc32; |
| 470 | | var extents: FileExtents = .{ |
| 471 | | .uncompressed_size = local_header.uncompressed_size, |
| 472 | | .compressed_size = local_header.compressed_size, |
| 473 | | .local_file_header_offset = 0, |
| 474 | | }; |
| 475 | | if (local_header.extra_len > 0) { |
| 476 | | var extra_buf: [std.math.maxInt(u16)]u8 = undefined; |
| 477 | | const extra = extra_buf[0..local_header.extra_len]; |
| 478 | | |
| 479 | | { |
| 480 | | try stream.seekTo(self.file_offset + @sizeOf(LocalFileHeader) + local_header.filename_len); |
| 481 | | const len = try stream.context.reader().readAll(extra); |
| 482 | | if (len != extra.len) |
| 483 | | return error.ZipTruncated; |
| 484 | | } |
| 435 | pub const Entry = struct { |
| 436 | version_needed_to_extract: u16, |
| 437 | flags: GeneralPurposeFlags, |
| 438 | compression_method: CompressionMethod, |
| 439 | last_modification_time: u16, |
| 440 | last_modification_date: u16, |
| 441 | header_zip_offset: u64, |
| 442 | crc32: u32, |
| 443 | filename_len: u32, |
| 444 | compressed_size: u64, |
| 445 | uncompressed_size: u64, |
| 446 | file_offset: u64, |
| 447 | |
| 448 | pub fn extract( |
| 449 | self: Entry, |
| 450 | stream: *File.Reader, |
| 451 | options: ExtractOptions, |
| 452 | filename_buf: []u8, |
| 453 | dest: std.fs.Dir, |
| 454 | ) !u32 { |
| 455 | if (filename_buf.len < self.filename_len) |
| 456 | return error.ZipInsufficientBuffer; |
| 457 | const filename = filename_buf[0..self.filename_len]; |
| 458 | |
| 459 | try stream.seekTo(self.header_zip_offset + @sizeOf(CentralDirectoryFileHeader)); |
| 485 | 460 | |
| 486 | | var extra_offset: usize = 0; |
| 487 | | while (extra_offset + 4 <= local_header.extra_len) { |
| 488 | | const header_id = std.mem.readInt(u16, extra[extra_offset..][0..2], .little); |
| 489 | | const data_size = std.mem.readInt(u16, extra[extra_offset..][2..4], .little); |
| 490 | | const end = extra_offset + 4 + data_size; |
| 491 | | if (end > local_header.extra_len) |
| 492 | | return error.ZipBadExtraFieldSize; |
| 493 | | const data = extra[extra_offset + 4 .. end]; |
| 494 | | switch (@as(ExtraHeader, @enumFromInt(header_id))) { |
| 495 | | .zip64_info => try readZip64FileExtents(LocalFileHeader, local_header, &extents, data), |
| 496 | | else => {}, // ignore |
| 497 | | } |
| 498 | | extra_offset = end; |
| 461 | { |
| 462 | const len = try stream.context.reader().readAll(filename); |
| 463 | if (len != filename.len) |
| 464 | return error.ZipBadFileOffset; |
| 465 | } |
| 466 | |
| 467 | const local_data_header_offset: u64 = local_data_header_offset: { |
| 468 | const local_header = blk: { |
| 469 | try stream.seekTo(self.file_offset); |
| 470 | break :blk try stream.context.reader().readStructEndian(LocalFileHeader, .little); |
| 471 | }; |
| 472 | if (!std.mem.eql(u8, &local_header.signature, &local_file_header_sig)) |
| 473 | return error.ZipBadFileOffset; |
| 474 | if (local_header.version_needed_to_extract != self.version_needed_to_extract) |
| 475 | return error.ZipMismatchVersionNeeded; |
| 476 | if (local_header.last_modification_time != self.last_modification_time) |
| 477 | return error.ZipMismatchModTime; |
| 478 | if (local_header.last_modification_date != self.last_modification_date) |
| 479 | return error.ZipMismatchModDate; |
| 480 | |
| 481 | if (@as(u16, @bitCast(local_header.flags)) != @as(u16, @bitCast(self.flags))) |
| 482 | return error.ZipMismatchFlags; |
| 483 | if (local_header.crc32 != 0 and local_header.crc32 != self.crc32) |
| 484 | return error.ZipMismatchCrc32; |
| 485 | var extents: FileExtents = .{ |
| 486 | .uncompressed_size = local_header.uncompressed_size, |
| 487 | .compressed_size = local_header.compressed_size, |
| 488 | .local_file_header_offset = 0, |
| 489 | }; |
| 490 | if (local_header.extra_len > 0) { |
| 491 | var extra_buf: [std.math.maxInt(u16)]u8 = undefined; |
| 492 | const extra = extra_buf[0..local_header.extra_len]; |
| 493 | |
| 494 | { |
| 495 | try stream.seekTo(self.file_offset + @sizeOf(LocalFileHeader) + local_header.filename_len); |
| 496 | const len = try stream.context.reader().readAll(extra); |
| 497 | if (len != extra.len) |
| 498 | return error.ZipTruncated; |
| 499 | } |
| 500 | |
| 501 | var extra_offset: usize = 0; |
| 502 | while (extra_offset + 4 <= local_header.extra_len) { |
| 503 | const header_id = std.mem.readInt(u16, extra[extra_offset..][0..2], .little); |
| 504 | const data_size = std.mem.readInt(u16, extra[extra_offset..][2..4], .little); |
| 505 | const end = extra_offset + 4 + data_size; |
| 506 | if (end > local_header.extra_len) |
| 507 | return error.ZipBadExtraFieldSize; |
| 508 | const data = extra[extra_offset + 4 .. end]; |
| 509 | switch (@as(ExtraHeader, @enumFromInt(header_id))) { |
| 510 | .zip64_info => try readZip64FileExtents(LocalFileHeader, local_header, &extents, data), |
| 511 | else => {}, // ignore |
| 499 | 512 | } |
| 513 | extra_offset = end; |
| 500 | 514 | } |
| 515 | } |
| 501 | 516 | |
| 502 | | if (extents.compressed_size != 0 and |
| 503 | | extents.compressed_size != self.compressed_size) |
| 504 | | return error.ZipMismatchCompLen; |
| 505 | | if (extents.uncompressed_size != 0 and |
| 506 | | extents.uncompressed_size != self.uncompressed_size) |
| 507 | | return error.ZipMismatchUncompLen; |
| 517 | if (extents.compressed_size != 0 and |
| 518 | extents.compressed_size != self.compressed_size) |
| 519 | return error.ZipMismatchCompLen; |
| 520 | if (extents.uncompressed_size != 0 and |
| 521 | extents.uncompressed_size != self.uncompressed_size) |
| 522 | return error.ZipMismatchUncompLen; |
| 508 | 523 | |
| 509 | | if (local_header.filename_len != self.filename_len) |
| 510 | | return error.ZipMismatchFilenameLen; |
| 524 | if (local_header.filename_len != self.filename_len) |
| 525 | return error.ZipMismatchFilenameLen; |
| 511 | 526 | |
| 512 | | break :local_data_header_offset @as(u64, local_header.filename_len) + |
| 513 | | @as(u64, local_header.extra_len); |
| 514 | | }; |
| 527 | break :local_data_header_offset @as(u64, local_header.filename_len) + |
| 528 | @as(u64, local_header.extra_len); |
| 529 | }; |
| 515 | 530 | |
| 516 | | if (isBadFilename(filename)) |
| 517 | | return error.ZipBadFilename; |
| 531 | if (isBadFilename(filename)) |
| 532 | return error.ZipBadFilename; |
| 518 | 533 | |
| 519 | | if (options.allow_backslashes) { |
| 520 | | std.mem.replaceScalar(u8, filename, '\\', '/'); |
| 521 | | } else { |
| 522 | | if (std.mem.indexOfScalar(u8, filename, '\\')) |_| |
| 523 | | return error.ZipFilenameHasBackslash; |
| 524 | | } |
| 534 | if (options.allow_backslashes) { |
| 535 | std.mem.replaceScalar(u8, filename, '\\', '/'); |
| 536 | } else { |
| 537 | if (std.mem.indexOfScalar(u8, filename, '\\')) |_| |
| 538 | return error.ZipFilenameHasBackslash; |
| 539 | } |
| 525 | 540 | |
| 526 | | // All entries that end in '/' are directories |
| 527 | | if (filename[filename.len - 1] == '/') { |
| 528 | | if (self.uncompressed_size != 0) |
| 529 | | return error.ZipBadDirectorySize; |
| 530 | | try dest.makePath(filename[0 .. filename.len - 1]); |
| 531 | | return std.hash.Crc32.hash(&.{}); |
| 532 | | } |
| 541 | // All entries that end in '/' are directories |
| 542 | if (filename[filename.len - 1] == '/') { |
| 543 | if (self.uncompressed_size != 0) |
| 544 | return error.ZipBadDirectorySize; |
| 545 | try dest.makePath(filename[0 .. filename.len - 1]); |
| 546 | return std.hash.Crc32.hash(&.{}); |
| 547 | } |
| 533 | 548 | |
| 534 | | const out_file = blk: { |
| 535 | | if (std.fs.path.dirname(filename)) |dirname| { |
| 536 | | var parent_dir = try dest.makeOpenPath(dirname, .{}); |
| 537 | | defer parent_dir.close(); |
| 549 | const out_file = blk: { |
| 550 | if (std.fs.path.dirname(filename)) |dirname| { |
| 551 | var parent_dir = try dest.makeOpenPath(dirname, .{}); |
| 552 | defer parent_dir.close(); |
| 538 | 553 | |
| 539 | | const basename = std.fs.path.basename(filename); |
| 540 | | break :blk try parent_dir.createFile(basename, .{ .exclusive = true }); |
| 541 | | } |
| 542 | | break :blk try dest.createFile(filename, .{ .exclusive = true }); |
| 543 | | }; |
| 544 | | defer out_file.close(); |
| 545 | | const local_data_file_offset: u64 = |
| 546 | | @as(u64, self.file_offset) + |
| 547 | | @as(u64, @sizeOf(LocalFileHeader)) + |
| 548 | | local_data_header_offset; |
| 549 | | try stream.seekTo(local_data_file_offset); |
| 550 | | var compressed_remaining: u64 = self.compressed_size; |
| 551 | | const crc = try decompress( |
| 552 | | self.compression_method, |
| 553 | | self.uncompressed_size, |
| 554 | | stream.context.reader(), |
| 555 | | out_file.writer(), |
| 556 | | &compressed_remaining, |
| 557 | | ); |
| 558 | | if (compressed_remaining != 0) return error.ZipDecompressTruncated; |
| 559 | | return crc; |
| 560 | | } |
| 561 | | }; |
| 554 | const basename = std.fs.path.basename(filename); |
| 555 | break :blk try parent_dir.createFile(basename, .{ .exclusive = true }); |
| 556 | } |
| 557 | break :blk try dest.createFile(filename, .{ .exclusive = true }); |
| 558 | }; |
| 559 | defer out_file.close(); |
| 560 | const local_data_file_offset: u64 = |
| 561 | @as(u64, self.file_offset) + |
| 562 | @as(u64, @sizeOf(LocalFileHeader)) + |
| 563 | local_data_header_offset; |
| 564 | try stream.seekTo(local_data_file_offset); |
| 565 | var compressed_remaining: u64 = self.compressed_size; |
| 566 | const crc = try decompress( |
| 567 | self.compression_method, |
| 568 | self.uncompressed_size, |
| 569 | stream.context.reader(), |
| 570 | out_file.writer(), |
| 571 | &compressed_remaining, |
| 572 | ); |
| 573 | if (compressed_remaining != 0) return error.ZipDecompressTruncated; |
| 574 | return crc; |
| 575 | } |
| 562 | 576 | }; |
| 563 | | } |
| 577 | }; |
| 564 | 578 | |
| 565 | 579 | // returns true if `filename` starts with `root` followed by a forward slash |
| 566 | 580 | fn filenameInRoot(filename: []const u8, root: []const u8) bool { |
| ... | ... | @@ -609,17 +623,13 @@ pub const ExtractOptions = struct { |
| 609 | 623 | diagnostics: ?*Diagnostics = null, |
| 610 | 624 | }; |
| 611 | 625 | |
| 612 | | /// Extract the zipped files inside `seekable_stream` to the given `dest` directory. |
| 613 | | /// Note that `seekable_stream` must be an instance of `std.io.SeekableStream` and |
| 614 | | /// its context must also have a `.reader()` method that returns an instance of |
| 615 | | /// `std.io.Reader`. |
| 616 | | pub fn extract(dest: std.fs.Dir, seekable_stream: anytype, options: ExtractOptions) !void { |
| 617 | | const SeekableStream = @TypeOf(seekable_stream); |
| 618 | | var iter = try Iterator(SeekableStream).init(seekable_stream); |
| 626 | /// Extract the zipped files to the given `dest` directory. |
| 627 | pub fn extract(dest: std.fs.Dir, fr: *File.Reader, options: ExtractOptions) !void { |
| 628 | var iter = try Iterator.init(fr); |
| 619 | 629 | |
| 620 | 630 | var filename_buf: [std.fs.max_path_bytes]u8 = undefined; |
| 621 | 631 | while (try iter.next()) |entry| { |
| 622 | | const crc32 = try entry.extract(seekable_stream, options, &filename_buf, dest); |
| 632 | const crc32 = try entry.extract(fr, options, &filename_buf, dest); |
| 623 | 633 | if (crc32 != entry.crc32) |
| 624 | 634 | return error.ZipCrcMismatch; |
| 625 | 635 | if (options.diagnostics) |d| { |
| ... | ... | @@ -628,173 +638,6 @@ pub fn extract(dest: std.fs.Dir, seekable_stream: anytype, options: ExtractOptio |
| 628 | 638 | } |
| 629 | 639 | } |
| 630 | 640 | |
| 631 | | fn testZip(options: ExtractOptions, comptime files: []const File, write_opt: testutil.WriteZipOptions) !void { |
| 632 | | var store: [files.len]FileStore = undefined; |
| 633 | | try testZipWithStore(options, files, write_opt, &store); |
| 634 | | } |
| 635 | | fn testZipWithStore( |
| 636 | | options: ExtractOptions, |
| 637 | | test_files: []const File, |
| 638 | | write_opt: testutil.WriteZipOptions, |
| 639 | | store: []FileStore, |
| 640 | | ) !void { |
| 641 | | var zip_buf: [4096]u8 = undefined; |
| 642 | | var fbs = try testutil.makeZipWithStore(&zip_buf, test_files, write_opt, store); |
| 643 | | |
| 644 | | var tmp = testing.tmpDir(.{ .no_follow = true }); |
| 645 | | defer tmp.cleanup(); |
| 646 | | try extract(tmp.dir, fbs.seekableStream(), options); |
| 647 | | try testutil.expectFiles(test_files, tmp.dir, .{}); |
| 648 | | } |
| 649 | | fn testZipError(expected_error: anyerror, file: File, options: ExtractOptions) !void { |
| 650 | | var zip_buf: [4096]u8 = undefined; |
| 651 | | var store: [1]FileStore = undefined; |
| 652 | | var fbs = try testutil.makeZipWithStore(&zip_buf, &[_]File{file}, .{}, &store); |
| 653 | | var tmp = testing.tmpDir(.{ .no_follow = true }); |
| 654 | | defer tmp.cleanup(); |
| 655 | | try testing.expectError(expected_error, extract(tmp.dir, fbs.seekableStream(), options)); |
| 656 | | } |
| 657 | | |
| 658 | | test "zip one file" { |
| 659 | | try testZip(.{}, &[_]File{ |
| 660 | | .{ .name = "onefile.txt", .content = "Just a single file\n", .compression = .store }, |
| 661 | | }, .{}); |
| 662 | | } |
| 663 | | test "zip multiple files" { |
| 664 | | try testZip(.{ .allow_backslashes = true }, &[_]File{ |
| 665 | | .{ .name = "foo", .content = "a foo file\n", .compression = .store }, |
| 666 | | .{ .name = "subdir/bar", .content = "bar is this right?\nanother newline\n", .compression = .store }, |
| 667 | | .{ .name = "subdir\\whoa", .content = "you can do backslashes", .compression = .store }, |
| 668 | | .{ .name = "subdir/another/baz", .content = "bazzy mc bazzerson", .compression = .store }, |
| 669 | | }, .{}); |
| 670 | | } |
| 671 | | test "zip deflated" { |
| 672 | | try testZip(.{}, &[_]File{ |
| 673 | | .{ .name = "deflateme", .content = "This is a deflated file.\nIt should be smaller in the Zip file1\n", .compression = .deflate }, |
| 674 | | // TODO: re-enable this if/when we add support for deflate64 |
| 675 | | //.{ .name = "deflateme64", .content = "The 64k version of deflate!\n", .compression = .deflate64 }, |
| 676 | | .{ .name = "raw", .content = "Not all files need to be deflated in the same Zip.\n", .compression = .store }, |
| 677 | | }, .{}); |
| 678 | | } |
| 679 | | test "zip verify filenames" { |
| 680 | | // no empty filenames |
| 681 | | try testZipError(error.ZipBadFilename, .{ .name = "", .content = "", .compression = .store }, .{}); |
| 682 | | // no absolute paths |
| 683 | | try testZipError(error.ZipBadFilename, .{ .name = "/", .content = "", .compression = .store }, .{}); |
| 684 | | try testZipError(error.ZipBadFilename, .{ .name = "/foo", .content = "", .compression = .store }, .{}); |
| 685 | | try testZipError(error.ZipBadFilename, .{ .name = "/foo/bar", .content = "", .compression = .store }, .{}); |
| 686 | | // no '..' components |
| 687 | | try testZipError(error.ZipBadFilename, .{ .name = "..", .content = "", .compression = .store }, .{}); |
| 688 | | try testZipError(error.ZipBadFilename, .{ .name = "foo/..", .content = "", .compression = .store }, .{}); |
| 689 | | try testZipError(error.ZipBadFilename, .{ .name = "foo/bar/..", .content = "", .compression = .store }, .{}); |
| 690 | | try testZipError(error.ZipBadFilename, .{ .name = "foo/bar/../", .content = "", .compression = .store }, .{}); |
| 691 | | // no backslashes |
| 692 | | try testZipError(error.ZipFilenameHasBackslash, .{ .name = "foo\\bar", .content = "", .compression = .store }, .{}); |
| 693 | | } |
| 694 | | |
| 695 | | test "zip64" { |
| 696 | | const test_files = [_]File{ |
| 697 | | .{ .name = "fram", .content = "fram foo fro fraba", .compression = .store }, |
| 698 | | .{ .name = "subdir/barro", .content = "aljdk;jal;jfd;lajkf", .compression = .store }, |
| 699 | | }; |
| 700 | | |
| 701 | | try testZip(.{}, &test_files, .{ |
| 702 | | .end = .{ |
| 703 | | .zip64 = .{}, |
| 704 | | .record_count_disk = std.math.maxInt(u16), // trigger zip64 |
| 705 | | }, |
| 706 | | }); |
| 707 | | try testZip(.{}, &test_files, .{ |
| 708 | | .end = .{ |
| 709 | | .zip64 = .{}, |
| 710 | | .record_count_total = std.math.maxInt(u16), // trigger zip64 |
| 711 | | }, |
| 712 | | }); |
| 713 | | try testZip(.{}, &test_files, .{ |
| 714 | | .end = .{ |
| 715 | | .zip64 = .{}, |
| 716 | | .record_count_disk = std.math.maxInt(u16), // trigger zip64 |
| 717 | | .record_count_total = std.math.maxInt(u16), // trigger zip64 |
| 718 | | }, |
| 719 | | }); |
| 720 | | try testZip(.{}, &test_files, .{ |
| 721 | | .end = .{ |
| 722 | | .zip64 = .{}, |
| 723 | | .central_directory_size = std.math.maxInt(u32), // trigger zip64 |
| 724 | | }, |
| 725 | | }); |
| 726 | | try testZip(.{}, &test_files, .{ |
| 727 | | .end = .{ |
| 728 | | .zip64 = .{}, |
| 729 | | .central_directory_offset = std.math.maxInt(u32), // trigger zip64 |
| 730 | | }, |
| 731 | | }); |
| 732 | | try testZip(.{}, &test_files, .{ |
| 733 | | .end = .{ |
| 734 | | .zip64 = .{}, |
| 735 | | .central_directory_offset = std.math.maxInt(u32), // trigger zip64 |
| 736 | | }, |
| 737 | | .local_header = .{ |
| 738 | | .zip64 = .{ // trigger local header zip64 |
| 739 | | .data_size = 16, |
| 740 | | }, |
| 741 | | .compressed_size = std.math.maxInt(u32), |
| 742 | | .uncompressed_size = std.math.maxInt(u32), |
| 743 | | .extra_len = 20, |
| 744 | | }, |
| 745 | | }); |
| 746 | | } |
| 747 | | |
| 748 | | test "bad zip files" { |
| 749 | | var tmp = testing.tmpDir(.{ .no_follow = true }); |
| 750 | | defer tmp.cleanup(); |
| 751 | | var zip_buf: [4096]u8 = undefined; |
| 752 | | |
| 753 | | const file_a = [_]File{.{ .name = "a", .content = "", .compression = .store }}; |
| 754 | | |
| 755 | | { |
| 756 | | var fbs = try testutil.makeZip(&zip_buf, &.{}, .{ .end = .{ .sig = [_]u8{ 1, 2, 3, 4 } } }); |
| 757 | | try testing.expectError(error.ZipNoEndRecord, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 758 | | } |
| 759 | | { |
| 760 | | var fbs = try testutil.makeZip(&zip_buf, &.{}, .{ .end = .{ .comment_len = 1 } }); |
| 761 | | try testing.expectError(error.ZipNoEndRecord, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 762 | | } |
| 763 | | { |
| 764 | | var fbs = try testutil.makeZip(&zip_buf, &.{}, .{ .end = .{ .comment = "a", .comment_len = 0 } }); |
| 765 | | try testing.expectError(error.ZipNoEndRecord, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 766 | | } |
| 767 | | { |
| 768 | | var fbs = try testutil.makeZip(&zip_buf, &.{}, .{ .end = .{ .disk_number = 1 } }); |
| 769 | | try testing.expectError(error.ZipMultiDiskUnsupported, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 770 | | } |
| 771 | | { |
| 772 | | var fbs = try testutil.makeZip(&zip_buf, &.{}, .{ .end = .{ .central_directory_disk_number = 1 } }); |
| 773 | | try testing.expectError(error.ZipMultiDiskUnsupported, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 774 | | } |
| 775 | | { |
| 776 | | var fbs = try testutil.makeZip(&zip_buf, &.{}, .{ .end = .{ .record_count_disk = 1 } }); |
| 777 | | try testing.expectError(error.ZipDiskRecordCountTooLarge, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 778 | | } |
| 779 | | { |
| 780 | | var fbs = try testutil.makeZip(&zip_buf, &.{}, .{ .end = .{ .central_directory_size = 1 } }); |
| 781 | | try testing.expectError(error.ZipCdOversized, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 782 | | } |
| 783 | | { |
| 784 | | var fbs = try testutil.makeZip(&zip_buf, &file_a, .{ .end = .{ .central_directory_size = 0 } }); |
| 785 | | try testing.expectError(error.ZipCdUndersized, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 786 | | } |
| 787 | | { |
| 788 | | var fbs = try testutil.makeZip(&zip_buf, &file_a, .{ .end = .{ .central_directory_offset = 0 } }); |
| 789 | | try testing.expectError(error.ZipBadCdOffset, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 790 | | } |
| 791 | | { |
| 792 | | var fbs = try testutil.makeZip(&zip_buf, &file_a, .{ |
| 793 | | .end = .{ |
| 794 | | .zip64 = .{ .locator_sig = [_]u8{ 1, 2, 3, 4 } }, |
| 795 | | .central_directory_size = std.math.maxInt(u32), // trigger 64 |
| 796 | | }, |
| 797 | | }); |
| 798 | | try testing.expectError(error.ZipBadLocatorSig, extract(tmp.dir, fbs.seekableStream(), .{})); |
| 799 | | } |
| 641 | test { |
| 642 | _ = @import("zip/test.zig"); |
| 800 | 643 | } |