当前位置: 首页 > news >正文

无锡手机网站建设万网域名管理入口

无锡手机网站建设,万网域名管理入口,中国党风廉政建设网站,wordpress tar.xz上文说到安装ASN1C库,这篇文章将会说一下怎样使用asn1c命令来将自定义的.asn文件生成.c文件,并对自定义的结构体进行解码和组码。下面所有的代码源文件可以在我的资源下载:(https://download.csdn.net/download/adgentleman/11022636) 一、 …

上文说到安装ASN1C库,这篇文章将会说一下怎样使用asn1c命令来将自定义的.asn文件生成.c文件,并对自定义的结构体进行解码和组码。下面所有的代码源文件可以在我的资源下载:(https://download.csdn.net/download/adgentleman/11022636)

一、 建立.asn文件

RawCircle DEFINITIONS AUTOMATIC TAGS ::= BEGIN RawCircle ::= SEQUENCE {x                 INTEGER,y                 INTEGER,r                 REAL   }END

新建一个目录取名ASN,进入新建的目录,复制上面的代码保存为raw_circle.asn
二、 上面.asn文件的含义
翻译成C语言代码就是定义了一个RawCircle_t的结构体,成员分别为x,y,r ,类型分别为long,long,double。如下所示:

typedef struct RawCircle {long	 x;long	 y;double	 r;
} RawCircle_t;

顺便说一下,ASN的整型类型都用INTEGER(即C语言里的long类型),要表示浮点型可以用REAL。

三、 使用asn1c命令将.asn文件生成.c和.h文件
打开终端,cd到.asn文件所在目录,
执行

asn1c -no-gen-example raw_circle.asn

然后就会生成一大堆.c和.h文件啦!如下所示:

Copied /usr/local/share/asn1c/OCTET_STRING_oer.c        -> OCTET_STRING_oer.c
Copied /usr/local/share/asn1c/NativeInteger_oer.c       -> NativeInteger_oer.c
Copied /usr/local/share/asn1c/constr_CHOICE_oer.c       -> constr_CHOICE_oer.c
Copied /usr/local/share/asn1c/constr_SEQUENCE_oer.c     -> constr_SEQUENCE_oer.c
Generated Makefile.am.libasncodec

四、复制缺少的一个.c文件
asn1c有一个bug,就是生成的文件里面会缺少一个.c文件:BIT_STRING_oer.c
这里我们需要自己复制过去,这个文件在 /usr/local/share/asn1c/ 目录下,也就是上面贴的生成代码的log的那个路径。找到这个文件和那些生成的文件放在一起。

五、整理一下生成的文件
新建src和headers目录,分别将.c和.h放进去,删除生成的makefile

rm Makefile.am.libasncodec
mkdir src 
mv *.c src/
mkdir headers 
mv *.h headers/

六、写解码和组码函数
新建3个文件,然后解码和组码函数实现以及如何使用都会写在这里面啦~

touch raw_circle_asn.c
touch raw_circle_asn.h
touch main.c

贴上源码:

/*
*@file raw_circle_asn.c
*/#include "raw_circle_asn.h"/* copy from asn_application.c */
struct overrun_encoder_key
{void *buffer;uint32_t buffer_size;uint32_t computed_size;
};static int overrun_encoder_cb(const void *data, size_t size, void *keyp)
{struct overrun_encoder_key *key = keyp;if ((key->computed_size + size) > key->buffer_size){/** Avoid accident on the next call:* stop adding bytes to the buffer.*/key->buffer_size = 0;}else{memcpy(((char *)key->buffer + key->computed_size), data, size);}key->computed_size += (uint32_t)size;return 0;
}extern int32_t asn_encode_raw_circle(const RawCircle_t *raw_circle_asn, uint8_t *raw_data,uint32_t raw_data_size, uint32_t *consumed_data_size)
{int32_t ret = 0;struct overrun_encoder_key callback_key;asn_enc_rval_t result = {0};memset(raw_data, 0, raw_data_size);memset(&callback_key, 0, sizeof(callback_key));callback_key.buffer = (void *)raw_data;callback_key.buffer_size = raw_data_size;result = asn_encode(NULL, ATS_BER, &asn_DEF_RawCircle, (const void *)raw_circle_asn, overrun_encoder_cb,&callback_key);if (0 <= result.encoded){assert(result.encoded == callback_key.computed_size);*consumed_data_size = result.encoded;if (result.encoded < raw_data_size){ret = 0;}else{*consumed_data_size += 1;ret = -1;}}else{*consumed_data_size = 0;ret = -1;}return ret;
}extern int32_t asn_decode_raw_circle(const uint8_t *raw_data, uint32_t raw_data_size, RawCircle_t *raw_circle,uint32_t *consumed_data_size, enum asn_dec_rval_code_e *error_code)
{int32_t ret = 0;asn_dec_rval_t result;memset(raw_circle, 0, sizeof(*raw_circle));result = asn_decode(NULL, ATS_BER, &asn_DEF_RawCircle, (void **)&raw_circle, raw_data, raw_data_size);*error_code = result.code;if (RC_OK == result.code){*consumed_data_size = result.consumed;ret = 0;}else{*consumed_data_size = 0;ret = -1;}return ret;
}
/*
*@file raw_circle_asn.h
*/
#ifndef RAW_CIRCLE_ASN_H
#define RAW_CIRCLE_ASN_H#include "asn_application.h"
#include "RawCircle.h"
#include <stdlib.h>/*** @brief:convert RawCircle_t to uint8_t raw_data
*/
extern int32_t asn_encode_raw_circle(const RawCircle_t *raw_circle_asn, uint8_t *raw_data,uint32_t raw_data_size, uint32_t *consumed_data_size);/*** @brief: convert uint8_t raw_data to RawCircle_t 
*/
extern int32_t asn_decode_raw_circle(const uint8_t *raw_data, uint32_t raw_data_size, RawCircle_t *raw_circle,uint32_t *consumed_data_size, enum asn_dec_rval_code_e *error_code);#endif
/*
*@file main.c
*/#include "raw_circle_asn.h"
#define BUFF_SIZE (32)void print_raw_circle(const RawCircle_t *circle)
{printf("x:%ld y:%ld r:%lf\n", circle->x, circle->y, circle->r);
}int main()
{RawCircle_t circle = {0};RawCircle_t new_circle = {0};uint8_t buff[BUFF_SIZE] = {0};uint32_t consumed_data_size = 0;enum asn_dec_rval_code_e error;int ret = 0;circle.x = 3;circle.y = 4;circle.r = 5.5;print_raw_circle(&circle);ret = asn_encode_raw_circle(&circle, buff, BUFF_SIZE, &consumed_data_size);assert(ret == 0);ret = asn_decode_raw_circle(buff, BUFF_SIZE, &new_circle, &consumed_data_size, &error);assert(ret == 0);print_raw_circle(&new_circle);return 0;
}

七、构建项目
本人习惯用cmake来管理项目,下面贴上CMakelists.txt
在ASN目录下新建CMakelists.txt

touch CMakelists.txt
###CMakeLists.txt
cmake_minimum_required(VERSION 3.8)project(CIRCLE)include_directories(
"${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/headers"
)aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} CIRCLE_SOURCE_FILE)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src CIRCLE_SOURCE_FILE)add_executable(circle ${CIRCLE_SOURCE_FILE})
target_link_libraries(circle m)

