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_STR_LEN + sizeof (info))) {
148 map = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
150 if ((TMP = map) == MAP_FAILED) {
154 if (strncmp(TMP, COLLATE_VERSION, COLLATE_STR_LEN) != 0) {
155 (void) munmap(map, sbuf.st_size);
159 TMP += COLLATE_STR_LEN;
162 TMP += sizeof (*info);
164 if ((info->directive_count < 1) ||
165 (info->directive_count >= COLL_WEIGHTS_MAX) ||
166 ((chains = info->chain_count) < 0)) {
167 (void) munmap(map, sbuf.st_size);
172 i = (sizeof (collate_char_t) * (UCHAR_MAX + 1)) +
173 (sizeof (collate_chain_t) * chains) +
174 (sizeof (collate_large_t) * info->large_count);
175 for (z = 0; z < info->directive_count; z++) {
176 i += sizeof (collate_subst_t) * info->subst_count[z];
178 if (i != (sbuf.st_size - (TMP - map))) {
179 (void) munmap(map, sbuf.st_size);
184 if (table->map && (table->maplen > 0)) {
185 (void) munmap(table->map, table->maplen);
188 table->maplen = sbuf.st_size;
190 table->char_pri_table = (void *)TMP;
191 TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
193 for (z = 0; z < info->directive_count; z++) {
194 if (info->subst_count[z] > 0) {
195 table->subst_table[z] = (void *)TMP;
196 TMP += info->subst_count[z] * sizeof (collate_subst_t);
198 table->subst_table[z] = NULL;
203 table->chain_pri_table = (void *)TMP;
204 TMP += chains * sizeof (collate_chain_t);
206 table->chain_pri_table = NULL;
207 if (info->large_count > 0)
208 table->large_pri_table = (void *)TMP;
210 table->large_pri_table = NULL;
212 table->__collate_load_error = 0;
213 return (_LDP_LOADED);
216 static const int32_t *
217 substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
219 const collate_subst_t *p;
220 int n = table->info->subst_count[pass];
225 if (pass >= table->info->directive_count)
228 if (!(key & COLLATE_SUBST_PRIORITY))
231 p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
232 assert(p->key == key);
237 static collate_chain_t *
238 chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
241 int high = table->info->chain_count - 1;
244 collate_chain_t *tab = table->chain_pri_table;
249 while (low <= high) {
250 next = (low + high) / 2;
252 compar = *key - *p->str;
254 l = wcsnlen(p->str, COLLATE_STR_LEN);
255 compar = wcsncmp(key, p->str, l);
269 static collate_large_t *
270 largesearch(struct xlocale_collate *table, const wchar_t key)
273 int high = table->info->large_count - 1;
276 collate_large_t *tab = table->large_pri_table;
281 while (low <= high) {
282 next = (low + high) / 2;
284 compar = key - p->val;
296 _collate_lookup(struct xlocale_collate *table, const wchar_t *t, int *len,
297 int *pri, int which, const int **state)
300 collate_large_t *match;
305 * If this is the "last" pass for the UNDEFINED, then
306 * we just return the priority itself.
308 if (which >= table->info->directive_count) {
316 * If we have remaining substitution data from a previous
317 * call, consume it first.
319 if ((sptr = *state) != NULL) {
322 if ((sptr == *state) || (sptr == NULL))
330 /* No active substitutions */
334 * Check for composites such as diphthongs that collate as a
335 * single element (aka chains or collating-elements).
337 if (((p2 = chainsearch(table, t, &l)) != NULL) &&
338 ((p = p2->pri[which]) >= 0)) {
343 } else if (*t <= UCHAR_MAX) {
346 * Character is a small (8-bit) character.
347 * We just look these up directly for speed.
349 *pri = table->char_pri_table[*t].pri[which];
351 } else if ((table->info->large_count > 0) &&
352 ((match = largesearch(table, *t)) != NULL)) {
355 * Character was found in the extended table.
357 *pri = match->pri.pri[which];
361 * Character lacks a specific definition.
363 if (table->info->directive[which] & DIRECTIVE_UNDEFINED) {
364 /* Mask off sign bit to prevent ordering confusion. */
365 *pri = (*t & COLLATE_MAX_PRIORITY);
367 *pri = table->info->undef_pri[which];
369 /* No substitutions for undefined characters! */
374 * Try substituting (expanding) the character. We are
375 * currently doing this *after* the chain compression. I
376 * think it should not matter, but this way might be slightly
379 * We do this after the priority search, as this will help us
380 * to identify a single key value. In order for this to work,
381 * its important that the priority assigned to a given element
382 * to be substituted be unique for that level. The localedef
383 * code ensures this for us.
385 if ((sptr = substsearch(table, *pri, which)) != NULL) {
386 if ((*pri = *sptr) > 0) {
388 *state = *sptr ? sptr : NULL;
395 * This is the meaty part of wcsxfrm & strxfrm. Note that it does
396 * NOT NULL terminate. That is left to the caller.
399 _collate_wxfrm(struct xlocale_collate *table, const wchar_t *src, wchar_t *xf,
408 const int32_t *state;
411 int ndir = table->info->directive_count;
415 for (pass = 0; pass <= ndir; pass++) {
420 /* insert level separator from the previous pass */
428 /* special pass for undefined */
430 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
432 direc = table->info->directive[pass];
437 if (direc & DIRECTIVE_BACKWARD) {
440 if ((tr = wcsdup(t)) == NULL) {
445 fp = tr + wcslen(tr) - 1;
451 t = (const wchar_t *)tr;
454 if (direc & DIRECTIVE_POSITION) {
455 while (*t || state) {
456 _collate_lookup(table, t, &len, &pri, pass, &state);
464 pri = COLLATE_MAX_PRIORITY;
474 while (*t || state) {
475 _collate_lookup(table, t, &len, &pri, pass, &state);
499 return ((size_t)(-1));
503 * In the non-POSIX case, we transform each character into a string of
504 * characters representing the character's priority. Since char is usually
505 * signed, we are limited by 7 bits per byte. To avoid zero, we need to add
506 * XFRM_OFFSET, so we can't use a full 7 bits. For simplicity, we choose 6
509 * It turns out that we sometimes have real priorities that are
510 * 31-bits wide. (But: be careful using priorities where the high
511 * order bit is set -- i.e. the priority is negative. The sort order
512 * may be surprising!)
514 * TODO: This would be a good area to optimize somewhat. It turns out
515 * that real prioririties *except for the last UNDEFINED pass* are generally
516 * very small. We need the localedef code to precalculate the max
517 * priority for us, and ideally also give us a mask, and then we could
518 * severely limit what we expand to.
521 #define XFRM_OFFSET ('0') /* make all printable characters */
523 #define XFRM_MASK ((1 << XFRM_SHIFT) - 1)
524 #define XFRM_SEP ('.') /* chosen to be less than XFRM_OFFSET */
527 xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
529 /* we use unsigned to ensure zero fill on right shift */
530 uint32_t val = (uint32_t)table->info->pri_count[pass];
534 *p = (pri & XFRM_MASK) + XFRM_OFFSET;
544 _collate_sxfrm(struct xlocale_collate *table, const wchar_t *src, char *xf,
553 const int32_t *state;
557 uint8_t buf[XFRM_BYTES];
558 int ndir = table->info->directive_count;
562 for (pass = 0; pass <= ndir; pass++) {
567 /* insert level separator from the previous pass */
575 /* special pass for undefined */
577 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
579 direc = table->info->directive[pass];
584 if (direc & DIRECTIVE_BACKWARD) {
587 if ((tr = wcsdup(t)) == NULL) {
592 fp = tr + wcslen(tr) - 1;
598 t = (const wchar_t *)tr;
601 if (direc & DIRECTIVE_POSITION) {
602 while (*t || state) {
604 _collate_lookup(table, t, &len, &pri, pass, &state);
612 pri = COLLATE_MAX_PRIORITY;
615 b = xfrm(table, buf, pri, pass);
629 while (*t || state) {
630 _collate_lookup(table, t, &len, &pri, pass, &state);
641 b = xfrm(table, buf, pri, pass);
662 return ((size_t)(-1));
666 * __collate_equiv_value returns the primary collation value for the given
667 * collating symbol specified by str and len. Zero or negative is returned
668 * if the collating symbol was not found. This function is used by bracket
669 * code in the TRE regex library.
672 __collate_equiv_value(locale_t locale, const wchar_t *str, size_t len)
676 if (len < 1 || len >= COLLATE_STR_LEN)
680 struct xlocale_collate *table =
681 (struct xlocale_collate*)locale->components[XLC_COLLATE];
683 if (table->__collate_load_error)
684 return ((len == 1 && *str <= UCHAR_MAX) ? *str : -1);
688 if (*str <= UCHAR_MAX)
689 e = table->char_pri_table[*str].pri[0];
690 else if (table->info->large_count > 0) {
691 collate_large_t *match_large;
692 match_large = largesearch(table, *str);
694 e = match_large->pri.pri[0];
698 return (e > 0 ? e : 0);
700 if (table->info->chain_count > 0) {
701 wchar_t name[COLLATE_STR_LEN];
702 collate_chain_t *match_chain;
705 wcsncpy (name, str, len);
707 match_chain = chainsearch(table, name, &clen);
709 e = match_chain->pri[0];
712 return (e < 0 ? -e : e);