1 /*
2 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Paul Borman at Krystal Technologies.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "lint.h"
36 #include "file64.h"
37 #include <errno.h>
38 #include <limits.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <wchar.h>
44 #include "runetype.h"
45 #include "ldpart.h"
46 #include "mblocal.h"
47 #include "setlocale.h"
48 #include "_ctype.h"
49 #include "lctype.h"
50 #include "localeimpl.h"
51
52 extern _RuneLocale *_Read_RuneMagi(const char *);
53
54 struct lc_ctype lc_ctype_posix = {
55 .lc_mbrtowc = __mbrtowc_ascii,
56 .lc_mbsinit = __mbsinit_ascii,
57 .lc_mbsnrtowcs = __mbsnrtowcs_ascii,
58 .lc_wcrtomb = __wcrtomb_ascii,
59 .lc_wcsnrtombs = __wcsnrtombs_ascii,
60 .lc_is_ascii = 1,
61 .lc_max_mblen = 1,
62 .lc_trans_upper = _DefaultRuneLocale.__mapupper,
63 .lc_trans_lower = _DefaultRuneLocale.__maplower,
64 .lc_ctype_mask = _DefaultRuneLocale.__runetype,
65 };
66
67 struct locdata __posix_ctype_locdata = {
68 .l_lname = "C",
69 .l_refcnt = (uint32_t)-1,
70 .l_data = { &lc_ctype_posix, &_DefaultRuneLocale }
71 };
72
73
74 /*
75 * Table of initializers for encodings. When you add a new encoding type,
76 * this table should be updated.
77 */
78 static struct {
79 const char *e_name;
80 void (*e_init)(struct lc_ctype *);
81 } encodings[] = {
82 { "NONE", _none_init },
83 { "UTF-8", _UTF8_init },
84 { "EUC-CN", _EUC_CN_init },
85 { "EUC-JP", _EUC_JP_init },
86 { "EUC-KR", _EUC_KR_init },
87 { "EUC-TW", _EUC_TW_init },
88 { "GB18030", _GB18030_init },
89 { "GB2312", _GB2312_init },
90 { "GBK", _GBK_init },
91 { "BIG5", _BIG5_init },
92 { "MSKanji", _MSKanji_init },
93 { NULL, NULL }
94 };
95
96
97 struct locdata *
98 __lc_ctype_load(const char *name)
99 {
100 struct locdata *ldata;
101 struct lc_ctype *lct;
102 _RuneLocale *rl;
103 int i;
104 char path[PATH_MAX];
105
106 if ((ldata = __locdata_alloc(name, sizeof (*lct))) == NULL)
107 return (NULL);
108 lct = ldata->l_data[0];
109 /*
110 * Slurp the locale file into the cache.
111 */
112
113 (void) snprintf(path, sizeof (path), "%s/%s/LC_CTYPE/LCL_DATA",
114 _PathLocale, name);
115
116 if ((rl = _Read_RuneMagi(path)) == NULL) {
117 __locdata_release(ldata);
118 errno = EINVAL;
119 return (NULL);
120 }
121 ldata->l_data[1] = rl;
122
123 lct->lc_mbrtowc = NULL;
124 lct->lc_mbsinit = NULL;
125 lct->lc_mbsnrtowcs = NULL;
126 lct->lc_wcrtomb = NULL;
127 lct->lc_wcsnrtombs = NULL;
128 lct->lc_ctype_mask = rl->__runetype;
129 lct->lc_trans_upper = rl->__mapupper;
130 lct->lc_trans_lower = rl->__maplower;
131
132 /* set up the function pointers */
133 for (i = 0; encodings[i].e_name != NULL; i++) {
134 int l = strlen(encodings[i].e_name);
135 if ((strncmp(rl->__encoding, encodings[i].e_name, l) == 0) &&
136 (rl->__encoding[l] == '\0' || rl->__encoding[l] == '@')) {
137 encodings[i].e_init(lct);
138 break;
139 }
140 }
141 if (encodings[i].e_name == NULL) {
142 __locdata_release(ldata);
143 errno = EINVAL;
144 return (NULL);
145 }
146
147
148 return (ldata);
149 }