xref: /illumos-gate/usr/src/lib/libsmbfs/smb/nls.c (revision 613a2f6ba31e891e3d947a356daf5e563d43c1ce)
14bff34e3Sthurlow /*
24bff34e3Sthurlow  * Copyright (c) 2000-2001, Boris Popov
34bff34e3Sthurlow  * All rights reserved.
44bff34e3Sthurlow  *
54bff34e3Sthurlow  * Redistribution and use in source and binary forms, with or without
64bff34e3Sthurlow  * modification, are permitted provided that the following conditions
74bff34e3Sthurlow  * are met:
84bff34e3Sthurlow  * 1. Redistributions of source code must retain the above copyright
94bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer.
104bff34e3Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
114bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer in the
124bff34e3Sthurlow  *    documentation and/or other materials provided with the distribution.
134bff34e3Sthurlow  * 3. All advertising materials mentioning features or use of this software
144bff34e3Sthurlow  *    must display the following acknowledgement:
154bff34e3Sthurlow  *    This product includes software developed by Boris Popov.
164bff34e3Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
174bff34e3Sthurlow  *    may be used to endorse or promote products derived from this software
184bff34e3Sthurlow  *    without specific prior written permission.
194bff34e3Sthurlow  *
204bff34e3Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34e3Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34e3Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34e3Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34e3Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34e3Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34e3Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34e3Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34e3Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34e3Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34e3Sthurlow  * SUCH DAMAGE.
314bff34e3Sthurlow  *
324bff34e3Sthurlow  * $Id: nls.c,v 1.10 2004/12/13 00:25:22 lindak Exp $
334bff34e3Sthurlow  */
344bff34e3Sthurlow 
354bff34e3Sthurlow #include <sys/types.h>
364bff34e3Sthurlow #include <ctype.h>
374bff34e3Sthurlow #include <errno.h>
384bff34e3Sthurlow #include <stdio.h>
394bff34e3Sthurlow #include <strings.h>
404bff34e3Sthurlow #include <stdlib.h>
414bff34e3Sthurlow #include <locale.h>
424bff34e3Sthurlow #include <errno.h>
434bff34e3Sthurlow 
444bff34e3Sthurlow #include <netsmb/smb_lib.h>
454bff34e3Sthurlow 
464bff34e3Sthurlow /*
474bff34e3Sthurlow  * prototype iconv* functions
484bff34e3Sthurlow  */
494bff34e3Sthurlow typedef void *iconv_t;
504bff34e3Sthurlow 
514bff34e3Sthurlow static size_t(*my_iconv)(iconv_t, const char **, size_t *, char **, size_t *);
524bff34e3Sthurlow 
53*613a2f6bSGordon Ross uchar_t nls_lower[256];
54*613a2f6bSGordon Ross uchar_t nls_upper[256];
554bff34e3Sthurlow 
564bff34e3Sthurlow static iconv_t nls_toext, nls_toloc;
574bff34e3Sthurlow static int iconv_loaded;
584bff34e3Sthurlow 
594bff34e3Sthurlow int
nls_setlocale(const char * name)604bff34e3Sthurlow nls_setlocale(const char *name)
614bff34e3Sthurlow {
624bff34e3Sthurlow 	int i;
634bff34e3Sthurlow 
644bff34e3Sthurlow 	if (setlocale(LC_CTYPE, name) == NULL) {
654bff34e3Sthurlow 		fprintf(stdout, dgettext(TEXT_DOMAIN,
664bff34e3Sthurlow 		    "can't set locale '%s'\n"), name);
674bff34e3Sthurlow 	}
684bff34e3Sthurlow 	for (i = 0; i < 256; i++) {
694bff34e3Sthurlow 		nls_lower[i] = tolower(i);
704bff34e3Sthurlow 		nls_upper[i] = toupper(i);
714bff34e3Sthurlow 	}
72*613a2f6bSGordon Ross 	return (0);
734bff34e3Sthurlow }
744bff34e3Sthurlow 
75*613a2f6bSGordon Ross /*ARGSUSED*/
764bff34e3Sthurlow int
nls_setrecode(const char * local,const char * external)774bff34e3Sthurlow nls_setrecode(const char *local, const char *external)
784bff34e3Sthurlow {
79*613a2f6bSGordon Ross 	return (ENOENT);
804bff34e3Sthurlow }
814bff34e3Sthurlow 
824bff34e3Sthurlow char *
nls_str_toloc(char * dst,const char * src)834bff34e3Sthurlow nls_str_toloc(char *dst, const char *src)
844bff34e3Sthurlow {
854bff34e3Sthurlow 	char *p = dst;
864bff34e3Sthurlow 	size_t inlen, outlen;
874bff34e3Sthurlow 
884bff34e3Sthurlow 	if (!iconv_loaded)
89*613a2f6bSGordon Ross 		return (strcpy(dst, src));
904bff34e3Sthurlow 
914bff34e3Sthurlow 	if (nls_toloc == (iconv_t)0)
92*613a2f6bSGordon Ross 		return (strcpy(dst, src));
934bff34e3Sthurlow 	inlen = outlen = strlen(src);
944bff34e3Sthurlow 	my_iconv(nls_toloc, NULL, NULL, &p, &outlen);
954bff34e3Sthurlow 	my_iconv(nls_toloc, &src, &inlen, &p, &outlen);
964bff34e3Sthurlow 	*p = 0;
97*613a2f6bSGordon Ross 	return (dst);
984bff34e3Sthurlow }
994bff34e3Sthurlow 
1004bff34e3Sthurlow char *
nls_str_toext(char * dst,const char * src)1014bff34e3Sthurlow nls_str_toext(char *dst, const char *src)
1024bff34e3Sthurlow {
1034bff34e3Sthurlow 	char *p = dst;
1044bff34e3Sthurlow 	size_t inlen, outlen;
1054bff34e3Sthurlow 
1064bff34e3Sthurlow 	if (!iconv_loaded)
107*613a2f6bSGordon Ross 		return (strcpy(dst, src));
1084bff34e3Sthurlow 
1094bff34e3Sthurlow 	if (nls_toext == (iconv_t)0)
110*613a2f6bSGordon Ross 		return (strcpy(dst, src));
1114bff34e3Sthurlow 	inlen = outlen = strlen(src);
1124bff34e3Sthurlow 	my_iconv(nls_toext, NULL, NULL, &p, &outlen);
1134bff34e3Sthurlow 	my_iconv(nls_toext, &src, &inlen, &p, &outlen);
1144bff34e3Sthurlow 	*p = 0;
115*613a2f6bSGordon Ross 	return (dst);
1164bff34e3Sthurlow }
1174bff34e3Sthurlow 
1184bff34e3Sthurlow void *
nls_mem_toloc(void * dst,const void * src,int size)1194bff34e3Sthurlow nls_mem_toloc(void *dst, const void *src, int size)
1204bff34e3Sthurlow {
1214bff34e3Sthurlow 	char *p = dst;
1224bff34e3Sthurlow 	const char *s = src;
1234bff34e3Sthurlow 	size_t inlen, outlen;
1244bff34e3Sthurlow 
1254bff34e3Sthurlow 	if (!iconv_loaded)
126*613a2f6bSGordon Ross 		return (memcpy(dst, src, size));
1274bff34e3Sthurlow 
1284bff34e3Sthurlow 	if (size == 0)
129*613a2f6bSGordon Ross 		return (NULL);
1304bff34e3Sthurlow 
1314bff34e3Sthurlow 	if (nls_toloc == (iconv_t)0)
132*613a2f6bSGordon Ross 		return (memcpy(dst, src, size));
1334bff34e3Sthurlow 	inlen = outlen = size;
1344bff34e3Sthurlow 	my_iconv(nls_toloc, NULL, NULL, &p, &outlen);
1354bff34e3Sthurlow 	my_iconv(nls_toloc, &s, &inlen, &p, &outlen);
136*613a2f6bSGordon Ross 	return (dst);
1374bff34e3Sthurlow }
1384bff34e3Sthurlow 
1394bff34e3Sthurlow void *
nls_mem_toext(void * dst,const void * src,int size)1404bff34e3Sthurlow nls_mem_toext(void *dst, const void *src, int size)
1414bff34e3Sthurlow {
1424bff34e3Sthurlow 	char *p = dst;
1434bff34e3Sthurlow 	const char *s = src;
1444bff34e3Sthurlow 	size_t inlen, outlen;
1454bff34e3Sthurlow 
1464bff34e3Sthurlow 	if (size == 0)
147*613a2f6bSGordon Ross 		return (NULL);
1484bff34e3Sthurlow 
1494bff34e3Sthurlow 	if (!iconv_loaded || nls_toext == (iconv_t)0)
150*613a2f6bSGordon Ross 		return (memcpy(dst, src, size));
1514bff34e3Sthurlow 
1524bff34e3Sthurlow 	inlen = outlen = size;
1534bff34e3Sthurlow 	my_iconv(nls_toext, NULL, NULL, &p, &outlen);
1544bff34e3Sthurlow 	my_iconv(nls_toext, &s, &inlen, &p, &outlen);
155*613a2f6bSGordon Ross 	return (dst);
1564bff34e3Sthurlow }
1574bff34e3Sthurlow 
1584bff34e3Sthurlow char *
nls_str_upper(char * dst,const char * src)1594bff34e3Sthurlow nls_str_upper(char *dst, const char *src)
1604bff34e3Sthurlow {
1614bff34e3Sthurlow 	char *p = dst;
1624bff34e3Sthurlow 
1634bff34e3Sthurlow 	while (*src)
1644bff34e3Sthurlow 		*dst++ = toupper(*src++);
1654bff34e3Sthurlow 	*dst = 0;
166*613a2f6bSGordon Ross 	return (p);
1674bff34e3Sthurlow }
1684bff34e3Sthurlow 
1694bff34e3Sthurlow char *
nls_str_lower(char * dst,const char * src)1704bff34e3Sthurlow nls_str_lower(char *dst, const char *src)
1714bff34e3Sthurlow {
1724bff34e3Sthurlow 	char *p = dst;
1734bff34e3Sthurlow 
1744bff34e3Sthurlow 	while (*src)
1754bff34e3Sthurlow 		*dst++ = tolower(*src++);
1764bff34e3Sthurlow 	*dst = 0;
177*613a2f6bSGordon Ross 	return (p);
1784bff34e3Sthurlow }
179