// File: /workspace/github_c_makefile_star2_O3/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 0x401f27
movq 0x8(%rsi), %r14
movq 0x10(%rsi), %r15
leaq 0xb63fb(%rip), %rsi     # 0x4b802c
movq %r14, %rdi
callq 0x41b9b0
testq %rax, %rax
je 0x401f3c
movq %rax, %rbx
callq 0x414320
movq %rax, %r14
leaq 0xb63ee(%rip), %rsi     # 0x4b8042
movq %r15, %rdi
callq 0x41b9b0
movq %rax, 0x18(%rsp)
leaq 0x50(%rsp), %r15
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x41bab0
cmpl $0xfeff, (%r15)         # imm = 0xFEFF
je 0x401c8c
movq %rbx, %rdi
callq 0x41f9c0
leaq 0x50(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x401088
leaq 0x16(%rsp), %rax
movq %rax, (%rsp)
leaq 0xb63d7(%rip), %rsi     # 0x4b8084
leaq 0x48(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x17(%rsp), %r9
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4595b0
cmpl $0x5, %eax
jl 0x401e64
leaq 0x50(%rsp), %r12
leaq 0x38(%rsp), %r13
movl $0x2, %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x41f8e0
xorl %r15d, %r15d
movl %r15d, %eax
leaq (%rsp,%rax,4), %rbp
addq $0x50, %rbp
movl $0x2, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %rbx, %rcx
callq 0x41bab0
testl %r15d, %r15d
je 0x401d2c
movl (%rbp), %eax
cmpl $0x6e, %eax
je 0x401d31
cmpl $0xd, %eax
je 0x401d59
cmpl $0xa, %eax
jne 0x401d47
jmp 0x401d59
xorl %r15d, %r15d
jmp 0x401d47
leal -0x1(%r15), %eax
cmpl $0x5c, 0x50(%rsp,%rax,4)
jne 0x401d47
movl $0xa, 0x50(%rsp,%rax,4)
movl %eax, %r15d
leal 0x1(%r15), %eax
cmpl $0xfff, %r15d           # imm = 0xFFF
movl %eax, %r15d
jb 0x401cf3
jmp 0x401d74
decl %r15d
cmpl $0x22, 0x50(%rsp,%r15,4)
jne 0x401d6d
movl $0x0, 0x50(%rsp,%r15,4)
movl $0x0, (%rbp)
movq %r12, %rdi
callq 0x401038
movq %rax, 0x20(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x20(%rsp), %rdi
movq %r14, %rcx
callq 0x41bd00
movl $0x4, %esi
movl $0x1, %edx
leaq 0x48(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
callq 0x41bd00
movl $0x4, %esi
movl $0x1, %edx
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rcx
callq 0x41bd00
movl $0x2, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x41bd00
movsbl 0x17(%rsp), %edi
movq %r14, %rsi
callq 0x41f7a0
movsbl 0x16(%rsp), %edi
movq %r14, %rsi
callq 0x41f7a0
movq 0x20(%rsp), %rdx
movl $0x2, %esi
movq %r12, %rdi
movq %r14, %rcx
callq 0x41bd00
xorl %edi, %edi
movq %r14, %rsi
callq 0x41c2f0
xorl %edi, %edi
movq %r14, %rsi
callq 0x41c2f0
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x401088
leaq 0x16(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0xb623e(%rip), %rsi     # 0x4b8084
movq %r15, %rdx
movq %rbp, %rcx
movq %r13, %r8
leaq 0x17(%rsp), %r9
xorl %eax, %eax
callq 0x4595b0
cmpl $0x4, %eax
jg 0x401cde
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x41f8e0
movq %r14, %rdi
callq 0x41bbb0
movq %rax, %r12
movq %rax, 0x28(%rsp)
movq %r14, %rdi
callq 0x41f9c0
movq %r12, %rdi
callq 0x402100
leaq 0x30(%rsp), %r13
movq %rax, (%r13)
movl $0x1, %esi
movq %rax, %rdi
callq 0x42c3f0
movq %rax, %r15
movl $0x1, %esi
movq %r12, %rdi
callq 0x42c3f0
movq %rax, %rbp
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x41bab0
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movl $0x1, %r8d
callq 0x401fb0
cmpl $-0x5, %eax
je 0x401f64
cmpl $-0x4, %eax
movq 0x18(%rsp), %r12
je 0x401f54
testl %eax, %eax
jne 0x401f77
leaq 0x28(%rsp), %rdi
movl $0x1, %esi
movl $0x4, %edx
movq %r12, %rcx
callq 0x41bd00
movq 0x30(%rsp), %rdx
movl $0x1, %esi
movq %r15, %rdi
movq %r12, %rcx
callq 0x41bd00
jmp 0x401f77
leaq 0xb60d6(%rip), %rdi     # 0x4b8004
xorl %eax, %eax
callq 0x414190
movl $0x1, %eax
jmp 0x401f91
leaq 0xb60ec(%rip), %rdi     # 0x4b802f
movq %r14, %rsi
xorl %eax, %eax
callq 0x414190
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x401f91
leaq 0xb60ea(%rip), %rdi     # 0x4b8045
xorl %eax, %eax
callq 0x414190
jmp 0x401f77
leaq 0xb60f4(%rip), %rdi     # 0x4b805f
xorl %eax, %eax
callq 0x414190
movq 0x18(%rsp), %r12
movq %rbx, %rdi
callq 0x41b280
movq %r14, %rdi
callq 0x41b280
movq %r12, %rdi
callq 0x41b280
xorl %eax, %eax
addq $0x4058, %rsp           # imm = 0x4058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)