1226031Sstas#!/usr/local/bin/python
2226031Sstas# -*- coding: iso-8859-1 -*-
3226031Sstas
4226031Sstas# $Id$
5226031Sstas
6226031Sstas# Copyright (c) 2004 Kungliga Tekniska H��gskolan
7226031Sstas# (Royal Institute of Technology, Stockholm, Sweden).
8226031Sstas# All rights reserved.
9226031Sstas#
10226031Sstas# Redistribution and use in source and binary forms, with or without
11226031Sstas# modification, are permitted provided that the following conditions
12226031Sstas# are met:
13226031Sstas#
14226031Sstas# 1. Redistributions of source code must retain the above copyright
15226031Sstas#    notice, this list of conditions and the following disclaimer.
16226031Sstas#
17226031Sstas# 2. Redistributions in binary form must reproduce the above copyright
18226031Sstas#    notice, this list of conditions and the following disclaimer in the
19226031Sstas#    documentation and/or other materials provided with the distribution.
20226031Sstas#
21226031Sstas# 3. Neither the name of the Institute nor the names of its contributors
22226031Sstas#    may be used to endorse or promote products derived from this software
23226031Sstas#    without specific prior written permission.
24226031Sstas#
25226031Sstas# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26226031Sstas# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27226031Sstas# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28226031Sstas# ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29226031Sstas# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30226031Sstas# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31226031Sstas# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32226031Sstas# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33226031Sstas# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34226031Sstas# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35226031Sstas# SUCH DAMAGE.
36226031Sstas
37226031Sstasimport re
38226031Sstasimport string
39226031Sstasimport sys
40226031Sstas
41226031Sstasimport generate
42226031Sstas
43226031Sstasif len(sys.argv) != 3:
44226031Sstas    print "usage: %s rfc3492.txt" % sys.argv[0]
45226031Sstas    sys.exit(1)
46226031Sstas
47226031Sstasf = open(sys.argv[1], 'r')
48226031Sstas
49226031Sstasexamples_h = generate.Header('%s/punycode_examples.h' % sys.argv[2])
50226031Sstasexamples_c = generate.Header('%s/punycode_examples.c' % sys.argv[2])
51226031Sstas
52226031Sstasstart = False
53226031Sstas
54226031Sstaswhile True:
55226031Sstas    l = f.readline()
56226031Sstas    if not l:
57226031Sstas        break
58226031Sstas    if l[-2:] == "\\\n":
59226031Sstas        l2 = f.readline()
60226031Sstas        if not l2:
61226031Sstas            raise Exception("EOF in backslash escape")
62226031Sstas        l2 = re.sub('^ *', '', l2)
63226031Sstas        l = l[:-2] + l2
64226031Sstas    if start:
65226031Sstas        if re.match('7\.2', l):
66226031Sstas            start = False
67226031Sstas        else:
68226031Sstas            m = re.search('^ *\([A-Z]\) *(.*)$', l);
69226031Sstas            if m:
70226031Sstas                desc = m.group(1)
71226031Sstas                codes = []
72226031Sstas            else:
73226031Sstas                m = re.search('^ *([uU]+.*) *$', l)
74226031Sstas                if m:
75226031Sstas                    codes.extend(string.split(m.group(1), ' '))
76226031Sstas                else:
77226031Sstas                    m = re.search('^ *Punycode: (.*) *$', l)
78226031Sstas                    if m:
79226031Sstas                        cases.append([codes, m.group(1), desc])
80226031Sstas    else:
81226031Sstas        if re.match('^7\.1', l):
82226031Sstas            start = True
83226031Sstas            cases = []
84226031Sstas
85226031Sstasf.close()
86226031Sstas
87226031Sstasexamples_h.file.write(
88226031Sstas'''
89226031Sstas#include <krb5-types.h>
90226031Sstas
91226031Sstas#define MAX_LENGTH 40
92226031Sstas
93226031Sstasstruct punycode_example {
94226031Sstas    size_t len;
95226031Sstas    uint32_t val[MAX_LENGTH];
96226031Sstas    const char *pc;
97226031Sstas    const char *description;
98226031Sstas};
99226031Sstas
100226031Sstasextern const struct punycode_example punycode_examples[];
101226031Sstas
102226031Sstasextern const size_t punycode_examples_size;
103226031Sstas''')
104226031Sstas
105226031Sstasexamples_c.file.write(
106226031Sstas'''
107226031Sstas#include <stdlib.h>
108226031Sstas#include "punycode_examples.h"
109226031Sstas
110226031Sstasconst struct punycode_example punycode_examples[] = {
111226031Sstas''')
112226031Sstas
113226031Sstasfor x in cases:
114226031Sstas    [cp, pc, desc] = x
115226031Sstas    examples_c.file.write(
116226031Sstas        "  {%u, {%s}, \"%s\", \"%s\"},\n" %
117226031Sstas        (len(cp),
118226031Sstas         string.join([re.sub('[uU]\+', '0x', x) for x in cp], ', '),
119226031Sstas         pc,
120226031Sstas         desc))
121226031Sstas
122226031Sstasexamples_c.file.write(
123226031Sstas'''};
124226031Sstas
125226031Sstas''')
126226031Sstas
127226031Sstasexamples_c.file.write(
128226031Sstas    "const size_t punycode_examples_size = %u;\n\n" % len(cases))
129226031Sstas
130226031Sstasexamples_h.close()
131226031Sstasexamples_c.close()
132