// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-mgs/qar-extract_psp.c
// Function: main
int main( int argc, char **argv ) {
	
	char name[64];
	uint32_t len;
	unsigned int remember;
	
	entry *filelist = NULL;
	char **namelist = NULL;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s QAR-file\n", argv[0]);
		return 1;
	}
	
	uint32_t numFiles, workfiles, listoff;
	FILE *f, *o;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, -4, SEEK_END);
	
	fread( &listoff, 4, 1, f );
	
	printf("Offset of list: %08x\n", listoff);
	
	fseek(f, listoff, SEEK_SET);
	
	fread( &numFiles, 4, 1, f );
	
	printf("Number of files: %u\n", numFiles );
	
	filelist = malloc(numFiles*sizeof(entry));
	namelist = malloc(numFiles*sizeof(char *));
	
	fread(filelist, sizeof(filelist)*numFiles, 1, f);
	
	for( workfiles = 0; workfiles < numFiles; workfiles++ ) {
		remember = ftell(f);
		len = 0;
		while(fgetc(f)) len++;
		len++;
		fseek(f, remember, SEEK_SET);
		namelist[workfiles] = malloc(len);
		
		fread(namelist[workfiles], len, 1, f );
	}
	
	fseek(f, 0, SEEK_SET);
	
	for( workfiles = 0; workfiles < numFiles; workfiles++ ) {
		printf("Current file: %s - Size %08x - Hash %08x\n", namelist[workfiles], filelist[workfiles].size, filelist[workfiles].hash);
		
		if( !(o = fopen( namelist[workfiles], "wb" ))) {
			printf("Couldnt open output %s\n", name);
			return 1;
		}
		
		writeFile( f, filelist[workfiles].size, o );
		fclose(o);
		if(ftell(f)%0x80) fseek(f, 0x80-(ftell(f)%0x80), SEEK_CUR);
		free(namelist[workfiles]);
	}
	free(namelist);
	free(filelist);
	
	printf("Done.\n");
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
cmpl $0x1, %edi
jg 0x12e0
movq (%r14), %rsi
leaq 0xd77(%rip), %rdi       # 0x2052
jmp 0x1514
movq 0x8(%r14), %rdi
leaq 0xd8a(%rip), %rsi       # 0x2075
callq 0x10c0
testq %rax, %rax
je 0x14fb
movq %rax, %rbx
movq %rax, %rdi
movq $-0x4, %rsi
movl $0x2, %edx
callq 0x10b0
leaq 0x18(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1050
movl (%r14), %r14d
leaq 0xd5a(%rip), %rdi       # 0x208e
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x10b0
leaq 0x1c(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1050
movl (%r14), %r14d
leaq 0xd35(%rip), %rdi       # 0x20a4
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
leaq (,%r14,8), %r15
movq %r15, %rdi
callq 0x10a0
movq %rax, %r12
movq %r15, %rdi
callq 0x10a0
movq %rax, 0x8(%rsp)
movl $0x1, %edx
movq %r12, 0x10(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x1050
testq %r14, %r14
je 0x1410
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x1090
movq %rax, %r13
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x1080
incl %ebp
testl %eax, %eax
jne 0x13c6
movl %r13d, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10b0
movl %ebp, %r15d
movq %r15, %rdi
callq 0x10a0
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r12,8)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x1050
incq %r12
cmpq %r14, %r12
jne 0x13b9
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10b0
testq %r14, %r14
je 0x14cf
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %rbp
movq 0x10(%rsp), %rax
movl (%rax,%r12,8), %ecx
movl 0x4(%rax,%r12,8), %r13d
leaq 0xc73(%rip), %rdi       # 0x20b9
movq %rbp, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1070
movq %rbp, %rdi
leaq 0xc86(%rip), %rsi       # 0x20e3
callq 0x10c0
testq %rax, %rax
je 0x1508
movq %rax, %r15
movq %rbx, %rdi
movl %r13d, %esi
movq %rax, %rdx
callq 0x11ec
movq %r15, %rdi
callq 0x1060
movq %rbx, %rdi
callq 0x1090
testb $0x7f, %al
je 0x14bb
movq %rbx, %rdi
callq 0x1090
leaq 0x7f(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
andq $-0x80, %rsi
subq %rax, %rsi
subq $-0x80, %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x10b0
movq %rbp, %rdi
callq 0x1030
incq %r12
cmpq %r12, %r14
jne 0x1428
movq 0x8(%rsp), %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
callq 0x1030
leaq 0xc14(%rip), %rdi       # 0x20fe
callq 0x1040
movq %rbx, %rdi
callq 0x1060
xorl %eax, %eax
jmp 0x1520
movq 0x8(%r14), %rsi
leaq 0xb72(%rip), %rdi       # 0x2078
jmp 0x1514
leaq 0xbd7(%rip), %rdi       # 0x20e6
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x1070
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq