xref: /freebsd/lib/libc/string/wcsxfrm.c (revision 3c87aa1d3dc1d8dad3efad322852a8e1e76dee55)
1fd4f1dd9STim J. Robbins /*-
2fd4f1dd9STim J. Robbins  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
3fd4f1dd9STim J. Robbins  *		at Electronni Visti IA, Kiev, Ukraine.
4fd4f1dd9STim J. Robbins  *			All rights reserved.
5fd4f1dd9STim J. Robbins  *
6*3c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
7*3c87aa1dSDavid Chisnall  * All rights reserved.
8*3c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
9*3c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
10*3c87aa1dSDavid Chisnall  *
11fd4f1dd9STim J. Robbins  * Redistribution and use in source and binary forms, with or without
12fd4f1dd9STim J. Robbins  * modification, are permitted provided that the following conditions
13fd4f1dd9STim J. Robbins  * are met:
14fd4f1dd9STim J. Robbins  * 1. Redistributions of source code must retain the above copyright
15fd4f1dd9STim J. Robbins  *    notice, this list of conditions and the following disclaimer.
16fd4f1dd9STim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
17fd4f1dd9STim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
18fd4f1dd9STim J. Robbins  *    documentation and/or other materials provided with the distribution.
19fd4f1dd9STim J. Robbins  *
20fd4f1dd9STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
21fd4f1dd9STim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22fd4f1dd9STim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23fd4f1dd9STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
24fd4f1dd9STim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25fd4f1dd9STim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26fd4f1dd9STim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27fd4f1dd9STim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28fd4f1dd9STim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29fd4f1dd9STim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30fd4f1dd9STim J. Robbins  * SUCH DAMAGE.
31fd4f1dd9STim J. Robbins  */
32fd4f1dd9STim J. Robbins 
33fd4f1dd9STim J. Robbins #include <sys/cdefs.h>
34fd4f1dd9STim J. Robbins #if 0
35fd4f1dd9STim J. Robbins __FBSDID("FreeBSD: src/lib/libc/string/strxfrm.c,v 1.15 2002/09/06 11:24:06 tjr Exp ");
36fd4f1dd9STim J. Robbins #endif
37fd4f1dd9STim J. Robbins __FBSDID("$FreeBSD$");
38fd4f1dd9STim J. Robbins 
39fd4f1dd9STim J. Robbins #include <stdlib.h>
40fd4f1dd9STim J. Robbins #include <string.h>
41fd4f1dd9STim J. Robbins #include <wchar.h>
42fd4f1dd9STim J. Robbins #include "collate.h"
43fd4f1dd9STim J. Robbins 
44fd4f1dd9STim J. Robbins static char *__mbsdup(const wchar_t *);
45fd4f1dd9STim J. Robbins 
46fd4f1dd9STim J. Robbins /*
47fd4f1dd9STim J. Robbins  * Placeholder wcsxfrm() implementation. See wcscoll.c for a description of
48fd4f1dd9STim J. Robbins  * the logic used.
49fd4f1dd9STim J. Robbins  */
50fd4f1dd9STim J. Robbins size_t
51*3c87aa1dSDavid Chisnall wcsxfrm_l(wchar_t * __restrict dest, const wchar_t * __restrict src, size_t len, locale_t locale)
52fd4f1dd9STim J. Robbins {
53fd4f1dd9STim J. Robbins 	int prim, sec, l;
54fd4f1dd9STim J. Robbins 	size_t slen;
55fd4f1dd9STim J. Robbins 	char *mbsrc, *s, *ss;
56*3c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
57*3c87aa1dSDavid Chisnall 	struct xlocale_collate *table =
58*3c87aa1dSDavid Chisnall 		(struct xlocale_collate*)locale->components[XLC_COLLATE];
59fd4f1dd9STim J. Robbins 
60fd4f1dd9STim J. Robbins 	if (*src == L'\0') {
61fd4f1dd9STim J. Robbins 		if (len != 0)
62fd4f1dd9STim J. Robbins 			*dest = L'\0';
63fd4f1dd9STim J. Robbins 		return (0);
64fd4f1dd9STim J. Robbins 	}
65fd4f1dd9STim J. Robbins 
66*3c87aa1dSDavid Chisnall 	if (table->__collate_load_error || MB_CUR_MAX > 1) {
67fd4f1dd9STim J. Robbins 		slen = wcslen(src);
68fd4f1dd9STim J. Robbins 		if (len > 0) {
69fd4f1dd9STim J. Robbins 			if (slen < len)
70fd4f1dd9STim J. Robbins 				wcscpy(dest, src);
71fd4f1dd9STim J. Robbins 			else {
72fd4f1dd9STim J. Robbins 				wcsncpy(dest, src, len - 1);
73fd4f1dd9STim J. Robbins 				dest[len - 1] = L'\0';
74fd4f1dd9STim J. Robbins 			}
75fd4f1dd9STim J. Robbins 		}
76fd4f1dd9STim J. Robbins 		return (slen);
77fd4f1dd9STim J. Robbins 	}
78fd4f1dd9STim J. Robbins 
79fd4f1dd9STim J. Robbins 	mbsrc = __mbsdup(src);
80fd4f1dd9STim J. Robbins 	slen = 0;
81fd4f1dd9STim J. Robbins 	prim = sec = 0;
82*3c87aa1dSDavid Chisnall 	ss = s = __collate_substitute(table, mbsrc);
83fd4f1dd9STim J. Robbins 	while (*s != '\0') {
84fd4f1dd9STim J. Robbins 		while (*s != '\0' && prim == 0) {
85*3c87aa1dSDavid Chisnall 			__collate_lookup(table, s, &l, &prim, &sec);
86fd4f1dd9STim J. Robbins 			s += l;
87fd4f1dd9STim J. Robbins 		}
88fd4f1dd9STim J. Robbins 		if (prim != 0) {
89fd4f1dd9STim J. Robbins 			if (len > 1) {
90fd4f1dd9STim J. Robbins 				*dest++ = (wchar_t)prim;
91fd4f1dd9STim J. Robbins 				len--;
92fd4f1dd9STim J. Robbins 			}
93fd4f1dd9STim J. Robbins 			slen++;
94fd4f1dd9STim J. Robbins 			prim = 0;
95fd4f1dd9STim J. Robbins 		}
96fd4f1dd9STim J. Robbins 	}
97fd4f1dd9STim J. Robbins 	free(ss);
98fd4f1dd9STim J. Robbins 	free(mbsrc);
99fd4f1dd9STim J. Robbins 	if (len != 0)
100fd4f1dd9STim J. Robbins 		*dest = L'\0';
101fd4f1dd9STim J. Robbins 
102fd4f1dd9STim J. Robbins 	return (slen);
103fd4f1dd9STim J. Robbins }
104*3c87aa1dSDavid Chisnall size_t
105*3c87aa1dSDavid Chisnall wcsxfrm(wchar_t * __restrict dest, const wchar_t * __restrict src, size_t len)
106*3c87aa1dSDavid Chisnall {
107*3c87aa1dSDavid Chisnall 	return wcsxfrm_l(dest, src, len, __get_locale());
108*3c87aa1dSDavid Chisnall }
109fd4f1dd9STim J. Robbins 
110fd4f1dd9STim J. Robbins static char *
111fd4f1dd9STim J. Robbins __mbsdup(const wchar_t *ws)
112fd4f1dd9STim J. Robbins {
113dc763237STim J. Robbins 	static const mbstate_t initial;
114dc763237STim J. Robbins 	mbstate_t st;
115fd4f1dd9STim J. Robbins 	const wchar_t *wcp;
116fd4f1dd9STim J. Robbins 	size_t len;
117fd4f1dd9STim J. Robbins 	char *mbs;
118fd4f1dd9STim J. Robbins 
119fd4f1dd9STim J. Robbins 	wcp = ws;
120dc763237STim J. Robbins 	st = initial;
121dc763237STim J. Robbins 	if ((len = wcsrtombs(NULL, &wcp, 0, &st)) == (size_t)-1)
122fd4f1dd9STim J. Robbins 		return (NULL);
123fd4f1dd9STim J. Robbins 	if ((mbs = malloc(len + 1)) == NULL)
124fd4f1dd9STim J. Robbins 		return (NULL);
125dc763237STim J. Robbins 	st = initial;
126dc763237STim J. Robbins 	wcsrtombs(mbs, &ws, len + 1, &st);
127fd4f1dd9STim J. Robbins 
128fd4f1dd9STim J. Robbins 	return (mbs);
129fd4f1dd9STim J. Robbins }
130