+/*
+ * __collate_collating_symbol takes the multibyte string specified by
+ * src and slen, and using ps, converts that to a wide character. Then
+ * it is checked to verify it is a collating symbol, and then copies
+ * it to the wide character string specified by dst and dlen (the
+ * results are not null terminated). The length of the wide characters
+ * copied to dst is returned if successful. Zero is returned if no such
+ * collating symbol exists. (size_t)-1 is returned if there are wide-character
+ * conversion errors, if the length of the converted string is greater that
+ * STR_LEN or if dlen is too small. It is up to the calling routine to
+ * preserve the mbstate_t structure as needed.
+ */
+__private_extern__ size_t
+__collate_collating_symbol(wchar_t *dst, size_t dlen, const char *src, size_t slen, mbstate_t *ps, locale_t loc)
+{
+ wchar_t wname[STR_LEN];
+ wchar_t w, *wp;
+ size_t len, l;
+
+ /* POSIX locale */
+ if (loc->__collate_load_error) {
+ if (dlen < 1)
+ return (size_t)-1;
+ if (slen != 1 || !isascii(*src))
+ return 0;
+ *dst = *src;
+ return 1;
+ }
+ for(wp = wname, len = 0; slen > 0; len++) {
+ l = mbrtowc_l(&w, src, slen, ps, loc);
+ if (l == (size_t)-1 || l == (size_t)-2)
+ return (size_t)-1;
+ if (l == 0)
+ break;
+ if (len >= STR_LEN)
+ return -1;
+ *wp++ = w;
+ src += l;
+ slen = (long)slen - (long)l;
+ }
+ if (len == 0 || len > dlen)
+ return (size_t)-1;
+ if (len == 1) {
+ if (*wname <= UCHAR_MAX) {
+ if (__collate_char_pri_table[*wname].pri[0] >= 0) {
+ if (dlen > 0)
+ *dst = *wname;
+ return 1;
+ }
+ return 0;
+ } else if (__collate_info->large_pri_count > 0) {
+ struct __collate_st_large_char_pri *match;
+ match = largesearch(*wname, loc);
+ if (match && match->pri.pri[0] >= 0) {
+ if (dlen > 0)
+ *dst = *wname;
+ return 1;
+ }
+ }
+ return 0;
+ }
+ *wp = 0;
+ if (__collate_info->chain_count > 0) {
+ struct __collate_st_chain_pri *match;
+ int ll;
+ match = chainsearch(wname, &ll, loc);
+ if (match) {
+ if (ll < dlen)
+ dlen = ll;
+ wcsncpy(dst, wname, dlen);
+ return ll;
+ }
+ }
+ return 0;
+}
+
+/*
+ * __collate_equiv_class returns the equivalence class number for the symbol
+ * specified by src and slen, using ps to convert from multi-byte to wide
+ * character. Zero is returned if the symbol is not in an equivalence
+ * class. -1 is returned if there are wide character conversion error,
+ * if there are any greater-than-8-bit characters or if a multi-byte symbol
+ * is greater or equal to STR_LEN in length. It is up to the calling
+ * routine to preserve the mbstate_t structure as needed.
+ */
+__private_extern__ int
+__collate_equiv_class(const char *src, size_t slen, mbstate_t *ps, locale_t loc)
+{
+ wchar_t wname[STR_LEN];
+ wchar_t w, *wp;
+ size_t len, l;
+ int e;
+
+ /* POSIX locale */
+ if (loc->__collate_load_error)
+ return 0;
+ for(wp = wname, len = 0; slen > 0; len++) {
+ l = mbrtowc_l(&w, src, slen, ps, loc);
+ if (l == (size_t)-1 || l == (size_t)-2)
+ return -1;
+ if (l == 0)
+ break;
+ if (len >= STR_LEN)
+ return -1;
+ *wp++ = w;
+ src += l;
+ slen = (long)slen - (long)l;
+ }
+ if (len == 0)
+ return -1;
+ if (len == 1) {
+ e = -1;
+ if (*wname <= UCHAR_MAX)
+ e = __collate_char_pri_table[*wname].pri[0];
+ else if (__collate_info->large_pri_count > 0) {
+ struct __collate_st_large_char_pri *match;
+ match = largesearch(*wname, loc);
+ if (match)
+ e = match->pri.pri[0];
+ }
+ if (e == 0)
+ return IGNORE_EQUIV_CLASS;
+ return e > 0 ? e : 0;
+ }
+ *wp = 0;
+ if (__collate_info->chain_count > 0) {
+ struct __collate_st_chain_pri *match;
+ int ll;
+ match = chainsearch(wname, &ll, loc);
+ if (match) {
+ e = match->pri[0];
+ if (e == 0)
+ return IGNORE_EQUIV_CLASS;
+ return e < 0 ? -e : e;
+ }
+ }
+ return 0;
+}
+
+/*
+ * __collate_equiv_match tries to match any single or multi-character symbol
+ * in equivalence class equiv_class in the multi-byte string specified by src
+ * and slen. If start is non-zero, it is taken to be the first (pre-converted)
+ * wide character. Subsequence wide characters, if needed, will use ps in
+ * the conversion. On a successful match, the length of the matched string
+ * is returned (including the start character). If dst is non-NULL, the
+ * matched wide-character string is copied to dst, a wide character array of
+ * length dlen (the results are not zero-terminated). If rlen is non-NULL,
+ * the number of character in src actually used is returned. Zero is
+ * returned by __collate_equiv_match if there is no match. (size_t)-1 is
+ * returned on error: if there were conversion errors or if dlen is too small
+ * to accept the results. On no match or error, ps is restored to its incoming
+ * state.
+ */
+size_t
+__collate_equiv_match(int equiv_class, wchar_t *dst, size_t dlen, wchar_t start, const char *src, size_t slen, mbstate_t *ps, size_t *rlen, locale_t loc)
+{
+ wchar_t w;
+ size_t len, l, clen;
+ int i;
+ wchar_t buf[STR_LEN], *wp;
+ mbstate_t save;
+ const char *s = src;
+ size_t sl = slen;
+ struct __collate_st_chain_pri *ch = NULL;
+
+ /* POSIX locale */
+ if (loc->__collate_load_error)
+ return (size_t)-1;
+ if (equiv_class == IGNORE_EQUIV_CLASS)
+ equiv_class = 0;
+ if (ps)
+ save = *ps;
+ wp = buf;
+ len = clen = 0;
+ if (start) {
+ *wp++ = start;
+ len = 1;
+ }
+ /* convert up to the max chain length */
+ while(sl > 0 && len < __collate_info->chain_max_len) {
+ l = mbrtowc_l(&w, s, sl, ps, loc);
+ if (l == (size_t)-1 || l == (size_t)-2 || l == 0)
+ break;
+ *wp++ = w;
+ s += l;
+ clen += l;
+ sl -= l;
+ len++;
+ }
+ *wp = 0;
+ if (len > 1 && (ch = chainsearch(buf, &i, loc)) != NULL) {
+ int e = ch->pri[0];
+ if (e < 0)
+ e = -e;
+ if (e == equiv_class)
+ goto found;
+ }
+ /* try single character */
+ i = 1;
+ if (*buf <= UCHAR_MAX) {
+ if (equiv_class == __collate_char_pri_table[*buf].pri[0])
+ goto found;
+ } else if (__collate_info->large_pri_count > 0) {
+ struct __collate_st_large_char_pri *match;
+ match = largesearch(*buf, loc);
+ if (match && equiv_class == match->pri.pri[0])
+ goto found;
+ }
+ /* no match */
+ if (ps)
+ *ps = save;
+ return 0;
+found:
+ /* if we converted more than we used, restore to initial and reconvert
+ * up to what did match */
+ if (i < len) {
+ len = i;
+ if (ps)
+ *ps = save;
+ if (start)
+ i--;
+ clen = 0;
+ while(i-- > 0) {
+ l = mbrtowc_l(&w, src, slen, ps, loc);
+ src += l;
+ clen += l;
+ slen -= l;
+ }
+ }
+ if (dst) {
+ if (dlen < len) {
+ if (ps)
+ *ps = save;
+ return (size_t)-1;
+ }
+ for(wp = buf; len > 0; len--)
+ *dst++ = *wp++;
+ }
+ if (rlen)
+ *rlen = clen;
+ return len;
+}
+
+/*
+ * __collate_equiv_value returns the primary collation value for the given
+ * collating symbol specified by str and len. Zero or negative is return
+ * if the collating symbol was not found. (Use by the bracket code in TRE.)
+ */
+__private_extern__ int
+__collate_equiv_value(locale_t loc, const wchar_t *str, size_t len)
+{
+ int e;
+
+ if (len < 1 || len >= STR_LEN)
+ return -1;
+
+ /* POSIX locale */
+ if (loc->__collate_load_error)
+ return (len == 1 && *str <= UCHAR_MAX) ? *str : -1;
+
+ if (len == 1) {
+ e = -1;
+ if (*str <= UCHAR_MAX)
+ e = __collate_char_pri_table[*str].pri[0];
+ else if (__collate_info->large_pri_count > 0) {
+ struct __collate_st_large_char_pri *match;
+ match = largesearch(*str, loc);
+ if (match)
+ e = match->pri.pri[0];
+ }
+ if (e == 0)
+ return IGNORE_EQUIV_CLASS;
+ return e > 0 ? e : 0;
+ }
+ if (__collate_info->chain_count > 0) {
+ wchar_t name[STR_LEN];
+ struct __collate_st_chain_pri *match;
+ int ll;
+
+ wcsncpy(name, str, len);
+ name[len] = 0;
+ match = chainsearch(name, &ll, loc);
+ if (match) {
+ e = match->pri[0];
+ if (e == 0)
+ return IGNORE_EQUIV_CLASS;
+ return e < 0 ? -e : e;
+ }
+ }
+ return 0;
+}
+
+#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
+static void
+wntohl(wchar_t *str, int len)
+{
+ for(; *str && len > 0; str++, len--)
+ *str = ntohl(*str);
+}
+#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
+