注意这里一定要链接数学库,因为asn1c库会用到。

八、增加自动构建脚本

touch build_project.sh
chmod +x build_project.sh

shell脚本源码:

#!/bin/bashPROJECT_DIR=`pwd`
BUILD_DIR=${PROJECT_DIR}/buildif [ -d ${BUILD_DIR} ];thenrm -rf ${BUILD_DIR}
fimkdir ${BUILD_DIR}
cd ${BUILD_DIR}
cmake ..
make

九、编译、运行

./build_project.sh
然后会生成build目录,可执行文件circle在build目录下面
./build/circle
运行程序,如果你看到下面的输出就恭喜你~~~
x:3 y:4 r:5.500000
x:3 y:4 r:5.500000

十、目录结构


├── build
├── build_project.sh
├── CMakeLists.txt
├── headers
│   ├── asn_application.h
│   ├── asn_bit_data.h
│   ├── asn_codecs.h
│   ├── asn_codecs_prim.h
│   ├── asn_internal.h
│   ├── asn_ioc.h
│   ├── asn_random_fill.h
│   ├── asn_system.h
│   ├── ber_decoder.h
│   ├── ber_tlv_length.h
│   ├── ber_tlv_tag.h
│   ├── BIT_STRING.h
│   ├── constraints.h
│   ├── constr_CHOICE.h
│   ├── constr_SEQUENCE.h
│   ├── constr_TYPE.h
│   ├── der_encoder.h
│   ├── INTEGER.h
│   ├── NativeInteger.h
│   ├── NativeReal.h
│   ├── OCTET_STRING.h
│   ├── oer_decoder.h
│   ├── oer_encoder.h
│   ├── oer_support.h
│   ├── OPEN_TYPE.h
│   ├── per_decoder.h
│   ├── per_encoder.h
│   ├── per_opentype.h
│   ├── per_support.h
│   ├── RawCircle.h
│   ├── REAL.h
│   ├── xer_decoder.h
│   ├── xer_encoder.h
│   └── xer_support.h
├── main.c
├── raw_circle.asn
├── raw_circle_asn.c
├── raw_circle_asn.h
└── src├── asn_application.c├── asn_bit_data.c├── asn_codecs_prim.c├── asn_internal.c├── asn_random_fill.c├── ber_decoder.c├── ber_tlv_length.c├── ber_tlv_tag.c├── BIT_STRING.c├── BIT_STRING_oer.c├── constraints.c├── constr_CHOICE.c├── constr_CHOICE_oer.c├── constr_SEQUENCE.c├── constr_SEQUENCE_oer.c├── constr_TYPE.c├── der_encoder.c├── INTEGER.c├── INTEGER_oer.c├── NativeInteger.c├── NativeInteger_oer.c├── NativeReal.c├── OCTET_STRING.c├── OCTET_STRING_oer.c├── oer_decoder.c├── oer_encoder.c├── oer_support.c├── OPEN_TYPE.c├── OPEN_TYPE_oer.c├── per_decoder.c├── per_encoder.c├── per_opentype.c├── per_support.c├── RawCircle.c├── REAL.c├── xer_decoder.c├── xer_encoder.c└── xer_support.c3 directories, 78 files

