// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x680, %rsp            # imm = 0x680
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x400, -0x434(%rbp)    # imm = 0x400
movl -0x434(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2290
movq %rax, -0x440(%rbp)
movl $0x0, -0x44c(%rbp)
movl $0x0, -0x450(%rbp)
cmpl $0x1, 0x22ace(%rip)     # 0x276f0
jl 0x4c3e
leaq 0x14d74(%rip), %rdi     # 0x1999f
leaq 0x1489d(%rip), %rsi     # 0x194cf
movl $0x4eb, %edx            # imm = 0x4EB
movb $0x0, %al
callq 0x117c0
leaq -0x430(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
addq $0x5efd, %r8            # imm = 0x5EFD
movl $0x400, %esi            # imm = 0x400
leaq 0x1489c(%rip), %rdx     # 0x194fc
movb $0x0, %al
callq 0x2130
leaq -0x430(%rbp), %rdi
movl $0x41, %esi
movl $0x180, %edx            # imm = 0x180
movb $0x0, %al
callq 0x2320
movl %eax, -0x454(%rbp)
cmpl $-0x1, -0x454(%rbp)
jne 0x4cd8
leaq -0x430(%rbp), %rax
movq %rax, -0x668(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x23a0
movq -0x668(%rbp), %rcx
movq %rax, %r8
leaq 0x14d0a(%rip), %rdi     # 0x199c3
leaq 0x1480f(%rip), %rsi     # 0x194cf
movl $0x4f2, %edx            # imm = 0x4F2
movb $0x0, %al
callq 0x117c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x50fd
jmp 0x4cda
movq -0x10(%rbp), %rdi
movq -0x440(%rbp), %rsi
movl -0x434(%rbp), %edx
callq 0xa120
cltq
movq %rax, -0x448(%rbp)
cmpq $0x0, %rax
jbe 0x4e3a
cmpl $0x1, 0x229e6(%rip)     # 0x276f0
jl 0x4d34
movq -0x440(%rbp), %rcx
movq -0x440(%rbp), %r8
leaq 0x14ce7(%rip), %rdi     # 0x19a08
leaq 0x147a7(%rip), %rsi     # 0x194cf
movl $0x4f8, %edx            # imm = 0x4F8
movb $0x0, %al
callq 0x117c0
cmpl $0x0, -0x44c(%rbp)
jne 0x4dac
movq -0x440(%rbp), %rdi
callq 0x131a0
cmpl $0x0, %eax
je 0x4dac
cmpl $0x1, 0x2299b(%rip)     # 0x276f0
jl 0x4d78
movq -0x440(%rbp), %rcx
leaq 0x14cd2(%rip), %rdi     # 0x19a37
leaq 0x14763(%rip), %rsi     # 0x194cf
movl $0x4fc, %edx            # imm = 0x4FC
movb $0x0, %al
callq 0x117c0
cmpl $0x1, 0x22971(%rip)     # 0x276f0
jl 0x4da2
movq -0x440(%rbp), %rcx
leaq 0x14ce4(%rip), %rdi     # 0x19a73
leaq 0x14739(%rip), %rsi     # 0x194cf
movl $0x4fe, %edx            # imm = 0x4FE
movb $0x0, %al
callq 0x117c0
movl $0x1, -0x44c(%rbp)
movq -0x440(%rbp), %rdi
movq -0x448(%rbp), %rax
movl %eax, %esi
callq 0x12d80
cmpl $0x0, %eax
je 0x4deb
cmpl $0x1, 0x22923(%rip)     # 0x276f0
jl 0x4de9
leaq 0x14cdc(%rip), %rdi     # 0x19ab2
leaq 0x146f2(%rip), %rsi     # 0x194cf
movl $0x505, %edx            # imm = 0x505
movb $0x0, %al
callq 0x117c0
jmp 0x4e3a
cmpl $0x1, 0x228fe(%rip)     # 0x276f0
jl 0x4e15
movq -0x440(%rbp), %rcx
leaq 0x14cef(%rip), %rdi     # 0x19af1
leaq 0x146c6(%rip), %rsi     # 0x194cf
movl $0x509, %edx            # imm = 0x509
movb $0x0, %al
callq 0x117c0
movl -0x454(%rbp), %edi
movq -0x440(%rbp), %rsi
movq -0x448(%rbp), %rdx
callq 0x20d0
movq %rax, -0x460(%rbp)
jmp 0x4cda
cmpl $0x1, 0x228af(%rip)     # 0x276f0
jl 0x4e5d
leaq 0x14cd1(%rip), %rdi     # 0x19b1b
leaq 0x1467e(%rip), %rsi     # 0x194cf
movl $0x50e, %edx            # imm = 0x50E
movb $0x0, %al
callq 0x117c0
movq -0x440(%rbp), %rdi
callq 0x2030
movl -0x454(%rbp), %edi
callq 0x2180
cmpl $0x1, 0x22875(%rip)     # 0x276f0
jl 0x4e97
leaq 0x14cd0(%rip), %rdi     # 0x19b54
leaq 0x14644(%rip), %rsi     # 0x194cf
movl $0x511, %edx            # imm = 0x511
movb $0x0, %al
callq 0x117c0
cmpl $0x0, -0x44c(%rbp)
je 0x50d1
leaq -0x660(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
addq $0x5efd, %r8            # imm = 0x5EFD
movl $0x200, %esi            # imm = 0x200
leaq 0x14636(%rip), %rdx     # 0x194fc
movb $0x0, %al
callq 0x2130
cmpl $0x1, 0x2281c(%rip)     # 0x276f0
jl 0x4ef0
leaq 0x14cb3(%rip), %rdi     # 0x19b90
leaq 0x145eb(%rip), %rsi     # 0x194cf
movl $0x518, %edx            # imm = 0x518
movb $0x0, %al
callq 0x117c0
movq -0x20(%rbp), %rax
cmpl $0x6a, 0x166f8(%rax)
jne 0x4f07
movl $0x0, -0x450(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x660(%rbp), %rdx
movq -0x20(%rbp), %rcx
addq $0x17b90, %rcx          # imm = 0x17B90
movl -0x450(%rbp), %r9d
movl -0x24(%rbp), %eax
xorl %edi, %edi
movl $0x1001, %r8d           # imm = 0x1001
movl %eax, (%rsp)
callq 0x134d0
movl %eax, -0x28(%rbp)
cmpl $-0x1, -0x28(%rbp)
jne 0x5003
leaq 0x30e45(%rip), %rax     # 0x35d90
movl (%rax), %eax
movl %eax, -0x66c(%rbp)
addl $-0x64, %eax
subl $0x2, %eax
jb 0x4f77
jmp 0x4f5d
movl -0x66c(%rbp), %eax
subl $0x66, %eax
je 0x4fac
jmp 0x4f6a
movl -0x66c(%rbp), %eax
subl $0x67, %eax
jne 0x4fd2
jmp 0x4f77
cmpl $0x1, 0x22772(%rip)     # 0x276f0
jl 0x4fa3
leaq 0x30e09(%rip), %rax     # 0x35d90
movl (%rax), %ecx
leaq 0x14c36(%rip), %rdi     # 0x19bc6
leaq 0x14538(%rip), %rsi     # 0x194cf
movl $0x523, %edx            # imm = 0x523
movb $0x0, %al
callq 0x117c0
movl $0x0, -0x28(%rbp)
jmp 0x5001
leaq 0x14c54(%rip), %rdi     # 0x19c07
leaq 0x14515(%rip), %rsi     # 0x194cf
movl $0x528, %edx            # imm = 0x528
movb $0x0, %al
callq 0x117c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x50fd
leaq 0x30db7(%rip), %rax     # 0x35d90
movl (%rax), %ecx
leaq 0x14c72(%rip), %rdi     # 0x19c54
leaq 0x144e6(%rip), %rsi     # 0x194cf
movl $0x52d, %edx            # imm = 0x52D
movb $0x0, %al
callq 0x117c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x50fd
jmp 0x5003
cmpl $0x64, -0x28(%rbp)
jne 0x5010
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
addl 0x22ef3(%rip), %eax     # 0x27f0c
movl %eax, 0x22eed(%rip)     # 0x27f0c
movq -0x20(%rbp), %rdi
addq $0x17b90, %rdi          # imm = 0x17B90
callq 0x2100
cmpq $0x0, %rax
je 0x50cf
movq -0x20(%rbp), %rdi
addq $0x17b90, %rdi          # imm = 0x17B90
leaq 0x14c55(%rip), %rsi     # 0x19ca0
callq 0x2040
cmpl $0x0, %eax
jne 0x50b3
cmpl $0x1, 0x22694(%rip)     # 0x276f0
jl 0x5078
leaq 0x14c47(%rip), %rdi     # 0x19cac
leaq 0x14463(%rip), %rsi     # 0x194cf
movl $0x537, %edx            # imm = 0x537
movb $0x0, %al
callq 0x117c0
movq -0x20(%rbp), %rdi
addq $0x5efd, %rdi           # imm = 0x5EFD
movq -0x20(%rbp), %rcx
addq $0x17b90, %rcx          # imm = 0x17B90
movl $0x80, %esi
leaq 0x19f10(%rip), %rdx     # 0x1efaa
movb $0x0, %al
callq 0x2130
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
callq 0x4970
jmp 0x50cd
leaq 0x14c25(%rip), %rdi     # 0x19cdf
leaq 0x1440e(%rip), %rsi     # 0x194cf
movl $0x53b, %edx            # imm = 0x53B
movb $0x0, %al
callq 0x117c0
jmp 0x50cf
jmp 0x50d1
cmpl $0x1, 0x22618(%rip)     # 0x276f0
jl 0x50f7
movl -0x28(%rbp), %ecx
leaq 0x14c35(%rip), %rdi     # 0x19d19
leaq 0x143e4(%rip), %rsi     # 0x194cf
movl $0x53f, %edx            # imm = 0x53F
movb $0x0, %al
callq 0x117c0
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x680, %rsp            # imm = 0x680
popq %rbp
retq
nopl (%rax)