I am trying to use the der_encode_to_buffer() function with the hopes of creating an ASN.1 BER encoded packet (buffer) for transmission over UDP/IP. Right now, I am trying to get part of the rectangle example working, and I have run into several problems.
I am using the provided rectangle.asn example and ran
$> asn1c rectangle.asn
I am using asn1c version 0.9.21
I used the following command line to compile:
$> gcc -I. -o main *.c
(note: I had to remove converter-sample.c to get it to work. Also, g++ showed up with numerious errors. I compiled successfully with gcc 4.1.1)
Here is the main.c I used:
#include <stdio.h>
#include <memory.h>
#include <sys/types.h>
#include "Rectangle.h"
#define RBSIZE 100
int
main(int argc, char *argv[])
{
char buf[RBSIZE];
asn_dec_rval_t rval;
int i;
//rect->height = decaf;
//rect->width = coffee;
/* gcc complained about invalid types on the above lines. The only thing
I could think of doing at this point was a memcpy. This might be
where I am going wrong. */
memcpy(&rect->height, &decaf, sizeof(int));
memcpy(&rect->width, &coffee, sizeof(int));
I also tried changing my height and width variables (decaf and coffee) to several
different values, and could not get the bit pattern in the buffer to change at all.
Please let me know if you see anything wrong with this code. I would be very greatful for any help you could provide.
Travis
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Check out the Rectangle.h file. It shows that you have height and width members of type INTEGER_t. The INTEGER_t type (INTEGER.h) consists of struct { uint8_t *buf, int size };
There are two approaches to handle this problem:
1. Use -fnative-types flag to the asn1c compiler:
asn1c -fnative-types rectangle.asn
with this flag, the height and width members will be of type `long`, which will allow you to have a height=0xdecaf assignment without any warnings.
2. Use asn_long2INTEGER() function, supplied in INTEGER.h file.
asn_long2INTEGER(&rectangle->height, 0xdecaf);
this way, you will convert a 0xdecaf (long) value into the value of type INTEGER_t.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you. That solved my problem. Another question though. If you use the -fnative-types flag, how does the size field get populated in the INTEGER_t struct? Is that critical information for the BER encoder? Or does the encoder assume local machine sizes if -fnative-types is used?
Also, is there something extra I have to do to get the example to compile with g++?
Thank you so much for your help,
Travis
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1. when you use -fnative-types, all INTEGER_t types are replaced by `long` types. There will be no .buf and .size members to speak of.
Consider this:
Type ::= SEQUENCE {
member INTEGER
}
without -fnative-types it will be compiled as
typedef struct {
INTEGER_t member;
} Type_t;
where member contains .buf and .size.
With -fnative-types it will be compiled as
typedef struct {
long member;
} Type_t;
where member is long, which does not have .buf and .size.
2. g++ is not supported.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Dear Mr.Lev
Thank so much for your useful asn1c tool. I'm programming in C++ i try "extend C"but C++ seems don't understand your asn1c function, data types :
error: ‘MyTypes_t’ was not declared in this scope.
Help me please, thank advanced!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Lev or anyone else that might know the answer.
I am trying to use the der_encode_to_buffer() function with the hopes of creating an ASN.1 BER encoded packet (buffer) for transmission over UDP/IP. Right now, I am trying to get part of the rectangle example working, and I have run into several problems.
I am using the provided rectangle.asn example and ran
$> asn1c rectangle.asn
I am using asn1c version 0.9.21
I used the following command line to compile:
$> gcc -I. -o main *.c
(note: I had to remove converter-sample.c to get it to work. Also, g++ showed up with numerious errors. I compiled successfully with gcc 4.1.1)
Here is the main.c I used:
#include <stdio.h>
#include <memory.h>
#include <sys/types.h>
#include "Rectangle.h"
#define RBSIZE 100
int
main(int argc, char *argv[])
{
char buf[RBSIZE];
asn_dec_rval_t rval;
int i;
size_t bufsize = sizeof(buf);
memset(buf, 0, bufsize);
Rectangle_t *rect;
Rectangle_t *rect_dec;
int decaf = 0xDECAF;
int coffee = 0xC0FFEE;
rect = (Rectangle_t *)calloc( 1, sizeof(Rectangle_t) );
rect_dec = (Rectangle_t *)calloc( 1, sizeof(Rectangle_t) );
if ( !rect || !rect_dec )
{
perror("calloc() failed\n");
exit(71);
}
//rect->height = decaf;
//rect->width = coffee;
/* gcc complained about invalid types on the above lines. The only thing
I could think of doing at this point was a memcpy. This might be
where I am going wrong. */
memcpy(&rect->height, &decaf, sizeof(int));
memcpy(&rect->width, &coffee, sizeof(int));
printf("%08x\r\n", rect->height);
printf("%08x\r\n", rect->width);
xer_fprint(stdout, &asn_DEF_Rectangle, rect);
/* Encode Rectangle into buffer. */
der_encode_to_buffer( &asn_DEF_Rectangle, rect, buf, bufsize );
printf("Encoded Buffer {");
for ( i = 0; i < bufsize; i++ )
printf(" %02x", buf[i]);
printf("}\n");
/* Decode Rectangle from buffer. */
rval = ber_decode( 0, &asn_DEF_Rectangle, (void **)&rect_dec, buf, bufsize );
if (rval.code != RC_OK)
{
printf("ERROR: Broken Rectangle encoding at byte %ld\n", (long)rval.consumed );
exit(65);
}
printf("%08x\r\n", rect_dec->height);
printf("%08x\r\n", rect_dec->width);
xer_fprint(stdout, &asn_DEF_Rectangle, rect_dec);
return 0;
}
Here is the error I got when I tried to assign rect as shown in the rectangle example:
main.c: In function 'main':
main.c:33: error: incompatible types in assignment
main.c:34: error: incompatible types in assignment
Here is the output that I got when I used the memcpy's in the program:
000decaf
00c0ffee
<Rectangle>
<height>0</height>
<width>0</width>
</Rectangle>
Encoded Buffer { 30 04 02 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00}
09fb7058
09fb7068
<Rectangle>
<height>0</height>
<width>0</width>
</Rectangle>
I also tried changing my height and width variables (decaf and coffee) to several
different values, and could not get the bit pattern in the buffer to change at all.
Please let me know if you see anything wrong with this code. I would be very greatful for any help you could provide.
Travis
No, this is all wrong.
Check out the Rectangle.h file. It shows that you have height and width members of type INTEGER_t. The INTEGER_t type (INTEGER.h) consists of struct { uint8_t *buf, int size };
There are two approaches to handle this problem:
1. Use -fnative-types flag to the asn1c compiler:
asn1c -fnative-types rectangle.asn
with this flag, the height and width members will be of type `long`, which will allow you to have a height=0xdecaf assignment without any warnings.
2. Use asn_long2INTEGER() function, supplied in INTEGER.h file.
asn_long2INTEGER(&rectangle->height, 0xdecaf);
this way, you will convert a 0xdecaf (long) value into the value of type INTEGER_t.
Lev,
Thank you. That solved my problem. Another question though. If you use the -fnative-types flag, how does the size field get populated in the INTEGER_t struct? Is that critical information for the BER encoder? Or does the encoder assume local machine sizes if -fnative-types is used?
Also, is there something extra I have to do to get the example to compile with g++?
Thank you so much for your help,
Travis
1. when you use -fnative-types, all INTEGER_t types are replaced by `long` types. There will be no .buf and .size members to speak of.
Consider this:
Type ::= SEQUENCE {
member INTEGER
}
without -fnative-types it will be compiled as
typedef struct {
INTEGER_t member;
} Type_t;
where member contains .buf and .size.
With -fnative-types it will be compiled as
typedef struct {
long member;
} Type_t;
where member is long, which does not have .buf and .size.
2. g++ is not supported.
Dear Mr.Lev
Thank so much for your useful asn1c tool. I'm programming in C++ i try "extend C"but C++ seems don't understand your asn1c function, data types :
error: ‘MyTypes_t’ was not declared in this scope.
Help me please, thank advanced!