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

How to use asn.1 while transmit data in the internet?

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 位小端 int)发送到 SPARC(32 位大端 int),那么您不能只在两边都使用 int

A​​SN.1 有两个主要部分:

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

asn1c web site上有一些例子。