asn.1工具的使用

来源:互联网 发布:nba历届总决赛数据 编辑:程序博客网 时间:2024/06/10 08:44

在http://lionet.info/asn1c/download.html下载asn1c-0.9.24.tar.gz

解压: tar  -zxvf asn1c-0.9.24.tar.gz

进入:   cd  asn1c........

配置:  ./configure

编译:make

安装:sudo make install

然后编写两个文件:

ubuntu:~/asn1ctemp/test$ vim rectangle.asn1

rectangle.asn1:

RectangleModule1DEFINITIONS ::=BEGINRectangle ::=SEQUENCE {       height  INTEGER,       width   INTEGER}END

 

执行命令
   asn1c -fnative-types rectangle.asn1

   可能没有 -fnative-types 这个参数,直接执行 asn1c rectangle.asn1

生成一堆编码用的.c,.h


我在当前路径下写代码:

gedit  main.c


 #include <stdio.h>#include <sys/types.h>#include <Rectangle.h> /* Rectangle ASN.1 type *//* Write the encoded output into some FILE stream. */static int write_out(const void *buffer, size_t size, void *app_key) {FILE *out_fp = app_key;size_t wrote = fwrite(buffer, 1, size, out_fp);return (wrote == size) ? 0 : -1;}int main(int ac, char **av) {Rectangle_t *rectangle; /* Type to encode */asn_enc_rval_t ec; /* Encoder return value *//* Allocate the Rectangle_t */rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */if(!rectangle) {perror("calloc() failed ");exit(1);}/* Initialize the Rectangle members */rectangle->height = 42; /* any random value */rectangle->width = 23; /* any random value *//* BER encode the data if filename is given */if(ac < 2) {fprintf(stderr," specify filename for BER output\n");} else {const char *filename = av[1];FILE *fp = fopen(filename, "wb"); /* for BER output */if(!fp) {perror(filename);exit(1);}/* Encode the Rectangle type as BER (DER) */ec = der_encode(&asn_DEF_Rectangle, rectangle, write_out, fp);fclose(fp);if(ec.encoded == -1) {fprintf(stderr, "”Could not encode Rectangle (at %s)\n”",ec.failed_type ? ec.failed_type->name : "unknown");exit(1);} else {fprintf(stderr, "”Created %s with BER encoded Rectangle\n”", filename);}}/* Also print the constructed Rectangle XER encoded (XML) */xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);return 0; /* Encoding finished successfully */}

然后编译:gcc -I. *.c

注意这里必须是*.c ,要把所有的.c都编译


如果出错提示有未定义的函数,你把converter-sample.c 删掉,或者移到其他路径或名字,

因为这个文件文件也是一个例子,主函数

 mv converter-sample.c converter-sample.c-

我是直接改了后缀名,然后再  gcc -I. *.c 就会生成a.out


运行./a.out


原创粉丝点击