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.6 2000/07/23 17:32:16 skimo Exp $ (Berkeley) $Date: 2000/07/23 17:32:16 $";
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
] & 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;
119 CONV default_conv
= { 0, 0, default_char2int
, default_int2char
,
120 default_char2int
, default_int2char
, default_int2disp
};
121 CONV gb_conv
= { 0, 0, default_char2int
, default_int2char
,
122 gb2int
, int2gb
, default_int2disp
};
125 conv_init (SCR
*orig
, SCR
*sp
)
128 sp
->conv
= orig
->conv
;
130 sp
->conv
= &default_conv
;
134 conv_enc (SCR
*sp
, char *enc
)
137 sp
->conv
= &default_conv
;
140 if (!strcmp(enc
,"GB")) {