Skip to content

Commit

Permalink
Improve int and TLV support
Browse files Browse the repository at this point in the history
  • Loading branch information
benallard committed Sep 22, 2011
1 parent 4288fe6 commit 5e20c17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
27 changes: 20 additions & 7 deletions pythoncardx/framework/tlv/bertlv.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,33 @@ def _getLength(bArray):
else:
return (1, bArray[0])

def getLengthLen(length):
""" how much btes are needed to write 'length' """
if length > 127:
return (length // 127) + 1
else:
return 1

class BERTLV(object):
def __init__(self):
self._tag = None
self._length = 0
self._value = None
self._size = 0

@staticmethod
def getInstance(bArray, bOff, bLen):
def getInstance(bArray, bOff, bLen = None):
if bLen is None:
# This is a trick of mine, the JC framework will anyway always
# call it will three arguments
bLen = BERTLV.getLength(bArray, bOff)
tag = BERTag.getInstance(bArray, bOff)
if tag._tagConstr:
tlv = ConstructedBERTLV(0)
else:
tlv = PrimitiveBERTLV(0)
bOff += tag.size()
l, length = _getLength(bArray[bOff:])
# this calls the init method of the child class, not BERTLV
tlv.init(tag, bArray, bOff + l, bLen - tag.size() - l)
return tlv

Expand All @@ -61,7 +72,7 @@ def _getLengthStatic(berTLVArray, bOff):
getLength = NotAlwaysStatic('_getLengthBound', '_getLengthStatic')

def size(self):
return self._size
return self._tag.size() + getLengthLen(self._length) + self._length

def _toBytesBound(self, outBuf, bOff):
bLen = self._tag.toBytes(outBuf, bOff)
Expand Down Expand Up @@ -109,10 +120,12 @@ def _appendBound(self, aTLV):
return self.size()
@staticmethod
def _appendStatic(berTLVInArray, bTLVInOff, berTLVOutArray, bTLVOutOff):
# that's a funny one, actually, it is a CopyArrayAtomic (or not)
# but just with the length parameter missing ...
length = BERTLV.getLength(berTLVInArray, bTLVInOff)
Util.arrayCopy(berTLVInArray, bTLVInOff, berTLVOutArray, bTLVOutOff, length)
""" append a piece to the value of the out TLV, making it even more
constructed """
intlv = BERTLV.getInstance(berTLVInArray, bTLVInOff)
outtlv = BERTLV.getInstance(berTLVOutArray, bTLVOutOff)
outtlv.append(intlv)
return outtlv.toBytes(berTLVOutArray, bTLVOutOff)
append = NotAlwaysStatic('_appendBound', '_appendStatic')


Expand Down
24 changes: 13 additions & 11 deletions pythoncardx/framework/util/intx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@ def signed(value, depth):
def s4(i):
return signed(i, 4)

def s1(b):
return signed(b, 1)

def sanatize(array):
for i in range(len(array)):
array[i] = signed(array[i], 1)
array[i] = s1(array[i])

class JCint(object):

@staticmethod

def getInt(bArray, bOff):
i = bArray[bOff] << 24 + \
bArray[bOff + 1] << 16 + \
bArray[bOff + 2] << 8 + \
i = (bArray[bOff] << 24) + \
(bArray[bOff + 1] << 16) + \
(bArray[bOff + 2] << 8) + \
bArray[bOff + 3]
return s4(i)

@staticmethod
def makeInt(*args):
if len(args) == 4:
(b1, b2, b3, b4) = args
i = b1 << 24 + b2 << 16 + b3 << 8 + b4
i = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4
return s4(i)
else:
(s1, s2) = args
i = s1 << 16 + s2
i = (s1 << 16) + s2
return s4(i)

@staticmethod
Expand All @@ -48,8 +50,8 @@ def makeTransientIntArray(length, event):
def setInt(bArray, bOff, iValue1, iValue2):
# ints are transmited as two params ...
iVal = JCint.makeInt(iValue1, iValue2)
bArray[bOff] = (iVal >> 24) & 0xff
bArray[bOff + 1] = iVal >> 16 & 0xff
bArray[bOff + 2] = iVal >> 8 & 0xff
bArray[bOff + 3] = iVal & 0xff
bArray[bOff] = s1((iVal >> 24) & 0xff)
bArray[bOff + 1] = s1((iVal >> 16) & 0xff)
bArray[bOff + 2] = s1((iVal >> 8) & 0xff)
bArray[bOff + 3] = s1(iVal & 0xff)
return bOff + 4

0 comments on commit 5e20c17

Please sign in to comment.