// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/lbr.c
// Function: crcr_read_filename_etc
static int crcr_read_filename_etc(deark *c, i64 pos1, struct crcr_filename_data *fnd)
{
	int retval = 0;
	i64 pos = pos1;
	enum crcrfnstate {
		CRCRFNST_NEUTRAL, CRCRFNST_FILENAME, CRCRFNST_COMMENT, CRCRFNST_DATE
	};
	enum crcrfnstate state = CRCRFNST_FILENAME;
	int found_dot = 0;
	int extension_char_count = 0;
	char attr_str[4] = "...";
	static const char attr_codes[3] = {'R', 'S', 'A'};
	int found_attr = 0;

	// Note: Only ASCII can really be supported, because the characters are 7-bit.
	// Normally, we'd use ucstring_append_bytes_ex() for something like this, but
	// it's pointless here.
	fnd->fn = ucstring_create(c);

	while(1) {
		u8 b1, b2;

		// Note: CFX limits this entire field to about 80 bytes.
		if(pos-pos1 > 300) goto done;
		if(pos >= c->infile->len) goto done;

		b1 = de_getbyte_p(&pos);
		if(b1==0) {
			break;
		}
		b2 = b1 & 0x7f;

		if(b2==0x01) {
			state = CRCRFNST_DATE; // TODO: Figure this field out
		}
		else if(state==CRCRFNST_FILENAME && b2=='[') {
			state = CRCRFNST_COMMENT;
		}
		else if(state==CRCRFNST_FILENAME && extension_char_count>=3) {
			state = CRCRFNST_NEUTRAL;
		}
		else if(state==CRCRFNST_FILENAME) {
			ucstring_append_char(fnd->fn, (de_rune)b2);
			if(found_dot) {
				if(extension_char_count<3 && (b1 & 0x80)) {
					// The CP/M low-level directory structure uses the high bit of
					// the file extension bytes to store attributes. Some Crunch/
					// CRLZH files do the same thing.
					// CP/M also uses the high bit of the *filename*, for less-common
					// attributes, but that doesn't seem possible here, because all 8
					// bytes are not always stored.
					found_attr = 1;
					attr_str[extension_char_count] = attr_codes[extension_char_count];
				}
				extension_char_count++;
			}
			else {
				if(b2=='.') found_dot = 1;
			}
		}
		else if(state==CRCRFNST_COMMENT && b2==']') {
			state = CRCRFNST_NEUTRAL;
		}
		else if(state==CRCRFNST_COMMENT) {
			if(!fnd->comment) {
				fnd->comment = ucstring_create(c);
			}
			ucstring_append_char(fnd->comment, (de_rune)b2);
		}
	}

	ucstring_strip_trailing_spaces(fnd->fn);
	fnd->size = pos - pos1;
	retval = 1;
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(fnd->fn));

	if(found_attr) {
		de_dbg(c, "attribs: %s", attr_str);
	}

	if(fnd->comment) {
		de_dbg(c, "comment: \"%s\"", ucstring_getpsz_d(fnd->comment));
	}

done:
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8(%rsp), %r13
movq %rsi, (%r13)
movl $0x2e2e2e, 0x24(%rsp)   # imm = 0x2E2E2E
callq 0xf6410
movq %r14, 0x18(%rsp)
movq %rax, (%r14)
movl $0x1, %r14d
movq %r15, %rax
movl $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0xa0a79
movq %r13, %rsi
callq 0xeeb4b
testb %al, %al
je 0xa0a7d
movl %eax, %ebp
andb $0x7f, %al
movl $0x3, %r12d
cmpb $0x1, %al
je 0xa0a5e
movl $0x2, %r12d
cmpl $0x1, %r14d
jne 0xa097d
cmpb $0x5b, %al
je 0xa0a5e
cmpl $0x1, %r14d
sete %cl
cmpl $0x3, 0x14(%rsp)
setge %dl
testb %dl, %cl
je 0xa0998
xorl %r12d, %r12d
jmp 0xa0a5e
movzbl %al, %r13d
cmpl $0x1, %r14d
jne 0xa0a00
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl %r13d, %esi
callq 0xf6542
cmpl $0x0, 0x10(%rsp)
je 0xa0a41
movl 0x14(%rsp), %edx
cmpl $0x3, %edx
setge %al
testb %bpl, %bpl
setns %cl
orb %al, %cl
jne 0xa09ea
movslq %edx, %rax
leaq 0x91af9(%rip), %rcx     # 0x1324d4
movb (%rax,%rcx), %cl
movb %cl, 0x24(%rsp,%rax)
movl $0x1, 0x20(%rsp)
incl %edx
movl %edx, 0x14(%rsp)
movl $0x1, %r12d
movl $0x1, 0x10(%rsp)
jmp 0xa0a59
cmpl $0x2, %r14d
jne 0xa0a0f
cmpb $0x5d, %al
jne 0xa0a0f
xorl %r12d, %r12d
jmp 0xa0a59
cmpl $0x2, %r14d
jne 0xa0a56
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
jne 0xa0a37
movq %rbx, %rdi
callq 0xf6410
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq %rdi, 0x8(%rax)
movl %r13d, %esi
callq 0xf6542
jmp 0xa0a59
xorl %eax, %eax
cmpl $0x2e, %r13d
sete %al
movl %eax, 0x10(%rsp)
movl $0x1, %r12d
jmp 0xa0a59
movl %r14d, %r12d
leaq 0x8(%rsp), %r13
movq 0x8(%rsp), %rax
movq %rax, %rcx
subq %r15, %rcx
movl %r12d, %r14d
cmpq $0x12c, %rcx            # imm = 0x12C
jle 0xa0939
xorl %ebp, %ebp
jmp 0xa0afb
movq 0x18(%rsp), %r14
movq (%r14), %rdi
callq 0xf64a0
movq 0x8(%rsp), %rax
subq %r15, %rax
movq %rax, 0x10(%r14)
movq (%r14), %rdi
callq 0xf70f6
leaq 0x9b2a9(%rip), %rsi     # 0x13bd4e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
cmpl $0x0, 0x20(%rsp)
je 0xa0acf
leaq 0x91801(%rip), %rsi     # 0x1322c1
leaq 0x24(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movl $0x1, %ebp
testq %rdi, %rdi
je 0xa0afb
callq 0xf70f6
leaq 0x73d92(%rip), %rsi     # 0x114880
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq