Home | History | Annotate | Download | only in _sha3
      1 #!/usr/bin/env python
      2 # Copyright (C) 2012   Christian Heimes (christian (at] 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(r"\ //(.*)")
     12 
     13 STATICS = ("void ", "int ", "HashReturn ",
     14            "const UINT64 ", "UINT16 ", "    int prefix##")
     15 
     16 HERE = os.path.dirname(os.path.abspath(__file__))
     17 KECCAK = os.path.join(HERE, "kcp")
     18 
     19 def getfiles():
     20     for name in os.listdir(KECCAK):
     21         name = os.path.join(KECCAK, name)
     22         if os.path.isfile(name):
     23             yield name
     24 
     25 def cleanup(f):
     26     buf = []
     27     for line in f:
     28         # mark all functions and global data as static
     29         #if line.startswith(STATICS):
     30         #    buf.append("static " + line)
     31         #    continue
     32         # remove UINT64 typedef, we have our own
     33         if line.startswith("typedef unsigned long long int"):
     34             buf.append("/* %s */\n" % line.strip())
     35             continue
     36         ## remove #include "brg_endian.h"
     37         if "brg_endian.h" in line:
     38             buf.append("/* %s */\n" % line.strip())
     39             continue
     40         # transform C++ comments into ANSI C comments
     41         line = CPP1.sub(r"/*\1 */\n", line)
     42         line = CPP2.sub(r" /*\1 */\n", line)
     43         buf.append(line)
     44     return "".join(buf)
     45 
     46 for name in getfiles():
     47     with open(name) as f:
     48         res = cleanup(f)
     49     with open(name, "w") as f:
     50         f.write(res)
     51