// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/cardfile.c
// Function: de_run_cardfile
static void de_run_cardfile(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 pos;
	i64 n;

	d = de_malloc(c, sizeof(lctx));

	pos = 0;
	d->fmt = detect_crd_fmt(c);
	if(d->fmt==DE_CRDFMT_MGC) {
		d->signature = "MGC";
		de_declare_fmt(c, "CardFile");
	}
	else if(d->fmt==DE_CRDFMT_RRG) {
		d->signature = "RRG";
		de_declare_fmt(c, "CardFile, with objects");
	}
	else if(d->fmt==DE_CRDFMT_DKO) {
		d->signature = "DKO";
		de_declare_fmt(c, "CardFile, Unicode");
	}
	else {
		de_err(c, "This is not a known/supported CardFile format");
		goto done;
	}
	de_dbg(c, "signature: %s", d->signature);
	pos+=3;

	if(d->fmt==DE_CRDFMT_DKO) {
		// TODO: Samples needed
		de_warn(c, "Unicode Cardfile files might not be supported correctly");
	}

	// Microsoft's (old) Cardfile format documentation says that text is in "low
	// ASCII format", but that seems doubtful on the face of it, and indeed I have
	// seen files where it is not.
	d->ole_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_WINDOWS1252);
	if(d->fmt==DE_CRDFMT_DKO) {
		d->crd_encoding = DE_ENCODING_UTF16LE;
	}
	else {
		d->crd_encoding = d->ole_encoding;
	}

	if(d->fmt==DE_CRDFMT_RRG) {
		pos += 4; // Last object's ID
	}

	d->numcards = de_getu16le_p(&pos);
	de_dbg(c, "number of cards: %d", (int)d->numcards);

	d->data_area_start = pos + CRD_INDEX_ITEM_LEN*d->numcards;

	for(n=0; n<d->numcards; n++) {
		if(d->fatalerrflag) goto done;
		if(pos+CRD_INDEX_ITEM_LEN > c->infile->len) goto done;
		do_card(c, d, n, pos);
		pos += CRD_INDEX_ITEM_LEN;
	}

