5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *rcsid = "$Id: punycode.c,v 1.1 2003/06/04 00:26:06 marka Exp $";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (c) 2001,2002 Japan Network Information Center.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * All rights reserved.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * By using this file, you agree to the terms and conditions set forth bellow.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LICENSE TERMS AND CONDITIONS
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 * 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 * 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 * 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 * 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 * 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 * 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 * 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 * Parameters.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int punycode_getwc(const char *s, size_t len,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int punycode_update_bias(unsigned long delta,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn__punycode_decode(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from, unsigned long *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t prefixlen = strlen(IDN_PUNYCODE_PREFIX);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_decode(from=\"%s\", tolen=%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (!idn__util_asciihaveaceprefix(from, IDN_PUNYCODE_PREFIX)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Find the last delimiter, and copy the characters
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * before it verbatim.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len = punycode_getwc(from + fidx, fromlen - fidx, bias, &delta);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews bias = punycode_update_bias(delta, ucslen + 1, first);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Insert 'c' at uidx. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen-- <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Terminate with NUL. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_decode(): succcess (to=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_decode(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn__punycode_encode(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from, char *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t prefixlen = strlen(IDN_PUNYCODE_PREFIX);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_encode(from=\"%s\", tolen=%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_ucs4xstring(from, 50), (int)tolen));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (idn__util_ucs4haveaceprefix(from, IDN_PUNYCODE_PREFIX)) {
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 "the input string is too long to convert Punycode\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ucsdone = 0; /* number of characters processed */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * First, pick up basic code points and copy them to 'to'.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * If there are any basic code points, output a delimiter
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * (hyphen-minus).
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Then encode non-basic characters.
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 if (from[uidx] >= cur_code && from[uidx] < next_code) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* There must be such code point. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews delta += (next_code - cur_code) * (ucsdone + 1);
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 for (uidx = 0, rest = ucsdone; uidx <= limit; uidx++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int sz = punycode_putwc(to, tolen, delta, bias);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Terminate with NUL.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_encode(): succcess (to=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn__punycode_encode(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewspunycode_getwc(const char *s, size_t len, int bias, unsigned long *vp) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v = 0, w = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (k = PUNYCODE_BASE - bias; len > 0; k += PUNYCODE_BASE) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int c = *s++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c = c - 'a';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c = c - 'A';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0); /* invalid character */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (c < t) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0); /* final character missing */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewspunycode_putwc(char *s, size_t len, unsigned long delta, int bias) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *punycode_base36 = "abcdefghijklmnopqrstuvwxyz0123456789";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (k = PUNYCODE_BASE - bias; 1; k += PUNYCODE_BASE) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *s++ = punycode_base36[t + ((delta - t) % (PUNYCODE_BASE - t))];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (s - sorg);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewspunycode_update_bias(unsigned long delta, size_t npoints, int first) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (delta > ((PUNYCODE_BASE - PUNYCODE_TMIN) * PUNYCODE_TMAX) / 2) {