1998-03-15 08:39:04 +01:00
|
|
|
/*
|
1998-07-24 05:32:46 +02:00
|
|
|
* conversion functions between pg_wchar and multi-byte streams.
|
1998-03-15 08:39:04 +01:00
|
|
|
* Tatsuo Ishii
|
2001-10-15 03:19:15 +02:00
|
|
|
* $Id: wchar.c,v 1.24 2001/10/15 01:19:15 ishii Exp $
|
1999-07-12 00:47:21 +02:00
|
|
|
*
|
|
|
|
* WIN1250 client encoding updated by Pavel Behal
|
|
|
|
*
|
1998-03-15 08:39:04 +01:00
|
|
|
*/
|
2001-02-10 03:31:31 +01:00
|
|
|
/* can be used in either frontend or backend */
|
Commit Karel's patch.
-------------------------------------------------------------------
Subject: Re: [PATCHES] encoding names
From: Karel Zak <zakkr@zf.jcu.cz>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-patches <pgsql-patches@postgresql.org>
Date: Fri, 31 Aug 2001 17:24:38 +0200
On Thu, Aug 30, 2001 at 01:30:40AM +0200, Peter Eisentraut wrote:
> > - convert encoding 'name' to 'id'
>
> I thought we decided not to add functions returning "new" names until we
> know exactly what the new names should be, and pending schema
Ok, the patch not to add functions.
> better
>
> ...(): encoding name too long
Fixed.
I found new bug in command/variable.c in parse_client_encoding(), nobody
probably never see this error:
if (pg_set_client_encoding(encoding))
{
elog(ERROR, "Conversion between %s and %s is not supported",
value, GetDatabaseEncodingName());
}
because pg_set_client_encoding() returns -1 for error and 0 as true.
It's fixed too.
IMHO it can be apply.
Karel
PS:
* following files are renamed:
src/utils/mb/Unicode/KOI8_to_utf8.map -->
src/utils/mb/Unicode/koi8r_to_utf8.map
src/utils/mb/Unicode/WIN_to_utf8.map -->
src/utils/mb/Unicode/win1251_to_utf8.map
src/utils/mb/Unicode/utf8_to_KOI8.map -->
src/utils/mb/Unicode/utf8_to_koi8r.map
src/utils/mb/Unicode/utf8_to_WIN.map -->
src/utils/mb/Unicode/utf8_to_win1251.map
* new file:
src/utils/mb/encname.c
* removed file:
src/utils/mb/common.c
--
Karel Zak <zakkr@zf.jcu.cz>
http://home.zf.jcu.cz/~zakkr/
C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz
2001-09-06 06:57:30 +02:00
|
|
|
#ifdef FRONTEND
|
2001-09-21 17:27:38 +02:00
|
|
|
#include "postgres_fe.h"
|
|
|
|
#define Assert(condition)
|
Commit Karel's patch.
-------------------------------------------------------------------
Subject: Re: [PATCHES] encoding names
From: Karel Zak <zakkr@zf.jcu.cz>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-patches <pgsql-patches@postgresql.org>
Date: Fri, 31 Aug 2001 17:24:38 +0200
On Thu, Aug 30, 2001 at 01:30:40AM +0200, Peter Eisentraut wrote:
> > - convert encoding 'name' to 'id'
>
> I thought we decided not to add functions returning "new" names until we
> know exactly what the new names should be, and pending schema
Ok, the patch not to add functions.
> better
>
> ...(): encoding name too long
Fixed.
I found new bug in command/variable.c in parse_client_encoding(), nobody
probably never see this error:
if (pg_set_client_encoding(encoding))
{
elog(ERROR, "Conversion between %s and %s is not supported",
value, GetDatabaseEncodingName());
}
because pg_set_client_encoding() returns -1 for error and 0 as true.
It's fixed too.
IMHO it can be apply.
Karel
PS:
* following files are renamed:
src/utils/mb/Unicode/KOI8_to_utf8.map -->
src/utils/mb/Unicode/koi8r_to_utf8.map
src/utils/mb/Unicode/WIN_to_utf8.map -->
src/utils/mb/Unicode/win1251_to_utf8.map
src/utils/mb/Unicode/utf8_to_KOI8.map -->
src/utils/mb/Unicode/utf8_to_koi8r.map
src/utils/mb/Unicode/utf8_to_WIN.map -->
src/utils/mb/Unicode/utf8_to_win1251.map
* new file:
src/utils/mb/encname.c
* removed file:
src/utils/mb/common.c
--
Karel Zak <zakkr@zf.jcu.cz>
http://home.zf.jcu.cz/~zakkr/
C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz
2001-09-06 06:57:30 +02:00
|
|
|
#else
|
2001-09-21 17:27:38 +02:00
|
|
|
#include "postgres.h"
|
Commit Karel's patch.
-------------------------------------------------------------------
Subject: Re: [PATCHES] encoding names
From: Karel Zak <zakkr@zf.jcu.cz>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-patches <pgsql-patches@postgresql.org>
Date: Fri, 31 Aug 2001 17:24:38 +0200
On Thu, Aug 30, 2001 at 01:30:40AM +0200, Peter Eisentraut wrote:
> > - convert encoding 'name' to 'id'
>
> I thought we decided not to add functions returning "new" names until we
> know exactly what the new names should be, and pending schema
Ok, the patch not to add functions.
> better
>
> ...(): encoding name too long
Fixed.
I found new bug in command/variable.c in parse_client_encoding(), nobody
probably never see this error:
if (pg_set_client_encoding(encoding))
{
elog(ERROR, "Conversion between %s and %s is not supported",
value, GetDatabaseEncodingName());
}
because pg_set_client_encoding() returns -1 for error and 0 as true.
It's fixed too.
IMHO it can be apply.
Karel
PS:
* following files are renamed:
src/utils/mb/Unicode/KOI8_to_utf8.map -->
src/utils/mb/Unicode/koi8r_to_utf8.map
src/utils/mb/Unicode/WIN_to_utf8.map -->
src/utils/mb/Unicode/win1251_to_utf8.map
src/utils/mb/Unicode/utf8_to_KOI8.map -->
src/utils/mb/Unicode/utf8_to_koi8r.map
src/utils/mb/Unicode/utf8_to_WIN.map -->
src/utils/mb/Unicode/utf8_to_win1251.map
* new file:
src/utils/mb/encname.c
* removed file:
src/utils/mb/common.c
--
Karel Zak <zakkr@zf.jcu.cz>
http://home.zf.jcu.cz/~zakkr/
C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz
2001-09-06 06:57:30 +02:00
|
|
|
#endif
|
|
|
|
|
2001-09-21 17:27:38 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
|
Commit Karel's patch.
-------------------------------------------------------------------
Subject: Re: [PATCHES] encoding names
From: Karel Zak <zakkr@zf.jcu.cz>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-patches <pgsql-patches@postgresql.org>
Date: Fri, 31 Aug 2001 17:24:38 +0200
On Thu, Aug 30, 2001 at 01:30:40AM +0200, Peter Eisentraut wrote:
> > - convert encoding 'name' to 'id'
>
> I thought we decided not to add functions returning "new" names until we
> know exactly what the new names should be, and pending schema
Ok, the patch not to add functions.
> better
>
> ...(): encoding name too long
Fixed.
I found new bug in command/variable.c in parse_client_encoding(), nobody
probably never see this error:
if (pg_set_client_encoding(encoding))
{
elog(ERROR, "Conversion between %s and %s is not supported",
value, GetDatabaseEncodingName());
}
because pg_set_client_encoding() returns -1 for error and 0 as true.
It's fixed too.
IMHO it can be apply.
Karel
PS:
* following files are renamed:
src/utils/mb/Unicode/KOI8_to_utf8.map -->
src/utils/mb/Unicode/koi8r_to_utf8.map
src/utils/mb/Unicode/WIN_to_utf8.map -->
src/utils/mb/Unicode/win1251_to_utf8.map
src/utils/mb/Unicode/utf8_to_KOI8.map -->
src/utils/mb/Unicode/utf8_to_koi8r.map
src/utils/mb/Unicode/utf8_to_WIN.map -->
src/utils/mb/Unicode/utf8_to_win1251.map
* new file:
src/utils/mb/encname.c
* removed file:
src/utils/mb/common.c
--
Karel Zak <zakkr@zf.jcu.cz>
http://home.zf.jcu.cz/~zakkr/
C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz
2001-09-06 06:57:30 +02:00
|
|
|
|
1998-03-15 08:39:04 +01:00
|
|
|
/*
|
1998-06-16 09:29:54 +02:00
|
|
|
* 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
|
1998-09-01 06:40:42 +02:00
|
|
|
* supported in the client, you don't need to define
|
1998-06-16 09:29:54 +02:00
|
|
|
* mb2wchar_with_len() function (SJIS is the case).
|
1998-03-15 08:39:04 +01:00
|
|
|
*/
|
1998-08-25 06:19:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SQL/ASCII
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
static int pg_ascii2wchar_with_len
|
1998-09-01 06:40:42 +02:00
|
|
|
(const unsigned char *from, pg_wchar * to, int len)
|
1998-08-25 06:19:16 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int cnt = 0;
|
2000-08-27 12:40:48 +02:00
|
|
|
|
2001-03-08 01:24:34 +01:00
|
|
|
while (len > 0 && *from)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
*to++ = *from++;
|
|
|
|
len--;
|
2000-08-27 12:40:48 +02:00
|
|
|
cnt++;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
|
|
|
*to = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
return (cnt);
|
1998-08-25 06:19:16 +02:00
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static int
|
|
|
|
pg_ascii_mblen(const unsigned char *s)
|
1998-08-25 06:19:16 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
return (1);
|
1998-08-25 06:19:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EUC
|
|
|
|
*/
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
static int pg_euc2wchar_with_len
|
1998-09-01 06:40:42 +02:00
|
|
|
(const unsigned char *from, pg_wchar * to, int len)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int cnt = 0;
|
2000-08-27 12:40:48 +02:00
|
|
|
|
2001-03-08 01:24:34 +01:00
|
|
|
while (len > 0 && *from)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2001-03-08 01:24:34 +01:00
|
|
|
if (*from == SS2 && len >= 2)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
from++;
|
|
|
|
*to = 0xff & *from++;
|
2001-03-08 01:24:34 +01:00
|
|
|
len -= 2;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if (*from == SS3 && len >= 3)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
from++;
|
|
|
|
*to = *from++ << 8;
|
|
|
|
*to |= 0x3f & *from++;
|
|
|
|
len -= 3;
|
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if ((*from & 0x80) && len >= 2)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
*to = *from++ << 8;
|
|
|
|
*to |= *from++;
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*to = *from++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
to++;
|
2000-08-27 12:40:48 +02:00
|
|
|
cnt++;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
|
|
|
*to = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
return (cnt);
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static int
|
|
|
|
pg_euc_mblen(const unsigned char *s)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int len;
|
1998-06-16 09:29:54 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
if (*s == SS2)
|
|
|
|
len = 2;
|
|
|
|
else if (*s == SS3)
|
|
|
|
len = 3;
|
|
|
|
else if (*s & 0x80)
|
|
|
|
len = 2;
|
|
|
|
else
|
|
|
|
len = 1;
|
|
|
|
return (len);
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-06-16 09:29:54 +02:00
|
|
|
* EUC_JP
|
1998-03-15 08:39:04 +01:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
static int pg_eucjp2wchar_with_len
|
1998-09-01 06:40:42 +02:00
|
|
|
(const unsigned char *from, pg_wchar * to, int len)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
return (pg_euc2wchar_with_len(from, to, len));
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static int
|
|
|
|
pg_eucjp_mblen(const unsigned char *s)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
return (pg_euc_mblen(s));
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-06-16 09:29:54 +02:00
|
|
|
* EUC_KR
|
1998-03-15 08:39:04 +01:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
static int pg_euckr2wchar_with_len
|
1998-09-01 06:40:42 +02:00
|
|
|
(const unsigned char *from, pg_wchar * to, int len)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
return (pg_euc2wchar_with_len(from, to, len));
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static int
|
|
|
|
pg_euckr_mblen(const unsigned char *s)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
return (pg_euc_mblen(s));
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
1998-06-16 09:29:54 +02:00
|
|
|
/*
|
|
|
|
* EUC_CN
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
static int pg_euccn2wchar_with_len
|
1998-09-01 06:40:42 +02:00
|
|
|
(const unsigned char *from, pg_wchar * to, int len)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int cnt = 0;
|
2000-08-27 12:40:48 +02:00
|
|
|
|
2001-03-08 01:24:34 +01:00
|
|
|
while (len > 0 && *from)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2001-03-08 01:24:34 +01:00
|
|
|
if (*from == SS2 && len >= 3)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
from++;
|
|
|
|
*to = 0x3f00 & (*from++ << 8);
|
|
|
|
*to = *from++;
|
2001-03-08 01:24:34 +01:00
|
|
|
len -= 3;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if (*from == SS3 && len >= 3)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
from++;
|
|
|
|
*to = *from++ << 8;
|
|
|
|
*to |= 0x3f & *from++;
|
|
|
|
len -= 3;
|
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if ((*from & 0x80) && len >= 2)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
*to = *from++ << 8;
|
|
|
|
*to |= *from++;
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*to = *from++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
to++;
|
2000-08-27 12:40:48 +02:00
|
|
|
cnt++;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
|
|
|
*to = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
return (cnt);
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static int
|
|
|
|
pg_euccn_mblen(const unsigned char *s)
|
1998-06-16 09:29:54 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int len;
|
1998-06-16 09:29:54 +02:00
|
|
|
|
2000-01-25 03:12:27 +01:00
|
|
|
if (*s & 0x80)
|
1998-09-01 06:40:42 +02:00
|
|
|
len = 2;
|
|
|
|
else
|
|
|
|
len = 1;
|
|
|
|
return (len);
|
1998-06-16 09:29:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EUC_TW
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
static int pg_euctw2wchar_with_len
|
1998-09-01 06:40:42 +02:00
|
|
|
(const unsigned char *from, pg_wchar * to, int len)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int cnt = 0;
|
2000-08-27 12:40:48 +02:00
|
|
|
|
2001-03-08 01:24:34 +01:00
|
|
|
while (len > 0 && *from)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2001-03-08 01:24:34 +01:00
|
|
|
if (*from == SS2 && len >= 4)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
from++;
|
|
|
|
*to = *from++ << 16;
|
|
|
|
*to |= *from++ << 8;
|
|
|
|
*to |= *from++;
|
2001-03-08 01:24:34 +01:00
|
|
|
len -= 4;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if (*from == SS3 && len >= 3)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
from++;
|
|
|
|
*to = *from++ << 8;
|
|
|
|
*to |= 0x3f & *from++;
|
|
|
|
len -= 3;
|
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if ((*from & 0x80) && len >= 2)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
*to = *from++ << 8;
|
|
|
|
*to |= *from++;
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*to = *from++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
to++;
|
2000-08-27 12:40:48 +02:00
|
|
|
cnt++;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
|
|
|
*to = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
return (cnt);
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static int
|
|
|
|
pg_euctw_mblen(const unsigned char *s)
|
1998-06-16 09:29:54 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int len;
|
1998-06-16 09:29:54 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
if (*s == SS2)
|
|
|
|
len = 4;
|
|
|
|
else if (*s == SS3)
|
|
|
|
len = 3;
|
|
|
|
else if (*s & 0x80)
|
|
|
|
len = 2;
|
|
|
|
else
|
|
|
|
len = 1;
|
|
|
|
return (len);
|
1998-06-16 09:29:54 +02:00
|
|
|
}
|
|
|
|
|
1998-03-15 08:39:04 +01:00
|
|
|
/*
|
2000-10-12 08:06:50 +02:00
|
|
|
* convert UTF-8 string to pg_wchar (UCS-2)
|
1998-03-15 08:39:04 +01:00
|
|
|
* caller should allocate enough space for "to"
|
|
|
|
* len: length of from.
|
|
|
|
* "from" not necessarily null terminated.
|
|
|
|
*/
|
2000-08-27 12:40:48 +02:00
|
|
|
static int
|
1998-09-01 06:40:42 +02:00
|
|
|
pg_utf2wchar_with_len(const unsigned char *from, pg_wchar * to, int len)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
unsigned char c1,
|
|
|
|
c2,
|
|
|
|
c3;
|
2001-03-22 05:01:46 +01:00
|
|
|
int cnt = 0;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2001-03-08 01:24:34 +01:00
|
|
|
while (len > 0 && *from)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
if ((*from & 0x80) == 0)
|
|
|
|
{
|
|
|
|
*to = *from++;
|
|
|
|
len--;
|
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if ((*from & 0xe0) == 0xc0 && len >= 2)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
c1 = *from++ & 0x1f;
|
|
|
|
c2 = *from++ & 0x3f;
|
|
|
|
*to = c1 << 6;
|
|
|
|
*to |= c2;
|
2001-03-08 01:24:34 +01:00
|
|
|
len -= 2;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if ((*from & 0xe0) == 0xe0 && len >= 3)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
c1 = *from++ & 0x0f;
|
|
|
|
c2 = *from++ & 0x3f;
|
|
|
|
c3 = *from++ & 0x3f;
|
|
|
|
*to = c1 << 12;
|
|
|
|
*to |= c2 << 6;
|
|
|
|
*to |= c3;
|
2001-03-08 01:24:34 +01:00
|
|
|
len -= 3;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
1999-04-25 22:35:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*to = *from++;
|
|
|
|
len--;
|
|
|
|
}
|
1998-09-01 06:40:42 +02:00
|
|
|
to++;
|
2000-08-27 12:40:48 +02:00
|
|
|
cnt++;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
|
|
|
*to = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
return (cnt);
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
2000-10-12 08:06:50 +02:00
|
|
|
/*
|
|
|
|
* returns the byte length of a UTF-8 word pointed to by s
|
|
|
|
*/
|
|
|
|
int
|
1998-09-01 06:40:42 +02:00
|
|
|
pg_utf_mblen(const unsigned char *s)
|
1998-06-16 09:29:54 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int len = 1;
|
1998-06-16 09:29:54 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
if ((*s & 0x80) == 0)
|
|
|
|
len = 1;
|
|
|
|
else if ((*s & 0xe0) == 0xc0)
|
|
|
|
len = 2;
|
|
|
|
else if ((*s & 0xe0) == 0xe0)
|
|
|
|
len = 3;
|
|
|
|
return (len);
|
1998-06-16 09:29:54 +02:00
|
|
|
}
|
|
|
|
|
1998-03-15 08:39:04 +01:00
|
|
|
/*
|
|
|
|
* convert mule internal code to pg_wchar
|
|
|
|
* caller should allocate enough space for "to"
|
|
|
|
* len: length of from.
|
|
|
|
* "from" not necessarily null terminated.
|
|
|
|
*/
|
2000-08-27 12:40:48 +02:00
|
|
|
static int
|
1998-09-01 06:40:42 +02:00
|
|
|
pg_mule2wchar_with_len(const unsigned char *from, pg_wchar * to, int len)
|
1998-03-15 08:39:04 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int cnt = 0;
|
2000-08-27 12:40:48 +02:00
|
|
|
|
2001-03-08 01:24:34 +01:00
|
|
|
while (len > 0 && *from)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2001-03-08 01:24:34 +01:00
|
|
|
if (IS_LC1(*from) && len >= 2)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
*to = *from++ << 16;
|
|
|
|
*to |= *from++;
|
|
|
|
len -= 2;
|
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if (IS_LCPRV1(*from) && len >= 3)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
from++;
|
|
|
|
*to = *from++ << 16;
|
|
|
|
*to |= *from++;
|
|
|
|
len -= 3;
|
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if (IS_LC2(*from) && len >= 3)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
*to = *from++ << 16;
|
|
|
|
*to |= *from++ << 8;
|
|
|
|
*to |= *from++;
|
|
|
|
len -= 3;
|
|
|
|
}
|
2001-03-08 01:24:34 +01:00
|
|
|
else if (IS_LCPRV2(*from) && len >= 4)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
|
|
|
from++;
|
|
|
|
*to = *from++ << 16;
|
|
|
|
*to |= *from++ << 8;
|
|
|
|
*to |= *from++;
|
|
|
|
len -= 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* assume ASCII */
|
|
|
|
*to = (unsigned char) *from++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
to++;
|
2000-08-27 12:40:48 +02:00
|
|
|
cnt++;
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
|
|
|
*to = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
return (cnt);
|
1998-03-15 08:39:04 +01:00
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
int
|
|
|
|
pg_mule_mblen(const unsigned char *s)
|
1998-04-27 19:10:50 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int len;
|
1998-04-27 19:10:50 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
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);
|
1998-04-27 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
1998-06-16 09:29:54 +02:00
|
|
|
/*
|
|
|
|
* ISO8859-1
|
|
|
|
*/
|
2000-08-27 12:40:48 +02:00
|
|
|
static int
|
1998-09-01 06:40:42 +02:00
|
|
|
pg_latin12wchar_with_len(const unsigned char *from, pg_wchar * to, int len)
|
1998-04-27 19:10:50 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int cnt = 0;
|
2000-08-27 12:40:48 +02:00
|
|
|
|
2001-03-08 01:24:34 +01:00
|
|
|
while (len > 0 && *from)
|
2000-08-27 12:40:48 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
*to++ = *from++;
|
2001-03-08 01:24:34 +01:00
|
|
|
len--;
|
2000-08-27 12:40:48 +02:00
|
|
|
cnt++;
|
|
|
|
}
|
1998-09-01 06:40:42 +02:00
|
|
|
*to = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
return (cnt);
|
1998-04-27 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
static int
|
|
|
|
pg_latin1_mblen(const unsigned char *s)
|
1998-04-27 19:10:50 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
return (1);
|
1998-04-27 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
1998-06-16 09:29:54 +02:00
|
|
|
/*
|
|
|
|
* SJIS
|
|
|
|
*/
|
1998-09-01 06:40:42 +02:00
|
|
|
static int
|
|
|
|
pg_sjis_mblen(const unsigned char *s)
|
1998-04-27 19:10:50 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int len;
|
1998-04-27 19:10:50 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
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);
|
1998-04-27 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
1999-02-02 19:51:40 +01:00
|
|
|
/*
|
|
|
|
* Big5
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pg_big5_mblen(const unsigned char *s)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (*s > 0x7f)
|
|
|
|
{ /* kanji? */
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* should be ASCII */
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
1998-07-24 05:32:46 +02:00
|
|
|
pg_wchar_tbl pg_wchar_table[] = {
|
2001-09-11 06:50:36 +02:00
|
|
|
{pg_ascii2wchar_with_len, pg_ascii_mblen, 1}, /* 0; PG_SQL_ASCII */
|
|
|
|
{pg_eucjp2wchar_with_len, pg_eucjp_mblen, 3}, /* 1; PG_EUC_JP */
|
|
|
|
{pg_euccn2wchar_with_len, pg_euccn_mblen, 3}, /* 2; PG_EUC_CN */
|
|
|
|
{pg_euckr2wchar_with_len, pg_euckr_mblen, 3}, /* 3; PG_EUC_KR */
|
|
|
|
{pg_euctw2wchar_with_len, pg_euctw_mblen, 3}, /* 4; PG_EUC_TW */
|
|
|
|
{pg_utf2wchar_with_len, pg_utf_mblen, 3}, /* 5; PG_UNICODE */
|
|
|
|
{pg_mule2wchar_with_len, pg_mule_mblen, 3}, /* 6; PG_MULE_INTERNAL */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 7; PG_LATIN1 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 8; PG_LATIN2 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 9; PG_LATIN3 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 10; PG_LATIN4 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 11; PG_LATIN5 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 12; PG_KOI8 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 13; PG_WIN1251 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 14; PG_ALT */
|
2001-10-11 16:20:35 +02:00
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 15; ISO-8859-5 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 16; ISO-8859-6 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 17; ISO-8859-7 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 18; ISO-8859-8 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 19; ISO-8859-10 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 20; ISO-8859-13 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 21; ISO-8859-14 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 22; ISO-8859-15 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1}, /* 23; ISO-8859-16 */
|
|
|
|
{0, pg_sjis_mblen, 2}, /* 24; PG_SJIS */
|
|
|
|
{0, pg_big5_mblen, 2}, /* 25; PG_BIG5 */
|
|
|
|
{pg_latin12wchar_with_len, pg_latin1_mblen, 1} /* 26; PG_WIN1250 */
|
1998-06-16 09:29:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* returns the byte length of a word for mule internal code */
|
1998-09-01 06:40:42 +02:00
|
|
|
int
|
|
|
|
pg_mic_mblen(const unsigned char *mbstr)
|
1998-06-16 09:29:54 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
return (pg_mule_mblen(mbstr));
|
1998-06-16 09:29:54 +02:00
|
|
|
}
|
2001-02-11 02:59:22 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2001-02-11 02:59:22 +01:00
|
|
|
* Returns the byte length of a multi-byte word.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pg_encoding_mblen(int encoding, const unsigned char *mbstr)
|
|
|
|
{
|
Commit Karel's patch.
-------------------------------------------------------------------
Subject: Re: [PATCHES] encoding names
From: Karel Zak <zakkr@zf.jcu.cz>
To: Peter Eisentraut <peter_e@gmx.net>
Cc: pgsql-patches <pgsql-patches@postgresql.org>
Date: Fri, 31 Aug 2001 17:24:38 +0200
On Thu, Aug 30, 2001 at 01:30:40AM +0200, Peter Eisentraut wrote:
> > - convert encoding 'name' to 'id'
>
> I thought we decided not to add functions returning "new" names until we
> know exactly what the new names should be, and pending schema
Ok, the patch not to add functions.
> better
>
> ...(): encoding name too long
Fixed.
I found new bug in command/variable.c in parse_client_encoding(), nobody
probably never see this error:
if (pg_set_client_encoding(encoding))
{
elog(ERROR, "Conversion between %s and %s is not supported",
value, GetDatabaseEncodingName());
}
because pg_set_client_encoding() returns -1 for error and 0 as true.
It's fixed too.
IMHO it can be apply.
Karel
PS:
* following files are renamed:
src/utils/mb/Unicode/KOI8_to_utf8.map -->
src/utils/mb/Unicode/koi8r_to_utf8.map
src/utils/mb/Unicode/WIN_to_utf8.map -->
src/utils/mb/Unicode/win1251_to_utf8.map
src/utils/mb/Unicode/utf8_to_KOI8.map -->
src/utils/mb/Unicode/utf8_to_koi8r.map
src/utils/mb/Unicode/utf8_to_WIN.map -->
src/utils/mb/Unicode/utf8_to_win1251.map
* new file:
src/utils/mb/encname.c
* removed file:
src/utils/mb/common.c
--
Karel Zak <zakkr@zf.jcu.cz>
http://home.zf.jcu.cz/~zakkr/
C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz
2001-09-06 06:57:30 +02:00
|
|
|
Assert(PG_VALID_ENCODING(encoding));
|
|
|
|
|
|
|
|
return( (encoding >= 0 &&
|
|
|
|
encoding < sizeof(pg_wchar_table)/sizeof(pg_wchar_tbl)) ?
|
|
|
|
((*pg_wchar_table[encoding].mblen) (mbstr)) :
|
|
|
|
((*pg_wchar_table[PG_SQL_ASCII].mblen) (mbstr)));
|
2001-02-11 02:59:22 +01:00
|
|
|
}
|
2001-09-11 06:50:36 +02:00
|
|
|
|
2001-09-21 17:27:38 +02:00
|
|
|
/*
|
|
|
|
* fetch maximum length of a char encoding
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pg_encoding_max_length(int encoding)
|
|
|
|
{
|
|
|
|
Assert(PG_VALID_ENCODING(encoding));
|
|
|
|
|
|
|
|
return pg_wchar_table[encoding].maxmblen;
|
|
|
|
}
|
|
|
|
|
2001-09-11 06:50:36 +02:00
|
|
|
#ifndef FRONTEND
|
|
|
|
/*
|
|
|
|
* Verify mbstr to make sure that it has a valid character sequence.
|
|
|
|
* mbstr is not necessarily NULL terminated. length of mbstr is
|
|
|
|
* specified by len. If an error was found, returns an error message.
|
|
|
|
* Note that the message is kept in a static buffer, the next invocation
|
|
|
|
* might break the message.
|
|
|
|
* If no error was found, this function returns NULL.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
pg_verifymbstr(const unsigned char *mbstr, int len)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
int i, j;
|
|
|
|
static char buf[256];
|
|
|
|
int slen = 0;
|
|
|
|
|
|
|
|
/* we do not check single byte encodings */
|
2001-10-15 03:19:15 +02:00
|
|
|
if (pg_database_encoding_max_length() <= 1)
|
2001-09-11 06:50:36 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (len > 0 && *mbstr)
|
|
|
|
{
|
2001-10-15 03:19:15 +02:00
|
|
|
/* special UTF-8 check */
|
|
|
|
if (GetDatabaseEncoding() == PG_UTF8 &&
|
|
|
|
(*mbstr & 0xf8) == 0xf0)
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "Unicode >= 0x10000 is not supoorted");
|
|
|
|
return(buf);
|
|
|
|
}
|
|
|
|
|
2001-09-11 06:50:36 +02:00
|
|
|
l = pg_mblen(mbstr);
|
|
|
|
|
|
|
|
/* multi-byte letter? */
|
|
|
|
if (l > 1)
|
|
|
|
{
|
|
|
|
for (i=1;i<l;i++)
|
|
|
|
{
|
|
|
|
if (i > len || *(mbstr+i) == '\0' ||
|
|
|
|
/* we assume that every muti-byte letter
|
|
|
|
* consists of bytes being the 8th bit set
|
|
|
|
*/
|
|
|
|
((*(mbstr+i) & 0x80) == 0))
|
|
|
|
{
|
|
|
|
int remains = sizeof(buf);
|
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
slen = snprintf(p, remains, "Invalid %s character sequence found (0x",
|
|
|
|
GetDatabaseEncodingName());
|
|
|
|
p += slen;
|
|
|
|
remains -= slen;
|
|
|
|
|
|
|
|
i = ((*(mbstr+i) & 0x80) == 0)?l:i;
|
|
|
|
|
|
|
|
for (j=0;j<i;j++)
|
|
|
|
{
|
|
|
|
slen = snprintf(p, remains, "%02x",
|
|
|
|
*(mbstr+j));
|
|
|
|
p += slen;
|
|
|
|
remains -= slen;
|
|
|
|
}
|
|
|
|
snprintf(p, remains, ")");
|
|
|
|
return(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len -= l;
|
|
|
|
mbstr += l;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-23 12:59:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fetch maximum length of a char encoding for the current database
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pg_database_encoding_max_length(void)
|
|
|
|
{
|
|
|
|
return pg_wchar_table[GetDatabaseEncoding()].maxmblen;
|
|
|
|
}
|
|
|
|
|
2001-09-11 06:50:36 +02:00
|
|
|
#endif
|