Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / import_gcry.py
blob2b3322d3a12107a4215fe677a424abc5254154da
1 #*
2 #* GRUB -- GRand Unified Bootloader
3 #* Copyright (C) 2009 Free Software Foundation, Inc.
4 #*
5 #* GRUB is free software: you can redistribute it and/or modify
6 #* it under the terms of the GNU General Public License as published by
7 #* the Free Software Foundation, either version 3 of the License, or
8 #* (at your option) any later version.
9 #*
10 #* GRUB is distributed in the hope that it will be useful,
11 #* but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #* GNU General Public License for more details.
15 #* You should have received a copy of the GNU General Public License
16 #* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 import re
20 import sys
21 import os
22 import datetime
23 import codecs
25 if len (sys.argv) < 3:
26 print ("Usage: %s SOURCE DESTINATION" % sys.argv[0])
27 exit (0)
28 indir = sys.argv[1]
29 outdir = sys.argv[2]
31 basedir = os.path.join (outdir, "lib/libgcrypt-grub")
32 try:
33 os.makedirs (basedir)
34 except:
35 print ("WARNING: %s already exists" % basedir)
36 cipher_dir_in = os.path.join (indir, "cipher")
37 cipher_dir_out = os.path.join (basedir, "cipher")
38 try:
39 os.makedirs (cipher_dir_out)
40 except:
41 print ("WARNING: %s already exists" % cipher_dir_out)
42 mpidir = os.path.join (basedir, "mpi")
43 try:
44 os.makedirs (mpidir)
45 except:
46 print ("WARNING: %s already exists" % mpidir)
48 srcdir = os.path.join (basedir, "src")
49 try:
50 os.makedirs (srcdir)
51 except:
52 print ("WARNING: %s already exists" % srcdir)
54 cipher_files = sorted (os.listdir (cipher_dir_in))
55 conf = codecs.open (os.path.join ("grub-core", "Makefile.gcry.def"), "w", "utf-8")
56 conf.write ("AutoGen definitions Makefile.tpl;\n\n")
57 confutil = codecs.open ("Makefile.utilgcry.def", "w", "utf-8")
58 confutil.write ("AutoGen definitions Makefile.tpl;\n\n")
59 confutil.write ("library = {\n");
60 confutil.write (" name = libgrubgcry.a;\n");
61 confutil.write (" cflags = '$(CFLAGS_GCRY)';\n");
62 confutil.write (" cppflags = '$(CPPFLAGS_GCRY)';\n");
63 confutil.write (" extra_dist = grub-core/lib/libgcrypt-grub/cipher/ChangeLog;\n");
64 confutil.write ("\n");
65 chlog = ""
66 modules_sym_md = []
68 # Strictly speaking CRC32/CRC24 work on bytes so this value should be 1
69 # But libgcrypt uses 64. Let's keep the value for compatibility. Since
70 # noone uses CRC24/CRC32 for HMAC this is no problem
71 mdblocksizes = {"_gcry_digest_spec_crc32" : 64,
72 "_gcry_digest_spec_crc32_rfc1510" : 64,
73 "_gcry_digest_spec_crc24_rfc2440" : 64,
74 "_gcry_digest_spec_md4" : 64,
75 "_gcry_digest_spec_md5" : 64,
76 "_gcry_digest_spec_rmd160" : 64,
77 "_gcry_digest_spec_sha1" : 64,
78 "_gcry_digest_spec_sha224" : 64,
79 "_gcry_digest_spec_sha256" : 64,
80 "_gcry_digest_spec_sha384" : 128,
81 "_gcry_digest_spec_sha512" : 128,
82 "_gcry_digest_spec_tiger" : 64,
83 "_gcry_digest_spec_tiger1" : 64,
84 "_gcry_digest_spec_tiger2" : 64,
85 "_gcry_digest_spec_whirlpool" : 64}
87 cryptolist = codecs.open (os.path.join (cipher_dir_out, "crypto.lst"), "w", "utf-8")
89 # rijndael is the only cipher using aliases. So no need for mangling, just
90 # hardcode it
91 cryptolist.write ("RIJNDAEL: gcry_rijndael\n");
92 cryptolist.write ("RIJNDAEL192: gcry_rijndael\n");
93 cryptolist.write ("RIJNDAEL256: gcry_rijndael\n");
94 cryptolist.write ("AES128: gcry_rijndael\n");
95 cryptolist.write ("AES-128: gcry_rijndael\n");
96 cryptolist.write ("AES-192: gcry_rijndael\n");
97 cryptolist.write ("AES-256: gcry_rijndael\n");
99 cryptolist.write ("ADLER32: adler32\n");
100 cryptolist.write ("CRC64: crc64\n");
102 for cipher_file in cipher_files:
103 infile = os.path.join (cipher_dir_in, cipher_file)
104 outfile = os.path.join (cipher_dir_out, cipher_file)
105 if cipher_file == "ChangeLog" or cipher_file == "ChangeLog-2011":
106 continue
107 chlognew = " * %s" % cipher_file
108 if re.match ("(Manifest|Makefile\.am|ac\.c|cipher\.c|hash-common\.c|hmac-tests\.c|md\.c|pubkey\.c)$", cipher_file) or cipher_file == "kdf.c" or cipher_file == "elgamal.c" or cipher_file == "primegen.c" or cipher_file == "ecc.c" or cipher_file == "test-getrusage.c":
109 chlog = "%s%s: Removed\n" % (chlog, chlognew)
110 continue
111 # Autogenerated files. Not even worth mentionning in ChangeLog
112 if re.match ("Makefile\.in$", cipher_file):
113 continue
114 nch = False
115 if re.match (".*\.[ch]$", cipher_file):
116 isc = re.match (".*\.c$", cipher_file)
117 f = codecs.open (infile, "r", "utf-8")
118 fw = codecs.open (outfile, "w", "utf-8")
119 fw.write ("/* This file was automatically imported with \n")
120 fw.write (" import_gcry.py. Please don't modify it */\n")
121 fw.write ("#include <grub/dl.h>\n")
122 if cipher_file == "camellia.h":
123 fw.write ("#include <grub/misc.h>\n")
124 fw.write ("void camellia_setup128(const unsigned char *key, grub_uint32_t *subkey);\n")
125 fw.write ("void camellia_setup192(const unsigned char *key, grub_uint32_t *subkey);\n")
126 fw.write ("void camellia_setup256(const unsigned char *key, grub_uint32_t *subkey);\n")
127 fw.write ("void camellia_encrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
128 fw.write ("void camellia_encrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
129 fw.write ("void camellia_encrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
130 fw.write ("void camellia_decrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
131 fw.write ("void camellia_decrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
132 fw.write ("void camellia_decrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
133 fw.write ("#define memcpy grub_memcpy\n")
134 # Whole libgcrypt is distributed under GPLv3+ or compatible
135 if isc:
136 fw.write ("GRUB_MOD_LICENSE (\"GPLv3+\");\n")
138 ciphernames = []
139 mdnames = []
140 mdctxsizes = []
141 pknames = []
142 hold = False
143 skip = 0
144 skip2 = False
145 ismd = False
146 mdarg = 0
147 ispk = False
148 iscipher = False
149 iscryptostart = False
150 iscomma = False
151 isglue = False
152 skip_statement = False
153 if isc:
154 modname = cipher_file [0:len(cipher_file) - 2]
155 if re.match (".*-glue$", modname):
156 modname = modname.replace ("-glue", "")
157 isglue = True
158 modname = "gcry_%s" % modname
159 for line in f:
160 line = line
161 if skip_statement:
162 if not re.search (";", line) is None:
163 skip_statement = False
164 continue
165 if skip > 0:
166 if line[0] == "}":
167 skip = skip - 1
168 continue
169 if skip2:
170 if not re.search (" *};", line) is None:
171 skip2 = False
172 continue
173 if iscryptostart:
174 s = re.search (" *\"([A-Z0-9_a-z]*)\"", line)
175 if not s is None:
176 sg = s.groups()[0]
177 cryptolist.write (("%s: %s\n") % (sg, modname))
178 iscryptostart = False
179 if ismd:
180 spl = line.split (",")
181 if mdarg + len (spl) > 9 and mdarg <= 9 and ("sizeof" in spl[9-mdarg]):
182 mdctxsizes.append (spl[9-mdarg].lstrip ().rstrip())
183 mdarg = mdarg + len (spl) - 1
184 if ismd or iscipher or ispk:
185 if not re.search (" *};", line) is None:
186 if not iscomma:
187 fw.write (" ,\n")
188 fw.write ("#ifdef GRUB_UTIL\n");
189 fw.write (" .modname = \"%s\",\n" % modname);
190 fw.write ("#endif\n");
191 if ismd:
192 if not (mdname in mdblocksizes):
193 print ("ERROR: Unknown digest blocksize: %s\n"
194 % mdname)
195 exit (1)
196 fw.write (" .blocksize = %s\n"
197 % mdblocksizes [mdname])
198 ismd = False
199 mdarg = 0
200 iscipher = False
201 ispk = False
202 iscomma = not re.search (",$", line) is None
203 # Used only for selftests.
204 m = re.match ("(static byte|static unsigned char) (weak_keys_chksum)\[[0-9]*\] =", line)
205 if not m is None:
206 skip = 1
207 fname = m.groups ()[1]
208 chmsg = "(%s): Removed." % fname
209 if nch:
210 chlognew = "%s\n %s" % (chlognew, chmsg)
211 else:
212 chlognew = "%s %s" % (chlognew, chmsg)
213 nch = True
214 continue
215 if hold:
216 hold = False
217 # We're optimising for size and exclude anything needing good
218 # randomness.
219 if not re.match ("(run_selftests|selftest|_gcry_aes_c.._..c|_gcry_[a-z0-9]*_hash_buffer|tripledes_set2keys|do_tripledes_set_extra_info|_gcry_rmd160_mixblock|serpent_test|dsa_generate_ext|test_keys|gen_k|sign|gen_x931_parm_xp|generate_x931|generate_key|dsa_generate|dsa_sign|ecc_sign|generate|generate_fips186|_gcry_register_pk_dsa_progress|_gcry_register_pk_ecc_progress|progress|scanval|ec2os|ecc_generate_ext|ecc_generate|compute_keygrip|ecc_get_param|_gcry_register_pk_dsa_progress|gen_x931_parm_xp|gen_x931_parm_xi|rsa_decrypt|rsa_sign|rsa_generate_ext|rsa_generate|secret|check_exponent|rsa_blind|rsa_unblind|extract_a_from_sexp|curve_free|curve_copy|point_set)", line) is None:
221 skip = 1
222 if not re.match ("selftest", line) is None and cipher_file == "idea.c":
223 skip = 3
225 if not re.match ("serpent_test", line) is None:
226 fw.write ("static const char *serpent_test (void) { return 0; }\n");
227 if not re.match ("dsa_generate", line) is None:
228 fw.write ("#define dsa_generate 0");
229 if not re.match ("ecc_generate", line) is None:
230 fw.write ("#define ecc_generate 0");
231 if not re.match ("rsa_generate ", line) is None:
232 fw.write ("#define rsa_generate 0");
233 if not re.match ("rsa_sign", line) is None:
234 fw.write ("#define rsa_sign 0");
235 if not re.match ("rsa_decrypt", line) is None:
236 fw.write ("#define rsa_decrypt 0");
237 if not re.match ("dsa_sign", line) is None:
238 fw.write ("#define dsa_sign 0");
239 if not re.match ("ecc_sign", line) is None:
240 fw.write ("#define ecc_sign 0");
241 fname = re.match ("[a-zA-Z0-9_]*", line).group ()
242 chmsg = "(%s): Removed." % fname
243 if nch:
244 chlognew = "%s\n %s" % (chlognew, chmsg)
245 else:
246 chlognew = "%s %s" % (chlognew, chmsg)
247 nch = True
248 continue
249 else:
250 fw.write (holdline)
251 m = re.match ("# *include <(.*)>", line)
252 if not m is None:
253 chmsg = "Removed including of %s" % m.groups ()[0]
254 if nch:
255 chlognew = "%s\n %s" % (chlognew, chmsg)
256 else:
257 chlognew = "%s: %s" % (chlognew, chmsg)
258 nch = True
259 continue
260 m = re.match ("gcry_cipher_spec_t", line)
261 if isc and not m is None:
262 assert (not ismd)
263 assert (not ispk)
264 assert (not iscipher)
265 assert (not iscryptostart)
266 ciphername = line [len ("gcry_cipher_spec_t"):].strip ()
267 ciphername = re.match("[a-zA-Z0-9_]*",ciphername).group ()
268 ciphernames.append (ciphername)
269 iscipher = True
270 iscryptostart = True
272 m = re.match ("gcry_pk_spec_t", line)
273 if isc and not m is None:
274 assert (not ismd)
275 assert (not ispk)
276 assert (not iscipher)
277 assert (not iscryptostart)
278 pkname = line [len ("gcry_pk_spec_t"):].strip ()
279 pkname = re.match("[a-zA-Z0-9_]*",pkname).group ()
280 pknames.append (pkname)
281 ispk = True
282 iscryptostart = True
284 m = re.match ("gcry_md_spec_t", line)
285 if isc and not m is None:
286 assert (not ismd)
287 assert (not ispk)
288 assert (not iscipher)
289 assert (not iscryptostart)
290 mdname = line [len ("gcry_md_spec_t"):].strip ()
291 mdname = re.match("[a-zA-Z0-9_]*",mdname).group ()
292 mdnames.append (mdname)
293 ismd = True
294 mdarg = 0
295 iscryptostart = True
296 m = re.match ("static const char \*selftest.*;$", line)
297 if not m is None:
298 fname = line[len ("static const char \*"):]
299 fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
300 chmsg = "(%s): Removed declaration." % fname
301 if nch:
302 chlognew = "%s\n %s" % (chlognew, chmsg)
303 else:
304 chlognew = "%s %s" % (chlognew, chmsg)
305 nch = True
306 continue
307 m = re.match ("static gcry_mpi_t gen_k .*;$", line)
308 if not m is None:
309 chmsg = "(gen_k): Removed declaration."
310 if nch:
311 chlognew = "%s\n %s" % (chlognew, chmsg)
312 else:
313 chlognew = "%s %s" % (chlognew, chmsg)
314 nch = True
315 continue
316 m = re.match ("static (int|void) test_keys .*;$", line)
317 if not m is None:
318 chmsg = "(test_keys): Removed declaration."
319 if nch:
320 chlognew = "%s\n %s" % (chlognew, chmsg)
321 else:
322 chlognew = "%s %s" % (chlognew, chmsg)
323 nch = True
324 continue
325 m = re.match ("static void secret .*;$", line)
326 if not m is None:
327 chmsg = "(secret): Removed declaration."
328 if nch:
329 chlognew = "%s\n %s" % (chlognew, chmsg)
330 else:
331 chlognew = "%s %s" % (chlognew, chmsg)
332 nch = True
333 continue
334 m = re.match ("static void \(\*progress_cb\).*;$", line)
335 if not m is None:
336 chmsg = "(progress_cb): Removed declaration."
337 if nch:
338 chlognew = "%s\n %s" % (chlognew, chmsg)
339 else:
340 chlognew = "%s %s" % (chlognew, chmsg)
341 nch = True
342 continue
343 m = re.match ("static void \*progress_cb_data.*;$", line)
344 if not m is None:
345 chmsg = "(progress_cb): Removed declaration."
346 if nch:
347 chlognew = "%s\n %s" % (chlognew, chmsg)
348 else:
349 chlognew = "%s %s" % (chlognew, chmsg)
350 nch = True
351 continue
353 m = re.match ("(static const char( |)\*|static gpg_err_code_t|void|static int|static gcry_err_code_t|static gcry_mpi_t|static void|void|static elliptic_curve_t) *$", line)
354 if not m is None:
355 hold = True
356 holdline = line
357 continue
358 m = re.match ("static int tripledes_set2keys \(.*\);", line)
359 if not m is None:
360 continue
361 m = re.match ("static int tripledes_set3keys \(.*\);", line)
362 if not m is None:
363 continue
364 m = re.match ("static int tripledes_set2keys \(", line)
365 if not m is None:
366 skip_statement = True
367 continue
368 m = re.match ("static int tripledes_set3keys \(", line)
369 if not m is None:
370 skip_statement = True
371 continue
372 m = re.match ("static const char sample_secret_key", line)
373 if not m is None:
374 skip_statement = True
375 continue
376 m = re.match ("static const char sample_public_key", line)
377 if not m is None:
378 skip_statement = True
379 continue
380 m = re.match ("static void sign|static gpg_err_code_t sign|static gpg_err_code_t generate",
381 line)
382 if not m is None:
383 skip_statement = True
384 continue
386 m = re.match ("cipher_extra_spec_t", line)
387 if isc and not m is None:
388 skip2 = True
389 fname = line[len ("cipher_extra_spec_t "):]
390 fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
391 chmsg = "(%s): Removed." % fname
392 if nch:
393 chlognew = "%s\n %s" % (chlognew, chmsg)
394 else:
395 chlognew = "%s %s" % (chlognew, chmsg)
396 nch = True
397 continue
398 m = re.match ("pk_extra_spec_t", line)
399 if isc and not m is None:
400 skip2 = True
401 fname = line[len ("pk_extra_spec_t "):]
402 fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
403 chmsg = "(%s): Removed." % fname
404 if nch:
405 chlognew = "%s\n %s" % (chlognew, chmsg)
406 else:
407 chlognew = "%s %s" % (chlognew, chmsg)
408 nch = True
409 continue
410 m = re.match ("md_extra_spec_t", line)
411 if isc and not m is None:
412 skip2 = True
413 fname = line[len ("md_extra_spec_t "):]
414 fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
415 chmsg = "(%s): Removed." % fname
416 if nch:
417 chlognew = "%s\n %s" % (chlognew, chmsg)
418 else:
419 chlognew = "%s %s" % (chlognew, chmsg)
420 nch = True
421 continue
422 fw.write (line)
423 if len (ciphernames) > 0 or len (mdnames) > 0 or len (pknames) > 0:
424 if isglue:
425 modfiles = "lib/libgcrypt-grub/cipher/%s lib/libgcrypt-grub/cipher/%s" \
426 % (cipher_file, cipher_file.replace ("-glue.c", ".c"))
427 else:
428 modfiles = "lib/libgcrypt-grub/cipher/%s" % cipher_file
429 if len (ciphernames) > 0 or len (mdnames) > 0:
430 modules_sym_md.append (modname)
431 chmsg = "(GRUB_MOD_INIT(%s)): New function\n" % modname
432 if nch:
433 chlognew = "%s\n %s" % (chlognew, chmsg)
434 else:
435 chlognew = "%s%s" % (chlognew, chmsg)
436 nch = True
437 fw.write ("\n\nGRUB_MOD_INIT(%s)\n" % modname)
438 fw.write ("{\n")
439 for ciphername in ciphernames:
440 chmsg = "Register cipher %s" % ciphername
441 chlognew = "%s\n %s" % (chlognew, chmsg)
442 fw.write (" grub_cipher_register (&%s);\n" % ciphername)
443 for ctxsize in mdctxsizes:
444 fw.write (" COMPILE_TIME_ASSERT(%s <= GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);\n" % ctxsize)
445 for mdname in mdnames:
446 chmsg = "Register digest %s" % mdname
447 chlognew = "%s\n %s" % (chlognew, chmsg)
448 fw.write (" grub_md_register (&%s);\n" % mdname)
449 for pkname in pknames:
450 chmsg = "Register pk %s" % mdname
451 chlognew = "%s\n %s" % (chlognew, chmsg)
452 fw.write (" grub_crypto_pk_%s = &%s;\n"
453 % (pkname.replace ("_gcry_pubkey_spec_", ""), pkname))
454 fw.write ("}")
455 chmsg = "(GRUB_MOD_FINI(%s)): New function\n" % modname
456 chlognew = "%s\n %s" % (chlognew, chmsg)
457 fw.write ("\n\nGRUB_MOD_FINI(%s)\n" % modname)
458 fw.write ("{\n")
459 for ciphername in ciphernames:
460 chmsg = "Unregister cipher %s" % ciphername
461 chlognew = "%s\n %s" % (chlognew, chmsg)
462 fw.write (" grub_cipher_unregister (&%s);\n" % ciphername)
463 for mdname in mdnames:
464 chmsg = "Unregister MD %s" % mdname
465 chlognew = "%s\n %s" % (chlognew, chmsg)
466 fw.write (" grub_md_unregister (&%s);\n" % mdname)
467 for pkname in pknames:
468 chmsg = "Unregister pk %s" % mdname
469 chlognew = "%s\n %s" % (chlognew, chmsg)
470 fw.write (" grub_crypto_pk_%s = 0;\n"
471 % (pkname.replace ("_gcry_pubkey_spec_", "")))
472 fw.write ("}\n")
473 conf.write ("module = {\n")
474 conf.write (" name = %s;\n" % modname)
475 for src in modfiles.split():
476 conf.write (" common = %s;\n" % src)
477 if len (ciphernames) > 0 or len (mdnames) > 0:
478 confutil.write (" common = grub-core/%s;\n" % src)
479 if modname == "gcry_ecc":
480 conf.write (" common = lib/libgcrypt-grub/mpi/ec.c;\n")
481 conf.write (" cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls -Wno-sign-compare';\n")
482 elif modname == "gcry_rijndael" or modname == "gcry_md4" or modname == "gcry_md5" or modname == "gcry_rmd160" or modname == "gcry_sha1" or modname == "gcry_sha256" or modname == "gcry_sha512" or modname == "gcry_tiger":
483 # Alignment checked by hand
484 conf.write (" cflags = '$(CFLAGS_GCRY) -Wno-cast-align';\n");
485 else:
486 conf.write (" cflags = '$(CFLAGS_GCRY)';\n");
487 conf.write (" cppflags = '$(CPPFLAGS_GCRY)';\n");
488 conf.write ("};\n\n")
489 f.close ()
490 fw.close ()
491 if nch:
492 chlog = "%s%s\n" % (chlog, chlognew)
493 elif isc and cipher_file != "camellia.c":
494 print ("WARNING: C file isn't a module: %s" % cipher_file)
495 f.close ()
496 fw.close ()
497 os.remove (outfile)
498 chlog = "%s\n * %s: Removed" % (chlog, cipher_file)
499 continue
500 chlog = "%s%sSkipped unknown file\n" % (chlog, chlognew)
501 print ("WARNING: unknown file %s" % cipher_file)
503 cryptolist.close ()
505 for src in sorted (os.listdir (os.path.join (indir, "src"))):
506 if src == "versioninfo.rc.in" or src == "ath.c" or src == "ChangeLog-2011" \
507 or src == "dumpsexp.c" or src == "fips.c" or src == "gcrypt.h.in" \
508 or src == "gcryptrnd.c"or src == "getrandom.c" \
509 or src == "global.c" or src == "hmac256.c" \
510 or src == "hwfeatures.c" or src == "libgcrypt-config.in" \
511 or src == "libgcrypt.def" or src == "libgcrypt.m4" \
512 or src == "libgcrypt.vers" or src == "Makefile.am" \
513 or src == "Manifest" or src == "misc.c" \
514 or src == "missing-string.c" or src == "module.c" \
515 or src == "secmem.c" or src == "sexp.c" \
516 or src == "stdmem.c" or src == "visibility.c":
517 continue
518 outfile = os.path.join (basedir, "src", src)
519 infile = os.path.join (indir, "src", src)
520 if os.path.isdir (infile):
521 continue
522 fw = codecs.open (outfile, "w", "utf-8")
523 if src == "gcrypt-module.h":
524 fw.close ()
525 continue
526 if src == "visibility.h":
527 fw.write ("# include <grub/gcrypt/gcrypt.h>\n")
528 fw.close ()
529 continue
530 f = codecs.open (infile, "r", "utf-8")
531 if src == "types.h":
532 fw.write (f.read ().replace ("float f;", "").replace ("double g;", ""))
533 f.close ()
534 fw.close ()
535 continue
537 if src == "g10lib.h":
538 fw.write (f.read ().replace ("(printf,f,a)", "(__printf__,f,a)"))
539 f.close ()
540 fw.close ()
541 continue
543 fw.write (f.read ())
544 f.close ()
545 fw.close ()
547 for src in sorted (os.listdir (os.path.join (indir, "mpi"))):
548 if src == "config.links" or src == "ChangeLog-2011" \
549 or src == "mpi-scan.c" or src == "Manifest" \
550 or src == "Makefile.am":
551 continue
552 infile = os.path.join (indir, "mpi", src)
553 outfile = os.path.join (basedir, "mpi", src)
554 if os.path.isdir (infile):
555 continue
556 f = codecs.open (infile, "r", "utf-8")
557 fw = codecs.open (outfile, "w", "utf-8")
558 fw.write ("/* This file was automatically imported with \n")
559 fw.write (" import_gcry.py. Please don't modify it */\n")
560 hold = False
561 skip = 0
562 for line in f:
563 if skip > 0:
564 if line[0] == "}":
565 skip = skip - 1
566 continue
567 if hold:
568 hold = False
569 # We're optimising for size and exclude anything needing good
570 # randomness.
571 if not re.match ("(_gcry_mpi_get_hw_config|gcry_mpi_randomize)", line) is None:
572 skip = 1
573 continue
574 else:
575 fw.write (holdline)
576 m = re.match ("(const char( |)\*|void) *$", line)
577 if not m is None:
578 hold = True
579 holdline = line
580 continue
581 m = re.match ("#include \"mod-source-info\.h\"", line)
582 if not m is None:
583 continue
584 fw.write (line)
586 chlog = "%s * crypto.lst: New file.\n" % chlog
588 outfile = os.path.join (cipher_dir_out, "types.h")
589 fw=codecs.open (outfile, "w", "utf-8")
590 fw.write ("#include <grub/types.h>\n")
591 fw.write ("#include <cipher_wrap.h>\n")
592 chlog = "%s * types.h: New file.\n" % chlog
593 fw.close ()
595 outfile = os.path.join (cipher_dir_out, "memory.h")
596 fw=codecs.open (outfile, "w", "utf-8")
597 fw.write ("#include <cipher_wrap.h>\n")
598 chlog = "%s * memory.h: New file.\n" % chlog
599 fw.close ()
602 outfile = os.path.join (cipher_dir_out, "cipher.h")
603 fw=codecs.open (outfile, "w", "utf-8")
604 fw.write ("#include <grub/crypto.h>\n")
605 fw.write ("#include <cipher_wrap.h>\n")
606 chlog = "%s * cipher.h: Likewise.\n" % chlog
607 fw.close ()
609 outfile = os.path.join (cipher_dir_out, "g10lib.h")
610 fw=codecs.open (outfile, "w", "utf-8")
611 fw.write ("#include <cipher_wrap.h>\n")
612 chlog = "%s * g10lib.h: Likewise.\n" % chlog
613 fw.close ()
615 infile = os.path.join (cipher_dir_in, "ChangeLog")
616 outfile = os.path.join (cipher_dir_out, "ChangeLog")
618 conf.close ();
620 initfile = codecs.open (os.path.join (cipher_dir_out, "init.c"), "w", "utf-8")
621 initfile.write ("#include <grub/crypto.h>\n")
622 for module in modules_sym_md:
623 initfile.write ("extern void grub_%s_init (void);\n" % module)
624 initfile.write ("extern void grub_%s_fini (void);\n" % module)
625 initfile.write ("\n")
626 initfile.write ("void\n")
627 initfile.write ("grub_gcry_init_all (void)\n")
628 initfile.write ("{\n")
629 for module in modules_sym_md:
630 initfile.write (" grub_%s_init ();\n" % module)
631 initfile.write ("}\n")
632 initfile.write ("\n")
633 initfile.write ("void\n")
634 initfile.write ("grub_gcry_fini_all (void)\n")
635 initfile.write ("{\n")
636 for module in modules_sym_md:
637 initfile.write (" grub_%s_fini ();\n" % module)
638 initfile.write ("}\n")
639 initfile.close ()
641 confutil.write (" common = grub-core/lib/libgcrypt-grub/cipher/init.c;\n")
642 confutil.write ("};\n");
643 confutil.close ();
646 f=codecs.open (infile, "r", "utf-8")
647 fw=codecs.open (outfile, "w", "utf-8")
648 dt = datetime.date.today ()
649 fw.write ("%04d-%02d-%02d Automatic import tool\n" % \
650 (dt.year,dt.month, dt.day))
651 fw.write ("\n")
652 fw.write (" Imported ciphers to GRUB\n")
653 fw.write ("\n")
654 fw.write (chlog)
655 fw.write ("\n")
656 for line in f:
657 fw.write (line)
658 f.close ()
659 fw.close ()