2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
5 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
6 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
7 * at Electronni Visti IA, Kiev, Ukraine.
10 * Copyright (c) 2011 The FreeBSD Foundation
11 * All rights reserved.
12 * Portions of this software were developed by David Chisnall
13 * under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Adapted to xlocale by John Marino <draco@marino.st>
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "namespace.h"
44 #include <sys/types.h>
56 #include "un-namespace.h"
59 #include "setlocale.h"
61 #include "libc_private.h"
63 struct xlocale_collate __xlocale_global_collate = {
64 {{0}, "C"}, 1, 0, 0, 0
67 struct xlocale_collate __xlocale_C_collate = {
68 {{0}, "C"}, 1, 0, 0, 0
72 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table);
75 destruct_collate(void *t)
77 struct xlocale_collate *table = t;
78 if (table->map && (table->maplen > 0)) {
79 (void) munmap(table->map, table->maplen);
85 __collate_load(const char *encoding, __unused locale_t unused)
87 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0 ||
88 strncmp(encoding, "C.", 2) == 0) {
89 return &__xlocale_C_collate;
91 struct xlocale_collate *table = calloc(sizeof(struct xlocale_collate), 1);
92 table->header.header.destructor = destruct_collate;
93 // FIXME: Make sure that _LDP_CACHE is never returned. We should be doing
94 // the caching outside of this section
95 if (__collate_load_tables_l(encoding, table) != _LDP_LOADED) {
96 xlocale_release(table);
103 * Load the collation tables for the specified encoding into the global table.
106 __collate_load_tables(const char *encoding)
109 return (__collate_load_tables_l(encoding, &__xlocale_global_collate));
113 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table)
119 collate_info_t *info;
123 table->__collate_load_error = 1;
125 /* 'encoding' must be already checked. */
126 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0 ||
127 strncmp(encoding, "C.", 2) == 0) {
131 if (asprintf(&buf, "%s/%s/LC_COLLATE", _PathLocale, encoding) == -1)
134 if ((fd = _open(buf, O_RDONLY)) < 0) {
139 if (_fstat(fd, &sbuf) < 0) {
143 if (sbuf.st_size < (COLLATE_FMT_VERSION_LEN +
144 XLOCALE_DEF_VERSION_LEN +
150 map = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
152 if ((TMP = map) == MAP_FAILED) {
156 if (strncmp(TMP, COLLATE_FMT_VERSION, COLLATE_FMT_VERSION_LEN) != 0) {
157 (void) munmap(map, sbuf.st_size);
161 TMP += COLLATE_FMT_VERSION_LEN;
162 strlcat(table->header.version, TMP, sizeof (table->header.version));
163 TMP += XLOCALE_DEF_VERSION_LEN;
166 TMP += sizeof (*info);
168 if ((info->directive_count < 1) ||
169 (info->directive_count >= COLL_WEIGHTS_MAX) ||
170 ((chains = info->chain_count) < 0)) {
171 (void) munmap(map, sbuf.st_size);
176 i = (sizeof (collate_char_t) * (UCHAR_MAX + 1)) +
177 (sizeof (collate_chain_t) * chains) +
178 (sizeof (collate_large_t) * info->large_count);
179 for (z = 0; z < info->directive_count; z++) {
180 i += sizeof (collate_subst_t) * info->subst_count[z];
182 if (i != (sbuf.st_size - (TMP - map))) {
183 (void) munmap(map, sbuf.st_size);
188 if (table->map && (table->maplen > 0)) {
189 (void) munmap(table->map, table->maplen);
192 table->maplen = sbuf.st_size;
194 table->char_pri_table = (void *)TMP;
195 TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
197 for (z = 0; z < info->directive_count; z++) {
198 if (info->subst_count[z] > 0) {
199 table->subst_table[z] = (void *)TMP;
200 TMP += info->subst_count[z] * sizeof (collate_subst_t);
202 table->subst_table[z] = NULL;
207 table->chain_pri_table = (void *)TMP;
208 TMP += chains * sizeof (collate_chain_t);
210 table->chain_pri_table = NULL;
211 if (info->large_count > 0)
212 table->large_pri_table = (void *)TMP;
214 table->large_pri_table = NULL;
216 table->__collate_load_error = 0;
217 return (_LDP_LOADED);
220 static const int32_t *
221 substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
223 const collate_subst_t *p;
224 int n = table->info->subst_count[pass];
229 if (pass >= table->info->directive_count)
232 if (!(key & COLLATE_SUBST_PRIORITY))
235 p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
236 assert(p->key == key);
241 static collate_chain_t *
242 chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
245 int high = table->info->chain_count - 1;
248 collate_chain_t *tab = table->chain_pri_table;
253 while (low <= high) {
254 next = (low + high) / 2;
256 compar = *key - *p->str;
258 l = wcsnlen(p->str, COLLATE_STR_LEN);
259 compar = wcsncmp(key, p->str, l);
273 static collate_large_t *
274 largesearch(struct xlocale_collate *table, const wchar_t key)
277 int high = table->info->large_count - 1;
280 collate_large_t *tab = table->large_pri_table;
285 while (low <= high) {
286 next = (low + high) / 2;
288 compar = key - p->val;
300 _collate_lookup(struct xlocale_collate *table, const wchar_t *t, int *len,
301 int *pri, int which, const int **state)
304 collate_large_t *match;
309 * If this is the "last" pass for the UNDEFINED, then
310 * we just return the priority itself.
312 if (which >= table->info->directive_count) {
320 * If we have remaining substitution data from a previous
321 * call, consume it first.
323 if ((sptr = *state) != NULL) {
326 if ((sptr == *state) || (sptr == NULL))
334 /* No active substitutions */
338 * Check for composites such as diphthongs that collate as a
339 * single element (aka chains or collating-elements).
341 if (((p2 = chainsearch(table, t, &l)) != NULL) &&
342 ((p = p2->pri[which]) >= 0)) {
347 } else if (*t <= UCHAR_MAX) {
350 * Character is a small (8-bit) character.
351 * We just look these up directly for speed.
353 *pri = table->char_pri_table[*t].pri[which];
355 } else if ((table->info->large_count > 0) &&
356 ((match = largesearch(table, *t)) != NULL)) {
359 * Character was found in the extended table.
361 *pri = match->pri.pri[which];
365 * Character lacks a specific definition.
367 if (table->info->directive[which] & DIRECTIVE_UNDEFINED) {
368 /* Mask off sign bit to prevent ordering confusion. */
369 *pri = (*t & COLLATE_MAX_PRIORITY);
371 *pri = table->info->undef_pri[which];
373 /* No substitutions for undefined characters! */
378 * Try substituting (expanding) the character. We are
379 * currently doing this *after* the chain compression. I
380 * think it should not matter, but this way might be slightly
383 * We do this after the priority search, as this will help us
384 * to identify a single key value. In order for this to work,
385 * its important that the priority assigned to a given element
386 * to be substituted be unique for that level. The localedef
387 * code ensures this for us.
389 if ((sptr = substsearch(table, *pri, which)) != NULL) {
390 if ((*pri = *sptr) > 0) {
392 *state = *sptr ? sptr : NULL;
399 * This is the meaty part of wcsxfrm & strxfrm. Note that it does
400 * NOT NULL terminate. That is left to the caller.
403 _collate_wxfrm(struct xlocale_collate *table, const wchar_t *src, wchar_t *xf,
412 const int32_t *state;
415 int ndir = table->info->directive_count;
419 for (pass = 0; pass <= ndir; pass++) {
424 /* insert level separator from the previous pass */
432 /* special pass for undefined */
434 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
436 direc = table->info->directive[pass];
441 if (direc & DIRECTIVE_BACKWARD) {
444 if ((tr = wcsdup(t)) == NULL) {
449 fp = tr + wcslen(tr) - 1;
455 t = (const wchar_t *)tr;
458 if (direc & DIRECTIVE_POSITION) {
459 while (*t || state) {
460 _collate_lookup(table, t, &len, &pri, pass, &state);
468 pri = COLLATE_MAX_PRIORITY;
478 while (*t || state) {
479 _collate_lookup(table, t, &len, &pri, pass, &state);
503 return ((size_t)(-1));
507 * In the non-POSIX case, we transform each character into a string of
508 * characters representing the character's priority. Since char is usually
509 * signed, we are limited by 7 bits per byte. To avoid zero, we need to add
510 * XFRM_OFFSET, so we can't use a full 7 bits. For simplicity, we choose 6
513 * It turns out that we sometimes have real priorities that are
514 * 31-bits wide. (But: be careful using priorities where the high
515 * order bit is set -- i.e. the priority is negative. The sort order
516 * may be surprising!)
518 * TODO: This would be a good area to optimize somewhat. It turns out
519 * that real prioririties *except for the last UNDEFINED pass* are generally
520 * very small. We need the localedef code to precalculate the max
521 * priority for us, and ideally also give us a mask, and then we could
522 * severely limit what we expand to.
525 #define XFRM_OFFSET ('0') /* make all printable characters */
527 #define XFRM_MASK ((1 << XFRM_SHIFT) - 1)
528 #define XFRM_SEP ('.') /* chosen to be less than XFRM_OFFSET */
531 xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
533 /* we use unsigned to ensure zero fill on right shift */
534 uint32_t val = (uint32_t)table->info->pri_count[pass];
538 *p = (pri & XFRM_MASK) + XFRM_OFFSET;
548 _collate_sxfrm(struct xlocale_collate *table, const wchar_t *src, char *xf,
557 const int32_t *state;
561 uint8_t buf[XFRM_BYTES];
562 int ndir = table->info->directive_count;
566 for (pass = 0; pass <= ndir; pass++) {
571 /* insert level separator from the previous pass */
579 /* special pass for undefined */
581 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
583 direc = table->info->directive[pass];
588 if (direc & DIRECTIVE_BACKWARD) {
591 if ((tr = wcsdup(t)) == NULL) {
596 fp = tr + wcslen(tr) - 1;
602 t = (const wchar_t *)tr;
605 if (direc & DIRECTIVE_POSITION) {
606 while (*t || state) {
608 _collate_lookup(table, t, &len, &pri, pass, &state);
616 pri = COLLATE_MAX_PRIORITY;
619 b = xfrm(table, buf, pri, pass);
633 while (*t || state) {
634 _collate_lookup(table, t, &len, &pri, pass, &state);
645 b = xfrm(table, buf, pri, pass);
666 return ((size_t)(-1));
670 * __collate_equiv_value returns the primary collation value for the given
671 * collating symbol specified by str and len. Zero or negative is returned
672 * if the collating symbol was not found. This function is used by bracket
673 * code in the TRE regex library.
676 __collate_equiv_value(locale_t locale, const wchar_t *str, size_t len)
680 if (len < 1 || len >= COLLATE_STR_LEN)
684 struct xlocale_collate *table =
685 (struct xlocale_collate*)locale->components[XLC_COLLATE];
687 if (table->__collate_load_error)
688 return ((len == 1 && *str <= UCHAR_MAX) ? *str : -1);
692 if (*str <= UCHAR_MAX)
693 e = table->char_pri_table[*str].pri[0];
694 else if (table->info->large_count > 0) {
695 collate_large_t *match_large;
696 match_large = largesearch(table, *str);
698 e = match_large->pri.pri[0];
702 return (e > 0 ? e : 0);
704 if (table->info->chain_count > 0) {
705 wchar_t name[COLLATE_STR_LEN];
706 collate_chain_t *match_chain;
709 wcsncpy (name, str, len);
711 match_chain = chainsearch(table, name, &clen);
713 e = match_chain->pri[0];
716 return (e < 0 ? -e : e);