// File: /workspace/github_c_makefile_star2_O2/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 0x12c4
movq (%r14), %rsi
leaq 0xd93(%rip), %rdi       # 0x2052
jmp 0x14da
movq 0x8(%r14), %rdi
leaq 0xda6(%rip), %rsi       # 0x2075
callq 0x10c0
testq %rax, %rax
je 0x1497
movq %rax, %rbx
pushq $-0x4
popq %rsi
pushq $0x2
popq %rdx
movq %rax, %rdi
callq 0x10b0
leaq 0x18(%rsp), %r14
pushq $0x4
popq %r15
pushq $0x1
popq %rbp
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1050
movl (%r14), %r14d
leaq 0xd79(%rip), %rdi       # 0x208e
xorl %r13d, %r13d
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x10b0
leaq 0x1c(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1050
movl (%r14), %r14d
leaq 0xd55(%rip), %rdi       # 0x20a4
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movq %r14, 0x10(%rsp)
leaq (,%r14,8), %r15
movq %r15, %rdi
callq 0x10a0
movq %rax, %r14
movq %r15, %rdi
callq 0x10a0
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1050
cmpq 0x10(%rsp), %r13
je 0x13e7
movq %rbx, %rdi
callq 0x1090
movq %rax, %rbp
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x1080
incl %r14d
testl %eax, %eax
jne 0x13a7
movl %ebp, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10b0
movl %r14d, %r15d
movq %r15, %rdi
callq 0x10a0
movq %rax, (%r12,%r13,8)
movq %rax, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1050
incq %r13
jmp 0x1392
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10b0
xorl %r14d, %r14d
cmpq %r14, 0x10(%rsp)
je 0x14a4
movq (%r12,%r14,8), %r13
movq 0x8(%rsp), %rax
movl (%rax,%r14,8), %ecx
movl 0x4(%rax,%r14,8), %ebp
leaq 0xc9f(%rip), %rdi       # 0x20b9
movq %r13, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1070
movq %r13, %rdi
leaq 0xcb3(%rip), %rsi       # 0x20e3
callq 0x10c0
testq %rax, %rax
je 0x14ce
movq %rax, %r15
movq %rbx, %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x11ec
movq %r15, %rdi
callq 0x1060
movq %rbx, %rdi
callq 0x1090
testb $0x7f, %al
je 0x1487
movq %rbx, %rdi
callq 0x1090
cqto
movl $0x80, %ecx
idivq %rcx
movl $0x80, %esi
subq %rdx, %rsi
movq %rbx, %rdi
pushq $0x1
popq %rdx
callq 0x10b0
movq %r13, %rdi
callq 0x1030
incq %r14
jmp 0x13f6
movq 0x8(%r14), %rsi
leaq 0xbd6(%rip), %rdi       # 0x2078
jmp 0x14da
movq %r12, %rdi
callq 0x1030
movq 0x8(%rsp), %rdi
callq 0x1030
leaq 0xc41(%rip), %rdi       # 0x20fe
callq 0x1040
movq %rbx, %rdi
callq 0x1060
xorl %eax, %eax
jmp 0x14e4
leaq 0xc11(%rip), %rdi       # 0x20e6
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x1070
pushq $0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq