// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
movq %rdi, %rbx
leaq 0xce4db(%rip), %rsi     # 0x10d62c
xorl %r12d, %r12d
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe600b
movq %rax, %r14
testq %rax, %rax
je 0x3f41d
xorl %r12d, %r12d
movq %r14, %rdi
xorl %esi, %esi
callq 0xe3fa3
movq %rax, %r15
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0xe3fa3
movq %rax, %rbp
leaq 0xe86f5(%rip), %rsi     # 0x127897
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0xe3fa3
leaq 0xe589e(%rip), %rsi     # 0x124a5e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x6
popq %rsi
movq %r14, %rdi
callq 0xe3eb4
movl %eax, %r13d
leaq 0xc8fb7(%rip), %rsi     # 0x108199
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x7
popq %rsi
movq %r14, %rdi
callq 0xe3eb4
movl %eax, %ebp
movzbl %al, %edx
leaq 0xce40a(%rip), %rsi     # 0x10d60f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpq $0x4841, %r15           # imm = 0x4841
jne 0x3f275
cmpb $0xa, %r13b
jne 0x3f275
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0xe3fa3
leaq 0xce409(%rip), %rsi     # 0x10d639
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
pushq $0xa
popq %rsi
movq %r14, %rdi
callq 0xe3fa3
leaq 0xce3fc(%rip), %rsi     # 0x10d64a
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
testb %bpl, %bpl
je 0x3f28b
leaq 0xce3fa(%rip), %rsi     # 0x10d660
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
jmp 0x3f417
leaq 0xce3a8(%rip), %rsi     # 0x10d624
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x3f41d
pushq $0xc
popq %rsi
movq %r14, %rdi
callq 0xe3fa3
leaq 0xce3f0(%rip), %rsi     # 0x10d68d
movq %rbx, %rdi
movq %rax, 0x8(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
pushq $0xe
popq %r12
leaq 0xce3df(%rip), %rbp     # 0x10d698
xorl %r13d, %r13d
cmpq $0x14, %r12
je 0x3f2ff
movq %r14, %rdi
movq %r12, %rsi
callq 0xe3fa3
movq %rax, %r15
movq %rax, 0x8(%rsp,%r12,4)
movq %rbx, %rdi
movq %rbp, %rsi
movl %r13d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xde896
testq %r15, %r15
jg 0x3f2f6
movq $0x1, 0x8(%rsp,%r12,4)
addq $0x2, %r12
incl %r13d
jmp 0x3f2bc
leaq 0x18(%rsp), %r12
movq $0x28, (%r12)
movl $0xff, %eax
movq 0x8(%rsp), %rcx
cmpq %rax, %rcx
cmovlq %rcx, %rax
testq %rax, %rax
pushq $-0x1
popq %rcx
cmovnsq %rax, %rcx
incq %rcx
movq %rcx, 0x8(%rsp)
xorl %r13d, %r13d
cmpq 0x8(%rsp), %r13
je 0x3f40e
movq 0x18(%rsp), %rdi
movq %rdi, %rax
cqto
movl $0x200, %ecx            # imm = 0x200
idivq %rcx
cmpq $0x1fb, %rdx            # imm = 0x1FB
jl 0x3f367
movl $0x200, %esi            # imm = 0x200
callq 0xdfbb6
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
cmpq $0x3, %rbp
je 0x3f39e
movq %r14, %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, 0x20(%rsp,%rbp,8)
movq 0x40(%rsp,%rbp,8), %rdi
cmpq $0x100, %rdi            # imm = 0x100
jl 0x3f395
movq %rax, %rsi
callq 0xe1d3e
movb %al, 0x5(%rsp,%rbp)
incq %rbp
jmp 0x3f369
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 0x10(%rsp), %rbp
movl %eax, 0x18(%rbp,%r13,4)
movl 0x20(%rsp), %ecx
movl 0x28(%rsp), %r8d
movl 0x30(%rsp), %r9d
leaq 0x60(%rsp), %r15
movq %r15, %rdi
pushq $0x40
popq %rsi
leaq 0xce2c2(%rip), %rdx     # 0x10d6a8
xorl %eax, %eax
callq 0xde40c
movl 0x18(%rbp,%r13,4), %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xdf153
incq %r13
jmp 0x3f332
movq 0x10(%rsp), %rax
movb $0x1, 0x10(%rax)
pushq $0x1
popq %rax
movl %eax, %r12d
movq %r14, %rdi
callq 0xe5591
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movl %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq