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 { \ |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 111 | NEXT_INCHAR(i); \ |
| 112 | NEXT_OUT(o); \ |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 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) \ |
Victor Stinner | 28c63f7 | 2013-10-29 00:59:44 +0100 | [diff] [blame] | 116 | do { \ |
| 117 | if (inleft < (n)) \ |
| 118 | return MBERR_TOOFEW; \ |
| 119 | } while (0) |
| 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | #define REQUIRE_OUTBUF(n) \ |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 122 | do { \ |
| 123 | if (outleft < (n)) \ |
| 124 | return MBERR_TOOSMALL; \ |
| 125 | } while (0) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 126 | |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 127 | #define INBYTE1 ((*inbuf)[0]) |
| 128 | #define INBYTE2 ((*inbuf)[1]) |
| 129 | #define INBYTE3 ((*inbuf)[2]) |
| 130 | #define INBYTE4 ((*inbuf)[3]) |
| 131 | |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 132 | #define INCHAR1 (PyUnicode_READ(kind, data, *inpos)) |
| 133 | #define INCHAR2 (PyUnicode_READ(kind, data, *inpos + 1)) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 134 | |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 135 | #define OUTCHAR(c) \ |
| 136 | do { \ |
| 137 | if (_PyUnicodeWriter_WriteChar(writer, (c)) < 0) \ |
Victor Stinner | d1f9942 | 2013-07-16 21:41:43 +0200 | [diff] [blame] | 138 | return MBERR_EXCEPTION; \ |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 139 | } while (0) |
| 140 | |
| 141 | #define OUTCHAR2(c1, c2) \ |
| 142 | do { \ |
| 143 | Py_UCS4 _c1 = (c1); \ |
| 144 | Py_UCS4 _c2 = (c2); \ |
| 145 | if (_PyUnicodeWriter_Prepare(writer, 2, Py_MAX(_c1, c2)) < 0) \ |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 146 | return MBERR_EXCEPTION; \ |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 147 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, _c1); \ |
| 148 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos + 1, _c2); \ |
| 149 | writer->pos += 2; \ |
| 150 | } while (0) |
| 151 | |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 152 | #define OUTBYTE1(c) \ |
| 153 | do { ((*outbuf)[0]) = (c); } while (0) |
| 154 | #define OUTBYTE2(c) \ |
| 155 | do { ((*outbuf)[1]) = (c); } while (0) |
| 156 | #define OUTBYTE3(c) \ |
| 157 | do { ((*outbuf)[2]) = (c); } while (0) |
| 158 | #define OUTBYTE4(c) \ |
| 159 | do { ((*outbuf)[3]) = (c); } while (0) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 160 | |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 161 | #define WRITEBYTE1(c1) \ |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 162 | do { \ |
| 163 | REQUIRE_OUTBUF(1); \ |
| 164 | (*outbuf)[0] = (c1); \ |
| 165 | } while (0) |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 166 | #define WRITEBYTE2(c1, c2) \ |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 167 | do { \ |
| 168 | REQUIRE_OUTBUF(2); \ |
| 169 | (*outbuf)[0] = (c1); \ |
| 170 | (*outbuf)[1] = (c2); \ |
| 171 | } while (0) |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 172 | #define WRITEBYTE3(c1, c2, c3) \ |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 173 | do { \ |
| 174 | REQUIRE_OUTBUF(3); \ |
| 175 | (*outbuf)[0] = (c1); \ |
| 176 | (*outbuf)[1] = (c2); \ |
| 177 | (*outbuf)[2] = (c3); \ |
| 178 | } while (0) |
Victor Stinner | d949126 | 2013-04-14 02:06:32 +0200 | [diff] [blame] | 179 | #define WRITEBYTE4(c1, c2, c3, c4) \ |
Victor Stinner | 14c9fea | 2013-10-29 00:19:27 +0100 | [diff] [blame] | 180 | do { \ |
| 181 | REQUIRE_OUTBUF(4); \ |
| 182 | (*outbuf)[0] = (c1); \ |
| 183 | (*outbuf)[1] = (c2); \ |
| 184 | (*outbuf)[2] = (c3); \ |
| 185 | (*outbuf)[3] = (c4); \ |
| 186 | } while (0) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | #define _TRYMAP_ENC(m, assi, val) \ |
| 189 | ((m)->map != NULL && (val) >= (m)->bottom && \ |
| 190 | (val)<= (m)->top && ((assi) = (m)->map[(val) - \ |
| 191 | (m)->bottom]) != NOCHAR) |
Victor Stinner | bd97ac3 | 2013-10-28 23:54:13 +0100 | [diff] [blame] | 192 | #define TRYMAP_ENC(charset, assi, uni) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 194 | |
Victor Stinner | 11bdf91 | 2013-10-28 23:18:39 +0100 | [diff] [blame] | 195 | #define _TRYMAP_DEC(m, assi, val) \ |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 196 | ((m)->map != NULL && \ |
| 197 | (val) >= (m)->bottom && \ |
| 198 | (val)<= (m)->top && \ |
| 199 | ((assi) = (m)->map[(val) - (m)->bottom]) != UNIINV) |
Victor Stinner | 11bdf91 | 2013-10-28 23:18:39 +0100 | [diff] [blame] | 200 | #define TRYMAP_DEC(charset, assi, c1, c2) \ |
| 201 | _TRYMAP_DEC(&charset##_decmap[c1], assi, c2) |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 202 | |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 203 | #define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 204 | #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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | #define END_MAPPINGS_LIST \ |
| 208 | {"", NULL, NULL} }; \ |
| 209 | static const struct dbcs_map *mapping_list = \ |
| 210 | (const struct dbcs_map *)_mapping_list; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 211 | |
Hye-Shik Chang | 64a9e38 | 2004-07-18 15:02:45 +0000 | [diff] [blame] | 212 | #define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | #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 Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 226 | }, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | #define CODEC_STATELESS(enc) { \ |
| 228 | #enc, NULL, NULL, \ |
| 229 | _STATELESS_METHODS(enc) \ |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 230 | }, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | #define CODEC_STATELESS_WINIT(enc) { \ |
| 232 | #enc, NULL, \ |
| 233 | enc##_codec_init, \ |
| 234 | _STATELESS_METHODS(enc) \ |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 235 | }, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | #define END_CODECS_LIST \ |
| 237 | {"", NULL,} }; \ |
| 238 | static const MultibyteCodec *codec_list = \ |
| 239 | (const MultibyteCodec *)_codec_list; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 240 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 241 | |
| 242 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 243 | static PyObject * |
| 244 | getmultibytecodec(void) |
| 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | static PyObject *cofunc = NULL; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | 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 Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static PyObject * |
| 259 | getcodec(PyObject *self, PyObject *encoding) |
| 260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | PyObject *codecobj, *r, *cofunc; |
| 262 | const MultibyteCodec *codec; |
| 263 | const char *enc; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | 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 Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | cofunc = getmultibytecodec(); |
| 275 | if (cofunc == NULL) |
| 276 | return NULL; |
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 | for (codec = codec_list; codec->encoding[0]; codec++) |
| 279 | if (strcmp(codec->encoding, enc) == 0) |
| 280 | break; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | if (codec->encoding[0] == '\0') { |
| 283 | PyErr_SetString(PyExc_LookupError, |
| 284 | "no such codec is supported."); |
| 285 | return NULL; |
| 286 | } |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL); |
| 289 | if (codecobj == NULL) |
| 290 | return NULL; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL); |
| 293 | Py_DECREF(codecobj); |
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 | return r; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static struct PyMethodDef __methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | {"getcodec", (PyCFunction)getcodec, METH_O, ""}, |
| 300 | {NULL, NULL}, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | static int |
| 304 | register_maps(PyObject *module) |
| 305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | const struct dbcs_map *h; |
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 | 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 Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | #ifdef USING_BINARY_PAIR_SEARCH |
| 321 | static DBCHAR |
| 322 | find_pairencmap(ucs2_t body, ucs2_t modifier, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | const struct pair_encodemap *haystack, int haystacksize) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | int pos, min, max; |
Victor Stinner | a0dd021 | 2013-04-11 22:09:04 +0200 | [diff] [blame] | 326 | Py_UCS4 value = body << 16 | modifier; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | min = 0; |
| 329 | max = haystacksize; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | 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 Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | if (value == haystack[pos].uniseq) |
| 344 | return haystack[pos].code; |
| 345 | else |
| 346 | return DBCINV; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 347 | } |
| 348 | #endif |
| 349 | |
| 350 | #ifdef USING_IMPORTED_MAPS |
| 351 | #define IMPORT_MAP(locale, charset, encmap, decmap) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | importmap("_codecs_" #locale, "__map_" #charset, \ |
| 353 | (const void**)encmap, (const void**)decmap) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 354 | |
| 355 | static int |
| 356 | importmap(const char *modname, const char *symbol, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | const void **encmap, const void **decmap) |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 358 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | PyObject *o, *mod; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 360 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 361 | mod = PyImport_ImportModule(modname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | if (mod == NULL) |
| 363 | return -1; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | 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 Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | Py_DECREF(mod); |
| 384 | return 0; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 385 | |
| 386 | errorexit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | Py_DECREF(mod); |
| 388 | return -1; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 389 | } |
| 390 | #endif |
| 391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | #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 Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 412 | |
| 413 | #endif |