Module: RASN2::ValueNotation

Defined in:
lib/rasn2/value_notation.rb

Overview

Support for ASN.1 value notation (text representation of ASN.1 data instances).

ASN.1 value notation allows defining values for ASN.1 types in a human-readable text format. For example:

myPerson PersonnelRecord ::= {
  name    "John Doe",
  title   "Engineer",
  age     30,
  employed TRUE
}

This module provides methods to parse such text into Model instances and to generate such text from Model instances.

Examples:

Parse a value notation string

models = RASN2::SchemaParser.parse_file('schema.asn')
record = RASN2::ValueNotation.parse(
  File.read('instance.asn'),
  models: models
)
record[:name].value  # => "John Doe"

Generate value notation from a model

record = PersonnelRecord.new(name: 'John Doe', title: 'Engineer', age: 30, employed: true)
text = RASN2::ValueNotation.emit(record, name: 'myPerson', type_name: 'PersonnelRecord')

Author:

  • rickmark

Since:

  • 0.17.0

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.emit(model, name: nil, type_name: nil) ⇒ String

Emit ASN.1 value notation text from a model instance.

Parameters:

  • model (Model)

    the model instance to emit

  • name (String) (defaults to: nil)

    the value name (e.g. 'myPerson')

  • type_name (String) (defaults to: nil)

    the type name (e.g. 'PersonnelRecord')

  • indent (Integer)

    indentation level (number of spaces per level)

Returns:

  • (String)

    value notation text

Since:

  • 0.17.0



76
77
78
79
80
81
# File 'lib/rasn2/value_notation.rb', line 76

def emit(model, name: nil, type_name: nil)
  name ||= model.respond_to?(:value_name) ? model.value_name : 'value'
  type_name ||= model.respond_to?(:type_name) ? model.type_name : model.class.type
  body = emit_constructed(model, indent_level: 1)
  "#{name} #{type_name} ::= {\n#{body}\n}\n"
end

.parse(text, models:) ⇒ Model

Parse an ASN.1 value notation string and populate a model instance.

Parameters:

  • text (String)

    the value notation text

  • models (Hash{String => Class})

    hash mapping type names to Model subclasses

Returns:

  • (Model)

    populated model instance

Raises:

  • (Error)

    if the text cannot be parsed

Since:

  • 0.17.0



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rasn2/value_notation.rb', line 42

def parse(text, models:)
  text = text.strip
  match = text.match(/\A(\w+)\s+(\w+)\s*::=\s*(.*)\z/m)
  raise Error, 'Invalid value notation format' unless match

  value_name = match[1]
  type_name = match[2]
  body = match[3].strip

  model_class = models[type_name]
  raise Error, "Unknown type: #{type_name}" unless model_class

  values = parse_constructed_value(body)
  instance = model_class.new(values)
  instance.define_singleton_method(:value_name) { value_name }
  instance.define_singleton_method(:type_name) { type_name }
  instance
end

.parse_file(filename, models:) ⇒ Model

Parse an ASN.1 value notation file.

Parameters:

  • filename (String)

    path to the value notation file

  • models (Hash{String => Class})

    hash mapping type names to Model subclasses

Returns:

  • (Model)

    populated model instance

Raises:

  • (Error)

    if the file cannot be parsed

Since:

  • 0.17.0



66
67
68
# File 'lib/rasn2/value_notation.rb', line 66

def parse_file(filename, models:)
  parse(File.read(filename), models: models)
end