All files / src/analysis ASN1Visitor.ts

100% Statements 85/85
93.33% Branches 28/30
100% Functions 21/21
100% Lines 85/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197  1x 1x 1x 1x 1x 1x 1x 1x   1x           1x 1x                                               1x       1x   1x   6x 6x       6x 6x 6x 6x       6x 6x       124x 124x 124x 124x 124x       801x 533x   268x 268x         533x       268x 266x   2x 2x         529x 342x   187x 69x   118x 6x   112x 49x   63x 7x   56x 4x   52x       342x       342x 342x 342x 342x       342x 342x 342x       342x 38x   304x       342x       266x 266x       69x 69x 69x       325x 325x 119x   325x       4x 4x 4x       8x 8x       333x 333x 333x 333x       7x       7x 2x   5x 4x   1x      
// import {inspect} from 'util';
import {ASN1Assignment} from '../asn1/ASN1Assignment';
import {ASN1DefinedType} from '../asn1/ASN1DefinedType';
import {ASN1Module} from '../asn1/ASN1Module';
import {ASN1NamedType} from '../asn1/ASN1NamedType';
import {ASN1Sequence} from '../asn1/ASN1Sequence';
import {ASN1ChoiceType} from '../asn1/ASN1ChoiceType';
import {ASN1Tag} from '../asn1/ASN1Tag';
import {ASN1TaggedType} from '../asn1/ASN1TaggedType';
import {ASN1Type} from '../asn1/ASN1Type';
import {
  ASN1BooleanType,
  ASN1GeneralStringType,
  ASN1IA5StringType,
  ASN1IntegerType,
} from '../asn1/types/builtin';
import {TagClass} from '../interfaces/TagClass';
import {ASN1CstParser} from './ASN1CstParser';
import {
  ASN1CstNode,
  ModuleIdentifierCstNode,
  TypeAssignmentCstNode,
  TypeCstNode,
  SequenceTypeCstNode,
  BuiltinTypeCstNode,
  NamedTypeCstNode,
  ComponentTypeCstNode,
  CharacterStringTypeCstNode,
  ConstrainedTypeCstNode,
  PrefixedTypeCstNode,
  TaggedTypeCstNode,
  TagCstNode,
  ClassCstNode,
  ClassNumberCstNode,
  DefinedTypeCstNode,
  ReferencedTypeCstNode,
  RestrictedCharacterStringTypeCstNode,
  ChoiceTypeCstNode,
  AlternativeTypeCstNode,
} from './interfaces';
 
const parserInstance = new ASN1CstParser();
// The base visitor class can be accessed via the a parser instance.
// const BaseASN1Visitor = parserInstance.getBaseCstVisitorConstructor();
const BaseASN1VisitorWithDefaults =
  parserInstance.getBaseCstVisitorConstructorWithDefaults();
 
export class ASN1Visitor extends BaseASN1VisitorWithDefaults {
  constructor() {
    super();
    this.validateVisitor();
  }
 
  ModuleDefinition(ctx: ASN1CstNode): ASN1Module {
    const name = this.visit(ctx.ModuleIdentifier);
    const module = new ASN1Module(name);
    this.visit(ctx.ModuleBody, module);
    return module;
  }
 
  ModuleIdentifier(ctx: ModuleIdentifierCstNode) {
    const name = ctx.TypeReference[0].image;
    return name;
  }
 
  TypeAssignment(ctx: TypeAssignmentCstNode, module: ASN1Module) {
    const name = ctx.TypeReference[0].image;
    const assignment = new ASN1Assignment(name);
    const type = this.visit(ctx.Type);
    assignment.setType(type);
    module.addAssignment(assignment);
  }
 
  Type(ctx: TypeCstNode) {
    if (ctx.ConstrainedType) {
      return this.visit(ctx.ConstrainedType);
    }
    Eif (ctx.ReferencedType) {
      return this.visit(ctx.ReferencedType);
    }
  }
 
  ConstrainedType(ctx: ConstrainedTypeCstNode) {
    return this.visit(ctx.BuiltinType);
  }
 
  ReferencedType(ctx: ReferencedTypeCstNode) {
    if (ctx.DefinedType) {
      return this.visit(ctx.DefinedType);
    }
    Eif (ctx.UsefulType) {
      return this.visit(ctx.UsefulType);
    }
  }
 
  BuiltinType(ctx: BuiltinTypeCstNode) {
    if (ctx.PrefixedType) {
      return this.visit(ctx.PrefixedType);
    }
    if (ctx.SequenceType) {
      return this.visit(ctx.SequenceType);
    }
    if (ctx.BooleanType) {
      return new ASN1BooleanType();
    }
    if (ctx.IntegerType) {
      return new ASN1IntegerType();
    }
    if (ctx.CharacterStringType) {
      return this.visit(ctx.CharacterStringType);
    }
    if (ctx.ChoiceType) {
      return this.visit(ctx.ChoiceType);
    }
    return null;
  }
 
  PrefixedType(ctx: PrefixedTypeCstNode) {
    return this.visit(ctx.TaggedType);
  }
 
  TaggedType(ctx: TaggedTypeCstNode) {
    const tag = this.visit(ctx.Tag) as ASN1Tag;
    const type = this.visit(ctx.Type) as ASN1Type;
    const isImplicit = ctx.IMPLICIT ? true : false;
    return new ASN1TaggedType(tag, type, isImplicit);
  }
 
  Tag(ctx: TagCstNode) {
    const tagClass = this.visit(ctx.Class) as TagClass;
    const tagCode = this.visit(ctx.ClassNumber) as number;
    return new ASN1Tag(tagClass, tagCode);
  }
 
  Class(ctx: ClassCstNode) {
    if (ctx.APPLICATION) {
      return TagClass.APPLICATION;
    }
    return TagClass.CONTEXT_SPECIFIC;
  }
 
  ClassNumber(ctx: ClassNumberCstNode) {
    return +ctx.Number[0].image;
  }
 
  DefinedType(ctx: DefinedTypeCstNode) {
    const name = ctx.TypeReference[0].image;
    return new ASN1DefinedType(name);
  }
 
  SequenceType(ctx: SequenceTypeCstNode) {
    const sequence = new ASN1Sequence();
    this.visit(ctx.ComponentTypeLists, sequence);
    return sequence;
  }
 
  ComponentType(ctx: ComponentTypeCstNode, sequence: ASN1Sequence) {
    const namedType = this.visit(ctx.NamedType) as ASN1NamedType;
    if (ctx.OPTIONAL) {
      namedType.optional = true;
    }
    sequence.addComponent(namedType);
  }
 
  ChoiceType(ctx: ChoiceTypeCstNode) {
    const choice = new ASN1ChoiceType();
    this.visit(ctx.AlternativeTypeLists, choice);
    return choice;
  }
 
  AlternativeType(ctx: AlternativeTypeCstNode, choice: ASN1ChoiceType) {
    const namedType = this.visit(ctx.NamedType) as ASN1NamedType;
    choice.addAlternative(namedType);
  }
 
  NamedType(ctx: NamedTypeCstNode) {
    const name = ctx.Identifier[0].image;
    const type = this.visit(ctx.Type);
    const namedType = new ASN1NamedType(name, type);
    return namedType;
  }
 
  CharacterStringType(ctx: CharacterStringTypeCstNode) {
    return this.visit(ctx.RestrictedCharacterStringType);
  }
 
  RestrictedCharacterStringType(ctx: RestrictedCharacterStringTypeCstNode) {
    if (ctx.IA5String) {
      return new ASN1IA5StringType();
    }
    if (ctx.GeneralString) {
      return new ASN1GeneralStringType();
    }
    return new ASN1IA5StringType();
  }
}