euc.c (122283) | euc.c (128004) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 2002, 2003 Tim J. Robbins. All rights reserved. | 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 --- 22 unchanged lines hidden (view full) --- 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) 39static char sccsid[] = "@(#)euc.c 8.1 (Berkeley) 6/4/93"; 40#endif /* LIBC_SCCS and not lint */ | 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 --- 22 unchanged lines hidden (view full) --- 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) 39static char sccsid[] = "@(#)euc.c 8.1 (Berkeley) 6/4/93"; 40#endif /* LIBC_SCCS and not lint */ |
41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/lib/libc/locale/euc.c 122283 2003-11-08 02:58:37Z tjr $"); | 41#include <sys/param.h> 42__FBSDID("$FreeBSD: head/lib/libc/locale/euc.c 128004 2004-04-07 10:48:19Z tjr $"); |
43 | 43 |
44#include <sys/types.h> 45 | |
46#include <errno.h> | 44#include <errno.h> |
45#include <limits.h> |
|
47#include <runetype.h> 48#include <stdlib.h> | 46#include <runetype.h> 47#include <stdlib.h> |
48#include <string.h> |
|
49#include <wchar.h> 50 51extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict, 52 size_t, mbstate_t * __restrict); | 49#include <wchar.h> 50 51extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict, 52 size_t, mbstate_t * __restrict); |
53extern int (*__mbsinit)(const mbstate_t *); |
|
53extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict); 54 55int _EUC_init(_RuneLocale *); 56size_t _EUC_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t, 57 mbstate_t * __restrict); | 54extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict); 55 56int _EUC_init(_RuneLocale *); 57size_t _EUC_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t, 58 mbstate_t * __restrict); |
59int _EUC_mbsinit(const mbstate_t *); |
|
58size_t _EUC_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict); 59 60typedef struct { 61 int count[4]; 62 wchar_t bits[4]; 63 wchar_t mask; 64} _EucInfo; 65 | 60size_t _EUC_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict); 61 62typedef struct { 63 int count[4]; 64 wchar_t bits[4]; 65 wchar_t mask; 66} _EucInfo; 67 |
68typedef struct { 69 int count; 70 u_char bytes[MB_LEN_MAX]; 71} _EucState; 72 |
|
66int 67_EUC_init(_RuneLocale *rl) 68{ 69 _EucInfo *ei; 70 int x, new__mb_cur_max; 71 char *v, *e; 72 73 if (rl->variable == NULL) --- 32 unchanged lines hidden (view full) --- 106 return (EFTYPE); 107 } 108 rl->variable = ei; 109 rl->variable_len = sizeof(_EucInfo); 110 _CurrentRuneLocale = rl; 111 __mb_cur_max = new__mb_cur_max; 112 __mbrtowc = _EUC_mbrtowc; 113 __wcrtomb = _EUC_wcrtomb; | 73int 74_EUC_init(_RuneLocale *rl) 75{ 76 _EucInfo *ei; 77 int x, new__mb_cur_max; 78 char *v, *e; 79 80 if (rl->variable == NULL) --- 32 unchanged lines hidden (view full) --- 113 return (EFTYPE); 114 } 115 rl->variable = ei; 116 rl->variable_len = sizeof(_EucInfo); 117 _CurrentRuneLocale = rl; 118 __mb_cur_max = new__mb_cur_max; 119 __mbrtowc = _EUC_mbrtowc; 120 __wcrtomb = _EUC_wcrtomb; |
121 __mbsinit = _EUC_mbsinit; |
|
114 return (0); 115} 116 | 122 return (0); 123} 124 |
125int 126_EUC_mbsinit(const mbstate_t *ps) 127{ 128 129 return (ps == NULL || ((_EucState *)ps)->count == 0); 130} 131 |
|
117#define CEI ((_EucInfo *)(_CurrentRuneLocale->variable)) 118 119#define _SS2 0x008e 120#define _SS3 0x008f 121 122#define GR_BITS 0x80808080 /* XXX: to be fixed */ 123 124static __inline int 125_euc_set(u_int c) 126{ 127 c &= 0xff; 128 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0); 129} 130 131size_t 132_EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, | 132#define CEI ((_EucInfo *)(_CurrentRuneLocale->variable)) 133 134#define _SS2 0x008e 135#define _SS3 0x008f 136 137#define GR_BITS 0x80808080 /* XXX: to be fixed */ 138 139static __inline int 140_euc_set(u_int c) 141{ 142 c &= 0xff; 143 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0); 144} 145 146size_t 147_EUC_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, |
133 mbstate_t * __restrict ps __unused) | 148 mbstate_t * __restrict ps) |
134{ | 149{ |
135 int len, remain, set; | 150 _EucState *es; 151 int len, ocount, remain, set; |
136 wchar_t wc; | 152 wchar_t wc; |
153 size_t ncopy; |
|
137 | 154 |
138 if (s == NULL) 139 /* Reset to initial shift state (no-op) */ 140 return (0); | 155 es = (_EucState *)ps; 156 157 if (s == NULL) { 158 s = ""; 159 n = 1; 160 pwc = NULL; 161 } 162 163 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(es->bytes) - es->count); 164 memcpy(es->bytes + es->count, s, ncopy); 165 ocount = es->count; 166 es->count += ncopy; 167 s = (char *)es->bytes; 168 n = es->count; 169 |
141 if (n == 0 || (size_t)(len = CEI->count[set = _euc_set(*s)]) > n) 142 /* Incomplete multibyte sequence */ 143 return ((size_t)-2); 144 wc = 0; 145 remain = len; 146 switch (set) { 147 case 3: 148 case 2: --- 4 unchanged lines hidden (view full) --- 153 case 0: 154 while (remain-- > 0) 155 wc = (wc << 8) | (unsigned char)*s++; 156 break; 157 } 158 wc = (wc & ~CEI->mask) | CEI->bits[set]; 159 if (pwc != NULL) 160 *pwc = wc; | 170 if (n == 0 || (size_t)(len = CEI->count[set = _euc_set(*s)]) > n) 171 /* Incomplete multibyte sequence */ 172 return ((size_t)-2); 173 wc = 0; 174 remain = len; 175 switch (set) { 176 case 3: 177 case 2: --- 4 unchanged lines hidden (view full) --- 182 case 0: 183 while (remain-- > 0) 184 wc = (wc << 8) | (unsigned char)*s++; 185 break; 186 } 187 wc = (wc & ~CEI->mask) | CEI->bits[set]; 188 if (pwc != NULL) 189 *pwc = wc; |
161 return (wc == L'\0' ? 0 : len); | 190 es->count = 0; 191 return (wc == L'\0' ? 0 : len - ocount); |
162} 163 164size_t 165_EUC_wcrtomb(char * __restrict s, wchar_t wc, 166 mbstate_t * __restrict ps __unused) 167{ 168 wchar_t m, nm; 169 int i, len; --- 36 unchanged lines hidden --- | 192} 193 194size_t 195_EUC_wcrtomb(char * __restrict s, wchar_t wc, 196 mbstate_t * __restrict ps __unused) 197{ 198 wchar_t m, nm; 199 int i, len; --- 36 unchanged lines hidden --- |