// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1080, %rsp           # imm = 0x1080
movaps 0xdbd(%rip), %xmm0      # 0x2010
movaps %xmm0, 0x20(%rsp)
xorl %eax, %eax
movb %al, 0x30(%rsp)
movaps 0xdcb(%rip), %xmm0      # 0x2030
movaps %xmm0, (%rsp)
movb %al, 0x10(%rsp)
cmpl $0x3, %edi
je 0x12b1
cmpl $0x4, %edi
jne 0x1296
movq 0x8(%rsi), %rcx
movzbl (%rcx), %eax
subl $0x2d, %eax
jne 0x1290
movzbl 0x1(%rcx), %eax
subl $0x78, %eax
jne 0x1290
movzbl 0x2(%rcx), %eax
negl %eax
testl %eax, %eax
je 0x12c2
movq (%rsi), %rsi
leaq 0xda1(%rip), %rdi       # 0x2041
xorl %eax, %eax
callq 0x1070
movl $0x1, %eax
jmp 0x1463
leaq 0x20(%rsp), %r15
movl $0x10, %eax
movl $0x8, %ecx
jmp 0x12cf
movq %rsp, %r15
movl $0x18, %eax
movl $0x10, %ecx
movq (%rsi,%rcx), %rbx
movq (%rsi,%rax), %r14
movq %rbx, %rdi
callq 0x1060
leaq (%rbx,%rax), %rdi
addq $-0x6, %rdi
leaq 0xe32(%rip), %rsi       # 0x2120
callq 0x1080
testl %eax, %eax
movq %rsp, %r12
cmovneq %r15, %r12
leaq 0xe24(%rip), %rsi       # 0x2127
movq %rbx, %rdi
callq 0x10d0
testq %rax, %rax
je 0x142c
movq %rax, %r13
movq %rax, %rdi
callq 0x11fc
movq %rax, %rbx
movq %rax, %rdi
callq 0x10b0
testq %rax, %rax
je 0x143f
movq %rax, %r15
movl $0x1, %esi
movq %rax, %rdi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x1040
cmpq %rbx, %rax
jne 0x13f6
movq %r13, %rdi
callq 0x1050
movq %r12, %rdi
callq 0x1060
leaq 0x38(%rsp), %rsi
movq %r12, %rdi
movq %rax, %rdx
callq 0x1e3a
testq %rbx, %rbx
je 0x139c
xorl %r13d, %r13d
leaq 0x38(%rsp), %r12
leaq (%r15,%r13), %rdi
movq %rdi, %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x1957
addq $0x8, %r13
cmpq %r13, %rbx
ja 0x137f
movzbl -0x1(%r15,%rbx), %r12d
cmpq $0x9, %r12
jb 0x13b4
leaq 0xe41(%rip), %rdi       # 0x21f0
callq 0x1030
leaq 0xde3(%rip), %rsi       # 0x219e
movq %r14, %rdi
callq 0x10d0
testq %rax, %rax
je 0x1452
movq %rax, %r14
subq %r12, %rbx
movl $0x1, %esi
movq %r15, %rdi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x10f0
cmpq %rbx, %rax
jne 0x1411
movq %r14, %rdi
callq 0x1050
xorl %eax, %eax
jmp 0x1463
leaq 0xd70(%rip), %rdi       # 0x216d
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1070
movl $0x5, %eax
jmp 0x1463
leaq 0xda5(%rip), %rdi       # 0x21bd
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1070
movl $0x7, %eax
jmp 0x1463
leaq 0xcf7(%rip), %rdi       # 0x212a
callq 0x10e0
movl $0x2, %eax
jmp 0x1463
leaq 0xcfe(%rip), %rdi       # 0x2144
callq 0x10e0
movl $0x4, %eax
jmp 0x1463
leaq 0xd49(%rip), %rdi       # 0x21a2
callq 0x10e0
movl $0x6, %eax
addq $0x1080, %rsp           # imm = 0x1080
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq