translator.c revision 04bdb234571448ed6194e1d4048e6512f2446f1c
#ifndef lint
static char *rcsid = "$Id: translator.c,v 1.17 2000/11/21 02:09:05 ishisone Exp $";
#endif
/*
* Copyright (c) 2000 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, Fuundo Bldg., 1-2 Kanda Ogawamachi, Chiyoda-ku,
* Tokyo, 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) Japan Network Information Center. All rights reserved."
*
* 4. Neither the name of JPNIC may 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.
*
* 6. Indemnification by Licensee
* this License Terms and Conditions shall defend indemnify and hold
* harmless JPNIC from and against any and all judgements damages,
* expenses, settlement liabilities, cost and other liabilities of any
* kind as a result of use and redistribution of this Software or any
* claim, suite, action, litigation or proceeding by any third party
* arising out of or relates to this License Terms and Conditions.
*
* 7. Governing Law, Jurisdiction and Venue
* This License Terms and Conditions shall be governed by and and
* construed in accordance with the law of Japan. Any person or entities
* Conditions hereby agrees and consent to the personal and exclusive
* jurisdiction and venue of Tokyo District Court of Japan.
*/
#include <config.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <mdn/logmacro.h>
#include <mdn/converter.h>
#include <mdn/normalizer.h>
#include <mdn/translator.h>
static int numdots(const char *s);
static int contain_invalid_char(const char *s);
const char *local_zld,
const char *target_zld,
{
mdn_result_t r;
int process;
TRACE(("mdn_translator_translate(local_encoding=%s,local_zld=%s,"
"target_encoding=%s,target_zld=%s,from=\"%s\")\n",
WARNING(("mdn_translator_translate: "
"given domainname too long\n"));
return (mdn_invalid_name);
}
DUMP(("mdn_translator_translate: before translation \"%s\"\n",
#define PROCESS_LOCAL 1
#define PROCESS_LOCALALT 2
#define PROCESS_DONE 4
process = 0;
/*
* Check if the domain name matches the local ZLD.
* If it does, strip ZLD and continue translation.
* Otherwise, no further processing is needed.
*/
/* Remove ZLD. */
process |= PROCESS_LOCAL;
if (local_alternate_converter != NULL &&
}
} else if (contain_invalid_char(domainbuf1)) {
/*
* The name contains invalid characters (as a legal
* traditional domain name). So there's no point in
* trying local-alt codeset.
*/
process |= PROCESS_LOCAL;
} else {
/*
* The given name is a valid ASCII domain name.
*/
process |= PROCESS_LOCAL;
if (local_alternate_converter != NULL)
}
if ((process & PROCESS_LOCALALT) != 0) {
/*
* First, try converting from the alternate encoding to UTF-8.
*/
TRACE(("mdn_translator_translate: %s to UTF-8\n",
sizeof(domainbuf2));
if (r == mdn_success)
process |= PROCESS_DONE;
else if (r != mdn_invalid_encoding)
return (r);
}
/*
* Convert from local encoding to UTF-8.
*/
TRACE(("mdn_translator_translate: %s to UTF-8\n",
sizeof(domainbuf2));
if (r == mdn_success)
process |= PROCESS_DONE;
else if (r != mdn_invalid_encoding)
return (r);
}
if ((process & PROCESS_DONE) == 0) {
/*
* Not converted. Copy verbatim.
*/
TRACE(("mdn_translator_translate: no translation required\n"));
return (mdn_buffer_overflow);
return (mdn_success);
}
DUMP(("mdn_translator_translate: UTF-8 string \"%s\"\n",
/*
* Normalize, if normalizer is specified.
*/
if (normalizer != NULL) {
sizeof(domainbuf1));
if (r != mdn_success)
return (r);
DUMP(("mdn_translator_translate: after normalization \"%s\"\n",
INFO(("mdn_translator_translate: "
"number of labels has been changed by "
"normalization\n"));
}
}
/*
* Convert from UTF-8 to target encoding.
*/
TRACE(("mdn_translator_translate: UTF-8 to %s\n",
normalizer == NULL ?
TRACE(("mdn_translator_translate: use alternate encoding\n"));
normalizer == NULL ?
}
if (r != mdn_success)
return (r);
/*
* Append ZLD, if any.
*/
if (target_zld != NULL)
DUMP(("mdn_translator_translate: after translation \"%s\"\n",
return (r);
}
int
int dlen;
int zlen;
const char *p;
int i;
/* An empty ZLD can match everything. */
return (1);
/*
* Since ZLD is canonicalized, it must end with dot.
* DOMAIN may or may not end with dot.
*/
zlen--;
/* If ZLD is longer than domain, no way. */
return (0);
for (i = 0; p[i] != '\0'; i++) {
/* ZLD is canonicalized (i.e. uppercase letters) */
if (p[i] == zld[i] ||
continue;
else
return (0);
}
return (0);
return (1);
}
/*
* Canonicalize ZLD.
* -- empty ZLD are nullified.
* -- leading dot is removed.
* -- append dot if it does not end with dot.
* -- lowercase characters are converted to uppercase.
*/
int append_dot = 0;
char *canonicalized, *p;
int c;
/* Remove leading '.' */
zld++;
/* Is it empty? */
*canonicalizedp = NULL;
return (mdn_success);
}
append_dot = 1;
return (mdn_nomemory);
if ('a' <= c && c <= 'z')
c += 'A' - 'a';
*p = c;
}
if (append_dot)
*p++ = '.';
*p = '\0';
return (mdn_success);
}
static int
numdots(const char *s) {
int n = 0;
n++;
s++;
}
return (n);
}
static int
contain_invalid_char(const char *s) {
int c;
while ((c = *s++) != '\0') {
if (('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') ||
c == '.' || c == '-')
continue; /* valid character */
return (1);
}
return (0);
}
static mdn_result_t
return (mdn_buffer_overflow);
return (mdn_success);
}