public static BERNode Create(Stream strm) { BERNode ret = null; Tag tg = BER.DecodeTag(strm); uint lenght = 0; bool indefiniteForm; BER.DecodeLength(strm, out lenght, out indefiniteForm); if (tg.isPrimitive) { byte[] data = new byte[lenght]; if (strm.Read(data, 0, (int)lenght) != lenght) throw new LengthMismatchException(); ret = new BERNodePrimitive(tg, data); } else { BERNodeConstructed retCon = new BERNodeConstructed(tg); ret = retCon; // if (lenght == 0 && (strm.Position != strm.Length)) if (indefiniteForm) { while (!BER.AreNextTwoBytesZeros(strm)) { retCon.m_children.Add(Create(strm)); } BER.DecodeTwoZeros(strm); } else { long initPos = strm.Position; while (strm.Position < initPos + lenght) { retCon.m_children.Add(Create(strm)); } if (strm.Position != initPos + lenght) throw new LengthMismatchException(); } } return ret; }
public static IEnumerable<BERNode> GetNodes(Stream strm) { BERNode ret = null; Tag tg = BER.DecodeTag(strm); uint lenght = 0; bool indefiniteForm; BER.DecodeLength(strm, out lenght, out indefiniteForm); if (tg.isPrimitive) { byte[] data = new byte[lenght]; if (strm.Read(data, 0, (int)lenght) != lenght) throw new LengthMismatchException(); ret = new BERNodePrimitive(tg, data); yield return ret; } else { yield return new BERNodeConstructed(tg); if (indefiniteForm) { while (!BER.AreNextTwoBytesZeros(strm)) { foreach(BERNode n in GetNodes(strm)) yield return n; } BER.DecodeTwoZeros(strm); } else { long initPos = strm.Position; while (strm.Position < initPos + lenght) { foreach (BERNode n in GetNodes(strm)) yield return n; } if (strm.Position != initPos + lenght) throw new LengthMismatchException(); } } }