1995-02-16 04:24:39 +00:00
|
|
|
/*-
|
2017-11-25 17:12:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2015-08-08 18:22:14 +00:00
|
|
|
* Copyright 2014 Garrett D'Amore <garrett@damore.org>
|
2015-10-22 14:29:19 +00:00
|
|
|
* Copyright 2010 Nexenta Systems, Inc. All rights reserved.
|
1995-02-16 04:24:39 +00:00
|
|
|
* Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
|
|
|
|
* at Electronni Visti IA, Kiev, Ukraine.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2011-11-20 14:45:42 +00:00
|
|
|
* Copyright (c) 2011 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
* Portions of this software were developed by David Chisnall
|
|
|
|
* under sponsorship from the FreeBSD Foundation.
|
|
|
|
*
|
1995-02-16 04:24:39 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2015-08-07 23:41:26 +00:00
|
|
|
*
|
|
|
|
* Adapted to xlocale by John Marino <draco@marino.st>
|
1995-02-16 04:24:39 +00:00
|
|
|
*/
|
|
|
|
|
2002-03-22 21:53:29 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-01-24 13:01:12 +00:00
|
|
|
#include "namespace.h"
|
2015-10-14 12:46:05 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2015-10-22 14:29:19 +00:00
|
|
|
#include <assert.h>
|
1995-02-16 04:24:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-08-07 23:41:26 +00:00
|
|
|
#include <wchar.h>
|
1995-02-17 16:36:12 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2015-08-07 23:41:26 +00:00
|
|
|
#include <fcntl.h>
|
2001-01-24 13:01:12 +00:00
|
|
|
#include "un-namespace.h"
|
|
|
|
|
1995-02-16 04:24:39 +00:00
|
|
|
#include "collate.h"
|
1997-02-06 09:11:06 +00:00
|
|
|
#include "setlocale.h"
|
2002-08-08 05:51:54 +00:00
|
|
|
#include "ldpart.h"
|
2015-08-09 00:21:59 +00:00
|
|
|
#include "libc_private.h"
|
1995-02-16 04:24:39 +00:00
|
|
|
|
2011-11-20 14:45:42 +00:00
|
|
|
struct xlocale_collate __xlocale_global_collate = {
|
2015-08-07 23:41:26 +00:00
|
|
|
{{0}, "C"}, 1, 0, 0, 0
|
2011-11-20 14:45:42 +00:00
|
|
|
};
|
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
struct xlocale_collate __xlocale_C_collate = {
|
|
|
|
{{0}, "C"}, 1, 0, 0, 0
|
2011-11-20 14:45:42 +00:00
|
|
|
};
|
1995-02-16 04:24:39 +00:00
|
|
|
|
2015-08-09 10:24:24 +00:00
|
|
|
static int
|
2011-11-20 14:45:42 +00:00
|
|
|
__collate_load_tables_l(const char *encoding, struct xlocale_collate *table);
|
|
|
|
|
|
|
|
static void
|
|
|
|
destruct_collate(void *t)
|
|
|
|
{
|
|
|
|
struct xlocale_collate *table = t;
|
2015-08-07 23:41:26 +00:00
|
|
|
if (table->map && (table->maplen > 0)) {
|
|
|
|
(void) munmap(table->map, table->maplen);
|
2011-11-20 14:45:42 +00:00
|
|
|
}
|
|
|
|
free(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2015-08-07 23:41:26 +00:00
|
|
|
__collate_load(const char *encoding, __unused locale_t unused)
|
2011-11-20 14:45:42 +00:00
|
|
|
{
|
2018-11-04 22:13:22 +00:00
|
|
|
if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0 ||
|
|
|
|
strncmp(encoding, "C.", 2) == 0) {
|
2011-11-20 14:45:42 +00:00
|
|
|
return &__xlocale_C_collate;
|
|
|
|
}
|
|
|
|
struct xlocale_collate *table = calloc(sizeof(struct xlocale_collate), 1);
|
|
|
|
table->header.header.destructor = destruct_collate;
|
|
|
|
// FIXME: Make sure that _LDP_CACHE is never returned. We should be doing
|
|
|
|
// the caching outside of this section
|
|
|
|
if (__collate_load_tables_l(encoding, table) != _LDP_LOADED) {
|
|
|
|
xlocale_release(table);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the collation tables for the specified encoding into the global table.
|
|
|
|
*/
|
1995-02-17 16:36:12 +00:00
|
|
|
int
|
2002-08-08 05:51:54 +00:00
|
|
|
__collate_load_tables(const char *encoding)
|
2011-11-20 14:45:42 +00:00
|
|
|
{
|
2015-08-09 11:47:01 +00:00
|
|
|
|
|
|
|
return (__collate_load_tables_l(encoding, &__xlocale_global_collate));
|
2011-11-20 14:45:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 20:49:26 +00:00
|
|
|
static int
|
2011-11-20 14:45:42 +00:00
|
|
|
__collate_load_tables_l(const char *encoding, struct xlocale_collate *table)
|
1995-02-16 04:24:39 +00:00
|
|
|
{
|
2015-08-07 23:41:26 +00:00
|
|
|
int i, chains, z;
|
2015-08-09 11:50:50 +00:00
|
|
|
char *buf;
|
2015-08-07 23:41:26 +00:00
|
|
|
char *TMP;
|
|
|
|
char *map;
|
|
|
|
collate_info_t *info;
|
|
|
|
struct stat sbuf;
|
|
|
|
int fd;
|
1995-02-16 04:24:39 +00:00
|
|
|
|
2015-10-22 14:29:19 +00:00
|
|
|
table->__collate_load_error = 1;
|
|
|
|
|
2002-08-08 05:51:54 +00:00
|
|
|
/* 'encoding' must be already checked. */
|
2018-11-04 22:13:22 +00:00
|
|
|
if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0 ||
|
|
|
|
strncmp(encoding, "C.", 2) == 0) {
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_CACHE);
|
1995-10-23 20:08:24 +00:00
|
|
|
}
|
2002-08-08 05:51:54 +00:00
|
|
|
|
2017-09-29 16:30:50 +00:00
|
|
|
if (asprintf(&buf, "%s/%s/LC_COLLATE", _PathLocale, encoding) == -1)
|
2015-08-09 11:50:50 +00:00
|
|
|
return (_LDP_ERROR);
|
2002-08-08 05:51:54 +00:00
|
|
|
|
2015-08-09 12:13:30 +00:00
|
|
|
if ((fd = _open(buf, O_RDONLY)) < 0) {
|
|
|
|
free(buf);
|
2002-08-30 20:26:02 +00:00
|
|
|
return (_LDP_ERROR);
|
2015-08-09 12:13:30 +00:00
|
|
|
}
|
2015-08-09 11:50:50 +00:00
|
|
|
free(buf);
|
2015-08-07 23:41:26 +00:00
|
|
|
if (_fstat(fd, &sbuf) < 0) {
|
|
|
|
(void) _close(fd);
|
2002-08-30 20:26:02 +00:00
|
|
|
return (_LDP_ERROR);
|
|
|
|
}
|
Add collation version support to querylocale(3).
Provide a way to ask for an opaque version string for a locale_t, so
that potential changes in sort order can be detected. Similar to
ICU's ucol_getVersion() and Windows' GetNLSVersionEx(), this API is
intended to allow databases to detect when text order-based indexes
might need to be rebuilt.
The CLDR version is extracted from CLDR source data by the Makefile
under tools/tools/locale, written into the machine-generated Makefile
under shared/colldef, passed to localedef -V, and then written into
LC_COLLATE file headers. The initial version is 34.0.
tools/tools/locale was recently updated to pull down 35.0, but the
output hasn't been committed under share/colldef yet, so that will
provide the first observable change when it happens. Other versioning
schemes are possible in future, because the format is unspecified.
Reviewed by: bapt, 0mp, kib, yuripv (albeit a long time ago)
Differential Revision: https://reviews.freebsd.org/D17166
2020-11-08 02:50:34 +00:00
|
|
|
if (sbuf.st_size < (COLLATE_FMT_VERSION_LEN +
|
|
|
|
XLOCALE_DEF_VERSION_LEN +
|
|
|
|
sizeof (info))) {
|
2015-08-07 23:41:26 +00:00
|
|
|
(void) _close(fd);
|
|
|
|
errno = EINVAL;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_ERROR);
|
1995-10-23 20:08:24 +00:00
|
|
|
}
|
2015-08-07 23:41:26 +00:00
|
|
|
map = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
(void) _close(fd);
|
2020-01-09 20:49:26 +00:00
|
|
|
if ((TMP = map) == MAP_FAILED) {
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_ERROR);
|
1996-10-15 21:53:23 +00:00
|
|
|
}
|
2015-08-07 23:41:26 +00:00
|
|
|
|
Add collation version support to querylocale(3).
Provide a way to ask for an opaque version string for a locale_t, so
that potential changes in sort order can be detected. Similar to
ICU's ucol_getVersion() and Windows' GetNLSVersionEx(), this API is
intended to allow databases to detect when text order-based indexes
might need to be rebuilt.
The CLDR version is extracted from CLDR source data by the Makefile
under tools/tools/locale, written into the machine-generated Makefile
under shared/colldef, passed to localedef -V, and then written into
LC_COLLATE file headers. The initial version is 34.0.
tools/tools/locale was recently updated to pull down 35.0, but the
output hasn't been committed under share/colldef yet, so that will
provide the first observable change when it happens. Other versioning
schemes are possible in future, because the format is unspecified.
Reviewed by: bapt, 0mp, kib, yuripv (albeit a long time ago)
Differential Revision: https://reviews.freebsd.org/D17166
2020-11-08 02:50:34 +00:00
|
|
|
if (strncmp(TMP, COLLATE_FMT_VERSION, COLLATE_FMT_VERSION_LEN) != 0) {
|
2015-08-07 23:41:26 +00:00
|
|
|
(void) munmap(map, sbuf.st_size);
|
|
|
|
errno = EINVAL;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_ERROR);
|
|
|
|
}
|
Add collation version support to querylocale(3).
Provide a way to ask for an opaque version string for a locale_t, so
that potential changes in sort order can be detected. Similar to
ICU's ucol_getVersion() and Windows' GetNLSVersionEx(), this API is
intended to allow databases to detect when text order-based indexes
might need to be rebuilt.
The CLDR version is extracted from CLDR source data by the Makefile
under tools/tools/locale, written into the machine-generated Makefile
under shared/colldef, passed to localedef -V, and then written into
LC_COLLATE file headers. The initial version is 34.0.
tools/tools/locale was recently updated to pull down 35.0, but the
output hasn't been committed under share/colldef yet, so that will
provide the first observable change when it happens. Other versioning
schemes are possible in future, because the format is unspecified.
Reviewed by: bapt, 0mp, kib, yuripv (albeit a long time ago)
Differential Revision: https://reviews.freebsd.org/D17166
2020-11-08 02:50:34 +00:00
|
|
|
TMP += COLLATE_FMT_VERSION_LEN;
|
|
|
|
strlcat(table->header.version, TMP, sizeof (table->header.version));
|
|
|
|
TMP += XLOCALE_DEF_VERSION_LEN;
|
2002-08-08 05:51:54 +00:00
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
info = (void *)TMP;
|
|
|
|
TMP += sizeof (*info);
|
2002-08-08 05:51:54 +00:00
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
if ((info->directive_count < 1) ||
|
|
|
|
(info->directive_count >= COLL_WEIGHTS_MAX) ||
|
2018-10-20 20:51:05 +00:00
|
|
|
((chains = info->chain_count) < 0)) {
|
2015-08-07 23:41:26 +00:00
|
|
|
(void) munmap(map, sbuf.st_size);
|
|
|
|
errno = EINVAL;
|
|
|
|
return (_LDP_ERROR);
|
2005-02-27 20:31:13 +00:00
|
|
|
}
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
i = (sizeof (collate_char_t) * (UCHAR_MAX + 1)) +
|
|
|
|
(sizeof (collate_chain_t) * chains) +
|
2018-10-20 20:51:05 +00:00
|
|
|
(sizeof (collate_large_t) * info->large_count);
|
2015-10-22 14:29:19 +00:00
|
|
|
for (z = 0; z < info->directive_count; z++) {
|
2018-10-20 20:51:05 +00:00
|
|
|
i += sizeof (collate_subst_t) * info->subst_count[z];
|
2015-08-07 23:41:26 +00:00
|
|
|
}
|
|
|
|
if (i != (sbuf.st_size - (TMP - map))) {
|
|
|
|
(void) munmap(map, sbuf.st_size);
|
|
|
|
errno = EINVAL;
|
|
|
|
return (_LDP_ERROR);
|
2005-02-27 20:31:13 +00:00
|
|
|
}
|
2015-08-07 23:41:26 +00:00
|
|
|
|
2020-01-09 20:49:26 +00:00
|
|
|
if (table->map && (table->maplen > 0)) {
|
|
|
|
(void) munmap(table->map, table->maplen);
|
|
|
|
}
|
|
|
|
table->map = map;
|
|
|
|
table->maplen = sbuf.st_size;
|
2015-10-22 14:29:19 +00:00
|
|
|
table->info = info;
|
2015-08-07 23:41:26 +00:00
|
|
|
table->char_pri_table = (void *)TMP;
|
|
|
|
TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
|
|
|
|
|
|
|
|
for (z = 0; z < info->directive_count; z++) {
|
2018-10-20 20:51:05 +00:00
|
|
|
if (info->subst_count[z] > 0) {
|
2015-08-07 23:41:26 +00:00
|
|
|
table->subst_table[z] = (void *)TMP;
|
2018-10-20 20:51:05 +00:00
|
|
|
TMP += info->subst_count[z] * sizeof (collate_subst_t);
|
2015-08-07 23:41:26 +00:00
|
|
|
} else {
|
|
|
|
table->subst_table[z] = NULL;
|
1999-09-12 21:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
if (chains > 0) {
|
|
|
|
table->chain_pri_table = (void *)TMP;
|
|
|
|
TMP += chains * sizeof (collate_chain_t);
|
|
|
|
} else
|
|
|
|
table->chain_pri_table = NULL;
|
2018-10-20 20:51:05 +00:00
|
|
|
if (info->large_count > 0)
|
2015-08-07 23:41:26 +00:00
|
|
|
table->large_pri_table = (void *)TMP;
|
|
|
|
else
|
|
|
|
table->large_pri_table = NULL;
|
|
|
|
|
2012-07-06 20:16:22 +00:00
|
|
|
table->__collate_load_error = 0;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (_LDP_LOADED);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 14:29:19 +00:00
|
|
|
static const int32_t *
|
2015-08-07 23:41:26 +00:00
|
|
|
substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
|
|
|
|
{
|
2015-10-22 14:29:19 +00:00
|
|
|
const collate_subst_t *p;
|
2018-10-20 20:51:05 +00:00
|
|
|
int n = table->info->subst_count[pass];
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (pass >= table->info->directive_count)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (!(key & COLLATE_SUBST_PRIORITY))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
|
2018-10-20 20:51:05 +00:00
|
|
|
assert(p->key == key);
|
2016-11-01 13:54:44 +00:00
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
return (p->pri);
|
|
|
|
}
|
|
|
|
|
|
|
|
static collate_chain_t *
|
|
|
|
chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
|
1995-02-16 04:24:39 +00:00
|
|
|
{
|
2015-10-23 23:24:03 +00:00
|
|
|
int low = 0;
|
2018-10-20 20:51:05 +00:00
|
|
|
int high = table->info->chain_count - 1;
|
2015-08-07 23:41:26 +00:00
|
|
|
int next, compar, l;
|
|
|
|
collate_chain_t *p;
|
2015-10-23 23:24:03 +00:00
|
|
|
collate_chain_t *tab = table->chain_pri_table;
|
2015-08-07 23:41:26 +00:00
|
|
|
|
2015-10-23 23:24:03 +00:00
|
|
|
if (high < 0)
|
2015-08-07 23:41:26 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
while (low <= high) {
|
|
|
|
next = (low + high) / 2;
|
|
|
|
p = tab + next;
|
2018-10-20 20:51:05 +00:00
|
|
|
compar = *key - *p->str;
|
2015-08-07 23:41:26 +00:00
|
|
|
if (compar == 0) {
|
|
|
|
l = wcsnlen(p->str, COLLATE_STR_LEN);
|
|
|
|
compar = wcsncmp(key, p->str, l);
|
|
|
|
if (compar == 0) {
|
|
|
|
*len = l;
|
|
|
|
return (p);
|
|
|
|
}
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
2015-08-07 23:41:26 +00:00
|
|
|
if (compar > 0)
|
|
|
|
low = next + 1;
|
|
|
|
else
|
|
|
|
high = next - 1;
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
2015-08-07 23:41:26 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static collate_large_t *
|
|
|
|
largesearch(struct xlocale_collate *table, const wchar_t key)
|
|
|
|
{
|
|
|
|
int low = 0;
|
2018-10-20 20:51:05 +00:00
|
|
|
int high = table->info->large_count - 1;
|
2015-08-07 23:41:26 +00:00
|
|
|
int next, compar;
|
|
|
|
collate_large_t *p;
|
|
|
|
collate_large_t *tab = table->large_pri_table;
|
|
|
|
|
2015-10-23 23:24:03 +00:00
|
|
|
if (high < 0)
|
2015-08-07 23:41:26 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
while (low <= high) {
|
|
|
|
next = (low + high) / 2;
|
|
|
|
p = tab + next;
|
2018-10-20 20:51:05 +00:00
|
|
|
compar = key - p->val;
|
2015-08-07 23:41:26 +00:00
|
|
|
if (compar == 0)
|
|
|
|
return (p);
|
|
|
|
if (compar > 0)
|
|
|
|
low = next + 1;
|
|
|
|
else
|
|
|
|
high = next - 1;
|
|
|
|
}
|
|
|
|
return (NULL);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-07 23:41:26 +00:00
|
|
|
_collate_lookup(struct xlocale_collate *table, const wchar_t *t, int *len,
|
|
|
|
int *pri, int which, const int **state)
|
1995-02-16 04:24:39 +00:00
|
|
|
{
|
2015-08-07 23:41:26 +00:00
|
|
|
collate_chain_t *p2;
|
|
|
|
collate_large_t *match;
|
|
|
|
int p, l;
|
|
|
|
const int *sptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is the "last" pass for the UNDEFINED, then
|
|
|
|
* we just return the priority itself.
|
|
|
|
*/
|
|
|
|
if (which >= table->info->directive_count) {
|
|
|
|
*pri = *t;
|
|
|
|
*len = 1;
|
|
|
|
*state = NULL;
|
|
|
|
return;
|
|
|
|
}
|
1995-02-16 04:24:39 +00:00
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
/*
|
|
|
|
* If we have remaining substitution data from a previous
|
|
|
|
* call, consume it first.
|
|
|
|
*/
|
|
|
|
if ((sptr = *state) != NULL) {
|
|
|
|
*pri = *sptr;
|
|
|
|
sptr++;
|
2016-07-15 23:03:20 +00:00
|
|
|
if ((sptr == *state) || (sptr == NULL))
|
2015-10-23 23:24:03 +00:00
|
|
|
*state = NULL;
|
|
|
|
else
|
|
|
|
*state = sptr;
|
2015-08-07 23:41:26 +00:00
|
|
|
*len = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No active substitutions */
|
1995-02-16 04:24:39 +00:00
|
|
|
*len = 1;
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
/*
|
2016-04-30 01:24:24 +00:00
|
|
|
* Check for composites such as diphthongs that collate as a
|
2015-08-07 23:41:26 +00:00
|
|
|
* single element (aka chains or collating-elements).
|
|
|
|
*/
|
|
|
|
if (((p2 = chainsearch(table, t, &l)) != NULL) &&
|
|
|
|
((p = p2->pri[which]) >= 0)) {
|
|
|
|
|
|
|
|
*len = l;
|
|
|
|
*pri = p;
|
|
|
|
|
|
|
|
} else if (*t <= UCHAR_MAX) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Character is a small (8-bit) character.
|
|
|
|
* We just look these up directly for speed.
|
|
|
|
*/
|
2018-10-20 20:51:05 +00:00
|
|
|
*pri = table->char_pri_table[*t].pri[which];
|
2015-08-07 23:41:26 +00:00
|
|
|
|
2018-10-20 20:51:05 +00:00
|
|
|
} else if ((table->info->large_count > 0) &&
|
2015-08-07 23:41:26 +00:00
|
|
|
((match = largesearch(table, *t)) != NULL)) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Character was found in the extended table.
|
|
|
|
*/
|
2018-10-20 20:51:05 +00:00
|
|
|
*pri = match->pri.pri[which];
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Character lacks a specific definition.
|
|
|
|
*/
|
|
|
|
if (table->info->directive[which] & DIRECTIVE_UNDEFINED) {
|
|
|
|
/* Mask off sign bit to prevent ordering confusion. */
|
|
|
|
*pri = (*t & COLLATE_MAX_PRIORITY);
|
|
|
|
} else {
|
2018-10-20 20:51:05 +00:00
|
|
|
*pri = table->info->undef_pri[which];
|
2015-08-07 23:41:26 +00:00
|
|
|
}
|
|
|
|
/* No substitutions for undefined characters! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try substituting (expanding) the character. We are
|
|
|
|
* currently doing this *after* the chain compression. I
|
|
|
|
* think it should not matter, but this way might be slightly
|
|
|
|
* faster.
|
|
|
|
*
|
|
|
|
* We do this after the priority search, as this will help us
|
|
|
|
* to identify a single key value. In order for this to work,
|
|
|
|
* its important that the priority assigned to a given element
|
|
|
|
* to be substituted be unique for that level. The localedef
|
|
|
|
* code ensures this for us.
|
|
|
|
*/
|
|
|
|
if ((sptr = substsearch(table, *pri, which)) != NULL) {
|
2018-10-20 20:51:05 +00:00
|
|
|
if ((*pri = *sptr) > 0) {
|
2015-08-07 23:41:26 +00:00
|
|
|
sptr++;
|
2018-10-20 20:51:05 +00:00
|
|
|
*state = *sptr ? sptr : NULL;
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-07 23:41:26 +00:00
|
|
|
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
/*
|
|
|
|
* This is the meaty part of wcsxfrm & strxfrm. Note that it does
|
|
|
|
* NOT NULL terminate. That is left to the caller.
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
_collate_wxfrm(struct xlocale_collate *table, const wchar_t *src, wchar_t *xf,
|
|
|
|
size_t room)
|
1995-02-16 04:24:39 +00:00
|
|
|
{
|
2015-08-07 23:41:26 +00:00
|
|
|
int pri;
|
|
|
|
int len;
|
|
|
|
const wchar_t *t;
|
|
|
|
wchar_t *tr = NULL;
|
|
|
|
int direc;
|
|
|
|
int pass;
|
|
|
|
const int32_t *state;
|
|
|
|
size_t want = 0;
|
|
|
|
size_t need = 0;
|
2015-10-22 14:29:19 +00:00
|
|
|
int ndir = table->info->directive_count;
|
2015-08-07 23:41:26 +00:00
|
|
|
|
2015-10-22 14:29:19 +00:00
|
|
|
assert(src);
|
|
|
|
|
|
|
|
for (pass = 0; pass <= ndir; pass++) {
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
state = NULL;
|
|
|
|
|
|
|
|
if (pass != 0) {
|
|
|
|
/* insert level separator from the previous pass */
|
|
|
|
if (room) {
|
|
|
|
*xf++ = 1;
|
|
|
|
room--;
|
|
|
|
}
|
|
|
|
want++;
|
|
|
|
}
|
1995-02-16 04:24:39 +00:00
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
/* special pass for undefined */
|
2015-10-22 14:29:19 +00:00
|
|
|
if (pass == ndir) {
|
2015-08-07 23:41:26 +00:00
|
|
|
direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
|
|
|
|
} else {
|
|
|
|
direc = table->info->directive[pass];
|
|
|
|
}
|
|
|
|
|
|
|
|
t = src;
|
|
|
|
|
|
|
|
if (direc & DIRECTIVE_BACKWARD) {
|
|
|
|
wchar_t *bp, *fp, c;
|
2015-10-22 14:29:19 +00:00
|
|
|
free(tr);
|
2015-08-07 23:41:26 +00:00
|
|
|
if ((tr = wcsdup(t)) == NULL) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
bp = tr;
|
|
|
|
fp = tr + wcslen(tr) - 1;
|
|
|
|
while (bp < fp) {
|
|
|
|
c = *bp;
|
|
|
|
*bp++ = *fp;
|
|
|
|
*fp-- = c;
|
|
|
|
}
|
|
|
|
t = (const wchar_t *)tr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direc & DIRECTIVE_POSITION) {
|
|
|
|
while (*t || state) {
|
|
|
|
_collate_lookup(table, t, &len, &pri, pass, &state);
|
|
|
|
t += len;
|
|
|
|
if (pri <= 0) {
|
|
|
|
if (pri < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-07-15 23:03:20 +00:00
|
|
|
state = NULL;
|
2015-08-07 23:41:26 +00:00
|
|
|
pri = COLLATE_MAX_PRIORITY;
|
|
|
|
}
|
|
|
|
if (room) {
|
|
|
|
*xf++ = pri;
|
|
|
|
room--;
|
|
|
|
}
|
|
|
|
want++;
|
|
|
|
need = want;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*t || state) {
|
|
|
|
_collate_lookup(table, t, &len, &pri, pass, &state);
|
|
|
|
t += len;
|
|
|
|
if (pri <= 0) {
|
|
|
|
if (pri < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-07-15 23:03:20 +00:00
|
|
|
state = NULL;
|
2015-08-07 23:41:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (room) {
|
|
|
|
*xf++ = pri;
|
|
|
|
room--;
|
|
|
|
}
|
|
|
|
want++;
|
|
|
|
need = want;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 14:29:19 +00:00
|
|
|
free(tr);
|
2015-08-07 23:41:26 +00:00
|
|
|
return (need);
|
|
|
|
|
|
|
|
fail:
|
2015-10-22 14:29:19 +00:00
|
|
|
free(tr);
|
2015-08-07 23:41:26 +00:00
|
|
|
return ((size_t)(-1));
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
/*
|
|
|
|
* In the non-POSIX case, we transform each character into a string of
|
|
|
|
* characters representing the character's priority. Since char is usually
|
|
|
|
* signed, we are limited by 7 bits per byte. To avoid zero, we need to add
|
|
|
|
* XFRM_OFFSET, so we can't use a full 7 bits. For simplicity, we choose 6
|
|
|
|
* bits per byte.
|
|
|
|
*
|
|
|
|
* It turns out that we sometimes have real priorities that are
|
|
|
|
* 31-bits wide. (But: be careful using priorities where the high
|
|
|
|
* order bit is set -- i.e. the priority is negative. The sort order
|
|
|
|
* may be surprising!)
|
|
|
|
*
|
|
|
|
* TODO: This would be a good area to optimize somewhat. It turns out
|
|
|
|
* that real prioririties *except for the last UNDEFINED pass* are generally
|
|
|
|
* very small. We need the localedef code to precalculate the max
|
|
|
|
* priority for us, and ideally also give us a mask, and then we could
|
|
|
|
* severely limit what we expand to.
|
|
|
|
*/
|
|
|
|
#define XFRM_BYTES 6
|
|
|
|
#define XFRM_OFFSET ('0') /* make all printable characters */
|
|
|
|
#define XFRM_SHIFT 6
|
|
|
|
#define XFRM_MASK ((1 << XFRM_SHIFT) - 1)
|
|
|
|
#define XFRM_SEP ('.') /* chosen to be less than XFRM_OFFSET */
|
|
|
|
|
|
|
|
static int
|
|
|
|
xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
|
1995-02-17 16:36:12 +00:00
|
|
|
{
|
2015-08-07 23:41:26 +00:00
|
|
|
/* we use unsigned to ensure zero fill on right shift */
|
2018-10-20 20:51:05 +00:00
|
|
|
uint32_t val = (uint32_t)table->info->pri_count[pass];
|
2015-08-07 23:41:26 +00:00
|
|
|
int nc = 0;
|
|
|
|
|
|
|
|
while (val) {
|
|
|
|
*p = (pri & XFRM_MASK) + XFRM_OFFSET;
|
|
|
|
pri >>= XFRM_SHIFT;
|
|
|
|
val >>= XFRM_SHIFT;
|
|
|
|
p++;
|
|
|
|
nc++;
|
|
|
|
}
|
|
|
|
return (nc);
|
1995-02-17 16:36:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
size_t
|
|
|
|
_collate_sxfrm(struct xlocale_collate *table, const wchar_t *src, char *xf,
|
|
|
|
size_t room)
|
|
|
|
{
|
|
|
|
int pri;
|
|
|
|
int len;
|
|
|
|
const wchar_t *t;
|
|
|
|
wchar_t *tr = NULL;
|
|
|
|
int direc;
|
|
|
|
int pass;
|
|
|
|
const int32_t *state;
|
|
|
|
size_t want = 0;
|
|
|
|
size_t need = 0;
|
|
|
|
int b;
|
|
|
|
uint8_t buf[XFRM_BYTES];
|
2015-10-22 14:29:19 +00:00
|
|
|
int ndir = table->info->directive_count;
|
|
|
|
|
|
|
|
assert(src);
|
2015-08-07 23:41:26 +00:00
|
|
|
|
2015-10-22 14:29:19 +00:00
|
|
|
for (pass = 0; pass <= ndir; pass++) {
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
state = NULL;
|
|
|
|
|
|
|
|
if (pass != 0) {
|
|
|
|
/* insert level separator from the previous pass */
|
|
|
|
if (room) {
|
|
|
|
*xf++ = XFRM_SEP;
|
|
|
|
room--;
|
|
|
|
}
|
|
|
|
want++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* special pass for undefined */
|
2015-10-22 14:29:19 +00:00
|
|
|
if (pass == ndir) {
|
2015-08-07 23:41:26 +00:00
|
|
|
direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
|
|
|
|
} else {
|
|
|
|
direc = table->info->directive[pass];
|
|
|
|
}
|
|
|
|
|
|
|
|
t = src;
|
|
|
|
|
|
|
|
if (direc & DIRECTIVE_BACKWARD) {
|
|
|
|
wchar_t *bp, *fp, c;
|
2015-10-22 14:29:19 +00:00
|
|
|
free(tr);
|
2015-08-07 23:41:26 +00:00
|
|
|
if ((tr = wcsdup(t)) == NULL) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
bp = tr;
|
|
|
|
fp = tr + wcslen(tr) - 1;
|
|
|
|
while (bp < fp) {
|
|
|
|
c = *bp;
|
|
|
|
*bp++ = *fp;
|
|
|
|
*fp-- = c;
|
|
|
|
}
|
|
|
|
t = (const wchar_t *)tr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direc & DIRECTIVE_POSITION) {
|
|
|
|
while (*t || state) {
|
|
|
|
|
|
|
|
_collate_lookup(table, t, &len, &pri, pass, &state);
|
|
|
|
t += len;
|
|
|
|
if (pri <= 0) {
|
|
|
|
if (pri < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-07-15 23:03:20 +00:00
|
|
|
state = NULL;
|
2015-08-07 23:41:26 +00:00
|
|
|
pri = COLLATE_MAX_PRIORITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = xfrm(table, buf, pri, pass);
|
|
|
|
want += b;
|
|
|
|
if (room) {
|
|
|
|
while (b) {
|
|
|
|
b--;
|
|
|
|
if (room) {
|
|
|
|
*xf++ = buf[b];
|
|
|
|
room--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
need = want;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*t || state) {
|
|
|
|
_collate_lookup(table, t, &len, &pri, pass, &state);
|
|
|
|
t += len;
|
|
|
|
if (pri <= 0) {
|
|
|
|
if (pri < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-07-15 23:03:20 +00:00
|
|
|
state = NULL;
|
2015-08-07 23:41:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = xfrm(table, buf, pri, pass);
|
|
|
|
want += b;
|
|
|
|
if (room) {
|
|
|
|
|
|
|
|
while (b) {
|
|
|
|
b--;
|
|
|
|
if (room) {
|
|
|
|
*xf++ = buf[b];
|
|
|
|
room--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
need = want;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 14:29:19 +00:00
|
|
|
free(tr);
|
2015-08-07 23:41:26 +00:00
|
|
|
return (need);
|
|
|
|
|
|
|
|
fail:
|
2015-10-22 14:29:19 +00:00
|
|
|
free(tr);
|
2015-08-07 23:41:26 +00:00
|
|
|
return ((size_t)(-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* __collate_equiv_value returns the primary collation value for the given
|
|
|
|
* collating symbol specified by str and len. Zero or negative is returned
|
|
|
|
* if the collating symbol was not found. This function is used by bracket
|
|
|
|
* code in the TRE regex library.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
__collate_equiv_value(locale_t locale, const wchar_t *str, size_t len)
|
1995-02-16 04:24:39 +00:00
|
|
|
{
|
2015-08-07 23:41:26 +00:00
|
|
|
int32_t e;
|
|
|
|
|
|
|
|
if (len < 1 || len >= COLLATE_STR_LEN)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
FIX_LOCALE(locale);
|
|
|
|
struct xlocale_collate *table =
|
|
|
|
(struct xlocale_collate*)locale->components[XLC_COLLATE];
|
|
|
|
|
|
|
|
if (table->__collate_load_error)
|
|
|
|
return ((len == 1 && *str <= UCHAR_MAX) ? *str : -1);
|
|
|
|
|
|
|
|
if (len == 1) {
|
|
|
|
e = -1;
|
|
|
|
if (*str <= UCHAR_MAX)
|
|
|
|
e = table->char_pri_table[*str].pri[0];
|
2018-10-20 20:51:05 +00:00
|
|
|
else if (table->info->large_count > 0) {
|
2015-08-07 23:41:26 +00:00
|
|
|
collate_large_t *match_large;
|
|
|
|
match_large = largesearch(table, *str);
|
|
|
|
if (match_large)
|
|
|
|
e = match_large->pri.pri[0];
|
|
|
|
}
|
|
|
|
if (e == 0)
|
|
|
|
return (1);
|
|
|
|
return (e > 0 ? e : 0);
|
|
|
|
}
|
2018-10-20 20:51:05 +00:00
|
|
|
if (table->info->chain_count > 0) {
|
2015-08-07 23:41:26 +00:00
|
|
|
wchar_t name[COLLATE_STR_LEN];
|
|
|
|
collate_chain_t *match_chain;
|
|
|
|
int clen;
|
|
|
|
|
|
|
|
wcsncpy (name, str, len);
|
|
|
|
name[len] = 0;
|
|
|
|
match_chain = chainsearch(table, name, &clen);
|
|
|
|
if (match_chain) {
|
|
|
|
e = match_chain->pri[0];
|
|
|
|
if (e == 0)
|
|
|
|
return (1);
|
|
|
|
return (e < 0 ? -e : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
1995-02-16 04:24:39 +00:00
|
|
|
}
|