1 /*	$NetBSD: nameprep_template.c,v 1.4 2014/12/10 04:37:55 christos Exp $	*/
2 
3 /* Id: nameprep_template.c,v 1.1 2003/06/04 00:25:57 marka Exp  */
4 
5 /*
6  * Copyright (c) 2001 Japan Network Information Center.  All rights reserved.
7  *
8  * By using this file, you agree to the terms and conditions set forth bellow.
9  *
10  * 			LICENSE TERMS AND CONDITIONS
11  *
12  * The following License Terms and Conditions apply, unless a different
13  * license is obtained from Japan Network Information Center ("JPNIC"),
14  * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
15  * Chiyoda-ku, Tokyo 101-0047, Japan.
16  *
17  * 1. Use, Modification and Redistribution (including distribution of any
18  *    modified or derived work) in source and/or binary forms is permitted
19  *    under this License Terms and Conditions.
20  *
21  * 2. Redistribution of source code must retain the copyright notices as they
22  *    appear in each source code file, this License Terms and Conditions.
23  *
24  * 3. Redistribution in binary form must reproduce the Copyright Notice,
25  *    this License Terms and Conditions, in the documentation and/or other
26  *    materials provided with the distribution.  For the purposes of binary
27  *    distribution the "Copyright Notice" refers to the following language:
28  *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
29  *
30  * 4. The name of JPNIC may not be used to endorse or promote products
31  *    derived from this Software without specific prior written approval of
32  *    JPNIC.
33  *
34  * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
35  *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37  *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
38  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
43  *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44  *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
45  */
46 
47 /*
48  * Include this file once for each version of NAMEPREP.
49  * VERSION should be defined to appropriate value before inclusion.
50  */
51 
52 #ifndef NAMEPREP_TEMPLATE_INIT
53 #define NAMEPREP_TEMPLATE_INIT
54 
55 /* Symbol composition. */
56 #define compose_sym2(a, b)		compose_sym2X(a, b)
57 #define compose_sym2X(a, b)		a ## b
58 #define compose_sym3(a, b, c)		compose_sym3X(a, b, c)
59 #define compose_sym3X(a, b, c)		a ## b ## c
60 
61 /* Index calculation for multi-level index tables. */
62 #define IDX0(type, v) IDX_0(v, BITS1(type), BITS2(type))
63 #define IDX1(type, v) IDX_1(v, BITS1(type), BITS2(type))
64 #define IDX2(type, v) IDX_2(v, BITS1(type), BITS2(type))
65 
66 #define IDX_0(v, bits1, bits2)	((v) >> ((bits1) + (bits2)))
67 #define IDX_1(v, bits1, bits2)	(((v) >> (bits2)) & ((1 << (bits1)) - 1))
68 #define IDX_2(v, bits1, bits2)	((v) & ((1 << (bits2)) - 1))
69 
70 #define BITS1(type)	type ## _BITS_1
71 #define BITS2(type)	type ## _BITS_2
72 
73 #endif /* NAMEPREP_TEMPLATE_INIT */
74 
75 static const char *
76 compose_sym2(nameprep_map_, VERSION) (unsigned long v) {
77 	int idx0 = IDX0(MAP, v);
78 	int idx1 = IDX1(MAP, v);
79 	int idx2 = IDX2(MAP, v);
80 	int offset;
81 
82 #define IMAP	compose_sym3(nameprep_, VERSION, _map_imap)
83 #define TABLE	compose_sym3(nameprep_, VERSION, _map_table)
84 #define DATA	compose_sym3(nameprep_, VERSION, _map_data)
85 	offset = TABLE[IMAP[IMAP[idx0] + idx1]].tbl[idx2];
86 	if (offset == 0)
87 		return (NULL);	/* no mapping */
88 	return (const char *)(DATA + offset);
89 #undef IMAP
90 #undef TABLE
91 #undef DATA
92 }
93 
94 static int
95 compose_sym2(nameprep_prohibited_, VERSION) (unsigned long v) {
96 	int idx0 = IDX0(PROH, v);
97 	int idx1 = IDX1(PROH, v);
98 	int idx2 = IDX2(PROH, v);
99 	const unsigned char *bm;
100 
101 #define IMAP	compose_sym3(nameprep_, VERSION, _prohibited_imap)
102 #define BITMAP	compose_sym3(nameprep_, VERSION, _prohibited_bitmap)
103 	bm = BITMAP[IMAP[IMAP[idx0] + idx1]].bm;
104 	return (bm[idx2 / 8] & (1 << (idx2 % 8)));
105 #undef IMAP
106 #undef BITMAP
107 }
108 
109 static int
110 compose_sym2(nameprep_unassigned_, VERSION) (unsigned long v) {
111 	int idx0 = IDX0(UNAS, v);
112 	int idx1 = IDX1(UNAS, v);
113 	int idx2 = IDX2(UNAS, v);
114 	const unsigned char *bm;
115 
116 #define IMAP	compose_sym3(nameprep_, VERSION, _unassigned_imap)
117 #define BITMAP	compose_sym3(nameprep_, VERSION, _unassigned_bitmap)
118 	bm = BITMAP[IMAP[IMAP[idx0] + idx1]].bm;
119 	return (bm[idx2 / 8] & (1 << (idx2 % 8)));
120 #undef IMAP
121 #undef BITMAP
122 }
123 
124 static idn_biditype_t
125 compose_sym2(nameprep_biditype_, VERSION) (unsigned long v) {
126 	int idx0 = IDX0(BIDI, v);
127 	int idx1 = IDX1(BIDI, v);
128 	int idx2 = IDX2(BIDI, v);
129 	int offset;
130 
131 #define IMAP	compose_sym3(nameprep_, VERSION, _bidi_imap)
132 #define TABLE	compose_sym3(nameprep_, VERSION, _bidi_table)
133 #define DATA	compose_sym3(nameprep_, VERSION, _bidi_data)
134 	offset = TABLE[IMAP[IMAP[idx0] + idx1]].tbl[idx2];
135 	return DATA[offset];
136 #undef IMAP
137 #undef TABLE
138 #undef DATA
139 }
140