// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r12
movq %rax, %r15
leaq 0x28ab8(%rip), %rax     # 0x124598
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 0xfc0e2
movq %rax, (%r15)
movq (%r13), %rdi
movq %r15, %rsi
callq 0xeed31
cmpl $0x58504b46, %eax       # imm = 0x58504B46
jne 0xfc0e2
movq (%r13), %rdi
movq %r15, %rsi
callq 0xeed31
movq %rax, 0x8(%r15)
leaq 0x4f0a1(%rip), %rsi     # 0x14abe1
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq (%r13), %rdi
movq (%r15), %rsi
leaq 0x48(%r12), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1762
leaq 0x58(%r12), %rdx
leaq 0x4f081(%rip), %rsi     # 0x14abf3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
addq $0x4, (%r15)
movq (%r13), %rdi
movq %r15, %rsi
callq 0xeed31
movq %rax, 0x10(%r15)
leaq 0x1965c(%rip), %rsi     # 0x1151f3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq $0x10, (%r15)
movq (%r13), %rdi
movq %r15, %rsi
callq 0xeeb4b
movb %al, 0x39(%r15)
movzbl %al, %edx
leaq 0x344b4(%rip), %rsi     # 0x130076
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq (%r13), %rdi
movq %r15, %rsi
callq 0xeeb4b
movb %al, 0x3a(%r15)
movzbl %al, %edx
leaq 0x4f021(%rip), %rsi     # 0x14ac07
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq (%r13), %rdi
movq 0x8(%r13), %rsi
movb 0x3a(%r15), %al
leaq 0x58(%rsp), %rbp
movb %al, (%rbp)
leaq 0x832(%rip), %rcx       # 0xfc43d
movl $0x24, %edx
movq %rbp, %r8
callq 0xef5e4
movzbl (%rbp), %ebp
leaq 0x4eff1(%rip), %rsi     # 0x14ac14
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
movq (%r13), %rdi
movq %r15, %rsi
callq 0xeeb4b
movb %al, 0x3b(%r15)
movq (%r13), %rdi
movq %r15, %rsi
callq 0xeeb4b
movb %al, 0x3c(%r15)
movzbl %al, %edx
movzbl 0x3b(%r15), %ecx
leaq 0x4efd0(%rip), %rsi     # 0x14ac2e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpb 0x3a(%r15), %bpl
jne 0xfc0cc
testb $0x4, 0x39(%r15)
je 0xfbc88
movq (%r13), %rdi
movq %r15, %rsi
callq 0xeeb91
addq %rax, (%r15)
movl 0x4c(%r15), %eax
cmpl $0x4d415348, %eax       # imm = 0x4D415348
je 0xfbc9e
cmpl $0x4e4f4e45, %eax       # imm = 0x4E4F4E45
jne 0xfc126
testb $0x2, 0x39(%r15)
jne 0xfc0e2
cmpl $0x4d415348, %eax       # imm = 0x4D415348
jne 0xfbcbb
leaq 0x5973(%rip), %rax      # 0x10162a
movq %rax, 0x78(%r15)
subq $-0x80, %r12
cmpb $0x0, 0x38(%r15)
jne 0xfc0e7
movq (%r15), %rdi
movq 0x18(%r15), %rax
movq 0x8(%rax), %r13
subq %r13, %rdi
callq 0xe9c87
leaq (%rax,%r13), %rcx
movq %rcx, (%r15)
addq %r13, %rax
addq $0x8, %rax
cmpq 0x30(%r15), %rax
jg 0xfc0e7
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movl $0x0, 0x58(%rsp)
movb %al, 0x5c(%rsp)
movq %rax, 0x60(%rsp)
movb %al, 0x68(%rsp)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
callq 0xe8a1c
movq (%r15), %rdx
movq %rbx, %rdi
leaq 0x4efe5(%rip), %rsi     # 0x14ad22
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r12)
movups %xmm0, (%r12)
movq (%r15), %r13
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xeeb4b
movb %al, 0x80(%r15)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x2a969(%rip), %rsi     # 0x1266ee
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xeeb4b
movb %al, 0x81(%r15)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x4ef84(%rip), %rsi     # 0x14ad33
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xeeb91
movl %eax, 0x84(%r15)
movq %rbx, %rdi
leaq 0x4ef83(%rip), %rsi     # 0x14ad59
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
testb $0x1, 0x39(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
jne 0xfbe12
callq 0xeeb91
movq %rax, 0x88(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xeeb91
movl $0x8, %ebp
jmp 0xfbe32
callq 0xeed31
movq %rax, 0x88(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xeed31
movl $0xc, %ebp
movq %rax, 0x90(%r15)
movq 0x88(%r15), %rdx
movq %rbx, %rdi
leaq 0x4ef36(%rip), %rsi     # 0x14ad80
xorl %eax, %eax
callq 0xe8617
movq 0x90(%r15), %rdx
movq %rbx, %rdi
leaq 0x4ef28(%rip), %rsi     # 0x14ad8a
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rax
movq (%rax), %rdi
movb 0x81(%r15), %al
movb %al, 0x8(%rsp)
movq %r13, %rsi
movq %rbp, %rdx
leaq 0x5b5(%rip), %rcx       # 0xfc43d
leaq 0x8(%rsp), %r8
callq 0xef5e4
movzbl 0x8(%rsp), %ebp
movq %rbx, %rdi
leaq 0x4eef3(%rip), %rsi     # 0x14ad94
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
cmpb 0x81(%r15), %bpl
jne 0xfbf99
movzbl (%r12), %eax
cmpl $0x2, %eax
jae 0xfbfd0
movq (%r15), %rdx
movq %rdx, 0x98(%r15)
movq %rbx, %rdi
leaq 0x4eee3(%rip), %rsi     # 0x14adbc
xorl %eax, %eax
callq 0xe8617
movq 0x88(%r15), %rdi
movq 0x98(%r15), %r13
leaq (%rdi,%r13), %rax
cmpq 0x30(%r15), %rax
jg 0xfbfaf
movq 0x18(%r15), %rax
movq (%rax), %rbp
movl $0x0, 0x8(%rsp)
callq 0xe9c87
movq %rbp, %rdi
movq %r13, %rsi
movq %rax, %rdx
leaq 0x53c(%rip), %rcx       # 0xfc45c
leaq 0x8(%rsp), %r8
callq 0xef5e4
movl 0x8(%rsp), %ebp
movq %rbx, %rdi
leaq 0x4ee94(%rip), %rsi     # 0x14adcc
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
cmpl 0x84(%r15), %ebp
jne 0xfbf99
cmpb $0x0, (%r12)
je 0xfbf5b
cmpl $0x4e4f4e45, 0x4c(%r15) # imm = 0x4E4F4E45
jne 0xfbfd7
movq %rbx, %rdi
leaq 0x4ee90(%rip), %rsi     # 0x14adf5
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
movq (%rax), %rdi
movq 0x98(%r15), %rsi
movq 0x88(%r15), %rdx
movq (%rcx), %rcx
callq 0xef481
movq 0x88(%r15), %rax
addq %rax, (%r15)
jmp 0xfbfb4
movq 0x28(%r15), %rsi
movq 0x40(%r15), %rdx
leaq 0x4ed6b(%rip), %rcx     # 0x14ad13
xorl %eax, %eax
callq 0xfa21a
movb $0x1, 0x38(%r15)
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
cmpb $0xf, (%r12)
jne 0xfbcbf
jmp 0xfc0e7
cmpl $0xf, %eax
jne 0xfbfaf
jmp 0xfbfb4
cmpq $0x0, 0x78(%r15)
je 0xfbfaf
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 0x4eddd(%rip), %rsi     # 0x14ae0a
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x58(%rsp), %rcx
xorl %r8d, %r8d
callq *0x78(%r15)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x20(%r15), %rax
movq (%rax), %rdi
callq 0xf08ba
movl 0x58(%rsp), %eax
testl %eax, %eax
je 0xfc0af
movq 0x28(%r15), %rdi
cmpl $0x0, (%rdi)
jne 0xfbfaf
movq 0x40(%r15), %rcx
movl %eax, (%rdi)
addq $0x10, %rdi
movl $0x50, %esi
leaq 0x40a74(%rip), %rdx     # 0x13cb12
leaq 0x68(%rsp), %r8
xorl %eax, %eax
callq 0xe8193
jmp 0xfbfaf
movq 0x20(%r15), %rax
movq (%rax), %rax
addq 0x90(%r15), %r13
cmpq %r13, 0x18(%rax)
je 0xfbf8d
jmp 0xfbfaf
movq 0x28(%r15), %rsi
movq 0x40(%r15), %rdx
leaq 0x4ec38(%rip), %rcx     # 0x14ad13
xorl %eax, %eax
callq 0xfa21a
movb $0x1, 0x38(%r15)
cmpb $0x0, 0x38(%r15)
je 0xfc109
cmpl $0x0, (%r14)
jne 0xfc109
movq 0x40(%r15), %rdx
leaq 0x4e9da(%rip), %rcx     # 0x14aad9
movq %r14, %rsi
xorl %eax, %eax
callq 0xfa21a
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%r15), %rdx
addq $0x50, %r12
leaq 0x4eb05(%rip), %rcx     # 0x14ac3a
movq %r14, %rsi
movq %r12, %r8
xorl %eax, %eax
callq 0xfa21a
jmp 0xfc0e7