// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movb $0x0, 0x20(%rdi)
callq 0xf426
movq %r15, (%rsp)
leaq 0x10(%r15), %rdi
movq %rdi, 0x10(%rsp)
callq 0x18354
movq %r14, %rsi
movq %rbx, %rdx
callq 0x17474
testq %rax, %rax
je 0x1781f
movl 0x10(%rax), %eax
leaq 0x2e(%rax), %rcx
cmpq %rbx, %rcx
jbe 0x17830
xorl %r13d, %r13d
jmp 0x17843
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%r14,%rax), %rcx
xorl %r13d, %r13d
cmpl $0x2014b50, (%r14,%rax) # imm = 0x2014B50
cmoveq %rcx, %r13
movq %r14, 0x8(%rsp)
testq %r13, %r13
sete %al
je 0x17821
movl 0x2a(%r13), %ebp
leaq 0x1e(%rbp), %r15
cmpq %rbx, %r15
ja 0x17821
cmpl $0x4034b50, (%r14,%rbp) # imm = 0x4034B50
jne 0x17821
addq %r14, %rbp
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %r14, %rdx
movq %rbx, %r14
movq %rbx, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x17548
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x84fc
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0xf338
movq %rbx, %rdi
callq 0x56b4
movq %r12, %rdi
callq 0x56b4
movl 0x18(%r13), %eax
cmpl %eax, 0x16(%rbp)
je 0x178c3
movq (%rsp), %rcx
movb $0x1, 0x20(%rcx)
movl %eax, 0x18(%rsp)
movzwl 0xa(%r13), %eax
movw %ax, 0x1c(%rsp)
movzwl 0x1a(%rbp), %eax
movzwl 0x1c(%rbp), %ecx
addq %rax, %r15
addq %rcx, %r15
movl $0x0, %eax
movl $0x0, %ecx
movq %r14, %rbx
cmpq %r14, %r15
movq 0x8(%rsp), %r14
ja 0x17900
movl 0x14(%r13), %eax
addq %r14, %r15
movq %r15, %rcx
movq %rcx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xbcbe
movq 0xc(%r13), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x30(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x1835c
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x174e6
movq %rax, %r13
movq 0x20(%rsp), %rdi
callq 0x3430
jmp 0x17848
nop