Using asn1c compiler I get "long" instead of "int" for INTEGER data type

127 views Asked by At

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.

3

There are 3 answers

1
YaFred On

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

1
treuss On

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).

0
Kent Kostelac On

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