Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 36574 invoked from network); 30 Apr 2005 13:01:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 30 Apr 2005 13:01:24 -0000 Received: (qmail 55096 invoked by uid 500); 30 Apr 2005 13:02:48 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 55049 invoked by uid 500); 30 Apr 2005 13:02:48 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 55031 invoked by uid 99); 30 Apr 2005 13:02:47 -0000 X-ASF-Spam-Status: No, hits=0.2 required=10.0 tests=NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Sat, 30 Apr 2005 06:02:47 -0700 Received: (qmail 36533 invoked by uid 65534); 30 Apr 2005 13:01:21 -0000 Message-ID: <20050430130121.36532.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: svn commit: r165406 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java Date: Sat, 30 Apr 2005 13:01:21 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.0-dev X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: elecharny Date: Sat Apr 30 06:01:19 2005 New Revision: 165406 URL: http://svn.apache.org/viewcvs?rev=3D165406&view=3Drev Log: Completed the OID primitive class. It's needed for SPNEGO decoder Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primiti= ves/OID.java Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/p= rimitives/OID.java URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/s= rc/java/org/apache/asn1/primitives/OID.java?rev=3D165406&r1=3D165405&r2=3D1= 65406&view=3Ddiff =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primiti= ves/OID.java (original) +++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primiti= ves/OID.java Sat Apr 30 06:01:19 2005 @@ -16,9 +16,162 @@ */ package org.apache.asn1.primitives; =20 +import org.apache.asn1.DecoderException; +import org.apache.asn1.util.pools.PoolObject; + /** + * This class implement an OID (Object Identifier). + *=20 + * An OID is encoded as a list of bytes representing integers.=20 + *=20 + * An OID has a numeric representation where number are separated with dot= s : + * SPNEGO Oid =3D 1.3.6.1.5.5.2 + *=20 + * Translating from a byte list to a dot separated list of number follows = the rules : + * - the first number is in [0..2] + * - the second number is in [0..39] if the first number is 0 or 1 + * - the first byte has a value equal to : number 1 * 40 + number two + * - the upper bit of a byte is set if the next byte is a part of the numb= er + *=20 + * For instance, the SPNEGO Oid (1.3.6.1.5.5.2) will be encoded :=20 + * 1.3 -> 0x2B (1*40 + 3 =3D 43 =3D 0x2B)=20 + * .6 -> 0x06=20 + * .1 -> 0x01=20 + * .5 -> 0x05=20 + * .5 -> 0x05=20 + * .2 -> 0x02=20 + *=20 + * The Kerberos V5 Oid (1.2.840.48018.1.2.2) will be encoded : + * 1.2 -> 0x2A (1*40 + 2 =3D 42 =3D 0x2A)=20 + * 840 -> 0x86 0x48 (840 =3D 6 * 128 + 72 =3D (0x06 | 0x80) 0x48 =3D 0x8= 6 0x48 + * 48018 -> 0x82 0xF7 0x12 (2 * 128 * 128 + 119 * 128 + 18 =3D (0x02 | 0x8= 0) (0x77 | 0x80) 0x12 + *=20 * @author Apache Directory Pr= oject */ -public class OID { +public class OID extends PoolObject { + /** The OID as a array of int */ + private int[] oidValues; + + /** + * Creates a new OID object. + */ + public OID() + { + // We should not create this kind of object directly, it must + // be created through the factory. + } + =20 + /** + * Set the OID. It will be translated from a byte array to a string. + * @param oid + */ + public void setOID(byte[] oid) throws DecoderException + { + if ( oid =3D=3D null ) + { + throw new DecoderException("Null OID"); + } + =20 + if ( oid =3D=3D null | oid.length < 1 ) + { + throw new DecoderException("Invalid OID : " + oid); + } + =20 + // First, we have to calculate the number of int to allocate + int nbValues =3D 2; + =20 + int pos =3D 1; + + while ( pos < oid.length ) + { + if ( oid[pos] >=3D 0 ) + { + nbValues++; + } + =20 + pos++; + } + =20 + oidValues =3D new int[nbValues]; + =20 + nbValues =3D 0; + pos =3D 0; + int accumulator =3D 0; + =20 + + if ( oid[0] < 40 ) + { + oidValues[nbValues++] =3D 0; + oidValues[nbValues++] =3D oid[pos++] ; // itu-t + }=20 + else if ( oid[0] < 80 )=20 + { + oidValues[nbValues++] =3D 1; + oidValues[nbValues++] =3D oid[pos++] - 40 ; // iso + } + else=20 + { + oidValues[nbValues++] =3D 2; + =20 + while ( pos < oid.length ) + { + if ( oid[pos] >=3D 0 ) + { + oidValues[nbValues++] =3D (( accumulator << 7 ) + oid= [pos]) - 80;=20 + accumulator =3D 0; + pos++; + break; + } + else + { + accumulator =3D ( accumulator << 7 ) + ( oid[pos] & 0x= 007F );=20 + } + =20 + pos++; + } + } + + while ( pos < oid.length ) + { + =20 + if ( oid[pos] >=3D 0 ) + { + oidValues[nbValues++] =3D ( accumulator << 7 ) + oid[pos]= ;=20 + accumulator =3D 0; + } + else + { + accumulator =3D ( accumulator << 7 ) + ( oid[pos] & 0x007F= );=20 + } + =20 + pos++; + } + } + =20 + /** + * Get an array of int from the OID + * @return An array of int representing the OID + */ + public int[] getOIDValues() + { + return oidValues; + } + + /** + * Get the OID as a String + * @return A String representing the OID + */ + public String getOID() + { + StringBuffer sb =3D new StringBuffer(); + sb.append(oidValues[0]); + int pos =3D 1; =20 + for ( int i =3D 1 ; i < oidValues.length ; i++ ) + { + sb.append('.').append(oidValues[i]); + } + =20 + return sb.toString(); + } }