// File: /workspace/github_c_makefile_star2_O2/AMGarkin[P]BDOcrypt/BDO_decrypt.c
// Function: main
int main(int argc, char **argv)
{
	if (argc != 3){
		printf("BDO_decrypt <source file> <output file>");
		return 1;
	}

	char* srcFileName = argv[1];
	char* outFileName = argv[2];

	FILE* srcFile = fopen(srcFileName, "rb");
	if (srcFile == NULL){
		printf("File not found: %s", srcFileName);
		return -1;
	}
	FILE* tmpFile = tmpfile();
	FILE* outFile = fopen(outFileName, "wb");

	///Decompress source to temporary file
	unsigned long compressedSize = 0;
	unsigned long uncompressedSize = 0;

	fseek(srcFile, 0, SEEK_END);
	compressedSize = ftell(srcFile) - 4; // 1st 4 bytes holds information about uncompressed data size
	rewind(srcFile);

	fread(&uncompressedSize, 4, 1, srcFile);

	unsigned char *pCompressedData = (unsigned char *) calloc(compressedSize, sizeof(unsigned char));
	unsigned char *pUncompressedData = (unsigned char *) calloc(uncompressedSize, sizeof(unsigned char));

	fread(pCompressedData, 1, compressedSize, srcFile);

	int result = uncompress(pUncompressedData, &uncompressedSize, pCompressedData, compressedSize);

	if (result == Z_OK) {
		fwrite(pUncompressedData, 1, uncompressedSize, tmpFile);

		///Convert .bss data from temporary file to text file
		unsigned long strSize;
		unsigned long strType;
		unsigned long strID1;
		unsigned short strID2;
		unsigned char strID3;
		unsigned char strID4;
		int a, b;
		wchar_t tmp;
		wchar_t strBuff[MAX_BUFF_SIZE];

		rewind(tmpFile);

		//write BOM (byte order mark) 0xFEFF = UTF-16 little-endian
		fwrite(&BOM_UTF16LE, 2, 1, outFile);

		while (1){
			if (fread(&strSize, 4, 1, tmpFile) != 1) break;
			if (fread(&strType, 4, 1, tmpFile) != 1) break;
			if (fread(&strID1, 4, 1, tmpFile) != 1) break;
			if (fread(&strID2, 2, 1, tmpFile) != 1) break;
			if (fread(&strID3, 1, 1, tmpFile) != 1) break;
			if (fread(&strID4, 1, 1, tmpFile) != 1) break;
			b = 0;
			for (a = 0; a < strSize + 2; a++) {
				if (fread(&tmp, 2, 1, tmpFile) != 1) break;
				if (tmp == CHAR_LF) {
					strBuff[b] = L'\\';
					b++;
					strBuff[b] = L'n';
					b++;
				} else {
					strBuff[b] = tmp;
					b++;
				}
			}

			fwprintf(outFile, L"%u\t%u\t%u\t%u\t%u\t\"%s\"\r\n", strType, strID1, strID2, strID3, strID4, strBuff);
		}
	} else if (result == Z_MEM_ERROR) {
		printf("ERROR: Not enough memory.");
	} else if (result == Z_BUF_ERROR) {
		printf("ERROR: Output buffer is too small.");
	} else if (result == Z_DATA_ERROR) {
		printf("ERROR: Input data are corrupted or incomplete.");
	}

	fclose(srcFile);
	fclose(tmpFile);
	fclose(outFile);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4048, %rsp           # imm = 0x4048
cmpl $0x3, %edi
jne 0x4018a8
movq 0x8(%rsi), %r15
movq 0x10(%rsi), %r14
leaq 0x9e877(%rip), %rsi     # 0x4a0048
movq %r15, %rdi
callq 0x419710
testq %rax, %rax
je 0x4018ba
movq %rax, %rbx
callq 0x412080
movq %rax, %r15
leaq 0x9e86a(%rip), %rsi     # 0x4a005e
movq %r14, %rdi
callq 0x419710
movq %rax, 0x8(%rsp)
leaq 0x18(%rsp), %rbp
andq $0x0, (%rbp)
pushq $0x2
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41d4c0
movq %rbx, %rdi
callq 0x419910
leaq -0x4(%rax), %r13
movq %rbx, %rdi
callq 0x41d5a0
pushq $0x4
popq %rsi
pushq $0x1
popq %r14
movq %rbp, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x419810
movq %r13, %rdi
movq %r14, %rsi
callq 0x429fd0
movq %rax, %r12
movq (%rbp), %rdi
movq %r14, %rsi
callq 0x429fd0
movq %rax, %rbp
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, 0x20(%rsp)
movq %rbx, %rcx
callq 0x419810
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x401c50
addl $0x5, %eax
cmpl $0x5, %eax
ja 0x4018e7
leaq 0x9e76e(%rip), %rcx     # 0x4a0004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x9e7d5(%rip), %rdi     # 0x4a007b
jmp 0x4018e0
leaq 0x9e771(%rip), %rdi     # 0x4a0020
xorl %eax, %eax
callq 0x411ef0
pushq $0x1
jmp 0x4018cd
leaq 0x9e78a(%rip), %rdi     # 0x4a004b
movq %r15, %rsi
xorl %eax, %eax
callq 0x411ef0
pushq $-0x1
popq %rax
jmp 0x401905
leaq 0x9e7c7(%rip), %rdi     # 0x4a009e
jmp 0x4018e0
leaq 0x9e781(%rip), %rdi     # 0x4a0061
xorl %eax, %eax
callq 0x411ef0
movq 0x20(%rsp), %rdi
callq 0x418fe0
movq %r15, %rdi
callq 0x418fe0
movq 0x8(%rsp), %rdi
callq 0x418fe0
xorl %eax, %eax
addq $0x4048, %rsp           # imm = 0x4048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rdx
pushq $0x1
popq %r13
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x419a60
movq %r15, %rdi
callq 0x41d5a0
leaq 0x9e6df(%rip), %rdi     # 0x4a001c
pushq $0x2
popq %rbp
movq %rbp, %rsi
movq %r13, %rdx
movq 0x8(%rsp), %rcx
callq 0x419a60
pushq $0x4
popq %rbx
leaq 0x14(%rsp), %r12
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x419810
cmpq $0x1, %rax
jne 0x4018e7
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x419810
cmpq $0x1, %rax
jne 0x4018e7
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x419810
cmpq $0x1, %rax
jne 0x4018e7
leaq 0x6(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x419810
cmpq $0x1, %rax
jne 0x4018e7
leaq 0x5(%rsp), %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x419810
cmpq $0x1, %rax
jne 0x4018e7
leaq 0x4(%rsp), %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x419810
cmpq $0x1, %rax
jne 0x4018e7
movq 0x38(%rsp), %r14
addq $0x2, %r14
xorl %ebx, %ebx
subq $0x1, %r14
jb 0x401a5c
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x419810
cmpq $0x1, %rax
jne 0x401a5c
movl 0x14(%rsp), %ecx
leal 0x1(%rbx), %eax
movslq %ebx, %rbx
cmpl $0xa, %ecx
jne 0x401a54
movl $0x5c, 0x40(%rsp,%rbx,4)
cltq
movl $0x6e, 0x40(%rsp,%rax,4)
addl $0x2, %ebx
jmp 0x401a11
movl %ecx, 0x40(%rsp,%rbx,4)
movl %eax, %ebx
jmp 0x401a11
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rcx
movzwl 0x6(%rsp), %r8d
movzbl 0x5(%rsp), %r9d
movzbl 0x4(%rsp), %r10d
movq 0x8(%rsp), %rdi
leaq 0x9e64c(%rip), %rsi     # 0x4a00d0
xorl %eax, %eax
leaq 0x40(%rsp), %r11
pushq %r11
pushq %r10
callq 0x41a050
popq %rcx
popq %rdx
pushq $0x4
popq %rbx
jmp 0x401958
nop