2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: conv.c,v 1.7 2000/08/27 17:15:04 skimo Exp $ (Berkeley) $Date: 2000/08/27 17:15:04 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
31 default_char2int(CONV
*conv
, const char * str
, ssize_t len
, CHAR_T
**tostr
, size_t *tolen
, size_t *blen
)
35 BINC_RETW(NULL
, *tostr
, *blen
, len
);
38 for (i
= 0; i
< len
; ++i
)
39 (*tostr
)[i
] = (u_char
) str
[i
];
45 default_int2char(CONV
*conv
, const CHAR_T
* str
, ssize_t len
, char **tostr
, size_t *tolen
, size_t *blen
)
49 BINC_RET(NULL
, *tostr
, *blen
, len
);
52 for (i
= 0; i
< len
; ++i
)
59 default_int2disp(CONV
*conv
, const CHAR_T
* str
, ssize_t len
, char **tostr
, size_t *tolen
, size_t *blen
)
63 BINC_RET(NULL
, *tostr
, *blen
, len
* 2);
65 for (i
= 0, j
= 0; i
< len
; ++i
)
66 if (CHAR_WIDTH(NULL
, str
[i
]) > 1) {
70 (*tostr
)[j
++] = str
[i
];
77 gb2int(CONV
*conv
, const char * str
, ssize_t len
, CHAR_T
**tostr
, size_t *tolen
, size_t *blen
)
81 BINC_RETW(NULL
, *tostr
, *blen
, len
);
83 for (i
= 0, j
= 0; i
< len
; ++i
) {
85 if (i
+1 < len
&& str
[i
+1] & 0x80) {
86 (*tostr
)[j
++] = INT9494(F_GB
,str
[i
]&0x7F,str
[i
+1]&0x7F);
89 (*tostr
)[j
++] = INTILL(str
[i
]);
92 (*tostr
)[j
++] = str
[i
];
100 int2gb(CONV
*conv
, const CHAR_T
* str
, ssize_t len
, char **tostr
, size_t *tolen
, size_t *blen
)
104 BINC_RET(NULL
, *tostr
, *blen
, len
* 2);
106 for (i
= 0, j
= 0; i
< len
; ++i
) {
107 if (INTIS9494(str
[i
])) {
108 (*tostr
)[j
++] = INT9494R(str
[i
]) | 0x80;
109 (*tostr
)[j
++] = INT9494C(str
[i
]) | 0x80;
111 (*tostr
)[j
++] = str
[i
] & 0xFF;
120 utf82int(CONV
*conv
, const char * str
, ssize_t len
, CHAR_T
**tostr
, size_t *tolen
, size_t *blen
)
125 BINC_RETW(NULL
, *tostr
, *blen
, len
);
127 for (i
= 0, j
= 0; i
< len
; ++i
) {
129 if ((str
[i
] & 0xe0) == 0xc0 && i
+1 < len
&& str
[i
+1] & 0x80) {
130 c
= (str
[i
] & 0x1f) << 6;
131 c
|= (str
[i
+1] & 0x3f);
134 } else if ((str
[i
] & 0xf0) == 0xe0 && i
+2 < len
&&
135 str
[i
+1] & 0x80 && str
[i
+2] & 0x80) {
136 c
= (str
[i
] & 0xf) << 12;
137 c
|= (str
[i
+1] & 0x3f) << 6;
138 c
|= (str
[i
+2] & 0x3f);
142 (*tostr
)[j
++] = INTILL(str
[i
]);
145 (*tostr
)[j
++] = str
[i
];
153 int2utf8(CONV
*conv
, const CHAR_T
* str
, ssize_t len
, char **tostr
, size_t *tolen
, size_t *blen
)
155 BINC_RET(NULL
, *tostr
, *blen
, len
* 3);
157 *tolen
= ucs2utf8(str
, len
, *tostr
);
163 CONV default_conv
= { 0, 0, default_char2int
, default_int2char
,
164 default_char2int
, default_int2char
, default_int2disp
};
165 CONV gb_conv
= { 0, 0, default_char2int
, default_int2char
,
166 gb2int
, int2gb
, default_int2disp
};
167 CONV utf8_conv
= { 0, 0, default_char2int
, default_int2char
,
168 utf82int
, int2utf8
, default_int2disp
};
171 conv_init (SCR
*orig
, SCR
*sp
)
174 sp
->conv
= orig
->conv
;
176 sp
->conv
= &default_conv
;
180 conv_enc (SCR
*sp
, char *enc
)
183 sp
->conv
= &default_conv
;
186 if (!strcmp(enc
,"GB")) {
190 if (!strcmp(enc
,"UTF-8")) {
191 sp
->conv
= &utf8_conv
;