// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/sis.c
// Function: do_file_header
static int do_file_header(deark *c, lctx *d, i64 pos1)
{
	i64 pos = pos1;
	i64 k;
	i64 n, n2;
	int retval = 0;
	u32 crc_even;
	u32 crc_odd;
	de_ucstring *options_descr = NULL;
	struct de_crcobj *crco = NULL;
	u8 tmpbuf[12];

	de_dbg(c, "file header at %d", (int)pos);

	de_dbg_indent(c, 1);

	// Pre-read the first 12 bytes, to calculate some CRCs for later.
	de_read(tmpbuf, pos, 12);

	crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_XMODEM);
	for(k=0; k<12; k+=2) {
		de_crcobj_addbuf(crco, &tmpbuf[k], 1);
	}
	crc_even = de_crcobj_getval(crco);
	de_crcobj_reset(crco);
	for(k=1; k<12; k+=2) {
		de_crcobj_addbuf(crco, &tmpbuf[k], 1);
	}
	crc_odd = de_crcobj_getval(crco);
	de_crcobj_destroy(crco);
	crco = NULL;

	n = de_getu32le_p(&pos);
	de_dbg(c, "UID 1: 0x%08x", (unsigned int)n);

	n = de_getu32le_p(&pos);
	de_dbg(c, "UID 2: 0x%08x", (unsigned int)n);
	if(n==0x10003a12) {
		d->is_rel6 = 1;
	}

	n = de_getu32le_p(&pos);
	de_dbg(c, "UID 3: 0x%08x", (unsigned int)n);

	n = de_getu32le_p(&pos);
	de_dbg(c, "UID 4: 0x%08x", (unsigned int)n);
	// The way UID 4 is calculated is really silly.
	de_dbg(c, "expected value of UID 4: 0x%04x%04x",
		(unsigned int)crc_odd, (unsigned int)crc_even);

	if(d->is_rel6) {
		de_declare_fmt(c, "SIS, EPOC r6");
	}
	else {
		de_declare_fmt(c, "SIS, EPOC r3/4/5");
	}

	pos += 2; // checksum

	d->nlangs = de_getu16le_p(&pos);
	de_dbg(c, "num languages: %d", (int)d->nlangs);

	d->nfiles = de_getu16le_p(&pos);
	de_dbg(c, "num files: %d", (int)d->nfiles);

	d->nrequisites = de_getu16le_p(&pos);
	de_dbg(c, "num requisites: %d", (int)d->nrequisites);

	pos += 2; // installation language
	pos += 2; // installation files
	pos += 2; // installation drive

	n = de_getu16le_p(&pos);
	de_dbg(c, "num capabilities: %d", (int)n);

	d->installer_ver = de_getu32le_p(&pos);
	de_dbg(c, "installer ver: %d", (int)d->installer_ver);
	if(d->installer_ver<68) {
		de_warn(c, "Unknown version: %d", (int)d->installer_ver);
	}

	d->options = (unsigned int)de_getu16le_p(&pos);
	options_descr = ucstring_create(c);
	if(d->options&0x01) ucstring_append_flags_item(options_descr, "IsUnicode");
	if(d->options&0x02) ucstring_append_flags_item(options_descr, "IsDistributable");
	if(d->options&0x08) ucstring_append_flags_item(options_descr, "NoCompress");
	if(d->options&0x10) ucstring_append_flags_item(options_descr, "ShutdownApps");
	de_dbg(c, "options: 0x%04x (%s)", d->options, ucstring_getpsz(options_descr));
	if(d->is_rel6 && !(d->options&0x0008)) {
		d->files_are_compressed = 1;
	}

	pos += 2; // type (TODO)
	n = de_getu16le_p(&pos);
	n2 = de_getu16le_p(&pos);
	de_dbg(c, "app version: %d,%d", (int)n, (int)n2);
	pos += 4; // variant

	d->languages_ptr = de_getu32le_p(&pos);
	de_dbg(c, "languages ptr: %"I64_FMT, d->languages_ptr);
	d->files_ptr = de_getu32le_p(&pos);
	de_dbg(c, "files ptr: %"I64_FMT, d->files_ptr);

	d->requisites_ptr = de_getu32le_p(&pos);
	de_dbg(c, "requisites ptr: %"I64_FMT, d->requisites_ptr);
	d->certificates_ptr = de_getu32le_p(&pos);
	de_dbg(c, "certificates ptr: %"I64_FMT, d->certificates_ptr);
	d->component_name_ptr = de_getu32le_p(&pos);
	de_dbg(c, "component name ptr: %"I64_FMT, d->component_name_ptr);

	if(d->is_rel6) {
		n = de_getu32le_p(&pos);
		de_dbg(c, "signature ptr: %"I64_FMT, n);
		n = de_getu32le_p(&pos);
		de_dbg(c, "capabilities ptr: %"I64_FMT, n);
	}

	retval = 1;
	de_dbg_indent(c, -1);
	ucstring_destroy(options_descr);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x5d63f(%rip), %rsi     # 0x19fe73
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x64(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0xc, %ecx
callq 0x164d10
movq -0x8(%rbp), %rdi
movl $0x20, %esi
callq 0x15eac0
movq %rax, -0x58(%rbp)
movq $0x0, -0x28(%rbp)
cmpq $0xc, -0x28(%rbp)
jge 0x1428a8
movq -0x58(%rbp), %rdi
leaq -0x64(%rbp), %rsi
addq -0x28(%rbp), %rsi
movl $0x1, %edx
callq 0x15f430
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
jmp 0x14287d
movq -0x58(%rbp), %rdi
callq 0x15f340
movl %eax, -0x40(%rbp)
movq -0x58(%rbp), %rdi
callq 0x15f250
movq $0x1, -0x28(%rbp)
cmpq $0xc, -0x28(%rbp)
jge 0x1428f0
movq -0x58(%rbp), %rdi
leaq -0x64(%rbp), %rsi
addq -0x28(%rbp), %rsi
movl $0x1, %edx
callq 0x15f430
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
jmp 0x1428c5
movq -0x58(%rbp), %rdi
callq 0x15f340
movl %eax, -0x44(%rbp)
movq -0x58(%rbp), %rdi
callq 0x15f2e0
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x8bb0b(%rip), %rsi     # 0x1ce43e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x8baec(%rip), %rsi     # 0x1ce44c
movb $0x0, %al
callq 0x15b0d0
cmpq $0x10003a12, -0x30(%rbp) # imm = 0x10003A12
jne 0x142979
movq -0x10(%rbp), %rax
movb $0x1, 0xc(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x8babb(%rip), %rsi     # 0x1ce45a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x8ba9c(%rip), %rsi     # 0x1ce468
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x40(%rbp), %ecx
leaq 0x8ba92(%rip), %rsi     # 0x1ce476
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpb $0x0, 0xc(%rax)
je 0x142a07
movq -0x8(%rbp), %rdi
leaq 0x8ba9a(%rip), %rsi     # 0x1ce49a
callq 0x15e380
jmp 0x142a17
movq -0x8(%rbp), %rdi
leaq 0x8ba95(%rip), %rsi     # 0x1ce4a7
callq 0x15e380
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
leaq 0x8ba64(%rip), %rsi     # 0x1ce4b8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x8ba3e(%rip), %rsi     # 0x1ce4ca
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
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 0x8ba14(%rip), %rsi     # 0x1ce4d8
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x8b9d6(%rip), %rsi     # 0x1ce4eb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %eax, %edx
leaq 0x8b9b5(%rip), %rsi     # 0x1ce500
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x44, (%rax)
jge 0x142b77
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %eax, %edx
leaq 0x8b9a2(%rip), %rsi     # 0x1ce512
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x142bbd
movq -0x50(%rbp), %rdi
leaq 0x8b96e(%rip), %rsi     # 0x1ce526
callq 0x172680
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x142bdc
movq -0x50(%rbp), %rdi
leaq 0x8b959(%rip), %rsi     # 0x1ce530
callq 0x172680
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x142bfb
movq -0x50(%rbp), %rdi
leaq 0x8b94a(%rip), %rsi     # 0x1ce540
callq 0x172680
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x142c1a
movq -0x50(%rbp), %rdi
leaq 0x8b936(%rip), %rsi     # 0x1ce54b
callq 0x172680
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x68(%rbp)
movq -0x50(%rbp), %rdi
callq 0x172500
movq -0x70(%rbp), %rdi
movl -0x68(%rbp), %edx
movq %rax, %rcx
leaq 0x8b912(%rip), %rsi     # 0x1ce558
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0xc(%rax), %eax
cmpl $0x0, %eax
je 0x142c71
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x142c71
movq -0x10(%rbp), %rax
movb $0x1, 0xd(%rax)
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0x8b8af(%rip), %rsi     # 0x1ce56d
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x8b880(%rip), %rsi     # 0x1ce580
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
leaq 0x8b85d(%rip), %rsi     # 0x1ce593
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdx
leaq 0x8b836(%rip), %rsi     # 0x1ce5a2
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdx
leaq 0x8b814(%rip), %rsi     # 0x1ce5b6
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdx
leaq 0x8b7f4(%rip), %rsi     # 0x1ce5cc
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpb $0x0, 0xc(%rax)
je 0x142e3f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x8b7d7(%rip), %rsi     # 0x1ce5e4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x8b7bf(%rip), %rsi     # 0x1ce5f7
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x50(%rbp), %rdi
callq 0x171750
movl -0x3c(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)