1*16db8bacSJohn Marino /*
2*16db8bacSJohn Marino * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3716024cdSPeter Avalos * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
4716024cdSPeter Avalos * at Electronni Visti IA, Kiev, Ukraine.
5716024cdSPeter Avalos * All rights reserved.
6716024cdSPeter Avalos *
70d5acd74SJohn Marino * Copyright (c) 2011 The FreeBSD Foundation
80d5acd74SJohn Marino * All rights reserved.
90d5acd74SJohn Marino * Portions of this software were developed by David Chisnall
100d5acd74SJohn Marino * under sponsorship from the FreeBSD Foundation.
110d5acd74SJohn Marino *
12716024cdSPeter Avalos * Redistribution and use in source and binary forms, with or without
13716024cdSPeter Avalos * modification, are permitted provided that the following conditions
14716024cdSPeter Avalos * are met:
15716024cdSPeter Avalos * 1. Redistributions of source code must retain the above copyright
16716024cdSPeter Avalos * notice, this list of conditions and the following disclaimer.
17716024cdSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
18716024cdSPeter Avalos * notice, this list of conditions and the following disclaimer in the
19716024cdSPeter Avalos * documentation and/or other materials provided with the distribution.
20716024cdSPeter Avalos *
21716024cdSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
22716024cdSPeter Avalos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23716024cdSPeter Avalos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24716024cdSPeter Avalos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
25716024cdSPeter Avalos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26716024cdSPeter Avalos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27716024cdSPeter Avalos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28716024cdSPeter Avalos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29716024cdSPeter Avalos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30716024cdSPeter Avalos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31716024cdSPeter Avalos * SUCH DAMAGE.
32716024cdSPeter Avalos */
33716024cdSPeter Avalos
34716024cdSPeter Avalos #include <stdlib.h>
35716024cdSPeter Avalos #include <string.h>
36716024cdSPeter Avalos #include <wchar.h>
37716024cdSPeter Avalos #include "collate.h"
38716024cdSPeter Avalos
39716024cdSPeter Avalos size_t
wcsxfrm_l(wchar_t * __restrict dest,const wchar_t * __restrict src,size_t len,locale_t locale)400d5acd74SJohn Marino wcsxfrm_l(wchar_t * __restrict dest, const wchar_t * __restrict src, size_t len, locale_t locale)
41716024cdSPeter Avalos {
42716024cdSPeter Avalos size_t slen;
430d5acd74SJohn Marino FIX_LOCALE(locale);
440d5acd74SJohn Marino struct xlocale_collate *table =
450d5acd74SJohn Marino (struct xlocale_collate*)locale->components[XLC_COLLATE];
46716024cdSPeter Avalos
47716024cdSPeter Avalos if (*src == L'\0') {
48716024cdSPeter Avalos if (len != 0)
49716024cdSPeter Avalos *dest = L'\0';
50716024cdSPeter Avalos return (0);
51716024cdSPeter Avalos }
52716024cdSPeter Avalos
53*16db8bacSJohn Marino if ((table->__collate_load_error) ||
54*16db8bacSJohn Marino ((slen = _collate_wxfrm(table, src, dest, len)) == (size_t)-1)) {
55*16db8bacSJohn Marino goto error;
56*16db8bacSJohn Marino }
57*16db8bacSJohn Marino
58*16db8bacSJohn Marino /* Add null termination at the correct location. */
59*16db8bacSJohn Marino if (len > slen) {
60*16db8bacSJohn Marino dest[slen] = 0;
61*16db8bacSJohn Marino } else if (len) {
62*16db8bacSJohn Marino dest[len-1] = 0;
63*16db8bacSJohn Marino }
64*16db8bacSJohn Marino
65*16db8bacSJohn Marino return (slen);
66*16db8bacSJohn Marino
67*16db8bacSJohn Marino error:
68716024cdSPeter Avalos slen = wcslen(src);
69716024cdSPeter Avalos if (slen < len)
70*16db8bacSJohn Marino (void) wcscpy(dest, src);
71716024cdSPeter Avalos else {
72*16db8bacSJohn Marino (void) wcsncpy(dest, src, len - 1);
73716024cdSPeter Avalos dest[len - 1] = L'\0';
74716024cdSPeter Avalos }
75716024cdSPeter Avalos return (slen);
76716024cdSPeter Avalos }
77716024cdSPeter Avalos
780d5acd74SJohn Marino size_t
wcsxfrm(wchar_t * __restrict dest,const wchar_t * __restrict src,size_t len)790d5acd74SJohn Marino wcsxfrm(wchar_t * __restrict dest, const wchar_t * __restrict src, size_t len)
800d5acd74SJohn Marino {
810d5acd74SJohn Marino return wcsxfrm_l(dest, src, len, __get_locale());
820d5acd74SJohn Marino }
83