// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/rar.c
// Function: do_rar4_block_fileheader
static void do_rar4_block_fileheader(deark *c, de_arch_lctx *d, struct rar4_block *rb)
{
	struct de_arch_member_data *md = NULL;
	i64 pos;
	i64 fnlen;
	u32 filecrc_reported;
	UI attribs;
	u8 os;
	u8 b;

	md = de_arch_create_md(c, d);

	pos = rb->data1_pos;

	md->cmpr_pos = rb->data2_pos;
	md->cmpr_len = rb->block_size_2 + rb->block_size_high;
	de_arch_read_field_orig_len_p(md, &pos);

	os = de_getbyte_p(&pos);
	de_dbg(c, "OS: %u (%s)", (UI)os, rar4_get_OS_name(os));

	filecrc_reported = (u32)de_getu32le_p(&pos);
	de_dbg(c, "file crc: 0x%08x", (UI)filecrc_reported);

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

	b = de_getbyte_p(&pos);
	de_dbg(c, "min ver needed to unpack: %u", (UI)b);

	md->cmpr_meth = (UI)de_getbyte_p(&pos);
	de_dbg(c, "cmpr. method: 0x%02x (%s)", md->cmpr_meth, rar4_get_cmpr_name((u8)md->cmpr_meth));

	fnlen = de_getu16le_p(&pos);

	attribs = (UI)de_getu32le_p(&pos);
	de_dbg(c, "attribs: 0x%08x", attribs);
	if(os==0 || os==1 || os==2) {
		de_dbg_indent(c, 1);
		de_arch_handle_field_dos_attr(md, (attribs & 0xff));
		de_dbg_indent(c, -1);
	}

	if(rb->flags & 0x0100) {
		pos += 4; // HIGH_PACK_SIZE, already read
		pos += 4; // TODO: HIGH_UNP_SIZE
	}

	// TODO: Handle Unicode filenames (flags & 0x0200).
	// - If the name field contains a NUL byte, a Unicode name follows (but how
	//   is it encoded?).
	// - If there is no NUL byte, the entire filename is UTF-8.
	dbuf_read_to_ucstring_n(c->infile, pos, fnlen, 2048, md->filename,
		DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
	de_dbg(c, "%sname: \"%s\"", (rb->type==0x7a?"":"file"), ucstring_getpsz_d(md->filename));
	pos += fnlen;

	if(rb->flags & 0x0400) {
		pos += 8; // salt (first documented in v3.00)
	}

	if((rb->flags & 0x1008)==0x1008) {
		// Ext time and comment shouldn't both be present.
		// Ext time first documented in v3.40, which is also when the docs
		// started saying that RAR 3.x doesn't set the 0x0008 flag.
		goto done;
	}
	if(rb->type==0x7a && (rb->flags & 0x0008)) {
		// A new subblock can't have an old comment
		goto done;
	}

	// TODO: ext time (rb->flags & 0x1000)

	// Old-style comment -- A nested block
	if(rb->flags & 0x0008) {
		struct rar4_block *cmt_rb;

		cmt_rb = de_malloc(c, sizeof(struct rar4_block));
		cmt_rb->block_pos = pos;
		cmt_rb->block_max_endpos = rb->data2_pos;
		cmt_rb->parent_type = rb->type;
		rar_read_v4_block(c, d, cmt_rb);
		pos += cmt_rb->block_size_full;
		rar4_free_block(c, cmt_rb);
	}

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

done:
	de_arch_destroy_md(c, md);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x17c120
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x18(%rbp), %rax
addq 0x40(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x17c350
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movb %al, -0x39(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movzbl -0x39(%rbp), %eax
movl %eax, -0x5c(%rbp)
movzbl -0x39(%rbp), %edi
callq 0x1520c0
movq -0x68(%rbp), %rdi
movl -0x5c(%rbp), %edx
movq %rax, %rcx
leaq 0x5228d(%rip), %rsi     # 0x1a3e3b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165580
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %edx
leaq 0x7f3e7(%rip), %rsi     # 0x1d0fbe
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rsi
addq $0x18, %rsi
leaq 0x522fe(%rip), %rdx     # 0x1a3ef3
movl $0x4, %ecx
leaq -0x28(%rbp), %r8
callq 0x17c580
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movb %al, -0x3a(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x3a(%rbp), %edx
leaq 0x7f19c(%rip), %rsi     # 0x1d0dc2
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movzbl %al, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x90(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movl 0x90(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movl 0x90(%rax), %eax
movzbl %al, %edi
callq 0x152160
movq -0x58(%rbp), %rdi
movl -0x4c(%rbp), %edx
movq %rax, %rcx
leaq 0x7f34c(%rip), %rsi     # 0x1d0fcf
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165580
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %edx
leaq 0x7f328(%rip), %rsi     # 0x1d0fe9
movb $0x0, %al
callq 0x15b0d0
movzbl -0x39(%rbp), %eax
cmpl $0x0, %eax
je 0x151ce3
movzbl -0x39(%rbp), %eax
cmpl $0x1, %eax
je 0x151ce3
movzbl -0x39(%rbp), %eax
cmpl $0x2, %eax
jne 0x151d11
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x20(%rbp), %rdi
movl -0x38(%rbp), %esi
andl $0xff, %esi
callq 0x17c4a0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x151d3a
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x20(%rbp), %rax
movq 0x58(%rax), %r8
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl $0x800, %ecx            # imm = 0x800
movl $0x1, %r9d
movl %eax, (%rsp)
callq 0x166a40
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x18(%rax), %edx
leaq 0x83084(%rip), %rax     # 0x1d4e07
leaq 0x7ee46(%rip), %rcx     # 0x1d0bd0
cmpl $0x7a, %edx
cmoveq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x172650
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rdx
movq %rax, %rcx
leaq 0x7f245(%rip), %rsi     # 0x1d0ff9
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
je 0x151de4
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x1008, %eax           # imm = 0x1008
cmpl $0x1008, %eax           # imm = 0x1008
jne 0x151dfc
jmp 0x151eb7
movq -0x18(%rbp), %rax
movzbl 0x18(%rax), %eax
cmpl $0x7a, %eax
jne 0x151e1d
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x151e1d
jmp 0x151eb7
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x151e95
movq -0x8(%rbp), %rdi
movl $0x50, %esi
callq 0x15b780
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movb 0x18(%rax), %cl
movq -0x48(%rbp), %rax
movb %cl, 0x19(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x1511d0
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x1511a0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rcx
leaq 0x7ef62(%rip), %rsi     # 0x1d0e12
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x17c1a0
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)