// File: /workspace/github_c_makefile_star2_O2/AMGarkin[P]BDOcrypt/BDO_encrypt.c
// Function: main
int main(int argc, char **argv)
{
	if (argc != 3)
	{
		printf("BDO_encrypt <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");


	///Convert .txt source file into temporary .bss file
	unsigned long strSize;
	unsigned long strType;
	unsigned long strID1;
	unsigned long strID2; //for some reason "short" doesn't work well with fwscanf()
	unsigned char strID3;
	unsigned char strID4;
	int a;
	wchar_t strBuff[MAX_BUFF_SIZE];

	//Handle BOM (if present, skip it)
	fread(&strBuff[0], 2, 1, srcFile);
	if (strBuff[0] != BOM_UTF16LE) {
		rewind(srcFile);
	}

	while (1){
		wmemset(strBuff, CHAR_NULL, MAX_BUFF_SIZE);

		if (fwscanf(srcFile, L"%u\t%u\t%u\t%u\t%u\t", &strType, &strID1, &strID2, &strID3, &strID4) < 5) break; //this pattern "eats" leading white space from next string, so I had to enclose strings in double quotes (in bss -> txt conversion)

		fseek(srcFile, 2, SEEK_CUR); //skip leading double quotes

		for (a = 0; a < MAX_BUFF_SIZE; a++) {
			fread(&strBuff[a], 2, 1, srcFile);
			if (a > 0) {
				if (strBuff[a] == CHAR_LF || strBuff[a] == CHAR_CR) {
					if (strBuff[a-1] == L'"') {
						strBuff[a-1] = CHAR_NULL;
					}
					strBuff[a] = CHAR_NULL;
					break;
				}
				if (strBuff[a] == L'n' && strBuff[a-1] == L'\\') {
					a--;
					strBuff[a] = CHAR_LF;
				}
			}
		}

		strSize = wcslen(strBuff);

		fwrite(&strSize, 4, 1, tmpFile);
		fwrite(&strType, 4, 1, tmpFile);
		fwrite(&strID1,  4, 1, tmpFile);
		fwrite(&strID2,  2, 1, tmpFile);
		fwrite(&strID3,  1, 1, tmpFile);
		fwrite(&strID4,  1, 1, tmpFile);
		fwrite(&strBuff, 2, strSize, tmpFile);
		fputwc(CHAR_NULL, tmpFile);
		fputwc(CHAR_NULL, tmpFile);
	}


	///Compress temporary .bss file to .loc
	unsigned long compressedSize = 0;
	unsigned long uncompressedSize = 0;

	fseek(tmpFile, 0, SEEK_END);
	uncompressedSize = ftell(tmpFile);
	rewind(tmpFile);

	compressedSize = compressBound(uncompressedSize);

	unsigned char *pCompressedData = (unsigned char *) calloc(compressedSize, sizeof(unsigned char));
	unsigned char *pUncompressedData = (unsigned char *) calloc(uncompressedSize, sizeof(unsigned char));

	fread(pUncompressedData, 1, uncompressedSize, tmpFile);

	int result = compress2(pCompressedData, &compressedSize, pUncompressedData, uncompressedSize, Z_BEST_SPEED);

	if (result == Z_OK) {
		fwrite(&uncompressedSize, 1, 4, outFile);
		fwrite(pCompressedData, 1, compressedSize, outFile);
	} else if (result == Z_MEM_ERROR) {
		printf("ERROR: Not enough memory.");
	} else if (result == Z_BUF_ERROR) {
		printf("ERROR: Output buffer is too small.");
	}

	fclose(srcFile);
	fclose(tmpFile);
	fclose(outFile);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4058, %rsp           # imm = 0x4058
cmpl $0x3, %edi
jne 0x401e1f
movq 0x8(%rsi), %r14
movq 0x10(%rsi), %r15
leaq 0xb63fb(%rip), %rsi     # 0x4b802c
movq %r14, %rdi
callq 0x41b960
testq %rax, %rax
je 0x401ef4
movq %rax, %rbx
callq 0x4142d0
movq %rax, 0x10(%rsp)
leaq 0xb63ec(%rip), %rsi     # 0x4b8042
movq %r15, %rdi
callq 0x41b960
movq %rax, 0x18(%rsp)
leaq 0x50(%rsp), %r15
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x41ba60
cmpl $0xfeff, (%r15)         # imm = 0xFEFF
je 0x401c8a
movq %rbx, %rdi
callq 0x41f970
leaq 0x50(%rsp), %r14
leaq 0x28(%rsp), %r12
leaq 0x40(%rsp), %r13
pushq $0x2
popq %rbp
pushq $0x1
popq %r15
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x401088
leaq 0xe(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0xb63c2(%rip), %rsi     # 0x4b8084
movq %r12, %rdx
leaq 0x48(%rsp), %rcx
movq %r13, %r8
leaq 0xf(%rsp), %r9
xorl %eax, %eax
callq 0x459570
cmpl $0x5, %eax
jl 0x401e34
movq %rbx, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rdx
callq 0x41f890
xorl %r13d, %r13d
cmpl $0xfff, %r13d           # imm = 0xFFF
ja 0x401d68
movl %r13d, %eax
leaq (%rsp,%rax,4), %r12
addq $0x50, %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x41ba60
testl %r13d, %r13d
je 0x401d32
movl (%r12), %eax
cmpl $0x6e, %eax
je 0x401d37
cmpl $0xd, %eax
je 0x401d52
cmpl $0xa, %eax
jne 0x401d4d
jmp 0x401d52
xorl %r13d, %r13d
jmp 0x401d4d
leal -0x1(%r13), %eax
cmpl $0x5c, 0x50(%rsp,%rax,4)
jne 0x401d4d
movl $0xa, 0x50(%rsp,%rax,4)
movl %eax, %r13d
incl %r13d
jmp 0x401cf3
decl %r13d
cmpl $0x22, 0x50(%rsp,%r13,4)
jne 0x401d63
andl $0x0, 0x50(%rsp,%r13,4)
andl $0x0, (%r12)
leaq 0x50(%rsp), %rdi
callq 0x401038
movq %rax, 0x30(%rsp)
leaq 0x30(%rsp), %rdi
pushq $0x4
popq %r14
movq %r14, %rsi
movq %r15, %rdx
movq 0x10(%rsp), %r12
movq %r12, %rcx
callq 0x41bcb0
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x41bcb0
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %r14
movq %r15, %rdx
movq %r12, %rcx
callq 0x41bcb0
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x41bcb0
movsbl 0xf(%rsp), %edi
movq %r12, %rsi
callq 0x41f750
movsbl 0xe(%rsp), %edi
movq %r12, %rsi
callq 0x41f750
movq 0x30(%rsp), %rdx
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rcx
callq 0x41bcb0
xorl %edi, %edi
movq %r12, %rsi
callq 0x41c2a0
xorl %edi, %edi
movq %r12, %rsi
leaq 0x28(%rsp), %r12
callq 0x41c2a0
jmp 0x401ca0
leaq 0xb61de(%rip), %rdi     # 0x4b8004
xorl %eax, %eax
callq 0x414140
pushq $0x1
jmp 0x401f07
pushq $0x2
popq %rdx
movq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x41f890
movq %r14, %rdi
callq 0x41bb60
movq %rax, %r12
movq %rax, 0x38(%rsp)
movq %r14, %rdi
callq 0x41f970
movq %r12, %rdi
callq 0x4020b0
leaq 0x20(%rsp), %rcx
movq %rax, (%rcx)
pushq $0x1
popq %rbp
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42c3a0
movq %rax, %r15
movq %r12, %rdi
movq %rbp, %rsi
callq 0x42c3a0
movq %rax, %r13
movq %rax, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x41ba60
pushq $0x1
popq %r8
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x401f60
cmpl $-0x5, %eax
je 0x401f1a
cmpl $-0x4, %eax
movq 0x18(%rsp), %r13
je 0x401f0a
testl %eax, %eax
jne 0x401f2d
leaq 0x38(%rsp), %rdi
pushq $0x1
popq %r12
pushq $0x4
popq %rdx
movq %r12, %rsi
movq %r13, %rcx
callq 0x41bcb0
movq 0x20(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rcx
callq 0x41bcb0
jmp 0x401f2d
leaq 0xb6134(%rip), %rdi     # 0x4b802f
movq %r14, %rsi
xorl %eax, %eax
callq 0x414140
pushq $-0x1
popq %rax
jmp 0x401f49
leaq 0xb6134(%rip), %rdi     # 0x4b8045
xorl %eax, %eax
callq 0x414140
jmp 0x401f2d
leaq 0xb613e(%rip), %rdi     # 0x4b805f
xorl %eax, %eax
callq 0x414140
movq 0x18(%rsp), %r13
movq %rbx, %rdi
callq 0x41b230
movq 0x10(%rsp), %rdi
callq 0x41b230
movq %r13, %rdi
callq 0x41b230
xorl %eax, %eax
addq $0x4058, %rsp           # imm = 0x4058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)