// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x1, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl 0xe585b(%rip), %eax     # 0x1d2049
movl %eax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
subq -0x10(%rbp), %rax
cmpq $0x12c, %rax            # imm = 0x12C
jle 0xec820
jmp 0xeca13
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xec837
jmp 0xeca13
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movb %al, -0x3d(%rbp)
movzbl -0x3d(%rbp), %eax
cmpl $0x0, %eax
jne 0xec859
jmp 0xec974
movzbl -0x3d(%rbp), %eax
andl $0x7f, %eax
movb %al, -0x3e(%rbp)
movzbl -0x3e(%rbp), %eax
cmpl $0x1, %eax
jne 0xec878
movl $0x3, -0x2c(%rbp)
jmp 0xec96f
cmpl $0x1, -0x2c(%rbp)
jne 0xec893
movzbl -0x3e(%rbp), %eax
cmpl $0x5b, %eax
jne 0xec893
movl $0x2, -0x2c(%rbp)
jmp 0xec96d
cmpl $0x1, -0x2c(%rbp)
jne 0xec8ab
cmpl $0x3, -0x34(%rbp)
jl 0xec8ab
movl $0x0, -0x2c(%rbp)
jmp 0xec96b
cmpl $0x1, -0x2c(%rbp)
jne 0xec917
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movzbl -0x3e(%rbp), %esi
callq 0x1714c0
cmpl $0x0, -0x30(%rbp)
je 0xec903
cmpl $0x3, -0x34(%rbp)
jge 0xec8f8
movzbl -0x3d(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0xec8f8
movl $0x1, -0x3c(%rbp)
movslq -0x34(%rbp), %rcx
leaq 0xd159e(%rip), %rax     # 0x1bde8b
movb (%rax,%rcx), %cl
movslq -0x34(%rbp), %rax
movb %cl, -0x38(%rbp,%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xec915
movzbl -0x3e(%rbp), %eax
cmpl $0x2e, %eax
jne 0xec913
movl $0x1, -0x30(%rbp)
jmp 0xec915
jmp 0xec969
cmpl $0x2, -0x2c(%rbp)
jne 0xec92f
movzbl -0x3e(%rbp), %eax
cmpl $0x5d, %eax
jne 0xec92f
movl $0x0, -0x2c(%rbp)
jmp 0xec967
cmpl $0x2, -0x2c(%rbp)
jne 0xec965
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xec954
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movzbl -0x3e(%rbp), %esi
callq 0x1714c0
jmp 0xec967
jmp 0xec969
jmp 0xec96b
jmp 0xec96d
jmp 0xec96f
jmp 0xec80b
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x171360
movq -0x28(%rbp), %rcx
subq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x1, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x172650
movq -0x48(%rbp), %rdi
movq %rax, %rdx
leaq 0xdabca(%rip), %rsi     # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x3c(%rbp)
je 0xec9dc
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rdx
leaq 0xd12a3(%rip), %rsi     # 0x1bdc78
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0xeca11
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x172650
movq -0x50(%rbp), %rdi
movq %rax, %rdx
leaq 0xb3c42(%rip), %rsi     # 0x1a064c
movb $0x0, %al
callq 0x15b0d0
jmp 0xeca13
movl -0x1c(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)