// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movb $0x0, 0x20(%rdi)
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x130fc
leaq 0x10(%r15), %rdi
movq %rdi, 0x30(%rsp)
xorl %esi, %esi
callq 0x1cfee
cmpq $0x16, %rbx
jb 0x1c125
leaq -0x16(%rbx), %rax
movl $0xffff, %ecx           # imm = 0xFFFF
cmpq %rcx, %rax
cmovbq %rax, %rcx
negq %rcx
leaq (%r14,%rbx), %rsi
addq $-0x16, %rsi
movl $0x1, %edx
cmpl $0x6054b50, -0x1(%rsi,%rdx) # imm = 0x6054B50
je 0x1c117
decq %rdx
cmpq %rdx, %rcx
jne 0x1c103
jmp 0x1c122
addq %r14, %rax
cmpw $0x0, 0x3(%rdx,%rax)
je 0x1c137
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xf(%rdx,%rax), %r13d
leaq 0x2e(%r13), %rax
movb $0x1, %r12b
cmpq %rbx, %rax
ja 0x1c125
cmpl $0x2014b50, (%r14,%r13) # imm = 0x2014B50
jne 0x1c125
addq %r14, %r13
movl 0x2a(%r13), %ebp
leaq 0x1e(%rbp), %rax
cmpq %rbx, %rax
ja 0x1c122
cmpl $0x4034b50, (%r14,%rbp) # imm = 0x4034B50
jne 0x1c122
movq %rax, 0x28(%rsp)
addq %r14, %rbp
leaq 0x38(%rsp), %rdi
movq %r14, %rdx
movq %rbx, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x1bdd6
leaq 0x8(%rsp), %r12
movq %r12, %rdi
leaq 0x38(%rsp), %rsi
callq 0xa18c
movq 0x8(%r15), %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x13192
cmpb $0x0, 0x17(%rsp)
jns 0x1c1bc
movq 0x8(%rsp), %rdi
callq 0x3430
cmpb $0x0, 0x47(%rsp)
jns 0x1c1cd
movq 0x38(%rsp), %rdi
callq 0x3430
movl 0x18(%r13), %eax
cmpl %eax, 0x16(%rbp)
je 0x1c1db
movb $0x1, 0x20(%r15)
movl %eax, 0x8(%rsp)
movzwl 0xa(%r13), %eax
movw %ax, 0xc(%rsp)
movzwl 0x1a(%rbp), %eax
movq 0x28(%rsp), %rdx
addq %rax, %rdx
movzwl 0x1c(%rbp), %ecx
addq %rdx, %rcx
movl $0x0, %eax
movl $0x0, %r12d
cmpq %rbx, %rcx
ja 0x1c216
movl 0x14(%r13), %eax
addq %r14, %rcx
movq %rcx, %r12
movq $0x0, 0x10(%rsp)
leaq -0x1(%rax), %rcx
bsrq %rcx, %rcx
movl $0x2, %edi
shlq %cl, %rdi
movq %rax, 0x18(%rsp)
cmpq $0x8, %rax
movl $0x8, %eax
cmovbq %rax, %rdi
callq 0x1597c
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rdx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3280
movq 0xc(%r13), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x20(%rsp)
movq 0x18(%r15), %rsi
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x1d090
movzwl 0x1c(%r13), %ecx
movzwl 0x1e(%r13), %edx
movzwl 0x20(%r13), %eax
subq %r14, %r13
addq %rcx, %r13
addq %rdx, %r13
leaq (%rax,%r13), %rcx
addq $0x5c, %rcx
cmpq %rbx, %rcx
ja 0x1c2db
addq %rax, %r13
addq $0x2e, %r13
cmpl $0x2014b50, (%r14,%r13) # imm = 0x2014B50
jne 0x1c2db
addq %r14, %r13
movq 0x10(%rsp), %rdi
callq 0x3430
movl 0x2a(%r13), %ebp
leaq 0x1e(%rbp), %rax
xorl %r12d, %r12d
cmpq %rbx, %rax
jbe 0x1c162
jmp 0x1c125
movq 0x10(%rsp), %rdi
callq 0x3430
movb $0x1, %r12b
jmp 0x1c125
nop