// File: /workspace/github_c_makefile_star2_O3/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 0xe795
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, 0x21417(%rip)     # 0x2f9b4
jle 0xe5bf
leaq 0xa69d(%rip), %rdi      # 0x18c43
leaq 0xa6cb(%rip), %rsi      # 0x18c78
movl $0xbe, %edx
movl %r14d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0xc84f
cmpl $0x1, 0x213f6(%rip)     # 0x2f9bc
jne 0xe62d
cmpl $0x0, 0x213e5(%rip)     # 0x2f9b4
jle 0xe5ee
leaq 0xa6b3(%rip), %rdi      # 0x18c8b
leaq 0xa699(%rip), %rsi      # 0x18c78
movl $0xc5, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rax
movzbl (%rax), %ecx
cmpl $0x2f, %ecx
je 0xe5ff
testl %ecx, %ecx
jne 0xe602
jmp 0xe607
movb $0x2d, (%rax)
incq %rax
jmp 0xe5f1
cmpl $0x0, 0x213a6(%rip)     # 0x2f9b4
jle 0xe62d
leaq 0xa6af(%rip), %rdi      # 0x18cc6
leaq 0xa65a(%rip), %rsi      # 0x18c78
movl $0xcb, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc84f
cmpl $0x3, %r14d
jl 0xe6d0
cmpb $0x22, (%rbx)
jne 0xe6d0
leal -0x1(%r14), %eax
cmpb $0x22, (%rbx,%rax)
jne 0xe6d0
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpl $0x0, 0x21359(%rip)     # 0x2f9b4
jle 0xe6b9
leaq 0xa69a(%rip), %rdi      # 0x18cfe
leaq 0xa60d(%rip), %rsi      # 0x18c78
movl $0xd8, %edx
xorl %eax, %eax
callq 0xc84f
addl %r15d, %r14d
leaq 0x1(%rbx), %rsi
movl 0x2132f(%rip), %r15d    # 0x2f9b4
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2320
movb $0x0, (%rbx,%r14)
testl %r15d, %r15d
jle 0xe6d0
leaq 0xa698(%rip), %rdi      # 0x18d39
leaq 0xa5d0(%rip), %rsi      # 0x18c78
movl $0xe2, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc84f
jmp 0xe6d0
addl %r15d, %r14d
leaq 0x1(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2320
movb $0x0, (%rbx,%r14)
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2150
testq %rax, %rax
je 0xe798
incq %rax
movq %rsp, %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rax, %rsi
callq 0xbede
movslq %ebp, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbede
movq %rbx, %rdi
movl $0x3f, %esi
callq 0x2120
testq %rax, %rax
je 0xe72d
movb $0x2d, %cl
cmpq %rbx, %rax
jbe 0xe72b
cmpb $0x3d, -0x1(%rax)
je 0xe72d
xorl %ecx, %ecx
movb %cl, (%rax)
cmpl $0x0, 0x21280(%rip)     # 0x2f9b4
jle 0xe750
leaq 0xa63f(%rip), %rdi      # 0x18d7c
leaq 0xa534(%rip), %rsi      # 0x18c78
movl $0x113, %edx            # imm = 0x113
xorl %eax, %eax
callq 0xc84f
movq %rbx, %rdi
callq 0x2110
movq %rbx, %rdi
movl %eax, %esi
callq 0xe485
cmpl $0x0, 0x2124b(%rip)     # 0x2f9b4
jle 0xe788
leaq 0xa641(%rip), %rdi      # 0x18db3
leaq 0xa4ff(%rip), %rsi      # 0x18c78
movl $0x117, %edx            # imm = 0x117
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc84f
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 0xe70a
cmpb $0x22, 0x1(%rax)
jne 0xe6e6
jmp 0xe70a
nopl (%rax)