1/* $Id: unicode_template.c,v 1.1 2003/06/04 00:26:16 marka Exp $ */
2
3/*
4 * Copyright (c) 2000,2001 Japan Network Information Center.
5 * All rights reserved.
6 *
7 * By using this file, you agree to the terms and conditions set forth bellow.
8 *
9 * 			LICENSE TERMS AND CONDITIONS
10 *
11 * The following License Terms and Conditions apply, unless a different
12 * license is obtained from Japan Network Information Center ("JPNIC"),
13 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
14 * Chiyoda-ku, Tokyo 101-0047, Japan.
15 *
16 * 1. Use, Modification and Redistribution (including distribution of any
17 *    modified or derived work) in source and/or binary forms is permitted
18 *    under this License Terms and Conditions.
19 *
20 * 2. Redistribution of source code must retain the copyright notices as they
21 *    appear in each source code file, this License Terms and Conditions.
22 *
23 * 3. Redistribution in binary form must reproduce the Copyright Notice,
24 *    this License Terms and Conditions, in the documentation and/or other
25 *    materials provided with the distribution.  For the purposes of binary
26 *    distribution the "Copyright Notice" refers to the following language:
27 *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
28 *
29 * 4. The name of JPNIC may not be used to endorse or promote products
30 *    derived from this Software without specific prior written approval of
31 *    JPNIC.
32 *
33 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
34 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
36 *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
37 *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
40 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
41 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
42 *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43 *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
44 */
45
46#ifndef UNICODE_TEMPLATE_INIT
47#define UNICODE_TEMPLATE_INIT
48
49/*
50 * Macro for multi-level index table.
51 */
52#define LOOKUPTBL(vprefix, mprefix, v) \
53	DMAP(vprefix)[\
54		IMAP(vprefix)[\
55			IMAP(vprefix)[IDX0(mprefix, v)] + IDX1(mprefix, v)\
56		]\
57	].tbl[IDX2(mprefix, v)]
58
59#define IDX0(mprefix, v) IDX_0(v, BITS1(mprefix), BITS2(mprefix))
60#define IDX1(mprefix, v) IDX_1(v, BITS1(mprefix), BITS2(mprefix))
61#define IDX2(mprefix, v) IDX_2(v, BITS1(mprefix), BITS2(mprefix))
62
63#define IDX_0(v, bits1, bits2)	((v) >> ((bits1) + (bits2)))
64#define IDX_1(v, bits1, bits2)	(((v) >> (bits2)) & ((1 << (bits1)) - 1))
65#define IDX_2(v, bits1, bits2)	((v) & ((1 << (bits2)) - 1))
66
67#define BITS1(mprefix)	mprefix ## _BITS_1
68#define BITS2(mprefix)	mprefix ## _BITS_2
69
70#define IMAP(vprefix)	concat4(VERSION, _, vprefix, _imap)
71#define DMAP(vprefix)	concat4(VERSION, _, vprefix, _table)
72#define SEQ(vprefix)	concat4(VERSION, _, vprefix, _seq)
73#define concat4(a,b,c,d)	concat4X(a, b, c, d)
74#define concat4X(a,b,c,d)	a ## b ## c ## d
75
76#endif /* UNICODE_TEMPLATE_INIT */
77
78static int
79compose_sym(canonclass_, VERSION) (unsigned long c) {
80	/* Look up canonicalclass table. */
81	return (LOOKUPTBL(canon_class, CANON_CLASS, c));
82}
83
84static int
85compose_sym(decompose_, VERSION) (unsigned long c, const unsigned long **seqp)
86{
87	/* Look up decomposition table. */
88	int seqidx = LOOKUPTBL(decompose, DECOMP, c);
89	*seqp = SEQ(decompose) + (seqidx & ~DECOMP_COMPAT);
90	return (seqidx);
91}
92
93static int
94compose_sym(compose_, VERSION) (unsigned long c,
95				const struct composition **compp)
96{
97	/* Look up composition table. */
98	int seqidx = LOOKUPTBL(compose, CANON_COMPOSE, c);
99	*compp = SEQ(compose) + (seqidx & 0xffff);
100	return (seqidx >> 16);
101}
102