// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movb $0x2d, -0x31(%rbp)
cmpl $0x1, 0x23903(%rip)     # 0x35930
jl 0x1204d
movq -0x8(%rbp), %rcx
leaq 0xcbb6(%rip), %rdi      # 0x1ebf0
leaq 0xcaf0(%rip), %rsi      # 0x1eb31
movl $0x16e, %edx            # imm = 0x16E
movb $0x0, %al
callq 0x117c0
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2290
movq %rax, -0x30(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xcb8b(%rip), %rsi      # 0x1ec20
callq 0x23b0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x126d6
movq -0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x1, 0x23878(%rip)     # 0x35930
jl 0x120d8
movq -0x58(%rbp), %rcx
leaq 0xcb5e(%rip), %rdi      # 0x1ec23
leaq 0xca65(%rip), %rsi      # 0x1eb31
movl $0x185, %edx            # imm = 0x185
movb $0x0, %al
callq 0x117c0
movq -0x58(%rbp), %rdi
addq $0x2, %rdi
movl $0x3f, %esi
callq 0x2110
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x1210b
movq -0x60(%rbp), %rdi
addq $0x1, %rdi
movl $0x3f, %esi
callq 0x2110
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x1212a
movq -0x60(%rbp), %rdi
addq $0x1, %rdi
leaq 0xcb31(%rip), %rsi      # 0x1ec52
callq 0x22e0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x12149
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3f, %eax
jne 0x12149
movq -0x60(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x126d4
cmpq $0x0, -0x60(%rbp)
je 0x126d4
movb $0x0, -0x71(%rbp)
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0x3f, %esi
callq 0x2110
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x1218f
movq -0x70(%rbp), %rax
movb $0x0, (%rax)
leaq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x10, %esi
leaq 0xce07(%rip), %rdx      # 0x1efaa
movb $0x0, %al
callq 0x2130
cmpl $0x1, 0x2377f(%rip)     # 0x35930
jl 0x121d1
leaq -0x50(%rbp), %rcx
leaq 0xca9a(%rip), %rdi      # 0x1ec58
leaq 0xc96c(%rip), %rsi      # 0x1eb31
movl $0x197, %edx            # imm = 0x197
movb $0x0, %al
callq 0x117c0
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0x3f, %esi
callq 0x2110
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movb (%rax), %al
movb %al, -0x31(%rbp)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpl $0x1, 0x23725(%rip)     # 0x35930
jl 0x1222b
movsbl -0x31(%rbp), %ecx
leaq 0xca74(%rip), %rdi      # 0x1ec8c
leaq 0xc912(%rip), %rsi      # 0x1eb31
movl $0x1a1, %edx            # imm = 0x1A1
movb $0x0, %al
callq 0x117c0
movq -0x28(%rbp), %rdi
leaq 0xca8f(%rip), %rsi      # 0x1ecc5
callq 0x22e0
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x1226e
movq -0x70(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3f, %eax
je 0x12267
movq -0x70(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3b, %eax
je 0x12267
movq -0x70(%rbp), %rax
movb (%rax), %al
movb %al, -0x71(%rbp)
movq -0x70(%rbp), %rax
movb $0x0, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x126d2
cmpl $0x1, 0x236b0(%rip)     # 0x35930
jl 0x122a0
movq -0x28(%rbp), %rcx
leaq 0xca3d(%rip), %rdi      # 0x1ecca
leaq 0xc89d(%rip), %rsi      # 0x1eb31
movl $0x1b2, %edx            # imm = 0x1B2
movb $0x0, %al
callq 0x117c0
movsbl -0x31(%rbp), %eax
movl %eax, -0x84(%rbp)
subl $0x42, %eax
je 0x12311
jmp 0x122b1
movl -0x84(%rbp), %eax
subl $0x51, %eax
je 0x122d8
jmp 0x122be
movl -0x84(%rbp), %eax
subl $0x62, %eax
je 0x12311
jmp 0x122cb
movl -0x84(%rbp), %eax
subl $0x71, %eax
jne 0x1234a
jmp 0x122d8
cmpl $0x1, 0x23651(%rip)     # 0x35930
jl 0x122ff
movq -0x28(%rbp), %rcx
leaq 0xca15(%rip), %rdi      # 0x1ed01
leaq 0xc83e(%rip), %rsi      # 0x1eb31
movl $0x1b7, %edx            # imm = 0x1B7
movb $0x0, %al
callq 0x117c0
movq -0x28(%rbp), %rdi
callq 0x11fb0
movl $0x1, -0x64(%rbp)
jmp 0x12373
cmpl $0x1, 0x23618(%rip)     # 0x35930
jl 0x12338
movq -0x28(%rbp), %rcx
leaq 0xca2b(%rip), %rdi      # 0x1ed50
leaq 0xc805(%rip), %rsi      # 0x1eb31
movl $0x1be, %edx            # imm = 0x1BE
movb $0x0, %al
callq 0x117c0
movq -0x28(%rbp), %rdi
callq 0x11b20
movl $0x1, -0x64(%rbp)
jmp 0x12373
cmpl $0x0, 0x235e3(%rip)     # 0x35934
je 0x12371
movsbl -0x31(%rbp), %ecx
leaq 0xca37(%rip), %rdi      # 0x1ed95
leaq 0xc7cc(%rip), %rsi      # 0x1eb31
movl $0x1c4, %edx            # imm = 0x1C4
movb $0x0, %al
callq 0x117c0
jmp 0x12373
cmpl $0x1, -0x64(%rbp)
jne 0x126d0
cmpl $0x1, 0x235ac(%rip)     # 0x35930
jl 0x123a4
movq -0x28(%rbp), %rcx
leaq 0xca61(%rip), %rdi      # 0x1edf2
leaq 0xc799(%rip), %rsi      # 0x1eb31
movl $0x1cb, %edx            # imm = 0x1CB
movb $0x0, %al
callq 0x117c0
movq -0x58(%rbp), %rax
movb $0x0, (%rax)
cmpq $0x0, -0x70(%rbp)
je 0x124c0
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
cmpl $0x1, 0x23567(%rip)     # 0x35930
jl 0x123e9
movq -0x60(%rbp), %rcx
leaq 0xca53(%rip), %rdi      # 0x1ee29
leaq 0xc754(%rip), %rsi      # 0x1eb31
movl $0x1d8, %edx            # imm = 0x1D8
movb $0x0, %al
callq 0x117c0
jmp 0x123eb
movq -0x60(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x3f, %ecx
movb %al, -0x85(%rbp)
je 0x12412
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
sete %al
movb %al, -0x85(%rbp)
movb -0x85(%rbp), %al
testb $0x1, %al
jne 0x1241e
jmp 0x1242c
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x123eb
cmpl $0x1, 0x234fd(%rip)     # 0x35930
jl 0x12453
movq -0x60(%rbp), %rcx
leaq 0xc9e9(%rip), %rdi      # 0x1ee29
leaq 0xc6ea(%rip), %rsi      # 0x1eb31
movl $0x1da, %edx            # imm = 0x1DA
movb $0x0, %al
callq 0x117c0
movq -0x60(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, -0x86(%rbp)
je 0x12482
movq -0x80(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x9, %eax
sete %al
movb %al, -0x86(%rbp)
movb -0x86(%rbp), %al
testb $0x1, %al
jne 0x1248e
jmp 0x1249c
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x1245b
movq -0x80(%rbp), %rdi
leaq 0xc779(%rip), %rsi      # 0x1ec20
movl $0x2, %edx
callq 0x2090
cmpl $0x0, %eax
jne 0x124be
movq -0x80(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x124c8
movq $0x0, -0x60(%rbp)
cmpl $0x1, 0x23461(%rip)     # 0x35930
jl 0x124ef
movq -0x60(%rbp), %rcx
leaq 0xc97e(%rip), %rdi      # 0x1ee5a
leaq 0xc64e(%rip), %rsi      # 0x1eb31
movl $0x1e2, %edx            # imm = 0x1E2
movb $0x0, %al
callq 0x117c0
movsbl -0x71(%rbp), %eax
cmpl $0x0, %eax
je 0x125e5
cmpl $0x1, 0x2342d(%rip)     # 0x35930
jl 0x12523
movsbl -0x71(%rbp), %ecx
leaq 0xc97b(%rip), %rdi      # 0x1ee8b
leaq 0xc61a(%rip), %rsi      # 0x1eb31
movl $0x1ed, %edx            # imm = 0x1ED
movb $0x0, %al
callq 0x117c0
cmpl $0x1, 0x23406(%rip)     # 0x35930
jl 0x1254a
movq -0x60(%rbp), %rcx
leaq 0xc9a1(%rip), %rdi      # 0x1eed8
leaq 0xc5f3(%rip), %rsi      # 0x1eb31
movl $0x1ee, %edx            # imm = 0x1EE
movb $0x0, %al
callq 0x117c0
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
movslq -0xc(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x98(%rbp)
movsbl -0x71(%rbp), %eax
movl %eax, -0x8c(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x12594
movq -0x60(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x125a4
leaq 0xaebf(%rip), %rax      # 0x1d45a
movq %rax, -0xb8(%rbp)
jmp 0x125a4
movl -0x8c(%rbp), %r9d
movq -0x98(%rbp), %r8
movq -0xa0(%rbp), %rcx
movq -0xa8(%rbp), %rsi
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rax
leaq 0xc94b(%rip), %rdx      # 0x1ef20
movq %rax, (%rsp)
movb $0x0, %al
callq 0x2130
jmp 0x1268d
cmpl $0x1, 0x23344(%rip)     # 0x35930
jl 0x1260c
movsbl -0x71(%rbp), %ecx
leaq 0xc930(%rip), %rdi      # 0x1ef29
leaq 0xc531(%rip), %rsi      # 0x1eb31
movl $0x1f1, %edx            # imm = 0x1F1
movb $0x0, %al
callq 0x117c0
movq -0x30(%rbp), %rax
movq %rax, -0xd8(%rbp)
movslq -0xc(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x1264c
movq -0x60(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x1265c
leaq 0xae07(%rip), %rax      # 0x1d45a
movq %rax, -0xe0(%rbp)
jmp 0x1265c
movq -0xc0(%rbp), %r8
movq -0xc8(%rbp), %rcx
movq -0xd0(%rbp), %rsi
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %r9
leaq 0xc8eb(%rip), %rdx      # 0x1ef71
movb $0x0, %al
callq 0x2130
movq -0x8(%rbp), %rdi
movslq -0xc(%rbp), %rsi
movq -0x30(%rbp), %rcx
leaq 0xc90a(%rip), %rdx      # 0x1efaa
movb $0x0, %al
callq 0x2130
cmpl $0x1, 0x23282(%rip)     # 0x35930
jl 0x126ce
movq -0x8(%rbp), %rcx
leaq 0xc8bd(%rip), %rdi      # 0x1ef78
leaq 0xc46f(%rip), %rsi      # 0x1eb31
movl $0x1f7, %edx            # imm = 0x1F7
movb $0x0, %al
callq 0x117c0
jmp 0x126d0
jmp 0x126d2
jmp 0x126d4
jmp 0x126d6
jmp 0x126d8
cmpl $0x1, -0x64(%rbp)
je 0x12083
cmpq $0x0, -0x30(%rbp)
je 0x126f2
movq -0x30(%rbp), %rdi
callq 0x2030
xorl %eax, %eax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)