blob: fb6b5ce878d0f4744cdee304bb695561b21e372a [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 */
16#define UNIINV 0xFFFE
17
18/* internal-use DBCS codepoints which aren't used by any charsets */
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000019#define NOCHAR 0xFFFF
20#define MULTIC 0xFFFE
21#define DBCINV 0xFFFD
22
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 {
30 const ucs2_t *map;
31 unsigned char bottom, top;
32};
33typedef struct dbcs_index decode_map;
34
35struct widedbcs_index {
36 const ucs4_t *map;
37 unsigned char bottom, top;
38};
39typedef struct widedbcs_index widedecode_map;
40
41struct unim_index {
42 const DBCHAR *map;
43 unsigned char bottom, top;
44};
45typedef struct unim_index encode_map;
46
47struct unim_index_bytebased {
48 const unsigned char *map;
49 unsigned char bottom, top;
50};
51
52struct dbcs_map {
53 const char *charset;
54 const struct unim_index *encmap;
55 const struct dbcs_index *decmap;
56};
57
58struct pair_encodemap {
59 ucs4_t uniseq;
60 DBCHAR code;
61};
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
66#define CODEC_INIT(encoding) \
67 static int encoding##_codec_init(const void *config)
68
69#define ENCODER_INIT(encoding) \
70 static int encoding##_encode_init( \
71 MultibyteCodec_State *state, const void *config)
72#define ENCODER(encoding) \
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000073 static Py_ssize_t encoding##_encode( \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000074 MultibyteCodec_State *state, const void *config, \
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000075 const Py_UNICODE **inbuf, Py_ssize_t inleft, \
76 unsigned char **outbuf, Py_ssize_t outleft, int flags)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000077#define ENCODER_RESET(encoding) \
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000078 static Py_ssize_t encoding##_encode_reset( \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000079 MultibyteCodec_State *state, const void *config, \
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000080 unsigned char **outbuf, Py_ssize_t outleft)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000081
82#define DECODER_INIT(encoding) \
83 static int encoding##_decode_init( \
84 MultibyteCodec_State *state, const void *config)
85#define DECODER(encoding) \
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000086 static Py_ssize_t encoding##_decode( \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000087 MultibyteCodec_State *state, const void *config, \
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000088 const unsigned char **inbuf, Py_ssize_t inleft, \
89 Py_UNICODE **outbuf, Py_ssize_t outleft)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000090#define DECODER_RESET(encoding) \
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000091 static Py_ssize_t encoding##_decode_reset( \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000092 MultibyteCodec_State *state, const void *config)
93
94#if Py_UNICODE_SIZE == 4
95#define UCS4INVALID(code) \
96 if ((code) > 0xFFFF) \
97 return 1;
98#else
99#define UCS4INVALID(code) \
100 if (0) ;
101#endif
102
103#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)
111
112#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;
118
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
129#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);
147
148#if Py_UNICODE_SIZE == 2
149# define WRITEUCS4(c) \
150 REQUIRE_OUTBUF(2) \
151 (*outbuf)[0] = 0xd800 + (((c) - 0x10000) >> 10); \
152 (*outbuf)[1] = 0xdc00 + (((c) - 0x10000) & 0x3ff); \
153 NEXT_OUT(2)
154#else
155# define WRITEUCS4(c) \
156 REQUIRE_OUTBUF(1) \
157 **outbuf = (Py_UNICODE)(c); \
158 NEXT_OUT(1)
159#endif
160
161#define _TRYMAP_ENC(m, assi, val) \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000162 ((m)->map != NULL && (val) >= (m)->bottom && \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000163 (val)<= (m)->top && ((assi) = (m)->map[(val) - \
164 (m)->bottom]) != NOCHAR)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000165#define TRYMAP_ENC_COND(charset, assi, uni) \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000166 _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000167#define TRYMAP_ENC(charset, assi, uni) \
168 if TRYMAP_ENC_COND(charset, assi, uni)
169
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000170#define _TRYMAP_DEC(m, assi, val) \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000171 ((m)->map != NULL && (val) >= (m)->bottom && \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000172 (val)<= (m)->top && ((assi) = (m)->map[(val) - \
173 (m)->bottom]) != UNIINV)
174#define TRYMAP_DEC(charset, assi, c1, c2) \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000175 if _TRYMAP_DEC(&charset##_decmap[c1], assi, c2)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000176
177#define _TRYMAP_ENC_MPLANE(m, assplane, asshi, asslo, val) \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000178 ((m)->map != NULL && (val) >= (m)->bottom && \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000179 (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) \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000184 if _TRYMAP_ENC_MPLANE(&charset##_encmap[(uni) >> 8], \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000185 assplane, asshi, asslo, (uni) & 0xff)
186#define TRYMAP_DEC_MPLANE(charset, assi, plane, c1, c2) \
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 if _TRYMAP_DEC(&charset##_decmap[plane][c1], assi, c2)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000188
189#if Py_UNICODE_SIZE == 2
190#define DECODE_SURROGATE(c) \
191 if (c >> 10 == 0xd800 >> 10) { /* high surrogate */ \
192 REQUIRE_INBUF(2) \
193 if (IN2 >> 10 == 0xdc00 >> 10) { /* low surrogate */ \
194 c = 0x10000 + ((ucs4_t)(c - 0xd800) << 10) + \
195 ((ucs4_t)(IN2) - 0xdc00); \
196 } \
197 }
198#define GET_INSIZE(c) ((c) > 0xffff ? 2 : 1)
199#else
200#define DECODE_SURROGATE(c) {;}
201#define GET_INSIZE(c) 1
202#endif
203
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000204#define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000205#define MAPPING_ENCONLY(enc) {#enc, (void*)enc##_encmap, NULL},
206#define MAPPING_DECONLY(enc) {#enc, NULL, (void*)enc##_decmap},
207#define MAPPING_ENCDEC(enc) {#enc, (void*)enc##_encmap, (void*)enc##_decmap},
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000208#define END_MAPPINGS_LIST \
209 {"", NULL, NULL} }; \
210 static const struct dbcs_map *mapping_list = \
211 (const struct dbcs_map *)_mapping_list;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000212
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000213#define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000214#define _STATEFUL_METHODS(enc) \
215 enc##_encode, \
216 enc##_encode_init, \
217 enc##_encode_reset, \
218 enc##_decode, \
219 enc##_decode_init, \
220 enc##_decode_reset,
221#define _STATELESS_METHODS(enc) \
222 enc##_encode, NULL, NULL, \
223 enc##_decode, NULL, NULL,
224#define CODEC_STATEFUL(enc) { \
225 #enc, NULL, NULL, \
226 _STATEFUL_METHODS(enc) \
227},
228#define CODEC_STATELESS(enc) { \
229 #enc, NULL, NULL, \
230 _STATELESS_METHODS(enc) \
231},
232#define CODEC_STATELESS_WINIT(enc) { \
233 #enc, NULL, \
234 enc##_codec_init, \
235 _STATELESS_METHODS(enc) \
236},
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000237#define END_CODECS_LIST \
238 {"", NULL,} }; \
Hye-Shik Changf5a149a2004-08-19 17:49:56 +0000239 static const MultibyteCodec *codec_list = \
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000240 (const MultibyteCodec *)_codec_list;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000241
242static PyObject *
243getmultibytecodec(void)
244{
245 static PyObject *cofunc = NULL;
246
247 if (cofunc == NULL) {
Christian Heimes072c0f12008-01-03 23:01:04 +0000248 PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec");
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000249 if (mod == NULL)
250 return NULL;
251 cofunc = PyObject_GetAttrString(mod, "__create_codec");
252 Py_DECREF(mod);
253 }
254 return cofunc;
255}
256
257static PyObject *
258getcodec(PyObject *self, PyObject *encoding)
259{
260 PyObject *codecobj, *r, *cofunc;
261 const MultibyteCodec *codec;
262 const char *enc;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000263
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000264 if (!PyUnicode_Check(encoding)) {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000265 PyErr_SetString(PyExc_TypeError,
266 "encoding name must be a string.");
267 return NULL;
268 }
Neal Norwitz5616c6d2007-08-26 05:32:41 +0000269 enc = PyUnicode_AsString(encoding);
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000270 if (enc == NULL)
271 return NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000272
273 cofunc = getmultibytecodec();
274 if (cofunc == NULL)
275 return NULL;
276
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000277 for (codec = codec_list; codec->encoding[0]; codec++)
278 if (strcmp(codec->encoding, enc) == 0)
279 break;
280
281 if (codec->encoding[0] == '\0') {
282 PyErr_SetString(PyExc_LookupError,
283 "no such codec is supported.");
284 return NULL;
285 }
286
287 codecobj = PyCObject_FromVoidPtr((void *)codec, NULL);
288 if (codecobj == NULL)
289 return NULL;
290
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000291 r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL);
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000292 Py_DECREF(codecobj);
293
294 return r;
295}
296
297static struct PyMethodDef __methods[] = {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000298 {"getcodec", (PyCFunction)getcodec, METH_O, ""},
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000299 {NULL, NULL},
300};
301
302static int
303register_maps(PyObject *module)
304{
305 const struct dbcs_map *h;
306
307 for (h = mapping_list; h->charset[0] != '\0'; h++) {
308 char mhname[256] = "__map_";
309 int r;
310 strcpy(mhname + sizeof("__map_") - 1, h->charset);
311 r = PyModule_AddObject(module, mhname,
312 PyCObject_FromVoidPtr((void *)h, NULL));
313 if (r == -1)
314 return -1;
315 }
316 return 0;
317}
318
319#ifdef USING_BINARY_PAIR_SEARCH
320static DBCHAR
321find_pairencmap(ucs2_t body, ucs2_t modifier,
322 const struct pair_encodemap *haystack, int haystacksize)
323{
324 int pos, min, max;
325 ucs4_t value = body << 16 | modifier;
326
327 min = 0;
328 max = haystacksize;
329
330 for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1)
331 if (value < haystack[pos].uniseq) {
332 if (max == pos) break;
333 else max = pos;
334 }
335 else if (value > haystack[pos].uniseq) {
336 if (min == pos) break;
337 else min = pos;
338 }
339 else
340 break;
341
342 if (value == haystack[pos].uniseq)
343 return haystack[pos].code;
344 else
345 return DBCINV;
346}
347#endif
348
349#ifdef USING_IMPORTED_MAPS
350#define IMPORT_MAP(locale, charset, encmap, decmap) \
351 importmap("_codecs_" #locale, "__map_" #charset, \
352 (const void**)encmap, (const void**)decmap)
353
354static int
355importmap(const char *modname, const char *symbol,
356 const void **encmap, const void **decmap)
357{
358 PyObject *o, *mod;
359
360 mod = PyImport_ImportModule((char *)modname);
361 if (mod == NULL)
362 return -1;
363
364 o = PyObject_GetAttrString(mod, (char*)symbol);
365 if (o == NULL)
366 goto errorexit;
367 else if (!PyCObject_Check(o)) {
368 PyErr_SetString(PyExc_ValueError,
369 "map data must be a CObject.");
370 goto errorexit;
371 }
372 else {
373 struct dbcs_map *map;
374 map = PyCObject_AsVoidPtr(o);
375 if (encmap != NULL)
376 *encmap = map->encmap;
377 if (decmap != NULL)
378 *decmap = map->decmap;
379 Py_DECREF(o);
380 }
381
382 Py_DECREF(mod);
383 return 0;
384
385errorexit:
386 Py_DECREF(mod);
387 return -1;
388}
389#endif
390
391#define I_AM_A_MODULE_FOR(loc) \
392 void \
393 init_codecs_##loc(void) \
394 { \
395 PyObject *m = Py_InitModule("_codecs_" #loc, __methods);\
Hye-Shik Chang32feb852006-03-06 07:51:19 +0000396 if (m != NULL) \
397 (void)register_maps(m); \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000398 }
399
400#endif