// Public Constructors public Asn1Tag(Stream dataStream) : base() { Byte b = ReadByte(dataStream); tagNumber = GetNumber(b); tagType = GetType(b); tagClass = GetClass(b); if (tagNumber == Asn1TagNumber.HIGHNUMBER) { UInt32 tagNo = 0; b = ReadByte(dataStream); if ((b & 0x7F) == 0x00) { throw new IOException("Invalid tag number!"); } else { while ((b & 0x80) == 0x80) { // Get bits from intermdeiate octets tagNo |= (UInt32)(b & 0x7F); tagNo <<= 7; // Read next byte from dataStream b = ReadByte(dataStream); } // Get bits from last octet tagNo |= (UInt32)(b & 0x7F); } tagNumber = (Asn1TagNumber)tagNo; } }
public static Asn1Node ReadNode(Stream stream) { var identifier = stream.ReadByte(); Asn1UniversalNodeType type = (Asn1UniversalNodeType)(identifier & 0x1f); Asn1TagClass tagClass = (Asn1TagClass)(identifier >> 6); Asn1TagForm tagForm = (Asn1TagForm)((identifier >> 5) & 1); int?length = ReadTagLength(stream); if (identifier == 0 && length == 0) { return(null); // EOC detected } if (length > stream.Length) { throw new Asn1ParsingException($"Try to read more bytes from stream than exists {length} > {stream.Length}"); } if (length != null) { var data = new byte[(int)length]; stream.Read(data, 0, (int)length); stream = new MemoryStream(data); if (tagClass == Asn1TagClass.Universal) { var tag = ReadUniversalNode(type, tagForm, stream); tag.TagClass = tagClass; return(tag); } else { var tag = Asn1CustomNode.ReadFrom(type, tagForm, stream); tag.TagClass = tagClass; return(tag); } } else { // We must just keep on reading the stream rather than keeping a separate memory buffer Asn1DynamicArray tag = Asn1DynamicArray.ReadFrom(type, stream); tag.TagClass = tagClass; return(tag); } }
public Asn1Node FindSingleNode(Asn1TagClass tagClass, int tagId) { return(Nodes.FirstOrDefault(n => n.Is(tagClass, tagId))); }
public bool Is(Asn1TagClass @class, int tagId) { return(TagClass == @class && (int)NodeType == tagId); }
public Asn1CustomNode(int type, Asn1TagForm tagForm, Asn1TagClass tagClass) { Type = (Asn1UniversalNodeType)type; TagForm = tagForm; TagClass = tagClass; }
// Public Constructors public Asn1Tag( Stream dataStream ) : base() { Byte b = ReadByte(dataStream); tagNumber = GetNumber(b); tagType = GetType(b); tagClass = GetClass(b); if (tagNumber == Asn1TagNumber.HIGHNUMBER) { UInt32 tagNo = 0; b = ReadByte(dataStream); if ((b & 0x7F) == 0x00) throw new IOException("Invalid tag number!"); else { while ((b & 0x80) == 0x80) { // Get bits from intermdeiate octets tagNo |= (UInt32)(b & 0x7F); tagNo <<= 7; // Read next byte from dataStream b = ReadByte(dataStream); } // Get bits from last octet tagNo |= (UInt32) (b & 0x7F); } tagNumber = (Asn1TagNumber) tagNo; } }