mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
I really hope that I haven't missed anything in this one...
From: t-ishii@sra.co.jp Attached are patches to enhance the multi-byte support. (patches are against 7/18 snapshot) * determine encoding at initdb/createdb rather than compile time Now initdb/createdb has an option to specify the encoding. Also, I modified the syntax of CREATE DATABASE to accept encoding option. See README.mb for more details. For this purpose I have added new column "encoding" to pg_database. Also pg_attribute and pg_class are changed to catch up the modification to pg_database. Actually I haved added pg_database_mb.h, pg_attribute_mb.h and pg_class_mb.h. These are used only when MB is enabled. The reason having separate files is I couldn't find a way to use ifdef or whatever in those files. I have to admit it looks ugly. No way. * support for PGCLIENTENCODING when issuing COPY command commands/copy.c modified. * support for SQL92 syntax "SET NAMES" See gram.y. * support for LATIN2-5 * add UNICODE regression test case * new test suite for MB New directory test/mb added. * clean up source files Basic idea is to have MB's own subdirectory for easier maintenance. These are include/mb and backend/utils/mb.
This commit is contained in:
358
src/backend/utils/mb/wchar.c
Normal file
358
src/backend/utils/mb/wchar.c
Normal file
@@ -0,0 +1,358 @@
|
||||
/*
|
||||
* conversion functions between pg_wchar and multi-byte streams.
|
||||
* Tatsuo Ishii
|
||||
* $Id: wchar.c,v 1.1 1998/07/24 03:31:57 scrappy Exp $
|
||||
*/
|
||||
|
||||
#include "mb/pg_wchar.h"
|
||||
|
||||
/*
|
||||
* conversion to pg_wchar is done by "table driven."
|
||||
* to add an encoding support, define mb2wchar_with_len(), mblen()
|
||||
* for the particular encoding. Note that if the encoding is only
|
||||
* supported in the client, you don't need to define
|
||||
* mb2wchar_with_len() function (SJIS is the case).
|
||||
*/
|
||||
static void pg_euc2wchar_with_len
|
||||
(const unsigned char *from, pg_wchar *to, int len)
|
||||
{
|
||||
while (*from && len > 0) {
|
||||
if (*from == SS2) {
|
||||
from++;
|
||||
len--;
|
||||
*to = 0xff & *from++;
|
||||
len--;
|
||||
} else if (*from == SS3) {
|
||||
from++;
|
||||
*to = *from++ << 8;
|
||||
*to |= 0x3f & *from++;
|
||||
len -= 3;
|
||||
} else if (*from & 0x80) {
|
||||
*to = *from++ << 8;
|
||||
*to |= *from++;
|
||||
len -= 2;
|
||||
} else {
|
||||
*to = *from++;
|
||||
len--;
|
||||
}
|
||||
to++;
|
||||
}
|
||||
*to = 0;
|
||||
}
|
||||
|
||||
static int pg_euc_mblen(const unsigned char *s)
|
||||
{
|
||||
int len;
|
||||
|
||||
if (*s == SS2) {
|
||||
len = 2;
|
||||
} else if (*s == SS3) {
|
||||
len = 3;
|
||||
} else if (*s & 0x80) {
|
||||
len = 2;
|
||||
} else {
|
||||
len = 1;
|
||||
}
|
||||
return(len);
|
||||
}
|
||||
|
||||
/*
|
||||
* EUC_JP
|
||||
*/
|
||||
static void pg_eucjp2wchar_with_len
|
||||
(const unsigned char *from, pg_wchar *to, int len)
|
||||
{
|
||||
pg_euc2wchar_with_len(from,to,len);
|
||||
}
|
||||
|
||||
static int pg_eucjp_mblen(const unsigned char *s)
|
||||
{
|
||||
return(pg_euc_mblen(s));
|
||||
}
|
||||
|
||||
/*
|
||||
* EUC_KR
|
||||
*/
|
||||
static void pg_euckr2wchar_with_len
|
||||
(const unsigned char *from, pg_wchar *to, int len)
|
||||
{
|
||||
pg_euc2wchar_with_len(from,to,len);
|
||||
}
|
||||
|
||||
static int pg_euckr_mblen(const unsigned char *s)
|
||||
{
|
||||
return(pg_euc_mblen(s));
|
||||
}
|
||||
|
||||
/*
|
||||
* EUC_CN
|
||||
*/
|
||||
static void pg_euccn2wchar_with_len
|
||||
(const unsigned char *from, pg_wchar *to, int len)
|
||||
{
|
||||
while (*from && len > 0) {
|
||||
if (*from == SS2) {
|
||||
from++;
|
||||
len--;
|
||||
*to = 0x3f00 & (*from++ << 8);
|
||||
*to = *from++;
|
||||
len -= 2;
|
||||
} else if (*from == SS3) {
|
||||
from++;
|
||||
*to = *from++ << 8;
|
||||
*to |= 0x3f & *from++;
|
||||
len -= 3;
|
||||
} else if (*from & 0x80) {
|
||||
*to = *from++ << 8;
|
||||
*to |= *from++;
|
||||
len -= 2;
|
||||
} else {
|
||||
*to = *from++;
|
||||
len--;
|
||||
}
|
||||
to++;
|
||||
}
|
||||
*to = 0;
|
||||
}
|
||||
|
||||
static int pg_euccn_mblen(const unsigned char *s)
|
||||
{
|
||||
int len;
|
||||
|
||||
if (*s == SS2) {
|
||||
len = 3;
|
||||
} else if (*s == SS3) {
|
||||
len = 3;
|
||||
} else if (*s & 0x80) {
|
||||
len = 2;
|
||||
} else {
|
||||
len = 1;
|
||||
}
|
||||
return(len);
|
||||
}
|
||||
|
||||
/*
|
||||
* EUC_TW
|
||||
*/
|
||||
static void pg_euctw2wchar_with_len
|
||||
(const unsigned char *from, pg_wchar *to, int len)
|
||||
{
|
||||
while (*from && len > 0) {
|
||||
if (*from == SS2) {
|
||||
from++;
|
||||
len--;
|
||||
*to = *from++ << 16;
|
||||
*to |= *from++ << 8;
|
||||
*to |= *from++;
|
||||
len -= 3;
|
||||
} else if (*from == SS3) {
|
||||
from++;
|
||||
*to = *from++ << 8;
|
||||
*to |= 0x3f & *from++;
|
||||
len -= 3;
|
||||
} else if (*from & 0x80) {
|
||||
*to = *from++ << 8;
|
||||
*to |= *from++;
|
||||
len -= 2;
|
||||
} else {
|
||||
*to = *from++;
|
||||
len--;
|
||||
}
|
||||
to++;
|
||||
}
|
||||
*to = 0;
|
||||
}
|
||||
|
||||
static int pg_euctw_mblen(const unsigned char *s)
|
||||
{
|
||||
int len;
|
||||
|
||||
if (*s == SS2) {
|
||||
len = 4;
|
||||
} else if (*s == SS3) {
|
||||
len = 3;
|
||||
} else if (*s & 0x80) {
|
||||
len = 2;
|
||||
} else {
|
||||
len = 1;
|
||||
}
|
||||
return(len);
|
||||
}
|
||||
|
||||
/*
|
||||
* convert UTF-8 to pg_wchar (UCS-2)
|
||||
* caller should allocate enough space for "to"
|
||||
* len: length of from.
|
||||
* "from" not necessarily null terminated.
|
||||
*/
|
||||
static void pg_utf2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
|
||||
{
|
||||
unsigned char c1,c2,c3;
|
||||
while (*from && len > 0) {
|
||||
if ((*from & 0x80) == 0) {
|
||||
*to = *from++;
|
||||
len--;
|
||||
} else if ((*from & 0xe0) == 0xc0) {
|
||||
c1 = *from++ & 0x1f;
|
||||
c2 = *from++ & 0x3f;
|
||||
len -= 2;
|
||||
*to = c1 << 6;
|
||||
*to |= c2;
|
||||
} else if ((*from & 0xe0) == 0xe0) {
|
||||
c1 = *from++ & 0x0f;
|
||||
c2 = *from++ & 0x3f;
|
||||
c3 = *from++ & 0x3f;
|
||||
len -= 3;
|
||||
*to = c1 << 12;
|
||||
*to |= c2 << 6;
|
||||
*to |= c3;
|
||||
}
|
||||
to++;
|
||||
}
|
||||
*to = 0;
|
||||
}
|
||||
|
||||
static int pg_utf_mblen(const unsigned char *s)
|
||||
{
|
||||
int len = 1;
|
||||
|
||||
if ((*s & 0x80) == 0) {
|
||||
len = 1;
|
||||
} else if ((*s & 0xe0) == 0xc0) {
|
||||
len = 2;
|
||||
} else if ((*s & 0xe0) == 0xe0) {
|
||||
len = 3;
|
||||
}
|
||||
return(len);
|
||||
}
|
||||
|
||||
/*
|
||||
* convert mule internal code to pg_wchar
|
||||
* caller should allocate enough space for "to"
|
||||
* len: length of from.
|
||||
* "from" not necessarily null terminated.
|
||||
*/
|
||||
static void pg_mule2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
|
||||
{
|
||||
while (*from && len > 0) {
|
||||
if (IS_LC1(*from)) {
|
||||
*to = *from++ << 16;
|
||||
*to |= *from++;
|
||||
len -= 2;
|
||||
} else if (IS_LCPRV1(*from)) {
|
||||
from++;
|
||||
*to = *from++ << 16;
|
||||
*to |= *from++;
|
||||
len -= 3;
|
||||
} else if (IS_LC2(*from)) {
|
||||
*to = *from++ << 16;
|
||||
*to |= *from++ << 8;
|
||||
*to |= *from++;
|
||||
len -= 3;
|
||||
} else if (IS_LCPRV2(*from)) {
|
||||
from++;
|
||||
*to = *from++ << 16;
|
||||
*to |= *from++ << 8;
|
||||
*to |= *from++;
|
||||
len -= 4;
|
||||
} else { /* assume ASCII */
|
||||
*to = (unsigned char)*from++;
|
||||
len--;
|
||||
}
|
||||
to++;
|
||||
}
|
||||
*to = 0;
|
||||
}
|
||||
|
||||
int pg_mule_mblen(const unsigned char *s)
|
||||
{
|
||||
int len;
|
||||
|
||||
if (IS_LC1(*s)) {
|
||||
len = 2;
|
||||
} else if (IS_LCPRV1(*s)) {
|
||||
len = 3;
|
||||
} else if (IS_LC2(*s)) {
|
||||
len = 3;
|
||||
} else if (IS_LCPRV2(*s)) {
|
||||
len = 4;
|
||||
} else { /* assume ASCII */
|
||||
len = 1;
|
||||
}
|
||||
return(len);
|
||||
}
|
||||
|
||||
/*
|
||||
* ISO8859-1
|
||||
*/
|
||||
static void pg_latin12wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
|
||||
{
|
||||
while (*from && len-- > 0) {
|
||||
*to++ = *from++;
|
||||
}
|
||||
*to = 0;
|
||||
}
|
||||
|
||||
static int pg_latin1_mblen(const unsigned char *s)
|
||||
{
|
||||
return(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* SJIS
|
||||
*/
|
||||
static int pg_sjis_mblen(const unsigned char *s)
|
||||
{
|
||||
int len;
|
||||
|
||||
if (*s >= 0xa1 && *s <= 0xdf) { /* 1 byte kana? */
|
||||
len = 1;
|
||||
} else if (*s > 0x7f) { /* kanji? */
|
||||
len = 2;
|
||||
} else { /* should be ASCII */
|
||||
len = 1;
|
||||
}
|
||||
return(len);
|
||||
}
|
||||
|
||||
pg_wchar_tbl pg_wchar_table[] = {
|
||||
{pg_eucjp2wchar_with_len, pg_eucjp_mblen},
|
||||
{pg_euccn2wchar_with_len, pg_euccn_mblen},
|
||||
{pg_euckr2wchar_with_len, pg_euckr_mblen},
|
||||
{pg_euctw2wchar_with_len, pg_euctw_mblen},
|
||||
{pg_utf2wchar_with_len, pg_utf_mblen},
|
||||
{pg_mule2wchar_with_len, pg_mule_mblen},
|
||||
{pg_latin12wchar_with_len, pg_latin1_mblen},
|
||||
{pg_latin12wchar_with_len, pg_latin1_mblen},
|
||||
{pg_latin12wchar_with_len, pg_latin1_mblen},
|
||||
{pg_latin12wchar_with_len, pg_latin1_mblen},
|
||||
{pg_latin12wchar_with_len, pg_latin1_mblen},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{0, pg_sjis_mblen}
|
||||
};
|
||||
|
||||
/* returns the byte length of a word for mule internal code */
|
||||
int pg_mic_mblen(const unsigned char *mbstr)
|
||||
{
|
||||
return(pg_mule_mblen(mbstr));
|
||||
}
|
||||
Reference in New Issue
Block a user