/** * First convert filter string into byte[]. * For LDAP v3, the conversion uses Unicode -> UTF8 * For LDAP v2, the conversion uses Unicode -> ISO 8859 (Latin-1) * * Then parse the byte[] as a filter, converting \hh to * a single byte, and encoding the resulting filter * into the supplied BER buffer */ static void encodeFilterString(BerEncoder ber, String filterStr, boolean isLdapv3) throws IOException, NamingException { if ((filterStr == null) || (filterStr.equals(""))) { throw new InvalidSearchFilterException("Empty filter"); } byte[] filter; int filterLen; if (isLdapv3) { filter = filterStr.getBytes("UTF8"); } else { filter = filterStr.getBytes("8859_1"); } filterLen = filter.length; if (dbg) { dbgIndent = 0; System.err.println("String filter: " + filterStr); System.err.println("size: " + filterLen); dprint("original: ", filter, 0, filterLen); } encodeFilter(ber, filter, 0, filterLen); }
public static Filter parseFilter(String filter, Object[] args) throws InvalidSearchFilterException { if (filter == null) { // ldap.28=Parameter of filter should not be null throw new NullPointerException(Messages.getString("ldap.28")); //$NON-NLS-1$ } FilterParser parser = new FilterParser(filter); if (args == null) { args = new Object[0]; } parser.setArgs(args); try { return parser.parse(); } catch (ParseException e) { // ldap.29=Invalid search filter InvalidSearchFilterException ex = new InvalidSearchFilterException( Messages.getString("ldap.29")); //$NON-NLS-1$ ex.setRootCause(e); throw ex; } }
private static int[] findRightParen(byte[] filter, int filtOffset[], int end) throws IOException, NamingException { int balance = 1; boolean escape = false; int nextOffset = filtOffset[0]; while (nextOffset < end && balance > 0) { if (!escape) { if (filter[nextOffset] == '(') balance++; else if (filter[nextOffset] == ')') balance--; } if (filter[nextOffset] == '\\' && !escape) escape = true; else escape = false; if (balance > 0) nextOffset++; } if (balance != 0) { throw new InvalidSearchFilterException("Unbalanced parenthesis"); } // String tmp = filter.substring(filtOffset[0], nextOffset); int[] tmp = new int[] {filtOffset[0], nextOffset}; filtOffset[0] = nextOffset + 1; return tmp; }
public LdapSchemaFilter(String filterValue, Object[] filterArgs) throws InvalidSearchFilterException { try { FilterParser commonFilterParser = new FilterParser(filterValue); commonFilterParser.setArgs(filterArgs); commonFilter = commonFilterParser.parse(); } catch (ParseException e) { // ldap.29=Invalid search filter throw new InvalidSearchFilterException(Messages .getString("ldap.29")); //$NON-NLS-1$ } }
static byte[] unescapeFilterValue(byte[] orig, int start, int end) throws NamingException { boolean escape = false, escStart = false; int ival; byte ch; if (dbg) { dprint("unescape: " , orig, start, end); } int len = end - start; byte tbuf[] = new byte[len]; int j = 0; for (int i = start; i < end; i++) { ch = orig[i]; if (escape) { // Try LDAP V3 escape (\xx) if ((ival = hexchar2int(ch)) < 0) { /** * If there is no hex char following a '\' when * parsing a LDAP v3 filter (illegal by v3 way) * we fallback to the way we unescape in v2. */ if (escStart) { // V2: \* \( \) escape = false; tbuf[j++] = ch; } else { // escaping already started but we can't find 2nd hex throw new InvalidSearchFilterException("invalid escape sequence: " + orig); } } else { if (escStart) { tbuf[j] = (byte)(ival<<4); escStart = false; } else { tbuf[j++] |= (byte)ival; escape = false; } } } else if (ch != '\\') { tbuf[j++] = ch; escape = false; } else { escStart = escape = true; } } byte[] answer = new byte[j]; System.arraycopy(tbuf, 0, answer, 0, j); if (dbg) { Ber.dumpBER(System.err, "", answer, 0, j); } return answer; }
private static void encodeFilterList(BerEncoder ber, byte[] filter, int filterType, int start, int end) throws IOException, NamingException { if (dbg) { dprint("encFilterList: ", filter, start, end); dbgIndent++; } int filtOffset[] = new int[1]; int listNumber = 0; for (filtOffset[0] = start; filtOffset[0] < end; filtOffset[0]++) { if (Character.isSpaceChar((char)filter[filtOffset[0]])) continue; if ((filterType == LDAP_FILTER_NOT) && (listNumber > 0)) { throw new InvalidSearchFilterException( "Filter (!) cannot be followed by more than one filters"); } if (filter[filtOffset[0]] == '(') { continue; } int[] parens = findRightParen(filter, filtOffset, end); // add enclosing parens int len = parens[1]-parens[0]; byte[] newfilter = new byte[len+2]; System.arraycopy(filter, parens[0], newfilter, 1, len); newfilter[0] = (byte)'('; newfilter[len+1] = (byte)')'; encodeFilter(ber, newfilter, 0, newfilter.length); listNumber++; } if (dbg) { dbgIndent--; } }