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 2012/01/24 18:44:32 UTC
svn commit: r1235374 [26/27] - in /directory/shared/trunk:
ldap/model/src/main/java/org/apache/directory/shared/ldap/model/constants/
ldap/model/src/main/java/org/apache/directory/shared/ldap/model/csn/
ldap/model/src/main/java/org/apache/directory/sha...
Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/Strings.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/Strings.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/Strings.java (original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/Strings.java Tue Jan 24 17:44:03 2012
@@ -39,6 +39,7 @@ import org.apache.directory.shared.i18n.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+
/**
* Various string manipulation methods that are more efficient then chaining
* string operations: all is done in the same buffer without creating a bunch of
@@ -54,7 +55,6 @@ public final class Strings
/** The default charset, because it's not provided by JDK 1.5 */
static String defaultCharset = null;
-
/** Hex chars */
private static final byte[] HEX_CHAR = new byte[]
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
@@ -62,23 +62,128 @@ public final class Strings
/** A table containing booleans when the corresponding char is printable */
public static final boolean[] IS_PRINTABLE_CHAR =
{
- false, false, false, false, false, false, false, false, // ---, ---, ---, ---, ---, ---, ---, ---
- false, false, false, false, false, false, false, false, // ---, ---, ---, ---, ---, ---, ---, ---
- false, false, false, false, false, false, false, false, // ---, ---, ---, ---, ---, ---, ---, ---
- false, false, false, false, false, false, false, false, // ---, ---, ---, ---, ---, ---, ---, ---
- true, false, false, false, false, false, false, true, // ' ', ---, ---, ---, ---, ---, ---, "'"
- true, true, false, true, true, true, true, true, // '(', ')', ---, '+', ',', '-', '.', '/'
- true, true, true, true, true, true, true, true, // '0', '1', '2', '3', '4', '5', '6', '7',
- true, true, true, false, false, true, false, true, // '8', '9', ':', ---, ---, '=', ---, '?'
- false, true, true, true, true, true, true, true, // ---, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
- true, true, true, true, true, true, true, true, // 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'
- true, true, true, true, true, true, true, true, // 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W'
- true, true, true, false, false, false, false, false, // 'X', 'Y', 'Z', ---, ---, ---, ---, ---
- false, true, true, true, true, true, true, true, // ---, 'a', 'b', 'c', 'd', 'e', 'f', 'g'
- true, true, true, true, true, true, true, true, // 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'
- true, true, true, true, true, true, true, true, // 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'
- true, true, true, false, false, false, false, false // 'x', 'y', 'z', ---, ---, ---, ---, ---
- };
+ false, false, false, false, false, false, false, false, // ---, ---, ---, ---, ---, ---, ---, ---
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false, // ---, ---, ---, ---, ---, ---, ---, ---
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false, // ---, ---, ---, ---, ---, ---, ---, ---
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false, // ---, ---, ---, ---, ---, ---, ---, ---
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false,
+ true, // ' ', ---, ---, ---, ---, ---, ---, "'"
+ true,
+ true,
+ false,
+ true,
+ true,
+ true,
+ true,
+ true, // '(', ')', ---, '+', ',', '-', '.', '/'
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true, // '0', '1', '2', '3', '4', '5', '6', '7',
+ true,
+ true,
+ true,
+ false,
+ false,
+ true,
+ false,
+ true, // '8', '9', ':', ---, ---, '=', ---, '?'
+ false,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true, // ---, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true, // 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true, // 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W'
+ true,
+ true,
+ true,
+ false,
+ false,
+ false,
+ false,
+ false, // 'X', 'Y', 'Z', ---, ---, ---, ---, ---
+ false,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true, // ---, 'a', 'b', 'c', 'd', 'e', 'f', 'g'
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true, // 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true, // 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'
+ true,
+ true,
+ true,
+ false,
+ false,
+ false,
+ false,
+ false // 'x', 'y', 'z', ---, ---, ---, ---, ---
+ };
public static final char[] TO_LOWER_CASE =
{
@@ -86,18 +191,18 @@ public final class Strings
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
- ' ', 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, '\'',
- '(', ')', 0x2A, '+', ',', '-', '.', '/',
- '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', ':', 0x3B, 0x3C, '=', 0x3E, '?',
- 0x40, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
- 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
- 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
- 'x', 'y', 'z', 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
- 0x60, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
- 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
- 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
- 'x', 'y', 'z', 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
+ ' ', 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, '\'',
+ '(', ')', 0x2A, '+', ',', '-', '.', '/',
+ '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', ':', 0x3B, 0x3C, '=', 0x3E, '?',
+ 0x40, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+ 'x', 'y', 'z', 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
+ 0x60, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+ 'x', 'y', 'z', 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
@@ -114,36 +219,36 @@ public final class Strings
0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
- };
+ };
/** upperCase = 'A' .. 'Z', '0'..'9', '-' */
public static final char[] UPPER_CASE =
{
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, '-', 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, '-', 0, 0,
'0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 0, 0, 0, 0, 0, 0,
- 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
+ '8', '9', 0, 0, 0, 0, 0, 0,
+ 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
- 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
- 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
+ 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
+ 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
- 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0
- };
+ 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0
+ };
/**
@@ -444,7 +549,7 @@ public final class Strings
public static byte[] toByteArray( String hexString )
{
int arrLength = hexString.length() >> 1;
- byte [] buf = new byte[arrLength];
+ byte[] buf = new byte[arrLength];
for ( int ii = 0; ii < arrLength; ii++ )
{
@@ -1070,7 +1175,7 @@ public final class Strings
int length = bytes.length;
- while ( ( pos.start < length ) && ( bytes[ pos.start ] == ' ' ) )
+ while ( ( pos.start < length ) && ( bytes[pos.start] == ' ' ) )
{
pos.start++;
}
@@ -1144,7 +1249,7 @@ public final class Strings
while ( ( end > 0 ) && ( str.charAt( end - 1 ) == ' ' ) )
{
- if ( ( end > 1 ) && ( str.charAt( end - 2 ) == '\\' ) )
+ if ( ( end > 1 ) && ( str.charAt( end - 2 ) == '\\' ) )
{
break;
}
@@ -1187,7 +1292,7 @@ public final class Strings
while ( ( end > 0 ) && ( str.charAt( end - 1 ) == ' ' ) && ( end > escapedSpace ) )
{
- if ( ( end > 1 ) && ( str.charAt( end - 2 ) == '\\' ) )
+ if ( ( end > 1 ) && ( str.charAt( end - 2 ) == '\\' ) )
{
break;
}
@@ -1262,7 +1367,7 @@ public final class Strings
while ( ( pos.end >= 0 ) && ( string.charAt( pos.end - 1 ) == ' ' ) )
{
- if ( ( pos.end > 1 ) && ( string.charAt( pos.end - 2 ) == '\\' ) )
+ if ( ( pos.end > 1 ) && ( string.charAt( pos.end - 2 ) == '\\' ) )
{
break;
}
@@ -1312,11 +1417,11 @@ public final class Strings
if ( pos.end == bytes.length )
{
- return utf8ToString(bytes);
+ return utf8ToString( bytes );
}
else
{
- return utf8ToString(bytes, pos.end);
+ return utf8ToString( bytes, pos.end );
}
}
@@ -1378,7 +1483,7 @@ public final class Strings
}
else
{
- return string.charAt( index ) ;
+ return string.charAt( index );
}
}
@@ -1392,7 +1497,7 @@ public final class Strings
*/
public static String asciiBytesToString( byte[] bytes )
{
- if ( (bytes == null) || (bytes.length == 0 ) )
+ if ( ( bytes == null ) || ( bytes.length == 0 ) )
{
return "";
}
@@ -1401,7 +1506,7 @@ public final class Strings
for ( int i = 0; i < bytes.length; i++ )
{
- result[i] = (char)bytes[i];
+ result[i] = ( char ) bytes[i];
}
return new String( result );
@@ -1446,19 +1551,19 @@ public final class Strings
{
// Try with jdk 1.5 method, if we are using a 1.5 jdk :)
Method method = Charset.class.getMethod( "defaultCharset", new Class[0] );
- defaultCharset = ((Charset) method.invoke( null, new Object[0]) ).name();
+ defaultCharset = ( ( Charset ) method.invoke( null, new Object[0] ) ).name();
}
- catch (NoSuchMethodException e)
+ catch ( NoSuchMethodException e )
{
// fall back to old method
defaultCharset = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
}
- catch (InvocationTargetException e)
+ catch ( InvocationTargetException e )
{
// fall back to old method
defaultCharset = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
}
- catch (IllegalAccessException e)
+ catch ( IllegalAccessException e )
{
// fall back to old method
defaultCharset = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
@@ -1601,7 +1706,7 @@ public final class Strings
* @param map The map to transform to a string
* @return A csv string
*/
- public static String mapToString( Map<?,?> map )
+ public static String mapToString( Map<?, ?> map )
{
if ( ( map == null ) || ( map.size() == 0 ) )
{
@@ -1611,7 +1716,7 @@ public final class Strings
StringBuffer sb = new StringBuffer();
boolean isFirst = true;
- for ( Map.Entry<?, ?> entry:map.entrySet() )
+ for ( Map.Entry<?, ?> entry : map.entrySet() )
{
if ( isFirst )
{
@@ -1638,7 +1743,7 @@ public final class Strings
* @param tabs The tabs to add in ffront of the elements
* @return A csv string
*/
- public static String mapToString( Map<?,?> map, String tabs )
+ public static String mapToString( Map<?, ?> map, String tabs )
{
if ( ( map == null ) || ( map.size() == 0 ) )
{
@@ -1647,7 +1752,7 @@ public final class Strings
StringBuffer sb = new StringBuffer();
- for ( Map.Entry<?, ?> entry:map.entrySet() )
+ for ( Map.Entry<?, ?> entry : map.entrySet() )
{
sb.append( tabs );
sb.append( entry.getKey() );
@@ -1678,7 +1783,7 @@ public final class Strings
for ( int i = 0; i < chars.length; i++ )
{
- chars[i] = TO_LOWER_CASE[ chars[i] ];
+ chars[i] = TO_LOWER_CASE[chars[i]];
}
return new String( chars );
@@ -1704,7 +1809,7 @@ public final class Strings
for ( int i = 0; i < chars.length; i++ )
{
- chars[i] = UPPER_CASE[ chars[i] ];
+ chars[i] = UPPER_CASE[chars[i]];
}
return new String( chars );
@@ -1786,7 +1891,7 @@ public final class Strings
char[] chars = str.toCharArray();
int pos = 0;
- for ( char c:chars )
+ for ( char c : chars )
{
chars[pos++] = TO_LOWER_CASE[c];
}
@@ -1812,9 +1917,9 @@ public final class Strings
return true;
}
- for ( char c:str.toCharArray() )
+ for ( char c : str.toCharArray() )
{
- if ( ( c > 127 ) || !IS_PRINTABLE_CHAR[ c ] )
+ if ( ( c > 127 ) || !IS_PRINTABLE_CHAR[c] )
{
return false;
}
@@ -1863,7 +1968,7 @@ public final class Strings
}
// All the chars must be in [0x00, 0x7F]
- for ( char c:str.toCharArray() )
+ for ( char c : str.toCharArray() )
{
if ( ( c < 0 ) || ( c > 0x7F ) )
{
@@ -1885,7 +1990,7 @@ public final class Strings
{
byte[] b = uuid.getBytes();
- if ( b.length < 36)
+ if ( b.length < 36 )
{
return false;
}
@@ -1926,7 +2031,7 @@ public final class Strings
return "Invalid UUID";
}
- char[] hex = encodeHex(bytes);
+ char[] hex = encodeHex( bytes );
StringBuffer sb = new StringBuffer();
sb.append( hex, 0, 8 );
sb.append( '-' );
@@ -1950,33 +2055,33 @@ public final class Strings
*/
public static byte[] uuidToBytes( String string )
{
- if ( ! isValidUuid(string) )
+ if ( !isValidUuid( string ) )
{
return null;
}
char[] chars = string.toCharArray();
byte[] bytes = new byte[16];
- bytes[0] = getHexValue(chars[0], chars[1]);
- bytes[1] = getHexValue(chars[2], chars[3]);
- bytes[2] = getHexValue(chars[4], chars[5]);
- bytes[3] = getHexValue(chars[6], chars[7]);
-
- bytes[4] = getHexValue(chars[9], chars[10]);
- bytes[5] = getHexValue(chars[11], chars[12]);
-
- bytes[6] = getHexValue(chars[14], chars[15]);
- bytes[7] = getHexValue(chars[16], chars[17]);
-
- bytes[8] = getHexValue(chars[19], chars[20]);
- bytes[9] = getHexValue(chars[21], chars[22]);
-
- bytes[10] = getHexValue(chars[24], chars[25]);
- bytes[11] = getHexValue(chars[26], chars[27]);
- bytes[12] = getHexValue(chars[28], chars[29]);
- bytes[13] = getHexValue(chars[30], chars[31]);
- bytes[14] = getHexValue(chars[32], chars[33]);
- bytes[15] = getHexValue(chars[34], chars[35]);
+ bytes[0] = getHexValue( chars[0], chars[1] );
+ bytes[1] = getHexValue( chars[2], chars[3] );
+ bytes[2] = getHexValue( chars[4], chars[5] );
+ bytes[3] = getHexValue( chars[6], chars[7] );
+
+ bytes[4] = getHexValue( chars[9], chars[10] );
+ bytes[5] = getHexValue( chars[11], chars[12] );
+
+ bytes[6] = getHexValue( chars[14], chars[15] );
+ bytes[7] = getHexValue( chars[16], chars[17] );
+
+ bytes[8] = getHexValue( chars[19], chars[20] );
+ bytes[9] = getHexValue( chars[21], chars[22] );
+
+ bytes[10] = getHexValue( chars[24], chars[25] );
+ bytes[11] = getHexValue( chars[26], chars[27] );
+ bytes[12] = getHexValue( chars[28], chars[29] );
+ bytes[13] = getHexValue( chars[30], chars[31] );
+ bytes[14] = getHexValue( chars[32], chars[33] );
+ bytes[15] = getHexValue( chars[34], chars[35] );
return bytes;
}
@@ -2000,8 +2105,8 @@ public final class Strings
return copy;
}
-
-
+
+
/**
* From commons-httpclients. Converts the byte array of HTTP content
* characters to a string. If the specified charset is not supported,
Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/SynchronizedLRUMap.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/SynchronizedLRUMap.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/SynchronizedLRUMap.java (original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/SynchronizedLRUMap.java Tue Jan 24 17:44:03 2012
@@ -73,7 +73,7 @@ public final class SynchronizedLRUMap ex
* @param i
* Maximum capacity of the LRUMap
*/
- public SynchronizedLRUMap(int i)
+ public SynchronizedLRUMap( int i )
{
super( i );
maximumSize = i;
Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/Unicode.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/Unicode.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/Unicode.java (original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/Unicode.java Tue Jan 24 17:44:03 2012
@@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
+
/**
* Various unicode manipulation methods that are more efficient then chaining
* operations: all is done in the same buffer without creating a bunch of string
@@ -81,6 +82,7 @@ public final class Unicode
}
}
+
/**
* Return the Unicode char which is coded in the bytes at position 0.
*
@@ -92,6 +94,7 @@ public final class Unicode
return bytesToChar( bytes, 0 );
}
+
/**
* Return the Unicode char which is coded in the bytes at the given
* position.
@@ -118,15 +121,15 @@ public final class Unicode
{
// Two bytes char
return ( char ) ( ( ( bytes[pos] & 0x1C ) << 6 ) + // 110x-xxyy
- // 10zz-zzzz
- // ->
- // 0000-0xxx
- // 0000-0000
+ // 10zz-zzzz
+ // ->
+ // 0000-0xxx
+ // 0000-0000
( ( bytes[pos] & 0x03 ) << 6 ) + // 110x-xxyy 10zz-zzzz
- // -> 0000-0000
- // yy00-0000
+ // -> 0000-0000
+ // yy00-0000
( bytes[pos + 1] & 0x3F ) // 110x-xxyy 10zz-zzzz -> 0000-0000
- // 00zz-zzzz
+ // 00zz-zzzz
); // -> 0000-0xxx yyzz-zzzz (07FF)
}
else if ( ( bytes[pos] & UnicodeConstants.UTF8_THREE_BYTES_MASK ) == UnicodeConstants.UTF8_THREE_BYTES )
@@ -139,8 +142,8 @@ public final class Unicode
+ ( ( bytes[pos + 1] & 0x3C ) << 6 )
// 1110-tttt 10xx-xxyy 10zz-zzzz -> 0000-0000-yy00-0000
+ ( ( bytes[pos + 1] & 0x03 ) << 6 )
- // 1110-tttt 10xx-xxyy 10zz-zzzz -> 0000-0000-00zz-zzzz
- + ( bytes[pos + 2] & 0x3F )
+ // 1110-tttt 10xx-xxyy 10zz-zzzz -> 0000-0000-00zz-zzzz
+ + ( bytes[pos + 2] & 0x3F )
// -> tttt-xxxx yyzz-zzzz (FF FF)
);
}
@@ -163,9 +166,9 @@ public final class Unicode
// 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000
// 0000-0000 yy00-0000
+ ( ( bytes[pos + 2] & 0x03 ) << 6 )
- // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000
- // 0000-0000 00zz-zzzz
- + ( bytes[pos + 3] & 0x3F )
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000
+ // 0000-0000 00zz-zzzz
+ + ( bytes[pos + 3] & 0x3F )
// -> 000t-ttuu vvvv-xxxx yyzz-zzzz (1FFFFF)
);
}
@@ -191,9 +194,9 @@ public final class Unicode
// 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
// 0000-0000 0000-0000 0000-0000 yy00-0000
+ ( ( bytes[pos + 3] & 0x03 ) << 6 )
- // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
- // 0000-0000 0000-0000 0000-0000 00zz-zzzz
- + ( bytes[pos + 4] & 0x3F )
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-0000 0000-0000 00zz-zzzz
+ + ( bytes[pos + 4] & 0x3F )
// -> 0000-00tt uuuu-uuvv wwww-xxxx yyzz-zzzz (03 FF FF FF)
);
}
@@ -229,10 +232,10 @@ public final class Unicode
// 10zz-zzzz ->
// 0000-0000 0000-0000 0000-0000 yy00-0000
+ ( ( bytes[pos + 4] & 0x03 ) << 6 )
- // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz
- // ->
- // 0000-0000 0000-0000 0000-0000 00zz-zzzz
- + ( bytes[pos + 5] & 0x3F )
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz
+ // ->
+ // 0000-0000 0000-0000 0000-0000 00zz-zzzz
+ + ( bytes[pos + 5] & 0x3F )
// -> 0stt-tttt uuuu-uuvv wwww-xxxx yyzz-zzzz (7F FF FF FF)
);
}
@@ -243,6 +246,7 @@ public final class Unicode
}
}
+
/**
* Return the number of bytes that hold an Unicode char.
*
@@ -282,6 +286,7 @@ public final class Unicode
}
}
+
/**
* Count the number of bytes included in the given char[].
*
@@ -311,6 +316,7 @@ public final class Unicode
return nbBytes;
}
+
/**
* Count the number of chars included in the given byte[].
*
@@ -329,13 +335,14 @@ public final class Unicode
while ( currentPos < bytes.length )
{
- currentPos += countBytesPerChar(bytes, currentPos);
+ currentPos += countBytesPerChar( bytes, currentPos );
nbChars++;
}
return nbChars;
}
+
/**
* Return the Unicode char which is coded in the bytes at the given
* position.
@@ -348,7 +355,7 @@ public final class Unicode
*/
public static byte[] charToBytes( char car )
{
- byte[] bytes = new byte[countNbBytesPerChar(car)];
+ byte[] bytes = new byte[countNbBytesPerChar( car )];
if ( car <= 0x7F )
{
@@ -373,6 +380,7 @@ public final class Unicode
return bytes;
}
+
/**
* Check if the current char is in the unicodeSubset : all chars but
* '\0', '(', ')', '*' and '\'
@@ -393,6 +401,7 @@ public final class Unicode
return ( ( c > 127 ) || UnicodeConstants.UNICODE_SUBSET[c] );
}
+
/**
* Check if the current char is in the unicodeSubset : all chars but
* '\0', '(', ')', '*' and '\'
@@ -405,6 +414,7 @@ public final class Unicode
return ( ( c > 127 ) || UnicodeConstants.UNICODE_SUBSET[c] );
}
+
/**
*
* Writes four bytes of length information to the output stream, followed by the modified UTF-8 representation
@@ -456,6 +466,7 @@ public final class Unicode
}
}
+
/**
*
* Reads in a string that has been encoded using a modified UTF-8 format. The general contract of readUTF is
Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/UnixCrypt.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/UnixCrypt.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/UnixCrypt.java (original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/UnixCrypt.java Tue Jan 24 17:44:03 2012
@@ -40,474 +40,594 @@
package org.apache.directory.shared.util;
+
import org.apache.directory.shared.i18n.I18n;
- /*
- * @(#)UnixCrypt.java 0.9 96/11/25
- *
- * Copyright (c) 1996 Aki Yoshida. All rights reserved.
- *
- * Permission to use, copy, modify and distribute this software
- * for non-commercial or commercial purposes and without fee is
- * hereby granted provided that this copyright notice appears in
- * all copies.
- */
-
- /**
- * Unix crypt(3C) utility
- *
- * @author Aki Yoshida
- * 2001
- * by Iris Van den Broeke, Daniel Deville
- */
-
-
- /* ------------------------------------------------------------ */
- /** Unix Crypt.
- * Implements the one way cryptography used by Unix systems for
- * simple password protection.
- * @author Greg Wilkins (gregw)
- */
- public class UnixCrypt extends Object
- {
-
- /* (mostly) Standard DES Tables from Tom Truscott */
- private static final byte[] IP = { /* initial permutation */
- 58, 50, 42, 34, 26, 18, 10, 2,
- 60, 52, 44, 36, 28, 20, 12, 4,
- 62, 54, 46, 38, 30, 22, 14, 6,
- 64, 56, 48, 40, 32, 24, 16, 8,
- 57, 49, 41, 33, 25, 17, 9, 1,
- 59, 51, 43, 35, 27, 19, 11, 3,
- 61, 53, 45, 37, 29, 21, 13, 5,
- 63, 55, 47, 39, 31, 23, 15, 7};
-
- /* The final permutation is the inverse of IP - no table is necessary */
- private static final byte[] ExpandTr = { /* expansion operation */
- 32, 1, 2, 3, 4, 5,
- 4, 5, 6, 7, 8, 9,
- 8, 9, 10, 11, 12, 13,
- 12, 13, 14, 15, 16, 17,
- 16, 17, 18, 19, 20, 21,
- 20, 21, 22, 23, 24, 25,
- 24, 25, 26, 27, 28, 29,
- 28, 29, 30, 31, 32, 1};
-
- private static final byte[] PC1 = { /* permuted choice table 1 */
- 57, 49, 41, 33, 25, 17, 9,
- 1, 58, 50, 42, 34, 26, 18,
- 10, 2, 59, 51, 43, 35, 27,
- 19, 11, 3, 60, 52, 44, 36,
-
- 63, 55, 47, 39, 31, 23, 15,
- 7, 62, 54, 46, 38, 30, 22,
- 14, 6, 61, 53, 45, 37, 29,
- 21, 13, 5, 28, 20, 12, 4};
-
- private static final byte[] Rotates = { /* PC1 rotation schedule */
- 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
-
-
- private static final byte[] PC2 = { /* permuted choice table 2 */
- 9, 18, 14, 17, 11, 24, 1, 5,
- 22, 25, 3, 28, 15, 6, 21, 10,
- 35, 38, 23, 19, 12, 4, 26, 8,
- 43, 54, 16, 7, 27, 20, 13, 2,
-
- 0, 0, 41, 52, 31, 37, 47, 55,
- 0, 0, 30, 40, 51, 45, 33, 48,
- 0, 0, 44, 49, 39, 56, 34, 53,
- 0, 0, 46, 42, 50, 36, 29, 32};
-
- private static final byte[][] S = { /* 48->32 bit substitution tables */
- /* S[1] */
- {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
- 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
- 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
- 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13},
- /* S[2] */
- {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
- 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
- 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
- 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
- /* S[3] */
- {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
- 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
- 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
- 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
- /* S[4] */
- {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
- 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
- 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
- 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
- /* S[5] */
- {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
- 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
- 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
- 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
- /* S[6] */
- {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
- 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
- 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
- 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
- /* S[7] */
- {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
- 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
- 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
- 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
- /* S[8] */
- {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
- 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
- 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
- 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}};
-
- private static final byte[] P32Tr = { /* 32-bit permutation function */
- 16, 7, 20, 21,
- 29, 12, 28, 17,
- 1, 15, 23, 26,
- 5, 18, 31, 10,
- 2, 8, 24, 14,
- 32, 27, 3, 9,
- 19, 13, 30, 6,
- 22, 11, 4, 25};
-
- private static final byte[] CIFP = { /* compressed/interleaved permutation */
- 1, 2, 3, 4, 17, 18, 19, 20,
- 5, 6, 7, 8, 21, 22, 23, 24,
- 9, 10, 11, 12, 25, 26, 27, 28,
- 13, 14, 15, 16, 29, 30, 31, 32,
-
- 33, 34, 35, 36, 49, 50, 51, 52,
- 37, 38, 39, 40, 53, 54, 55, 56,
- 41, 42, 43, 44, 57, 58, 59, 60,
- 45, 46, 47, 48, 61, 62, 63, 64};
-
- private static final byte[] ITOA64 = { /* 0..63 => ascii-64 */
- (byte)'.',(byte) '/',(byte) '0',(byte) '1',(byte) '2',(byte) '3',(byte) '4',(byte) '5',
- (byte)'6',(byte) '7',(byte) '8',(byte) '9',(byte) 'A',(byte) 'B',(byte) 'C',(byte) 'D',
- (byte)'E',(byte) 'F',(byte) 'G',(byte) 'H',(byte) 'I',(byte) 'J',(byte) 'K',(byte) 'L',
- (byte)'M',(byte) 'N',(byte) 'O',(byte) 'P',(byte) 'Q',(byte) 'R',(byte) 'S',(byte) 'T',
- (byte)'U',(byte) 'V',(byte) 'W',(byte) 'X',(byte) 'Y',(byte) 'Z',(byte) 'a',(byte) 'b',
- (byte)'c',(byte) 'd',(byte) 'e',(byte) 'f',(byte) 'g',(byte) 'h',(byte) 'i',(byte) 'j',
- (byte)'k',(byte) 'l',(byte) 'm',(byte) 'n',(byte) 'o',(byte) 'p',(byte) 'q',(byte) 'r',
- (byte)'s',(byte) 't',(byte) 'u',(byte) 'v',(byte) 'w',(byte) 'x',(byte) 'y',(byte) 'z'};
-
- /* ===== Tables that are initialized at run time ==================== */
-
- private static byte[] A64TOI = new byte[128]; /* ascii-64 => 0..63 */
-
- /* Initial key schedule permutation */
- private static long[][] PC1ROT = new long[16][16];
-
- /* Subsequent key schedule rotation permutations */
- private static long[][][] PC2ROT = new long[2][16][16];
-
- /* Initial permutation/expansion table */
- private static long[][] IE3264 = new long[8][16];
-
- /* Table that combines the S, P, and E operations. */
- private static long[][] SPE = new long[8][64];
-
- /* compressed/interleaved => final permutation table */
- private static long[][] CF6464 = new long[16][16];
-
-
- /* ==================================== */
-
- static {
- byte[] perm = new byte[64];
- byte[] temp = new byte[64];
-
- // inverse table.
- for (int i=0; i<64; i++) A64TOI[ITOA64[i]] = (byte)i;
-
- // PC1ROT - bit reverse, then PC1, then Rotate, then PC2
- for (int i=0; i<64; i++) perm[i] = (byte)0;
- for (int i=0; i<64; i++) {
- int k;
- if ((k = PC2[i]) == 0) continue;
- k += Rotates[0]-1;
- if ((k%28) < Rotates[0]) k -= 28;
- k = PC1[k];
- if (k > 0) {
- k--;
- k = (k|0x07) - (k&0x07);
- k++;
- }
- perm[i] = (byte)k;
- }
- init_perm(PC1ROT, perm, 8);
-
- // PC2ROT - PC2 inverse, then Rotate, then PC2
- for (int j=0; j<2; j++) {
- int k;
- for (int i=0; i<64; i++) perm[i] = temp[i] = 0;
- for (int i=0; i<64; i++) {
- if ((k = PC2[i]) == 0) continue;
- temp[k-1] = (byte)(i+1);
- }
- for (int i=0; i<64; i++) {
- if ((k = PC2[i]) == 0) continue;
- k += j;
- if ((k%28) <= j) k -= 28;
- perm[i] = temp[k];
- }
-
- init_perm(PC2ROT[j], perm, 8);
- }
-
- // Bit reverse, intial permupation, expantion
- for (int i=0; i<8; i++) {
- for (int j=0; j<8; j++) {
- int k = (j < 2)? 0: IP[ExpandTr[i*6+j-2]-1];
- if (k > 32) k -= 32;
- else if (k > 0) k--;
- if (k > 0) {
- k--;
- k = (k|0x07) - (k&0x07);
- k++;
- }
- perm[i*8+j] = (byte)k;
- }
- }
-
- init_perm(IE3264, perm, 8);
-
- // Compression, final permutation, bit reverse
- for (int i=0; i<64; i++) {
- int k = IP[CIFP[i]-1];
- if (k > 0) {
- k--;
- k = (k|0x07) - (k&0x07);
- k++;
- }
- perm[k-1] = (byte)(i+1);
- }
-
- init_perm(CF6464, perm, 8);
-
- // SPE table
- for (int i=0; i<48; i++)
- perm[i] = P32Tr[ExpandTr[i]-1];
- for (int t=0; t<8; t++) {
- for (int j=0; j<64; j++) {
- int k = (((j >> 0) & 0x01) << 5) | (((j >> 1) & 0x01) << 3) |
- (((j >> 2) & 0x01) << 2) | (((j >> 3) & 0x01) << 1) |
- (((j >> 4) & 0x01) << 0) | (((j >> 5) & 0x01) << 4);
- k = S[t][k];
- k = (((k >> 3) & 0x01) << 0) | (((k >> 2) & 0x01) << 1) |
- (((k >> 1) & 0x01) << 2) | (((k >> 0) & 0x01) << 3);
- for (int i=0; i<32; i++) temp[i] = 0;
- for (int i=0; i<4; i++) temp[4*t+i] = (byte)((k >> i) & 0x01);
- long kk = 0;
- for (int i=24; --i>=0; ) kk = ((kk<<1) |
- ((long)temp[perm[i]-1])<<32 |
- (temp[perm[i+24]-1]));
-
- SPE[t][j] = to_six_bit(kk);
- }
- }
- }
-
- /**
- * You can't call the constructer.
- */
- private UnixCrypt() { }
-
- /**
- * Returns the transposed and split code of a 24-bit code
- * into a 4-byte code, each having 6 bits.
- */
- private static int to_six_bit(int num) {
- return (((num << 26) & 0xfc000000) | ((num << 12) & 0xfc0000) |
- ((num >> 2) & 0xfc00) | ((num >> 16) & 0xfc));
- }
-
- /**
- * Returns the transposed and split code of two 24-bit code
- * into two 4-byte code, each having 6 bits.
- */
- private static long to_six_bit(long num) {
- return (((num << 26) & 0xfc000000fc000000L) | ((num << 12) & 0xfc000000fc0000L) |
- ((num >> 2) & 0xfc000000fc00L) | ((num >> 16) & 0xfc000000fcL));
- }
-
- /**
- * Returns the permutation of the given 64-bit code with
- * the specified permutataion table.
- */
- private static long perm6464(long c, long[][]p) {
- long out = 0L;
- for (int i=8; --i>=0; ) {
- int t = (int)(0x00ff & c);
- c >>= 8;
- long tp = p[i<<1][t&0x0f];
- out |= tp;
- tp = p[(i<<1)+1][t>>4];
- out |= tp;
- }
- return out;
- }
-
- /**
- * Returns the permutation of the given 32-bit code with
- * the specified permutataion table.
- */
- private static long perm3264(int c, long[][]p) {
- long out = 0L;
- for (int i=4; --i>=0; ) {
- int t = (0x00ff & c);
- c >>= 8;
- long tp = p[i<<1][t&0x0f];
- out |= tp;
- tp = p[(i<<1)+1][t>>4];
- out |= tp;
- }
- return out;
- }
-
- /**
- * Returns the key schedule for the given key.
- */
- private static long[] des_setkey(long keyword) {
- long K = perm6464(keyword, PC1ROT);
- long[] KS = new long[16];
- KS[0] = K&~0x0303030300000000L;
-
- for (int i=1; i<16; i++) {
- KS[i] = K;
- K = perm6464(K, PC2ROT[Rotates[i]-1]);
-
- KS[i] = K&~0x0303030300000000L;
- }
- return KS;
- }
-
- /**
- * Returns the DES encrypted code of the given word with the specified
- * environment.
- */
- private static long des_cipher(long in, int salt, int num_iter, long[] KS) {
- salt = to_six_bit(salt);
- long L = in;
- long R = L;
- L &= 0x5555555555555555L;
- R = (R & 0xaaaaaaaa00000000L) | ((R >> 1) & 0x0000000055555555L);
- L = ((((L << 1) | (L << 32)) & 0xffffffff00000000L) |
- ((R | (R >> 32)) & 0x00000000ffffffffL));
-
- L = perm3264((int)(L>>32), IE3264);
- R = perm3264((int)(L&0xffffffff), IE3264);
-
- while (--num_iter >= 0) {
- for (int loop_count=0; loop_count<8; loop_count++) {
- long kp;
- long B;
- long k;
-
- kp = KS[(loop_count<<1)];
- k = ((R>>32) ^ R) & salt & 0xffffffffL;
- k |= (k<<32);
- B = (k ^ R ^ kp);
-
- L ^= (SPE[0][(int)((B>>58)&0x3f)] ^ SPE[1][(int)((B>>50)&0x3f)] ^
- SPE[2][(int)((B>>42)&0x3f)] ^ SPE[3][(int)((B>>34)&0x3f)] ^
- SPE[4][(int)((B>>26)&0x3f)] ^ SPE[5][(int)((B>>18)&0x3f)] ^
- SPE[6][(int)((B>>10)&0x3f)] ^ SPE[7][(int)((B>>2)&0x3f)]);
-
- kp = KS[(loop_count<<1)+1];
- k = ((L>>32) ^ L) & salt & 0xffffffffL;
- k |= (k<<32);
- B = (k ^ L ^ kp);
-
- R ^= (SPE[0][(int)((B>>58)&0x3f)] ^ SPE[1][(int)((B>>50)&0x3f)] ^
- SPE[2][(int)((B>>42)&0x3f)] ^ SPE[3][(int)((B>>34)&0x3f)] ^
- SPE[4][(int)((B>>26)&0x3f)] ^ SPE[5][(int)((B>>18)&0x3f)] ^
- SPE[6][(int)((B>>10)&0x3f)] ^ SPE[7][(int)((B>>2)&0x3f)]);
- }
- // swap L and R
- L ^= R;
- R ^= L;
- L ^= R;
- }
- L = ((((L>>35) & 0x0f0f0f0fL) | (((L&0xffffffff)<<1) & 0xf0f0f0f0L))<<32 |
- (((R>>35) & 0x0f0f0f0fL) | (((R&0xffffffff)<<1) & 0xf0f0f0f0L)));
-
- L = perm6464(L, CF6464);
-
- return L;
- }
-
- /**
- * Initializes the given permutation table with the mapping table.
- */
- private static void init_perm(long[][] perm, byte[] p,int chars_out) {
- for (int k=0; k<chars_out*8; k++) {
-
- int l = p[k] - 1;
- if (l < 0) continue;
- int i = l>>2;
- l = 1<<(l&0x03);
- for (int j=0; j<16; j++) {
- int s = ((k&0x07)+((7-(k>>3))<<3));
- if ((j & l) != 0x00) perm[i][j] |= (1L<<s);
- }
- }
- }
-
- /**
- * Encrypts String into crypt (Unix) code.
- * @param key the key to be encrypted
- * @param setting the salt to be used
- * @return the encrypted String
- */
- @SuppressWarnings("deprecation")
- public static String crypt(String key, String setting)
- {
- long constdatablock = 0L; /* encryption constant */
- byte[] cryptresult = new byte[13]; /* encrypted result */
- long keyword = 0L;
- /* invalid parameters! */
- if(key==null||setting==null)
- return "*"; // will NOT match under ANY circumstances!
-
- int keylen = key.length();
-
- for (int i=0; i<8 ; i++) {
- keyword = (keyword << 8) | ((i < keylen)? 2*key.charAt(i): 0);
- }
-
- long[] KS = des_setkey(keyword);
-
- int salt = 0;
- for (int i=2; --i>=0;) {
- char c = (i < setting.length())? setting.charAt(i): '.';
- cryptresult[i] = (byte)c;
- salt = (salt<<6) | (0x00ff&A64TOI[c]);
- }
-
- long rsltblock = des_cipher(constdatablock, salt, 25, KS);
-
- cryptresult[12] = ITOA64[(((int)rsltblock)<<2)&0x3f];
- rsltblock >>= 4;
- for (int i=12; --i>=2; ) {
- cryptresult[i] = ITOA64[((int)rsltblock)&0x3f];
- rsltblock >>= 6;
- }
-
- return new String(cryptresult, 0x00, 0, 13);
- }
-
- public static void main(String[] arg)
- {
- if (arg.length!=2)
- {
- System.err.println( I18n.err( I18n.ERR_04439 ) );
- System.exit(1);
- }
-
- System.err.println( I18n.err( I18n.ERR_04440, crypt(arg[0],arg[1]) ) );
- }
-
- }
+/*
+ * @(#)UnixCrypt.java 0.9 96/11/25
+ *
+ * Copyright (c) 1996 Aki Yoshida. All rights reserved.
+ *
+ * Permission to use, copy, modify and distribute this software
+ * for non-commercial or commercial purposes and without fee is
+ * hereby granted provided that this copyright notice appears in
+ * all copies.
+*/
+
+/**
+ * Unix crypt(3C) utility
+ *
+ * @author Aki Yoshida
+ * 2001
+ * by Iris Van den Broeke, Daniel Deville
+ */
+
+/* ------------------------------------------------------------ */
+/** Unix Crypt.
+ * Implements the one way cryptography used by Unix systems for
+ * simple password protection.
+ * @author Greg Wilkins (gregw)
+ */
+public class UnixCrypt extends Object
+{
+
+ /* (mostly) Standard DES Tables from Tom Truscott */
+ private static final byte[] IP =
+ { /* initial permutation */
+ 58, 50, 42, 34, 26, 18, 10, 2,
+ 60, 52, 44, 36, 28, 20, 12, 4,
+ 62, 54, 46, 38, 30, 22, 14, 6,
+ 64, 56, 48, 40, 32, 24, 16, 8,
+ 57, 49, 41, 33, 25, 17, 9, 1,
+ 59, 51, 43, 35, 27, 19, 11, 3,
+ 61, 53, 45, 37, 29, 21, 13, 5,
+ 63, 55, 47, 39, 31, 23, 15, 7 };
+
+ /* The final permutation is the inverse of IP - no table is necessary */
+ private static final byte[] ExpandTr =
+ { /* expansion operation */
+ 32, 1, 2, 3, 4, 5,
+ 4, 5, 6, 7, 8, 9,
+ 8, 9, 10, 11, 12, 13,
+ 12, 13, 14, 15, 16, 17,
+ 16, 17, 18, 19, 20, 21,
+ 20, 21, 22, 23, 24, 25,
+ 24, 25, 26, 27, 28, 29,
+ 28, 29, 30, 31, 32, 1 };
+
+ private static final byte[] PC1 =
+ { /* permuted choice table 1 */
+ 57, 49, 41, 33, 25, 17, 9,
+ 1, 58, 50, 42, 34, 26, 18,
+ 10, 2, 59, 51, 43, 35, 27,
+ 19, 11, 3, 60, 52, 44, 36,
+
+ 63, 55, 47, 39, 31, 23, 15,
+ 7, 62, 54, 46, 38, 30, 22,
+ 14, 6, 61, 53, 45, 37, 29,
+ 21, 13, 5, 28, 20, 12, 4 };
+
+ private static final byte[] Rotates =
+ { /* PC1 rotation schedule */
+ 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
+
+ private static final byte[] PC2 =
+ { /* permuted choice table 2 */
+ 9, 18, 14, 17, 11, 24, 1, 5,
+ 22, 25, 3, 28, 15, 6, 21, 10,
+ 35, 38, 23, 19, 12, 4, 26, 8,
+ 43, 54, 16, 7, 27, 20, 13, 2,
+
+ 0, 0, 41, 52, 31, 37, 47, 55,
+ 0, 0, 30, 40, 51, 45, 33, 48,
+ 0, 0, 44, 49, 39, 56, 34, 53,
+ 0, 0, 46, 42, 50, 36, 29, 32 };
+
+ private static final byte[][] S =
+ { /* 48->32 bit substitution tables */
+ /* S[1] */
+ { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
+ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
+ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
+ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 },
+ /* S[2] */
+ { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
+ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
+ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
+ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 },
+ /* S[3] */
+ { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
+ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
+ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
+ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 },
+ /* S[4] */
+ { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
+ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
+ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
+ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 },
+ /* S[5] */
+ { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
+ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
+ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
+ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 },
+ /* S[6] */
+ { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
+ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
+ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
+ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 },
+ /* S[7] */
+ { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
+ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
+ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
+ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 },
+ /* S[8] */
+ { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
+ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
+ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
+ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };
+
+ private static final byte[] P32Tr =
+ { /* 32-bit permutation function */
+ 16, 7, 20, 21,
+ 29, 12, 28, 17,
+ 1, 15, 23, 26,
+ 5, 18, 31, 10,
+ 2, 8, 24, 14,
+ 32, 27, 3, 9,
+ 19, 13, 30, 6,
+ 22, 11, 4, 25 };
+
+ private static final byte[] CIFP =
+ { /* compressed/interleaved permutation */
+ 1, 2, 3, 4, 17, 18, 19, 20,
+ 5, 6, 7, 8, 21, 22, 23, 24,
+ 9, 10, 11, 12, 25, 26, 27, 28,
+ 13, 14, 15, 16, 29, 30, 31, 32,
+
+ 33, 34, 35, 36, 49, 50, 51, 52,
+ 37, 38, 39, 40, 53, 54, 55, 56,
+ 41, 42, 43, 44, 57, 58, 59, 60,
+ 45, 46, 47, 48, 61, 62, 63, 64 };
+
+ private static final byte[] ITOA64 =
+ { /* 0..63 => ascii-64 */
+ ( byte ) '.',
+ ( byte ) '/',
+ ( byte ) '0',
+ ( byte ) '1',
+ ( byte ) '2',
+ ( byte ) '3',
+ ( byte ) '4',
+ ( byte ) '5',
+ ( byte ) '6',
+ ( byte ) '7',
+ ( byte ) '8',
+ ( byte ) '9',
+ ( byte ) 'A',
+ ( byte ) 'B',
+ ( byte ) 'C',
+ ( byte ) 'D',
+ ( byte ) 'E',
+ ( byte ) 'F',
+ ( byte ) 'G',
+ ( byte ) 'H',
+ ( byte ) 'I',
+ ( byte ) 'J',
+ ( byte ) 'K',
+ ( byte ) 'L',
+ ( byte ) 'M',
+ ( byte ) 'N',
+ ( byte ) 'O',
+ ( byte ) 'P',
+ ( byte ) 'Q',
+ ( byte ) 'R',
+ ( byte ) 'S',
+ ( byte ) 'T',
+ ( byte ) 'U',
+ ( byte ) 'V',
+ ( byte ) 'W',
+ ( byte ) 'X',
+ ( byte ) 'Y',
+ ( byte ) 'Z',
+ ( byte ) 'a',
+ ( byte ) 'b',
+ ( byte ) 'c',
+ ( byte ) 'd',
+ ( byte ) 'e',
+ ( byte ) 'f',
+ ( byte ) 'g',
+ ( byte ) 'h',
+ ( byte ) 'i',
+ ( byte ) 'j',
+ ( byte ) 'k',
+ ( byte ) 'l',
+ ( byte ) 'm',
+ ( byte ) 'n',
+ ( byte ) 'o',
+ ( byte ) 'p',
+ ( byte ) 'q',
+ ( byte ) 'r',
+ ( byte ) 's',
+ ( byte ) 't',
+ ( byte ) 'u',
+ ( byte ) 'v',
+ ( byte ) 'w',
+ ( byte ) 'x',
+ ( byte ) 'y',
+ ( byte ) 'z' };
+
+ /* ===== Tables that are initialized at run time ==================== */
+
+ private static byte[] A64TOI = new byte[128]; /* ascii-64 => 0..63 */
+
+ /* Initial key schedule permutation */
+ private static long[][] PC1ROT = new long[16][16];
+
+ /* Subsequent key schedule rotation permutations */
+ private static long[][][] PC2ROT = new long[2][16][16];
+
+ /* Initial permutation/expansion table */
+ private static long[][] IE3264 = new long[8][16];
+
+ /* Table that combines the S, P, and E operations. */
+ private static long[][] SPE = new long[8][64];
+
+ /* compressed/interleaved => final permutation table */
+ private static long[][] CF6464 = new long[16][16];
+
+ /* ==================================== */
+
+ static
+ {
+ byte[] perm = new byte[64];
+ byte[] temp = new byte[64];
+
+ // inverse table.
+ for ( int i = 0; i < 64; i++ )
+ A64TOI[ITOA64[i]] = ( byte ) i;
+
+ // PC1ROT - bit reverse, then PC1, then Rotate, then PC2
+ for ( int i = 0; i < 64; i++ )
+ perm[i] = ( byte ) 0;
+ for ( int i = 0; i < 64; i++ )
+ {
+ int k;
+ if ( ( k = PC2[i] ) == 0 )
+ continue;
+ k += Rotates[0] - 1;
+ if ( ( k % 28 ) < Rotates[0] )
+ k -= 28;
+ k = PC1[k];
+ if ( k > 0 )
+ {
+ k--;
+ k = ( k | 0x07 ) - ( k & 0x07 );
+ k++;
+ }
+ perm[i] = ( byte ) k;
+ }
+ init_perm( PC1ROT, perm, 8 );
+
+ // PC2ROT - PC2 inverse, then Rotate, then PC2
+ for ( int j = 0; j < 2; j++ )
+ {
+ int k;
+ for ( int i = 0; i < 64; i++ )
+ perm[i] = temp[i] = 0;
+ for ( int i = 0; i < 64; i++ )
+ {
+ if ( ( k = PC2[i] ) == 0 )
+ continue;
+ temp[k - 1] = ( byte ) ( i + 1 );
+ }
+ for ( int i = 0; i < 64; i++ )
+ {
+ if ( ( k = PC2[i] ) == 0 )
+ continue;
+ k += j;
+ if ( ( k % 28 ) <= j )
+ k -= 28;
+ perm[i] = temp[k];
+ }
+
+ init_perm( PC2ROT[j], perm, 8 );
+ }
+
+ // Bit reverse, intial permupation, expantion
+ for ( int i = 0; i < 8; i++ )
+ {
+ for ( int j = 0; j < 8; j++ )
+ {
+ int k = ( j < 2 ) ? 0 : IP[ExpandTr[i * 6 + j - 2] - 1];
+ if ( k > 32 )
+ k -= 32;
+ else if ( k > 0 )
+ k--;
+ if ( k > 0 )
+ {
+ k--;
+ k = ( k | 0x07 ) - ( k & 0x07 );
+ k++;
+ }
+ perm[i * 8 + j] = ( byte ) k;
+ }
+ }
+
+ init_perm( IE3264, perm, 8 );
+
+ // Compression, final permutation, bit reverse
+ for ( int i = 0; i < 64; i++ )
+ {
+ int k = IP[CIFP[i] - 1];
+ if ( k > 0 )
+ {
+ k--;
+ k = ( k | 0x07 ) - ( k & 0x07 );
+ k++;
+ }
+ perm[k - 1] = ( byte ) ( i + 1 );
+ }
+
+ init_perm( CF6464, perm, 8 );
+
+ // SPE table
+ for ( int i = 0; i < 48; i++ )
+ perm[i] = P32Tr[ExpandTr[i] - 1];
+ for ( int t = 0; t < 8; t++ )
+ {
+ for ( int j = 0; j < 64; j++ )
+ {
+ int k = ( ( ( j >> 0 ) & 0x01 ) << 5 ) | ( ( ( j >> 1 ) & 0x01 ) << 3 ) |
+ ( ( ( j >> 2 ) & 0x01 ) << 2 ) | ( ( ( j >> 3 ) & 0x01 ) << 1 ) |
+ ( ( ( j >> 4 ) & 0x01 ) << 0 ) | ( ( ( j >> 5 ) & 0x01 ) << 4 );
+ k = S[t][k];
+ k = ( ( ( k >> 3 ) & 0x01 ) << 0 ) | ( ( ( k >> 2 ) & 0x01 ) << 1 ) |
+ ( ( ( k >> 1 ) & 0x01 ) << 2 ) | ( ( ( k >> 0 ) & 0x01 ) << 3 );
+ for ( int i = 0; i < 32; i++ )
+ temp[i] = 0;
+ for ( int i = 0; i < 4; i++ )
+ temp[4 * t + i] = ( byte ) ( ( k >> i ) & 0x01 );
+ long kk = 0;
+ for ( int i = 24; --i >= 0; )
+ kk = ( ( kk << 1 ) |
+ ( ( long ) temp[perm[i] - 1] ) << 32 |
+ ( temp[perm[i + 24] - 1] ) );
+
+ SPE[t][j] = to_six_bit( kk );
+ }
+ }
+ }
+
+
+ /**
+ * You can't call the constructer.
+ */
+ private UnixCrypt()
+ {
+ }
+
+
+ /**
+ * Returns the transposed and split code of a 24-bit code
+ * into a 4-byte code, each having 6 bits.
+ */
+ private static int to_six_bit( int num )
+ {
+ return ( ( ( num << 26 ) & 0xfc000000 ) | ( ( num << 12 ) & 0xfc0000 ) |
+ ( ( num >> 2 ) & 0xfc00 ) | ( ( num >> 16 ) & 0xfc ) );
+ }
+
+
+ /**
+ * Returns the transposed and split code of two 24-bit code
+ * into two 4-byte code, each having 6 bits.
+ */
+ private static long to_six_bit( long num )
+ {
+ return ( ( ( num << 26 ) & 0xfc000000fc000000L ) | ( ( num << 12 ) & 0xfc000000fc0000L ) |
+ ( ( num >> 2 ) & 0xfc000000fc00L ) | ( ( num >> 16 ) & 0xfc000000fcL ) );
+ }
+
+
+ /**
+ * Returns the permutation of the given 64-bit code with
+ * the specified permutataion table.
+ */
+ private static long perm6464( long c, long[][] p )
+ {
+ long out = 0L;
+ for ( int i = 8; --i >= 0; )
+ {
+ int t = ( int ) ( 0x00ff & c );
+ c >>= 8;
+ long tp = p[i << 1][t & 0x0f];
+ out |= tp;
+ tp = p[( i << 1 ) + 1][t >> 4];
+ out |= tp;
+ }
+ return out;
+ }
+
+
+ /**
+ * Returns the permutation of the given 32-bit code with
+ * the specified permutataion table.
+ */
+ private static long perm3264( int c, long[][] p )
+ {
+ long out = 0L;
+ for ( int i = 4; --i >= 0; )
+ {
+ int t = ( 0x00ff & c );
+ c >>= 8;
+ long tp = p[i << 1][t & 0x0f];
+ out |= tp;
+ tp = p[( i << 1 ) + 1][t >> 4];
+ out |= tp;
+ }
+ return out;
+ }
+
+
+ /**
+ * Returns the key schedule for the given key.
+ */
+ private static long[] des_setkey( long keyword )
+ {
+ long K = perm6464( keyword, PC1ROT );
+ long[] KS = new long[16];
+ KS[0] = K & ~0x0303030300000000L;
+
+ for ( int i = 1; i < 16; i++ )
+ {
+ KS[i] = K;
+ K = perm6464( K, PC2ROT[Rotates[i] - 1] );
+
+ KS[i] = K & ~0x0303030300000000L;
+ }
+ return KS;
+ }
+
+
+ /**
+ * Returns the DES encrypted code of the given word with the specified
+ * environment.
+ */
+ private static long des_cipher( long in, int salt, int num_iter, long[] KS )
+ {
+ salt = to_six_bit( salt );
+ long L = in;
+ long R = L;
+ L &= 0x5555555555555555L;
+ R = ( R & 0xaaaaaaaa00000000L ) | ( ( R >> 1 ) & 0x0000000055555555L );
+ L = ( ( ( ( L << 1 ) | ( L << 32 ) ) & 0xffffffff00000000L ) |
+ ( ( R | ( R >> 32 ) ) & 0x00000000ffffffffL ) );
+
+ L = perm3264( ( int ) ( L >> 32 ), IE3264 );
+ R = perm3264( ( int ) ( L & 0xffffffff ), IE3264 );
+
+ while ( --num_iter >= 0 )
+ {
+ for ( int loop_count = 0; loop_count < 8; loop_count++ )
+ {
+ long kp;
+ long B;
+ long k;
+
+ kp = KS[( loop_count << 1 )];
+ k = ( ( R >> 32 ) ^ R ) & salt & 0xffffffffL;
+ k |= ( k << 32 );
+ B = ( k ^ R ^ kp );
+
+ L ^= ( SPE[0][( int ) ( ( B >> 58 ) & 0x3f )] ^ SPE[1][( int ) ( ( B >> 50 ) & 0x3f )] ^
+ SPE[2][( int ) ( ( B >> 42 ) & 0x3f )] ^ SPE[3][( int ) ( ( B >> 34 ) & 0x3f )] ^
+ SPE[4][( int ) ( ( B >> 26 ) & 0x3f )] ^ SPE[5][( int ) ( ( B >> 18 ) & 0x3f )] ^
+ SPE[6][( int ) ( ( B >> 10 ) & 0x3f )] ^ SPE[7][( int ) ( ( B >> 2 ) & 0x3f )] );
+
+ kp = KS[( loop_count << 1 ) + 1];
+ k = ( ( L >> 32 ) ^ L ) & salt & 0xffffffffL;
+ k |= ( k << 32 );
+ B = ( k ^ L ^ kp );
+
+ R ^= ( SPE[0][( int ) ( ( B >> 58 ) & 0x3f )] ^ SPE[1][( int ) ( ( B >> 50 ) & 0x3f )] ^
+ SPE[2][( int ) ( ( B >> 42 ) & 0x3f )] ^ SPE[3][( int ) ( ( B >> 34 ) & 0x3f )] ^
+ SPE[4][( int ) ( ( B >> 26 ) & 0x3f )] ^ SPE[5][( int ) ( ( B >> 18 ) & 0x3f )] ^
+ SPE[6][( int ) ( ( B >> 10 ) & 0x3f )] ^ SPE[7][( int ) ( ( B >> 2 ) & 0x3f )] );
+ }
+ // swap L and R
+ L ^= R;
+ R ^= L;
+ L ^= R;
+ }
+ L = ( ( ( ( L >> 35 ) & 0x0f0f0f0fL ) | ( ( ( L & 0xffffffff ) << 1 ) & 0xf0f0f0f0L ) ) << 32 |
+ ( ( ( R >> 35 ) & 0x0f0f0f0fL ) | ( ( ( R & 0xffffffff ) << 1 ) & 0xf0f0f0f0L ) ) );
+
+ L = perm6464( L, CF6464 );
+
+ return L;
+ }
+
+
+ /**
+ * Initializes the given permutation table with the mapping table.
+ */
+ private static void init_perm( long[][] perm, byte[] p, int chars_out )
+ {
+ for ( int k = 0; k < chars_out * 8; k++ )
+ {
+
+ int l = p[k] - 1;
+ if ( l < 0 )
+ continue;
+ int i = l >> 2;
+ l = 1 << ( l & 0x03 );
+ for ( int j = 0; j < 16; j++ )
+ {
+ int s = ( ( k & 0x07 ) + ( ( 7 - ( k >> 3 ) ) << 3 ) );
+ if ( ( j & l ) != 0x00 )
+ perm[i][j] |= ( 1L << s );
+ }
+ }
+ }
+
+
+ /**
+ * Encrypts String into crypt (Unix) code.
+ * @param key the key to be encrypted
+ * @param setting the salt to be used
+ * @return the encrypted String
+ */
+ @SuppressWarnings("deprecation")
+ public static String crypt( String key, String setting )
+ {
+ long constdatablock = 0L; /* encryption constant */
+ byte[] cryptresult = new byte[13]; /* encrypted result */
+ long keyword = 0L;
+ /* invalid parameters! */
+ if ( key == null || setting == null )
+ return "*"; // will NOT match under ANY circumstances!
+
+ int keylen = key.length();
+
+ for ( int i = 0; i < 8; i++ )
+ {
+ keyword = ( keyword << 8 ) | ( ( i < keylen ) ? 2 * key.charAt( i ) : 0 );
+ }
+
+ long[] KS = des_setkey( keyword );
+
+ int salt = 0;
+ for ( int i = 2; --i >= 0; )
+ {
+ char c = ( i < setting.length() ) ? setting.charAt( i ) : '.';
+ cryptresult[i] = ( byte ) c;
+ salt = ( salt << 6 ) | ( 0x00ff & A64TOI[c] );
+ }
+
+ long rsltblock = des_cipher( constdatablock, salt, 25, KS );
+
+ cryptresult[12] = ITOA64[( ( ( int ) rsltblock ) << 2 ) & 0x3f];
+ rsltblock >>= 4;
+ for ( int i = 12; --i >= 2; )
+ {
+ cryptresult[i] = ITOA64[( ( int ) rsltblock ) & 0x3f];
+ rsltblock >>= 6;
+ }
+
+ return new String( cryptresult, 0x00, 0, 13 );
+ }
+
+
+ public static void main( String[] arg )
+ {
+ if ( arg.length != 2 )
+ {
+ System.err.println( I18n.err( I18n.ERR_04439 ) );
+ System.exit( 1 );
+ }
+
+ System.err.println( I18n.err( I18n.ERR_04440, crypt( arg[0], arg[1] ) ) );
+ }
+
+}
Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/exception/MultiException.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/exception/MultiException.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/exception/MultiException.java (original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/exception/MultiException.java Tue Jan 24 17:44:03 2012
@@ -128,7 +128,7 @@ public class MultiException extends Exce
out.println( "Nested exceptions to follow:\n" );
boolean isFirst = true;
- for ( Throwable throwable:nestedExceptions )
+ for ( Throwable throwable : nestedExceptions )
{
if ( isFirst )
{
@@ -141,7 +141,7 @@ public class MultiException extends Exce
throwable.printStackTrace();
}
-
+
out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
}
@@ -160,7 +160,7 @@ public class MultiException extends Exce
out.println( "Nested exceptions to follow:\n" );
boolean isFirst = true;
- for ( Throwable throwable:nestedExceptions )
+ for ( Throwable throwable : nestedExceptions )
{
if ( isFirst )
{
@@ -173,7 +173,7 @@ public class MultiException extends Exce
throwable.printStackTrace();
}
-
+
out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
}
Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/exception/RuntimeMultiException.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/exception/RuntimeMultiException.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/exception/RuntimeMultiException.java (original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/shared/util/exception/RuntimeMultiException.java Tue Jan 24 17:44:03 2012
@@ -136,8 +136,8 @@ public class RuntimeMultiException exten
out.println( "Nested exceptions to follow:\n" );
boolean isFirst = true;
-
- for ( Throwable throwable:nestedExceptions )
+
+ for ( Throwable throwable : nestedExceptions )
{
if ( isFirst )
{
@@ -150,7 +150,7 @@ public class RuntimeMultiException exten
throwable.printStackTrace();
}
-
+
out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
}
@@ -169,7 +169,7 @@ public class RuntimeMultiException exten
out.println( "Nested exceptions to follow:\n" );
boolean isFirst = true;
- for ( Throwable throwable:nestedExceptions )
+ for ( Throwable throwable : nestedExceptions )
{
if ( isFirst )
{
@@ -182,7 +182,7 @@ public class RuntimeMultiException exten
throwable.printStackTrace();
}
-
+
out.println( "\n\t<<========= Last Nested Exception" + " ========>>\n" );
}
Modified: directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/ArrayEnumerationTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/ArrayEnumerationTest.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/ArrayEnumerationTest.java (original)
+++ directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/ArrayEnumerationTest.java Tue Jan 24 17:44:03 2012
@@ -33,6 +33,7 @@ import org.apache.directory.shared.util.
import org.junit.Test;
import org.junit.runner.RunWith;
+
/**
* Tests the ArrayEnumeration class.
*
@@ -51,13 +52,13 @@ public class ArrayEnumerationTest
ArrayEnumeration list = new ArrayEnumeration( array );
- assertFalse(list.hasMoreElements());
+ assertFalse( list.hasMoreElements() );
try
{
list.nextElement();
- fail("should never get here due to a NoSuchElementException");
+ fail( "should never get here due to a NoSuchElementException" );
}
catch ( NoSuchElementException e )
{
@@ -69,15 +70,15 @@ public class ArrayEnumerationTest
list = new ArrayEnumeration( array );
- assertFalse(list.hasMoreElements());
+ assertFalse( list.hasMoreElements() );
- assertFalse(list.hasMoreElements());
+ assertFalse( list.hasMoreElements() );
try
{
list.nextElement();
- fail("should never get here due to a NoSuchElementException");
+ fail( "should never get here due to a NoSuchElementException" );
}
catch ( NoSuchElementException e )
{
@@ -90,17 +91,17 @@ public class ArrayEnumerationTest
list = new ArrayEnumeration( array );
- assertTrue(list.hasMoreElements());
+ assertTrue( list.hasMoreElements() );
- assertNotNull(list.nextElement());
+ assertNotNull( list.nextElement() );
- assertFalse(list.hasMoreElements());
+ assertFalse( list.hasMoreElements() );
try
{
list.nextElement();
- fail("should never get here due to a NoSuchElementException");
+ fail( "should never get here due to a NoSuchElementException" );
}
catch ( NoSuchElementException e )
{
@@ -113,21 +114,21 @@ public class ArrayEnumerationTest
list = new ArrayEnumeration( array );
- assertTrue(list.hasMoreElements());
+ assertTrue( list.hasMoreElements() );
- assertNotNull(list.nextElement());
+ assertNotNull( list.nextElement() );
- assertTrue(list.hasMoreElements());
+ assertTrue( list.hasMoreElements() );
- assertNotNull(list.nextElement());
+ assertNotNull( list.nextElement() );
- assertFalse(list.hasMoreElements());
+ assertFalse( list.hasMoreElements() );
try
{
list.nextElement();
- fail("should never get here due to a NoSuchElementException");
+ fail( "should never get here due to a NoSuchElementException" );
}
catch ( NoSuchElementException e )
{
@@ -140,25 +141,25 @@ public class ArrayEnumerationTest
list = new ArrayEnumeration( array );
- assertTrue(list.hasMoreElements());
+ assertTrue( list.hasMoreElements() );
- assertNotNull(list.nextElement());
+ assertNotNull( list.nextElement() );
- assertTrue(list.hasMoreElements());
+ assertTrue( list.hasMoreElements() );
- assertNotNull(list.nextElement());
+ assertNotNull( list.nextElement() );
- assertTrue(list.hasMoreElements());
+ assertTrue( list.hasMoreElements() );
- assertNotNull(list.nextElement());
+ assertNotNull( list.nextElement() );
- assertFalse(list.hasMoreElements());
+ assertFalse( list.hasMoreElements() );
try
{
list.nextElement();
- fail("should never get here due to a NoSuchElementException");
+ fail( "should never get here due to a NoSuchElementException" );
}
catch ( NoSuchElementException e )
{
Modified: directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/Base32Test.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/Base32Test.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/Base32Test.java (original)
+++ directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/Base32Test.java Tue Jan 24 17:44:03 2012
@@ -19,6 +19,7 @@
*/
package org.apache.directory.shared.util;
+
import static org.junit.Assert.assertEquals;
import com.mycila.junit.concurrent.Concurrency;
@@ -27,6 +28,7 @@ import org.apache.directory.shared.util.
import org.junit.Test;
import org.junit.runner.RunWith;
+
@RunWith(ConcurrentJunitRunner.class)
@Concurrency()
public class Base32Test
@@ -34,16 +36,18 @@ public class Base32Test
@Test
public void testEncode()
{
- String[] data = new String[]{ "", "a", "ab", "abc", "abcd", "abcde", "abcdef" };
- String[] expected = new String[]{ "", "ME======", "MFRA====", "MFRGG===", "MFRGGZA=", "MFRGGZDF", "MFRGGZDFMY======"};
-
+ String[] data = new String[]
+ { "", "a", "ab", "abc", "abcd", "abcde", "abcdef" };
+ String[] expected = new String[]
+ { "", "ME======", "MFRA====", "MFRGG===", "MFRGGZA=", "MFRGGZDF", "MFRGGZDFMY======" };
+
for ( int i = 0; i < data.length; i++ )
{
String in = data[i];
-
- String res = Base32.encode(in);
-
- assertEquals(expected[i], res);
+
+ String res = Base32.encode( in );
+
+ assertEquals( expected[i], res );
}
}
}
Modified: directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/ByteBufferTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/ByteBufferTest.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/ByteBufferTest.java (original)
+++ directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/ByteBufferTest.java Tue Jan 24 17:44:03 2012
@@ -42,60 +42,63 @@ public class ByteBufferTest
public void testByteBufferGrowth()
{
ByteBuffer buf = new ByteBuffer();
- assertEquals(10, buf.capacity());
-
+ assertEquals( 10, buf.capacity() );
+
for ( int ii = 0; ii < 20; ii++ )
{
buf.append( ii );
- assertEquals(ii, buf.get(ii));
- assertEquals(ii, buf.buffer()[ii]);
+ assertEquals( ii, buf.get( ii ) );
+ assertEquals( ii, buf.buffer()[ii] );
}
-
- assertEquals(20, buf.capacity());
+
+ assertEquals( 20, buf.capacity() );
buf.append( 20 );
- assertEquals(30, buf.capacity());
+ assertEquals( 30, buf.capacity() );
// -------------------------------------------------------------------
-
+
buf = new ByteBuffer( 5 );
- assertEquals(5, buf.capacity());
-
+ assertEquals( 5, buf.capacity() );
+
for ( int ii = 0; ii < 5; ii++ )
{
buf.append( ii );
- assertEquals(ii, buf.get(ii));
- assertEquals(ii, buf.buffer()[ii]);
+ assertEquals( ii, buf.get( ii ) );
+ assertEquals( ii, buf.buffer()[ii] );
}
-
- assertEquals(5, buf.capacity());
+
+ assertEquals( 5, buf.capacity() );
buf.append( 5 );
- assertEquals(10, buf.capacity());
+ assertEquals( 10, buf.capacity() );
}
-
+
+
public void testCopyOfUsedBytes()
{
ByteBuffer buf = new ByteBuffer();
byte[] bytes = buf.copyOfUsedBytes();
- assertEquals(0, bytes.length);
-
+ assertEquals( 0, bytes.length );
+
for ( int ii = 0; ii < 20; ii++ )
{
buf.append( ii );
- assertEquals(ii, buf.get(ii));
- assertEquals(ii, buf.buffer()[ii]);
- assertEquals(ii, buf.copyOfUsedBytes()[ii]);
+ assertEquals( ii, buf.get( ii ) );
+ assertEquals( ii, buf.buffer()[ii] );
+ assertEquals( ii, buf.copyOfUsedBytes()[ii] );
}
}
-
+
+
public void testAppendByteArray()
{
ByteBuffer buf = new ByteBuffer();
- buf.append( new byte[]{ 0, 1, 2, 3, 4 } );
+ buf.append( new byte[]
+ { 0, 1, 2, 3, 4 } );
for ( int ii = 0; ii < 5; ii++ )
{
- assertEquals(ii, buf.get(ii));
- assertEquals(ii, buf.buffer()[ii]);
- assertEquals(ii, buf.copyOfUsedBytes()[ii]);
+ assertEquals( ii, buf.get( ii ) );
+ assertEquals( ii, buf.buffer()[ii] );
+ assertEquals( ii, buf.copyOfUsedBytes()[ii] );
}
}
}
Modified: directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/GeneralizedTimeTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/GeneralizedTimeTest.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/GeneralizedTimeTest.java (original)
+++ directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/GeneralizedTimeTest.java Tue Jan 24 17:44:03 2012
@@ -52,6 +52,7 @@ public class GeneralizedTimeTest
{
private static final Logger LOG = LoggerFactory.getLogger( GeneralizedTimeTest.class );
+
// Test all valid variants:
// Time: min + sec / min + no sec / no min + no sec
// Fraction: no fraction, dot, comma
@@ -509,8 +510,8 @@ public class GeneralizedTimeTest
TimeZoneFormat.DIFF_HOUR_MINUTE );
assertEquals( "2008010212+0000", result );
}
-
-
+
+
/**
* Test adjustment of time while formatting.
*/
@@ -521,7 +522,7 @@ public class GeneralizedTimeTest
GeneralizedTime generalizedTime = new GeneralizedTime( gt );
String result = generalizedTime.toGeneralizedTime();
assertEquals( gt, result );
-
+
result = generalizedTime.toGeneralizedTime( Format.YEAR_MONTH_DAY_HOUR_MIN_SEC, null, 0, TimeZoneFormat.Z );
assertEquals( "20080102104314Z", result );
}
@@ -1147,6 +1148,7 @@ public class GeneralizedTimeTest
assertFalse( generalizedTime1.equals( null ) );
}
+
/**
* Tests DIRSHARED-29 (GeneralizedTime.toString() generates wrong output
* when TimeZone has hours < 10 and minutes > 10).
@@ -1158,8 +1160,8 @@ public class GeneralizedTimeTest
String result = generalizedTime.toGeneralizedTime();
assertEquals( gt, result );
}
-
-
+
+
/**
* Tests to make sure the GeneralizedTime parser preserves the milliseconds
* component.
@@ -1173,9 +1175,9 @@ public class GeneralizedTimeTest
long originalTime = 0;
long millisLost = 0;
long trimmedMillis = 0;
-
+
// Get the current date and time now, also with trimmed milliseconds
-
+
while ( millisLost == 0 )
{
date = new Date();
@@ -1184,7 +1186,7 @@ public class GeneralizedTimeTest
trimmedMillis = trimmedMillis * 1000;
millisLost = originalTime - trimmedMillis;
}
-
+
LOG.info( "original time = {}", originalTime );
LOG.info( "trimmed milliseconds = {}", trimmedMillis );
LOG.info( "milliseconds lost = {}", millisLost );
@@ -1202,8 +1204,8 @@ public class GeneralizedTimeTest
long recalculatedTime = recalculatedGt.getCalendar().getTime().getTime();
LOG.info( "recalculated time = {}", recalculatedTime );
LOG.info( "generalized time string of recalculated time = {}", recalculatedGt.toGeneralizedTime() );
-
- assertEquals( "The time after round trip GeneralizedTime generation should stay the same",
+
+ assertEquals( "The time after round trip GeneralizedTime generation should stay the same",
originalTime, recalculatedTime );
}
}
\ No newline at end of file
Modified: directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/MethodUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/MethodUtilsTest.java?rev=1235374&r1=1235373&r2=1235374&view=diff
==============================================================================
--- directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/MethodUtilsTest.java (original)
+++ directory/shared/trunk/util/src/test/java/org/apache/directory/shared/util/MethodUtilsTest.java Tue Jan 24 17:44:03 2012
@@ -20,6 +20,7 @@
package org.apache.directory.shared.util;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
@@ -49,25 +50,28 @@ public class MethodUtilsTest
@SuppressWarnings("unused")
public static void methodA( String str )
{
-
+
}
-
+
+
@SuppressWarnings("unused")
public static void methodB( Collection<?> c )
{
-
+
}
}
-
+
+
@Test
public void testSameBehaviourOfStandardGetMethod()
{
Method m1 = null;
Method m2 = null;
-
+
try
{
- m1 = TestClass.class.getMethod( "methodA", new Class[] { String.class } );
+ m1 = TestClass.class.getMethod( "methodA", new Class[]
+ { String.class } );
}
catch ( SecurityException e )
{
@@ -79,29 +83,32 @@ public class MethodUtilsTest
// TODO Auto-generated catch block
e.printStackTrace();
}
-
+
try
{
- m2 = MethodUtils.getAssignmentCompatibleMethod(TestClass.class, "methodA", new Class[]{String.class});
+ m2 = MethodUtils.getAssignmentCompatibleMethod( TestClass.class, "methodA", new Class[]
+ { String.class } );
}
catch ( NoSuchMethodException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
-
+
assertEquals( m1, m2 );
-
+
}
-
+
+
@Test
public void testNewBehaviourOfAssignmentCompatibleGetMethod()
{
Method m2 = null;
-
+
try
{
- TestClass.class.getMethod( "methodB", new Class[] { ArrayList.class } );
+ TestClass.class.getMethod( "methodB", new Class[]
+ { ArrayList.class } );
fail( "We should not have come here." );
}
catch ( SecurityException e )
@@ -113,19 +120,20 @@ public class MethodUtilsTest
{
assertNotNull( e );
}
-
+
try
{
- m2 = MethodUtils.getAssignmentCompatibleMethod(TestClass.class, "methodB", new Class[]{ArrayList.class});
+ m2 = MethodUtils.getAssignmentCompatibleMethod( TestClass.class, "methodB", new Class[]
+ { ArrayList.class } );
}
catch ( NoSuchMethodException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
-
+
assertNotNull( m2 );
-
+
}
}