// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/binhex.c
// Function: do_extract_forks
static void do_extract_forks(deark *c, lctx *d)
{
	i64 name_len;
	dbuf *inf;
	i64 pos;
	u32 hc; // Header CRC
	struct de_stringreaderdata *fname = NULL;
	struct de_fourcc filetype;
	struct de_fourcc creator;

	inf = d->decompressed;
	pos = 0;

	// Read the header

	name_len = (i64)dbuf_getbyte(inf, pos);
	pos+=1;
	de_dbg(c, "name len: %d", (int)name_len);

	if(name_len > 0) {
		fname = dbuf_read_string(inf, pos, name_len, name_len, 0, d->input_encoding);
		ucstring_append_ucstring(d->advf->filename, fname->str);
		d->advf->original_filename_flag = 1;
		de_dbg(c, "name: \"%s\"", ucstring_getpsz_d(fname->str));
		de_advfile_set_orig_filename(d->advf, fname->sz, fname->sz_strlen);
	}
	else {
		ucstring_append_sz(d->advf->filename, "bin", DE_ENCODING_LATIN1);
	}

	pos+=name_len;
	pos+=1; // Skip the 0x00 byte after the name.

	dbuf_read_fourcc(inf, pos, &filetype, 4, 0x0);
	de_dbg(c, "filetype: '%s'", filetype.id_dbgstr);
	de_memcpy(d->advf->typecode, filetype.bytes, 4);
	d->advf->has_typecode = 1;
	pos += 4;
	dbuf_read_fourcc(inf, pos, &creator, 4, 0x0);
	de_dbg(c, "creator: '%s'", creator.id_dbgstr);
	de_memcpy(d->advf->creatorcode, creator.bytes, 4);
	d->advf->has_creatorcode = 1;
	pos += 4;

	d->advf->finderflags = (u16)dbuf_getu16be_p(inf, &pos);
	d->advf->has_finderflags = 1;
	de_dbg(c, "flags: 0x%04x", (unsigned int)d->advf->finderflags);

	d->fki_data.len = dbuf_getu32be_p(inf, &pos);
	de_dbg(c, "data fork len: %d", (int)d->fki_data.len);
	d->fki_rsrc.len = dbuf_getu32be_p(inf, &pos);
	de_dbg(c, "resource fork len: %d", (int)d->fki_rsrc.len);

	hc = (u32)dbuf_getu16be_p(inf, &pos);
	de_dbg(c, "header crc (reported): 0x%04x", (unsigned int)hc);
	// TODO: Verify header CRC

	d->fki_data.forkname = "data";
	d->fki_rsrc.forkname = "rsrc";

	// Walk through the file, and record some offsets
	d->fki_data.pos = pos;
	pos += d->fki_data.len;
	pos += 2; // for the CRC

	d->fki_rsrc.pos = pos;
	// [d->fki_rsrc.len bytes here]
	// [2 bytes here, for the CRC]

	d->fki_data.crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_XMODEM);
	d->fki_rsrc.crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_XMODEM);

	if(!do_pre_extract_fork(c, d, inf, &d->fki_data, &d->advf->mainfork)) {
		goto done;
	}
	(void)do_pre_extract_fork(c, d, inf, &d->fki_rsrc, &d->advf->rsrcfork);

	d->advf->mainfork.fork_exists = (d->fki_data.len > 0);
	d->advf->mainfork.fork_len = d->fki_data.len;
	d->advf->rsrcfork.fork_exists = (d->fki_rsrc.len > 0);
	d->advf->rsrcfork.fork_len = d->fki_rsrc.len;
	d->advf->userdata = (void*)d;
	d->advf->writefork_cbfn = my_advfile_cbfn;

	de_advfile_run(d->advf);

	do_post_extract_fork(c, d, &d->fki_data);
	do_post_extract_fork(c, d, &d->fki_rsrc);

done:
	de_destroy_stringreaderdata(c, fname);
	de_crcobj_destroy(d->fki_data.crco);
	d->fki_data.crco = NULL;
	de_crcobj_destroy(d->fki_rsrc.crco);
	d->fki_rsrc.crco = NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x1929d2(%rip), %rsi    # 0x1d0a42
movb $0x0, %al
callq 0x15b0d0
cmpq $0x0, -0x18(%rbp)
jle 0x3e119
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
xorl %r8d, %r8d
callq 0x1664c0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x171460
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movb $0x1, 0x79(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172650
movq -0xa0(%rbp), %rdi
movq %rax, %rdx
leaq 0x1700dd(%rip), %rsi    # 0x1ae1d0
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x17ab20
jmp 0x3e136
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rax), %rdi
leaq 0x1830db(%rip), %rsi    # 0x1c1207
movl $0x5, %edx
callq 0x1715e0
movq -0x18(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x169860
movq -0x8(%rbp), %rdi
leaq -0x68(%rbp), %rdx
addq $0x10, %rdx
leaq 0x16258d(%rip), %rsi    # 0x1a0707
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x68(%rbp), %ecx
movl %ecx, 0x90(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movb $0x1, 0x7b(%rax)
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x98(%rbp), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x169860
movq -0x8(%rbp), %rdi
leaq -0x98(%rbp), %rdx
addq $0x10, %rdx
leaq 0x181b56(%rip), %rsi    # 0x1bfd32
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x98(%rbp), %ecx
movl %ecx, 0x94(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movb $0x1, 0x7c(%rax)
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165210
movw %ax, %cx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movw %cx, 0x7e(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movb $0x1, 0x7d(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movzwl 0x7e(%rax), %edx
leaq 0x176a0d(%rip), %rsi    # 0x1b4c5b
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
leaq 0x164a28(%rip), %rsi    # 0x1a2caa
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
leaq 0x164a06(%rip), %rsi    # 0x1a2cbc
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165210
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %edx
leaq 0x179e5e(%rip), %rsi    # 0x1b8139
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
leaq 0x18788c(%rip), %rcx    # 0x1c5b79
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
leaq 0x16aa75(%rip), %rcx    # 0x1a8d71
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movl $0x20, %esi
callq 0x15eac0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movl $0x20, %esi
callq 0x15eac0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rcx
addq $0x20, %rcx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %r8
addq $0x10, %r8
callq 0x3e4e0
cmpl $0x0, %eax
jne 0x3e395
jmp 0x3e478
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rcx
addq $0x48, %rcx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %r8
addq $0x38, %r8
callq 0x3e4e0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
setg %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movb %cl, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
setg %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movb %cl, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
leaq 0x183(%rip), %rcx       # 0x3e5c0
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x17ac20
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rdx
addq $0x20, %rdx
callq 0x3e650
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rdx
addq $0x48, %rdx
callq 0x3e650
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x166830
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x15f2e0
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x15f2e0
movq -0x10(%rbp), %rax
movq $0x0, 0x60(%rax)
addq $0xa0, %rsp
popq %rbp
retq