1 /*- 2 * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved. 3 * Copyright (c) 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Paul Borman at Krystal Technologies. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #if defined(LIBC_SCCS) && !defined(lint) 39 static char sccsid[] = "@(#)euc.c 8.1 (Berkeley) 6/4/93"; 40 #endif /* LIBC_SCCS and not lint */ 41 #include <sys/param.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <errno.h> 45 #include <limits.h> 46 #include <runetype.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <wchar.h> 50 #include "mblocal.h" 51 52 int _EUC_init(_RuneLocale *); 53 size_t _EUC_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t, 54 mbstate_t * __restrict); 55 int _EUC_mbsinit(const mbstate_t *); 56 size_t _EUC_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict); 57 58 typedef struct { 59 int count[4]; 60 wchar_t bits[4]; 61 wchar_t mask; 62 } _EucInfo; 63 64 typedef struct { 65 int count; 66 u_char bytes[MB_LEN_MAX]; 67 } _EucState; 68 69 int 70 _EUC_init(_RuneLocale *rl) 71 { 72 _EucInfo *ei; 73 int x, new__mb_cur_max; 74 char *v, *e; 75 76 if (rl->variable == NULL) 77 return (EFTYPE); 78 79 v = (char *)rl->variable; 80 81 while (*v == ' ' || *v == '\t') 82 ++v; 83 84 if ((ei = malloc(sizeof(_EucInfo))) == NULL) 85 return (errno == 0 ? ENOMEM : errno); 86 87 new__mb_cur_max = 0; 88 for (x = 0; x < 4; ++x) { 89 ei->count[x] = (int)strtol(v, &e, 0); 90 if (v == e || !(v = e)) { 91 free(ei); 92 return (EFTYPE); 93 } 94 if (new__mb_cur_max < ei->count[x]) 95 new__mb_cur_max = ei->count[x]; 96 while (*v == ' ' || *v == '\t') 97 ++v; 98 ei->bits[x] = (int)strtol(v, &e, 0); 99 if (v == e || !(v = e)) { 100 free(ei); 101 return (EFTYPE); 102 } 103 while (*v == ' ' || *v == '\t') 104 ++v; 105 } 106 ei->mask = (int)strtol(v, &e, 0); 107 if (v == e || !(v = e)) { 108 free(ei); 109 return (EFTYPE); 110 } 111 rl->variable = ei; 112 rl->variable_len = sizeof(_EucInfo); 113 _CurrentRuneLocale = rl; 114 __mb_cur_max = new__mb_cur_max; 115 __mbrtowc = _EUC_mbrtowc; 116 __wcrtomb = _EUC_wcrtomb; 117 __mbsinit = _EUC_mbsinit; 118 return (0); 119 } 120 121 int 122 _EUC_mbsinit(const mbstate_t *ps) 123 { 124 125 return (ps == NULL || ((const _EucState *)ps)->count == 0); 126 } 127 128 #define CEI ((_EucInfo *)(_CurrentRuneLocale->variable)) 129 130 #define _SS2 0x008e 131 #define _SS3 0x008f 132 133 #define GR_BITS 0x80808080 /* XXX: to be fixed */ 134 135 static __inline int 136 _euc_set(u_int c) 137 { 138 c &= 0xff; 139 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0); 140 } 141 142 size_t 143 _EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, 144 mbstate_t * __restrict ps) 145 { 146 _EucState *es; 147 int len, ocount, remain, set; 148 wchar_t wc; 149 size_t ncopy; 150 151 es = (_EucState *)ps; 152 153 if (es->count < 0 || es->count > sizeof(es->bytes)) { 154 errno = EINVAL; 155 return ((size_t)-1); 156 } 157 158 if (s == NULL) { 159 s = ""; 160 n = 1; 161 pwc = NULL; 162 } 163 164 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(es->bytes) - es->count); 165 memcpy(es->bytes + es->count, s, ncopy); 166 ocount = es->count; 167 es->count += ncopy; 168 s = (char *)es->bytes; 169 n = es->count; 170 171 if (n == 0 || (size_t)(len = CEI->count[set = _euc_set(*s)]) > n) 172 /* Incomplete multibyte sequence */ 173 return ((size_t)-2); 174 wc = 0; 175 remain = len; 176 switch (set) { 177 case 3: 178 case 2: 179 --remain; 180 ++s; 181 /* FALLTHROUGH */ 182 case 1: 183 case 0: 184 wc = (unsigned char)*s++; 185 while (--remain > 0) { 186 if (*s == '\0') { 187 errno = EILSEQ; 188 return ((size_t)-1); 189 } 190 wc = (wc << 8) | (unsigned char)*s++; 191 } 192 break; 193 } 194 wc = (wc & ~CEI->mask) | CEI->bits[set]; 195 if (pwc != NULL) 196 *pwc = wc; 197 es->count = 0; 198 return (wc == L'\0' ? 0 : len - ocount); 199 } 200 201 size_t 202 _EUC_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps) 203 { 204 _EucState *es; 205 wchar_t m, nm; 206 int i, len; 207 208 es = (_EucState *)ps; 209 210 if (es->count != 0) { 211 errno = EINVAL; 212 return ((size_t)-1); 213 } 214 215 if (s == NULL) 216 /* Reset to initial shift state (no-op) */ 217 return (1); 218 219 m = wc & CEI->mask; 220 nm = wc & ~m; 221 222 if (m == CEI->bits[1]) { 223 CodeSet1: 224 /* Codeset 1: The first byte must have 0x80 in it. */ 225 i = len = CEI->count[1]; 226 while (i-- > 0) 227 *s++ = (nm >> (i << 3)) | 0x80; 228 } else { 229 if (m == CEI->bits[0]) 230 i = len = CEI->count[0]; 231 else if (m == CEI->bits[2]) { 232 i = len = CEI->count[2]; 233 *s++ = _SS2; 234 --i; 235 /* SS2 designates G2 into GR */ 236 nm |= GR_BITS; 237 } else if (m == CEI->bits[3]) { 238 i = len = CEI->count[3]; 239 *s++ = _SS3; 240 --i; 241 /* SS3 designates G3 into GR */ 242 nm |= GR_BITS; 243 } else 244 goto CodeSet1; /* Bletch */ 245 while (i-- > 0) 246 *s++ = (nm >> (i << 3)) & 0xff; 247 } 248 return (len); 249 } 250