// File: /workspace/github_c_makefile_star2_O1/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, 0x14(%rsp)
movq %rdx, %r14
movq %rsi, %rbp
movq %rdi, %r13
movl $0x401, %edi            # imm = 0x401
callq 0x22b0
movq %rax, %r12
cmpl $0x0, 0x1d4e7(%rip)     # 0x21300
jle 0x3e35
leaq 0xecad(%rip), %rdi      # 0x12acf
leaq 0xe5b6(%rip), %rsi      # 0x123df
movl $0x4eb, %edx            # imm = 0x4EB
xorl %eax, %eax
callq 0xc960
leaq 0x5efd(%r14), %r8
leaq 0xe5c9(%rip), %rdx      # 0x1240c
xorl %r15d, %r15d
leaq 0x230(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %rbp, 0x20(%rsp)
movq %rbp, %rcx
movq %r8, 0x18(%rsp)
xorl %eax, %eax
callq 0x2140
movq %rbx, %rdi
movl $0x41, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x2340
cmpl $-0x1, %eax
je 0x3fa7
movl %eax, %ebp
movq %r14, 0x28(%rsp)
movq %r13, %rdi
movq %r12, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x7df3
testl %eax, %eax
je 0x4000
movl %eax, %r14d
leaq 0xe52f(%rip), %rbx      # 0x123df
xorl %r15d, %r15d
cmpl $0x0, 0x1d446(%rip)     # 0x21300
jle 0x3ed8
leaq 0xec75(%rip), %rdi      # 0x12b38
movq %rbx, %rsi
movl $0x4f8, %edx            # imm = 0x4F8
movq %r12, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0xc960
testl %r15d, %r15d
je 0x3f46
movl $0x1, %r15d
movq %r12, %rdi
movl %r14d, %esi
callq 0xd799
movl 0x1d40c(%rip), %ecx     # 0x21300
testl %eax, %eax
jne 0x3fe2
testl %ecx, %ecx
jle 0x3f19
leaq 0xed1a(%rip), %rdi      # 0x12c21
movq %rbx, %rsi
movl $0x509, %edx            # imm = 0x509
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
movslq %r14d, %rdx
movl %ebp, %edi
movq %r12, %rsi
callq 0x20e0
movq %r13, %rdi
movq %r12, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x7df3
movl %eax, %r14d
testl %eax, %eax
jne 0x3eb3
jmp 0x4000
movq %r12, %rdi
callq 0xd9ce
testl %eax, %eax
je 0x3f9f
cmpl $0x0, 0x1d3a7(%rip)     # 0x21300
jle 0x3f74
leaq 0xec05(%rip), %rdi      # 0x12b67
movq %rbx, %rsi
movl $0x4fc, %edx            # imm = 0x4FC
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
cmpl $0x0, 0x1d385(%rip)     # 0x21300
jle 0x3edd
leaq 0xec1b(%rip), %rdi      # 0x12ba3
movq %rbx, %rsi
movl $0x4fe, %edx            # imm = 0x4FE
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
jmp 0x3edd
xorl %r15d, %r15d
jmp 0x3ee3
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0xeb39(%rip), %rdi      # 0x12af3
leaq 0xe41e(%rip), %rsi      # 0x123df
leaq 0x230(%rsp), %rcx
movl $0x4f2, %edx            # imm = 0x4F2
movq %rax, %r8
xorl %eax, %eax
callq 0xc960
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x4246
testl %ecx, %ecx
jle 0x4000
leaq 0xebf5(%rip), %rdi      # 0x12be2
leaq 0xe3eb(%rip), %rsi      # 0x123df
movl $0x505, %edx            # imm = 0x505
xorl %eax, %eax
callq 0xc960
cmpl $0x0, 0x1d2f9(%rip)     # 0x21300
jle 0x4023
leaq 0xec3b(%rip), %rdi      # 0x12c4b
leaq 0xe3c8(%rip), %rsi      # 0x123df
movl $0x50e, %edx            # imm = 0x50E
xorl %eax, %eax
callq 0xc960
movq %r12, %rdi
callq 0x2030
movl %ebp, %edi
callq 0x21a0
cmpl $0x0, 0x1d2c7(%rip)     # 0x21300
movq 0x28(%rsp), %r14
jle 0x405a
leaq 0xec3d(%rip), %rdi      # 0x12c84
leaq 0xe391(%rip), %rsi      # 0x123df
movl $0x511, %edx            # imm = 0x511
xorl %eax, %eax
callq 0xc960
testl %r15d, %r15d
movq 0x20(%rsp), %r15
je 0x412a
leaq 0xe39d(%rip), %rdx      # 0x1240c
leaq 0x30(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %r15, %rcx
movq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x1d271(%rip)     # 0x21300
jle 0x40ab
leaq 0xec28(%rip), %rdi      # 0x12cc0
leaq 0xe340(%rip), %rsi      # 0x123df
movl $0x518, %edx            # imm = 0x518
xorl %eax, %eax
callq 0xc960
leaq 0x17b90(%r14), %rbx
movl 0x14(%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 0xdbe8
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x4132
cmpl $0x64, %ebp
cmovnel %ebp, %r12d
addl %r12d, 0x1da30(%rip)    # 0x21b1c
movb $0x1, %bpl
cmpb $0x0, (%rbx)
je 0x4213
leaq 0xecd1(%rip), %rsi      # 0x12dd0
movq %rbx, %rdi
callq 0x2050
testl %eax, %eax
je 0x417f
leaq 0xecfd(%rip), %rdi      # 0x12e0f
leaq 0xe2c6(%rip), %rsi      # 0x123df
movl $0x53b, %edx            # imm = 0x53B
xorl %eax, %eax
callq 0xc960
jmp 0x4213
xorl %r12d, %r12d
jmp 0x421d
leaq 0x2b877(%rip), %rax     # 0x2f9b0
movl (%rax), %ecx
leal -0x64(%rcx), %eax
cmpl $0x2, %eax
jb 0x4155
cmpl $0x66, %ecx
je 0x41d3
cmpl $0x67, %ecx
jne 0x41f1
cmpl $0x0, 0x1d1a4(%rip)     # 0x21300
jle 0x41cc
leaq 0xeb91(%rip), %rdi      # 0x12cf6
leaq 0xe273(%rip), %rsi      # 0x123df
xorl %ebp, %ebp
movl $0x523, %edx            # imm = 0x523
xorl %eax, %eax
callq 0xc960
jmp 0x40de
cmpl $0x0, 0x1d17a(%rip)     # 0x21300
jle 0x41a2
leaq 0xec4d(%rip), %rdi      # 0x12ddc
leaq 0xe249(%rip), %rsi      # 0x123df
movl $0x537, %edx            # imm = 0x537
xorl %eax, %eax
callq 0xc960
leaq 0x13ee4(%rip), %rdx     # 0x1808d
movl $0x80, %esi
movq 0x18(%rsp), %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2140
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x3c6b
jmp 0x4213
xorl %ebp, %ebp
jmp 0x40de
leaq 0xeb5d(%rip), %rdi      # 0x12d37
leaq 0xe1fe(%rip), %rsi      # 0x123df
xorl %ebp, %ebp
movl $0x528, %edx            # imm = 0x528
xorl %eax, %eax
callq 0xc960
jmp 0x420d
leaq 0xeb8c(%rip), %rdi      # 0x12d84
leaq 0xe1e0(%rip), %rsi      # 0x123df
xorl %ebp, %ebp
movl $0x52d, %edx            # imm = 0x52D
xorl %eax, %eax
callq 0xc960
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %bpl, %bpl
je 0x4246
cmpl $0x0, 0x1d0dc(%rip)     # 0x21300
jle 0x4243
leaq 0xec1c(%rip), %rdi      # 0x12e49
leaq 0xe1ab(%rip), %rsi      # 0x123df
movl $0x53f, %edx            # imm = 0x53F
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc960
movl %r12d, %eax
addq $0x638, %rsp            # imm = 0x638
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq