// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/fnt.c
// Function: do_read_header
static int do_read_header(deark *c, lctx *d)
{
	i64 dfMaxWidth;
	int retval = 0;
	int saved_indent_level;
	u8 need_errmsg = 0;
	de_ucstring *s = NULL;

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "fixed header at %d", (int)0);
	de_dbg_indent(c, 1);

	d->fnt_version = (unsigned int)de_getu16le(0);
	de_dbg(c, "dfVersion: 0x%04x", d->fnt_version);
	if(d->fnt_version!=0x100 && d->fnt_version!=0x200 && d->fnt_version!=0x300) {
		de_err(c, "This version of FNT (0x%04x) is not supported", d->fnt_version);
		goto done;
	}

	s = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, 6, 60, s, DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
	de_dbg(c, "copyright: \"%s\"", ucstring_getpsz_d(s));

	d->dfType = (unsigned int)de_getu16le(66);
	d->is_vector = (d->dfType&0x1)?1:0;
	de_dbg(c, "dfType: 0x%04x (%s)", d->dfType, d->is_vector?"vector":"bitmap");

	d->dfPoints = (unsigned int)de_getu16le(68);
	de_dbg(c, "dfPoints: %u", d->dfPoints);

	d->dfPixWidth = de_getu16le(86);
	de_dbg(c, "dfPixWidth: %d", (int)d->dfPixWidth);
	d->dfPixHeight = de_getu16le(88);
	d->char_height = d->dfPixHeight;
	de_dbg(c, "dfPixHeight: %d", (int)d->dfPixHeight);

	d->dfCharSet = de_getbyte(85);
	de_dbg(c, "charset: 0x%02x (%s)", (int)d->dfCharSet,
		fmtutil_get_windows_charset_name(d->dfCharSet));
	if(d->dfCharSet==0x00) { // "ANSI"
		d->encoding = DE_ENCODING_WINDOWS1252; // Guess
	}
	else if(d->dfCharSet==0xff) { // "OEM"
		d->encoding = DE_ENCODING_CP437_G; // Guess
	}
	else {
		d->encoding = DE_ENCODING_UNKNOWN;
	}

	dfMaxWidth = de_getu16le(93);
	de_dbg(c, "dfMaxWidth: %d", (int)dfMaxWidth);

	if(d->dfPixWidth!=dfMaxWidth && d->dfPixWidth!=0) {
		de_warn(c, "dfMaxWidth (%d) does not equal dfPixWidth (%d)",
			(int)dfMaxWidth, (int)d->dfPixWidth);
	}

	d->first_char = de_getbyte(95);
	de_dbg(c, "first char: %d", (int)d->first_char);
	d->last_char = de_getbyte(96);
	de_dbg(c, "last char: %d", (int)d->last_char);
	if(d->last_char < d->first_char) {
		need_errmsg = 1;
		goto done;
	}

	// 97 = dfDefaultChar
	// 98 = dfBreakChar

	d->dfWidthBytes = de_getu16le(99);
	de_dbg(c, "dfWidthBytes: %d%s", (int)d->dfWidthBytes,
		((d->fnt_version>=0x200 || d->is_vector) ? " [unused]":""));
	// 101-104 = dfDevice

	d->dfFace = de_getu32le(105);
	de_dbg(c, "dfFace: %u", (unsigned int)d->dfFace);

	// 109-112 = dfBitsPointer
	d->dfBitsOffset = de_getu32le(113);
	de_dbg(c, "dfBitsOffset: %"I64_FMT, d->dfBitsOffset);

	// Apparently, the first 117 bytes (through the dfBitsOffset field) are
	// common to all versions

	retval = 1;
done:
	if(need_errmsg) {
		de_err(c, "Invalid or unsupported FNT file");
	}
	ucstring_destroy(s);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x1c(%rbp)
