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