Improve InvalidBERException.

Add feature of SEQUENCE.OF_Template whereby elements need not produce
any output. This is for dealing with very large SEQUENCEs, such as
large CRLs, where the list should be processed in some way, but not made
into an ASN1 object hierarchy.


git-svn-id: svn://10.0.0.236/trunk@133069 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
nicolson%netscape.com 2002-11-06 02:00:55 +00:00
parent 6f34f01f3d
commit 04974987a2
2 changed files with 63 additions and 22 deletions

View File

@ -32,17 +32,24 @@
*/
package org.mozilla.jss.asn1;
import java.util.Vector;
/**
* An exception thrown when BER decoding fails.
*/
public class InvalidBERException extends java.lang.Exception {
private InvalidBERException child=null;
private Vector mesgList = new Vector();
public InvalidBERException(String mesg) {
super(mesg);
}
public void append(String mesg) {
mesgList.addElement(mesg);
}
public InvalidBERException(InvalidBERException e, String mesg) {
super(mesg);
child = e;
@ -52,26 +59,33 @@ public class InvalidBERException extends java.lang.Exception {
* Prints out the exception class and error message, including
* all the nested exceptions.
*/
private void appendMessages(StringBuffer sb) {
int numMessages = mesgList.size();
for( int i=numMessages-1; i >= 0; --i ) {
sb.append(mesgList.elementAt(i));
sb.append(" >> ");
}
sb.append(getMessage());
}
public String toString() {
if(child != null) {
return (super.toString()+ " >> " + child.toStringNested());
} else {
return super.toString();
}
StringBuffer sb = new StringBuffer();
sb.append( this.getClass().getName() );
sb.append(": ");
appendMessages(sb);
return sb.toString();
}
/**
* Prints out the error message of this exception, including all the
* nested exceptions.
*/
public String toStringNested() {
if(child != null) {
return ( getMessage() + " >> " + child.toStringNested());
} else {
return getMessage();
StringBuffer sb = new StringBuffer();
appendMessages(sb);
if( child != null ) {
sb.append(" >> ");
sb.append( child.toStringNested() );
}
return sb.toString();
}
public static class EOF extends InvalidBERException {
public EOF() {
super("Unexpected end-of-file encountered");

View File

@ -384,7 +384,7 @@ public static class Template implements ASN1Template {
tagDesc = lookAhead.getTag().toString();
}
throw new InvalidBERException("Missing item #" + index +
": found " + lookAhead.getTag() );
": found " + tagDesc );
}
continue;
}
@ -412,12 +412,14 @@ public static class Template implements ASN1Template {
}
// Store this element in the SEQUENCE
if( e.getImplicitTag() == null ) {
// no implicit tag
seq.addElement( val );
} else {
// there is an implicit tag
seq.addElement( e.getImplicitTag(), val );
if( e.producesOutput() ) {
if( e.getImplicitTag() == null ) {
// no implicit tag
seq.addElement( val );
} else {
// there is an implicit tag
seq.addElement( e.getImplicitTag(), val );
}
}
// If this element is repeatable, don't go on to the next element
@ -449,7 +451,8 @@ public static class Template implements ASN1Template {
return seq;
} catch(InvalidBERException e) {
throw new InvalidBERException(e, "SEQUENCE(item #"+index+")");
e.append("SEQUENCE(item #" +index + ")");
throw e;
}
}
@ -463,11 +466,21 @@ public static class Template implements ASN1Template {
* Creates a new element, which may or may not be optional.
*/
public Element(Tag implicitTag, ASN1Template type, boolean optional)
{
this(implicitTag, type, optional, true);
}
/**
* Creates a new element, which may or may not be optional.
*/
public Element(Tag implicitTag, ASN1Template type, boolean optional,
boolean doesProduceOutput)
{
this.type = type;
defaultVal = null;
this.optional = optional;
this.implicitTag = implicitTag;
this.doesProduceOutput = doesProduceOutput;
}
/**
@ -481,6 +494,11 @@ public static class Template implements ASN1Template {
this.implicitTag = implicitTag;
}
private boolean doesProduceOutput;
boolean producesOutput() {
return doesProduceOutput;
}
// repeatability is provided to allow for SEQUENCE OF SIZE
// constructs. It is package private.
private boolean repeatable;
@ -553,6 +571,15 @@ public static class OF_Template implements ASN1Template {
template.addElement( el );
}
public static OF_Template makeOutputlessOFTemplate(ASN1Template type) {
OF_Template t = new OF_Template();
t.template = new Template();
Template.Element el = new Template.Element(null, type, true, false);
el.makeRepeatable();
t.template.addElement(el);
return t;
}
public boolean tagMatch(Tag tag) {
return TAG.equals(tag);
}