movb $0x0, -0x21(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
leaq 0x13c2fd(%rip), %rsi    # 0x1aa9b8
xorl %edx, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165280
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %edx
leaq 0x13c2cf(%rip), %rsi    # 0x1aa9cb
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x100, (%rax)          # imm = 0x100
je 0x6e744
movq -0x10(%rbp), %rax
cmpl $0x200, (%rax)          # imm = 0x200
je 0x6e744
movq -0x10(%rbp), %rax
cmpl $0x300, (%rax)          # imm = 0x300
je 0x6e744
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %edx
leaq 0x13c2a5(%rip), %rsi    # 0x1aa9dd
movb $0x0, %al
callq 0x15bfb0
jmp 0x6eb43
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rcx
movl $0x6, %esi
movl $0x3c, %edx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0x172650
movq -0x48(%rbp), %rdi
movq %rax, %rdx
leaq 0x13c277(%rip), %rsi    # 0x1aaa0b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x42, %esi
callq 0x165280
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
andl $0x1, %edx
xorl %ecx, %ecx
movl $0x1, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x7c(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x10(%rbp), %rax
movl 0x7c(%rax), %esi
leaq 0x134eb7(%rip), %rcx    # 0x1a36a4
leaq 0x13c67b(%rip), %rax    # 0x1aae6f
cmpl $0x0, %esi
cmovneq %rax, %rcx
leaq 0x13c219(%rip), %rsi    # 0x1aaa1b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x44, %esi
callq 0x165280
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x6c(%rax), %edx
leaq 0x13c1f9(%rip), %rsi    # 0x1aaa2f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x56, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl %eax, %edx
leaq 0x13c1cd(%rip), %rsi    # 0x1aaa3c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x58, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %edx
leaq 0x13c193(%rip), %rsi    # 0x1aaa4b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x55, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x78(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x78(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x78(%rax), %edi
callq 0x174f50
movq -0x40(%rbp), %rdi
movl -0x34(%rbp), %edx
movq %rax, %rcx
leaq 0x13922c(%rip), %rsi    # 0x1a7b37
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x78(%rax), %eax
cmpl $0x0, %eax
jne 0x6e92f
movq -0x10(%rbp), %rax
movl $0x9, 0x84(%rax)
jmp 0x6e95e
movq -0x10(%rbp), %rax
movzbl 0x78(%rax), %eax
cmpl $0xff, %eax
jne 0x6e94e
movq -0x10(%rbp), %rax
movl $0xd, 0x84(%rax)
jmp 0x6e95c
movq -0x10(%rbp), %rax
movl $0x0, 0x84(%rax)
jmp 0x6e95e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x5d, %esi
callq 0x165280
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x13c0d6(%rip), %rsi    # 0x1aaa5b
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
cmpq -0x18(%rbp), %rax
je 0x6e9c7
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x6e9c7
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl %eax, %ecx
leaq 0x13c0aa(%rip), %rsi    # 0x1aaa6a
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x5f, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x40(%rax), %edx
leaq 0x13c0a4(%rip), %rsi    # 0x1aaa99
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x60, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x41(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x41(%rax), %edx
leaq 0x13c1f9(%rip), %rsi    # 0x1aac23
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x41(%rax), %eax
movq -0x10(%rbp), %rcx
movzbl 0x40(%rcx), %ecx
cmpl %ecx, %eax
jge 0x6ea4e
movb $0x1, -0x21(%rbp)
jmp 0x6eb43
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x63, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rcx
movb $0x1, %al
cmpl $0x200, (%rcx)          # imm = 0x200
movb %al, -0x49(%rbp)
jae 0x6ea9d
movq -0x10(%rbp), %rax
cmpl $0x0, 0x7c(%rax)
setne %al
movb %al, -0x49(%rbp)
movl -0x50(%rbp), %edx
movq -0x58(%rbp), %rdi
movb -0x49(%rbp), %sil
leaq 0x162121(%rip), %rcx    # 0x1d0bd0
leaq 0x12e4ce(%rip), %rax    # 0x19cf84
testb $0x1, %sil
cmovneq %rax, %rcx
leaq 0x13bfe3(%rip), %rsi    # 0x1aaaa8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x69, %esi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl %eax, %edx
leaq 0x13bfbd(%rip), %rsi    # 0x1aaabb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x71, %esi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdx
leaq 0x13bf91(%rip), %rsi    # 0x1aaac6
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x1c(%rbp)
cmpb $0x0, -0x21(%rbp)
je 0x6eb5b
movq -0x8(%rbp), %rdi
leaq 0x13bf84(%rip), %rsi    # 0x1aaad8
movb $0x0, %al
callq 0x15bfb0
movq -0x30(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x15b5a0
movl -0x1c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)