1f1b9d127SSheldon Hearn /*
2f1b9d127SSheldon Hearn * Copyright (c) 2000-2001, Boris Popov
3f1b9d127SSheldon Hearn * All rights reserved.
4f1b9d127SSheldon Hearn *
5f1b9d127SSheldon Hearn * Redistribution and use in source and binary forms, with or without
6f1b9d127SSheldon Hearn * modification, are permitted provided that the following conditions
7f1b9d127SSheldon Hearn * are met:
8f1b9d127SSheldon Hearn * 1. Redistributions of source code must retain the above copyright
9f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer.
10f1b9d127SSheldon Hearn * 2. Redistributions in binary form must reproduce the above copyright
11f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer in the
12f1b9d127SSheldon Hearn * documentation and/or other materials provided with the distribution.
13f1b9d127SSheldon Hearn * 3. All advertising materials mentioning features or use of this software
14f1b9d127SSheldon Hearn * must display the following acknowledgement:
15f1b9d127SSheldon Hearn * This product includes software developed by Boris Popov.
16f1b9d127SSheldon Hearn * 4. Neither the name of the author nor the names of any co-contributors
17f1b9d127SSheldon Hearn * may be used to endorse or promote products derived from this software
18f1b9d127SSheldon Hearn * without specific prior written permission.
19f1b9d127SSheldon Hearn *
20f1b9d127SSheldon Hearn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21f1b9d127SSheldon Hearn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22f1b9d127SSheldon Hearn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f1b9d127SSheldon Hearn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24f1b9d127SSheldon Hearn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25f1b9d127SSheldon Hearn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26f1b9d127SSheldon Hearn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27f1b9d127SSheldon Hearn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28f1b9d127SSheldon Hearn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29f1b9d127SSheldon Hearn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30f1b9d127SSheldon Hearn * SUCH DAMAGE.
31f1b9d127SSheldon Hearn *
32b4bd78b0SBoris Popov * $Id: nls.c,v 1.10 2002/07/22 08:33:59 bp Exp $
33f1b9d127SSheldon Hearn */
34f1b9d127SSheldon Hearn
35f1b9d127SSheldon Hearn #include <sys/types.h>
36f1b9d127SSheldon Hearn #include <sys/sysctl.h>
37f1b9d127SSheldon Hearn #include <ctype.h>
38f1b9d127SSheldon Hearn #include <errno.h>
39f1b9d127SSheldon Hearn #include <stdio.h>
40caef65b8SDavid E. O'Brien #include <string.h>
41f1b9d127SSheldon Hearn #include <stdlib.h>
42f1b9d127SSheldon Hearn #include <locale.h>
43f1b9d127SSheldon Hearn #include <err.h>
44f1b9d127SSheldon Hearn #include <netsmb/smb_lib.h>
45f1b9d127SSheldon Hearn
46ce102ac2SGleb Smirnoff #ifdef HAVE_ICONV
47ce102ac2SGleb Smirnoff #include <iconv.h>
48ce102ac2SGleb Smirnoff #endif
49ce102ac2SGleb Smirnoff
50f1b9d127SSheldon Hearn u_char nls_lower[256];
51f1b9d127SSheldon Hearn u_char nls_upper[256];
52f1b9d127SSheldon Hearn
53ce102ac2SGleb Smirnoff #ifdef HAVE_ICONV
54f1b9d127SSheldon Hearn static iconv_t nls_toext, nls_toloc;
55ce102ac2SGleb Smirnoff #endif
56f1b9d127SSheldon Hearn
57f1b9d127SSheldon Hearn int
nls_setlocale(const char * name)58f1b9d127SSheldon Hearn nls_setlocale(const char *name)
59f1b9d127SSheldon Hearn {
60f1b9d127SSheldon Hearn int i;
61f1b9d127SSheldon Hearn
62f1b9d127SSheldon Hearn if (setlocale(LC_CTYPE, name) == NULL) {
63f1b9d127SSheldon Hearn warnx("can't set locale '%s'\n", name);
64f1b9d127SSheldon Hearn return EINVAL;
65f1b9d127SSheldon Hearn }
66f1b9d127SSheldon Hearn for (i = 0; i < 256; i++) {
67f1b9d127SSheldon Hearn nls_lower[i] = tolower(i);
68f1b9d127SSheldon Hearn nls_upper[i] = toupper(i);
69f1b9d127SSheldon Hearn }
70f1b9d127SSheldon Hearn return 0;
71f1b9d127SSheldon Hearn }
72f1b9d127SSheldon Hearn
73f1b9d127SSheldon Hearn int
nls_setrecode(const char * local,const char * external)74f1b9d127SSheldon Hearn nls_setrecode(const char *local, const char *external)
75f1b9d127SSheldon Hearn {
76ce102ac2SGleb Smirnoff #ifdef HAVE_ICONV
77f1b9d127SSheldon Hearn iconv_t icd;
78f1b9d127SSheldon Hearn
79f1b9d127SSheldon Hearn if (nls_toext)
8009601309SGleb Smirnoff iconv_close(nls_toext);
81f1b9d127SSheldon Hearn if (nls_toloc)
8209601309SGleb Smirnoff iconv_close(nls_toloc);
83f1b9d127SSheldon Hearn nls_toext = nls_toloc = (iconv_t)0;
8409601309SGleb Smirnoff icd = iconv_open(external, local);
85f1b9d127SSheldon Hearn if (icd == (iconv_t)-1)
86f1b9d127SSheldon Hearn return errno;
87f1b9d127SSheldon Hearn nls_toext = icd;
8809601309SGleb Smirnoff icd = iconv_open(local, external);
89f1b9d127SSheldon Hearn if (icd == (iconv_t)-1) {
9009601309SGleb Smirnoff iconv_close(nls_toext);
91f1b9d127SSheldon Hearn nls_toext = (iconv_t)0;
92f1b9d127SSheldon Hearn return errno;
93f1b9d127SSheldon Hearn }
94f1b9d127SSheldon Hearn nls_toloc = icd;
95f1b9d127SSheldon Hearn return 0;
96ce102ac2SGleb Smirnoff #else
97ce102ac2SGleb Smirnoff return ENOENT;
98df3342d6SSheldon Hearn #endif
99f1b9d127SSheldon Hearn }
100f1b9d127SSheldon Hearn
101f1b9d127SSheldon Hearn char *
nls_str_toloc(char * dst,char * src)102*1243a98eSTijl Coosemans nls_str_toloc(char *dst, char *src)
103f1b9d127SSheldon Hearn {
104ce102ac2SGleb Smirnoff #ifdef HAVE_ICONV
105f1b9d127SSheldon Hearn char *p = dst;
106caef65b8SDavid E. O'Brien size_t inlen, outlen;
107f1b9d127SSheldon Hearn
108f1b9d127SSheldon Hearn if (nls_toloc == (iconv_t)0)
109f1b9d127SSheldon Hearn return strcpy(dst, src);
110f1b9d127SSheldon Hearn inlen = outlen = strlen(src);
11109601309SGleb Smirnoff iconv(nls_toloc, NULL, NULL, &p, &outlen);
11209601309SGleb Smirnoff while (iconv(nls_toloc, &src, &inlen, &p, &outlen) == -1) {
113b4bd78b0SBoris Popov *p++ = *src++;
114b4bd78b0SBoris Popov inlen--;
115b4bd78b0SBoris Popov outlen--;
116b4bd78b0SBoris Popov }
117f1b9d127SSheldon Hearn *p = 0;
118f1b9d127SSheldon Hearn return dst;
119ce102ac2SGleb Smirnoff #else
120ce102ac2SGleb Smirnoff return strcpy(dst, src);
121ce102ac2SGleb Smirnoff #endif
122f1b9d127SSheldon Hearn }
123f1b9d127SSheldon Hearn
124f1b9d127SSheldon Hearn char *
nls_str_toext(char * dst,char * src)125*1243a98eSTijl Coosemans nls_str_toext(char *dst, char *src)
126f1b9d127SSheldon Hearn {
127ce102ac2SGleb Smirnoff #ifdef HAVE_ICONV
128f1b9d127SSheldon Hearn char *p = dst;
129caef65b8SDavid E. O'Brien size_t inlen, outlen;
130f1b9d127SSheldon Hearn
131f1b9d127SSheldon Hearn if (nls_toext == (iconv_t)0)
132f1b9d127SSheldon Hearn return strcpy(dst, src);
133f1b9d127SSheldon Hearn inlen = outlen = strlen(src);
13409601309SGleb Smirnoff iconv(nls_toext, NULL, NULL, &p, &outlen);
13509601309SGleb Smirnoff while (iconv(nls_toext, &src, &inlen, &p, &outlen) == -1) {
136b4bd78b0SBoris Popov *p++ = *src++;
137b4bd78b0SBoris Popov inlen--;
138b4bd78b0SBoris Popov outlen--;
139b4bd78b0SBoris Popov }
140f1b9d127SSheldon Hearn *p = 0;
141f1b9d127SSheldon Hearn return dst;
142ce102ac2SGleb Smirnoff #else
143ce102ac2SGleb Smirnoff return strcpy(dst, src);
144ce102ac2SGleb Smirnoff #endif
145f1b9d127SSheldon Hearn }
146f1b9d127SSheldon Hearn
147f1b9d127SSheldon Hearn void *
nls_mem_toloc(void * dst,void * src,int size)148*1243a98eSTijl Coosemans nls_mem_toloc(void *dst, void *src, int size)
149f1b9d127SSheldon Hearn {
150ce102ac2SGleb Smirnoff #ifdef HAVE_ICONV
151f1b9d127SSheldon Hearn char *p = dst;
152*1243a98eSTijl Coosemans char *s = src;
153caef65b8SDavid E. O'Brien size_t inlen, outlen;
154f1b9d127SSheldon Hearn
155f1b9d127SSheldon Hearn if (size == 0)
156f1b9d127SSheldon Hearn return NULL;
157f1b9d127SSheldon Hearn
158f1b9d127SSheldon Hearn if (nls_toloc == (iconv_t)0)
159f1b9d127SSheldon Hearn return memcpy(dst, src, size);
160f1b9d127SSheldon Hearn inlen = outlen = size;
16109601309SGleb Smirnoff iconv(nls_toloc, NULL, NULL, &p, &outlen);
16209601309SGleb Smirnoff while (iconv(nls_toloc, &s, &inlen, &p, &outlen) == -1) {
163b4bd78b0SBoris Popov *p++ = *s++;
164b4bd78b0SBoris Popov inlen--;
165b4bd78b0SBoris Popov outlen--;
166b4bd78b0SBoris Popov }
167f1b9d127SSheldon Hearn return dst;
168ce102ac2SGleb Smirnoff #else
169ce102ac2SGleb Smirnoff return memcpy(dst, src, size);
170ce102ac2SGleb Smirnoff #endif
171f1b9d127SSheldon Hearn }
172f1b9d127SSheldon Hearn
173f1b9d127SSheldon Hearn void *
nls_mem_toext(void * dst,void * src,int size)174*1243a98eSTijl Coosemans nls_mem_toext(void *dst, void *src, int size)
175f1b9d127SSheldon Hearn {
176ce102ac2SGleb Smirnoff #ifdef HAVE_ICONV
177f1b9d127SSheldon Hearn char *p = dst;
178*1243a98eSTijl Coosemans char *s = src;
179caef65b8SDavid E. O'Brien size_t inlen, outlen;
180f1b9d127SSheldon Hearn
181f1b9d127SSheldon Hearn if (size == 0)
182f1b9d127SSheldon Hearn return NULL;
183f1b9d127SSheldon Hearn
18409601309SGleb Smirnoff if (nls_toext == (iconv_t)0)
185f1b9d127SSheldon Hearn return memcpy(dst, src, size);
186f1b9d127SSheldon Hearn
187f1b9d127SSheldon Hearn inlen = outlen = size;
18809601309SGleb Smirnoff iconv(nls_toext, NULL, NULL, &p, &outlen);
18909601309SGleb Smirnoff while (iconv(nls_toext, &s, &inlen, &p, &outlen) == -1) {
190b4bd78b0SBoris Popov *p++ = *s++;
191b4bd78b0SBoris Popov inlen--;
192b4bd78b0SBoris Popov outlen--;
193b4bd78b0SBoris Popov }
194f1b9d127SSheldon Hearn return dst;
195ce102ac2SGleb Smirnoff #else
196ce102ac2SGleb Smirnoff return memcpy(dst, src, size);
197ce102ac2SGleb Smirnoff #endif
198f1b9d127SSheldon Hearn }
199f1b9d127SSheldon Hearn
200f1b9d127SSheldon Hearn char *
nls_str_upper(char * dst,const char * src)201f1b9d127SSheldon Hearn nls_str_upper(char *dst, const char *src)
202f1b9d127SSheldon Hearn {
203f1b9d127SSheldon Hearn char *p = dst;
204f1b9d127SSheldon Hearn
205f1b9d127SSheldon Hearn while (*src)
206f1b9d127SSheldon Hearn *dst++ = toupper(*src++);
207f1b9d127SSheldon Hearn *dst = 0;
208f1b9d127SSheldon Hearn return p;
209f1b9d127SSheldon Hearn }
210f1b9d127SSheldon Hearn
211f1b9d127SSheldon Hearn char *
nls_str_lower(char * dst,const char * src)212f1b9d127SSheldon Hearn nls_str_lower(char *dst, const char *src)
213f1b9d127SSheldon Hearn {
214f1b9d127SSheldon Hearn char *p = dst;
215f1b9d127SSheldon Hearn
216f1b9d127SSheldon Hearn while (*src)
217f1b9d127SSheldon Hearn *dst++ = tolower(*src++);
218f1b9d127SSheldon Hearn *dst = 0;
219f1b9d127SSheldon Hearn return p;
220f1b9d127SSheldon Hearn }
221