aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/mb/uconv.c
blob: 0db250f50073a3b7ed8b5b59655928f24712d08c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * $Id: uconv.c,v 1.1 2000/10/12 06:06:50 ishii Exp $
 */

#include "pg_wchar.h"

/*
 * convert UCS-2 to UTF-8
 * returns number of bytes of a UTF-8, that is atmost 3.
 */
static int
pg_ucs2utf(const unsigned short ucs, unsigned char *utf)
{
	int len;

	if (ucs <= 0x007f)
	{
		*utf = ucs;
		len = 1;
	}
	else if (ucs > 0x007f && ucs <= 0x07ff)
	{
		*utf++ = (ucs >> 6) | 0xc0;
		*utf = (ucs & 0x003f) | 0x80;
		len = 2;
	}
	else
	{
		*utf++ = (ucs >> 12) | 0xe0;
		*utf++ = ((ucs & 0x0fc0) >> 6) | 0x80;
		*utf = (ucs & 0x003f) | 0x80;
		len = 3;
	}
	return (len);
}

typedef struct
{
	unsigned short ucs;	/* UCS-2 */
	unsigned short code;		/* local code */
	unsigned char encoding;		/* encoding */
} ucs_to_local;

typedef struct
{
	unsigned short code;		/* local code */
	unsigned short ucs;	/* UCS-2 */
} local_to_ucs;

#include "ucs_to_iso8859.map"
#include "iso88592.rev"
#include "iso88593.rev"
#include "iso88594.rev"
#include "iso88595.rev"

#define X0208 0
#define X0212 1
#include "ucs_to_jis.map"

int
main()
{
  int i,j;
  int l;
  unsigned int euc;
  unsigned char u[4];
  FILE *fd;

  printf("static pg_utf_to_local mapISO8859[] = {\n");
  for (i=0;i<sizeof(mapISO8859)/sizeof(ucs_to_local);i++) {
	if (mapISO8859[i].encoding > LATIN5)
	  continue;
	l = pg_ucs2utf(mapISO8859[i].ucs, u);
	printf("  {0x");
	for(j=0;j<l;j++) {
	  printf("%02x", u[j]);
	}
	printf(", 0x%04x, %s},\n",
		   mapISO8859[i].code|0x80, 
		   pg_get_enc_ent(mapISO8859[i].encoding)->name);
  }
  printf("};\n");

  printf("\nstatic pg_local_to_utf ISO8859_2[] = {\n");
  for (i=0;i<sizeof(revISO8859_2)/sizeof(local_to_ucs);i++) {
	l = pg_ucs2utf(revISO8859_2[i].ucs, u);
	printf(" {0x%04x, ", revISO8859_2[i].code|0x80);
	printf("0x");
	for(j=0;j<l;j++) {
	  printf("%02x", u[j]);
	}
	printf("},\n");
  }
  printf("};\n");
  
  printf("\nstatic pg_local_to_utf ISO8859_3[] = {\n");
  for (i=0;i<sizeof(revISO8859_3)/sizeof(local_to_ucs);i++) {
	l = pg_ucs2utf(revISO8859_3[i].ucs, u);
	printf(" {0x%04x, ", revISO8859_3[i].code|0x80);
	printf("0x");
	for(j=0;j<l;j++) {
	  printf("%02x", u[j]);
	}
	printf("},\n");
  }
  printf("};\n");

  printf("\nstatic pg_local_to_utf ISO8859_4[] = {\n");
  for (i=0;i<sizeof(revISO8859_4)/sizeof(local_to_ucs);i++) {
	l = pg_ucs2utf(revISO8859_4[i].ucs, u);
	printf(" {0x%04x, ", revISO8859_4[i].code|0x80);
	printf("0x");
	for(j=0;j<l;j++) {
	  printf("%02x", u[j]);
	}
	printf("},\n");
  }
  printf("};\n");

  printf("\nstatic pg_local_to_utf ISO8859_5[] = {\n");
  for (i=0;i<sizeof(revISO8859_5)/sizeof(local_to_ucs);i++) {
	l = pg_ucs2utf(revISO8859_5[i].ucs, u);
	printf(" {0x%04x, ", revISO8859_5[i].code|0x80);
	printf("0x");
	for(j=0;j<l;j++) {
	  printf("%02x", u[j]);
	}
	printf("},\n");
  }
  printf("};\n");

  fd = fopen("UTF_to_EUC_JP.map", "w");
  fprintf(fd, "static pg_utf_to_local mapUTF_to_EUC_JP[] = {\n");
  for (i=0;i<sizeof(mapJIS)/sizeof(ucs_to_local);i++) {
	l = pg_ucs2utf(mapJIS[i].ucs, u);
	fprintf(fd, "  {0x");
	for(j=0;j<l;j++) {
	  fprintf(fd, "%02x", u[j]);
	}
	if (mapJIS[i].encoding == X0208)
	{
		euc = mapJIS[i].code|0x8080;
	}
	else
	{
		euc = SS3 << 16 | mapJIS[i].code | 0x8080;
	}
	fprintf(fd, ", 0x%04x, %s},\n",
		   euc,
		   "EUC_JP");
  }
  fprintf(fd, "};\n");

  fclose(fd);

  return(0);
}