#ifndef lint
#endif
/*
* Copyright (c) 2000,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>
#ifndef IDN_RACE_PREFIX
#endif
/*
* Unicode surrogate pair.
*/
#define COMBINE_SURROGATE(h, l) \
/*
* Compression type.
*/
enum {
};
unsigned short *buf,
static idn_result_t race_compress_encode(const unsigned short *p,
int compress_mode,
static int get_compress_mode(unsigned short *p);
idn_result_t r;
TRACE(("idn__race_decode(from=\"%s\", tolen=%d)\n",
if (*from == '\0') {
goto ret;
}
r = idn_invalid_encoding;
goto ret;
}
/*
* Allocate sufficient buffer.
*/
r = idn_nomemory;
goto ret;
}
/*
* Decode base32 and decompress.
*/
if (r != idn_success)
goto ret;
/*
* Now 'buf' points the decompressed string, which must contain
* UTF-16 characters.
*/
/*
* Convert to UCS4.
*/
if (r != idn_success)
goto ret;
ret:
if (r == idn_success) {
TRACE(("idn__race_decode(): succcess (to=\"%s\")\n",
} else {
}
return (r);
}
static idn_result_t
{
unsigned short *p = buf;
unsigned int bitbuf = 0;
int bitlen = 0;
int i, j;
while (*from != '\0') {
int c = *from++;
int x;
if ('a' <= c && c <= 'z')
x = c - 'a';
else if ('A' <= c && c <= 'Z')
x = c - 'A';
else if ('2' <= c && c <= '7')
x = c - '2' + 26;
else
return (idn_invalid_encoding);
bitlen += 5;
if (bitlen >= 8) {
bitlen -= 8;
}
}
/*
* Now 'buf' holds the decoded string.
*/
/*
* Decompress.
*/
if (buf[0] == RACE_2OCTET_MODE) {
return (idn_invalid_encoding);
len = j;
} else {
for (i = 1, j = 0; i < len; j++) {
if (buf[i] == RACE_ESCAPE) {
if (i + 1 >= len)
return (idn_invalid_encoding);
buf[j] = c | 0xff;
else
i += 2;
/*
* The RACE specification says this is error.
*/
return (idn_invalid_encoding);
} else {
}
}
len = j;
}
return (idn_success);
}
idn_result_t r;
int compress_mode;
TRACE(("idn__race_encode(from=\"%s\", tolen=%d)\n",
if (*from == '\0') {
goto ret;
r = idn_prohibited;
goto ret;
}
r = idn_buffer_overflow;
goto ret;
}
/*
* Convert to UTF-16.
* Preserve space for a character at the top of the buffer.
*/
for (;;) {
unsigned short *new_buf;
r = idn_nomemory;
goto ret;
}
if (r == idn_success)
break;
else if (r != idn_buffer_overflow)
goto ret;
}
p = buf + 1;
/*
* Now 'p' contains UTF-16 encoded string.
*/
/*
* Check U+0099.
* RACE doesn't permit U+0099 in an input string.
*/
if (*p == 0x0099) {
r = idn_invalid_encoding;
goto ret;
}
}
/*
* Compress, encode in base-32 and output.
*/
ret:
if (r == idn_success) {
TRACE(("idn__race_encode(): succcess (to=\"%s\")\n",
} else {
}
return (r);
}
static idn_result_t
{
while (*p != '\0' || bitlen > 0) {
unsigned int c = *p;
if (c == '\0') {
/* End of data. Flush. */
bitlen = 5;
} else if (compress_mode == compress_none) {
/* Push 16 bit data. */
bitlen += 16;
p++;
} else {/* compress_mode == compress_one/compress_two */
/* Push 8 or 16 bit data. */
if (compress_mode == compress_two &&
(c & 0xff00) == 0) {
/* Upper octet is zero (and not U1). */
bitlen += 16;
} else if ((c & 0xff) == 0xff) {
/* Lower octet is 0xff. */
bitlen += 16;
} else {
/* Just output lower octet. */
bitlen += 8;
}
p++;
}
/*
* Output bits in 'bitbuf' in 5-bit unit.
*/
while (bitlen >= 5) {
int x;
/* Get top 5 bits. */
bitlen -= 5;
/* Encode. */
if (x < 26)
x += 'a';
else
x = (x - 26) + '2';
if (tolen < 1)
return (idn_buffer_overflow);
*to++ = x;
tolen--;
}
}
if (tolen <= 0)
return (idn_buffer_overflow);
*to = '\0';
return (idn_success);
}
static int
get_compress_mode(unsigned short *p) {
int zero = 0;
unsigned int upper = 0;
while (*p != '\0') {
if (hi == 0) {
zero++;
;
} else if (upper == 0) {
} else {
return (compress_none);
}
}
return (compress_two);
else
return (compress_one);
}