-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasn1.ex
executable file
·677 lines (617 loc) · 36.7 KB
/
asn1.ex
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#!/usr/bin/env elixir
defmodule ASN1 do
def print(format, params) do
case :application.get_env(:asn1scg, "save", true) and :application.get_env(:asn1scg, "verbose", false) do
true -> :io.format(format, params)
_ -> []
end
end
def array(name,type,tag,level \\ "") when tag == :sequence or tag == :set do
name1 = bin(normalizeName(name))
type1 = bin(type)
case level do
"" -> []
_ -> print 'array: #{level} : ~ts = [~ts] ~p ~n', [name1, type1, tag]
end
setEnv(name1, "[#{type1}]")
setEnv({:array, name1}, {tag, type1})
name1
end
def fieldName({:contentType, {:Externaltypereference,_,_mod, name}}), do: normalizeName("#{name}")
def fieldName(name), do: normalizeName("#{name}")
def fieldType(name,field,{:ComponentType,_,_,{:type,_,oc,_,[],:no},_opt,_,_}), do: fieldType(name, field, oc)
def fieldType(name,field,{:"SEQUENCE", _, _, _, _}), do: bin(name) <> "_" <> bin(field) <> "_Sequence"
def fieldType(name,field,{:"CHOICE",_}), do: bin(name) <> "_" <> bin(field) <> "_Choice"
def fieldType(name,field,{:"ENUMERATED",_}), do: bin(name) <> "_" <> bin(field) <> "_Enum"
def fieldType(name,field,{:"INTEGER",_}), do: bin(name) <> "_" <> bin(field) <> "_IntEnum"
def fieldType(name,field,{:"SEQUENCE OF", type}) do bin = "[#{sequenceOf(name,field,type)}]" ; array("#{bin}", partArray(bin), :sequence, "pro #{name}.#{field}") end
def fieldType(name,field,{:"SET OF", type}) do bin = "[#{sequenceOf(name,field,type)}]" ; array("#{bin}", partArray(bin), :set, "pro #{name}.#{field}") end
def fieldType(_,_,{:contentType, {:Externaltypereference,_,_,type}}), do: "#{type}"
def fieldType(_,_,{:"BIT STRING", _}), do: "ASN1BitString"
def fieldType(_,_,{:pt, {_,_,_,type}, _}) when is_atom(type), do: "#{type}"
def fieldType(_,_,{:ANY_DEFINED_BY, type}) when is_atom(type), do: "ASN1Any"
def fieldType(_name,_field,{:Externaltypereference,_,_,type}) when type == :OrganizationalUnitNames, do: "#{substituteType(lookup(bin(type)))}"
def fieldType(_name,_field,{:Externaltypereference,_,_,type}), do: "#{substituteType(lookup(bin(type)))}"
def fieldType(_,_,{:ObjectClassFieldType,_,_,[{_,type}],_}), do: "#{type}"
def fieldType(_,_,type) when is_atom(type), do: "#{type}"
def fieldType(name,_,_), do: "#{name}"
def sequenceOf(name,field,type) do
sequenceOf2(name,field,type)
end
def sequenceOf2(name,field,{:type,_,{:Externaltypereference,_,_,type},_,_,_}), do: "#{sequenceOf(name,field,type)}"
def sequenceOf2(name,field,{:type,_,{:"SET OF", type},_,_,_}) do bin = "[#{sequenceOf(name,field,type)}]" ; array("#{bin}", partArray(bin), :set, "arr #{name}.#{field}") end
def sequenceOf2(name,field,{:type,_,{:"SEQUENCE OF", type},_,_,_}) do bin = "[#{sequenceOf(name,field,type)}]" ; array("#{bin}", partArray(bin), :sequence, "arr #{name}.#{field}") end
def sequenceOf2(name,field,{:type,_,{:CHOICE, cases} = sum,_,_,_}) do choice(fieldType(name,field,sum), cases, [], true) ; bin(name) <> "_" <> bin(field) <> "_Choice" end
def sequenceOf2(name,field,{:type,_,{:SEQUENCE, _, _, _, fields} = product,_,_,_}) do sequence(fieldType(name,field,product), fields, [], true) ; bin(name) <> "_" <> bin(field) <> "_Sequence" end
def sequenceOf2(name,field,{:type,_,type,_,_,_}) do "#{sequenceOf(name,field,type)}" end
def sequenceOf2(name,_,{:Externaltypereference, _, _, type}) do :application.get_env(:asn1scg, bin(name), bin(type)) end
def sequenceOf2(_,_,x) when is_tuple(x), do: substituteType("#{bin(:erlang.element(1, x))}")
def sequenceOf2(_,_,x) when is_atom(x), do: substituteType("#{lookup(x)}")
def sequenceOf2(_,_,x) when is_binary(x), do: substituteType("#{lookup(x)}")
def substituteType("TeletexString"), do: "ASN1TeletexString"
def substituteType("UniversalString"), do: "ASN1UniversalString"
def substituteType("IA5String"), do: "ASN1IA5String"
def substituteType("VisibleString"), do: "ASN1UTF8String"
def substituteType("UTF8String"), do: "ASN1UTF8String"
def substituteType("PrintableString"), do: "ASN1PrintableString"
def substituteType("NumericString"), do: "ASN1PrintableString"
def substituteType("BMPString"), do: "ASN1BMPString"
def substituteType("INTEGER"), do: "ArraySlice<UInt8>"
def substituteType("OCTET STRING"), do: "ASN1OctetString"
def substituteType("BIT STRING"), do: "ASN1BitString"
def substituteType("OBJECT IDENTIFIER"), do: "ASN1ObjectIdentifier"
def substituteType("BOOLEAN"), do: "Bool"
def substituteType("pt"), do: "ASN1Any"
def substituteType("ANY"), do: "ASN1Any"
def substituteType("NULL"), do: "ASN1Null"
def substituteType("URI"), do: "ASN1OctetString"
def substituteType(t), do: t
def emitImprint(), do: "// Generated by ASN1.ERP.UNO Compiler, Copyright © 2023—2024 Namdak Tönpa."
def emitArg(name), do: "#{name}: #{name}"
def emitCtorBodyElement(name), do: "self.#{name} = #{name}"
def emitCtorParam(name, type, opt \\ ""), do: "#{name}: #{normalizeName(type)}#{opt}"
def emitCtor(params,fields), do: pad(4) <> "@inlinable init(#{params}) {\n#{fields}\n }\n"
def emitEnumElement(type, field, value), do: pad(4) <> "static let #{field} = #{type}(rawValue: #{value})\n"
def emitIntegerEnumElement(field, value), do: pad(4) <> "public static let #{field} = Self(rawValue: #{value})\n"
def emitOptional(:OPTIONAL, name, body), do: "if let #{name} = self.#{name} { #{body} }"
def emitOptional(_, _, body), do: "#{body}"
def emitSequenceElementOptional(name, type, opt \\ ""), do: "@usableFromInline var #{name}: #{lookup(normalizeName(type))}#{opt}\n"
# Vector Decoder
def emitSequenceDecoderBodyElement(:OPTIONAL, plicit, no, name, type) when plicit == "Implicit", do:
"let #{name}: #{type}? = try DER.optionalImplicitlyTagged(&nodes, tag: ASN1Identifier(tagWithNumber: #{no}, tagClass: .contextSpecific))"
def emitSequenceDecoderBodyElement(:OPTIONAL, plicit, no, name, type) when plicit == "Explicit", do:
"let #{name}: #{type}? = try DER.optionalExplicitlyTagged(&nodes, tagNumber: #{no}, tagClass: .contextSpecific) { node in return try #{type}(derEncoded: node) }"
def emitSequenceDecoderBodyElement(_, plicit, no, name, type) when plicit == "Explicit", do:
"let #{name}: #{type} = try DER.explicitlyTagged(&nodes, tagNumber: #{no}, tagClass: .contextSpecific) { node in return try #{type}(derEncoded: node) }"
def emitSequenceDecoderBodyElement(_, plicit, no, name, type) when plicit == "Implicit", do:
"let #{name}: #{type} = (try DER.optionalImplicitlyTagged(&nodes, tag: ASN1Identifier(tagWithNumber: #{no}, tagClass: .contextSpecific)))!"
def emitSequenceDecoderBodyElement(:OPTIONAL, _, _, name, "ASN1Any"), do:
"let #{name}: ASN1Any? = nodes.next().map { ASN1Any(derEncoded: $0) }"
def emitSequenceDecoderBodyElement(_, _, _, name, "Bool"), do:
"let #{name}: Bool = try DER.decodeDefault(&nodes, defaultValue: false)"
def emitSequenceDecoderBodyElement(optional, _, _, name, type), do:
"let #{name}: #{type}#{opt(optional)} = try #{type}(derEncoded: &nodes)"
def emitSequenceDecoderBodyElementArray(:OPTIONAL, plicit, no, name, type, spec) when plicit == "Explicit" and no != [] and (spec == "set" or spec == "sequence"), do:
"let #{name}: [#{type}]? = try DER.optionalExplicitlyTagged(&nodes, tagNumber: #{no}, tagClass: .contextSpecific) { node in try DER.#{spec}(of: #{type}.self, identifier: .#{spec}, rootNode: node) }"
def emitSequenceDecoderBodyElementArray(_, plicit, no, name, type, spec) when plicit == "Implicit" and no != [] and (spec == "set" or spec == "sequence"), do:
"let #{name}: [#{type}] = try DER.#{spec}(of: #{type}.self, identifier: ASN1Identifier(tagWithNumber: #{no}, tagClass: .contextSpecific), nodes: &nodes)"
def emitSequenceDecoderBodyElementArray(_, _, no, name, type, spec) when no != [] and (spec == "set" or spec == "sequence"), do:
"let #{name}: [#{type}] = try DER.explicitlyTagged(&nodes, tagNumber: #{no}, tagClass: .contextSpecific) { node in try DER.#{spec}(of: #{type}.self, identifier: .#{spec}, rootNode: node) }"
def emitSequenceDecoderBodyElementArray(optional, _, no, name, type, spec) when no == [], do:
"let #{name}: [#{type}]#{opt(optional)} = try DER.#{spec}(of: #{type}.self, identifier: .#{spec}, nodes: &nodes)"
def emitSequenceDecoderBodyElementIntEnum(name, type), do:
"let #{name} = try #{type}(rawValue: Int(derEncoded: &nodes))"
# Vector Encoder
def emitSequenceEncoderBodyElement(_, plicit, no, name, s) when plicit == "Explicit" and no != [] and (s == "set" or s == "sequence"), do:
"try coder.serialize(explicitlyTaggedWithTagNumber: #{no}, tagClass: .contextSpecific) { codec in try codec.serialize#{spec(s)}(#{name}) }"
def emitSequenceEncoderBodyElement(_, plicit, no, name, s) when plicit == "Implicit" and no != [] and (s == "set" or s == "sequence"), do:
"try coder.serialize#{spec(s)}(#{name}, identifier: ASN1Identifier(tagWithNumber: #{no}, tagClass: .contextSpecific))"
def emitSequenceEncoderBodyElement(_, plicit, no, name, _) when no != [] and plicit == "Implicit", do:
"try coder.serializeOptionalImplicitlyTagged(#{name}, withIdentifier: ASN1Identifier(tagWithNumber: #{no}, tagClass: .contextSpecific))"
def emitSequenceEncoderBodyElement(_, plicit, no, name, _) when no != [] and plicit == "Explicit", do:
"try coder.serialize(explicitlyTaggedWithTagNumber: #{no}, tagClass: .contextSpecific) { codec in try codec.serialize(#{name}) }"
def emitSequenceEncoderBodyElement(_, _, no, name, spec) when spec == "sequence" and no == [], do:
"try coder.serializeSequenceOf(#{name})"
def emitSequenceEncoderBodyElement(_, _, no, name, spec) when spec == "set" and no == [], do:
"try coder.serializeSetOf(#{name})"
def emitSequenceEncoderBodyElement(_, _, no, name, _) when no == [], do:
"try coder.serialize(#{name})"
def emitSequenceEncoderBodyElementIntEnum(no, name) when no == [], do:
"try coder.serialize(#{name}.rawValue)"
def emitSequenceEncoderBodyElementIntEnum(no, name), do:
"try coder.serialize(#{name}.rawValue, explicitlyTaggedWithTagNumber: #{no}, tagClass: .contextSpecific)"
# Scalar Sum Component
def emitChoiceElement(name, type), do: "case #{name}(#{lookup(bin(normalizeName(type)))})\n"
def emitChoiceEncoderBodyElement(w, no, name, _type, spec) when no == [], do:
pad(w) <> "case .#{name}(let #{name}): try coder.serialize#{spec}(#{name})"
def emitChoiceEncoderBodyElement(w, no, name, _type, spec), do:
pad(w) <> "case .#{name}(let #{name}):\n" <>
pad(w+4) <> "try coder.appendConstructedNode(\n" <>
pad(w+4) <> "identifier: ASN1Identifier(tagWithNumber: #{tagNo(no)}, tagClass: #{tagClass(no)}),\n" <>
pad(w+4) <> "{ coder in try coder.serialize#{spec}(#{name}) })"
def emitChoiceDecoderBodyElement(w, no, name, type, _spec) when no == [], do:
pad(w) <> "case #{type}.defaultIdentifier:\n" <>
pad(w+4) <> "self = .#{name}(try #{type}(derEncoded: rootNode, withIdentifier: rootNode.identifier))"
def emitChoiceDecoderBodyElement(w, no, name, type, _spec), do:
pad(w) <> "case ASN1Identifier(tagWithNumber: #{tagNo(no)}, tagClass: #{tagClass(no)}):\n" <>
pad(w+4) <> "self = .#{name}(try #{type}(derEncoded: rootNode, withIdentifier: rootNode.identifier))"
# Vector Sum Component
def emitChoiceDecoderBodyElementForArray(w, no, name, type, spec) when no == [], do:
pad(w) <> "case ASN1Identifier.#{spec}:\n" <>
pad(w+4) <> "self = .#{name}(try DER.#{spec}(of: #{type}.self, identifier: .#{spec}, rootNode: rootNode))"
def emitChoiceDecoderBodyElementForArray(w, no, name, type, spec) when spec == "", do:
pad(w) <> "case ASN1Identifier(tagWithNumber: #{tagNo(no)}, tagClass: #{tagClass(no)}):\n" <>
pad(w+4) <> "self = .#{name}(try DER.#{spec}(of: #{type}.self, identifier: .#{spec}, nodes: &nodes))"
def emitChoiceDecoderBodyElementForArray(w, no, name, type, spec), do:
pad(w) <> "case ASN1Identifier(tagWithNumber: #{tagNo(no)}, tagClass: #{tagClass(no)}):\n" <>
pad(w+4) <> "self = .#{name}(try DER.#{spec}(of: #{type}.self, identifier: .#{spec}, rootNode: rootNode))"
def emitSequenceDefinition(name,fields,ctor,decoder,encoder), do:
"""
#{emitImprint()}
import SwiftASN1\nimport Foundation\n
@usableFromInline struct #{name}: DERImplicitlyTaggable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .sequence }\n#{fields}#{ctor}#{decoder}#{encoder}}
"""
def emitSetDefinition(name,fields,ctor,decoder,encoder), do:
"""
#{emitImprint()}
import SwiftASN1\nimport Foundation\n
@usableFromInline struct #{name}: DERImplicitlyTaggable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .set }\n#{fields}#{ctor}#{decoder}#{encoder}}
"""
def emitChoiceDefinition(name,cases,decoder,encoder), do:
"""
#{emitImprint()}
import SwiftASN1\nimport Foundation\n
@usableFromInline indirect enum #{name}: DERImplicitlyTaggable, DERParseable, DERSerializable, Hashable, Sendable {
@inlinable static var defaultIdentifier: ASN1Identifier { .enumerated }\n#{cases}#{decoder}#{encoder}
}
"""
def emitEnumerationDefinition(name,cases), do:
"""
#{emitImprint()}
import SwiftASN1\nimport Foundation\n
public struct #{name}: DERImplicitlyTaggable, Hashable, Sendable, RawRepresentable {
public static var defaultIdentifier: ASN1Identifier { .enumerated }
public var rawValue: Int
public init(rawValue: Int) { self.rawValue = rawValue }
public init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws {
self.rawValue = try .init(derEncoded: rootNode, withIdentifier: identifier)
}
public func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws {
try self.rawValue.serialize(into: &coder, withIdentifier: identifier)
}
#{cases}}
"""
def emitIntegerEnumDefinition(name,cases), do:
"""
#{emitImprint()}
public struct #{name} : Hashable, Sendable, Comparable {
@usableFromInline var rawValue: Int
@inlinable public static func < (lhs: #{name}, rhs: #{name}) -> Bool { lhs.rawValue < rhs.rawValue }
@inlinable init(rawValue: Int) { self.rawValue = rawValue }
#{cases}}
"""
def emitChoiceDecoder(cases), do:
"""
@inlinable init(derEncoded rootNode: ASN1Node, withIdentifier: ASN1Identifier) throws {
switch rootNode.identifier {\n#{cases}
default: throw ASN1Error.unexpectedFieldType(rootNode.identifier)
}
}
"""
def emitChoiceEncoder(cases), do:
"""
@inlinable func serialize(into coder: inout DER.Serializer, withIdentifier: ASN1Identifier) throws {
switch self {\n#{cases}
}
}
"""
def emitSetDecoder(fields, name, args), do:
"""
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.set(root, identifier: identifier) { nodes in\n#{fields}
return #{normalizeName(name)}(#{args})
}
}
"""
def emitSequenceDecoder(fields, name, args), do:
"""
@inlinable init(derEncoded root: ASN1Node,
withIdentifier identifier: ASN1Identifier) throws {
self = try DER.sequence(root, identifier: identifier) { nodes in\n#{fields}
return #{normalizeName(name)}(#{args})
}
}
"""
def emitSequenceEncoder(fields), do:
"""
@inlinable func serialize(into coder: inout DER.Serializer,
withIdentifier identifier: ASN1Identifier) throws {
try coder.appendConstructedNode(identifier: identifier) { coder in\n#{fields}
}
}
"""
def emitIntegerEnums(cases) when is_list(cases) do
Enum.join(:lists.map(fn
{:NamedNumber, fieldName, fieldValue} ->
trace(1)
emitIntegerEnumElement(fieldName(fieldName), fieldValue)
_ -> ""
end, cases), "")
end
def emitEnums(name, cases) when is_list(cases) do
Enum.join(:lists.map(fn
{:NamedNumber, fieldName, fieldValue} ->
trace(2)
emitEnumElement(name, fieldName(fieldName), fieldValue)
_ -> ""
end, cases), "")
end
def emitCases(name, w, cases, modname) when is_list(cases) do
Enum.join(:lists.map(fn
{:ComponentType,_,fieldName,{:type,_,fieldType,_elementSet,[],:no},_optional,_,_} ->
trace(3)
field = fieldType(name, fieldName, fieldType)
case fieldType do
{:SEQUENCE, _, _, _, fields} ->
sequence(fieldType(name,fieldName,fieldType), fields, modname, true)
{:CHOICE, cases} ->
choice(fieldType(name,fieldName,fieldType), cases, modname, true)
{:INTEGER, cases} ->
integerEnum(fieldType(name,fieldName,fieldType), cases, modname, true)
{:ENUMERATED, cases} ->
enumeration(fieldType(name,fieldName,fieldType), cases, modname, true)
_ ->
:skip
end
pad(w) <> emitChoiceElement(fieldName(fieldName), substituteType(lookup(field)))
_ -> ""
end, cases), "")
end
def emitFields(name, w, fields, modname) when is_list(fields) do
Enum.join(:lists.map(fn
{:"COMPONENTS OF", {:type, _, {_,_,_,n}, _, _, :no}} ->
trace(4)
inclusion = :application.get_env(:asn1scg, {:type,n}, [])
emitFields(n, w, inclusion, modname)
{:ComponentType,_,fieldName,{:type,_,fieldType,_elementSet,[],:no},optional,_,_} ->
trace(5)
field = fieldType(name, fieldName, fieldType)
case fieldType do
{:SEQUENCE, _, _, _, fields} ->
sequence(fieldType(name,fieldName,fieldType), fields, modname, true)
{:CHOICE, cases} ->
zip = :lists.zip(cases, :lists.seq(0, :erlang.length(cases)-1))
casesTagged = :lists.flatten(:lists.map(fn {line,newNo} ->
case line do
{:'ComponentType',l,name,{:type,tag,ref,x,y,no},opt,z,w} ->
tagNew = case tag do [] -> [{:tag,:APPLICATION,newNo,:'IMPLICIT',32}] ; x -> x end
:io.format 'line: ~p~n', [{line,tagNew}]
{:'ComponentType',l,name,{:type,tagNew,ref,x,y,no},opt,z,w}
_ -> []
end
end, zip))
:io.format 'casesTagged: ~p~n', [casesTagged]
choice(fieldType(name,fieldName,fieldType), casesTagged, modname, true)
{:INTEGER, cases} ->
integerEnum(fieldType(name,fieldName,fieldType), cases, modname, true)
{:ENUMERATED, cases} ->
enumeration(fieldType(name,fieldName,fieldType), cases, modname, true)
_ ->
:skip
end
print 'field: ~ts.~ts : ~ts ~n', [name,fieldName(fieldName), substituteType(lookup(field))]
pad(w) <>
emitSequenceElementOptional(fieldName(fieldName), substituteType(lookup(field)), opt(optional))
_ -> ""
end, fields), "")
end
def emitCtorBody(fields), do:
Enum.join(:lists.map(fn
{:"COMPONENTS OF", {:type, _, {_,_,_,n}, _, _, :no}} ->
trace(6)
inclusion = :application.get_env(:asn1scg, {:type,n}, [])
emitCtorBody(inclusion)
{:ComponentType,_,fieldName,{:type,_,_type,_elementSet,[],:no},_optional,_,_} ->
trace(7)
pad(8) <> emitCtorBodyElement(fieldName(fieldName))
_ -> ""
end, fields), "\n")
def emitChoiceEncoderBody(name,cases), do:
Enum.join(:lists.map(fn
{:ComponentType,_,fieldName,{:type,tag,{:"SEQUENCE OF", {_,_,type,_,_,_}},_,_,_},_,_,_} ->
trace(8)
emitChoiceEncoderBodyElement(12, tag, fieldName(fieldName), type, "SequenceOf")
{:ComponentType,_,fieldName,{:type,tag,{:"SET OF", {_,_,type,_,_,_}},_,_,_},_,_,_} ->
trace(9)
emitChoiceEncoderBodyElement(12, tag, fieldName(fieldName), type, "SetOf")
{:ComponentType,_,fieldName,{:type,tag,type,_elementSet,[],:no},_optional,_,_} ->
trace(10)
case {part(lookup(fieldType(name,fieldName,type)),0,1),
:application.get_env(:asn1scg, {:array, lookup(fieldType(name,fieldName(fieldName),type))}, [])} do
{"[", {:set, _}} -> emitChoiceEncoderBodyElement(12, tag, fieldName(fieldName), type, "SetOf")
{"[", {:sequence, _}} -> emitChoiceEncoderBodyElement(12, tag, fieldName(fieldName), type, "SequenceOf")
_ -> emitChoiceEncoderBodyElement(12, tag, fieldName(fieldName), type, "")
end
_ -> ""
end, cases), "\n")
def emitChoiceDecoderBody(name,cases), do:
Enum.join(:lists.map(fn
{:ComponentType,_,fieldName,{:type,tag,{:"SEQUENCE OF", {_,_,type,_,_,_}},_,_,_},_,_,_} ->
trace(11)
emitChoiceDecoderBodyElementForArray(12, tag, fieldName(fieldName),
substituteType(lookup(fieldType(name, fieldName(fieldName), type))), "sequence")
{:ComponentType,_,fieldName,{:type,tag,{:"SET OF", {_,_,type,_,_,_}},_,_,_},_,_,_} ->
trace(12)
emitChoiceDecoderBodyElementForArray(12, tag, fieldName(fieldName),
substituteType(lookup(fieldType(name, fieldName(fieldName), type))), "set")
{:ComponentType,_,fieldName,{:type,tag,type,_elementSet,[],:no},_optional,_x,_y} ->
trace(13)
case {part(lookup(fieldType(name,fieldName,type)),0,1),
:application.get_env(:asn1scg, {:array, lookup(fieldType(name,fieldName(fieldName),type))}, [])} do
{"[", {:set, inner}} -> emitChoiceDecoderBodyElementForArray(12, tag, fieldName(fieldName), inner, "set")
{"[", {:sequence, inner}} -> emitChoiceDecoderBodyElementForArray(12, tag, fieldName(fieldName), inner, "sequence")
_ -> emitChoiceDecoderBodyElement(12, tag, fieldName(fieldName),
substituteType(lookup(fieldType(name, fieldName(fieldName), type))), "")
end
_ -> ""
end, cases), "\n")
def emitSequenceDecoderBody(name,fields), do:
Enum.join(:lists.map(fn
{:"COMPONENTS OF", {:type, _, {_,_,_,n}, _, _, :no}} ->
trace(14)
inclusion = :application.get_env(:asn1scg, {:type,n}, [])
emitSequenceDecoderBody(n, inclusion)
{:ComponentType,_,fieldName,{:type,tag,type,_elementSet,[],:no},optional,_,_} ->
look = substituteType(normalizeName(lookup(fieldType(name,fieldName,type))))
res = case type do
{:"SEQUENCE OF", {:type, _, inner, _, _, _}} ->
trace(15)
emitSequenceDecoderBodyElementArray(optional, plicit(tag), tagNo(tag), fieldName(fieldName), substituteType(lookup(fieldType(name,fieldName,inner))), "sequence")
{:"SET OF", {:type, _, inner, _, _, _}} ->
trace(16)
emitSequenceDecoderBodyElementArray(optional, plicit(tag), tagNo(tag), fieldName(fieldName), substituteType(lookup(fieldType(name,fieldName,inner))), "set")
{:"INTEGER", _} ->
trace(17)
emitSequenceDecoderBodyElementIntEnum(fieldName(fieldName), substituteType(lookup(fieldType(name,fieldName(fieldName),type))))
{:Externaltypereference,_,_,inner} ->
trace(18)
case :application.get_env(:asn1scg, {:array, bin(inner)}, []) do
{:sequence, _} -> emitSequenceDecoderBodyElementArray(optional, plicit(tag), tagNo(tag), fieldName(fieldName), substituteType(part(look,1,:erlang.size(look)-2)), "sequence")
{:set, _} -> emitSequenceDecoderBodyElementArray(optional, plicit(tag), tagNo(tag), fieldName(fieldName), substituteType(part(look,1,:erlang.size(look)-2)), "set")
_ -> emitSequenceDecoderBodyElement(optional, plicit(tag), tagNo(tag), fieldName(fieldName), look)
end
_ -> trace(19)
emitSequenceDecoderBodyElement(optional, plicit(tag), tagNo(tag), fieldName(fieldName), look)
end
pad(12) <> res
_ -> ""
end, fields), "\n")
def emitSequenceEncoderBody(_name, fields), do:
Enum.join(:lists.map(fn
{:"COMPONENTS OF", {:type, _, {_,_,_,name}, _, _, :no}} ->
trace(20)
inclusion = :application.get_env(:asn1scg, {:type,name}, [])
emitSequenceEncoderBody(name, inclusion)
{:ComponentType,_,fieldName,{:type,tag,type,_elementSet,[],:no},optional,_,_} ->
res = case type do
{:"SEQUENCE OF", {:type, _, _innerType, _, _, _}} ->
trace(21)
emitSequenceEncoderBodyElement(optional, plicit(tag), tagNo(tag), fieldName(fieldName), "sequence")
{:"SET OF", {:type, _, _innerType, _, _, _}} ->
trace(22)
emitSequenceEncoderBodyElement(optional, plicit(tag), tagNo(tag), fieldName(fieldName), "set")
{:"INTEGER", _} ->
trace(23)
emitSequenceEncoderBodyElementIntEnum(tagNo(tag), fieldName(fieldName))
{:Externaltypereference,_,_,inner} ->
trace(24)
case :application.get_env(:asn1scg, {:array, bin(inner)}, []) do
{:sequence, _} -> emitSequenceEncoderBodyElement(optional, plicit(tag), tagNo(tag), fieldName(fieldName), "sequence")
{:set, _} -> emitSequenceEncoderBodyElement(optional, plicit(tag), tagNo(tag), fieldName(fieldName), "set")
_ -> emitSequenceEncoderBodyElement(optional, plicit(tag), tagNo(tag), fieldName(fieldName), "")
end
_ -> trace(25)
emitSequenceEncoderBodyElement(optional, plicit(tag), tagNo(tag), fieldName(fieldName), "")
end
pad(12) <> emitOptional(optional, fieldName(fieldName), res)
_ -> ""
end, fields), "\n")
def emitParams(name,fields) when is_list(fields) do
Enum.join(:lists.map(fn
{:"COMPONENTS OF", {:type, _, {_,_,_,n}, _, _, :no}} ->
trace(26)
inclusion = :application.get_env(:asn1scg, {:type,n}, [])
emitParams(n,inclusion)
{:ComponentType,_,fieldName,{:type,_,type,_elementSet,[],:no},optional,_,_} ->
trace(27)
emitCtorParam(fieldName(fieldName), substituteType(lookup(fieldType(name,fieldName,type))), opt(optional))
_ -> ""
end, fields), ", ")
end
def emitArgs(fields) when is_list(fields) do
Enum.join(:lists.map(fn
{:"COMPONENTS OF", {:type, _, {_,_,_,n}, _, _, :no}} ->
trace(28)
inclusion = :application.get_env(:asn1scg, {:type,n}, [])
emitArgs(inclusion)
{:ComponentType,_,fieldName,{:type,_,_type,_elementSet,[],:no},_optional,_,_} ->
trace(29)
emitArg(fieldName(fieldName))
_ -> ""
end, fields), ", ")
end
def dump() do
:lists.foldl(fn {{:array,x},{tag,y}}, _ -> print 'env array: ~ts = [~ts] ~tp ~n', [x,y,tag]
{x,y}, _ when is_binary(x) -> print 'env alias: ~ts = ~ts ~n', [x,y]
{{:type,x},_}, _ -> print 'env type: ~ts = ... ~n', [x]
_, _ -> :ok
end, [], :lists.sort(:application.get_all_env(:asn1scg)))
end
def compile() do
{:ok, f} = :file.list_dir inputDir()
:io.format 'F: ~p~n', [f]
files = :lists.filter(fn x -> [_,y] = :string.tokens(x, '.') ; y == 'asn1' end, f)
setEnv(:save, false) ; :lists.map(fn file -> compile(false, inputDir() <> :erlang.list_to_binary(file)) end, files)
setEnv(:save, false) ; :lists.map(fn file -> compile(false, inputDir() <> :erlang.list_to_binary(file)) end, files)
setEnv(:save, true) ; :lists.map(fn file -> compile(true, inputDir() <> :erlang.list_to_binary(file)) end, files)
print 'inputDir: ~ts~n', [inputDir()]
print 'outputDir: ~ts~n', [outputDir()]
print 'coverage: ~tp~n', [coverage()]
dump()
:ok
end
def coverage() do
:lists.map(fn x -> :application.get_env(:asn1scg,
{:trace, x}, []) end,:lists.seq(1,30)) end
def compile(save, file) do
tokens = :asn1ct_tok.file file
{:ok, mod} = :asn1ct_parser2.parse file, tokens
{:module, pos, modname, defid, tagdefault, exports, imports, _, declarations} = mod
:lists.map(fn
{:typedef, _, pos, name, type} -> compileType(pos, name, type, modname, save)
{:ptypedef, _, pos, name, args, type} -> compilePType(pos, name, args, type)
{:classdef, _, pos, name, mod, type} -> compileClass(pos, name, mod, type)
{:valuedef, _, pos, name, type, value, mod} -> compileValue(pos, name, type, value, mod)
end, declarations)
compileModule(pos, modname, defid, tagdefault, exports, imports)
end
def compileType(_, name, typeDefinition, modname, save \\ true) do
res = case typeDefinition do
{:type, _, {:"INTEGER", cases}, _, [], :no} -> setEnv(name, "Int") ; integerEnum(name, cases, modname, save)
{:type, _, {:"ENUMERATED", cases}, _, [], :no} -> enumeration(name, cases, modname, save)
{:type, _, {:"CHOICE", cases}, _, [], :no} -> choice(name, cases, modname, save)
{:type, _, {:"SEQUENCE", _, _, _, fields}, _, _, :no} -> sequence(name, fields, modname, save)
{:type, _, {:"SET", _, _, _, fields}, _, _, :no} -> set(name, fields, modname, save)
{:type, _, {:"SEQUENCE OF", {:type, _, type, _, _, :no}}, _, _, _} when is_atom(type) -> array(name,substituteType(lookup(bin(type))),:sequence,"top")
{:type, _, {:"SEQUENCE OF", {:type, _, {_, _, _, type}, _, _, _}}, _, _, _} -> array(name,substituteType(lookup(bin(type))),:sequence,"top")
{:type, _, {:"SET OF", {:type, _, type, _, _, :no}}, _, _, _} when is_atom(type) -> array(name,substituteType(lookup(bin(type))),:set,"top")
{:type, _, {:"SET OF", {:type, _, {_, _, _, type}, _, _, _}}, _, _, _} -> array(name,substituteType(lookup(bin(type))),:set,"top")
{:type, _, {:"BIT STRING",_}, _, [], :no} -> setEnv(name, "BIT STRING")
{:type, _, :'BIT STRING', _, [], :no} -> setEnv(name, "BIT STRING")
{:type, _, :'INTEGER', _set, [], :no} -> setEnv(name, "INTEGER")
{:type, _, :'NULL', _set, [], :no} -> setEnv(name, "NULL")
{:type, _, :'ANY', _set, [], :no} -> setEnv(name, "ANY")
{:type, _, :'PrintableString', _set, [], :no} -> setEnv(name, "PrintableString")
{:type, _, :'NumericString', _set, [], :no} -> setEnv(name, "PrintableString")
{:type, _, :'IA5String', _set, [], :no} -> setEnv(name, "IA5String")
{:type, _, :'TeletexString', _set, [], :no} -> setEnv(name, "TeletexString")
{:type, _, :'UniversalString', _set, [], :no} -> setEnv(name, "UniversalString")
{:type, _, :'OBJECT IDENTIFIER', _, _, :no} -> setEnv(name, "OBJECT IDENTIFIER")
{:type, _, :'OCTET STRING', [], [], :no} -> setEnv(name, "OCTET STRING")
{:type, _, {:Externaltypereference, _, _, ext}, _set, [], _} -> setEnv(name, ext)
{:type, _, {:pt, _, _}, _, [], _} -> :skip
{:type, _, {:ObjectClassFieldType, _, _, _, _fields}, _, _, :no} -> :skip
{:Object, _, _val} -> :skip
{:Object, _, _, _} -> :skip
{:ObjectSet, _, _, _, _} -> :skip
_ -> :skip
end
case res do
:skip -> print 'Unhandled type definition ~p: ~p~n', [name, typeDefinition]
_ -> :skip
end
end
def compileValue(_pos, name, type, value, _mod), do: (print 'Unhandled value definition ~p : ~p = ~p ~n', [name, type, value] ; [])
def compileClass(_pos, name, _mod, type), do: (print 'Unhandled class definition ~p : ~p~n', [name, type] ; [])
def compilePType(_pos, name, args, type), do: (print 'Unhandled PType definition ~p : ~p(~p)~n', [name, type, args] ; [])
def compileModule(_pos, _name, _defid, _tagdefault, _exports, _imports), do: []
def sequence(name, fields, modname, saveFlag) do
:application.set_env(:asn1scg, {:type,name}, fields)
save(saveFlag, modname, name, emitSequenceDefinition(normalizeName(name),
emitFields(name, 4, fields, modname), emitCtor(emitParams(name,fields), emitCtorBody(fields)),
emitSequenceDecoder(emitSequenceDecoderBody(name, fields), name, emitArgs(fields)),
emitSequenceEncoder(emitSequenceEncoderBody(name, fields))))
end
def set(name, fields, modname, saveFlag) do
:application.set_env(:asn1scg, {:type,name}, fields)
save(saveFlag, modname, name, emitSetDefinition(normalizeName(name),
emitFields(name, 4, fields, modname), emitCtor(emitParams(name,fields), emitCtorBody(fields)),
emitSetDecoder(emitSequenceDecoderBody(name, fields), name, emitArgs(fields)),
emitSequenceEncoder(emitSequenceEncoderBody(name, fields))))
end
def choice(name, cases, modname, saveFlag) do
save(saveFlag, modname, name, emitChoiceDefinition(normalizeName(name),
emitCases(name, 4, cases, modname),
emitChoiceDecoder(emitChoiceDecoderBody(name,cases)),
emitChoiceEncoder(emitChoiceEncoderBody(name,cases))))
end
def enumeration(name, cases, modname, saveFlag) do
save(saveFlag, modname, bin(name),
emitEnumerationDefinition(normalizeName(name),
emitEnums(name, cases)))
end
def integerEnum(name, cases, modname, saveFlag) do
save(saveFlag, modname, name,
emitIntegerEnumDefinition(normalizeName(name),
emitIntegerEnums(cases)))
end
def inputDir(), do: :application.get_env(:asn1scg, "input", "priv/apple/")
def outputDir(), do: :application.get_env(:asn1scg, "output", "Sources/ASN1SCG/Suite/")
def exceptions(), do: :application.get_env(:asn1scg, "exceptions", ["Name"])
def save(true, _, name, res) do
dir = outputDir()
:filelib.ensure_dir(dir)
norm = normalizeName(bin(name))
fileName = dir <> norm <> ".swift"
verbose = getEnv(:verbose, false)
case :lists.member(norm,exceptions()) do
true -> print 'skipping: ~ts.swift~n', [fileName] ; setEnv(:verbose, verbose)
false -> :ok = :file.write_file(fileName,res) ; setEnv(:verbose, true)
print 'compiled: ~ts.swift~n', [fileName] ; setEnv(:verbose, verbose) end
end
def save(_, _, _, _), do: []
def lookup(name) do
b = bin(name)
case :application.get_env(:asn1scg, b, b) do
a when a == b -> bin(a)
x -> lookup(x)
end
end
def plicit([]), do: ""
def plicit([{:tag,:CONTEXT,_,{:default,:IMPLICIT},_}]), do: "Implicit"
def plicit([{:tag,:CONTEXT,_,{:default,:EXPLICIT},_}]), do: "Explicit"
def plicit([{:tag,:CONTEXT,_,:IMPLICIT,_}]), do: "Implicit"
def plicit([{:tag,:CONTEXT,_,:EXPLICIT,_}]), do: "Explicit"
def plicit(_), do: ""
def opt(:OPTIONAL), do: "?"
def opt(_), do: ""
def spec("sequence"), do: "SequenceOf"
def spec("set"), do: "SetOf"
def spec(_), do: ""
def trace(x), do: setEnv({:trace, x}, x)
def normalizeName(name), do: Enum.join(String.split("#{name}", "-"), "_")
def setEnv(x,y), do: :application.set_env(:asn1scg, bin(x), y)
def getEnv(x,y), do: :application.get_env(:asn1scg, bin(x), y)
def bin(x) when is_atom(x), do: :erlang.atom_to_binary x
def bin(x) when is_list(x), do: :erlang.list_to_binary x
def bin(x), do: x
def tagNo([]), do: []
def tagNo([{:tag,_,nox,_,_}]) do nox end
def tagClass([]), do: []
def tagClass([{:tag,:CONTEXT,_,_,_}]), do: ".contextSpecific" # https://github.com/erlang/otp/blob/master/lib/asn1/src/asn1ct_parser2.erl#L2011
def tagClass([{:tag,:APPLICATION,_,_,_}]), do: ".application"
def tagClass([{:tag,:PRIVATE,_,_,_}]), do: ".private"
def tagClass([{:tag,:UNIVERSAL,_,_,_}]), do: ".universal"
def pad(x), do: String.duplicate(" ", x)
def partArray(bin), do: part(bin, 1, :erlang.size(bin) - 2)
def part(a, x, y) do
case :erlang.size(a) > y - x do
true -> :binary.part(a, x, y)
_ -> ""
end
end
end
case System.argv() do
["compile"] -> ASN1.compile
["compile","-v"] -> ASN1.setEnv(:verbose, true) ; ASN1.compile
["compile",i] -> ASN1.setEnv(:input, i <> "/") ; ASN1.compile
["compile","-v",i] -> ASN1.setEnv(:input, i <> "/") ; ASN1.setEnv(:verbose, true) ; ASN1.compile
["compile",i,o] -> ASN1.setEnv(:input, i <> "/") ; ASN1.setEnv(:output, o <> "/") ; ASN1.compile
["compile","-v",i,o] -> ASN1.setEnv(:input, i <> "/") ; ASN1.setEnv(:output, o <> "/") ; ASN1.setEnv(:verbose, true) ; ASN1.compile
_ -> :io.format('Copyright © 1994—2024 Namdak Tönpa.~n')
:io.format('ISO 8824 ITU/IETF X.680-690 ERP/1 ASN.1 DER Compiler, version 30.10.7.~n')
:io.format('Usage: ./asn1.ex help | compile [-v] [input [output]]~n')
end