Path: blob/main/crypto/heimdal/lib/wind/gen-map.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 rfc345442import rfc451843import stringprep44import util4546if len(sys.argv) != 3:47print "usage: %s rfc3454.txt out-dir" % sys.argv[0]48sys.exit(1)4950tables = rfc3454.read(sys.argv[1])51t2 = rfc4518.read()5253for x in t2.iterkeys():54tables[x] = t2[x]5556map_list = stringprep.get_maplist()5758map_h = generate.Header('%s/map_table.h' % sys.argv[2])5960map_c = generate.Implementation('%s/map_table.c' % sys.argv[2])6162map_h.file.write(63'''64#include "windlocl.h"6566struct translation {67uint32_t key;68unsigned short val_len;69unsigned short val_offset;70wind_profile_flags flags;71};7273extern const struct translation _wind_map_table[];7475extern const size_t _wind_map_table_size;7677extern const uint32_t _wind_map_table_val[];7879''')8081map_c.file.write(82'''83#include "map_table.h"8485const struct translation _wind_map_table[] = {86''')8788trans=[]8990for t in map_list.iterkeys():91for l in tables[t]:92m = re.search('^ *([0-9A-F]+)-([0-9A-F]+); *([^;]+); *(.*) *$', l)93if m:94start = int(m.group(1), 0x10)95end = int(m.group(2), 0x10)96value = m.group(3)97desc = m.group(4)98for key in xrange(start,end,1):99trans.append((key, value, desc, [t]))100continue101m = re.search('^ *([^;]+); *([^;]+); *(.*) *$', l)102if m:103key = int(m.group(1), 0x10)104value = m.group(2)105desc = m.group(3)106trans.append((key, value, desc, [t]))107continue108109valTable = []110offsetTable = {}111112trans = stringprep.sort_merge_trans(trans)113114for x in trans:115if x[0] == 0xad:116print "fooresult %s" % ",".join(x[3])117118for x in trans:119(key, value, description, table) = x120v = value.split()121i = util.subList(valTable, v)122if i:123offsetTable[key] = i124else:125offsetTable[key] = len(valTable)126valTable.extend(v)127128for x in trans:129(key, value, description, tables) = x130symbols = stringprep.symbols(map_list, tables)131if len(symbols) == 0:132print "no symbol for %s %s (%s)" % (key, description, tables)133sys.exit(1)134v = value.split()135map_c.file.write(" {0x%x, %u, %u, %s}, /* %s: %s */\n"136% (key, len(v), offsetTable[key], symbols, ",".join(tables), description))137138map_c.file.write(139'''140};141142''')143144map_c.file.write(145"const size_t _wind_map_table_size = %u;\n\n" % len(trans))146147map_c.file.write(148"const uint32_t _wind_map_table_val[] = {\n")149150for x in valTable:151map_c.file.write(" 0x%s,\n" % x)152153map_c.file.write(154"};\n\n")155156map_h.close()157map_c.close()158159160