// File: /workspace/github_c_makefile_star2_O1/fritzw[P]cube-utils/cube-encoder.c
// Function: main
int main(int argc, char **argv) {
    char cubepro_key[] = "221BBakerMycroft";
    char cubex_key[] = "kWd$qG*25Xmgf-Sg";
    char *userkey = cubepro_key;
    char *extension = ".cubepro";
    BLOWFISH_KEY key;
    char *infilename, *outfilename;
    int i;
	char *executable;

    /* TODO:
	This whole code is a mess that was hacked together quickly. Clean it up!
    Implement flag to enable CubeX encoding?
    Implement decoding of .cubepro/.cubex files?
    Implement legacy mode for compatibility with CodeX:
    argv[0] [CUBEPRO | CUBEX] [ENCODE | DECODE | RECODE] inputfile outputfile
    */
    
	for (executable = argv[0]; *executable; ++executable) {
		*executable = tolower(*executable);
	}
	executable = argv[0];
    if (ends_with(executable, "cubex-encoder") ||
		ends_with(executable, "cubex-encoder.exe")) {
        userkey = cubex_key;
        extension = ".cubex";
    } else if (ends_with(executable, "cube3-encoder") ||
		ends_with(executable, "cube3-encoder.exe")) {
        extension = ".cube3";
    }

    // "Parse" arguments
    if (argc == 2) {
        infilename = argv[1];
        size_t inlength = strlen(infilename);
        outfilename = malloc(inlength + 10);
        if (outfilename == NULL) {
            perror("Unable to allocate memory for output file name");
            return 4;
        }
        strncpy(outfilename, infilename, inlength);
        if (strncmp(outfilename + inlength - 4, ".bfb", 4) == 0) {
            strcpy(outfilename + inlength - 4, extension);
        } else {
            strcpy(outfilename + inlength, extension);
        }
    } else if (argc == 3) {
        infilename = argv[1];
        outfilename = argv[2];
    } else {
        printf("Usage:\n"
               "    %s inputfile [outputfile]\n"
               "    Default outputfile is inputfile minus the .bfb extension plus %s\n",
               argv[0], extension);
        return 1;
    }


    // Load input file
    FILE *infile = fopen(infilename, "rb");
    if (infile == NULL) {
        perror("Unable to open input file");
        return 2;
    }
    size_t infilesize = fsize(infile);
    if (infilesize < 0) {
        perror("Unable to determine size of input file");
        return 3;
    }
    size_t outfilesize = ((infilesize + 8) / 8) * 8;
    BYTE *data = malloc(outfilesize);
    if (data == NULL) {
        perror("Unable to allocate memory for input file");
        return 4;
    }
    size_t readcount = fread(data, 1, infilesize, infile);
    if (readcount != infilesize) {
        printf("Unable to read the whole input file: %zd != %zd\n", readcount, infilesize);
        return 5;
    }
    fclose(infile);

    // Pad input data
    BYTE pad = outfilesize - infilesize;
    for (i = infilesize; i < outfilesize; i++) {
        data[i] = pad;
    }

    // Encrypt data
    blowfish_key_setup((BYTE*) userkey, &key, strlen(userkey));
    for (i = 0; i < outfilesize; i += 8) {
        blowfish_encrypt(&data[i], &data[i], &key, 1);
    }


    // Save data to output file
    FILE *outfile = fopen(outfilename, "wb+");
    if (outfile == NULL) {
        perror("Unable to open output file");
        return 6;
    }
    size_t writecount = fwrite(data, 1, outfilesize, outfile);
    if (writecount != outfilesize) {
        printf("Unable to write the whole output file: %zd != %zd\n", writecount, outfilesize);
        return 7;
    }
    fclose(outfile);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1088, %rsp           # imm = 0x1088
movq %rsi, %r14
movl %edi, %ebp
movaps 0x1d4f(%rip), %xmm0     # 0x3010
movaps %xmm0, (%rsp)
xorl %eax, %eax
movb %al, 0x10(%rsp)
movaps 0x1d5e(%rip), %xmm0     # 0x3030
movaps %xmm0, 0x20(%rsp)
movb %al, 0x30(%rsp)
movq (%rsi), %rbx
movb (%rbx), %r15b
testb %r15b, %r15b
je 0x1306
callq 0x1110
incq %rbx
movq (%rax), %rcx
movsbq %r15b, %rdx
movb (%rcx,%rdx,4), %cl
movb %cl, -0x1(%rbx)
movb (%rbx), %r15b
incq %rbx
testb %r15b, %r15b
jne 0x12ee
movq (%r14), %r12
leaq 0x1d3a(%rip), %rsi      # 0x304a
movq %r12, %rdi
callq 0x125c
leaq 0x20(%rsp), %rbx
leaq 0x1d46(%rip), %r15      # 0x306a
testl %eax, %eax
jne 0x136f
leaq 0x1d29(%rip), %rsi      # 0x3058
movq %r12, %rdi
callq 0x125c
testl %eax, %eax
jne 0x136f
leaq 0x1d2f(%rip), %rsi      # 0x3071
movq %r12, %rdi
callq 0x125c
testl %eax, %eax
jne 0x1365
leaq 0x1d2a(%rip), %rsi      # 0x307f
movq %r12, %rdi
callq 0x125c
testl %eax, %eax
je 0x1574
movq %rsp, %rbx
leaq 0x1d22(%rip), %r15      # 0x3091
cmpl $0x3, %ebp
je 0x13d3
cmpl $0x2, %ebp
jne 0x13dd
movq 0x8(%r14), %r12
movq %r12, %rdi
callq 0x1080
movq %rax, %r13
leaq 0xa(%rax), %rdi
callq 0x10c0
movq %rax, %r14
testq %rax, %rax
je 0x1583
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1030
leaq (%r14,%r13), %rbp
addq $-0x4, %rbp
leaq 0x1d0d(%rip), %rsi      # 0x30c7
movl $0x4, %edx
movq %rbp, %rdi
callq 0x1040
testl %eax, %eax
je 0x13fb
addq %r14, %r13
movq %r13, %rdi
jmp 0x13fe
movq 0x8(%r14), %r12
movq 0x10(%r14), %r14
jmp 0x1411
leaq 0x1ce8(%rip), %rdi      # 0x30cc
movq %r12, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x1, %eax
jmp 0x15aa
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1050
xorl %eax, %eax
testq %r14, %r14
je 0x15aa
leaq 0x1d1f(%rip), %rsi      # 0x3137
movq %r12, %rdi
callq 0x10e0
testq %rax, %rax
je 0x154e
movq %rax, %rbp
movq %rax, %rdi
callq 0x121c
movq %rax, %r13
movq %rax, %r15
andq $-0x8, %r15
addq $0x8, %r15
movq %r15, %rdi
callq 0x10c0
testq %rax, %rax
je 0x1561
movq %rax, %r12
movl $0x1, %esi
movq %rax, %rdi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x1060
cmpq %r13, %rax
jne 0x1518
movq %rbp, %rdi
callq 0x1070
movslq %r13d, %rax
cmpq %rax, %r15
jbe 0x1494
movl %r15d, %ecx
subl %r13d, %ecx
movb %cl, (%r12,%rax)
incq %rax
cmpq %rax, %r15
ja 0x1488
movq %rbx, %rdi
callq 0x1080
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x20c0
testq %r15, %r15
je 0x14d6
xorl %r13d, %r13d
leaq 0x40(%rsp), %rbx
leaq (%r12,%r13), %rdi
movq %rdi, %rsi
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x15bc
addq $0x8, %r13
cmpq %r13, %r15
ja 0x14b9
leaq 0x1cd1(%rip), %rsi      # 0x31ae
movq %r14, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1599
movq %rax, %rbx
movl $0x1, %esi
movq %r12, %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x1100
cmpq %r15, %rax
jne 0x1533
movq %rbx, %rdi
callq 0x1070
xorl %eax, %eax
jmp 0x15aa
leaq 0x1c5e(%rip), %rdi      # 0x317d
movq %rax, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x5, %eax
jmp 0x15aa
leaq 0x1c93(%rip), %rdi      # 0x31cd
movq %rax, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x7, %eax
jmp 0x15aa
leaq 0x1be5(%rip), %rdi      # 0x313a
callq 0x10f0
movl $0x2, %eax
jmp 0x15aa
leaq 0x1bec(%rip), %rdi      # 0x3154
callq 0x10f0
movl $0x4, %eax
jmp 0x15aa
movq %rsp, %rbx
leaq 0x1ac3(%rip), %r15      # 0x3041
jmp 0x136f
leaq 0x1b0e(%rip), %rdi      # 0x3098
callq 0x10f0
movl $0x4, %eax
jmp 0x1408
leaq 0x1c12(%rip), %rdi      # 0x31b2
callq 0x10f0
movl $0x6, %eax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq