1

I am trying to compile a simple ASN to C++ format:

RectangleTest DEFINITIONS ::=
BEGIN
 
Rectangle ::= SEQUENCE {
    height  INTEGER,     
    width   INTEGER         
}
 
END

However, when I compile, I get the following output:

/* Rectangle */
typedef struct Rectangle {
    long     height;
    long     width;
    
    /* Context for parsing across buffer boundaries */
    asn_struct_ctx_t _asn_ctx;
} Rectangle_t;

I specified INTEGER data type and instead I get "long". I would like to have C++ "int" instead.

I am using this open source compiler:

https://github.com/vlm/asn1c

I already tried with -fwide-types option but it does not solve my problem.

José
  • 21
  • 1

3 Answers3

2

ASN.1 is language agnostic: INTEGER does not mean C++ int

The tool you use is doing the type mapping et gives you (or not) options to change it.

asn1c C++ mapping for INTEGER is long

I don't think you can change it

YaFred
  • 9,698
  • 3
  • 28
  • 40
  • With the caveat that, since the ASN.1 compiler is open source, OP can just change its code. Whether they're capable or willing is another question. – Useless May 01 '23 at 22:38
2

You can try to limit the range allowed for your integer and see if this causes the compiler to create other types:

RectangleTest DEFINITIONS ::=
BEGIN
 
Rectangle ::= SEQUENCE {
    height  INTEGER (0..2147483647),
    width   INTEGER (0..2147483647)        
}
 
END

The default ASN.1 INTEGER definition is not limited, so it is logical for the compiler to translate it to the largest reasonable int type of the language (long).

treuss
  • 1,913
  • 1
  • 15
  • Some compilers, such as the one from Objective Systems, allow you to configure it via an xml configuration file that mirrors your .asn schema. This allows you to tell it how you want a messge field to be implemented. You can direct it to make an INTEGER and int, or long, without having to constrain the INTEGER in the asn file. But, I prefer the explicitness of constraining the INTEGER - leads to no doubts anywhere else. – bazza May 02 '23 at 06:44
0

Option 1 is to go through asn1c source code and find it how and where it sets the datatype in the output files. My guess it is somewhere in asn1c_C.c. But the top most comment suggest you shouldn't:

/*
 * Don't look into this file. First, because it's a mess, and second, because
 * it's a brain of the compiler, and you don't wanna mess with brains do you? ;)
 */

Option 2 is to simply not care. With the compiler you use you can always set the target architecture to be 32 bit. In that case long will not take up more than 32 bits.

See this stackoverflow answer for more details

Kent Kostelac
  • 2,257
  • 3
  • 29
  • 42