// File: Alcaro[P]bridges/arlib/zip.cpp
// Function: zip::read_idx(unsigned long, array<unsigned char>&, bool, string*, long*) const
bool zip::read_idx(size_t id, array<uint8_t>& out, bool permissive, string* error, time_t * time) const
{
	{
		string discard;
		if (!error) error = &discard;
		else *error = "";
		
		out.reset();
		if (id==(size_t)-1) { *error = "file not found"; goto fail; }
		
		const zip::file& f = filedat[id];
		switch (f.method)
		{
			case 0:
			{
				out = f.data;
				break;
			}
			case 8:
			{
				out.resize(f.decomplen);
				if (!inflator::inflate(out, f.data)) { *error = "corrupt DEFLATE data"; goto fail; }
				break;
			}
			default:
			{
				*error = "unknown compression method 0x"+tostringhex(f.method);
				goto fail;
			}
		}
		
		//APPNOTE.TXT specifies some other generator constant, 0xdebb20e3
		//no idea how to use that, the normal crc32 (0xedb88320) works fine
		if (crc32(out) != f.crc32) { *error = "bad crc32"; goto fail; }
		if (time) *time = fromdosdate(f.dosdate);
		return true;
	}
	
fail:
	if (!permissive) out.reset();
	return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movb %cl, %al
movq %rdi, -0x60(%rbp)
movq %rsi, -0x68(%rbp)
movq %rdx, -0x70(%rbp)
andb $0x1, %al
movb %al, -0x71(%rbp)
movq %r8, -0x80(%rbp)
movq %r9, -0x88(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x7800
cmpq $0x0, -0x80(%rbp)
jne 0x44214
leaq -0x98(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x44234
movq -0x80(%rbp), %rax
movq %rax, -0x8(%rbp)
leaq 0x22226(%rip), %rax     # 0x66449
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x63e0
movq -0x70(%rbp), %rdi
callq 0x1d1f0
cmpq $-0x1, -0x68(%rbp)
jne 0x44273
movq -0x80(%rbp), %rax
movq %rax, -0x18(%rbp)
leaq 0x1fd33(%rip), %rax     # 0x63f86
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x63e0
movl $0x2, -0x114(%rbp)
jmp 0x444b7
movq -0x120(%rbp), %rdi
addq $0x10, %rdi
movq -0x68(%rbp), %rsi
callq 0x459e0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, -0x124(%rbp)
testl %eax, %eax
je 0x442b5
jmp 0x442a5
movl -0x124(%rbp), %eax
subl $0x8, %eax
je 0x442ed
jmp 0x44380
movq -0xa0(%rbp), %rsi
addq $0x8, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x1d2c0
movq -0x70(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x1d440
leaq -0xb0(%rbp), %rdi
callq 0x1d720
jmp 0x4441f
movq -0x70(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl (%rax), %eax
movl %eax, %esi
callq 0x1ce60
movq -0x70(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x74f0
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0xd0(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0xfe80
testb $0x1, %al
jne 0x4437b
movq -0x80(%rbp), %rax
movq %rax, -0x28(%rbp)
leaq 0x1fc3a(%rip), %rax     # 0x63f95
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x63e0
movl $0x2, -0x114(%rbp)
jmp 0x444b7
jmp 0x4441f
movq -0xa0(%rbp), %rax
leaq -0x100(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %edx
movzwl 0x4(%rax), %esi
callq 0x2f1d0
movq -0x100(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xf0(%rbp), %rdx
movq -0xe8(%rbp), %rcx
leaq -0xe0(%rbp), %rdi
leaq 0x1fbd7(%rip), %rsi     # 0x63faa
callq 0x57e0
movq -0x80(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0xe0(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x10670
leaq -0xe0(%rbp), %rdi
callq 0x5870
leaq -0x100(%rbp), %rdi
callq 0x5870
movl $0x2, -0x114(%rbp)
jmp 0x444b7
movq -0x70(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x110(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x108(%rbp)
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rsi
callq 0x43a40
movq -0xa0(%rbp), %rcx
cmpl 0x18(%rcx), %eax
je 0x44483
movq -0x80(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq 0x1fb62(%rip), %rax     # 0x63fc8
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x63e0
movl $0x2, -0x114(%rbp)
jmp 0x444b7
cmpq $0x0, -0x88(%rbp)
je 0x444a9
movq -0xa0(%rbp), %rax
movl 0x1c(%rax), %edi
callq 0x44500
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, (%rax)
movb $0x1, -0x51(%rbp)
movl $0x1, -0x114(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x5870
movl -0x114(%rbp), %eax
subl $0x1, %eax
je 0x444e3
jmp 0x444d0
testb $0x1, -0x71(%rbp)
jne 0x444df
movq -0x70(%rbp), %rdi
callq 0x1d1f0
movb $0x0, -0x51(%rbp)
movb -0x51(%rbp), %al
andb $0x1, %al
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)