collate.c revision 63481050feaa198bef4514642e69c860b336aa1b
/*
* Copright 2010 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
* at Electronni Visti IA, Kiev, Ukraine.
* All rights reserved.
*
* 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
*
* 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.
*/
#include "lint.h"
#include "file64.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <wchar.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include "collate.h"
#include "setlocale.h"
#include "ldpart.h"
/*
* information. It would also be very helpful to have a copy of the
* POSIX standard for collation (in the locale format manual page)
* handy (www.opengroup.org).
*/
static collate_char_t *char_pri_table;
static collate_large_t *large_pri_table;
static collate_chain_t *chain_pri_table;
/* Exposed externally to other parts of libc. */
int _collate_load_error = 1;
int
_collate_load_tables(const char *encoding)
{
int i, chains, z;
char *TMP;
char *map;
int fd;
/* 'encoding' must be already checked. */
_collate_load_error = 1;
return (_LDP_CACHE);
}
/*
* If the locale name is the same as our cache, use the cache.
*/
_collate_load_error = 0;
return (_LDP_CACHE);
}
/*
* Slurp the locale file into the cache.
*/
return (_LDP_ERROR);
return (_LDP_ERROR);
}
return (_LDP_ERROR);
}
return (_LDP_ERROR);
}
return (_LDP_ERROR);
}
TMP += COLLATE_STR_LEN;
return (_LDP_ERROR);
}
(sizeof (collate_chain_t) * chains) +
for (z = 0; z < (info->directive_count); z++) {
}
return (_LDP_ERROR);
}
char_pri_table = (void *)TMP;
for (z = 0; z < info->directive_count; z++) {
if (info->subst_count[z] > 0) {
subst_table[z] = (void *)TMP;
} else {
subst_table[z] = NULL;
}
}
if (chains > 0) {
chain_pri_table = (void *)TMP;
} else
if (info->large_count > 0)
large_pri_table = (void *)TMP;
else
if (cache)
_collate_load_error = 0;
return (_LDP_LOADED);
}
static int32_t *
{
collate_subst_t *p;
if (n == 0)
return (NULL);
return (NULL);
if (!(key & COLLATE_SUBST_PRIORITY))
return (NULL);
return (p->pri);
}
/*
* Note: for performance reasons, we have expanded bsearch here. This avoids
* function call overhead with each comparison.
*/
static collate_chain_t *
{
int low;
int high;
collate_chain_t *p;
if (_collate_info->chain_count == 0)
return (NULL);
low = 0;
if (compar == 0) {
if (compar == 0) {
*len = l;
return (p);
}
}
if (compar > 0)
else
}
return (NULL);
}
static collate_large_t *
{
int low = 0;
collate_large_t *p;
if (_collate_info->large_count == 0)
return (NULL);
if (compar == 0)
return (p);
if (compar > 0)
else
}
return (NULL);
}
void
{
int p, l;
int *sptr;
/*
* If this is the "last" pass for the UNDEFINED, then
* we just return the priority itself.
*/
*pri = *t;
*len = 1;
return;
}
/*
* If we have remaining substitution data from a previous
* call, consume it first.
*/
sptr++;
*len = 0;
return;
}
/* No active substitutions */
*len = 1;
/*
* Check for composites such as dipthongs that collate as a
* single element (aka chains or collating-elements).
*/
*len = l;
*pri = p;
} else if (*t <= UCHAR_MAX) {
/*
* Character is a small (8-bit) character.
* We just look these up directly for speed.
*/
} else if ((info->large_count > 0) &&
/*
* Character was found in the extended table.
*/
} else {
/*
* Character lacks a specific definition.
*/
/* Mask off sign bit to prevent ordering confusion. */
*pri = (*t & COLLATE_MAX_PRIORITY);
} else {
}
/* 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.
*/
sptr++;
}
}
}
/*
* This is the meaty part of wcsxfrm & strxfrm. Note that it does
* NOT NULL terminate. That is left to the caller.
*/
{
int pri;
int len;
const wchar_t *t;
int direc;
int pass;
if (pass != 0) {
/* insert level separator from the previous pass */
if (room) {
*xf++ = 1;
room--;
}
want++;
}
/* special pass for undefined */
} else {
}
t = src;
if (direc & DIRECTIVE_BACKWARD) {
if (tr)
goto fail;
}
c = *bp;
*fp-- = c;
}
}
if (direc & DIRECTIVE_POSITION) {
while (*t || state) {
t += len;
if (pri <= 0) {
if (pri < 0) {
goto fail;
}
}
if (room) {
room--;
}
want++;
}
} else {
while (*t || state) {
t += len;
if (pri <= 0) {
if (pri < 0) {
goto fail;
}
continue;
}
if (room) {
room--;
}
want++;
}
}
}
end:
if (tr)
return (need);
fail:
if (tr)
return ((size_t)(-1));
}
/*
* 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_SHIFT 6
static int
{
/* we use unsigned to ensure zero fill on right shift */
int nc = 0;
while (val) {
pri >>= XFRM_SHIFT;
val >>= XFRM_SHIFT;
p++;
nc++;
}
return (nc);
}
{
int pri;
int len;
const wchar_t *t;
int direc;
int pass;
int b;
if (pass != 0) {
/* insert level separator from the previous pass */
if (room) {
room--;
}
want++;
}
/* special pass for undefined */
} else {
}
t = src;
if (direc & DIRECTIVE_BACKWARD) {
if (tr)
goto fail;
}
c = *bp;
*fp-- = c;
}
}
if (direc & DIRECTIVE_POSITION) {
while (*t || state) {
t += len;
if (pri <= 0) {
if (pri < 0) {
goto fail;
}
}
want += b;
if (room) {
while (b) {
b--;
if (room) {
room--;
}
}
}
}
} else {
while (*t || state) {
t += len;
if (pri <= 0) {
if (pri < 0) {
goto fail;
}
continue;
}
want += b;
if (room) {
while (b) {
b--;
if (room) {
room--;
}
}
}
}
}
}
end:
if (tr)
return (need);
fail:
if (tr)
return ((size_t)(-1));
}