// File: /workspace/github_c_makefile_star2_O0/inflex[P]ripMIME/MIME_headers.c
// Function: MIMEH_headers_process
int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers )
{
    /** scan through our headers string looking for information that is
     ** valid **/
    char *safeh, *h, *safehl;
    char *current_header_position;
    int headerlength;

    if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Start [hinfo=%p]\n",FL, hinfo);

    safeh = h = headers;

    /** Duplicate the headers for processing - this way we don't 'taint' the
     ** original headers during our searching / altering. **/

    headerlength = strlen(h);
    safehl = malloc(sizeof(char) *(headerlength+1));
    PLD_strncpy(safehl, h, headerlength+1);

    if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_parse_headers:DEBUG: Header length = %d\n", FL,headerlength);

    MIMEH_strip_comments(h);

    current_header_position = h;

    // Searching through the headers, we seek out header 'name:value;value;value' sets,
    //      Each set is then cleaned up, seperated and parsed.

    while ((current_header_position != NULL)&&( current_header_position <= (h +headerlength) ))
    {
        char *header_name, *header_value;
        char *header_name_end_position;
        char *header_value_end_position;

        DMIMEH LOGGER_log("%s:%d:MIMEH_headers_process:DEBUG: Processing '%s'",FL,current_header_position);

        /** Tokenise for the header 'name', ie, content-type, subject etc **/
        header_name = current_header_position;
        header_name_end_position = strpbrk( header_name, ":\t " );
        if (header_name_end_position == NULL)
        {
            // We couldn't find a terminating :, so, instead we try to find
            //  the first whitespace
            //
            // PLD:DEV:11/08/2004-15H27
            //  Issue: c030804-006a
            //
            // NOTE: this may activate on the true-blank lines, hence why we
            //      dump the source string, just for confirmation

            DMIMEH LOGGER_log("%s:%d:MIMEH_headers_process:DEBUG: Could not locate ':' separator, using whitespace (source='%s')",FL,header_name);
            header_name_end_position = strpbrk( header_name, "\t " );
            if (header_name_end_position == NULL)
            {
                DMIMEH LOGGER_log("%s:%d:MIMEH_headers_process:DEBUG: Cannot find a header name:value pair in '%s'",FL, header_name);
            }
        }

        // Seek forward from the start of our header, looking for the first occurance
        //      of the line end (implying the end of the current header name:value,
        //      we can do this because we know that when the headers were read in, we
        //      have already unfolded them, such that there should only be one header:value
        //      pairing per 'line'.

        current_header_position = strpbrk( current_header_position, "\n\r");
        if ( current_header_position == NULL )
        {
            // Theoretically, this should not happen, as headers are always
            //      terminated with a \n\r\n\r finishing byte sequence, thus
            //      if this _does_ happen, then we will simply jump out of the
            //      current iteration and let the loop try find another pairing
            //
            // There probably should be a logging entry here to indicate that
            //      "something strange happened"

            continue;
        } else {

            // Shuffle our CHP (current-header-position) pointer along the header
            //      data until it is no longer pointing to a \r or \n, this is so
            //      that when the next iteration of this loop comes around, it'll
            //      immediately be in the right place for starting the next parse

            while (( *current_header_position == '\n') ||( *current_header_position == '\r' )) current_header_position++;
        }

        if (( header_name_end_position == NULL )||( header_name_end_position > current_header_position))
        {
            // Some headers can contain various levels of non name/value pairings,
            //      while their presence could be debatable in terms of RFC validity
            //      we will 'ignore' them rather than throwing up our arms.  This
            //      ensures that we are not made to break over spurilous data.

            if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: This line contains no header:value pair (%s)", FL, current_header_position);
            continue;

        } else {

            // Get the header-value string and prepare to
            //      parse the data through our various parsing
            //      functions.

            header_value = header_name_end_position +1;
            header_value_end_position = strpbrk( header_value, "\n\r" );
            if ( header_value_end_position != NULL )
            {
                *header_name_end_position = '\0';
                *header_value_end_position = '\0';
                if (MIMEH_DNORMAL)
                {
                    LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Header Name ='%s'", FL, header_name );
                    LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Header Value='%s'", FL, header_value );
                }

                // To make parsing simpler, convert our
                //      header name to lowercase, that way
                //      we also reduce the CPU requirements for
                //      searching because pre-lowering the header-name
                //      occurs once, but string testing against it
                //      occurs multiple times ( at least once per parsing

                PLD_strlower( header_name );
                MIMEH_parse_subject( header_name, header_value, hinfo );
                MIMEH_parse_contenttype( header_name, header_value, hinfo );
                MIMEH_parse_contenttransferencoding( header_name, header_value, hinfo );
                MIMEH_parse_contentdisposition( header_name, header_value, hinfo );
                /** These items aren't really -imperative- to have, but they do
                 ** help with the sanity checking **/
                MIMEH_parse_date( header_name, header_value, hinfo );
                MIMEH_parse_from( header_name, header_value, hinfo );
                MIMEH_parse_to( header_name, header_value, hinfo );
                MIMEH_parse_messageid( header_name, header_value, hinfo );
                MIMEH_parse_received( header_name, header_value, hinfo );
                MIMEH_parse_charset( header_name, header_value, hinfo );


                if (hinfo->filename[0] == '\0')
                {
                    MIMEH_parse_contentlocation( header_name, header_value, hinfo );
                }

            } else {
                if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headerss:DEBUG: Header value end position is NULL",FL);
            }
        }



    } // while

    // Final analysis on our headers:
    if ( hinfo->content_type == _CTYPE_MULTIPART_APPLEDOUBLE )
    {
        char tmp[128];
        snprintf( tmp, sizeof(tmp), "mac-%s", hinfo->filename );
        snprintf( hinfo->filename, sizeof(hinfo->filename), "%s", tmp );
        snprintf( hinfo->name, sizeof(hinfo->name), "%s", tmp );
    }

    // PLD:20031205
    // Act like Outlook *God forbid!* and if there's a octect-stream
    //  content-type, but the encoding is still null/empty, then
    //  change the content-encoding to be RAW

    if ( hinfo->content_type == _CTYPE_OCTECT )
    {
        if ((hinfo->content_transfer_encoding == _CTRANS_ENCODING_UNSPECIFIED)
                || (hinfo->content_transfer_encoding == _CTRANS_ENCODING_UNKNOWN)
                || (strlen(hinfo->content_transfer_encoding_string) < 1)
            )
        {
            //LOGGER_log("%s:%d:DEBUG: Encoding pair was octet but no encoding, filename=%s\n",FL,hinfo->filename);
            hinfo->content_transfer_encoding = _CTRANS_ENCODING_RAW;
        }
    }


    if (safehl)
    {
        free(safehl);
    }
    else LOGGER_log("%s:%d:MIME_parse_headers:WARNING: Unable to free HEADERS allocated memory\n", FL);

    if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: END [hinfo=%p]\n", FL, hinfo);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x1, 0x24392(%rip)     # 0x3277c
jl 0xe40a
movq -0x8(%rbp), %rcx
leaq 0xf9ac(%rip), %rdi      # 0x1dda3
leaq 0xe070(%rip), %rsi      # 0x1c46e
movl $0xac0, %edx            # imm = 0xAC0
movb $0x0, %al
callq 0x117c0
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2100
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2290
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdx
callq 0x10640
cmpl $0x1, 0x24327(%rip)     # 0x3277c
jl 0xe474
movl -0x34(%rbp), %ecx
leaq 0xf975(%rip), %rdi      # 0x1ddd6
leaq 0xe006(%rip), %rsi      # 0x1c46e
movl $0xacb, %edx            # imm = 0xACB
movb $0x0, %al
callq 0x117c0
movq -0x20(%rbp), %rdi
callq 0xab70
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x30(%rbp)
movb %al, -0xe1(%rbp)
je 0xe4af
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0x34(%rbp), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
setbe %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
testb $0x1, %al
jne 0xe4be
jmp 0xe797
cmpl $0x1, 0x242b7(%rip)     # 0x3277c
jl 0xe4e5
movq -0x30(%rbp), %rcx
leaq 0xf938(%rip), %rdi      # 0x1de0a
leaq 0xdf95(%rip), %rsi      # 0x1c46e
movl $0xada, %edx            # imm = 0xADA
movb $0x0, %al
callq 0x117c0
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0xf945(%rip), %rsi      # 0x1de3d
callq 0x22e0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xe575
cmpl $0x1, 0x2426d(%rip)     # 0x3277c
jl 0xe52f
movq -0x40(%rbp), %rcx
leaq 0xf925(%rip), %rdi      # 0x1de41
leaq 0xdf4b(%rip), %rsi      # 0x1c46e
movl $0xaea, %edx            # imm = 0xAEA
movb $0x0, %al
callq 0x117c0
movq -0x40(%rbp), %rdi
leaq 0xf23f(%rip), %rsi      # 0x1d779
callq 0x22e0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xe573
cmpl $0x1, 0x2422b(%rip)     # 0x3277c
jl 0xe571
movq -0x40(%rbp), %rcx
leaq 0xf945(%rip), %rdi      # 0x1dea3
leaq 0xdf09(%rip), %rsi      # 0x1c46e
movl $0xaee, %edx            # imm = 0xAEE
movb $0x0, %al
callq 0x117c0
jmp 0xe573
jmp 0xe575
movq -0x30(%rbp), %rdi
leaq 0x10747(%rip), %rsi     # 0x1ecc7
callq 0x22e0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xe595
jmp 0xe485
jmp 0xe597
movq -0x30(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0xa, %ecx
movb %al, -0xe2(%rbp)
je 0xe5be
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
sete %al
movb %al, -0xe2(%rbp)
movb -0xe2(%rbp), %al
testb $0x1, %al
jne 0xe5ca
jmp 0xe5d8
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xe597
jmp 0xe5da
cmpq $0x0, -0x50(%rbp)
je 0xe5eb
movq -0x50(%rbp), %rax
cmpq -0x30(%rbp), %rax
jbe 0xe617
cmpl $0x1, 0x2418a(%rip)     # 0x3277c
jl 0xe612
movq -0x30(%rbp), %rcx
leaq 0xf8f4(%rip), %rdi      # 0x1def3
leaq 0xde68(%rip), %rsi      # 0x1c46e
movl $0xb15, %edx            # imm = 0xB15
movb $0x0, %al
callq 0x117c0
jmp 0xe485
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
leaq 0x10699(%rip), %rsi     # 0x1ecc7
callq 0x22e0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0xe76b
movq -0x50(%rbp), %rax
movb $0x0, (%rax)
movq -0x58(%rbp), %rax
movb $0x0, (%rax)
cmpl $0x1, 0x24125(%rip)     # 0x3277c
jl 0xe695
movq -0x40(%rbp), %rcx
leaq 0xf8dd(%rip), %rdi      # 0x1df41
leaq 0xde03(%rip), %rsi      # 0x1c46e
movl $0xb26, %edx            # imm = 0xB26
movb $0x0, %al
callq 0x117c0
movq -0x48(%rbp), %rcx
leaq 0xf8f2(%rip), %rdi      # 0x1df74
leaq 0xdde5(%rip), %rsi      # 0x1c46e
movl $0xb27, %edx            # imm = 0xB27
movb $0x0, %al
callq 0x117c0
movq -0x40(%rbp), %rdi
callq 0x105f0
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xe140
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xcc00
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xd7e0
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xdb90
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xe1c0
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xe210
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xe260
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xe2b0
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xe300
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xe350
movq -0x8(%rbp), %rax
movsbl 0x5efd(%rax), %eax
cmpl $0x0, %eax
jne 0xe769
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rdx
callq 0xd650
jmp 0xe790
cmpl $0x1, 0x2400a(%rip)     # 0x3277c
jl 0xe78e
leaq 0xf82c(%rip), %rdi      # 0x1dfa7
leaq 0xdcec(%rip), %rsi      # 0x1c46e
movl $0xb46, %edx            # imm = 0xB46
movb $0x0, %al
callq 0x117c0
jmp 0xe790
jmp 0xe792
jmp 0xe485
movq -0x8(%rbp), %rax
cmpl $0x66, 0x27f8(%rax)
jne 0xe813
leaq -0xe0(%rbp), %rdi
movq -0x8(%rbp), %rcx
addq $0x5efd, %rcx           # imm = 0x5EFD
movl $0x80, %esi
leaq 0xf829(%rip), %rdx      # 0x1dfeb
movb $0x0, %al
callq 0x2130
movq -0x8(%rbp), %rdi
addq $0x5efd, %rdi           # imm = 0x5EFD
leaq -0xe0(%rbp), %rcx
movl $0x1001, %esi           # imm = 0x1001
leaq 0x107c3(%rip), %rdx     # 0x1efaa
movb $0x0, %al
callq 0x2130
movq -0x8(%rbp), %rdi
addq $0x6efe, %rdi           # imm = 0x6EFE
leaq -0xe0(%rbp), %rcx
movl $0x27f7, %esi           # imm = 0x27F7
leaq 0x1079e(%rip), %rdx     # 0x1efaa
movb $0x0, %al
callq 0x2130
movq -0x8(%rbp), %rax
cmpl $0x320, 0x27f8(%rax)    # imm = 0x320
jne 0xe863
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x166f8(%rax)
je 0xe853
movq -0x8(%rbp), %rax
cmpl $0x0, 0x166f8(%rax)
je 0xe853
movq -0x8(%rbp), %rdi
addq $0x166fc, %rdi          # imm = 0x166FC
callq 0x2100
cmpq $0x1, %rax
jae 0xe861
movq -0x8(%rbp), %rax
movl $0x68, 0x166f8(%rax)
jmp 0xe863
cmpq $0x0, -0x28(%rbp)
je 0xe875
movq -0x28(%rbp), %rdi
callq 0x2030
jmp 0xe88f
leaq 0xf776(%rip), %rdi      # 0x1dff2
leaq 0xdbeb(%rip), %rsi      # 0x1c46e
movl $0xb6d, %edx            # imm = 0xB6D
movb $0x0, %al
callq 0x117c0
cmpl $0x1, 0x23ee6(%rip)     # 0x3277c
jl 0xe8b6
movq -0x8(%rbp), %rcx
leaq 0xf79a(%rip), %rdi      # 0x1e03d
leaq 0xdbc4(%rip), %rsi      # 0x1c46e
movl $0xb6f, %edx            # imm = 0xB6F
movb $0x0, %al
callq 0x117c0
xorl %eax, %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)