当前位置: 首页 > 工具软件 > mod_asn > 使用案例 >

asn1c linux使用方法,asn1c工具使用

朱通
2023-12-01

解压: tar  -zxvfasn1c-0.9.24.tar.gz

依次按照configure,make,make install的步骤编译、安装

出现这个问题:

test -z "/usr/local/share/asn1c"|| /bin/mkdir -p "/usr/local/share/asn1c"

/bin/mkdir: cannot create directory`/usr/local/share/asn1c': Permission denied

make[3]: *** [install-dist_pkgdataDATA]Error 1

make[3]: Leaving directory`/home/liuguili/asn1ctemp/asn1c-0.9.24/skeletons'

make[2]: *** [install-am] Error 2

make[2]: Leaving directory`/home/liuguili/asn1ctemp/asn1c-0.9.24/skeletons'

make[1]: *** [install-recursive] Error 1

make[1]: Leaving directory`/home/liuguili/asn1ctemp/asn1c-0.9.24/skeletons'

make: *** [install-recursive] Error 1

用sudo make install即可

采用用户手册上的一个例子

liuguili@ubuntu:~/asn1ctemp/test$ vimrectangle.asn1

rectangle.asn1:

RectangleModule1DEFINITIONS ::=

BEGIN

Rectangle ::=SEQUENCE {

height  INTEGER,

width   INTEGER

}

END

执行命令

asn1c -fnative-types rectangle.asn1

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

编码清单:

#include

#include

#include

static int write_out(const void *buffer,size_t size, void *app_key)

{

FILE *out_fp = app_key;

size_t wrote;

wrote = fwrite(buffer, 1, size, out_fp);

return (wrote == size)?0:-1;

}

int main(int ac, char **av)

{

Rectangle_t *rectangle;

asn_enc_rval_t ec;

rectangle = calloc(1, sizeof(Rectangle_t));

if(!rectangle)

{

perror("callocfailed");

exit(71);

}

rectangle->height = 42;

rectangle->width = 23;

const char *filename = "Test";

FILE *fp = fopen(filename, "wb");

if(!fp)

{

perror("filename");

exit(71);

}

ec = xer_encode(&asn_DEF_Rectangle, rectangle,XER_F_CANONICAL,write_out, fp);

fclose(fp);

if(ec.encoded == -1)

{

perror("Count not encodeRectangle.\n");

exit(65);

}

else

{

printf("Created with BERencoded Rectangle.\n");

}

xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);

return 0;

}

liuguili@ubuntu:~/asn1ctemp/test$ gcc -I.-o rencode *.c

执行文件:

liuguili@ubuntu:~/asn1ctemp/test$ ./encode

Created with BER encoded Rectangle.

42

23

出现一个非常搞笑的事情,本来代码要生成一个Test文件,我编译完成未执行就去找Test,结果没找到,我还纳闷了半天。

解码清单:

#include

#include

#include

int main(int ac, char **av)

{

Char buf[1024];/*Temporarybuffer */

Rectangle_t*rectangle=0;/*Typetodecode*/

asn_dec_rval_t rval;/*Decoderreturnvalue*/

FILE*fp;/*Inputfilehandler*/

size_t size;/*Numberofbytesread*/

char*filename;/*Inputfilename*/

/*Requireasinglefilenameargument*/

if(ac!=2)

{

fprintf(stderr,”Usage:%s\n”,av[0]);

exit(64);/*better,EX_USAGE*/

}

else

{

filename=av[1];

}

/*Openinputfileasread-onlybinary*/

fp=fopen(filename,”rb”);

if(!fp)

{

perror(filename);

exit(66);/*better,EX_NOINPUT*/

} /*Readuptothebuffersize*/

size=fread(buf,1,sizeof(buf),fp);

fclose(fp);

if(!size)

{

fprintf(stderr,”%s:Emptyorbroken\n”,filename);

exit(65);/*better,EX_DATAERR*/

}

/*DecodetheinputbufferasRectangletype*/

rval=ber_decode(0,&asn_DEF_Rectangle,(void**)&rectangle,buf,size);

if(rval.code!=RC_OK)

{

fprintf(stderr,”%s:BrokenRectangleencodingatbyte%ld\n”,filename,(long)rval.consumed);

exit(65);/*better,EX_DATAERR*/

}

/*PrintthedecodedRectangletypeasXML*/

xer_fprint(stdout,&asn_DEF_Rectangle,rectangle);

return 0;/*Decodingfinishedsuccessfully*/

}

编译出现问题:

liuguili@ubuntu:~/asn1ctemp/test$ gcc -I.-o dencode *.c

main.c: In function ‘main’:

main.c:37:77: error: stray ‘\342’ inprogram

main.c:37:77: error: stray ‘\200’ inprogram

出现此问题的原因是,源代码中存在汉语字符,如“”等,将其改过来即可~

代码参考asn1c用户手册:

 类似资料: