// File: /workspace/github_c_makefile_star2_O2/excess-c64[P]lib1541img/src/lib/1541img/lynx.c
// Function: extractLynx
SOEXPORT int extractLynx(CbmdosVfs *vfs, const FileData *file)
{
    size_t size = FileData_size(file);
    const uint8_t *content = FileData_rcontent(file);
    size_t sigpos;
    uint8_t dirblocks;
    size_t pos;
    if (findHeader(&sigpos, &dirblocks, &pos, content, size) < 0)
    {
	logmsg(L_ERROR, "extractLynx: not a valid LyNX file.");
	return -1;
    }
    char sig[80] = {0};
    size_t siglen = pos - sigpos - 1;
    if (siglen >= sizeof sig) siglen = sizeof sig - 1;
    memcpy(sig, content + sigpos, siglen);
    logfmt(L_INFO, "extractLynx: found signature `%s'.", sig);
    uint16_t numfiles;
    if (parsePetsciiNum(&numfiles, &pos, content, size) < 0)
    {
	logmsg(L_ERROR, "extractLynx: couldn't read number of files.");
	return -1;
    }
    LynxDirEntry *dir = xmalloc(numfiles * sizeof *dir);
    memset(dir, 0, numfiles * sizeof *dir);
    int rc = -1;

    for (int i = 0; i < numfiles; ++i)
    {
	uint8_t namelen = 0;
	while (pos + namelen < size
		&& content[pos + namelen] != 0x0d) ++namelen;
	if (pos + namelen + 1 >= size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	size_t tmppos = pos + namelen + 1;
	while (content[pos + namelen - 1] == 0xa0) --namelen;
	dir[i].file = CbmdosFile_create();
	CbmdosFile_setName(dir[i].file, (const char *)(content + pos), namelen);
	pos = tmppos;
	if (parsePetsciiNum(&dir[i].blocks, &pos, content, size) < 0)
	{
	    logmsg(L_ERROR, "extractLynx: error parsing block size of file.");
	    goto done;
	}
	if (!content[pos])
	{
	    logmsg(L_ERROR, "extractLynx: invalid file type found.");
	    goto done;
	}
	int t;
	for (t = 1; t < (int) sizeof lynxfiletype; ++t)
	{
	    if (lynxfiletype[t] == content[pos]) break;
	}
	if (t == sizeof lynxfiletype)
	{
	    logmsg(L_ERROR, "extractLynx: invalid file type found.");
	    goto done;
	}
	CbmdosFileType type = (CbmdosFileType) t;
	if (++pos == size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	if (content[pos] != 0x0d)
	{
	    logmsg(L_ERROR, "extractLynx: invalid file type found.");
	    goto done;
	}
	if (++pos == size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	CbmdosFile_setType(dir[i].file, type);
	if (type == CFT_REL)
	{
	    uint16_t recordlen;
	    if (parsePetsciiNum(&recordlen, &pos, content, size) < 0)
	    {
		logmsg(L_ERROR,
			"extractLynx: error parsing record length of file.");
		goto done;
	    }
	    CbmdosFile_setRecordLength(dir[i].file, recordlen);
	}
	uint16_t lsu;
	if (parsePetsciiNum(&lsu, &pos, content, size) < 0)
	{
	    if (i == numfiles - 1)
	    {
		logmsg(L_INFO, "extractLynx: last block usage of last file "
			"missing, assuming size from the container.");
		dir[i].size = 0;
	    }
	    else
	    {
		logmsg(L_ERROR, "extractLynx: error parsing last block usage "
			"of file.");
		goto done;
	    }
	}
	else
	{
	    dir[i].size = 254 * (dir[i].blocks - 1) + (lsu - 1);
	}
    }

    for (int i = 0; i < numfiles; ++i)
    {
	size_t pad = 254 - (pos%254);
	pos += pad;
	if (pos >= size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	if (i == 0 && pos != dirblocks * 254)
	{
	    logmsg(L_WARNING, "extractLynx: inconsistent file, directory "
		    "block size is wrong.");
	}
	if (CbmdosFile_type(dir[i].file) == CFT_REL)
	{
	    uint8_t sidesectnum = (dir[i].blocks / 120)
		+ !!(dir[i].blocks % 120);
	    pos += 254 * sidesectnum;
	    if (pos >= size)
	    {
		logmsg(L_ERROR, "extractLynx: unexpected end of file.");
		goto done;
	    }
	}
	if (i == numfiles - 1 && !dir[i].size)
	{
	    dir[i].size = size - pos;
	    if (dir[i].size < (dir[i].blocks-1U) * 254)
	    {
		logmsg(L_ERROR, "extractLynx: unexpected end of file.");
		goto done;
	    }
	    if (dir[i].size > dir[i].blocks * 254)
	    {
		logmsg(L_WARNING, "extractLynx: unexpected garbage after end "
			"of file, archive might be corrupt.");
		dir[i].size = dir[i].blocks * 254;
	    }
	}
	if (pos + dir[i].size > size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	if (FileData_append(CbmdosFile_data(dir[i].file),
		    content + pos, dir[i].size) < 0)
	{
	    logmsg(L_ERROR, "extractLynx: error writing file.");
	    goto done;
	}
	pos += dir[i].size;
    }
    if (pos < size)
    {
	size_t pad = 254 - (pos%254);
	if (pos + pad != size)
	{
	    logmsg(L_WARNING,
		    "extractLynx: file doesn't have the expected size");
	}
    }

    for (int i = 0; i < numfiles; ++i)
    {
	if (CbmdosVfs_append(vfs, dir[i].file) < 0)
	{
	    logmsg(L_ERROR, "extractLynx: error adding file to filesystem.");
	    goto done;
	}
	else dir[i].file = 0;
    }

    rc = 0;

done:
    for (int i = 0; i < numfiles; ++i) CbmdosFile_destroy(dir[i].file);
    free(dir);
    return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movq %rsi, %rdi
callq 0x5530
movq %rax, %r12
movq %rbx, %rdi
callq 0x5680
movq %rax, %r13
leaq 0x48(%rsp), %rdi
leaq 0x15(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %rax, %rcx
movq %r12, %r8
callq 0xb07d
testl %eax, %eax
js 0xb48e
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rbx
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movq 0x48(%rsp), %rsi
movq %rsi, %rax
notq %rax
leaq 0x18(%rsp), %r14
addq (%r14), %rax
cmpq $0x4f, %rax
pushq $0x4f
popq %rdx
cmovbq %rax, %rdx
addq %r13, %rsi
movq %rbx, %rdi
callq 0x54a0
leaq 0x32da(%rip), %rsi      # 0xe55c
pushq $0x3
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x598d
leaq 0x16(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0xb709
testl %eax, %eax
js 0xb4d3
movq %r15, 0x30(%rsp)
movzwl 0x16(%rsp), %r14d
imulq $0x18, %r14, %rbx
movq %rbx, %rdi
callq 0x589c
movq %rax, (%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, 0x8(%rsp)
movq %rbx, %rdx
xorl %ebx, %ebx
callq 0x5330
movl %r14d, %eax
decl %eax
movq %rax, 0x28(%rsp)
movq %r12, 0x40(%rsp)
movq %r14, 0x38(%rsp)
movq 0x18(%rsp), %rbp
cmpq %r14, %rbx
je 0xb4fb
movb $0x1, %al
xorl %ecx, %ecx
movzbl %cl, %r14d
addq %rbp, %r14
cmpq %r12, %r14
jae 0xb319
cmpb $0xd, (%r13,%r14)
je 0xb319
incb %cl
incb %al
jmp 0xb2ff
incq %r14
cmpq %r12, %r14
jae 0xb635
addq %r13, %rbp
decb %al
movzbl %al, %r15d
cmpb $-0x60, -0x1(%rbp,%r15)
je 0xb328
callq 0x53b0
movq %rbx, 0x20(%rsp)
imulq $0x18, %rbx, %rbx
movq (%rsp), %r12
movq %rax, (%r12,%rbx)
movq %rax, %rdi
movq %rbp, %rsi
movl %r15d, %edx
callq 0x5300
movq %r14, 0x18(%rsp)
leaq (%r12,%rbx), %rbp
addq $0x8, %rbp
movq %rbp, %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
movq %r13, %rdx
movq 0x40(%rsp), %r12
movq %r12, %rcx
callq 0xb709
testl %eax, %eax
js 0xb641
movq 0x18(%rsp), %rax
movb (%r13,%rax), %cl
testb %cl, %cl
je 0xb497
addq (%rsp), %rbx
pushq $-0x4
popq %r14
leaq 0x3445(%rip), %rdx      # 0xe7f0
testq %r14, %r14
je 0xb497
cmpb %cl, 0x5(%r14,%rdx)
je 0xb3c0
incq %r14
jmp 0xb3ab
leaq 0x1(%rax), %rcx
cmpq %r12, %rcx
je 0xb635
cmpb $0xd, (%r13,%rcx)
jne 0xb497
addq $0x2, %rax
movq %rax, 0x18(%rsp)
cmpq %r12, %rax
je 0xb635
leaq 0x5(%r14), %rsi
movq (%rbx), %rdi
callq 0x5110
cmpq $-0x1, %r14
movq 0x38(%rsp), %r14
jne 0xb42a
leaq 0x12(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0xb709
testl %eax, %eax
js 0xb6b9
movq (%rbx), %rdi
movzbl 0x12(%rsp), %esi
callq 0x55b0
leaq 0x12(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0xb709
testl %eax, %eax
js 0xb45c
movzwl (%rbp), %eax
imulq $0xfe, %rax, %rax
movzwl 0x12(%rsp), %ecx
addq %rcx, %rax
addq $-0xff, %rax
jmp 0xb47d
movq 0x28(%rsp), %rax
cmpq %rax, 0x20(%rsp)
jne 0xb6c5
pushq $0x3
popq %rdi
leaq 0x31e1(%rip), %rsi      # 0xe657
callq 0x5960
xorl %eax, %eax
movq %rax, 0x10(%rbx)
movq 0x20(%rsp), %rbx
incq %rbx
jmp 0xb2ed
leaq 0x30a3(%rip), %rsi      # 0xe538
jmp 0xb4da
leaq 0x3161(%rip), %rsi      # 0xe5ff
pushq $0x1
popq %rdi
callq 0x5960
pushq $-0x1
popq %r15
movq 0x8(%rsp), %r14
movq (%rsp), %r12
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0xb4c9
movq (%r12,%rbx), %rdi
callq 0x50e0
addq $0x18, %rbx
jmp 0xb4b5
movq %r12, %rdi
callq 0x5080
jmp 0xb4e6
leaq 0x30a5(%rip), %rsi      # 0xe57f
pushq $0x1
popq %rdi
callq 0x5960
pushq $-0x1
popq %r15
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x15(%rsp), %eax
imulq $0xfe, %rax, %r14
imulq $0x18, 0x28(%rsp), %rax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
cmpq %r15, 0x8(%rsp)
je 0xb64d
movq %rbp, %rax
xorl %edx, %edx
movl $0xfe, %ecx
divq %rcx
subq %rdx, %rbp
addq %rcx, %rbp
cmpq %r12, %rbp
jae 0xb635
testq %r15, %r15
jne 0xb555
cmpq %r14, %rbp
je 0xb555
pushq $0x2
popq %rdi
leaq 0x3192(%rip), %rsi      # 0xe6e2
callq 0x5960
movq (%rsp), %rbx
movq (%rbx,%r15), %rdi
callq 0x57c0
cmpl $0x4, %eax
jne 0xb595
movzwl 0x8(%rbx,%r15), %eax
xorl %edx, %edx
movw $0x78, %cx
divw %cx
xorl %ecx, %ecx
testw %dx, %dx
setne %cl
addl %eax, %ecx
movzbl %cl, %eax
imull $0xfe, %eax, %eax
addq %rax, %rbp
cmpq %r12, %rbp
jae 0xb6e9
movq 0x10(%rbx,%r15), %rax
cmpq %r15, 0x20(%rsp)
jne 0xb5f7
testq %rax, %rax
jne 0xb5f7
movq %r12, %rax
subq %rbp, %rax
movq (%rsp), %rcx
movq %rax, 0x10(%rcx,%r15)
movzwl 0x8(%rcx,%r15), %ecx
imulq $0xfe, %rcx, %rcx
leal -0xfe(%rcx), %edx
cmpq %rdx, %rax
jb 0xb635
cmpq %rcx, %rax
jbe 0xb5f7
pushq $0x2
popq %rdi
leaq 0x3145(%rip), %rsi      # 0xe721
callq 0x5960
movq (%rsp), %rcx
movzwl 0x8(%rcx,%r15), %eax
imulq $0xfe, %rax, %rax
movq %rax, 0x10(%rcx,%r15)
addq %rbp, %rax
cmpq %r12, %rax
ja 0xb635
movq (%rsp), %rbx
movq (%rbx,%r15), %rdi
callq 0x5640
movq %r13, %rsi
addq %rbp, %rsi
movq 0x10(%rbx,%r15), %rdx
movq %rax, %rdi
callq 0x5760
testl %eax, %eax
js 0xb6b0
addq 0x10(%rbx,%r15), %rbp
addq $0x18, %r15
jmp 0xb515
leaq 0x2f6f(%rip), %rsi      # 0xe5ab
jmp 0xb49e
leaq 0x2f88(%rip), %rsi      # 0xe5d0
jmp 0xb49e
cmpq %r12, %rbp
jae 0xb679
movl $0xfe, %ecx
movq %rbp, %rax
xorl %edx, %edx
divq %rcx
subq %rdx, %rbp
addq %rcx, %rbp
cmpq %r12, %rbp
je 0xb679
leaq 0x311e(%rip), %rsi      # 0xe78f
pushq $0x2
popq %rdi
callq 0x5960
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x8(%rsp), %r14
movq (%rsp), %r12
movq 0x30(%rsp), %r13
cmpq %rbx, %r14
je 0xb4b3
movq (%r12,%rbx), %rsi
movq %r13, %rdi
callq 0x54e0
testl %eax, %eax
js 0xb6d1
andq $0x0, (%r12,%rbx)
addq $0x18, %rbx
jmp 0xb68c
leaq 0x30b7(%rip), %rsi      # 0xe76e
jmp 0xb6f0
leaq 0x2f65(%rip), %rsi      # 0xe625
jmp 0xb49e
leaq 0x2fe1(%rip), %rsi      # 0xe6ad
jmp 0xb49e
leaq 0x30e8(%rip), %rsi      # 0xe7c0
pushq $0x1
popq %rdi
callq 0x5960
pushq $-0x1
popq %r15
jmp 0xb4b3
leaq 0x2ebb(%rip), %rsi      # 0xe5ab
pushq $0x1
popq %rdi
callq 0x5960
pushq $-0x1
popq %r15
movq 0x8(%rsp), %r14
movq %rbx, %r12
jmp 0xb4b3