📄 asn1inputstream.java
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi
// Source File Name: ASN1InputStream.java
package jit.asn1;
import java.io.*;
import java.util.Vector;
// Referenced classes of package jit.asn1:
// DERInputStream, DERNull, ASN1EncodableVector, DERObject,
// DERSequence, DERSet, DERBoolean, DERInteger,
// DEREnumerated, DERObjectIdentifier, DERBitString, DERUTF8String,
// DERPrintableString, DERIA5String, DERT61String, DERVisibleString,
// DERUniversalString, DERBMPString, DEROctetString, DERUTCTime,
// DERGeneralizedTime, DERTaggedObject, DEREncodable, DERUnknownTag,
// BERConstructedOctetString, BERNull, BERSequence, BERSet,
// BERTaggedObject, DEROutputStream
public class ASN1InputStream extends DERInputStream
{
private DERObject END_OF_STREAM;
boolean eofFound;
public ASN1InputStream(InputStream is)
{
super(is);
END_OF_STREAM = ((ASN1InputStream)this). new DERObject() {
void encode(DEROutputStream out)
throws IOException
{
throw new IOException("Eeek!");
}
};
eofFound = false;
}
protected int readLength()
throws IOException
{
int length = read();
if(length < 0)
throw new IOException("EOF found when length expected");
if(length == 128)
return -1;
if(length > 127)
{
int size = length & 0x7f;
length = 0;
for(int i = 0; i < size; i++)
{
int next = read();
if(next < 0)
throw new IOException("EOF found reading length");
length = (length << 8) + next;
}
}
return length;
}
protected void readFully(byte bytes[])
throws IOException
{
int left = bytes.length;
if(left == 0)
return;
int len;
while((len = read(bytes, bytes.length - left, left)) > 0)
if((left -= len) == 0)
return;
if(left != 0)
throw new EOFException("EOF encountered in middle of object");
else
return;
}
protected DERObject buildObject(int tag, byte bytes[])
throws IOException
{
switch(tag)
{
case 5: // '\005'
return new DERNull();
case 48: // '0'
ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
ASN1InputStream aIn = new ASN1InputStream(bIn);
ASN1EncodableVector v = new ASN1EncodableVector();
for(DERObject obj = aIn.readObject(); obj != null; obj = aIn.readObject())
v.add(obj);
return new DERSequence(v);
case 49: // '1'
ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
ASN1InputStream aIn = new ASN1InputStream(bIn);
ASN1EncodableVector v = new ASN1EncodableVector();
for(DERObject obj = aIn.readObject(); obj != null; obj = aIn.readObject())
v.add(obj);
return new DERSet(v);
case 1: // '\001'
return new DERBoolean(bytes);
case 2: // '\002'
return new DERInteger(bytes);
case 10: // '\n'
return new DEREnumerated(bytes);
case 6: // '\006'
return new DERObjectIdentifier(bytes);
case 3: // '\003'
int padBits = bytes[0];
byte data[] = new byte[bytes.length - 1];
System.arraycopy(bytes, 1, data, 0, bytes.length - 1);
return new DERBitString(data, padBits);
case 12: // '\f'
return new DERUTF8String(bytes);
case 19: // '\023'
return new DERPrintableString(bytes);
case 22: // '\026'
return new DERIA5String(bytes);
case 20: // '\024'
return new DERT61String(bytes);
case 26: // '\032'
return new DERVisibleString(bytes);
case 28: // '\034'
return new DERUniversalString(bytes);
case 30: // '\036'
return new DERBMPString(bytes);
case 4: // '\004'
return new DEROctetString(bytes);
case 23: // '\027'
return new DERUTCTime(bytes);
case 24: // '\030'
return new DERGeneralizedTime(bytes);
}
if((tag & 0x80) != 0)
{
int tagNo = tag & 0x1f;
if(tagNo == 31)
{
int idx = 0;
for(tagNo = 0; (bytes[idx] & 0x80) != 0; tagNo <<= 7)
tagNo |= bytes[idx++] & 0x7f;
tagNo |= bytes[idx] & 0x7f;
byte tmp[] = bytes;
bytes = new byte[tmp.length - (idx + 1)];
System.arraycopy(tmp, idx + 1, bytes, 0, bytes.length);
}
if(bytes.length == 0)
if((tag & 0x20) == 0)
return new DERTaggedObject(false, tagNo, new DERNull());
else
return new DERTaggedObject(false, tagNo, new DERSequence());
if((tag & 0x20) == 0)
return new DERTaggedObject(false, tagNo, new DEROctetString(bytes));
ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
ASN1InputStream aIn = new ASN1InputStream(bIn);
DEREncodable dObj = aIn.readObject();
if(aIn.available() == 0)
return new DERTaggedObject(tagNo, dObj);
ASN1EncodableVector v = new ASN1EncodableVector();
for(; dObj != null; dObj = aIn.readObject())
v.add(dObj);
return new DERTaggedObject(false, tagNo, new DERSequence(v));
} else
{
return new DERUnknownTag(tag, bytes);
}
}
private byte[] readIndefiniteLengthFully()
throws IOException
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int b;
for(int b1 = read(); (b = read()) >= 0 && (b1 != 0 || b != 0); b1 = b)
bOut.write(b1);
return bOut.toByteArray();
}
private BERConstructedOctetString buildConstructedOctetString()
throws IOException
{
Vector octs = new Vector();
do
{
DERObject o = readObject();
if(o != END_OF_STREAM)
octs.addElement(o);
else
return new BERConstructedOctetString(octs);
} while(true);
}
public DERObject readObject()
throws IOException
{
int tag = read();
if(tag == -1)
if(eofFound)
{
throw new EOFException("attempt to read past end of file.");
} else
{
eofFound = true;
return null;
}
int length = readLength();
if(length < 0)
{
switch(tag)
{
case 5: // '\005'
return new BERNull();
case 48: // '0'
ASN1EncodableVector v = new ASN1EncodableVector();
do
{
DERObject obj = readObject();
if(obj != END_OF_STREAM)
v.add(obj);
else
return new BERSequence(v);
} while(true);
case 49: // '1'
ASN1EncodableVector v = new ASN1EncodableVector();
do
{
DERObject obj = readObject();
if(obj != END_OF_STREAM)
v.add(obj);
else
return new BERSet(v);
} while(true);
case 36: // '$'
return buildConstructedOctetString();
}
if((tag & 0x80) != 0)
{
int tagNo = tag & 0x1f;
if(tagNo == 31)
{
int b = read();
tagNo = 0;
for(; b >= 0 && (b & 0x80) != 0; b = read())
{
tagNo |= b & 0x7f;
tagNo <<= 7;
}
tagNo |= b & 0x7f;
}
if((tag & 0x20) == 0)
{
byte bytes[] = readIndefiniteLengthFully();
return new BERTaggedObject(false, tagNo, new DEROctetString(bytes));
}
DERObject dObj = readObject();
if(dObj == END_OF_STREAM)
return new DERTaggedObject(tagNo);
DERObject next = readObject();
if(next == END_OF_STREAM)
return new BERTaggedObject(tagNo, dObj);
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(dObj);
do
{
v.add(next);
next = readObject();
} while(next != END_OF_STREAM);
return new BERTaggedObject(false, tagNo, new BERSequence(v));
} else
{
throw new IOException("unknown BER object encountered");
}
}
if(tag == 0 && length == 0)
{
return END_OF_STREAM;
} else
{
byte bytes[] = new byte[length];
readFully(bytes);
return buildObject(tag, bytes);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -