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) == NULL) {
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);
185 table->char_pri_table = (void *)TMP;
186 TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
188 for (z = 0; z < info->directive_count; z++) {
189 if (info->subst_count[z] > 0) {
190 table->subst_table[z] = (void *)TMP;
191 TMP += info->subst_count[z] * sizeof (collate_subst_t);
193 table->subst_table[z] = NULL;
198 table->chain_pri_table = (void *)TMP;
199 TMP += chains * sizeof (collate_chain_t);
201 table->chain_pri_table = NULL;
202 if (info->large_count > 0)
203 table->large_pri_table = (void *)TMP;
205 table->large_pri_table = NULL;
207 table->__collate_load_error = 0;
208 return (_LDP_LOADED);
211 static const int32_t *
212 substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
214 const collate_subst_t *p;
215 int n = table->info->subst_count[pass];
220 if (pass >= table->info->directive_count)
223 if (!(key & COLLATE_SUBST_PRIORITY))
226 p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
227 assert(p->key == key);
232 static collate_chain_t *
233 chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
236 int high = table->info->chain_count - 1;
239 collate_chain_t *tab = table->chain_pri_table;
244 while (low <= high) {
245 next = (low + high) / 2;
247 compar = *key - *p->str;
249 l = wcsnlen(p->str, COLLATE_STR_LEN);
250 compar = wcsncmp(key, p->str, l);
264 static collate_large_t *
265 largesearch(struct xlocale_collate *table, const wchar_t key)
268 int high = table->info->large_count - 1;
271 collate_large_t *tab = table->large_pri_table;
276 while (low <= high) {
277 next = (low + high) / 2;
279 compar = key - p->val;
291 _collate_lookup(struct xlocale_collate *table, const wchar_t *t, int *len,
292 int *pri, int which, const int **state)
295 collate_large_t *match;
300 * If this is the "last" pass for the UNDEFINED, then
301 * we just return the priority itself.
303 if (which >= table->info->directive_count) {
311 * If we have remaining substitution data from a previous
312 * call, consume it first.
314 if ((sptr = *state) != NULL) {
317 if ((sptr == *state) || (sptr == NULL))
325 /* No active substitutions */
329 * Check for composites such as diphthongs that collate as a
330 * single element (aka chains or collating-elements).
332 if (((p2 = chainsearch(table, t, &l)) != NULL) &&
333 ((p = p2->pri[which]) >= 0)) {
338 } else if (*t <= UCHAR_MAX) {
341 * Character is a small (8-bit) character.
342 * We just look these up directly for speed.
344 *pri = table->char_pri_table[*t].pri[which];
346 } else if ((table->info->large_count > 0) &&
347 ((match = largesearch(table, *t)) != NULL)) {
350 * Character was found in the extended table.
352 *pri = match->pri.pri[which];
356 * Character lacks a specific definition.
358 if (table->info->directive[which] & DIRECTIVE_UNDEFINED) {
359 /* Mask off sign bit to prevent ordering confusion. */
360 *pri = (*t & COLLATE_MAX_PRIORITY);
362 *pri = table->info->undef_pri[which];
364 /* No substitutions for undefined characters! */
369 * Try substituting (expanding) the character. We are
370 * currently doing this *after* the chain compression. I
371 * think it should not matter, but this way might be slightly
374 * We do this after the priority search, as this will help us
375 * to identify a single key value. In order for this to work,
376 * its important that the priority assigned to a given element
377 * to be substituted be unique for that level. The localedef
378 * code ensures this for us.
380 if ((sptr = substsearch(table, *pri, which)) != NULL) {
381 if ((*pri = *sptr) > 0) {
383 *state = *sptr ? sptr : NULL;
390 * This is the meaty part of wcsxfrm & strxfrm. Note that it does
391 * NOT NULL terminate. That is left to the caller.
394 _collate_wxfrm(struct xlocale_collate *table, const wchar_t *src, wchar_t *xf,
403 const int32_t *state;
406 int ndir = table->info->directive_count;
410 for (pass = 0; pass <= ndir; pass++) {
415 /* insert level separator from the previous pass */
423 /* special pass for undefined */
425 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
427 direc = table->info->directive[pass];
432 if (direc & DIRECTIVE_BACKWARD) {
435 if ((tr = wcsdup(t)) == NULL) {
440 fp = tr + wcslen(tr) - 1;
446 t = (const wchar_t *)tr;
449 if (direc & DIRECTIVE_POSITION) {
450 while (*t || state) {
451 _collate_lookup(table, t, &len, &pri, pass, &state);
459 pri = COLLATE_MAX_PRIORITY;
469 while (*t || state) {
470 _collate_lookup(table, t, &len, &pri, pass, &state);
494 return ((size_t)(-1));
498 * In the non-POSIX case, we transform each character into a string of
499 * characters representing the character's priority. Since char is usually
500 * signed, we are limited by 7 bits per byte. To avoid zero, we need to add
501 * XFRM_OFFSET, so we can't use a full 7 bits. For simplicity, we choose 6
504 * It turns out that we sometimes have real priorities that are
505 * 31-bits wide. (But: be careful using priorities where the high
506 * order bit is set -- i.e. the priority is negative. The sort order
507 * may be surprising!)
509 * TODO: This would be a good area to optimize somewhat. It turns out
510 * that real prioririties *except for the last UNDEFINED pass* are generally
511 * very small. We need the localedef code to precalculate the max
512 * priority for us, and ideally also give us a mask, and then we could
513 * severely limit what we expand to.
516 #define XFRM_OFFSET ('0') /* make all printable characters */
518 #define XFRM_MASK ((1 << XFRM_SHIFT) - 1)
519 #define XFRM_SEP ('.') /* chosen to be less than XFRM_OFFSET */
522 xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
524 /* we use unsigned to ensure zero fill on right shift */
525 uint32_t val = (uint32_t)table->info->pri_count[pass];
529 *p = (pri & XFRM_MASK) + XFRM_OFFSET;
539 _collate_sxfrm(struct xlocale_collate *table, const wchar_t *src, char *xf,
548 const int32_t *state;
552 uint8_t buf[XFRM_BYTES];
553 int ndir = table->info->directive_count;
557 for (pass = 0; pass <= ndir; pass++) {
562 /* insert level separator from the previous pass */
570 /* special pass for undefined */
572 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
574 direc = table->info->directive[pass];
579 if (direc & DIRECTIVE_BACKWARD) {
582 if ((tr = wcsdup(t)) == NULL) {
587 fp = tr + wcslen(tr) - 1;
593 t = (const wchar_t *)tr;
596 if (direc & DIRECTIVE_POSITION) {
597 while (*t || state) {
599 _collate_lookup(table, t, &len, &pri, pass, &state);
607 pri = COLLATE_MAX_PRIORITY;
610 b = xfrm(table, buf, pri, pass);
624 while (*t || state) {
625 _collate_lookup(table, t, &len, &pri, pass, &state);
636 b = xfrm(table, buf, pri, pass);
657 return ((size_t)(-1));
661 * __collate_equiv_value returns the primary collation value for the given
662 * collating symbol specified by str and len. Zero or negative is returned
663 * if the collating symbol was not found. This function is used by bracket
664 * code in the TRE regex library.
667 __collate_equiv_value(locale_t locale, const wchar_t *str, size_t len)
671 if (len < 1 || len >= COLLATE_STR_LEN)
675 struct xlocale_collate *table =
676 (struct xlocale_collate*)locale->components[XLC_COLLATE];
678 if (table->__collate_load_error)
679 return ((len == 1 && *str <= UCHAR_MAX) ? *str : -1);
683 if (*str <= UCHAR_MAX)
684 e = table->char_pri_table[*str].pri[0];
685 else if (table->info->large_count > 0) {
686 collate_large_t *match_large;
687 match_large = largesearch(table, *str);
689 e = match_large->pri.pri[0];
693 return (e > 0 ? e : 0);
695 if (table->info->chain_count > 0) {
696 wchar_t name[COLLATE_STR_LEN];
697 collate_chain_t *match_chain;
700 wcsncpy (name, str, len);
702 match_chain = chainsearch(table, name, &clen);
704 e = match_chain->pri[0];
707 return (e < 0 ? -e : e);