diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/utils.py b/virtualsmartcard/src/vpicc/virtualsmartcard/utils.py index ead1f2e..0041aa6 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/utils.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/utils.py @@ -290,20 +290,24 @@ class C_APDU(APDU): apdu = apdu + [0] * max(4-len(apdu), 0) self.CLA, self.INS, self.P1, self.P2 = apdu[:4] # case 1, 2, 3, 4 + self.__extended_length = False if len(apdu) == 4: # case 1 self.data = "" elif (len(apdu) >= 7) and (apdu[5] == 0): # extended length apdu - self.Lc = (apdu[5]<<8) + apdu[6] + self.__extended_length = True if len(apdu) == 7: # case 2 extended length - self.data = "" - elif len(apdu) == 7 + self.Lc: # case 3 extended length - self.data = apdu[7:] - elif len(apdu) == 7 + self.Lc + 3: # case 4 extended length - self.data = apdu[7:-3] self.Le = (apdu[-2]<<8) + apdu[-1] - else: - raise ValueError, "Invalid Lc value. Is %s, should be %s or %s" % (self.Lc, - 7 + self.Lc, 7 + self.Lc + 3) + self.data = "" + else: # case 3, 4 extended length + self.Lc = (apdu[5]<<8) + apdu[6] + if len(apdu) == 7 + self.Lc: # case 3 extended length + self.data = apdu[7:] + elif len(apdu) == 7 + self.Lc + 3: # case 4 extended length + self.Le = (apdu[-2]<<8) + apdu[-1] + self.data = apdu[7:-3] + else: + raise ValueError, "Invalid Lc value. Is %s, should be %s or %s" % (self.Lc, + 7 + self.Lc, 7 + self.Lc + 3) else: # short apdu if len(apdu) == 5: # case 2 short apdu self.Le = apdu[-1] @@ -315,9 +319,9 @@ class C_APDU(APDU): elif len(apdu) == 5 + self.Lc + 1: # case 4 self.data = apdu[5:-1] self.Le = apdu[-1] - else: - raise ValueError, "Invalid Lc value. Is %s, should be %s or %s" % (self.Lc, - 5 + self.Lc, 5 + self.Lc + 1) + else: + raise ValueError, "Invalid Lc value. Is %s, should be %s or %s" % (self.Lc, + 5 + self.Lc, 5 + self.Lc + 1) CLA = _make_byte_property("CLA"); cla = CLA INS = _make_byte_property("INS"); ins = INS @@ -326,6 +330,16 @@ class C_APDU(APDU): Lc = _make_byte_property("Lc"); lc = Lc Le = _make_byte_property("Le"); le = Le + @property + def effective_Le(self): + if hasattr(self, "_Le") and (self.Le == 0): + if self.__extended_length: + return 0xffff + else: + return 0xff + else: + return self.Le + def _format_fields(self): fields = ["CLA", "INS", "P1", "P2"] if self.Lc > 0: