2 * Copright 2010 Nexenta Systems, Inc. All rights reserved.
3 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
4 * at Electronni Visti IA, Kiev, Ukraine.
7 * Copyright (c) 2011 The FreeBSD Foundation
9 * Portions of this software were developed by David Chisnall
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Adapted to xlocale by John Marino <draco@marino.st>
36 #include "namespace.h"
44 #include <sys/types.h>
47 #include "un-namespace.h"
50 #include "setlocale.h"
53 struct xlocale_collate __xlocale_global_collate = {
54 {{0}, "C"}, 1, 0, 0, 0
57 struct xlocale_collate __xlocale_C_collate = {
58 {{0}, "C"}, 1, 0, 0, 0
61 #include "libc_private.h"
64 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table);
67 destruct_collate(void *t)
69 struct xlocale_collate *table = t;
70 if (table->map && (table->maplen > 0)) {
71 (void) munmap(table->map, table->maplen);
77 __collate_load(const char *encoding, __unused locale_t unused)
79 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
80 return &__xlocale_C_collate;
82 struct xlocale_collate *table = calloc(sizeof(struct xlocale_collate), 1);
83 table->header.header.destructor = destruct_collate;
84 // FIXME: Make sure that _LDP_CACHE is never returned. We should be doing
85 // the caching outside of this section
86 if (__collate_load_tables_l(encoding, table) != _LDP_LOADED) {
87 xlocale_release(table);
94 * Load the collation tables for the specified encoding into the global table.
97 __collate_load_tables(const char *encoding)
99 int ret = __collate_load_tables_l(encoding, &__xlocale_global_collate);
104 __collate_load_tables_l(const char *encoding, struct xlocale_collate *table)
110 collate_info_t *info;
114 /* 'encoding' must be already checked. */
115 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
116 table->__collate_load_error = 1;
120 (void) snprintf(buf, sizeof (buf), "%s/%s/LC_COLLATE",
121 _PathLocale, encoding);
123 if ((fd = _open(buf, O_RDONLY)) < 0)
125 if (_fstat(fd, &sbuf) < 0) {
129 if (sbuf.st_size < (COLLATE_STR_LEN + sizeof (info))) {
134 map = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
136 if ((TMP = map) == NULL) {
140 if (strncmp(TMP, COLLATE_VERSION, COLLATE_STR_LEN) != 0) {
141 (void) munmap(map, sbuf.st_size);
145 TMP += COLLATE_STR_LEN;
148 TMP += sizeof (*info);
150 if ((info->directive_count < 1) ||
151 (info->directive_count >= COLL_WEIGHTS_MAX) ||
152 ((chains = info->chain_count) < 0)) {
153 (void) munmap(map, sbuf.st_size);
158 i = (sizeof (collate_char_t) * (UCHAR_MAX + 1)) +
159 (sizeof (collate_chain_t) * chains) +
160 (sizeof (collate_large_t) * info->large_count);
161 for (z = 0; z < (info->directive_count); z++) {
162 i += sizeof (collate_subst_t) * info->subst_count[z];
164 if (i != (sbuf.st_size - (TMP - map))) {
165 (void) munmap(map, sbuf.st_size);
170 table->char_pri_table = (void *)TMP;
171 TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
173 for (z = 0; z < info->directive_count; z++) {
174 if (info->subst_count[z] > 0) {
175 table->subst_table[z] = (void *)TMP;
176 TMP += info->subst_count[z] * sizeof (collate_subst_t);
178 table->subst_table[z] = NULL;
183 table->chain_pri_table = (void *)TMP;
184 TMP += chains * sizeof (collate_chain_t);
186 table->chain_pri_table = NULL;
187 if (info->large_count > 0)
188 table->large_pri_table = (void *)TMP;
190 table->large_pri_table = NULL;
193 table->__collate_load_error = 0;
195 return (_LDP_LOADED);
199 * Note: for performance reasons, we have expanded bsearch here. This avoids
200 * function call overhead with each comparison.
204 substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
207 int n = table->info->subst_count[pass];
212 if (pass >= table->info->directive_count)
215 if (!(key & COLLATE_SUBST_PRIORITY))
218 p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
222 static collate_chain_t *
223 chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
229 collate_chain_t *tab;
231 if (table->info->chain_count == 0)
235 high = table->info->chain_count - 1;
236 tab = table->chain_pri_table;
238 while (low <= high) {
239 next = (low + high) / 2;
241 compar = *key - *p->str;
243 l = wcsnlen(p->str, COLLATE_STR_LEN);
244 compar = wcsncmp(key, p->str, l);
258 static collate_large_t *
259 largesearch(struct xlocale_collate *table, const wchar_t key)
262 int high = table->info->large_count - 1;
265 collate_large_t *tab = table->large_pri_table;
267 if (table->info->large_count == 0)
270 while (low <= high) {
271 next = (low + high) / 2;
273 compar = key - p->val;
285 _collate_lookup(struct xlocale_collate *table, const wchar_t *t, int *len,
286 int *pri, int which, const int **state)
289 collate_large_t *match;
294 * If this is the "last" pass for the UNDEFINED, then
295 * we just return the priority itself.
297 if (which >= table->info->directive_count) {
305 * If we have remaining substitution data from a previous
306 * call, consume it first.
308 if ((sptr = *state) != NULL) {
311 *state = *sptr ? sptr : NULL;
316 /* No active substitutions */
320 * Check for composites such as dipthongs that collate as a
321 * single element (aka chains or collating-elements).
323 if (((p2 = chainsearch(table, t, &l)) != NULL) &&
324 ((p = p2->pri[which]) >= 0)) {
329 } else if (*t <= UCHAR_MAX) {
332 * Character is a small (8-bit) character.
333 * We just look these up directly for speed.
335 *pri = table->char_pri_table[*t].pri[which];
337 } else if ((table->info->large_count > 0) &&
338 ((match = largesearch(table, *t)) != NULL)) {
341 * Character was found in the extended table.
343 *pri = match->pri.pri[which];
347 * Character lacks a specific definition.
349 if (table->info->directive[which] & DIRECTIVE_UNDEFINED) {
350 /* Mask off sign bit to prevent ordering confusion. */
351 *pri = (*t & COLLATE_MAX_PRIORITY);
353 *pri = table->info->undef_pri[which];
355 /* No substitutions for undefined characters! */
360 * Try substituting (expanding) the character. We are
361 * currently doing this *after* the chain compression. I
362 * think it should not matter, but this way might be slightly
365 * We do this after the priority search, as this will help us
366 * to identify a single key value. In order for this to work,
367 * its important that the priority assigned to a given element
368 * to be substituted be unique for that level. The localedef
369 * code ensures this for us.
371 if ((sptr = substsearch(table, *pri, which)) != NULL) {
372 if ((*pri = *sptr) != 0) {
374 *state = *sptr ? sptr : NULL;
381 * This is the meaty part of wcsxfrm & strxfrm. Note that it does
382 * NOT NULL terminate. That is left to the caller.
385 _collate_wxfrm(struct xlocale_collate *table, const wchar_t *src, wchar_t *xf,
394 const int32_t *state;
398 for (pass = 0; pass <= table->info->directive_count; pass++) {
403 /* insert level separator from the previous pass */
411 /* special pass for undefined */
412 if (pass == table->info->directive_count) {
413 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
415 direc = table->info->directive[pass];
420 if (direc & DIRECTIVE_BACKWARD) {
424 if ((tr = wcsdup(t)) == NULL) {
429 fp = tr + wcslen(tr) - 1;
435 t = (const wchar_t *)tr;
438 if (direc & DIRECTIVE_POSITION) {
439 while (*t || state) {
440 _collate_lookup(table, t, &len, &pri, pass, &state);
447 pri = COLLATE_MAX_PRIORITY;
457 while (*t || state) {
458 _collate_lookup(table, t, &len, &pri, pass, &state);
483 return ((size_t)(-1));
487 * In the non-POSIX case, we transform each character into a string of
488 * characters representing the character's priority. Since char is usually
489 * signed, we are limited by 7 bits per byte. To avoid zero, we need to add
490 * XFRM_OFFSET, so we can't use a full 7 bits. For simplicity, we choose 6
493 * It turns out that we sometimes have real priorities that are
494 * 31-bits wide. (But: be careful using priorities where the high
495 * order bit is set -- i.e. the priority is negative. The sort order
496 * may be surprising!)
498 * TODO: This would be a good area to optimize somewhat. It turns out
499 * that real prioririties *except for the last UNDEFINED pass* are generally
500 * very small. We need the localedef code to precalculate the max
501 * priority for us, and ideally also give us a mask, and then we could
502 * severely limit what we expand to.
505 #define XFRM_OFFSET ('0') /* make all printable characters */
507 #define XFRM_MASK ((1 << XFRM_SHIFT) - 1)
508 #define XFRM_SEP ('.') /* chosen to be less than XFRM_OFFSET */
511 xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
513 /* we use unsigned to ensure zero fill on right shift */
514 uint32_t val = (uint32_t)table->info->pri_count[pass];
518 *p = (pri & XFRM_MASK) + XFRM_OFFSET;
528 _collate_sxfrm(struct xlocale_collate *table, const wchar_t *src, char *xf,
537 const int32_t *state;
541 uint8_t buf[XFRM_BYTES];
543 for (pass = 0; pass <= table->info->directive_count; pass++) {
548 /* insert level separator from the previous pass */
556 /* special pass for undefined */
557 if (pass == table->info->directive_count) {
558 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
560 direc = table->info->directive[pass];
565 if (direc & DIRECTIVE_BACKWARD) {
569 if ((tr = wcsdup(t)) == NULL) {
574 fp = tr + wcslen(tr) - 1;
580 t = (const wchar_t *)tr;
583 if (direc & DIRECTIVE_POSITION) {
584 while (*t || state) {
586 _collate_lookup(table, t, &len, &pri, pass, &state);
593 pri = COLLATE_MAX_PRIORITY;
596 b = xfrm(table, buf, pri, pass);
610 while (*t || state) {
611 _collate_lookup(table, t, &len, &pri, pass, &state);
621 b = xfrm(table, buf, pri, pass);
644 return ((size_t)(-1));
648 * __collate_equiv_value returns the primary collation value for the given
649 * collating symbol specified by str and len. Zero or negative is returned
650 * if the collating symbol was not found. This function is used by bracket
651 * code in the TRE regex library.
654 __collate_equiv_value(locale_t locale, const wchar_t *str, size_t len)
658 if (len < 1 || len >= COLLATE_STR_LEN)
662 struct xlocale_collate *table =
663 (struct xlocale_collate*)locale->components[XLC_COLLATE];
665 if (table->__collate_load_error)
666 return ((len == 1 && *str <= UCHAR_MAX) ? *str : -1);
670 if (*str <= UCHAR_MAX)
671 e = table->char_pri_table[*str].pri[0];
672 else if (table->info->large_count > 0) {
673 collate_large_t *match_large;
674 match_large = largesearch(table, *str);
676 e = match_large->pri.pri[0];
680 return (e > 0 ? e : 0);
682 if (table->info->chain_count > 0) {
683 wchar_t name[COLLATE_STR_LEN];
684 collate_chain_t *match_chain;
687 wcsncpy (name, str, len);
689 match_chain = chainsearch(table, name, &clen);
691 e = match_chain->pri[0];
694 return (e < 0 ? -e : e);