Skip to content

Commit

Permalink
Add more TLV and Tag methods
Browse files Browse the repository at this point in the history
  • Loading branch information
benallard committed Sep 22, 2011
1 parent 5e20c17 commit dab58bc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
29 changes: 26 additions & 3 deletions pythoncardx/framework/tlv/bertag.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def getInstance(bArray, bOff):

def init(self, bArray, bOff):
""" supposedly abstract """
self._tagClass = bArray[bOff] >> 6
self._tagClass = (bArray[bOff] & 0xff) >> 6
self._tagConstr = bool(bArray[bOff] & 0x20)
if (bArray[bOff] & 0x1f) == 0x1f:
self._tagNumber = 0
Expand Down Expand Up @@ -86,14 +86,37 @@ def _toBytesStatic(tagClass, isConstructed, tagNumber, outArray, bOff):

def _sizeBound(self):
return self._size

@staticmethod
def _sizeStatic(berTagArray, bOff):
tag = BERTag()
tag.init(berTagArray, bOff)
return tag.size()
size = NotAlwaysStatic('_sizeBound', '_sizeStatic')

def _isConstructedBound(self):
return self._tagConstr
@staticmethod
def _isConstructedStatic(berTagArray, bOff):
tag = BERTag.getInstance(berTagArray, bOff)
return tag.isConstructed()
isConstructed = NotAlwaysStatic('_isConstructedBound', '_isConstructedStatic')

def _tagClassBound(self):
return self._tagClass
@staticmethod
def _tagClassStatic(berTagArray, bOff):
tag = BERTag.getInstance(berTagArray, bOff)
return tag.tagClass()
tagClass = NotAlwaysStatic('_tagClassBound', '_tagClassStatic')

def _tagNumberBound(self):
return self._tagNumber
@staticmethod
def _tagNumberStatic(berTagArray, bOff):
tag = BERTag.getInstance(berTagArray, bOff)
return tag.tagNumber()
tagNumber = NotAlwaysStatic('_tagNumberBound', '_tagNumberStatic')

def __str__(self):
return '<BERTag: %d%s, %d>' % (self._tagClass, self._tagConstr and ', CONSTR' or '', self._tagNumber)

Expand Down Expand Up @@ -137,4 +160,4 @@ def init(self, param1, param2):
else:
self._tagClass = param1
self._tagNumber = param2

33 changes: 32 additions & 1 deletion pythoncardx/framework/tlv/bertlv.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def getInstance(bArray, bOff, bLen = None):
# call it will three arguments
bLen = BERTLV.getLength(bArray, bOff)
tag = BERTag.getInstance(bArray, bOff)
if tag._tagConstr:
if tag.isConstructed():
tlv = ConstructedBERTLV(0)
else:
tlv = PrimitiveBERTLV(0)
Expand Down Expand Up @@ -128,6 +128,32 @@ def _appendStatic(berTLVInArray, bTLVInOff, berTLVOutArray, bTLVOutOff):
return outtlv.toBytes(berTLVOutArray, bTLVOutOff)
append = NotAlwaysStatic('_appendBound', '_appendStatic')

def _find(self, tag, offset=0):
""" returns the offset of the found tag """
while offset < self._length:
tlv = BERTLV.getInstance(self._value, offset)
if tlv.getTag() == tag:
return offset
offset += tlv.size()
return -1

def _findBound(self, tag):
if tag is None:
# return the first one
return BERTLV.getInstance(self._value, 0)
offset = self._find(tag)
if offset == -1:
return None
return BERTLV.getInstance(self._value, offset)
@staticmethod
def _findStatic(berTLVArray, bTLVOff, berTagArray, bTagOff):
tlv = BERTLV.getInstance(berTLVArray, bTLVOff)
firstOff = bTLVOff + tlv.getTag().size() + getLengthLen(tlv.getLength())
if berTagArray is None:
# return the first one
return firstOff
return firstOff + tlv._find(BERTag.getInstance(berTagArray, bTagOff))
find = NotAlwaysStatic('_findBound', '_findStatic')

class PrimitiveBERTLV(BERTLV):
def __init__(self, numValueBytes):
Expand All @@ -145,3 +171,8 @@ def init(self, param1, *args):
bArray = param1
(bOff, bLen) = args
return BERTLV.init(self, bArray, bOff, bLen)

@staticmethod
def getValueOffset(berTLVArray, bTLVOff):
tlv = BERTLV.getInstance(berTLVArray, bTLVOff)
return bTLVOff + tlv.getTag().size() + getLengthLen(tlv.getLength())

0 comments on commit dab58bc

Please sign in to comment.