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