// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/misc3.c
// Function: do_mdcd_member
static int do_mdcd_member(deark *c, de_arch_lctx *d, struct de_arch_member_data *md)
{
	i64 pos;
	i64 s_len;
	i64 hdrlen;
	UI attr;
	int saved_indent_level;
	int retval = 0;
	int have_path = 0;
	u8 hdrtype;

	de_dbg_indent_save(c, &saved_indent_level);

	// Note: For info about the MDCD header format, see MDCD.PAS, near the
	// "FileHeader = Record" line.

	pos = md->member_hdr_pos;
	if(!mdcd_sig_at(c, pos)) {
		if(md->member_hdr_pos==0) {
			de_err(c, "Not an MDCD file");
		}
		else {
			de_dbg(c, "[member not found at %"I64_FMT"]", pos);
		}
		goto done;
	}
	pos += 4;

	de_dbg(c, "member at %"I64_FMT, md->member_hdr_pos);
	de_dbg_indent(c, 1);
	pos++; // software version?
	hdrtype = de_getbyte_p(&pos);
	de_dbg(c, "header type: %u", (UI)hdrtype);
	if(hdrtype!=1) {
		d->need_errmsg = 1;
		goto done;
	}
	hdrlen = de_getu16le_p(&pos);
	de_dbg(c, "header len: %"I64_FMT, hdrlen);
	if(hdrlen<MDCD_MINHEADERLEN) {
		d->need_errmsg = 1;
		goto done;
	}

	pos += 16; // various
	md->cmpr_meth = (UI)de_getbyte_p(&pos);
	de_dbg(c, "cmpr. method: %u", md->cmpr_meth);
	de_arch_read_field_orig_len_p(md, &pos);
	de_arch_read_field_cmpr_len_p(md, &pos);

	md->cmpr_pos = md->member_hdr_pos + hdrlen;
	// TODO: If we can set the filename first, we should.
	if(!de_arch_good_cmpr_data_pos(md)) {
		goto done;
	}
	md->member_total_size = hdrlen + md->cmpr_len;
	retval = 1;

	attr = (UI)de_getu16le_p(&pos);
	de_arch_handle_field_dos_attr(md, attr);

	de_arch_read_field_dttm_p(d, &md->fi->timestamp[DE_TIMESTAMPIDX_MODIFY], "mod",
		DE_ARCH_TSTYPE_DOS_TD, &pos);

	md->crc_reported = (u32)de_getu16le_p(&pos);
	de_dbg(c, "crc (reported): 0x%04x", (UI)md->crc_reported);

	s_len = de_getbyte_p(&pos);
	if(s_len>12) {
		d->need_errmsg = 1;
		goto done;
	}
	md->tmpfn_base = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, pos, s_len, md->tmpfn_base, 0, d->input_encoding);
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(md->tmpfn_base));
	pos += 12;

	if(hdrlen>=55) {
		s_len = de_getbyte_p(&pos);
	}
	else {
		s_len = 0;
	}
	md->tmpfn_path = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, pos, s_len, md->tmpfn_path, 0, d->input_encoding);
	de_dbg(c, "path: \"%s\"", ucstring_getpsz_d(md->tmpfn_path));

	// Ignore paths that look like absolute paths. Not sure what to do with them.
	have_path = ucstring_isnonempty(md->tmpfn_path);

	if(have_path && md->tmpfn_path->len >= 1 && (md->tmpfn_path->str[0]=='\\' ||
		md->tmpfn_path->str[0]=='/'))
	{
		have_path = 0;
	}
	if(have_path && md->tmpfn_path->len >= 2 && md->tmpfn_path->str[1]==':') {
		have_path = 0;
	}

	if(have_path) {
		de_arch_fixup_path(md->tmpfn_path, 0x1);
		ucstring_append_ucstring(md->filename, md->tmpfn_path);
		md->set_name_flags |= DE_SNFLAG_FULLPATH;
	}
	ucstring_append_ucstring(md->filename, md->tmpfn_base);

	de_dbg(c, "compressed data at %"I64_FMT", len=%"I64_FMT, md->cmpr_pos, md->cmpr_len);

	if(md->cmpr_meth>1) {
		de_err(c, "Unsupported compression: %u", (UI)md->cmpr_meth);
		goto done;
	}

	md->dfn = mdcd_decompressor_fn;

	md->validate_crc = 1;
	// When extracting, MDCD (1.0) does not validate the CRC of files that were
	// stored uncompressed. Some files seem to exploit this, and set the CRC to
	// 0, so we tolerate it with a warning.
	if(md->crc_reported==0 && md->cmpr_meth==0) {
		md->behavior_on_wrong_crc = 1;
	}

	de_arch_extract_member_file(md);

