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 2005/09/09 22:17:49 UTC
svn commit: r279865 [3/3] - in
/directory/asn1/branches/elecharny-cleanup/ber-new: ./ src/ src/java/
src/java/main/ src/java/main/org/ src/java/main/org/apache/
src/java/main/org/apache/asn1new/ src/java/main/org/apache/asn1new/ber/
src/java/main/org/a...
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/MutableString.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/MutableString.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/MutableString.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/MutableString.java Fri Sep 9 13:17:27 2005
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.asn1new.util;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * A Mutable version of the String class. This mutable String can be
+ * stored in a pool.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class MutableString {
+ /** The string is stored in a char array */
+ private char[] string;
+
+ /** We also store the byte array for performance sake */
+ private byte[] data;
+
+ /** Actual length of the string. Be aware that it's a number of bytes,
+ * not a number of chars ! */
+ private int length;
+
+ /** A null MutableString */
+ public transient static final MutableString EMPTY_STRING = new MutableString();
+
+ /** A flag to mark the MutableString as Streamed (for string larger than 1024 chars) */
+ // TODO implement the streaming...
+ public transient static final boolean STREAMED = true;
+
+ /**
+ * Creates a MutableString.
+ */
+ public MutableString()
+ {
+ string = null;
+ length = 0;
+ }
+
+ /**
+ * Creates a MutableString.
+ */
+ public MutableString(String string)
+ {
+ this( string.getBytes() );
+ }
+
+ /**
+ * Creates a MutableString with a specific length.
+ */
+ public MutableString(int length)
+ {
+ string = new char[length];
+ this.length = length;
+ }
+
+ /**
+ * Creates a streamed MutableString with a specific length.
+ * Actually, it's just a simple MutableString.
+ * TODO Implement streaming.
+ */
+ public MutableString(int length, boolean isStreamed)
+ {
+ string = new char[length];
+ this.length = length;
+ }
+
+ /**
+ * Creates a MutableString with a value. The stored string
+ * is coded in Unicode, so the bytes *MUST* be valid !
+ * @param bytes The value to store.
+ */
+ public MutableString(byte[] bytes)
+ {
+ setData( bytes );
+ }
+
+ /**
+ * Set a new string in the MutableString. It will replace the old string,
+ * and reset the current length with the new one.
+ *
+ * @param bytes The string to store
+ */
+ public void setData(byte[] bytes)
+ {
+ data = bytes;
+ length = bytes.length;
+ int charLength = StringUtils.countChars(bytes);
+
+ string = new char[charLength];
+
+ int pos = 0;
+
+ for ( int i = 0; i < charLength; i++ )
+ {
+ string[i] = StringUtils.bytesToChar(bytes, pos);
+ pos += StringUtils.countBytesPerChar(bytes, pos);
+ }
+ }
+
+ /**
+ * @return Returns the length, as a number of bytes, not a number
+ * of chars.
+ */
+ public int getLength()
+ {
+ return length;
+ }
+
+ /**
+ * @return Returns the data.
+ */
+ public byte[] getData()
+ {
+ return data;
+ }
+
+ /**
+ * Return a native String representation of the MutableString.
+ */
+ public String toString()
+ {
+ if ( length == 0 )
+ {
+ return "";
+ }
+ else
+ {
+ try
+ {
+ return new String(data, 0, length, "UTF8");
+ }
+ catch (UnsupportedEncodingException uee)
+ {
+ return "";
+ }
+ }
+ }
+}
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java Fri Sep 9 13:17:27 2005
@@ -0,0 +1,590 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.asn1new.util;
+
+/**
+ * Little helper class. Nothing that should stay here, but I need those
+ * to debug.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StringUtils
+{
+ //~ Static fields/initializers -----------------------------------------------------------------
+
+ /** 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' };
+
+ private static int UTF8_MULTI_BYTES_MASK = 0x0080;
+
+ private static int UTF8_TWO_BYTES_MASK = 0x00E0;
+ private static int UTF8_TWO_BYTES = 0x00C0;
+
+ private static int UTF8_THREE_BYTES_MASK = 0x00F0;
+ private static int UTF8_THREE_BYTES = 0x00E0;
+
+ private static int UTF8_FOUR_BYTES_MASK = 0x00F8;
+ private static int UTF8_FOUR_BYTES = 0x00F0;
+
+ private static int UTF8_FIVE_BYTES_MASK = 0x00FC;
+ private static int UTF8_FIVE_BYTES = 0x00F8;
+
+ private static int UTF8_SIX_BYTES_MASK = 0x00FE;
+ private static int UTF8_SIX_BYTES = 0x00FC;
+
+ /** <alpha> ::= [0x41-0x5A] | [0x61-0x7A] */
+ public static final boolean[] ALPHA =
+ {
+ 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, 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, false,
+ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+ true, true, true, true, true, true, true, true, true, true, true, false, false, false,
+ false, false, false, true, true, true, true, true, true, true, true, true, true, true,
+ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+ false, false, false, false, false
+ };
+
+ /** <alpha> | <digit> | '-' */
+ public static final boolean[] 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, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, true, false, false, true, true, true, true, true,
+ true, true, true, true, true, false, false, false, false, false, false, false, true, true,
+ true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+ true, true, true, true, true, true, true, true, true, false, false, false, false, false,
+ false, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
+ true, true, true, true, true, true, true, true, true, true, true, true, false, false,
+ false, false, false
+ };
+
+ /** '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' */
+ public static final boolean[] DIGIT =
+ {
+ 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, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, true, true, true, true,
+ true, true, true, true, true, true, 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, 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, false, false, false, false, false, false,
+ };
+
+ /** <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
+ private static final boolean[] HEX =
+ {
+ 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, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false, false, true, true, true, true,
+ true, true, true, true, true, true, false, false, false, false, false, false, false, true,
+ true, true, true, true, true, 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, true, true, true, true, true, 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
+ };
+
+
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
+ * Helper function that dump a byte in hex form
+ *
+ * @param octet The byte to dump
+ * @return A string representation of the byte
+ */
+ public static String dumpByte( byte octet )
+ {
+ return new String(
+ new byte[] { '0', 'x', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet & 0x000F] } );
+ }
+
+ /**
+ * Helper function that dump an array of bytes in hex form
+ *
+ * @param octet The bytes array to dump
+ * @return A string representation of the array of bytes
+ */
+ public static String dumpBytes( byte[] buffer )
+ {
+ StringBuffer sb = new StringBuffer();
+
+ for (int i = 0; i < buffer.length ; i++)
+ {
+ sb.append("0x").append((char)(HEX_CHAR[( buffer[i] & 0x00F0 ) >> 4])).append((char)(HEX_CHAR[buffer[i] & 0x000F])).append(" ");
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Return the Unicode char which is coded in the bytes at position 0.
+ *
+ * @param bytes The byte[] represntation of an Unicode string.
+ * @return The first char found.
+ */
+ public static char bytesToChar(byte[] bytes)
+ {
+ return bytesToChar(bytes, 0);
+ }
+
+ /**
+ * Count the number of bytes needed to return an Unicode char. This
+ * can be from 1 to 6.
+ * @param bytes The bytes to read
+ * @param pos Position to start counting. It must be a valid start of a
+ * encoded char !
+ * @return The number of bytes to create a char, or -1 if the encoding is wrong.
+ *
+ * TODO : Should stop after the third byte, as a char is only 2 bytes long.
+ */
+ public static int countBytesPerChar(byte[] bytes, int pos)
+ {
+ if ((bytes[0] & UTF8_MULTI_BYTES_MASK) == 0)
+ {
+ return 1;
+ } else if ((bytes[0] & UTF8_TWO_BYTES_MASK) == UTF8_TWO_BYTES)
+ {
+ return 2;
+ }
+ else if ((bytes[0] & UTF8_THREE_BYTES_MASK) == UTF8_THREE_BYTES)
+ {
+ return 3;
+ }
+ else if ((bytes[0] & UTF8_FOUR_BYTES_MASK) == UTF8_FOUR_BYTES)
+ {
+ return 4;
+ }
+ else if ((bytes[0] & UTF8_FIVE_BYTES_MASK) == UTF8_FIVE_BYTES)
+ {
+ return 5;
+ }
+ else if ((bytes[0] & UTF8_SIX_BYTES_MASK) == UTF8_SIX_BYTES)
+ {
+ return 6;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ /**
+ * Return the Unicode char which is coded in the bytes at the given position.
+ * @param bytes The byte[] represntation of an Unicode string.
+ * @param pos The current position to start decoding the char
+ * @return The char found.
+ * @return The decoded char, or -1 if no char can be decoded
+ *
+ * TODO : Should stop after the third byte, as a char is only 2 bytes long.
+ */
+ public static char bytesToChar(byte[] bytes, int pos)
+ {
+ if ((bytes[pos] & UTF8_MULTI_BYTES_MASK) == 0)
+ {
+ return (char)bytes[pos];
+ }
+ else
+ {
+ if ((bytes[pos] & UTF8_TWO_BYTES_MASK) == UTF8_TWO_BYTES)
+ {
+ // Two bytes char
+ return (char)(
+ ( ( bytes[pos] & 0x1C ) << 6 ) + // 110x-xxyy 10zz-zzzz -> 0000-0xxx 0000-0000
+ ( ( bytes[pos] & 0x03 ) << 6 ) + // 110x-xxyy 10zz-zzzz -> 0000-0000 yy00-0000
+ ( bytes[pos + 1] & 0x3F ) // 110x-xxyy 10zz-zzzz -> 0000-0000 00zz-zzzz
+ ); // -> 0000-0xxx yyzz-zzzz (07FF)
+ }
+ else if ((bytes[pos] & UTF8_THREE_BYTES_MASK) == UTF8_THREE_BYTES)
+ {
+ // Three bytes char
+ return (char)(
+ // 1110-tttt 10xx-xxyy 10zz-zzzz -> tttt-0000-0000-0000
+ ( ( bytes[pos] & 0x0F) << 12 ) +
+ // 1110-tttt 10xx-xxyy 10zz-zzzz -> 0000-xxxx-0000-0000
+ ( ( 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 )
+ // -> tttt-xxxx yyzz-zzzz (FF FF)
+ );
+ }
+ else if ((bytes[pos] & UTF8_FOUR_BYTES_MASK) == UTF8_FOUR_BYTES)
+ {
+ // Four bytes char
+ return (char)(
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 000t-tt00 0000-0000 0000-0000
+ ( ( bytes[pos] & 0x07) << 18 ) +
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-00uu 0000-0000 0000-0000
+ ( ( bytes[pos + 1] & 0x30) << 16 ) +
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000 vvvv-0000 0000-0000
+ ( ( bytes[pos + 1] & 0x0F) << 12 ) +
+ // 1111-0ttt 10uu-vvvv 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-xxxx 0000-0000
+ ( ( bytes[pos + 2] & 0x3C) << 6 ) +
+ // 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 )
+ // -> 000t-ttuu vvvv-xxxx yyzz-zzzz (1FFFFF)
+ );
+ }
+ else if ((bytes[pos] & UTF8_FIVE_BYTES_MASK) == UTF8_FIVE_BYTES)
+ {
+ // Five bytes char
+ return (char)(
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-00tt 0000-0000 0000-0000 0000-0000
+ ( ( bytes[pos] & 0x03) << 24 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 uuuu-uu00 0000-0000 0000-0000
+ ( ( bytes[pos + 1] & 0x3F) << 18 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-00vv 0000-0000 0000-0000
+ ( ( bytes[pos + 2] & 0x30) << 12 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-0000 wwww-0000 0000-0000
+ ( ( bytes[pos + 2] & 0x0F) << 12 ) +
+ // 1111-10tt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz -> 0000-0000 0000-0000 0000-xxxx 0000-0000
+ ( ( bytes[pos + 3] & 0x3C) << 6 ) +
+ // 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 )
+ // -> 0000-00tt uuuu-uuvv wwww-xxxx yyzz-zzzz (03 FF FF FF)
+ );
+ }
+ else if ((bytes[pos] & UTF8_FIVE_BYTES_MASK) == UTF8_FIVE_BYTES)
+ {
+ // Six bytes char
+ return (char)(
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0s00-0000 0000-0000 0000-0000 0000-0000
+ ( ( bytes[pos] & 0x01) << 30 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 00tt-tttt 0000-0000 0000-0000 0000-0000
+ ( ( bytes[pos + 1] & 0x3F) << 24 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 uuuu-uu00 0000-0000 0000-0000
+ ( ( bytes[pos + 2] & 0x3F) << 18 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-00vv 0000-0000 0000-0000
+ ( ( bytes[pos + 3] & 0x30) << 12 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-0000 wwww-0000 0000-0000
+ ( ( bytes[pos + 3] & 0x0F) << 12 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 10zz-zzzz ->
+ // 0000-0000 0000-0000 0000-xxxx 0000-0000
+ ( ( bytes[pos + 4] & 0x3C) << 6 ) +
+ // 1111-110s 10tt-tttt 10uu-uuuu 10vv-wwww 10xx-xxyy 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 )
+ // -> 0stt-tttt uuuu-uuvv wwww-xxxx yyzz-zzzz (7F FF FF FF)
+ );
+ }
+ else
+ {
+ return (char)-1;
+ }
+ }
+ }
+
+ /**
+ * Count the number of chars included in the given byte[].
+ * @param bytes The byte array to decode
+ * @return The number of char in the byte array
+ */
+ public static int countChars(byte[] bytes)
+ {
+ int nbChars = 0;
+ int currentPos = 0;
+
+ while (currentPos < bytes.length)
+ {
+ currentPos += countBytesPerChar(bytes, currentPos);
+ nbChars ++;
+ }
+
+ return nbChars;
+ }
+
+ /**
+ * Check if a text is present at the current position in a buffer.
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param text The text we want to check
+ *
+ * @return <code>true</code> if the buffer contains the text.
+ */
+ public static int areEquals( byte[] byteArray, int index, String text )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( byteArray.length <= index ) ||
+ ( index < 0 ) || ( text == null ) )
+ {
+ return -1;
+ }
+ else
+ {
+ byte[] data = text.getBytes();
+
+ return areEquals( byteArray, index, data );
+ }
+ }
+
+ /**
+ * Check if a text is present at the current position in a buffer.
+ *
+ * @param charArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param charArray2 The text we want to check
+ *
+ * @return <code>true</code> if the buffer contains the text.
+ */
+ public static int areEquals( char[] charArray, int index, char[] charArray2 )
+ {
+
+ if ( ( charArray == null ) ||
+ ( charArray.length == 0 ) ||
+ ( charArray.length <= index ) ||
+ ( index < 0 ) ||
+ ( charArray2 == null ) ||
+ ( charArray2.length == 0 ) ||
+ ( charArray2.length > ( charArray.length + index ) ) )
+ {
+ return -1;
+ }
+ else
+ {
+ for ( int i = 0; i < charArray2.length; i++ )
+ {
+ if ( charArray[index++] != charArray2[i] )
+ {
+ return -1;
+ }
+ }
+
+ return index;
+ }
+ }
+
+ /**
+ * Check if a text is present at the current position in a buffer.
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param byteArray2 The text we want to check
+ *
+ * @return <code>true</code> if the buffer contains the text.
+ */
+ public static int areEquals( byte[] byteArray, int index, byte[] byteArray2 )
+ {
+
+ if ( ( byteArray == null ) ||
+ ( byteArray.length == 0 ) ||
+ ( byteArray.length <= index ) ||
+ ( index < 0 ) ||
+ ( byteArray2 == null ) ||
+ ( byteArray2.length == 0 ) ||
+ ( byteArray2.length > ( byteArray.length + index ) ) )
+ {
+ return -1;
+ }
+ else
+ {
+ for ( int i = 0; i < byteArray2.length; i++ )
+ {
+ if ( byteArray[index++] != byteArray2[i] )
+ {
+ return -1;
+ }
+ }
+
+ return index;
+ }
+ }
+
+ /**
+ * Test if the current character is equal to a specific character.
+ * This function works only for character between 0 and 127, as it
+ * does compare a byte and a char (which is 16 bits wide)
+ *
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ * @param car The character we want to compare with the current buffer position
+ *
+ * @return <code>true</code> if the current character equals the given character.
+ */
+ public static boolean isCharASCII( byte[] byteArray, int index, char car )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( byteArray[index] == car ) ? true : false );
+ }
+ }
+
+ /**
+ * Check if the current character is an Hex Char
+ * <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is a Hex Char
+ */
+ public static boolean isHex( byte[] byteArray, int index )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ byte c = byteArray[index];
+
+ if ( ( c > 127 ) || ( StringUtils.HEX[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
+ * Test if the current character is a digit
+ * <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
+ *
+ * @param byteArray The buffer which contains the data
+ *
+ * @return <code>true</code> if the current character is a Digit
+ */
+ public static boolean isDigit( byte[] byteArray )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( ( byteArray[0] > 127 ) || ! StringUtils.DIGIT[byteArray[0]] ) ? false : true );
+ }
+ }
+
+ /**
+ * Test if the current character is an Alpha character :
+ * <alpha> ::= [0x41-0x5A] | [0x61-0x7A]
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is an Alpha character
+ */
+ public static boolean isAlphaASCII( byte[] byteArray, int index )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ byte c = byteArray[index++];
+
+ if ( ( c > 127 ) || ( StringUtils.ALPHA[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ /**
+ * Test if the current character is a digit
+ * <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is a Digit
+ */
+ public static boolean isDigit( byte[] byteArray, int index )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ return ( ( ( byteArray[index] > 127 ) || ! StringUtils.DIGIT[byteArray[index]] ) ? false : true );
+ }
+ }
+
+ /**
+ * Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
+ * <char> ::= <alpha> | <digit> | '-'
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return The position of the next character, if the current one is a CHAR.
+ */
+ public static boolean isAlphaDigitMinus( byte[] byteArray, int index )
+ {
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+ ( index >= byteArray.length ) )
+ {
+ return false;
+ }
+ else
+ {
+ byte c = byteArray[index++];
+
+ if ( ( c > 127 ) || ( StringUtils.CHAR[c] == false ) )
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+
+}
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/ber/tlv/LengthTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/ber/tlv/LengthTest.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/ber/tlv/LengthTest.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/ber/tlv/LengthTest.java Fri Sep 9 13:17:27 2005
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.asn1new.ber.tlv;
+
+import java.util.Arrays;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * This class is used to test the Length class
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class LengthTest extends TestCase {
+
+ /**
+ * Test the getNbBytes method
+ */
+ public void testLengthGetNbBytes()
+ {
+ Assert.assertEquals(1, Length.getNbBytes(0));
+ Assert.assertEquals(1, Length.getNbBytes(1));
+ Assert.assertEquals(1, Length.getNbBytes(127));
+ Assert.assertEquals(2, Length.getNbBytes(128));
+ Assert.assertEquals(2, Length.getNbBytes(255));
+ Assert.assertEquals(3, Length.getNbBytes(256));
+ Assert.assertEquals(3, Length.getNbBytes(65535));
+ Assert.assertEquals(4, Length.getNbBytes(65536));
+ Assert.assertEquals(4, Length.getNbBytes(16777215));
+ Assert.assertEquals(5, Length.getNbBytes(16777216));
+ Assert.assertEquals(5, Length.getNbBytes(0xFFFFFFFF));
+ }
+
+ /**
+ * Test the getBytes method
+ */
+ public void testLengthGetBytes()
+ {
+ assertTrue(Arrays.equals(new byte[]{0x01}, Length.getBytes(1)));
+ assertTrue(Arrays.equals(new byte[]{0x7F}, Length.getBytes(127)));
+ assertTrue(Arrays.equals(new byte[]{(byte)0x81, (byte)0x80}, Length.getBytes(128)));
+ assertTrue(Arrays.equals(new byte[]{(byte)0x81, (byte)0xFF}, Length.getBytes(255)));
+ assertTrue(Arrays.equals(new byte[]{(byte)0x82, 0x01, 0x00}, Length.getBytes(256)));
+ assertTrue(Arrays.equals(new byte[]{(byte)0x82, (byte)0xFF, (byte)0xFF}, Length.getBytes(65535)));
+ assertTrue(Arrays.equals(new byte[]{(byte)0x83, 0x01, 0x00, 0x00}, Length.getBytes(65536)));
+ assertTrue(Arrays.equals(new byte[]{(byte)0x83, (byte)0xFF, (byte)0xFF, (byte)0xFF}, Length.getBytes(16777215)));
+ assertTrue(Arrays.equals(new byte[]{(byte)0x84, 0x01, 0x00, 0x00, 0x00}, Length.getBytes(16777216)));
+ assertTrue(Arrays.equals(new byte[]{(byte)0x84, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF}, Length.getBytes(0xFFFFFFFF)));
+ }
+}
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/BitStringTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/BitStringTest.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/BitStringTest.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/BitStringTest.java Fri Sep 9 13:17:27 2005
@@ -0,0 +1,173 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.asn1new.primitives;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.asn1.codec.DecoderException;
+
+
+/**
+ * Test the Bit String primitive
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class BitStringTest extends TestCase
+{
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
+ * Test a null BitString
+ */
+ public void testBitStringNull()
+ {
+
+ BitString bitString = new BitString();
+
+ bitString.setData( null );
+
+ try
+ {
+ bitString.getBit( 0 );
+ Assert.fail( "Should not reach this point ..." );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+ }
+
+ /**
+ * Test an empty BitString
+ */
+ public void testBitStringEmpty()
+ {
+
+ BitString bitString = new BitString();
+
+ bitString.setData( new byte[] {} );
+
+ try
+ {
+ bitString.getBit( 0 );
+ Assert.fail( "Should not reach this point ..." );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+ }
+
+ /**
+ * Test a single bit BitString BitString
+ */
+ public void testSingleBitBitString() throws DecoderException
+ {
+
+ BitString bitString = new BitString( 1 );
+
+ bitString.setData( new byte[] { 0x07, ( byte ) 0x80 } );
+
+ Assert.assertEquals( true, bitString.getBit( 0 ) );
+ }
+
+ /**
+ * Test a 32 bits BitString
+ */
+ public void test32BitsBitString() throws DecoderException
+ {
+
+ BitString bitString = new BitString( 32 );
+
+ bitString.setData(
+ new byte[] { 0x00, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF } );
+
+ for ( int i = 0; i < 32; i++ )
+ {
+ Assert.assertEquals( true, bitString.getBit( i ) );
+ }
+ }
+
+ /**
+ * Test a 33 bits BitString
+ */
+ public void test33BitsBitString() throws DecoderException
+ {
+
+ BitString bitString = new BitString( 33 );
+
+ bitString.setData(
+ new byte[]
+ {
+ 0x07, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0x80
+ } );
+
+ for ( int i = 0; i < 33; i++ )
+ {
+ Assert.assertEquals( true, bitString.getBit( i ) );
+ }
+
+ Assert.assertEquals( true, bitString.getBit( 32 ) );
+ }
+
+
+ /**
+ * Test all bits from 0 to 128 BitString
+ */
+ public void test0to128BitString() throws DecoderException
+ {
+
+ // bit number 14
+ BitString bitString14 = new BitString( 14 );
+
+ bitString14.setData( new byte[] { 0x02, ( byte ) 0xFF, ( byte ) 0xFC } );
+
+ for ( int i = 0; i < 14; i++ )
+ {
+ Assert.assertEquals( true, bitString14.getBit( i ) );
+ }
+
+ // bit number 31
+ BitString bitString31 = new BitString( 31 );
+
+ bitString31.setData(
+ new byte[] { 0x01, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFE } );
+
+ for ( int i = 0; i < 31; i++ )
+ {
+ Assert.assertEquals( true, bitString31.getBit( i ) );
+ }
+
+ // bit number 128
+ BitString bitString128 = new BitString( 128 );
+
+ bitString128.setData(
+ new byte[]
+ {
+ 0x00, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
+ ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
+ ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
+ ( byte ) 0xFF
+ } );
+
+ for ( int i = 0; i < 128; i++ )
+ {
+ Assert.assertEquals( true, bitString128.getBit( i ) );
+ }
+ }
+}
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/OIDTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/OIDTest.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/OIDTest.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/OIDTest.java Fri Sep 9 13:17:27 2005
@@ -0,0 +1,418 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.asn1new.primitives;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.asn1.codec.DecoderException;
+
+
+/**
+ * Test the OID primitive
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class OIDTest extends TestCase
+{
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
+ * Test a null OID
+ */
+ public void testOidNull()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+ oid.setOID( ( byte[] ) null );
+ Assert.fail( "Should not reach this point ..." );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+ }
+
+ /**
+ * Test an empty OID
+ */
+ public void testOidEmpty()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+ oid.setOID( new byte[] {} );
+ Assert.fail( "Should not reach this point ..." );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+ }
+
+ /**
+ * Test itu-t OID tree
+ */
+ public void testOidItuT()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+
+ // itu-t(0), recommendation(0), series a-z (0..26)
+ for ( int i = 1; i < 27; i++ )
+ {
+ oid.setOID( new byte[] { 0x00, ( byte ) i } );
+ Assert.assertEquals( "0.0." + i, oid.toString() );
+ }
+
+ // itu-t(0), question(1)
+ oid.setOID( new byte[] { 0x01 } );
+ Assert.assertEquals( "0.1", oid.toString() );
+
+ // itu-t(0), administration(2), country(202 .. 748)
+ for ( int i = 202; i < 748; i++ )
+ {
+ oid.setOID(
+ new byte[] { 0x02, ( byte ) ( ( i / 128 ) | 0x0080 ), ( byte ) ( i % 128 ) } );
+ Assert.assertEquals( "0.2." + i, oid.toString() );
+ }
+
+ // itu-t(0), network-operator(3), operator(2023 .. 41363)
+ for ( int i = 2023; i < 41363; i++ )
+ {
+
+ if ( i < ( 128 * 128 ) )
+ {
+ oid.setOID(
+ new byte[] { 0x03, ( byte ) ( ( i / 128 ) | 0x0080 ), ( byte ) ( i % 128 ) } );
+ Assert.assertEquals( "0.3." + i, oid.toString() );
+ }
+ else
+ {
+ oid.setOID(
+ new byte[]
+ {
+ 0x03, ( byte ) ( ( i / ( 128 * 128 ) ) | 0x0080 ),
+ ( byte ) ( ( ( i / 128 ) % 128 ) | 0x0080 ), ( byte ) ( i % 128 )
+ } );
+ Assert.assertEquals( "0.3." + i, oid.toString() );
+
+ }
+ }
+ }
+ catch ( DecoderException de )
+ {
+ Assert.fail();
+ }
+ }
+
+ /**
+ * Test iso OID tree
+ */
+ public void testOidIso()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+
+ // iso(1), standard(0)
+ oid.setOID( new byte[] { 40 + 0 } );
+ Assert.assertEquals( "1.0", oid.toString() );
+
+ // iso(1), registration-authority(1)
+ oid.setOID( new byte[] { 40 + 1 } );
+ Assert.assertEquals( "1.1", oid.toString() );
+
+ // iso(1), member-body(2)
+ oid.setOID( new byte[] { 40 + 2 } );
+ Assert.assertEquals( "1.2", oid.toString() );
+
+ // iso(1), identified-organization(3) | org(3) | organization(3)
+ oid.setOID( new byte[] { 40 + 3 } );
+ Assert.assertEquals( "1.3", oid.toString() );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.fail();
+ }
+ }
+
+ /**
+ * Test joint-iso-itu-t OID tree
+ */
+ public void testOidJointIsoItuT()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+
+ // joint-iso-itu-t(2), presentation(0)
+ oid.setOID( new byte[] { 80 + 0 } );
+ Assert.assertEquals( "2.0", oid.toString() );
+
+ // joint-iso-itu-t(2), asn1(1)
+ oid.setOID( new byte[] { 80 + 1 } );
+ Assert.assertEquals( "2.1", oid.toString() );
+
+ // joint-iso-itu-t(2), association-control(2)
+ oid.setOID( new byte[] { 80 + 2 } );
+ Assert.assertEquals( "2.2", oid.toString() );
+
+ // joint-iso-itu-t(2), reliable-transfer(3)
+ oid.setOID( new byte[] { 80 + 3 } );
+ Assert.assertEquals( "2.3", oid.toString() );
+
+ // ...
+ // joint-iso-itu-t(2), upu(40)
+ oid.setOID( new byte[] { 80 + 40 } );
+ Assert.assertEquals( "2.40", oid.toString() );
+
+ // ...
+ // joint-iso-itu-t(2), xxx(100)
+ oid.setOID( new byte[] { ( byte ) ( 0x81 ), 0x34 } );
+ Assert.assertEquals( "2.100", oid.toString() );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.fail();
+ }
+ }
+
+ /**
+ * Test valid String OIDs
+ */
+ public void testOidStringGood()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+ oid.setOID( "0.0" );
+ Assert.assertEquals( "0.0", oid.toString() );
+
+ oid.setOID( "0.0.0.0.0" );
+ Assert.assertEquals( "0.0.0.0.0", oid.toString() );
+
+ oid.setOID( "0.1.2.3.4" );
+ Assert.assertEquals( "0.1.2.3.4", oid.toString() );
+
+ oid.setOID( "2.123456" );
+ Assert.assertEquals( "2.123456", oid.toString() );
+
+ oid.setOID( "1.2.840.113554.1.2.2" );
+ Assert.assertEquals( "1.2.840.113554.1.2.2", oid.toString() );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.fail();
+ }
+ }
+
+ /**
+ * Test invalid String OIDs
+ */
+ public void testOidStringBad()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+ oid.setOID( "0" );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "0." );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "." );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "0.1.2." );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "3.1" );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "0..1" );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "0..12" );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "0.a.2" );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "0.123456" );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ try
+ {
+ oid.setOID( "1.123456" );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.assertTrue( true );
+ }
+
+ }
+
+ /**
+ * Test Spnego OID
+ */
+ public void testOidSpnego()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+ oid.setOID( new byte[] { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02 } );
+
+ Assert.assertEquals( "1.3.6.1.5.5.2", oid.toString() );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.fail();
+ }
+ }
+
+ /**
+ * Test Kerberos V5 OID
+ */
+ public void testOidKerberosV5()
+ {
+
+ OID oid = new OID();
+
+ try
+ {
+ oid.setOID(
+ new byte[]
+ {
+ 0x2a, ( byte ) 0x86, 0x48, ( byte ) 0x86, ( byte ) 0xf7, 0x12, 0x01, 0x02,
+ 0x02
+ } );
+
+ Assert.assertEquals( "1.2.840.113554.1.2.2", oid.toString() );
+ }
+ catch ( DecoderException de )
+ {
+ Assert.fail();
+ }
+ }
+
+ /**
+ * Test OIDs bytes
+ */
+ public void testOidBytes()
+ {
+ OID oid = new OID();
+ OID oid2 = new OID();
+
+ try
+ {
+ oid.setOID( "0.0" );
+ oid2.setOID(oid.getOID());
+ Assert.assertEquals( oid.toString(), oid2.toString());
+
+ oid.setOID( "0.0.0.0.0" );
+ oid2.setOID(oid.getOID());
+ Assert.assertEquals( oid.toString(), oid2.toString());
+
+ oid.setOID( "0.1.2.3.4" );
+ oid2.setOID(oid.getOID());
+ Assert.assertEquals( oid.toString(), oid2.toString());
+
+ oid.setOID( "2.123456" );
+ oid2.setOID(oid.getOID());
+ Assert.assertEquals( oid.toString(), oid2.toString());
+
+ oid.setOID( "1.2.840.113554.1.2.2" );
+ oid2.setOID(oid.getOID());
+ Assert.assertEquals( oid.toString(), oid2.toString());
+ }
+ catch ( DecoderException de )
+ {
+ Assert.fail();
+ }
+ }
+}
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/PrimitivesTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/PrimitivesTest.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/PrimitivesTest.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/primitives/PrimitivesTest.java Fri Sep 9 13:17:27 2005
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.asn1new.primitives;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.asn1.codec.DecoderException;
+import org.apache.asn1new.ber.tlv.Value;
+import org.apache.asn1new.util.IntegerDecoder;
+import org.apache.log4j.PropertyConfigurator;
+
+/**
+ * Test the Primitives
+ *
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PrimitivesTest extends TestCase
+{
+ //~ Static fields/initializers -----------------------------------------------------------------
+
+ static
+ {
+ PropertyConfigurator.configure( "conf/log4j.conf" );
+ }
+
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
+ * Test the Integer Primitive
+ */
+ public void testIntegerPrimitive()
+ {
+
+ try
+ {
+
+ Value value = new Value();
+
+ value.init(1);
+ value.setData( new byte[] { 0x00 } ); // res = 0
+ Assert.assertEquals( 0, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(1);
+ value.setData( new byte[] { 0x01 } ); // res = 1
+ Assert.assertEquals( 1, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(1);
+ value.setData( new byte[] { ( byte ) 0xFF } ); // res = 255
+ Assert.assertEquals( 255, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(2);
+ value.setData( new byte[] { 0x00, 0x01 } ); // res = 1
+ Assert.assertEquals( 1, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(2);
+ value.setData( new byte[] { 0x01, 0x00 } ); // res = 256
+ Assert.assertEquals( 256, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(2);
+ value.setData( new byte[] { 0x01, 0x01 } ); // res = 257
+ Assert.assertEquals( 257, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(2);
+ value.setData( new byte[] { 0x01, ( byte ) 0xFF } ); // res = 511
+ Assert.assertEquals( 511, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(2);
+ value.setData( new byte[] { 0x02, 0x00 } ); // res = 512
+ Assert.assertEquals( 512, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(2);
+ value.setData( new byte[] { ( byte ) 0xFF, ( byte ) 0xFF } ); // res = 65535
+ Assert.assertEquals( 65535, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(4);
+ value.setData(
+ new byte[] { ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF } ); // res = 2^32 - 1 = -1
+ Assert.assertEquals( -1, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(4);
+ value.setData(
+ new byte[] { ( byte ) 0x7F, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF } ); // res = 2^31 - 1 = MaxInt
+ Assert.assertEquals( Integer.MAX_VALUE, IntegerDecoder.parse( value ) );
+ value.reset();
+
+ value.init(4);
+ value.setData(
+ new byte[] { ( byte ) 0x80, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00 } ); // res = 2^31 = MinInt
+ Assert.assertEquals( Integer.MIN_VALUE, IntegerDecoder.parse( value ) );
+ value.reset();
+ }
+ catch ( DecoderException de )
+ {
+
+ }
+ }
+} // end class TLVTagDecoderTest
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/util/MutableStringTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/util/MutableStringTest.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/util/MutableStringTest.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/util/MutableStringTest.java Fri Sep 9 13:17:27 2005
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.asn1new.util;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class MutableStringTest extends TestCase {
+ public void testEmptyString()
+ {
+ String s = new MutableString().toString();
+
+ Assert.assertEquals("", s);
+ }
+
+ public void testNormalString()
+ {
+ String s = new MutableString("abcdef".getBytes()).toString();
+
+ Assert.assertEquals("abcdef", s);
+ }
+
+ /**
+ * Test a string with non ASCII chars
+ *
+ */
+ public void testNonASCIIString()
+ {
+ MutableString ms = new MutableString("Emmanuel Lécharny".getBytes());
+ String s = ms.toString();
+
+ int msLength = ms.getLength();
+ int sLength = s.getBytes().length;
+
+ Assert.assertEquals("Emmanuel Lécharny", s);
+ Assert.assertEquals( sLength, msLength);
+ }
+}
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/util/StringUtilsTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/util/StringUtilsTest.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/util/StringUtilsTest.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/test/org/apache/asn1new/util/StringUtilsTest.java Fri Sep 9 13:17:27 2005
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.asn1new.util;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class StringUtilsTest extends TestCase {
+
+ public void testOneByteChar()
+ {
+ char res = StringUtils.bytesToChar(new byte[]{0x30});
+
+ Assert.assertEquals('0', res);
+ }
+
+ public void testOneByteChar00()
+ {
+ char res = StringUtils.bytesToChar(new byte[]{0x00});
+
+ Assert.assertEquals(0x00, res);
+ }
+
+ public void testOneByteChar7F()
+ {
+ char res = StringUtils.bytesToChar(new byte[]{0x7F});
+
+ Assert.assertEquals(0x7F, res);
+ }
+
+ public void testTwoBytesChar()
+ {
+ char res = StringUtils.bytesToChar(new byte[]{(byte)0xCE, (byte)0x91});
+
+ Assert.assertEquals(0x0391, res);
+ }
+
+ public void testThreeBytesChar()
+ {
+ char res = StringUtils.bytesToChar(new byte[]{(byte)0xE2, (byte)0x89, (byte)0xA2});
+
+ Assert.assertEquals(0x2262, res);
+ }
+
+ /*
+ public void testSixBytesChar()
+ {
+ char res = StringUtils.bytesToChar(new byte[]{(byte)0xFD, (byte)0x93, (byte)0x91, (byte)0xBA, (byte)0x95, (byte)0xA3});
+
+ Assert.assertEquals(0x7347A563, res);
+ }
+ */
+}
Added: directory/asn1/branches/elecharny-cleanup/ber-new/todo.txt
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/todo.txt?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/todo.txt (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/todo.txt Fri Sep 9 13:17:27 2005
@@ -0,0 +1,39 @@
+
+
+ T O D O L I S T
+ =================
+
+* Need an equality test for TupleNodes hence trees.
+
+* For the mock decoder in Eve see if we can rig in a decoder that builds on this
+ BERDecoder that can at a bare minimum detect when an LDAP message starts and
+ ends. This way we can continue working on Eve while the snickers code
+ progresses forward. In fact we could even capture a full buffer and feed it
+ to the old synchronous SNACC provider just for now to get decodes working.
+
+--o--
+
+* Add a value size protection feature to the decoder to either break up
+ incomming massive tlv's into smaller tlv's or to stream them to disk,
+ and providing a data reference rather than a byte[] to the callback's
+ decodeOccurred method. This will be huge for both performance and the
+ prevention of denial of service attacks where massive TLV's bombard the
+ decoder. We need to look at the big picture to figure out which approach
+ is best within the whole decoding processs.
+
+--o--
+
+* Build a library of primitive ASN.1 data types mapping them to Java types.
+
+* Build a digester like tool on top of the BERDecoder, for now we'll call it a
+ TLVDecoder. It is fed a train of TLV's and it statefully constructs ASN.1
+ stubs based on the input. When an entire stub has been assembled it is
+ emitted via the decodeOccurred callback.
+
+* It will be worth while taking a good look at the way digester works to get
+ some ideas here.
+
+* Also we need to read the Sample Neufeld papper to make sure we're decoding
+ appropriately.
+
+