[KoCo-CVS] [Commit] cjkcodecs/src _euc_kr.c codeccommon.h multibytecodec.c multibytecodec.h
Brought to you by:
perky
From: Hye-Shik C. <pe...@us...> - 2003-05-19 06:06:39
|
perky 03/05/18 23:06:38 Modified: src multibytecodec.c multibytecodec.h Added: src _euc_kr.c codeccommon.h Log: Add on-going first implementation. Revision Changes Path 1.3 +2 -2 cjkcodecs/src/multibytecodec.c Index: multibytecodec.c =================================================================== RCS file: /cvsroot/koco/cjkcodecs/src/multibytecodec.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- multibytecodec.c 19 May 2003 02:53:49 -0000 1.2 +++ multibytecodec.c 19 May 2003 06:06:38 -0000 1.3 @@ -26,7 +26,7 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: multibytecodec.c,v 1.2 2003/05/19 02:53:49 perky Exp $ + * $Id: multibytecodec.c,v 1.3 2003/05/19 06:06:38 perky Exp $ */ #include "Python.h" @@ -268,7 +268,7 @@ /* we don't reuse inleft and outleft here. * error callbacks can relocate the cursor anywhere on buffer */ - inleft = (size_t)buf->inbuf_end - (size_t)buf->inbuf; + inleft = (size_t)(buf->inbuf_end - buf->inbuf); outleft = (size_t)(buf->outbuf_end - buf->outbuf); r = codec->encode(state, &buf->inbuf, inleft, &buf->outbuf, outleft); 1.4 +5 -5 cjkcodecs/src/multibytecodec.h Index: multibytecodec.h =================================================================== RCS file: /cvsroot/koco/cjkcodecs/src/multibytecodec.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- multibytecodec.h 19 May 2003 02:53:49 -0000 1.3 +++ multibytecodec.h 19 May 2003 06:06:38 -0000 1.4 @@ -26,7 +26,7 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - * $Id: multibytecodec.h,v 1.3 2003/05/19 02:53:49 perky Exp $ + * $Id: multibytecodec.h,v 1.4 2003/05/19 06:06:38 perky Exp $ */ #ifndef _PYTHON_MULTIBYTECODEC_H_ @@ -41,11 +41,11 @@ } PyMultibyteCodec_State; typedef int (*mbencode_func)(PyMultibyteCodec_State *state, - const Py_UNICODE **inbuf, int inleft, - unsigned char **outbuf, int outleft); + const Py_UNICODE **inbuf, size_t inleft, + unsigned char **outbuf, size_t outleft); typedef int (*mbdecode_func)(PyMultibyteCodec_State *state, - const unsigned char **inbuf, int inleft, - Py_UNICODE **outbuf, int outleft); + const unsigned char **inbuf, size_t inleft, + Py_UNICODE **outbuf, size_t outleft); typedef struct { const char *encoding; 1.1 cjkcodecs/src/_euc_kr.c Index: _euc_kr.c =================================================================== /* * _euc_kr.c: the EUC-KR codec * * Copyright (C) 2003 Hye-Shik Chang <pe...@Fr...>. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: _euc_kr.c,v 1.1 2003/05/19 06:06:38 perky Exp $ */ #include "Python.h" #include "multibytecodec.h" #include "cjkcommon.h" #include "codeccommon.h" const static struct unim_index *cp949encmap; const static struct dbcs_index *ksx1001decmap, *cp949decmap; static int euc_kr_encode(PyMultibyteCodec_State *state, const Py_UNICODE **inbuf, size_t inleft, unsigned char **outbuf, size_t outleft) { while (inleft > 0) { const struct unim_index *map; Py_UNICODE c = **inbuf, clow; DBCHAR code; if (c < 0x80) { if (outleft < 1) return MBERR_TOOSMALL; **outbuf = c; (*inbuf)++; inleft--; (*outbuf)++; outleft--; continue; } if (outleft < 2) return MBERR_TOOSMALL; map = &cp949encmap[c >> 8]; clow = c & 0xff; if (map->map == NULL || clow < map->bottom || clow > map->top || (code = map->map[clow - map->bottom]) == UNIINV) return 2; if (code & 0x8000) /* MSB set: CP949 */ return 2; (*outbuf)[0] = (code >> 8) | 0x80; (*outbuf)[1] = (code & 0xFF) | 0x80; (*outbuf) += 2; outleft -= 2; (*inbuf)++; inleft--; } return 0; } static int euc_kr_decode(PyMultibyteCodec_State *state, const unsigned char **inbuf, size_t inleft, Py_UNICODE **outbuf, size_t outleft) { return 0; } static PyMultibyteCodec __codec = { "euc_kr", euc_kr_encode, euc_kr_decode, }; static struct PyMethodDef __methods[] = { {NULL, NULL}, }; void init_euc_kr(void) { PyObject *codec; PyObject *m = NULL, *mod = NULL, *o = NULL; m = Py_InitModule("_euc_kr", __methods); /* Import mapdata */ MAPOPEN(mod, "ko_KR") if (importmap(mod, "__map_ksx1001", NULL, &ksx1001decmap) || importmap(mod, "__map_cp949ext", NULL, &cp949decmap) || importmap(mod, "__map_cp949", &cp949encmap, NULL)) goto errorexit; MAPCLOSE(mod) /* Create Codec Instances */ MULTIBYTECODEC_OPEN(mod, o) codec = createcodec(o, &__codec); if (codec == NULL) goto errorexit; PyModule_AddObject(m, "codec", codec); MULTIBYTECODEC_CLOSE(mod, o) if (PyErr_Occurred()) Py_FatalError("can't initialize the _euc_kr module"); return; errorexit: Py_XDECREF(m); Py_XDECREF(mod); Py_XDECREF(o); } /* * ex: ts=8 sts=4 et */ 1.1 cjkcodecs/src/codeccommon.h Index: codeccommon.h =================================================================== /* * codeccommon.h: Common Codec Routines * * Copyright (C) 2003 Hye-Shik Chang <pe...@Fr...>. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: codeccommon.h,v 1.1 2003/05/19 06:06:38 perky Exp $ */ #define MAPOPEN(mod, locale) \ mod = PyImport_ImportModule("mapdata_" locale); \ if (mod == NULL) goto errorexit; #define MAPCLOSE(mod) \ Py_DECREF(mod); #define MULTIBYTECODEC_OPEN(mod, o) \ mod = PyImport_ImportModule("multibytecodec"); \ if (mod == NULL) goto errorexit; \ o = PyObject_GetAttrString(mod, "__create_codec"); \ if (o == NULL || !PyCallable_Check(o)) \ goto errorexit; #define MULTIBYTECODEC_CLOSE(mod, o) \ Py_DECREF(o); Py_DECREF(mod); static int importmap(PyObject *mod, const char *symbol, const struct unim_index **encmap, const struct dbcs_index **decmap) { PyObject *o; o = PyObject_GetAttrString(mod, (char*)symbol); if (o == NULL) return -1; else if (!PyCObject_Check(o)) { PyErr_SetString(PyExc_ValueError, "map data must be a CObject."); return -1; } else { struct dbcs_map *map; map = PyCObject_AsVoidPtr(o); if (encmap != NULL) *encmap = map->encmap; if (decmap != NULL) *decmap = map->decmap; Py_DECREF(o); } return 0; } static PyObject * createcodec(PyObject *cofunc, PyMultibyteCodec *codec) { PyObject *args, *r; args = PyTuple_New(1); if (args == NULL) return NULL; PyTuple_SET_ITEM(args, 0, PyCObject_FromVoidPtr(codec, NULL)); r = PyObject_CallObject(cofunc, args); Py_DECREF(args); return r; } /* * ex: ts=8 sts=4 et */ |