• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * multibytecodec.h: Common Multibyte Codec Implementation
3  *
4  * Written by Hye-Shik Chang <perky@FreeBSD.org>
5  */
6 
7 #ifndef _PYTHON_MULTIBYTECODEC_H_
8 #define _PYTHON_MULTIBYTECODEC_H_
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #ifdef uint16_t
14 typedef uint16_t ucs2_t, DBCHAR;
15 #else
16 typedef unsigned short ucs2_t, DBCHAR;
17 #endif
18 
19 typedef union {
20     void *p;
21     int i;
22     unsigned char c[8];
23     ucs2_t u2[4];
24     Py_UCS4 u4[2];
25 } MultibyteCodec_State;
26 
27 typedef int (*mbcodec_init)(const void *config);
28 typedef Py_ssize_t (*mbencode_func)(MultibyteCodec_State *state,
29                         const void *config,
30                         int kind, void *data,
31                         Py_ssize_t *inpos, Py_ssize_t inlen,
32                         unsigned char **outbuf, Py_ssize_t outleft,
33                         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                         const void *config,
38                         unsigned char **outbuf, Py_ssize_t outleft);
39 typedef Py_ssize_t (*mbdecode_func)(MultibyteCodec_State *state,
40                         const void *config,
41                         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     const char *encoding;
50     const void *config;
51     mbcodec_init codecinit;
52     mbencode_func encode;
53     mbencodeinit_func encinit;
54     mbencodereset_func encreset;
55     mbdecode_func decode;
56     mbdecodeinit_func decinit;
57     mbdecodereset_func decreset;
58 } MultibyteCodec;
59 
60 typedef struct {
61     PyObject_HEAD
62     MultibyteCodec *codec;
63 } MultibyteCodecObject;
64 
65 #define MultibyteCodec_Check(op) ((op)->ob_type == &MultibyteCodec_Type)
66 
67 #define _MultibyteStatefulCodec_HEAD            \
68     PyObject_HEAD                               \
69     MultibyteCodec *codec;                      \
70     MultibyteCodec_State state;                 \
71     PyObject *errors;
72 typedef struct {
73     _MultibyteStatefulCodec_HEAD
74 } MultibyteStatefulCodecContext;
75 
76 #define MAXENCPENDING   2
77 #define _MultibyteStatefulEncoder_HEAD          \
78     _MultibyteStatefulCodec_HEAD                \
79     PyObject *pending;
80 typedef struct {
81     _MultibyteStatefulEncoder_HEAD
82 } MultibyteStatefulEncoderContext;
83 
84 #define MAXDECPENDING   8
85 #define _MultibyteStatefulDecoder_HEAD          \
86     _MultibyteStatefulCodec_HEAD                \
87     unsigned char pending[MAXDECPENDING];       \
88     Py_ssize_t pendingsize;
89 typedef struct {
90     _MultibyteStatefulDecoder_HEAD
91 } MultibyteStatefulDecoderContext;
92 
93 typedef struct {
94     _MultibyteStatefulEncoder_HEAD
95 } MultibyteIncrementalEncoderObject;
96 
97 typedef struct {
98     _MultibyteStatefulDecoder_HEAD
99 } MultibyteIncrementalDecoderObject;
100 
101 typedef struct {
102     _MultibyteStatefulDecoder_HEAD
103     PyObject *stream;
104 } MultibyteStreamReaderObject;
105 
106 typedef struct {
107     _MultibyteStatefulEncoder_HEAD
108     PyObject *stream;
109 } MultibyteStreamWriterObject;
110 
111 /* positive values for illegal sequences */
112 #define MBERR_TOOSMALL          (-1) /* insufficient output buffer space */
113 #define MBERR_TOOFEW            (-2) /* incomplete input buffer */
114 #define MBERR_INTERNAL          (-3) /* internal runtime error */
115 #define MBERR_EXCEPTION         (-4) /* an exception has been raised */
116 
117 #define ERROR_STRICT            (PyObject *)(1)
118 #define ERROR_IGNORE            (PyObject *)(2)
119 #define ERROR_REPLACE           (PyObject *)(3)
120 #define ERROR_ISCUSTOM(p)       ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
121 #define ERROR_DECREF(p)                             \
122     do {                                            \
123         if (p != NULL && ERROR_ISCUSTOM(p))         \
124             Py_DECREF(p);                           \
125     } while (0);
126 
127 #define MBENC_FLUSH             0x0001 /* encode all characters encodable */
128 #define MBENC_MAX               MBENC_FLUSH
129 
130 #define PyMultibyteCodec_CAPSULE_NAME "multibytecodec.__map_*"
131 
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 #endif
137