// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/cardfile.c
// Function: do_carddata_mgc
static void do_carddata_mgc(deark *c, lctx *d, struct page_ctx *pg)
{
	i64 bitmap_len;
	i64 text_len;
	i64 text_pos;
	u8 bad_data_flag = 0;

	bitmap_len = de_getu16le(pg->datapos);
	de_dbg(c, "bitmap length: %d", (int)bitmap_len);
	if(bitmap_len==0) {
		pg->text_item_pos = pg->datapos+2;
	}
	else {
		pg->text_item_pos = pg->datapos + 10 + bitmap_len;
	}
	text_pos = pg->text_item_pos+2;

	if(pg->text_item_pos > pg->data_endpos-2) {
		bad_data_flag = 1;
		goto done;
	}

	// Look ahead at the text length field, to help detect errors.
	text_len = de_getu16le(pg->text_item_pos);
	de_dbg(c, "text length: %d", (int)text_len);

	// I don't understand this, but I have a file with some cards in which the
	// high byte of the text length seems to be unused and set to 0xff.
	if((text_pos+text_len > pg->data_endpos) && ((text_len&0xff00)==0xff00) &&
		(text_pos+(text_len & 0x00ff)==pg->data_endpos))
	{
		text_len = text_len & 0x00ff;
	}

	if(text_pos+text_len > pg->data_endpos) {
		bad_data_flag = 1;
		goto done;
	}

	if(bitmap_len!=0) {
		if(!do_bitmap_mgc(c, d, pg)) {
			bad_data_flag = 1;
			goto done;
		}
	}

	if(text_len!=0) {
		do_text(c, d, pg, text_pos, text_len);
	}

done:
	if(bad_data_flag) {
		de_err(c, "card #%d: Malformed data segment", (int)pg->cardnum);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x31(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x165280
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x154a9e(%rip), %rsi    # 0x1a5460
movb $0x0, %al
callq 0x15b0d0
cmpq $0x0, -0x20(%rbp)
jne 0x509e6
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x2, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x509fe
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0xa, %rcx
addq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
jle 0x50a30
movb $0x1, -0x31(%rbp)
jmp 0x50b0e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x154959(%rip), %rsi    # 0x1a53b3
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jle 0x50aaf
movq -0x28(%rbp), %rax
andq $0xff00, %rax           # imm = 0xFF00
cmpq $0xff00, %rax           # imm = 0xFF00
jne 0x50aaf
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
andq $0xff, %rcx
addq %rcx, %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x50aaf
movq -0x28(%rbp), %rax
andq $0xff, %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jle 0x50ac7
movb $0x1, -0x31(%rbp)
jmp 0x50b0e
cmpq $0x0, -0x20(%rbp)
je 0x50aec
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x50f80
cmpl $0x0, %eax
jne 0x50aea
movb $0x1, -0x31(%rbp)
jmp 0x50b0e
jmp 0x50aec
cmpq $0x0, -0x28(%rbp)
je 0x50b0c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %r8
callq 0x50da0
jmp 0x50b0e
cmpb $0x0, -0x31(%rbp)
je 0x50b2f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl %eax, %edx
leaq 0x15489b(%rip), %rsi    # 0x1a53c3
movb $0x0, %al
callq 0x15bfb0
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)