Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * multibytecodec.h: Common Multibyte Codec Implementation |
| 3 | * |
| 4 | * Written by Hye-Shik Chang <perky@FreeBSD.org> |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 5 | * $CJKCodecs: multibytecodec.h,v 1.7 2004/06/27 10:39:28 perky Exp $ |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef _PYTHON_MULTIBYTECODEC_H_ |
| 9 | #define _PYTHON_MULTIBYTECODEC_H_ |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 14 | #ifdef uint32_t |
| 15 | typedef uint32_t ucs4_t; |
| 16 | #else |
| 17 | typedef unsigned int ucs4_t; |
| 18 | #endif |
| 19 | |
| 20 | #ifdef uint16_t |
| 21 | typedef uint16_t ucs2_t, DBCHAR; |
| 22 | #else |
| 23 | typedef unsigned short ucs2_t, DBCHAR; |
| 24 | #endif |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 25 | |
| 26 | typedef union { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 27 | void *p; |
| 28 | int i; |
| 29 | unsigned char c[8]; |
| 30 | ucs2_t u2[4]; |
| 31 | ucs4_t u4[2]; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 32 | } MultibyteCodec_State; |
| 33 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 34 | typedef int (*mbcodec_init)(const void *config); |
| 35 | typedef 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); |
| 39 | typedef int (*mbencodeinit_func)(MultibyteCodec_State *state, |
| 40 | const void *config); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 41 | typedef int (*mbencodereset_func)(MultibyteCodec_State *state, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 42 | const void *config, |
| 43 | unsigned char **outbuf, size_t outleft); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 44 | typedef int (*mbdecode_func)(MultibyteCodec_State *state, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 45 | const void *config, |
| 46 | const unsigned char **inbuf, size_t inleft, |
| 47 | Py_UNICODE **outbuf, size_t outleft); |
| 48 | typedef int (*mbdecodeinit_func)(MultibyteCodec_State *state, |
| 49 | const void *config); |
| 50 | typedef int (*mbdecodereset_func)(MultibyteCodec_State *state, |
| 51 | const void *config); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 52 | |
| 53 | typedef struct { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 54 | 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 Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 63 | } MultibyteCodec; |
| 64 | |
| 65 | typedef struct { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 66 | PyObject_HEAD |
| 67 | MultibyteCodec *codec; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 68 | } MultibyteCodecObject; |
| 69 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 70 | #define MAXDECPENDING 8 |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 71 | typedef struct { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 72 | PyObject_HEAD |
| 73 | MultibyteCodec *codec; |
| 74 | MultibyteCodec_State state; |
| 75 | unsigned char pending[MAXDECPENDING]; |
| 76 | int pendingsize; |
| 77 | PyObject *stream, *errors; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 78 | } MultibyteStreamReaderObject; |
| 79 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 80 | #define MAXENCPENDING 2 |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 81 | typedef struct { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 82 | PyObject_HEAD |
| 83 | MultibyteCodec *codec; |
| 84 | MultibyteCodec_State state; |
| 85 | Py_UNICODE pending[MAXENCPENDING]; |
| 86 | int pendingsize; |
| 87 | PyObject *stream, *errors; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 88 | } MultibyteStreamWriterObject; |
| 89 | |
| 90 | /* positive values for illegal sequences */ |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 91 | #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 Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 94 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 95 | #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 Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 99 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 100 | #define MBENC_FLUSH 0x0001 /* encode all characters encodable */ |
| 101 | #define MBENC_MAX MBENC_FLUSH |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 102 | |
| 103 | #ifdef __cplusplus |
| 104 | } |
| 105 | #endif |
| 106 | #endif |