2 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
5 * at Electronni Visti IA, Kiev, Ukraine.
8 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Adapted to xlocale by John Marino <draco@marino.st>
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "namespace.h"
42 #include <sys/types.h>
54 #include "un-namespace.h"
58 #include "setlocale.h"
60 #include "libc_private.h"
62 struct xlocale_collate __xlocale_global_collate = {
63 {{0}, "C"}, 1, 0, 0, 0
66 struct xlocale_collate __xlocale_C_collate = {
67 {{0}, "C"}, 1, 0, 0, 0
71 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table);
74 destruct_collate(void *t)
76 struct xlocale_collate *table = t;
77 if (table->map && (table->maplen > 0)) {
78 (void) munmap(table->map, table->maplen);
84 __collate_load(const char *encoding, __unused locale_t unused)
86 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
87 return &__xlocale_C_collate;
89 struct xlocale_collate *table = calloc(sizeof(struct xlocale_collate), 1);
90 table->header.header.destructor = destruct_collate;
91 // FIXME: Make sure that _LDP_CACHE is never returned. We should be doing
92 // the caching outside of this section
93 if (__collate_load_tables_l(encoding, table) != _LDP_LOADED) {
94 xlocale_release(table);
101 * Load the collation tables for the specified encoding into the global table.
104 __collate_load_tables(const char *encoding)
107 return (__collate_load_tables_l(encoding, &__xlocale_global_collate));
111 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table)
117 collate_info_t *info;
121 table->__collate_load_error = 1;
123 /* 'encoding' must be already checked. */
124 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
128 if (asprintf(&buf, "%s/%s/LC_COLLATE", _PathLocale, encoding) == -1)
131 if ((fd = _open(buf, O_RDONLY)) < 0) {
136 if (_fstat(fd, &sbuf) < 0) {
140 if (sbuf.st_size < (COLLATE_STR_LEN + sizeof (info))) {
145 map = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
147 if ((TMP = map) == NULL) {
151 if (strncmp(TMP, COLLATE_VERSION, COLLATE_STR_LEN) != 0) {
152 (void) munmap(map, sbuf.st_size);
156 TMP += COLLATE_STR_LEN;
159 TMP += sizeof (*info);
161 if ((info->directive_count < 1) ||
162 (info->directive_count >= COLL_WEIGHTS_MAX) ||
163 ((chains = BSWAP(info->chain_count)) < 0)) {
164 (void) munmap(map, sbuf.st_size);
169 i = (sizeof (collate_char_t) * (UCHAR_MAX + 1)) +
170 (sizeof (collate_chain_t) * chains) +
171 (sizeof (collate_large_t) * BSWAP(info->large_count));
172 for (z = 0; z < info->directive_count; z++) {
173 i += sizeof (collate_subst_t) * BSWAP(info->subst_count[z]);
175 if (i != (sbuf.st_size - (TMP - map))) {
176 (void) munmap(map, sbuf.st_size);
182 table->char_pri_table = (void *)TMP;
183 TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
185 for (z = 0; z < info->directive_count; z++) {
186 if (BSWAP(info->subst_count[z]) > 0) {
187 table->subst_table[z] = (void *)TMP;
188 TMP += BSWAP(info->subst_count[z]) * sizeof (collate_subst_t);
190 table->subst_table[z] = NULL;
195 table->chain_pri_table = (void *)TMP;
196 TMP += chains * sizeof (collate_chain_t);
198 table->chain_pri_table = NULL;
199 if (BSWAP(info->large_count) > 0)
200 table->large_pri_table = (void *)TMP;
202 table->large_pri_table = NULL;
204 table->__collate_load_error = 0;
205 return (_LDP_LOADED);
208 static const int32_t *
209 substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
211 const collate_subst_t *p;
212 int n = BSWAP(table->info->subst_count[pass]);
217 if (pass >= table->info->directive_count)
220 if (!(key & COLLATE_SUBST_PRIORITY))
223 p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
224 assert(BSWAP(p->key) == key);
229 static collate_chain_t *
230 chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
233 int high = BSWAP(table->info->chain_count) - 1;
236 collate_chain_t *tab = table->chain_pri_table;
241 while (low <= high) {
242 next = (low + high) / 2;
244 compar = *key - le16toh(*p->str);
246 l = wcsnlen(p->str, COLLATE_STR_LEN);
247 compar = wcsncmp(key, p->str, l);
261 static collate_large_t *
262 largesearch(struct xlocale_collate *table, const wchar_t key)
265 int high = BSWAP(table->info->large_count) - 1;
268 collate_large_t *tab = table->large_pri_table;
273 while (low <= high) {
274 next = (low + high) / 2;
276 compar = key - BSWAP(p->val);
288 _collate_lookup(struct xlocale_collate *table, const wchar_t *t, int *len,
289 int *pri, int which, const int **state)
292 collate_large_t *match;
297 * If this is the "last" pass for the UNDEFINED, then
298 * we just return the priority itself.
300 if (which >= table->info->directive_count) {
308 * If we have remaining substitution data from a previous
309 * call, consume it first.
311 if ((sptr = *state) != NULL) {
314 if ((sptr == *state) || (sptr == NULL))
322 /* No active substitutions */
326 * Check for composites such as diphthongs that collate as a
327 * single element (aka chains or collating-elements).
329 if (((p2 = chainsearch(table, t, &l)) != NULL) &&
330 ((p = p2->pri[which]) >= 0)) {
335 } else if (*t <= UCHAR_MAX) {
338 * Character is a small (8-bit) character.
339 * We just look these up directly for speed.
341 *pri = BSWAP(table->char_pri_table[*t].pri[which]);
343 } else if ((BSWAP(table->info->large_count) > 0) &&
344 ((match = largesearch(table, *t)) != NULL)) {
347 * Character was found in the extended table.
349 *pri = BSWAP(match->pri.pri[which]);
353 * Character lacks a specific definition.
355 if (table->info->directive[which] & DIRECTIVE_UNDEFINED) {
356 /* Mask off sign bit to prevent ordering confusion. */
357 *pri = (*t & COLLATE_MAX_PRIORITY);
359 *pri = BSWAP(table->info->undef_pri[which]);
361 /* No substitutions for undefined characters! */
366 * Try substituting (expanding) the character. We are
367 * currently doing this *after* the chain compression. I
368 * think it should not matter, but this way might be slightly
371 * We do this after the priority search, as this will help us
372 * to identify a single key value. In order for this to work,
373 * its important that the priority assigned to a given element
374 * to be substituted be unique for that level. The localedef
375 * code ensures this for us.
377 if ((sptr = substsearch(table, *pri, which)) != NULL) {
378 if ((*pri = BSWAP(*sptr)) > 0) {
380 *state = BSWAP(*sptr) ? sptr : NULL;
387 * This is the meaty part of wcsxfrm & strxfrm. Note that it does
388 * NOT NULL terminate. That is left to the caller.
391 _collate_wxfrm(struct xlocale_collate *table, const wchar_t *src, wchar_t *xf,
400 const int32_t *state;
403 int ndir = table->info->directive_count;
407 for (pass = 0; pass <= ndir; pass++) {
412 /* insert level separator from the previous pass */
420 /* special pass for undefined */
422 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
424 direc = table->info->directive[pass];
429 if (direc & DIRECTIVE_BACKWARD) {
432 if ((tr = wcsdup(t)) == NULL) {
437 fp = tr + wcslen(tr) - 1;
443 t = (const wchar_t *)tr;
446 if (direc & DIRECTIVE_POSITION) {
447 while (*t || state) {
448 _collate_lookup(table, t, &len, &pri, pass, &state);
456 pri = COLLATE_MAX_PRIORITY;
466 while (*t || state) {
467 _collate_lookup(table, t, &len, &pri, pass, &state);
491 return ((size_t)(-1));
495 * In the non-POSIX case, we transform each character into a string of
496 * characters representing the character's priority. Since char is usually
497 * signed, we are limited by 7 bits per byte. To avoid zero, we need to add
498 * XFRM_OFFSET, so we can't use a full 7 bits. For simplicity, we choose 6
501 * It turns out that we sometimes have real priorities that are
502 * 31-bits wide. (But: be careful using priorities where the high
503 * order bit is set -- i.e. the priority is negative. The sort order
504 * may be surprising!)
506 * TODO: This would be a good area to optimize somewhat. It turns out
507 * that real prioririties *except for the last UNDEFINED pass* are generally
508 * very small. We need the localedef code to precalculate the max
509 * priority for us, and ideally also give us a mask, and then we could
510 * severely limit what we expand to.
513 #define XFRM_OFFSET ('0') /* make all printable characters */
515 #define XFRM_MASK ((1 << XFRM_SHIFT) - 1)
516 #define XFRM_SEP ('.') /* chosen to be less than XFRM_OFFSET */
519 xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
521 /* we use unsigned to ensure zero fill on right shift */
522 uint32_t val = BSWAP((uint32_t)table->info->pri_count[pass]);
526 *p = (pri & XFRM_MASK) + XFRM_OFFSET;
536 _collate_sxfrm(struct xlocale_collate *table, const wchar_t *src, char *xf,
545 const int32_t *state;
549 uint8_t buf[XFRM_BYTES];
550 int ndir = table->info->directive_count;
554 for (pass = 0; pass <= ndir; pass++) {
559 /* insert level separator from the previous pass */
567 /* special pass for undefined */
569 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
571 direc = table->info->directive[pass];
576 if (direc & DIRECTIVE_BACKWARD) {
579 if ((tr = wcsdup(t)) == NULL) {
584 fp = tr + wcslen(tr) - 1;
590 t = (const wchar_t *)tr;
593 if (direc & DIRECTIVE_POSITION) {
594 while (*t || state) {
596 _collate_lookup(table, t, &len, &pri, pass, &state);
604 pri = COLLATE_MAX_PRIORITY;
607 b = xfrm(table, buf, pri, pass);
621 while (*t || state) {
622 _collate_lookup(table, t, &len, &pri, pass, &state);
633 b = xfrm(table, buf, pri, pass);
654 return ((size_t)(-1));
658 * __collate_equiv_value returns the primary collation value for the given
659 * collating symbol specified by str and len. Zero or negative is returned
660 * if the collating symbol was not found. This function is used by bracket
661 * code in the TRE regex library.
664 __collate_equiv_value(locale_t locale, const wchar_t *str, size_t len)
668 if (len < 1 || len >= COLLATE_STR_LEN)
672 struct xlocale_collate *table =
673 (struct xlocale_collate*)locale->components[XLC_COLLATE];
675 if (table->__collate_load_error)
676 return ((len == 1 && *str <= UCHAR_MAX) ? *str : -1);
680 if (*str <= UCHAR_MAX)
681 e = table->char_pri_table[*str].pri[0];
682 else if (BSWAP(table->info->large_count) > 0) {
683 collate_large_t *match_large;
684 match_large = largesearch(table, *str);
686 e = match_large->pri.pri[0];
690 return (e > 0 ? e : 0);
692 if (BSWAP(table->info->chain_count) > 0) {
693 wchar_t name[COLLATE_STR_LEN];
694 collate_chain_t *match_chain;
697 wcsncpy (name, str, len);
699 match_chain = chainsearch(table, name, &clen);
701 e = match_chain->pri[0];
704 return (e < 0 ? -e : e);