done:
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
pushq $0x38
popq %rsi
callq 0xdecaf
movq %rax, %r14
andq $0x0, 0x18(%rsp)
movq %rbx, %rdi
callq 0x3a82c
movl %eax, (%r14)
movl %eax, %eax
leaq 0xd2456(%rip), %rcx     # 0x10c4ac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xd24bb(%rip), %rsi     # 0x10c521
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd2463(%rip), %rsi     # 0x10c4f4
leaq 0xd2458(%rip), %rax     # 0x10c4f0
jmp 0x3a0b8
leaq 0xd246e(%rip), %rsi     # 0x10c50f
leaq 0xd2463(%rip), %rax     # 0x10c50b
jmp 0x3a0b8
leaq 0xd2436(%rip), %rsi     # 0x10c4e7
leaq 0xd242b(%rip), %rax     # 0x10c4e3
movq %rax, 0x10(%r14)
movq %rbx, %rdi
callq 0xe07f7
movq 0x10(%r14), %rdx
leaq 0xf4c65(%rip), %rsi     # 0x12ed34
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq $0x3, 0x18(%rsp)
cmpl $0x3, (%r14)
jne 0x3a0f9
leaq 0xd2460(%rip), %rsi     # 0x10c54f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
pushq $0x9
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, 0x8(%r14)
movl (%r14), %ecx
cmpl $0x3, %ecx
jne 0x3a11c
movl $0x3, 0x4(%r14)
jmp 0x3a12e
movl %eax, 0x4(%r14)
cmpl $0x2, %ecx
jne 0x3a12e
movq $0x7, 0x18(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x18(%r14)
leaq 0xd243d(%rip), %rsi     # 0x10c587
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq (%r15), %r12
movq 0x18(%r14), %rax
imulq $0x34, %rax, %rcx
addq %r12, %rcx
movq %rcx, 0x20(%r14)
pushq $0x28
popq %rbp
xorl %r13d, %r13d
cmpq %rax, %r13
jge 0x3a070
cmpb $0x0, 0x30(%r14)
jne 0x3a070
leaq 0x34(%r12), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x3a070
movq %rbx, %rdi
leaq 0x4c(%rsp), %rsi
callq 0xdebae
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xdecaf
movq %rax, %r15
movq %r13, (%rax)
movq %rbx, %rdi
leaq 0xd23de(%rip), %rsi     # 0x10c59b
movq %r13, 0x50(%rsp)
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %r13
movl %r13d, %esi
callq 0xdeba7
movq %rbx, %rdi
leaq 0xf26f0(%rip), %rsi     # 0x12c8d5
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movl %r13d, %esi
callq 0xdeba7
movq 0x18(%rbx), %rdi
leaq 0x6(%r12), %rsi
callq 0xe409f
movq %rax, 0x8(%r15)
movq %rbx, %rdi
leaq 0xd238e(%rip), %rsi     # 0x10c5a4
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
callq 0xeac74
movq %rax, 0x20(%r15)
movl 0x4(%r14), %r9d
leaq 0xb(%r12), %rsi
movq %rbx, %rbp
movq 0x18(%rbx), %rdi
pushq $0x28
popq %rdx
movq %rax, %rcx
cmpl $0x3, %r9d
jne 0x3a25f
xorl %r8d, %r8d
pushq $0x3
popq %r9
callq 0xe4c9a
movq 0x20(%r15), %rdi
callq 0xeacc0
jmp 0x3a267
movl %r13d, %r8d
callq 0xe4c9a
movq 0x20(%r15), %rdi
callq 0xeb877
movq %rbp, %rbx
movq %rbp, %rdi
leaq 0xd2334(%rip), %rsi     # 0x10c5b1
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x8(%r15), %rax
cmpq 0x20(%r14), %rax
movq 0x50(%rsp), %r13
jl 0x3a2a9
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rbp
subq %rax, %rbp
jl 0x3a2a9
cmpq 0x28(%r14), %rax
jge 0x3a2ff
movb $0x1, 0x30(%r14)
movl (%r15), %edx
movq %rbx, %rdi
leaq 0xd2317(%rip), %rsi     # 0x10c5d2
xorl %eax, %eax
callq 0xdf2a5
pushq $0x28
popq %rbp
movq 0x20(%r15), %rdi
callq 0xeaf3c
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
movl 0x4c(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq 0x18(%rsp), %r12
addq $0x34, %r12
movq %r12, 0x18(%rsp)
incq %r13
movq 0x18(%r14), %rax
jmp 0x3a16e
movq %rax, 0x28(%r14)
movq 0x18(%r14), %rax
decq %rax
cmpq %rax, (%r15)
jge 0x3a330
addq $0x3a, %r12
movq %r12, %rsi
callq 0xe409f
movq %rax, %rcx
subq 0x8(%r15), %rcx
jle 0x3a330
movq 0x18(%rbx), %rdx
cmpq 0x18(%rdx), %rax
cmovlq %rcx, %rbp
movq %rbx, %rdi
leaq 0xd2288(%rip), %rsi     # 0x10c5c2
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde9f1
cmpq $0x2, %rbp
jl 0x3a2ae
addq 0x8(%r15), %rbp
movq %rbp, 0x10(%r15)
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x8(%r15), %rdx
movq %rbx, %rdi
leaq 0xd2317(%rip), %rsi     # 0x10c686
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rbp
movl %ebp, %esi
callq 0xdeba7
cmpl $0x2, (%r14)
jne 0x3a47f
movq 0x8(%r15), %rax
movq %rax, 0x20(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0xe3fc4
movq %rax, %r12
movq %rbx, %rdi
leaq 0xfdd93(%rip), %rsi     # 0x138144
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
testl %r12d, %r12d
je 0x3a735
movq 0x20(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe40bf
movq %rbx, %rdi
leaq 0xd2284(%rip), %rsi     # 0x10c66f
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x70
popq %rsi
callq 0xdecaf
movq %rax, %r12
leaq 0xd5df6(%rip), %rax     # 0x1101ff
movq %rax, (%r12)
movl 0x8(%r14), %eax
movl %eax, 0xc(%r12)
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
leaq 0xd225c(%rip), %rsi     # 0x10c681
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq 0x18(%rbx), %rcx
movq 0x8(%rsp), %r8
movq 0x10(%r15), %r9
subq %r8, %r9
addq $-0x10, %r9
movq %rbx, %rdi
leaq 0xd223e(%rip), %rsi     # 0x10c692
movq %r12, %rdx
callq 0xdf9b8
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq %r12, %rax
movl 0x40(%r12), %ecx
testb $0x1, %cl
jne 0x3a60d
xorl %ebp, %ebp
jmp 0x3a6fa
movq 0x18(%rbx), %rdi
movq 0x8(%r15), %rsi
callq 0xe3fa3
movq %rax, %r12
movq %rbx, %rdi
leaq 0xd2252(%rip), %rsi     # 0x10c6eb
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
leaq 0xa(%r12), %rax
testq %r12, %r12
pushq $0x2
popq %rcx
cmoveq %rcx, %rax
movq 0x8(%r15), %rcx
movq 0x10(%r15), %rdx
leaq (%rcx,%rax), %rsi
movq %rsi, 0x18(%r15)
addq $-0x2, %rdx
cmpq %rdx, %rsi
jg 0x3a5fb
addq %rcx, %rax
addq $0x2, %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rbx), %rdi
callq 0xe3fa3
movq %r12, 0x10(%rsp)
movq %rax, %r12
movl %r12d, %ebp
notl %ebp
movq %rbx, %rdi
leaq 0xd2143(%rip), %rsi     # 0x10c63e
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x28(%rsp), %rsi
leaq (%r12,%rsi), %rax
movq 0x10(%r15), %rcx
andl $0xff00, %ebp           # imm = 0xFF00
movzbl %r12b, %r8d
leaq (%r8,%rsi), %rdx
xorq %rcx, %rdx
cmpq %rcx, %rax
cmovleq %r12, %r8
orq %rdx, %rbp
cmovneq %r12, %r8
movq 0x10(%rsp), %rdx
leaq (%r8,%rsi), %rax
cmpq %rcx, %rax
jg 0x3a5fb
testq %rdx, %rdx
je 0x3a7f8
movq %r8, 0x10(%rsp)
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r12
cmpb $0x0, 0xd0(%rbx)
je 0x3a576
movq 0x20(%r15), %rdx
movq %rbx, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0xdfd55
movq %r12, 0x40(%rsp)
movq 0x18(%rbx), %rdi
movq 0x8(%r15), %rsi
addq $0x2, %rsi
callq 0xe3fa3
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq 0x8(%r15), %rsi
addq $0x4, %rsi
callq 0xe3fa3
movq %rax, %rbp
movq %rbx, %rdi
leaq 0xf248d(%rip), %rsi     # 0x12ca3a
movl %r12d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xde896
movq %r12, %rsi
leaq 0xf(%r12), %rax
cqto
pushq $0x10
popq %rcx
idivq %rcx
movq %rax, %r12
addq %rax, %r12
movq 0x8(%r15), %rax
movq %r12, %rcx
imulq %rbp, %rcx
addq %rcx, %rax
addq $0xa, %rax
cmpq 0x18(%r15), %rax
jg 0x3a5fb
testq %rsi, %rsi
sete %al
testq %rbp, %rbp
sete %cl
orb %al, %cl
je 0x3a79c
movl (%r15), %edx
movq %rbx, %rdi
leaq 0xd2046(%rip), %rsi     # 0x10c64e
jmp 0x3a2bb
movq 0x8(%rsp), %rdx
addq 0x58(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq %rbx, %rdi
leaq 0xd2072(%rip), %rsi     # 0x10c697
movq %rax, 0x10(%rsp)
xorl %eax, %eax
movl %ecx, 0x40(%rsp)
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0xe3fc4
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rbx, %rdi
leaq 0xd2051(%rip), %rsi     # 0x10c6b0
movl %ebp, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe4038
movq %rax, 0x38(%rsp)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe4038
movq %rax, 0x30(%rsp)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rbx, %rdi
leaq 0xd2018(%rip), %rsi     # 0x10c6c9
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movl %ebp, %r8d
movl %eax, %r9d
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rbx, %rdi
leaq 0xd1ffd(%rip), %rsi     # 0x10c6db
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movl 0x40(%rsp), %ecx
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rbp
subq 0x28(%rsp), %rbp
testb $0x1, %cl
sete %r12b
movq %rbx, %rdi
movq %rax, %rsi
callq 0xded32
testq %rbp, %rbp
setle %al
orb %r12b, %al
je 0x3a730
movl (%r15), %edx
movq %rbx, %rdi
leaq 0xd1ec9(%rip), %rsi     # 0x10c5ed
xorl %eax, %eax
callq 0xdf05a
jmp 0x3a2c2
addq %rbp, 0x20(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0xe3fc4
movq %rax, %r12
movq %rbx, %rdi
leaq 0xd1eee(%rip), %rsi     # 0x10c63e
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x20(%rsp), %rcx
leaq (%rcx,%r12), %rax
cmpq 0x10(%r15), %rax
pushq $0x28
popq %rbp
jle 0x3a785
movl (%r15), %edx
movq %rbx, %rdi
leaq 0xd1ed5(%rip), %rsi     # 0x10c64e
xorl %eax, %eax
callq 0xdf2a5
jmp 0x3a2c5
testq %r12, %r12
je 0x3a2c5
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %r8
jmp 0x3a812
movq %rbx, %rdi
movq %rsi, 0x30(%rsp)
movq %rbp, %rdx
pushq $0x1
popq %rcx
callq 0xe7437
movq %rax, 0x38(%rsp)
movq 0x18(%rbx), %rdi
movq 0x8(%r15), %rsi
addq $0xa, %rsi
movq 0x30(%rsp), %rdx
movq %rbp, %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq $0x0
movq 0x48(%rsp), %r12
pushq %r12
callq 0xe78af
popq %rax
popq %rcx
movq 0x38(%rsp), %rdi
callq 0xe7084
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfd24
movq 0x10(%rsp), %r8
testq %r8, %r8
pushq $0x28
popq %rbp
je 0x3a2c5
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x28(%rsp), %rcx
callq 0x3a8a2
jmp 0x3a2c5