11573Srgrimes/*- 26509Sache * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua> 36509Sache * at Electronni Visti IA, Kiev, Ukraine. 46509Sache * All rights reserved. 51573Srgrimes * 6227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation 7227753Stheraven * All rights reserved. 8227753Stheraven * Portions of this software were developed by David Chisnall 9227753Stheraven * under sponsorship from the FreeBSD Foundation. 10227753Stheraven * 111573Srgrimes * Redistribution and use in source and binary forms, with or without 121573Srgrimes * modification, are permitted provided that the following conditions 131573Srgrimes * are met: 141573Srgrimes * 1. Redistributions of source code must retain the above copyright 151573Srgrimes * notice, this list of conditions and the following disclaimer. 161573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 171573Srgrimes * notice, this list of conditions and the following disclaimer in the 181573Srgrimes * documentation and/or other materials provided with the distribution. 191573Srgrimes * 206509Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 236509Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301573Srgrimes * SUCH DAMAGE. 311573Srgrimes */ 321573Srgrimes 3386170Sobrien#include <sys/cdefs.h> 3486170Sobrien__FBSDID("$FreeBSD$"); 3577117Sobrien 366509Sache#include <stdlib.h> 371573Srgrimes#include <string.h> 386509Sache#include "collate.h" 391573Srgrimes 401573Srgrimessize_t 41227753Stheravenstrxfrm_l(char * __restrict dest, const char * __restrict src, size_t len, locale_t loc); 42227753Stheravensize_t 43103012Stjrstrxfrm(char * __restrict dest, const char * __restrict src, size_t len) 441573Srgrimes{ 45227753Stheraven return strxfrm_l(dest, src, len, __get_locale()); 46227753Stheraven} 47227753Stheraven 48227753Stheravensize_t 49227753Stheravenstrxfrm_l(char * __restrict dest, const char * __restrict src, size_t len, locale_t locale) 50227753Stheraven{ 516509Sache int prim, sec, l; 5243945Sache size_t slen; 5343945Sache char *s, *ss; 54227753Stheraven FIX_LOCALE(locale); 55227753Stheraven struct xlocale_collate *table = 56227753Stheraven (struct xlocale_collate*)locale->components[XLC_COLLATE]; 571573Srgrimes 586509Sache if (!*src) { 5943945Sache if (len > 0) 6043945Sache *dest = '\0'; 616509Sache return 0; 621573Srgrimes } 636509Sache 64227753Stheraven if (table->__collate_load_error) 65184055Sdelphij return strlcpy(dest, src, len); 666509Sache 6743945Sache slen = 0; 6836665Sache prim = sec = 0; 69227753Stheraven ss = s = __collate_substitute(table, src); 7043945Sache while (*s) { 716509Sache while (*s && !prim) { 72227753Stheraven __collate_lookup(table, s, &l, &prim, &sec); 736509Sache s += l; 746509Sache } 756509Sache if (prim) { 7643945Sache if (len > 1) { 7743945Sache *dest++ = (char)prim; 7843945Sache len--; 7943945Sache } 8043945Sache slen++; 8136665Sache prim = 0; 826509Sache } 836509Sache } 8436665Sache free(ss); 8543945Sache if (len > 0) 8643945Sache *dest = '\0'; 876509Sache 8843945Sache return slen; 891573Srgrimes} 90