blob: aabcb0442cefd11675768e754f26dd2b5a8e5f2c [file] [log] [blame]
Christian Heimes4a0270d2012-10-06 02:23:36 +02001#!/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
7import os
8import re
9
10CPP1 = re.compile("^//(.*)")
11CPP2 = re.compile("\ //(.*)")
12
13STATICS = ("void ", "int ", "HashReturn ", "const UINT64 ", "UINT16 ")
14
15HERE = os.path.dirname(os.path.abspath(__file__))
16KECCAK = os.path.join(HERE, "keccak")
17
18def 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
24def 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 Heimes743e0cd2012-10-17 23:52:17 +020035 # remove #include "brg_endian.h"
36 if "brg_endian.h" in line:
37 buf.append("/* %s */\n" % line.strip())
38 continue
Christian Heimes4a0270d2012-10-06 02:23:36 +020039 # 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
45for name in getfiles():
46 with open(name) as f:
47 res = cleanup(f)
48 with open(name, "w") as f:
49 f.write(res)