// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0x38, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x50450
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x5023d
movq -0x18(%rbp), %rax
leaq 0x155031(%rip), %rcx    # 0x1a5258
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
leaq 0x155026(%rip), %rsi    # 0x1a525c
callq 0x15e380
jmp 0x502ac
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x50267
movq -0x18(%rbp), %rax
leaq 0x155014(%rip), %rcx    # 0x1a5265
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
leaq 0x155009(%rip), %rsi    # 0x1a5269
callq 0x15e380
jmp 0x502aa
movq -0x18(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x50291
movq -0x18(%rbp), %rax
leaq 0x155005(%rip), %rcx    # 0x1a5280
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
leaq 0x154ffa(%rip), %rsi    # 0x1a5284
callq 0x15e380
jmp 0x502a8
movq -0x8(%rbp), %rdi
leaq 0x154ffa(%rip), %rsi    # 0x1a5296
movb $0x0, %al
callq 0x15bfb0
jmp 0x503fc
jmp 0x502aa
jmp 0x502ac
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x17758b(%rip), %rsi    # 0x1c784a
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x502ed
movq -0x8(%rbp), %rdi
leaq 0x154fde(%rip), %rsi    # 0x1a52c4
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x9, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x5031e
movq -0x18(%rbp), %rax
movl $0x3, 0x4(%rax)
jmp 0x5032c
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x50341
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x154f8a(%rip), %rsi    # 0x1a52fc
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
imulq $0x34, 0x18(%rax), %rax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x503fa
movq -0x18(%rbp), %rax
cmpb $0x0, 0x30(%rax)
je 0x503b3
jmp 0x503fc
movq -0x20(%rbp), %rax
addq $0x34, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x503cb
jmp 0x503fc
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x504f0
movq -0x20(%rbp), %rax
addq $0x34, %rax
movq %rax, -0x20(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x50399
jmp 0x503fc
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x30, %rsp
popq %rbp
retq
nop