#ifndef lint
#endif
/*
* Copyright (c) 2000,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/normalizer.h>
#include <idn/mapselector.h>
#include <idn/delimitermap.h>
#ifndef IDN_UTF8_ENCODING_NAME
#endif
#ifndef WITHOUT_ICONV
#define ENCODE_MASK \
#define DECODE_MASK \
#else
#define ENCODE_MASK \
#define DECODE_MASK \
#endif
/*
* label to convert.
*/
struct labellist {
unsigned long *name;
unsigned long *undo_name;
int dot_followed;
};
static void idn_res_initialize(void);
const unsigned long *name);
static const unsigned long *
static const unsigned long *
unsigned long *name,
#ifndef WITHOUT_ICONV
#endif
const unsigned long *original_name);
static int initialized;
static int enabled;
void
if (!initialized) {
}
if (on_off == 0) {
enabled = 0;
} else {
enabled = 1;
}
}
static void
idn_res_initialize(void) {
if (!initialized) {
enabled = 1;
} else {
enabled = 0;
}
initialized = 1;
}
}
idn_result_t r;
int from_is_root;
int idn_is_ace;
TRACE(("idn_res_encodename(actions=%s, from=\"%s\", tolen=%d)\n",
if (actions & ~ENCODE_MASK) {
WARNING(("idn_res_encodename: invalid actions 0x%x\n",
actions));
r = idn_invalid_action;
goto ret;
}
if (!initialized)
goto ret;
} else if (tolen <= 0) {
r = idn_buffer_overflow;
goto ret;
}
if (actions & IDN_ENCODE_QUERY) {
#ifndef WITHOUT_ICONV
#else
#endif
}
/*
* Convert `from' to UCS4.
*/
#ifndef WITHOUT_ICONV
if (local_converter == NULL) {
r = idn_invalid_name;
goto ret;
}
#endif
if (idn_converter != NULL &&
idn_is_ace = 1;
else
idn_is_ace = 0;
for (;;) {
void *new_buffer;
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
buffer = (unsigned long *)new_buffer;
if (actions & IDN_LOCALCONV) {
} else {
}
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
buffer_length *= 2;
}
if (*buffer == '\0') {
if (tolen <= 0) {
r = idn_buffer_overflow;
goto ret;
}
*to = '\0';
r = idn_success;
goto ret;
}
/*
* Delimiter map.
*/
if (actions & IDN_DELIMMAP) {
TRACE(("res delimitermap(name=\"%s\")\n",
if (delimiter_mapper != NULL) {
if (r != idn_success)
goto ret;
}
TRACE(("res delimitermap(): success (name=\"%s\")\n",
}
/*
* Split the name into a list of labels.
*/
if (r != idn_success)
goto ret;
/*
* Perform conversions and tests.
*/
l = labellist_previous(l)) {
if (!idn__util_ucs4isasciirange(labellist_getname(l))) {
if (actions & IDN_LOCALMAP) {
r = label_localmap(ctx, l);
if (r != idn_success)
goto ret;
}
}
if (!idn__util_ucs4isasciirange(labellist_getname(l))) {
if (r != idn_success)
goto ret;
}
if (actions & IDN_NORMALIZE) {
r = label_normalize(ctx, l);
if (r != idn_success)
goto ret;
}
if (actions & IDN_PROHCHECK) {
r = label_prohcheck(ctx, l);
if (r == idn_prohibited &&
(actions & IDN_UNDOIFERR)) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
if (actions & IDN_UNASCHECK) {
r = label_unascheck(ctx, l);
if (r == idn_prohibited &&
(actions & IDN_UNDOIFERR)) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
if (actions & IDN_BIDICHECK) {
r = label_bidicheck(ctx, l);
if (r == idn_prohibited &&
(actions & IDN_UNDOIFERR)) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
}
if (actions & IDN_ASCCHECK) {
r = label_asccheck(ctx, l);
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
if (!idn__util_ucs4isasciirange(labellist_getname(l))) {
r = label_idnencode_ace(ctx, l);
if (r != idn_success)
goto ret;
}
}
if (idn_is_ace)
r = label_lencheck_ace(ctx, l);
else
r = label_lencheck_nonace(ctx, l);
if (r == idn_invalid_length &&
(actions & IDN_UNDOIFERR)) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
}
/*
* Concat a list of labels to a name.
*/
for (;;) {
void *new_buffer;
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
buffer = (unsigned long *)new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
buffer_length *= 2;
}
tolen);
} else {
}
ret:
if (r == idn_success) {
TRACE(("idn_res_encodename(): success (to=\"%s\")\n",
} else {
}
if (local_converter != NULL)
if (idn_converter != NULL)
return (r);
}
idn_result_t r;
int idn_is_ace;
TRACE(("idn_res_decodename(actions=%s, from=\"%s\", tolen=%d)\n",
if (actions & ~DECODE_MASK) {
WARNING(("idn_res_decodename: invalid actions 0x%x\n",
actions));
r = idn_invalid_action;
goto ret;
}
if (!initialized)
goto ret;
} else if (tolen <= 0) {
r = idn_buffer_overflow;
goto ret;
}
if (actions & IDN_DECODE_QUERY) {
#ifndef WITHOUT_ICONV
#else
#endif
}
/*
* Convert `from' to UCS4.
*/
#ifndef WITHOUT_ICONV
if (local_converter == NULL) {
r = idn_invalid_name;
goto ret;
}
#endif
if (idn_converter != NULL &&
idn_is_ace = 1;
else
idn_is_ace = 0;
for (;;) {
void *new_buffer;
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
buffer = (unsigned long *)new_buffer;
if ((actions & IDN_IDNCONV) &&
} else {
}
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
buffer_length *= 2;
}
if (*buffer == '\0') {
if (tolen <= 0) {
r = idn_buffer_overflow;
goto ret;
}
*to = '\0';
r = idn_success;
goto ret;
}
/*
* Delimiter map.
*/
if (actions & IDN_DELIMMAP) {
TRACE(("res delimitermap(name=\"%s\")\n",
if (delimiter_mapper != NULL) {
if (r != idn_success)
goto ret;
}
TRACE(("res delimitermap(): success (name=\"%s\")\n",
}
/*
* Split the name into a list of labels.
*/
if (r != idn_success)
goto ret;
/*
* Perform conversions and tests.
*/
l = labellist_previous(l)) {
saved_name = NULL;
if (!idn__util_ucs4isasciirange(labellist_getname(l))) {
if (r != idn_success)
goto ret;
}
if (actions & IDN_NORMALIZE) {
r = label_normalize(ctx, l);
if (r != idn_success)
goto ret;
}
if (actions & IDN_PROHCHECK) {
r = label_prohcheck(ctx, l);
if (r == idn_prohibited) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
if (actions & IDN_UNASCHECK) {
r = label_unascheck(ctx, l);
if (r == idn_prohibited) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
if (actions & IDN_BIDICHECK) {
r = label_bidicheck(ctx, l);
if (r == idn_prohibited) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
}
if (saved_name == NULL) {
r = idn_nomemory;
goto ret;
}
r = label_idndecode(ctx, l);
if (r == idn_invalid_encoding) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
if (r == idn_invalid_encoding) {
labellist_undo(l);
continue;
} else if (r != idn_success) {
goto ret;
}
}
#ifndef WITHOUT_ICONV
if (actions & IDN_LOCALCONV) {
r = label_localdecodecheck(ctx, l);
if (r != idn_success)
goto ret;
}
#endif
}
/*
* Concat a list of labels to a name.
*/
for (;;) {
void *new_buffer;
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
buffer = (unsigned long *)new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
buffer_length *= 2;
}
if (actions & IDN_LOCALCONV) {
tolen);
} else {
}
ret:
if (r == idn_success) {
TRACE(("idn_res_decodename(): success (to=\"%s\")\n",
} else {
}
if (local_converter != NULL)
if (idn_converter != NULL)
return (r);
}
#ifdef WITHOUT_ICONV
return idn_failure;
#else /* WITHOUT_ICONV */
idn_result_t r;
unsigned long *buffer_ucs4 = NULL;
char *buffer_utf8 = NULL;
TRACE(("idn_res_decodename2(actions=%s, from=\"%s\", tolen=%d, "
"auxencoding=\"%s\")\n",
if (!initialized)
goto ret;
} else if (tolen <= 0) {
r = idn_buffer_overflow;
goto ret;
}
if (auxencoding == NULL ||
}
/*
* Convert `from' to UCS4.
*/
if (r != idn_success) {
goto ret;
}
if (aux_converter == NULL) {
r = idn_failure;
goto ret;
}
for (;;) {
void *new_buffer;
sizeof(*buffer_ucs4) * buffer_length);
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
buffer_ucs4 = (unsigned long *)new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
buffer_length *= 2;
}
if (*buffer_ucs4 == '\0') {
if (tolen <= 0) {
r = idn_buffer_overflow;
goto ret;
}
*to = '\0';
r = idn_success;
goto ret;
}
/*
* Convert `buffer_ucs4' to UTF-8.
*/
for (;;) {
void *new_buffer;
sizeof(*buffer_utf8) * buffer_length);
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
buffer_utf8 = (char *)new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
buffer_length *= 2;
}
if (*buffer_utf8 == '\0') {
if (tolen <= 0) {
r = idn_buffer_overflow;
goto ret;
}
*to = '\0';
r = idn_success;
goto ret;
}
ret:
if (r == idn_success) {
TRACE(("idn_res_decodename2(): success (to=\"%s\")\n",
} else {
}
if (aux_converter != NULL)
return (r);
#endif /* WITHOUT_ICONV */
}
static idn_result_t
return (idn_buffer_overflow);
return (idn_success);
}
static idn_result_t
idn_result_t r;
while (*name != '\0') {
; /* nothing to be done */
new_label = (labellist_t)
r = idn_nomemory;
goto ret;
}
if (head_label == NULL)
malloc(sizeof(long) * malloc_length);
r = idn_nomemory;
goto ret;
}
malloc(sizeof(long) * malloc_length);
r = idn_nomemory;
goto ret;
}
if (tail_label != NULL) {
}
if (*endp == '.')
else
}
*labelp = head_label;
r = idn_success;
ret:
if (r != idn_success) {
}
if (head_label != NULL)
}
return (r);
}
static void
free(l);
}
}
static idn_result_t
unsigned long *new_name;
new_name = (unsigned long *)
return (idn_nomemory);
}
return (idn_success);
}
static const unsigned long *
}
static const unsigned long *
labellist_t l;
return (idn_mapselector_getnotld());
; /* nothing to be done */
return (l->name);
}
static idn_result_t
labellist_t l;
length++; /* for NUL */
if (name_length < length)
return (idn_buffer_overflow);
*name = '\0';
if (l->dot_followed)
}
return (idn_success);
}
static void
}
static labellist_t
labellist_t l;
return (NULL);
; /* nothing to be done */
return (l);
}
static labellist_t
}
#ifndef WITHOUT_ICONV
static idn_result_t
const unsigned long *from;
idn_result_t r;
TRACE(("res ucs4tolocal_check(label=\"%s\")\n",
if (local_converter == NULL) {
r = idn_success;
goto ret;
}
for (;;) {
char *new_buffer;
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
to = new_buffer;
if (r == idn_success)
break;
else if (r == idn_nomapping) {
if (r != idn_success)
goto ret;
break;
} else if (r != idn_buffer_overflow) {
goto ret;
}
to_length *= 2;
}
r = idn_success;
ret:
if (local_converter != NULL)
return (r);
}
#endif /* !WITHOUT_ICONV */
static idn_result_t
const unsigned long *from;
idn_result_t r;
TRACE(("res idntoucs4(label=\"%s\")\n",
if (idn_converter == NULL) {
r = idn_success;
goto ret;
}
for (;;) {
char *new_buffer;
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
from_length *= 2;
}
for (;;) {
unsigned long *new_buffer;
new_buffer = (unsigned long *)
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
to = new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
to_length *= 2;
}
ret:
if (r == idn_success) {
TRACE(("res idntoucs4(): success (label=\"%s\")\n",
50)));
} else {
}
if (idn_converter != NULL)
return (r);
}
static idn_result_t
const unsigned long *from;
idn_result_t r;
TRACE(("res ucs4toidn(label=\"%s\")\n",
if (idn_converter == NULL) {
r = idn_success;
goto ret;
}
for (;;) {
char *new_buffer;
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
to_length *= 2;
}
for (;;) {
unsigned long *new_buffer;
new_buffer = (unsigned long *)
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
to = new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
to_length *= 2;
}
if (r != idn_success)
goto ret;
ret:
if (r == idn_success) {
TRACE(("res ucs4toidn(): success (label=\"%s\")\n",
50)));
} else {
}
if (idn_converter != NULL)
return (r);
}
static idn_result_t
const unsigned long *from;
const unsigned long *tld;
idn_result_t r;
TRACE(("res localmap(label=\"%s\", tld=\"%s\")\n",
if (local_mapper == NULL) {
r = idn_success;
goto ret;
}
for (;;) {
unsigned long *new_buffer;
new_buffer = (unsigned long *)
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
to = new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
to_length *= 2;
}
ret:
if (r == idn_success) {
TRACE(("res localmap(): success (label=\"%s\")\n",
50)));
} else {
}
if (local_mapper != NULL)
return (r);
}
static idn_result_t
const unsigned long *from;
idn_result_t r;
r = idn_success;
goto ret;
}
for (;;) {
unsigned long *new_buffer;
new_buffer = (unsigned long *)
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
to = new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
to_length *= 2;
}
ret:
if (r == idn_success) {
TRACE(("res map(): success (label=\"%s\")\n",
50)));
} else {
}
return (r);
}
static idn_result_t
const unsigned long *from;
idn_result_t r;
TRACE(("res normalzie(label=\"%s\")\n",
if (normalizer == NULL) {
r = idn_success;
goto ret;
}
for (;;) {
unsigned long *new_buffer;
new_buffer = (unsigned long *)
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
to = new_buffer;
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
to_length *= 2;
}
ret:
if (r == idn_success) {
TRACE(("res normalize(): success (label=\"%s\")\n",
50)));
} else {
}
if (normalizer != NULL)
return (r);
}
static idn_result_t
idn_result_t r;
TRACE(("res prohcheck(label=\"%s\")\n",
if (prohibit_checker == NULL) {
r = idn_success;
goto ret;
}
r = idn_prohibited;
ret:
return (r);
}
static idn_result_t
idn_result_t r;
TRACE(("res unascheck(label=\"%s\")\n",
if (unassigned_checker == NULL) {
r = idn_success;
goto ret;
}
r = idn_prohibited;
ret:
return (r);
}
static idn_result_t
idn_result_t r;
TRACE(("res bidicheck(label=\"%s\")\n",
if (bidi_checker == NULL) {
r = idn_success;
goto ret;
}
r = idn_prohibited;
ret:
return (r);
}
static idn_result_t
const unsigned long *name, *n;
idn_result_t r;
TRACE(("res asccheck(label=\"%s\")\n",
if (*name == '-') {
r = idn_prohibited;
goto ret;
}
for (n = name; *n != '\0'; n++) {
if (*n <= '\177') {
if ((*n < '0' || *n > '9') &&
(*n < 'A' || *n > 'Z') &&
(*n < 'a' || *n > 'z') &&
*n != '-') {
r = idn_prohibited;
goto ret;
}
}
}
r = idn_prohibited;
goto ret;
}
r = idn_success;
ret:
return (r);
}
static idn_result_t
const unsigned long *name;
idn_result_t r;
TRACE(("res lencheck(label=\"%s\")\n",
r = idn_invalid_length;
goto ret;
}
r = idn_success;
ret:
return (r);
}
static idn_result_t
const unsigned long *from;
idn_result_t r;
TRACE(("res lencheck(label=\"%s\")\n",
for (;;) {
void *new_buffer;
if (new_buffer == NULL) {
r = idn_nomemory;
goto ret;
}
buffer = (char *)new_buffer;
if (idn_converter != NULL) {
} else {
}
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
buffer_length *= 2;
}
r = idn_invalid_length;
goto ret;
}
r = idn_success;
ret:
if (idn_converter != NULL)
return (r);
}
static idn_result_t
const unsigned long *original_name) {
const unsigned long *rt_name;
const unsigned long *cur_name;
idn_result_t r;
TRACE(("res rtcheck(label=\"%s\", org_label=\"%s\")\n",
if (r != idn_success)
goto ret;
if (*original_name == '\0')
r = idn_success;
else
r = idn_invalid_encoding;
goto ret;
}
if (r != idn_success)
goto ret;
if (r != idn_success)
goto ret;
if (r != idn_success)
goto ret;
if (actions & IDN_UNASCHECK) {
if (r != idn_success)
goto ret;
}
if (r != idn_success)
goto ret;
}
if (actions & IDN_ASCCHECK) {
if (r != idn_success)
goto ret;
}
if (r != idn_success)
goto ret;
}
if (r != idn_success)
goto ret;
TRACE(("res rtcheck(): round trip failed, org =\"%s\", rt=\"%s\"\n",
r = idn_invalid_encoding;
goto ret;
}
r = idn_success;
ret:
if (r != idn_nomemory && r != idn_success)
r = idn_invalid_encoding;
return (r);
}
const char *
buf[0] = '\0';
if (actions == IDN_ENCODE_QUERY)
else if (actions == IDN_DECODE_QUERY)
else if (actions == IDN_ENCODE_APP)
else if (actions == IDN_DECODE_APP)
else if (actions == IDN_ENCODE_STORED)
else if (actions == IDN_DECODE_STORED)
else {
if (actions & IDN_LOCALCONV)
if (actions & IDN_DELIMMAP)
if (actions & IDN_LOCALMAP)
if (actions & IDN_NORMALIZE)
if (actions & IDN_PROHCHECK)
if (actions & IDN_UNASCHECK)
if (actions & IDN_BIDICHECK)
if (actions & IDN_IDNCONV)
if (actions & IDN_ASCCHECK)
if (actions & IDN_LENCHECK)
if (actions & IDN_RTCHECK)
}
if (buf[0] == '|')
return (buf + 1);
else
return (buf);
}