| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * cjkcodecs.h: common header for cjkcodecs | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Written by Hye-Shik Chang <perky@FreeBSD.org> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef _CJKCODECS_H_
 | 
					
						
							|  |  |  | #define _CJKCODECS_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-04 16:08:19 +00:00
										 |  |  | #define PY_SSIZE_T_CLEAN
 | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | #include "multibytecodec.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-18 11:28:37 +02:00
										 |  |  | /* a unicode "undefined" code point */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define UNIINV  0xFFFE
 | 
					
						
							| 
									
										
										
										
											2005-10-06 15:51:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-18 11:28:37 +02:00
										 |  |  | /* internal-use DBCS code points which aren't used by any charsets */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define NOCHAR  0xFFFF
 | 
					
						
							|  |  |  | #define MULTIC  0xFFFE
 | 
					
						
							|  |  |  | #define DBCINV  0xFFFD
 | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* shorter macros to save source size of mapping tables */ | 
					
						
							|  |  |  | #define U UNIINV
 | 
					
						
							|  |  |  | #define N NOCHAR
 | 
					
						
							|  |  |  | #define M MULTIC
 | 
					
						
							|  |  |  | #define D DBCINV
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct dbcs_index { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     const ucs2_t *map; | 
					
						
							|  |  |  |     unsigned char bottom, top; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | typedef struct dbcs_index decode_map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct widedbcs_index { | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  |     const Py_UCS4 *map; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     unsigned char bottom, top; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | typedef struct widedbcs_index widedecode_map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct unim_index { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     const DBCHAR *map; | 
					
						
							|  |  |  |     unsigned char bottom, top; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | typedef struct unim_index encode_map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct unim_index_bytebased { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     const unsigned char *map; | 
					
						
							|  |  |  |     unsigned char bottom, top; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct dbcs_map { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     const char *charset; | 
					
						
							|  |  |  |     const struct unim_index *encmap; | 
					
						
							|  |  |  |     const struct dbcs_index *decmap; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct pair_encodemap { | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  |     Py_UCS4 uniseq; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     DBCHAR code; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 15:02:45 +00:00
										 |  |  | static const MultibyteCodec *codec_list; | 
					
						
							|  |  |  | static const struct dbcs_map *mapping_list; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define CODEC_INIT(encoding)                                            \
 | 
					
						
							|  |  |  |     static int encoding##_codec_init(const void *config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ENCODER_INIT(encoding)                                          \
 | 
					
						
							|  |  |  |     static int encoding##_encode_init(                                  \ | 
					
						
							|  |  |  |         MultibyteCodec_State *state, const void *config) | 
					
						
							|  |  |  | #define ENCODER(encoding)                                               \
 | 
					
						
							|  |  |  |     static Py_ssize_t encoding##_encode(                                \ | 
					
						
							|  |  |  |         MultibyteCodec_State *state, const void *config,                \ | 
					
						
							| 
									
										
										
										
											2020-04-11 10:48:40 +03:00
										 |  |  |         int kind, const void *data,                                     \ | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  |         Py_ssize_t *inpos, Py_ssize_t inlen,                            \ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         unsigned char **outbuf, Py_ssize_t outleft, int flags) | 
					
						
							|  |  |  | #define ENCODER_RESET(encoding)                                         \
 | 
					
						
							|  |  |  |     static Py_ssize_t encoding##_encode_reset(                          \ | 
					
						
							|  |  |  |         MultibyteCodec_State *state, const void *config,                \ | 
					
						
							|  |  |  |         unsigned char **outbuf, Py_ssize_t outleft) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define DECODER_INIT(encoding)                                          \
 | 
					
						
							|  |  |  |     static int encoding##_decode_init(                                  \ | 
					
						
							|  |  |  |         MultibyteCodec_State *state, const void *config) | 
					
						
							|  |  |  | #define DECODER(encoding)                                               \
 | 
					
						
							|  |  |  |     static Py_ssize_t encoding##_decode(                                \ | 
					
						
							|  |  |  |         MultibyteCodec_State *state, const void *config,                \ | 
					
						
							|  |  |  |         const unsigned char **inbuf, Py_ssize_t inleft,                 \ | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  |         _PyUnicodeWriter *writer) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define DECODER_RESET(encoding)                                         \
 | 
					
						
							|  |  |  |     static Py_ssize_t encoding##_decode_reset(                          \ | 
					
						
							|  |  |  |         MultibyteCodec_State *state, const void *config) | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define NEXT_IN(i)                              \
 | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  |     do {                                        \ | 
					
						
							|  |  |  |         (*inbuf) += (i);                        \ | 
					
						
							|  |  |  |         (inleft) -= (i);                        \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  | #define NEXT_INCHAR(i)                          \
 | 
					
						
							|  |  |  |     do {                                        \ | 
					
						
							|  |  |  |         (*inpos) += (i);                        \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define NEXT_OUT(o)                             \
 | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  |     do {                                        \ | 
					
						
							|  |  |  |         (*outbuf) += (o);                       \ | 
					
						
							|  |  |  |         (outleft) -= (o);                       \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define NEXT(i, o)                              \
 | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  |     do {                                        \ | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |         NEXT_INCHAR(i);                         \ | 
					
						
							|  |  |  |         NEXT_OUT(o);                            \ | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define REQUIRE_INBUF(n)                        \
 | 
					
						
							| 
									
										
										
										
											2013-10-29 00:59:44 +01:00
										 |  |  |     do {                                        \ | 
					
						
							|  |  |  |         if (inleft < (n))                       \ | 
					
						
							|  |  |  |             return MBERR_TOOFEW;                \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define REQUIRE_OUTBUF(n)                       \
 | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     do {                                        \ | 
					
						
							|  |  |  |         if (outleft < (n))                      \ | 
					
						
							|  |  |  |             return MBERR_TOOSMALL;              \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  | #define INBYTE1 ((*inbuf)[0])
 | 
					
						
							|  |  |  | #define INBYTE2 ((*inbuf)[1])
 | 
					
						
							|  |  |  | #define INBYTE3 ((*inbuf)[2])
 | 
					
						
							|  |  |  | #define INBYTE4 ((*inbuf)[3])
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  | #define INCHAR1 (PyUnicode_READ(kind, data, *inpos))
 | 
					
						
							|  |  |  | #define INCHAR2 (PyUnicode_READ(kind, data, *inpos + 1))
 | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  | #define OUTCHAR(c)                                                         \
 | 
					
						
							|  |  |  |     do {                                                                   \ | 
					
						
							|  |  |  |         if (_PyUnicodeWriter_WriteChar(writer, (c)) < 0)                   \ | 
					
						
							| 
									
										
										
										
											2013-07-16 21:41:43 +02:00
										 |  |  |             return MBERR_EXCEPTION;                                         \ | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define OUTCHAR2(c1, c2)                                                   \
 | 
					
						
							|  |  |  |     do {                                                                   \ | 
					
						
							|  |  |  |         Py_UCS4 _c1 = (c1);                                                \ | 
					
						
							|  |  |  |         Py_UCS4 _c2 = (c2);                                                \ | 
					
						
							|  |  |  |         if (_PyUnicodeWriter_Prepare(writer, 2, Py_MAX(_c1, c2)) < 0)      \ | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |             return MBERR_EXCEPTION;                                        \ | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  |         PyUnicode_WRITE(writer->kind, writer->data, writer->pos, _c1);     \ | 
					
						
							|  |  |  |         PyUnicode_WRITE(writer->kind, writer->data, writer->pos + 1, _c2); \ | 
					
						
							|  |  |  |         writer->pos += 2;                                                  \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 10:48:47 +03:00
										 |  |  | #define OUTBYTEI(c, i)                     \
 | 
					
						
							|  |  |  |     do {                                   \ | 
					
						
							|  |  |  |         assert((unsigned char)(c) == (c)); \ | 
					
						
							|  |  |  |         ((*outbuf)[i]) = (c);              \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define OUTBYTE1(c) OUTBYTEI(c, 0)
 | 
					
						
							|  |  |  | #define OUTBYTE2(c) OUTBYTEI(c, 1)
 | 
					
						
							|  |  |  | #define OUTBYTE3(c) OUTBYTEI(c, 2)
 | 
					
						
							|  |  |  | #define OUTBYTE4(c) OUTBYTEI(c, 3)
 | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  | #define WRITEBYTE1(c1)              \
 | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     do {                            \ | 
					
						
							|  |  |  |         REQUIRE_OUTBUF(1);          \ | 
					
						
							| 
									
										
										
										
											2019-03-29 10:48:47 +03:00
										 |  |  |         OUTBYTE1(c1);               \ | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  | #define WRITEBYTE2(c1, c2)          \
 | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     do {                            \ | 
					
						
							|  |  |  |         REQUIRE_OUTBUF(2);          \ | 
					
						
							| 
									
										
										
										
											2019-03-29 10:48:47 +03:00
										 |  |  |         OUTBYTE1(c1);               \ | 
					
						
							|  |  |  |         OUTBYTE2(c2);               \ | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  | #define WRITEBYTE3(c1, c2, c3)      \
 | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     do {                            \ | 
					
						
							|  |  |  |         REQUIRE_OUTBUF(3);          \ | 
					
						
							| 
									
										
										
										
											2019-03-29 10:48:47 +03:00
										 |  |  |         OUTBYTE1(c1);               \ | 
					
						
							|  |  |  |         OUTBYTE2(c2);               \ | 
					
						
							|  |  |  |         OUTBYTE3(c3);               \ | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2013-04-14 02:06:32 +02:00
										 |  |  | #define WRITEBYTE4(c1, c2, c3, c4)  \
 | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     do {                            \ | 
					
						
							|  |  |  |         REQUIRE_OUTBUF(4);          \ | 
					
						
							| 
									
										
										
										
											2019-03-29 10:48:47 +03:00
										 |  |  |         OUTBYTE1(c1);               \ | 
					
						
							|  |  |  |         OUTBYTE2(c2);               \ | 
					
						
							|  |  |  |         OUTBYTE3(c3);               \ | 
					
						
							|  |  |  |         OUTBYTE4(c4);               \ | 
					
						
							| 
									
										
										
										
											2013-10-29 00:19:27 +01:00
										 |  |  |     } while (0) | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define _TRYMAP_ENC(m, assi, val)                               \
 | 
					
						
							|  |  |  |     ((m)->map != NULL && (val) >= (m)->bottom &&                \ | 
					
						
							|  |  |  |         (val)<= (m)->top && ((assi) = (m)->map[(val) -          \ | 
					
						
							|  |  |  |         (m)->bottom]) != NOCHAR) | 
					
						
							| 
									
										
										
										
											2013-10-28 23:54:13 +01:00
										 |  |  | #define TRYMAP_ENC(charset, assi, uni)                     \
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-28 23:18:39 +01:00
										 |  |  | #define _TRYMAP_DEC(m, assi, val)                             \
 | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  |     ((m)->map != NULL &&                                        \ | 
					
						
							|  |  |  |      (val) >= (m)->bottom &&                                    \ | 
					
						
							|  |  |  |      (val)<= (m)->top &&                                        \ | 
					
						
							|  |  |  |      ((assi) = (m)->map[(val) - (m)->bottom]) != UNIINV) | 
					
						
							| 
									
										
										
										
											2013-10-28 23:18:39 +01:00
										 |  |  | #define TRYMAP_DEC(charset, assi, c1, c2)                     \
 | 
					
						
							|  |  |  |     _TRYMAP_DEC(&charset##_decmap[c1], assi, c2) | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 15:02:45 +00:00
										 |  |  | #define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = {
 | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | #define MAPPING_ENCONLY(enc) {#enc, (void*)enc##_encmap, NULL},
 | 
					
						
							|  |  |  | #define MAPPING_DECONLY(enc) {#enc, NULL, (void*)enc##_decmap},
 | 
					
						
							|  |  |  | #define MAPPING_ENCDEC(enc) {#enc, (void*)enc##_encmap, (void*)enc##_decmap},
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define END_MAPPINGS_LIST                               \
 | 
					
						
							|  |  |  |     {"", NULL, NULL} };                                 \ | 
					
						
							|  |  |  |     static const struct dbcs_map *mapping_list =        \ | 
					
						
							|  |  |  |         (const struct dbcs_map *)_mapping_list; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 15:02:45 +00:00
										 |  |  | #define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = {
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define _STATEFUL_METHODS(enc)          \
 | 
					
						
							|  |  |  |     enc##_encode,                       \ | 
					
						
							|  |  |  |     enc##_encode_init,                  \ | 
					
						
							|  |  |  |     enc##_encode_reset,                 \ | 
					
						
							|  |  |  |     enc##_decode,                       \ | 
					
						
							|  |  |  |     enc##_decode_init,                  \ | 
					
						
							|  |  |  |     enc##_decode_reset, | 
					
						
							|  |  |  | #define _STATELESS_METHODS(enc)         \
 | 
					
						
							|  |  |  |     enc##_encode, NULL, NULL,           \ | 
					
						
							|  |  |  |     enc##_decode, NULL, NULL, | 
					
						
							|  |  |  | #define CODEC_STATEFUL(enc) {           \
 | 
					
						
							|  |  |  |     #enc, NULL, NULL,                   \
 | 
					
						
							|  |  |  |     _STATEFUL_METHODS(enc)              \ | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define CODEC_STATELESS(enc) {          \
 | 
					
						
							|  |  |  |     #enc, NULL, NULL,                   \
 | 
					
						
							|  |  |  |     _STATELESS_METHODS(enc)             \ | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define CODEC_STATELESS_WINIT(enc) {    \
 | 
					
						
							|  |  |  |     #enc, NULL,                         \
 | 
					
						
							|  |  |  |     enc##_codec_init,                   \ | 
					
						
							|  |  |  |     _STATELESS_METHODS(enc)             \ | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | }, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define END_CODECS_LIST                                 \
 | 
					
						
							|  |  |  |     {"", NULL,} };                                      \ | 
					
						
							|  |  |  |     static const MultibyteCodec *codec_list =           \ | 
					
						
							|  |  |  |         (const MultibyteCodec *)_codec_list; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:31:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | getmultibytecodec(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  |     PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec"); | 
					
						
							|  |  |  |     if (mod == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PyObject *cofunc = PyObject_GetAttrString(mod, "__create_codec"); | 
					
						
							|  |  |  |     Py_DECREF(mod); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return cofunc; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | getcodec(PyObject *self, PyObject *encoding) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *codecobj, *r, *cofunc; | 
					
						
							|  |  |  |     const MultibyteCodec *codec; | 
					
						
							|  |  |  |     const char *enc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyUnicode_Check(encoding)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                         "encoding name must be a string."); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-20 09:13:07 +02:00
										 |  |  |     enc = PyUnicode_AsUTF8(encoding); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (enc == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cofunc = getmultibytecodec(); | 
					
						
							|  |  |  |     if (cofunc == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (codec = codec_list; codec->encoding[0]; codec++) | 
					
						
							|  |  |  |         if (strcmp(codec->encoding, enc) == 0) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (codec->encoding[0] == '\0') { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_LookupError, | 
					
						
							|  |  |  |                         "no such codec is supported."); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL); | 
					
						
							|  |  |  |     if (codecobj == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 17:46:57 +01:00
										 |  |  |     r = PyObject_CallOneArg(cofunc, codecobj); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(codecobj); | 
					
						
							| 
									
										
										
										
											2021-01-08 15:01:38 +01:00
										 |  |  |     Py_DECREF(cofunc); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return r; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | register_maps(PyObject *module) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     const struct dbcs_map *h; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (h = mapping_list; h->charset[0] != '\0'; h++) { | 
					
						
							|  |  |  |         char mhname[256] = "__map_"; | 
					
						
							|  |  |  |         strcpy(mhname + sizeof("__map_") - 1, h->charset); | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         PyObject *capsule = PyCapsule_New((void *)h, | 
					
						
							|  |  |  |                                           PyMultibyteCodec_CAPSULE_NAME, NULL); | 
					
						
							|  |  |  |         if (capsule == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (PyModule_AddObject(module, mhname, capsule) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(capsule); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef USING_BINARY_PAIR_SEARCH
 | 
					
						
							|  |  |  | static DBCHAR | 
					
						
							|  |  |  | find_pairencmap(ucs2_t body, ucs2_t modifier, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                 const struct pair_encodemap *haystack, int haystacksize) | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int pos, min, max; | 
					
						
							| 
									
										
										
										
											2013-04-11 22:09:04 +02:00
										 |  |  |     Py_UCS4 value = body << 16 | modifier; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     min = 0; | 
					
						
							|  |  |  |     max = haystacksize; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 22:47:50 -07:00
										 |  |  |     for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (value < haystack[pos].uniseq) { | 
					
						
							| 
									
										
										
										
											2016-05-23 22:47:50 -07:00
										 |  |  |             if (max != pos) { | 
					
						
							|  |  |  |                 max = pos; | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (value > haystack[pos].uniseq) { | 
					
						
							| 
									
										
										
										
											2016-05-23 22:47:50 -07:00
										 |  |  |             if (min != pos) { | 
					
						
							|  |  |  |                 min = pos; | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-23 22:47:50 -07:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 22:47:50 -07:00
										 |  |  |     if (value == haystack[pos].uniseq) { | 
					
						
							|  |  |  |         return haystack[pos].code; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return DBCINV; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef USING_IMPORTED_MAPS
 | 
					
						
							|  |  |  | #define IMPORT_MAP(locale, charset, encmap, decmap) \
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     importmap("_codecs_" #locale, "__map_" #charset, \ | 
					
						
							|  |  |  |               (const void**)encmap, (const void**)decmap) | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | importmap(const char *modname, const char *symbol, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |           const void **encmap, const void **decmap) | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *o, *mod; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-19 21:03:34 +03:00
										 |  |  |     mod = PyImport_ImportModule(modname); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (mod == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 11:27:24 +03:00
										 |  |  |     o = PyObject_GetAttrString(mod, symbol); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (o == NULL) | 
					
						
							|  |  |  |         goto errorexit; | 
					
						
							|  |  |  |     else if (!PyCapsule_IsValid(o, PyMultibyteCodec_CAPSULE_NAME)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |                         "map data must be a Capsule."); | 
					
						
							|  |  |  |         goto errorexit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         struct dbcs_map *map; | 
					
						
							|  |  |  |         map = PyCapsule_GetPointer(o, PyMultibyteCodec_CAPSULE_NAME); | 
					
						
							|  |  |  |         if (encmap != NULL) | 
					
						
							|  |  |  |             *encmap = map->encmap; | 
					
						
							|  |  |  |         if (decmap != NULL) | 
					
						
							|  |  |  |             *decmap = map->decmap; | 
					
						
							|  |  |  |         Py_DECREF(o); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_DECREF(mod); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | errorexit: | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(mod); | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | _cjk_exec(PyObject *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return register_maps(module); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyMethodDef _cjk_methods[] = { | 
					
						
							|  |  |  |     {"getcodec", (PyCFunction)getcodec, METH_O, ""}, | 
					
						
							|  |  |  |     {NULL, NULL}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyModuleDef_Slot _cjk_slots[] = { | 
					
						
							|  |  |  |     {Py_mod_exec, _cjk_exec}, | 
					
						
							|  |  |  |     {0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define I_AM_A_MODULE_FOR(loc)                                          \
 | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  |     static struct PyModuleDef _cjk_module = {                           \ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         PyModuleDef_HEAD_INIT,                                          \ | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  |         .m_name = "_codecs_"#loc,                                       \ | 
					
						
							|  |  |  |         .m_size = 0,                                                    \ | 
					
						
							|  |  |  |         .m_methods = _cjk_methods,                                      \ | 
					
						
							|  |  |  |         .m_slots = _cjk_slots,                                          \ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     };                                                                  \ | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  |                                                                         \ | 
					
						
							| 
									
										
										
										
											2015-03-17 17:48:27 +01:00
										 |  |  |     PyMODINIT_FUNC                                                      \ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyInit__codecs_##loc(void)                                          \ | 
					
						
							|  |  |  |     {                                                                   \ | 
					
						
							| 
									
										
										
										
											2021-01-08 00:15:22 +01:00
										 |  |  |         return PyModuleDef_Init(&_cjk_module);                          \ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2004-07-18 03:06:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif
 |