// File: /workspace/github_c_makefile_star2_O1/inflex[P]ripMIME/filename-filters.c
// Function: FNFILTER_filter
int FNFILTER_filter( char *fname, int size )
{
    int fnl;
    char tmp[1024];
    char *p;

    if (fname == NULL) return 0;

    fnl = strlen(fname);

    DFN LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: fname[%d chars] = '%s'\n", FL, fnl, fname );

    /** If we're handling a Mac file, prefilter **/
    if (glb.x_mac == 1)
    {
        char *q = fname;

        DFN LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: Filtering x-mac filename '%s'",FL,fname);
        while (*q)
        {
            if (*q == '/') *q = '-'; /** Convert the Mac / separator to a hyphen **/
            q++;
        }
        DFN LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: x-mac filename is now '%s'",FL,fname);
    }


    /* We only look at trimming the quotes off a filename if it has more than 2 chars
        * because obviously we'll need to strip off 2 chars (leading and finishing quote)
        */
    if ( fnl > 2 )
    {

        /* if the MIME_filename starts and ends with "'s */
        if ((fname[0] == '\"') && (fname[fnl-1] == '\"'))
        {
            if (FNFILTER_DNORMAL) LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: Trimming quotes off filename\n", FL );

            /* reduce the file namelength by two*/
            fnl = fnl -2; // 17-11-2002: was =-2, thanks to Vasily Chernikov for spotting the glaring error!

            /* shuffle the MIME_filename chars down */
            memmove(fname,fname+1,fnl);

            /* terminate the string */
            fname[fnl] = '\0';
            if (FNFILTER_DNORMAL) LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: Trimming filename done, fname = '%s'\n", FL, fname );
        } /* if */
    } /* if */

    p = strrchr(fname,'/');
    if (p)
    {
        p++;
        PLD_strncpy( tmp, p, sizeof(tmp) );
        PLD_strncpy( fname, tmp, size);

    } else {

        // Check for Windows/DOS backslash seperator

        p = strrchr( fname, '\\' );
        if ( p )
        {
            if ( *(p+1) != '"' )
            {
                p++;
                PLD_strncpy( tmp, p, sizeof(tmp) );
                PLD_strncpy( fname, tmp, size );
            }
        }
    }

    // Scan for ? symbols - these are often used to make the email client pass paremeters to the filename
    //  Check though to see that the previous character is not a '=' symbol, because if it is, then we
    //  actually have an ISO encoded filename

    p = strchr( fname, '?' );
    if (p != NULL)
    {
        if (p > fname)
        {
            if (*(p-1) != '=')
            {
                *p = '\0';
            } else {
                // leave the ? alone, as it's part of an ISO encoded filename
            }

        } else {
            // First char of the filename is a '?', change this to a hypen.
            *p = '-';
        }
    }

    if (FNFILTER_DNORMAL) LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: Starting paranoia filter\n", FL );

    FNFILTER_paranoid_filter( fname, strlen( fname ) );

    if (FNFILTER_DNORMAL) LOGGER_log("%s:%d:FNFILTER_filter:DEBUG: paranoia filter done. Filename='%s'\n", FL, fname );

    return 0;
}

// Assembly:
testq %rdi, %rdi
je 0xe8bf
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movl %esi, %ebp
movq %rdi, %rbx
callq 0x2110
movq %rax, %r14
cmpl $0x0, 0x212c5(%rip)     # 0x2f9b4
jle 0xe711
leaq 0xa55b(%rip), %rdi      # 0x18c53
leaq 0xa589(%rip), %rsi      # 0x18c88
movl $0xbe, %edx
movl %r14d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0xc960
cmpl $0x1, 0x212a4(%rip)     # 0x2f9bc
jne 0xe77f
cmpl $0x0, 0x21293(%rip)     # 0x2f9b4
jle 0xe740
leaq 0xa571(%rip), %rdi      # 0x18c9b
leaq 0xa557(%rip), %rsi      # 0x18c88
movl $0xc5, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
movq %rbx, %rax
movzbl (%rax), %ecx
cmpl $0x2f, %ecx
je 0xe751
testl %ecx, %ecx
jne 0xe754
jmp 0xe759
movb $0x2d, (%rax)
incq %rax
jmp 0xe743
cmpl $0x0, 0x21254(%rip)     # 0x2f9b4
jle 0xe77f
leaq 0xa56d(%rip), %rdi      # 0x18cd6
leaq 0xa518(%rip), %rsi      # 0x18c88
movl $0xcb, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
cmpl $0x3, %r14d
jl 0xe7fa
cmpb $0x22, (%rbx)
jne 0xe7fa
leal -0x1(%r14), %eax
cmpb $0x22, (%rbx,%rax)
jne 0xe7fa
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpl $0x0, 0x21213(%rip)     # 0x2f9b4
jle 0xe7bd
leaq 0xa564(%rip), %rdi      # 0x18d0e
leaq 0xa4d7(%rip), %rsi      # 0x18c88
movl $0xd8, %edx
xorl %eax, %eax
callq 0xc960
addl %r15d, %r14d
leaq 0x1(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2330
movb $0x0, (%rbx,%r14)
cmpl $0x0, 0x211d9(%rip)     # 0x2f9b4
jle 0xe7fa
leaq 0xa565(%rip), %rdi      # 0x18d49
leaq 0xa49d(%rip), %rsi      # 0x18c88
movl $0xe2, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2150
testq %rax, %rax
je 0xe8c2
incq %rax
movq %rsp, %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rax, %rsi
callq 0xbfb6
movslq %ebp, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbfb6
movq %rbx, %rdi
movl $0x3f, %esi
callq 0x2120
testq %rax, %rax
je 0xe857
movb $0x2d, %cl
cmpq %rbx, %rax
jbe 0xe855
cmpb $0x3d, -0x1(%rax)
je 0xe857
xorl %ecx, %ecx
movb %cl, (%rax)
cmpl $0x0, 0x21156(%rip)     # 0x2f9b4
jle 0xe87a
leaq 0xa525(%rip), %rdi      # 0x18d8c
leaq 0xa41a(%rip), %rsi      # 0x18c88
movl $0x113, %edx            # imm = 0x113
xorl %eax, %eax
callq 0xc960
movq %rbx, %rdi
callq 0x2110
movq %rbx, %rdi
movl %eax, %esi
callq 0xe5c9
cmpl $0x0, 0x21121(%rip)     # 0x2f9b4
jle 0xe8b2
leaq 0xa527(%rip), %rdi      # 0x18dc3
leaq 0xa3e5(%rip), %rsi      # 0x18c88
movl $0x117, %edx            # imm = 0x117
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc960
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r14
popq %r15
popq %rbp
xorl %eax, %eax
retq
movq %rbx, %rdi
movl $0x5c, %esi
callq 0x2150
testq %rax, %rax
je 0xe834
cmpb $0x22, 0x1(%rax)
jne 0xe810
jmp 0xe834
nop