// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil-cmpr.c
// Function: fmtutil_xpk_codectype1
void fmtutil_xpk_codectype1(deark *c, struct de_dfilter_in_params *dcmpri,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
	void *codec_private_params)
{
	struct xpkc_ctx *xc = NULL;
	u8 hchk_calc;

	xc = de_malloc(c, sizeof(struct xpkc_ctx));
	xc->modname = "xpk";
	xc->dcmpri = dcmpri;
	xc->dcmpro = dcmpro;
	xc->dres = dres;
	xc->inf_endpos = dcmpri->pos + dcmpri->len;

	if(dcmpri->len<12) {
		xc->errflag = 1;
		goto done;
	}
	xc->pos = dcmpri->pos;

	if((UI)dbuf_getu32be_p(dcmpri->f, &xc->pos) != CODE_XPKF) {
		xc->errflag = 1;
		goto done;
	}

	xc->cmpr_len = dbuf_getu32be_p(dcmpri->f, &xc->pos);
	de_dbg(c, "XPK cmpr len: %"I64_FMT, xc->cmpr_len);

	dbuf_read_fourcc(dcmpri->f, xc->pos, &xc->method4cc, 4, 0);
	de_dbg(c, "XPK cmpr type: '%s'", xc->method4cc.id_dbgstr);
	xc->pos += 4;

	// TODO: There's too much dbg info, except maybe when used with XPKF format.

	xc->orig_len = dbuf_getu32be_p(dcmpri->f, &xc->pos);
	de_dbg(c, "orig len: %"I64_FMT, xc->orig_len);

	xc->pos += 16;
	xc->flags = dbuf_getbyte_p(dcmpri->f, &xc->pos);
	de_dbg(c, "flags: 0x%02x", (UI)xc->flags);
	xc->hchk = dbuf_getbyte_p(dcmpri->f, &xc->pos);
	de_dbg(c, "hchk: 0x%02x", (UI)xc->hchk);

	hchk_calc = xpk_checksumh(dcmpri->f, dcmpri->pos, 36, xc->hchk);
	de_dbg(c, "hchk (calculated): 0x%02x", (UI)hchk_calc);

	xc->subvrs = dbuf_getbyte_p(dcmpri->f, &xc->pos);
	xc->masvrs = dbuf_getbyte_p(dcmpri->f, &xc->pos);
	de_dbg(c, "vers: %u.%u", (UI)xc->masvrs, (UI)xc->subvrs);

	if(hchk_calc != xc->hchk) {
		xpk_on_checksum_error(c, xc);
		goto done;
	}

	if(xc->flags & XPKSTREAMF_EXTHEADER) {
		i64 ehlen;

		ehlen = dbuf_getu16be_p(dcmpri->f, &xc->pos);
		xc->pos += ehlen;
	}

	if(!fmtutil_xpk_ismethodsupported(xc->method4cc.id)) {
		de_dfilter_set_errorf(c, dres, xc->modname, "Unsupported XPK compession method '%s'",
			xc->method4cc.id_sanitized_sz);
		goto done;
	}

	if(xc->flags & XPKSTREAMF_PASSWORD) {
		xc->errflag = 1;
		goto done;
	}

	switch(xc->method4cc.id) {
	case CODE_MASH:
		xc->codec = fmtutil_xpkMASH_codectype1;
		break;
	}

	while(1) {
		if(xc->errflag) goto done;
		xc->pos = xc->dcmpri->pos + de_pad_to_4(xc->pos - xc->dcmpri->pos);
		if(xc->pos+8 > xc->inf_endpos) goto done;
		do_xpk_chunk(c, xc);
		if(xc->curchunk.chk_type==XPKCHUNK_END) goto done;
	}

done:
	if(xc) {
		if(xc->errflag) {
			de_dfilter_set_generic_error(c, dres, xc->modname);
		}
	}
	de_free(c, xc);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, %r14
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %rbx
movl $0xa0, %esi
callq 0xdecaf
movq %rax, %r12
movq %rax, %r15
leaq 0x27544(%rip), %rax     # 0x117398
movq %rax, 0x40(%r12)
movq %r13, 0x18(%r12)
movq %rbp, 0x20(%r12)
movq %r14, 0x28(%r12)
movq 0x8(%r13), %rax
movq 0x10(%r13), %rcx
leaq (%rcx,%rax), %rdx
movq %rdx, 0x30(%r12)
cmpq $0xb, %rcx
jle 0xf03e2
movq %rax, (%r15)
movq (%r13), %rdi
movq %r15, %rsi
callq 0xe4071
cmpl $0x58504b46, %eax       # imm = 0x58504B46
jne 0xf03e2
movq (%r13), %rdi
movq %r15, %rsi
callq 0xe4071
movq %rax, 0x8(%r15)
leaq 0x4d85d(%rip), %rsi     # 0x13d711
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq (%r13), %rdi
movq (%r15), %rsi
leaq 0x48(%r12), %rdx
pushq $0x4
popq %rcx
xorl %r8d, %r8d
callq 0xe657c
leaq 0x58(%r12), %rdx
leaq 0x4d83f(%rip), %rsi     # 0x13d723
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
addq $0x4, (%r15)
movq (%r13), %rdi
movq %r15, %rsi
callq 0xe4071
movq %rax, 0x10(%r15)
leaq 0x1825e(%rip), %rsi     # 0x108167
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq $0x10, (%r15)
movq (%r13), %rdi
movq %r15, %rsi
callq 0xe3f2a
movb %al, 0x39(%r15)
movzbl %al, %edx
leaq 0x32eb2(%rip), %rsi     # 0x122de6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq (%r13), %rdi
movq %r15, %rsi
callq 0xe3f2a
movb %al, 0x3a(%r15)
movzbl %al, %edx
leaq 0x4d7df(%rip), %rsi     # 0x13d737
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq (%r13), %rdi
movq 0x8(%r13), %rsi
movzbl 0x3a(%r15), %ecx
pushq $0x24
popq %rdx
callq 0xf0442
movzbl %al, %ebp
leaq 0x4d7c3(%rip), %rsi     # 0x13d744
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq (%r13), %rdi
movq %r15, %rsi
callq 0xe3f2a
movb %al, 0x3b(%r15)
movq (%r13), %rdi
movq %r15, %rsi
callq 0xe3f2a
movb %al, 0x3c(%r15)
movzbl %al, %edx
movzbl 0x3b(%r15), %ecx
leaq 0x4d7a2(%rip), %rsi     # 0x13d75e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
cmpb 0x3a(%r15), %bpl
jne 0xf0417
testb $0x4, 0x39(%r15)
je 0xeffe6
movq (%r13), %rdi
movq %r15, %rsi
callq 0xe3f6e
addq %rax, (%r15)
movl 0x4c(%r15), %eax
cmpl $0x4d415348, %eax       # imm = 0x4D415348
je 0xefffc
cmpl $0x4e4f4e45, %eax       # imm = 0x4E4F4E45
jne 0xf0424
testb $0x2, 0x39(%r15)
jne 0xf03e2
cmpl $0x4d415348, %eax       # imm = 0x4D415348
jne 0xf0019
leaq 0x566c(%rip), %rax      # 0xf5681
movq %rax, 0x78(%r15)
subq $-0x80, %r12
cmpb $0x0, 0x38(%r15)
jne 0xf03e7
movq (%r15), %rdi
movq 0x18(%r15), %rax
movq 0x8(%rax), %r13
subq %r13, %rdi
callq 0xdfb90
leaq (%rax,%r13), %rcx
movq %rcx, (%r15)
addq %r13, %rax
addq $0x8, %rax
cmpq 0x30(%r15), %rax
jg 0xf03e7
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
andl $0x0, 0x58(%rsp)
xorl %eax, %eax
movb %al, 0x5c(%rsp)
andq $0x0, 0x60(%rsp)
movb %al, 0x68(%rsp)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0xdebae
movq (%r15), %rdx
movq %rbx, %rdi
leaq 0x4d7b8(%rip), %rsi     # 0x13d852
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r12)
movups %xmm0, (%r12)
movq (%r15), %r13
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xe3f2a
movb %al, 0x80(%r15)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x2940e(%rip), %rsi     # 0x1194ee
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xe3f2a
movb %al, 0x81(%r15)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x4d759(%rip), %rsi     # 0x13d863
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xe3f6e
movl %eax, 0x84(%r15)
movq %rbx, %rdi
leaq 0x4d758(%rip), %rsi     # 0x13d889
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
testb $0x1, 0x39(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
jne 0xf016a
callq 0xe3f6e
movq %rax, 0x88(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xe3f6e
pushq $0x8
jmp 0xf0187
callq 0xe4071
movq %rax, 0x88(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xe4071
pushq $0xc
popq %rbp
movq %rax, 0x90(%r15)
movq 0x88(%r15), %rdx
movq %rbx, %rdi
leaq 0x4d710(%rip), %rsi     # 0x13d8b0
xorl %eax, %eax
callq 0xde896
movq 0x90(%r15), %rdx
movq %rbx, %rdi
leaq 0x4d702(%rip), %rsi     # 0x13d8ba
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rax
movq (%rax), %rdi
movzbl 0x81(%r15), %ecx
movq %r13, %rsi
movq %rbp, %rdx
callq 0xf0442
movzbl %al, %ebp
movq %rbx, %rdi
leaq 0x4d6de(%rip), %rsi     # 0x13d8c4
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
cmpb 0x81(%r15), %bpl
jne 0xf02e0
movzbl (%r12), %eax
cmpl $0x2, %eax
jae 0xf02ed
movq (%r15), %rdx
movq %rdx, 0x98(%r15)
movq %rbx, %rdi
leaq 0x4d6ce(%rip), %rsi     # 0x13d8ec
xorl %eax, %eax
callq 0xde896
movq 0x88(%r15), %rdi
movq 0x98(%r15), %r13
leaq (%rdi,%r13), %rax
cmpq 0x30(%r15), %rax
jg 0xf02f2
movq 0x18(%r15), %rax
movq (%rax), %rbp
andl $0x0, 0x8(%rsp)
callq 0xdfb90
movq %rbp, %rdi
movq %r13, %rsi
movq %rax, %rdx
leaq 0x534(%rip), %rcx       # 0xf0796
leaq 0x8(%rsp), %r8
callq 0xe4745
movl 0x8(%rsp), %ebp
movq %rbx, %rdi
leaq 0x4d682(%rip), %rsi     # 0x13d8fc
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
cmpl 0x84(%r15), %ebp
jne 0xf02e0
cmpb $0x0, (%r12)
leaq 0x58(%rsp), %rbp
je 0xf02a2
cmpl $0x4e4f4e45, 0x4c(%r15) # imm = 0x4E4F4E45
jne 0xf0313
movq %rbx, %rdi
leaq 0x4d679(%rip), %rsi     # 0x13d925
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
movq (%rax), %rdi
movq 0x98(%r15), %rsi
movq 0x88(%r15), %rdx
movq (%rcx), %rcx
callq 0xe45f4
movq 0x88(%r15), %rax
addq %rax, (%r15)
jmp 0xf02f7
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf0465
jmp 0xf02f7
cmpl $0xf, %eax
je 0xf02f7
movb $0x1, 0x38(%r15)
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
cmpb $0xf, (%r12)
jne 0xf001d
jmp 0xf03e7
cmpq $0x0, 0x78(%r15)
je 0xf02f2
movq 0x18(%r15), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x98(%r15), %rax
movq %rax, 0x38(%rsp)
movq 0x88(%r15), %rax
movq %rax, 0x40(%rsp)
movq 0x20(%r15), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x90(%r15), %rcx
movq %rcx, 0x20(%rsp)
movb $0x1, 0x18(%rsp)
movq 0x18(%rax), %r13
movq %rbx, %rdi
leaq 0x4d5d1(%rip), %rsi     # 0x13d93a
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbp, %rcx
xorl %r8d, %r8d
callq *0x78(%r15)
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x20(%r15), %rax
movq (%rax), %rdi
callq 0xe58e8
cmpl $0x0, 0x58(%rsp)
je 0xf03c5
movq 0x28(%r15), %rdx
movq 0x40(%r15), %rcx
movq %rbp, %rsi
callq 0xef547
jmp 0xf02f2
movq 0x20(%r15), %rax
movq (%rax), %rax
addq 0x90(%r15), %r13
cmpq %r13, 0x18(%rax)
je 0xf02d4
jmp 0xf02f2
movb $0x1, 0x38(%r15)
cmpb $0x0, 0x38(%r15)
je 0xf03fa
movq 0x40(%r15), %rdx
movq %r14, %rsi
callq 0xee91c
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf0465
jmp 0xf03e7
movq 0x40(%r15), %rdx
addq $0x50, %r12
leaq 0x4d337(%rip), %rcx     # 0x13d76a
movq %r14, %rsi
movq %r12, %r8
xorl %eax, %eax
callq 0xee823
jmp 0xf03e7