Path: blob/main/crypto/heimdal/lib/wind/gen-punycode-examples.py
34889 views
#!/usr/local/bin/python1# -*- coding: iso-8859-1 -*-23# $Id$45# Copyright (c) 2004 Kungliga Tekniska Högskolan6# (Royal Institute of Technology, Stockholm, Sweden).7# All rights reserved.8#9# Redistribution and use in source and binary forms, with or without10# modification, are permitted provided that the following conditions11# are met:12#13# 1. Redistributions of source code must retain the above copyright14# notice, this list of conditions and the following disclaimer.15#16# 2. Redistributions in binary form must reproduce the above copyright17# notice, this list of conditions and the following disclaimer in the18# documentation and/or other materials provided with the distribution.19#20# 3. Neither the name of the Institute nor the names of its contributors21# may be used to endorse or promote products derived from this software22# without specific prior written permission.23#24# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND25# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27# ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE28# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL29# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS30# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT32# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY33# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF34# SUCH DAMAGE.3536import re37import string38import sys3940import generate4142if len(sys.argv) != 3:43print "usage: %s rfc3492.txt" % sys.argv[0]44sys.exit(1)4546f = open(sys.argv[1], 'r')4748examples_h = generate.Header('%s/punycode_examples.h' % sys.argv[2])49examples_c = generate.Header('%s/punycode_examples.c' % sys.argv[2])5051start = False5253while True:54l = f.readline()55if not l:56break57if l[-2:] == "\\\n":58l2 = f.readline()59if not l2:60raise Exception("EOF in backslash escape")61l2 = re.sub('^ *', '', l2)62l = l[:-2] + l263if start:64if re.match('7\.2', l):65start = False66else:67m = re.search('^ *\([A-Z]\) *(.*)$', l);68if m:69desc = m.group(1)70codes = []71else:72m = re.search('^ *([uU]+.*) *$', l)73if m:74codes.extend(string.split(m.group(1), ' '))75else:76m = re.search('^ *Punycode: (.*) *$', l)77if m:78cases.append([codes, m.group(1), desc])79else:80if re.match('^7\.1', l):81start = True82cases = []8384f.close()8586examples_h.file.write(87'''88#include <krb5-types.h>8990#define MAX_LENGTH 409192struct punycode_example {93size_t len;94uint32_t val[MAX_LENGTH];95const char *pc;96const char *description;97};9899extern const struct punycode_example punycode_examples[];100101extern const size_t punycode_examples_size;102''')103104examples_c.file.write(105'''106#include <stdlib.h>107#include "punycode_examples.h"108109const struct punycode_example punycode_examples[] = {110''')111112for x in cases:113[cp, pc, desc] = x114examples_c.file.write(115" {%u, {%s}, \"%s\", \"%s\"},\n" %116(len(cp),117string.join([re.sub('[uU]\+', '0x', x) for x in cp], ', '),118pc,119desc))120121examples_c.file.write(122'''};123124''')125126examples_c.file.write(127"const size_t punycode_examples_size = %u;\n\n" % len(cases))128129examples_h.close()130examples_c.close()131132133