// File: /workspace/github_c_makefile_star2_O1/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, 0x28(%rsp)
leaq 0x140(%rsp), %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2170
leaq 0x5efd(%r14), %r12
leaq 0xe171(%rip), %rdx      # 0x1240c
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %r15, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x1d049(%rip)     # 0x21300
jle 0x42e2
movl 0x166f8(%r14), %ecx
leaq 0xebb1(%rip), %rdi      # 0x12e78
leaq 0xe111(%rip), %rsi      # 0x123df
leaq 0x140(%rsp), %r8
movl $0x55a, %edx            # imm = 0x55A
xorl %eax, %eax
callq 0xc960
cmpq $0x0, 0x28(%rsp)
je 0x44ee
leaq 0xec06(%rip), %rsi      # 0x12efb
leaq 0x140(%rsp), %rdi
callq 0x2350
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x4512
movl %ebp, 0x1c(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x20(%rsp)
leaq 0x540(%rsp), %r14
xorl %ebx, %ebx
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
movq %r14, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq 0x28(%rsp), %rdx
callq 0x7a09
movq %rax, %rbp
testq %rax, %rax
je 0x4544
movq %r14, %rdi
callq 0x2110
movq %rax, %r15
cmpl $0x0, 0x1cf98(%rip)     # 0x21300
jle 0x438a
leaq 0xebc5(%rip), %rdi      # 0x12f36
leaq 0xe067(%rip), %rsi      # 0x123df
movl $0x571, %edx            # imm = 0x571
movl %r15d, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0xc960
cmpb $0x2d, 0x540(%rsp)
jne 0x4406
cmpl $0x0, 0x1cf65(%rip)     # 0x21300
jle 0x43b7
leaq 0xebc0(%rip), %rdi      # 0x12f64
leaq 0xe034(%rip), %rsi      # 0x123df
movl $0x575, %edx            # imm = 0x575
xorl %eax, %eax
callq 0xc960
callq 0xd626
testl %eax, %eax
jle 0x4406
movq %r14, %rdi
movl %r15d, %esi
callq 0xd799
testl %eax, %eax
je 0x4406
movl $0x1, 0xc(%rsp)
cmpl $0x0, 0x1cf22(%rip)     # 0x21300
jle 0x44e4
xorl %r12d, %r12d
leaq 0xeba1(%rip), %rdi      # 0x12f8f
leaq 0xdfea(%rip), %rsi      # 0x123df
movl $0x578, %edx            # imm = 0x578
xorl %eax, %eax
callq 0xc960
jmp 0x44a2
cmpl $0x0, 0xc(%rsp)
je 0x4412
movl %ebx, %r15d
jmp 0x4479
movq 0x20(%rsp), %rax
cmpl $0x67, 0x166f8(%rax)
jne 0x4462
cmpl $0x0, 0x1ced9(%rip)     # 0x21300
jle 0x4443
leaq 0xeb5f(%rip), %rdi      # 0x12f8f
leaq 0xdfa8(%rip), %rsi      # 0x123df
movl $0x584, %edx            # imm = 0x584
xorl %eax, %eax
callq 0xc960
movq %r14, %rdi
callq 0xce1d
movslq %eax, %rdx
movl $0x1, %esi
movq %r14, %rdi
movq 0x10(%rsp), %rcx
callq 0x23a0
jmp 0x446f
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x2160
movl $0x1, %r15d
testl %ebx, %ebx
je 0x44b3
movb $0x1, %r12b
cmpl $0x0, 0x1ce7d(%rip)     # 0x21300
jle 0x449f
leaq 0xeb77(%rip), %rdi      # 0x13003
leaq 0xdf4c(%rip), %rsi      # 0x123df
movl $0x593, %edx            # imm = 0x593
xorl %eax, %eax
callq 0xc960
movl %r15d, %ebx
incl %r13d
testb %r12b, %r12b
jne 0x4338
jmp 0x453e
movq %r14, %rdi
callq 0xd9ce
testl %eax, %eax
je 0x44e9
cmpl $0x0, 0x1ce3a(%rip)     # 0x21300
jle 0x4479
leaq 0xeaf5(%rip), %rdi      # 0x12fc4
leaq 0xdf09(%rip), %rsi      # 0x123df
movl $0x58e, %edx            # imm = 0x58E
xorl %eax, %eax
callq 0xc960
jmp 0x4479
xorl %r12d, %r12d
jmp 0x44a2
xorl %r15d, %r15d
jmp 0x4479
leaq 0xe9c4(%rip), %rdi      # 0x12eb9
leaq 0xdee3(%rip), %rsi      # 0x123df
movl $0x55f, %edx            # imm = 0x55F
xorl %eax, %eax
callq 0xc960
movl $0xde, %eax
jmp 0x4825
leaq 0xe9e4(%rip), %rdi      # 0x12efd
leaq 0xdebf(%rip), %rsi      # 0x123df
leaq 0x140(%rsp), %rcx
movl $0x569, %edx            # imm = 0x569
xorl %eax, %eax
callq 0xc960
movl $0xdd, %eax
jmp 0x4825
movl $0x1, %r13d
cmpl $0x0, 0x1cdb5(%rip)     # 0x21300
jle 0x456f
leaq 0xeade(%rip), %rdi      # 0x13032
leaq 0xde84(%rip), %rsi      # 0x123df
leaq 0x140(%rsp), %rcx
movl $0x595, %edx            # imm = 0x595
xorl %eax, %eax
callq 0xc960
movq 0x10(%rsp), %rdi
callq 0x20f0
testl %r13d, %r13d
je 0x475b
cmpl $0x0, 0x1cd77(%rip)     # 0x21300
jle 0x45a5
leaq 0xeaf7(%rip), %rdi      # 0x13089
leaq 0xde46(%rip), %rsi      # 0x123df
movl $0x5a1, %edx            # imm = 0x5A1
xorl %eax, %eax
callq 0xc960
xorl %r15d, %r15d
testq %rbp, %rbp
movq 0x20(%rsp), %r12
movq 0x38(%rsp), %r13
movq 0x30(%rsp), %rbp
jne 0x45e5
movl $0xf1, %r15d
cmpl $0x0, 0x1cd37(%rip)     # 0x21300
jle 0x45e5
leaq 0xeadd(%rip), %rdi      # 0x130af
leaq 0xde06(%rip), %rsi      # 0x123df
movl $0x5a9, %edx            # imm = 0x5A9
xorl %eax, %eax
callq 0xc960
testl %ebx, %ebx
je 0x47fc
leaq 0xde18(%rip), %rdx      # 0x1240c
leaq 0x40(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r13, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x2140
leaq 0x17b90(%r12), %r14
movb $0x0, 0x17b90(%r12)
cmpl $0x0, 0x1ccdd(%rip)     # 0x21300
jle 0x4642
leaq 0xeade(%rip), %rdi      # 0x1310a
leaq 0xddac(%rip), %rsi      # 0x123df
movl $0x5b9, %edx            # imm = 0x5B9
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc960
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
leaq 0x40(%rsp), %rdx
xorl %edi, %edi
movq %r13, %rsi
movq %r14, %rcx
movl $0x1001, %r8d           # imm = 0x1001
movl $0x1, %r9d
callq 0xdbe8
movl %eax, %r15d
cmpl $-0x1, %eax
je 0x4765
testl %r15d, %r15d
jle 0x4682
addl %r15d, 0x1d49d(%rip)    # 0x21b1c
xorl %r15d, %r15d
cmpl $0x0, 0x1cc77(%rip)     # 0x21300
jle 0x46a8
leaq 0xeabc(%rip), %rdi      # 0x1314e
leaq 0xdd46(%rip), %rsi      # 0x123df
movl $0x5d8, %edx            # imm = 0x5D8
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
cmpl $0x0, 0x1cc51(%rip)     # 0x21300
jle 0x46ce
leaq 0xeac0(%rip), %rdi      # 0x13178
leaq 0xdd20(%rip), %rsi      # 0x123df
movl $0x5d9, %edx            # imm = 0x5D9
movq %r14, %rcx
xorl %eax, %eax
callq 0xc960
leaq 0xe6fb(%rip), %rsi      # 0x12dd0
movl $0xb, %edx
movq %r14, %rdi
callq 0x22c0
testl %eax, %eax
jne 0x472d
cmpl $0x0, 0x1cc13(%rip)     # 0x21300
jle 0x4709
leaq 0xeab9(%rip), %rdi      # 0x131af
leaq 0xdce2(%rip), %rsi      # 0x123df
movl $0x5dc, %edx            # imm = 0x5DC
xorl %eax, %eax
callq 0xc960
leaq 0x1397d(%rip), %rdx     # 0x1808d
movl $0x80, %esi
movq %rbp, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2140
movq %r13, %rdi
movq %r12, %rsi
callq 0x3c6b
movb $0x1, %bl
cmpl $0x0, 0x1cbca(%rip)     # 0x21300
jle 0x47f3
leaq 0xeaa0(%rip), %rdi      # 0x131e3
leaq 0xdc95(%rip), %rsi      # 0x123df
movl $0x5e0, %edx            # imm = 0x5E0
xorl %eax, %eax
callq 0xc960
jmp 0x47f3
movl $0x64, %eax
jmp 0x4825
leaq 0x2b244(%rip), %rax     # 0x2f9b0
movl (%rax), %ecx
leal -0x64(%rcx), %eax
cmpl $0x2, %eax
jb 0x4780
cmpl $0x66, %ecx
je 0x47b3
cmpl $0x67, %ecx
jne 0x47d1
cmpl $0x0, 0x1cb79(%rip)     # 0x21300
jle 0x47ab
leaq 0xe566(%rip), %rdi      # 0x12cf6
leaq 0xdc48(%rip), %rsi      # 0x123df
xorl %r15d, %r15d
movl $0x5cb, %edx            # imm = 0x5CB
xorl %eax, %eax
callq 0xc960
jmp 0x4673
xorl %r15d, %r15d
jmp 0x4673
leaq 0xe57d(%rip), %rdi      # 0x12d37
leaq 0xdc1e(%rip), %rsi      # 0x123df
xorl %ebx, %ebx
movl $0x5cf, %edx            # imm = 0x5CF
xorl %eax, %eax
callq 0xc960
jmp 0x47ed
leaq 0xe5ac(%rip), %rdi      # 0x12d84
leaq 0xdc00(%rip), %rsi      # 0x123df
xorl %ebx, %ebx
movl $0x5d3, %edx            # imm = 0x5D3
xorl %eax, %eax
callq 0xc960
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %bl, %bl
je 0x4825
cmpl $0x0, 0x1cafd(%rip)     # 0x21300
jle 0x4822
leaq 0xea19(%rip), %rdi      # 0x13225
leaq 0xdbcc(%rip), %rsi      # 0x123df
movl $0x5e2, %edx            # imm = 0x5E2
movl %r15d, %ecx
xorl %eax, %eax
callq 0xc960
movl %r15d, %eax
addq $0x948, %rsp            # imm = 0x948
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq