// File: /workspace/github_c_makefile_star2_O2/inflex[P]ripMIME/mime.c
// Function: MIME_decode_text
int MIME_decode_text( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo, int keep )
{

    FILE *of;                           // output file
    int linecount = 0;                  // The number of lines
    int file_has_uuencode = 0;          // Flag to indicate this text has UUENCODE in it
    char fullfilename[1024]="";     // Filename of the output file
    char line[1024];                    // The input lines from the file we're decoding
    char *get_result = &line[0];
    int lastlinewasboundary = 0;
    int result = 0;
    int decodesize=0;

    snprintf(fullfilename,sizeof(fullfilename),"%s/%s",unpackdir,hinfo->filename);
    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, fullfilename);

    if (!f)
    {
        /** If we cannot open the file for reading, leave an error and return -1 **/
        LOGGER_log("%s:%d:MIME_decode_text:ERROR: print-quotable input stream broken.",FL);
        return _EXITERR_MIMEREAD_CANNOT_OPEN_INPUT;
    }
    if (f)
    {
        /** If we were able to open the input file, try opening the output file and process the data **/
        of = fopen(fullfilename,"w");
        if (!of)
        {
            /** If we were unable to open the output file, report the error and return -1 **/
            LOGGER_log("%s:%d:MIME_decode_text:ERROR: cannot open %s for writing",FL,fullfilename);
            return _EXITERR_MIMEREAD_CANNOT_WRITE_OUTPUT;
        }
        while ((get_result = FFGET_fgets(line,1023,f))&&(of))
        {
            int line_len = strlen(line);
            linecount++;
            //      if (MIME_DPEDANTIC) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: line=%s",FL,line);
            if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: line[len=%d]=%s",FL,line_len,line);
            //20041217-1529:PLD:
            if (line[0] == '-')
            {
                if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Testing boundary",FL);
                if ((BS_count() > 0)&&(BS_cmp(line,line_len)))
                {
                    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL);
                    lastlinewasboundary = 1;
                    result = 0;
                    break;
                }
            }

            if (lastlinewasboundary == 0)
            {
                if (hinfo->content_transfer_encoding == _CTRANS_ENCODING_QP)
                {
                    size_t bc;
                    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL);
                    decodesize = MDECODE_decode_qp_text(line);
                    bc = fwrite(line, 1, decodesize, of);

                } else {
                    fprintf(of,"%s",line);
                }

                if ((!file_has_uuencode)&&( UUENCODE_is_uuencode_header( line )))
                {
                    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: UUENCODED data located in file.\n",FL);
                    file_has_uuencode = 1;
                }
            }
            //  linecount++;
            if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: End processing line.",FL);
        } // while
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done writing output file '%s'...now attempting to close.",FL, fullfilename);
        // if the file is still safely open
        if (of)
        {
            fclose(of);
        } // if file still safely open

        if (linecount == 0)
        {
            result = MIME_STATUS_ZERO_FILE;
            return result;
        }
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Closed.",FL);
    } // if main input file stream was open

    // If our input from the file was invalid due to EOF or other
    // then we return a -1 code to indicate that the end has
    // occured.
    //
    if (!get_result) {
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: FFGET module ran out of file data while attempting to decode",FL);
        //      result = -1;
        result = MIME_ERROR_FFGET_EMPTY; // 20040305-1323:PLD
    }

    // If there was UUEncoded portions [potentially] in the email, the
    // try to extract them using the MIME_decode_uu()
    //
    if (file_has_uuencode)
    {
        char ffname[256];
        snprintf(ffname,256,"%s/%s", unpackdir, hinfo->filename);
        // PLD-20040627-1212
        // Make sure uudec_name is blank too
        //
        hinfo->uudec_name[0] = '\0';
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding UUencoded data in file '%s'\n",FL,hinfo->filename);
        //result = UUENCODE_decode_uu( NULL, unpackdir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep );
        // Attempt to decode the UUENCODED data in the file,
        //      NOTE - hinfo->uudec_name is a blank buffer which will be filled by the UUENCODE_decode_uu
        //          function once it has located a filename in the UUENCODED data.  A bit of a problem here
        //          is that it can only hold ONE filename!
        //
        //      NOTE - this function returns the NUMBER of attachments it decoded in the return value!  Don't
        //          propergate this value unintentionally to parent functions (ie, if you were thinking it was
        //          an error-status return value

        result = UUENCODE_decode_uu( NULL, unpackdir, ffname, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, 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 > 0 ) { glb.attachment_count += result; result = 0; }
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: hinfo = %p\n",FL,hinfo);
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done. [ UUName = '%s' ]\n",FL,hinfo->uudec_name);
        if (strncasecmp(hinfo->uudec_name,"winmail.dat",11)==0)
        {
            if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TNEF format\n",FL);
            snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name);
            MIME_decode_TNEF( unpackdir, hinfo, keep );
        }
        if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Completed decoding UUencoded data.\n",FL);
    }
    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: result=%d ----------------Done\n",FL,result);
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x948, %rsp            # imm = 0x948
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0x30(%rsp)
leaq 0x140(%rsp), %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2170
leaq 0x5efd(%r14), %r12
leaq 0xd2f3(%rip), %rdx      # 0x1140c
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r15, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x1c1cb(%rip)     # 0x20300
jle 0x4160
movl 0x166f8(%r14), %ecx
leaq 0xdd63(%rip), %rdi      # 0x11ea8
leaq 0xd293(%rip), %rsi      # 0x113df
leaq 0x140(%rsp), %r8
movl $0x55a, %edx            # imm = 0x55A
xorl %eax, %eax
callq 0xc2a1
cmpq $0x0, 0x30(%rsp)
je 0x4308
leaq 0xddb8(%rip), %rsi      # 0x11f2b
leaq 0x140(%rsp), %rdi
callq 0x2350
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x432d
movl %ebp, 0x1c(%rsp)
movq %r12, 0x20(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x28(%rsp)
leaq 0x540(%rsp), %rbp
leaq 0xd22f(%rip), %r15      # 0x113df
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rbp, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq 0x30(%rsp), %rdx
callq 0x76f2
movq %rax, %r14
testq %rax, %rax
je 0x4385
movq %rbp, %rdi
callq 0x2110
movq %rax, %rbx
cmpl $0x0, 0x1c11a(%rip)     # 0x20300
jle 0x4203
leaq 0xdd77(%rip), %rdi      # 0x11f66
movq %r15, %rsi
movl $0x571, %edx            # imm = 0x571
movl %ebx, %ecx
movq %rbp, %r8
xorl %eax, %eax
callq 0xc2a1
cmpb $0x2d, 0x540(%rsp)
jne 0x4247
cmpl $0x0, 0x1c0ec(%rip)     # 0x20300
jle 0x422c
leaq 0xdd77(%rip), %rdi      # 0x11f94
movq %r15, %rsi
movl $0x575, %edx            # imm = 0x575
xorl %eax, %eax
callq 0xc2a1
callq 0xce6a
testl %eax, %eax
jle 0x4247
movq %rbp, %rdi
movl %ebx, %esi
callq 0xcfc7
testl %eax, %eax
jne 0x435a
movq 0x28(%rsp), %rax
cmpl $0x67, 0x166f8(%rax)
jne 0x4291
cmpl $0x0, 0x1c0a4(%rip)     # 0x20300
jle 0x4274
leaq 0xdd5a(%rip), %rdi      # 0x11fbf
movq %r15, %rsi
movl $0x584, %edx            # imm = 0x584
xorl %eax, %eax
callq 0xc2a1
movq %rbp, %rdi
callq 0xc6d9
movslq %eax, %rdx
movq %rbp, %rdi
pushq $0x1
popq %rsi
movq 0x10(%rsp), %rcx
callq 0x23a0
jmp 0x429e
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x2160
testl %r13d, %r13d
pushq $0x1
popq %r13
je 0x42ce
cmpl $0x0, 0x1c052(%rip)     # 0x20300
jle 0x42c6
leaq 0xdd7c(%rip), %rdi      # 0x12033
movq %r15, %rsi
movl $0x593, %edx            # imm = 0x593
xorl %eax, %eax
callq 0xc2a1
incl %r12d
jmp 0x41b6
movq %rbp, %rdi
callq 0xd1f4
testl %eax, %eax
je 0x4303
cmpl $0x0, 0x1c01f(%rip)     # 0x20300
pushq $0x1
popq %r13
jle 0x42c6
leaq 0xdd06(%rip), %rdi      # 0x11ff4
movq %r15, %rsi
movl $0x58e, %edx            # imm = 0x58E
xorl %eax, %eax
callq 0xc2a1
pushq $0x1
popq %r13
jmp 0x42a7
xorl %r13d, %r13d
jmp 0x42a7
leaq 0xdbda(%rip), %rdi      # 0x11ee9
leaq 0xd0c9(%rip), %rsi      # 0x113df
movl $0x55f, %edx            # imm = 0x55F
xorl %eax, %eax
callq 0xc2a1
movl $0xde, %r12d
jmp 0x4616
leaq 0xdbf9(%rip), %rdi      # 0x11f2d
leaq 0xd0a4(%rip), %rsi      # 0x113df
leaq 0x140(%rsp), %rcx
movl $0x569, %edx            # imm = 0x569
xorl %eax, %eax
callq 0xc2a1
movl $0xdd, %r12d
jmp 0x4616
cmpl $0x0, 0x1bf9f(%rip)     # 0x20300
jle 0x4566
leaq 0xdc51(%rip), %rdi      # 0x11fbf
leaq 0xd06a(%rip), %rsi      # 0x113df
movl $0x578, %edx            # imm = 0x578
xorl %eax, %eax
callq 0xc2a1
pushq $0x1
popq %r12
cmpl $0x0, 0x1bf74(%rip)     # 0x20300
jle 0x43b0
leaq 0xdccd(%rip), %rdi      # 0x12062
leaq 0xd043(%rip), %rsi      # 0x113df
leaq 0x140(%rsp), %rcx
movl $0x595, %edx            # imm = 0x595
xorl %eax, %eax
callq 0xc2a1
movq 0x10(%rsp), %rdi
callq 0x20f0
testl %r12d, %r12d
je 0x44bd
cmpl $0x0, 0x1bf36(%rip)     # 0x20300
jle 0x43e6
leaq 0xdce6(%rip), %rdi      # 0x120b9
leaq 0xd005(%rip), %rsi      # 0x113df
movl $0x5a1, %edx            # imm = 0x5A1
xorl %eax, %eax
callq 0xc2a1
xorl %r12d, %r12d
testq %r14, %r14
jne 0x4417
movl $0xf1, %r12d
cmpl $0x0, 0x1bf05(%rip)     # 0x20300
jle 0x4417
leaq 0xdcdb(%rip), %rdi      # 0x120df
leaq 0xcfd4(%rip), %rsi      # 0x113df
movl $0x5a9, %edx            # imm = 0x5A9
xorl %eax, %eax
callq 0xc2a1
testl %r13d, %r13d
je 0x45f0
leaq 0xcfe5(%rip), %rdx      # 0x1140c
leaq 0x40(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq 0x38(%rsp), %r13
movq %r13, %rcx
movq 0x20(%rsp), %rbx
movq %rbx, %r8
xorl %eax, %eax
callq 0x2140
movq 0x28(%rsp), %r15
leaq 0x17b90(%r15), %r14
movb $0x0, 0x17b90(%r15)
cmpl $0x0, 0x1be9d(%rip)     # 0x20300
jle 0x4482
leaq 0xdcce(%rip), %rdi      # 0x1213a
leaq 0xcf6c(%rip), %rsi      # 0x113df
movl $0x5b9, %edx            # imm = 0x5B9
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
leaq 0x40(%rsp), %rdx
pushq $0x1
popq %r9
xorl %edi, %edi
movq %r13, %rsi
movq %r14, %rcx
movl $0x1001, %r8d           # imm = 0x1001
callq 0xd3fd
cmpl $-0x1, %eax
je 0x44c6
movl %eax, %r12d
testl %eax, %eax
jle 0x4510
addl %r12d, 0x1c664(%rip)    # 0x20b1c
xorl %r12d, %r12d
jmp 0x4510
pushq $0x64
popq %r12
jmp 0x4616
leaq 0x2a4e3(%rip), %rax     # 0x2e9b0
movl (%rax), %ecx
leal -0x64(%rcx), %eax
cmpl $0x2, %eax
jb 0x44e9
cmpl $0x66, %ecx
je 0x462b
cmpl $0x67, %ecx
jne 0x4647
movl 0x1be11(%rip), %ebx     # 0x20300
testl %ebx, %ebx
jle 0x456f
leaq 0xd82c(%rip), %rdi      # 0x11d26
leaq 0xcede(%rip), %rsi      # 0x113df
xorl %r12d, %r12d
movl $0x5cb, %edx            # imm = 0x5CB
xorl %eax, %eax
callq 0xc2a1
movl 0x1bdea(%rip), %ebx     # 0x20300
testl %ebx, %ebx
jle 0x4572
leaq 0xdc5d(%rip), %rdi      # 0x1217e
leaq 0xceb7(%rip), %rsi      # 0x113df
movl $0x5d8, %edx            # imm = 0x5D8
movq %r15, %rcx
xorl %eax, %eax
callq 0xc2a1
movl 0x1bdc3(%rip), %ebx     # 0x20300
testl %ebx, %ebx
jle 0x4572
leaq 0xdc60(%rip), %rdi      # 0x121a8
leaq 0xce90(%rip), %rsi      # 0x113df
movl $0x5d9, %edx            # imm = 0x5D9
movq %r14, %rcx
xorl %eax, %eax
callq 0xc2a1
movl 0x1bd9c(%rip), %ebx     # 0x20300
jmp 0x4572
pushq $0x1
popq %r12
jmp 0x43b0
xorl %r12d, %r12d
leaq 0xd887(%rip), %rsi      # 0x11e00
pushq $0xb
popq %rdx
movq %r14, %rdi
callq 0x22b0
testl %eax, %eax
jne 0x45d2
testl %ebx, %ebx
jle 0x45a6
leaq 0xdc4c(%rip), %rdi      # 0x121df
leaq 0xce45(%rip), %rsi      # 0x113df
movl $0x5dc, %edx            # imm = 0x5DC
xorl %eax, %eax
callq 0xc2a1
leaq 0x12ab3(%rip), %rdx     # 0x17060
movl $0x80, %esi
movq 0x20(%rsp), %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2140
movq %r13, %rdi
movq %r15, %rsi
callq 0x3afe
movl 0x1bd2e(%rip), %ebx     # 0x20300
testl %ebx, %ebx
jle 0x45f0
leaq 0xdc36(%rip), %rdi      # 0x12213
leaq 0xcdfb(%rip), %rsi      # 0x113df
movl $0x5e0, %edx            # imm = 0x5E0
xorl %eax, %eax
callq 0xc2a1
cmpl $0x0, 0x1bd09(%rip)     # 0x20300
jle 0x4616
leaq 0xdc55(%rip), %rdi      # 0x12255
leaq 0xcdd8(%rip), %rsi      # 0x113df
movl $0x5e2, %edx            # imm = 0x5E2
movl %r12d, %ecx
xorl %eax, %eax
callq 0xc2a1
movl %r12d, %eax
addq $0x948, %rsp            # imm = 0x948
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd735(%rip), %rdi      # 0x11d67
leaq 0xcda6(%rip), %rsi      # 0x113df
movl $0x5cf, %edx            # imm = 0x5CF
xorl %eax, %eax
callq 0xc2a1
jmp 0x4661
leaq 0xd766(%rip), %rdi      # 0x11db4
leaq 0xcd8a(%rip), %rsi      # 0x113df
movl $0x5d3, %edx            # imm = 0x5D3
xorl %eax, %eax
callq 0xc2a1
pushq $-0x1
jmp 0x44bf