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"
60 #include "setlocale.h"
62 #include "libc_private.h"
64 struct xlocale_collate __xlocale_global_collate = {
65 {{0}, "C"}, 1, 0, 0, 0
68 struct xlocale_collate __xlocale_C_collate = {
69 {{0}, "C"}, 1, 0, 0, 0
73 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table);
76 destruct_collate(void *t)
78 struct xlocale_collate *table = t;
79 if (table->map && (table->maplen > 0)) {
80 (void) munmap(table->map, table->maplen);
86 __collate_load(const char *encoding, __unused locale_t unused)
88 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 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) {
130 if (asprintf(&buf, "%s/%s/LC_COLLATE", _PathLocale, encoding) == -1)
133 if ((fd = _open(buf, O_RDONLY)) < 0) {
138 if (_fstat(fd, &sbuf) < 0) {
142 if (sbuf.st_size < (COLLATE_STR_LEN + sizeof (info))) {
147 map = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
149 if ((TMP = map) == NULL) {
153 if (strncmp(TMP, COLLATE_VERSION, COLLATE_STR_LEN) != 0) {
154 (void) munmap(map, sbuf.st_size);
158 TMP += COLLATE_STR_LEN;
161 TMP += sizeof (*info);
163 if ((info->directive_count < 1) ||
164 (info->directive_count >= COLL_WEIGHTS_MAX) ||
165 ((chains = BSWAP(info->chain_count)) < 0)) {
166 (void) munmap(map, sbuf.st_size);
171 i = (sizeof (collate_char_t) * (UCHAR_MAX + 1)) +
172 (sizeof (collate_chain_t) * chains) +
173 (sizeof (collate_large_t) * BSWAP(info->large_count));
174 for (z = 0; z < info->directive_count; z++) {
175 i += sizeof (collate_subst_t) * BSWAP(info->subst_count[z]);
177 if (i != (sbuf.st_size - (TMP - map))) {
178 (void) munmap(map, sbuf.st_size);
184 table->char_pri_table = (void *)TMP;
185 TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
187 for (z = 0; z < info->directive_count; z++) {
188 if (BSWAP(info->subst_count[z]) > 0) {
189 table->subst_table[z] = (void *)TMP;
190 TMP += BSWAP(info->subst_count[z]) * sizeof (collate_subst_t);
192 table->subst_table[z] = NULL;
197 table->chain_pri_table = (void *)TMP;
198 TMP += chains * sizeof (collate_chain_t);
200 table->chain_pri_table = NULL;
201 if (BSWAP(info->large_count) > 0)
202 table->large_pri_table = (void *)TMP;
204 table->large_pri_table = NULL;
206 table->__collate_load_error = 0;
207 return (_LDP_LOADED);
210 static const int32_t *
211 substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
213 const collate_subst_t *p;
214 int n = BSWAP(table->info->subst_count[pass]);
219 if (pass >= table->info->directive_count)
222 if (!(key & COLLATE_SUBST_PRIORITY))
225 p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
226 assert(BSWAP(p->key) == key);
231 static collate_chain_t *
232 chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
235 int high = BSWAP(table->info->chain_count) - 1;
238 collate_chain_t *tab = table->chain_pri_table;
243 while (low <= high) {
244 next = (low + high) / 2;
246 compar = *key - le16toh(*p->str);
248 l = wcsnlen(p->str, COLLATE_STR_LEN);
249 compar = wcsncmp(key, p->str, l);
263 static collate_large_t *
264 largesearch(struct xlocale_collate *table, const wchar_t key)
267 int high = BSWAP(table->info->large_count) - 1;
270 collate_large_t *tab = table->large_pri_table;
275 while (low <= high) {
276 next = (low + high) / 2;
278 compar = key - BSWAP(p->val);
290 _collate_lookup(struct xlocale_collate *table, const wchar_t *t, int *len,
291 int *pri, int which, const int **state)
294 collate_large_t *match;
299 * If this is the "last" pass for the UNDEFINED, then
300 * we just return the priority itself.
302 if (which >= table->info->directive_count) {
310 * If we have remaining substitution data from a previous
311 * call, consume it first.
313 if ((sptr = *state) != NULL) {
316 if ((sptr == *state) || (sptr == NULL))
324 /* No active substitutions */
328 * Check for composites such as diphthongs that collate as a
329 * single element (aka chains or collating-elements).
331 if (((p2 = chainsearch(table, t, &l)) != NULL) &&
332 ((p = p2->pri[which]) >= 0)) {
337 } else if (*t <= UCHAR_MAX) {
340 * Character is a small (8-bit) character.
341 * We just look these up directly for speed.
343 *pri = BSWAP(table->char_pri_table[*t].pri[which]);
345 } else if ((BSWAP(table->info->large_count) > 0) &&
346 ((match = largesearch(table, *t)) != NULL)) {
349 * Character was found in the extended table.
351 *pri = BSWAP(match->pri.pri[which]);
355 * Character lacks a specific definition.
357 if (table->info->directive[which] & DIRECTIVE_UNDEFINED) {
358 /* Mask off sign bit to prevent ordering confusion. */
359 *pri = (*t & COLLATE_MAX_PRIORITY);
361 *pri = BSWAP(table->info->undef_pri[which]);
363 /* No substitutions for undefined characters! */
368 * Try substituting (expanding) the character. We are
369 * currently doing this *after* the chain compression. I
370 * think it should not matter, but this way might be slightly
373 * We do this after the priority search, as this will help us
374 * to identify a single key value. In order for this to work,
375 * its important that the priority assigned to a given element
376 * to be substituted be unique for that level. The localedef
377 * code ensures this for us.
379 if ((sptr = substsearch(table, *pri, which)) != NULL) {
380 if ((*pri = BSWAP(*sptr)) > 0) {
382 *state = BSWAP(*sptr) ? sptr : NULL;
389 * This is the meaty part of wcsxfrm & strxfrm. Note that it does
390 * NOT NULL terminate. That is left to the caller.
393 _collate_wxfrm(struct xlocale_collate *table, const wchar_t *src, wchar_t *xf,
402 const int32_t *state;
405 int ndir = table->info->directive_count;
409 for (pass = 0; pass <= ndir; pass++) {
414 /* insert level separator from the previous pass */
422 /* special pass for undefined */
424 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
426 direc = table->info->directive[pass];
431 if (direc & DIRECTIVE_BACKWARD) {
434 if ((tr = wcsdup(t)) == NULL) {
439 fp = tr + wcslen(tr) - 1;
445 t = (const wchar_t *)tr;
448 if (direc & DIRECTIVE_POSITION) {
449 while (*t || state) {
450 _collate_lookup(table, t, &len, &pri, pass, &state);
458 pri = COLLATE_MAX_PRIORITY;
468 while (*t || state) {
469 _collate_lookup(table, t, &len, &pri, pass, &state);
493 return ((size_t)(-1));
497 * In the non-POSIX case, we transform each character into a string of
498 * characters representing the character's priority. Since char is usually
499 * signed, we are limited by 7 bits per byte. To avoid zero, we need to add
500 * XFRM_OFFSET, so we can't use a full 7 bits. For simplicity, we choose 6
503 * It turns out that we sometimes have real priorities that are
504 * 31-bits wide. (But: be careful using priorities where the high
505 * order bit is set -- i.e. the priority is negative. The sort order
506 * may be surprising!)
508 * TODO: This would be a good area to optimize somewhat. It turns out
509 * that real prioririties *except for the last UNDEFINED pass* are generally
510 * very small. We need the localedef code to precalculate the max
511 * priority for us, and ideally also give us a mask, and then we could
512 * severely limit what we expand to.
515 #define XFRM_OFFSET ('0') /* make all printable characters */
517 #define XFRM_MASK ((1 << XFRM_SHIFT) - 1)
518 #define XFRM_SEP ('.') /* chosen to be less than XFRM_OFFSET */
521 xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
523 /* we use unsigned to ensure zero fill on right shift */
524 uint32_t val = BSWAP((uint32_t)table->info->pri_count[pass]);
528 *p = (pri & XFRM_MASK) + XFRM_OFFSET;
538 _collate_sxfrm(struct xlocale_collate *table, const wchar_t *src, char *xf,
547 const int32_t *state;
551 uint8_t buf[XFRM_BYTES];
552 int ndir = table->info->directive_count;
556 for (pass = 0; pass <= ndir; pass++) {
561 /* insert level separator from the previous pass */
569 /* special pass for undefined */
571 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
573 direc = table->info->directive[pass];
578 if (direc & DIRECTIVE_BACKWARD) {
581 if ((tr = wcsdup(t)) == NULL) {
586 fp = tr + wcslen(tr) - 1;
592 t = (const wchar_t *)tr;
595 if (direc & DIRECTIVE_POSITION) {
596 while (*t || state) {
598 _collate_lookup(table, t, &len, &pri, pass, &state);
606 pri = COLLATE_MAX_PRIORITY;
609 b = xfrm(table, buf, pri, pass);
623 while (*t || state) {
624 _collate_lookup(table, t, &len, &pri, pass, &state);
635 b = xfrm(table, buf, pri, pass);
656 return ((size_t)(-1));
660 * __collate_equiv_value returns the primary collation value for the given
661 * collating symbol specified by str and len. Zero or negative is returned
662 * if the collating symbol was not found. This function is used by bracket
663 * code in the TRE regex library.
666 __collate_equiv_value(locale_t locale, const wchar_t *str, size_t len)
670 if (len < 1 || len >= COLLATE_STR_LEN)
674 struct xlocale_collate *table =
675 (struct xlocale_collate*)locale->components[XLC_COLLATE];
677 if (table->__collate_load_error)
678 return ((len == 1 && *str <= UCHAR_MAX) ? *str : -1);
682 if (*str <= UCHAR_MAX)
683 e = table->char_pri_table[*str].pri[0];
684 else if (BSWAP(table->info->large_count) > 0) {
685 collate_large_t *match_large;
686 match_large = largesearch(table, *str);
688 e = match_large->pri.pri[0];
692 return (e > 0 ? e : 0);
694 if (BSWAP(table->info->chain_count) > 0) {
695 wchar_t name[COLLATE_STR_LEN];
696 collate_chain_t *match_chain;
699 wcsncpy (name, str, len);
701 match_chain = chainsearch(table, name, &clen);
703 e = match_chain->pri[0];
706 return (e < 0 ? -e : e);