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