Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * cjkcodecs.h: common header for cjkcodecs |
| 3 | * |
| 4 | * Written by Hye-Shik Chang <perky@FreeBSD.org> |
Hye-Shik Chang | f5a149a | 2004-08-19 17:49:56 +0000 | [diff] [blame] | 5 | * $CJKCodecs: cjkcodecs.h,v 1.6 2004/07/18 15:22:31 perky Exp $ |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _CJKCODECS_H_ |
| 9 | #define _CJKCODECS_H_ |
| 10 | |
| 11 | #include "Python.h" |
| 12 | #include "multibytecodec.h" |
| 13 | |
| 14 | |
| 15 | #define UNIINV Py_UNICODE_REPLACEMENT_CHARACTER |
| 16 | #define NOCHAR 0xFFFF |
| 17 | #define MULTIC 0xFFFE |
| 18 | #define DBCINV 0xFFFD |
| 19 | |
| 20 | /* shorter macros to save source size of mapping tables */ |
| 21 | #define U UNIINV |
| 22 | #define N NOCHAR |
| 23 | #define M MULTIC |
| 24 | #define D DBCINV |
| 25 | |
| 26 | struct dbcs_index { |
| 27 | const ucs2_t *map; |
| 28 | unsigned char bottom, top; |
| 29 | }; |
| 30 | typedef struct dbcs_index decode_map; |
| 31 | |
| 32 | struct widedbcs_index { |
| 33 | const ucs4_t *map; |
| 34 | unsigned char bottom, top; |
| 35 | }; |
| 36 | typedef struct widedbcs_index widedecode_map; |
| 37 | |
| 38 | struct unim_index { |
| 39 | const DBCHAR *map; |
| 40 | unsigned char bottom, top; |
| 41 | }; |
| 42 | typedef struct unim_index encode_map; |
| 43 | |
| 44 | struct unim_index_bytebased { |
| 45 | const unsigned char *map; |
| 46 | unsigned char bottom, top; |
| 47 | }; |
| 48 | |
| 49 | struct dbcs_map { |
| 50 | const char *charset; |
| 51 | const struct unim_index *encmap; |
| 52 | const struct dbcs_index *decmap; |
| 53 | }; |
| 54 | |
| 55 | struct pair_encodemap { |
| 56 | ucs4_t uniseq; |
| 57 | DBCHAR code; |
| 58 | }; |
| 59 | |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 60 | static const MultibyteCodec *codec_list; |
| 61 | static const struct dbcs_map *mapping_list; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 62 | |
| 63 | #define CODEC_INIT(encoding) \ |
| 64 | static int encoding##_codec_init(const void *config) |
| 65 | |
| 66 | #define ENCODER_INIT(encoding) \ |
| 67 | static int encoding##_encode_init( \ |
| 68 | MultibyteCodec_State *state, const void *config) |
| 69 | #define ENCODER(encoding) \ |
| 70 | static int encoding##_encode( \ |
| 71 | MultibyteCodec_State *state, const void *config, \ |
| 72 | const Py_UNICODE **inbuf, size_t inleft, \ |
| 73 | unsigned char **outbuf, size_t outleft, int flags) |
| 74 | #define ENCODER_RESET(encoding) \ |
| 75 | static int encoding##_encode_reset( \ |
| 76 | MultibyteCodec_State *state, const void *config, \ |
| 77 | unsigned char **outbuf, size_t outleft) |
| 78 | |
| 79 | #define DECODER_INIT(encoding) \ |
| 80 | static int encoding##_decode_init( \ |
| 81 | MultibyteCodec_State *state, const void *config) |
| 82 | #define DECODER(encoding) \ |
| 83 | static int encoding##_decode( \ |
| 84 | MultibyteCodec_State *state, const void *config, \ |
| 85 | const unsigned char **inbuf, size_t inleft, \ |
| 86 | Py_UNICODE **outbuf, size_t outleft) |
| 87 | #define DECODER_RESET(encoding) \ |
| 88 | static int encoding##_decode_reset( \ |
| 89 | MultibyteCodec_State *state, const void *config) |
| 90 | |
| 91 | #if Py_UNICODE_SIZE == 4 |
| 92 | #define UCS4INVALID(code) \ |
| 93 | if ((code) > 0xFFFF) \ |
| 94 | return 1; |
| 95 | #else |
| 96 | #define UCS4INVALID(code) \ |
| 97 | if (0) ; |
| 98 | #endif |
| 99 | |
| 100 | #define NEXT_IN(i) \ |
| 101 | (*inbuf) += (i); \ |
| 102 | (inleft) -= (i); |
| 103 | #define NEXT_OUT(o) \ |
| 104 | (*outbuf) += (o); \ |
| 105 | (outleft) -= (o); |
| 106 | #define NEXT(i, o) \ |
| 107 | NEXT_IN(i) NEXT_OUT(o) |
| 108 | |
| 109 | #define REQUIRE_INBUF(n) \ |
| 110 | if (inleft < (n)) \ |
| 111 | return MBERR_TOOFEW; |
| 112 | #define REQUIRE_OUTBUF(n) \ |
| 113 | if (outleft < (n)) \ |
| 114 | return MBERR_TOOSMALL; |
| 115 | |
| 116 | #define IN1 ((*inbuf)[0]) |
| 117 | #define IN2 ((*inbuf)[1]) |
| 118 | #define IN3 ((*inbuf)[2]) |
| 119 | #define IN4 ((*inbuf)[3]) |
| 120 | |
| 121 | #define OUT1(c) ((*outbuf)[0]) = (c); |
| 122 | #define OUT2(c) ((*outbuf)[1]) = (c); |
| 123 | #define OUT3(c) ((*outbuf)[2]) = (c); |
| 124 | #define OUT4(c) ((*outbuf)[3]) = (c); |
| 125 | |
| 126 | #define WRITE1(c1) \ |
| 127 | REQUIRE_OUTBUF(1) \ |
| 128 | (*outbuf)[0] = (c1); |
| 129 | #define WRITE2(c1, c2) \ |
| 130 | REQUIRE_OUTBUF(2) \ |
| 131 | (*outbuf)[0] = (c1); \ |
| 132 | (*outbuf)[1] = (c2); |
| 133 | #define WRITE3(c1, c2, c3) \ |
| 134 | REQUIRE_OUTBUF(3) \ |
| 135 | (*outbuf)[0] = (c1); \ |
| 136 | (*outbuf)[1] = (c2); \ |
| 137 | (*outbuf)[2] = (c3); |
| 138 | #define WRITE4(c1, c2, c3, c4) \ |
| 139 | REQUIRE_OUTBUF(4) \ |
| 140 | (*outbuf)[0] = (c1); \ |
| 141 | (*outbuf)[1] = (c2); \ |
| 142 | (*outbuf)[2] = (c3); \ |
| 143 | (*outbuf)[3] = (c4); |
| 144 | |
| 145 | #if Py_UNICODE_SIZE == 2 |
| 146 | # define WRITEUCS4(c) \ |
| 147 | REQUIRE_OUTBUF(2) \ |
| 148 | (*outbuf)[0] = 0xd800 + (((c) - 0x10000) >> 10); \ |
| 149 | (*outbuf)[1] = 0xdc00 + (((c) - 0x10000) & 0x3ff); \ |
| 150 | NEXT_OUT(2) |
| 151 | #else |
| 152 | # define WRITEUCS4(c) \ |
| 153 | REQUIRE_OUTBUF(1) \ |
| 154 | **outbuf = (Py_UNICODE)(c); \ |
| 155 | NEXT_OUT(1) |
| 156 | #endif |
| 157 | |
| 158 | #define _TRYMAP_ENC(m, assi, val) \ |
| 159 | if ((m)->map != NULL && (val) >= (m)->bottom && \ |
| 160 | (val)<= (m)->top && ((assi) = (m)->map[(val) - \ |
| 161 | (m)->bottom]) != NOCHAR) |
| 162 | #define TRYMAP_ENC(charset, assi, uni) \ |
| 163 | _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff) |
| 164 | #define _TRYMAP_DEC(m, assi, val) \ |
| 165 | if ((m)->map != NULL && (val) >= (m)->bottom && \ |
| 166 | (val)<= (m)->top && ((assi) = (m)->map[(val) - \ |
| 167 | (m)->bottom]) != UNIINV) |
| 168 | #define TRYMAP_DEC(charset, assi, c1, c2) \ |
| 169 | _TRYMAP_DEC(&charset##_decmap[c1], assi, c2) |
| 170 | |
| 171 | #define _TRYMAP_ENC_MPLANE(m, assplane, asshi, asslo, val) \ |
| 172 | if ((m)->map != NULL && (val) >= (m)->bottom && \ |
| 173 | (val)<= (m)->top && \ |
| 174 | ((assplane) = (m)->map[((val) - (m)->bottom)*3]) != 0 && \ |
| 175 | (((asshi) = (m)->map[((val) - (m)->bottom)*3 + 1]), 1) && \ |
| 176 | (((asslo) = (m)->map[((val) - (m)->bottom)*3 + 2]), 1)) |
| 177 | #define TRYMAP_ENC_MPLANE(charset, assplane, asshi, asslo, uni) \ |
| 178 | _TRYMAP_ENC_MPLANE(&charset##_encmap[(uni) >> 8], \ |
| 179 | assplane, asshi, asslo, (uni) & 0xff) |
| 180 | #define TRYMAP_DEC_MPLANE(charset, assi, plane, c1, c2) \ |
| 181 | _TRYMAP_DEC(&charset##_decmap[plane][c1], assi, c2) |
| 182 | |
| 183 | #if Py_UNICODE_SIZE == 2 |
| 184 | #define DECODE_SURROGATE(c) \ |
| 185 | if (c >> 10 == 0xd800 >> 10) { /* high surrogate */ \ |
| 186 | REQUIRE_INBUF(2) \ |
| 187 | if (IN2 >> 10 == 0xdc00 >> 10) { /* low surrogate */ \ |
| 188 | c = 0x10000 + ((ucs4_t)(c - 0xd800) << 10) + \ |
| 189 | ((ucs4_t)(IN2) - 0xdc00); \ |
| 190 | } \ |
| 191 | } |
| 192 | #define GET_INSIZE(c) ((c) > 0xffff ? 2 : 1) |
| 193 | #else |
| 194 | #define DECODE_SURROGATE(c) {;} |
| 195 | #define GET_INSIZE(c) 1 |
| 196 | #endif |
| 197 | |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 198 | #define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 199 | #define MAPPING_ENCONLY(enc) {#enc, (void*)enc##_encmap, NULL}, |
| 200 | #define MAPPING_DECONLY(enc) {#enc, NULL, (void*)enc##_decmap}, |
| 201 | #define MAPPING_ENCDEC(enc) {#enc, (void*)enc##_encmap, (void*)enc##_decmap}, |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 202 | #define END_MAPPINGS_LIST \ |
| 203 | {"", NULL, NULL} }; \ |
| 204 | static const struct dbcs_map *mapping_list = \ |
| 205 | (const struct dbcs_map *)_mapping_list; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 206 | |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 207 | #define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 208 | #define _STATEFUL_METHODS(enc) \ |
| 209 | enc##_encode, \ |
| 210 | enc##_encode_init, \ |
| 211 | enc##_encode_reset, \ |
| 212 | enc##_decode, \ |
| 213 | enc##_decode_init, \ |
| 214 | enc##_decode_reset, |
| 215 | #define _STATELESS_METHODS(enc) \ |
| 216 | enc##_encode, NULL, NULL, \ |
| 217 | enc##_decode, NULL, NULL, |
| 218 | #define CODEC_STATEFUL(enc) { \ |
| 219 | #enc, NULL, NULL, \ |
| 220 | _STATEFUL_METHODS(enc) \ |
| 221 | }, |
| 222 | #define CODEC_STATELESS(enc) { \ |
| 223 | #enc, NULL, NULL, \ |
| 224 | _STATELESS_METHODS(enc) \ |
| 225 | }, |
| 226 | #define CODEC_STATELESS_WINIT(enc) { \ |
| 227 | #enc, NULL, \ |
| 228 | enc##_codec_init, \ |
| 229 | _STATELESS_METHODS(enc) \ |
| 230 | }, |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 231 | #define END_CODECS_LIST \ |
| 232 | {"", NULL,} }; \ |
Hye-Shik Chang | f5a149a | 2004-08-19 17:49:56 +0000 | [diff] [blame] | 233 | static const MultibyteCodec *codec_list = \ |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 234 | (const MultibyteCodec *)_codec_list; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 235 | |
| 236 | static PyObject * |
| 237 | getmultibytecodec(void) |
| 238 | { |
| 239 | static PyObject *cofunc = NULL; |
| 240 | |
| 241 | if (cofunc == NULL) { |
| 242 | PyObject *mod = PyImport_ImportModule("_multibytecodec"); |
| 243 | if (mod == NULL) |
| 244 | return NULL; |
| 245 | cofunc = PyObject_GetAttrString(mod, "__create_codec"); |
| 246 | Py_DECREF(mod); |
| 247 | } |
| 248 | return cofunc; |
| 249 | } |
| 250 | |
| 251 | static PyObject * |
| 252 | getcodec(PyObject *self, PyObject *encoding) |
| 253 | { |
| 254 | PyObject *codecobj, *r, *cofunc; |
| 255 | const MultibyteCodec *codec; |
| 256 | const char *enc; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 257 | |
| 258 | if (!PyString_Check(encoding)) { |
| 259 | PyErr_SetString(PyExc_TypeError, |
| 260 | "encoding name must be a string."); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | cofunc = getmultibytecodec(); |
| 265 | if (cofunc == NULL) |
| 266 | return NULL; |
| 267 | |
| 268 | enc = PyString_AS_STRING(encoding); |
| 269 | for (codec = codec_list; codec->encoding[0]; codec++) |
| 270 | if (strcmp(codec->encoding, enc) == 0) |
| 271 | break; |
| 272 | |
| 273 | if (codec->encoding[0] == '\0') { |
| 274 | PyErr_SetString(PyExc_LookupError, |
| 275 | "no such codec is supported."); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | codecobj = PyCObject_FromVoidPtr((void *)codec, NULL); |
| 280 | if (codecobj == NULL) |
| 281 | return NULL; |
| 282 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 283 | r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL); |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 284 | Py_DECREF(codecobj); |
| 285 | |
| 286 | return r; |
| 287 | } |
| 288 | |
| 289 | static struct PyMethodDef __methods[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 290 | {"getcodec", (PyCFunction)getcodec, METH_O, ""}, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 291 | {NULL, NULL}, |
| 292 | }; |
| 293 | |
| 294 | static int |
| 295 | register_maps(PyObject *module) |
| 296 | { |
| 297 | const struct dbcs_map *h; |
| 298 | |
| 299 | for (h = mapping_list; h->charset[0] != '\0'; h++) { |
| 300 | char mhname[256] = "__map_"; |
| 301 | int r; |
| 302 | strcpy(mhname + sizeof("__map_") - 1, h->charset); |
| 303 | r = PyModule_AddObject(module, mhname, |
| 304 | PyCObject_FromVoidPtr((void *)h, NULL)); |
| 305 | if (r == -1) |
| 306 | return -1; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | #ifdef USING_BINARY_PAIR_SEARCH |
| 312 | static DBCHAR |
| 313 | find_pairencmap(ucs2_t body, ucs2_t modifier, |
| 314 | const struct pair_encodemap *haystack, int haystacksize) |
| 315 | { |
| 316 | int pos, min, max; |
| 317 | ucs4_t value = body << 16 | modifier; |
| 318 | |
| 319 | min = 0; |
| 320 | max = haystacksize; |
| 321 | |
| 322 | for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1) |
| 323 | if (value < haystack[pos].uniseq) { |
| 324 | if (max == pos) break; |
| 325 | else max = pos; |
| 326 | } |
| 327 | else if (value > haystack[pos].uniseq) { |
| 328 | if (min == pos) break; |
| 329 | else min = pos; |
| 330 | } |
| 331 | else |
| 332 | break; |
| 333 | |
| 334 | if (value == haystack[pos].uniseq) |
| 335 | return haystack[pos].code; |
| 336 | else |
| 337 | return DBCINV; |
| 338 | } |
| 339 | #endif |
| 340 | |
| 341 | #ifdef USING_IMPORTED_MAPS |
| 342 | #define IMPORT_MAP(locale, charset, encmap, decmap) \ |
| 343 | importmap("_codecs_" #locale, "__map_" #charset, \ |
| 344 | (const void**)encmap, (const void**)decmap) |
| 345 | |
| 346 | static int |
| 347 | importmap(const char *modname, const char *symbol, |
| 348 | const void **encmap, const void **decmap) |
| 349 | { |
| 350 | PyObject *o, *mod; |
| 351 | |
| 352 | mod = PyImport_ImportModule((char *)modname); |
| 353 | if (mod == NULL) |
| 354 | return -1; |
| 355 | |
| 356 | o = PyObject_GetAttrString(mod, (char*)symbol); |
| 357 | if (o == NULL) |
| 358 | goto errorexit; |
| 359 | else if (!PyCObject_Check(o)) { |
| 360 | PyErr_SetString(PyExc_ValueError, |
| 361 | "map data must be a CObject."); |
| 362 | goto errorexit; |
| 363 | } |
| 364 | else { |
| 365 | struct dbcs_map *map; |
| 366 | map = PyCObject_AsVoidPtr(o); |
| 367 | if (encmap != NULL) |
| 368 | *encmap = map->encmap; |
| 369 | if (decmap != NULL) |
| 370 | *decmap = map->decmap; |
| 371 | Py_DECREF(o); |
| 372 | } |
| 373 | |
| 374 | Py_DECREF(mod); |
| 375 | return 0; |
| 376 | |
| 377 | errorexit: |
| 378 | Py_DECREF(mod); |
| 379 | return -1; |
| 380 | } |
| 381 | #endif |
| 382 | |
| 383 | #define I_AM_A_MODULE_FOR(loc) \ |
| 384 | void \ |
| 385 | init_codecs_##loc(void) \ |
| 386 | { \ |
| 387 | PyObject *m = Py_InitModule("_codecs_" #loc, __methods);\ |
| 388 | (void)register_maps(m); \ |
| 389 | } |
| 390 | |
| 391 | #endif |