// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/mime.c
// Function: MIME_decode_raw
int MIME_decode_raw( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo, int keep )
{
    int result = 0;
    char fullpath[1024];
    int bufsize=1024;
    char *buffer = malloc((bufsize +1)*sizeof(char));
    size_t readcount;
    int file_has_uuencode = 0;
    int decode_entire_file = 0;
    int fo;

    /* Decoding / reading a binary attachment is a real interesting situation, as we
     * still use the fgets() call, but we do so repeatedly until it returns a line with a
     * \n\r and the boundary specifier in it.... all in all, I wouldn't rate this code
     * as being perfect, it's activated only if the user intentionally specifies it with
     * --binary flag
     */

    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Start\n",FL);

    snprintf(fullpath,sizeof(fullpath),"%s/%s",unpackdir,hinfo->filename);
    fo = open(fullpath, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);

    if (fo == -1)
    {
        LOGGER_log("%s:%d:MIME_decode_raw:ERROR: cannot open file %s for writing. (%s)\n\n",FL,fullpath,strerror(errno));
        return -1;
    }

    while ((readcount=FFGET_raw(f, (unsigned char *) buffer,bufsize)) > 0)
    {
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: BUFFER[%p]= '%s'\n",FL,buffer, buffer);

        if ((!file_has_uuencode)&&(UUENCODE_is_uuencode_header( buffer )))
        {
            if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: UUENCODED is YES (buffer=[%p]\n",FL,buffer);

            if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: File contains UUENCODED data(%s)\n",FL,buffer);

            file_has_uuencode = 1;
        }

        if (BS_cmp(buffer, readcount))
        {
            if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Boundary located - breaking out.\n",FL);
            break;
        } else {
            size_t bc;
            if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: writing: %s\n",FL, buffer);
            bc = write( fo, buffer, readcount);
        }
    }

    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Completed reading RAW data\n",FL);
    free(buffer);
    close(fo);
    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Closed file and free'd buffer\n",FL);
    // If there was UUEncoded portions [potentially] in the email, the
    // try to extract them using the MIME_decode_uu()
    if (file_has_uuencode)
    {
        char full_decode_path[512];
        snprintf(full_decode_path,sizeof(full_decode_path),"%s/%s",unpackdir,hinfo->filename);
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Decoding UUencoded data\n",FL);
        if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0;

        //result = UUENCODE_decode_uu(NULL, unpackdir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep );
        result = UUENCODE_decode_uu(NULL, unpackdir, full_decode_path, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep );
        if (result == -1)
        {
            switch (uuencode_error) {
                case UUENCODE_STATUS_SHORT_FILE:
                case UUENCODE_STATUS_CANNOT_OPEN_FILE:
                case UUENCODE_STATUS_CANNOT_FIND_FILENAME:
                    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Nullifying uuencode_error result %d",FL, uuencode_error);
                    result = 0;
                    break;

                case UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY:
                    LOGGER_log("%s:%d:MIME_decode_raw:ERROR: Failure to allocate memory for UUdecode process",FL);
                    return -1;
                    break;

                default:
                    LOGGER_log("%s:%d:MIME_decode_raw:ERROR: Unknown return code from UUDecode process [%d]",FL,uuencode_error);
                    return -1;
            }
        }
        if (result == UUENCODE_STATUS_SHORT_FILE) result = 0;
        glb.attachment_count += result;
        if (strlen(hinfo->uudec_name))
        {
            if (strcasecmp(hinfo->uudec_name,"winmail.dat")==0)
            {
                if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Decoding TNEF format\n",FL);
                snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name);
                MIME_decode_TNEF( unpackdir, hinfo, keep);
            }
            else LOGGER_log("%s:%d:MIME_decode_raw:WARNING: hinfo has been clobbered.\n",FL);
        }
    }

    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: End[result = %d]\n",FL,result);
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x638, %rsp            # imm = 0x638
movl %ecx, 0x1c(%rsp)
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbp
movl $0x401, %edi            # imm = 0x401
callq 0x22a0
movq %rax, %r12
cmpl $0x0, 0x1d52e(%rip)     # 0x21300
jle 0x3dee
leaq 0xed24(%rip), %rdi      # 0x12aff
leaq 0xe5fd(%rip), %rsi      # 0x123df
movl $0x4eb, %edx            # imm = 0x4EB
xorl %eax, %eax
callq 0xc84f
leaq 0x5efd(%r14), %r8
leaq 0xe610(%rip), %rdx      # 0x1240c
xorl %r15d, %r15d
leaq 0x230(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r13, 0x28(%rsp)
movq %r13, %rcx
movq %r8, 0x20(%rsp)
xorl %eax, %eax
callq 0x2140
movq %rbx, %rdi
movl $0x41, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x2330
cmpl $-0x1, %eax
je 0x3f62
movl %eax, %r13d
movq %r14, 0x10(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x7d3d
testl %eax, %eax
je 0x3fbc
movl %eax, %r14d
leaq 0xe575(%rip), %rbx      # 0x123df
xorl %r15d, %r15d
cmpl $0x0, 0x1d48c(%rip)     # 0x21300
jle 0x3e92
leaq 0xeceb(%rip), %rdi      # 0x12b68
movq %rbx, %rsi
movl $0x4f8, %edx            # imm = 0x4F8
movq %r12, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0xc84f
testl %r15d, %r15d
je 0x3f01
movl $0x1, %r15d
movq %r12, %rdi
movl %r14d, %esi
callq 0xd609
movl 0x1d452(%rip), %ecx     # 0x21300
testl %eax, %eax
jne 0x3f9e
testl %ecx, %ecx
jle 0x3ed3
leaq 0xed90(%rip), %rdi      # 0x12c51
movq %rbx, %rsi
movl $0x509, %edx            # imm = 0x509
movq %r12, %rcx
xorl %eax, %eax
callq 0xc84f
movslq %r14d, %rdx
movl %r13d, %edi
movq %r12, %rsi
callq 0x20e0
movq %rbp, %rdi
movq %r12, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x7d3d
movl %eax, %r14d
testl %eax, %eax
jne 0x3e6d
jmp 0x3fbc
movq %r12, %rdi
callq 0xd832
testl %eax, %eax
je 0x3f5a
cmpl $0x0, 0x1d3ec(%rip)     # 0x21300
jle 0x3e97
leaq 0xec7a(%rip), %rdi      # 0x12b97
movq %rbx, %rsi
movl $0x4fc, %edx            # imm = 0x4FC
movq %r12, %rcx
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0x1d3ca(%rip)     # 0x21300
jle 0x3e97
leaq 0xec90(%rip), %rdi      # 0x12bd3
movq %rbx, %rsi
movl $0x4fe, %edx            # imm = 0x4FE
movq %r12, %rcx
xorl %eax, %eax
callq 0xc84f
jmp 0x3e97
xorl %r15d, %r15d
jmp 0x3e9d
callq 0x2060
movl (%rax), %edi
callq 0x23b0
leaq 0xebae(%rip), %rdi      # 0x12b23
leaq 0xe463(%rip), %rsi      # 0x123df
leaq 0x230(%rsp), %rcx
movl $0x4f2, %edx            # imm = 0x4F2
movq %rax, %r8
xorl %eax, %eax
callq 0xc84f
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x41ab
testl %ecx, %ecx
jle 0x3fdf
leaq 0xec69(%rip), %rdi      # 0x12c12
leaq 0xe42f(%rip), %rsi      # 0x123df
movl $0x505, %edx            # imm = 0x505
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0x1d33d(%rip)     # 0x21300
jle 0x3fdf
leaq 0xecaf(%rip), %rdi      # 0x12c7b
leaq 0xe40c(%rip), %rsi      # 0x123df
movl $0x50e, %edx            # imm = 0x50E
xorl %eax, %eax
callq 0xc84f
movq 0x10(%rsp), %r14
movq %r12, %rdi
callq 0x2030
movl %r13d, %edi
callq 0x21a0
cmpl $0x0, 0x1d305(%rip)     # 0x21300
jle 0x4017
leaq 0xecb0(%rip), %rdi      # 0x12cb4
leaq 0xe3d4(%rip), %rsi      # 0x123df
movl $0x511, %edx            # imm = 0x511
xorl %eax, %eax
callq 0xc84f
testl %r15d, %r15d
movq 0x28(%rsp), %r15
je 0x40e2
leaq 0xe3e0(%rip), %rdx      # 0x1240c
leaq 0x30(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rcx
movq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x1d2b4(%rip)     # 0x21300
jle 0x4068
leaq 0xec9b(%rip), %rdi      # 0x12cf0
leaq 0xe383(%rip), %rsi      # 0x123df
movl $0x518, %edx            # imm = 0x518
xorl %eax, %eax
callq 0xc84f
leaq 0x17b90(%r14), %rbx
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
xorl %r12d, %r12d
leaq 0x30(%rsp), %rdx
xorl %edi, %edi
movq %r15, %rsi
movq %rbx, %rcx
movl $0x1001, %r8d           # imm = 0x1001
xorl %r9d, %r9d
callq 0xda45
cmpl $-0x1, %eax
je 0x40ea
cmpl $0x64, %eax
cmovnel %eax, %r12d
addl %r12d, 0x1da75(%rip)    # 0x21b1c
cmpb $0x0, (%rbx)
je 0x4185
leaq 0xed49(%rip), %rsi      # 0x12e00
movq %rbx, %rdi
callq 0x2050
testl %eax, %eax
je 0x413c
leaq 0xed75(%rip), %rdi      # 0x12e3f
leaq 0xe30e(%rip), %rsi      # 0x123df
movl $0x53b, %edx            # imm = 0x53B
xorl %eax, %eax
callq 0xc84f
jmp 0x4185
xorl %r12d, %r12d
jmp 0x4185
leaq 0x2b8bf(%rip), %rax     # 0x2f9b0
movl (%rax), %ecx
leal -0x64(%rcx), %eax
cmpl $0x2, %eax
jb 0x410d
cmpl $0x66, %ecx
je 0x41c8
cmpl $0x67, %ecx
jne 0x41e7
cmpl $0x0, 0x1d1ec(%rip)     # 0x21300
jle 0x41c0
leaq 0xec05(%rip), %rdi      # 0x12d26
leaq 0xe2b7(%rip), %rsi      # 0x123df
xorl %r12d, %r12d
movl $0x523, %edx            # imm = 0x523
xorl %eax, %eax
callq 0xc84f
jmp 0x40a0
cmpl $0x0, 0x1d1bd(%rip)     # 0x21300
jle 0x415f
leaq 0xecc0(%rip), %rdi      # 0x12e0c
leaq 0xe28c(%rip), %rsi      # 0x123df
movl $0x537, %edx            # imm = 0x537
xorl %eax, %eax
callq 0xc84f
leaq 0x13efa(%rip), %rdx     # 0x18060
movl $0x80, %esi
movq 0x20(%rsp), %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2140
movq %r15, %rdi
movq %r14, %rsi
callq 0x3c24
cmpl $0x0, 0x1d174(%rip)     # 0x21300
jle 0x41ab
leaq 0xece4(%rip), %rdi      # 0x12e79
leaq 0xe243(%rip), %rsi      # 0x123df
movl $0x53f, %edx            # imm = 0x53F
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc84f
movl %r12d, %eax
addq $0x638, %rsp            # imm = 0x638
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0x40a0
leaq 0xeb98(%rip), %rdi      # 0x12d67
leaq 0xe209(%rip), %rsi      # 0x123df
movl $0x528, %edx            # imm = 0x528
xorl %eax, %eax
callq 0xc84f
jmp 0x3f93
leaq 0xebc6(%rip), %rdi      # 0x12db4
leaq 0xe1ea(%rip), %rsi      # 0x123df
movl $0x52d, %edx            # imm = 0x52D
xorl %eax, %eax
callq 0xc84f
jmp 0x3f93