1 1.1 elric #!/usr/local/bin/python 2 1.1 elric # -*- coding: iso-8859-1 -*- 3 1.1 elric 4 1.1.1.2 elric # Id 5 1.1 elric 6 1.1 elric # Copyright (c) 2004 Kungliga Tekniska Hgskolan 7 1.1 elric # (Royal Institute of Technology, Stockholm, Sweden). 8 1.1 elric # All rights reserved. 9 1.1 elric # 10 1.1 elric # Redistribution and use in source and binary forms, with or without 11 1.1 elric # modification, are permitted provided that the following conditions 12 1.1 elric # are met: 13 1.1 elric # 14 1.1 elric # 1. Redistributions of source code must retain the above copyright 15 1.1 elric # notice, this list of conditions and the following disclaimer. 16 1.1 elric # 17 1.1 elric # 2. Redistributions in binary form must reproduce the above copyright 18 1.1 elric # notice, this list of conditions and the following disclaimer in the 19 1.1 elric # documentation and/or other materials provided with the distribution. 20 1.1 elric # 21 1.1 elric # 3. Neither the name of the Institute nor the names of its contributors 22 1.1 elric # may be used to endorse or promote products derived from this software 23 1.1 elric # without specific prior written permission. 24 1.1 elric # 25 1.1 elric # THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 26 1.1 elric # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 1.1 elric # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 1.1 elric # ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 29 1.1 elric # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 1.1 elric # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 1.1 elric # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 1.1 elric # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 1.1 elric # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 1.1 elric # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 1.1 elric # SUCH DAMAGE. 36 1.1 elric 37 1.1 elric import re 38 1.1 elric import sys 39 1.1 elric 40 1.1 elric import generate 41 1.1 elric 42 1.1 elric if len(sys.argv) != 3: 43 1.1.1.3 christos print("usage: %s rfc3492.txt" % sys.argv[0]) 44 1.1 elric sys.exit(1) 45 1.1 elric 46 1.1 elric f = open(sys.argv[1], 'r') 47 1.1 elric 48 1.1 elric examples_h = generate.Header('%s/punycode_examples.h' % sys.argv[2]) 49 1.1 elric examples_c = generate.Header('%s/punycode_examples.c' % sys.argv[2]) 50 1.1 elric 51 1.1 elric start = False 52 1.1 elric 53 1.1 elric while True: 54 1.1 elric l = f.readline() 55 1.1 elric if not l: 56 1.1 elric break 57 1.1 elric if l[-2:] == "\\\n": 58 1.1 elric l2 = f.readline() 59 1.1 elric if not l2: 60 1.1 elric raise Exception("EOF in backslash escape") 61 1.1 elric l2 = re.sub('^ *', '', l2) 62 1.1 elric l = l[:-2] + l2 63 1.1 elric if start: 64 1.1 elric if re.match('7\.2', l): 65 1.1 elric start = False 66 1.1 elric else: 67 1.1 elric m = re.search('^ *\([A-Z]\) *(.*)$', l); 68 1.1 elric if m: 69 1.1 elric desc = m.group(1) 70 1.1 elric codes = [] 71 1.1 elric else: 72 1.1 elric m = re.search('^ *([uU]+.*) *$', l) 73 1.1 elric if m: 74 1.1.1.3 christos codes.extend(m.group(1).split(' ')) 75 1.1 elric else: 76 1.1 elric m = re.search('^ *Punycode: (.*) *$', l) 77 1.1 elric if m: 78 1.1 elric cases.append([codes, m.group(1), desc]) 79 1.1 elric else: 80 1.1 elric if re.match('^7\.1', l): 81 1.1 elric start = True 82 1.1 elric cases = [] 83 1.1.1.3 christos 84 1.1 elric f.close() 85 1.1 elric 86 1.1 elric examples_h.file.write( 87 1.1 elric ''' 88 1.1 elric #include <krb5-types.h> 89 1.1 elric 90 1.1 elric #define MAX_LENGTH 40 91 1.1 elric 92 1.1 elric struct punycode_example { 93 1.1 elric size_t len; 94 1.1 elric uint32_t val[MAX_LENGTH]; 95 1.1 elric const char *pc; 96 1.1 elric const char *description; 97 1.1 elric }; 98 1.1 elric 99 1.1 elric extern const struct punycode_example punycode_examples[]; 100 1.1 elric 101 1.1 elric extern const size_t punycode_examples_size; 102 1.1 elric ''') 103 1.1 elric 104 1.1 elric examples_c.file.write( 105 1.1 elric ''' 106 1.1 elric #include <stdlib.h> 107 1.1 elric #include "punycode_examples.h" 108 1.1 elric 109 1.1 elric const struct punycode_example punycode_examples[] = { 110 1.1 elric ''') 111 1.1 elric 112 1.1 elric for x in cases: 113 1.1 elric [cp, pc, desc] = x 114 1.1 elric examples_c.file.write( 115 1.1 elric " {%u, {%s}, \"%s\", \"%s\"},\n" % 116 1.1 elric (len(cp), 117 1.1.1.3 christos ",".join([re.sub('[uU]\+', '0x', x) for x in cp]), 118 1.1 elric pc, 119 1.1 elric desc)) 120 1.1 elric 121 1.1 elric examples_c.file.write( 122 1.1 elric '''}; 123 1.1 elric 124 1.1 elric ''') 125 1.1 elric 126 1.1 elric examples_c.file.write( 127 1.1 elric "const size_t punycode_examples_size = %u;\n\n" % len(cases)) 128 1.1 elric 129 1.1 elric examples_h.close() 130 1.1 elric examples_c.close() 131