1226031Sstas#!/usr/bin/python
2226031Sstas# -*- coding: utf-8 -*-
3226031Sstas#
4226031Sstas# Copyright (c) 2010 Kungliga Tekniska H��gskolan
5226031Sstas# (Royal Institute of Technology, Stockholm, Sweden).
6226031Sstas# All rights reserved.
7226031Sstas#
8226031Sstas# Redistribution and use in source and binary forms, with or without
9226031Sstas# modification, are permitted provided that the following conditions
10226031Sstas# are met:
11226031Sstas#
12226031Sstas# 1. Redistributions of source code must retain the above copyright
13226031Sstas#    notice, this list of conditions and the following disclaimer.
14226031Sstas#
15226031Sstas# 2. Redistributions in binary form must reproduce the above copyright
16226031Sstas#    notice, this list of conditions and the following disclaimer in the
17226031Sstas#    documentation and/or other materials provided with the distribution.
18226031Sstas#
19226031Sstas# 3. Neither the name of the Institute nor the names of its contributors
20226031Sstas#    may be used to endorse or promote products derived from this software
21226031Sstas#    without specific prior written permission.
22226031Sstas#
23226031Sstas# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24226031Sstas# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25226031Sstas# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26226031Sstas# ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27226031Sstas# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28226031Sstas# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29226031Sstas# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30226031Sstas# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31226031Sstas# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32226031Sstas# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33226031Sstas# SUCH DAMAGE.
34226031Sstas#
35226031Sstas
36226031SstasCONTROL_CHAR = 1
37226031SstasPRINTABLE = 2
38226031SstasRFC2253_QUOTE_FIRST = 4
39226031SstasRFC2253_QUOTE_LAST = 8
40226031SstasRFC2253_QUOTE = 16
41226031SstasRFC2253_HEX = 32
42226031Sstas
43226031Sstaschars = []
44226031Sstas
45226031Sstasfor i in range(0, 256):
46226031Sstas    chars.append(0);
47226031Sstas
48226031Sstasfor i in range(0, 256):
49226031Sstas    if (i < 32 or i > 126):
50226031Sstas        chars[i] |= CONTROL_CHAR | RFC2253_HEX;
51226031Sstas
52226031Sstasfor i in range(ord("A"), ord("Z") + 1):
53226031Sstas    chars[i] |= PRINTABLE
54226031Sstasfor i in range(ord("a"), ord("z") + 1):
55226031Sstas    chars[i] |= PRINTABLE
56226031Sstasfor i in range(ord("0"), ord("9") + 1):
57226031Sstas    chars[i] |= PRINTABLE
58226031Sstas
59226031Sstaschars[ord(' ')] |= PRINTABLE
60226031Sstaschars[ord('+')] |= PRINTABLE
61226031Sstaschars[ord(',')] |= PRINTABLE
62226031Sstaschars[ord('-')] |= PRINTABLE
63226031Sstaschars[ord('.')] |= PRINTABLE
64226031Sstaschars[ord('/')] |= PRINTABLE
65226031Sstaschars[ord(':')] |= PRINTABLE
66226031Sstaschars[ord('=')] |= PRINTABLE
67226031Sstaschars[ord('?')] |= PRINTABLE
68226031Sstas
69226031Sstaschars[ord(' ')] |= RFC2253_QUOTE_FIRST | RFC2253_QUOTE_FIRST
70226031Sstas
71226031Sstaschars[ord(',')] |= RFC2253_QUOTE
72226031Sstaschars[ord('=')] |= RFC2253_QUOTE
73226031Sstaschars[ord('+')] |= RFC2253_QUOTE
74226031Sstaschars[ord('<')] |= RFC2253_QUOTE
75226031Sstaschars[ord('>')] |= RFC2253_QUOTE
76226031Sstaschars[ord('#')] |= RFC2253_QUOTE
77226031Sstaschars[ord(';')] |= RFC2253_QUOTE
78226031Sstas
79226031Sstasprint "#define Q_CONTROL_CHAR		1"
80226031Sstasprint "#define Q_PRINTABLE		2"
81226031Sstasprint "#define Q_RFC2253_QUOTE_FIRST	4"
82226031Sstasprint "#define Q_RFC2253_QUOTE_LAST	8"
83226031Sstasprint "#define Q_RFC2253_QUOTE		16"
84226031Sstasprint "#define Q_RFC2253_HEX		32"
85226031Sstasprint ""
86226031Sstasprint "#define Q_RFC2253		(Q_RFC2253_QUOTE_FIRST|Q_RFC2253_QUOTE_LAST|Q_RFC2253_QUOTE|Q_RFC2253_HEX)"
87226031Sstasprint "\n" * 2
88226031Sstas
89226031Sstas
90226031Sstas
91226031Sstas
92226031Sstasprint "unsigned char char_map[] = {\n\t",
93226031Sstasfor x in range(0, 256):
94226031Sstas    if (x % 8) == 0 and x != 0:
95226031Sstas        print "\n\t",
96226031Sstas    print "0x%(char)02x" % { 'char' : chars[x] },
97226031Sstas    if x < 255:
98226031Sstas        print ", ",
99226031Sstas    else:
100226031Sstas        print ""
101226031Sstasprint "};"
102