Skip to content

Commit

Permalink
APDU: Resolve some inconsistencies in handling of signed/unsigned in …
Browse files Browse the repository at this point in the history
…the lengths
  • Loading branch information
benallard committed Jan 28, 2013
1 parent a465988 commit 4d11aee
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
12 changes: 7 additions & 5 deletions pythoncard/framework/apdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

from pythoncard.framework import APDUException, ISOException, ISO7816

from pythoncard.utils import u1

# there can only be one (1) APDU at a time ...
_current = None

Expand Down Expand Up @@ -115,10 +117,10 @@ def __init__(self, bytesarr):
def __getInLengths(self):
""" return a tuple (value, length of value) """
if self.__buffer[ISO7816.OFFSET_LC] == 0:
return (self.__buffer[ISO7816.OFFSET_LC + 1] * 256 +
self.__buffer[ISO7816.OFFSET_LC + 2]), 3
return (u1(self.__buffer[ISO7816.OFFSET_LC + 1]) * 256 +
u1(self.__buffer[ISO7816.OFFSET_LC + 2])), 3
else:
return self.__buffer[ISO7816.OFFSET_LC], 1
return u1(self.__buffer[ISO7816.OFFSET_LC]), 1

def __getOutLengths(self, length):
""" return a tuple (value, length of value) """
Expand All @@ -133,8 +135,8 @@ def __getOutLengths(self, length):
else:
if length != ISO7816.OFFSET_LC:
if self.__buffer[ISO7816.OFFSET_LC] == 0:
return self.__buffer[length-1] * 256 + self.__buffer[length], 2
return self.__buffer[length], 1
return u1(self.__buffer[length-1]) * 256 + u1(self.__buffer[length]), 2
return u1(self.__buffer[length]), 1


def getBuffer(self):
Expand Down
26 changes: 5 additions & 21 deletions pythoncard/framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from python.lang import ArrayIndexOutOfBoundsException
from pythoncard.framework import JCSystem

from pythoncard.utils import s1, s2

def arrayCompare(src, srcOff, dest, destOff, length):
try:
for i in range(length):
Expand Down Expand Up @@ -46,7 +48,7 @@ def arrayFillNonAtomic(bArray, bOff, bLen, bValue):

def makeShort(b1, b2):
""" a short is signed ... """
return _signed2(((b1 << 8) & 0xFF00) | (b2 & 0xFF))
return s2(((b1 << 8) & 0xFF00) | (b2 & 0xFF))

def getShort(bArray, bOff):
try:
Expand All @@ -55,28 +57,10 @@ def getShort(bArray, bOff):
raise ArrayIndexOutOfBoundsException()

def setShort(bArray, bOff, sValue):
b1 = _signed1((sValue & 0xFF00) >> 8)
b2 = _signed1(sValue & 0xFF)
b1 = s1((sValue & 0xFF00) >> 8)
b2 = s1(sValue & 0xFF)
try:
bArray[bOff] = b1
bArray[bOff + 1] = b2
except IndexError:
raise ArrayIndexOutOfBoundsException()

# Taken from CAPRunner

def _signed(value, depth):
"""
return the signed value of the number on the specified depth
"""
mask = (1 << (depth*8)) - 1
if value > ((1 << (depth*8)-1) - 1):
return -(~(value-1) & mask)
else:
return value

def _signed1(value):
return _signed(value, 1)

def _signed2(value):
return _signed(value, 2)
5 changes: 5 additions & 0 deletions pythoncard/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def signed(value, depth):
def s1(value):
return signed(value, 1)

def s2(value):
return signed(value, 2)

def u1(value):
return value & 0xff

class NotAlwaysStatic(object):
""" This makes a function both static and not static
Expand Down

0 comments on commit 4d11aee

Please sign in to comment.