// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/libmime-decoders.c
// Function: MDECODE_decode_ISO
int MDECODE_decode_ISO( char *isostring, int size )
{
    char *start_pair, *end_pair;
    char *iso, *iso_copy;
    char encoding_type='-';
    char encoding_charset[ MDECODE_ISO_CHARSET_SIZE_MAX ];
    char *iso_start, *iso_end;
    int iso_decoded;

    DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO-string='%s'",FL,isostring);

    // Process of decoding the ISO encoded string sequence.
    //  ( this process is repeated until we run out of ISO sequences )
    //
    //  1. Check that the string has a =? sequence within it ( indicates the start of the ISO encoding
    //  2. tokenise the sequence succeeding the =? token into its three (3) parts, namely the code-page, encoding-type and string respectively
    //  3. decode the string based on the encoding type, Q = Quoted-Printable, B = BASE64
    //

    iso_end = iso_start = NULL;
    start_pair = end_pair = NULL;
    iso_copy = malloc( sizeof(char) *( size +1 ) );
    do {
        iso_decoded = 0;
        start_pair = strstr( isostring, "=?" );
//      if ( start_pair ) end_pair   = strstr( start_pair +2, "?=" );
        if (( start_pair != NULL ))
        {
            iso_start = start_pair;
            // There's probably a better way of doing this, but, for us to find the end of this
            //  particular 'ISO' sequence, we need to hop past 3 more ?'s ( assuming we've already
            //  found the first one.
            DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO start = %s",FL,iso_start);

            iso_end = strchr( iso_start +strlen("=?"), '?' ); // Jump past the encoding
            if (iso_end) iso_end = strchr( iso_end +1, '?' ); // Jump past the Q or B
            if (iso_end) iso_end = strpbrk( iso_end +1, "?\n\r\t;" );  // dropped the SPACE here.
            if ((iso_end != NULL)&&(*iso_end == '?')) iso_end+=2;

            if ( (iso_start) && (iso_end) )
            {
                char *token_end;
                char restore_char='\0';

                // Copy the Encoding page/code.
                iso = iso_start +strlen("=?");

                token_end = strchr(iso,'?');
                if (token_end) *token_end = '\0';
                snprintf( encoding_charset, sizeof( encoding_charset ), "%s", iso);
                DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO char set = '%s'",FL,encoding_charset);

                iso = token_end +1;

                // Get the encoding _type_ (BASE64/QuotedPrintable etc)
                token_end = strchr(iso,'?');
                encoding_type = *iso;

                iso = token_end +1;

                DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO encoding char = '%c'",FL,encoding_type);

                // Get the encoded string
                token_end = strpbrk(iso,"?;\n\r"); //DROPPED THE SPACE here
                // When unfolding, WSP should be ignored. (https://tools.ietf.org/html/rfc5322#section-2.2.3)
                // WSP =  SP / HTAB ; white space (https://tools.ietf.org/html/rfc5234#appendix-B.1)
                if (token_end != NULL)
                {
                    if ((*token_end != '?')&&(*token_end != ';'))
                    {
                        restore_char = *token_end;
                    }
                    *token_end = '\0';
                }

                if (iso)
                {
                    DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Encoded String = '%s'\n", FL, iso );
                    switch ( encoding_type ) {

                        case MDECODE_ISO_ENCODING_Q:
                        case MDECODE_ISO_ENCODING_q:
                            DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Decoding filename using Quoted-Printable (%s)\n", FL, iso);
                            MDECODE_decode_qp_ISO(iso);
                            iso_decoded = 1;
                            break;

                        case MDECODE_ISO_ENCODING_B:
                        case MDECODE_ISO_ENCODING_b:
                            DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Decoding filename using BASE64 (%s)\n", FL, iso);
                            MDECODE_decode_short64( iso );
                            iso_decoded = 1;
                            break;

                        default:
                            if (glb.verbose) LOGGER_log("%s:%d:MDECODE_decode_ISO:ERROR: The encoding character '%c' is not a valid type of encoding\n", FL, encoding_type );
                    }

                    // If we decoded the string okay, then we need to recompose the string
                    if ( iso_decoded == 1 )
                    {
                        char *new_end_pos;
                        DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Decoded String = '%s'\n", FL, iso );
                        *iso_start = '\0'; // Terminate the original string before the start of the ISO data

                        // Because sometimes ISO strings are broken over multiple lines
                        //      due to wrapping requirements of RFC(2)822, we need to
                        //      sniff out these tab or spaces and crop them out of our
                        //      final ISO string.  We cannot simply search for the next
                        //      =? sequence using strstr() because it might traverse
                        //      beyond the end of the current 'line' (ie, \r\n termination)

                        if (token_end)
                        {
                            iso_end = token_end +1;
                            DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: iso_end = '%20s'",FL, iso_end);
                            while ((*iso_end == '?')||(*iso_end == '=')) iso_end++;
                            DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: iso_end = '%20s'",FL, iso_end);
                            new_end_pos = iso_end;
                            while ((*new_end_pos == ' ')||(*new_end_pos == '\t')) new_end_pos++;
                            if (strncmp(new_end_pos,"=?",2)==0) iso_end = new_end_pos;
                        } else {
                            iso_end = NULL;
                        }

                        DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO-END = '%20s'",FL,iso_end);

                        /** We now have the string split into 3 peices,
                          **    isostring = pointing to the start
                          **    iso = newly decoded string
                          **    iso_end = start of string after the non-decoded ISO portion
                          **/

                        /** Generate new string using the decoded ISO to a temporary string **/
                        if (restore_char != '\0')
                        {
                            DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Recomposing string with restore-char of '%c'",FL,restore_char);
                            DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO-end (start of end of string) is \n%s",FL,iso_end);
                            snprintf( iso_copy, size, "%s%s%c%s", isostring, iso, restore_char, (iso_end?iso_end:"") );
                        } else {
                            DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Recomposing string with NO restore-char",FL,restore_char);
                            snprintf( iso_copy, size, "%s%s%s", isostring, iso, (iso_end?iso_end:"") );
                        }

                        /** Switch the new headers over to the original headers again **/
                        snprintf( isostring, size, "%s", iso_copy );
                        DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: New ISO string = \n%s",FL,isostring);
                    }
                }
            }

        } // if (iso_start)
    }
    while (iso_decoded == 1 );
    if (iso_copy) free(iso_copy);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %ebx
movq %rdi, %r14
cmpl $0x0, 0x2284f(%rip)     # 0x2f540
jle 0xcd10
leaq 0xafac(%rip), %rdi      # 0x17ca6
leaq 0xaf43(%rip), %rsi      # 0x17c44
movl $0x16e, %edx            # imm = 0x16E
movq %r14, %rcx
xorl %eax, %eax
callq 0xc84f
leal 0x1(%rbx), %eax
movslq %eax, %rdi
callq 0x22a0
movq %rax, 0x10(%rsp)
leaq 0xafaf(%rip), %rsi      # 0x17cd6
movq %r14, %rdi
callq 0x23c0
testq %rax, %rax
je 0xd15c
movq %rax, %r12
movslq %ebx, %rax
movq %rax, 0x18(%rsp)
movl 0x227f7(%rip), %ebx     # 0x2f540
leaq 0xaef4(%rip), %r13      # 0x17c44
testl %ebx, %ebx
jle 0xcd6d
leaq 0xaf7e(%rip), %rdi      # 0x17cd9
movq %r13, %rsi
movl $0x185, %edx            # imm = 0x185
movq %r12, %rcx
xorl %eax, %eax
callq 0xc84f
leaq 0x2(%r12), %r15
movq %r15, %rdi
movl $0x3f, %esi
callq 0x2120
testq %rax, %rax
je 0xd15c
movq %rax, %rbp
leaq 0x1(%rax), %rbx
movq %rbx, %rdi
movl $0x3f, %esi
callq 0x2120
testq %rax, %rax
je 0xd15c
incq %rax
movq %rax, %rdi
leaq 0xaf56(%rip), %rsi      # 0x17d08
callq 0x22f0
testq %rax, %rax
je 0xd15c
movb $0x0, (%rbp)
movl $0x10, %esi
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
leaq 0xb288(%rip), %rdx      # 0x18060
movq %r15, %rcx
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x22757(%rip)     # 0x2f540
jle 0xce42
leaq 0xaf1c(%rip), %rdi      # 0x17d0e
movq %r13, %rsi
movl $0x197, %edx            # imm = 0x197
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc84f
movl 0x22735(%rip), %r15d    # 0x2f540
movq %rbx, %rdi
movl $0x3f, %esi
callq 0x2120
movq %rax, %rbp
incq %rbp
movq %r13, %rsi
movb (%rbx), %r13b
testl %r15d, %r15d
jle 0xce58
movsbl %r13b, %ecx
leaq 0xaf0e(%rip), %rdi      # 0x17d42
movl $0x1a1, %edx            # imm = 0x1A1
xorl %eax, %eax
callq 0xc84f
jmp 0xce58
movq %rbx, %rdi
movl $0x3f, %esi
callq 0x2120
movq %rax, %rbp
movb (%rbx), %r13b
incq %rbp
movq %rbp, %rdi
leaq 0xaf19(%rip), %rsi      # 0x17d7b
callq 0x22f0
movq %rax, %r15
testq %rax, %rax
je 0xce88
movzbl (%r15), %eax
xorl %ebx, %ebx
movl %eax, %ecx
orl $0x4, %ecx
cmpl $0x3f, %ecx
je 0xce82
movsbl %al, %ebx
movb $0x0, (%r15)
jmp 0xce8a
xorl %ebx, %ebx
cmpl $0x0, 0x226af(%rip)     # 0x2f540
jle 0xceb0
leaq 0xaee6(%rip), %rdi      # 0x17d80
leaq 0xada3(%rip), %rsi      # 0x17c44
movl $0x1b2, %edx            # imm = 0x1B2
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc84f
movzbl %r13b, %eax
cmpl $0x61, %eax
jg 0xcec8
cmpl $0x42, %eax
je 0xcf0d
cmpl $0x51, %eax
je 0xced6
jmp 0xd17c
cmpl $0x62, %eax
je 0xcf0d
cmpl $0x71, %eax
jne 0xd17c
cmpl $0x0, 0x22663(%rip)     # 0x2f540
jle 0xcefc
leaq 0xaed1(%rip), %rdi      # 0x17db7
leaq 0xad57(%rip), %rsi      # 0x17c44
movl $0x1b7, %edx            # imm = 0x1B7
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc84f
movq %rbp, %rdi
xorl %esi, %esi
movl $0x3d, %edx
callq 0xcb2a
jmp 0xcf3b
cmpl $0x0, 0x2262c(%rip)     # 0x2f540
jle 0xcf33
leaq 0xaee9(%rip), %rdi      # 0x17e06
leaq 0xad20(%rip), %rsi      # 0x17c44
movl $0x1be, %edx            # imm = 0x1BE
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc84f
movq %rbp, %rdi
callq 0xca31
movq 0x10(%rsp), %r13
movl 0x225fa(%rip), %eax     # 0x2f540
testl %eax, %eax
jle 0xcf6d
leaq 0xaf57(%rip), %rdi      # 0x17ea8
leaq 0xacec(%rip), %rsi      # 0x17c44
movl $0x1cb, %edx            # imm = 0x1CB
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc84f
movl 0x225d3(%rip), %eax     # 0x2f540
movb $0x0, (%r12)
testq %r15, %r15
je 0xd005
incq %r15
testl %eax, %eax
movq 0x18(%rsp), %r12
jle 0xcfa9
leaq 0xaf51(%rip), %rdi      # 0x17edf
leaq 0xacaf(%rip), %rsi      # 0x17c44
movl $0x1d8, %edx            # imm = 0x1D8
movq %r15, %rcx
xorl %eax, %eax
callq 0xc84f
jmp 0xcfa9
incq %r15
movzbl (%r15), %eax
orl $0x2, %eax
cmpl $0x3f, %eax
je 0xcfa6
cmpl $0x0, 0x22584(%rip)     # 0x2f540
jle 0xcfdb
leaq 0xaf1a(%rip), %rdi      # 0x17edf
leaq 0xac78(%rip), %rsi      # 0x17c44
movl $0x1da, %edx            # imm = 0x1DA
movq %r15, %rcx
xorl %eax, %eax
callq 0xc84f
movq %r15, %rax
movzbl (%rax), %ecx
cmpl $0x9, %ecx
je 0xcfeb
cmpl $0x20, %ecx
jne 0xcff0
incq %rax
jmp 0xcfde
cmpl $0x3d, %ecx
jne 0xcffd
cmpb $0x3f, 0x1(%rax)
cmoveq %rax, %r15
movl 0x2253d(%rip), %eax     # 0x2f540
jmp 0xd00d
xorl %r15d, %r15d
movq 0x18(%rsp), %r12
testl %eax, %eax
jle 0xd02e
leaq 0xaef8(%rip), %rdi      # 0x17f10
leaq 0xac25(%rip), %rsi      # 0x17c44
movl $0x1e2, %edx            # imm = 0x1E2
movq %r15, %rcx
xorl %eax, %eax
callq 0xc84f
movl 0x2250c(%rip), %eax     # 0x2f540
testl %ebx, %ebx
je 0xd0af
testl %eax, %eax
jle 0xd07e
leaq 0xaefe(%rip), %rdi      # 0x17f41
leaq 0xabfa(%rip), %rsi      # 0x17c44
movl $0x1ed, %edx            # imm = 0x1ED
movl %ebx, %ecx
xorl %eax, %eax
callq 0xc84f
cmpl $0x0, 0x224e1(%rip)     # 0x2f540
jle 0xd07e
leaq 0xaf26(%rip), %rdi      # 0x17f8e
leaq 0xabd5(%rip), %rsi      # 0x17c44
movl $0x1ee, %edx            # imm = 0x1EE
movq %r15, %rcx
xorl %eax, %eax
callq 0xc84f
testq %r15, %r15
leaq 0x9242(%rip), %rax      # 0x162ca
cmoveq %rax, %r15
movq %r15, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
leaq 0xaf39(%rip), %rdx      # 0x17fd6
movq %r14, %rcx
movq %rbp, %r8
movl %ebx, %r9d
xorl %eax, %eax
callq 0x2140
jmp 0xd0fa
testl %eax, %eax
jle 0xd0cf
leaq 0xaf25(%rip), %rdi      # 0x17fdf
leaq 0xab83(%rip), %rsi      # 0x17c44
movl $0x1f1, %edx            # imm = 0x1F1
xorl %ecx, %ecx
xorl %eax, %eax
callq 0xc84f
testq %r15, %r15
leaq 0x91f1(%rip), %rax      # 0x162ca
cmoveq %rax, %r15
movq %r13, %rdi
movq %r12, %rsi
leaq 0xaf3d(%rip), %rdx      # 0x18027
movq %r14, %rcx
movq %rbp, %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x2140
movq %r14, %rdi
movq %r12, %rsi
leaq 0xaf59(%rip), %rdx      # 0x18060
movq %r13, %rcx
xorl %eax, %eax
callq 0x2140
movl 0x22429(%rip), %ebx     # 0x2f540
testl %ebx, %ebx
leaq 0xab24(%rip), %r13      # 0x17c44
jle 0xd141
leaq 0xaf05(%rip), %rdi      # 0x1802e
movq %r13, %rsi
movl $0x1f7, %edx            # imm = 0x1F7
movq %r14, %rcx
xorl %eax, %eax
callq 0xc84f
movl 0x223ff(%rip), %ebx     # 0x2f540
movq %r14, %rdi
leaq 0xab8b(%rip), %rsi      # 0x17cd6
callq 0x23c0
movq %rax, %r12
testq %rax, %rax
jne 0xcd50
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xd16b
callq 0x2030
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x223c1(%rip)     # 0x2f544
je 0xd15c
movsbl %r13b, %ecx
leaq 0xacbb(%rip), %rdi      # 0x17e4b
leaq 0xaaad(%rip), %rsi      # 0x17c44
movl $0x1c4, %edx            # imm = 0x1C4
xorl %eax, %eax
callq 0xc84f
jmp 0xd15c
nopl (%rax)