asn1c-使⽤最新版asn1c将asn⽂件转换成C代码
1,从下载最新版的asn1c的源码;苏联解体的影响
2,打开Linux系统,将asn1c源码解压,找到INSTALL.md⽂件,根据INSTALL.md⽂件步骤安装即可;
3,将下述内容的asn⽂件保存为Rectangle.asn⽂件(假设所在⽂件夹⽬录为../RectangleTest,./⽬录为asn1c安装⽬录asn1c-master)RectangleTest DEFINITIONS ::= BEGIN
Rectangle ::= SEQUENCE {
height INTEGER, -- Height of the rectangle
得其大者可以兼其小width INTEGER -- Width of the rectangle
}
END
4,编译asn⽂件
5,⽣成的C⽂件在RectangleTest⽬录中
6,将下述代码保存为main.c⽂件,什么的雪山
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <Rectangle.h> /* Rectangle ASN.1 type */
4/* Write the encoded output into some FILE stream. */
5static int write_out(const void *buffer, size_t size, void *app_key) {
6 FILE *out_fp = app_key;
7 size_t wrote = fwrite(buffer, 1, size, out_fp);
邻里关系
8return (wrote == size) ? 0 : -1;
9 }
10int main(int ac, char **av) {
11 Rectangle_t *rectangle; /* Type to encode */
12 asn_enc_rval_t ec; /* Encoder return value */
13/* Allocate the Rectangle_t */
14 rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
15if(!rectangle) {
发色16 perror("calloc() failed ");
17 exit(1);
18 }
19/* Initialize the Rectangle members */
20 rectangle->height = 42; /* any random value */
21 rectangle->width = 23; /* any random value */
22/* BER encode the data if filename is given */
23if(ac < 2) {
24 fprintf(stderr," specify filename for BER output\n");
颁奖典礼主持词
25 } el {
26const char *filename = av[1];
27 FILE *fp = fopen(filename, "wb"); /* for BER output */
28if(!fp) {
29 perror(filename);
30 exit(1);
31 }
32/* Encode the Rectangle type as BER (DER) */
33 ec = der_encode(&asn_DEF_Rectangle, rectangle, write_out, fp);
关于济南的诗句
34 fclo(fp);
ded == -1) {
36 fprintf(stderr, "”Could not encode Rectangle (at %s)\n”",
37 ec.failed_type ? ec.failed_type->name : "unknown");英语六级评分标准
38 exit(1);
39 } el {
40 fprintf(stderr, "”Created %s with BER encoded Rectangle\n”", filename);
41 }
42 }
43/* Also print the constructed Rectangle XER encoded (XML) */
44 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
45return0; /* Encoding finished successfully */
46 }
7.输⼊gcc -I. -o rencode *.c ,⽣成recode⽂件
8,执⾏./recode,结果如下: