// File: /workspace/github_c_makefile_star2_O2/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 0x20(%rsp), %r12
movq %rsi, (%r12)
movl $0x2e2e2e, 0x1c(%rsp)   # imm = 0x2E2E2E
callq 0xeac74
movq %r14, 0x10(%rsp)
movq %rax, (%r14)
pushq $0x1
popq %r14
pushq $0x3
popq %r13
movq %r15, %rcx
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x18(%rsp)
movq %rcx, %rax
subq %r15, %rax
cmpq $0x12c, %rax            # imm = 0x12C
jg 0x9ad2f
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rcx
jge 0x9ad2f
movq %r12, %rsi
callq 0xe3f2a
testb %al, %al
je 0x9ad33
movl %eax, %ebp
movl %eax, %ecx
andb $0x7f, %cl
cmpb $0x1, %cl
jne 0x9ac3e
movl %r13d, %eax
jmp 0x9ad22
cmpl $0x1, %r14d
jne 0x9ac51
cmpb $0x5b, %cl
jne 0x9ac51
pushq $0x2
popq %rax
jmp 0x9ad22
cmpl $0x1, %r14d
sete %dl
cmpl $0x3, 0xc(%rsp)
setge %sil
xorl %eax, %eax
testb %sil, %dl
jne 0x9ad22
movzbl %cl, %r13d
cmpl $0x1, %r14d
jne 0x9acca
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl %r13d, %esi
callq 0xeada5
cmpl $0x0, 0x8(%rsp)
je 0x9ad09
movl 0xc(%rsp), %edx
cmpl $0x3, %edx
setge %al
testb %bpl, %bpl
setns %cl
orb %al, %cl
jne 0x9acb9
movslq %edx, %rax
leaq 0x8a53d(%rip), %rcx     # 0x1251e8
movb (%rax,%rcx), %cl
movb %cl, 0x1c(%rsp,%rax)
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
incl %edx
movl %edx, 0xc(%rsp)
pushq $0x1
popq %rcx
movl %ecx, %eax
movl %ecx, 0x8(%rsp)
jmp 0x9ad1e
cmpl $0x2, %r14d
jne 0x9acd5
cmpb $0x5d, %cl
je 0x9ad1e
cmpl $0x2, %r14d
jne 0x9ad1b
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
jne 0x9acfd
movq %rbx, %rdi
callq 0xeac74
movq %rax, %rdi
movq 0x10(%rsp), %rax
movq %rdi, 0x8(%rax)
movl %r13d, %esi
callq 0xeada5
pushq $0x2
jmp 0x9ad18
xorl %eax, %eax
cmpl $0x2e, %r13d
sete %al
movl %eax, 0x8(%rsp)
pushq $0x1
popq %rax
jmp 0x9ad1e
movl %r14d, %eax
pushq $0x3
popq %r13
movq 0x20(%rsp), %rcx
movl %eax, %r14d
jmp 0x9abfa
xorl %ebp, %ebp
jmp 0x9adaf
movq 0x10(%rsp), %r14
movq (%r14), %rdi
callq 0xead03
movq 0x20(%rsp), %rax
subq %r15, %rax
movq %rax, 0x10(%r14)
movq (%r14), %rdi
callq 0xeb877
leaq 0x93d03(%rip), %rsi     # 0x12ea5e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
cmpl $0x0, 0x18(%rsp)
je 0x9ad85
leaq 0x8a25f(%rip), %rsi     # 0x124fd5
leaq 0x1c(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
pushq $0x1
popq %rbp
testq %rdi, %rdi
je 0x9adaf
callq 0xeb877
leaq 0x6ca53(%rip), %rsi     # 0x1077f5
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq