Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (C) 2012 Christian Heimes (christian@python.org) |
| 3 | # Licensed to PSF under a Contributor Agreement. |
| 4 | # |
| 5 | # cleanup Keccak sources |
| 6 | |
| 7 | import os |
| 8 | import re |
| 9 | |
| 10 | CPP1 = re.compile("^//(.*)") |
| 11 | CPP2 = re.compile("\ //(.*)") |
| 12 | |
| 13 | STATICS = ("void ", "int ", "HashReturn ", "const UINT64 ", "UINT16 ") |
| 14 | |
| 15 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 16 | KECCAK = os.path.join(HERE, "keccak") |
| 17 | |
| 18 | def getfiles(): |
| 19 | for name in os.listdir(KECCAK): |
| 20 | name = os.path.join(KECCAK, name) |
| 21 | if os.path.isfile(name): |
| 22 | yield name |
| 23 | |
| 24 | def cleanup(f): |
| 25 | buf = [] |
| 26 | for line in f: |
| 27 | # mark all functions and global data as static |
| 28 | if line.startswith(STATICS): |
| 29 | buf.append("static " + line) |
| 30 | continue |
| 31 | # remove UINT64 typedef, we have our own |
| 32 | if line.startswith("typedef unsigned long long int"): |
| 33 | buf.append("/* %s */\n" % line.strip()) |
| 34 | continue |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 35 | # remove #include "brg_endian.h" |
| 36 | if "brg_endian.h" in line: |
| 37 | buf.append("/* %s */\n" % line.strip()) |
| 38 | continue |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 39 | # transform C++ comments into ANSI C comments |
| 40 | line = CPP1.sub(r"/* \1 */", line) |
| 41 | line = CPP2.sub(r" /* \1 */", line) |
| 42 | buf.append(line) |
| 43 | return "".join(buf) |
| 44 | |
| 45 | for name in getfiles(): |
| 46 | with open(name) as f: |
| 47 | res = cleanup(f) |
| 48 | with open(name, "w") as f: |
| 49 | f.write(res) |