// File: Alcaro[P]bridges/arlib/zip.cpp
// Function: zip::init(arrayview<unsigned char>)
bool zip::init(arrayview<uint8_t> data)
{
	corrupt = false;
	
	filenames.reset();
	filedat.reset();
	
	endofcdr* eod = getendofcdr(data);
	if (!eod) return false;
	
	centdirrec* cdr = getcdr(data, eod);
	while (cdr)
	{
		locfhead* fh = geth(data, cdr);
		if (!fh) return false;
//puts(fh_fname(data, fh, cdr));
//printf("cdr %.8x %.4x %.4x, %.4x %.4x, %.8x %.8x, %.8x %.8x, %.4x %.4x %.4x, %.4x %.4x %.8x %.8x\n",
	//(uint32_t)cdr->signature, (uint16_t)cdr->verused, (uint16_t)cdr->vermin,
	//(uint16_t)cdr->bitflags, (uint16_t)cdr->compmethod,
	//(uint32_t)cdr->moddate, (uint32_t)cdr->crc32,
	//(uint32_t)cdr->size_comp, (uint32_t)cdr->size_decomp,
	//(uint16_t)cdr->len_fname, (uint16_t)cdr->len_exfield, (uint16_t)cdr->len_fcomment,
	//(uint16_t)cdr->disknr, (uint16_t)cdr->attr_int, (uint32_t)cdr->attr_ext, (uint32_t)cdr->header_start);
//printf("fh  %.8x      %.4x, %.4x %.4x, %.8x %.8x, %.8x %.8x, %.4x %.4x\n",
	//(uint32_t)fh->signature, (uint16_t)fh->vermin,
	//(uint16_t)fh->bitflags, (uint16_t)fh->compmethod,
	//(uint32_t)fh->moddate, (uint32_t)fh->crc32,
	//(uint32_t)fh->size_comp, (uint32_t)fh->size_decomp,
	//(uint16_t)fh->len_fname, (uint16_t)fh->len_exfield);
		
		filenames.append(::file::sanitize_rel_path(fh_fname(data, fh, cdr)));
		// the OSX default zipper keeps zeroing half the fields in fh, have to use cdr instead
		if (fh->size_decomp != cdr->size_decomp) corrupt = true;
		file f = { cdr->size_decomp, cdr->compmethod, fh_data(data, fh, cdr), cdr->crc32, cdr->moddate };
		filedat.append(f);
		
		cdr = nextcdr(data, cdr);
	}
	
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rsi, -0x18(%rbp)
movq %rdx, -0x10(%rbp)
movq %rdi, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
movb $0x0, 0x20(%rdi)
callq 0x27b70
movq -0xf0(%rbp), %rdi
addq $0x10, %rdi
callq 0x45970
movq -0xf0(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x42d00
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x43ee3
movb $0x0, -0x1(%rbp)
jmp 0x4411d
movq -0xf0(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rcx
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x42df0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x44119
movq -0xf0(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rcx
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x42fc0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x43f56
movb $0x0, -0x1(%rbp)
jmp 0x4411d
movq -0xf0(%rbp), %rsi
movq -0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x58(%rbp), %r8
movq -0x40(%rbp), %r9
movq -0x98(%rbp), %rdx
movq -0x90(%rbp), %rcx
leaq -0x88(%rbp), %rdi
callq 0x43070
leaq -0x78(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x12370
movq -0xf0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x279c0
leaq -0x78(%rbp), %rdi
callq 0x5870
leaq -0x88(%rbp), %rdi
callq 0x5870
movq -0x58(%rbp), %rdi
addq $0x16, %rdi
callq 0x45940
movl %eax, -0xf4(%rbp)
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
callq 0x45940
movl %eax, %ecx
movl -0xf4(%rbp), %eax
cmpl %ecx, %eax
je 0x44001
movq -0xf0(%rbp), %rax
movb $0x1, 0x20(%rax)
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
callq 0x45940
movl %eax, -0xb8(%rbp)
movq -0x40(%rbp), %rdi
addq $0xa, %rdi
callq 0x45950
movq -0xf0(%rbp), %rdi
movw %ax, -0xb4(%rbp)
leaq -0xb8(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x100(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
callq 0x43d80
movq -0x100(%rbp), %rdi
movq %rax, -0xc8(%rbp)
movq %rdx, -0xc0(%rbp)
leaq -0xc8(%rbp), %rsi
callq 0x1d2f0
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0x45940
movl %eax, -0xa0(%rbp)
movq -0x40(%rbp), %rdi
addq $0xc, %rdi
callq 0x45940
movq -0xf0(%rbp), %rdi
movl %eax, -0x9c(%rbp)
addq $0x10, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x45990
movq -0xf0(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x40(%rbp), %rcx
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
callq 0x42ea0
movq %rax, -0x40(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x459c0
jmp 0x43f0f
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax,%rax)