ยปCore Development>Code coverage>Modules/cjkcodecs/_codecs_kr.c

Python code coverage for Modules/cjkcodecs/_codecs_kr.c

#countcontent
1n/a/*
2n/a * _codecs_kr.c: Codecs collection for Korean encodings
3n/a *
4n/a * Written by Hye-Shik Chang <perky@FreeBSD.org>
5n/a */
6n/a
7n/a#include "cjkcodecs.h"
8n/a#include "mappings_kr.h"
9n/a
10n/a/*
11n/a * EUC-KR codec
12n/a */
13n/a
14n/a#define EUCKR_JAMO_FIRSTBYTE 0xA4
15n/a#define EUCKR_JAMO_FILLER 0xD4
16n/a
17n/astatic const unsigned char u2cgk_choseong[19] = {
18n/a 0xa1, 0xa2, 0xa4, 0xa7, 0xa8, 0xa9, 0xb1, 0xb2,
19n/a 0xb3, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb,
20n/a 0xbc, 0xbd, 0xbe
21n/a};
22n/astatic const unsigned char u2cgk_jungseong[21] = {
23n/a 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
24n/a 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
25n/a 0xcf, 0xd0, 0xd1, 0xd2, 0xd3
26n/a};
27n/astatic const unsigned char u2cgk_jongseong[28] = {
28n/a 0xd4, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
29n/a 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0,
30n/a 0xb1, 0xb2, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xba,
31n/a 0xbb, 0xbc, 0xbd, 0xbe
32n/a};
33n/a
34n/aENCODER(euc_kr)
35n/a{
36n/a while (*inpos < inlen) {
37n/a Py_UCS4 c = INCHAR1;
38n/a DBCHAR code;
39n/a
40n/a if (c < 0x80) {
41n/a WRITEBYTE1((unsigned char)c);
42n/a NEXT(1, 1);
43n/a continue;
44n/a }
45n/a
46n/a if (c > 0xFFFF)
47n/a return 1;
48n/a
49n/a REQUIRE_OUTBUF(2);
50n/a if (TRYMAP_ENC(cp949, code, c))
51n/a ;
52n/a else
53n/a return 1;
54n/a
55n/a if ((code & 0x8000) == 0) {
56n/a /* KS X 1001 coded character */
57n/a OUTBYTE1((code >> 8) | 0x80);
58n/a OUTBYTE2((code & 0xFF) | 0x80);
59n/a NEXT(1, 2);
60n/a }
61n/a else {
62n/a /* Mapping is found in CP949 extension,
63n/a but we encode it in KS X 1001:1998 Annex 3,
64n/a make-up sequence for EUC-KR. */
65n/a
66n/a REQUIRE_OUTBUF(8);
67n/a
68n/a /* syllable composition precedence */
69n/a OUTBYTE1(EUCKR_JAMO_FIRSTBYTE);
70n/a OUTBYTE2(EUCKR_JAMO_FILLER);
71n/a
72n/a /* All code points in CP949 extension are in unicode
73n/a * Hangul Syllable area. */
74n/a assert(0xac00 <= c && c <= 0xd7a3);
75n/a c -= 0xac00;
76n/a
77n/a OUTBYTE3(EUCKR_JAMO_FIRSTBYTE);
78n/a OUTBYTE4(u2cgk_choseong[c / 588]);
79n/a NEXT_OUT(4);
80n/a
81n/a OUTBYTE1(EUCKR_JAMO_FIRSTBYTE);
82n/a OUTBYTE2(u2cgk_jungseong[(c / 28) % 21]);
83n/a OUTBYTE3(EUCKR_JAMO_FIRSTBYTE);
84n/a OUTBYTE4(u2cgk_jongseong[c % 28]);
85n/a NEXT(1, 4);
86n/a }
87n/a }
88n/a
89n/a return 0;
90n/a}
91n/a
92n/a#define NONE 127
93n/a
94n/astatic const unsigned char cgk2u_choseong[] = { /* [A1, BE] */
95n/a 0, 1, NONE, 2, NONE, NONE, 3, 4,
96n/a 5, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
97n/a 6, 7, 8, NONE, 9, 10, 11, 12,
98n/a 13, 14, 15, 16, 17, 18
99n/a};
100n/astatic const unsigned char cgk2u_jongseong[] = { /* [A1, BE] */
101n/a 1, 2, 3, 4, 5, 6, 7, NONE,
102n/a 8, 9, 10, 11, 12, 13, 14, 15,
103n/a 16, 17, NONE, 18, 19, 20, 21, 22,
104n/a NONE, 23, 24, 25, 26, 27
105n/a};
106n/a
107n/aDECODER(euc_kr)
108n/a{
109n/a while (inleft > 0) {
110n/a unsigned char c = INBYTE1;
111n/a Py_UCS4 decoded;
112n/a
113n/a if (c < 0x80) {
114n/a OUTCHAR(c);
115n/a NEXT_IN(1);
116n/a continue;
117n/a }
118n/a
119n/a REQUIRE_INBUF(2);
120n/a
121n/a if (c == EUCKR_JAMO_FIRSTBYTE &&
122n/a INBYTE2 == EUCKR_JAMO_FILLER) {
123n/a /* KS X 1001:1998 Annex 3 make-up sequence */
124n/a DBCHAR cho, jung, jong;
125n/a
126n/a REQUIRE_INBUF(8);
127n/a if ((*inbuf)[2] != EUCKR_JAMO_FIRSTBYTE ||
128n/a (*inbuf)[4] != EUCKR_JAMO_FIRSTBYTE ||
129n/a (*inbuf)[6] != EUCKR_JAMO_FIRSTBYTE)
130n/a return 1;
131n/a
132n/a c = (*inbuf)[3];
133n/a if (0xa1 <= c && c <= 0xbe)
134n/a cho = cgk2u_choseong[c - 0xa1];
135n/a else
136n/a cho = NONE;
137n/a
138n/a c = (*inbuf)[5];
139n/a jung = (0xbf <= c && c <= 0xd3) ? c - 0xbf : NONE;
140n/a
141n/a c = (*inbuf)[7];
142n/a if (c == EUCKR_JAMO_FILLER)
143n/a jong = 0;
144n/a else if (0xa1 <= c && c <= 0xbe)
145n/a jong = cgk2u_jongseong[c - 0xa1];
146n/a else
147n/a jong = NONE;
148n/a
149n/a if (cho == NONE || jung == NONE || jong == NONE)
150n/a return 1;
151n/a
152n/a OUTCHAR(0xac00 + cho*588 + jung*28 + jong);
153n/a NEXT_IN(8);
154n/a }
155n/a else if (TRYMAP_DEC(ksx1001, decoded, c ^ 0x80, INBYTE2 ^ 0x80)) {
156n/a OUTCHAR(decoded);
157n/a NEXT_IN(2);
158n/a }
159n/a else
160n/a return 1;
161n/a }
162n/a
163n/a return 0;
164n/a}
165n/a#undef NONE
166n/a
167n/a
168n/a/*
169n/a * CP949 codec
170n/a */
171n/a
172n/aENCODER(cp949)
173n/a{
174n/a while (*inpos < inlen) {
175n/a Py_UCS4 c = INCHAR1;
176n/a DBCHAR code;
177n/a
178n/a if (c < 0x80) {
179n/a WRITEBYTE1((unsigned char)c);
180n/a NEXT(1, 1);
181n/a continue;
182n/a }
183n/a
184n/a if (c > 0xFFFF)
185n/a return 1;
186n/a
187n/a REQUIRE_OUTBUF(2);
188n/a if (TRYMAP_ENC(cp949, code, c))
189n/a ;
190n/a else
191n/a return 1;
192n/a
193n/a OUTBYTE1((code >> 8) | 0x80);
194n/a if (code & 0x8000)
195n/a OUTBYTE2(code & 0xFF); /* MSB set: CP949 */
196n/a else
197n/a OUTBYTE2((code & 0xFF) | 0x80); /* MSB unset: ks x 1001 */
198n/a NEXT(1, 2);
199n/a }
200n/a
201n/a return 0;
202n/a}
203n/a
204n/aDECODER(cp949)
205n/a{
206n/a while (inleft > 0) {
207n/a unsigned char c = INBYTE1;
208n/a Py_UCS4 decoded;
209n/a
210n/a if (c < 0x80) {
211n/a OUTCHAR(c);
212n/a NEXT_IN(1);
213n/a continue;
214n/a }
215n/a
216n/a REQUIRE_INBUF(2);
217n/a if (TRYMAP_DEC(ksx1001, decoded, c ^ 0x80, INBYTE2 ^ 0x80))
218n/a OUTCHAR(decoded);
219n/a else if (TRYMAP_DEC(cp949ext, decoded, c, INBYTE2))
220n/a OUTCHAR(decoded);
221n/a else
222n/a return 1;
223n/a
224n/a NEXT_IN(2);
225n/a }
226n/a
227n/a return 0;
228n/a}
229n/a
230n/a
231n/a/*
232n/a * JOHAB codec
233n/a */
234n/a
235n/astatic const unsigned char u2johabidx_choseong[32] = {
236n/a 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
237n/a 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
238n/a 0x10, 0x11, 0x12, 0x13, 0x14,
239n/a};
240n/astatic const unsigned char u2johabidx_jungseong[32] = {
241n/a 0x03, 0x04, 0x05, 0x06, 0x07,
242n/a 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
243n/a 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
244n/a 0x1a, 0x1b, 0x1c, 0x1d,
245n/a};
246n/astatic const unsigned char u2johabidx_jongseong[32] = {
247n/a 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
248n/a 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
249n/a 0x10, 0x11, 0x13, 0x14, 0x15, 0x16, 0x17,
250n/a 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
251n/a};
252n/astatic const DBCHAR u2johabjamo[] = {
253n/a 0x8841, 0x8c41, 0x8444, 0x9041, 0x8446, 0x8447, 0x9441,
254n/a 0x9841, 0x9c41, 0x844a, 0x844b, 0x844c, 0x844d, 0x844e, 0x844f,
255n/a 0x8450, 0xa041, 0xa441, 0xa841, 0x8454, 0xac41, 0xb041, 0xb441,
256n/a 0xb841, 0xbc41, 0xc041, 0xc441, 0xc841, 0xcc41, 0xd041, 0x8461,
257n/a 0x8481, 0x84a1, 0x84c1, 0x84e1, 0x8541, 0x8561, 0x8581, 0x85a1,
258n/a 0x85c1, 0x85e1, 0x8641, 0x8661, 0x8681, 0x86a1, 0x86c1, 0x86e1,
259n/a 0x8741, 0x8761, 0x8781, 0x87a1,
260n/a};
261n/a
262n/aENCODER(johab)
263n/a{
264n/a while (*inpos < inlen) {
265n/a Py_UCS4 c = INCHAR1;
266n/a DBCHAR code;
267n/a
268n/a if (c < 0x80) {
269n/a WRITEBYTE1((unsigned char)c);
270n/a NEXT(1, 1);
271n/a continue;
272n/a }
273n/a
274n/a if (c > 0xFFFF)
275n/a return 1;
276n/a
277n/a REQUIRE_OUTBUF(2);
278n/a
279n/a if (c >= 0xac00 && c <= 0xd7a3) {
280n/a c -= 0xac00;
281n/a code = 0x8000 |
282n/a (u2johabidx_choseong[c / 588] << 10) |
283n/a (u2johabidx_jungseong[(c / 28) % 21] << 5) |
284n/a u2johabidx_jongseong[c % 28];
285n/a }
286n/a else if (c >= 0x3131 && c <= 0x3163)
287n/a code = u2johabjamo[c - 0x3131];
288n/a else if (TRYMAP_ENC(cp949, code, c)) {
289n/a unsigned char c1, c2, t2;
290n/a unsigned short t1;
291n/a
292n/a assert((code & 0x8000) == 0);
293n/a c1 = code >> 8;
294n/a c2 = code & 0xff;
295n/a if (((c1 >= 0x21 && c1 <= 0x2c) ||
296n/a (c1 >= 0x4a && c1 <= 0x7d)) &&
297n/a (c2 >= 0x21 && c2 <= 0x7e)) {
298n/a t1 = (c1 < 0x4a ? (c1 - 0x21 + 0x1b2) :
299n/a (c1 - 0x21 + 0x197));
300n/a t2 = ((t1 & 1) ? 0x5e : 0) + (c2 - 0x21);
301n/a OUTBYTE1(t1 >> 1);
302n/a OUTBYTE2(t2 < 0x4e ? t2 + 0x31 : t2 + 0x43);
303n/a NEXT(1, 2);
304n/a continue;
305n/a }
306n/a else
307n/a return 1;
308n/a }
309n/a else
310n/a return 1;
311n/a
312n/a OUTBYTE1(code >> 8);
313n/a OUTBYTE2(code & 0xff);
314n/a NEXT(1, 2);
315n/a }
316n/a
317n/a return 0;
318n/a}
319n/a
320n/a#define FILL 0xfd
321n/a#define NONE 0xff
322n/a
323n/astatic const unsigned char johabidx_choseong[32] = {
324n/a NONE, FILL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
325n/a 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
326n/a 0x0e, 0x0f, 0x10, 0x11, 0x12, NONE, NONE, NONE,
327n/a NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
328n/a};
329n/astatic const unsigned char johabidx_jungseong[32] = {
330n/a NONE, NONE, FILL, 0x00, 0x01, 0x02, 0x03, 0x04,
331n/a NONE, NONE, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
332n/a NONE, NONE, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
333n/a NONE, NONE, 0x11, 0x12, 0x13, 0x14, NONE, NONE,
334n/a};
335n/astatic const unsigned char johabidx_jongseong[32] = {
336n/a NONE, FILL, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
337n/a 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
338n/a 0x0f, 0x10, NONE, 0x11, 0x12, 0x13, 0x14, 0x15,
339n/a 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, NONE, NONE,
340n/a};
341n/a
342n/astatic const unsigned char johabjamo_choseong[32] = {
343n/a NONE, FILL, 0x31, 0x32, 0x34, 0x37, 0x38, 0x39,
344n/a 0x41, 0x42, 0x43, 0x45, 0x46, 0x47, 0x48, 0x49,
345n/a 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, NONE, NONE, NONE,
346n/a NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
347n/a};
348n/astatic const unsigned char johabjamo_jungseong[32] = {
349n/a NONE, NONE, FILL, 0x4f, 0x50, 0x51, 0x52, 0x53,
350n/a NONE, NONE, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
351n/a NONE, NONE, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
352n/a NONE, NONE, 0x60, 0x61, 0x62, 0x63, NONE, NONE,
353n/a};
354n/astatic const unsigned char johabjamo_jongseong[32] = {
355n/a NONE, FILL, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
356n/a 0x37, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
357n/a 0x40, 0x41, NONE, 0x42, 0x44, 0x45, 0x46, 0x47,
358n/a 0x48, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, NONE, NONE,
359n/a};
360n/a
361n/aDECODER(johab)
362n/a{
363n/a while (inleft > 0) {
364n/a unsigned char c = INBYTE1, c2;
365n/a Py_UCS4 decoded;
366n/a
367n/a if (c < 0x80) {
368n/a OUTCHAR(c);
369n/a NEXT_IN(1);
370n/a continue;
371n/a }
372n/a
373n/a REQUIRE_INBUF(2);
374n/a c2 = INBYTE2;
375n/a
376n/a if (c < 0xd8) {
377n/a /* johab hangul */
378n/a unsigned char c_cho, c_jung, c_jong;
379n/a unsigned char i_cho, i_jung, i_jong;
380n/a
381n/a c_cho = (c >> 2) & 0x1f;
382n/a c_jung = ((c << 3) | c2 >> 5) & 0x1f;
383n/a c_jong = c2 & 0x1f;
384n/a
385n/a i_cho = johabidx_choseong[c_cho];
386n/a i_jung = johabidx_jungseong[c_jung];
387n/a i_jong = johabidx_jongseong[c_jong];
388n/a
389n/a if (i_cho == NONE || i_jung == NONE || i_jong == NONE)
390n/a return 1;
391n/a
392n/a /* we don't use U+1100 hangul jamo yet. */
393n/a if (i_cho == FILL) {
394n/a if (i_jung == FILL) {
395n/a if (i_jong == FILL)
396n/a OUTCHAR(0x3000);
397n/a else
398n/a OUTCHAR(0x3100 |
399n/a johabjamo_jongseong[c_jong]);
400n/a }
401n/a else {
402n/a if (i_jong == FILL)
403n/a OUTCHAR(0x3100 |
404n/a johabjamo_jungseong[c_jung]);
405n/a else
406n/a return 1;
407n/a }
408n/a } else {
409n/a if (i_jung == FILL) {
410n/a if (i_jong == FILL)
411n/a OUTCHAR(0x3100 |
412n/a johabjamo_choseong[c_cho]);
413n/a else
414n/a return 1;
415n/a }
416n/a else
417n/a OUTCHAR(0xac00 +
418n/a i_cho * 588 +
419n/a i_jung * 28 +
420n/a (i_jong == FILL ? 0 : i_jong));
421n/a }
422n/a NEXT_IN(2);
423n/a } else {
424n/a /* KS X 1001 except hangul jamos and syllables */
425n/a if (c == 0xdf || c > 0xf9 ||
426n/a c2 < 0x31 || (c2 >= 0x80 && c2 < 0x91) ||
427n/a (c2 & 0x7f) == 0x7f ||
428n/a (c == 0xda && (c2 >= 0xa1 && c2 <= 0xd3)))
429n/a return 1;
430n/a else {
431n/a unsigned char t1, t2;
432n/a
433n/a t1 = (c < 0xe0 ? 2 * (c - 0xd9) :
434n/a 2 * c - 0x197);
435n/a t2 = (c2 < 0x91 ? c2 - 0x31 : c2 - 0x43);
436n/a t1 = t1 + (t2 < 0x5e ? 0 : 1) + 0x21;
437n/a t2 = (t2 < 0x5e ? t2 : t2 - 0x5e) + 0x21;
438n/a
439n/a if (TRYMAP_DEC(ksx1001, decoded, t1, t2)) {
440n/a OUTCHAR(decoded);
441n/a NEXT_IN(2);
442n/a }
443n/a else {
444n/a return 1;
445n/a }
446n/a }
447n/a }
448n/a }
449n/a
450n/a return 0;
451n/a}
452n/a#undef NONE
453n/a#undef FILL
454n/a
455n/a
456n/aBEGIN_MAPPINGS_LIST
457n/a MAPPING_DECONLY(ksx1001)
458n/a MAPPING_ENCONLY(cp949)
459n/a MAPPING_DECONLY(cp949ext)
460n/aEND_MAPPINGS_LIST
461n/a
462n/aBEGIN_CODECS_LIST
463n/a CODEC_STATELESS(euc_kr)
464n/a CODEC_STATELESS(cp949)
465n/a CODEC_STATELESS(johab)
466n/aEND_CODECS_LIST
467n/a
468n/aI_AM_A_MODULE_FOR(kr)