// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rdi, %rbp
cmpl $0x0, 0x226d6(%rip)     # 0x2f540
jle 0xce89
leaq 0xae60(%rip), %rdi      # 0x17cd3
leaq 0xad9a(%rip), %rsi      # 0x17c14
movl $0x16e, %edx            # imm = 0x16E
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc960
leal 0x1(%r14), %eax
movslq %eax, %rdi
callq 0x22b0
movq %rax, 0x8(%rsp)
movslq %r14d, %rax
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
leaq 0xae57(%rip), %rsi      # 0x17d03
callq 0x23d0
testq %rax, %rax
je 0xd30c
movq %rax, %r15
movq %rax, %rbx
cmpl $0x0, 0x22679(%rip)     # 0x2f540
jle 0xcee6
leaq 0xae36(%rip), %rdi      # 0x17d06
leaq 0xad3d(%rip), %rsi      # 0x17c14
movl $0x185, %edx            # imm = 0x185
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
addq $0x2, %r15
movq %r15, %rdi
movl $0x3f, %esi
callq 0x2120
movq %rax, %r13
testq %rax, %rax
je 0xcf12
leaq 0x1(%r13), %rdi
movl $0x3f, %esi
callq 0x2120
movq %rax, %rdi
jmp 0xcf14
xorl %edi, %edi
testq %rdi, %rdi
je 0xd30c
incq %rdi
leaq 0xae0e(%rip), %rsi      # 0x17d35
callq 0x2300
testq %rax, %rax
je 0xd30c
movq %rbx, 0x18(%rsp)
testq %r13, %r13
je 0xcf44
movb $0x0, (%r13)
movq %rbp, %r14
movl $0x10, %esi
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
leaq 0xb132(%rip), %rdx      # 0x1808d
movq %r15, %rcx
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x225d4(%rip)     # 0x2f540
jle 0xcf8b
leaq 0xadc6(%rip), %rdi      # 0x17d3b
leaq 0xac98(%rip), %rsi      # 0x17c14
movl $0x197, %edx            # imm = 0x197
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
movq %r13, %rdi
incq %rdi
movl $0x3f, %esi
callq 0x2120
movq %rax, %r12
movsbl 0x1(%r13), %ebp
cmpl $0x0, 0x22596(%rip)     # 0x2f540
jle 0xcfc8
leaq 0xadbc(%rip), %rdi      # 0x17d6f
leaq 0xac5a(%rip), %rsi      # 0x17c14
movl $0x1a1, %edx            # imm = 0x1A1
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc960
incq %r12
movq %r12, %rdi
leaq 0xadd3(%rip), %rsi      # 0x17da8
callq 0x2300
movq %rax, %r13
testq %rax, %rax
je 0xcfff
movzbl (%r13), %eax
xorl %r15d, %r15d
movl %eax, %ecx
orl $0x4, %ecx
cmpl $0x3f, %ecx
je 0xcff8
movsbl %al, %r15d
movb $0x0, (%r13)
jmp 0xd002
xorl %r15d, %r15d
cmpl $0x0, 0x22537(%rip)     # 0x2f540
jle 0xd028
leaq 0xad9b(%rip), %rdi      # 0x17dad
leaq 0xabfb(%rip), %rsi      # 0x17c14
movl $0x1b2, %edx            # imm = 0x1B2
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
movzbl %bpl, %eax
cmpl $0x61, %eax
jg 0xd068
cmpl $0x42, %eax
je 0xd0a9
cmpl $0x51, %eax
je 0xd072
cmpl $0x0, 0x22502(%rip)     # 0x2f544
je 0xd305
xorl %ebx, %ebx
leaq 0xae27(%rip), %rdi      # 0x17e78
leaq 0xabbc(%rip), %rsi      # 0x17c14
movl $0x1c4, %edx            # imm = 0x1C4
movl %ebp, %ecx
xorl %eax, %eax
callq 0xc960
jmp 0xd0d9
cmpl $0x62, %eax
je 0xd0a9
cmpl $0x71, %eax
jne 0xd03b
cmpl $0x0, 0x224c7(%rip)     # 0x2f540
jle 0xd098
leaq 0xad62(%rip), %rdi      # 0x17de4
leaq 0xab8b(%rip), %rsi      # 0x17c14
movl $0x1b7, %edx            # imm = 0x1B7
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
movq %r12, %rdi
xorl %esi, %esi
movl $0x3d, %edx
callq 0xcc3e
jmp 0xd0d7
cmpl $0x0, 0x22490(%rip)     # 0x2f540
jle 0xd0cf
leaq 0xad7a(%rip), %rdi      # 0x17e33
leaq 0xab54(%rip), %rsi      # 0x17c14
movl $0x1be, %edx            # imm = 0x1BE
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
movq %r12, %rdi
callq 0xcb45
movb $0x1, %bl
movq %r14, %rbp
testb %bl, %bl
movq 0x18(%rsp), %r14
je 0xd30c
cmpl $0x0, 0x22450(%rip)     # 0x2f540
jle 0xd10f
leaq 0xaddc(%rip), %rdi      # 0x17ed5
leaq 0xab14(%rip), %rsi      # 0x17c14
movl $0x1cb, %edx            # imm = 0x1CB
movq %r12, %rcx
xorl %eax, %eax
callq 0xc960
movb $0x0, (%r14)
testq %r13, %r13
je 0xd1af
incq %r13
cmpl $0x0, 0x2241a(%rip)     # 0x2f540
jle 0xd14a
leaq 0xaddd(%rip), %rdi      # 0x17f0c
leaq 0xaade(%rip), %rsi      # 0x17c14
movl $0x1d8, %edx            # imm = 0x1D8
movq %r13, %rcx
xorl %eax, %eax
callq 0xc960
jmp 0xd14a
incq %r13
movzbl (%r13), %eax
orl $0x2, %eax
cmpl $0x3f, %eax
je 0xd147
cmpl $0x0, 0x223e2(%rip)     # 0x2f540
jle 0xd17d
leaq 0xada5(%rip), %rdi      # 0x17f0c
leaq 0xaaa6(%rip), %rsi      # 0x17c14
movl $0x1da, %edx            # imm = 0x1DA
movq %r13, %rcx
xorl %eax, %eax
callq 0xc960
movq %r13, %r14
movzbl (%r14), %eax
cmpl $0x20, %eax
je 0xd18e
cmpl $0x9, %eax
jne 0xd193
incq %r14
jmp 0xd180
movl $0x2, %edx
movq %r14, %rdi
leaq 0xab61(%rip), %rsi      # 0x17d03
callq 0x20a0
testl %eax, %eax
cmoveq %r14, %r13
jmp 0xd1b2
xorl %r13d, %r13d
cmpl $0x0, 0x22387(%rip)     # 0x2f540
jle 0xd1d8
leaq 0xad7b(%rip), %rdi      # 0x17f3d
leaq 0xaa4b(%rip), %rsi      # 0x17c14
movl $0x1e2, %edx            # imm = 0x1E2
movq %r13, %rcx
xorl %eax, %eax
callq 0xc960
movl 0x22362(%rip), %eax     # 0x2f540
testl %r15d, %r15d
je 0xd269
testl %eax, %eax
jle 0xd208
leaq 0xad7c(%rip), %rdi      # 0x17f6e
leaq 0xaa1b(%rip), %rsi      # 0x17c14
movl $0x1ed, %edx            # imm = 0x1ED
movl %r15d, %ecx
xorl %eax, %eax
callq 0xc960
cmpl $0x0, 0x22331(%rip)     # 0x2f540
jle 0xd22e
leaq 0xada3(%rip), %rdi      # 0x17fbb
leaq 0xa9f5(%rip), %rsi      # 0x17c14
movl $0x1ee, %edx            # imm = 0x1EE
movq %r13, %rcx
xorl %eax, %eax
callq 0xc960
testq %r13, %r13
leaq 0x9062(%rip), %rax      # 0x1629a
cmoveq %rax, %r13
movq %r13, (%rsp)
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %r13
movq %r13, %rsi
leaq 0xadac(%rip), %rdx      # 0x18003
movq %rbp, %rcx
movq %r12, %r8
movl %r15d, %r9d
xorl %eax, %eax
callq 0x2140
jmp 0xd2be
testl %eax, %eax
jle 0xd289
leaq 0xad98(%rip), %rdi      # 0x1800c
leaq 0xa999(%rip), %rsi      # 0x17c14
movl $0x1f1, %edx            # imm = 0x1F1
xorl %ecx, %ecx
xorl %eax, %eax
callq 0xc960
testq %r13, %r13
leaq 0x9007(%rip), %rax      # 0x1629a
cmoveq %rax, %r13
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rsi
leaq 0xada9(%rip), %rdx      # 0x18054
movq %rbp, %rcx
movq %r12, %r8
movq %r13, %r9
movq %rsi, %r13
xorl %eax, %eax
callq 0x2140
movq %rbp, %rdi
movq %r13, %rsi
leaq 0xadc2(%rip), %rdx      # 0x1808d
movq %r14, %rcx
xorl %eax, %eax
callq 0x2140
cmpl $0x0, 0x22264(%rip)     # 0x2f540
jle 0xd2fb
leaq 0xad76(%rip), %rdi      # 0x1805b
leaq 0xa928(%rip), %rsi      # 0x17c14
movl $0x1f7, %edx            # imm = 0x1F7
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc960
testb %bl, %bl
jne 0xcea2
jmp 0xd30c
xorl %ebx, %ebx
jmp 0xd0d9
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xd31b
callq 0x2030
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq