// File: /workspace/github_c_makefile_star2_O2/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 0x1d51(%rip), %xmm0     # 0x3010
movaps %xmm0, (%rsp)
xorl %eax, %eax
movb %al, 0x10(%rsp)
movaps 0x1d60(%rip), %xmm0     # 0x3030
movaps %xmm0, 0x20(%rsp)
movb %al, 0x30(%rsp)
movq (%rsi), %rbx
movsbq (%rbx), %r15
testq %r15, %r15
je 0x12f8
callq 0x1110
movq (%rax), %rax
movb (%rax,%r15,4), %al
movb %al, (%rbx)
incq %rbx
jmp 0x12dc
movq (%r14), %r12
leaq 0x1d48(%rip), %rsi      # 0x304a
movq %r12, %rdi
callq 0x125a
leaq 0x20(%rsp), %rbx
leaq 0x1d54(%rip), %r15      # 0x306a
testl %eax, %eax
jne 0x1361
leaq 0x1d37(%rip), %rsi      # 0x3058
movq %r12, %rdi
callq 0x125a
testl %eax, %eax
jne 0x1361
leaq 0x1d3d(%rip), %rsi      # 0x3071
movq %r12, %rdi
callq 0x125a
testl %eax, %eax
jne 0x1357
leaq 0x1d38(%rip), %rsi      # 0x307f
movq %r12, %rdi
callq 0x125a
testl %eax, %eax
je 0x152e
movq %rsp, %rbx
leaq 0x1d30(%rip), %r15      # 0x3091
cmpl $0x3, %ebp
je 0x13c3
cmpl $0x2, %ebp
jne 0x13cd
movq 0x8(%r14), %r12
movq %r12, %rdi
callq 0x1080
movq %rax, %r13
leaq 0xa(%rax), %rdi
callq 0x10c0
testq %rax, %rax
je 0x153d
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1030
leaq (%r14,%r13), %rbp
addq $-0x4, %rbp
leaq 0x1d1b(%rip), %rsi      # 0x30c7
pushq $0x4
popq %rdx
movq %rbp, %rdi
callq 0x1040
testl %eax, %eax
je 0x13e8
movq %r14, %rdi
addq %r13, %rdi
jmp 0x13eb
movq 0x8(%r14), %r12
movq 0x10(%r14), %r14
jmp 0x13f3
leaq 0x1cf8(%rip), %rdi      # 0x30cc
movq %r12, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1090
pushq $0x1
jmp 0x155b
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1050
leaq 0x1d3d(%rip), %rsi      # 0x3137
movq %r12, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1515
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 0x1525
movq %rax, %r12
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x1060
cmpq %r13, %rax
jne 0x146d
movq %rbp, %rdi
callq 0x1070
movl %r15d, %eax
subl %r13d, %eax
movslq %r13d, %rcx
cmpq %rcx, %r15
jbe 0x1488
movb %al, (%r12,%rcx)
incq %rcx
jmp 0x145f
leaq 0x1d09(%rip), %rdi      # 0x317d
movq %rax, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1090
pushq $0x5
jmp 0x155b
movq %rbx, %rdi
callq 0x1080
leaq 0x40(%rsp), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x1f36
xorl %ebp, %ebp
pushq $0x1
popq %rbx
cmpq %rbp, %r15
jbe 0x14c4
leaq (%r12,%rbp), %rdi
movq %rdi, %rsi
movq %r13, %rdx
movl %ebx, %ecx
callq 0x1570
addq $0x8, %rbp
jmp 0x14a8
leaq 0x1ce3(%rip), %rsi      # 0x31ae
movq %r14, %rdi
callq 0x10e0
testq %rax, %rax
je 0x154d
movq %rax, %rbx
pushq $0x1
popq %rsi
movq %r12, %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x1100
cmpq %r15, %rax
jne 0x14fd
movq %rbx, %rdi
callq 0x1070
xorl %eax, %eax
jmp 0x155c
leaq 0x1cc9(%rip), %rdi      # 0x31cd
movq %rax, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1090
pushq $0x7
jmp 0x155b
leaq 0x1c1e(%rip), %rdi      # 0x313a
callq 0x10f0
pushq $0x2
jmp 0x155b
leaq 0x1c28(%rip), %rdi      # 0x3154
jmp 0x1544
movq %rsp, %rbx
leaq 0x1b09(%rip), %r15      # 0x3041
jmp 0x1361
leaq 0x1b54(%rip), %rdi      # 0x3098
callq 0x10f0
pushq $0x4
jmp 0x155b
leaq 0x1c5e(%rip), %rdi      # 0x31b2
callq 0x10f0
pushq $0x6
popq %rax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop