⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conv.c

📁 关系型数据库 Postgresql 6.5.2
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * conversion between client encoding and server internal encoding * (currently mule internal code (mic) is used) * Tatsuo Ishii * WIN1250 client encoding support contributed by Pavel Behal * * $Id: conv.c,v 1.12 1999/07/11 22:47:20 ishii Exp $ * * */#include <stdio.h>#include <string.h>#include "mb/pg_wchar.h"/* * convert bogus chars that cannot be represented in the currentencoding * system. */static voidprintBogusChar(unsigned char **mic, unsigned char **p){	char		strbuf[16];	int			l = pg_mic_mblen(*mic);	*(*p)++ = '(';	while (l--)	{		sprintf(strbuf, "%02x", *(*mic)++);		*(*p)++ = strbuf[0];		*(*p)++ = strbuf[1];	}	*(*p)++ = ')';}/* * SJIS ---> MIC */static voidsjis2mic(unsigned char *sjis, unsigned char *p, int len){	int			c1,				c2;	while (len > 0 && (c1 = *sjis++))	{		if (c1 >= 0xa1 && c1 <= 0xdf)		{						/* 1 byte kana? */			len--;			*p++ = LC_JISX0201K;			*p++ = c1;		}		else if (c1 > 0x7f)		{						/* kanji? */			c2 = *sjis++;			len -= 2;			*p++ = LC_JISX0208;			*p++ = ((c1 & 0x3f) << 1) + 0x9f + (c2 > 0x9e);			*p++ = c2 + ((c2 > 0x9e) ? 2 : 0x60) + (c2 < 0x80);		}		else		{						/* should be ASCII */			len--;			*p++ = c1;		}	}	*p = '\0';}/* * MIC ---> SJIS */static voidmic2sjis(unsigned char *mic, unsigned char *p, int len){	int			c1,				c2;	while (len > 0 && (c1 = *mic))	{		len -= pg_mic_mblen(mic++);		if (c1 == LC_JISX0201K)			*p++ = *mic++;		else if (c1 == LC_JISX0208)		{			c1 = *mic++;			c2 = *mic++;			*p++ = ((c1 - 0xa1) >> 1) + ((c1 < 0xdf) ? 0x81 : 0xc1);			*p++ = c2 - ((c1 & 1) ? ((c2 < 0xe0) ? 0x61 : 0x60) : 2);		}		else if (c1 > 0x7f)		{						/* cannot convert to SJIS! */			mic--;			printBogusChar(&mic, &p);		}		else		{						/* should be ASCII */			*p++ = c1;		}	}	*p = '\0';}/* * EUC_JP ---> MIC */static voideuc_jp2mic(unsigned char *euc, unsigned char *p, int len){	int			c1;	while (len > 0 && (c1 = *euc++))	{		if (c1 == SS2)		{						/* 1 byte kana? */			len -= 2;			*p++ = LC_JISX0201K;			*p++ = *euc++;		}		else if (c1 == SS3)		{						/* JIS X0212 kanji? */			len -= 3;			*p++ = LC_JISX0212;			*p++ = *euc++;			*p++ = *euc++;		}		else if (c1 & 0x80)		{						/* kanji? */			len -= 2;			*p++ = LC_JISX0208;			*p++ = c1;			*p++ = *euc++;		}		else		{						/* should be ASCII */			len--;			*p++ = c1;		}	}	*p = '\0';}/* * MIC ---> EUC_JP */static voidmic2euc_jp(unsigned char *mic, unsigned char *p, int len){	int			c1;	while (len > 0 && (c1 = *mic))	{		len -= pg_mic_mblen(mic++);		if (c1 == LC_JISX0201K)		{			*p++ = SS2;			*p++ = *mic++;		}		else if (c1 == LC_JISX0212)		{			*p++ = SS3;			*p++ = *mic++;			*p++ = *mic++;		}		else if (c1 == LC_JISX0208)		{			*p++ = *mic++;			*p++ = *mic++;		}		else if (c1 > 0x7f)		{						/* cannot convert to EUC_JP! */			mic--;			printBogusChar(&mic, &p);		}		else		{						/* should be ASCII */			*p++ = c1;		}	}	*p = '\0';}/* * EUC_KR ---> MIC */static voideuc_kr2mic(unsigned char *euc, unsigned char *p, int len){	int			c1;	while (len > 0 && (c1 = *euc++))	{		if (c1 & 0x80)		{			len -= 2;			*p++ = LC_KS5601;			*p++ = c1;			*p++ = *euc++;		}		else		{						/* should be ASCII */			len--;			*p++ = c1;		}	}	*p = '\0';}/* * MIC ---> EUC_KR */static voidmic2euc_kr(unsigned char *mic, unsigned char *p, int len){	int			c1;	while (len > 0 && (c1 = *mic))	{		len -= pg_mic_mblen(mic++);		if (c1 == LC_KS5601)		{			*p++ = *mic++;			*p++ = *mic++;		}		else if (c1 > 0x7f)		{						/* cannot convert to EUC_KR! */			mic--;			printBogusChar(&mic, &p);		}		else		{						/* should be ASCII */			*p++ = c1;		}	}	*p = '\0';}/* * EUC_CN ---> MIC */static voideuc_cn2mic(unsigned char *euc, unsigned char *p, int len){	int			c1;	while (len > 0 && (c1 = *euc++))	{		if (c1 & 0x80)		{			len -= 2;			*p++ = LC_GB2312_80;			*p++ = c1;			*p++ = *euc++;		}		else		{						/* should be ASCII */			len--;			*p++ = c1;		}	}	*p = '\0';}/* * MIC ---> EUC_CN */static voidmic2euc_cn(unsigned char *mic, unsigned char *p, int len){	int			c1;	while (len > 0 && (c1 = *mic))	{		len -= pg_mic_mblen(mic++);		if (c1 == LC_GB2312_80)		{			*p++ = *mic++;			*p++ = *mic++;		}		else if (c1 > 0x7f)		{						/* cannot convert to EUC_CN! */			mic--;			printBogusChar(&mic, &p);		}		else		{						/* should be ASCII */			*p++ = c1;		}	}	*p = '\0';}/* * EUC_TW ---> MIC */static voideuc_tw2mic(unsigned char *euc, unsigned char *p, int len){	int			c1;	while (len > 0 && (c1 = *euc++))	{		if (c1 == SS2)		{			len -= 4;			c1 = *euc++;		/* plane No. */			if (c1 == 0xa1)				*p++ = LC_CNS11643_1;			else if (c1 == 0xa2)				*p++ = LC_CNS11643_2;			else			{				*p++ = 0x9d;	/* LCPRV2 */				*p++ = 0xa3 - c1 + LC_CNS11643_3;			}			*p++ = *euc++;			*p++ = *euc++;		}		else if (c1 & 0x80)		{						/* CNS11643-1 */			len -= 2;			*p++ = LC_CNS11643_1;			*p++ = c1;			*p++ = *euc++;		}		else		{						/* should be ASCII */			len--;			*p++ = c1;		}	}	*p = '\0';}/* * MIC ---> EUC_TW */static voidmic2euc_tw(unsigned char *mic, unsigned char *p, int len){	int			c1;	while (len > 0 && (c1 = *mic))	{		len -= pg_mic_mblen(mic++);		if (c1 == LC_CNS11643_1 || c1 == LC_CNS11643_2)		{			*p++ = *mic++;			*p++ = *mic++;		}		else if (c1 == 0x9d)		{						/* LCPRV2? */			*p++ = SS2;			*p++ = c1 - LC_CNS11643_3 + 0xa3;			*p++ = *mic++;			*p++ = *mic++;		}		else if (c1 > 0x7f)		{						/* cannot convert to EUC_TW! */			mic--;			printBogusChar(&mic, &p);		}		else		{						/* should be ASCII */			*p++ = c1;		}	}	*p = '\0';}/* * Big5 ---> MIC */static voidbig52mic(unsigned char *big5, unsigned char *p, int len){	unsigned short c1;	unsigned short big5buf,				cnsBuf;	unsigned char lc;	char		bogusBuf[2];	int			i;	while (len > 0 && (c1 = *big5++))	{		if (c1 <= 0x7fU)		{						/* ASCII */			len--;			*p++ = c1;		}		else		{			len -= 2;			big5buf = c1 << 8;			c1 = *big5++;			big5buf |= c1;			cnsBuf = BIG5toCNS(big5buf, &lc);			if (lc != 0)			{				if (lc == LC_CNS11643_3 || lc == LC_CNS11643_4)				{					*p++ = 0x9d;/* LCPRV2 */				}				*p++ = lc;		/* Plane No. */				*p++ = (cnsBuf >> 8) & 0x00ff;				*p++ = cnsBuf & 0x00ff;			}			else			{					/* cannot convert */				big5 -= 2;				*p++ = '(';				for (i = 0; i < 2; i++)				{					sprintf(bogusBuf, "%02x", *big5++);					*p++ = bogusBuf[0];					*p++ = bogusBuf[1];				}				*p++ = ')';			}		}	}	*p = '\0';}/* * MIC ---> Big5 */static voidmic2big5(unsigned char *mic, unsigned char *p, int len){	int			l;	unsigned short c1;	unsigned short big5buf,				cnsBuf;	while (len > 0 && (c1 = *mic))	{		l = pg_mic_mblen(mic++);		len -= l;		/* 0x9d means LCPRV2 */		if (c1 == LC_CNS11643_1 || c1 == LC_CNS11643_2 || c1 == 0x9d)		{			if (c1 == 0x9d)			{				c1 = *mic++;	/* get plane no. */			}			cnsBuf = (*mic++) << 8;			cnsBuf |= (*mic++) & 0x00ff;			big5buf = CNStoBIG5(cnsBuf, c1);			if (big5buf == 0)			{					/* cannot convert to Big5! */				mic -= l;				printBogusChar(&mic, &p);			}			else			{				*p++ = (big5buf >> 8) & 0x00ff;				*p++ = big5buf & 0x00ff;			}		}		else if (c1 <= 0x7f)	/* ASCII */			*p++ = c1;		else		{						/* cannot convert to Big5! */			mic--;			printBogusChar(&mic, &p);		}	}	*p = '\0';}/* * LATINn ---> MIC */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -