Path: blob/main/crypto/heimdal/lib/wind/punycode.c
34914 views
/*1* Copyright (c) 2004 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#ifdef HAVE_CONFIG_H34#include <config.h>35#endif36#include <string.h>37#include "windlocl.h"3839static const unsigned base = 36;40static const unsigned t_min = 1;41static const unsigned t_max = 26;42static const unsigned skew = 38;43static const unsigned damp = 700;44static const unsigned initial_n = 128;45static const unsigned initial_bias = 72;4647static unsigned48digit(unsigned n)49{50return "abcdefghijklmnopqrstuvwxyz0123456789"[n];51}5253static unsigned54adapt(unsigned delta, unsigned numpoints, int first)55{56unsigned k;5758if (first)59delta = delta / damp;60else61delta /= 2;62delta += delta / numpoints;63k = 0;64while (delta > ((base - t_min) * t_max) / 2) {65delta /= base - t_min;66k += base;67}68return k + (((base - t_min + 1) * delta) / (delta + skew));69}7071/**72* Convert an UCS4 string to a puny-coded DNS label string suitable73* when combined with delimiters and other labels for DNS lookup.74*75* @param in an UCS4 string to convert76* @param in_len the length of in.77* @param out the resulting puny-coded string. The string is not NUL78* terminatied.79* @param out_len before processing out_len should be the length of80* the out variable, after processing it will be the length of the out81* string.82*83* @return returns 0 on success, an wind error code otherwise84* @ingroup wind85*/8687int88wind_punycode_label_toascii(const uint32_t *in, size_t in_len,89char *out, size_t *out_len)90{91unsigned n = initial_n;92unsigned delta = 0;93unsigned bias = initial_bias;94unsigned h = 0;95unsigned b;96unsigned i;97unsigned o = 0;98unsigned m;99100for (i = 0; i < in_len; ++i) {101if (in[i] < 0x80) {102++h;103if (o >= *out_len)104return WIND_ERR_OVERRUN;105out[o++] = in[i];106}107}108b = h;109if (b > 0) {110if (o >= *out_len)111return WIND_ERR_OVERRUN;112out[o++] = 0x2D;113}114/* is this string punycoded */115if (h < in_len) {116if (o + 4 >= *out_len)117return WIND_ERR_OVERRUN;118memmove(out + 4, out, o);119memcpy(out, "xn--", 4);120o += 4;121}122123while (h < in_len) {124m = (unsigned)-1;125for (i = 0; i < in_len; ++i)126if(in[i] < m && in[i] >= n)127m = in[i];128129delta += (m - n) * (h + 1);130n = m;131for (i = 0; i < in_len; ++i) {132if (in[i] < n) {133++delta;134} else if (in[i] == n) {135unsigned q = delta;136unsigned k;137for (k = base; ; k += base) {138unsigned t;139if (k <= bias)140t = t_min;141else if (k >= bias + t_max)142t = t_max;143else144t = k - bias;145if (q < t)146break;147if (o >= *out_len)148return WIND_ERR_OVERRUN;149out[o++] = digit(t + ((q - t) % (base - t)));150q = (q - t) / (base - t);151}152if (o >= *out_len)153return WIND_ERR_OVERRUN;154out[o++] = digit(q);155/* output */156bias = adapt(delta, h + 1, h == b);157delta = 0;158++h;159}160}161++delta;162++n;163}164165*out_len = o;166return 0;167}168169170