multibytecodec.h 4.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * multibytecodec.h: Common Multibyte Codec Implementation
 *
 * Written by Hye-Shik Chang <perky@FreeBSD.org>
 */

#ifndef _PYTHON_MULTIBYTECODEC_H_
#define _PYTHON_MULTIBYTECODEC_H_
#ifdef __cplusplus
extern "C" {
#endif

13 14 15 16 17
#ifdef uint16_t
typedef uint16_t ucs2_t, DBCHAR;
#else
typedef unsigned short ucs2_t, DBCHAR;
#endif
18 19

typedef union {
20 21 22 23
    void *p;
    int i;
    unsigned char c[8];
    ucs2_t u2[4];
24
    Py_UCS4 u4[2];
25 26
} MultibyteCodec_State;

27
typedef int (*mbcodec_init)(const void *config);
28
typedef Py_ssize_t (*mbencode_func)(MultibyteCodec_State *state,
29
                        const void *config,
30 31
                        int kind, void *data,
                        Py_ssize_t *inpos, Py_ssize_t inlen,
32 33
                        unsigned char **outbuf, Py_ssize_t outleft,
                        int flags);
34
typedef int (*mbencodeinit_func)(MultibyteCodec_State *state,
35
                                 const void *config);
36
typedef Py_ssize_t (*mbencodereset_func)(MultibyteCodec_State *state,
37 38
                        const void *config,
                        unsigned char **outbuf, Py_ssize_t outleft);
39
typedef Py_ssize_t (*mbdecode_func)(MultibyteCodec_State *state,
40 41
                        const void *config,
                        const unsigned char **inbuf, Py_ssize_t inleft,
42
                        _PyUnicodeWriter *writer);
43
typedef int (*mbdecodeinit_func)(MultibyteCodec_State *state,
44
                                 const void *config);
45
typedef Py_ssize_t (*mbdecodereset_func)(MultibyteCodec_State *state,
46
                                         const void *config);
47 48

typedef struct {
49 50 51 52 53 54 55 56 57
    const char *encoding;
    const void *config;
    mbcodec_init codecinit;
    mbencode_func encode;
    mbencodeinit_func encinit;
    mbencodereset_func encreset;
    mbdecode_func decode;
    mbdecodeinit_func decinit;
    mbdecodereset_func decreset;
58 59 60
} MultibyteCodec;

typedef struct {
61 62
    PyObject_HEAD
    MultibyteCodec *codec;
63 64
} MultibyteCodecObject;

65 66
#define MultibyteCodec_Check(op) ((op)->ob_type == &MultibyteCodec_Type)

67 68 69 70 71
#define _MultibyteStatefulCodec_HEAD            \
    PyObject_HEAD                               \
    MultibyteCodec *codec;                      \
    MultibyteCodec_State state;                 \
    PyObject *errors;
72
typedef struct {
73
    _MultibyteStatefulCodec_HEAD
74
} MultibyteStatefulCodecContext;
75

76 77 78
#define MAXENCPENDING   2
#define _MultibyteStatefulEncoder_HEAD          \
    _MultibyteStatefulCodec_HEAD                \
79
    PyObject *pending;
80
typedef struct {
81
    _MultibyteStatefulEncoder_HEAD
82 83
} MultibyteStatefulEncoderContext;

84 85 86 87 88
#define MAXDECPENDING   8
#define _MultibyteStatefulDecoder_HEAD          \
    _MultibyteStatefulCodec_HEAD                \
    unsigned char pending[MAXDECPENDING];       \
    Py_ssize_t pendingsize;
89
typedef struct {
90
    _MultibyteStatefulDecoder_HEAD
91 92 93
} MultibyteStatefulDecoderContext;

typedef struct {
94
    _MultibyteStatefulEncoder_HEAD
95 96 97
} MultibyteIncrementalEncoderObject;

typedef struct {
98
    _MultibyteStatefulDecoder_HEAD
99 100 101
} MultibyteIncrementalDecoderObject;

typedef struct {
102 103
    _MultibyteStatefulDecoder_HEAD
    PyObject *stream;
104 105 106
} MultibyteStreamReaderObject;

typedef struct {
107 108
    _MultibyteStatefulEncoder_HEAD
    PyObject *stream;
109 110 111
} MultibyteStreamWriterObject;

/* positive values for illegal sequences */
112 113 114
#define MBERR_TOOSMALL          (-1) /* insufficient output buffer space */
#define MBERR_TOOFEW            (-2) /* incomplete input buffer */
#define MBERR_INTERNAL          (-3) /* internal runtime error */
115
#define MBERR_EXCEPTION         (-4) /* an exception has been raised */
116 117 118 119 120

#define ERROR_STRICT            (PyObject *)(1)
#define ERROR_IGNORE            (PyObject *)(2)
#define ERROR_REPLACE           (PyObject *)(3)
#define ERROR_ISCUSTOM(p)       ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
121 122 123 124 125
#define ERROR_DECREF(p)                             \
    do {                                            \
        if (p != NULL && ERROR_ISCUSTOM(p))         \
            Py_DECREF(p);                           \
    } while (0);
126

127 128
#define MBENC_FLUSH             0x0001 /* encode all characters encodable */
#define MBENC_MAX               MBENC_FLUSH
129

130 131 132
#define PyMultibyteCodec_CAPSULE_NAME "multibytecodec.__map_*"


133 134 135 136
#ifdef __cplusplus
}
#endif
#endif