done:
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x15b580
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xa2ca0
cmpl $0x0, %eax
jne 0xa2817
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0xa27fc
movq -0x8(%rbp), %rdi
leaq 0x10f23a(%rip), %rsi    # 0x1b1a2d
movb $0x0, %al
callq 0x15bfb0
jmp 0xa2812
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x10f233(%rip), %rsi    # 0x1b1a3e
movb $0x0, %al
callq 0x15b0d0
jmp 0xa2c7d
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x124b5c(%rip), %rsi    # 0x1c7392
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, -0x41(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x41(%rbp), %edx
leaq 0x10f1de(%rip), %rsi    # 0x1b1a58
movb $0x0, %al
callq 0x15b0d0
movzbl -0x41(%rbp), %eax
cmpl $0x1, %eax
je 0xa2897
movq -0x10(%rbp), %rax
movb $0x1, 0x14(%rax)
jmp 0xa2c7d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x10f2fa(%rip), %rsi    # 0x1b1bb5
movb $0x0, %al
callq 0x15b0d0
cmpq $0x36, -0x30(%rbp)
jge 0xa28d6
movq -0x10(%rbp), %rax
movb $0x1, 0x14(%rax)
jmp 0xa2c7d
movq -0x20(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x90(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x90(%rax), %edx
leaq 0x10edd4(%rip), %rsi    # 0x1b16e9
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x17c350
movq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x17c400
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
addq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rdi
callq 0x17c7f0
cmpl $0x0, %eax
jne 0xa295d
jmp 0xa2c7d
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
addq 0x30(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movl $0x1, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x17c4a0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rsi
addq $0x18, %rsi
leaq 0x101544(%rip), %rdx    # 0x1a3ef3
movl $0x4, %ecx
leaq -0x20(%rbp), %r8
callq 0x17c580
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x48(%rax), %edx
leaq 0x12df63(%rip), %rsi    # 0x1d094c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x28(%rbp)
cmpq $0xc, -0x28(%rbp)
jle 0xa2a1c
movq -0x10(%rbp), %rax
movb $0x1, 0x14(%rax)
jmp 0xa2c7d
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x18(%rax), %r9d
xorl %r8d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x172650
movq -0x50(%rbp), %rdi
movq %rax, %rdx
leaq 0x124b08(%rip), %rsi    # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x20(%rbp)
cmpq $0x37, -0x30(%rbp)
jl 0xa2aaf
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x28(%rbp)
jmp 0xa2ab7
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x18(%rax), %r9d
xorl %r8d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x172650
movq -0x58(%rbp), %rdi
movq %rax, %rdx
leaq 0x12d346(%rip), %rsi    # 0x1cfe5c
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x1717f0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0xa2b6b
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
cmpq $0x1, 0x10(%rax)
jl 0xa2b6b
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x5c, (%rax)
je 0xa2b64
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x2f, (%rax)
jne 0xa2b6b
movl $0x0, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0xa2b99
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
cmpq $0x2, 0x10(%rax)
jl 0xa2b99
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x3a, 0x4(%rax)
jne 0xa2b99
movl $0x0, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0xa2bd3
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rdi
movl $0x1, %esi
callq 0x17c2b0
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rsi
callq 0x171460
movq -0x18(%rbp), %rax
movl 0x78(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x78(%rax)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rsi
callq 0x171460
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
leaq 0x10ee65(%rip), %rsi    # 0x1b1a68
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpl $0x1, 0x90(%rax)
jbe 0xa2c35
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x90(%rax), %edx
leaq 0xfcb56(%rip), %rsi     # 0x19f782
movb $0x0, %al
callq 0x15bfb0
jmp 0xa2c7d
movq -0x18(%rbp), %rax
leaq 0xa0(%rip), %rcx        # 0xa2ce0
movq %rcx, 0xa8(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0xa1(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x48(%rax)
jne 0xa2c74
movq -0x18(%rbp), %rax
cmpl $0x0, 0x90(%rax)
jne 0xa2c74
movq -0x18(%rbp), %rax
movb $0x1, 0xa2(%rax)
movq -0x18(%rbp), %rdi
callq 0x17c920
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x15b5a0
movl -0x3c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)