gpt4 book ai didi

c - 在互联网上传输数据时如何使用asn.1?

转载 作者:行者123 更新时间:2023-11-30 16:36:29 25 4
gpt4 key购买 nike

asn.1 = 抽象语法注释一。我知道它可以定义某些类型的数据,通过使用 asn1c 编译器,我们可以将 Rectangle.asn 转换为一些 .c 和 .h 文件。然后我们可以使用这些 .c 和 .h 文件对不同形式的数据(ber、per、xer 等)进行编码或解码。但如何呢?我有一个名为 Rectangle.asn 的文件:

RectangleTest DEFINITIONS ::= BEGIN

Rectangle ::= SEQUENCE {
height INTEGER, -- Height of the rectangle
width INTEGER -- Width of the rectangle
}

END

asn1c Rectangle.asn命令带来了许多.c和.h文件。相关的文件如 Rectangle.h 和 Rectangle.c 并不像我关心的那么简单。为什么结构体是嵌套的?为什么有一个名为 INTEGER_t 的数据类型而不只是 int?当我只想发送序列化的 rect = {10, 20} 时,我很困惑,不知道如何真正使用这些 .c 和 .h 文件?请帮忙!Rectangle.h 和 Rectangle.c 上的一些代码

/* Rectangle */
typedef struct Rectangle {
INTEGER_t height;
INTEGER_t width;

/* Context for parsing across buffer boundaries */
asn_struct_ctx_t _asn_ctx;
} Rectangle_t;


#include "Rectangle.h"

static asn_TYPE_member_t asn_MBR_Rectangle_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct Rectangle, height),
(ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
0,
&asn_DEF_INTEGER,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"height"
},
{ ATF_NOFLAGS, 0, offsetof(struct Rectangle, width),
(ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
0,
&asn_DEF_INTEGER,
0, /* Defer constraints checking to the member type */
0, /* PER is not compiled, use -gen-PER */
0,
"width"
},
};
static ber_tlv_tag_t asn_DEF_Rectangle_tags_1[] = {
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static asn_TYPE_tag2member_t asn_MAP_Rectangle_tag2el_1[] = {
{ (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 0, 0, 1 }, /* height at 4 */
{ (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 1, -1, 0 } /* width at 6 */
};
static asn_SEQUENCE_specifics_t asn_SPC_Rectangle_specs_1 = {
sizeof(struct Rectangle),
offsetof(struct Rectangle, _asn_ctx),
asn_MAP_Rectangle_tag2el_1,
2, /* Count of tags in the map */
0, 0, 0, /* Optional elements (not needed) */
-1, /* Start extensions */
-1 /* Stop extensions */
};

最佳答案

您需要阅读有关 ASN.1 的更多信息。

它解决的问题是不同计算机上的东西并不完全相同。如果您想将数据从嵌入式 x86 DOS 系统(16 位小端整数)发送到 SPARC(32 位大端整数),那么您不能只使用 int两侧。

ASN.1 有两个主要部分:

  • 语言描述,即 ASN.1 语法
  • 应用语法将主机数据与编码数据相互转换的编码规则

asn1c web site 上有一些示例.

关于c - 在互联网上传输数据时如何使用asn.1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48463901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com