#ifndef lint
#endif
/*
* Copyright (c) 2001 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>
/*
* Unicode surrogate pair.
*/
#define COMBINE_SURROGATE(h, l) \
/*
* ASCII ctype macros.
* Note that these macros evaluate the argument multiple times. Be careful.
*/
#define ASCII_TOUPPER(c) \
(('a' <= (c) && (c) <= 'z') ? ((c) - 'a' + 'A') : (c))
#define ASCII_TOLOWER(c) \
(('A' <= (c) && (c) <= 'Z') ? ((c) - 'A' + 'a') : (c))
unsigned long v;
idn_result_t r;
TRACE(("idn_ucs4_ucs4toutf16(ucs4=\"%s\", tolen=%d)\n",
while (*ucs4 != '\0') {
v = *ucs4++;
if (IS_SURROGATE_LOW(v) || IS_SURROGATE_HIGH(v)) {
WARNING(("idn_ucs4_ucs4toutf16: UCS4 string contains "
"surrogate pair\n"));
r = idn_invalid_encoding;
goto ret;
} else if (v > 0xffff) {
/* Convert to surrogate pair */
if (v >= 0x110000) {
r = idn_invalid_encoding;
goto ret;
}
if (tolen < 2) {
r = idn_buffer_overflow;
goto ret;
}
*utf16p++ = SURROGATE_HIGH(v);
*utf16p++ = SURROGATE_LOW(v);
tolen -= 2;
} else {
if (tolen < 1) {
r = idn_buffer_overflow;
goto ret;
}
*utf16p++ = v;
tolen--;
}
}
if (tolen < 1) {
r = idn_buffer_overflow;
goto ret;
}
*utf16p = '\0';
r = idn_success;
ret:
if (r == idn_success) {
TRACE(("idn_ucs4_ucs4toutf16(): success (utf16=\"%s\")\n",
} else {
TRACE(("idn_ucs4_ucs4toutf16(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
idn_result_t r;
TRACE(("idn_ucs4_utf16toucs4(utf16=\"%s\", tolen=%d)\n",
while (*utf16 != '\0') {
if (tolen < 1) {
r = idn_buffer_overflow;
goto ret;
}
if (IS_SURROGATE_HIGH(v0)) {
if (!IS_SURROGATE_LOW(v1)) {
WARNING(("idn_ucs4_utf16toucs4: "
"corrupted surrogate pair\n"));
r = idn_invalid_encoding;
goto ret;
}
tolen--;
utf16 += 2;
} else {
tolen--;
utf16++;
}
}
if (tolen < 1) {
r = idn_buffer_overflow;
goto ret;
}
*ucs4p = '\0';
r = idn_success;
ret:
if (r == idn_success) {
TRACE(("idn_ucs4_utf16toucs4(): success (ucs4=\"%s\")\n",
} else {
TRACE(("idn_ucs4_utf16toucs4(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
unsigned long v, min;
unsigned char c;
int width;
int i;
idn_result_t r;
TRACE(("idn_ucs4_utf8toucs4(utf8=\"%s\", tolen=%d)\n",
while(*utf8p != '\0') {
c = *utf8p++;
if (c < 0x80) {
v = c;
min = 0;
width = 1;
} else if (c < 0xc0) {
WARNING(("idn_ucs4_utf8toucs4: invalid character\n"));
r = idn_invalid_encoding;
goto ret;
} else if (c < 0xe0) {
v = c & 0x1f;
min = 0x80;
width = 2;
} else if (c < 0xf0) {
v = c & 0x0f;
min = 0x800;
width = 3;
} else if (c < 0xf8) {
v = c & 0x07;
min = 0x10000;
width = 4;
} else if (c < 0xfc) {
v = c & 0x03;
min = 0x200000;
width = 5;
} else if (c < 0xfe) {
v = c & 0x01;
min = 0x4000000;
width = 6;
} else {
WARNING(("idn_ucs4_utf8toucs4: invalid character\n"));
r = idn_invalid_encoding;
goto ret;
}
for (i = width - 1; i > 0; i--) {
c = *utf8p++;
if (c < 0x80 || 0xc0 <= c) {
WARNING(("idn_ucs4_utf8toucs4: "
"invalid character\n"));
r = idn_invalid_encoding;
goto ret;
}
v = (v << 6) | (c & 0x3f);
}
if (v < min) {
WARNING(("idn_ucs4_utf8toucs4: invalid character\n"));
r = idn_invalid_encoding;
goto ret;
}
if (IS_SURROGATE_LOW(v) || IS_SURROGATE_HIGH(v)) {
WARNING(("idn_ucs4_utf8toucs4: UTF-8 string contains "
"surrogate pair\n"));
r = idn_invalid_encoding;
goto ret;
}
if (tolen < 1) {
r = idn_buffer_overflow;
goto ret;
}
tolen--;
*ucs4p++ = v;
}
if (tolen < 1) {
r = idn_buffer_overflow;
goto ret;
}
*ucs4p = '\0';
r = idn_success;
ret:
if (r == idn_success) {
TRACE(("idn_ucs4_utf8toucs4(): success (ucs4=\"%s\")\n",
} else {
TRACE(("idn_ucs4_utf8toucs4(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
unsigned long v;
int width;
int mask;
int offset;
idn_result_t r;
TRACE(("idn_ucs4_ucs4toutf8(ucs4=\"%s\", tolen=%d)\n",
while (*ucs4 != '\0') {
v = *ucs4++;
if (IS_SURROGATE_LOW(v) || IS_SURROGATE_HIGH(v)) {
WARNING(("idn_ucs4_ucs4toutf8: UCS4 string contains "
"surrogate pair\n"));
r = idn_invalid_encoding;
goto ret;
}
if (v < 0x80) {
mask = 0;
width = 1;
} else if (v < 0x800) {
mask = 0xc0;
width = 2;
} else if (v < 0x10000) {
mask = 0xe0;
width = 3;
} else if (v < 0x200000) {
mask = 0xf0;
width = 4;
} else if (v < 0x4000000) {
mask = 0xf8;
width = 5;
} else if (v < 0x80000000) {
mask = 0xfc;
width = 6;
} else {
WARNING(("idn_ucs4_ucs4toutf8: invalid character\n"));
r = idn_invalid_encoding;
goto ret;
}
r = idn_buffer_overflow;
goto ret;
}
mask = 0x80;
while (offset > 0) {
offset -= 6;
}
}
if (tolen < 1) {
r = idn_buffer_overflow;
goto ret;
}
*utf8p = '\0';
r = idn_success;
ret:
if (r == idn_success) {
TRACE(("idn_ucs4_ucs4toutf8(): success (utf8=\"%s\")\n",
} else {
TRACE(("idn_ucs4_ucs4toutf8(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
/* nothing to do */ ;
return (len);
}
unsigned long *
while (*from != '\0')
*to = '\0';
return (result);
}
unsigned long *
while (*to != '\0')
to++;
while (*from != '\0')
*to = '\0';
return (result);
}
int
while (*str1 != '\0') {
return (1);
return (-1);
str1++;
str2++;
}
return (1);
return (-1);
return (0);
}
int
while (*str1 != '\0') {
return (1);
return (-1);
str1++;
str2++;
}
return (1);
return (-1);
return (0);
}
unsigned long *
unsigned long *dupstr;
return NULL;
return dupstr;
}