1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/dns/race.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,235 @@ 1.4 +/* 1.5 + * Copyright (c) 2000,2001,2002 Japan Network Information Center. 1.6 + * All rights reserved. 1.7 + * 1.8 + * By using this file, you agree to the terms and conditions set forth bellow. 1.9 + * 1.10 + * LICENSE TERMS AND CONDITIONS 1.11 + * 1.12 + * The following License Terms and Conditions apply, unless a different 1.13 + * license is obtained from Japan Network Information Center ("JPNIC"), 1.14 + * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda, 1.15 + * Chiyoda-ku, Tokyo 101-0047, Japan. 1.16 + * 1.17 + * 1. Use, Modification and Redistribution (including distribution of any 1.18 + * modified or derived work) in source and/or binary forms is permitted 1.19 + * under this License Terms and Conditions. 1.20 + * 1.21 + * 2. Redistribution of source code must retain the copyright notices as they 1.22 + * appear in each source code file, this License Terms and Conditions. 1.23 + * 1.24 + * 3. Redistribution in binary form must reproduce the Copyright Notice, 1.25 + * this License Terms and Conditions, in the documentation and/or other 1.26 + * materials provided with the distribution. For the purposes of binary 1.27 + * distribution the "Copyright Notice" refers to the following language: 1.28 + * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved." 1.29 + * 1.30 + * 4. The name of JPNIC may not be used to endorse or promote products 1.31 + * derived from this Software without specific prior written approval of 1.32 + * JPNIC. 1.33 + * 1.34 + * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC 1.35 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.36 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 1.37 + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE 1.38 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.39 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.40 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 1.41 + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 1.42 + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 1.43 + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 1.44 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 1.45 + */ 1.46 + 1.47 + 1.48 +#include <stddef.h> 1.49 +#include <stdlib.h> 1.50 +#include <string.h> 1.51 + 1.52 +#include "nsIDNKitInterface.h" 1.53 + 1.54 + 1.55 +#define RACE_2OCTET_MODE 0xd8 1.56 +#define RACE_ESCAPE 0xff 1.57 +#define RACE_ESCAPE_2ND 0x99 1.58 + 1.59 +/* 1.60 + * Compression type. 1.61 + */ 1.62 +enum { 1.63 + compress_one, /* all characters are in a single row */ 1.64 + compress_two, /* row 0 and another row */ 1.65 + compress_none /* nope */ 1.66 +}; 1.67 + 1.68 + 1.69 +idn_result_t 1.70 +race_decode_decompress(const char *from, uint16_t *buf, size_t buflen) 1.71 +{ 1.72 + uint16_t *p = buf; 1.73 + unsigned int bitbuf = 0; 1.74 + int bitlen = 0; 1.75 + unsigned int i, j; 1.76 + size_t len; 1.77 + 1.78 + while (*from != '\0') { 1.79 + int c = *from++; 1.80 + int x; 1.81 + 1.82 + if ('a' <= c && c <= 'z') 1.83 + x = c - 'a'; 1.84 + else if ('A' <= c && c <= 'Z') 1.85 + x = c - 'A'; 1.86 + else if ('2' <= c && c <= '7') 1.87 + x = c - '2' + 26; 1.88 + else 1.89 + return (idn_invalid_encoding); 1.90 + 1.91 + bitbuf = (bitbuf << 5) + x; 1.92 + bitlen += 5; 1.93 + if (bitlen >= 8) { 1.94 + *p++ = (bitbuf >> (bitlen - 8)) & 0xff; 1.95 + bitlen -= 8; 1.96 + } 1.97 + } 1.98 + len = p - buf; 1.99 + 1.100 + /* 1.101 + * Now 'buf' holds the decoded string. 1.102 + */ 1.103 + 1.104 + /* 1.105 + * Decompress. 1.106 + */ 1.107 + if (buf[0] == RACE_2OCTET_MODE) { 1.108 + if ((len - 1) % 2 != 0) 1.109 + return (idn_invalid_encoding); 1.110 + for (i = 1, j = 0; i < len; i += 2, j++) 1.111 + buf[j] = (buf[i] << 8) + buf[i + 1]; 1.112 + len = j; 1.113 + } else { 1.114 + uint16_t c = buf[0] << 8; /* higher octet */ 1.115 + 1.116 + for (i = 1, j = 0; i < len; j++) { 1.117 + if (buf[i] == RACE_ESCAPE) { 1.118 + if (i + 1 >= len) 1.119 + return (idn_invalid_encoding); 1.120 + else if (buf[i + 1] == RACE_ESCAPE_2ND) 1.121 + buf[j] = c | 0xff; 1.122 + else 1.123 + buf[j] = buf[i + 1]; 1.124 + i += 2; 1.125 + 1.126 + } else if (buf[i] == 0x99 && c == 0x00) { 1.127 + /* 1.128 + * The RACE specification says this is error. 1.129 + */ 1.130 + return (idn_invalid_encoding); 1.131 + 1.132 + } else { 1.133 + buf[j] = c | buf[i++]; 1.134 + } 1.135 + } 1.136 + len = j; 1.137 + } 1.138 + buf[len] = '\0'; 1.139 + 1.140 + return (idn_success); 1.141 +} 1.142 + 1.143 +idn_result_t 1.144 +race_compress_encode(const uint16_t *p, int compress_mode, 1.145 + char *to, size_t tolen) 1.146 +{ 1.147 + uint32_t bitbuf = *p++; /* bit stream buffer */ 1.148 + int bitlen = 8; /* # of bits in 'bitbuf' */ 1.149 + 1.150 + while (*p != '\0' || bitlen > 0) { 1.151 + unsigned int c = *p; 1.152 + 1.153 + if (c == '\0') { 1.154 + /* End of data. Flush. */ 1.155 + bitbuf <<= (5 - bitlen); 1.156 + bitlen = 5; 1.157 + } else if (compress_mode == compress_none) { 1.158 + /* Push 16 bit data. */ 1.159 + bitbuf = (bitbuf << 16) | c; 1.160 + bitlen += 16; 1.161 + p++; 1.162 + } else {/* compress_mode == compress_one/compress_two */ 1.163 + /* Push 8 or 16 bit data. */ 1.164 + if (compress_mode == compress_two && 1.165 + (c & 0xff00) == 0) { 1.166 + /* Upper octet is zero (and not U1). */ 1.167 + bitbuf = (bitbuf << 16) | 0xff00 | c; 1.168 + bitlen += 16; 1.169 + } else if ((c & 0xff) == 0xff) { 1.170 + /* Lower octet is 0xff. */ 1.171 + bitbuf = (bitbuf << 16) | 1.172 + (RACE_ESCAPE << 8) | RACE_ESCAPE_2ND; 1.173 + bitlen += 16; 1.174 + } else { 1.175 + /* Just output lower octet. */ 1.176 + bitbuf = (bitbuf << 8) | (c & 0xff); 1.177 + bitlen += 8; 1.178 + } 1.179 + p++; 1.180 + } 1.181 + 1.182 + /* 1.183 + * Output bits in 'bitbuf' in 5-bit unit. 1.184 + */ 1.185 + while (bitlen >= 5) { 1.186 + int x; 1.187 + 1.188 + /* Get top 5 bits. */ 1.189 + x = (bitbuf >> (bitlen - 5)) & 0x1f; 1.190 + bitlen -= 5; 1.191 + 1.192 + /* Encode. */ 1.193 + if (x < 26) 1.194 + x += 'a'; 1.195 + else 1.196 + x = (x - 26) + '2'; 1.197 + 1.198 + if (tolen < 1) 1.199 + return (idn_buffer_overflow); 1.200 + 1.201 + *to++ = x; 1.202 + tolen--; 1.203 + } 1.204 + } 1.205 + 1.206 + if (tolen <= 0) 1.207 + return (idn_buffer_overflow); 1.208 + 1.209 + *to = '\0'; 1.210 + return (idn_success); 1.211 +} 1.212 + 1.213 +int 1.214 +get_compress_mode(uint16_t *p) { 1.215 + int zero = 0; 1.216 + unsigned int upper = 0; 1.217 + uint16_t *modepos = p - 1; 1.218 + 1.219 + while (*p != '\0') { 1.220 + unsigned int hi = *p++ & 0xff00; 1.221 + 1.222 + if (hi == 0) { 1.223 + zero++; 1.224 + } else if (hi == upper) { 1.225 + ; 1.226 + } else if (upper == 0) { 1.227 + upper = hi; 1.228 + } else { 1.229 + *modepos = RACE_2OCTET_MODE; 1.230 + return (compress_none); 1.231 + } 1.232 + } 1.233 + *modepos = upper >> 8; 1.234 + if (upper > 0 && zero > 0) 1.235 + return (compress_two); 1.236 + else 1.237 + return (compress_one); 1.238 +}