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