|
12 | 12 | parser.add_argument('-v', '--version', nargs='?', dest='version', type=str, |
13 | 13 | const='255.255.255', default='255.255.255', help='Build version to encode') |
14 | 14 | args = parser.parse_args() |
15 | | - |
16 | 15 | version = args.version.strip('v') |
17 | | - |
18 | | - print('Patching file ' + args.patch + ' using the applet version from ' + args.source + ' and the additional build version ' + version) |
| 16 | + print('info: Patching file: ' + args.patch) |
| 17 | + print('info: Applet version from: ' + args.source) |
| 18 | + print('info: Additional build version: ' + version) |
19 | 19 |
|
| 20 | + # Parse version source file |
| 21 | + _, ext = os.path.splitext(args.source) |
20 | 22 | if(not os.path.isfile(args.source)): |
21 | 23 | print('error: source file ' + args.source + 'does not exist') |
22 | 24 | exit(1) |
23 | | - |
24 | | - # Parse version source file |
25 | | - _, ext = os.path.splitext(args.source) |
26 | 25 | with open(args.source, 'r') as f: |
27 | 26 | config = f.read() |
28 | 27 | if(ext == '.xml'): |
29 | 28 | # Ant XML file |
30 | | - matches = re.findall("cap.*version[\s='\"]*([^\s'\"]*)", config, flags=re.DOTALL) |
| 29 | + matches = re.search("cap.*version[\s='\"]*([^\s'\"]*)", config, flags=re.DOTALL) |
31 | 30 | elif(ext == '.gradle'): |
32 | 31 | # Gradle config |
33 | | - matches = re.findall("javacard {.*version[\s=']*([^\s']*)", config, flags=re.DOTALL) |
| 32 | + matches = re.search("javacard {.*version[\s=']*([^\s']*)", config, flags=re.DOTALL) |
34 | 33 | else: |
35 | 34 | print('error: Unknown source file format: ' + ext) |
36 | | - |
37 | | - if(len(matches) == 0): |
| 35 | + if(matches == None): |
38 | 36 | print("error: Cannot find version in config file") |
39 | 37 | exit(1) |
40 | | - appversion = matches[0] |
41 | | - print("info: Found source version: " + appversion) |
| 38 | + appversion = matches.group(1) |
| 39 | + print("info: Found applet version: " + appversion) |
| 40 | + |
| 41 | + # Generate patch |
| 42 | + appversion = appversion.split('.') |
| 43 | + version = version.split('.') |
| 44 | + patch = ''' |
| 45 | + byte[] ver_buf = apdu.getBuffer(); |
| 46 | + if(ver_buf[ISO7816.OFFSET_INS] == (byte) 0xF4 && ver_buf[ISO7816.OFFSET_P1] == (byte) 0x99 && ver_buf[ISO7816.OFFSET_P2] == (byte) 0x99) {{ |
| 47 | + short ver_le = apdu.setOutgoing(); |
| 48 | + short ver_len = (short) 5; |
| 49 | + ver_len = ver_le > (short) 0 ? (ver_le > ver_len ? ver_len : ver_le) : ver_len; |
| 50 | + ver_buf[0] = (byte) {}; |
| 51 | + ver_buf[1] = (byte) {}; |
| 52 | + ver_buf[2] = (byte) {}; |
| 53 | + ver_buf[3] = (byte) {}; |
| 54 | + ver_buf[4] = (byte) {}; |
| 55 | + apdu.setOutgoingLength(ver_len); |
| 56 | + apdu.sendBytes((short) 0, ver_len); |
| 57 | + ver_buf = null; |
| 58 | + return; |
| 59 | + }} else {{ |
| 60 | + ver_buf = null; |
| 61 | + }} |
| 62 | + '''.format(appversion[0], appversion[1], version[0], version[1], version[2]) |
42 | 63 |
|
| 64 | + # Find patch location and apply patch |
43 | 65 | if(not os.path.isfile(args.patch)): |
44 | 66 | print('error: patch file ' + args.patch + 'does not exist') |
45 | 67 | exit(1) |
| 68 | + with open(args.patch, 'r') as f: |
| 69 | + patchtarget = f.read() |
| 70 | + match = re.search("void\s*process\s*\((?:final)?\s*APDU apdu\)\s*(?:throws ISOException)?\s*{", patchtarget, flags=re.DOTALL) |
| 71 | + if(match == None): |
| 72 | + print("error: Cannot find process method in patch file") |
| 73 | + exit(1) |
| 74 | + offset = match.end() |
| 75 | + patchtarget = patchtarget[:offset] + patch + patchtarget[offset:] |
| 76 | + with open(args.patch, 'w') as f: |
| 77 | + f.write(patchtarget) |
| 78 | + |
| 79 | + print("info: Done patching") |
0 commit comments