// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/drhalo.c
// Function: do_read_pal_file
static int do_read_pal_file(deark *c, lctx *d, const char *palfn)
{
	dbuf *palfile = NULL;
	i64 pos;
	i64 sig;
	i64 filever;
	i64 datasize;
	i64 k, z;
	i64 num_entries;
	i64 maxidx;
	i64 maxsamp[3];
	UI board_id;
	UI graphics_mode;
	u8 filetype;
	u8 filesubtype;
	i64 osamp[3];
	u8 samp[3];
	int retval = 0;
	char tmps[64];

	de_dbg(c, "palette file");
	de_dbg_indent(c, 1);

	palfile = dbuf_open_input_file(c, palfn);
	if(!palfile) {
		goto done;
	}

	sig = dbuf_getu16le(palfile, 0);
	filever = dbuf_getu16le(palfile, 2);
	de_dbg(c, "file version: %d", (int)filever);
	datasize = dbuf_getu16le(palfile, 4);
	de_dbg(c, "data size: %"I64_FMT, datasize);
	filetype = dbuf_getbyte(palfile, 6);
	de_dbg(c, "file type: 0x%02x", (UI)filever);
	filesubtype = dbuf_getbyte(palfile, 7);
	de_dbg(c, "file subtype: 0x%02x", (UI)filesubtype);

	if(sig!=0x4841 /* "HA" */ || filetype!=0x0a) {
		de_err(c, "Invalid palette file");
		goto done;
	}

	board_id = (UI)dbuf_getu16le(palfile, 8);
	de_dbg(c, "board id: 0x%04x", board_id);
	graphics_mode = (UI)dbuf_getu16le(palfile, 10);
	de_dbg(c, "graphics mode: 0x%04x", graphics_mode);

	if(filesubtype!=0) {
		de_warn(c, "Hardware-specific palettes are not supported");
		retval = 1;
		goto done;
	}

	maxidx = dbuf_getu16le(palfile, 0x0c);
	de_dbg(c, "maxidx: %u", (UI)maxidx);

	for(k=0; k<3; k++) {
		maxsamp[k] = dbuf_getu16le(palfile, 0x0e + 2*k);
		de_dbg(c, "maxsamp[%d]: %u", (int)k, (UI)maxsamp[k]);
		if(maxsamp[k]<1) maxsamp[k]=1;
	}

	pos = 0x14;

	pos += 20; // Skip palette name  TODO: Display this

	num_entries = maxidx+1;
	if(num_entries>256) num_entries=256;

	for(k=0; k<num_entries; k++) {
		// If we imagine the palette file being split into 512-byte chunks, a
		// (6-byte) palette entry is not allowed to cross a chunk boundary.
		// If an entry would do so, it instead starts at the beginning of the
		// next chunk.
		if((pos%512) > 506) {
			pos = de_pad_to_n(pos, 512);
		}

		for(z=0; z<3; z++) {
			osamp[z] = dbuf_getu16le_p(palfile, &pos);
			// I think portable palette samples are always in the range 0-255,
			// regardless of the maxsamp fields.
			if(maxsamp[z]>255) {
				samp[z] = de_scale_n_to_255(maxsamp[z], osamp[z]);
			}
			else {
				samp[z] = (u8)osamp[z];
			}
		}
		d->pal[k] = DE_MAKE_RGB(samp[0], samp[1], samp[2]);

		de_snprintf(tmps, sizeof(tmps), "(%5d,%5d,%5d) "DE_CHAR_RIGHTARROW" ",
			(int)osamp[0], (int)osamp[1], (int)osamp[2]);
		de_dbg_pal_entry2(c, k, d->pal[k], tmps, NULL, NULL);
	}

	d->have_palfile = 1;
	retval = 1;
done:
	dbuf_close(palfile);
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
leaq 0xd96ff(%rip), %rsi     # 0x11a5fc
xorl %r12d, %r12d
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf1717
movq %rax, %r14
testq %rax, %rax
je 0x411e2
xorl %r12d, %r12d
movq %r14, %rdi
xorl %esi, %esi
callq 0xef1ca
movq %rax, %r15
movl $0x2, %esi
movq %r14, %rdi
callq 0xef1ca
movq %rax, %rbp
leaq 0xf3825(%rip), %rsi     # 0x134777
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movl $0x4, %esi
movq %r14, %rdi
callq 0xef1ca
leaq 0xf0a54(%rip), %rsi     # 0x1319c6
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movl $0x6, %esi
movq %r14, %rdi
callq 0xef0d5
movl %eax, %r13d
leaq 0xd41df(%rip), %rsi     # 0x115175
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movl $0x7, %esi
movq %r14, %rdi
callq 0xef0d5
movl %eax, %ebp
movzbl %al, %edx
leaq 0xd9624(%rip), %rsi     # 0x11a5df
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpq $0x4841, %r15           # imm = 0x4841
jne 0x4102f
cmpb $0xa, %r13b
jne 0x4102f
movl $0x8, %esi
movq %r14, %rdi
callq 0xef1ca
leaq 0xd9621(%rip), %rsi     # 0x11a609
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movl $0xa, %esi
movq %r14, %rdi
callq 0xef1ca
leaq 0xd9612(%rip), %rsi     # 0x11a61a
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
testb %bpl, %bpl
je 0x41045
leaq 0xd9610(%rip), %rsi     # 0x11a630
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x411dc
leaq 0xd95be(%rip), %rsi     # 0x11a5f4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x411e2
movl $0xc, %esi
movq %r14, %rdi
callq 0xef1ca
leaq 0xd9604(%rip), %rsi     # 0x11a65d
movq %rbx, %rdi
movq %rax, 0x8(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movl $0xe, %r13d
leaq 0xd95f1(%rip), %rbp     # 0x11a668
xorl %r12d, %r12d
movq %r14, %rdi
movq %r13, %rsi
callq 0xef1ca
movq %rax, %r15
movq %rax, 0x8(%rsp,%r13,4)
movq %rbx, %rdi
movq %rbp, %rsi
movl %r12d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe9226
testq %r15, %r15
jg 0x410ae
movq $0x1, 0x8(%rsp,%r13,4)
addq $0x2, %r13
incl %r12d
cmpq $0x14, %r13
jne 0x4107a
movq $0x28, 0x10(%rsp)
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
js 0x411d3
movl $0xff, %eax
cmpq %rax, %rcx
cmovlq %rcx, %rax
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %r12
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
leaq 0x1ff(%rdi), %rax
testq %rdi, %rdi
cmovnsq %rdi, %rax
andq $-0x200, %rax           # imm = 0xFE00
movq %rdi, %rcx
subq %rax, %rcx
cmpq $0x1fb, %rcx            # imm = 0x1FB
jl 0x41122
movl $0x200, %esi            # imm = 0x200
callq 0xea80b
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
movq %r14, %rdi
movq %r12, %rsi
callq 0xef1ed
movq %rax, 0x20(%rsp,%rbp,8)
movq 0x40(%rsp,%rbp,8), %rdi
cmpq $0x100, %rdi            # imm = 0x100
jl 0x4114a
movq %rax, %rsi
callq 0xece35
movb %al, 0x5(%rsp,%rbp)
incq %rbp
cmpq $0x3, %rbp
jne 0x41124
movzbl 0x5(%rsp), %eax
shll $0x10, %eax
movzbl 0x6(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x7(%rsp), %eax
orl %ecx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movq 0x18(%rsp), %rbp
movl %eax, 0x18(%rbp,%r13,4)
movl 0x20(%rsp), %ecx
movl 0x28(%rsp), %r8d
movl 0x30(%rsp), %r9d
movl $0x40, %esi
leaq 0x60(%rsp), %r15
movq %r15, %rdi
leaq 0xd94d7(%rip), %rdx     # 0x11a678
xorl %eax, %eax
callq 0xe8d67
movl 0x18(%rbp,%r13,4), %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe9c1e
leaq 0x1(%r13), %rax
cmpq 0x8(%rsp), %r13
movq %rax, %r13
jne 0x410eb
movq 0x18(%rsp), %rax
movb $0x1, 0x10(%rax)
movl $0x1, %r12d
movq %r14, %rdi
callq 0xf0b13
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movl %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq