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 asprintf(&buf, "%s/%s/LC_COLLATE", _PathLocale, encoding);
132 if ((fd = _open(buf, O_RDONLY)) < 0) {
137 if (_fstat(fd, &sbuf) < 0) {
141 if (sbuf.st_size < (COLLATE_STR_LEN + sizeof (info))) {
146 map = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
148 if ((TMP = map) == NULL) {
152 if (strncmp(TMP, COLLATE_VERSION, COLLATE_STR_LEN) != 0) {
153 (void) munmap(map, sbuf.st_size);
157 TMP += COLLATE_STR_LEN;
160 TMP += sizeof (*info);
162 if ((info->directive_count < 1) ||
163 (info->directive_count >= COLL_WEIGHTS_MAX) ||
164 ((chains = BSWAP(info->chain_count)) < 0)) {
165 (void) munmap(map, sbuf.st_size);
170 i = (sizeof (collate_char_t) * (UCHAR_MAX + 1)) +
171 (sizeof (collate_chain_t) * chains) +
172 (sizeof (collate_large_t) * BSWAP(info->large_count));
173 for (z = 0; z < info->directive_count; z++) {
174 i += sizeof (collate_subst_t) * BSWAP(info->subst_count[z]);
176 if (i != (sbuf.st_size - (TMP - map))) {
177 (void) munmap(map, sbuf.st_size);
183 table->char_pri_table = (void *)TMP;
184 TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
186 for (z = 0; z < info->directive_count; z++) {
187 if (BSWAP(info->subst_count[z]) > 0) {
188 table->subst_table[z] = (void *)TMP;
189 TMP += BSWAP(info->subst_count[z]) * sizeof (collate_subst_t);
191 table->subst_table[z] = NULL;
196 table->chain_pri_table = (void *)TMP;
197 TMP += chains * sizeof (collate_chain_t);
199 table->chain_pri_table = NULL;
200 if (BSWAP(info->large_count) > 0)
201 table->large_pri_table = (void *)TMP;
203 table->large_pri_table = NULL;
205 table->__collate_load_error = 0;
206 return (_LDP_LOADED);
209 static const int32_t *
210 substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
212 const collate_subst_t *p;
213 int n = BSWAP(table->info->subst_count[pass]);
218 if (pass >= table->info->directive_count)
221 if (!(key & COLLATE_SUBST_PRIORITY))
224 p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
225 assert(BSWAP(p->key) == key);
230 static collate_chain_t *
231 chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
234 int high = BSWAP(table->info->chain_count) - 1;
237 collate_chain_t *tab = table->chain_pri_table;
242 while (low <= high) {
243 next = (low + high) / 2;
245 compar = *key - le16toh(*p->str);
247 l = wcsnlen(p->str, COLLATE_STR_LEN);
248 compar = wcsncmp(key, p->str, l);
262 static collate_large_t *
263 largesearch(struct xlocale_collate *table, const wchar_t key)
266 int high = BSWAP(table->info->large_count) - 1;
269 collate_large_t *tab = table->large_pri_table;
274 while (low <= high) {
275 next = (low + high) / 2;
277 compar = key - BSWAP(p->val);
289 _collate_lookup(struct xlocale_collate *table, const wchar_t *t, int *len,
290 int *pri, int which, const int **state)
293 collate_large_t *match;
298 * If this is the "last" pass for the UNDEFINED, then
299 * we just return the priority itself.
301 if (which >= table->info->directive_count) {
309 * If we have remaining substitution data from a previous
310 * call, consume it first.
312 if ((sptr = *state) != NULL) {
315 if ((sptr == *state) || (sptr == NULL))
323 /* No active substitutions */
327 * Check for composites such as diphthongs that collate as a
328 * single element (aka chains or collating-elements).
330 if (((p2 = chainsearch(table, t, &l)) != NULL) &&
331 ((p = p2->pri[which]) >= 0)) {
336 } else if (*t <= UCHAR_MAX) {
339 * Character is a small (8-bit) character.
340 * We just look these up directly for speed.
342 *pri = BSWAP(table->char_pri_table[*t].pri[which]);
344 } else if ((BSWAP(table->info->large_count) > 0) &&
345 ((match = largesearch(table, *t)) != NULL)) {
348 * Character was found in the extended table.
350 *pri = BSWAP(match->pri.pri[which]);
354 * Character lacks a specific definition.
356 if (table->info->directive[which] & DIRECTIVE_UNDEFINED) {
357 /* Mask off sign bit to prevent ordering confusion. */
358 *pri = (*t & COLLATE_MAX_PRIORITY);
360 *pri = BSWAP(table->info->undef_pri[which]);
362 /* No substitutions for undefined characters! */
367 * Try substituting (expanding) the character. We are
368 * currently doing this *after* the chain compression. I
369 * think it should not matter, but this way might be slightly
372 * We do this after the priority search, as this will help us
373 * to identify a single key value. In order for this to work,
374 * its important that the priority assigned to a given element
375 * to be substituted be unique for that level. The localedef
376 * code ensures this for us.
378 if ((sptr = substsearch(table, *pri, which)) != NULL) {
379 if ((*pri = BSWAP(*sptr)) > 0) {
381 *state = BSWAP(*sptr) ? sptr : NULL;
388 * This is the meaty part of wcsxfrm & strxfrm. Note that it does
389 * NOT NULL terminate. That is left to the caller.
392 _collate_wxfrm(struct xlocale_collate *table, const wchar_t *src, wchar_t *xf,
401 const int32_t *state;
404 int ndir = table->info->directive_count;
408 for (pass = 0; pass <= ndir; pass++) {
413 /* insert level separator from the previous pass */
421 /* special pass for undefined */
423 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
425 direc = table->info->directive[pass];
430 if (direc & DIRECTIVE_BACKWARD) {
433 if ((tr = wcsdup(t)) == NULL) {
438 fp = tr + wcslen(tr) - 1;
444 t = (const wchar_t *)tr;
447 if (direc & DIRECTIVE_POSITION) {
448 while (*t || state) {
449 _collate_lookup(table, t, &len, &pri, pass, &state);
457 pri = COLLATE_MAX_PRIORITY;
467 while (*t || state) {
468 _collate_lookup(table, t, &len, &pri, pass, &state);
492 return ((size_t)(-1));
496 * In the non-POSIX case, we transform each character into a string of
497 * characters representing the character's priority. Since char is usually
498 * signed, we are limited by 7 bits per byte. To avoid zero, we need to add
499 * XFRM_OFFSET, so we can't use a full 7 bits. For simplicity, we choose 6
502 * It turns out that we sometimes have real priorities that are
503 * 31-bits wide. (But: be careful using priorities where the high
504 * order bit is set -- i.e. the priority is negative. The sort order
505 * may be surprising!)
507 * TODO: This would be a good area to optimize somewhat. It turns out
508 * that real prioririties *except for the last UNDEFINED pass* are generally
509 * very small. We need the localedef code to precalculate the max
510 * priority for us, and ideally also give us a mask, and then we could
511 * severely limit what we expand to.
514 #define XFRM_OFFSET ('0') /* make all printable characters */
516 #define XFRM_MASK ((1 << XFRM_SHIFT) - 1)
517 #define XFRM_SEP ('.') /* chosen to be less than XFRM_OFFSET */
520 xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
522 /* we use unsigned to ensure zero fill on right shift */
523 uint32_t val = BSWAP((uint32_t)table->info->pri_count[pass]);
527 *p = (pri & XFRM_MASK) + XFRM_OFFSET;
537 _collate_sxfrm(struct xlocale_collate *table, const wchar_t *src, char *xf,
546 const int32_t *state;
550 uint8_t buf[XFRM_BYTES];
551 int ndir = table->info->directive_count;
555 for (pass = 0; pass <= ndir; pass++) {
560 /* insert level separator from the previous pass */
568 /* special pass for undefined */
570 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
572 direc = table->info->directive[pass];
577 if (direc & DIRECTIVE_BACKWARD) {
580 if ((tr = wcsdup(t)) == NULL) {
585 fp = tr + wcslen(tr) - 1;
591 t = (const wchar_t *)tr;
594 if (direc & DIRECTIVE_POSITION) {
595 while (*t || state) {
597 _collate_lookup(table, t, &len, &pri, pass, &state);
605 pri = COLLATE_MAX_PRIORITY;
608 b = xfrm(table, buf, pri, pass);
622 while (*t || state) {
623 _collate_lookup(table, t, &len, &pri, pass, &state);
634 b = xfrm(table, buf, pri, pass);
655 return ((size_t)(-1));
659 * __collate_equiv_value returns the primary collation value for the given
660 * collating symbol specified by str and len. Zero or negative is returned
661 * if the collating symbol was not found. This function is used by bracket
662 * code in the TRE regex library.
665 __collate_equiv_value(locale_t locale, const wchar_t *str, size_t len)
669 if (len < 1 || len >= COLLATE_STR_LEN)
673 struct xlocale_collate *table =
674 (struct xlocale_collate*)locale->components[XLC_COLLATE];
676 if (table->__collate_load_error)
677 return ((len == 1 && *str <= UCHAR_MAX) ? *str : -1);
681 if (*str <= UCHAR_MAX)
682 e = table->char_pri_table[*str].pri[0];
683 else if (BSWAP(table->info->large_count) > 0) {
684 collate_large_t *match_large;
685 match_large = largesearch(table, *str);
687 e = match_large->pri.pri[0];
691 return (e > 0 ? e : 0);
693 if (BSWAP(table->info->chain_count) > 0) {
694 wchar_t name[COLLATE_STR_LEN];
695 collate_chain_t *match_chain;
698 wcsncpy (name, str, len);
700 match_chain = chainsearch(table, name, &clen);
702 e = match_chain->pri[0];
705 return (e < 0 ? -e : e);