// File: /workspace/github_c_makefile_star2_O2/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
movaps 0xdbe(%rip), %xmm0      # 0x2010
movaps %xmm0, 0x20(%rsp)
xorl %eax, %eax
movb %al, 0x30(%rsp)
movaps 0xdcc(%rip), %xmm0      # 0x2030
movaps %xmm0, (%rsp)
movb %al, 0x10(%rsp)
cmpl $0x3, %edi
je 0x12ad
cmpl $0x4, %edi
jne 0x1295
movq 0x8(%rsi), %rcx
movzbl (%rcx), %eax
subl $0x2d, %eax
jne 0x128f
movzbl 0x1(%rcx), %eax
subl $0x78, %eax
jne 0x128f
movzbl 0x2(%rcx), %eax
negl %eax
testl %eax, %eax
je 0x12b9
movq (%rsi), %rsi
leaq 0xda2(%rip), %rdi       # 0x2041
xorl %eax, %eax
callq 0x1070
pushq $0x1
jmp 0x1439
leaq 0x20(%rsp), %r15
pushq $0x10
popq %rax
pushq $0x8
jmp 0x12c1
movq %rsp, %r15
pushq $0x18
popq %rax
pushq $0x10
popq %rcx
movq (%rsi,%rcx), %rbx
movq (%rsi,%rax), %r14
movq %rbx, %rdi
callq 0x1060
leaq (%rbx,%rax), %rdi
addq $-0x6, %rdi
leaq 0xe3f(%rip), %rsi       # 0x2120
callq 0x1080
testl %eax, %eax
movq %rsp, %r12
cmovneq %r15, %r12
leaq 0xe31(%rip), %rsi       # 0x2127
movq %rbx, %rdi
callq 0x10d0
testq %rax, %rax
je 0x140b
movq %rax, %r13
movq %rax, %rdi
callq 0x11fc
movq %rax, %rbx
movq %rax, %rdi
callq 0x10b0
testq %rax, %rax
je 0x141b
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x1040
cmpq %rbx, %rax
jne 0x1384
movq %r13, %rdi
callq 0x1050
movq %r12, %rdi
callq 0x1060
leaq 0x40(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x1e12
xorl %r12d, %r12d
pushq $0x1
popq %rbp
cmpq %r12, %rbx
jbe 0x139f
leaq (%r15,%r12), %rdi
movq %rdi, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x192f
addq $0x8, %r12
jmp 0x1368
leaq 0xde2(%rip), %rdi       # 0x216d
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1070
pushq $0x5
jmp 0x1439
movzbl -0x1(%r15,%rbx), %r12d
cmpq $0x9, %r12
jb 0x13b7
leaq 0xe3e(%rip), %rdi       # 0x21f0
callq 0x1030
leaq 0xde0(%rip), %rsi       # 0x219e
movq %r14, %rdi
callq 0x10d0
testq %rax, %rax
je 0x142b
movq %rax, %r14
subq %r12, %rbx
pushq $0x1
popq %rsi
movq %r15, %rdi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x10f0
cmpq %rbx, %rax
jne 0x13f3
movq %r14, %rdi
callq 0x1050
xorl %eax, %eax
jmp 0x143a
leaq 0xdc3(%rip), %rdi       # 0x21bd
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1070
pushq $0x7
jmp 0x1439
leaq 0xd18(%rip), %rdi       # 0x212a
callq 0x10e0
pushq $0x2
jmp 0x1439
leaq 0xd22(%rip), %rdi       # 0x2144
callq 0x10e0
pushq $0x4
jmp 0x1439
leaq 0xd70(%rip), %rdi       # 0x21a2
callq 0x10e0
pushq $0x6
popq %rax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq