【问题标题】:What is this `enum`ish notation in RFC on TLS?RFC 中关于 TLS 的这个“枚举”符号是什么?
【发布时间】:2013-10-17 12:02:11
【问题描述】:

来自RFC 3749(传输层安全协议压缩方法):

  1. 压缩方法

    TLS [2] 在 第 6.1 和 7.4.1.2 节以及附录 A.4.1 和 A.6 节:

    enum { null(0), (255) } CompressionMethod;
    

我对 C 不是很熟悉,但我知道的足以将其标记为与 C enum 相似。但我无法理解的是null(0)(255) 部分。我似乎无法在任何地方找到括号和 null 在这种情况下的含义。

(我似乎很难想出一个(谷歌?)搜索模式,它包含比“rfc”、“null”、“c”、“括号”更普遍的东西,并且会引导我到其他地方而不是关于“空函数指针”或最基本的问题。)

那么这些符号在语法上是什么意思?

  • 为什么括号里是255?

  • 为什么null 看起来像一个函数调用?

这甚至应该是C吗?还是它是跨 RFC 共享的通用符号?如果是 C,是不是特定于 enum

这与enum { 0, 255 } CompressionMethod;enum { NULL, 255 } CompressionMethod; 有何不同?

【问题讨论】:

    标签: c enums notation rfc


    【解决方案1】:

    你在这里可能有点过度推理:)

    你应该已经引用了你的引用之后的行:

    which allows for later specification of up to 256 different
    compression methods.
    

    这已经解释了该行的含义。现在,如果您将[2] 跟踪到引用列表,您会注意到它引用了RFC 2246。该文件包含以下段落:

    4. Presentation language
    
    This document deals with the formatting of data in an external   
    representation. The following very basic and somewhat casually   
    defined presentation syntax will be used. The syntax draws from   
    several sources in its structure. Although it resembles the   
    programming language "C" in its syntax and XDR [XDR] in both its   
    syntax and intent, it would be risky to draw too many parallels. The  
    purpose of this presentation language is to document TLS only, not to 
    have general application beyond that particular goal.
    

    因此,该 RFC 的作者似乎从熟悉的元素中炮制了一种简单的语法来简化 RFC 主题的表示,即 TLS。对于枚举,它们指定 4.5 中使用的语言:

    4.5. Enumerateds
    
       An additional sparse data type is available called enum. A field of
       type enum can only assume the values declared in the definition.
       Each definition is a different type. Only enumerateds of the same
       type may be assigned or compared. Every element of an enumerated must
       be assigned a value, as demonstrated in the following example.  Since
       the elements of the enumerated are not ordered, they can be assigned
       any unique value, in any order.
    
           enum { e1(v1), e2(v2), ... , en(vn) [[, (n)]] } Te;
    
       Enumerateds occupy as much space in the byte stream as would its
       maximal defined ordinal value. The following definition would cause
       one byte to be used to carry fields of type Color.
    
           enum { red(3), blue(5), white(7) } Color;
    
       One may optionally specify a value without its associated tag to
       force the width definition without defining a superfluous element.
       In the following example, Taste will consume two bytes in the data
       stream but can only assume the values 1, 2 or 4.
    
           enum { sweet(1), sour(2), bitter(4), (32000) } Taste;
    
       The names of the elements of an enumeration are scoped within the
       defined type. In the first example, a fully qualified reference to
       the second element of the enumeration would be Color.blue. Such
       qualification is not required if the target of the assignment is well
       specified.
    
           Color color = Color.blue;     /* overspecified, legal */
           Color color = blue;           /* correct, type implicit */
    
       For enumerateds that are never converted to external representation,
       the numerical information may be omitted.
    
           enum { low, medium, high } Amount;
    

    【讨论】:

    • 你可能有点过分推理了 :) 你不是第一个告诉我这个的,但这些问题就像我脑袋里的饥饿小鸡!一个人根本无法专注于噪音......:D
    • 好吧,我真诚地希望我的回答至少能让这个饥饿的小妞沉默 :) 更重要的是,我还展示了从您的原始问题回到源头的步骤 - give a man a fish 和其他东西......跨度>
    【解决方案2】:

    它的意思是CompressionMethod.null 的值是0,然后255 slots 被保留:

    which allows for later specification of up to 256 different
    

    压缩方法

    【讨论】:

      【解决方案3】:

      (255) 通知您该字段的大小。因此,对于编码,您知道需要多少字节。如果是(400),则需要2 个字节来为compressionMethod.null0x00 + 0x00 指定0。因为 255 可以表示为 1 个字节,所以您只需要 0x00

      本质上,它让您知道枚举字段的大小。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-09
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 2010-09-28
        • 1970-01-01
        • 2021-10-21
        相关资源
        最近更新 更多