/// <summary> /// Initializes a new instance of the <see cref="DataElement"/> struct. /// </summary> /// <param name="class">The class.</param> /// <param name="pc">The PC.</param> /// <param name="tag">The tag.</param> /// <param name="content">The content.</param> public DataElement(BerClass @class, BerPC pc, BerTag tag, byte[] content) : this() { this.Class = @class; this.PC = pc; this.Tag = tag; this.Content = content; }
public Ber(BerClass cl, BerTags tg, bool c) { allSet(); if ((int)cl > 3) { throw new Exception("class id must be from 0 to 3"); } tagClass = (byte)((int)cl & 3); _container = c; _tag = (ulong)tg; }
public Ber(BerClass cl, BerTags tg, bool c, byte[] pl) { allSet(); if ((int)cl > 3) { throw new Exception("class id must be from 0 to 3"); } tagClass = (byte)((int)cl & 3); _container = c; _tag = (ulong)tg; _payload = pl; _payloadLength = pl.Length; }
/// <summary> /// Reads a sequence of ASN.1 elements from a stream. /// </summary> /// <param name="stream">The stream.</param> /// <returns>A sequence of elements.</returns> /// <remarks> /// The stream may not contain exclusively ASN.1 data. /// This method will read the stream exactly one element at a time, /// and the caller should only enumerate as many elements as are expected /// to avoid reading into other data. /// If the end of the stream is reached, the sequence terminates. /// </remarks> internal static IEnumerable <DataElement> ReadAsn1Elements(this Stream stream) { Requires.NotNull(stream, "stream"); do { int b = stream.ReadByte(); if (b == -1) { yield break; } BerClass clazz = (BerClass)b & BerClass.Mask; BerPC pc = (BerPC)b & BerPC.Mask; BerTag tag = (BerTag)b & BerTag.Mask; uint length = 0; b = stream.ReadByte(); if ((b & 0x80) == 0x80) { // long form byte lengthOfLength = (byte)(b & 0x7F); for (int i = 0; i < lengthOfLength; i++) { // big endian b = stream.ReadByte(); length <<= 8; length += (uint)b; } } else { // short form. length = (uint)b; } if (length > 8 * 1024) { throw new FormatException("Invalid format or length too large."); } byte[] content = new byte[length]; int bytesRead = stream.Read(content, 0, (int)length); if (bytesRead != length) { throw new ArgumentException("Unexpected end of stream."); } yield return(new DataElement(clazz, pc, tag, content)); }while (true); }
/// <summary> /// Reverses the operation GetShortClassName(), returning true /// if <paramref name="name"/> could be parsed into a BerClass value. /// </summary> public static bool GetClassFromName(string name, bool ignoreCase, out BerClass tagClass) { var comparison = ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; for (int index = 0; index < ShortClassNames.Length; index++) { if (String.Compare(ShortClassNames[index], name, comparison) == 0) { tagClass = (BerClass)(index << 6); return(true); } } tagClass = BerClass.Universal; return(false); }
/// <summary> /// Constructs a new BerTag instance. /// </summary> /// <param name="tagClass">The tag class.</param> /// <param name="number">The tag number.</param> /// <param name="isContainer">If true, the container flag is set.</param> public BerTag(BerClass tagClass, uint number, bool isContainer) : this(tagClass, number) { IsContainer = isContainer; }
/// <summary> /// Constructs a new BerTag instance, not setting /// the container flag. /// </summary> /// <param name="tagClass">The tag class.</param> /// <param name="number">The tag number.</param> public BerTag(BerClass tagClass, uint number) : this() { Preamble = (byte)(tagClass & BerClass.Mask); Number = number; }
/// <summary> /// Initializes a new instance of the <see cref="DataElement"/> struct. /// </summary> /// <param name="class">The class.</param> /// <param name="pc">The PC.</param> /// <param name="tag">The tag.</param> /// <param name="nestedElements">The content.</param> public DataElement(BerClass @class, BerPC pc, BerTag tag, params DataElement[] nestedElements) : this(@class, pc, tag, Asn.WriteAsn1Elements(nestedElements)) { }
/// <summary> /// Gets the name of the tag class identified by <paramref name="tagClass"/>: /// either 'C' (context-specific), 'A' (application), 'U' (universal) or /// 'P' (private). /// </summary> public static string GetShortClassName(BerClass tagClass) { return(ShortClassNames[(byte)tagClass >> 6]); }
/// <summary> /// Gets the name of the tag class identified by <paramref name="tagClass"/>: /// either 'C' (context-specific), 'A' (application), 'U' (universal) or /// 'P' (private). /// </summary> public static string GetShortClassName(BerClass tagClass) { return ShortClassNames[(byte)tagClass >> 6]; }
/// <summary> /// Reverses the operation GetShortClassName(), returning true /// if <paramref name="name"/> could be parsed into a BerClass value. /// </summary> public static bool GetClassFromName(string name, bool ignoreCase, out BerClass tagClass) { var comparison = ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; for(int index = 0; index < ShortClassNames.Length; index++) { if(String.Compare(ShortClassNames[index], name, comparison) == 0) { tagClass = (BerClass)(index << 6); return true; } } tagClass = BerClass.Universal; return false; }