// File: /workspace/github_c_makefile_star2_O2/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 0xe124
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movl %esi, %ebp
movq %rdi, %rbx
callq 0x2110
movq %rax, %r14
cmpl $0x0, 0x20a86(%rip)     # 0x2e9b4
jle 0xdf50
leaq 0x9d0c(%rip), %rdi      # 0x17c43
leaq 0x9d3a(%rip), %rsi      # 0x17c78
movl $0xbe, %edx
movl %r14d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0xc2a1
cmpl $0x1, 0x20a65(%rip)     # 0x2e9bc
jne 0xdfbe
cmpl $0x0, 0x20a54(%rip)     # 0x2e9b4
jle 0xdf7f
leaq 0x9d22(%rip), %rdi      # 0x17c8b
leaq 0x9d08(%rip), %rsi      # 0x17c78
movl $0xc5, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rax
movzbl (%rax), %ecx
cmpl $0x2f, %ecx
je 0xdf90
testl %ecx, %ecx
jne 0xdf93
jmp 0xdf98
movb $0x2d, (%rax)
incq %rax
jmp 0xdf82
cmpl $0x0, 0x20a15(%rip)     # 0x2e9b4
jle 0xdfbe
leaq 0x9d1e(%rip), %rdi      # 0x17cc6
leaq 0x9cc9(%rip), %rsi      # 0x17c78
movl $0xcb, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
cmpl $0x3, %r14d
jl 0xe061
cmpb $0x22, (%rbx)
jne 0xe061
leal -0x1(%r14), %eax
cmpb $0x22, (%rbx,%rax)
jne 0xe061
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpl $0x0, 0x209c8(%rip)     # 0x2e9b4
jle 0xe04a
leaq 0x9d09(%rip), %rdi      # 0x17cfe
leaq 0x9c7c(%rip), %rsi      # 0x17c78
movl $0xd8, %edx
xorl %eax, %eax
callq 0xc2a1
movl 0x209a5(%rip), %r12d    # 0x2e9b4
addl %r15d, %r14d
leaq 0x1(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2330
movb $0x0, (%rbx,%r14)
testl %r12d, %r12d
jle 0xe061
leaq 0x9d07(%rip), %rdi      # 0x17d39
leaq 0x9c3f(%rip), %rsi      # 0x17c78
movl $0xe2, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
jmp 0xe061
addl %r15d, %r14d
leaq 0x1(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2330
movb $0x0, (%rbx,%r14)
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x2150
testq %rax, %rax
je 0xe127
incq %rax
movq %rsp, %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rax, %rsi
callq 0xba3e
movslq %ebp, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xba3e
pushq $0x3f
popq %rsi
movq %rbx, %rdi
callq 0x2120
testq %rax, %rax
je 0xe0ba
movb $0x2d, %cl
cmpq %rbx, %rax
jbe 0xe0b8
cmpb $0x3d, -0x1(%rax)
je 0xe0ba
xorl %ecx, %ecx
movb %cl, (%rax)
cmpl $0x0, 0x208f3(%rip)     # 0x2e9b4
jle 0xe0dd
leaq 0x9cb2(%rip), %rdi      # 0x17d7c
leaq 0x9ba7(%rip), %rsi      # 0x17c78
movl $0x113, %edx            # imm = 0x113
xorl %eax, %eax
callq 0xc2a1
movq %rbx, %rdi
callq 0x2110
movq %rbx, %rdi
movl %eax, %esi
callq 0xde26
cmpl $0x0, 0x208be(%rip)     # 0x2e9b4
jle 0xe115
leaq 0x9cb4(%rip), %rdi      # 0x17db3
leaq 0x9b72(%rip), %rsi      # 0x17c78
movl $0x117, %edx            # imm = 0x117
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
xorl %eax, %eax
retq
pushq $0x5c
popq %rsi
movq %rbx, %rdi
callq 0x2150
testq %rax, %rax
je 0xe099
cmpb $0x22, 0x1(%rax)
jne 0xe075
jmp 0xe099
nop