-
Notifications
You must be signed in to change notification settings - Fork 0
/
johabcode.c
163 lines (144 loc) · 3.81 KB
/
johabcode.c
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
/* This file is a part of Ami software, (C) Hwang chi-deok 2001 */
/*
일반 완성형 한글
0xb0a1 => 0x8861
...
0xc8fe => 0xd3b7
자모
0xa4a1 => 0x
0xa4fe => 0x
특수문자
0xa1a1 => 0xd931
...
0xacf1 => 0xdef1
*/
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#if defined(__sun__)
#define CD_EUC_KR "ko_KR-euc"
#define CD_JOHAB "ko_KR-johap92"
#define CD_UTF8 "ko_KR-UTF-8"
#else
#define CD_EUC_KR "EUC-KR"
#define CD_JOHAB "JOHAB"
#define CD_UTF8 "UTF-8"
#endif
static iconv_t johab_to_wan_cd = (iconv_t) -1;
static iconv_t wan_to_johab_cd = (iconv_t) -1;
static iconv_t johab_to_utf8_cd = (iconv_t) -1;
int johab_to_wansung(char *s, int johab) {
int in_len, out_len;
char in[2];
char *in_p, *out_p;
if (johab < 128) {
*s = johab;
return 1;
}
if (johab_to_wan_cd == (iconv_t) -1) {
johab_to_wan_cd = iconv_open(CD_EUC_KR, CD_JOHAB);
if (johab_to_wan_cd == (iconv_t) -1) {
perror("johab_to_wansung");
exit(-1);
}
}
in_p = in; out_p = s;
in_len = 2; out_len = 2;
in[0] = (johab >> 8) | 0x80;
in[1] = johab & 0xff;
iconv(johab_to_wan_cd, &in_p, &in_len, &out_p, &out_len);
return 2 - out_len;
}
int johab_to_wansung_str(char *s, char *johab, int len) {
char *out_p = s;
char *in_p = johab;
int in_len = len;
int out_len = len;
if (johab_to_wan_cd == (iconv_t) -1) {
johab_to_wan_cd = iconv_open(CD_EUC_KR, CD_JOHAB);
if (johab_to_wan_cd == (iconv_t) -1) {
perror("johab_to_wansung_str");
exit(-1);
}
}
iconv(johab_to_wan_cd, &in_p, &in_len, &out_p, &out_len);
return len - in_len;
}
int johab_from_wansung(char *s, int wansung) {
int in_len, out_len;
char in[2];
char *in_p, *out_p;
if (wansung < 128) {
*s = wansung;
return 1;
}
if (wan_to_johab_cd == (iconv_t) -1) {
wan_to_johab_cd = iconv_open(CD_JOHAB, CD_EUC_KR);
if (wan_to_johab_cd == (iconv_t) -1) {
perror("johab_from_wansung");
exit(-1);
}
}
in_p = in; out_p = s;
in_len = 2; out_len = 2;
in[0] = (wansung >> 8) | 0x80;
in[1] = wansung & 0xff;
iconv(wan_to_johab_cd, &in_p, &in_len, &out_p, &out_len);
*out_p = 0;
return 2 - out_len;
}
int johab_from_wansung_str(char *s, char *wansung, int len) {
char *out_p = s;
char *in_p = wansung;
int in_len = len;
int out_len = len;
if (wan_to_johab_cd == (iconv_t) -1) {
wan_to_johab_cd = iconv_open(CD_JOHAB, CD_EUC_KR);
if (wan_to_johab_cd == (iconv_t) -1) {
perror("johab_from_wansung_str");
exit(-1);
}
}
iconv(wan_to_johab_cd, &in_p, &in_len, &out_p, &out_len);
*out_p = 0;
return len - in_len;
}
int johab_to_johab(char *s, int johab) {
if (johab < 128) {
*s = johab;
return 1;
}
*s++ = 0x80 | (johab >> 8);
*s = johab & 0xff;
return 2;
}
void johab_to_3(int johab, int *cho, int *jung, int *jong) {
*cho = (johab >> 10) & 0x1f;
*jung = (johab >> 5) & 0x1f;
*jong = (johab) & 0x1f;
}
int johab_from_3(int cho, int jung, int jong) {
return 0x8000 | (cho << 10) | (jung << 5) | jong;
}
int johab_to_utf8(char *s, int johab) {
int in_len, out_len;
char in[2];
char *in_p, *out_p;
if (johab < 128) {
*s = johab;
return 1;
}
if (johab_to_utf8_cd == (iconv_t) -1) {
johab_to_utf8_cd = iconv_open(CD_UTF8, CD_JOHAB);
if (johab_to_utf8_cd == (iconv_t) -1) {
perror("johab_to_utf8");
exit(-1);
}
}
in_p = in; out_p = s;
in_len = 2; out_len = 3;
in[0] = (johab >> 8) | 0x80;
in[1] = johab & 0xff;
iconv(johab_to_utf8_cd, &in_p, &in_len, &out_p, &out_len);
return 3 - out_len;
}