// 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;
            }
        }
        /// <summary>
        /// Decodes a tag from the buffer.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="tag"></param>
        /// <returns>The number of the bytes consumed in the buffer to decode the tag.</returns>
        public static int TagBerDecode(IAsn1DecodingBuffer buffer, out Asn1Tag tag)
        {
            byte        prefix       = buffer.ReadByte();
            int         firstTwoBits = (prefix >> 6);
            Asn1TagType tagType      = Asn1TagType.Private;
            long        tagValue;

            switch (firstTwoBits)
            {
            case 0:
            {
                tagType = Asn1TagType.Universal;
            } break;

            case 1:
            {
                tagType = Asn1TagType.Application;
            } break;

            case 2:
            {
                tagType = Asn1TagType.Context;
            } break;

            case 3:
            {
                throw new NotImplementedException(ExceptionMessages.Unreachable);
            };
            }
            tagValue = prefix & ((byte)(1 << 5) - 1);
            if (tagValue <= 30)
            {
                tag = new Asn1Tag(tagType, tagValue);
                if ((prefix & (1 << 5)) != 0)
                {
                    tag.EncodingWay = EncodingWay.Constructed;
                }
                else
                {
                    tag.EncodingWay = EncodingWay.Primitive;
                }
                return(1);
            }
            else
            {
                throw new NotImplementedException("Case tag > 30 is not implemented. Check X.690: 8.1.2.4.3.");
            }
        }
 /// <summary>
 /// Initializes a new instance of the Asn1Tag class with a given tag.
 /// </summary>
 /// <param name="TagValue">The value of the given tag.</param>
 /// <param name="TagType">The type of the given tag.</param>
 public Asn1Tag(Asn1TagType TagType, long TagValue)
 {
     this.type = TagType;
     this.val = TagValue;
     this.encodingWay = null;
 }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the Asn1Tag class with a given tag.
 /// </summary>
 /// <param name="TagValue">The value of the given tag.</param>
 /// <param name="TagType">The type of the given tag.</param>
 public Asn1Tag(Asn1TagType TagType, long TagValue)
 {
     this.type        = TagType;
     this.val         = TagValue;
     this.encodingWay = null;
 }
Beispiel #5
0
 public Asn1Tag()
 {
     this.type        = 0;
     this.val         = 0;
     this.encodingWay = 0;
 }
Beispiel #6
0
        // 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;
              }
        }