二、標籤模式
EXPLICIT tagging:----------------------默認的模式,即默認使用顯式模式,它會在已存在的標籤之前添加自定義的值
– Default tagging mode
– EXPLICIT tag is added in front of the existing tag
• IMPLICIT tagging:-------------------------------------,覆蓋已存在的標籤的值
– IMPLICIT tag replaces an existing tag
明確定義的標籤:
Primary use of tags
• Avoid ambiguous descriptions
• Example:
Unambiguous ::= SEQUENCE {
val1 [0] INTEGER OPTIONAL,
val2 INTEGER OPTIONAL
}
Tagging distinguishes val1 and val2 encodings
結構類型標註規則:
• Tags values must be sequential from 0------------------標籤值必須是連續的從0開始的
• Tagging class must not be UNIVERSAL--------------------標籤類不能是UNIVERSAL
InvalidSeq ::= SEQUENCE {
version INTEGER,
name [UNIVERSAL 0] PrintableString
}
這不是一個有效的聲明
透明類型標註規則
OpenType and CHOICE types are always explicitly
tagged (even when not mentioned in an IMPLICIT
TAGS module header)------開放類型和選擇類型總是顯式的標籤(即使在一個使用隱式標籤的模塊使用)
• CHOICE inner elements tags must be distinct-----選擇類型內部的元素標籤必須不同
Generic ::= [0] IMPLICIT OpenType---------這不是有效的,(因爲只能使用顯式標籤)
多個標籤
asn1提供多標籤(或者遞歸標籤)
TaggedInt ::= [1] EXPLICIT [0] IMPLICIT INTEGER
Equivalent to:
TaggedInt ::= [1] EXPLICIT Int
Int ::= [0] IMPLICIT INTEGER
隱藏的含糊不清的聲明
HiddenSequence ::= SEQUENCE {
first Seq OPTIONAL,
second SeqOfInt
}
Seq ::= SEQUENCE {
val INTEGER
}
SeqOfInt ::= SEQUENCE OF INTEGER
When first is not present and second holds a
single INTEGER value, the encoding is successfully
generated…-------------當第一個不在,第二個只有一個integer值,可以正確的編碼
… but the decoder cannot determine whether first or
second is encoded!--------但是解碼者不能區分是第一個還是第二個編碼
修正後:
HiddenSequence ::= SEQUENCE {
first Seq OPTIONAL,
second [0] SeqOf
}
標籤執行中的錯誤
TrickySequence ::= SEQUENCE {
first Seq OPTIONAL,
second SeqOfBool
}
Seq ::= SEQUENCE {
val INTEGER
}
SeqOfBool ::= SEQUENCE OF BOOLEAN
first and second hold different inner elements
types (INTEGER and BOOLEAN)…
… but when first is absent, decoders have to go inside
the SEQUENCE OF and fail to decode the first
INTEGER value to know that the encoding does not
enclose first…-----------但是當第一個不在,解碼者必須進入sequence of 標籤,解釋第一個integer失敗,不能夠知道沒有將第一個元素添加到編碼中。
修正:
TrickySequence ::= SEQUENCE {
first Seq OPTIONAL,
second [0] SeqOfBool
}
Sequence是有序的,而Set是無序的