// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rdi, %rbp
movl $0x401, %edi            # imm = 0x401
callq 0x22a0
movq %rax, %r12
cmpl $0x0, 0x1c654(%rip)     # 0x20300
jle 0x3cc8
leaq 0xde4a(%rip), %rdi      # 0x11aff
leaq 0xd723(%rip), %rsi      # 0x113df
movl $0x4eb, %edx            # imm = 0x4EB
xorl %eax, %eax
callq 0xc2a1
leaq 0x5efd(%r14), %r8
leaq 0xd736(%rip), %rdx      # 0x1140c
leaq 0x230(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r15, %rcx
movq %r8, 0x20(%rsp)
xorl %eax, %eax
callq 0x2140
pushq $0x41
popq %rsi
movq %rbx, %rdi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x2340
cmpl $-0x1, %eax
je 0x3e2d
movl %eax, %r13d
movq %r15, 0x10(%rsp)
movq %r14, 0x28(%rsp)
pushq $0x1
popq %r15
xorl %r14d, %r14d
movq %rbp, %rdi
movq %r12, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x7ad2
testl %eax, %eax
je 0x3e85
movl %eax, %ebx
cmpl $0x0, 0x1c5bb(%rip)     # 0x20300
jle 0x3d67
leaq 0xde1a(%rip), %rdi      # 0x11b68
leaq 0xd68a(%rip), %rsi      # 0x113df
movl $0x4f8, %edx            # imm = 0x4F8
movq %r12, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0xc2a1
testl %r14d, %r14d
movl %r15d, %r14d
je 0x3dbb
movq %r12, %rdi
movl %ebx, %esi
callq 0xcfc7
movl 0x1c581(%rip), %ecx     # 0x20300
testl %eax, %eax
jne 0x3e67
testl %ecx, %ecx
jle 0x3da8
leaq 0xdebf(%rip), %rdi      # 0x11c51
leaq 0xd646(%rip), %rsi      # 0x113df
movl $0x509, %edx            # imm = 0x509
movq %r12, %rcx
xorl %eax, %eax
callq 0xc2a1
movslq %ebx, %rdx
movl %r13d, %edi
movq %r12, %rsi
callq 0x20e0
jmp 0x3d24
movq %r12, %rdi
callq 0xd1f4
testl %eax, %eax
je 0x3e25
cmpl $0x0, 0x1c532(%rip)     # 0x20300
movl %r15d, %r14d
jle 0x3d6f
leaq 0xddbd(%rip), %rdi      # 0x11b97
leaq 0xd5fe(%rip), %rsi      # 0x113df
movl $0x4fc, %edx            # imm = 0x4FC
movq %r12, %rcx
xorl %eax, %eax
callq 0xc2a1
cmpl $0x0, 0x1c509(%rip)     # 0x20300
movl %r15d, %r14d
jle 0x3d6f
leaq 0xddcc(%rip), %rdi      # 0x11bd3
leaq 0xd5d1(%rip), %rsi      # 0x113df
movl $0x4fe, %edx            # imm = 0x4FE
movq %r12, %rcx
xorl %eax, %eax
callq 0xc2a1
movl %r15d, %r14d
jmp 0x3d6f
xorl %r14d, %r14d
jmp 0x3d6f
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0xdce3(%rip), %rdi      # 0x11b23
leaq 0xd598(%rip), %rsi      # 0x113df
leaq 0x230(%rsp), %rcx
movl $0x4f2, %edx            # imm = 0x4F2
movq %rax, %r8
xorl %eax, %eax
callq 0xc2a1
pushq $-0x1
popq %r12
jmp 0x407b
testl %ecx, %ecx
jle 0x3eaf
leaq 0xdda0(%rip), %rdi      # 0x11c12
leaq 0xd566(%rip), %rsi      # 0x113df
movl $0x505, %edx            # imm = 0x505
xorl %eax, %eax
callq 0xc2a1
movq 0x10(%rsp), %r15
cmpl $0x0, 0x1c46f(%rip)     # 0x20300
jle 0x3eb4
leaq 0xdde1(%rip), %rdi      # 0x11c7b
leaq 0xd53e(%rip), %rsi      # 0x113df
movl $0x50e, %edx            # imm = 0x50E
xorl %eax, %eax
callq 0xc2a1
jmp 0x3eb4
movq 0x10(%rsp), %r15
movq %r12, %rdi
callq 0x2030
movl %r13d, %edi
callq 0x21a0
cmpl $0x0, 0x1c435(%rip)     # 0x20300
jle 0x3ee7
leaq 0xdde0(%rip), %rdi      # 0x11cb4
leaq 0xd504(%rip), %rsi      # 0x113df
movl $0x511, %edx            # imm = 0x511
xorl %eax, %eax
callq 0xc2a1
testl %r14d, %r14d
je 0x3fb2
leaq 0xd515(%rip), %rdx      # 0x1140c
leaq 0x30(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rcx
movq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x1c3e9(%rip)     # 0x20300
jle 0x3f33
leaq 0xddd0(%rip), %rdi      # 0x11cf0
leaq 0xd4b8(%rip), %rsi      # 0x113df
movl $0x518, %edx            # imm = 0x518
xorl %eax, %eax
callq 0xc2a1
movq 0x28(%rsp), %r14
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 0xd3fd
cmpl $-0x1, %eax
je 0x3fba
cmpl $0x64, %eax
cmovnel %eax, %r12d
addl %r12d, 0x1cba5(%rip)    # 0x20b1c
cmpb $0x0, (%rbx)
je 0x4055
leaq 0xde79(%rip), %rsi      # 0x11e00
movq %rbx, %rdi
callq 0x2050
testl %eax, %eax
je 0x400c
leaq 0xdea5(%rip), %rdi      # 0x11e3f
leaq 0xd43e(%rip), %rsi      # 0x113df
movl $0x53b, %edx            # imm = 0x53B
xorl %eax, %eax
callq 0xc2a1
jmp 0x4055
xorl %r12d, %r12d
jmp 0x4055
leaq 0x2a9ef(%rip), %rax     # 0x2e9b0
movl (%rax), %ecx
leal -0x64(%rcx), %eax
cmpl $0x2, %eax
jb 0x3fdd
cmpl $0x66, %ecx
je 0x4098
cmpl $0x67, %ecx
jne 0x40b7
cmpl $0x0, 0x1c31c(%rip)     # 0x20300
jle 0x4090
leaq 0xdd35(%rip), %rdi      # 0x11d26
leaq 0xd3e7(%rip), %rsi      # 0x113df
xorl %r12d, %r12d
movl $0x523, %edx            # imm = 0x523
xorl %eax, %eax
callq 0xc2a1
jmp 0x3f70
cmpl $0x0, 0x1c2ed(%rip)     # 0x20300
jle 0x402f
leaq 0xddf0(%rip), %rdi      # 0x11e0c
leaq 0xd3bc(%rip), %rsi      # 0x113df
movl $0x537, %edx            # imm = 0x537
xorl %eax, %eax
callq 0xc2a1
leaq 0x1302a(%rip), %rdx     # 0x17060
movl $0x80, %esi
movq 0x20(%rsp), %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2140
movq %r15, %rdi
movq %r14, %rsi
callq 0x3afe
cmpl $0x0, 0x1c2a4(%rip)     # 0x20300
jle 0x407b
leaq 0xde14(%rip), %rdi      # 0x11e79
leaq 0xd373(%rip), %rsi      # 0x113df
movl $0x53f, %edx            # imm = 0x53F
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc2a1
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 0x3f70
leaq 0xdcc8(%rip), %rdi      # 0x11d67
leaq 0xd339(%rip), %rsi      # 0x113df
movl $0x528, %edx            # imm = 0x528
xorl %eax, %eax
callq 0xc2a1
jmp 0x3e5e
leaq 0xdcf6(%rip), %rdi      # 0x11db4
leaq 0xd31a(%rip), %rsi      # 0x113df
movl $0x52d, %edx            # imm = 0x52D
xorl %eax, %eax
callq 0xc2a1
jmp 0x3e5e