// File: /workspace/github_c_makefile_star2_O3/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 0xeeb4b
movzbl %al, %ecx
movq %rcx, 0x28(%r15)
movl %ecx, %eax
addb $-0x40, %al
cmpb $-0x40, %al
ja 0xb9876
movw $0x101, (%rbx)          # imm = 0x101
jmp 0xb9b88
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %r12
movq (%r12), %rsi
movq %rcx, %rdx
xorl %r8d, %r8d
movl $0x11, %r9d
callq 0xef87c
movq %rax, 0x40(%r15)
movq 0x18(%rax), %rdi
callq 0xf705c
leaq 0x824a6(%rip), %rsi     # 0x13bd4e
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq (%r12), %rsi
addq $0x3f, %rsi
movq %rsi, (%r12)
movq 0x18(%r15), %rdi
leaq 0x48(%r15), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1762
leaq 0x58(%r15), %rdx
leaq 0x5b05a(%rip), %rsi     # 0x11493b
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8617
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 0xf1762
leaq 0x88(%r15), %rdx
leaq 0x7a9b8(%rip), %rsi     # 0x1342eb
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8617
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 0xeeb91
movl %eax, 0x30(%r15)
leaq 0x5afdb(%rip), %rsi     # 0x11494a
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
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 0xeed31
movq %rax, 0xd0(%r15)
leaq 0x5afa5(%rip), %rsi     # 0x11495f
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
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 0xeed31
movq %rax, 0xe0(%r15)
leaq 0x5af74(%rip), %rsi     # 0x114972
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
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 0xeed31
movq %rax, %r13
leaq 0xb8(%r15), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0xea8ff
leaq 0x10(%rsp), %rbp
movl $0x40, %edx
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xeaf46
leaq 0x7e94e(%rip), %rsi     # 0x1383c1
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe8617
cmpq $0x5f5e101, %r13        # imm = 0x5F5E101
jl 0xb9a9d
movq 0x38(%r15), %rax
movq 0x20(%rax), %rax
movups (%r12), %xmm0
movups %xmm0, 0x28(%rax)
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0xeed31
movq %rax, %r13
leaq 0xa8(%r15), %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0xea8ff
leaq 0x10(%rsp), %rbp
movl $0x40, %edx
movq %r12, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xeaf46
leaq 0x5852e(%rip), %rsi     # 0x11200c
movq %r14, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe8617
cmpq $0x5f5e101, %r13        # imm = 0x5F5E101
jl 0xb9b08
movq 0x38(%r15), %rax
movq 0x20(%rax), %rax
movups (%r12), %xmm0
movups %xmm0, 0x18(%rax)
movq 0x18(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0xeeb91
movq %rax, %r12
leaq 0x72b3d(%rip), %rsi     # 0x12c65d
movq %r14, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%rbx), %rdi
callq 0xeb7fe
movq 0x10(%rbx), %rdi
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rsi
movl $0x5c, %ecx
callq 0xeb944
movq 0x10(%rbx), %rdi
callq 0xeb865
movl %eax, %ebp
leaq 0x75a17(%rip), %rsi     # 0x12f575
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpl %r12d, %ebp
je 0xb9b88
leaq 0x7e861(%rip), %rsi     # 0x1383d7
movq %r14, %rdi
movl %r12d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9295
movb $0x1, (%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq