protected override Asn1Object CreateSpecificTaggedObject(Asn1Document document, TaggedObject content, Asn1Object parent) { var asn1 = base.CreateSpecificTaggedObject(document, content, parent); if (asn1 != null) { return(asn1); } var tagValue = content.Tag.Value; var ef = Tags.FindEF(tagValue); if (!ef.HasValue) { return(null); } switch (ef.Value) { case Tags.EF.DG1: return(new Asn1DG1Data(document, content, parent)); } return(null); }
/// <summary> /// Parses the portion of the document at the current position, according to the /// instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro being executed.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { if (Document.namedNodes.TryGetValue(this.identifier, out Asn1Node Node)) { if (Node is Asn1TypeDefinition TypeDef) { return(TypeDef.Definition.Parse(Document, Macro)); } else if (Node is Asn1Type Type) { return(Type.Parse(Document, Macro)); } else { throw Document.SyntaxError("Type reference expected: " + this.identifier); } } foreach (SupportingSyntax Syntax in Macro.SupportingSyntax) { if (Syntax.Name == this.identifier) { return(Syntax.Parse(Document, Macro)); } } throw Document.SyntaxError("Supporting syntax for " + this.identifier + " not found."); }
/// <summary> /// Represents one import instruction. /// </summary> /// <param name="Identifiers">Identifiers to import.</param> /// <param name="Module">Module reference.</param> /// <param name="Document">ASN.1 Document</param> public Asn1Import(string[] Identifiers, string Module, Asn1Document Document) { this.identifiers = Identifiers; this.module = Module; this.document = Document; this.importedDocument = null; }
/// <summary> /// Parses the portion of the document at the current position, according to the /// instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro being executed.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { int Bak = Document.pos; int BestPos = -1; Asn1Node Best = null; Asn1Node Option; foreach (UserDefinedItem Item in this.options) { Document.pos = Bak; try { Option = Item.Parse(Document, Macro); if (Best is null || Document.pos > BestPos) { BestPos = Document.pos; Best = Option; } } catch (Exception) { // Ignore } } if (BestPos < 0) { throw Document.SyntaxError("Invalid option."); } Document.pos = BestPos; return(Best); }
public void ParseData() { try { asnTreeView.Nodes.Clear(); hexViewer.Select(0, 0); asciiTextBox.Clear(); bytesTextBox.Clear(); if (IsIcaoMrtd) { var doc = new Asn1IcaoDocument(content, ParseOctetStrings, ShowInvalidTaggedObjects); asnTreeView.CreateDocumentNodes(doc, "ICAO Document"); } else { var doc = new Asn1Document(content, ParseOctetStrings, ShowInvalidTaggedObjects); asnTreeView.CreateDocumentNodes(doc, "Document"); } asnTreeView.ExpandAll(); } catch (Exception ex) { HandleException("Unable to decode content: {0}.", ex); } }
/// <summary> /// Represents an ASN.1 field value definition. /// </summary> /// <param name="FieldName">Field name.</param> /// <param name="Type">Type</param> /// <param name="Value">Value</param> /// <param name="Document">ASN.1 Document in where the value is defined.</param> public Asn1FieldValueDefinition(string FieldName, Asn1Type Type, Asn1Value Value, Asn1Document Document) : base() { this.fieldName = FieldName; this.type = Type; this.value = Value; this.document = Document; }
/// <summary> /// ASN.1 Macro /// </summary> /// <param name="Name">Name of macro.</param> /// <param name="TypeNotation">Type Notation</param> /// <param name="ValueNotation">Value Notation</param> /// <param name="SupportingSyntax">Supporting Syntax</param> /// <param name="Document">Document defining macro.</param> public Asn1Macro(string Name, UserDefinedItem TypeNotation, UserDefinedItem ValueNotation, SupportingSyntax[] SupportingSyntax, Asn1Document Document) { this.name = Name; this.typeNotation = TypeNotation; this.valueNotation = ValueNotation; this.supportingSyntax = SupportingSyntax; this.document = Document; }
/// <summary> /// Represents a collection of ASN.1 definitions. /// </summary> /// <param name="Identifier">Identifier</param> /// <param name="Oid">Optional Object ID</param> /// <param name="Tags">How tags are handled.</param> /// <param name="Abstract">If abstract syntax is used.</param> /// <param name="Body">Definition body.</param> /// <param name="Document">ASN.1 document</param> public Asn1Definitions(string Identifier, Asn1Oid Oid, Asn1Tags?Tags, bool Abstract, Asn1Module Body, Asn1Document Document) { this.identifier = Identifier; this.oid = Oid; this.tags = Tags; this._abstract = Abstract; this.body = Body; this.document = Document; }
/// <summary> /// Parses a type from the portion of the document at the current position, /// according to the instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <returns>Parsed ASN.1 type node.</returns> public Asn1Type ParseType(Asn1Document Document) { if (this.typeNotation.Parse(Document, this) is Asn1Type Type) { return(Type); } else { throw Document.SyntaxError("Unable to evaluate type."); } }
/// <summary> /// Parses the portion of the document at the current position, according to the type. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro performing parsing.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { if (Document.ParseValue() is Values.Asn1BooleanValue Value) { return(Value); } else { throw Document.SyntaxError("Boolean value expected."); } }
/// <summary> /// Parses the portion of the document at the current position, according to the /// instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro being executed.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { switch (this.Identifier.ToUpper()) { case "TYPE": return(Document.ParseType(this.Identifier, false)); case "VALUE": return(Document.ParseValue()); default: return(this.type.Parse(Document, Macro)); } }
/// <summary> /// Parses the portion of the document at the current position, according to the /// instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro being executed.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { Asn1Node Result = null; foreach (UserDefinedItem Item in this.items) { Result = Item.Parse(Document, Macro); } return(Result); }
/// <summary> /// Parses the portion of the document at the current position, according to the type. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro performing parsing.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { if (Document.ParseValue() is Values.Asn1FloatingPointValue Value) { return(Value); } else { throw Document.SyntaxError("String value expected."); } }
/// <summary> /// Parses the portion of the document at the current position, according to the /// instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <returns>Parsed ASN.1 node.</returns> public KeyValuePair <Asn1Type, Asn1Value> Parse(Asn1Document Document) { this.typeNotation.Parse(Document, this); Asn1Node Node = this.valueNotation.Parse(Document, this); if (!(Node is Asn1Value Value)) { throw Document.SyntaxError("Value expected."); } return(new KeyValuePair <Asn1Type, Asn1Value>(null, Value)); }
/// <summary> /// Parses the portion of the document at the current position, according to the type. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro performing parsing.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { if (!(Macro.Document.namedNodes.TryGetValue(this.identifier, out Asn1Node Node))) { throw Document.SyntaxError("Type named " + this.identifier + " not found."); } if (!(Node is Asn1Type Type)) { throw Document.SyntaxError("Type expected."); } return(Type.Parse(Document, Macro)); }
/// <summary> /// Parses a value from the portion of the document at the current position, /// according to the instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <returns>Parsed ASN.1 value node.</returns> public Asn1Value ParseValue(Asn1Document Document) { this.typeNotation.Parse(Document, this); Document.AssertNextToken("::="); Asn1Node Node = this.valueNotation.Parse(Document, this); if (!(Node is Asn1Value Value)) { throw Document.SyntaxError("Value expected."); } return(Value); }
/// <summary> /// ASN.1 Macro /// </summary> /// <param name="Name">Name of macro.</param> /// <param name="TypeNotation">Type Notation</param> /// <param name="ValueNotation">Value Notation</param> /// <param name="SupportingSyntax">Supporting Syntax</param> /// <param name="Document">Document defining macro.</param> public Asn1Macro(string Name, UserDefinedItem TypeNotation, UserDefinedItem ValueNotation, SupportingSyntax[] SupportingSyntax, Asn1Document Document) { this.name = Name; this.typeNotation = TypeNotation; this.valueNotation = ValueNotation; this.supportingSyntaxArray = SupportingSyntax; this.document = Document; this.supportingSyntax = new Dictionary <string, SupportingSyntax>(); foreach (SupportingSyntax Syntax in SupportingSyntax) { this.supportingSyntax[Syntax.Name] = Syntax; } }
protected override Asn1Object CreateSpecificTaggedObject(Asn1Document document, TaggedObject content, Asn1Object parent) { var result = base.CreateSpecificTaggedObject(document, content, parent); if (result != null) { return(result); } var tag = content.Tag.Value; if (tag == (ushort)Tags.Icao.Mrz) { return(new Asn1Mrz(document, content, parent)); } return(null); }
public TreeNodeEx CreateDocumentNodes(Asn1Document document, string rootNodeText) { var rootNode = new TreeNodeEx(rootNodeText); rootNode.SelectedImageIndex = documentImageIndex; rootNode.ImageIndex = documentImageIndex; rootNode.Tag = document; foreach (var node in document.Nodes) { AddAsnNode(node, rootNode); } Nodes.Add(rootNode); return(rootNode); }
/// <summary> /// Parses the portion of the document at the current position, according to the /// instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro being executed.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { int Bak = Document.pos; foreach (UserDefinedItem Item in this.options) { Document.pos = Bak; try { return(Item.Parse(Document, Macro)); } catch (Exception) { // Ignore } } throw Document.SyntaxError("Invalid option."); }
/// <summary> /// Parses the portion of the document at the current position, according to the type. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro performing parsing.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { int Bak = Document.pos; foreach (Asn1Node Choice in this.Nodes) { if (Choice is Asn1Type Type) { try { Document.pos = Bak; return(Type.Parse(Document, Macro)); } catch (Exception) { // Ignore } } } throw Document.SyntaxError("Unable to parse choices."); }
/// <summary> /// Parses the portion of the document at the current position, according to the /// instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro being executed.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { if (Macro.Document.namedNodes.TryGetValue(this.identifier, out Asn1Node Node)) { if (Node is Asn1TypeDefinition TypeDef) { return(TypeDef.Definition.Parse(Document, Macro)); } else if (Node is Asn1Type Type) { return(Type.Parse(Document, Macro)); } else if (Node is Asn1FieldDefinition FieldDef) { return(FieldDef.Type.Parse(Document, Macro)); } else { throw Document.SyntaxError("Type reference expected: " + this.identifier); } } if (Macro.supportingSyntax.TryGetValue(this.identifier, out SupportingSyntax Syntax)) { return(Syntax.Parse(Document, Macro)); } switch (this.identifier.ToLower()) { case "empty": return(null); case "type": return(Document.ParseType(this.Identifier, false)); case "value": return(Document.ParseValue()); default: throw Document.SyntaxError("Supporting syntax for " + this.identifier + " not found."); } }
/// <summary> /// Loads the ASN.1 document to import. /// </summary> /// <exception cref="FileNotFoundException">If the import file is not found.</exception> public Asn1Document LoadDocument() { if (!(this.importedDocument is null)) { return(this.importedDocument); } string Folder = Path.GetDirectoryName(this.document.Location); string FileName = Path.Combine(Folder, this.module); string Extension = Path.GetExtension(this.document.Location); if (!File.Exists(FileName)) { FileName = null; foreach (string ImportFolder in this.document.ImportFolders) { FileName = Path.Combine(ImportFolder, this.module) + Extension; if (File.Exists(FileName)) { break; } else { FileName = null; } } if (FileName is null) { throw new FileNotFoundException("Unable to find import file for module " + this.module); } } this.importedDocument = Asn1Document.FromFile(FileName, this.document.ImportFolders); return(this.importedDocument); }
/// <summary> /// Parses the portion of the document at the current position, according to the /// instructions available in the macro. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro being executed.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { Document.AssertNextToken(this.value); return(new Values.Asn1StringValue(this.value)); }
/// <summary> /// Exports to C# /// </summary> /// <param name="Output">C# Output.</param> /// <param name="State">C# export state.</param> /// <param name="Indent">Indentation</param> /// <param name="Pass">Export pass</param> public override void ExportCSharp(StringBuilder Output, CSharpExportState State, int Indent, CSharpExportPass Pass) { if (Pass == CSharpExportPass.Explicit && !(this.body is null)) { if (!(this.body.Imports is null)) { foreach (Asn1Import Import in this.body.Imports) { if (string.IsNullOrEmpty(Import.Module)) { continue; } Asn1Document ImportedDocument; if (State.Settings.ContainsCode(Import.Module)) { ImportedDocument = State.Settings.GetDocument(Import.Module); } else { ImportedDocument = Import.LoadDocument(); string CSharp = ImportedDocument.ExportCSharp(State.Settings); State.Settings.AddCode(Import.Module, CSharp, ImportedDocument); } Output.Append("using "); Output.Append(State.Settings.Namespace(Import.Module)); Output.AppendLine(";"); } Output.AppendLine(); } else { Output.AppendLine(); } Output.Append(Tabs(Indent)); Output.Append("namespace "); Output.Append(State.Settings.BaseNamespace); Output.Append('.'); Output.AppendLine(ToCSharp(this.identifier)); Output.Append(Tabs(Indent)); Output.AppendLine("{"); Indent++; if (!(this.body.Imports is null)) { foreach (Asn1Import Import in this.body.Imports) { if (string.IsNullOrEmpty(Import.Module)) { continue; } Asn1Document ImportedDocument = State.Settings.GetDocument(Import.Module); Asn1Node[] Nodes = ImportedDocument.Root?.Body?.Items; if (!(Nodes is null)) { foreach (Asn1Node Node in Nodes) { if (Node is Asn1TypeDefinition TypeDef && !TypeDef.ConstructedType && Array.IndexOf <string>(Import.Identifiers, TypeDef.Name) >= 0) { TypeDef.ExportCSharp(Output, State, Indent, CSharpExportPass.Preprocess); } } } } } this.body.ExportCSharp(Output, State, Indent, CSharpExportPass.Preprocess); State.ClosePending(Output); this.body.ExportCSharp(Output, State, Indent, CSharpExportPass.Variables); State.ClosePending(Output); this.body.ExportCSharp(Output, State, Indent, CSharpExportPass.Explicit); State.ClosePending(Output); Indent--; Output.Append(Tabs(Indent)); Output.AppendLine("}"); }
public static string GenerateCSharp(string FileName) { string BaseNamespace = "Test"; Asn1Document Doc = ParsingTests.ParseAsn1Document(FileName); CSharpExportSettings Settings = new CSharpExportSettings(BaseNamespace); string CSharp = Doc.ExportCSharp(Settings); List <SyntaxTree> Modules = new List <SyntaxTree>() { CSharpSyntaxTree.ParseText(CSharp) }; foreach (string ImportedModule in Settings.Modules) { string CSharp2 = Settings.GetCode(ImportedModule); Modules.Add(CSharpSyntaxTree.ParseText(CSharp2)); } Dictionary <string, bool> Dependencies = new Dictionary <string, bool>() { { GetLocation(typeof(object)), true }, { Path.Combine(Path.GetDirectoryName(GetLocation(typeof(object))), "System.Runtime.dll"), true }, { Path.Combine(Path.GetDirectoryName(GetLocation(typeof(Encoding))), "System.Text.Encoding.dll"), true }, { Path.Combine(Path.GetDirectoryName(GetLocation(typeof(MemoryStream))), "System.IO.dll"), true }, { Path.Combine(Path.GetDirectoryName(GetLocation(typeof(MemoryStream))), "System.Runtime.Extensions.dll"), true }, { Path.Combine(Path.GetDirectoryName(GetLocation(typeof(Task))), "System.Threading.Tasks.dll"), true }, { GetLocation(typeof(Asn1Document)), true } }; List <MetadataReference> References = new List <MetadataReference>(); foreach (string Location in Dependencies.Keys) { if (!string.IsNullOrEmpty(Location)) { References.Add(MetadataReference.CreateFromFile(Location)); } } CSharpCompilation Compilation = CSharpCompilation.Create( BaseNamespace, Modules.ToArray(), References, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); MemoryStream Output = new MemoryStream(); MemoryStream PdbOutput = new MemoryStream(); EmitResult CompilerResults = Compilation.Emit(Output, pdbStream: PdbOutput); if (!CompilerResults.Success) { StringBuilder sb = new StringBuilder(); foreach (Diagnostic Error in CompilerResults.Diagnostics) { sb.AppendLine(); sb.Append(Error.Location.ToString()); sb.Append(": "); sb.Append(Error.GetMessage()); } sb.AppendLine(); sb.AppendLine(); sb.AppendLine("Code generated:"); sb.AppendLine(); sb.AppendLine(CSharp); foreach (string ImportedModule in Settings.Modules) { string CSharp2 = Settings.GetCode(ImportedModule); sb.AppendLine(); sb.AppendLine(new string('-', 80)); sb.AppendLine(); sb.AppendLine(CSharp2); } throw new Exception(sb.ToString()); } Console.Out.WriteLine(CSharp); return(CSharp); }
/// <summary> /// Parses the portion of the document at the current position, according to the type. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro performing parsing.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { return(this.definition.Parse(Document, Macro)); }
/// <summary> /// Parses the portion of the document at the current position, according to the type. /// </summary> /// <param name="Document">ASN.1 document being parsed.</param> /// <param name="Macro">Macro performing parsing.</param> /// <returns>Parsed ASN.1 node.</returns> public override Asn1Node Parse(Asn1Document Document, Asn1Macro Macro) { return(null); }
/// <summary> /// Represents a named value. /// </summary> /// <param name="Identifier">Identifier</param> /// <param name="Value">Optional Value</param> /// <param name="Document">ASN.1 Document containing the reference</param> public Asn1NamedValue(string Identifier, Asn1Value Value, Asn1Document Document) : base(Identifier, Document) { this.value = Value; }
/// <summary> /// Adds C# code for a given identifier. /// </summary> /// <param name="Identifier">Identifier.</param> /// <param name="Code">C# Code</param> /// <param name="Document">Document</param> public void AddCode(string Identifier, string Code, Asn1Document Document) { this.imported[Identifier] = new KeyValuePair <Asn1Document, string>(Document, Code); }