5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef lint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *rcsid = "$Id: punycode.c,v 1.1 2003/06/04 00:26:06 marka Exp $";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (c) 2001,2002 Japan Network Information Center.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * All rights reserved.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * By using this file, you agree to the terms and conditions set forth bellow.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LICENSE TERMS AND CONDITIONS
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * The following License Terms and Conditions apply, unless a different
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * license is obtained from Japan Network Information Center ("JPNIC"),
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Chiyoda-ku, Tokyo 101-0047, Japan.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 1. Use, Modification and Redistribution (including distribution of any
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * modified or derived work) in source and/or binary forms is permitted
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * under this License Terms and Conditions.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 2. Redistribution of source code must retain the copyright notices as they
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * appear in each source code file, this License Terms and Conditions.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 3. Redistribution in binary form must reproduce the Copyright Notice,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * this License Terms and Conditions, in the documentation and/or other
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * materials provided with the distribution. For the purposes of binary
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * distribution the "Copyright Notice" refers to the following language:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 4. The name of JPNIC may not be used to endorse or promote products
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * derived from this Software without specific prior written approval of
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * JPNIC.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <config.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <stddef.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <stdlib.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <string.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/result.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/assert.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/logmacro.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/converter.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/ucs4.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/debug.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/punycode.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/util.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Although draft-ietf-idn-punycode-00.txt doesn't specify the ACE
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * signature, we have to choose one. In order to prevent the converted
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * name from beginning with a hyphen, we should choose a prefix rather
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * than a suffix.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef IDN_PUNYCODE_PREFIX
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDN_PUNYCODE_PREFIX "xn--"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define INVALID_UCS 0x80000000
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define MAX_UCS 0x10FFFF
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * As the draft states, it is possible that `delta' may overflow during
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * the encoding. The upper bound of 'delta' is:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * <# of chars. of input string> + <max. difference in code point> *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * <# of chars. of input string + 1>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * For this value not to be greater than 0xffffffff (since the calculation
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * is done using unsigned long, which is at least 32bit long), the maxmum
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * input string size is about 3850 characters, which is long enough for
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * a domain label...
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define PUNYCODE_MAXINPUT 3800
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Parameters.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define PUNYCODE_BASE 36
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define PUNYCODE_TMIN 1
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define PUNYCODE_TMAX 26
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define PUNYCODE_SKEW 38
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define PUNYCODE_DAMP 700
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define PUNYCODE_INITIAL_BIAS 72
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define PUNYCODE_INITIAL_N 0x80
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int punycode_getwc(const char *s, size_t len,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int bias, unsigned long *vp);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int punycode_putwc(char *s, size_t len,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long delta, int bias);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int punycode_update_bias(unsigned long delta,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t npoints, int first);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn__punycode_decode(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from, unsigned long *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *to_org = to;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long c, idx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t prefixlen = strlen(IDN_PUNYCODE_PREFIX);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t fromlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t uidx, fidx, ucslen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int first, bias;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_decode(from=\"%s\", tolen=%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_xstring(from, 50), (int)tolen));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (!idn__util_asciihaveaceprefix(from, IDN_PUNYCODE_PREFIX)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*from == '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4(from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_invalid_encoding;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews from += prefixlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fromlen = strlen(from);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Find the last delimiter, and copy the characters
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * before it verbatim.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ucslen = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (fidx = fromlen; fidx > 0; fidx--) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (from[fidx - 1] == '-') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen < fidx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (uidx = 0; uidx < fidx - 1; uidx++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to[uidx] = from[uidx];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ucslen = uidx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews first = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews bias = PUNYCODE_INITIAL_BIAS;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c = PUNYCODE_INITIAL_N;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idx = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (fidx < fromlen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int len;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long delta;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int i;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len = punycode_getwc(from + fidx, fromlen - fidx, bias, &delta);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (len == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_invalid_encoding;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fidx += len;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews bias = punycode_update_bias(delta, ucslen + 1, first);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews first = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idx += delta;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c += idx / (ucslen + 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews uidx = idx % (ucslen + 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Insert 'c' at uidx. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen-- <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (i = ucslen; i > uidx; i--)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to[i] = to[i - 1];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to[uidx] = c;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ucslen++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idx = uidx + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Terminate with NUL. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to[ucslen] = '\0';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_success;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r == idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_decode(): succcess (to=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_ucs4xstring(to_org, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_decode(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn__punycode_encode(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from, char *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *to_org = to;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long cur_code, next_code, delta;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t prefixlen = strlen(IDN_PUNYCODE_PREFIX);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t fromlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t ucsdone;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t toidx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int uidx, bias, first;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_encode(from=\"%s\", tolen=%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_ucs4xstring(from, 50), (int)tolen));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*from == '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (idn__util_ucs4haveaceprefix(from, IDN_PUNYCODE_PREFIX)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_prohibited;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen < prefixlen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews memcpy(to, IDN_PUNYCODE_PREFIX, prefixlen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to += prefixlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tolen -= prefixlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fromlen = idn_ucs4_strlen(from);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * If the input string is too long (actually too long to be sane),
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * return failure in order to prevent possible overflow.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (fromlen > PUNYCODE_MAXINPUT) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ERROR(("idn__punycode_encode(): "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "the input string is too long to convert Punycode\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_ucs4xstring(from, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ucsdone = 0; /* number of characters processed */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews toidx = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * First, pick up basic code points and copy them to 'to'.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (uidx = 0; uidx < fromlen; uidx++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (from[uidx] < 0x80) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (toidx >= tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to[toidx++] = from[uidx];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ucsdone++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * If there are any basic code points, output a delimiter
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * (hyphen-minus).
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (toidx > 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (toidx >= tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to[toidx++] = '-';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to += toidx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tolen -= toidx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Then encode non-basic characters.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews first = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cur_code = PUNYCODE_INITIAL_N;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews bias = PUNYCODE_INITIAL_BIAS;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (ucsdone < fromlen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int limit = -1, rest;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Find the smallest code point equal to or greater
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * than 'cur_code'. Also remember the index of the
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * last occurence of the code point.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (next_code = MAX_UCS, uidx = fromlen - 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews uidx >= 0; uidx--) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (from[uidx] >= cur_code && from[uidx] < next_code) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews next_code = from[uidx];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews limit = uidx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* There must be such code point. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(limit >= 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta += (next_code - cur_code) * (ucsdone + 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cur_code = next_code;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Scan the input string again, and encode characters
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * whose code point is 'cur_code'. Use 'limit' to avoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * unnecessary scan.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (uidx = 0, rest = ucsdone; uidx <= limit; uidx++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (from[uidx] < cur_code) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews rest--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (from[uidx] == cur_code) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int sz = punycode_putwc(to, tolen, delta, bias);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sz == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to += sz;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tolen -= sz;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ucsdone++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews bias = punycode_update_bias(delta, ucsdone,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews first);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews first = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta += rest + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews cur_code++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Terminate with NUL.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = '\0';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_success;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r == idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_encode(): succcess (to=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_xstring(to_org, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_encode(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewspunycode_getwc(const char *s, size_t len, int bias, unsigned long *vp) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t orglen = len;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v = 0, w = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int k;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (k = PUNYCODE_BASE - bias; len > 0; k += PUNYCODE_BASE) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int c = *s++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int t = (k < PUNYCODE_TMIN) ? PUNYCODE_TMIN :
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (k > PUNYCODE_TMAX) ? PUNYCODE_TMAX : k;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ('a' <= c && c <= 'z')
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c = c - 'a';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if ('A' <= c && c <= 'Z')
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c = c - 'A';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if ('0' <= c && c <= '9')
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c = c - '0' + 26;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c = -1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (c < 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0); /* invalid character */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v += c * w;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (c < t) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *vp = v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (orglen - len);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews w *= (PUNYCODE_BASE - t);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0); /* final character missing */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewspunycode_putwc(char *s, size_t len, unsigned long delta, int bias) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *punycode_base36 = "abcdefghijklmnopqrstuvwxyz0123456789";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int k;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *sorg = s;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (k = PUNYCODE_BASE - bias; 1; k += PUNYCODE_BASE) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int t = (k < PUNYCODE_TMIN) ? PUNYCODE_TMIN :
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (k > PUNYCODE_TMAX) ? PUNYCODE_TMAX : k;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (delta < t)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (len < 1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *s++ = punycode_base36[t + ((delta - t) % (PUNYCODE_BASE - t))];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta = (delta - t) / (PUNYCODE_BASE - t);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (len < 1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *s++ = punycode_base36[delta];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (s - sorg);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewspunycode_update_bias(unsigned long delta, size_t npoints, int first) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int k = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta /= first ? PUNYCODE_DAMP : 2;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta += delta / npoints;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (delta > ((PUNYCODE_BASE - PUNYCODE_TMIN) * PUNYCODE_TMAX) / 2) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta /= PUNYCODE_BASE - PUNYCODE_TMIN;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews k++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (PUNYCODE_BASE * k +
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (((PUNYCODE_BASE - PUNYCODE_TMIN + 1) * delta) /
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (delta + PUNYCODE_SKEW)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}