// File: /workspace/github_c_makefile_star2_O3/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 $0x938, %rsp            # imm = 0x938
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x130(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
xorl %esi, %esi
callq 0x2170
leaq 0x5efd(%r15), %r13
leaq 0xe1c5(%rip), %rdx      # 0x1240c
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdi
movq %rbx, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x1d09d(%rip)     # 0x21300
jle 0x428e
movl 0x166f8(%r15), %ecx
leaq 0xec35(%rip), %rdi      # 0x12ea8
leaq 0xe165(%rip), %rsi      # 0x123df
leaq 0x130(%rsp), %r8
movl $0x55a, %edx            # imm = 0x55A
xorl %eax, %eax
callq 0xc84f
testq %r12, %r12
je 0x4451
leaq 0xec8d(%rip), %rsi      # 0x12f2b
leaq 0x130(%rsp), %rdi
callq 0x2340
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x4475
movq %rbx, 0x20(%rsp)
leaq 0x530(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0x7949
movb $0x1, %r14b
movq %rax, 0x28(%rsp)
testq %rax, %rax
movq %r13, 0x18(%rsp)
je 0x44aa
movl %ebp, 0xc(%rsp)
leaq 0x530(%rsp), %r14
leaq 0xe0e3(%rip), %rbp      # 0x123df
xorl %ebx, %ebx
movq %r15, %r13
movq %r14, %rdi
callq 0x2110
movq %rax, %r15
cmpl $0x0, 0x1cfed(%rip)     # 0x21300
jle 0x4331
leaq 0xec4a(%rip), %rdi      # 0x12f66
movq %rbp, %rsi
movl $0x571, %edx            # imm = 0x571
movl %r15d, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0xc84f
cmpb $0x2d, 0x530(%rsp)
jne 0x4376
cmpl $0x0, 0x1cfbe(%rip)     # 0x21300
jle 0x435a
leaq 0xec49(%rip), %rdi      # 0x12f94
movq %rbp, %rsi
movl $0x575, %edx            # imm = 0x575
xorl %eax, %eax
callq 0xc84f
callq 0xd496
testl %eax, %eax
jle 0x4376
movq %r14, %rdi
movl %r15d, %esi
callq 0xd609
testl %eax, %eax
jne 0x468f
movq %r13, %r15
cmpl $0x67, 0x166f8(%r13)
jne 0x43c1
cmpl $0x0, 0x1cf76(%rip)     # 0x21300
jle 0x43a2
leaq 0xec2c(%rip), %rdi      # 0x12fbf
movq %rbp, %rsi
movl $0x584, %edx            # imm = 0x584
xorl %eax, %eax
callq 0xc84f
movq %r14, %rdi
callq 0xcca5
movslq %eax, %rdx
movl $0x1, %esi
movq %r14, %rdi
movq 0x10(%rsp), %rcx
callq 0x2390
jmp 0x43ce
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x2160
testl %ebx, %ebx
movl $0x1, %ebx
je 0x4414
cmpl $0x0, 0x1cf22(%rip)     # 0x21300
jle 0x43f6
leaq 0xec4c(%rip), %rdi      # 0x13033
movq %rbp, %rsi
movl $0x593, %edx            # imm = 0x593
xorl %eax, %eax
callq 0xc84f
movq %r14, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0x7949
testq %rax, %rax
jne 0x42fe
jmp 0x44a1
movq %r14, %rdi
callq 0xd832
testl %eax, %eax
je 0x4446
cmpl $0x0, 0x1ced9(%rip)     # 0x21300
jle 0x444a
leaq 0xebc4(%rip), %rdi      # 0x12ff4
movq %rbp, %rsi
movl $0x58e, %edx            # imm = 0x58E
xorl %eax, %eax
callq 0xc84f
movl $0x1, %ebx
jmp 0x43d7
xorl %ebx, %ebx
jmp 0x43d7
movl $0x1, %ebx
jmp 0x43f6
leaq 0xea91(%rip), %rdi      # 0x12ee9
leaq 0xdf80(%rip), %rsi      # 0x123df
movl $0x55f, %edx            # imm = 0x55F
xorl %eax, %eax
callq 0xc84f
movl $0xde, %ebp
jmp 0x4783
leaq 0xeab1(%rip), %rdi      # 0x12f2d
leaq 0xdf5c(%rip), %rsi      # 0x123df
leaq 0x130(%rsp), %rcx
movl $0x569, %edx            # imm = 0x569
xorl %eax, %eax
callq 0xc84f
movl $0xdd, %ebp
jmp 0x4783
movl 0xc(%rsp), %ebp
movb $0x1, %r14b
jmp 0x44ac
xorl %ebx, %ebx
cmpl $0x0, 0x1ce4d(%rip)     # 0x21300
jle 0x44d7
leaq 0xeba6(%rip), %rdi      # 0x13062
leaq 0xdf1c(%rip), %rsi      # 0x123df
leaq 0x130(%rsp), %rcx
movl $0x595, %edx            # imm = 0x595
xorl %eax, %eax
callq 0xc84f
movq 0x10(%rsp), %rdi
callq 0x20f0
cmpq $0x0, 0x28(%rsp)
je 0x45de
movl %ebp, %r13d
cmpl $0x0, 0x1ce09(%rip)     # 0x21300
jle 0x4513
leaq 0xebb9(%rip), %rdi      # 0x130b9
leaq 0xded8(%rip), %rsi      # 0x123df
movl $0x5a1, %edx            # imm = 0x5A1
xorl %eax, %eax
callq 0xc84f
xorl %ebp, %ebp
testb %r14b, %r14b
je 0x4542
movl $0xf1, %ebp
cmpl $0x0, 0x1cdda(%rip)     # 0x21300
jle 0x4542
leaq 0xebb0(%rip), %rdi      # 0x130df
leaq 0xdea9(%rip), %rsi      # 0x123df
movl $0x5a9, %edx            # imm = 0x5A9
xorl %eax, %eax
callq 0xc84f
testl %ebx, %ebx
je 0x475e
leaq 0xdebb(%rip), %rdx      # 0x1240c
leaq 0x30(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq 0x20(%rsp), %rbx
movq %rbx, %rcx
movq 0x18(%rsp), %r12
movq %r12, %r8
xorl %eax, %eax
callq 0x2140
leaq 0x17b90(%r15), %r14
movb $0x0, 0x17b90(%r15)
cmpl $0x0, 0x1cd78(%rip)     # 0x21300
jle 0x45a7
leaq 0xeba9(%rip), %rdi      # 0x1313a
leaq 0xde47(%rip), %rsi      # 0x123df
movl $0x5b9, %edx            # imm = 0x5B9
movq %r12, %rcx
xorl %eax, %eax
callq 0xc84f
movl %r13d, (%rsp)
leaq 0x30(%rsp), %rdx
xorl %edi, %edi
movq %rbx, %rsi
movq %r14, %rcx
movl $0x1001, %r8d           # imm = 0x1001
movl $0x1, %r9d
callq 0xda45
cmpl $-0x1, %eax
je 0x45e8
movl %eax, %ebp
testl %eax, %eax
jle 0x4635
addl %ebp, 0x1d542(%rip)     # 0x21b1c
xorl %ebp, %ebp
jmp 0x4635
movl $0x64, %ebp
jmp 0x4783
leaq 0x2b3c1(%rip), %rax     # 0x2f9b0
movl (%rax), %ecx
leal -0x64(%rcx), %eax
cmpl $0x2, %eax
jb 0x460b
cmpl $0x66, %ecx
je 0x4797
cmpl $0x67, %ecx
jne 0x47b3
movl 0x1ccef(%rip), %ebx     # 0x21300
testl %ebx, %ebx
jle 0x46da
leaq 0xe706(%rip), %rdi      # 0x12d26
leaq 0xddb8(%rip), %rsi      # 0x123df
xorl %ebp, %ebp
movl $0x5cb, %edx            # imm = 0x5CB
xorl %eax, %eax
callq 0xc84f
movl 0x1ccc5(%rip), %ebx     # 0x21300
testl %ebx, %ebx
jle 0x46dc
leaq 0xeb34(%rip), %rdi      # 0x1317e
leaq 0xdd8e(%rip), %rsi      # 0x123df
movl $0x5d8, %edx            # imm = 0x5D8
movq %r15, %rcx
xorl %eax, %eax
callq 0xc84f
movl 0x1cc9a(%rip), %ebx     # 0x21300
testl %ebx, %ebx
jle 0x46dc
leaq 0xeb37(%rip), %rdi      # 0x131a8
leaq 0xdd67(%rip), %rsi      # 0x123df
movl $0x5d9, %edx            # imm = 0x5D9
movq %r14, %rcx
xorl %eax, %eax
callq 0xc84f
movl 0x1cc73(%rip), %ebx     # 0x21300
jmp 0x46dc
cmpl $0x0, 0x1cc6a(%rip)     # 0x21300
jle 0x46c1
leaq 0xe920(%rip), %rdi      # 0x12fbf
leaq 0xdd39(%rip), %rsi      # 0x123df
xorl %r14d, %r14d
movl $0x578, %edx            # imm = 0x578
xorl %eax, %eax
callq 0xc84f
movq %r13, %r15
movl 0xc(%rsp), %ebp
jmp 0x44ac
movq 0x10(%rsp), %rdi
callq 0x20f0
xorl %r14d, %r14d
movq %r13, %r15
movl 0xc(%rsp), %ebp
jmp 0x44ed
xorl %ebp, %ebp
leaq 0xe71d(%rip), %rsi      # 0x12e00
movl $0xb, %edx
movq %r14, %rdi
callq 0x22b0
testl %eax, %eax
jne 0x4740
testl %ebx, %ebx
jle 0x4712
leaq 0xeae0(%rip), %rdi      # 0x131df
leaq 0xdcd9(%rip), %rsi      # 0x123df
movl $0x5dc, %edx            # imm = 0x5DC
xorl %eax, %eax
callq 0xc84f
leaq 0x13947(%rip), %rdx     # 0x18060
movl $0x80, %esi
movq 0x18(%rsp), %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2140
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x3c24
movl 0x1cbc0(%rip), %ebx     # 0x21300
testl %ebx, %ebx
jle 0x475e
leaq 0xeac8(%rip), %rdi      # 0x13213
leaq 0xdc8d(%rip), %rsi      # 0x123df
movl $0x5e0, %edx            # imm = 0x5E0
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0x1cb9b(%rip)     # 0x21300
jle 0x4783
leaq 0xeae7(%rip), %rdi      # 0x13255
leaq 0xdc6a(%rip), %rsi      # 0x123df
movl $0x5e2, %edx            # imm = 0x5E2
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc84f
movl %ebp, %eax
addq $0x938, %rsp            # imm = 0x938
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe5c9(%rip), %rdi      # 0x12d67
leaq 0xdc3a(%rip), %rsi      # 0x123df
movl $0x5cf, %edx            # imm = 0x5CF
xorl %eax, %eax
callq 0xc84f
jmp 0x47cd
leaq 0xe5fa(%rip), %rdi      # 0x12db4
leaq 0xdc1e(%rip), %rsi      # 0x123df
movl $0x5d3, %edx            # imm = 0x5D3
xorl %eax, %eax
callq 0xc84f
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x4783