// File: /workspace/github_cpp_makefile_star2_O1/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 0x12d9e
movq %r15, 0x8(%rsp)
leaq 0x10(%r15), %rdi
movq %rdi, 0x30(%rsp)
xorl %esi, %esi
callq 0x1d6f6
cmpq $0x16, %rbx
jb 0x1c86d
leaq -0x16(%rbx), %rcx
movl $0xffff, %eax           # imm = 0xFFFF
cmpq %rax, %rcx
cmovbq %rcx, %rax
incq %rax
leaq (%r14,%rbx), %rcx
addq $-0x16, %rcx
movl (%rcx), %edx
cmpl $0x6054b50, %edx        # imm = 0x6054B50
jne 0x1c85a
cmpw $0x0, 0x4(%rcx)
movl $0x0, %r12d
cmoveq %rcx, %r12
cmpl $0x6054b50, %edx        # imm = 0x6054B50
je 0x1c86d
decq %rcx
decq %rax
jne 0x1c841
xorl %r12d, %r12d
testq %r12, %r12
je 0x1ca41
movl 0x10(%r12), %eax
leaq 0x2e(%rax), %rcx
cmpq %rbx, %rcx
jbe 0x1c889
xorl %r13d, %r13d
jmp 0x1c89c
leaq (%r14,%rax), %rcx
xorl %r13d, %r13d
cmpl $0x2014b50, (%r14,%rax) # imm = 0x2014B50
cmoveq %rcx, %r13
testq %r13, %r13
sete %al
je 0x1ca43
movl 0x2a(%r13), %ebp
leaq 0x1e(%rbp), %rax
cmpq %rbx, %rax
ja 0x1c8bf
cmpl $0x4034b50, (%r14,%rbp) # imm = 0x4034B50
je 0x1c8c3
xorl %ebp, %ebp
jmp 0x1c8c6
addq %r14, %rbp
testq %rbp, %rbp
je 0x1ca38
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq %r14, %rdx
movq %rbx, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x1c4b4
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x9c7a
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq %r15, %rdx
callq 0x12e34
cmpb $0x0, 0x1f(%rsp)
jns 0x1c91a
movq 0x10(%rsp), %rdi
callq 0x3430
cmpb $0x0, 0x47(%rsp)
jns 0x1c92b
movq 0x38(%rsp), %rdi
callq 0x3430
movl 0x16(%rbp), %eax
cmpl 0x18(%r13), %eax
je 0x1c93d
movq 0x8(%rsp), %rax
movb $0x1, 0x20(%rax)
movl 0x18(%r13), %eax
movl %eax, 0x10(%rsp)
movzwl 0xa(%r13), %eax
movw %ax, 0x14(%rsp)
movzwl 0x1a(%rbp), %eax
movzwl 0x1c(%rbp), %ecx
movq %rbp, %rdx
subq %r14, %rdx
addq %rax, %rdx
addq %rdx, %rcx
addq $0x1e, %rcx
movl $0x0, %eax
movl $0x0, %r12d
cmpq %rbx, %rcx
ja 0x1c981
movl 0x14(%r13), %eax
addq %r14, %rcx
movq %rcx, %r12
leaq 0x18(%rsp), %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rcx)
leaq -0x1(%rax), %rcx
bsrq %rcx, %rcx
movl $0x2, %edi
shlq %cl, %rdi
movq %rax, 0x20(%rsp)
cmpq $0x8, %rax
movl $0x8, %eax
cmovbq %rax, %rdi
callq 0x1573c
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rdx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3280
movl 0x10(%r13), %eax
movl %eax, 0x28(%rsp)
movl 0xc(%r13), %eax
movl %eax, 0x2c(%rsp)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rsi
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x1d79e
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 0x1ca26
addq %rax, %r13
addq $0x2e, %r13
cmpl $0x2014b50, (%r14,%r13) # imm = 0x2014B50
je 0x1ca2b
xorl %r13d, %r13d
jmp 0x1ca2e
addq %r14, %r13
movq 0x18(%rsp), %rdi
callq 0x3430
testq %rbp, %rbp
jne 0x1c89c
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq