1
2 """smartcard.util package
3
4 __author__ = "http://www.gemalto.com"
5
6 Copyright 2001-2011 gemalto
7 Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
8
9 This file is part of pyscard.
10
11 pyscard is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or
14 (at your option) any later version.
15
16 pyscard is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with pyscard; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 """
25
26 PACK = 1
27 HEX = 2
28 UPPERCASE = 4
29 COMMA = 8
30
31
32 -def padd(bytelist, length, padding='FF'):
33 """ Padds a byte list with a constant byte value (default is x0FF)
34 bytelist: the byte list to padd
35 length: the total length of the resulting byte list;
36 no padding if length is smaller than the byte list length
37 padding: padding value (default is 0xff)
38
39 returns the padded bytelist
40 example:
41 padd(toBytes(\"3B 65 00 00 9C 11 01 01 03\"), 16) returns [0x3B, 0x65, 0, 0, 0x9C, 0x11, 1, 1, 3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
42 padd(toBytes(\"3B 65 00 00 9C 11 01 01 03\"), 8) returns [0x3B, 0x65, 0, 0, 0x9C, 0x11, 1, 1, 3 ]
43
44 """
45
46 if len(bytelist) < length:
47 for index in range(length - len(bytelist)):
48 bytelist.append(eval('0x' + padding))
49
50 return bytelist
51
52
54 """Returns a list of ASCII bytes from a string.
55
56 stringtoconvert: the string to convert into a byte list
57
58 returns a byte list of the ASCII codes of the string characters
59
60 toASCIIBytes() is the reverse of toASCIIString()
61
62 example:
63 toASCIIBytes("Number 101") returns[ 0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0x20, 0x31, 0x30, 0x31 ]
64 """
65
66 return map(ord, list(stringtoconvert))
67
68
70 """Returns a string representing a list of ASCII bytes.
71
72 bytelist: list of ASCII bytes to convert into a string
73
74 returns a string from the ASCII code list
75
76 toASCIIString() is the reverse of toASCIIBytes()
77
78 example:
79
80 toASCIIString([ 0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0x20, 0x31, 0x30, 0x31 ]) returns "Number 101")
81 """
82
83 return ''.join(map(chr, bytelist))
84
85
87 """Returns a list of bytes from a byte string
88
89 bytestring: a byte string of the format \"3B 65 00 00 9C 11 01 01 03\" or \"3B6500009C11010103\" or \"3B6500 009C1101 0103\"
90 """
91 from struct import unpack
92 import re
93 packedstring = ''.join(re.split('\W+', bytestring))
94 try:
95 return reduce(lambda x, y: x + [int(y, 16)], unpack('2s' * (len(packedstring) / 2), packedstring), [])
96 except:
97 raise TypeError, 'not a string representing a list of bytes'
98
99
100 """GSM3.38 character conversion table."""
101 __dic_GSM_3_38__ = {
102 '@': 0x00,
103 '£': 0x01,
104 '$': 0x02,
105 chr(0xA5): 0x03,
106 'è': 0x04,
107 'é': 0x05,
108 'ù': 0x06,
109 chr(0xEC): 0x07,
110 chr(0xF2): 0x08,
111 chr(0xC7): 0x09,
112 chr(0x0A): 0x0A,
113 chr(0xD8): 0x0B,
114 chr(0xF8): 0x0C,
115 chr(0x0D): 0x0D,
116 chr(0xC5): 0x0E,
117 chr(0xE5): 0x0F,
118 '_': 0x11,
119 chr(0xC6): 0x1C,
120 chr(0xE6): 0x1D,
121 chr(0xDF): 0x1E,
122 chr(0xC9): 0x1F,
123 ' ': 0x20,
124 '!': 0x21,
125 '\"': 0x22,
126 '#': 0x23,
127 '¤': 0x24,
128 chr(0xA1): 0x40,
129 chr(0xC4): 0x5B,
130 chr(0xE4): 0x7B,
131 chr(0xD6): 0x5C,
132 chr(0xF6): 0x7C,
133 chr(0xD1): 0x5D,
134 chr(0xF1): 0x7D,
135 chr(0xDC): 0x5E,
136 chr(0xFC): 0x7E,
137 chr(0xA7): 0x5F,
138 chr(0xBF): 0x60,
139 'à': 0x7F
140 }
141
142
144 """Returns a list of bytes from a string using GSM 3.38 conversion table.
145
146 stringtoconvert: string to convert
147
148 returns a list of bytes
149
150 example:
151 toGSM3_38Bytes("@ùPascal") returns [ 0x00, 0x06, 0x50, 0x61, 0x73, 0x63, 0x61, 0x6C ]
152 """
153
154 bytes = []
155 for char in stringtoconvert:
156 if ((char >= "%") and (char <= "?")):
157 bytes.append(ord(char))
158 elif ((char >= "A") and (char <= "Z")):
159 bytes.append(ord(char))
160 elif ((char >= "a") and (char <= "z")):
161 bytes.append(ord(char))
162 else:
163 bytes.append(int(__dic_GSM_3_38__[char]))
164 return bytes
165
166
168 """Returns an hex string representing bytes
169
170 bytes: a list of bytes to stringify, e.g. [59, 22, 148, 32, 2, 1, 0, 0, 13]
171 format: a logical OR of
172 COMMA: add a comma between bytes
173 HEX: add the 0x chars before bytes
174 UPPERCASE: use 0X before bytes (need HEX)
175 PACK: remove blanks
176
177 example:
178 bytes = [ 0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03 ]
179
180 toHexString(bytes) returns 3B 65 00 00 9C 11 01 01 03
181
182 toHexString(bytes, COMMA) returns 3B, 65, 00, 00, 9C, 11, 01, 01, 03
183 toHexString(bytes, HEX) returns 0x3B 0x65 0x00 0x00 0x9C 0x11 0x01 0x01 0x03
184 toHexString(bytes, HEX | COMMA) returns 0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03
185
186 toHexString(bytes, PACK) returns 3B6500009C11010103
187
188 toHexString(bytes, HEX | UPPERCASE) returns 0X3B 0X65 0X00 0X00 0X9C 0X11 0X01 0X01 0X03
189 toHexString(bytes, HEX | UPPERCASE | COMMA) returns 0X3B, 0X65, 0X00, 0X00, 0X9C, 0X11, 0X01, 0X01, 0X03
190 """
191
192 from string import rstrip
193
194 for byte in tuple(bytes):
195 pass
196
197 if type(bytes) is not list:
198 raise TypeError, 'not a list of bytes'
199
200 if bytes == None or bytes == []:
201 return ""
202 else:
203 pformat = "%-0.2X"
204 if COMMA & format:
205 pformat = pformat + ","
206 pformat = pformat + " "
207 if PACK & format:
208 pformat = rstrip(pformat)
209 if HEX & format:
210 if UPPERCASE & format:
211 pformat = "0X" + pformat
212 else:
213 pformat = "0x" + pformat
214 return rstrip(rstrip(reduce(lambda a, b: a + pformat % ((b + 256) % 256), [""] + bytes)), ',')
215
216
218 binstring = ""
219 for byte in hexlist:
220 binstring = binstring + chr(eval('0x%x' % byte))
221 return binstring
222
223
225 hexlist = []
226 for byte in binstring:
227 hexlist = hexlist + [ord(byte)]
228 return hexlist
229
230
233
234
237