1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/dns/nameprep_template.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,139 @@ 1.4 +/* 1.5 + * Copyright (c) 2001 Japan Network Information Center. All rights reserved. 1.6 + * 1.7 + * By using this file, you agree to the terms and conditions set forth bellow. 1.8 + * 1.9 + * LICENSE TERMS AND CONDITIONS 1.10 + * 1.11 + * The following License Terms and Conditions apply, unless a different 1.12 + * license is obtained from Japan Network Information Center ("JPNIC"), 1.13 + * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda, 1.14 + * Chiyoda-ku, Tokyo 101-0047, Japan. 1.15 + * 1.16 + * 1. Use, Modification and Redistribution (including distribution of any 1.17 + * modified or derived work) in source and/or binary forms is permitted 1.18 + * under this License Terms and Conditions. 1.19 + * 1.20 + * 2. Redistribution of source code must retain the copyright notices as they 1.21 + * appear in each source code file, this License Terms and Conditions. 1.22 + * 1.23 + * 3. Redistribution in binary form must reproduce the Copyright Notice, 1.24 + * this License Terms and Conditions, in the documentation and/or other 1.25 + * materials provided with the distribution. For the purposes of binary 1.26 + * distribution the "Copyright Notice" refers to the following language: 1.27 + * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved." 1.28 + * 1.29 + * 4. The name of JPNIC may not be used to endorse or promote products 1.30 + * derived from this Software without specific prior written approval of 1.31 + * JPNIC. 1.32 + * 1.33 + * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC 1.34 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.35 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 1.36 + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE 1.37 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.38 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.39 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 1.40 + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 1.41 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 1.42 + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 1.43 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 1.44 + */ 1.45 + 1.46 +/* 1.47 + * Include this file once for each version of NAMEPREP. 1.48 + * VERSION should be defined to appropriate value before inclusion. 1.49 + */ 1.50 + 1.51 +#ifndef NAMEPREP_TEMPLATE_INIT 1.52 +#define NAMEPREP_TEMPLATE_INIT 1.53 + 1.54 +/* Symbol composition. */ 1.55 +#define compose_sym2(a, b) compose_sym2X(a, b) 1.56 +#define compose_sym2X(a, b) a ## b 1.57 +#define compose_sym3(a, b, c) compose_sym3X(a, b, c) 1.58 +#define compose_sym3X(a, b, c) a ## b ## c 1.59 + 1.60 +/* The table is based on "Optimized Two-Stage Table" mentioned in 1.61 + * Unicode 3.0 page 106, extended to handle 21bit data instead of 16 bit. 1.62 + */ 1.63 + 1.64 +/* Index calculation for multi-level index tables. */ 1.65 +#define IDX0(type, v) IDX_0(v, BITS1(type), BITS2(type)) 1.66 +#define IDX1(type, v) IDX_1(v, BITS1(type), BITS2(type)) 1.67 +#define IDX2(type, v) IDX_2(v, BITS1(type), BITS2(type)) 1.68 + 1.69 +#define IDX_0(v, bits1, bits2) ((v) >> ((bits1) + (bits2))) 1.70 +#define IDX_1(v, bits1, bits2) (((v) >> (bits2)) & ((1 << (bits1)) - 1)) 1.71 +#define IDX_2(v, bits1, bits2) ((v) & ((1 << (bits2)) - 1)) 1.72 + 1.73 +#define BITS1(type) type ## _BITS_1 1.74 +#define BITS2(type) type ## _BITS_2 1.75 + 1.76 +#endif /* NAMEPREP_TEMPLATE_INIT */ 1.77 + 1.78 +static const char * 1.79 +compose_sym2(nameprep_map_, VERSION) (uint32_t v) { 1.80 + int idx0 = IDX0(MAP, v); 1.81 + int idx1 = IDX1(MAP, v); 1.82 + int idx2 = IDX2(MAP, v); 1.83 + int offset; 1.84 + 1.85 +#define IMAP compose_sym3(nameprep_, VERSION, _map_imap) 1.86 +#define TABLE compose_sym3(nameprep_, VERSION, _map_table) 1.87 +#define DATA compose_sym3(nameprep_, VERSION, _map_data) 1.88 + offset = TABLE[IMAP[IMAP[idx0] + idx1]].tbl[idx2]; 1.89 + if (offset == 0) 1.90 + return (NULL); /* no mapping */ 1.91 + return (const char *)(DATA + offset); 1.92 +#undef IMAP 1.93 +#undef TABLE 1.94 +#undef DATA 1.95 +} 1.96 + 1.97 +static int 1.98 +compose_sym2(nameprep_prohibited_, VERSION) (uint32_t v) { 1.99 + int idx0 = IDX0(PROH, v); 1.100 + int idx1 = IDX1(PROH, v); 1.101 + int idx2 = IDX2(PROH, v); 1.102 + const unsigned char *bm; 1.103 + 1.104 +#define IMAP compose_sym3(nameprep_, VERSION, _prohibited_imap) 1.105 +#define BITMAP compose_sym3(nameprep_, VERSION, _prohibited_bitmap) 1.106 + bm = BITMAP[IMAP[IMAP[idx0] + idx1]].bm; 1.107 + return (bm[idx2 / 8] & (1 << (idx2 % 8))); 1.108 +#undef IMAP 1.109 +#undef BITMAP 1.110 +} 1.111 + 1.112 +static int 1.113 +compose_sym2(nameprep_unassigned_, VERSION) (uint32_t v) { 1.114 + int idx0 = IDX0(UNAS, v); 1.115 + int idx1 = IDX1(UNAS, v); 1.116 + int idx2 = IDX2(UNAS, v); 1.117 + const unsigned char *bm; 1.118 + 1.119 +#define IMAP compose_sym3(nameprep_, VERSION, _unassigned_imap) 1.120 +#define BITMAP compose_sym3(nameprep_, VERSION, _unassigned_bitmap) 1.121 + bm = BITMAP[IMAP[IMAP[idx0] + idx1]].bm; 1.122 + return (bm[idx2 / 8] & (1 << (idx2 % 8))); 1.123 +#undef IMAP 1.124 +#undef BITMAP 1.125 +} 1.126 + 1.127 +static idn_biditype_t 1.128 +compose_sym2(nameprep_biditype_, VERSION) (uint32_t v) { 1.129 + int idx0 = IDX0(BIDI, v); 1.130 + int idx1 = IDX1(BIDI, v); 1.131 + int idx2 = IDX2(BIDI, v); 1.132 + int offset; 1.133 + 1.134 +#define IMAP compose_sym3(nameprep_, VERSION, _bidi_imap) 1.135 +#define TABLE compose_sym3(nameprep_, VERSION, _bidi_table) 1.136 +#define DATA compose_sym3(nameprep_, VERSION, _bidi_data) 1.137 + offset = TABLE[IMAP[IMAP[idx0] + idx1]].tbl[idx2]; 1.138 + return DATA[offset]; 1.139 +#undef IMAP 1.140 +#undef TABLE 1.141 +#undef DATA 1.142 +}