// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl $0xa0, %esi
callq 0x15b780
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
leaq 0x3639b(%rip), %rcx     # 0x1afe6c
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x10(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
cmpq $0xc, 0x10(%rax)
jge 0x179b29
movq -0x30(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x179e63
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1654b0
cmpl $0x58504b46, %eax       # imm = 0x58504B46
je 0x179b5c
movq -0x30(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x179e63
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x5cad3(%rip), %rsi     # 0x1d665d
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rdx
addq $0x48, %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0x169860
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
addq $0x48, %rdx
addq $0x10, %rdx
leaq 0x5caa4(%rip), %rsi     # 0x1d666f
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $0x4, %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x27389(%rip), %rsi     # 0x1a0f97
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
movq (%rax), %rcx
addq $0x10, %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x39(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movzbl 0x39(%rax), %edx
leaq 0x41eca(%rip), %rsi     # 0x1bbb19
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x3a(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movzbl 0x3a(%rax), %edx
leaq 0x5ca01(%rip), %rsi     # 0x1d6683
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x30(%rbp), %rax
movl $0x24, %edx
movzbl 0x3a(%rax), %ecx
callq 0x179ea0
movb %al, -0x31(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x31(%rbp), %edx
leaq 0x5c9d4(%rip), %rsi     # 0x1d6690
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x3b(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x3c(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movzbl 0x3c(%rax), %edx
movq -0x30(%rbp), %rax
movzbl 0x3b(%rax), %ecx
leaq 0x5c99a(%rip), %rsi     # 0x1d66aa
movb $0x0, %al
callq 0x15b0d0
movzbl -0x31(%rbp), %eax
movq -0x30(%rbp), %rcx
movzbl 0x3a(%rcx), %ecx
cmpl %ecx, %eax
je 0x179d39
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x179ef0
jmp 0x179e63
movq -0x30(%rbp), %rax
movzbl 0x39(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x179d6b
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165210
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movl 0x4c(%rax), %edi
callq 0x179a50
cmpl $0x0, %eax
jne 0x179dab
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rdx
movq -0x30(%rbp), %r8
addq $0x48, %r8
addq $0x8, %r8
leaq 0x5c917(%rip), %rcx     # 0x1d66b6
movb $0x0, %al
callq 0x177450
jmp 0x179e63
movq -0x30(%rbp), %rax
movzbl 0x39(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x179dc8
movq -0x30(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x179e63
movq -0x30(%rbp), %rax
movl 0x4c(%rax), %eax
subl $0x4d415348, %eax       # imm = 0x4D415348
jne 0x179de7
jmp 0x179dd8
movq -0x30(%rbp), %rax
leaq 0x6f1d(%rip), %rcx      # 0x180d00
movq %rcx, 0x78(%rax)
jmp 0x179de9
movq -0x30(%rbp), %rax
cmpb $0x0, 0x38(%rax)
je 0x179df5
jmp 0x179e63
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
subq 0x8(%rax), %rdi
callq 0x15ce30
movq -0x48(%rbp), %rcx
addq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
addq $0x8, %rax
movq -0x30(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jle 0x179e42
jmp 0x179e63
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x179f30
movq -0x30(%rbp), %rax
movzbl 0x80(%rax), %eax
cmpl $0xf, %eax
jne 0x179e61
jmp 0x179e63
jmp 0x179de9
cmpq $0x0, -0x30(%rbp)
je 0x179e8b
movq -0x30(%rbp), %rax
cmpb $0x0, 0x38(%rax)
je 0x179e89
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rdx
callq 0x177560
jmp 0x179e8b
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nop