directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r191606 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/Asn1Decoder.java
Date Tue, 21 Jun 2005 00:49:23 GMT
Author: elecharny
Date: Mon Jun 20 17:49:22 2005
New Revision: 191606

URL: http://svn.apache.org/viewcvs?rev=191606&view=rev
Log:
Major improvment in the way the LENGTH part is handled.
Right now, we don't need to deal with actions to be executed when 
LENGTH are totally read, the work is done in this class. Grammars will be
much simpler !

I'm pretty sure that this can also be done for tags (we don't need to execute actions).

WARNING ! ALl the grammars are to be reviewed. SPNEGO is working fine.

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/Asn1Decoder.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/Asn1Decoder.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/Asn1Decoder.java?rev=191606&r1=191605&r2=191606&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/Asn1Decoder.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/Asn1Decoder.java Mon
Jun 20 17:49:22 2005
@@ -235,7 +235,7 @@
             Tag tag = container.getCurrentTLV().getTag();
             log.debug( tag.toString() + " has been decoded" );
         }
-
+        
         // After having decoded a tag, we have to execute the action
         // which controls if this tag is allowed and well formed.
         container.getGrammar().executeAction( container );
@@ -368,6 +368,35 @@
             return END;
         }
     }
+    
+    /**
+     * A debug function used to dump the expected length stack.
+     * @param tlv The current TLV.
+     * @return A string which represent the expected length stack.
+     */
+    private String getParentLength(TLV tlv)
+    {
+        StringBuffer buffer = new StringBuffer();
+        
+        buffer.append("TLV expected length stack : ");
+        
+        while (true)
+        {
+            if ( tlv == null )
+            {
+                buffer.append(" - null");
+                break;
+            }
+            else
+            {
+                buffer.append(" - ").append(tlv.getExpectedLength());
+            }
+            
+            tlv = tlv.getParent();
+        }
+        
+        return buffer.toString();
+    }
 
     /**
      * The Length is fully decoded. We have to call an action to check the
@@ -381,8 +410,103 @@
     private void treatLengthEndState( IAsn1Container container )
         throws DecoderException
     {
+        TLV tlv = container.getCurrentTLV();
+        Length length = tlv.getLength();
+
+        // We will check the length here. What we must control is
+        // that the enclosing constructed TLV expected length is not
+        // exceeded by the current TLV.
+        TLV parentTLV = container.getParentTLV();
+        
+        if (DEBUG)
+        {
+            log.debug(getParentLength(parentTLV));
+        }
+        
+        if (parentTLV == null) 
+        {
+            // This is the first TLV, so we can't check anything. We will
+            // just store this TLV as the root of the PDU
+            tlv.setExpectedLength(length.getLength());
+            container.setParentTLV(tlv);
+            
+            if (DEBUG)
+            {
+                log.debug("Root TLV[" + tlv.getLength().getLength() + "]");
+            }
+        }
+        else
+        {
+            // We have a parent, so we will check that its expected length is
+            // not exceeded. 
+            int expectedLength = parentTLV.getExpectedLength();
+            int currentLength = tlv.getSize();
+            
+            if (expectedLength < currentLength)
+            {
+                // The expected length is lower than the Value length of the
+                // current TLV. This is an error...
+                log.error("tlv[" + expectedLength + ", " + currentLength + "]");
+                throw new DecoderException("The current Value length is above the expected
length");
+            }
+            
+            if (expectedLength == currentLength)
+            {
+                parentTLV.setExpectedLength(0);
 
-        Length length = container.getCurrentTLV().getLength();
+                // deal with the particuliar case where expected length equal
+                // the current length, which means that the parentTLV has been
+                // completed. 
+                // We also have to check that the current TLV is a constructed
+                // one. In this case, we won't change the parent TLV
+                // In this case, we have to switch from this parent TLV
+                // to the parent's parent TLV.
+                if (tlv.getTag().isConstructed())
+                {
+                    // It's a constructed TLV. We will set it's parent to the 
+                    // parentTLV, and it will become the new parent TLV, after
+                    // having set the new expected length.
+                    tlv.setParent(parentTLV);
+                    tlv.setExpectedLength(tlv.getLength().getLength());
+                    container.setParentTLV(tlv);
+                }
+                else
+                {
+                    // It's over, the parent TLV has been completed.
+                    // Go back to the parent's parent TLV until we find
+                    // a tlv which is not complete.
+                    while (parentTLV != null)
+                    {
+                        if ( parentTLV.getExpectedLength() != 0 )
+                        {
+                            // ok, we have an incomplete parent. we will
+                            // stop the recursion right here
+                            break;
+                        }
+                        else
+                        {
+                            parentTLV = parentTLV.getParent();
+                        }
+                    }
+                    
+                    container.setParentTLV(parentTLV);
+                }
+            }
+            else
+            {
+	            // Renew the expected Length.
+	            parentTLV.setExpectedLength(expectedLength - currentLength);
+	            
+	            if (tlv.getTag().isConstructed())
+	            {
+	                // We have a constructed tag, so we must switch the parentTLV
+                    tlv.setParent(parentTLV);
+                    tlv.setExpectedLength(tlv.getLength().getLength());
+                    container.setParentTLV(tlv);
+	            }
+            }
+            
+        }
 
         if ( DEBUG )
         {
@@ -401,9 +525,6 @@
             // Go ahead and decode the value part
             container.setState( TLVStateEnum.VALUE_STATE_START );
         }
-
-        // Execute the action to check if the length is OK
-        container.getGrammar().executeAction( container );
     }
 
     /**



Mime
View raw message