diff options
author | hploetz <hploetz@f711b948-2313-0410-aaa9-d29f33439f0b> | 2006-06-16 21:54:35 +0000 |
---|---|---|
committer | hploetz <hploetz@f711b948-2313-0410-aaa9-d29f33439f0b> | 2006-06-16 21:54:35 +0000 |
commit | 4ae6e05dcc246c48c1a4256a8177148a77e648e6 (patch) | |
tree | ea061df280456838efb82ecb5ccb7c684bdacf89 /cards | |
parent | 2112e830fde7b064e1d01123851d3f40c3e32140 (diff) |
refactored ls code to a building blocks module
git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@90 f711b948-2313-0410-aaa9-d29f33439f0b
Diffstat (limited to 'cards')
-rw-r--r-- | cards/building_blocks.py | 85 | ||||
-rw-r--r-- | cards/tcos_card.py | 99 |
2 files changed, 94 insertions, 90 deletions
diff --git a/cards/building_blocks.py b/cards/building_blocks.py new file mode 100644 index 0000000..48d0a7f --- /dev/null +++ b/cards/building_blocks.py @@ -0,0 +1,85 @@ +"This module holds some commonly used card functionality that is not ISO" + +from utils import C_APDU, R_APDU +import utils, TLV_utils + +class Card_with_80_aa: + def list_x(self, x): + "Get a list of x objects, where x is one of 1 (DFs) or 2 (EFs) or 3 (DFs and EFs)" + result = self.send_apdu(C_APDU(self.APDU_LIST_X, p1=x)) + + tail = result.data + result_list = [] + while len(tail) > 0: + head, tail = tail[:2], tail[2:] + result_list.append(head) + return result_list + + def cmd_listdirs(self): + "List DFs in current DF" + result = self.list_x(1) + print "DFs: " + ", ".join([utils.hexdump(a, short=True) for a in result]) + + def cmd_listfiles(self): + "List EFs in current DF" + result = self.list_x(2) + print "EFs: " + ", ".join([utils.hexdump(a, short=True) for a in result]) + + 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 = Card_with_80_aa._find_recursive(search_tag, value) + if ret is not None: return ret + return None + _find_recursive = staticmethod(_find_recursive) + + _ls_l_template = "%(name)-12s\t%(type)3s\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 + + if "-l" in options: + print self._ls_l_template % {"name": "Name", "type": "Type", "size": "Size"} + dirs.sort() + files.sort() + 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 = self._str_to_long(self._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] ) diff --git a/cards/tcos_card.py b/cards/tcos_card.py index 7b6497b..3e43cc5 100644 --- a/cards/tcos_card.py +++ b/cards/tcos_card.py @@ -1,98 +1,17 @@ import utils, TLV_utils from iso_7816_4_card import * +import building_blocks -class TCOS_Card(ISO_7816_4_Card): +class TCOS_Card(ISO_7816_4_Card,building_blocks.Card_with_80_aa): DRIVER_NAME = "TCOS" APDU_LIST_X = C_APDU("\x80\xaa\x01\x00\x00") - - def list_x(self, x): - "Get a list of x objects, where x is one of 1 (DFs) or 2 (EFs)" - result = self.send_apdu(C_APDU(self.APDU_LIST_X, p1=x)) - - tail = result.data - result_list = [] - while len(tail) > 0: - head, tail = tail[:2], tail[2:] - result_list.append(head) - return result_list - - def cmd_listdirs(self): - "List DFs in current DF" - result = self.list_x(1) - print "DFs: " + ", ".join([utils.hexdump(a, short=True) for a in result]) - - def cmd_listfiles(self): - "List EFs in current DF" - result = self.list_x(2) - print "EFs: " + ", ".join([utils.hexdump(a, short=True) for a in result]) - - 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)-12s\t%(type)3s\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 - - if "-l" in options: - print self._ls_l_template % {"name": "Name", "type": "Type", "size": "Size"} - dirs.sort() - files.sort() - 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( [ + ATRS = [ ("3bba96008131865d0064057b0203318090007d", None), - ] ) + ] - COMMANDS = dict(ISO_7816_4_Card.COMMANDS) - COMMANDS.update( { - "list_dirs": cmd_listdirs, - "list_files": cmd_listfiles, - "ls": cmd_list, - } ) + COMMANDS = { + "list_dirs": building_blocks.Card_with_80_aa.cmd_listdirs, + "list_files": building_blocks.Card_with_80_aa.cmd_listfiles, + "ls": building_blocks.Card_with_80_aa.cmd_list, + } |