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 | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef _CJKCODECS_H_ |
| 8 | #define _CJKCODECS_H_ |
| 9 | |
Hye-Shik Chang | 4b96c13 | 2006-03-04 16:08:19 +0000 | [diff] [blame] | 10 | #define PY_SSIZE_T_CLEAN |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 11 | #include "Python.h" |
| 12 | #include "multibytecodec.h" |
| 13 | |
| 14 | |
Hye-Shik Chang | 331649a | 2005-10-06 15:51:59 +0000 | [diff] [blame] | 15 | /* a unicode "undefined" codepoint */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 16 | #define UNIINV 0xFFFE |
Hye-Shik Chang | 331649a | 2005-10-06 15:51:59 +0000 | [diff] [blame] | 17 | |
| 18 | /* internal-use DBCS codepoints which aren't used by any charsets */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | #define NOCHAR 0xFFFF |
| 20 | #define MULTIC 0xFFFE |
| 21 | #define DBCINV 0xFFFD |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 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 | |
| 29 | struct dbcs_index { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | const ucs2_t *map; |
| 31 | unsigned char bottom, top; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 32 | }; |
| 33 | typedef struct dbcs_index decode_map; |
| 34 | |
| 35 | struct widedbcs_index { |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 36 | const Py_UCS4 *map; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | unsigned char bottom, top; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 38 | }; |
| 39 | typedef struct widedbcs_index widedecode_map; |
| 40 | |
| 41 | struct unim_index { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | const DBCHAR *map; |
| 43 | unsigned char bottom, top; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 44 | }; |
| 45 | typedef struct unim_index encode_map; |
| 46 | |
| 47 | struct unim_index_bytebased { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | const unsigned char *map; |
| 49 | unsigned char bottom, top; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct dbcs_map { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | const char *charset; |
| 54 | const struct unim_index *encmap; |
| 55 | const struct dbcs_index *decmap; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct pair_encodemap { |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 59 | Py_UCS4 uniseq; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | DBCHAR code; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 63 | static const MultibyteCodec *codec_list; |
| 64 | static const struct dbcs_map *mapping_list; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | #define CODEC_INIT(encoding) \ |
| 67 | static int encoding##_codec_init(const void *config) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 68 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | #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, \ |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 75 | int kind, void *data, \ |
| 76 | Py_ssize_t *inpos, Py_ssize_t inlen, \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | unsigned char **outbuf, Py_ssize_t outleft, int flags) |
| 78 | #define ENCODER_RESET(encoding) \ |
| 79 | static Py_ssize_t encoding##_encode_reset( \ |
| 80 | MultibyteCodec_State *state, const void *config, \ |
| 81 | unsigned char **outbuf, Py_ssize_t outleft) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 82 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | #define DECODER_INIT(encoding) \ |
| 84 | static int encoding##_decode_init( \ |
| 85 | MultibyteCodec_State *state, const void *config) |
| 86 | #define DECODER(encoding) \ |
| 87 | static Py_ssize_t encoding##_decode( \ |
| 88 | MultibyteCodec_State *state, const void *config, \ |
| 89 | const unsigned char **inbuf, Py_ssize_t inleft, \ |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 90 | _PyUnicodeWriter *writer) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | #define DECODER_RESET(encoding) \ |
| 92 | static Py_ssize_t encoding##_decode_reset( \ |
| 93 | MultibyteCodec_State *state, const void *config) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | #define NEXT_IN(i) \ |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 96 | do { \ |
| 97 | (*inbuf) += (i); \ |
| 98 | (inleft) -= (i); \ |
| 99 | } while (0) |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 100 | #define NEXT_INCHAR(i) \ |
| 101 | do { \ |
| 102 | (*inpos) += (i); \ |
| 103 | } while (0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | #define NEXT_OUT(o) \ |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 105 | do { \ |
| 106 | (*outbuf) += (o); \ |
| 107 | (outleft) -= (o); \ |
| 108 | } while (0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | #define NEXT(i, o) \ |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 110 | do { \ |
| 111 | NEXT_INCHAR(i); \ |
| 112 | NEXT_OUT(o); \ |
| 113 | } while (0) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | #define REQUIRE_INBUF(n) \ |
| 116 | if (inleft < (n)) \ |
| 117 | return MBERR_TOOFEW; |
| 118 | #define REQUIRE_OUTBUF(n) \ |
| 119 | if (outleft < (n)) \ |
| 120 | return MBERR_TOOSMALL; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 121 | |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 122 | #define INBYTE1 ((*inbuf)[0]) |
| 123 | #define INBYTE2 ((*inbuf)[1]) |
| 124 | #define INBYTE3 ((*inbuf)[2]) |
| 125 | #define INBYTE4 ((*inbuf)[3]) |
| 126 | |
| 127 | #define INCHAR1 PyUnicode_READ(kind, data, *inpos) |
| 128 | #define INCHAR2 PyUnicode_READ(kind, data, *inpos + 1) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 129 | |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 130 | #define OUTCHAR(c) \ |
| 131 | do { \ |
| 132 | if (_PyUnicodeWriter_WriteChar(writer, (c)) < 0) \ |
| 133 | return MBERR_TOOSMALL; \ |
| 134 | } while (0) |
| 135 | |
| 136 | #define OUTCHAR2(c1, c2) \ |
| 137 | do { \ |
| 138 | Py_UCS4 _c1 = (c1); \ |
| 139 | Py_UCS4 _c2 = (c2); \ |
| 140 | if (_PyUnicodeWriter_Prepare(writer, 2, Py_MAX(_c1, c2)) < 0) \ |
| 141 | return MBERR_TOOSMALL; \ |
| 142 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, _c1); \ |
| 143 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos + 1, _c2); \ |
| 144 | writer->pos += 2; \ |
| 145 | } while (0) |
| 146 | |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 147 | #define OUTBYTE1(c) ((*outbuf)[0]) = (c); |
| 148 | #define OUTBYTE2(c) ((*outbuf)[1]) = (c); |
| 149 | #define OUTBYTE3(c) ((*outbuf)[2]) = (c); |
| 150 | #define OUTBYTE4(c) ((*outbuf)[3]) = (c); |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 151 | |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 152 | #define WRITEBYTE1(c1) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | REQUIRE_OUTBUF(1) \ |
| 154 | (*outbuf)[0] = (c1); |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 155 | #define WRITEBYTE2(c1, c2) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | REQUIRE_OUTBUF(2) \ |
| 157 | (*outbuf)[0] = (c1); \ |
| 158 | (*outbuf)[1] = (c2); |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 159 | #define WRITEBYTE3(c1, c2, c3) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | REQUIRE_OUTBUF(3) \ |
| 161 | (*outbuf)[0] = (c1); \ |
| 162 | (*outbuf)[1] = (c2); \ |
| 163 | (*outbuf)[2] = (c3); |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 164 | #define WRITEBYTE4(c1, c2, c3, c4) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | REQUIRE_OUTBUF(4) \ |
| 166 | (*outbuf)[0] = (c1); \ |
| 167 | (*outbuf)[1] = (c2); \ |
| 168 | (*outbuf)[2] = (c3); \ |
| 169 | (*outbuf)[3] = (c4); |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | #define _TRYMAP_ENC(m, assi, val) \ |
| 172 | ((m)->map != NULL && (val) >= (m)->bottom && \ |
| 173 | (val)<= (m)->top && ((assi) = (m)->map[(val) - \ |
| 174 | (m)->bottom]) != NOCHAR) |
| 175 | #define TRYMAP_ENC_COND(charset, assi, uni) \ |
| 176 | _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff) |
| 177 | #define TRYMAP_ENC(charset, assi, uni) \ |
| 178 | if TRYMAP_ENC_COND(charset, assi, uni) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 179 | |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 180 | Py_LOCAL_INLINE(int) |
| 181 | _TRYMAP_DEC_WRITE(_PyUnicodeWriter *writer, Py_UCS4 c) |
| 182 | { |
| 183 | if (c == UNIINV || _PyUnicodeWriter_WriteChar(writer, c) < 0) |
| 184 | return UNIINV; |
| 185 | else |
| 186 | return c; |
| 187 | } |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 188 | |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 189 | #define _TRYMAP_DEC(m, writer, val) \ |
| 190 | ((m)->map != NULL && \ |
| 191 | (val) >= (m)->bottom && \ |
| 192 | (val)<= (m)->top && \ |
| 193 | _TRYMAP_DEC_WRITE(writer, (m)->map[(val) - (m)->bottom]) != UNIINV) |
| 194 | #define _TRYMAP_DEC_CHAR(m, assi, val) \ |
| 195 | ((m)->map != NULL && \ |
| 196 | (val) >= (m)->bottom && \ |
| 197 | (val)<= (m)->top && \ |
| 198 | ((assi) = (m)->map[(val) - (m)->bottom]) != UNIINV) |
| 199 | #define TRYMAP_DEC(charset, writer, c1, c2) \ |
| 200 | if _TRYMAP_DEC(&charset##_decmap[c1], writer, c2) |
| 201 | #define TRYMAP_DEC_CHAR(charset, assi, c1, c2) \ |
| 202 | if _TRYMAP_DEC_CHAR(&charset##_decmap[c1], assi, c2) |
| 203 | |
| 204 | #define _TRYMAP_ENC_MPLANE(m, assplane, asshi, asslo, val) \ |
| 205 | ((m)->map != NULL && (val) >= (m)->bottom && \ |
| 206 | (val)<= (m)->top && \ |
| 207 | ((assplane) = (m)->map[((val) - (m)->bottom)*3]) != 0 && \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | (((asshi) = (m)->map[((val) - (m)->bottom)*3 + 1]), 1) && \ |
| 209 | (((asslo) = (m)->map[((val) - (m)->bottom)*3 + 2]), 1)) |
| 210 | #define TRYMAP_ENC_MPLANE(charset, assplane, asshi, asslo, uni) \ |
| 211 | if _TRYMAP_ENC_MPLANE(&charset##_encmap[(uni) >> 8], \ |
| 212 | assplane, asshi, asslo, (uni) & 0xff) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 213 | #define TRYMAP_DEC_MPLANE(charset, writer, plane, c1, c2) \ |
| 214 | if _TRYMAP_DEC(&charset##_decmap[plane][c1], writer, c2) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 215 | |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 216 | #define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 217 | #define MAPPING_ENCONLY(enc) {#enc, (void*)enc##_encmap, NULL}, |
| 218 | #define MAPPING_DECONLY(enc) {#enc, NULL, (void*)enc##_decmap}, |
| 219 | #define MAPPING_ENCDEC(enc) {#enc, (void*)enc##_encmap, (void*)enc##_decmap}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | #define END_MAPPINGS_LIST \ |
| 221 | {"", NULL, NULL} }; \ |
| 222 | static const struct dbcs_map *mapping_list = \ |
| 223 | (const struct dbcs_map *)_mapping_list; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 224 | |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 225 | #define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | #define _STATEFUL_METHODS(enc) \ |
| 227 | enc##_encode, \ |
| 228 | enc##_encode_init, \ |
| 229 | enc##_encode_reset, \ |
| 230 | enc##_decode, \ |
| 231 | enc##_decode_init, \ |
| 232 | enc##_decode_reset, |
| 233 | #define _STATELESS_METHODS(enc) \ |
| 234 | enc##_encode, NULL, NULL, \ |
| 235 | enc##_decode, NULL, NULL, |
| 236 | #define CODEC_STATEFUL(enc) { \ |
| 237 | #enc, NULL, NULL, \ |
| 238 | _STATEFUL_METHODS(enc) \ |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 239 | }, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | #define CODEC_STATELESS(enc) { \ |
| 241 | #enc, NULL, NULL, \ |
| 242 | _STATELESS_METHODS(enc) \ |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 243 | }, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | #define CODEC_STATELESS_WINIT(enc) { \ |
| 245 | #enc, NULL, \ |
| 246 | enc##_codec_init, \ |
| 247 | _STATELESS_METHODS(enc) \ |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 248 | }, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | #define END_CODECS_LIST \ |
| 250 | {"", NULL,} }; \ |
| 251 | static const MultibyteCodec *codec_list = \ |
| 252 | (const MultibyteCodec *)_codec_list; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 253 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 254 | |
| 255 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 256 | static PyObject * |
| 257 | getmultibytecodec(void) |
| 258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | static PyObject *cofunc = NULL; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | if (cofunc == NULL) { |
| 262 | PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec"); |
| 263 | if (mod == NULL) |
| 264 | return NULL; |
| 265 | cofunc = PyObject_GetAttrString(mod, "__create_codec"); |
| 266 | Py_DECREF(mod); |
| 267 | } |
| 268 | return cofunc; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static PyObject * |
| 272 | getcodec(PyObject *self, PyObject *encoding) |
| 273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | PyObject *codecobj, *r, *cofunc; |
| 275 | const MultibyteCodec *codec; |
| 276 | const char *enc; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | if (!PyUnicode_Check(encoding)) { |
| 279 | PyErr_SetString(PyExc_TypeError, |
| 280 | "encoding name must be a string."); |
| 281 | return NULL; |
| 282 | } |
| 283 | enc = _PyUnicode_AsString(encoding); |
| 284 | if (enc == NULL) |
| 285 | return NULL; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | cofunc = getmultibytecodec(); |
| 288 | if (cofunc == NULL) |
| 289 | return NULL; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | for (codec = codec_list; codec->encoding[0]; codec++) |
| 292 | if (strcmp(codec->encoding, enc) == 0) |
| 293 | break; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | if (codec->encoding[0] == '\0') { |
| 296 | PyErr_SetString(PyExc_LookupError, |
| 297 | "no such codec is supported."); |
| 298 | return NULL; |
| 299 | } |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL); |
| 302 | if (codecobj == NULL) |
| 303 | return NULL; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL); |
| 306 | Py_DECREF(codecobj); |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | return r; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static struct PyMethodDef __methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | {"getcodec", (PyCFunction)getcodec, METH_O, ""}, |
| 313 | {NULL, NULL}, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | static int |
| 317 | register_maps(PyObject *module) |
| 318 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | const struct dbcs_map *h; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | for (h = mapping_list; h->charset[0] != '\0'; h++) { |
| 322 | char mhname[256] = "__map_"; |
| 323 | int r; |
| 324 | strcpy(mhname + sizeof("__map_") - 1, h->charset); |
| 325 | r = PyModule_AddObject(module, mhname, |
| 326 | PyCapsule_New((void *)h, PyMultibyteCodec_CAPSULE_NAME, NULL)); |
| 327 | if (r == -1) |
| 328 | return -1; |
| 329 | } |
| 330 | return 0; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | #ifdef USING_BINARY_PAIR_SEARCH |
| 334 | static DBCHAR |
| 335 | find_pairencmap(ucs2_t body, ucs2_t modifier, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | const struct pair_encodemap *haystack, int haystacksize) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 337 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | int pos, min, max; |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 339 | Py_UCS4 value = body << 16 | modifier; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | min = 0; |
| 342 | max = haystacksize; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1) |
| 345 | if (value < haystack[pos].uniseq) { |
| 346 | if (max == pos) break; |
| 347 | else max = pos; |
| 348 | } |
| 349 | else if (value > haystack[pos].uniseq) { |
| 350 | if (min == pos) break; |
| 351 | else min = pos; |
| 352 | } |
| 353 | else |
| 354 | break; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | if (value == haystack[pos].uniseq) |
| 357 | return haystack[pos].code; |
| 358 | else |
| 359 | return DBCINV; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 360 | } |
| 361 | #endif |
| 362 | |
| 363 | #ifdef USING_IMPORTED_MAPS |
| 364 | #define IMPORT_MAP(locale, charset, encmap, decmap) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | importmap("_codecs_" #locale, "__map_" #charset, \ |
| 366 | (const void**)encmap, (const void**)decmap) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 367 | |
| 368 | static int |
| 369 | importmap(const char *modname, const char *symbol, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | const void **encmap, const void **decmap) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 371 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | PyObject *o, *mod; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | mod = PyImport_ImportModule((char *)modname); |
| 375 | if (mod == NULL) |
| 376 | return -1; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | o = PyObject_GetAttrString(mod, (char*)symbol); |
| 379 | if (o == NULL) |
| 380 | goto errorexit; |
| 381 | else if (!PyCapsule_IsValid(o, PyMultibyteCodec_CAPSULE_NAME)) { |
| 382 | PyErr_SetString(PyExc_ValueError, |
| 383 | "map data must be a Capsule."); |
| 384 | goto errorexit; |
| 385 | } |
| 386 | else { |
| 387 | struct dbcs_map *map; |
| 388 | map = PyCapsule_GetPointer(o, PyMultibyteCodec_CAPSULE_NAME); |
| 389 | if (encmap != NULL) |
| 390 | *encmap = map->encmap; |
| 391 | if (decmap != NULL) |
| 392 | *decmap = map->decmap; |
| 393 | Py_DECREF(o); |
| 394 | } |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 395 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | Py_DECREF(mod); |
| 397 | return 0; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 398 | |
| 399 | errorexit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | Py_DECREF(mod); |
| 401 | return -1; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 402 | } |
| 403 | #endif |
| 404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | #define I_AM_A_MODULE_FOR(loc) \ |
| 406 | static struct PyModuleDef __module = { \ |
| 407 | PyModuleDef_HEAD_INIT, \ |
| 408 | "_codecs_"#loc, \ |
| 409 | NULL, \ |
| 410 | 0, \ |
| 411 | __methods, \ |
| 412 | NULL, \ |
| 413 | NULL, \ |
| 414 | NULL, \ |
| 415 | NULL \ |
| 416 | }; \ |
| 417 | PyObject* \ |
| 418 | PyInit__codecs_##loc(void) \ |
| 419 | { \ |
| 420 | PyObject *m = PyModule_Create(&__module); \ |
| 421 | if (m != NULL) \ |
| 422 | (void)register_maps(m); \ |
| 423 | return m; \ |
| 424 | } |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 425 | |
| 426 | #endif |