You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2006/08/18 08:46:43 UTC

svn commit: r432500 - /directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java

Author: elecharny
Date: Thu Aug 17 23:46:42 2006
New Revision: 432500

URL: http://svn.apache.org/viewvc?rev=432500&view=rev
Log:
Added two utility methods to parse #xyxyxy values in a DN

Modified:
    directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java

Modified: directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java?rev=432500&r1=432499&r2=432500&view=diff
==============================================================================
--- directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java (original)
+++ directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java Thu Aug 17 23:46:42 2006
@@ -703,6 +703,22 @@
     }
 
     /**
+     * Parse an hex pair <hexpair> ::= <hex> <hex>
+     * 
+     * @param string
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @return The new position, -1 if the string does not contain an HexPair,
+     *         -2 if the string contains an hex byte but not two.
+     */
+    private static byte getHexPair( String string, int index )
+    {
+    	return (byte)((StringTools.HEX_VALUE[string.charAt( index )] << 4) | 
+    				(StringTools.HEX_VALUE[string.charAt( index + 1 )]) );
+    }
+
+    /**
      * Parse an hex string, which is a list of hex pairs <hexstring> ::=
      * <hexpair> <hexpairs> <hexpairs> ::= <hexpair> <hexpairs> | e
      * 
@@ -797,6 +813,42 @@
 
         while ( ( result = parseHexPair( string, pos.end ) ) >= 0 )
         {
+            pos.end += TWO_CHARS;
+        }
+
+        return ( ( result == BAD_HEX_PAIR ) ? PARSING_ERROR : PARSING_OK );
+    }
+
+    /**
+     * Parse an hex string, which is a list of hex pairs <hexstring> ::=
+     * <hexpair> <hexpairs> <hexpairs> ::= <hexpair> <hexpairs> | e
+     * 
+     * @param string The string which contains the data
+     * @param hex The result as a byte array
+     * @param Position Current position in the string
+     * @return Return the first position which is not an hex pair, or -1 if
+     *         there is no hexpair at the beginning or if an hexpair is invalid
+     *         (if we have only one hex instead of 2)
+     */
+    public static int parseHexString( String string, byte[] hex, Position pos )
+    {
+    	int i = 0;
+        pos.end = pos.start;
+        int result = parseHexPair( string, pos.start );
+
+        if ( result < 0 )
+        {
+            return PARSING_ERROR;
+        }
+        else
+        {
+        	hex[i++] = getHexPair( string, pos.end );
+            pos.end += TWO_CHARS;
+        }
+
+        while ( ( result = parseHexPair( string, pos.end ) ) >= 0 )
+        {
+        	hex[i++] = getHexPair( string, pos.end );
             pos.end += TWO_CHARS;
         }