[KoCo-CVS] [Commit] cjkcodecs/src _gb2312.c
Brought to you by:
perky
From: Hye-Shik C. <pe...@us...> - 2003-05-20 07:31:40
|
perky 03/05/20 00:31:37 Added: src _gb2312.c Log: Add gb2312 codec implementation. Revision Changes Path 1.1 cjkcodecs/src/_gb2312.c Index: _gb2312.c =================================================================== /* * _gb2312.c: the GB2312 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: _gb2312.c,v 1.1 2003/05/20 07:31:37 perky Exp $ */ #include "codeccommon.h" ENCMAP(gbcommon) DECMAP(gb2312) ENCODER(gb2312) { while (inleft > 0) { const encode_map *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 = &gbcommonencmap[c >> 8]; clow = c & 0xff; if (map->map == NULL || clow < map->bottom || clow > map->top || (code = map->map[clow - map->bottom]) == UNIINV) return 1; if (code & 0x8000) /* MSB set: GBK */ return 1; (*outbuf)[0] = (code >> 8) | 0x80; (*outbuf)[1] = (code & 0xFF) | 0x80; (*outbuf) += 2; outleft -= 2; (*inbuf)++; inleft--; } return 0; } DECODER(gb2312) { while (inleft > 0) { const decode_map *map; unsigned char c = **inbuf, c2; Py_UNICODE code; if (outleft < 1) return MBERR_TOOSMALL; if (c < 0x80) { **outbuf = c; (*inbuf)++; inleft--; (*outbuf)++; outleft--; continue; } if (inleft < 2) return MBERR_TOOFEW; c2 = (*inbuf)[1] ^ 0x80; map = &gb2312decmap[c & 0x7f]; if (map->map == NULL || c2 < map->bottom || c2 > map->top || (code = map->map[c2 - map->bottom]) == UNIINV) return 2; **outbuf = code; (*outbuf)++; outleft--; (*inbuf) += 2; inleft -= 2; } return 0; } CODECDEF(gb2312) NOMETHODS(__methods) void init_gb2312(void) { PyObject *codec; PyObject *m = NULL, *mod = NULL, *o = NULL; m = Py_InitModule("_gb2312", __methods); /* Import mapdata */ MAPOPEN(mod, "zh_CN") if (IMPORTMAP(mod, gb2312, NULL, &gb2312decmap) || IMPORTMAP(mod, gbcommon, &gbcommonencmap, NULL)) goto errorexit; MAPCLOSE(mod) /* Create Codec Instances */ MULTIBYTECODEC_OPEN(mod, o) REGISTERCODEC(m, o, codec) MULTIBYTECODEC_CLOSE(mod, o) if (PyErr_Occurred()) Py_FatalError("can't initialize the _gb2312 module"); return; errorexit: Py_XDECREF(m); Py_XDECREF(mod); Py_XDECREF(o); } /* * ex: ts=8 sts=4 et */ |