Path: blob/main/crypto/heimdal/lib/wind/gen-errorlist.py
34914 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 stringprep4445if len(sys.argv) != 3:46print "usage: %s rfc3454.txt out-dir" % sys.argv[0]47sys.exit(1)4849tables = rfc3454.read(sys.argv[1])50t2 = rfc4518.read()5152for x in t2.iterkeys():53tables[x] = t2[x]5455error_list = stringprep.get_errorlist()5657errorlist_h = generate.Header('%s/errorlist_table.h' % sys.argv[2])5859errorlist_c = generate.Implementation('%s/errorlist_table.c' % sys.argv[2])6061errorlist_h.file.write(62'''63#include "windlocl.h"6465struct error_entry {66uint32_t start;67unsigned len;68wind_profile_flags flags;69};7071extern const struct error_entry _wind_errorlist_table[];7273extern const size_t _wind_errorlist_table_size;7475''')7677errorlist_c.file.write(78'''79#include <stdlib.h>80#include "errorlist_table.h"8182const struct error_entry _wind_errorlist_table[] = {83''')8485trans=[]8687for t in error_list.iterkeys():88for l in tables[t]:89m = re.search('^ *([0-9A-F]+)-([0-9A-F]+); *(.*) *$', l)90if m:91start = int(m.group(1), 0x10)92end = int(m.group(2), 0x10)93desc = m.group(3)94trans.append([start, end - start + 1, desc, [t]])95else:96m = re.search('^ *([0-9A-F]+); *(.*) *$', l)97if m:98trans.append([int(m.group(1), 0x10), 1, m.group(2), [t]])99100trans = stringprep.sort_merge_trans(trans)101102for x in trans:103(start, length, description, tables) = x104symbols = stringprep.symbols(error_list, tables)105if len(symbols) == 0:106print "no symbol for %s" % description107sys.exit(1)108errorlist_c.file.write(" {0x%x, 0x%x, %s}, /* %s: %s */\n"109% (start, length, symbols, ",".join(tables), description))110111errorlist_c.file.write(112'''};113114''')115116errorlist_c.file.write(117"const size_t _wind_errorlist_table_size = %u;\n" % len(trans))118119errorlist_h.close()120errorlist_c.close()121122123