1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
#ifndef lint
static char *rcsid = "$Id: ucs4.c,v 1.1 2003/06/04 00:26:14 marka Exp $";
#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
* modified or derived work) in source and/or binary forms is permitted
* 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,
* this License Terms and Conditions, in the documentation and/or other
* 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/assert.h>
#include <idn/result.h>
#include <idn/logmacro.h>
#include <idn/util.h>
#include <idn/ucs4.h>
#include <idn/debug.h>
/*
* Unicode surrogate pair.
*/
#define IS_SURROGATE_HIGH(v) (0xd800 <= (v) && (v) <= 0xdbff)
#define IS_SURROGATE_LOW(v) (0xdc00 <= (v) && (v) <= 0xdfff)
#define SURROGATE_HIGH(v) (SURROGATE_H_OFF + (((v) - 0x10000) >> 10))
#define SURROGATE_LOW(v) (SURROGATE_L_OFF + ((v) & 0x3ff))
#define SURROGATE_BASE 0x10000
#define SURROGATE_H_OFF 0xd800
#define SURROGATE_L_OFF 0xdc00
#define COMBINE_SURROGATE(h, l) \
(SURROGATE_BASE + (((h)-SURROGATE_H_OFF)<<10) + ((l)-SURROGATE_L_OFF))
/*
* 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))
idn_result_t
idn_ucs4_ucs4toutf16(const unsigned long *ucs4, unsigned short *utf16,
size_t tolen) {
unsigned short *utf16p = utf16;
unsigned long v;
idn_result_t r;
TRACE(("idn_ucs4_ucs4toutf16(ucs4=\"%s\", tolen=%d)\n",
idn__debug_ucs4xstring(ucs4, 50), (int)tolen));
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",
idn__debug_utf16xstring(utf16, 50)));
} else {
TRACE(("idn_ucs4_ucs4toutf16(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
idn_result_t
idn_ucs4_utf16toucs4(const unsigned short *utf16, unsigned long *ucs4,
size_t tolen) {
unsigned long *ucs4p = ucs4;
unsigned short v0, v1;
idn_result_t r;
TRACE(("idn_ucs4_utf16toucs4(utf16=\"%s\", tolen=%d)\n",
idn__debug_utf16xstring(utf16, 50), (int)tolen));
while (*utf16 != '\0') {
v0 = *utf16;
if (tolen < 1) {
r = idn_buffer_overflow;
goto ret;
}
if (IS_SURROGATE_HIGH(v0)) {
v1 = *(utf16 + 1);
if (!IS_SURROGATE_LOW(v1)) {
WARNING(("idn_ucs4_utf16toucs4: "
"corrupted surrogate pair\n"));
r = idn_invalid_encoding;
goto ret;
}
*ucs4p++ = COMBINE_SURROGATE(v0, v1);
tolen--;
utf16 += 2;
} else {
*ucs4p++ = v0;
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",
idn__debug_ucs4xstring(ucs4, 50)));
} else {
TRACE(("idn_ucs4_utf16toucs4(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
idn_result_t
idn_ucs4_utf8toucs4(const char *utf8, unsigned long *ucs4, size_t tolen) {
const unsigned char *utf8p = (const unsigned char *)utf8;
unsigned long *ucs4p = ucs4;
unsigned long v, min;
unsigned char c;
int width;
int i;
idn_result_t r;
TRACE(("idn_ucs4_utf8toucs4(utf8=\"%s\", tolen=%d)\n",
idn__debug_xstring(utf8, 50), (int)tolen));
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",
idn__debug_ucs4xstring(ucs4, 50)));
} else {
TRACE(("idn_ucs4_utf8toucs4(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
idn_result_t
idn_ucs4_ucs4toutf8(const unsigned long *ucs4, char *utf8, size_t tolen) {
unsigned char *utf8p = (unsigned char *)utf8;
unsigned long v;
int width;
int mask;
int offset;
idn_result_t r;
TRACE(("idn_ucs4_ucs4toutf8(ucs4=\"%s\", tolen=%d)\n",
idn__debug_ucs4xstring(ucs4, 50), (int)tolen));
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;
}
if (tolen < width) {
r = idn_buffer_overflow;
goto ret;
}
offset = 6 * (width - 1);
*utf8p++ = (v >> offset) | mask;
mask = 0x80;
while (offset > 0) {
offset -= 6;
*utf8p++ = ((v >> offset) & 0x3f) | mask;
}
tolen -= width;
}
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",
idn__debug_xstring(utf8, 50)));
} else {
TRACE(("idn_ucs4_ucs4toutf8(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
size_t
idn_ucs4_strlen(const unsigned long *ucs4) {
size_t len;
for (len = 0; *ucs4 != '\0'; ucs4++, len++)
/* nothing to do */ ;
return (len);
}
unsigned long *
idn_ucs4_strcpy(unsigned long *to, const unsigned long *from) {
unsigned long *result = to;
while (*from != '\0')
*to++ = *from++;
*to = '\0';
return (result);
}
unsigned long *
idn_ucs4_strcat(unsigned long *to, const unsigned long *from) {
unsigned long *result = to;
while (*to != '\0')
to++;
while (*from != '\0')
*to++ = *from++;
*to = '\0';
return (result);
}
int
idn_ucs4_strcmp(const unsigned long *str1, const unsigned long *str2) {
while (*str1 != '\0') {
if (*str1 > *str2)
return (1);
else if (*str1 < *str2)
return (-1);
str1++;
str2++;
}
if (*str1 > *str2)
return (1);
else if (*str1 < *str2)
return (-1);
return (0);
}
int
idn_ucs4_strcasecmp(const unsigned long *str1, const unsigned long *str2) {
unsigned long c1, c2;
while (*str1 != '\0') {
c1 = ASCII_TOLOWER(*str1);
c2 = ASCII_TOLOWER(*str2);
if (c1 > c2)
return (1);
else if (c1 < c2)
return (-1);
str1++;
str2++;
}
c1 = ASCII_TOLOWER(*str1);
c2 = ASCII_TOLOWER(*str2);
if (c1 > c2)
return (1);
else if (c1 < c2)
return (-1);
return (0);
}
unsigned long *
idn_ucs4_strdup(const unsigned long *str) {
size_t length = idn_ucs4_strlen(str);
unsigned long *dupstr;
dupstr = (unsigned long *)malloc(sizeof(*str) * (length + 1));
if (dupstr == NULL)
return NULL;
memcpy(dupstr, str, sizeof(*str) * (length + 1));
return dupstr;
}
|