// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq %rsi, %r12
movq %rdi, %rbx
movl $0xa0, %esi
callq 0xe969b
movq %rax, %r15
leaq 0x282e8(%rip), %rax     # 0x124378
movq %rax, 0x40(%r15)
movq %r12, 0x18(%r15)
movq %r13, 0x20(%r15)
movq %r14, 0x28(%r15)
movq 0x8(%r12), %rax
movq 0x10(%r12), %rcx
leaq (%rcx,%rax), %rdx
movq %rdx, 0x30(%r15)
cmpq $0xb, %rcx
jle 0xfc69f
movq %rax, (%r15)
movq (%r12), %rdi
movq %r15, %rsi
callq 0xef34e
cmpl $0x58504b46, %eax       # imm = 0x58504B46
jne 0xfc69f
movq (%r12), %rdi
movq %r15, %rsi
callq 0xef34e
movq %rax, 0x8(%r15)
leaq 0x4e544(%rip), %rsi     # 0x14a631
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq (%r12), %rdi
movq (%r15), %rsi
leaq 0x48(%r15), %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1d74
leaq 0x58(%r15), %rdx
leaq 0x4e526(%rip), %rsi     # 0x14a643
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
addq $0x4, (%r15)
movq (%r12), %rdi
movq %r15, %rsi
callq 0xef34e
movq %rax, 0x10(%r15)
leaq 0x19001(%rip), %rsi     # 0x115143
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq $0x10, (%r15)
movq (%r12), %rdi
movq %r15, %rsi
callq 0xef14d
movb %al, 0x39(%r15)
movzbl %al, %edx
leaq 0x33bfd(%rip), %rsi     # 0x12fd6a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq (%r12), %rdi
movq %r15, %rsi
callq 0xef14d
movb %al, 0x3a(%r15)
movzbl %al, %edx
leaq 0x4e4c6(%rip), %rsi     # 0x14a657
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq (%r12), %rdi
movq 0x8(%r12), %rsi
movb 0x3a(%r15), %al
leaq 0x58(%rsp), %r13
movb %al, (%r13)
leaq 0x841(%rip), %rcx       # 0xfc9f9
movl $0x24, %edx
movq %r13, %r8
callq 0xefc28
movzbl (%r13), %ebp
leaq 0x4e493(%rip), %rsi     # 0x14a664
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movq (%r12), %rdi
movq %r15, %rsi
callq 0xef14d
movb %al, 0x3b(%r15)
movq (%r12), %rdi
movq %r15, %rsi
callq 0xef14d
movb %al, 0x3c(%r15)
movzbl %al, %edx
movzbl 0x3b(%r15), %ecx
leaq 0x4e472(%rip), %rsi     # 0x14a67e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpb 0x3a(%r15), %bpl
jne 0xfc689
testb $0x4, 0x39(%r15)
je 0xfc236
movq (%r12), %rdi
movq %r15, %rsi
callq 0xef193
addq %rax, (%r15)
movl 0x4c(%r15), %eax
cmpl $0x4d415348, %eax       # imm = 0x4D415348
je 0xfc24c
cmpl $0x4e4f4e45, %eax       # imm = 0x4E4F4E45
jne 0xfc6e8
testb $0x2, 0x39(%r15)
jne 0xfc69f
cmpl $0x4d415348, %eax       # imm = 0x4D415348
jne 0xfc269
leaq 0x5b8c(%rip), %rax      # 0x101df1
movq %rax, 0x78(%r15)
leaq 0x80(%r15), %r12
cmpb $0x0, 0x38(%r15)
jne 0xfc6a4
movq (%r15), %rdi
movq 0x18(%r15), %rax
movq 0x8(%rax), %r13
subq %r13, %rdi
callq 0xea7de
leaq (%rax,%r13), %rcx
movq %rcx, (%r15)
addq %r13, %rax
addq $0x8, %rax
cmpq 0x30(%r15), %rax
jg 0xfc6a4
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 0xe958c
movq (%r15), %rdx
movq %rbx, %rdi
leaq 0x4e484(%rip), %rsi     # 0x14a772
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r12)
movups %xmm0, (%r12)
movq (%r15), %rbp
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xef14d
movb %al, 0x80(%r15)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x2a188(%rip), %rsi     # 0x1264be
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xef14d
movb %al, 0x81(%r15)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x4e423(%rip), %rsi     # 0x14a783
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xef193
movl %eax, 0x84(%r15)
movq %rbx, %rdi
leaq 0x4e422(%rip), %rsi     # 0x14a7a9
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
testb $0x1, 0x39(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
jne 0xfc3cb
callq 0xef193
movq %rax, 0x88(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xef193
movq %rax, 0x90(%r15)
movl $0x8, %r13d
jmp 0xfc3f3
callq 0xef34e
movq %rax, 0x88(%r15)
movq 0x18(%r15), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0xef34e
movq %rax, 0x90(%r15)
movl $0xc, %r13d
movq 0x88(%r15), %rdx
movq %rbx, %rdi
leaq 0x4e3cc(%rip), %rsi     # 0x14a7d0
xorl %eax, %eax
callq 0xe9226
movq 0x90(%r15), %rdx
movq %rbx, %rdi
leaq 0x4e3be(%rip), %rsi     # 0x14a7da
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rax
movq (%rax), %rdi
movb 0x81(%r15), %al
movb %al, 0x8(%rsp)
movq %rbp, %rsi
movq %r13, %rdx
leaq 0x5b7(%rip), %rcx       # 0xfc9f9
leaq 0x8(%rsp), %r8
callq 0xefc28
movzbl 0x8(%rsp), %ebp
movq %rbx, %rdi
leaq 0x4e389(%rip), %rsi     # 0x14a7e4
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
cmpb 0x81(%r15), %bpl
jne 0xfc553
movzbl (%r12), %eax
cmpl $0x2, %eax
jae 0xfc58a
movq (%r15), %rdx
movq %rdx, 0x98(%r15)
movq %rbx, %rdi
leaq 0x4e379(%rip), %rsi     # 0x14a80c
xorl %eax, %eax
callq 0xe9226
movq 0x88(%r15), %rdi
movq 0x98(%r15), %r13
leaq (%rdi,%r13), %rax
cmpq 0x30(%r15), %rax
jg 0xfc569
movq 0x18(%r15), %rax
movq (%rax), %rbp
movl $0x0, 0x8(%rsp)
callq 0xea7de
movq %rbp, %rdi
movq %r13, %rsi
movq %rax, %rdx
leaq 0x53e(%rip), %rcx       # 0xfca18
leaq 0x8(%rsp), %r8
callq 0xefc28
movl 0x8(%rsp), %ebp
movq %rbx, %rdi
leaq 0x4e32a(%rip), %rsi     # 0x14a81c
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
cmpl 0x84(%r15), %ebp
jne 0xfc553
cmpb $0x0, (%r12)
je 0xfc515
cmpl $0x4e4f4e45, 0x4c(%r15) # imm = 0x4E4F4E45
jne 0xfc591
movq %rbx, %rdi
leaq 0x4e326(%rip), %rsi     # 0x14a845
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
movq (%rax), %rdi
movq 0x98(%r15), %rsi
movq 0x88(%r15), %rdx
movq (%rcx), %rcx
callq 0xefabc
movq 0x88(%r15), %rax
addq %rax, (%r15)
jmp 0xfc56e
movq 0x28(%r15), %rsi
movq 0x40(%r15), %rdx
leaq 0x4e201(%rip), %rcx     # 0x14a763
xorl %eax, %eax
callq 0xfa71e
movb $0x1, 0x38(%r15)
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
cmpb $0xf, (%r12)
jne 0xfc270
jmp 0xfc6a4
cmpl $0xf, %eax
jne 0xfc569
jmp 0xfc56e
cmpq $0x0, 0x78(%r15)
je 0xfc569
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), %rcx
movq %rcx, 0x10(%rsp)
movq 0x90(%r15), %rcx
movq %rcx, 0x20(%rsp)
movb $0x1, 0x18(%rsp)
movq (%rax), %rax
movq 0x18(%rax), %r13
movq %rbx, %rdi
leaq 0x4e270(%rip), %rsi     # 0x14a85a
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
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 0xe9585
movq 0x20(%r15), %rax
movq (%rax), %rdi
callq 0xf0ea4
movl 0x58(%rsp), %eax
testl %eax, %eax
je 0xfc66c
movq 0x28(%r15), %rdi
cmpl $0x0, (%rdi)
jne 0xfc569
movq 0x40(%r15), %rcx
movl %eax, (%rdi)
addq $0x10, %rdi
movl $0x50, %esi
leaq 0x400b7(%rip), %rdx     # 0x13c712
leaq 0x68(%rsp), %r8
xorl %eax, %eax
callq 0xe8d67
jmp 0xfc569
movq 0x20(%r15), %rax
movq (%rax), %rax
addq 0x90(%r15), %r13
cmpq %r13, 0x18(%rax)
je 0xfc547
jmp 0xfc569
movq 0x28(%r15), %rsi
movq 0x40(%r15), %rdx
leaq 0x4e0cb(%rip), %rcx     # 0x14a763
xorl %eax, %eax
callq 0xfa71e
movb $0x1, 0x38(%r15)
testq %r15, %r15
je 0xfc6cb
cmpb $0x0, 0x38(%r15)
je 0xfc6cb
cmpl $0x0, (%r14)
jne 0xfc6cb
movq 0x40(%r15), %rdx
leaq 0x4de68(%rip), %rcx     # 0x14a529
movq %r14, %rsi
xorl %eax, %eax
callq 0xfa71e
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%r15), %rdx
leaq 0x50(%r15), %r8
leaq 0x4df93(%rip), %rcx     # 0x14a68a
movq %r14, %rsi
xorl %eax, %eax
callq 0xfa71e
jmp 0xfc6a4