Back to index

lightning-sunbird  0.9+nobinonly
nameprep_template.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001 Japan Network Information Center.  All rights reserved.
00003  *  
00004  * By using this file, you agree to the terms and conditions set forth bellow.
00005  * 
00006  *                   LICENSE TERMS AND CONDITIONS 
00007  * 
00008  * The following License Terms and Conditions apply, unless a different
00009  * license is obtained from Japan Network Information Center ("JPNIC"),
00010  * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
00011  * Chiyoda-ku, Tokyo 101-0047, Japan.
00012  * 
00013  * 1. Use, Modification and Redistribution (including distribution of any
00014  *    modified or derived work) in source and/or binary forms is permitted
00015  *    under this License Terms and Conditions.
00016  * 
00017  * 2. Redistribution of source code must retain the copyright notices as they
00018  *    appear in each source code file, this License Terms and Conditions.
00019  * 
00020  * 3. Redistribution in binary form must reproduce the Copyright Notice,
00021  *    this License Terms and Conditions, in the documentation and/or other
00022  *    materials provided with the distribution.  For the purposes of binary
00023  *    distribution the "Copyright Notice" refers to the following language:
00024  *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
00025  * 
00026  * 4. The name of JPNIC may not be used to endorse or promote products
00027  *    derived from this Software without specific prior written approval of
00028  *    JPNIC.
00029  * 
00030  * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
00031  *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00032  *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00033  *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
00034  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00035  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00036  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
00037  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00038  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00039  *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00040  *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
00041  */
00042 
00043 /*
00044  * Include this file once for each version of NAMEPREP.
00045  * VERSION should be defined to appropriate value before inclusion.
00046  */
00047 
00048 #ifndef NAMEPREP_TEMPLATE_INIT
00049 #define NAMEPREP_TEMPLATE_INIT
00050 
00051 #include "prtypes.h"
00052 
00053 /* Symbol composition. */
00054 #define compose_sym2(a, b)         compose_sym2X(a, b)
00055 #define compose_sym2X(a, b)        a ## b
00056 #define compose_sym3(a, b, c)             compose_sym3X(a, b, c)
00057 #define compose_sym3X(a, b, c)            a ## b ## c
00058 
00059 /* The table is based on "Optimized Two-Stage Table" mentioned in
00060  * Unicode 3.0 page 106, extended to handle 21bit data instead of 16 bit.
00061  */
00062 
00063 /* Index calculation for multi-level index tables. */
00064 #define IDX0(type, v) IDX_0(v, BITS1(type), BITS2(type))
00065 #define IDX1(type, v) IDX_1(v, BITS1(type), BITS2(type))
00066 #define IDX2(type, v) IDX_2(v, BITS1(type), BITS2(type))
00067 
00068 #define IDX_0(v, bits1, bits2)     ((v) >> ((bits1) + (bits2)))
00069 #define IDX_1(v, bits1, bits2)     (((v) >> (bits2)) & ((1 << (bits1)) - 1))
00070 #define IDX_2(v, bits1, bits2)     ((v) & ((1 << (bits2)) - 1))
00071 
00072 #define BITS1(type)  type ## _BITS_1
00073 #define BITS2(type)  type ## _BITS_2
00074 
00075 #endif /* NAMEPREP_TEMPLATE_INIT */
00076 
00077 static const char *
00078 compose_sym2(nameprep_map_, VERSION) (PRUint32 v) {
00079        int idx0 = IDX0(MAP, v);
00080        int idx1 = IDX1(MAP, v);
00081        int idx2 = IDX2(MAP, v);
00082        int offset;
00083 
00084 #define IMAP  compose_sym3(nameprep_, VERSION, _map_imap)
00085 #define TABLE compose_sym3(nameprep_, VERSION, _map_table)
00086 #define DATA  compose_sym3(nameprep_, VERSION, _map_data)
00087        offset = TABLE[IMAP[IMAP[idx0] + idx1]].tbl[idx2];
00088        if (offset == 0)
00089               return (NULL);       /* no mapping */
00090        return (const char *)(DATA + offset);
00091 #undef IMAP
00092 #undef TABLE
00093 #undef DATA
00094 }
00095 
00096 static int
00097 compose_sym2(nameprep_prohibited_, VERSION) (PRUint32 v) {
00098        int idx0 = IDX0(PROH, v);
00099        int idx1 = IDX1(PROH, v);
00100        int idx2 = IDX2(PROH, v);
00101        const unsigned char *bm;
00102 
00103 #define IMAP  compose_sym3(nameprep_, VERSION, _prohibited_imap)
00104 #define BITMAP       compose_sym3(nameprep_, VERSION, _prohibited_bitmap)
00105        bm = BITMAP[IMAP[IMAP[idx0] + idx1]].bm;
00106        return (bm[idx2 / 8] & (1 << (idx2 % 8)));
00107 #undef IMAP
00108 #undef BITMAP
00109 }
00110 
00111 static int
00112 compose_sym2(nameprep_unassigned_, VERSION) (PRUint32 v) {
00113        int idx0 = IDX0(UNAS, v);
00114        int idx1 = IDX1(UNAS, v);
00115        int idx2 = IDX2(UNAS, v);
00116        const unsigned char *bm;
00117 
00118 #define IMAP  compose_sym3(nameprep_, VERSION, _unassigned_imap)
00119 #define BITMAP       compose_sym3(nameprep_, VERSION, _unassigned_bitmap)
00120        bm = BITMAP[IMAP[IMAP[idx0] + idx1]].bm;
00121        return (bm[idx2 / 8] & (1 << (idx2 % 8)));
00122 #undef IMAP
00123 #undef BITMAP
00124 }
00125 
00126 static idn_biditype_t
00127 compose_sym2(nameprep_biditype_, VERSION) (PRUint32 v) {
00128        int idx0 = IDX0(BIDI, v);
00129        int idx1 = IDX1(BIDI, v);
00130        int idx2 = IDX2(BIDI, v);
00131        int offset;
00132 
00133 #define IMAP  compose_sym3(nameprep_, VERSION, _bidi_imap)
00134 #define TABLE compose_sym3(nameprep_, VERSION, _bidi_table)
00135 #define DATA  compose_sym3(nameprep_, VERSION, _bidi_data)
00136        offset = TABLE[IMAP[IMAP[idx0] + idx1]].tbl[idx2];
00137        return DATA[offset];
00138 #undef IMAP
00139 #undef TABLE
00140 #undef DATA
00141 }