/// <summary> /// Recursively go through internal node and make corresponding structure of Asn1TreeNodes. /// </summary> /// <param name="treeNode">Node whose children will be filled by this method.</param> /// <param name="node">Internal node that will be processed to get tree of Asn1TreeNodes.</param> /// <param name="depth">Current depth of processing.</param> /// <param name="parseEncapsulatedData">Flag if encapsulated data should be parsed as well.</param> internal static void MakeTreeNode(TreeNode treeNode, InternalNode node, int depth, bool parseEncapsulatedData) { // count new depth if node has children int innerdepth = (node.ChildNodes.Count > 0) ? depth + 1 : depth; // recursively go through children and print structure of them foreach (InternalNode innerNode in node.ChildNodes) { if (parseEncapsulatedData) { ParseEncapsulatedData(innerNode); } try { var innerTreeNode = new Asn1TreeNode(innerNode); treeNode.Nodes.Add(innerTreeNode); MakeTreeNode(innerTreeNode, innerNode, innerdepth, parseEncapsulatedData); } catch (Exception) { ; } } }
/// <summary> /// Load ASN.1 object in form of a stream, parse it and display its structure. /// </summary> /// <param name="asn1Content">ASN.1 object to parse.</param> public void LoadContent(Stream asn1Content) { // clear any existing nodes Nodes.Clear(); // use parser to get the structure using (var reader = new BerReader(asn1Content)) { var rootNode = new InternalNode(); try { // read whole object. This may fail if there is no valid ASN.1 object in the stream. rootNode = reader.ReadToEnd(ReadContent); } catch (Exception ex) { // something went wrong when reading file. Possibly it was not an ASN.1 object. Trace.WriteLine(String.Format("Parsing exception: {0}", ex)); var safetyNode = new Asn1TreeNode(null, "Invalid ASN.1 structure."); Nodes.Add(safetyNode); } // reader does not parse encapsulated data. Do it manually. foreach (InternalNode internalNode in rootNode.ChildNodes) { // This will enrich list of nodes with additional nodes parsed out of values of Primitive ASN.1 nodes if (EncapsulatedDataParsing) { ParseEncapsulatedData(internalNode); } // build tree from list of ASN.1 nodes var rootTreeNode = new Asn1TreeNode(internalNode); MakeTreeNode(rootTreeNode, internalNode, 1, EncapsulatedDataParsing); Nodes.Add(rootTreeNode); } } // expand tree ExpandAll(); SelectedNode = Nodes[0]; }