// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/os2pack.c
// Function: do_os2pack12_member
static void do_os2pack12_member(deark *c, de_arch_lctx *d, struct de_arch_member_data *md)
{
	i64 pos;
	i64 fnlen;
	i64 ea_pos = 0;
	i64 ea_len = 0;
	i64 member_endpos;
	i64 unk2, unk3, unk4;
	UI attribs;
	u8 flag_unsupp = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "member at %"I64_FMT, md->member_hdr_pos);
	de_dbg_indent(c, 1);

	pos = md->member_hdr_pos + 4;

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

	// Apparently a file attributes field, but don't know if it's 1, 2, or 4 bytes.
	attribs = (UI)de_getbyte_p(&pos);
	de_arch_handle_field_dos_attr(md, attribs);

	if(d->fmtcode==0x1400 || d->fmtcode==0x0a14) {
		pos += 1;
	}
	else if(d->fmtcode==0xffff) {
		pos += 6; // ?
	}
	else {
		pos += 3;
		ea_pos = de_getu32le_p(&pos);
		de_dbg(c, "ext. attr. pos: %"I64_FMT, ea_pos);

		de_arch_read_field_orig_len_p(md, &pos);
		// TODO: Figure out why some files have 1 here, and others have the original
		// file size.
		if(d->fmtcode==0xfffe && md->orig_len==1) {
			md->orig_len = 0;
			md->orig_len_known = 0;
		}

		md->next_member_pos = de_getu32le_p(&pos);
		de_dbg(c, "next member pos: %"I64_FMT, md->next_member_pos);
		if(md->next_member_pos!=0) {
			md->next_member_exists = 1;
		}
	}

	if(d->fmtcode==0xfffd) {
		pos += 7; // "FTCOMP\0"

		unk2 = de_getu16le_p(&pos);
		de_dbg(c, "unk2: %u", (UI)unk2);

		unk3 = de_getu16le_p(&pos);
		de_dbg(c, "unk3: %u", (UI)unk3);

		unk4 = de_getu32le_p(&pos);
		de_dbg(c, "unk4: %"I64_FMT, unk4);
	}

	if(d->fmtcode==0x1400 || d->fmtcode==0x0a14) {
		fnlen = 13;
	}
	else if(d->fmtcode==0xffff) {
		i64 foundpos = 0;

		if(dbuf_search_byte(c->infile, 0, pos, 260, &foundpos)) {
			fnlen = foundpos + 1 - pos;
		}
		else {
			d->need_errmsg = 1;
			goto done;
		}
	}
	else {
		fnlen = de_getu16le_p(&pos);
	}

	dbuf_read_to_ucstring_n(c->infile, pos, fnlen, 512, md->filename,
		DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(md->filename));
	pos += fnlen;
	de_arch_fixup_path(md->filename, 0);
	md->set_name_flags |= DE_SNFLAG_FULLPATH;

	md->cmpr_pos = pos;

	if(md->next_member_exists) {
		member_endpos = md->next_member_pos;
	}
	else {
		member_endpos = c->infile->len;
	}

	if(member_endpos > c->infile->len) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	md->cmpr_len = member_endpos - md->cmpr_pos;
	// if ea_pos is set, adjust cmpr_len downward
	if(ea_pos != 0) {
		if(ea_pos<md->cmpr_pos || ea_pos>member_endpos) {
			d->fatalerrflag = 1;
			d->need_errmsg = 1;
			goto done;
		}
		md->cmpr_len = ea_pos - md->cmpr_pos;
		ea_len = member_endpos - ea_pos;
	}

	if(md->cmpr_len<0) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	if(ea_len>0) {
		de_dbg(c, "cmpr ext. attr. at %"I64_FMT", len=%"I64_FMT, ea_pos, ea_len);
		de_dbg_indent(c, 1);
		if(d->fmtcode==0xfffd) {
			os2pack2_read_cmpr_method(c, ea_pos, ea_len);
		}
		de_dbg_indent(c, -1);
	}

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

	de_dbg_indent(c, 1);
	if(d->fmtcode==0xfffd) {
		// Most likely, the compressed data is considered to start after the
		// filename field.
		// It seems to have a compression header that we can peek at.
		os2pack2_read_cmpr_method(c, md->cmpr_pos, md->cmpr_len);
	}

	if(d->fmtcode==0x1400 || d->fmtcode==0x0a14 || d->fmtcode==0xffff ||
		d->fmtcode==0xfffe)
	{
		md->dfn = os2pack_decompressor_fn;
		de_arch_extract_member_file(md);
		if(ea_pos>0 && ea_len>0) {
			do_os2pack1_ea(c, d, md, ea_pos, ea_len);
		}
	}

	de_dbg_indent(c, -1);

	// TODO: There may be a few more bytes at the end of a member, purpose unknown.
	// If so, we should adjust md->cmpr_len or ea_len accordingly.

