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 462 463 464 465 466 467 468 469 470 471
|
#ifndef lint
static char *rcsid = "$Id: unormalize.c,v 1.16 2001/10/31 08:51:29 m-kasahr Exp $";
#endif
/*
* Copyright (c) 2000,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) 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
* Any person or entities using and/or redistributing this Software under
* 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
* using and/or redistributing this Software under this License Terms and
* 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/result.h>
#include <mdn/assert.h>
#include <mdn/logmacro.h>
#include <mdn/utf8.h>
#include <mdn/unicode.h>
#include <mdn/unormalize.h>
#include <mdn/debug.h>
#if !defined(HAVE_MEMMOVE) && defined(HAVE_BCOPY)
#define memmove(a,b,c) bcopy((char *)(b),(char *)(a),(int)(c))
#endif
#define WORKBUF_SIZE 128
#define WORKBUF_SIZE_MAX 10000
typedef struct {
mdn__unicode_version_t version; /* Unicode version */
int cur; /* pointing now processing character */
int last; /* pointing just after the last character */
int size; /* size of UCS and CLASS array */
unsigned long *ucs; /* UCS-4 characters */
int *class; /* and their canonical classes */
unsigned long ucs_buf[WORKBUF_SIZE]; /* local buffer */
int class_buf[WORKBUF_SIZE]; /* ditto */
} workbuf_t;
static mdn_result_t normalize(mdn__unicode_version_t version,
int do_composition, int compat,
const char *from, char *to, size_t tolen);
static mdn_result_t decompose(workbuf_t *wb, unsigned long c, int compat);
static void get_class(workbuf_t *wb);
static void reorder(workbuf_t *wb);
static void compose(workbuf_t *wb);
static mdn_result_t flush_before_cur(workbuf_t *wb,
char **top, size_t *tolenp);
static void workbuf_init(workbuf_t *wb);
static void workbuf_free(workbuf_t *wb);
static mdn_result_t workbuf_extend(workbuf_t *wb);
static mdn_result_t workbuf_append(workbuf_t *wb, unsigned long c);
static void workbuf_shift(workbuf_t *wb, int shift);
static void workbuf_removevoid(workbuf_t *wb);
mdn_result_t
mdn__unormalize_formc(mdn__unicode_version_t version,
const char *from, char *to, size_t tolen)
{
assert(version != NULL && from != NULL && to != NULL && tolen >= 0);
TRACE(("mdn__unormalize_formc(from=\"%s\", tolen=%d)\n",
mdn_debug_xstring(from, 20), tolen));
return (normalize(version, 1, 0, from, to, tolen));
}
mdn_result_t
mdn__unormalize_formd(mdn__unicode_version_t version,
const char *from, char *to, size_t tolen)
{
assert(version != NULL && from != NULL && to != NULL && tolen >= 0);
TRACE(("mdn__unormalize_formd(from=\"%s\", tolen=%d)\n",
mdn_debug_xstring(from, 20), tolen));
return (normalize(version, 0, 0, from, to, tolen));
}
mdn_result_t
mdn__unormalize_formkc(mdn__unicode_version_t version,
const char *from, char *to, size_t tolen)
{
assert(version != NULL && from != NULL && to != NULL && tolen >= 0);
TRACE(("mdn__unormalize_formkc(from=\"%s\", tolen=%d)\n",
mdn_debug_xstring(from, 20), tolen));
return (normalize(version, 1, 1, from, to, tolen));
}
mdn_result_t
mdn__unormalize_formkd(mdn__unicode_version_t version,
const char *from, char *to, size_t tolen)
{
assert(version != NULL && from != NULL && to != NULL && tolen >= 0);
TRACE(("mdn__unormalize_formkd(from=\"%s\", tolen=%d)\n",
mdn_debug_xstring(from, 20), tolen));
return (normalize(version, 0, 1, from, to, tolen));
}
static mdn_result_t
normalize(mdn__unicode_version_t version, int do_composition, int compat,
const char *from, char *to, size_t tolen)
{
workbuf_t wb;
size_t fromlen = strlen(from);
mdn_result_t r = mdn_success;
/*
* Initialize working buffer.
*/
workbuf_init(&wb);
wb.version = version;
while (fromlen > 0) {
unsigned long c;
int mblen;
assert(wb.cur == wb.last);
/*
* Get one character from 'from'.
*/
if ((mblen = mdn_utf8_getwc(from, fromlen, &c)) == 0) {
r = mdn_invalid_encoding;
break;
}
from += mblen;
fromlen -= mblen;
/*
* Decompose it.
*/
if ((r = decompose(&wb, c, compat)) != mdn_success)
break;
/*
* Get canonical class.
*/
get_class(&wb);
/*
* Reorder & compose.
*/
for (; wb.cur < wb.last; wb.cur++) {
if (wb.cur == 0) {
continue;
} else if (wb.class[wb.cur] > 0) {
/*
* This is not a starter. Try reordering.
* Note that characters up to it are
* already in canonical order.
*/
reorder(&wb);
continue;
}
/*
* This is a starter character, and there are
* some characters before it. Those characters
* have been reordered properly, and
* ready for composition.
*/
if (do_composition && wb.class[0] == 0)
compose(&wb);
/*
* If CUR points to a starter character,
* then process of characters before CUR are
* already finished, because any further
* reordering/composition for them are blocked
* by the starter CUR points.
*/
if (wb.cur > 0 && wb.class[wb.cur] == 0) {
/* Flush everything before CUR. */
r = flush_before_cur(&wb, &to, &tolen);
if (r != mdn_success)
break;
}
}
}
if (r == mdn_success) {
if (do_composition && wb.cur > 0 && wb.class[0] == 0) {
/*
* There is some characters left in WB.
* They are ordered, but not composed yet.
* Now CUR points just after the last character in WB,
* and since compose() tries to compose characters
* between top and CUR inclusive, we must make CUR
* one character back during compose().
*/
wb.cur--;
compose(&wb);
wb.cur++;
}
/*
* Call this even when WB.CUR == 0, to make TO
* NUL-terminated.
*/
r = flush_before_cur(&wb, &to, &tolen);
}
workbuf_free(&wb);
return (r);
}
static mdn_result_t
decompose(workbuf_t *wb, unsigned long c, int compat) {
mdn_result_t r;
int dec_len;
again:
r = mdn__unicode_decompose(wb->version, compat, wb->ucs + wb->last,
wb->size - wb->last, c, &dec_len);
switch (r) {
case mdn_success:
wb->last += dec_len;
return (mdn_success);
case mdn_notfound:
return (workbuf_append(wb, c));
case mdn_buffer_overflow:
if ((r = workbuf_extend(wb)) != mdn_success)
return (r);
if (wb->size > WORKBUF_SIZE_MAX) {
WARNING(("mdn__unormalize_form*: "
"working buffer too large\n"));
return (mdn_nomemory);
}
goto again;
default:
return (r);
}
/* NOTREACHED */
}
static void
get_class(workbuf_t *wb) {
int i;
for (i = wb->cur; i < wb->last; i++)
wb->class[i] = mdn__unicode_canonicalclass(wb->version,
wb->ucs[i]);
}
static void
reorder(workbuf_t *wb) {
unsigned long c;
int i;
int class;
assert(wb != NULL);
i = wb->cur;
c = wb->ucs[i];
class = wb->class[i];
while (i > 0 && wb->class[i - 1] > class) {
wb->ucs[i] = wb->ucs[i - 1];
wb->class[i] =wb->class[i - 1];
i--;
wb->ucs[i] = c;
wb->class[i] = class;
}
}
static void
compose(workbuf_t *wb) {
int cur;
unsigned long *ucs;
int *class;
int last_class;
int nvoids;
int i;
mdn__unicode_version_t ver;
assert(wb != NULL && wb->class[0] == 0);
cur = wb->cur;
ucs = wb->ucs;
class = wb->class;
ver = wb->version;
/*
* If there are no decomposition sequence that begins with
* the top character, composition is impossible.
*/
if (!mdn__unicode_iscompositecandidate(ver, ucs[0]))
return;
last_class = 0;
nvoids = 0;
for (i = 1; i <= cur; i++) {
unsigned long c;
int cl = class[i];
if ((last_class < cl || cl == 0) &&
mdn__unicode_compose(ver, ucs[0], ucs[i],
&c) == mdn_success) {
/*
* Replace the top character with the composed one.
*/
ucs[0] = c;
class[0] = mdn__unicode_canonicalclass(ver, c);
class[i] = -1; /* void this character */
nvoids++;
} else {
last_class = cl;
}
}
/* Purge void characters, if any. */
if (nvoids > 0)
workbuf_removevoid(wb);
}
static mdn_result_t
flush_before_cur(workbuf_t *wb, char **top, size_t *tolenp) {
int cur = wb->cur;
char *to = *top;
size_t tolen = *tolenp;
int i;
for (i = 0; i < cur; i++) {
int len = mdn_utf8_putwc(to, tolen, wb->ucs[i]);
if (len == 0)
return (mdn_buffer_overflow);
to += len;
tolen -= len;
}
if (tolen < 1)
return (mdn_buffer_overflow);
*to = '\0';
*top = to;
*tolenp = tolen;
workbuf_shift(wb, cur);
return (mdn_success);
}
static void
workbuf_init(workbuf_t *wb) {
wb->cur = 0;
wb->last = 0;
wb->size = WORKBUF_SIZE;
wb->ucs = wb->ucs_buf;
wb->class = wb->class_buf;
}
static void
workbuf_free(workbuf_t *wb) {
if (wb->ucs != wb->ucs_buf) {
free(wb->ucs);
free(wb->class);
}
}
static mdn_result_t
workbuf_extend(workbuf_t *wb) {
int newsize = wb->size * 3;
if (wb->ucs == wb->ucs_buf) {
wb->ucs = malloc(sizeof(wb->ucs[0]) * newsize);
wb->class = malloc(sizeof(wb->class[0]) * newsize);
} else {
wb->ucs = realloc(wb->ucs, sizeof(wb->ucs[0]) * newsize);
wb->class = realloc(wb->class, sizeof(wb->class[0]) * newsize);
}
if (wb->ucs == NULL || wb->class == NULL)
return (mdn_nomemory);
else
return (mdn_success);
}
static mdn_result_t
workbuf_append(workbuf_t *wb, unsigned long c) {
mdn_result_t r;
if (wb->last >= wb->size && (r = workbuf_extend(wb)) != mdn_success)
return (r);
wb->ucs[wb->last++] = c;
return (mdn_success);
}
static void
workbuf_shift(workbuf_t *wb, int shift) {
int nmove;
assert(wb != NULL && wb->cur >= shift);
nmove = wb->last - shift;
(void)memmove(&wb->ucs[0], &wb->ucs[shift],
nmove * sizeof(wb->ucs[0]));
(void)memmove(&wb->class[0], &wb->class[shift],
nmove * sizeof(wb->class[0]));
wb->cur -= shift;
wb->last -= shift;
}
static void
workbuf_removevoid(workbuf_t *wb) {
int i, j;
int last = wb->last;
for (i = j = 0; i < last; i++) {
if (wb->class[i] >= 0) {
if (j < i) {
wb->ucs[j] = wb->ucs[i];
wb->class[j] = wb->class[i];
}
j++;
}
}
wb->cur -= last - j;
wb->last = j;
}
|