📄 berinputstream.java
字号:
public AsnBitString bitstring_codec(Object instance, boolean is_constructed) throws java.io.IOException { AsnBitString abs = (AsnBitString)instance; // debug("Bitstring codec..."); int unused = read(); if ( next_length > 0 ) { byte[] data = new byte[next_length-1]; data = new byte[next_length-1]; int bytes_left_to_read = next_length-1; int offset = 0; // We may need to call read repeatedly until we have all the data. while ( bytes_left_to_read > 0 ) { int bytes_read = read(data,offset,bytes_left_to_read); bytes_left_to_read -= bytes_read; offset += bytes_read; } abs = new AsnBitString(data, unused); // debug("got "+next_length+" bytes of bitstring with "+unused+" unused bits at end\n"); } // debug("bitstring returns ... length="+next_length+"\n"); return abs; } public AsnNull null_codec(Object instance, boolean is_constructed) throws java.io.IOException { AsnNull retval = null; // No contents octets allowed for null encoding if ( next_length != 0 ) { throw new java.io.IOException("Unexpected length encoding of null"); } return new AsnNull(); } public Object choice(Object current_instance, Object[][] choice_info, int which, String name) throws java.io.IOException { ChoiceType retval = (ChoiceType)current_instance; // debug("choice_codec ("+name+") current = "+retval+" number of options="+choice_info.length+"\n"); retval.which=-1; for ( int i=0; ( ( i< choice_info.length ) && ( retval.which == -1 ) ) ;i++ ) { Integer tagmode = (Integer)(choice_info[i][0]); Integer tagclass = (Integer)(choice_info[i][1]); Integer tagnumber = (Integer)(choice_info[i][2]); base_codec codec_to_use = ((base_codec)(choice_info[i][3])); // debug("choice Trying ["+i+"] : "+tagmode+" "+tagclass+" "+tagnumber+" "+codec_to_use); if ( tagmode.equals(SerializationManager.TAGMODE_NONE ) ) { retval.o = codec_to_use.serialize(this, retval.o, true, ((String)(choice_info[i][4]))); } else { if ( tagmode.equals(SerializationManager.IMPLICIT ) ) { // Implicit Tagging // debug("<implicit tagging, so simply calling codec for "+((String)(choice_info[i][4]))+">\n"); retval.o = implicit_tag(codec_to_use, retval.o, tagclass.intValue(), tagnumber.intValue(), true, ((String)(choice_info[i][4]))); // implicit_settag(tagclass.intValue(), tagnumber.intValue()); // retval.o = codec_to_use.serialize(this, retval.o, false, ((String)(choice_info[i][4]))); } else { // debug("<explicit tagging, so simply calling codec for "+((String)(choice_info[i][4]))+">\n"); // retval.o = explicit_tag(codec_to_use, retval.o, tagclass.intValue(), tagnumber.intValue(), true, ((String)(choice_info[i][4]))); if ( constructedBegin(tagclass.intValue(), tagnumber.intValue()) ) { retval.o = codec_to_use.serialize(this, retval.o, false, ((String)(choice_info[i][4]))); constructedEnd(); } } } if ( null != retval.o ) { retval.which = i; } } // It's valid to return null if the component is optional // if ( retval.which == -1 ) // throw new java.io.IOException("Unable to decode choice element"); return retval; } public boolean sequenceBegin() throws java.io.IOException { if ( tag_class < 0 ) { tag_class = SerializationManager.UNIVERSAL; tag_value = SerializationManager.SEQUENCE; } return constructedBegin(tag_class, tag_value); } public boolean sequenceEnd() throws java.io.IOException { return constructedEnd(); } public boolean constructedBegin(int tagclass, int tagnumber) throws java.io.IOException { if ( tag_class < 0 ) { tag_class = tagclass; tag_value = tagnumber; } if ( tag_codec(true) >= 0 ) { // decode length octets and store in csi... // debug("Constructed Begin ["+encoding_info.size()+"] ("+tag_class+","+tag_value+") "+tagclass+" len="+next_length+"\n"); CodecStackInfo csi = new CodecStackInfo(); csi.content_length = next_length; csi.bytes_processed = 0; csi.is_constructed = next_is_constructed; csi.is_indefinite_length = next_is_indefinite; encoding_info.push(csi); return true; } return false; } public boolean constructedEnd() throws java.io.IOException { CodecStackInfo csi = (CodecStackInfo)encoding_info.pop(); // We now need to add any bytes read on to the bytes processed total for the next // constructed item in the stack // If we are closing an indefinite length encoding, consume the terminating octets // if ( csi.content_length == 0 ) if ( csi.is_indefinite_length ) { // debug("Reading indefinite length terminating octets\n"); byte b1 = (byte)read(); byte b2 = (byte)read(); if ( ( b1 == 0 ) && ( b2 == 0 ) ) { next_tag_class=-1; tag_class=-1; } else { throw new java.io.IOException("Expected indefinite length terminating octets for constructed type, found other values"); } } if ( encoding_info.size() > 0 ) { CodecStackInfo curr = (CodecStackInfo)encoding_info.peek(); curr.bytes_processed += csi.bytes_processed; } // debug("Constructed End ["+encoding_info.size()+"] ("+csi.bytes_processed+" bytes)\n"); return true; } public Object implicit_tag(base_codec c, Object current_instance, int tag_class, int tag_number, boolean is_optional, String name) throws java.io.IOException { Object retval = null; // debug("implicit_tag "+tag_class+","+tag_number+" "+name+"\n"); implicit_settag(tag_class, tag_number); // if ( tag_codec(false) > 0 ) // { retval = c.serialize(this, current_instance, is_optional, name); // } return retval; } public Object explicit_tag(base_codec c, Object current_instance, int tag_class, int tag_number, boolean is_optional, String name) throws java.io.IOException { Object retval = current_instance; // debug("explicit_tag "+tag_class+","+tag_number+" "+name+"\n"); if ( constructedBegin(tag_class, tag_number) ) { retval = c.serialize(this, retval, is_optional, name); constructedEnd(); } return retval; } public boolean sequenceOf(Vector v, base_codec codec) throws java.io.IOException { // debug("probably SequenceOF"); if ( null != v ) { if ( sequenceBegin() ) { // debug("SequenceOF\n"); while ( moreData() ) { // debug("adding another sequenceOf member (size="+v.size()+")\n"); Object item_to_add = codec.serialize(this, null, true, "SequenceOf item"); if ( null == item_to_add ) throw new java.io.IOException("Error expecting member of sequenceOf"); v.add ( item_to_add ); // v.add ( codec.serialize(this, null, true, "SequenceOf item") ); // debug("Done adding another sequenceOf member (size="+v.size()+")\n"); } // debug("About to call sequenceEnd for SequenceOf\n"); sequenceEnd(); return true; } } else throw new java.io.IOException("Must create vector before calling sequenceOf codec"); return false; } public void implicit_settag(int tagclass, int tagvalue) { if ( tag_class < 0 ) { tag_class = tagclass; tag_value = tagvalue; } } private void debug(String msg) { StringWriter sw = new StringWriter(); for ( int i=0; i < encoding_info.size(); i++ ) { sw.write(" "); } sw.write(msg); System.err.print(sw.toString()); } // Override default read method with octet counting for constructed members public int read() throws java.io.IOException { int retval = in.read(); if ( encoding_info.size() > 0 ) { // Get head off stack and add 1 to bytes processed CodecStackInfo csi = (CodecStackInfo)encoding_info.peek(); csi.bytes_processed += 1; } return retval; } public int read(byte[] buffer, int offset, int max) throws java.io.IOException { int retval = in.read(buffer, offset, max); if ( encoding_info.size() > 0 ) { // Get head off stack and add retval to bytes processed CodecStackInfo csi = (CodecStackInfo) encoding_info.peek(); csi.bytes_processed += retval; } return retval; } // This function should NEVER consume the 2 octets that terminate indefinite length encoding... // That should be left to the thing encoding the sequence... This func just takes a peek at the // next octets to see what's to come... public boolean moreData() throws java.io.IOException { // Get the current top of the encoding stack and compare it's content_length // with the current decode position // debug("moreData called, stack="+encoding_info.size()+"\n"); if ( encoding_info.size() > 0 ) { CodecStackInfo csi = (CodecStackInfo) encoding_info.peek(); // debug ("Content length="+ csi.content_length+ // ", bytes_processed="+csi.bytes_processed+ // ", constructed="+csi.is_constructed+ // ", indefinite length encoding ="+csi.is_indefinite_length+"\n"); if ( csi.content_length > 0 ) { // debug("MoreData comparing "+csi.bytes_processed+" < "+ csi.content_length+"\n"); if ( csi.bytes_processed < csi.content_length ) return true; else return false; } else if ( csi.is_indefinite_length ) { // Indefinite length encodings are terminated by 00 // debug("MoreData... Indefinite length encoding, so check for terminating octets\n"); in.mark(5); int i1 = in.read(); int i2 = in.read(); in.reset(); if ( ( i1 == 0 ) && ( i2 == 0 ) ) { // debug("MoreData... false ( Next octets are 00 )\n"); // csi.bytes_processed += 2; return false; } else { // debug("MoreData... true because next 2 octets are not 00: "+i1+" and "+i2+"\n"); // in.reset(); return true; } } } return false; } private int decodeBase128Int(InputStream ins) throws java.io.IOException { int retval = 0; byte octet = (byte)128; while ( ( octet & 128 ) == 128 ) { octet = (byte)ins.read(); retval = ( ( retval << 7 ) | ( octet & 127 ) ); } return retval; } public base_codec getHintCodec() { return codec_hint; } public void setHintCodec(base_codec c) { codec_hint = c; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -