[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: How to use JLDAP to search for an entry in OpenLDAP using a binary search filter?



On Saturday 17 September 2005 04:47, Marc Boorshtein wrote:

> I believe you can escape the bytes in the filter with a "\". So your filter
> would be "(byBinaryVal=\\2B\\1A)"

Something like that yes. On Windows there's an API called
ldap_escape_filter_element() that does exactly this. Everything
except alphanumeric characters need to be escaped and the result
is zero-terminated. Below is my implementation in C for reference.

 -Hans


static ULONG get_escape_size( PCHAR src, ULONG srclen )
{
    ULONG i, size = 0;

    if (src)
    {
        for (i = 0; i < srclen; i++)
        {
            if ((src[i] >= '0' && src[i] <= '9') ||
                (src[i] >= 'A' && src[i] <= 'Z') ||
                (src[i] >= 'a' && src[i] <= 'z'))
                size++;
            else
                size += 3;
        }
    }
    return size + 1;
}

static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst )
{
    ULONG i;
    char fmt[] = "\\%02X";
    char *d = dst;

    for (i = 0; i < srclen; i++)
    {
        if ((src[i] >= '0' && src[i] <= '9') ||
            (src[i] >= 'A' && src[i] <= 'Z') ||
            (src[i] >= 'a' && src[i] <= 'z'))
            *d++ = src[i];
        else
        {
            sprintf( d, fmt, (unsigned char)src[i] );
            d += 3;
        }
    }
    *++d = 0;
}

ULONG ldap_escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen )
{
    ULONG len;

    TRACE( "(%p, 0x%08lx, %p, 0x%08lx)\n", src, srclen, dst, dstlen );

    len = get_escape_size( src, srclen );
    if (!dst) return len;

    if (!src || dstlen < len)
        return LDAP_PARAM_ERROR;
    else
    {
        escape_filter_element( src, srclen, dst );
        return LDAP_SUCCESS;
    }
}