14297a3b0SGarrett D'Amore /*
2*2d08521bSGarrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore * Copyright 2010 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 <errno.h>
346b5e5868SGarrett D'Amore #include <wchar.h>
356b5e5868SGarrett D'Amore #include <assert.h>
36*2d08521bSGarrett D'Amore #include <xlocale.h>
374297a3b0SGarrett D'Amore #include "collate.h"
384297a3b0SGarrett D'Amore
394297a3b0SGarrett D'Amore size_t
strxfrm_l(char * _RESTRICT_KYWD xf,const char * _RESTRICT_KYWD src,size_t dlen,locale_t loc)40*2d08521bSGarrett D'Amore strxfrm_l(char *_RESTRICT_KYWD xf, const char *_RESTRICT_KYWD src,
41*2d08521bSGarrett D'Amore size_t dlen, locale_t loc)
424297a3b0SGarrett D'Amore {
434297a3b0SGarrett D'Amore size_t slen;
446b5e5868SGarrett D'Amore size_t xlen;
456b5e5868SGarrett D'Amore wchar_t *wcs = NULL;
464297a3b0SGarrett D'Amore
474297a3b0SGarrett D'Amore if (!*src) {
486b5e5868SGarrett D'Amore if (dlen > 0)
496b5e5868SGarrett D'Amore *xf = '\0';
504297a3b0SGarrett D'Amore return (0);
514297a3b0SGarrett D'Amore }
524297a3b0SGarrett D'Amore
536b5e5868SGarrett D'Amore /*
546b5e5868SGarrett D'Amore * The conversion from multibyte to wide character strings is
556b5e5868SGarrett D'Amore * strictly reducing (one byte of an mbs cannot expand to more
566b5e5868SGarrett D'Amore * than one wide character.)
576b5e5868SGarrett D'Amore */
586b5e5868SGarrett D'Amore slen = strlen(src);
594297a3b0SGarrett D'Amore
60*2d08521bSGarrett D'Amore if (loc->collate->lc_is_posix)
616b5e5868SGarrett D'Amore goto error;
626b5e5868SGarrett D'Amore
636b5e5868SGarrett D'Amore if ((wcs = malloc((slen + 1) * sizeof (wchar_t))) == NULL)
646b5e5868SGarrett D'Amore goto error;
656b5e5868SGarrett D'Amore
66*2d08521bSGarrett D'Amore if (mbstowcs_l(wcs, src, slen + 1, loc) == (size_t)-1)
676b5e5868SGarrett D'Amore goto error;
686b5e5868SGarrett D'Amore
69*2d08521bSGarrett D'Amore if ((xlen = _collate_sxfrm(wcs, xf, dlen, loc)) == (size_t)-1)
706b5e5868SGarrett D'Amore goto error;
716b5e5868SGarrett D'Amore
726b5e5868SGarrett D'Amore if (wcs)
736b5e5868SGarrett D'Amore free(wcs);
746b5e5868SGarrett D'Amore
756b5e5868SGarrett D'Amore if (dlen > xlen) {
766b5e5868SGarrett D'Amore xf[xlen] = 0;
776b5e5868SGarrett D'Amore } else if (dlen) {
786b5e5868SGarrett D'Amore xf[dlen-1] = 0;
794297a3b0SGarrett D'Amore }
806b5e5868SGarrett D'Amore
816b5e5868SGarrett D'Amore return (xlen);
826b5e5868SGarrett D'Amore
836b5e5868SGarrett D'Amore error:
846b5e5868SGarrett D'Amore /* errno should be set to ENOMEM if malloc failed */
856b5e5868SGarrett D'Amore if (wcs)
866b5e5868SGarrett D'Amore free(wcs);
876b5e5868SGarrett D'Amore (void) strlcpy(xf, src, dlen);
884297a3b0SGarrett D'Amore
894297a3b0SGarrett D'Amore return (slen);
904297a3b0SGarrett D'Amore }
91*2d08521bSGarrett D'Amore
92*2d08521bSGarrett D'Amore size_t
strxfrm(char * _RESTRICT_KYWD xf,const char * _RESTRICT_KYWD src,size_t dlen)93*2d08521bSGarrett D'Amore strxfrm(char *_RESTRICT_KYWD xf, const char *_RESTRICT_KYWD src, size_t dlen)
94*2d08521bSGarrett D'Amore {
95*2d08521bSGarrett D'Amore return (strxfrm_l(xf, src, dlen, uselocale(NULL)));
96*2d08521bSGarrett D'Amore }
97