You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by we...@apache.org on 2004/01/27 03:20:42 UTC

svn commit: rev 6311 - incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/util

Author: wesmckean
Date: Mon Jan 26 18:20:42 2004
New Revision: 6311

Added:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/util/
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/util/TLVParser.java
Log:
Utility class to parse TLVs from the stream.  This will be the bases for our new parser.

Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/util/TLVParser.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/util/TLVParser.java	Mon Jan 26 18:20:42 2004
@@ -0,0 +1,232 @@
+/*
+
+ ============================================================================
+                   The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of  source code must  retain the above copyright  notice,
+    this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation
+    and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+    include  the following  acknowledgment:  "This product includes  software
+    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
+    Alternately, this  acknowledgment may  appear in the software itself,  if
+    and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve" 
+    and "Apache Software Foundation"  must not be used to endorse or promote
+    products derived  from this  software without  prior written
+    permission. For written permission, please contact apache@apache.org.
+
+ 5. Products  derived from this software may not  be called "Apache", nor may
+    "Apache" appear  in their name,  without prior written permission  of the
+    Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
+ APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
+ ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
+ (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software  consists of voluntary contributions made  by many individuals
+ on  behalf of the Apache Software  Foundation. For more  information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.snickers.ber.util;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.FileInputStream;
+
+import org.apache.commons.codec.binary.*;
+import org.apache.snickers.ber.*;
+
+/**
+ * @author Wes McKean
+ *
+ * A simply utility class to parse the ASN.1 TLVs out of a
+ * stream and display some useful information.
+ */
+public class TLVParser {
+
+    private InputStream m_is = null;
+    
+	/**
+	 * Basic constructor
+	 */
+	public TLVParser( InputStream is ) {
+		super();
+        m_is = is;
+	}
+
+    public void execute() {
+        
+        boolean finished = false;
+        
+        while( decodeTLV(0) != -1 );
+    }
+    
+    private int decodeTLV( int indent ) {
+    
+        int count = 0;
+        StringBuffer sb = new StringBuffer();
+        
+        for( int j = 0; j < indent; j++ ) {
+            sb.append(" ");
+        }
+        
+        String strIndent = sb.toString();
+        int valueLen = 0;
+        
+        try {
+            // Read the tag first
+            int tag = m_is.read();
+            
+            if( tag == -1 ) return tag;
+            
+            count++;
+            
+            int id = tag & (Binary.BIT_4 | Binary.BIT_3 | Binary.BIT_2 | Binary.BIT_1 | Binary.BIT_0 );
+            boolean primative =  BerUtils.isPrimitive(tag);
+            TypeClass tc = BerUtils.getTypeClass(tag);
+            
+            System.out.println( strIndent + ">>>>>>>>>>>>>> TLV <<<<<<<<<<<<<" );
+            System.out.println( strIndent + "Tag:  " + toBits(tag) );
+            System.out.println( strIndent + "   class:  " + tc );
+            System.out.println( strIndent + "   id:     " + id );
+            System.out.println( strIndent + "   p/c:    " + ( primative ? "primative" : "constructed" ) );
+            
+            int length = m_is.read();
+            count++;
+            
+            System.out.println( strIndent + "   length: " + toBits( length ) );
+            
+            // Try to determine the length value
+            if( ( length & Binary.BIT_7 ) == 0 ) {
+                valueLen = length & ~Binary.BIT_7;
+                System.out.println( strIndent + "            SHORT FORM=" + (valueLen)); 
+            }
+            else {
+                // BIT 8 is set to one, and we therefore have
+                // a long form or an indefinite form
+                int lenlen = (length & ~Binary.BIT_7);
+                
+                if( lenlen == 0 ) {
+                    System.out.println( strIndent + "            INDEFINITE FORM=" + (lenlen)); 
+                }
+                else {
+                    valueLen = 0;
+                    
+                    for( int i = 0; i < lenlen; i++ ) {
+                        int b = m_is.read();
+                        count++;
+                        valueLen = (( valueLen << 8 ) | b );
+                    }
+
+                    System.out.println( strIndent + "            LONG FORM=" + (valueLen)); 
+                }
+            }
+            
+            System.out.println(   strIndent+ "   value:  " );
+            
+            if( primative ) {
+                if( tc.equals( TypeClass.PRIVATE )) {
+                    while ( valueLen > 0 ) {
+                        int t = decodeTLV( indent + 5 );
+                        count += t;
+                        valueLen -= t;
+                    }
+                }
+                else {
+                    decodePrimative( id, valueLen, strIndent );
+                    count += valueLen;
+                }
+            }
+            else {
+                while ( valueLen > 0 ) {
+                    int t = decodeTLV( indent + 5 );
+                    count += t;
+                    valueLen -= t;
+                }
+            }
+            
+            System.out.println( strIndent + ">>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<" );
+            
+            return count;
+        }
+        catch( IOException e ) {
+            e.printStackTrace();
+        }
+        
+        return -1;
+    }
+
+    private void decodePrimative( int id, int len, String strIndent ) throws IOException {
+        byte[] b = new byte[len];
+        m_is.read(b, 0, len );
+        int value = 0;
+                
+        switch( id ) {
+            case Primative.BOOLEAN:
+                boolean bVal = ( ((int)b[0] ) > 0 ) ? true : false;
+                System.out.println( strIndent + "           BOOLEAN = " + true );
+                break;
+            case Primative.INTEGER:
+                
+                for( int i = 0; i < len; i++ ) {
+                    value = (value << 8) | (int)b[i];
+                }
+                System.out.println( strIndent + "           INTEGER = " + value );
+                break;
+            case Primative.OCTETSTRING:
+                String svalue = new String(b);
+                System.out.println( strIndent + "           String = " + svalue );
+                break;
+            case Primative.ENUMERATION:
+                for( int i = 0; i < len; i++ ) {
+                    value = (value << 8) | (int)b[i];
+                }
+                System.out.println( strIndent + "           ENUMERATION = " + value );
+                break;
+            default:
+                System.out.println( strIndent + "           CONTEXT SPECIFIC = " + new String(b) );
+        }    
+    }
+    
+    private String toBits( int octet ) {
+        String result = "";
+        
+        for( int i = 0; i < 8; i++ ) {
+            if( (( 1 << i ) & octet) != 0 ) {
+                result = "1" + result;
+            }
+            else
+                result = "0" + result;
+        }
+        
+        return result;
+    }
+    
+	public static void main(String[] args) throws Exception {
+        FileInputStream fis = new FileInputStream( args[0] );
+        TLVParser p = new TLVParser( fis );
+        p.execute();
+        
+        fis.close();        
+	}
+}