// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/macbinary.c
// Function: do_header
static void do_header(deark *c, lctx *d, struct de_advfile *advf)
{
	u8 b;
	i64 namelen;
	i64 pos = 0;
	i64 n, n2;
	i64 mod_time_raw;
	u32 crc_reported, crc_calc;
	struct de_fourcc type4cc;
	struct de_fourcc creator4cc;
	char timestamp_buf[64];

	d->oldver = de_getbyte_p(&pos);
	de_dbg(c, "original version: %u", (unsigned int)d->oldver);
	if(d->oldver!=0) {
		de_warn(c, "Unsupported MacBinary version");
		goto done;
	}

	d->extver = de_getbyte(122);
	de_dbg(c, "extended version: %u", (unsigned int)d->extver);
	if(d->extver==129 || d->extver==130) {
		d->is_v23 = 1;
	}
	if(d->extver >= 129) {
		d->extver_minneeded = de_getbyte(123);
		de_dbg(c, "extended version, min needed: %u", (unsigned int)d->extver_minneeded);
	}

	namelen = (i64)de_getbyte_p(&pos);
	if(namelen>=1 && namelen<=63) {
		// Required to be 1-63 by MacBinary II spec.
		// Original spec has no written requirements.
		// Not supposed to be NUL terminated, but such files exist.
		d->filename_srd = dbuf_read_string(c->infile, pos, namelen, namelen,
			DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
		de_dbg(c, "filename: \"%s\"", ucstring_getpsz(d->filename_srd->str));
	}
	else {
		de_warn(c, "Bad MacBinary filename length (%d)", (int)namelen);
	}
	pos += 63;

	de_dbg(c, "finder info:");
	de_dbg_indent(c, 1);

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

	advf->has_finderflags = 1;
	if(d->is_v23) {
		u8 fflags_hibyte;

		fflags_hibyte = de_getbyte_p(&pos);
		de_dbg(c, "finder flags (high byte): 0x%02x__", (unsigned int)fflags_hibyte);
		pos++;
		advf->finderflags = (u16)fflags_hibyte << 8;
	}
	else {
		advf->finderflags = (u16)de_getu16be_p(&pos);
		de_dbg(c, "finder flags: 0x%04x", (unsigned int)advf->finderflags);
	}

	n = de_geti16be_p(&pos);
	n2 = de_geti16be_p(&pos);
	de_dbg(c, "position in window: %d,%d", (int)n2, (int)n);

	n = de_getu16be_p(&pos);
	de_dbg(c, "window/folder id: %d", (int)n);
	de_dbg_indent(c, -1);

	b = de_getbyte_p(&pos);
	de_dbg(c, "protected: 0x%02x", (unsigned int)b);

	pos++;

	d->dflen = de_getu32be_p(&pos);
	de_dbg(c, "data fork len: %u", (unsigned int)d->dflen);
	d->rflen = de_getu32be_p(&pos);
	de_dbg(c, "resource fork len: %u", (unsigned int)d->rflen);

	n = de_getu32be_p(&pos);
	if(n==0) {
		d->create_time.is_valid = 0;
		de_strlcpy(timestamp_buf, "unknown", sizeof(timestamp_buf));
	}
	else {
		de_mac_time_to_timestamp(n, &d->create_time);
		d->create_time.tzcode = DE_TZCODE_LOCAL;
		de_timestamp_to_string(&d->create_time, timestamp_buf, sizeof(timestamp_buf), 0);
	}
	de_dbg(c, "create date: %"I64_FMT" (%s)", n, timestamp_buf);

	mod_time_raw = de_getu32be_p(&pos);
	if(mod_time_raw==0) {
		d->mod_time.is_valid = 0;
		de_strlcpy(timestamp_buf, "unknown", sizeof(timestamp_buf));
	}
	else {
		de_mac_time_to_timestamp(mod_time_raw, &d->mod_time);
		d->mod_time.tzcode = DE_TZCODE_LOCAL;
		de_timestamp_to_string(&d->mod_time, timestamp_buf, sizeof(timestamp_buf), 0);
	}
	de_dbg(c, "mod date: %"I64_FMT" (%s)", mod_time_raw, timestamp_buf);

	pos += 2; // length of Get Info comment

	if(d->is_v23) {
		u8 fflags_lobyte;

		fflags_lobyte = de_getbyte(pos);
		de_dbg(c, "finder flags (low byte): 0x__%02x", (unsigned int)fflags_lobyte);
		advf->finderflags |= (u16)fflags_lobyte;
	}
	pos += 1;

	pos += 14; // unused
	pos += 4; // unpacked total length

	if(d->is_v23) {
		n = de_getu16be(pos);
		de_dbg(c, "length of secondary header: %u", (unsigned int)n);
	}
	pos += 2;

	pos += 1; // version number, already read
	pos += 1; // version number, already read

	crc_reported = (u32)de_getu16be_p(&pos);
	if(d->is_v23 || crc_reported!=0) {
		struct de_crcobj *crco;

		de_dbg(c, "crc of header (reported%s): 0x%04x",
			(d->is_v23)?"":", hypothetical", (unsigned int)crc_reported);
		crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_XMODEM);
		de_crcobj_addslice(crco, c->infile, 0, 124);
		crc_calc = de_crcobj_getval(crco);
		de_crcobj_destroy(crco);
		de_dbg(c, "crc of header (calculated): 0x%04x", (unsigned int)crc_calc);

		if(d->is_v23 && crc_reported!=0 && crc_calc!=crc_reported) {
			de_warn(c, "MacBinary header CRC check failed");
		}
	}

	pos += 2; // Reserved for computer type and OS ID
done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x5(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %edx
leaq 0xe8b64(%rip), %rsi     # 0x1ba3d0
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x0, %eax
je 0xd1897
movq -0x8(%rbp), %rdi
leaq 0xe8b5a(%rip), %rsi     # 0x1ba3e5
movb $0x0, %al
callq 0x15bc10
jmp 0xd1f78
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x7a, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x6(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %edx
leaq 0xe8b3e(%rip), %rsi     # 0x1ba403
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x81, %eax
je 0xd18ea
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x82, %eax
jne 0xd18f5
movq -0x10(%rbp), %rax
movl $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x81, %eax
jl 0xd1939
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x7b, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x7(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x7(%rax), %edx
leaq 0xe8ae6(%rip), %rsi     # 0x1ba418
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x28(%rbp)
cmpq $0x1, -0x28(%rbp)
jl 0xd19c6
cmpq $0x3f, -0x28(%rbp)
jg 0xd19c6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x1, %r8d
callq 0x1664c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0x108(%rbp), %rdi
movq %rax, %rdx
leaq 0xf5bc6(%rip), %rsi     # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
jmp 0xd19de
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0xe8a62(%rip), %rsi     # 0x1ba439
movb $0x0, %al
callq 0x15bc10
movq -0x30(%rbp), %rax
addq $0x3f, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xe8a67(%rip), %rsi     # 0x1ba45c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
leaq -0x80(%rbp), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x169860
movq -0x8(%rbp), %rdi
leaq -0x80(%rbp), %rdx
addq $0x10, %rdx
leaq 0xdbfa5(%rip), %rsi     # 0x1ad9df
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movl -0x80(%rbp), %ecx
movl %ecx, 0x90(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x7b(%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x169860
movq -0x8(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
addq $0x10, %rdx
leaq 0xee29a(%rip), %rsi     # 0x1bfd32
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movl -0xb0(%rbp), %ecx
movl %ecx, 0x94(%rax)
movq -0x18(%rbp), %rax
movb $0x1, 0x7c(%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movb $0x1, 0x7d(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0xd1b2b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0xf1(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0xf1(%rbp), %edx
leaq 0xe896b(%rip), %rsi     # 0x1ba469
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movzbl -0xf1(%rbp), %eax
movzwl %ax, %eax
shll $0x8, %eax
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x7e(%rax)
jmp 0xd1b61
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x7e(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl 0x7e(%rax), %edx
leaq 0xcebbc(%rip), %rsi     # 0x1a0716
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1653b0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1653b0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0xe88ea(%rip), %rsi     # 0x1ba48c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0xe88d7(%rip), %rsi     # 0x1ba4a6
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x19(%rbp), %edx
leaq 0xe88b4(%rip), %rsi     # 0x1ba4bb
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0xe8882(%rip), %rsi     # 0x1ba4cd
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
leaq 0xe885c(%rip), %rsi     # 0x1ba4df
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xd1cc8
movq -0x10(%rbp), %rax
movb $0x0, 0x38(%rax)
leaq -0xf0(%rbp), %rdi
leaq 0xcc8a0(%rip), %rsi     # 0x19e55c
movl $0x40, %edx
callq 0x15aa30
jmp 0xd1cfc
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x38, %rsi
callq 0x15d720
movq -0x10(%rbp), %rax
movb $0x2, 0x39(%rax)
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
leaq -0xf0(%rbp), %rsi
movl $0x40, %edx
xorl %ecx, %ecx
callq 0x15e130
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq -0xf0(%rbp), %rcx
leaq 0xe87e3(%rip), %rsi     # 0x1ba4f5
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1654b0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xd1d57
movq -0x10(%rbp), %rax
movb $0x0, 0x48(%rax)
leaq -0xf0(%rbp), %rdi
leaq 0xcc811(%rip), %rsi     # 0x19e55c
movl $0x40, %edx
callq 0x15aa30
jmp 0xd1d8b
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x48, %rsi
callq 0x15d720
movq -0x10(%rbp), %rax
movb $0x2, 0x49(%rax)
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
leaq -0xf0(%rbp), %rsi
movl $0x40, %edx
xorl %ecx, %ecx
callq 0x15e130
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq -0xf0(%rbp), %rcx
leaq 0xe876a(%rip), %rsi     # 0x1ba50b
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0xd1e06
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movb %al, -0xf2(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0xf2(%rbp), %edx
leaq 0xe8737(%rip), %rsi     # 0x1ba51e
movb $0x0, %al
callq 0x15b0d0
movzbl -0xf2(%rbp), %eax
movzwl %ax, %edx
movq -0x18(%rbp), %rax
movzwl 0x7e(%rax), %ecx
orl %edx, %ecx
movw %cx, 0x7e(%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0xe, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0xd1e61
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1651d0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0xe86e6(%rip), %rsi     # 0x1ba540
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0xd1ead
cmpl $0x0, -0x4c(%rbp)
je 0xd1f6c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
leaq 0xe86c3(%rip), %rdx     # 0x1ba582
leaq 0xfed0a(%rip), %rax     # 0x1d0bd0
cmpl $0x0, %ecx
cmovneq %rax, %rdx
movl -0x4c(%rbp), %ecx
leaq 0xe8688(%rip), %rsi     # 0x1ba55f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x20, %esi
callq 0x15eac0
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x7c, %ecx
callq 0x15f500
movq -0x100(%rbp), %rdi
callq 0x15f340
movl %eax, -0x50(%rbp)
movq -0x100(%rbp), %rdi
callq 0x15f2e0
movq -0x8(%rbp), %rdi
movl -0x50(%rbp), %edx
leaq 0xe8658(%rip), %rsi     # 0x1ba591
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0xd1f6a
cmpl $0x0, -0x4c(%rbp)
je 0xd1f6a
movl -0x50(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0xd1f6a
movq -0x8(%rbp), %rdi
leaq 0xe8651(%rip), %rsi     # 0x1ba5b4
movb $0x0, %al
callq 0x15bc10
jmp 0xd1f6c
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)