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

    /* TODO:
    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
    */

    // "Parse" arguments
    if (argc == 4 && strcmp("-x", argv[1]) == 0) {
        userkey = cubex_key;
        infilename = argv[2];
        outfilename = argv[3];
    } else if (argc == 3) {
        infilename = argv[1];
        outfilename = argv[2];
    } else {
        printf("Usage:\n"
               "    %s [-x] inputfile outputfile\n"
               "    Normally, the inputfile will be decoded as a .cubepro file.\n"
               "    If the file name ends in .cubex or the -x parameter is present,i\n"
               "    it will be decoded as a .cubex file instead.\n",
               argv[0]);
        return 1;
    }

    if (strcmp(infilename + strlen(infilename) - 6, ".cubex") == 0) {
        userkey = cubex_key;
    }

    // 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;
    }
    BYTE *data = malloc(infilesize);
    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);

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

    // Remove padding
    BYTE pad = data[infilesize - 1];
    if (pad > 8) {
        printf("Decoding error: Invalid padding. Make sure that this is a valid encoded file.\n");
        //return 8;
    }
    outfilesize = infilesize - pad;

    // 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, %rbx
movaps 0x1db9(%rip), %xmm0     # 0x3010
leaq 0x20(%rsp), %r13
movaps %xmm0, (%r13)
xorl %eax, %eax
movb %al, 0x10(%r13)
movaps 0x1dc2(%rip), %xmm0     # 0x3030
movaps %xmm0, (%rsp)
movb %al, 0x10(%rsp)
movl $0x8, %r14d
cmpl $0x3, %edi
je 0x12c1
movl %edi, %ebp
cmpl $0x4, %edi
jne 0x12a6
movq 0x8(%rbx), %rsi
leaq 0x1dae(%rip), %rdi      # 0x3041
callq 0x1080
testl %eax, %eax
je 0x12c8
movl $0x10, %eax
cmpl $0x3, %ebp
je 0x12d6
movq (%rbx), %rsi
leaq 0x1d94(%rip), %rdi      # 0x3044
xorl %eax, %eax
callq 0x1070
movl $0x1, %eax
jmp 0x146a
movl $0x10, %eax
jmp 0x12d6
movq %rsp, %r13
movl $0x18, %eax
movl $0x10, %r14d
movq (%rbx,%r14), %r15
movq (%rbx,%rax), %r14
movq %r15, %rdi
callq 0x1060
leaq (%r15,%rax), %rdi
addq $-0x6, %rdi
leaq 0x1e2e(%rip), %rsi      # 0x3123
callq 0x1080
testl %eax, %eax
movq %rsp, %r12
cmovneq %r13, %r12
leaq 0x1e20(%rip), %rsi      # 0x312a
movq %r15, %rdi
callq 0x10d0
testq %rax, %rax
je 0x1433
movq %rax, %r13
movq %rax, %rdi
callq 0x11fc
movq %rax, %rbx
movq %rax, %rdi
callq 0x10b0
testq %rax, %rax
je 0x1446
movq %rax, %r15
movl $0x1, %esi
movq %rax, %rdi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x1040
cmpq %rbx, %rax
jne 0x13fd
movq %r13, %rdi
callq 0x1050
movq %r12, %rdi
callq 0x1060
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
movq %rax, %rdx
callq 0x1f80
testq %rbx, %rbx
je 0x13a3
xorl %r13d, %r13d
leaq 0x40(%rsp), %r12
leaq (%r15,%r13), %rdi
movq %rdi, %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x19fe
addq $0x8, %r13
cmpq %r13, %rbx
ja 0x1386
movzbl -0x1(%r15,%rbx), %r12d
cmpq $0x9, %r12
jb 0x13bb
leaq 0x1e3d(%rip), %rdi      # 0x31f3
callq 0x1030
leaq 0x1ddf(%rip), %rsi      # 0x31a1
movq %r14, %rdi
callq 0x10d0
testq %rax, %rax
je 0x1459
movq %rax, %r14
subq %r12, %rbx
movl $0x1, %esi
movq %r15, %rdi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x10f0
cmpq %rbx, %rax
jne 0x1418
movq %r14, %rdi
callq 0x1050
xorl %eax, %eax
jmp 0x146a
leaq 0x1d6c(%rip), %rdi      # 0x3170
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1070
movl $0x5, %eax
jmp 0x146a
leaq 0x1da1(%rip), %rdi      # 0x31c0
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1070
movl $0x7, %eax
jmp 0x146a
leaq 0x1cf3(%rip), %rdi      # 0x312d
callq 0x10e0
movl $0x2, %eax
jmp 0x146a
leaq 0x1cfa(%rip), %rdi      # 0x3147
callq 0x10e0
movl $0x4, %eax
jmp 0x146a
leaq 0x1d45(%rip), %rdi      # 0x31a5
callq 0x10e0
movl $0x6, %eax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq