// File: /workspace/github_c_makefile_star2_O2/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 0xe3f2a
movzbl %al, %ecx
movq %rcx, 0x28(%r15)
movl %ecx, %eax
addb $-0x40, %al
cmpb $-0x40, %al
ja 0xb25be
movw $0x101, (%rbx)          # imm = 0x101
jmp 0xb28c5
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %r12
movq (%r12), %rsi
pushq $0x11
popq %r9
movq %rcx, %rdx
xorl %r8d, %r8d
callq 0xe495c
movq %rax, 0x40(%r15)
movq 0x18(%rax), %rdi
callq 0xeb7e4
leaq 0x7c470(%rip), %rsi     # 0x12ea5e
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq (%r12), %rsi
addq $0x3f, %rsi
movq %rsi, (%r12)
movq 0x18(%r15), %rdi
leaq 0x48(%r15), %rdx
pushq $0x4
popq %rbp
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0xe657c
leaq 0x58(%r15), %rdx
leaq 0x55289(%rip), %rsi     # 0x1078b0
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x38(%r15), %rax
movl 0x48(%r15), %ecx
movl %ecx, 0x90(%rax)
movq 0x38(%r15), %rax
movb $0x1, %r13b
movb %r13b, 0x7b(%rax)
movq (%r12), %rsi
addq $0x4, %rsi
movq %rsi, (%r12)
movq 0x18(%r15), %rdi
leaq 0x78(%r15), %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0xe657c
leaq 0x88(%r15), %rdx
leaq 0x7499d(%rip), %rsi     # 0x127013
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x38(%r15), %rax
movl 0x78(%r15), %ecx
movl %ecx, 0x94(%rax)
movq 0x38(%r15), %rax
movb %r13b, 0x7c(%rax)
addq $0x4, (%r12)
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xe3f6e
movl %eax, 0x30(%r15)
leaq 0x5520d(%rip), %rsi     # 0x1078bf
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movzwl 0x30(%r15), %eax
movq 0x38(%r15), %rcx
movw %ax, 0x7e(%rcx)
movb %r13b, 0x7d(%rcx)
addq $0x2, (%r12)
movq 0x10(%r15), %rax
movq 0x18(%r15), %rdi
addq $0x5e, %rax
movq %rax, 0xc8(%r15)
movq %r12, %rsi
callq 0xe4071
movq %rax, 0xd0(%r15)
leaq 0x551d7(%rip), %rsi     # 0x1078d4
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
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 0xe4071
movq %rax, 0xe0(%r15)
leaq 0x551a6(%rip), %rsi     # 0x1078e7
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
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 0xe4071
movq %rax, %r13
leaq 0xb8(%r15), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0xe0102
leaq 0x10(%rsp), %rbp
pushq $0x40
popq %rdx
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xe0644
leaq 0x7890d(%rip), %rsi     # 0x12b0c1
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xde896
cmpq $0x5f5e101, %r13        # imm = 0x5F5E101
jl 0xb27de
movq 0x38(%r15), %rax
movq 0x20(%rax), %rax
movups (%r12), %xmm0
movups %xmm0, 0x28(%rax)
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe4071
movq %rax, %r13
leaq 0xa8(%r15), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0xe0102
leaq 0x10(%rsp), %rbp
pushq $0x40
popq %rdx
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xe0644
leaq 0x527cf(%rip), %rsi     # 0x104fec
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xde896
cmpq $0x5f5e101, %r13        # imm = 0x5F5E101
jl 0xb2847
movq 0x38(%r15), %rax
movq 0x20(%rax), %rax
movups (%r12), %xmm0
movups %xmm0, 0x18(%rax)
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe3f6e
movq %rax, %r12
leaq 0x6cb8e(%rip), %rsi     # 0x11f3ed
movq %r14, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x10(%rbx), %rdi
callq 0xe0e44
movq 0x10(%rbx), %rdi
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rsi
pushq $0x5c
popq %rcx
callq 0xe0f33
movq 0x10(%rbx), %rdi
callq 0xe0e95
movl %eax, %ebp
leaq 0x6fa52(%rip), %rsi     # 0x1222ed
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
cmpl %r12d, %ebp
je 0xb28c5
leaq 0x78824(%rip), %rsi     # 0x12b0d7
movq %r14, %rdi
movl %r12d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xdf2a5
movb $0x1, (%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq