blob: 3a00d4144624430a6037cb1b0a4c0b7d12acaa49 [file] [log] [blame]
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +00001/*
2 * cjkcodecs.h: common header for cjkcodecs
3 *
4 * Written by Hye-Shik Chang <perky@FreeBSD.org>
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +00005 */
6
7#ifndef _CJKCODECS_H_
8#define _CJKCODECS_H_
9
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000010#define PY_SSIZE_T_CLEAN
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000011#include "Python.h"
12#include "multibytecodec.h"
13
14
Hye-Shik Chang331649a2005-10-06 15:51:59 +000015/* a unicode "undefined" codepoint */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016#define UNIINV 0xFFFE
Hye-Shik Chang331649a2005-10-06 15:51:59 +000017
18/* internal-use DBCS codepoints which aren't used by any charsets */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019#define NOCHAR 0xFFFF
20#define MULTIC 0xFFFE
21#define DBCINV 0xFFFD
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000022
23/* shorter macros to save source size of mapping tables */
24#define U UNIINV
25#define N NOCHAR
26#define M MULTIC
27#define D DBCINV
28
29struct dbcs_index {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 const ucs2_t *map;
31 unsigned char bottom, top;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000032};
33typedef struct dbcs_index decode_map;
34
35struct widedbcs_index {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 const ucs4_t *map;
37 unsigned char bottom, top;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000038};
39typedef struct widedbcs_index widedecode_map;
40
41struct unim_index {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 const DBCHAR *map;
43 unsigned char bottom, top;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000044};
45typedef struct unim_index encode_map;
46
47struct unim_index_bytebased {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 const unsigned char *map;
49 unsigned char bottom, top;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000050};
51
52struct dbcs_map {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 const char *charset;
54 const struct unim_index *encmap;
55 const struct dbcs_index *decmap;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000056};
57
58struct pair_encodemap {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 ucs4_t uniseq;
60 DBCHAR code;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000061};
62
Hye-Shik Chang64a9e382004-07-18 15:02:45 +000063static const MultibyteCodec *codec_list;
64static const struct dbcs_map *mapping_list;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066#define CODEC_INIT(encoding) \
67 static int encoding##_codec_init(const void *config)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069#define ENCODER_INIT(encoding) \
70 static int encoding##_encode_init( \
71 MultibyteCodec_State *state, const void *config)
72#define ENCODER(encoding) \
73 static Py_ssize_t encoding##_encode( \
74 MultibyteCodec_State *state, const void *config, \
75 const Py_UNICODE **inbuf, Py_ssize_t inleft, \
76 unsigned char **outbuf, Py_ssize_t outleft, int flags)
77#define ENCODER_RESET(encoding) \
78 static Py_ssize_t encoding##_encode_reset( \
79 MultibyteCodec_State *state, const void *config, \
80 unsigned char **outbuf, Py_ssize_t outleft)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082#define DECODER_INIT(encoding) \
83 static int encoding##_decode_init( \
84 MultibyteCodec_State *state, const void *config)
85#define DECODER(encoding) \
86 static Py_ssize_t encoding##_decode( \
87 MultibyteCodec_State *state, const void *config, \
88 const unsigned char **inbuf, Py_ssize_t inleft, \
89 Py_UNICODE **outbuf, Py_ssize_t outleft)
90#define DECODER_RESET(encoding) \
91 static Py_ssize_t encoding##_decode_reset( \
92 MultibyteCodec_State *state, const void *config)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000093
94#if Py_UNICODE_SIZE == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095#define UCS4INVALID(code) \
96 if ((code) > 0xFFFF) \
97 return 1;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000098#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099#define UCS4INVALID(code) \
100 if (0) ;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000101#endif
102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103#define NEXT_IN(i) \
104 (*inbuf) += (i); \
105 (inleft) -= (i);
106#define NEXT_OUT(o) \
107 (*outbuf) += (o); \
108 (outleft) -= (o);
109#define NEXT(i, o) \
110 NEXT_IN(i) NEXT_OUT(o)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112#define REQUIRE_INBUF(n) \
113 if (inleft < (n)) \
114 return MBERR_TOOFEW;
115#define REQUIRE_OUTBUF(n) \
116 if (outleft < (n)) \
117 return MBERR_TOOSMALL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000118
119#define IN1 ((*inbuf)[0])
120#define IN2 ((*inbuf)[1])
121#define IN3 ((*inbuf)[2])
122#define IN4 ((*inbuf)[3])
123
124#define OUT1(c) ((*outbuf)[0]) = (c);
125#define OUT2(c) ((*outbuf)[1]) = (c);
126#define OUT3(c) ((*outbuf)[2]) = (c);
127#define OUT4(c) ((*outbuf)[3]) = (c);
128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129#define WRITE1(c1) \
130 REQUIRE_OUTBUF(1) \
131 (*outbuf)[0] = (c1);
132#define WRITE2(c1, c2) \
133 REQUIRE_OUTBUF(2) \
134 (*outbuf)[0] = (c1); \
135 (*outbuf)[1] = (c2);
136#define WRITE3(c1, c2, c3) \
137 REQUIRE_OUTBUF(3) \
138 (*outbuf)[0] = (c1); \
139 (*outbuf)[1] = (c2); \
140 (*outbuf)[2] = (c3);
141#define WRITE4(c1, c2, c3, c4) \
142 REQUIRE_OUTBUF(4) \
143 (*outbuf)[0] = (c1); \
144 (*outbuf)[1] = (c2); \
145 (*outbuf)[2] = (c3); \
146 (*outbuf)[3] = (c4);
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000147
148#if Py_UNICODE_SIZE == 2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149# define WRITEUCS4(c) \
150 REQUIRE_OUTBUF(2) \
Victor Stinner76df43d2012-10-30 01:42:39 +0100151 (*outbuf)[0] = Py_UNICODE_HIGH_SURROGATE(c); \
152 (*outbuf)[1] = Py_UNICODE_LOW_SURROGATE(c); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 NEXT_OUT(2)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000154#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155# define WRITEUCS4(c) \
156 REQUIRE_OUTBUF(1) \
157 **outbuf = (Py_UNICODE)(c); \
158 NEXT_OUT(1)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000159#endif
160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161#define _TRYMAP_ENC(m, assi, val) \
162 ((m)->map != NULL && (val) >= (m)->bottom && \
163 (val)<= (m)->top && ((assi) = (m)->map[(val) - \
164 (m)->bottom]) != NOCHAR)
165#define TRYMAP_ENC_COND(charset, assi, uni) \
166 _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
167#define TRYMAP_ENC(charset, assi, uni) \
168 if TRYMAP_ENC_COND(charset, assi, uni)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170#define _TRYMAP_DEC(m, assi, val) \
171 ((m)->map != NULL && (val) >= (m)->bottom && \
172 (val)<= (m)->top && ((assi) = (m)->map[(val) - \
173 (m)->bottom]) != UNIINV)
174#define TRYMAP_DEC(charset, assi, c1, c2) \
175 if _TRYMAP_DEC(&charset##_decmap[c1], assi, c2)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177#define _TRYMAP_ENC_MPLANE(m, assplane, asshi, asslo, val) \
178 ((m)->map != NULL && (val) >= (m)->bottom && \
179 (val)<= (m)->top && \
180 ((assplane) = (m)->map[((val) - (m)->bottom)*3]) != 0 && \
181 (((asshi) = (m)->map[((val) - (m)->bottom)*3 + 1]), 1) && \
182 (((asslo) = (m)->map[((val) - (m)->bottom)*3 + 2]), 1))
183#define TRYMAP_ENC_MPLANE(charset, assplane, asshi, asslo, uni) \
184 if _TRYMAP_ENC_MPLANE(&charset##_encmap[(uni) >> 8], \
185 assplane, asshi, asslo, (uni) & 0xff)
186#define TRYMAP_DEC_MPLANE(charset, assi, plane, c1, c2) \
187 if _TRYMAP_DEC(&charset##_decmap[plane][c1], assi, c2)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000188
189#if Py_UNICODE_SIZE == 2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190#define DECODE_SURROGATE(c) \
Victor Stinner76df43d2012-10-30 01:42:39 +0100191 if (Py_UNICODE_IS_HIGH_SURROGATE(c)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 REQUIRE_INBUF(2) \
Victor Stinner76df43d2012-10-30 01:42:39 +0100193 if (Py_UNICODE_IS_LOW_SURROGATE(IN2)) { \
Victor Stinnerb9e2d3f2012-10-30 02:30:31 +0100194 c = Py_UNICODE_JOIN_SURROGATES(c, IN2); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 } \
196 }
197#define GET_INSIZE(c) ((c) > 0xffff ? 2 : 1)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000198#else
199#define DECODE_SURROGATE(c) {;}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200#define GET_INSIZE(c) 1
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000201#endif
202
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000203#define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000204#define MAPPING_ENCONLY(enc) {#enc, (void*)enc##_encmap, NULL},
205#define MAPPING_DECONLY(enc) {#enc, NULL, (void*)enc##_decmap},
206#define MAPPING_ENCDEC(enc) {#enc, (void*)enc##_encmap, (void*)enc##_decmap},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207#define END_MAPPINGS_LIST \
208 {"", NULL, NULL} }; \
209 static const struct dbcs_map *mapping_list = \
210 (const struct dbcs_map *)_mapping_list;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000211
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000212#define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213#define _STATEFUL_METHODS(enc) \
214 enc##_encode, \
215 enc##_encode_init, \
216 enc##_encode_reset, \
217 enc##_decode, \
218 enc##_decode_init, \
219 enc##_decode_reset,
220#define _STATELESS_METHODS(enc) \
221 enc##_encode, NULL, NULL, \
222 enc##_decode, NULL, NULL,
223#define CODEC_STATEFUL(enc) { \
224 #enc, NULL, NULL, \
225 _STATEFUL_METHODS(enc) \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000226},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227#define CODEC_STATELESS(enc) { \
228 #enc, NULL, NULL, \
229 _STATELESS_METHODS(enc) \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000230},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231#define CODEC_STATELESS_WINIT(enc) { \
232 #enc, NULL, \
233 enc##_codec_init, \
234 _STATELESS_METHODS(enc) \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000235},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236#define END_CODECS_LIST \
237 {"", NULL,} }; \
238 static const MultibyteCodec *codec_list = \
239 (const MultibyteCodec *)_codec_list;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000240
Benjamin Petersonb173f782009-05-05 22:31:58 +0000241
242
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000243static PyObject *
244getmultibytecodec(void)
245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 static PyObject *cofunc = NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (cofunc == NULL) {
249 PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec");
250 if (mod == NULL)
251 return NULL;
252 cofunc = PyObject_GetAttrString(mod, "__create_codec");
253 Py_DECREF(mod);
254 }
255 return cofunc;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000256}
257
258static PyObject *
259getcodec(PyObject *self, PyObject *encoding)
260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 PyObject *codecobj, *r, *cofunc;
262 const MultibyteCodec *codec;
263 const char *enc;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (!PyUnicode_Check(encoding)) {
266 PyErr_SetString(PyExc_TypeError,
267 "encoding name must be a string.");
268 return NULL;
269 }
270 enc = _PyUnicode_AsString(encoding);
271 if (enc == NULL)
272 return NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 cofunc = getmultibytecodec();
275 if (cofunc == NULL)
276 return NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 for (codec = codec_list; codec->encoding[0]; codec++)
279 if (strcmp(codec->encoding, enc) == 0)
280 break;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 if (codec->encoding[0] == '\0') {
283 PyErr_SetString(PyExc_LookupError,
284 "no such codec is supported.");
285 return NULL;
286 }
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL);
289 if (codecobj == NULL)
290 return NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL);
293 Py_DECREF(codecobj);
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 return r;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000296}
297
298static struct PyMethodDef __methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 {"getcodec", (PyCFunction)getcodec, METH_O, ""},
300 {NULL, NULL},
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000301};
302
303static int
304register_maps(PyObject *module)
305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 const struct dbcs_map *h;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 for (h = mapping_list; h->charset[0] != '\0'; h++) {
309 char mhname[256] = "__map_";
310 int r;
311 strcpy(mhname + sizeof("__map_") - 1, h->charset);
312 r = PyModule_AddObject(module, mhname,
313 PyCapsule_New((void *)h, PyMultibyteCodec_CAPSULE_NAME, NULL));
314 if (r == -1)
315 return -1;
316 }
317 return 0;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000318}
319
320#ifdef USING_BINARY_PAIR_SEARCH
321static DBCHAR
322find_pairencmap(ucs2_t body, ucs2_t modifier,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 const struct pair_encodemap *haystack, int haystacksize)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 int pos, min, max;
326 ucs4_t value = body << 16 | modifier;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 min = 0;
329 max = haystacksize;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1)
332 if (value < haystack[pos].uniseq) {
333 if (max == pos) break;
334 else max = pos;
335 }
336 else if (value > haystack[pos].uniseq) {
337 if (min == pos) break;
338 else min = pos;
339 }
340 else
341 break;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (value == haystack[pos].uniseq)
344 return haystack[pos].code;
345 else
346 return DBCINV;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000347}
348#endif
349
350#ifdef USING_IMPORTED_MAPS
351#define IMPORT_MAP(locale, charset, encmap, decmap) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 importmap("_codecs_" #locale, "__map_" #charset, \
353 (const void**)encmap, (const void**)decmap)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000354
355static int
356importmap(const char *modname, const char *symbol,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 const void **encmap, const void **decmap)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 PyObject *o, *mod;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 mod = PyImport_ImportModule((char *)modname);
362 if (mod == NULL)
363 return -1;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 o = PyObject_GetAttrString(mod, (char*)symbol);
366 if (o == NULL)
367 goto errorexit;
368 else if (!PyCapsule_IsValid(o, PyMultibyteCodec_CAPSULE_NAME)) {
369 PyErr_SetString(PyExc_ValueError,
370 "map data must be a Capsule.");
371 goto errorexit;
372 }
373 else {
374 struct dbcs_map *map;
375 map = PyCapsule_GetPointer(o, PyMultibyteCodec_CAPSULE_NAME);
376 if (encmap != NULL)
377 *encmap = map->encmap;
378 if (decmap != NULL)
379 *decmap = map->decmap;
380 Py_DECREF(o);
381 }
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_DECREF(mod);
384 return 0;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000385
386errorexit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 Py_DECREF(mod);
388 return -1;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000389}
390#endif
391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392#define I_AM_A_MODULE_FOR(loc) \
393 static struct PyModuleDef __module = { \
394 PyModuleDef_HEAD_INIT, \
395 "_codecs_"#loc, \
396 NULL, \
397 0, \
398 __methods, \
399 NULL, \
400 NULL, \
401 NULL, \
402 NULL \
403 }; \
404 PyObject* \
405 PyInit__codecs_##loc(void) \
406 { \
407 PyObject *m = PyModule_Create(&__module); \
408 if (m != NULL) \
409 (void)register_maps(m); \
410 return m; \
411 }
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000412
413#endif