#include #include #include PyObject *xlate(PyObject *s, PyObject *o) { unsigned char *inbuf; int i, length, pos=0; PyObject *map, *key, *value, *ret; Py_UNICODE *u, *ru; if(!PyArg_ParseTuple(o, "s#O", (char*)&inbuf, &length, &map)) return NULL; if(!PyDict_Check(map)) { PyErr_SetString(PyExc_TypeError, "Argument 2 must be a dictionary"); return NULL; } u = PyMem_Malloc(sizeof(Py_UNICODE) * 256); if(!u) { return NULL; } for(i=0; i<256; i++) { u[i] = 0xfffd; } while(PyDict_Next(map, &pos, &key, &value)) { int ki, vi; if(!PyInt_Check(key)) { PyErr_SetString(PyExc_TypeError, "Dictionary keys must be ints"); return NULL; } ki = PyInt_AsLong(key); if(ki < 0 || ki > 255) { PyErr_Format(PyExc_TypeError, "Dictionary keys must be in the range 0..255 (saw %d)", ki); return NULL; } if(value == Py_None) continue; if(!PyInt_Check(value)) { PyErr_SetString(PyExc_TypeError, "Dictionary values must be ints or None"); return NULL; } vi = PyInt_AsLong(value); u[ki] = vi; } ret = PyUnicode_FromUnicode(NULL, length); if(!ret) { free(u); return NULL; } ru = PyUnicode_AsUnicode(ret); for(i=0; i