// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x4090, %rsp           # imm = 0x4090
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x401c19
leaq 0xb63fe(%rip), %rdi     # 0x4b8004
movb $0x0, %al
callq 0x414210
movl $0x1, -0x4(%rbp)
jmp 0x402022
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
leaq 0xb63f0(%rip), %rsi     # 0x4b802c
callq 0x41ba30
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x401c6a
movq -0x18(%rbp), %rsi
leaq 0xb63d8(%rip), %rdi     # 0x4b802f
movb $0x0, %al
callq 0x414210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x402022
callq 0x4143a0
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
leaq 0xb63c4(%rip), %rsi     # 0x4b8042
callq 0x41ba30
movq %rax, -0x38(%rbp)
leaq -0x4060(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x2, %esi
movl $0x1, %edx
callq 0x41bb30
cmpl $0xfeff, -0x4060(%rbp)  # imm = 0xFEFF
je 0x401cb6
movq -0x28(%rbp), %rdi
callq 0x41f900
jmp 0x401cb8
leaq -0x4060(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x401088
movq -0x28(%rbp), %rdi
leaq 0xb63ae(%rip), %rsi     # 0x4b8084
leaq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rcx
leaq -0x58(%rbp), %r8
leaq -0x59(%rbp), %r9
leaq -0x5a(%rbp), %rax
movq %rax, (%rsp)
movb $0x0, %al
callq 0x4594f0
cmpl $0x5, %eax
jge 0x401cff
jmp 0x401ebe
movq -0x28(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x41f820
movl $0x0, -0x60(%rbp)
movslq -0x60(%rbp), %rax
cmpq $0x1000, %rax           # imm = 0x1000
jae 0x401df4
movslq -0x60(%rbp), %rax
leaq -0x4060(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rcx
movl $0x2, %esi
movl $0x1, %edx
callq 0x41bb30
cmpl $0x0, -0x60(%rbp)
jle 0x401de4
movslq -0x60(%rbp), %rax
cmpl $0xa, -0x4060(%rbp,%rax,4)
je 0x401d74
movslq -0x60(%rbp), %rax
cmpl $0xd, -0x4060(%rbp,%rax,4)
jne 0x401daa
movl -0x60(%rbp), %eax
subl $0x1, %eax
cltq
cmpl $0x22, -0x4060(%rbp,%rax,4)
jne 0x401d99
movl -0x60(%rbp), %eax
subl $0x1, %eax
cltq
movl $0x0, -0x4060(%rbp,%rax,4)
movslq -0x60(%rbp), %rax
movl $0x0, -0x4060(%rbp,%rax,4)
jmp 0x401df4
movslq -0x60(%rbp), %rax
cmpl $0x6e, -0x4060(%rbp,%rax,4)
jne 0x401de2
movl -0x60(%rbp), %eax
subl $0x1, %eax
cltq
cmpl $0x5c, -0x4060(%rbp,%rax,4)
jne 0x401de2
movl -0x60(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x60(%rbp)
movslq -0x60(%rbp), %rax
movl $0xa, -0x4060(%rbp,%rax,4)
jmp 0x401de4
jmp 0x401de6
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x401d19
leaq -0x4060(%rbp), %rdi
callq 0x401038
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x41bd80
movq -0x30(%rbp), %rcx
leaq -0x48(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x41bd80
movq -0x30(%rbp), %rcx
leaq -0x50(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x41bd80
movq -0x30(%rbp), %rcx
leaq -0x58(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x41bd80
movq -0x30(%rbp), %rcx
leaq -0x59(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x41bd80
movq -0x30(%rbp), %rcx
leaq -0x5a(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x41bd80
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x4060(%rbp), %rdi
movl $0x2, %esi
callq 0x41bd80
movq -0x30(%rbp), %rsi
xorl %edi, %edi
callq 0x41c370
movq -0x30(%rbp), %rsi
xorl %edi, %edi
callq 0x41c370
jmp 0x401cb8
movq $0x0, -0x4068(%rbp)
movq $0x0, -0x4070(%rbp)
movq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x41f820
movq -0x30(%rbp), %rdi
callq 0x41bc30
movq %rax, -0x4070(%rbp)
movq -0x30(%rbp), %rdi
callq 0x41f900
movq -0x4070(%rbp), %rdi
callq 0x402180
movq %rax, -0x4068(%rbp)
movq -0x4068(%rbp), %rdi
movl $0x1, %esi
callq 0x42c330
movq %rax, -0x4078(%rbp)
movq -0x4070(%rbp), %rdi
movl $0x1, %esi
callq 0x42c330
movq %rax, -0x4080(%rbp)
movq -0x4080(%rbp), %rdi
movq -0x4070(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl $0x1, %esi
callq 0x41bb30
movq -0x4078(%rbp), %rdi
movq -0x4080(%rbp), %rdx
movq -0x4070(%rbp), %rcx
leaq -0x4068(%rbp), %rsi
movl $0x1, %r8d
callq 0x402030
movl %eax, -0x4084(%rbp)
cmpl $0x0, -0x4084(%rbp)
jne 0x401fcc
movq -0x38(%rbp), %rcx
leaq -0x4070(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x41bd80
movq -0x4078(%rbp), %rdi
movq -0x4068(%rbp), %rdx
movq -0x38(%rbp), %rcx
movl $0x1, %esi
callq 0x41bd80
jmp 0x402000
cmpl $-0x4, -0x4084(%rbp)
jne 0x401fe5
leaq 0xb6069(%rip), %rdi     # 0x4b8045
movb $0x0, %al
callq 0x414210
jmp 0x401ffe
cmpl $-0x5, -0x4084(%rbp)
jne 0x401ffc
leaq 0xb606a(%rip), %rdi     # 0x4b805f
movb $0x0, %al
callq 0x414210
jmp 0x401ffe
jmp 0x402000
movq -0x28(%rbp), %rdi
callq 0x41b300
movq -0x30(%rbp), %rdi
callq 0x41b300
movq -0x38(%rbp), %rdi
callq 0x41b300
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x4090, %rsp           # imm = 0x4090
popq %rbp
retq
nop