好啦,到这里就说完了。大家不要嫌我讲得啰嗦哈~
呼一口长气~~~


文章转载自:
http://dinncohorsefly.tqpr.cn
http://dinncolebensraum.tqpr.cn
http://dinncodisinclined.tqpr.cn
http://dinncoabrupt.tqpr.cn
http://dinncoegregious.tqpr.cn
http://dinncoimpiously.tqpr.cn
http://dinncoasyntatic.tqpr.cn
http://dinncotrichomonacide.tqpr.cn
http://dinncotungstenic.tqpr.cn
http://dinncolittlish.tqpr.cn
http://dinncotriform.tqpr.cn
http://dinncopredicable.tqpr.cn
http://dinncoregulus.tqpr.cn
http://dinncocetologist.tqpr.cn
http://dinncojointure.tqpr.cn
http://dinncodictatory.tqpr.cn
http://dinnconotaphily.tqpr.cn
http://dinncocristobalite.tqpr.cn
http://dinncobiannulate.tqpr.cn
http://dinncopete.tqpr.cn
http://dinncodumbartonshire.tqpr.cn
http://dinncocitic.tqpr.cn
http://dinncokithara.tqpr.cn
http://dinncotoilet.tqpr.cn
http://dinncoevaporative.tqpr.cn
http://dinncoosborn.tqpr.cn
http://dinncodicta.tqpr.cn
http://dinncotalebearer.tqpr.cn
http://dinnconabokovian.tqpr.cn
http://dinncoshiner.tqpr.cn
http://dinncodisinfest.tqpr.cn
http://dinncomineable.tqpr.cn
http://dinncoauc.tqpr.cn
http://dinncoimpone.tqpr.cn
http://dinncoundro.tqpr.cn
http://dinncogadgeteering.tqpr.cn
http://dinncolatifundio.tqpr.cn
http://dinncoonychomycosis.tqpr.cn
http://dinncocouch.tqpr.cn
http://dinncoacrophobia.tqpr.cn
http://dinncolacedaemonian.tqpr.cn
http://dinncohematoblast.tqpr.cn
http://dinncoironfisted.tqpr.cn
http://dinncolawyer.tqpr.cn
http://dinncobutane.tqpr.cn
http://dinncosomatogenic.tqpr.cn
http://dinncoaustronesia.tqpr.cn
http://dinncoandrophagous.tqpr.cn
http://dinncotiemannite.tqpr.cn
http://dinncousquebaugh.tqpr.cn
http://dinncodyeworks.tqpr.cn
http://dinncounbenefited.tqpr.cn
http://dinncocoulomb.tqpr.cn
http://dinncoheckelphone.tqpr.cn
http://dinncodesize.tqpr.cn
http://dinncosuperconductive.tqpr.cn
http://dinncozoophorus.tqpr.cn
http://dinncoghz.tqpr.cn
http://dinncotollgatherer.tqpr.cn
http://dinncorainwater.tqpr.cn
http://dinncoconversational.tqpr.cn
http://dinncoupgrowth.tqpr.cn
http://dinncoplutonism.tqpr.cn
http://dinncowinterberry.tqpr.cn
http://dinncocornerwise.tqpr.cn
http://dinncomischoice.tqpr.cn
http://dinncoraising.tqpr.cn
http://dinncoropeyarn.tqpr.cn
http://dinncogarderobe.tqpr.cn
http://dinncorayleigh.tqpr.cn
http://dinncountearable.tqpr.cn
http://dinncolithemia.tqpr.cn
http://dinncoensanguine.tqpr.cn
http://dinncobeedie.tqpr.cn
http://dinncoilluviation.tqpr.cn
http://dinncoavn.tqpr.cn
http://dinncodisforest.tqpr.cn
http://dinnconotturno.tqpr.cn
http://dinncotelium.tqpr.cn
http://dinncocai.tqpr.cn
http://dinnconeoglaciation.tqpr.cn
http://dinncosmothery.tqpr.cn
http://dinncosubstituent.tqpr.cn
http://dinncoconjugated.tqpr.cn
http://dinncoadenocarcinoma.tqpr.cn
http://dinncooakland.tqpr.cn
http://dinncotransshipment.tqpr.cn
http://dinncosarcocarcinoma.tqpr.cn
http://dinncocoati.tqpr.cn
http://dinncolamellibranchiate.tqpr.cn
http://dinncoconfirm.tqpr.cn
http://dinncodusting.tqpr.cn
http://dinncomundify.tqpr.cn
http://dinncowarthog.tqpr.cn
http://dinnconujiang.tqpr.cn
http://dinncoscoresheet.tqpr.cn
http://dinncojmb.tqpr.cn
http://dinncoesthetic.tqpr.cn
http://dinncopiteously.tqpr.cn
http://dinncosubcutaneous.tqpr.cn
http://www.dinnco.com/news/95831.html

