// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/packit.c
// Function: packit_read_member_header
static void packit_read_member_header(deark *c, struct packit_ctx *d,
	struct packit_md *pmd)
{
	i64 pos = pmd->hdr_pos_in_inf;
	i64 n;
	u32 hdr_crc_reported;
	u32 hdr_crc_calc;
	char timestamp_buf[64];

	pmd->namelen = (i64)dbuf_getbyte_p(pmd->inf, &pos);
	if(pmd->namelen<1 || pmd->namelen>63) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}
	pmd->fname = dbuf_read_string(pmd->inf, pos, pmd->namelen, pmd->namelen,
		0, DE_ENCODING_MACROMAN);
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz(pmd->fname->str));
	pos += 63;

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

	pmd->finder_flags = (UI)dbuf_getu16be_p(pmd->inf, &pos);
	de_dbg(c, "finder flags: 0x%04x", pmd->finder_flags);
	pmd->advf->finderflags = (u16)pmd->finder_flags;
	pmd->advf->has_finderflags = 1;

	pos += 2; // "locked"

	pmd->dfork_pos = pmd->hdr_pos_in_inf + 94;
	pmd->dfork_len = dbuf_getu32be_p(pmd->inf, &pos);
	de_dbg(c, "data fork len: %"I64_FMT, pmd->dfork_len);
	pmd->advf->mainfork.fork_len = pmd->dfork_len;

	pmd->rfork_pos = pmd->dfork_pos + pmd->dfork_len;
	pmd->rfork_len = dbuf_getu32be_p(pmd->inf, &pos);
	de_dbg(c, "rsrc fork len: %"I64_FMT, pmd->rfork_len);
	pmd->advf->rsrcfork.fork_len = pmd->rfork_len;

	pmd->advf->mainfork.fork_exists = (pmd->dfork_len!=0 ||
		pmd->rfork_len==0);
	pmd->advf->rsrcfork.fork_exists = (pmd->rfork_len!=0);

	n = dbuf_getu32be_p(pmd->inf, &pos);
	de_mac_time_to_timestamp(n, &pmd->create_time);
	de_timestamp_to_string(&pmd->create_time, timestamp_buf, sizeof(timestamp_buf), 0);
	de_dbg(c, "create time: %"I64_FMT" (%s)", n, timestamp_buf);
	if(n > 100000000) {
		pmd->advf->mainfork.fi->timestamp[DE_TIMESTAMPIDX_CREATE] = pmd->create_time;
	}

	n = dbuf_getu32be_p(pmd->inf, &pos);
	de_mac_time_to_timestamp(n, &pmd->mod_time);
	de_timestamp_to_string(&pmd->mod_time, timestamp_buf, sizeof(timestamp_buf), 0);
	de_dbg(c, "mod time: %"I64_FMT" (%s)", n, timestamp_buf);
	// TODO: Many files have nonsense timestamps, and there's probably a reason.
	// Maybe they can be decoded.
	if(n > 100000000) {
		pmd->advf->mainfork.fi->timestamp[DE_TIMESTAMPIDX_MODIFY] = pmd->mod_time;
	}

	hdr_crc_reported = (u32)dbuf_getu16be_p(pmd->inf, &pos);
	de_dbg(c, "header crc (reported): 0x%04x", (UI)hdr_crc_reported);

	de_crcobj_reset(d->crco_hdr);
	de_crcobj_addslice(d->crco_hdr, pmd->inf, pmd->hdr_pos_in_inf, 92);
	hdr_crc_calc = de_crcobj_getval(d->crco_hdr);
	de_dbg(c, "header crc (calculated): 0x%04x", (UI)hdr_crc_calc);
	if(hdr_crc_reported != hdr_crc_calc) {
		// TODO: Maybe should be a warning? Or nonfatal?
		de_err(c, "Bad header CRC (reported 0x%04x, calculated 0x%04x)", (UI)hdr_crc_reported,
			(UI)hdr_crc_calc);
		d->fatalerrflag = 1;
		goto done;
	}

done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
movq 0x10(%rdx), %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq 0x18(%rdx), %rdi
callq 0xef14d
movzbl %al, %ecx
movq %rcx, 0x28(%r15)
movl %ecx, %eax
addb $-0x40, %al
cmpb $-0x40, %al
ja 0xba419
movw $0x101, (%rbx)          # imm = 0x101
jmp 0xba72b
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %r12
movq (%r12), %rsi
movq %rcx, %rdx
xorl %r8d, %r8d
movl $0x11, %r9d
callq 0xefe67
movq %rax, 0x40(%r15)
movq 0x18(%rax), %rdi
callq 0xf74bd
leaq 0x81589(%rip), %rsi     # 0x13b9d4
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%r12), %rsi
addq $0x3f, %rsi
movq %rsi, (%r12)
movq 0x18(%r15), %rdi
leaq 0x48(%r15), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1d74
leaq 0x58(%r15), %rdx
leaq 0x5a3dc(%rip), %rsi     # 0x114860
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x38(%r15), %rax
movl 0x48(%r15), %ecx
movl %ecx, 0x90(%rax)
movq 0x38(%r15), %rax
movb $0x1, %bpl
movb %bpl, 0x7b(%rax)
movq (%r12), %rsi
addq $0x4, %rsi
movq %rsi, (%r12)
movq 0x18(%r15), %rdi
leaq 0x78(%r15), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1d74
leaq 0x88(%r15), %rdx
leaq 0x79a1d(%rip), %rsi     # 0x133ef3
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x38(%r15), %rax
movl 0x78(%r15), %ecx
movl %ecx, 0x94(%rax)
movq 0x38(%r15), %rax
movb %bpl, 0x7c(%rax)
addq $0x4, (%r12)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef193
movl %eax, 0x30(%r15)
leaq 0x5a35d(%rip), %rsi     # 0x11486f
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movzwl 0x30(%r15), %eax
movq 0x38(%r15), %rcx
movw %ax, 0x7e(%rcx)
movb %bpl, 0x7d(%rcx)
addq $0x2, (%r12)
movq 0x10(%r15), %rax
movq 0x18(%r15), %rdi
addq $0x5e, %rax
movq %rax, 0xc8(%r15)
movq %r12, %rsi
callq 0xef34e
movq %rax, 0xd0(%r15)
leaq 0x5a327(%rip), %rsi     # 0x114884
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x38(%r15), %rax
movq 0xd0(%r15), %rcx
movq %rcx, 0x18(%rax)
addq 0xc8(%r15), %rcx
movq %rcx, 0xd8(%r15)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef34e
movq %rax, 0xe0(%r15)
leaq 0x5a2f6(%rip), %rsi     # 0x114897
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x38(%r15), %rax
movq 0xe0(%r15), %rcx
movq %rcx, 0x40(%rax)
cmpq $0x0, 0xd0(%r15)
setne %dl
testq %rcx, %rcx
setne 0x38(%rax)
sete %cl
orb %dl, %cl
movb %cl, 0x10(%rax)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef34e
movq %rax, %r13
leaq 0xb8(%r15), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0xeae04
leaq 0x10(%rsp), %rbp
movl $0x40, %edx
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xeb4f8
leaq 0x7da1f(%rip), %rsi     # 0x138035
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe9226
cmpq $0x5f5e101, %r13        # imm = 0x5F5E101
jl 0xba640
movq 0x38(%r15), %rax
movq 0x20(%rax), %rax
movups (%r12), %xmm0
movups %xmm0, 0x28(%rax)
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef34e
movq %rax, %r13
leaq 0xa8(%r15), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0xeae04
leaq 0x10(%rsp), %rbp
movl $0x40, %edx
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xeb4f8
leaq 0x5797b(%rip), %rsi     # 0x111ffc
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe9226
cmpq $0x5f5e101, %r13        # imm = 0x5F5E101
jl 0xba6ab
movq 0x38(%r15), %rax
movq 0x20(%rax), %rax
movups (%r12), %xmm0
movups %xmm0, 0x18(%rax)
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef193
movq %rax, %r12
leaq 0x71cb2(%rip), %rsi     # 0x12c375
movq %r14, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x10(%rbx), %rdi
callq 0xebdc3
movq 0x10(%rbx), %rdi
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rsi
movl $0x5c, %ecx
callq 0xebf09
movq 0x10(%rbx), %rdi
callq 0xebe2a
movl %eax, %ebp
leaq 0x74b91(%rip), %rsi     # 0x12f292
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpl %r12d, %ebp
je 0xba72b
leaq 0x7d932(%rip), %rsi     # 0x13804b
movq %r14, %rdi
movl %r12d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9df7
movb $0x1, (%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq