u8_textprep.c revision 85bb5f1d642e430b889478fb1200b511338085d7
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* UTF-8 text preparation functions (PSARC/2007/149, PSARC/2007/458).
*
* Man pages: u8_textprep_open(9F), u8_textprep_buf(9F), u8_textprep_close(9F),
* u8_textprep_str(9F), u8_strcmp(9F), and u8_validate(9F). See also
* the section 3C man pages.
* Interface stability: Committed.
*/
#ifdef _KERNEL
#include <sys/sysmacros.h>
#else
#include <sys/u8_textprep.h>
#include <strings.h>
#endif /* _KERNEL */
#include <sys/byteorder.h>
#include <sys/u8_textprep_data.h>
/* The maximum possible number of bytes in a UTF-8 character. */
#define U8_MB_CUR_MAX (4)
/*
* The maximum number of bytes needed for a UTF-8 character to cover
* U+0000 - U+FFFF, i.e., the coding space of now deprecated UCS-2.
*/
#define U8_MAX_BYTES_UCS2 (3)
/* The maximum possible number of bytes in a Stream-Safe Text. */
#define U8_STREAM_SAFE_TEXT_MAX (128)
/*
* The maximum number of characters in a combining/conjoining sequence and
* the actual upperbound limit of a combining/conjoining sequence.
*/
#define U8_MAX_CHARS_A_SEQ (32)
#define U8_UPPER_LIMIT_IN_A_SEQ (31)
/* The combining class value for Starter. */
#define U8_COMBINING_CLASS_STARTER (0)
/*
* Some Hangul related macros at below.
*
* The first and the last of Hangul syllables, Hangul Jamo Leading consonants,
* Vowels, and optional Trailing consonants in Unicode scalar values.
*
* Please be noted that the U8_HANGUL_JAMO_T_FIRST is 0x11A7 at below not
* the actual U+11A8. This is due to that the trailing consonant is optional
* and thus we are doing a pre-calculation of subtracting one.
*
* Each of 19 modern leading consonants has total 588 possible syllables since
* Hangul has 21 modern vowels and 27 modern trailing consonants plus 1 for
* no trailing consonant case, i.e., 21 x 28 = 588.
*
* We also have bunch of Hangul related macros at below. Please bear in mind
* that the U8_HANGUL_JAMO_1ST_BYTE can be used to check whether it is
* a Hangul Jamo or not but the value does not guarantee that it is a Hangul
* Jamo; it just guarantee that it will be most likely.
*/
#define U8_HANGUL_SYL_FIRST (0xAC00U)
#define U8_HANGUL_SYL_LAST (0xD7A3U)
#define U8_HANGUL_JAMO_L_FIRST (0x1100U)
#define U8_HANGUL_JAMO_L_LAST (0x1112U)
#define U8_HANGUL_JAMO_V_FIRST (0x1161U)
#define U8_HANGUL_JAMO_V_LAST (0x1175U)
#define U8_HANGUL_JAMO_T_FIRST (0x11A7U)
#define U8_HANGUL_JAMO_T_LAST (0x11C2U)
#define U8_HANGUL_V_COUNT (21)
#define U8_HANGUL_VT_COUNT (588)
#define U8_HANGUL_T_COUNT (28)
#define U8_HANGUL_JAMO_1ST_BYTE (0xE1U)
#define U8_SAVE_HANGUL_AS_UTF8(s, i, j, k, b) \
#define U8_HANGUL_JAMO_L(u) \
((u) >= U8_HANGUL_JAMO_L_FIRST && (u) <= U8_HANGUL_JAMO_L_LAST)
#define U8_HANGUL_JAMO_V(u) \
((u) >= U8_HANGUL_JAMO_V_FIRST && (u) <= U8_HANGUL_JAMO_V_LAST)
#define U8_HANGUL_JAMO_T(u) \
((u) > U8_HANGUL_JAMO_T_FIRST && (u) <= U8_HANGUL_JAMO_T_LAST)
#define U8_HANGUL_JAMO(u) \
((u) >= U8_HANGUL_JAMO_L_FIRST && (u) <= U8_HANGUL_JAMO_T_LAST)
#define U8_HANGUL_SYLLABLE(u) \
((u) >= U8_HANGUL_SYL_FIRST && (u) <= U8_HANGUL_SYL_LAST)
#define U8_HANGUL_COMPOSABLE_L_V(s, u) \
((s) == U8_STATE_HANGUL_L && U8_HANGUL_JAMO_V((u)))
#define U8_HANGUL_COMPOSABLE_LV_T(s, u) \
((s) == U8_STATE_HANGUL_LV && U8_HANGUL_JAMO_T((u)))
/* The types of decomposition mappings. */
#define U8_DECOMP_BOTH (0xF5U)
#define U8_DECOMP_CANONICAL (0xF6U)
/* The indicator for 16-bit table. */
#define U8_16BIT_TABLE_INDICATOR (0x8000U)
/* The following are some convenience macros. */
#define U8_SIMPLE_SWAP(a, b, t) \
(t) = (a); \
(a) = (b); \
(b) = (t);
#define U8_ASCII_TOUPPER(c) \
(((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 'A' : (c))
#define U8_ASCII_TOLOWER(c) \
(((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))
/*
* The following macro assumes that the two characters that are to be
* swapped are adjacent to each other and 'a' comes before 'b'.
*
* If the assumptions are not met, then, the macro will fail.
*/
#define U8_SWAP_COMB_MARKS(a, b) \
for (k = 0; k < disp[(a)]; k++) \
for (k = 0; k < disp[(b)]; k++) \
for (k = 0; k < disp[(a)]; k++) \
/* The possible states during normalization. */
typedef enum {
U8_STATE_START = 0,
U8_STATE_HANGUL_L = 1,
U8_STATE_HANGUL_LV = 2,
U8_STATE_HANGUL_LVT = 3,
U8_STATE_HANGUL_V = 4,
U8_STATE_HANGUL_T = 5,
/*
* The three vectors at below are used to check bytes of a given UTF-8
* character are valid and not containing any malformed byte values.
*
* We used to have a quite relaxed UTF-8 binary representation but then there
* was some security related issues and so the Unicode Consortium defined
* and announced the UTF-8 Corrigendum at Unicode 3.1 and then refined it
* one more time at the Unicode 3.2. The following three tables are based on
* that.
*/
#define I_ U8_ILLEGAL_CHAR
#define O_ U8_OUT_OF_RANGE_CHAR
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1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0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
/* C0 C1 C2 C3 C4 C5 C6 C7 */
0, 0, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/* C8 C9 CA CB CC CD CE CF */
0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/* D0 D1 D2 D3 D4 D5 D6 D7 */
0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/* D8 D9 DA DB DC DD DE DF */
0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/* E0 E1 E2 E3 E4 E5 E6 E7 */
0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf,
/* E8 E9 EA EB EC ED EE EF */
0xbf, 0xbf, 0xbf, 0xbf, 0xbf, 0x9f, 0xbf, 0xbf,
/* F0 F1 F2 F3 F4 F5 F6 F7 */
0xbf, 0xbf, 0xbf, 0xbf, 0x8f, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
/*
* The u8_validate() validates on the given UTF-8 character string and
* calculate the byte length. It is quite similar to mblen(3C) except that
* this will validate against the list of characters if required and
* specific to UTF-8 and Unicode.
*/
int
{
uchar_t **p;
uchar_t f;
int sz;
size_t i;
int ret_val;
if (! u8str)
return (0);
ret_val = 0;
/*
* The first byte of a UTF-8 character tells how many
* bytes will follow for the character. If the first byte
* is an illegal byte value or out of range value, we just
* return -1 with an appropriate error number.
*/
if (sz == U8_ILLEGAL_CHAR) {
return (-1);
}
if (sz == U8_OUT_OF_RANGE_CHAR ||
return (-1);
}
/*
* If we don't have enough bytes to check on, that's also
* an error. As you can see, we give illegal byte sequence
* checking higher priority then EINVAL cases.
*/
return (-1);
}
if (sz == 1) {
ib++;
ret_val++;
} else {
/*
* Check on the multi-byte UTF-8 character. For more
* details on this, see comment added for the used
* data structures at the beginning of the file.
*/
f = *ib++;
ret_val++;
for (i = 1; i < sz; i++) {
if (second) {
if (*ib < u8_valid_min_2nd_byte[f] ||
*ib > u8_valid_max_2nd_byte[f]) {
return (-1);
}
} else if (U8_ILLEGAL_NEXT_BYTE_COMMON(*ib)) {
return (-1);
}
ib++;
ret_val++;
}
}
if (check_additional) {
s2 = p[i];
break;
s1++;
s2++;
}
return (-1);
}
}
}
break;
}
return (ret_val);
}
/*
* The do_case_conv() looks at the mapping tables and returns found
* bytes if any. If not found, the input bytes are returned. The function
* always terminate the return bytes with a null character assuming that
* there are plenty of room to do so.
*
* The case conversions are simple case conversions mapping a character to
* another character as specified in the Unicode data. The byte size of
* the mapped character could be different from that of the input character.
*
* The return value is the byte length of the returned character excluding
* the terminating null byte.
*/
static size_t
{
size_t i;
/*
* At this point, the only possible values for sz are 2, 3, and 4.
* The u8s should point to a vector that is well beyond the size of
* 5 bytes.
*/
if (sz == 2) {
} else if (sz == 3) {
} else if (sz == 4) {
} else {
/* This is not possible but just in case as a fallback. */
if (is_it_toupper)
*u8s = U8_ASCII_TOUPPER(*s);
else
*u8s = U8_ASCII_TOLOWER(*s);
return (1);
}
/*
* Let's find out if we have a corresponding character.
*/
if (b1 == U8_TBL_ELEMENT_NOT_DEF)
if (b2 == U8_TBL_ELEMENT_NOT_DEF)
if (is_it_toupper) {
if (b3_tbl == U8_TBL_ELEMENT_NOT_DEF)
/* Either there is no match or an error at the table. */
} else {
if (b3_tbl == U8_TBL_ELEMENT_NOT_DEF)
}
/*
* If i is still zero, that means there is no corresponding character.
*/
if (i == 0)
u8s[i] = '\0';
return (i);
}
/*
* The do_case_compare() function compares the two input strings, s1 and s2,
* one character at a time doing case conversions if applicable and return
* the comparison result as like strcmp().
*
* Since, in empirical sense, most of text data are 7-bit ASCII characters,
* we treat the 7-bit ASCII characters as a special case trying to yield
* faster processing time.
*/
static int
{
int f;
int sz1;
int sz2;
size_t j;
/*
* Find out what would be the byte length for this UTF-8
* character at string s1 and also find out if this is
* an illegal start byte or not and if so, issue a proper
* error number and yet treat this byte as a character.
*/
if (sz1 < 0) {
sz1 = 1;
}
/*
* For 7-bit ASCII characters mainly, we do a quick case
* conversion right at here.
*
* If we don't have enough bytes for this character, issue
* an EINVAL error and use what are available.
*
* If we have enough bytes, find out if there is
* a corresponding uppercase character and if so, copy over
* the bytes for a comparison later. If there is no
* corresponding uppercase character, then, use what we have
* for the comparison.
*/
if (sz1 == 1) {
if (is_it_toupper)
else
s1++;
u8s1[j] = '\0';
} else {
}
/* Do the same for the string s2. */
if (sz2 < 0) {
sz2 = 1;
}
if (sz2 == 1) {
if (is_it_toupper)
else
s2++;
u8s2[j] = '\0';
} else {
}
/* Now compare the two characters. */
return (1);
return (-1);
} else {
if (f != 0)
return (f);
}
/*
* They were the same. Let's move on to the next
* characters then.
*/
}
/*
* We compared until the end of either or both strings.
*
* If we reached to or went over the ends for the both, that means
* they are the same.
*
* If we reached only one of the two ends, that means the other string
* has something which then the fact can be used to determine
* the return value.
*/
return (0);
return (-1);
}
return (1);
}
/*
* The combining_class() function checks on the given bytes and find out
* the corresponding Unicode combining class value. The return value 0 means
* it is a Starter. Any illegal UTF-8 character will also be treated as
* a Starter.
*/
static uchar_t
{
return (0);
if (sz == 2) {
b3 = s[0];
b4 = s[1];
} else if (sz == 3) {
b2 = s[0];
b3 = s[1];
b4 = s[2];
} else if (sz == 4) {
b1 = s[0];
b2 = s[1];
b3 = s[2];
b4 = s[3];
}
if (b1 == U8_TBL_ELEMENT_NOT_DEF)
return (0);
if (b2 == U8_TBL_ELEMENT_NOT_DEF)
return (0);
if (b3 == U8_TBL_ELEMENT_NOT_DEF)
return (0);
}
/*
* The do_decomp() function finds out a matching decomposition if any
* and return. If there is no match, the input bytes are copied and returned.
* The function also checks if there is a Hangul, decomposes it if necessary
* and returns.
*
* To save time, a single byte 7-bit ASCII character should be handled by
* the caller.
*
* The function returns the number of bytes returned sans always terminating
* the null byte. It will also return a state that will tell if there was
* a Hangul character decomposed which then will be used by the caller.
*/
static size_t
{
size_t i;
if (sz == 2) {
} else if (sz == 3) {
/* Convert it to a Unicode scalar value. */
/*
* If this is a Hangul syllable, we decompose it into
* a leading consonant, a vowel, and an optional trailing
* consonant and then return.
*/
if (U8_HANGUL_SYLLABLE(u1)) {
if (b3) {
return (9);
}
return (6);
}
/*
* If this is a Hangul Jamo, we know there is nothing
* further that we can decompose.
*/
if (U8_HANGUL_JAMO_L(u1)) {
return (3);
}
if (U8_HANGUL_JAMO_V(u1)) {
if (*state == U8_STATE_HANGUL_L)
else
return (3);
}
if (U8_HANGUL_JAMO_T(u1)) {
if (*state == U8_STATE_HANGUL_LV)
else
return (3);
}
} else if (sz == 4) {
} else {
/*
* This is a fallback and should not happen if the function
* was called properly.
*/
u8s[0] = s[0];
*state = U8_STATE_START;
return (1);
}
/*
* At this point, this rountine does not know what it would get.
* The caller should sort it out if the state isn't a Hangul one.
*/
*state = U8_STATE_START;
/* Try to find matching decomposition mapping byte sequence. */
if (b1 == U8_TBL_ELEMENT_NOT_DEF)
if (b2 == U8_TBL_ELEMENT_NOT_DEF)
if (b3_tbl == U8_TBL_ELEMENT_NOT_DEF)
/*
* If b3_tbl is bigger than or equal to U8_16BIT_TABLE_INDICATOR
* which is 0x8000, this means we couldn't fit the mappings into
* the cardinality of a unsigned byte.
*/
if (b3_tbl >= U8_16BIT_TABLE_INDICATOR) {
} else {
}
/* This also means there wasn't any matching decomposition. */
/*
* The final table for decomposition mappings has three types of
* byte sequences depending on whether a mapping is for compatibility
* decomposition, canonical decomposition, or both like the following:
*
* (1) Compatibility decomposition mappings:
*
* +---+---+-...-+---+
* | B0| B1| ... | Bm|
* +---+---+-...-+---+
*
* The first byte, B0, is always less then 0xF5 (U8_DECOMP_BOTH).
*
* (2) Canonical decomposition mappings:
*
* +---+---+---+-...-+---+
* | T | b0| b1| ... | bn|
* +---+---+---+-...-+---+
*
* where the first byte, T, is 0xF6 (U8_DECOMP_CANONICAL).
*
* (3) Both mappings:
*
* +---+---+---+---+-...-+---+---+---+-...-+---+
* | T | D | b0| b1| ... | bn| B0| B1| ... | Bm|
* +---+---+---+---+-...-+---+---+---+-...-+---+
*
* where T is 0xF5 (U8_DECOMP_BOTH) and D is a displacement
* byte, b0 to bn are canonical mapping bytes and B0 to Bm are
* compatibility mapping bytes.
*
* Note that compatibility decomposition means doing recursive
* decompositions using both compatibility decomposition mappings and
* canonical decomposition mappings. On the other hand, canonical
* decomposition means doing recursive decompositions using only
* canonical decomposition mappings. Since the table we have has gone
* through the recursions already, we do not need to do so during
* runtime, i.e., the table has been completely flattened out
* already.
*/
/* Get the type, T, of the byte sequence. */
/*
* If necessary, adjust start_id, end_id, or both. Note that if
* this is compatibility decomposition mapping, there is no
* adjustment.
*/
if (canonical_decomposition) {
/* Is the mapping only for compatibility decomposition? */
if (b1 < U8_DECOMP_BOTH)
start_id++;
if (b1 == U8_DECOMP_BOTH) {
start_id++;
}
} else {
/*
* Unless this is a compatibility decomposition mapping,
* we adjust the start_id.
*/
if (b1 == U8_DECOMP_BOTH) {
start_id++;
} else if (b1 == U8_DECOMP_CANONICAL) {
start_id++;
}
}
u8s[i] = '\0';
return (i);
}
/*
* The find_composition_start() function uses the character bytes given and
* find out the matching composition mappings if any and return the address
* to the composition mappings as explained in the do_composition().
*/
static uchar_t *
{
if (sz == 1) {
b4 = s[0];
} else if (sz == 2) {
b3 = s[0];
b4 = s[1];
} else if (sz == 3) {
b2 = s[0];
b3 = s[1];
b4 = s[2];
} else if (sz == 4) {
b1 = s[0];
b2 = s[1];
b3 = s[2];
b4 = s[3];
} else {
/*
* This is a fallback and should not happen if the function
* was called properly.
*/
return (NULL);
}
if (b1 == U8_TBL_ELEMENT_NOT_DEF)
return (NULL);
if (b2 == U8_TBL_ELEMENT_NOT_DEF)
return (NULL);
if (b3_tbl == U8_TBL_ELEMENT_NOT_DEF)
return (NULL);
if (b3_tbl >= U8_16BIT_TABLE_INDICATOR) {
} else {
}
return (NULL);
}
/*
* The blocked() function checks on the combining class values of previous
* characters in this sequence and return whether it is blocked or not.
*/
static boolean_t
{
size_t i;
for (i = 1; i < last; i++)
if (comb_class[i] >= my_comb_class ||
return (B_TRUE);
return (B_FALSE);
}
/*
* The do_composition() reads the character string pointed by 's' and
* do necessary canonical composition and then copy over the result back to
* the 's'.
*
* The input argument 's' cannot contain more than 32 characters.
*/
static size_t
{
uchar_t *p;
uchar_t *q;
size_t i;
size_t j;
size_t k;
size_t l;
size_t C;
/*
* This should never happen unless the callers are doing some strange
* and unexpected things.
*
* The "last" is the index pointing to the last character not last + 1.
*/
if (last >= U8_MAX_CHARS_A_SEQ)
for (i = l = 0; i <= last; i++) {
/*
* The last or any non-Starters at the beginning, we don't
* have any chance to do composition and so we just copy them
* to the temporary buffer.
*/
p = s + start[i];
for (k = 0; k < size; k++)
t[l++] = *p++;
continue;
}
/*
* If this could be a start of Hangul Jamos, then, we try to
* conjoin them.
*/
if (s[start[i]] == U8_HANGUL_JAMO_1ST_BYTE) {
i += 2;
if (i <= last) {
s[start[i] + 2]);
if (U8_HANGUL_JAMO_T(u2)) {
i++;
}
}
i--;
l += 3;
continue;
}
}
/*
* Let's then find out if this Starter has composition
* mapping.
*/
if (p == NULL)
goto SAVE_THE_CHAR;
/*
* We have a Starter with composition mapping and the next
* character is a non-Starter. Let's try to find out if
* we can do composition.
*/
saved_p = p;
saved_i = i;
saved_l = l;
saved_marks_count = 0;
q = s + start[++i];
/*
* The next for() loop compares the non-Starter pointed by
* 'q' with the possible (joinable) characters pointed by 'p'.
*
* The composition final table entry pointed by the 'p'
* looks like the following:
*
* +---+---+---+-...-+---+---+---+---+-...-+---+---+
* | C | b0| b2| ... | bn| F | B0| B1| ... | Bm| F |
* +---+---+---+-...-+---+---+---+---+-...-+---+---+
*
* where C is the count byte indicating the number of
* mapping pairs where each pair would be look like
* (b0-bn F, B0-Bm F). The b0-bn are the bytes of the second
* character of a canonical decomposition and the B0-Bm are
* the bytes of a matching composite character. The F is
* a filler byte after each character as the separator.
*/
for (C = *p++; C > 0; C--) {
for (k = 0; k < size; p++, k++)
if (*p != q[k])
break;
/* Have we found it? */
if (k >= size && *p == U8_TBL_ELEMENT_FILLER) {
l = saved_l;
while (*++p != U8_TBL_ELEMENT_FILLER)
t[l++] = *p;
break;
}
/* We didn't find; skip to the next pair. */
if (*p != U8_TBL_ELEMENT_FILLER)
while (*++p != U8_TBL_ELEMENT_FILLER)
;
while (*++p != U8_TBL_ELEMENT_FILLER)
;
p++;
}
/*
* If there was no match, we will need to save the combining
* mark for later appending. After that, if the next one
* is a non-Starter and not blocked, then, we try once
* again to do composition with the next non-Starter.
*
* If there was no match and this was a Starter, then,
* this is a new start.
*
* If there was a match and a composition done and we have
* more to check on, then, we retrieve a new composition final
* table entry for the composite and then try to do the
* composition again.
*/
if (match_not_found) {
if (comb_class[i] == U8_COMBINING_CLASS_STARTER) {
i--;
goto SAVE_THE_CHAR;
}
saved_marks[saved_marks_count++] = i;
}
if (saved_l == l) {
while (i < last) {
saved_marks[saved_marks_count++] = ++i;
else
break;
}
if (i < last) {
p = saved_p;
goto TRY_THE_NEXT_MARK;
}
} else if (i < last) {
l - saved_l);
if (p != NULL) {
saved_p = p;
goto TRY_THE_NEXT_MARK;
}
}
/*
* There is no more composition possible.
*
* If there was no composition what so ever then we copy
* over the original Starter and then append any non-Starters
* remaining at the target string sequentially after that.
*/
if (saved_l == l) {
for (j = 0; j < size; j++)
t[l++] = *p++;
}
for (k = 0; k < saved_marks_count; k++) {
p = s + start[saved_marks[k]];
for (j = 0; j < size; j++)
t[l++] = *p++;
}
}
/*
* If the last character is a Starter and if we have a character
* (possibly another Starter) that can be turned into a composite,
* we do so and we do so until there is no more of composition
* possible.
*/
p = *os;
while (p < oslast) {
size = u8_number_of_bytes[*p];
break;
saved_p = p;
for (i = 0; i < size; i++)
tc[i] = *p++;
l - saved_l);
if (q == NULL) {
p = saved_p;
break;
}
for (C = *q++; C > 0; C--) {
for (k = 0; k < size; q++, k++)
if (*q != tc[k])
break;
if (k >= size && *q == U8_TBL_ELEMENT_FILLER) {
l = saved_l;
while (*++q != U8_TBL_ELEMENT_FILLER) {
/*
* This is practically
* impossible but we don't
* want to take any chances.
*/
if (l >=
p = saved_p;
goto SAFE_RETURN;
}
t[l++] = *q;
}
break;
}
if (*q != U8_TBL_ELEMENT_FILLER)
while (*++q != U8_TBL_ELEMENT_FILLER)
;
while (*++q != U8_TBL_ELEMENT_FILLER)
;
q++;
}
if (match_not_found) {
p = saved_p;
break;
}
}
*os = p;
}
/*
* Now we copy over the temporary string to the target string.
* Since composition always reduces the number of characters or
* the number of characters stay, we don't need to worry about
* the buffer overflow here.
*/
for (i = 0; i < l; i++)
s[i] = t[i];
s[l] = '\0';
return (l);
}
/*
* The collect_a_seq() function checks on the given string s, collect
* a sequence of characters at u8s, and return the sequence. While it collects
* a sequence, it also applies case conversion, canonical or compatibility
* decomposition, canonical decomposition, or some or all of them and
* in that order.
*
* The collected sequence cannot be bigger than 32 characters since if
* it is having more than 31 characters, the sequence will be terminated
* with a U+034F COMBINING GRAPHEME JOINER (CGJ) character and turned into
* a Stream-Safe Text. The collected sequence is always terminated with
* a null byte and the return value is the byte length of the sequence
* including 0. The return value does not include the terminating
* null byte.
*/
static size_t
{
uchar_t *s;
int sz;
int saved_sz;
size_t i;
size_t j;
size_t k;
size_t l;
/*
* Save the source string pointer which we will return a changed
* pointer if we do processing.
*/
s = *source;
/*
* The following is a fallback for just in case callers are not
* checking the string boundaries before the calling.
*/
if (s >= slast) {
u8s[0] = '\0';
return (0);
}
/*
* As the first thing, let's collect a character and do case
* conversion if necessary.
*/
sz = u8_number_of_bytes[*s];
if (sz < 0) {
u8s[0] = *s++;
*source = s;
return (1);
}
if (sz == 1) {
if (is_it_toupper)
u8s[0] = U8_ASCII_TOUPPER(*s);
else if (is_it_tolower)
u8s[0] = U8_ASCII_TOLOWER(*s);
else
u8s[0] = *s;
s++;
for (i = 0; s < slast; )
u8s[i++] = *s++;
u8s[i] = '\0';
*source = s;
return (i);
} else {
if (is_it_toupper || is_it_tolower) {
s += sz;
sz = i;
} else {
for (i = 0; i < sz; )
u8s[i++] = *s++;
u8s[i] = '\0';
}
}
/*
* And then canonical/compatibility decomposition followed by
* an optional canonical composition. Please be noted that
* canonical composition is done only when a decomposition is
* done.
*/
if (sz == 1) {
*state = U8_STATE_START;
saved_sz = 1;
comb_class[0] = 0;
start[0] = 0;
disp[0] = 1;
last = 1;
} else {
last = 0;
for (i = 0; i < saved_sz; ) {
last++;
i += sz;
}
/*
* Decomposition yields various Hangul related
* states but not on combining marks. We need to
* find out at here by checking on the last
* character.
*/
if (*state == U8_STATE_START) {
}
}
saved_last = last;
while (s < slast) {
sz = u8_number_of_bytes[*s];
/*
* If this is an illegal character, an incomplete
* character, or an 7-bit ASCII Starter character,
* then we have collected a sequence; break and let
* the next call deal with the two cases.
*
* Note that this is okay only if you are using this
* function with a fixed length string, not on
* a buffer with multiple calls of one chunk at a time.
*/
if (sz <= 1) {
break;
break;
} else {
/*
* If the previous character was a Hangul Jamo
* and this character is a Hangul Jamo that
* can be conjoined, we collect the Jamo.
*/
if (*s == U8_HANGUL_JAMO_1ST_BYTE) {
*s, *(s + 1), *(s + 2));
u1)) {
i = 0;
goto COLLECT_A_HANGUL;
}
u1)) {
i = 0;
goto COLLECT_A_HANGUL;
}
}
/*
* Regardless of whatever it was, if this is
* a Starter, we don't collect the character
* since that's a new start and we will deal
* with it at the next time.
*/
if (i == U8_COMBINING_CLASS_STARTER)
break;
/*
* We know the current character is a combining
* mark. If the previous character wasn't
* a Starter (not Hangul) or a combining mark,
* then, we don't collect this combining mark.
*/
if (*state != U8_STATE_START &&
break;
/*
* If we collected a Starter and combining
* marks up to 30, i.e., total 31 characters,
* then, we terminate this degenerately long
* combining sequence with a U+034F COMBINING
* GRAPHEME JOINER (CGJ) which is 0xCD 0x8F in
* UTF-8 and turn this into a Stream-Safe
* Text. This will be extremely rare but
* possible.
*
* The following will also guarantee that
* we are not writing more than 32 characters
* plus a NULL at u8s[].
*/
if (last >= U8_UPPER_LIMIT_IN_A_SEQ) {
*state = U8_STATE_START;
comb_class[last] = 0;
last++;
break;
}
/*
* Some combining marks also do decompose into
* another combining mark or marks.
*/
if (*state == U8_STATE_COMBINING_MARK) {
k = last;
l = sz;
for (j = 0; j < i; ) {
comb_class[last] =
last++;
if (last >=
last = k;
goto TURN_STREAM_SAFE;
}
j += sz;
}
sz = i;
s += l;
for (i = 0; i < sz; i++)
} else {
comb_class[last] = i;
last++;
for (i = 0; i < sz; i++)
}
/*
* If this is U+0345 COMBINING GREEK
* YPOGEGRAMMENI (0xCD 0x85 in UTF-8), a.k.a.,
* iota subscript, and need to be converted to
* uppercase letter, convert it to U+0399 GREEK
* CAPITAL LETTER IOTA (0xCE 0x99 in UTF-8),
* i.e., convert to capital adscript form as
* specified in the Unicode standard.
*
* This is the only special case of (ambiguous)
* case conversion at combining marks and
* probably the standard will never have
* anything similar like this in future.
*/
}
}
}
/*
* Let's try to ensure a canonical ordering for the collected
* combining marks. We do this only if we have collected
* at least one more non-Starter. (The decomposition mapping
* data tables have fully (and recursively) expanded and
* canonically ordered decompositions.)
*
* The U8_SWAP_COMB_MARKS() convenience macro has some
* assumptions and we are meeting the assumptions.
*/
last--;
if (last >= saved_last) {
for (i = 0; i < last; i++)
for (j = last; j > i; j--)
if (comb_class[j] &&
U8_SWAP_COMB_MARKS(j - 1, j);
}
}
*source = s;
if (! canonical_composition) {
return (saved_sz);
}
/*
* Now do the canonical composition. Note that we do this
* only after a canonical or compatibility decomposition to
* finish up NFC or NFKC.
*/
&s, slast);
}
*source = s;
}
/*
* The do_norm_compare() function does string comparion based on Unicode
* simple case mappings and Unicode Normalization definitions.
*
* It does so by collecting a sequence of character at a time and comparing
* the collected sequences from the strings.
*
* The meanings on the return values are the same as the usual strcmp().
*/
static int
{
int result;
/*
* If the current character is a 7-bit ASCII and the last
* character, or, if the current character and the next
* character are both some 7-bit ASCII characters then
* we treat the current character as a sequence.
*
* In any other cases, we need to call collect_a_seq().
*/
if (is_it_toupper)
else if (is_it_tolower)
else
sz1 = 1;
s1++;
} else {
}
if (is_it_toupper)
else if (is_it_tolower)
else
sz2 = 1;
s2++;
} else {
}
/*
* Now compare the two characters. If they are the same,
* we move on to the next character sequences.
*/
return (1);
return (-1);
} else {
if (result != 0)
return (result);
}
}
/*
* We compared until the end of either or both strings.
*
* If we reached to or went over the ends for the both, that means
* they are the same.
*
* If we reached only one end, that means the other string has
* something which then can be used to determine the return value.
*/
return (0);
return (-1);
}
return (1);
}
/*
* The u8_strcmp() function compares two UTF-8 strings quite similar to
* the strcmp(). For the comparison, however, Unicode Normalization specific
* equivalency and Unicode simple case conversion mappings based equivalency
* can be requested and checked against.
*/
int
int *errnum)
{
int f;
*errnum = 0;
/*
* Check on the requested Unicode version, case conversion, and
* normalization flag values.
*/
if (uv > U8_UNICODE_LATEST) {
}
if (flag == 0) {
flag = U8_STRCMP_CS;
} else {
if (f == 0) {
flag |= U8_STRCMP_CS;
} else if (f != U8_STRCMP_CS && f != U8_STRCMP_CI_UPPER &&
f != U8_STRCMP_CI_LOWER) {
flag = U8_STRCMP_CS;
}
if (f && f != U8_STRCMP_NFD && f != U8_STRCMP_NFC &&
f != U8_STRCMP_NFKD && f != U8_STRCMP_NFKC) {
flag = U8_STRCMP_CS;
}
}
if (flag == U8_STRCMP_CS) {
}
if (n != 0) {
if (n < n1)
n1 = n;
if (n < n2)
n2 = n;
}
/*
* Simple case conversion can be done much faster and so we do
* them separately here.
*/
if (flag == U8_STRCMP_CI_UPPER) {
} else if (flag == U8_STRCMP_CI_LOWER) {
}
}
{
int f;
int sz;
size_t i;
size_t j;
if (unicode_version > U8_UNICODE_LATEST) {
return ((size_t)-1);
}
if (f == (U8_TEXTPREP_TOUPPER | U8_TEXTPREP_TOLOWER)) {
return ((size_t)-1);
}
if (f && f != U8_TEXTPREP_NFD && f != U8_TEXTPREP_NFC &&
f != U8_TEXTPREP_NFKD && f != U8_TEXTPREP_NFKC) {
return ((size_t)-1);
}
return (0);
return ((size_t)-1);
}
ret_val = 0;
/*
* If we don't have a normalization flag set, we do the simple case
* conversion based text preparation separately below. Text
* preparation involving Normalization will be done in the false task
* block, again, separately since it will take much more time and
* resource than doing simple case conversions.
*/
if (f == 0) {
break;
if (sz < 0) {
if (do_not_ignore_invalid) {
break;
}
sz = 1;
ret_val++;
}
if (sz == 1) {
break;
}
if (is_it_toupper)
else if (is_it_tolower)
else
ib++;
ob++;
if (do_not_ignore_invalid) {
break;
}
break;
}
/*
* We treat the remaining incomplete character
* bytes as a character.
*/
ret_val++;
} else {
if (is_it_toupper || is_it_tolower) {
break;
}
} else {
break;
}
for (i = 0; i < sz; i++)
}
}
}
} else {
break;
/*
* If the current character is a 7-bit ASCII
* character and it is the last character, or,
* if the current character is a 7-bit ASCII
* character and the next character is also a 7-bit
* ASCII character, then, we copy over this
* character without going through collect_a_seq().
*
* In any other cases, we need to look further with
* the collect_a_seq() function.
*/
break;
}
if (is_it_toupper)
else if (is_it_tolower)
else
ib++;
ob++;
} else {
*errnum = 0;
if (*errnum && do_not_ignore_invalid) {
break;
}
break;
}
for (i = 0; i < j; i++)
}
}
}
return (ret_val);
}