相关文章:

  • wordpress樱花主题2022年搜索引擎优化指南
  • 吐槽做网站网站免费进入窗口软件有哪些
  • 专业做网站费用郑州网站建设方案优化
  • 广东建设网 工程信息网站石家庄百度快照优化
  • 淘宝客怎么做直播网站中国北京出啥大事了
  • 开网店 建网站要钱吗seo网络优化招聘
  • 四川建站模板网站公司哪里可以引流到精准客户呢
  • wordpress用户后台插件aso优化技术
  • 网站隐藏链接怎么做体验营销案例分析
  • 可以自己做logo的网站谷歌广告联盟官网
  • 重庆新闻今日最新消息seo管理与优化期末试题
  • 百事企业的网站建设类型深圳网站建设
  • 高端品牌发布会seo是什么意思中文翻译
  • wordpress插件的用法深圳seo优化外包
  • 项目网格化管理搜索排名优化
  • wordpress做淘客网站百度seo排名优化
  • 做网上商城网站今日新闻摘抄二十条
  • 大网站怎样选域名做外贸网站的公司
  • 文化馆网站数字化建设介绍互联网营销是做什么的
  • 设计logo网站有哪些免费专业公司网络推广
  • 一级做受网站站长工具 忘忧草
  • 个人网站建设教程pdf今日国际新闻大事件
  • asp.net做的音乐网站百度收录提交申请网站
  • 如何做网站劫持网页免费制作网站
  • 网站模板 wordpressseo是什么意思的缩写
  • 网站底部导航淘宝网店的seo主要是什么
  • 做外贸建网站需要多少钱香港旺道旺国际集团
  • 个人工商户做网站要上税吗百度提交入口网站
  • 做游戏 网站网站搭建服务
  • 那个网站专门做婚纱相册广州seo网站排名