1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1990 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#if !defined(lint) && defined(SCCSIDS)
static char *sccsid = "%Z%%M% %I% %E% SMI";
#endif
#include <stdio.h>
#include <sys/types.h>
#define CS377 0377
#define MASK 0x0000ffff
#define TOP1 0x80000000
#define TOP2 0x08000000
/*
* mbtowc routines for the Xerox XCCS codeset standard
*/
int
_mbtowc_xccs(pwc, s, n)
wchar_t *pwc;
char *s;
int n;
{
static unsigned int CSselect = 0;
static int CSlength = 1;
wchar_t twchar = 0;
/*
* If length is negative, return error
*/
if (n <= 0)
return (-1);
/*
* End of string ?
*/
if (*s == 0 && CSlength == 1)
return (0);
if (*s == 0 && *(s + 1) == 0 && CSlength == 2)
return (0);
/*
* Get a character
*/
if ((unsigned char)*s == CS377) {
/*
* Switching code set
*/
++s;
/*
* Change characteristics
*/
if ((unsigned char)*s == CS377) {
++s;
/*
* two byte sequence
*/
if (*s++ != 0)
return (-1);
CSselect = 0;
CSlength = 2;
}
else {
/*
* Change CSselect
*/
CSselect = (unsigned int)*s++;
CSlength = 1;
}
}
/*
* Get a character and return
*/
if (CSlength == 1) {
twchar = CSselect;
}
else {
twchar = *s++;
}
twchar = twchar << 8;
twchar = twchar | *s;
if (pwc)
*pwc = twchar & MASK;
/*
* Encode additional information
*/
if (CSlength == 2)
if (pwc)
*pwc |= TOP1;
return (CSlength);
}
/*
* wctomb routines
*/
int
_wctomb_xccs(s, pwc)
char *s;
wchar_t pwc;
{
unsigned char upper, lower;
char *old = s;
#ifdef DEBUG
printf ("XCCS- xctomb\n");
#endif
if (!s)
return (0);
/*
* Get lower and upper anyway
*/
lower = pwc & 0x00ff;
upper = (pwc >> 8) & 0x00ff;
if (lower == CS377 || upper == CS377)
return (-1);
if (pwc & TOP1) { /* length == 2 */
/*
* This was the marker.
* Emitt 3 additional characters.
*/
*s++ = CS377;
*s++ = CS377;
*s++ = 0;
*s++ = upper;
*s++ = lower;
}
else {
/*
* This was the marker.
* Emitt 2 additional characters.
*/
*s++ = CS377;
*s++ = upper;
*s++ = lower;
}
return (s - old);
}
/*
* mbstowcs routines
*/
size_t
_mbstowcs_xccs(pwc, s, n)
wchar_t *pwc;
char *s;
int n;
{
static unsigned int CSselect = 0;
static int CSlength = 1;
wchar_t twchar = 0;
int cnt = 0;
/*
* If length is negative, return error
*/
if (n <= 0)
return (-1);
/*
* End of string ?
*/
if (*s == 0 && CSlength == 1)
return (0);
if (*s == 0 && *(s + 1) == 0 && CSlength == 2)
return (0);
do {
/*
* Check for an end of the string
*/
if (((*s == 0 && CSlength == 1)) ||
((*s == 0 && *(s + 1) == 0 && CSlength == 2))) {
*pwc = 0;
++cnt;
--n;
break;
}
/*
* Get a character
*/
if ((unsigned char)*s == CS377) {
++s;
/*
* Change characterristics
*/
if ((unsigned char)*s == CS377) {
++s;
/*
* two byte sequence
*/
if (*s++ != 0)
return (-1);
CSselect = 0;
CSlength = 2;
}
else {
/*
* Change CSselect
*/
CSselect = (unsigned int)*s++;
CSlength = 1;
}
}
/*
* Get a character and return
*/
if (CSlength == 1) {
twchar = CSselect;
}
else {
twchar = *s++;
}
twchar = twchar << 8;
twchar = twchar | *s++;
*pwc = twchar & MASK;
if (CSlength == 2)
*pwc |= TOP1;
++pwc;
++cnt;
--n;
} while (n >= 0);
return (cnt);
}
/*
* wcstombs routines
*/
size_t
_wcstombs_xccs(s, pwc, n)
char *s;
wchar_t *pwc;
int n;
{
int cnt = 0;
unsigned char lower, upper;
int in_2byte = 0;
int in_1byte = 0;
int current = 0;
if (n <= 0)
return (-1);
if (*pwc == 0)
return (0);
do {
lower = *pwc & 0x00ff;
upper = (*pwc >> 8) & 0x00ff;
/*
* End of string ?
*/
if (lower == 0) {
*s++ = 0;
++cnt;
--n;
if (n == 0)
break;
*s++ = 0;
++cnt;
break;
}
if (lower == CS377 || upper == CS377)
return (-1);
if (*pwc & TOP1) { /* length == 2 */
if (in_2byte == 0) {
/*
* This was the marker.
* Emitt 3 additional characters.
*/
*s++ = CS377; ++cnt; --n;
*s++ = CS377; ++cnt; --n;
*s++ = 0; ++cnt; --n;
in_2byte = 1;
in_1byte = 0;
}
*s++ = upper; ++cnt; --n;
if (n == 0)
break;
*s++ = lower; ++cnt; --n;
if (n == 0)
break;
}
else {
if ((in_1byte == 0 && in_2byte == 1) ||
(in_1byte == 1 && upper != current) ||
(in_1byte == 0 && in_2byte == 0 && upper != 0)) {
/*
* This was the marker.
* Emitt 2 additional characters.
*/
*s++ = CS377; ++cnt; --n;
if (n == 0)
break;
*s++ = upper; ++cnt; --n;
if (n == 0)
break;
in_2byte = 0;
in_1byte = 1;
current = upper;
}
*s++ = lower; ++cnt; --n;
if (n == 0)
break;
}
++pwc;
} while (n >= 0);
return (cnt);
}
|