16007Sthurlow /*
26007Sthurlow * Copyright (c) 2000-2001, Boris Popov
36007Sthurlow * All rights reserved.
46007Sthurlow *
56007Sthurlow * Redistribution and use in source and binary forms, with or without
66007Sthurlow * modification, are permitted provided that the following conditions
76007Sthurlow * are met:
86007Sthurlow * 1. Redistributions of source code must retain the above copyright
96007Sthurlow * notice, this list of conditions and the following disclaimer.
106007Sthurlow * 2. Redistributions in binary form must reproduce the above copyright
116007Sthurlow * notice, this list of conditions and the following disclaimer in the
126007Sthurlow * documentation and/or other materials provided with the distribution.
136007Sthurlow * 3. All advertising materials mentioning features or use of this software
146007Sthurlow * must display the following acknowledgement:
156007Sthurlow * This product includes software developed by Boris Popov.
166007Sthurlow * 4. Neither the name of the author nor the names of any co-contributors
176007Sthurlow * may be used to endorse or promote products derived from this software
186007Sthurlow * without specific prior written permission.
196007Sthurlow *
206007Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
216007Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226007Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236007Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
246007Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256007Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266007Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276007Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286007Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296007Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306007Sthurlow * SUCH DAMAGE.
316007Sthurlow *
326007Sthurlow * $Id: nls.c,v 1.10 2004/12/13 00:25:22 lindak Exp $
336007Sthurlow */
346007Sthurlow
356007Sthurlow #include <sys/types.h>
366007Sthurlow #include <ctype.h>
376007Sthurlow #include <errno.h>
386007Sthurlow #include <stdio.h>
396007Sthurlow #include <strings.h>
406007Sthurlow #include <stdlib.h>
416007Sthurlow #include <locale.h>
426007Sthurlow #include <errno.h>
436007Sthurlow
446007Sthurlow #include <netsmb/smb_lib.h>
456007Sthurlow
466007Sthurlow /*
476007Sthurlow * prototype iconv* functions
486007Sthurlow */
496007Sthurlow typedef void *iconv_t;
506007Sthurlow
516007Sthurlow static size_t(*my_iconv)(iconv_t, const char **, size_t *, char **, size_t *);
526007Sthurlow
53*10023SGordon.Ross@Sun.COM uchar_t nls_lower[256];
54*10023SGordon.Ross@Sun.COM uchar_t nls_upper[256];
556007Sthurlow
566007Sthurlow static iconv_t nls_toext, nls_toloc;
576007Sthurlow static int iconv_loaded;
586007Sthurlow
596007Sthurlow int
nls_setlocale(const char * name)606007Sthurlow nls_setlocale(const char *name)
616007Sthurlow {
626007Sthurlow int i;
636007Sthurlow
646007Sthurlow if (setlocale(LC_CTYPE, name) == NULL) {
656007Sthurlow fprintf(stdout, dgettext(TEXT_DOMAIN,
666007Sthurlow "can't set locale '%s'\n"), name);
676007Sthurlow }
686007Sthurlow for (i = 0; i < 256; i++) {
696007Sthurlow nls_lower[i] = tolower(i);
706007Sthurlow nls_upper[i] = toupper(i);
716007Sthurlow }
72*10023SGordon.Ross@Sun.COM return (0);
736007Sthurlow }
746007Sthurlow
75*10023SGordon.Ross@Sun.COM /*ARGSUSED*/
766007Sthurlow int
nls_setrecode(const char * local,const char * external)776007Sthurlow nls_setrecode(const char *local, const char *external)
786007Sthurlow {
79*10023SGordon.Ross@Sun.COM return (ENOENT);
806007Sthurlow }
816007Sthurlow
826007Sthurlow char *
nls_str_toloc(char * dst,const char * src)836007Sthurlow nls_str_toloc(char *dst, const char *src)
846007Sthurlow {
856007Sthurlow char *p = dst;
866007Sthurlow size_t inlen, outlen;
876007Sthurlow
886007Sthurlow if (!iconv_loaded)
89*10023SGordon.Ross@Sun.COM return (strcpy(dst, src));
906007Sthurlow
916007Sthurlow if (nls_toloc == (iconv_t)0)
92*10023SGordon.Ross@Sun.COM return (strcpy(dst, src));
936007Sthurlow inlen = outlen = strlen(src);
946007Sthurlow my_iconv(nls_toloc, NULL, NULL, &p, &outlen);
956007Sthurlow my_iconv(nls_toloc, &src, &inlen, &p, &outlen);
966007Sthurlow *p = 0;
97*10023SGordon.Ross@Sun.COM return (dst);
986007Sthurlow }
996007Sthurlow
1006007Sthurlow char *
nls_str_toext(char * dst,const char * src)1016007Sthurlow nls_str_toext(char *dst, const char *src)
1026007Sthurlow {
1036007Sthurlow char *p = dst;
1046007Sthurlow size_t inlen, outlen;
1056007Sthurlow
1066007Sthurlow if (!iconv_loaded)
107*10023SGordon.Ross@Sun.COM return (strcpy(dst, src));
1086007Sthurlow
1096007Sthurlow if (nls_toext == (iconv_t)0)
110*10023SGordon.Ross@Sun.COM return (strcpy(dst, src));
1116007Sthurlow inlen = outlen = strlen(src);
1126007Sthurlow my_iconv(nls_toext, NULL, NULL, &p, &outlen);
1136007Sthurlow my_iconv(nls_toext, &src, &inlen, &p, &outlen);
1146007Sthurlow *p = 0;
115*10023SGordon.Ross@Sun.COM return (dst);
1166007Sthurlow }
1176007Sthurlow
1186007Sthurlow void *
nls_mem_toloc(void * dst,const void * src,int size)1196007Sthurlow nls_mem_toloc(void *dst, const void *src, int size)
1206007Sthurlow {
1216007Sthurlow char *p = dst;
1226007Sthurlow const char *s = src;
1236007Sthurlow size_t inlen, outlen;
1246007Sthurlow
1256007Sthurlow if (!iconv_loaded)
126*10023SGordon.Ross@Sun.COM return (memcpy(dst, src, size));
1276007Sthurlow
1286007Sthurlow if (size == 0)
129*10023SGordon.Ross@Sun.COM return (NULL);
1306007Sthurlow
1316007Sthurlow if (nls_toloc == (iconv_t)0)
132*10023SGordon.Ross@Sun.COM return (memcpy(dst, src, size));
1336007Sthurlow inlen = outlen = size;
1346007Sthurlow my_iconv(nls_toloc, NULL, NULL, &p, &outlen);
1356007Sthurlow my_iconv(nls_toloc, &s, &inlen, &p, &outlen);
136*10023SGordon.Ross@Sun.COM return (dst);
1376007Sthurlow }
1386007Sthurlow
1396007Sthurlow void *
nls_mem_toext(void * dst,const void * src,int size)1406007Sthurlow nls_mem_toext(void *dst, const void *src, int size)
1416007Sthurlow {
1426007Sthurlow char *p = dst;
1436007Sthurlow const char *s = src;
1446007Sthurlow size_t inlen, outlen;
1456007Sthurlow
1466007Sthurlow if (size == 0)
147*10023SGordon.Ross@Sun.COM return (NULL);
1486007Sthurlow
1496007Sthurlow if (!iconv_loaded || nls_toext == (iconv_t)0)
150*10023SGordon.Ross@Sun.COM return (memcpy(dst, src, size));
1516007Sthurlow
1526007Sthurlow inlen = outlen = size;
1536007Sthurlow my_iconv(nls_toext, NULL, NULL, &p, &outlen);
1546007Sthurlow my_iconv(nls_toext, &s, &inlen, &p, &outlen);
155*10023SGordon.Ross@Sun.COM return (dst);
1566007Sthurlow }
1576007Sthurlow
1586007Sthurlow char *
nls_str_upper(char * dst,const char * src)1596007Sthurlow nls_str_upper(char *dst, const char *src)
1606007Sthurlow {
1616007Sthurlow char *p = dst;
1626007Sthurlow
1636007Sthurlow while (*src)
1646007Sthurlow *dst++ = toupper(*src++);
1656007Sthurlow *dst = 0;
166*10023SGordon.Ross@Sun.COM return (p);
1676007Sthurlow }
1686007Sthurlow
1696007Sthurlow char *
nls_str_lower(char * dst,const char * src)1706007Sthurlow nls_str_lower(char *dst, const char *src)
1716007Sthurlow {
1726007Sthurlow char *p = dst;
1736007Sthurlow
1746007Sthurlow while (*src)
1756007Sthurlow *dst++ = tolower(*src++);
1766007Sthurlow *dst = 0;
177*10023SGordon.Ross@Sun.COM return (p);
1786007Sthurlow }
179