[KoCo-CVS] [Commit] KoreanCodecs/src _koco.c koco_stream.h
Brought to you by:
perky
From: Chang <pe...@us...> - 2002-04-28 21:33:17
|
perky 02/04/27 23:54:12 Modified: src _koco.c koco_stream.h Log: - Fix unlimited access on boundary problem on readline_finalize - Let python.c.euc_kr uses _koco.StreamReader as stream reader Revision Changes Path 1.18 +7 -7 KoreanCodecs/src/_koco.c Index: _koco.c =================================================================== RCS file: /cvsroot/koco/KoreanCodecs/src/_koco.c,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- _koco.c 28 Apr 2002 06:16:07 -0000 1.17 +++ _koco.c 28 Apr 2002 06:54:11 -0000 1.18 @@ -4,14 +4,14 @@ * KoreanCodecs C Implementations * * Author : Hye-Shik Chang <pe...@fa...> - * Date : $Date: 2002/04/28 06:16:07 $ + * Date : $Date: 2002/04/28 06:54:11 $ * Created : 15 March 2002 * - * $Revision: 1.17 $ + * $Revision: 1.18 $ */ static char *version = -"$Id: _koco.c,v 1.17 2002/04/28 06:16:07 perky Exp $"; +"$Id: _koco.c,v 1.18 2002/04/28 06:54:11 perky Exp $"; #define UNIFIL 0xfffd @@ -23,10 +23,10 @@ PyObject* (*decoder)(state_t*, char*, int slen, int errtype, PyObject* (*finalizer)(const Py_UNICODE *, int)); } streaminfo; #define STATE_EXIST 0x100 -#define HAS_STATE(c) ((*(c))&STATE_EXIST) -#define GET_STATE(c) (unsigned char)((*(c))&0xFF) -#define RESET_STATE(c) ((*(c))&=0xFE00) -#define SET_STATE(c, v) (*(c)=STATE_EXIST|(v)) +#define HAS_STATE(c) ((c)&STATE_EXIST) +#define GET_STATE(c) (unsigned char)((c)&0xFF) +#define RESET_STATE(c) ((c)&=0xFE00) +#define SET_STATE(c, v) ((c)=STATE_EXIST|(v)) #ifndef max #define max(a, b) ((a)<(b) ? (b) : (a)) 1.2 +54 -20 KoreanCodecs/src/koco_stream.h Index: koco_stream.h =================================================================== RCS file: /cvsroot/koco/KoreanCodecs/src/koco_stream.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- koco_stream.h 28 Apr 2002 06:16:07 -0000 1.1 +++ koco_stream.h 28 Apr 2002 06:54:12 -0000 1.2 @@ -4,10 +4,10 @@ * KoreanCodecs EUC-KR StreamReader C Implementation * * Author : Hye-Shik Chang <pe...@fa...> - * Date : $Date: 2002/04/28 06:16:07 $ + * Date : $Date: 2002/04/28 06:54:12 $ * Created : 28 April 2002 * - * $Revision: 1.1 $ + * $Revision: 1.2 $ */ static PyObject * @@ -23,8 +23,8 @@ srccur = s; srcend = s + slen; - if (HAS_STATE(state)) { - unsigned char c = GET_STATE(state); + if (HAS_STATE(*state)) { + unsigned char c = GET_STATE(*state); if (c & 0x80) { if (slen > 0) { @@ -60,13 +60,13 @@ } else *(destcur++) = c; - RESET_STATE(state); + RESET_STATE(*state); } for (; srccur < srcend; srccur++) { if (*srccur & 0x80) { if (srccur+1 >= srcend) /* state out */ - SET_STATE(state, *srccur); + SET_STATE(*state, *srccur); else { codemap = ksc5601_decode_map[*srccur & 0x7F]; if (!codemap) @@ -111,7 +111,7 @@ if ((list = PyList_New(0)) == NULL) return NULL; - for (;datalen--; data++) { + for (;(datalen--) > 0; data++) { if (*data == '\n') { append: if ((uobj = PyUnicode_FromUnicode(linestart, data-linestart+1)) == NULL) { Py_DECREF(list); @@ -125,8 +125,10 @@ linestart = data+1; } } - if (linestart < data) + if (linestart < data) { + data--; goto append; /* datalen < 0 here */ + } return list; } @@ -159,7 +161,7 @@ return NULL; stnfo = PyMem_New(streaminfo, 1); - RESET_STATE(&(stnfo->state)); + RESET_STATE(stnfo->state); if (!strcmp(encoding, "euc-kr")) stnfo->decoder = __euc_kr_decode; @@ -194,13 +196,23 @@ static PyObject* StreamReader_read(PyObject *typeself, PyObject *args) { - PyObject *self, *tmp, *r = NULL; + PyObject *self, *tmp = NULL, *r = NULL; PyObject *stream, *stnfoobj; streaminfo *stnfo; - int size = -1, errtype; + long size = -1; + int errtype; + + if (!PyArg_ParseTuple(args, "O|O:read", &self, &tmp)) + return NULL; - if (!PyArg_ParseTuple(args, "O|i:read", &self, &size)) + if (tmp == Py_None || tmp == NULL) + size = -1; + else if (PyInt_Check(tmp)) + size = PyInt_AsLong(tmp); + else { + PyErr_SetString(PyExc_TypeError, "an integer is required"); return NULL; + } if (size == 0) return PyUnicode_FromUnicode(NULL, 0); @@ -227,7 +239,8 @@ if (size < 0) tmp = PyObject_CallMethod(stream, "read", NULL); /* without tuple */ else - tmp = PyObject_CallMethod(stream, "read", "i", size); + tmp = PyObject_CallMethod(stream, "read", "i", + HAS_STATE(stnfo->state) ? size : max(2, size) ); if (tmp == NULL) goto out; @@ -249,14 +262,24 @@ static PyObject* StreamReader_readline(PyObject *typeself, PyObject *args) { - PyObject *self, *tmp, *r = NULL; + PyObject *self, *tmp = NULL, *r = NULL; PyObject *stream, *stnfoobj; streaminfo *stnfo; - int size = -1, errtype; + long size = -1; + int errtype; - if (!PyArg_ParseTuple(args, "O|i:readline", &self, &size)) + if (!PyArg_ParseTuple(args, "O|O:readline", &self, &tmp)) return NULL; + if (tmp == Py_None || tmp == NULL) + size = -1; + else if (PyInt_Check(tmp)) + size = PyInt_AsLong(tmp); + else { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return NULL; + } + if (size == 0) return PyUnicode_FromUnicode(NULL, 0); @@ -282,7 +305,8 @@ if (size < 0) tmp = PyObject_CallMethod(stream, "readline", NULL); /* without tuple */ else - tmp = PyObject_CallMethod(stream, "readline", "i", size); + tmp = PyObject_CallMethod(stream, "readline", "i", + HAS_STATE(stnfo->state) ? size : max(2, size) ); if (tmp == NULL) goto out; @@ -304,14 +328,23 @@ static PyObject* StreamReader_readlines(PyObject *typeself, PyObject *args) { - PyObject *self, *tmp, *r = NULL; + PyObject *self, *r = NULL, *tmp = NULL; PyObject *stream, *stnfoobj; streaminfo *stnfo; int size = -1, errtype; - if (!PyArg_ParseTuple(args, "O|i:readlines", &self, &size)) + if (!PyArg_ParseTuple(args, "O|O:readlines", &self, &tmp)) return NULL; + if (tmp == Py_None || tmp == NULL) + size = -1; + else if (PyInt_Check(tmp)) + size = PyInt_AsLong(tmp); + else { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return NULL; + } + if (size == 0) return PyUnicode_FromUnicode(NULL, 0); @@ -337,7 +370,8 @@ if (size < 0) tmp = PyObject_CallMethod(stream, "read", NULL); /* without tuple */ else - tmp = PyObject_CallMethod(stream, "read", "i", size); + tmp = PyObject_CallMethod(stream, "read", "i", + HAS_STATE(stnfo->state) ? size : max(2, size) ); if (tmp == NULL) goto out; @@ -369,7 +403,7 @@ return NULL; if ((stnfo = (streaminfo*)PyCObject_AsVoidPtr(stnfoobj)) != NULL) - RESET_STATE(&(stnfo->state)); + RESET_STATE(stnfo->state); Py_DECREF(stnfoobj); Py_INCREF(Py_None); |