From b1f2a56881a1892b17bdd0fa53caba832c2d64ff Mon Sep 17 00:00:00 2001 From: hploetz Date: Wed, 24 May 2006 07:58:55 +0000 Subject: [PATCH] ls -l (still ugly) git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@75 f711b948-2313-0410-aaa9-d29f33439f0b --- TLV_utils.py | 16 ++++++++++++ cards/tcos_card.py | 59 +++++++++++++++++++++++++++++++++++++++++---- cyberflex-shell.e3p | 28 ++++++++++++++++++--- 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/TLV_utils.py b/TLV_utils.py index 25f9207..5f76946 100644 --- a/TLV_utils.py +++ b/TLV_utils.py @@ -169,6 +169,7 @@ tags = { 0x62: (recurse, "File Control Parameters", context_FCP), 0x64: (recurse, "File Management Data", context_FMD), 0x6F: (recurse, "File Control Information", context_FCI), + 0x80: (number, "Number of data bytes in the file, excluding structural information"), 0x81: (number, "Number of data bytes in the file, including structural information"), 0x82: (decode_file_descriptor_byte, "File descriptor byte"), @@ -264,6 +265,21 @@ def decode(data, context = None, level = 0): return "\n".join(result) +def unpack(data): + result = [] + while len(data) > 0: + if ord(data[0]) in (0x00, 0xFF): + data = data[1:] + continue + + ber_class, constructed, tag, length, value, data = tlv_unpack(data) + if not constructed: + result.append( (tag, length, value) ) + else: + result.append( (tag, length, unpack(value)) ) + + return result + if __name__ == "__main__": test = binascii.unhexlify("".join(("6f 2b 83 02 2f 00 81 02 01 00 82 03 05 41 26 85" \ +"02 01 00 86 18 60 00 00 00 ff ff b2 00 00 00 ff" \ diff --git a/cards/tcos_card.py b/cards/tcos_card.py index fad0850..0145e82 100644 --- a/cards/tcos_card.py +++ b/cards/tcos_card.py @@ -1,4 +1,4 @@ -import utils +import utils, TLV_utils from iso_7816_4_card import * class TCOS_Card(ISO_7816_4_Card): @@ -26,13 +26,62 @@ class TCOS_Card(ISO_7816_4_Card): result = self.list_x(2) print "EFs: " + ", ".join([utils.hexdump(a, short=True) for a in result]) - def cmd_list(self): - "List all EFs and DFs in current DF" + def _str_to_long(value): + num = 0 + for i in value: + num = num * 256 + num = num + ord(i) + return num + _str_to_long = staticmethod(_str_to_long) + + def _find_recursive(search_tag, data): + while len(data) > 0: + if ord(data[0]) in (0x00, 0xFF): + data = data[1:] + continue + + ber_class, constructed, tag, length, value, data = TLV_utils.tlv_unpack(data) + if not constructed: + if tag == search_tag: + return value + else: + ret = TCOS_Card._find_recursive(search_tag, value) + if ret is not None: return ret + return None + _find_recursive = staticmethod(_find_recursive) + + _ls_l_template = "%(name)10s\t%(type)s\t%(size)4s" + def cmd_list(self, *options): + """List all EFs and DFs in current DF. Call with -l for verbose information (caution: deselects current file)""" dirs = self.list_x(1) files = self.list_x(2) + + if "-l" in options: + response_DF = {} + response_EF = {} + for DF in dirs: + response_DF[DF] = self.select_file(0x01, 0x00, DF) + self.select_file(0x03, 0x00, "") + for EF in files: + response_EF[EF] = self.select_file(0x02, 0x00, EF) + self.sw_changed = False - print "\n".join( ["[%s]" % utils.hexdump(a, short=True) for a in dirs] - + [" %s " % utils.hexdump(a, short=True) for a in files] ) + + if "-l" in options: + print self._ls_l_template % {"name": "Name", "type": "Type", "size": "Size"} + for FID in dirs: + name = "[" + utils.hexdump(FID, short=True) + "]" + type = "DF" + size = "" + print self._ls_l_template % locals() + for FID in files: + name = " " + utils.hexdump(FID, short=True) + " " + type = "EF" + size = TCOS_Card._str_to_long(TCOS_Card._find_recursive(0x81, response_EF[FID].data)) + print self._ls_l_template % locals() + else: + print "\n".join( ["[%s]" % utils.hexdump(a, short=True) for a in dirs] + + [" %s " % utils.hexdump(a, short=True) for a in files] ) ATRS = list(Card.ATRS) ATRS.extend( [ diff --git a/cyberflex-shell.e3p b/cyberflex-shell.e3p index 002aab9..771685a 100644 --- a/cyberflex-shell.e3p +++ b/cyberflex-shell.e3p @@ -1,9 +1,11 @@ - + - - - + + + + Python + Qt 0.1 Henryk Plötz @@ -37,6 +39,24 @@ shell.py + + TLV_utils.py + + + brutefid.py + + + cards + iso_7816_4_card.py + + + cards + tcos_card.py + + + cards + starcos_card.py +