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