| 1 |
/* |
| 2 |
* multibytecodec.h: Common Multibyte Codec Implementation |
| 3 |
* |
| 4 |
* Written by Hye-Shik Chang <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef _PYTHON_MULTIBYTECODEC_H_ |
| 8 |
#define _PYTHON_MULTIBYTECODEC_H_ |
| 9 |
#ifdef __cplusplus |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
#include "pycore_unicodeobject.h" // _PyUnicodeWriter |
| 14 |
|
| 15 |
#ifdef uint16_t |
| 16 |
typedef uint16_t ucs2_t, DBCHAR; |
| 17 |
#else |
| 18 |
typedef unsigned short ucs2_t, DBCHAR; |
| 19 |
#endif |
| 20 |
|
| 21 |
/* |
| 22 |
* A struct that provides 8 bytes of state for multibyte |
| 23 |
* codecs. Codecs are free to use this how they want. Note: if you |
| 24 |
* need to add a new field to this struct, ensure that its byte order |
| 25 |
* is independent of CPU endianness so that the return value of |
| 26 |
* getstate doesn't differ between little and big endian CPUs. |
| 27 |
*/ |
| 28 |
typedef struct { |
| 29 |
unsigned char c[8]; |
| 30 |
} MultibyteCodec_State; |
| 31 |
|
| 32 |
struct _cjk_mod_state; |
| 33 |
struct _multibyte_codec; |
| 34 |
|
| 35 |
typedef int (*mbcodec_init)(const struct _multibyte_codec *codec); |
| 36 |
typedef Py_ssize_t (*mbencode_func)(MultibyteCodec_State *state, |
| 37 |
const struct _multibyte_codec *codec, |
| 38 |
int kind, const void *data, |
| 39 |
Py_ssize_t *inpos, Py_ssize_t inlen, |
| 40 |
unsigned char **outbuf, Py_ssize_t outleft, |
| 41 |
int flags); |
| 42 |
typedef int (*mbencodeinit_func)(MultibyteCodec_State *state, |
| 43 |
const struct _multibyte_codec *codec); |
| 44 |
typedef Py_ssize_t (*mbencodereset_func)(MultibyteCodec_State *state, |
| 45 |
const struct _multibyte_codec *codec, |
| 46 |
unsigned char **outbuf, Py_ssize_t outleft); |
| 47 |
typedef Py_ssize_t (*mbdecode_func)(MultibyteCodec_State *state, |
| 48 |
const struct _multibyte_codec *codec, |
| 49 |
const unsigned char **inbuf, Py_ssize_t inleft, |
| 50 |
_PyUnicodeWriter *writer); |
| 51 |
typedef int (*mbdecodeinit_func)(MultibyteCodec_State *state, |
| 52 |
const struct _multibyte_codec *codec); |
| 53 |
typedef Py_ssize_t (*mbdecodereset_func)(MultibyteCodec_State *state, |
| 54 |
const struct _multibyte_codec *codec); |
| 55 |
|
| 56 |
typedef struct _multibyte_codec { |
| 57 |
const char *encoding; |
| 58 |
const void *config; |
| 59 |
mbcodec_init codecinit; |
| 60 |
mbencode_func encode; |
| 61 |
mbencodeinit_func encinit; |
| 62 |
mbencodereset_func encreset; |
| 63 |
mbdecode_func decode; |
| 64 |
mbdecodeinit_func decinit; |
| 65 |
mbdecodereset_func decreset; |
| 66 |
struct _cjk_mod_state *modstate; |
| 67 |
} MultibyteCodec; |
| 68 |
|
| 69 |
typedef struct { |
| 70 |
PyObject_HEAD |
| 71 |
const MultibyteCodec *codec; |
| 72 |
PyObject *cjk_module; |
| 73 |
} MultibyteCodecObject; |
| 74 |
|
| 75 |
#define MultibyteCodec_Check(state, op) Py_IS_TYPE((op), state->multibytecodec_type) |
| 76 |
|
| 77 |
#define _MultibyteStatefulCodec_HEAD \ |
| 78 |
PyObject_HEAD \ |
| 79 |
const MultibyteCodec *codec; \ |
| 80 |
MultibyteCodec_State state; \ |
| 81 |
PyObject *errors; |
| 82 |
typedef struct { |
| 83 |
_MultibyteStatefulCodec_HEAD |
| 84 |
} MultibyteStatefulCodecContext; |
| 85 |
|
| 86 |
#define MAXENCPENDING 2 |
| 87 |
#define _MultibyteStatefulEncoder_HEAD \ |
| 88 |
_MultibyteStatefulCodec_HEAD \ |
| 89 |
PyObject *pending; |
| 90 |
typedef struct { |
| 91 |
_MultibyteStatefulEncoder_HEAD |
| 92 |
} MultibyteStatefulEncoderContext; |
| 93 |
|
| 94 |
#define MAXDECPENDING 8 |
| 95 |
#define _MultibyteStatefulDecoder_HEAD \ |
| 96 |
_MultibyteStatefulCodec_HEAD \ |
| 97 |
unsigned char pending[MAXDECPENDING]; \ |
| 98 |
Py_ssize_t pendingsize; |
| 99 |
typedef struct { |
| 100 |
_MultibyteStatefulDecoder_HEAD |
| 101 |
} MultibyteStatefulDecoderContext; |
| 102 |
|
| 103 |
typedef struct { |
| 104 |
_MultibyteStatefulEncoder_HEAD |
| 105 |
} MultibyteIncrementalEncoderObject; |
| 106 |
|
| 107 |
typedef struct { |
| 108 |
_MultibyteStatefulDecoder_HEAD |
| 109 |
} MultibyteIncrementalDecoderObject; |
| 110 |
|
| 111 |
typedef struct { |
| 112 |
_MultibyteStatefulDecoder_HEAD |
| 113 |
PyObject *stream; |
| 114 |
} MultibyteStreamReaderObject; |
| 115 |
|
| 116 |
typedef struct { |
| 117 |
_MultibyteStatefulEncoder_HEAD |
| 118 |
PyObject *stream; |
| 119 |
} MultibyteStreamWriterObject; |
| 120 |
|
| 121 |
/* positive values for illegal sequences */ |
| 122 |
#define MBERR_TOOSMALL (-1) /* insufficient output buffer space */ |
| 123 |
#define MBERR_TOOFEW (-2) /* incomplete input buffer */ |
| 124 |
#define MBERR_INTERNAL (-3) /* internal runtime error */ |
| 125 |
#define MBERR_EXCEPTION (-4) /* an exception has been raised */ |
| 126 |
|
| 127 |
#define ERROR_STRICT (PyObject *)(1) |
| 128 |
#define ERROR_IGNORE (PyObject *)(2) |
| 129 |
#define ERROR_REPLACE (PyObject *)(3) |
| 130 |
#define ERROR_ISCUSTOM(p) ((p) < ERROR_STRICT || ERROR_REPLACE < (p)) |
| 131 |
#define ERROR_DECREF(p) \ |
| 132 |
do { \ |
| 133 |
if (p != NULL && ERROR_ISCUSTOM(p)) \ |
| 134 |
Py_DECREF(p); \ |
| 135 |
} while (0); |
| 136 |
|
| 137 |
#define MBENC_FLUSH 0x0001 /* encode all characters encodable */ |
| 138 |
#define MBENC_MAX MBENC_FLUSH |
| 139 |
|
| 140 |
typedef struct { |
| 141 |
const MultibyteCodec *codec; |
| 142 |
PyObject *cjk_module; |
| 143 |
} codec_capsule; |
| 144 |
|
| 145 |
#define MAP_CAPSULE "multibytecodec.map" |
| 146 |
#define CODEC_CAPSULE "multibytecodec.codec" |
| 147 |
|
| 148 |
|
| 149 |
#ifdef __cplusplus |
| 150 |
} |
| 151 |
#endif |
| 152 |
#endif |