xref: /illumos-gate/usr/src/lib/libc/port/locale/wcsxfrm.c (revision 2d08521b)
14297a3b0SGarrett D'Amore /*
2*2d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
34c94caaeSGordon Ross  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
44297a3b0SGarrett D'Amore  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
54297a3b0SGarrett D'Amore  *		at Electronni Visti IA, Kiev, Ukraine.
64297a3b0SGarrett D'Amore  *			All rights reserved.
74297a3b0SGarrett D'Amore  *
84297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
94297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
104297a3b0SGarrett D'Amore  * are met:
114297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
124297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
134297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
144297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
154297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
164297a3b0SGarrett D'Amore  *
174297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
184297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
214297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274297a3b0SGarrett D'Amore  * SUCH DAMAGE.
284297a3b0SGarrett D'Amore  */
294297a3b0SGarrett D'Amore 
304297a3b0SGarrett D'Amore #include "lint.h"
314297a3b0SGarrett D'Amore #include <stdlib.h>
324297a3b0SGarrett D'Amore #include <string.h>
334297a3b0SGarrett D'Amore #include <wchar.h>
346b5e5868SGarrett D'Amore #include <assert.h>
354297a3b0SGarrett D'Amore #include "collate.h"
364297a3b0SGarrett D'Amore 
376b5e5868SGarrett D'Amore #define	WCS_XFRM_OFFSET	1
384297a3b0SGarrett D'Amore 
394297a3b0SGarrett D'Amore size_t
wcsxfrm_l(wchar_t * _RESTRICT_KYWD dest,const wchar_t * _RESTRICT_KYWD src,size_t len,locale_t loc)40*2d08521bSGarrett D'Amore wcsxfrm_l(wchar_t *_RESTRICT_KYWD dest,
41*2d08521bSGarrett D'Amore     const wchar_t *_RESTRICT_KYWD src, size_t len, locale_t loc)
424297a3b0SGarrett D'Amore {
434297a3b0SGarrett D'Amore 	size_t slen;
44*2d08521bSGarrett D'Amore 	const struct lc_collate *lcc = loc->collate;
454297a3b0SGarrett D'Amore 
464297a3b0SGarrett D'Amore 	if (*src == L'\0') {
474297a3b0SGarrett D'Amore 		if (len != 0)
484297a3b0SGarrett D'Amore 			*dest = L'\0';
494297a3b0SGarrett D'Amore 		return (0);
504297a3b0SGarrett D'Amore 	}
514297a3b0SGarrett D'Amore 
52*2d08521bSGarrett D'Amore 	if ((lcc->lc_is_posix) ||
53*2d08521bSGarrett D'Amore 	    ((slen = _collate_wxfrm(lcc, src, dest, len)) == (size_t)-1)) {
546b5e5868SGarrett D'Amore 		goto error;
554297a3b0SGarrett D'Amore 	}
564297a3b0SGarrett D'Amore 
576b5e5868SGarrett D'Amore 	/* Add null termination at the correct location. */
586b5e5868SGarrett D'Amore 	if (len > slen) {
596b5e5868SGarrett D'Amore 		dest[slen] = 0;
604c94caaeSGordon Ross 	} else if (len != 0) {
616b5e5868SGarrett D'Amore 		dest[len-1] = 0;
624297a3b0SGarrett D'Amore 	}
634297a3b0SGarrett D'Amore 
644297a3b0SGarrett D'Amore 	return (slen);
654297a3b0SGarrett D'Amore 
666b5e5868SGarrett D'Amore error:
676b5e5868SGarrett D'Amore 	slen = wcslen(src);
686b5e5868SGarrett D'Amore 	if (slen < len)
696b5e5868SGarrett D'Amore 		(void) wcscpy(dest, src);
704c94caaeSGordon Ross 	else if (len != 0) {
716b5e5868SGarrett D'Amore 		(void) wcsncpy(dest, src, len - 1);
726b5e5868SGarrett D'Amore 		dest[len - 1] = L'\0';
736b5e5868SGarrett D'Amore 	}
746b5e5868SGarrett D'Amore 	return (slen);
754297a3b0SGarrett D'Amore }
76*2d08521bSGarrett D'Amore 
77*2d08521bSGarrett D'Amore size_t
wcsxfrm(wchar_t * _RESTRICT_KYWD dest,const wchar_t * _RESTRICT_KYWD src,size_t len)78*2d08521bSGarrett D'Amore wcsxfrm(wchar_t *_RESTRICT_KYWD dest,
79*2d08521bSGarrett D'Amore     const wchar_t *_RESTRICT_KYWD src, size_t len)
80*2d08521bSGarrett D'Amore {
81*2d08521bSGarrett D'Amore 	return (wcsxfrm_l(dest, src, len, uselocale(NULL)));
82*2d08521bSGarrett D'Amore }
83