// File: /workspace/github_c_makefile_star2_O1/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), %rax
movq %rsi, (%rax)
movl $0x2e2e2e, 0x14(%rsp)   # imm = 0x2E2E2E
callq 0xf6870
movq %r14, 0x18(%rsp)
movq %rax, (%r14)
movl $0x0, 0x8(%rsp)
movl $0x1, %r12d
movl $0x0, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq %rax, %rcx
subq %r15, %rcx
movl $0x4, %r13d
cmpq $0x12c, %rcx            # imm = 0x12C
jg 0xa1258
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0xa1258
leaq 0x20(%rsp), %rsi
callq 0xef14d
testb %al, %al
je 0xa1252
movl %eax, %ebp
andb $0x7f, %al
xorl %r13d, %r13d
movl $0x3, %r14d
cmpb $0x1, %al
je 0xa125b
movl $0x2, %r14d
cmpl $0x1, %r12d
jne 0xa1237
cmpb $0x5b, %al
je 0xa125b
cmpl $0x1, %r12d
sete %cl
cmpl $0x3, 0xc(%rsp)
setge %dl
xorl %r13d, %r13d
testb %dl, %cl
je 0xa1268
xorl %r14d, %r14d
jmp 0xa125b
movl $0x3, %r13d
movl %r12d, %r14d
movl %r14d, %r12d
testl %r13d, %r13d
je 0xa11e4
jmp 0xa132c
movzbl %al, %esi
cmpl $0x1, %r12d
jne 0xa12cf
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl %esi, %r14d
callq 0xf69a2
cmpl $0x0, 0x8(%rsp)
je 0xa1314
movl 0xc(%rsp), %edx
cmpl $0x3, %edx
setge %al
testb %bpl, %bpl
setns %cl
orb %al, %cl
jne 0xa12b9
movslq %edx, %rax
leaq 0x90ea6(%rip), %rcx     # 0x132150
movb (%rax,%rcx), %cl
movb %cl, 0x14(%rsp,%rax)
movl $0x1, 0x10(%rsp)
incl %edx
movl %edx, 0xc(%rsp)
movl $0x1, %r14d
movl $0x1, 0x8(%rsp)
jmp 0xa125b
cmpl $0x2, %r12d
jne 0xa12dd
cmpb $0x5d, %al
je 0xa124d
cmpl $0x2, %r12d
jne 0xa1258
movq 0x18(%rsp), %r12
cmpq $0x0, 0x8(%r12)
jne 0xa1305
movq %rbx, %rdi
movl %esi, %ebp
callq 0xf6870
movl %ebp, %esi
movq %rax, 0x8(%r12)
movq 0x8(%r12), %rdi
callq 0xf69a2
jmp 0xa125b
xorl %eax, %eax
cmpl $0x2e, %r14d
sete %al
movl %eax, 0x8(%rsp)
movl $0x1, %r14d
jmp 0xa125b
cmpl $0x3, %r13d
jne 0xa13ad
movq 0x18(%rsp), %r14
movq (%r14), %rdi
callq 0xf6900
movq 0x20(%rsp), %rax
subq %r15, %rax
movq %rax, 0x10(%r14)
movq (%r14), %rdi
callq 0xf7557
leaq 0x9a67a(%rip), %rsi     # 0x13b9d4
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
cmpl $0x0, 0x10(%rsp)
je 0xa1384
leaq 0x90bc8(%rip), %rsi     # 0x131f3d
leaq 0x14(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x8(%r14), %rdi
movl $0x1, %ebp
testq %rdi, %rdi
je 0xa13af
callq 0xf7557
leaq 0x73407(%rip), %rsi     # 0x1147a5
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
jmp 0xa13af
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq