aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <holger@moiji-mobile.com>2014-02-09 22:04:04 +0100
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2014-02-09 22:04:04 +0100
commitabbad4a7afb4ee93a559e7a89a82235c43378c71 (patch)
tree5719fd0f400240a569bba2160b4cab6388b3a64c
parent564087a32d4243d8d988edf025e3eb31f0287656 (diff)
tlv: Only write a tag if it was forced or the type is optional
-rw-r--r--core/TLV.st15
-rw-r--r--core/TLVTests.st17
2 files changed, 27 insertions, 5 deletions
diff --git a/core/TLV.st b/core/TLV.st
index e7785f9..381ec5b 100644
--- a/core/TLV.st
+++ b/core/TLV.st
@@ -132,7 +132,7 @@ Object subclass: TLVDescription [
hasLength [
<category: 'access'>
- ^ type = self class tagLengthValue
+ ^ type = self class tagLengthValue or: [type = self class lengthValue]
]
isLen16 [
@@ -150,6 +150,16 @@ Object subclass: TLVDescription [
^ force_tag
]
+ hasTag [
+ <category: 'access'>
+ ^type ~= self class lengthValue and: [type ~= self class valueOnly]
+ ]
+
+ needsTag [
+ <category: 'access'>
+ ^force_tag or: [self hasTag and: [self isOptional]].
+ ]
+
presenceKind: aKind [
<category: 'creation'>
"Is this required, optional, variable?"
@@ -286,8 +296,7 @@ Object subclass: TLVParserBase [
"Now write it"
val isNil ifFalse: [
- aMsg
- putByte: attr tag.
+ attr needsTag ifTrue: [aMsg putByte: attr tag].
val writeOn: aMsg with: attr.
].
]
diff --git a/core/TLVTests.st b/core/TLVTests.st
index 73c6b5a..c2675d6 100644
--- a/core/TLVTests.st
+++ b/core/TLVTests.st
@@ -42,8 +42,21 @@ TestCase subclass: TLVDescriptionTest [
self assert: tlv tag = 16r23.
tlv beLV.
- self assert: tlv typeKind = #lv.
+ self
+ assert: tlv typeKind equals: #lv;
+ assert: tlv hasLength;
+ deny: tlv hasTag.
+
tlv beTLV.
- self assert: tlv typeKind = #tlv.
+ self
+ assert: tlv typeKind equals: #tlv;
+ assert: tlv hasLength;
+ assert: tlv hasTag.
+
+ tlv beTagOnly.
+ self
+ assert: tlv typeKind equals: #tagOnly;
+ assert: tlv hasTag;
+ deny: tlv hasLength.
]
]