Path: blob/main/crypto/heimdal/lib/wind/gen-normalize.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 generate41import UnicodeData42import util4344if len(sys.argv) != 4:45print "usage: %s UnicodeData.txt"46" CompositionExclusions-3.2.0.txt out-dir" % sys.argv[0]47sys.exit(1)4849ud = UnicodeData.read(sys.argv[1])5051def sortedKeys(d):52"""Return a sorted list of the keys of a dict"""53keys = d.keys()54keys.sort()55return keys5657trans = dict([(k, [re.sub('<[a-zA-Z]+>', '', v[4]), v[0]])58for k,v in ud.items() if v[4]])5960maxLength = 061for v in trans.values():62maxLength = max(maxLength, len(v[0].split()))6364normalize_h = generate.Header('%s/normalize_table.h' % sys.argv[3])65normalize_c = generate.Implementation('%s/normalize_table.c' % sys.argv[3])6667normalize_h.file.write(68'''69#include <krb5-types.h>7071#define MAX_LENGTH_CANON %u7273struct translation {74uint32_t key;75unsigned short val_len;76unsigned short val_offset;77};7879extern const struct translation _wind_normalize_table[];8081extern const uint32_t _wind_normalize_val_table[];8283extern const size_t _wind_normalize_table_size;8485struct canon_node {86uint32_t val;87unsigned char next_start;88unsigned char next_end;89unsigned short next_offset;90};9192extern const struct canon_node _wind_canon_table[];9394extern const unsigned short _wind_canon_next_table[];95''' % maxLength)9697normalize_c.file.write(98'''99#include <stdlib.h>100#include "normalize_table.h"101102const struct translation _wind_normalize_table[] = {103''')104105normalizeValTable = []106107for k in sortedKeys(trans) :108v = trans[k]109(key, value, description) = k, v[0], v[1]110vec = [int(x, 0x10) for x in value.split()];111offset = util.subList(normalizeValTable, vec)112if not offset:113offset = len(normalizeValTable)114normalizeValTable.extend(vec) # [("0x%s" % i) for i in vec])115normalize_c.file.write(" {0x%x, %u, %u}, /* %s */\n"116% (key, len(vec), offset, description))117118normalize_c.file.write(119'''};120121''')122123normalize_c.file.write(124"const size_t _wind_normalize_table_size = %u;\n\n" % len(trans))125126normalize_c.file.write("const uint32_t _wind_normalize_val_table[] = {\n")127128for v in normalizeValTable:129normalize_c.file.write(" 0x%x,\n" % v)130131normalize_c.file.write("};\n\n");132133exclusions = UnicodeData.read(sys.argv[2])134135inv = dict([(''.join(["%05x" % int(x, 0x10) for x in v[4].split(' ')]),136[k, v[0]])137for k,v in ud.items()138if v[4] and not re.search('<[a-zA-Z]+> *', v[4]) and not exclusions.has_key(k)])139140table = 0141142tables = {}143144def createTable():145"""add a new table"""146global table, tables147ret = table148table += 1149tables[ret] = [0] + [None] * 16150return ret151152def add(table, k, v):153"""add an entry (k, v) to table (recursively)"""154if len(k) == 0:155table[0] = v[0]156else:157i = int(k[0], 0x10) + 1158if table[i] == None:159table[i] = createTable()160add(tables[table[i]], k[1:], v)161162top = createTable()163164for k,v in inv.items():165add(tables[top], k, v)166167next_table = []168tableToNext = {}169tableEnd = {}170tableStart = {}171172for k in sortedKeys(tables) :173t = tables[k]174tableToNext[k] = len(next_table)175l = t[1:]176start = 0177while start < 16 and l[start] == None:178start += 1179end = 16180while end > start and l[end - 1] == None:181end -= 1182tableStart[k] = start183tableEnd[k] = end184n = []185for i in range(start, end):186x = l[i]187if x:188n.append(x)189else:190n.append(0)191next_table.extend(n)192193normalize_c.file.write("const struct canon_node _wind_canon_table[] = {\n")194195for k in sortedKeys(tables) :196t = tables[k]197normalize_c.file.write(" {0x%x, %u, %u, %u},\n" %198(t[0], tableStart[k], tableEnd[k], tableToNext[k]))199200normalize_c.file.write("};\n\n")201202normalize_c.file.write("const unsigned short _wind_canon_next_table[] = {\n")203204for k in next_table:205normalize_c.file.write(" %u,\n" % k)206207normalize_c.file.write("};\n\n")208209normalize_h.close()210normalize_c.close()211212213