1994-05-27 05:00:24 +00:00
|
|
|
/*-
|
2017-11-25 17:12:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2015-08-07 23:41:26 +00:00
|
|
|
* Copyright 2010 Nexenta Systems, Inc. All rights reserved.
|
1995-02-16 17:01:11 +00:00
|
|
|
* Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
|
|
|
|
* at Electronni Visti IA, Kiev, Ukraine.
|
|
|
|
* All rights reserved.
|
1994-05-27 05:00:24 +00:00
|
|
|
*
|
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.
|
|
|
|
*
|
1994-05-27 05:00:24 +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.
|
|
|
|
*
|
1995-02-16 17:01:11 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
|
1994-05-27 05:00:24 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1995-02-16 17:01:11 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
|
1994-05-27 05:00:24 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-11-07 19:55:16 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2001-05-24 08:47:42 +00:00
|
|
|
|
1995-02-16 17:01:11 +00:00
|
|
|
#include <stdlib.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <string.h>
|
2015-08-07 23:41:26 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <wchar.h>
|
1995-02-16 17:01:11 +00:00
|
|
|
#include "collate.h"
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2011-11-20 14:45:42 +00:00
|
|
|
size_t
|
|
|
|
strxfrm_l(char * __restrict dest, const char * __restrict src, size_t len, locale_t loc);
|
1994-05-27 05:00:24 +00:00
|
|
|
size_t
|
2002-09-06 11:24:06 +00:00
|
|
|
strxfrm(char * __restrict dest, const char * __restrict src, size_t len)
|
2011-11-20 14:45:42 +00:00
|
|
|
{
|
|
|
|
return strxfrm_l(dest, src, len, __get_locale());
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
strxfrm_l(char * __restrict dest, const char * __restrict src, size_t len, locale_t locale)
|
1994-05-27 05:00:24 +00:00
|
|
|
{
|
1999-02-12 23:40:41 +00:00
|
|
|
size_t slen;
|
2015-08-07 23:41:26 +00:00
|
|
|
size_t xlen;
|
|
|
|
wchar_t *wcs = NULL;
|
|
|
|
|
2011-11-20 14:45:42 +00:00
|
|
|
FIX_LOCALE(locale);
|
|
|
|
struct xlocale_collate *table =
|
|
|
|
(struct xlocale_collate*)locale->components[XLC_COLLATE];
|
1995-02-16 17:01:11 +00:00
|
|
|
|
|
|
|
if (!*src) {
|
1999-02-12 23:40:41 +00:00
|
|
|
if (len > 0)
|
|
|
|
*dest = '\0';
|
2015-08-07 23:41:26 +00:00
|
|
|
return (0);
|
1995-02-16 17:01:11 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
/*
|
|
|
|
* The conversion from multibyte to wide character strings is
|
|
|
|
* strictly reducing (one byte of an mbs cannot expand to more
|
|
|
|
* than one wide character.)
|
|
|
|
*/
|
|
|
|
slen = strlen(src);
|
|
|
|
|
2011-11-20 14:45:42 +00:00
|
|
|
if (table->__collate_load_error)
|
2015-08-07 23:41:26 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((wcs = malloc((slen + 1) * sizeof (wchar_t))) == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (mbstowcs_l(wcs, src, slen + 1, locale) == (size_t)-1)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((xlen = _collate_sxfrm(table, wcs, dest, len)) == (size_t)-1)
|
|
|
|
goto error;
|
|
|
|
|
2015-08-09 00:19:14 +00:00
|
|
|
free(wcs);
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
if (len > xlen) {
|
|
|
|
dest[xlen] = 0;
|
|
|
|
} else if (len) {
|
|
|
|
dest[len-1] = 0;
|
1995-02-16 17:01:11 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 23:41:26 +00:00
|
|
|
return (xlen);
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* errno should be set to ENOMEM if malloc failed */
|
2015-08-09 00:19:14 +00:00
|
|
|
free(wcs);
|
|
|
|
strlcpy(dest, src, len);
|
2015-08-07 23:41:26 +00:00
|
|
|
|
|
|
|
return (slen);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|