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 *
32df3342d6SSheldon Hearn * $Id: kiconv.c,v 1.3 2001/08/22 03:31:36 bp Exp $
33db750196SPeter Wemm * $FreeBSD$
34f1b9d127SSheldon Hearn */
35f1b9d127SSheldon Hearn
36f1b9d127SSheldon Hearn #include <sys/types.h>
37f1b9d127SSheldon Hearn #include <sys/iconv.h>
38f1b9d127SSheldon Hearn #include <sys/sysctl.h>
39f1b9d127SSheldon Hearn #include <ctype.h>
40f1b9d127SSheldon Hearn #include <errno.h>
411ac62e0bSDavid E. O'Brien #include <string.h>
42f1b9d127SSheldon Hearn
43df3342d6SSheldon Hearn #ifdef APPLE
44df3342d6SSheldon Hearn #include <sys/types.h>
45df3342d6SSheldon Hearn extern uid_t real_uid, eff_uid;
46df3342d6SSheldon Hearn #endif
47df3342d6SSheldon Hearn
48f1b9d127SSheldon Hearn int
kiconv_add_xlat_table(const char * to,const char * from,const u_char * table)49f1b9d127SSheldon Hearn kiconv_add_xlat_table(const char *to, const char *from, const u_char *table)
50f1b9d127SSheldon Hearn {
51f1b9d127SSheldon Hearn struct iconv_add_in din;
52f1b9d127SSheldon Hearn struct iconv_add_out dout;
53db750196SPeter Wemm size_t olen;
54f1b9d127SSheldon Hearn
55f373a824SR. Imura if (strlen(from) >= ICONV_CSNMAXLEN || strlen(to) >= ICONV_CSNMAXLEN)
56f1b9d127SSheldon Hearn return EINVAL;
57f1b9d127SSheldon Hearn din.ia_version = ICONV_ADD_VER;
58f1b9d127SSheldon Hearn strcpy(din.ia_converter, "xlat");
59f1b9d127SSheldon Hearn strcpy(din.ia_from, from);
60f1b9d127SSheldon Hearn strcpy(din.ia_to, to);
61f1b9d127SSheldon Hearn din.ia_data = table;
62f1b9d127SSheldon Hearn din.ia_datalen = 256;
63f1b9d127SSheldon Hearn olen = sizeof(dout);
64df3342d6SSheldon Hearn #ifdef APPLE
65df3342d6SSheldon Hearn seteuid(eff_uid); /* restore setuid root briefly */
66df3342d6SSheldon Hearn if (sysctlbyname("net.smb.fs.iconv.add", &dout, &olen, &din, sizeof(din)) == -1) {
67df3342d6SSheldon Hearn seteuid(real_uid); /* and back to real user */
68df3342d6SSheldon Hearn return errno;
69df3342d6SSheldon Hearn }
70df3342d6SSheldon Hearn seteuid(real_uid); /* and back to real user */
71df3342d6SSheldon Hearn #else
72f1b9d127SSheldon Hearn if (sysctlbyname("kern.iconv.add", &dout, &olen, &din, sizeof(din)) == -1)
73f1b9d127SSheldon Hearn return errno;
74df3342d6SSheldon Hearn #endif
75f1b9d127SSheldon Hearn return 0;
76f1b9d127SSheldon Hearn }
77f1b9d127SSheldon Hearn
78