maxime-esa / asn1scc Goto Github PK
View Code? Open in Web Editor NEWASN1SCC: An open source ASN.1 compiler for embedded systems
Home Page: https://www.thanassis.space/asn1.html
License: Other
ASN1SCC: An open source ASN.1 compiler for embedded systems
Home Page: https://www.thanassis.space/asn1.html
License: Other
Given following ASN.1:
MyModel DEFINITIONS ::= BEGIN
MySequenceOf ::= SEQUENCE (SIZE(1)) OF INTEGER
MySequence ::= SEQUENCE { i INTEGER }
mySequenceOfValue MySequenceOf ::= { 1 }
mySequence MySequence ::= { i 1 }
END
Below is relevant part of AST XML file obtained by calling asn1.exe --xml-ast tree.xml MyModel.asn1
:
<ValueAssignments>
<ValueAssignment Name="mySequenceOfValue" Line="6" CharPositionInLine="0">
<Asn1Type id="MyModel.mySequenceOfValue" Line="6" CharPositionInLine="18" ParameterizedTypeInstance="false">
<REFERENCE_TYPE Module="MyModel" TypeAssignment="MySequenceOf">
<Asn1Type id="MyModel.mySequenceOfValue" Line="3" CharPositionInLine="17" ParameterizedTypeInstance="false" tasInfoModule="MyModel" tasInfoName="MySequenceOf">
<SEQUENCE_OF acnMaxSizeInBits="72" acnMinSizeInBits="72" uperMaxSizeInBits="72" uperMinSizeInBits="8">
<Constraints>
<SIZE>
<IntegerValue>1</IntegerValue>
</SIZE>
</Constraints>
<WithComponentConstraints />
<Asn1Type id="MyModel.mySequenceOfValue.#" Line="3" CharPositionInLine="39" ParameterizedTypeInstance="false">
<INTEGER acnMaxSizeInBits="72" acnMinSizeInBits="8" uperMaxSizeInBits="72" uperMinSizeInBits="8">
<Constraints />
<WithComponentConstraints />
</INTEGER>
</Asn1Type>
</SEQUENCE_OF>
</Asn1Type>
</REFERENCE_TYPE>
</Asn1Type>
<SequenceOfValue>
<IntegerValue>1</IntegerValue>
</SequenceOfValue>
</ValueAssignment>
<ValueAssignment Name="mySequence" Line="7" CharPositionInLine="0">
<Asn1Type id="MyModel.mySequence" Line="7" CharPositionInLine="11" ParameterizedTypeInstance="false">
<REFERENCE_TYPE Module="MyModel" TypeAssignment="MySequence">
<Asn1Type id="MyModel.mySequence" Line="4" CharPositionInLine="15" ParameterizedTypeInstance="false" tasInfoModule="MyModel" tasInfoName="MySequence">
<SEQUENCE acnMaxSizeInBits="72" acnMinSizeInBits="8" uperMaxSizeInBits="72" uperMinSizeInBits="8">
<SEQUENCE_COMPONENT Name="i" Line="4" CharPositionInLine="26" AdaName="i" CName="i">
<Asn1Type id="MyModel.mySequence.i" Line="4" CharPositionInLine="28" ParameterizedTypeInstance="false">
<INTEGER acnMaxSizeInBits="72" acnMinSizeInBits="8" uperMaxSizeInBits="72" uperMinSizeInBits="8">
<Constraints />
<WithComponentConstraints />
</INTEGER>
</Asn1Type>
</SEQUENCE_COMPONENT>
<Constraints />
<WithComponentConstraints />
</SEQUENCE>
</Asn1Type>
</REFERENCE_TYPE>
</Asn1Type>
<SequenceOfValue>
<NamedValue Name="i">
<IntegerValue>1</IntegerValue>
</NamedValue>
</SequenceOfValue>
</ValueAssignment>
</ValueAssignments>
In value definitions instantiated from both SEQUENCE
and SEQUENCE OF
types, tag <SequenceOfValue>
is used, which makes the values indistinguishable while reading XML file.
If a sequence with optional components is fully constrained by a WITH COMPONENTS clause, the autogenerated type nevertheless contains an "exist" field, and absent components are still encoded.
For example, the following ASN.1
Test DEFINITIONS ::=
BEGIN
T-Test ::= SEQUENCE {
field1 INTEGER,
field2 INTEGER,
field3 INTEGER,
field4 INTEGER,
field5 INTEGER
}
T-Test2 ::= SEQUENCE {
field1 INTEGER OPTIONAL,
field2 INTEGER OPTIONAL,
field3 INTEGER OPTIONAL,
field4 INTEGER OPTIONAL,
field5 INTEGER OPTIONAL
}
T-Test3 ::= T-Test2 ( WITH COMPONENTS {
field1 PRESENT,
field2 PRESENT,
field3 ABSENT,
field4 ABSENT,
field5 PRESENT
} )
END
generates these types:
typedef struct {
asn1SccSint field1;
asn1SccSint field2;
asn1SccSint field3;
asn1SccSint field4;
asn1SccSint field5;
} T_Test;
#define T_Test_REQUIRED_BYTES_FOR_ENCODING 45
typedef struct {
asn1SccSint field1;
asn1SccSint field2;
asn1SccSint field3;
asn1SccSint field4;
asn1SccSint field5;
struct {
unsigned int field1:1;
unsigned int field2:1;
unsigned int field3:1;
unsigned int field4:1;
unsigned int field5:1;
} exist;
} T_Test2;
#define T_Test2_REQUIRED_BYTES_FOR_ENCODING 46
typedef struct {
asn1SccSint field1;
asn1SccSint field2;
asn1SccSint field3;
asn1SccSint field4;
asn1SccSint field5;
struct {
unsigned int field1:1;
unsigned int field2:1;
unsigned int field3:1;
unsigned int field4:1;
unsigned int field5:1;
} exist;
} T_Test3_1;
#define T_Test3_1_REQUIRED_BYTES_FOR_ENCODING 46
So space for the fields is still reserved in the encoding even when they are known to be absent, and space for the "exist" field is also unnecessary IMHO.
I would strongly prefer the ABSENT fields not to appear in the C struct. Please could this be considered?
In the context of safety-critical applications dead code is not allowed in the release binary.
Moreover, floating point is forbidden by certain standards.
The request is to modularize the asn1scc runtime so that an application can only include the actually used parts (features).
Candidate features
ACN related parts:
FixedLength[encoding ASCII, size null-terminated, termination-pattern 'FF'H]
ASN.1 related parts:
FixedLength ::= IA5String(SIZE(21))
fl FixedLength ::= "Konrad Grochowski "
C code:
void p_proxy_decode() {
unsigned char p_buffer[FixedLength_REQUIRED_BYTES_FOR_ACN_ENCODING];
BitStream p_stream;
BitStream_Init(&p_stream, p_buffer, FixedLength_REQUIRED_BYTES_FOR_ACN_ENCODING);
p_read_from_file(p_filename, (char *) p_buffer, FixedLength_REQUIRED_BYTES_FOR_ACN_ENCODING);
int p_error_code=0;
FixedLength p_to_decode;
flag p_result=FixedLength_ACN_Decode(p_to_decode, &p_stream, &p_error_code);
if (p_result==0) {
printf("p_result of FixedLength_ACN_Decode evaluated to false\n");
return;
}
if (p_error_code!=0) {
printf("Error code of FixedLength_ACN_Decode was set: %d\n",p_error_code);
return;
}
/* ... */
}
gdb lookup:
(gdb) n
16 flag p_result=FixedLength_ACN_Decode(p_to_decode, &p_stream, &p_error_code);
(gdb) info locals
p_buffer = "Konrad Grochowski \377"
p_stream = {buf = 0x7fffffffe0a0 "Konrad Grochowski \377", count = 22, currentByte = 0,
currentBit = 0}
p_error_code = 0
p_to_decode = '\000' <repeats 16 times>, "\272\344\377\377\377\177"
p_result = false
p_equal = false
(gdb) n
17 if (p_result==0) {
(gdb) info locals
p_buffer = "Konrad Grochowski \377"
p_stream = {buf = 0x7fffffffe0a0 "Konrad Grochowski \377", count = 22, currentByte = 22,
currentBit = 0}
p_error_code = 268435457
p_to_decode = "Konrad Grochowski \377"
p_result = false
p_equal = false
The function FixedLength_ACN_Decode
wrote correct string to p_to_decode
variable after decoding, but returned false. I check for the value and the test fails.
This is one of the tests developed as a #117, tested with branch master.
Given ASN.1 file:
MyModel DEFINITIONS ::= BEGIN
MyEnum ::= ENUMERATED
{
v1 (10),
v2 (20),
v3 (30)
}
END
and following ACN files:
MyModel DEFINITIONS ::= BEGIN
MyEnum [size 8, encoding BCD, encode-values]
END
and
MyModel DEFINITIONS ::= BEGIN
MyEnum [size 8, encoding BCD]
END
No mater which of the two ACN encoding files is used, generated AST file is the same in both cases, and information about encode-values
property is missing.
https://github.com/ttsiodras/asn1scc/blob/master/Ast/FsUtils.fs#L229
I'm new to GitHub and F#. I was reading some code and then I wondered why this modular function "Replace" was capitalized?
Although I do see connection that (for example) ASN.1 INTEGER
is now represented by <INTEGER>
tag, it seems like inconsistency in XML itself - and I'm just fan of making things consistent ;) (even if it would mean - "change everything to UPPERCASE")
Given model: bug.zip
after generating code using asn1.exe -c -ACN Bug.asn1 Bug.acn
and then trying to compile it (Bug.c
)
compilation fails - *
used to dereference not a pointer.
Model uses two tiers of types to access field introduced in ACN (parameterizedType <typeTier0.typeTier1.someType>
)
Given ASN.1:
TESTMODULE DEFINITIONS AUTOMATIC TAGS ::= BEGIN
GenericCommand ::= SEQUENCE {
commandArgument INTEGER OPTIONAL,
instructions SEQUENCE(SIZE(1..100)) OF INTEGER
}
Command ::= GenericCommand (WITH COMPONENTS {commandArgument PRESENT})
END
and ACN:
TESTMODULE DEFINITIONS ::= BEGIN
GenericCommand [] {
commandArgument [],
n INTEGER[],
instructions [size n]
}
END
when compiling the code to generate HTML ICD, the following error message is produced by asn1scc:
File:testmodule.acn, line:5, the ACN inserted field 'n' is not reference by any other field (e.g. as length determinant or presence determinant etc)
In fact, "n" is used as a size determinant for field "instruction". I believe that this behaviour is incorrect.
ACN related parts:
Positive[encoding ASCII, size null-terminated]
ASN.1 related parts:
Positive ::= INTEGER(1..MAX)
p Positive ::= 911
Ada code:
procedure p_proxy_encode is
p_stream : PINT.Positive_ACN_Stream;
p_result : adaasn1rtl.ASN1_RESULT;
begin
PINT.Positive_ACN_Encode(PINT.p, p_stream, p_result);
if (p_result.Success /= True) then
Ada.Text_IO.Put("Error was set after call to PINT.Positive_ACN_Encode. Error code: ");
Ada.Integer_Text_IO.Put(p_result.ErrorCode);
Ada.Text_IO.Put_Line("");
return;
--...
gdb lookup:
16 PINT.Positive_ACN_Encode(PINT.p, p_stream, p_result);
(gdb) info locals
p_stream = (k => -136516376, datalen => 32767, data => (0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1,
1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1 <repeats 19 times>, 0 <repeats 18 times>, 1, 1,
0 <repeats 66 times>, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0))
p_result = (success => 144, errorcode => 32767)
(gdb) p PINT.p
$1 = 911
(gdb) n
17 if (p_result.Success /= True) then
(gdb) info locals
p_stream = (k => 0, datalen => 0, data => (0 <repeats 152 times>))
p_result = (success => true, errorcode => 1001)
After Positive_ACN_Encode
memory in p_stream
is zeroed. p_result.success
is set to true, despite the fact that something obviously went wrong.
This is one of the tests developed as a #117, tested with branch master.
ACN related parts:
Positive[encoding ASCII, size null-terminated]
ASN.1 related parts:
Positive ::= INTEGER(1..MAX)
p Positive ::= 911
Ada code:
procedure p_proxy_decode is
p_stream : PINT.Positive_ACN_Stream;
p_value : PINT.Positive;
p_result : adaasn1rtl.ASN1_RESULT;
begin
p_read_from_file(p_stream.Data);
p_stream.DataLen := PINT.Positive_REQUIRED_BITS_FOR_ACN_ENCODING;
p_stream.k := 0;
PINT.Positive_ACN_Decode(p_value, p_stream, p_result);
if (p_result.Success /= True) then
Ada.Text_IO.Put("Error was set after call to PINT.Positive_ACN_Decode. Error code: ");
Ada.Integer_Text_IO.Put(p_result.ErrorCode);
Ada.Text_IO.Put_Line("");
return;
end if;
--...
gdb lookup:
36 PINT.Positive_ACN_Decode(p_value, p_stream, p_result);
(gdb) info locals
p_stream = (k => 0, datalen => 152, data => (0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0,
0, 1, 1, 0, 0, 0, 1, 0 <repeats 128 times>))
p_value = 140737347289027
p_result = (success => 96, errorcode => 32767)
(gdb) n
37 if (p_result.Success /= True) then
(gdb) info locals
p_stream = (k => 0, datalen => 152, data => (0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0,
0, 1, 1, 0, 0, 0, 1, 0 <repeats 128 times>))
p_value = 140737346065408
p_result = (success => false, errorcode => 32767)
p_stream
data as encoded by C function: Positive_ACN_Encode (xxd output):
00000000: 00111001 00110001 00110001 00000000 00000000 00000000 911...`
The data is read into p_stream as it was encoded by C function, but it seems it's not what Ada expect. I am not sure what 'k' field is for - I set it to zero in every test case and these are the only ones that fail.
This is one of the tests developed as a #117, tested with branch master.
ATC generated in C need manual wrapping by extern "C" code to build and use them in C++.
Variable vs Value :) (Value seems to be the new one)
Using present-when
encoding property inside CHOICE
together with IA5String results in incorrect C code generation (asn.exe -c -ACN String.asn1 String.acn). Asn1scc inserts string without ""
so it's treated as undeclared variable. The second problem is as gcc says - assignment to expression with array type
.
Example code causing these issues: String.zip
wireshark is a widely-used traffic analyzer.
In order to provide analysis of a specific protocol message encoding, it can be extended with a protocol-specific 'dissector'.
The dissector can be written either in C or LUA.
https://www.wireshark.org/docs/wsdg_html_chunked/ChapterDissection.html
https://www.wireshark.org/docs/wsdg_html_chunked/wsluarm.html
Note: It already supports ASN1 with standard encodings. ( to certain extent).
For ACN encoding, the wireshark dissector framework can be viewed as an alternate target platform.
( besides C , ADA for serialization purposes).
It is an alternative decoding of the protocol (ie - to a tree view ).
asn1scc could support this.
Hi,
I'm trying to compile asn1scc on MacOS X Yosemite, and I get an error from ANTLR:
XBuild Engine Version 3.2.6.0
Mono, Version 3.2.6.0
Copyright (C) Marek Sieradzki 2005-2008, Novell 2008-2011.
Build started 14/11/2014 10:35:16.
__________________________________________________
Project "/Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/parseStg2/parseStg2.csproj" (default target(s)):
Target PrepareForBuild:
Configuration: Debug Platform: x86
Target GenerateSatelliteAssemblies:
No input files were specified for target GenerateSatelliteAssemblies, skipping.
Target GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because its outputs are up-to-date.
Target CoreCompile:
Skipping target "CoreCompile" because its outputs are up-to-date.
Done building project "/Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/parseStg2/parseStg2.csproj".
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.3030340
XBuild Engine Version 3.2.6.0
Mono, Version 3.2.6.0
Copyright (C) Marek Sieradzki 2005-2008, Novell 2008-2011.
Build started 14/11/2014 10:35:17.
__________________________________________________
Project "/Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/Antlr.csproj" (default target(s)):
Target PrepareForBuild:
Configuration: Debug Platform: AnyCPU
Created directory "bin/Debug/"
Created directory "obj/Debug/"
Target PreBuildEvent:
Executing: java -jar /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/ /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/asn1.g
java -jar /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/ /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/acn.g
warning(200): /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/asn1.g:485:2: Decision can match input such as "{UID, NULL..BIT, BOOLEAN..ENUMERATED, INTEGER..SET, OCTET..UTCTime, OBJECT, RELATIVE_OID}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/asn1.g:513:7: Decision can match input such as "L_BRACKET LID LID {R_BRACKET, ':'}" using multiple alternatives: 15, 17
As a result, alternative(s) 17 were disabled for that input
warning(200): /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/asn1.g:513:7: Decision can match input such as "L_BRACKET LID {R_BRACKET, ':'}" using multiple alternatives: 16, 17
As a result, alternative(s) 17 were disabled for that input
error(10): internal error: Can't get property indirectDelegates using method get/isIndirectDelegates from org.antlr.tool.Grammar instance : java.lang.NullPointerException
java.util.Objects.requireNonNull(Objects.java:203)
java.util.ArrayList.removeAll(ArrayList.java:689)
org.antlr.tool.CompositeGrammar.getIndirectDelegates(CompositeGrammar.java:222)
org.antlr.tool.Grammar.getIndirectDelegates(Grammar.java:2620)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.antlr.stringtemplate.language.ASTExpr.invokeMethod(ASTExpr.java:564)
org.antlr.stringtemplate.language.ASTExpr.rawGetObjectProperty(ASTExpr.java:515)
org.antlr.stringtemplate.language.ASTExpr.getObjectProperty(ASTExpr.java:417)
org.antlr.stringtemplate.language.ActionEvaluator.attribute(ActionEvaluator.java:351)
org.antlr.stringtemplate.language.ActionEvaluator.expr(ActionEvaluator.java:136)
org.antlr.stringtemplate.language.ActionEvaluator.templateApplication(ActionEvaluator.java:216)
org.antlr.stringtemplate.language.ActionEvaluator.expr(ActionEvaluator.java:126)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:84)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.stringtemplate.language.ASTExpr.writeTemplate(ASTExpr.java:750)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:680)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:660)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.stringtemplate.language.ASTExpr.writeTemplate(ASTExpr.java:750)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:680)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:660)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.stringtemplate.language.ASTExpr.writeTemplate(ASTExpr.java:750)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:680)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:660)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.stringtemplate.language.ASTExpr.writeTemplate(ASTExpr.java:750)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:680)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:660)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.codegen.CodeGenerator.write(CodeGenerator.java:1281)
org.antlr.codegen.Target.genRecognizerFile(Target.java:94)
org.antlr.codegen.CodeGenerator.genRecognizer(CodeGenerator.java:466)
org.antlr.Tool.generateRecognizer(Tool.java:641)
org.antlr.Tool.process(Tool.java:454)
org.antlr.Tool.main(Tool.java:91)
warning(200): /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/acn.g:167:2: Decision can match input such as "SIZE NULL_TERMINATED" using multiple alternatives: 1, 4
As a result, alternative(s) 4 were disabled for that input
error(10): internal error: Can't get property indirectDelegates using method get/isIndirectDelegates from org.antlr.tool.Grammar instance : java.lang.NullPointerException
java.util.Objects.requireNonNull(Objects.java:203)
java.util.ArrayList.removeAll(ArrayList.java:689)
org.antlr.tool.CompositeGrammar.getIndirectDelegates(CompositeGrammar.java:222)
org.antlr.tool.Grammar.getIndirectDelegates(Grammar.java:2620)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.antlr.stringtemplate.language.ASTExpr.invokeMethod(ASTExpr.java:564)
org.antlr.stringtemplate.language.ASTExpr.rawGetObjectProperty(ASTExpr.java:515)
org.antlr.stringtemplate.language.ASTExpr.getObjectProperty(ASTExpr.java:417)
org.antlr.stringtemplate.language.ActionEvaluator.attribute(ActionEvaluator.java:351)
org.antlr.stringtemplate.language.ActionEvaluator.expr(ActionEvaluator.java:136)
org.antlr.stringtemplate.language.ActionEvaluator.templateApplication(ActionEvaluator.java:216)
org.antlr.stringtemplate.language.ActionEvaluator.expr(ActionEvaluator.java:126)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:84)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.stringtemplate.language.ASTExpr.writeTemplate(ASTExpr.java:750)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:680)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:660)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.stringtemplate.language.ASTExpr.writeTemplate(ASTExpr.java:750)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:680)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:660)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.stringtemplate.language.ASTExpr.writeTemplate(ASTExpr.java:750)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:680)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:660)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.stringtemplate.language.ASTExpr.writeTemplate(ASTExpr.java:750)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:680)
org.antlr.stringtemplate.language.ASTExpr.writeAttribute(ASTExpr.java:660)
org.antlr.stringtemplate.language.ActionEvaluator.action(ActionEvaluator.java:86)
org.antlr.stringtemplate.language.ASTExpr.write(ASTExpr.java:149)
org.antlr.stringtemplate.StringTemplate.write(StringTemplate.java:705)
org.antlr.codegen.CodeGenerator.write(CodeGenerator.java:1281)
org.antlr.codegen.Target.genRecognizerFile(Target.java:94)
org.antlr.codegen.CodeGenerator.genRecognizer(CodeGenerator.java:466)
org.antlr.Tool.generateRecognizer(Tool.java:641)
org.antlr.Tool.process(Tool.java:454)
org.antlr.Tool.main(Tool.java:91)
/Library/Frameworks/Mono.framework/Versions/3.2.6/lib/mono/4.0/Microsoft.Common.targets: error : Command 'java -jar /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/ /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/asn1.g
java -jar /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/ /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/acn.g' exited with code: 1.
Task "Exec" execution -- FAILED
Done building target "PreBuildEvent" in project "/Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/Antlr.csproj".-- FAILED
Done building project "/Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/Antlr.csproj".-- FAILED
Build FAILED.
Errors:
/Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/Antlr.csproj (default targets) ->
/Library/Frameworks/Mono.framework/Versions/3.2.6/lib/mono/4.0/Microsoft.Common.targets (PreBuildEvent target) ->
/Library/Frameworks/Mono.framework/Versions/3.2.6/lib/mono/4.0/Microsoft.Common.targets: error : Command 'java -jar /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/ /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/asn1.g
java -jar /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/ /Users/alexey/Dropbox/Alex/Code/asn1scc/asn1scc/Antlr/acn.g' exited with code: 1.
0 Warning(s)
1 Error(s)
I made two minor changes to build.sh to be able to compile on Mac:
cp XER.stg xer.stg
, since filesystem on Mac is case-insensitive by default../parseStg2/bin/Debug/parseStg2.exe
witih mono ../parseStg2/bin/Debug/parseStg2.exe
, since Mac can't execute .exe without additional kernel modules<begin><end>
?
<min><max>
?
<beginIncluded><endIncluded>
?
it's not big change, but and suggest IMHO ;)
nuget is really helpful when developing .NET apps - it would be nice to have it available in docker image used by CircleCI
Compiler accepts complex string constraints, but fails to generate proper code for them:
Str ::= IA5String ((SIZE(1 .. 5) ^ FROM("a" .. "z")) | (SIZE(50 .. 100) ^ FROM ("A" .. "Z")))
C code:
flag Str_IsConstraintValid(const Str val, int* pErrCode)
{
flag ret = TRUE;
ret = (((((1 <= strlen(val)) && (strlen(val) <= 5)) && Str_CharsAreValid(val))) || ((((50 <= strlen(val)) && (strlen(val) <= 100)) && Str_CharsAreValid(val))));
*pErrCode = ret ? 0 : ERR_STR;
return ret;
}
Str_CharsAreValid
is used twiceeven less complex expressions fail with same issue:
Str2 ::= IA5String (SIZE(10) ^(FROM("a" .."z") | FROM("1" .. "3")))
flag Str2_IsConstraintValid(const Str2 val, int* pErrCode)
{
flag ret = TRUE;
ret = ((strlen(val) == 10) && ((Str2_CharsAreValid(val)) || (Str2_CharsAreValid(val))));
*pErrCode = ret ? 0 : ERR_STR2;
return ret;
}
Given ASN.1 file:
PUS-8-1 DEFINITIONS AUTOMATIC TAGS ::= BEGIN
EXPORTS ALL;
maxTC-8-1-Function-ID-Size INTEGER ::= 8
TC-8-1-PerformAFunction ::= SEQUENCE
{
functionArgs FunctionArguments
}
Function-ID ::= IA5String (SIZE(maxTC-8-1-Function-ID-Size))
FunctionArguments ::= CHOICE
{
pus8-Function1 NULL,
pus8-Function2 IA5String(SIZE(16))
}
END
and ACN file:
PUS-8-1 DEFINITIONS ::= BEGIN
TC-8-1-PerformAFunction []
{
function-ID Function-ID [],
functionArgs <function-ID> []
}
Function-ID []
FunctionArguments <Function-ID: function> []
{
pus8-Function1 [present-when function == "Function1"],
pus8-Function2 [present-when function == "Function2"]
}
END
ASN1SCC generates the following C code:
flag FunctionArguments_ACN_Decode(FunctionArguments* pVal, BitStream* pBitStrm, int* pErrCode, Function_ID function)
{
flag ret = TRUE;
int i1;
*pErrCode = 0;
else if ((strcmp(function, "Function2") == 0)) {
pVal->kind = pus8_Function2_PRESENT;
for(i1=0; (i1 < (int)16) && ret; i1++)
{
asn1SccSint charValue=0;
ret = BitStream_DecodeConstraintWholeNumber(pBitStrm, &charValue, 0, 127);
*pErrCode = ret ? 0 : ERR_INSUFFICIENT_DATA;
pVal->u.pus8_Function2[i1] = (char)charValue;
}
pVal->u.pus8_Function2[16] = 0x0;
}
else {
*pErrCode = ERR_ACN_DECODE_FUNCTIONARGUMENTS; /*COVERAGE_IGNORE*/
ret = FALSE; /*COVERAGE_IGNORE*/
}
return ret;
}
The code does not compile due to mutilated "else if" construct.
It's not clear: Program.cs has the command-line args for BER & XER commented out.
If I pass -BER to the compiler I get prototypes, but no implementation.
After generating C code with asn.1 constant (asn1.exe -c String.asn1
):
String DEFINITIONS AUTOMATIC TAGS ::= BEGIN
stringConstant IA5String ::= "Constant"
END
I get following result:
const char stringConstant = "Constant";
Looks like either *
or []
is missing in generated code.
Given following ASN.1
MyModel DEFINITIONS ::= BEGIN
MyBitString ::= BIT STRING(SIZE(5))
MySeq ::= SEQUENCE
{
bitStr MyBitString
}
END
and ACN:
MyModel DEFINITIONS ::= BEGIN
MySeq []
{
number INTEGER [encoding BCD, size 32],
bitStr [size number]
}
END
When calling asn.1.exe -c -ACN MyModel.asn1 MyModel.acn
and than gcc -c acn.c asn1crt.c MyModel.c real.c
, the gcc build fails.
The same applies when changing BIT STRING to OCTET STRING.
For both following ASN.1 files:
OptionalExample DEFINITIONS ::= BEGIN
MySequence ::= SEQUENCE
{
i1 INTEGER OPTIONAL,
i2 INTEGER OPTIONAL
}
END
and
OptionalExample DEFINITIONS ::= BEGIN
MySequence ::= SEQUENCE
{
i1 INTEGER,
i2 INTEGER
}
END
the produced AST XML is the same, and it does not contain information if sequence component is OPTIONAL.
I have two modules (in test.asn and second.asn)
test.asn contains:
----------------------------------------------------------------------------------------
-- Test module
----------------------------------------------------------------------------------------
Test-Dataview DEFINITIONS ::=
BEGIN
IMPORTS
T-SecondModule-Type1 FROM SecondModule-Dataview;
T-Test{TypeParameter} ::= SEQUENCE {
type1Thing T-SecondModule-Type1,
value TypeParameter
}
T-MyParameterisedType ::= T-Test{INTEGER}
END
second.asn contains:
----------------------------------------------------------------------------------------
-- Second module
----------------------------------------------------------------------------------------
SecondModule-Dataview DEFINITIONS ::=
BEGIN
T-SecondModule-Type1 ::= INTEGER
END
When I run the following command:
asn1.exe test.asn second.asn
I get the following error report:
Duplicate definition: T-SecondModule-Type1
I don't understand why I get this error. Am I doing something wrong?
I guess, that are really :) but I have no idea what <WITH_CONS /> is - maybe better name could help future users.
Instead of current "File [filename], line: [line] ...." format it could be helpful for developers to move to "[filename]:[line]:[column]: error: ....." format.
This way, whenever asn1scc would be included in some build toolchain (.asn -> .c -> .o -> ...), most IDEs, log scanners etc., would automatically capture it's messages.
I've already implemented required changes (ttsiodras/asn1scc@master...hcorg:gcc_style_errors), but a lot of tests would require an update, so I want to know if it's a desired change, before I edit 10+ files :)
Subtree of parametrized type use copies tree from parametrized type definition, which makes multiple occurrences of same Line
'CharPositionsInLine` in file.
I know it's not something that could be fixed completely, but maybe at least some marker "this type comes from parametrized type"? So IDE could ignore subtree.
Final solution could include both - copied tree and additionally something like "ParentType" and "ParametrTypes" (or something named better :) )
Hi,
It's not an "issue", it's a question, but I hope Issues is the right place to ask it.
I wrote an open-source UPER runtime for Java, to encode/decode classes annotated with asn1-datatypes. But I don't have a compiler yet, so I convert *.asn
files to annotated Java classes by hand. Now I'm looking into writing a compiler. Do you think I can reuse your parser and generator to produce Java? Do you have any tips where could I start? I see you have four different Backend*
folders, I wonder if you have any developer notes that tell what is what?
Best Regards,
Alex
I am testing the latest version of ASN1CC with GNAT Pro on the ARM Cortex R5 (little-endian) using ACN format for big-endian unsigned 16-bit integers. However, I am experiencing a problem with the expected bit order of the code generated by the compiler. The bytes: 0x02 0x03
should decode to the integer 0x0203
, but is instead decoded as 0x40C0
. If you look at the bits you can see that each byte has a reversed bit order. I assume that this is resulted by the compiler formatting the packed Boolean array with byte alignment, and the bytes in the expected order, but within each byte the lowest index is the least significant bit, not the most significant as expected by the code:
PROCEDURE BitStream_Decode_Non_Negative_Integer (S : in BitArray;
P : in out DECODE_PARAMS;
IntValue : out Asn1UInt;
nBitsRange : IN Integer;
result : OUT Boolean)
--# derives IntValue from S, P, nBitsRange & P from P, nBitsRange & result from P, nBitsRange;
--# pre nBitsRange >= 0 and nBitsRange <= 64 and
--# P.K+1>= S'First and P.K + nBitsRange <= S'Last;
--# post P.K = P~.K + nBitsRange;
IS
BEGIN
IntValue := 0;
FOR I IN Integer range 1..nBitsRange LOOP
--# assert nBitsRange >= 1 and nBitsRange <= 64 and
--# P.K + nBitsRange <= S'Last and
--# P.K+1>= S'First and
--# P.K=P~.K and P.K + I - 1< S'Last;
IF S(P.K+I)=0 THEN
IntValue := 2*IntValue;
ELSE
IntValue := 2*IntValue + 1;
END IF;
END LOOP;
P.K := P.K + nBitsRange;
result := P.DataLen-P.K>=0;
END BitStream_Decode_Non_Negative_Integer;
Am I missing something here? This seems to be the standard way of GNAT to output packed Boolean arrays. The C output uses the right bit order. Also, the Ada code above is not very efficient, the C codes use of bytes from the buffer seems better.
It would be valuable to support JSON serialization.
Running 3.3.9 in TASTE VM, the ASN.1 compiler emits some illegal code.
The following ASN.1 module:
----------------------------------------------------------------------------------------
-- Test module
----------------------------------------------------------------------------------------
Test-Dataview DEFINITIONS ::=
BEGIN
T-Test ::= SEQUENCE {
type1Thing INTEGER,
value INTEGER
}
c-A-Thing SEQUENCE SIZE(1 .. 10) OF T-Test ::= {
{type1Thing 0, value 1}
}
END
gives the following error when compiled:
assert@tastevm:~/asn1scc_bug2$ asn1.exe -c test.asn
assert@tastevm:~/asn1scc_bug2$ gcc -c test.c
test.c:40:3: error: conflicting types for 'c_A_Thing'
} c_A_Thing = { .nCount = 1, .arr =
^
In file included from test.c:7:0:
test.h:32:3: note: previous declaration of 'c_A_Thing' was here
} c_A_Thing;
^
The reason is that C treats both anonymous structure definitions (i.e. the definition in the C file and the extern declaration in the H file) as new types:
ยง6.7.2.3/p5: "Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type."
Given ASN.1:
MyModel DEFINITIONS ::= BEGIN
MyIA5String ::= IA5String("A" .. "Z")
MyNumericString ::= NumericString("1" .. "9")
END
when trying to generate code by calling asn1.exe -c MyModel.asn
uncaught Exception of type 'FsUtils+BugErrorException'
is thrown.
Even though presented code might be invalid ASN.1, the crash is unexpected.
Given ASN.1:
MyModel DEFINITIONS ::= BEGIN
MyNumericString ::= NumericString(SIZE(10))(FROM("A" .. "Z"))
END
Calling asn1.exe -c MyModel.asn
ends with uncought System.Collections.Generic.KeyNotFoundException
being thrown.
Hello,
Is it possible to have custom types specified in an ASN1 grammar, equivalent to basic C types?
E.g. for unsigned short type can i specify an ASN1 grammar in order to generate an unsigned short equivalent? I have tried using an INTEGER(0..65535) constraint, but it generates an uint64_t and verifies its constraints, but still an integer with 32 or 64 bits in length, according to target architecture.
I have manually modified the generated code in order to obtain the desired requirements.
I also attach a small project in order to show the patch i used. It contains:
i)asn1-sources folder: the asn1 sources of the type equivalent to the C standard unsigned short type (mytype.asn1 file) and the definition of a message structure that has fields defined using the previous mentioned type (includingtype.asn1 file);
ii)generated-c-sources: the C files generated by the asn1 compiler.
iii)modified-c-sources: C files generated by the asn1 compiler and modified in order to accommodate the internal requirements to the unsigned short type.
Could you, please, tell me if there is an workaround to manual modifications?
And is this okay from your point of view ?
Best regards,
Iulian.
int GetStrmPos(ByteStream* pByteStrm) {
return pByteStrm->currentByte;
}
asn1SccSint ByteStream_GetLength(ByteStream* pStrm)
{
return pStrm->currentByte;
}
GetStrmPos is not used from the runtime. Maybe generated code uses it ?
Also, it does not have appropriate prefix while it is an external function.
# Below, you will find a sequence of bash commands you can use to re-produce
# a bug involving a silently ignored decoding failure. You could probably
# run all this as a script, but it is reccomended that you run it
# line-by-line (with the exception of the multi-line cat statements which be
# all run together to correctly re-construct the source files.)
mkdir /tmp/invalid_read_demo/
cd /tmp/invalid_read_demo/
# ASN1 schema file:
cat >ABC.asn <<EOL
Foo DEFINITIONS AUTOMATIC TAGS ::= BEGIN
ABC ::= SEQUENCE {
member REAL (-90.0..90.0)
}
END
EOL
# Example C program that triggers bug:
cat >main.c<<EOL
#include "ABC.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main(){
char a[] = "\x12\x34\x56\x78";
uint32_t a_len = 4;
ABC p;
int errCode;
unsigned char bitStreamBuffer[a_len];
flag f;
BitStream bs;
BitStream_Init(&bs, &bitStreamBuffer[0], a_len);
memcpy(&bitStreamBuffer[0], &a[0], a_len);
f = ABC_Decode(&p, &bs, &errCode);
if(f){
printf("member: %f\n", p.member);
}else{
printf("Decode failure\n");
}
}
EOL
git clone https://github.com/ttsiodras/asn1scc.git
cd asn1scc
git checkout 959fbb65e476b4a1b10df51519804f904568a7af # Jan 21, 2018
xbuild /p:TargetFrameworkVersion="v4.5"
cd ../
asn1scc/Asn1f2/bin/Debug/Asn1f2.exe -c -uPER ABC.asn
gcc *.c
valgrind ./a.out # Observe uninitialized reads that occur due to silently ignored decoding failure.
# The observed result is that the decoding returns a 'success' flag = 1 but subsequent
# accessing of the member results in an un-initialized read because decoding was actually aborted.
# The expected result is to see a decoding failure from (flag = 0).
# The root cause of the problem is a missing 'ret =' assignment in ABC_member_Decode.
# For a safety-critical system, the general design pattern of assuming success is a code smell.
# Consider assuming failure for return codes unless proven otherwise throughout this application.
When building project on Linux platform, following error is reported
Service.Implementation/Asn1Service.fs(87,36): error FS0001: This expression was expected to have type 'a * 'b but here has type (Asn1Ast.AstRoot -> AcnCreateFromAntlr.AcnAst -> unit) -> Asn1AcnAst.AstRoot * Asn1AcnAst.AcnInsertedFieldDependencies
which terminates build.
HEAD is : 2bbc66af93ca6be25c26bcecece47f8b6f99ee4a
OS information:
Operating System: Debian GNU/Linux 9 (stretch)
Kernel: Linux 4.9.0-6-amd64
Architecture: x86-64
also some other attributes (like id
or value
) do not follow CamelCase
, which seems to be dominant style
Hi
Very cool project. Just wondering if the code generator would have any hiccups with a big fully blown ASN.1 definition like X400's P1? Are there features of ASN.1 that asn1scc does not support?
Trying to parse x420 asn.1 definitions and the grammar does not seem to account for the possibility that an import might have {} like ExtensionField{} in the fragment below:
-- Module MTAAbstractService (X.411:06/1999)
MTAAbstractService {joint-iso-itu-t mhs(6) mts(3) modules(0)
mta-abstract-service(2) version-1999(1)} DEFINITIONS IMPLICIT TAGS ::=
BEGIN
-- Prologue
-- Exports everything
IMPORTS
-- Remote Operations
CONNECTION-PACKAGE, CONTRACT
--==
FROM Remote-Operations-Information-Objects {joint-iso-itu-t
remote-operations(4) informationObjects(5) version1(0)}
emptyUnbind
--==
FROM Remote-Operations-Useful-Definitions {joint-iso-itu-t
remote-operations(4) useful-definitions(7) version1(0)}
-- MTS Abstract Service Parameters
ABSTRACT-ERROR, ABSTRACT-OPERATION, administration, AdministrationDomainName,
certificate-selectors, certificate-selectors-override, Content,
ContentIdentifier, ContentLength, ContentType,
content-confidentiality-algorithm-identifier, content-correlator,
content-integrity-check, conversion-with-loss-prohibited,
ConvertedEncodedInformationTypes, CountryName, DeferredDeliveryTime,
delivery, dl-exempted-recipients, dl-expansion-history,
dl-expansion-prohibited, ExplicitConversion, EXTENSION, ExtensionField{},
GlobalDomainIdentifier, InitiatorCredentials, latest-delivery-time,
message-origin-authentication-check, message-security-label, message-token,
MHS-OBJECT, MTAName, MTSIdentifier, multiple-originator-certificates,
ORAddressAndOptionalDirectoryName, OriginalEncodedInformationTypes,
originator-and-DL-expansion-history, originator-certificate,
originator-return-address, PerMessageIndicators, physical-delivery-modes,
physical-delivery-report-request, physical-forwarding-address,
physical-forwarding-address-request, physical-forwarding-prohibited,
physical-rendition-attributes, PORT, Priority, PrivateDomainIdentifier,
PrivateExtensions, probe-origin-authentication-check, proof-of-delivery,
proof-of-delivery-request, recipient-certificate,
recipient-number-for-advice, recipient-reassignment-prohibited,
redirection-history, registered-mail-type, reporting-DL-name,
reporting-MTA-certificate, reporting-MTA-name, ReportType,
report-origin-authentication-check, requested-delivery-method,
ResponderCredentials, SecurityContext, submission,
SupplementaryInformation, Time
--==
FROM MTSAbstractService {joint-iso-itu-t mhs(6) mts(3) modules(0)
mts-abstract-service(1) version-1999(1)}
# Below, you will find a sequence of bash commands you can use to re-produce
# a bug involving a silently ignored decoding failure. You could probably
# run all this as a script, but it is reccomended that you run it
# line-by-line (with the exception of the multi-line cat statements which be
# all run together to correctly re-construct the source files.)
mkdir /tmp/ignore_constraint/
cd /tmp/ignore_constraint/
# ASN1 schema file:
cat >ABC.asn <<EOL
Foo DEFINITIONS AUTOMATIC TAGS ::= BEGIN
ABC ::= SEQUENCE {
n INTEGER (0..2)
}
END
EOL
# Example C program that triggers bug:
cat >main.c<<EOL
#include "ABC.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main(){
char a[] = "\xC0";
uint32_t a_len = 1;
ABC p;
int errCode;
unsigned char bitStreamBuffer[a_len];
flag f;
BitStream bs;
BitStream_Init(&bs, &bitStreamBuffer[0], a_len);
memcpy(&bitStreamBuffer[0], &a[0], a_len);
f = ABC_Decode(&p, &bs, &errCode);
if(f){
printf("n is: %llu\n", p.n);
}else{
printf("Decode failure\n");
}
}
EOL
git clone https://github.com/ttsiodras/asn1scc.git
cd asn1scc
git checkout 959fbb65e476b4a1b10df51519804f904568a7af # Jan 21, 2018
xbuild /p:TargetFrameworkVersion="v4.5"
cd ../
asn1scc/Asn1f2/bin/Debug/Asn1f2.exe -c -uPER ABC.asn
gcc *.c
./a.out
# The observed result is that running the above program produces
# the output 'n is: 3'
# The expected result is a decoding error that allows the application to
# avoid using a value that may be out of range (possibly resulting in overflow etc.).
# The root cause of the problem is that the function BitStream_DecodeConstraintPosWholeNumber
# (and likely other similar functions) does not do any range checks to verify that the
# decoded number is within the specified range. The min/max values are only used to
# compute the size of the range of values that can be represented in order to determine
# how many bits must be accepted for decoding. This still allows out of range values
# to be decoded when the invalid value is larger than the max of the range, but less
# than the nearest power of two.
Given file:
Exporter DEFINITIONS AUTOMATIC TAGS ::= BEGIN
EXPORTS ALL;
val INTEGER ::= 157
END
and file:
Importer DEFINITIONS AUTOMATIC TAGS ::= BEGIN
IMPORTS val FROM Exporter;
MyInt ::= INTEGER (-256 .. val)
END
produced AST is following (please note that ImportedModules section is empty):
<?xml version="1.0" encoding="utf-8"?>
<ASN1AST>
<Asn1File FileName="Importer.asn">
<Asn1Module ID="Importer">
<ExportedTypes>
<ExportedType Name="MyInt" />
</ExportedTypes>
<ExportedVariables>
</ExportedVariables>
<ImportedModules>
</ImportedModules>
<TypeAssignments>
<TypeAssignment Name="MyInt" Line="5" CharPositionInLine="4">
<Type Line="5" CharPositionInLine="14">
<IntegerType Min="-256" Max="157"/>
</Type>
</TypeAssignment>
</TypeAssignments>
<VariablesAssignments>
</VariablesAssignments>
</Asn1Module>
</Asn1File>
<Asn1File FileName="Exporter.asn">
<Asn1Module ID="Exporter">
<ExportedTypes>
</ExportedTypes>
<ExportedVariables>
<ExportedVariable Name="val" />
</ExportedVariables>
<ImportedModules>
</ImportedModules>
<TypeAssignments>
</TypeAssignments>
<VariablesAssignments>
<VariableAssignment Name="val" Line="5" CharPositionInLine="4">
<Type Line="5" CharPositionInLine="8">
<IntegerType Min="MIN" Max="MAX"/>
</Type>
<Value>157</Value>
</VariableAssignment>
</VariablesAssignments>
</Asn1Module>
</Asn1File>
</ASN1AST>
Produced C code is also invalid for that case.
However, given file:
Exporter DEFINITIONS AUTOMATIC TAGS ::= BEGIN
EXPORTS ALL;
val INTEGER ::= 157
IntType ::= INTEGER
END
and file:
Importer DEFINITIONS AUTOMATIC TAGS ::= BEGIN
IMPORTS val, IntType FROM Exporter;
MyInt ::= INTEGER (-256 .. val)
END
produced AST is following (please note ImportedModules section is not empty):
<?xml version="1.0" encoding="utf-8"?>
<ASN1AST>
<Asn1File FileName="Importer.asn">
<Asn1Module ID="Importer">
<ExportedTypes>
<ExportedType Name="MyInt" />
<ExportedType Name="IntType" />
</ExportedTypes>
<ExportedVariables>
</ExportedVariables>
<ImportedModules>
<ImportedModule ID="Exporter">
<ImportedTypes>
<ImportedType Name="IntType" />
</ImportedTypes>
<ImportedVariables>
<ImportedVariable Name="val" />
</ImportedVariables>
</ImportedModule>
</ImportedModules>
<TypeAssignments>
<TypeAssignment Name="MyInt" Line="5" CharPositionInLine="4">
<Type Line="5" CharPositionInLine="14">
<IntegerType Min="-256" Max="157"/>
</Type>
</TypeAssignment>
</TypeAssignments>
<VariablesAssignments>
</VariablesAssignments>
</Asn1Module>
</Asn1File>
<Asn1File FileName="Exporter.asn">
<Asn1Module ID="Exporter">
<ExportedTypes>
<ExportedType Name="IntType" />
</ExportedTypes>
<ExportedVariables>
<ExportedVariable Name="val" />
</ExportedVariables>
<ImportedModules>
</ImportedModules>
<TypeAssignments>
<TypeAssignment Name="IntType" Line="6" CharPositionInLine="4">
<Type Line="6" CharPositionInLine="16">
<IntegerType Min="MIN" Max="MAX"/>
</Type>
</TypeAssignment>
</TypeAssignments>
<VariablesAssignments>
<VariableAssignment Name="val" Line="5" CharPositionInLine="4">
<Type Line="5" CharPositionInLine="8">
<IntegerType Min="MIN" Max="MAX"/>
</Type>
<Value>157</Value>
</VariableAssignment>
</VariablesAssignments>
</Asn1Module>
</Asn1File>
</ASN1AST>
Produced C code is valid in this case.
ACN related parts:
Positive[encoding ASCII, size 256]
ASN.1 related parts:
Positive ::= INTEGER(1..MAX)
p Positive ::= 911
Encoding with Ada yields:
"+0000000000000000000000000000911
"
whereas with C:
"00000000000000000000000000000911
"
Then, when trying to decode the former in C, an assertion is thrown:
build/PositiveInt/Ascii/acn.c:683: Acn_Dec_UInt_ASCII_ConstSize:
Assertion `digit >= '0' && digit <= '9'' failed.']
The problem is caused by the '+' sign that Ada inserts.
Function adaasn1rtl.Acn_Enc_Int_ASCII_ConstSize
puts the sign character at the very beginning of its body:
IF IntVal>=0 THEN
tmp(1) := Character'Pos('+');
intValCopy := IntVal;
ELSE
tmp(1) := Character'Pos('-');
intValCopy := -IntVal;
END IF;
and, on C side, Acn_Dec_UInt_ASCII_ConstSize
asserts against it.
This is one of the tests developed as a #117, tested with branch master.
The OER encoding is intended to be very easy to decode. It may be a good fit for this compiler.
Mac OS X 10.12.1, Xcode-8.1. Installed Mono-3.12.1. Cloned the current asn1scc master. Followed the instructions - tried to run ./build.sh
:
$ ./build.sh
XBuild Engine Version 12.0
Mono, Version 3.12.1.0
Copyright (C) 2005-2013 Various Mono authors
Build started 11/7/2016 11:38:15 PM.
__________________________________________________
Project "/Users/uri/src/asn1scc/parseStg2/parseStg2.csproj" (default target(s)):
Target PrepareForBuild:
Configuration: Debug Platform: x86
Created directory "bin/Debug/"
Created directory "obj/x86/Debug/"
Target GenerateSatelliteAssemblies:
No input files were specified for target GenerateSatelliteAssemblies, skipping.
Target CoreCompile:
Tool /opt/local/lib/mono/4.5/mcs.exe execution started with arguments: /noconfig /debug:full /debug+ /optimize- /out:obj/x86/Debug/parseStg2.exe Program.cs Properties/AssemblyInfo.cs obj/x86/Debug/.NETFramework,Version=v4.0.AssemblyAttribute.cs /target:exe /define:"DEBUG;TRACE" /nostdlib /platform:x86 /reference:/opt/local/lib/mono/4.0/System.dll /reference:/opt/local/lib/mono/4.0/System.Xml.Linq.dll /reference:/opt/local/lib/mono/4.0/System.Data.DataSetExtensions.dll /reference:/opt/local/lib/mono/4.0/Microsoft.CSharp.dll /reference:/opt/local/lib/mono/4.0/System.Data.dll /reference:/opt/local/lib/mono/4.0/System.Xml.dll /reference:/opt/local/lib/mono/4.0/System.Core.dll /reference:/opt/local/lib/mono/4.0/mscorlib.dll /warn:4
Target DeployOutputFiles:
Copying file from '/Users/uri/src/asn1scc/parseStg2/obj/x86/Debug/parseStg2.exe.mdb' to '/Users/uri/src/asn1scc/parseStg2/bin/Debug/parseStg2.exe.mdb'
Copying file from '/Users/uri/src/asn1scc/parseStg2/obj/x86/Debug/parseStg2.exe' to '/Users/uri/src/asn1scc/parseStg2/bin/Debug/parseStg2.exe'
Done building project "/Users/uri/src/asn1scc/parseStg2/parseStg2.csproj".
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.9982800
XBuild Engine Version 12.0
Mono, Version 3.12.1.0
Copyright (C) 2005-2013 Various Mono authors
Build started 11/7/2016 11:38:17 PM.
__________________________________________________
Project "/Users/uri/src/asn1scc/Antlr/Antlr.csproj" (default target(s)):
Target PrepareForBuild:
Configuration: Debug Platform: AnyCPU
Created directory "bin/Debug/"
Created directory "obj/Debug/"
Target PreBuildEvent:
Executing: java -jar /Users/uri/src/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/uri/src/asn1scc/Antlr/ /Users/uri/src/asn1scc/Antlr/asn1.g
java -jar /Users/uri/src/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/uri/src/asn1scc/Antlr/ /Users/uri/src/asn1scc/Antlr/acn.g
warning(200): /Users/uri/src/asn1scc/Antlr/asn1.g:487:2: Decision can match input such as "{UID, NULL..BIT, BOOLEAN..ENUMERATED, INTEGER..SET, OCTET..UTCTime, OBJECT, RELATIVE_OID}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): /Users/uri/src/asn1scc/Antlr/asn1.g:515:7: Decision can match input such as "L_BRACKET LID {R_BRACKET, COMMA, ':'}" using multiple alternatives: 16, 17
As a result, alternative(s) 17 were disabled for that input
warning(200): /Users/uri/src/asn1scc/Antlr/asn1.g:515:7: Decision can match input such as "L_BRACKET LID LID {R_BRACKET, ':'}" using multiple alternatives: 15, 17
As a result, alternative(s) 17 were disabled for that input
warning(200): /Users/uri/src/asn1scc/Antlr/acn.g:170:2: Decision can match input such as "SIZE NULL_TERMINATED" using multiple alternatives: 1, 4
As a result, alternative(s) 4 were disabled for that input
Target CopyFilesMarkedCopyLocal:
Copying file from '/Users/uri/src/asn1scc/Antlr/antlr313/antlr.runtime.dll' to '/Users/uri/src/asn1scc/Antlr/bin/Debug/antlr.runtime.dll'
Copying file from '/Users/uri/src/asn1scc/Antlr/antlr313/Antlr3.Runtime.dll' to '/Users/uri/src/asn1scc/Antlr/bin/Debug/Antlr3.Runtime.dll'
Copying file from '/Users/uri/src/asn1scc/Antlr/antlr313/Antlr3.Utility.dll' to '/Users/uri/src/asn1scc/Antlr/bin/Debug/Antlr3.Utility.dll'
Copying file from '/Users/uri/src/asn1scc/Antlr/antlr313/StringTemplate.dll' to '/Users/uri/src/asn1scc/Antlr/bin/Debug/StringTemplate.dll'
Target GenerateSatelliteAssemblies:
No input files were specified for target GenerateSatelliteAssemblies, skipping.
Target CoreCompile:
Tool /opt/local/lib/mono/4.5/mcs.exe execution started with arguments: /noconfig /debug:full /debug+ /optimize- /out:obj/Debug/Antlr.dll acnLexer.cs acnParser.cs asn1Lexer.cs asn1Parser.cs Comments.cs Properties/AssemblyInfo.cs obj/Debug/.NETFramework,Version=v4.0.AssemblyAttribute.cs /target:library /define:"DEBUG;TRACE" /nostdlib /reference:antlr313/antlr.runtime.dll /reference:antlr313/Antlr3.Runtime.dll /reference:antlr313/Antlr3.Utility.dll /reference:antlr313/StringTemplate.dll /reference:/opt/local/lib/mono/4.0/System.dll /reference:/opt/local/lib/mono/4.0/System.Xml.Linq.dll /reference:/opt/local/lib/mono/4.0/System.Data.DataSetExtensions.dll /reference:/opt/local/lib/mono/4.0/Microsoft.CSharp.dll /reference:/opt/local/lib/mono/4.0/System.Data.dll /reference:/opt/local/lib/mono/4.0/System.Xml.dll /reference:/opt/local/lib/mono/4.0/System.Core.dll /reference:/opt/local/lib/mono/4.0/mscorlib.dll /warn:4
Target DeployOutputFiles:
Copying file from '/Users/uri/src/asn1scc/Antlr/obj/Debug/Antlr.dll.mdb' to '/Users/uri/src/asn1scc/Antlr/bin/Debug/Antlr.dll.mdb'
Copying file from '/Users/uri/src/asn1scc/Antlr/obj/Debug/Antlr.dll' to '/Users/uri/src/asn1scc/Antlr/bin/Debug/Antlr.dll'
Done building project "/Users/uri/src/asn1scc/Antlr/Antlr.csproj".
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:05.8285770
XBuild Engine Version 12.0
Mono, Version 3.12.1.0
Copyright (C) 2005-2013 Various Mono authors
Build started 11/7/2016 11:38:23 PM.
__________________________________________________
/Users/uri/src/asn1scc/Asn1.sln: warning : Ignoring vcproj 'asn1crt'.
/Users/uri/src/asn1scc/Asn1.sln: warning : Failed to find project 6094fc85-8ce9-4716-9255-41c428f3b9ea
/Users/uri/src/asn1scc/Asn1.sln: warning : Don't know how to handle GlobalSection TestCaseManagementSettings, Ignoring.
Project "/Users/uri/src/asn1scc/Asn1.sln" (default target(s)):
Target ValidateSolutionConfiguration:
Building solution configuration "Debug|Mixed Platforms".
Target Build:
Project "/Users/uri/src/asn1scc/Antlr/Antlr.csproj" (default target(s)):
Target PrepareForBuild:
Configuration: Debug Platform: AnyCPU
Target PreBuildEvent:
Executing: java -jar /Users/uri/src/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/uri/src/asn1scc/Antlr/ /Users/uri/src/asn1scc/Antlr/asn1.g
java -jar /Users/uri/src/asn1scc/Antlr/antlr313/antlr-3.2.jar -o /Users/uri/src/asn1scc/Antlr/ /Users/uri/src/asn1scc/Antlr/acn.g
warning(200): /Users/uri/src/asn1scc/Antlr/asn1.g:487:2: Decision can match input such as "{UID, NULL..BIT, BOOLEAN..ENUMERATED, INTEGER..SET, OCTET..UTCTime, OBJECT, RELATIVE_OID}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): /Users/uri/src/asn1scc/Antlr/asn1.g:515:7: Decision can match input such as "L_BRACKET LID {R_BRACKET, COMMA, ':'}" using multiple alternatives: 16, 17
As a result, alternative(s) 17 were disabled for that input
warning(200): /Users/uri/src/asn1scc/Antlr/asn1.g:515:7: Decision can match input such as "L_BRACKET LID LID {R_BRACKET, ':'}" using multiple alternatives: 15, 17
As a result, alternative(s) 17 were disabled for that input
warning(200): /Users/uri/src/asn1scc/Antlr/acn.g:170:2: Decision can match input such as "SIZE NULL_TERMINATED" using multiple alternatives: 1, 4
As a result, alternative(s) 4 were disabled for that input
Target GenerateSatelliteAssemblies:
No input files were specified for target GenerateSatelliteAssemblies, skipping.
Target GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because its outputs are up-to-date.
Target CoreCompile:
Tool /opt/local/lib/mono/4.5/mcs.exe execution started with arguments: /noconfig /debug:full /debug+ /optimize- /out:obj/Debug/Antlr.dll acnLexer.cs acnParser.cs asn1Lexer.cs asn1Parser.cs Comments.cs Properties/AssemblyInfo.cs /target:library /define:"DEBUG;TRACE" /nostdlib /reference:antlr313/antlr.runtime.dll /reference:antlr313/Antlr3.Runtime.dll /reference:antlr313/Antlr3.Utility.dll /reference:antlr313/StringTemplate.dll /reference:/opt/local/lib/mono/4.0/System.dll /reference:/opt/local/lib/mono/4.0/System.Xml.Linq.dll /reference:/opt/local/lib/mono/4.0/System.Data.DataSetExtensions.dll /reference:/opt/local/lib/mono/4.0/Microsoft.CSharp.dll /reference:/opt/local/lib/mono/4.0/System.Data.dll /reference:/opt/local/lib/mono/4.0/System.Xml.dll /reference:/opt/local/lib/mono/4.0/System.Core.dll /reference:/opt/local/lib/mono/4.0/mscorlib.dll /warn:4
Target DeployOutputFiles:
Copying file from '/Users/uri/src/asn1scc/Antlr/obj/Debug/Antlr.dll.mdb' to '/Users/uri/src/asn1scc/Antlr/bin/Debug/Antlr.dll.mdb'
Copying file from '/Users/uri/src/asn1scc/Antlr/obj/Debug/Antlr.dll' to '/Users/uri/src/asn1scc/Antlr/bin/Debug/Antlr.dll'
Done building project "/Users/uri/src/asn1scc/Antlr/Antlr.csproj".
Project "/Users/uri/src/asn1scc/AntlrUtils/AntlrUtils.fsproj" (default target(s)):
/Users/uri/src/asn1scc/AntlrUtils/AntlrUtils.fsproj: error : Target named 'Build' not found in the project.
Done building project "/Users/uri/src/asn1scc/AntlrUtils/AntlrUtils.fsproj".-- FAILED
Task "MSBuild" execution -- FAILED
Done building target "Build" in project "/Users/uri/src/asn1scc/Asn1.sln".-- FAILED
Done building project "/Users/uri/src/asn1scc/Asn1.sln".-- FAILED
Build FAILED.
Warnings:
/Users/uri/src/asn1scc/Asn1.sln: warning : Ignoring vcproj 'asn1crt'.
/Users/uri/src/asn1scc/Asn1.sln: warning : Failed to find project 6094fc85-8ce9-4716-9255-41c428f3b9ea
/Users/uri/src/asn1scc/Asn1.sln: warning : Don't know how to handle GlobalSection TestCaseManagementSettings, Ignoring.
Errors:
/Users/uri/src/asn1scc/Asn1.sln (default targets) ->
(Build target) ->
/Users/uri/src/asn1scc/AntlrUtils/AntlrUtils.fsproj (default targets) ->
/Users/uri/src/asn1scc/AntlrUtils/AntlrUtils.fsproj: error : Target named 'Build' not found in the project.
3 Warning(s)
1 Error(s)
Time Elapsed 00:00:05.4389260
Although code shown below might not be a good model, it's a bad thing asn1scc crashes, instead of providing some sort of information to the user.
Given ASN.1:
Crashing DEFINITIONS AUTOMATIC TAGS ::= BEGIN
SomeInt ::= INTEGER (0 .. 65535)
SomeSequence ::= SEQUENCE
{
field SomeInt
}
END
And ACN:
Crashing DEFINITIONS ::= BEGIN
SomeInt [size 16, encoding pos-int]
SomeSequence []
{
field [size 16] -- here's a line that causes problems, adding encoding here solves problem
}
END
when trying to generate code using asn1.exe -c -ACN Crash.asn1 Crash.acn
causes asn1scc to exit with message Object reference not set to an instance of an object
which suggests uncaught System.NullReferenceException
.
For complex models, a diagram generated from the asn1 files would be a valuable documentation.
Given ASN.1:
Module DEFINITIONS AUTOMATIC TAGS ::= BEGIN
MySet ::= SET { }
MySequence ::= SEQUENCE { }
END
the AST obtained with after invoking: Asn1f2.exe -ast ast.xml sequences.asn1
is as follows:
<?xml version="1.0" encoding="utf-8"?>
<ASN1AST>
<Asn1File FileName="sequences.asn1">
<Asn1Module ID="Module">
<ExportedTypes>
<ExportedType Name="MySet" />
<ExportedType Name="MySequence" />
</ExportedTypes>
<ExportedVariables>
</ExportedVariables>
<ImportedModules>
</ImportedModules>
<TypeAssignments>
<TypeAssignment Name="MySet" Line="2" CharPositionInLine="4">
<Type Line="2" CharPositionInLine="14">
<SequenceType>
</SequenceType>
</Type>
</TypeAssignment>
<TypeAssignment Name="MySequence" Line="3" CharPositionInLine="4">
<Type Line="3" CharPositionInLine="19">
<SequenceType>
</SequenceType>
</Type>
</TypeAssignment>
</TypeAssignments>
<VariablesAssignments>
</VariablesAssignments>
</Asn1Module>
</Asn1File>
</ASN1AST>
The information about whether type definition is SEQUENCE or SET is missing in produced XML file. It would be a welcome change, if SEQUENCE/SEQUENCE OF could be distinguished from SET/SET OF, based on AST XML file.
list of imported items is currently used by asn1scc,IDE to provide code completion suggestions.
It's not blocking, but without this IDE will become little less helpful.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.