done:
	if(flag_unsupp) {
		de_err(c, "Unsupported member file format");
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movb $0x0, -0x5d(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x64(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0xddda7(%rip), %rsi     # 0x1c7392
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rsi
addq $0x18, %rsi
leaq 0xba8cc(%rip), %rdx     # 0x1a3ef3
movl $0x3, %ecx
leaq -0x20(%rbp), %r8
callq 0x17c580
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x5c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x17c4a0
movq -0x10(%rbp), %rax
cmpl $0x1400, 0x54(%rax)     # imm = 0x1400
je 0xe9672
movq -0x10(%rbp), %rax
cmpl $0xa14, 0x54(%rax)      # imm = 0xA14
jne 0xe9683
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0xe976a
movq -0x10(%rbp), %rax
cmpl $0xffff, 0x54(%rax)     # imm = 0xFFFF
jne 0xe96a1
movq -0x20(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x20(%rbp)
jmp 0xe9768
movq -0x20(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0xd416d(%rip), %rsi     # 0x1bd83e
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x17c350
movq -0x10(%rbp), %rax
cmpl $0xfffe, 0x54(%rax)     # imm = 0xFFFE
jne 0xe9711
movq -0x18(%rbp), %rax
cmpq $0x1, 0x38(%rax)
jne 0xe9711
movq -0x18(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x18(%rbp), %rax
movb $0x0, 0x4c(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x98(%rax), %rdx
leaq 0xd410c(%rip), %rsi     # 0x1bd852
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0xe9766
movq -0x18(%rbp), %rax
movb $0x1, 0xa0(%rax)
jmp 0xe9768
jmp 0xe976a
movq -0x10(%rbp), %rax
cmpl $0xfffd, 0x54(%rax)     # imm = 0xFFFD
jne 0xe980c
movq -0x20(%rbp), %rax
addq $0x7, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq 0xd40ba(%rip), %rsi     # 0x1bd867
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0xd4096(%rip), %rsi     # 0x1bd870
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rdx
leaq 0xd4074(%rip), %rsi     # 0x1bd879
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x1400, 0x54(%rax)     # imm = 0x1400
je 0xe9826
movq -0x10(%rbp), %rax
cmpl $0xa14, 0x54(%rax)      # imm = 0xA14
jne 0xe9830
movq $0xd, -0x28(%rbp)
jmp 0xe989e
movq -0x10(%rbp), %rax
cmpl $0xffff, 0x54(%rax)     # imm = 0xFFFF
jne 0xe9887
movq $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rdx
xorl %esi, %esi
movl $0x104, %ecx            # imm = 0x104
leaq -0x70(%rbp), %r8
callq 0x1667a0
cmpl $0x0, %eax
je 0xe9878
movq -0x70(%rbp), %rax
addq $0x1, %rax
subq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xe9885
movq -0x10(%rbp), %rax
movb $0x1, 0x14(%rax)
jmp 0xe9b4b
jmp 0xe989c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x28(%rbp)
jmp 0xe989e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x58(%rax), %r8
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl $0x200, %ecx            # imm = 0x200
movl $0x1, %r9d
movl %eax, (%rsp)
callq 0x166a40
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x172650
movq -0x78(%rbp), %rdi
movq %rax, %rdx
leaq 0xddc90(%rip), %rsi     # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdi
xorl %esi, %esi
callq 0x17c2b0
movq -0x18(%rbp), %rax
movl 0x78(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x78(%rax)
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
cmpb $0x0, 0xa0(%rax)
je 0xe994c
movq -0x18(%rbp), %rax
movq 0x98(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0xe995c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0xe9986
movq -0x10(%rbp), %rax
movl $0x1, 0x38(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x14(%rax)
jmp 0xe9b4b
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
subq 0x40(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
cmpq $0x0, -0x30(%rbp)
je 0xe99f1
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jl 0xe99b9
movq -0x30(%rbp), %rax
cmpq -0x40(%rbp), %rax
jle 0xe99d1
movq -0x10(%rbp), %rax
movl $0x1, 0x38(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x14(%rax)
jmp 0xe9b4b
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
subq 0x40(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x40(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jge 0xe9a14
movq -0x10(%rbp), %rax
movl $0x1, 0x38(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x14(%rax)
jmp 0xe9b4b
cmpq $0x0, -0x38(%rbp)
jle 0xe9a6f
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq 0xd3e55(%rip), %rsi     # 0x1bd883
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
cmpl $0xfffd, 0x54(%rax)     # imm = 0xFFFD
jne 0xe9a61
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0xe9c60
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
leaq 0xd3e19(%rip), %rsi     # 0x1bd8a3
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
cmpl $0xfffd, 0x54(%rax)     # imm = 0xFFFD
jne 0xe9ac5
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdx
callq 0xe9c60
movq -0x10(%rbp), %rax
cmpl $0x1400, 0x54(%rax)     # imm = 0x1400
je 0xe9af9
movq -0x10(%rbp), %rax
cmpl $0xa14, 0x54(%rax)      # imm = 0xA14
je 0xe9af9
movq -0x10(%rbp), %rax
cmpl $0xffff, 0x54(%rax)     # imm = 0xFFFF
je 0xe9af9
movq -0x10(%rbp), %rax
cmpl $0xfffe, 0x54(%rax)     # imm = 0xFFFE
jne 0xe9b3d
movq -0x18(%rbp), %rax
leaq 0x1bc(%rip), %rcx       # 0xe9cc0
movq %rcx, 0xa8(%rax)
movq -0x18(%rbp), %rdi
callq 0x17c920
cmpq $0x0, -0x30(%rbp)
jle 0xe9b3b
cmpq $0x0, -0x38(%rbp)
jle 0xe9b3b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
callq 0xe9d10
jmp 0xe9b3d
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpb $0x0, -0x5d(%rbp)
je 0xe9b63
movq -0x8(%rbp), %rdi
leaq 0xd3d61(%rip), %rsi     # 0x1bd8bd
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x15b5a0
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)