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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
/* **********************************************************
* Copyright 2007 VMware, Inc. All rights reserved.
* **********************************************************/
/*
* codeset.h --
*
* UTF-16 handling macros. Based on utf16.h from ICU 1.8.1.
*
* ICU 1.8.1 license follows:
*
* ICU License - ICU 1.8.1 and later
*
* COPYRIGHT AND PERMISSION NOTICE
*
* Copyright (c) 1995-2006 International Business Machines Corporation
* and others
*
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all
* copies of the Software and that both the above copyright
* notice(s) and this permission notice appear in supporting
* documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
* BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR
* CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Except as contained in this notice, the name of a
* copyright holder shall not be used in advertising or otherwise
* to promote the sale, use or other dealings in this Software
* without prior written authorization of the copyright holder.
*/
#ifndef __CODESET_H__
# define __CODESET_H__
#include "vm_basic_types.h"
#include "vm_assert.h"
#include "dynbuf.h"
/*
* These platforms use UTF-8 (or pretend to):
* FreeBSD: really UTF-8
* ESX: UTF-8 by policy decree
* Mac: really UTF-8
* Netware: don't know (either no iconv, or really UTF-8)
*/
#if defined(__FreeBSD__) || \
defined(VMX86_SERVER) || \
defined(__APPLE__) || \
defined(N_PLAT_NLM)
#define CURRENT_IS_UTF8
#endif
/*
* Guard these defines, borrowed from ICU's utf16.h, so that source files
* can include both.
*/
#ifndef __UTF16_H__
/**
* Is this code point a surrogate (U+d800..U+dfff)?
* @param c 32-bit code point
* @return TRUE or FALSE
* @stable ICU 2.4
*/
#define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
/**
* Does this code unit alone encode a code point (BMP, not a surrogate)?
* @param c 16-bit code unit
* @return TRUE or FALSE
* @stable ICU 2.4
*/
#define U16_IS_SINGLE(c) (!U_IS_SURROGATE(c))
/**
* Is this code unit a lead surrogate (U+d800..U+dbff)?
* @param c 16-bit code unit
* @return TRUE or FALSE
* @stable ICU 2.4
*/
#define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
/**
* Is this code unit a trail surrogate (U+dc00..U+dfff)?
* @param c 16-bit code unit
* @return TRUE or FALSE
* @stable ICU 2.4
*/
#define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
/**
* Is this code unit a surrogate (U+d800..U+dfff)?
* @param c 16-bit code unit
* @return TRUE or FALSE
* @stable ICU 2.4
*/
#define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
/**
* Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
* is it a lead surrogate?
* @param c 16-bit code unit
* @return TRUE or FALSE
* @stable ICU 2.4
*/
#define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
/**
* Helper constant for U16_GET_SUPPLEMENTARY.
* @internal
*/
#define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
/**
* Get a supplementary code point value (U+10000..U+10ffff)
* from its lead and trail surrogates.
* The result is undefined if the input values are not
* lead and trail surrogates.
*
* @param lead lead surrogate (U+d800..U+dbff)
* @param trail trail surrogate (U+dc00..U+dfff)
* @return supplementary code point (U+10000..U+10ffff)
* @stable ICU 2.4
*/
#define U16_GET_SUPPLEMENTARY(lead, trail) \
(((uint32)(lead)<<10UL)+(uint32)(trail)-U16_SURROGATE_OFFSET)
/**
* Get the lead surrogate (0xd800..0xdbff) for a
* supplementary code point (0x10000..0x10ffff).
* @param supplementary 32-bit code point (U+10000..U+10ffff)
* @return lead surrogate (U+d800..U+dbff) for supplementary
* @stable ICU 2.4
*/
#define U16_LEAD(supplementary) ((utf16_t)(((supplementary)>>10)+0xd7c0))
/**
* Get the trail surrogate (0xdc00..0xdfff) for a
* supplementary code point (0x10000..0x10ffff).
* @param supplementary 32-bit code point (U+10000..U+10ffff)
* @return trail surrogate (U+dc00..U+dfff) for supplementary
* @stable ICU 2.4
*/
#define U16_TRAIL(supplementary) ((utf16_t)(((supplementary)&0x3ff)|0xdc00))
/**
* How many 16-bit code units are used to encode this Unicode code point? (1 or 2)
* The result is not defined if c is not a Unicode code point (U+0000..U+10ffff).
* @param c 32-bit code point
* @return 1 or 2
* @stable ICU 2.4
*/
#define U16_LENGTH(c) ((uint32)(c)<=0xffff ? 1 : 2)
/**
* The maximum number of 16-bit code units per Unicode code point (U+0000..U+10ffff).
* @return 2
* @stable ICU 2.4
*/
#define U16_MAX_LENGTH 2
/**
* Get a code point from a string at a code point boundary offset,
* and advance the offset to the next code point boundary.
* (Post-incrementing forward iteration.)
* "Safe" macro, handles unpaired surrogates and checks for string boundaries.
*
* The offset may point to the lead surrogate unit
* for a supplementary code point, in which case the macro will read
* the following trail surrogate as well.
* If the offset points to a trail surrogate or
* to a single, unpaired lead surrogate, then that itself
* will be returned as the code point.
*
* @param s const utf16_t * string
* @param i string offset, must be i<length
* @param length string length
* @param c output uint32 variable
* @see U16_NEXT_UNSAFE
* @stable ICU 2.4
*/
#define U16_NEXT(s, i, length, c) { \
(c)=(s)[(i)++]; \
if(U16_IS_LEAD(c)) { \
utf16_t __c2; \
if((i)<(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
++(i); \
(c)=U16_GET_SUPPLEMENTARY((c), __c2); \
} \
} \
}
/**
* Move the string offset from one code point boundary to the previous one
* and get the code point between them.
* (Pre-decrementing backward iteration.)
* "Safe" macro, handles unpaired surrogates and checks for string boundaries.
*
* The input offset may be the same as the string length.
* If the offset is behind a trail surrogate unit
* for a supplementary code point, then the macro will read
* the preceding lead surrogate as well.
* If the offset is behind a lead surrogate or behind a single, unpaired
* trail surrogate, then that itself
* will be returned as the code point.
*
* @param s const UChar * string
* @param start starting string offset (usually 0)
* @param i string offset, must be start<i
* @param c output UChar32 variable
* @see U16_PREV_UNSAFE
* @stable ICU 2.4
*/
#define U16_PREV(s, start, i, c) { \
(c)=(s)[--(i)]; \
if(U16_IS_TRAIL(c)) { \
utf16_t __c2; \
if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
--(i); \
(c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
} \
} \
}
#endif // __UTF16_H__
/*
* Use this instead of "UTF-16" to specify UTF-16 in native byte order.
*/
#define CODESET_NATIVE_UTF16 "UTF-16LE"
/*
* Flags for conversion functions
*/
#define CSGTG_NORMAL 0x0000 /* Without any information loss. */
#define CSGTG_TRANSLIT 0x0001 /* Transliterate unknown characters. */
#define CSGTG_IGNORE 0x0002 /* Skip over untranslatable characters. */
/*
* XXX -- this function is a temporary fix. It should be removed once we fix
* the 3rd party library pathname issue.
*/
char *
CodeSet_GetAltPathName(const utf16_t *pathW); // IN
Bool
CodeSet_Init(const char *icuDataDir); // IN: ICU datafile directory in current page,
// can be NULL.
void
CodeSet_DontUseIcu(void);
Bool
CodeSet_GenericToGenericDb(const char *codeIn, // IN
const char *bufIn, // IN
size_t sizeIn, // IN
const char *codeOut, // IN
unsigned int flags, // IN
DynBuf *db); // IN/OUT
Bool
CodeSet_GenericToGeneric(const char *codeIn, // IN
const char *bufIn, // IN
size_t sizeIn, // IN
const char *codeOut, // IN
unsigned int flags, // IN
char **bufOut, // IN/OUT
size_t *sizeOut); // IN/OUT
Bool
CodeSet_Utf8ToCurrent(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
Bool
CodeSet_CurrentToUtf8(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
Bool
CodeSet_Utf16leToUtf8Db(const char *bufIn, // IN
size_t sizeIn, // IN
DynBuf *db); // IN
Bool
CodeSet_Utf16leToUtf8(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
Bool
CodeSet_Utf8ToUtf16le(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
Bool
CodeSet_CurrentToUtf16le(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
Bool
CodeSet_Utf16leToCurrent(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
Bool
CodeSet_Utf16beToCurrent(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
Bool
CodeSetOld_Utf8Normalize(const char *bufIn, // IN
size_t sizeIn, // IN
Bool precomposed, // IN
DynBuf *db); // OUT
Bool
CodeSet_Utf8FormDToUtf8FormC(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
Bool
CodeSet_Utf8FormCToUtf8FormD(const char *bufIn, // IN
size_t sizeIn, // IN
char **bufOut, // OUT
size_t *sizeOut); // OUT
const char *
CodeSet_GetCurrentCodeSet(void);
Bool
CodeSet_IsEncodingSupported(const char *name);
Bool
CodeSet_Validate(const char *buf, // IN: the string
size_t size, // IN: length of string
const char *code); // IN: encoding
/*
*-----------------------------------------------------------------------------
*
* CodeSet_Utf8ToUtf16 --
*
* A convenience wrapper that accepts a NUL-terminated UTF-8 string
* and returns an allocated UTF-16 (LE) string. ASSERTs on failure.
*
* Results:
* The allocted UTF-16 (LE) string, free with free().
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static INLINE utf16_t *
CodeSet_Utf8ToUtf16(const char *str) // IN:
{
utf16_t *strW;
if (!CodeSet_Utf8ToUtf16le(str, strlen(str), (char **) &strW, NULL)) {
ASSERT_MEM_ALLOC(FALSE);
}
return strW;
}
/*
*-----------------------------------------------------------------------------
*
* CodeSet_Utf16ToUtf8 --
*
* A convenience wrapper that accepts a NUL-terminated UTF-16 (LE)
* string and returns an allocated UTF-8 string. ASSERTs on failure.
*
* Results:
* The allocted UTF-8 string, free with free().
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE char *
CodeSet_Utf16ToUtf8(const utf16_t *strW) // IN:
{
char *str;
size_t len;
for (len = 0; strW[len]; len++)
;
if (!CodeSet_Utf16leToUtf8((const char *) strW, len * sizeof strW[0],
(char **) &str, NULL)) {
ASSERT_NOT_IMPLEMENTED(0);
}
return str;
}
/*
*-----------------------------------------------------------------------------
*
* CodeSet_Utf8FindCodePointBoundary
*
* Determine if buf[offset] is a valid UTF-8 code point boundary
* and find the previous boundary if it is not. The contents of
* buf[offset] need not be defined, only data prior to this
* location is examined. Useful for finding a suitable place to
* put a NUL terminator.
*
* Results:
*
* Returns the offset of the byte immediately following the last
* complete UTF-8 code point in buf that is entirely within the
* range [0, offset-1]. Note that if the final UTF-8 code point
* is complete, the input offset will be returned unchanged.
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE size_t
CodeSet_Utf8FindCodePointBoundary(const char *buf, // IN
size_t offset) // IN
{
size_t origOffset = offset;
signed char c;
if (offset > 0) {
/*
* Back up 1 byte and then find the start of the UTF-8 code
* point occupying that location.
*/
offset--;
while (offset > 0 && (buf[offset] & 0xc0) == 0x80) {
offset--;
}
/*
* Maximum UTF-8 code point length is 4
*/
ASSERT(origOffset - offset <= 4);
c = buf[offset];
/*
* The first byte of a UTF-8 code point needs to be one of
* 0b0XXXXXXX, 0b110XXXXX, 0b1110XXXX, 0b11110XXX
*/
ASSERT(c >= 0 || (c >> 5) == -2 || (c >> 4) == -2 || (c >> 3) == -2);
/*
* offset now points to the start of a UTF-8 code point. If it
* is a single byte or if the length, as encoded in the first
* byte, matches the number of bytes we have backed up, then the
* entire code point is present, so the original offset is a
* valid code point starting offset.
*
* Length is encoded as
* 2 bytes: 0b110XXXXX
* 3 bytes: 0b1110XXXX
* 4 bytes: 0b11110XXX
* Thus the first byte is -2 when shifted right (signed) by
* (7 - length).
*/
if (c >= 0 || (c >> (7 - origOffset + offset)) == -2) {
return origOffset;
}
/*
* Else we truncated a code point. Return its starting point.
*/
}
return offset;
}
/*
*-----------------------------------------------------------------------------
*
* CodeSet_Utf16FindCodePointBoundary
*
* Determine if buf[offset] is a valid UTF-16 code point boundary
* and find the previous boundary if it is not. The contents of
* buf[offset] need not be defined, only data prior to this
* location is examined. Useful for finding a suitable place to
* put a NUL terminator.
*
* Results:
*
* Returns the offset of the byte immediately following the last
* complete UTF-16 code point in buf that is entirely within the
* range [0, offset-1]. Note that if the final UTF-16 code point
* is complete, the input offset will be returned unchanged.
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE size_t
CodeSet_Utf16FindCodePointBoundary(const char *buf, // IN
size_t offset) // IN
{
size_t origOffset;
const utf16_t *utf16Buf = (const utf16_t *)buf;
origOffset = offset / 2;
offset = origOffset - 1;
if (origOffset > 0 && U16_IS_LEAD(utf16Buf[offset])) {
return offset * 2;
}
return origOffset * 2;
}
#endif /* __CODESET_H__ */
|