// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/zar-extract.c
// Function: main
int main( int argc, char **argv ) {
	
	char *name = NULL;
	unsigned char *comdata = NULL, *uncomdata = NULL;
	uint32_t *size;
	unsigned int memoff, len;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s _zar-file\n", argv[0]);
		return 1;
	}
	
	uint32_t numfiles, workfiles, uncomsize, filesize;
	unsigned long int havebuf;
	
	FILE *f, *o;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_END);
	filesize = ftell(f);
	fseek(f, 0, SEEK_SET);
	
	fread( &uncomsize, 4, 1, f );
	
	printf("Compressed size %08x (file is %08x)\n", filesize-4, filesize);
	printf("Uncompressed size: %08x\n", uncomsize );
	havebuf = uncomsize;
	
	comdata = malloc(filesize-4);
	uncomdata = malloc(uncomsize);
	
	fread(comdata, filesize-4, 1, f);
	
	if((workfiles = uncompress(uncomdata, &havebuf, comdata, filesize-4)) != Z_OK) {
		printf("=====================================couldnt decompress data\n");
		switch(workfiles) {
			case Z_MEM_ERROR: {
				printf("memory error\n");
				break;
			}
			case Z_NEED_DICT: {
				printf("need dictionary\n");
				break;
			}
			case Z_DATA_ERROR: {
				printf("input corrupted\n");
				//~ printf("%s\n", strm.msg);
				break;
			}
			case Z_STREAM_ERROR: {
				printf("broken stream\n");
				break;
			}
			case Z_BUF_ERROR: {
				printf("buffer error\n");
				break;
			}
			default: {
				printf("unknown error\n");
				break;
			}
		}
		printf("uncomsize %08x\n", uncomsize);
		return 1;
	}
	free(comdata);
	fclose(f);
	
	numfiles = *uncomdata;
	
	printf("Number of files: %u\n", numfiles);
	
	memoff = 4;
	
	for( workfiles = 0; workfiles < numfiles; workfiles++ ) {
		printf("offset %08x\n", memoff);
		name = (char *)uncomdata+memoff;
		len = strlen(name) + 1;
		if((memoff+len)%4) len+= 4-((memoff+len)%4);
		memoff += len;
		
		printf("offset %08x\n", memoff);
		size = (uint32_t *)(uncomdata+memoff);
		len = 4;
		if((memoff+len)%0x10) len += 0x10-((memoff+len)%0x10);
		memoff += len;
		
		printf("Current file: %s - Size %08x\n", name, *size);
		
		if( !(o = fopen( name, "wb" ))) {
			printf("Couldnt open output %s\n", name);
			return 1;
		}
		
		fwrite(uncomdata+memoff, *size, 1, o);
		
		fclose(o);
		
		memoff += *size + 1;
	}
	
	free(uncomdata);
	printf("Done.\n");
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
cmpl $0x1, %edi
jg 0x12d4
movq (%rbx), %rsi
leaq 0xd83(%rip), %rdi       # 0x2052
jmp 0x1514
movq 0x8(%rbx), %rdi
leaq 0xd97(%rip), %rsi       # 0x2076
callq 0x10d0
testq %rax, %rax
je 0x13cd
movq %rax, %r14
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x10c0
movq %r14, %rdi
callq 0x10a0
movq %rax, %rbx
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10c0
leaq 0xc(%rsp), %r15
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1050
leal -0x4(%rbx), %r13d
leaq 0xd5a(%rip), %rdi       # 0x208f
movl %r13d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1090
movl (%r15), %r15d
leaq 0xd69(%rip), %rdi       # 0x20b4
movl %r15d, %esi
xorl %eax, %eax
callq 0x1090
leaq 0x10(%rsp), %r12
movq %r15, (%r12)
movq %r13, %rdi
callq 0x10b0
movq %rax, %rbp
movq %r15, %rdi
callq 0x10b0
movq %rax, %rbx
movq %rbp, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x1050
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x1070
testl %eax, %eax
je 0x13dd
movl %eax, %r12d
leaq 0xd9a(%rip), %rdi       # 0x213e
callq 0x1040
addl $0x5, %r12d
cmpl $0x7, %r12d
ja 0x14d3
movl %r12d, %eax
leaq 0xe13(%rip), %rcx       # 0x21d4
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
jmp 0x14da
movq 0x8(%rbx), %rsi
leaq 0xca1(%rip), %rdi       # 0x2079
jmp 0x1514
movq %rbp, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1060
movzbl (%rbx), %ebp
leaq 0xce6(%rip), %rdi       # 0x20dd
movl %ebp, %esi
xorl %eax, %eax
callq 0x1090
pushq $0x4
popq %rax
leaq 0xce8(%rip), %r14       # 0x20f2
movl %eax, %r12d
subl $0x1, %ebp
jb 0x14f2
movq %r14, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
movl %r12d, %r15d
addq %rbx, %r15
movq %r15, %rdi
callq 0x1080
movq %rax, %r13
incl %eax
leal (%r12,%r13), %ecx
incl %ecx
andl $0x3, %ecx
subl %ecx, %r13d
addl $0x5, %r13d
testl %ecx, %ecx
cmovel %eax, %r13d
addl %r12d, %r13d
movq %r14, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x1090
movl (%rbx,%r13), %edx
leaq 0xc98(%rip), %rdi       # 0x20ff
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
movq %r15, %rdi
leaq 0xca2(%rip), %rsi       # 0x211d
callq 0x10d0
testq %rax, %rax
je 0x150a
movq %rax, %r12
leal 0x4(%r13), %eax
andl $0xf, %eax
pushq $0x14
popq %rcx
movl %ecx, %r15d
subl %eax, %r15d
testl %eax, %eax
pushq $0x4
popq %rax
cmovel %eax, %r15d
addl %r13d, %r15d
leaq (%rbx,%r15), %rdi
movl (%rbx,%r13), %esi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x10f0
movq %r12, %rdi
callq 0x1060
movl (%rbx,%r13), %eax
leal (%rax,%r15), %r12d
incl %r12d
jmp 0x140d
leaq 0xce9(%rip), %rdi       # 0x21c3
callq 0x1040
leaq 0xbe7(%rip), %rdi       # 0x20cd
movl %r15d, %esi
xorl %eax, %eax
callq 0x1090
jmp 0x151b
movq %rbx, %rdi
callq 0x1030
leaq 0xc37(%rip), %rdi       # 0x2138
callq 0x1040
xorl %eax, %eax
jmp 0x151e
leaq 0xc0f(%rip), %rdi       # 0x2120
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
pushq $0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq