Skip to content

Commit df3a2b7

Browse files
authored
Fixed bad TagSet initializer at OctetString encoder (#107)
localize explicit tag slitting to chunked mode at OctetString and BitString encoders The inner chunks tagging logic is to be researched -- I'm not certain it works as it supposed to
1 parent 136890e commit df3a2b7

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

CHANGES.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
Revision 0.4.2, released 23-11-2017
3+
-----------------------------------
4+
5+
- Fixed explicit tag splitting in chunked encoding mode at
6+
OctetString and BitString encoders
7+
28
Revision 0.4.1, released 23-11-2017
39
-----------------------------------
410

pyasn1/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22

33
# http://www.python.org/dev/peps/pep-0396/
4-
__version__ = '0.4.1'
4+
__version__ = '0.4.2'
55

66
if sys.version_info[:2] < (2, 4):
77
raise RuntimeError('PyASN1 requires Python 2.4 or later')

pyasn1/codec/ber/encoder.py

+42-15
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,15 @@ def encodeValue(self, value, asn1Spec, encodeFun, **options):
157157
substrate = alignedValue.asOctets()
158158
return int2oct(len(substrate) * 8 - valueLength) + substrate, False, True
159159

160-
tagSet = value.tagSet
160+
baseTag = value.tagSet.baseTag
161161

162162
# strip off explicit tags
163-
alignedValue = alignedValue.clone(
164-
tagSet=tag.TagSet(tagSet.baseTag, tagSet.baseTag)
165-
)
163+
if baseTag:
164+
tagSet = tag.TagSet(baseTag, baseTag)
165+
else:
166+
tagSet = tag.TagSet()
167+
168+
alignedValue = alignedValue.clone(tagSet=tagSet)
166169

167170
stop = 0
168171
substrate = null
@@ -175,28 +178,52 @@ def encodeValue(self, value, asn1Spec, encodeFun, **options):
175178

176179

177180
class OctetStringEncoder(AbstractItemEncoder):
181+
178182
def encodeValue(self, value, asn1Spec, encodeFun, **options):
179-
if asn1Spec is None:
180-
# will strip off explicit tags
181-
tagSet = value.tagSet
182-
asn1Spec = value.clone(tagSet=tag.TagSet(tagSet.baseTag, tagSet.baseTag))
183183

184-
value = value.asOctets()
184+
if asn1Spec is None:
185+
substrate = value.asOctets()
185186

186187
elif not isOctetsType(value):
187-
# will strip off explicit tags
188-
tagSet = asn1Spec.tagSet
189-
asn1Spec = asn1Spec.clone(tagSet=tag.TagSet(tagSet.baseTag, tagSet.baseTag))
188+
substrate = asn1Spec.clone(value).asOctets()
190189

191-
value = asn1Spec.clone(value).asOctets()
190+
else:
191+
substrate = value
192192

193193
maxChunkSize = options.get('maxChunkSize', 0)
194-
if not maxChunkSize or len(value) <= maxChunkSize:
195-
return value, False, True
194+
195+
if not maxChunkSize or len(substrate) <= maxChunkSize:
196+
return substrate, False, True
196197

197198
else:
199+
200+
# strip off explicit tags for inner chunks
201+
202+
if asn1Spec is None:
203+
baseTag = value.tagSet.baseTag
204+
205+
# strip off explicit tags
206+
if baseTag:
207+
tagSet = tag.TagSet(baseTag, baseTag)
208+
else:
209+
tagSet = tag.TagSet()
210+
211+
asn1Spec = value.clone(tagSet=tagSet)
212+
213+
elif not isOctetsType(value):
214+
baseTag = asn1Spec.tagSet.baseTag
215+
216+
# strip off explicit tags
217+
if baseTag:
218+
tagSet = tag.TagSet(baseTag, baseTag)
219+
else:
220+
tagSet = tag.TagSet()
221+
222+
asn1Spec = asn1Spec.clone(tagSet=tagSet)
223+
198224
pos = 0
199225
substrate = null
226+
200227
while True:
201228
chunk = value[pos:pos + maxChunkSize]
202229
if not chunk:

0 commit comments

Comments
 (0)