xref: /titanic_44/usr/src/lib/libsmbfs/smb/nls.c (revision 8bd1bae7b80c19127d25a15440e161140bcaacea)
1 /*
2  * Copyright (c) 2000-2001, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: nls.c,v 1.10 2004/12/13 00:25:22 lindak Exp $
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/types.h>
38 #include <ctype.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <strings.h>
42 #include <stdlib.h>
43 #include <locale.h>
44 #include <errno.h>
45 
46 #include <netsmb/smb_lib.h>
47 
48 /*
49  * prototype iconv* functions
50  */
51 typedef void *iconv_t;
52 
53 static size_t(*my_iconv)(iconv_t, const char **, size_t *, char **, size_t *);
54 
55 u_char nls_lower[256];
56 u_char nls_upper[256];
57 
58 static iconv_t nls_toext, nls_toloc;
59 static int iconv_loaded;
60 
61 int
62 nls_setlocale(const char *name)
63 {
64 	int i;
65 
66 	if (setlocale(LC_CTYPE, name) == NULL) {
67 		fprintf(stdout, dgettext(TEXT_DOMAIN,
68 		    "can't set locale '%s'\n"), name);
69 	}
70 	for (i = 0; i < 256; i++) {
71 		nls_lower[i] = tolower(i);
72 		nls_upper[i] = toupper(i);
73 	}
74 	return 0;
75 }
76 
77 int
78 nls_setrecode(const char *local, const char *external)
79 {
80 	return ENOENT;
81 }
82 
83 char *
84 nls_str_toloc(char *dst, const char *src)
85 {
86 	char *p = dst;
87 	size_t inlen, outlen;
88 
89 	if (!iconv_loaded)
90 		return strcpy(dst, src);
91 
92 	if (nls_toloc == (iconv_t)0)
93 		return strcpy(dst, src);
94 	inlen = outlen = strlen(src);
95 	my_iconv(nls_toloc, NULL, NULL, &p, &outlen);
96 	my_iconv(nls_toloc, &src, &inlen, &p, &outlen);
97 	*p = 0;
98 	return dst;
99 }
100 
101 char *
102 nls_str_toext(char *dst, const char *src)
103 {
104 	char *p = dst;
105 	size_t inlen, outlen;
106 
107 	if (!iconv_loaded)
108 		return strcpy(dst, src);
109 
110 	if (nls_toext == (iconv_t)0)
111 		return strcpy(dst, src);
112 	inlen = outlen = strlen(src);
113 	my_iconv(nls_toext, NULL, NULL, &p, &outlen);
114 	my_iconv(nls_toext, &src, &inlen, &p, &outlen);
115 	*p = 0;
116 	return dst;
117 }
118 
119 void *
120 nls_mem_toloc(void *dst, const void *src, int size)
121 {
122 	char *p = dst;
123 	const char *s = src;
124 	size_t inlen, outlen;
125 
126 	if (!iconv_loaded)
127 		return memcpy(dst, src, size);
128 
129 	if (size == 0)
130 		return NULL;
131 
132 	if (nls_toloc == (iconv_t)0)
133 		return memcpy(dst, src, size);
134 	inlen = outlen = size;
135 	my_iconv(nls_toloc, NULL, NULL, &p, &outlen);
136 	my_iconv(nls_toloc, &s, &inlen, &p, &outlen);
137 	return dst;
138 }
139 
140 void *
141 nls_mem_toext(void *dst, const void *src, int size)
142 {
143 	char *p = dst;
144 	const char *s = src;
145 	size_t inlen, outlen;
146 
147 	if (size == 0)
148 		return NULL;
149 
150 	if (!iconv_loaded || nls_toext == (iconv_t)0)
151 		return memcpy(dst, src, size);
152 
153 	inlen = outlen = size;
154 	my_iconv(nls_toext, NULL, NULL, &p, &outlen);
155 	my_iconv(nls_toext, &s, &inlen, &p, &outlen);
156 	return dst;
157 }
158 
159 char *
160 nls_str_upper(char *dst, const char *src)
161 {
162 	char *p = dst;
163 
164 	while (*src)
165 		*dst++ = toupper(*src++);
166 	*dst = 0;
167 	return p;
168 }
169 
170 char *
171 nls_str_lower(char *dst, const char *src)
172 {
173 	char *p = dst;
174 
175 	while (*src)
176 		*dst++ = tolower(*src++);
177 	*dst = 0;
178 	return p;
179 }
180