blob: 9435e0c03f77efe792832a9d6f2857a92d39cd48 [file] [log] [blame]
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00001/*
2 * multibytecodec.h: Common Multibyte Codec Implementation
3 *
4 * Written by Hye-Shik Chang <perky@FreeBSD.org>
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +00005 * $CJKCodecs: multibytecodec.h,v 1.7 2004/06/27 10:39:28 perky Exp $
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00006 */
7
8#ifndef _PYTHON_MULTIBYTECODEC_H_
9#define _PYTHON_MULTIBYTECODEC_H_
10#ifdef __cplusplus
11extern "C" {
12#endif
13
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000014#ifdef uint32_t
15typedef uint32_t ucs4_t;
16#else
17typedef unsigned int ucs4_t;
18#endif
19
20#ifdef uint16_t
21typedef uint16_t ucs2_t, DBCHAR;
22#else
23typedef unsigned short ucs2_t, DBCHAR;
24#endif
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000025
26typedef union {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000027 void *p;
28 int i;
29 unsigned char c[8];
30 ucs2_t u2[4];
31 ucs4_t u4[2];
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000032} MultibyteCodec_State;
33
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000034typedef int (*mbcodec_init)(const void *config);
35typedef int (*mbencode_func)(MultibyteCodec_State *state, const void *config,
36 const Py_UNICODE **inbuf, size_t inleft,
37 unsigned char **outbuf, size_t outleft,
38 int flags);
39typedef int (*mbencodeinit_func)(MultibyteCodec_State *state,
40 const void *config);
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000041typedef int (*mbencodereset_func)(MultibyteCodec_State *state,
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000042 const void *config,
43 unsigned char **outbuf, size_t outleft);
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000044typedef int (*mbdecode_func)(MultibyteCodec_State *state,
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000045 const void *config,
46 const unsigned char **inbuf, size_t inleft,
47 Py_UNICODE **outbuf, size_t outleft);
48typedef int (*mbdecodeinit_func)(MultibyteCodec_State *state,
49 const void *config);
50typedef int (*mbdecodereset_func)(MultibyteCodec_State *state,
51 const void *config);
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000052
53typedef struct {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000054 const char *encoding;
55 const void *config;
56 mbcodec_init codecinit;
57 mbencode_func encode;
58 mbencodeinit_func encinit;
59 mbencodereset_func encreset;
60 mbdecode_func decode;
61 mbdecodeinit_func decinit;
62 mbdecodereset_func decreset;
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000063} MultibyteCodec;
64
65typedef struct {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000066 PyObject_HEAD
67 MultibyteCodec *codec;
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000068} MultibyteCodecObject;
69
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000070#define MAXDECPENDING 8
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000071typedef struct {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000072 PyObject_HEAD
73 MultibyteCodec *codec;
74 MultibyteCodec_State state;
75 unsigned char pending[MAXDECPENDING];
76 int pendingsize;
77 PyObject *stream, *errors;
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000078} MultibyteStreamReaderObject;
79
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000080#define MAXENCPENDING 2
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000081typedef struct {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000082 PyObject_HEAD
83 MultibyteCodec *codec;
84 MultibyteCodec_State state;
85 Py_UNICODE pending[MAXENCPENDING];
86 int pendingsize;
87 PyObject *stream, *errors;
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000088} MultibyteStreamWriterObject;
89
90/* positive values for illegal sequences */
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000091#define MBERR_TOOSMALL (-1) /* insufficient output buffer space */
92#define MBERR_TOOFEW (-2) /* incomplete input buffer */
93#define MBERR_INTERNAL (-3) /* internal runtime error */
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000094
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000095#define ERROR_STRICT (PyObject *)(1)
96#define ERROR_IGNORE (PyObject *)(2)
97#define ERROR_REPLACE (PyObject *)(3)
98#define ERROR_MAX ERROR_REPLACE
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +000099
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000100#define MBENC_FLUSH 0x0001 /* encode all characters encodable */
101#define MBENC_MAX MBENC_FLUSH
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000102
103#ifdef __cplusplus
104}
105#endif
106#endif