1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #define _DEFAULT_SOURCE
31 /* Make sure we're using fast ctype */
32 #define _PICOLIBC_CTYPE_SMALL 0
33 #include "ctype_.h"
34 #include "../locale/setlocale.h"
35 
36 #define _CTYPE_DATA_0_127 \
37 	_C,	_C,	_C,	_C,	_C,	_C,	_C,	_C, \
38 	_C,	_C|_S, _C|_S, _C|_S,	_C|_S,	_C|_S,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72 #if defined(_MB_CAPABLE)
73 #if defined(_MB_EXTENDED_CHARSETS_ISO)
74 #include "ctype_iso.h"
75 #endif
76 #if defined(_MB_EXTENDED_CHARSETS_WINDOWS)
77 #include "ctype_cp.h"
78 #endif
79 #endif
80 
81 #if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
82 /* No static const on Cygwin since it's referenced and potentially overwritten
83    for compatibility with older applications. */
84 const
85 char _ctype_b[128 + 256] = {
86 	_CTYPE_DATA_128_255,
87 	_CTYPE_DATA_0_127,
88 	_CTYPE_DATA_128_255
89 };
90 
91 #else	/* !ALLOW_NEGATIVE_CTYPE_INDEX */
92 
93 const char _ctype_[1 + 256] = {
94 	0,
95 	_CTYPE_DATA_0_127,
96 	_CTYPE_DATA_128_255
97 };
98 
99 #endif	/* !ALLOW_NEGATIVE_CTYPE_INDEX */
100 
101 #if defined(_MB_CAPABLE)
102 /* Cygwin has its own implementation which additionally maintains backward
103    compatibility with applications built under older Cygwin releases. */
104 void
__set_ctype(struct __locale_t * loc,const char * charset)105 __set_ctype (struct __locale_t *loc, const char *charset)
106 {
107 #if defined(_MB_EXTENDED_CHARSETS_ISO) || defined(_MB_EXTENDED_CHARSETS_WINDOWS)
108   int idx;
109 #endif
110   const char *ctype_ptr = NULL;
111 
112   switch (*charset)
113     {
114 #if defined(_MB_EXTENDED_CHARSETS_ISO)
115     case 'I':
116       idx = __iso_8859_index (charset + 9);
117       /* The ctype table has a leading ISO-8859-1 element so we have to add
118 	 1 to the index returned by __iso_8859_index.  If __iso_8859_index
119 	 returns < 0, it's ISO-8859-1. */
120       if (idx < 0)
121         idx = 0;
122       else
123         ++idx;
124       ctype_ptr = __ctype_iso[idx];
125       break;
126 #endif
127 #if defined(_MB_EXTENDED_CHARSETS_WINDOWS)
128     case 'C':
129       idx = __cp_index (charset + 2);
130       if (idx < 0)
131         break;
132       ctype_ptr = __ctype_cp[idx];
133       break;
134 #endif
135     default:
136       break;
137     }
138   if (!ctype_ptr)
139     {
140 #  if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
141      ctype_ptr = _ctype_b;
142 #  else
143      ctype_ptr = _ctype_;
144 #  endif
145     }
146 #  if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
147   loc->ctype_ptr = ctype_ptr + 127;
148 #  else
149   loc->ctype_ptr = ctype_ptr;
150 #  endif
151 }
152 #endif /* _MB_CAPABLE */
153