// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x970, %rsp            # imm = 0x970
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
leaq -0x440(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x2150
leaq -0x840(%rbp), %rax
movq %rax, -0x848(%rbp)
movl $0x0, -0x84c(%rbp)
movl $0x0, -0x850(%rbp)
movl $0x0, -0x854(%rbp)
leaq -0x440(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
addq $0x5efd, %r8            # imm = 0x5EFD
movl $0x400, %esi            # imm = 0x400
leaq 0x14363(%rip), %rdx     # 0x194fc
movb $0x0, %al
callq 0x2130
cmpl $0x1, 0x22549(%rip)     # 0x276f0
jl 0x51d4
movq -0x20(%rbp), %rax
movl 0x166f8(%rax), %ecx
leaq -0x440(%rbp), %r8
leaq 0x14b87(%rip), %rdi     # 0x19d48
leaq 0x14307(%rip), %rsi     # 0x194cf
movl $0x55a, %edx            # imm = 0x55A
movb $0x0, %al
callq 0x117c0
cmpq $0x0, -0x10(%rbp)
jne 0x5201
leaq 0x14ba7(%rip), %rdi     # 0x19d89
leaq 0x142e6(%rip), %rsi     # 0x194cf
movl $0x55f, %edx            # imm = 0x55F
movb $0x0, %al
callq 0x117c0
movl $0xde, -0x4(%rbp)
jmp 0x57ea
cmpq $0x0, -0x10(%rbp)
je 0x54f5
leaq -0x440(%rbp), %rdi
leaq 0x14bb1(%rip), %rsi     # 0x19dcb
callq 0x2330
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x5257
leaq -0x440(%rbp), %rcx
leaq 0x14b95(%rip), %rdi     # 0x19dcd
leaq 0x14290(%rip), %rsi     # 0x194cf
movl $0x569, %edx            # imm = 0x569
movb $0x0, %al
callq 0x117c0
movl $0xdd, -0x4(%rbp)
jmp 0x57ea
jmp 0x5259
leaq -0x840(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x3ff, %esi            # imm = 0x3FF
callq 0x9ab0
movq %rax, %rcx
movq %rcx, -0x848(%rbp)
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0x961(%rbp)
je 0x5294
cmpq $0x0, -0x30(%rbp)
setne %al
movb %al, -0x961(%rbp)
movb -0x961(%rbp), %al
testb $0x1, %al
jne 0x52a3
jmp 0x5478
leaq -0x840(%rbp), %rdi
callq 0x2100
movl %eax, -0x858(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x1, 0x2242b(%rip)     # 0x276f0
jl 0x52ee
movl -0x858(%rbp), %ecx
leaq -0x840(%rbp), %r8
leaq 0x14b2b(%rip), %rdi     # 0x19e06
leaq 0x141ed(%rip), %rsi     # 0x194cf
movl $0x571, %edx            # imm = 0x571
movb $0x0, %al
callq 0x117c0
movsbl -0x840(%rbp), %eax
cmpl $0x2d, %eax
jne 0x5380
cmpl $0x1, 0x223eb(%rip)     # 0x276f0
jl 0x5321
leaq 0x14b26(%rip), %rdi     # 0x19e34
leaq 0x141ba(%rip), %rsi     # 0x194cf
movl $0x575, %edx            # imm = 0x575
movb $0x0, %al
callq 0x117c0
callq 0x12ba0
cmpl $0x0, %eax
jle 0x537e
leaq -0x840(%rbp), %rdi
movl -0x858(%rbp), %esi
callq 0x12d80
cmpl $0x0, %eax
je 0x537e
cmpl $0x1, 0x223a7(%rip)     # 0x276f0
jl 0x5365
leaq 0x14b0d(%rip), %rdi     # 0x19e5f
leaq 0x14176(%rip), %rsi     # 0x194cf
movl $0x578, %edx            # imm = 0x578
movb $0x0, %al
callq 0x117c0
movl $0x1, -0x84c(%rbp)
movl $0x0, -0x850(%rbp)
jmp 0x5478
jmp 0x5380
cmpl $0x0, -0x84c(%rbp)
jne 0x5450
movq -0x20(%rbp), %rax
cmpl $0x67, 0x166f8(%rax)
jne 0x53f4
cmpl $0x1, 0x2234f(%rip)     # 0x276f0
jl 0x53bd
leaq 0x14ab5(%rip), %rdi     # 0x19e5f
leaq 0x1411e(%rip), %rsi     # 0x194cf
movl $0x584, %edx            # imm = 0x584
movb $0x0, %al
callq 0x117c0
leaq -0x840(%rbp), %rdi
callq 0x11f70
movl %eax, -0x854(%rbp)
leaq -0x840(%rbp), %rdi
movslq -0x854(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl $0x1, %esi
callq 0x2380
movq %rax, -0x860(%rbp)
jmp 0x540d
movq -0x30(%rbp), %rdi
leaq -0x840(%rbp), %rdx
leaq 0x19ba4(%rip), %rsi     # 0x1efaa
movb $0x0, %al
callq 0x2200
cmpl $0x0, -0x38(%rbp)
jne 0x544e
leaq -0x840(%rbp), %rdi
callq 0x131a0
cmpl $0x0, %eax
je 0x544e
cmpl $0x1, 0x222c5(%rip)     # 0x276f0
jl 0x5447
leaq 0x14a60(%rip), %rdi     # 0x19e94
leaq 0x14094(%rip), %rsi     # 0x194cf
movl $0x58e, %edx            # imm = 0x58E
movb $0x0, %al
callq 0x117c0
movl $0x1, -0x38(%rbp)
jmp 0x5450
cmpl $0x1, 0x22299(%rip)     # 0x276f0
jl 0x5473
leaq 0x14a73(%rip), %rdi     # 0x19ed3
leaq 0x14068(%rip), %rsi     # 0x194cf
movl $0x593, %edx            # imm = 0x593
movb $0x0, %al
callq 0x117c0
jmp 0x5259
cmpl $0x1, 0x22271(%rip)     # 0x276f0
jl 0x54a2
leaq -0x440(%rbp), %rcx
leaq 0x14a73(%rip), %rdi     # 0x19f02
leaq 0x14039(%rip), %rsi     # 0x194cf
movl $0x595, %edx            # imm = 0x595
movb $0x0, %al
callq 0x117c0
cmpq $0x0, -0x30(%rbp)
je 0x54b2
movq -0x30(%rbp), %rdi
callq 0x20e0
cmpl $0x0, -0x34(%rbp)
jne 0x54d0
movl $0x64, -0x850(%rbp)
movl -0x850(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x57ea
cmpl $0x1, 0x22219(%rip)     # 0x276f0
jl 0x54f3
leaq 0x14a79(%rip), %rdi     # 0x19f59
leaq 0x13fe8(%rip), %rsi     # 0x194cf
movl $0x5a1, %edx            # imm = 0x5A1
movb $0x0, %al
callq 0x117c0
jmp 0x54f5
cmpq $0x0, -0x848(%rbp)
jne 0x552c
cmpl $0x1, 0x221ea(%rip)     # 0x276f0
jl 0x5522
leaq 0x14a70(%rip), %rdi     # 0x19f7f
leaq 0x13fb9(%rip), %rsi     # 0x194cf
movl $0x5a9, %edx            # imm = 0x5A9
movb $0x0, %al
callq 0x117c0
movl $0xf1, -0x850(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x57b8
leaq -0x960(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
addq $0x5efd, %r8            # imm = 0x5EFD
movl $0x100, %esi            # imm = 0x100
leaq 0x13fa4(%rip), %rdx     # 0x194fc
movb $0x0, %al
callq 0x2130
movq -0x20(%rbp), %rax
movb $0x0, 0x17b90(%rax)
cmpl $0x1, 0x2217f(%rip)     # 0x276f0
jl 0x5598
movq -0x20(%rbp), %rcx
addq $0x5efd, %rcx           # imm = 0x5EFD
leaq 0x14a55(%rip), %rdi     # 0x19fda
leaq 0x13f43(%rip), %rsi     # 0x194cf
movl $0x5b9, %edx            # imm = 0x5B9
movb $0x0, %al
callq 0x117c0
movq -0x18(%rbp), %rsi
leaq -0x960(%rbp), %rdx
movq -0x20(%rbp), %rcx
addq $0x17b90, %rcx          # imm = 0x17B90
movl -0x24(%rbp), %eax
xorl %edi, %edi
movl $0x1001, %r8d           # imm = 0x1001
movl $0x1, %r9d
movl %eax, (%rsp)
callq 0x134d0
movl %eax, -0x850(%rbp)
cmpl $-0x1, -0x850(%rbp)
jne 0x569c
leaq 0x307af(%rip), %rax     # 0x35d90
movl (%rax), %eax
movl %eax, -0x968(%rbp)
addl $-0x64, %eax
subl $0x2, %eax
jb 0x560d
jmp 0x55f3
movl -0x968(%rbp), %eax
subl $0x66, %eax
je 0x5645
jmp 0x5600
movl -0x968(%rbp), %eax
subl $0x67, %eax
jne 0x566b
jmp 0x560d
cmpl $0x1, 0x220dc(%rip)     # 0x276f0
jl 0x5639
leaq 0x30773(%rip), %rax     # 0x35d90
movl (%rax), %ecx
leaq 0x145a0(%rip), %rdi     # 0x19bc6
leaq 0x13ea2(%rip), %rsi     # 0x194cf
movl $0x5cb, %edx            # imm = 0x5CB
movb $0x0, %al
callq 0x117c0
movl $0x0, -0x850(%rbp)
jmp 0x569a
leaq 0x145bb(%rip), %rdi     # 0x19c07
leaq 0x13e7c(%rip), %rsi     # 0x194cf
movl $0x5cf, %edx            # imm = 0x5CF
movb $0x0, %al
callq 0x117c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x57ea
leaq 0x3071e(%rip), %rax     # 0x35d90
movl (%rax), %ecx
leaq 0x145d9(%rip), %rdi     # 0x19c54
leaq 0x13e4d(%rip), %rsi     # 0x194cf
movl $0x5d3, %edx            # imm = 0x5D3
movb $0x0, %al
callq 0x117c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x57ea
jmp 0x569c
cmpl $0x0, -0x850(%rbp)
jle 0x56c1
movl -0x850(%rbp), %eax
addl 0x2285b(%rip), %eax     # 0x27f0c
movl %eax, 0x22855(%rip)     # 0x27f0c
movl $0x0, -0x850(%rbp)
cmpl $0x1, 0x22028(%rip)     # 0x276f0
jl 0x56e8
movq -0x20(%rbp), %rcx
leaq 0x14949(%rip), %rdi     # 0x1a01e
leaq 0x13df3(%rip), %rsi     # 0x194cf
movl $0x5d8, %edx            # imm = 0x5D8
movb $0x0, %al
callq 0x117c0
cmpl $0x1, 0x22001(%rip)     # 0x276f0
jl 0x5716
movq -0x20(%rbp), %rcx
addq $0x17b90, %rcx          # imm = 0x17B90
leaq 0x14945(%rip), %rdi     # 0x1a048
leaq 0x13dc5(%rip), %rsi     # 0x194cf
movl $0x5d9, %edx            # imm = 0x5D9
movb $0x0, %al
callq 0x117c0
movq -0x20(%rbp), %rdi
addq $0x17b90, %rdi          # imm = 0x17B90
leaq 0x14578(%rip), %rsi     # 0x19ca0
movl $0xb, %edx
callq 0x22a0
cmpl $0x0, %eax
jne 0x5793
cmpl $0x1, 0x21fb2(%rip)     # 0x276f0
jl 0x575a
leaq 0x14938(%rip), %rdi     # 0x1a07f
leaq 0x13d81(%rip), %rsi     # 0x194cf
movl $0x5dc, %edx            # imm = 0x5DC
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 0x1982e(%rip), %rdx     # 0x1efaa
movb $0x0, %al
callq 0x2130
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
callq 0x4970
cmpl $0x1, 0x21f56(%rip)     # 0x276f0
jl 0x57b6
leaq 0x14910(%rip), %rdi     # 0x1a0b3
leaq 0x13d25(%rip), %rsi     # 0x194cf
movl $0x5e0, %edx            # imm = 0x5E0
movb $0x0, %al
callq 0x117c0
jmp 0x57b8
cmpl $0x1, 0x21f31(%rip)     # 0x276f0
jl 0x57e1
movl -0x850(%rbp), %ecx
leaq 0x14927(%rip), %rdi     # 0x1a0f5
leaq 0x13cfa(%rip), %rsi     # 0x194cf
movl $0x5e2, %edx            # imm = 0x5E2
movb $0x0, %al
callq 0x117c0
movl -0x850(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x970, %rsp            # imm = 0x970
popq %rbp
retq
nopw %cs:(%rax,%rax)