1 /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
22 
23 #include <stdio.h>
24 #include <string.h>
25 
print_short_array(unsigned short * a,size_t width)26 static void print_short_array(unsigned short *a, size_t width)
27 {
28   int i;
29   printf("{\n");
30   for (i=0; i<=0xFF; i++)
31   {
32     const char *fmt= (width==4) ? "0x%04X" : "0x%02X";
33     printf(fmt,(int)a[i]);
34     printf("%s%s",i<0xFF?",":"",(i+1) % 8 ? "" :"\n");
35   }
36   printf("};\n");
37 
38 }
39 
40 
41 
main(void)42 int main(void)
43 {
44   char str[160];
45   unsigned short touni[256];
46   unsigned short fromuni[65536];
47   unsigned short fromstat[256];
48   int i;
49 
50   memset(touni, 0, sizeof(touni));
51   memset(fromuni, 0, sizeof(fromuni));
52   memset(fromstat, 0, sizeof(fromstat));
53 
54   while (fgets(str,sizeof(str),stdin))
55   {
56     unsigned int c,u;
57 
58     if ((str[0]=='#') || (2!=sscanf(str,"%x%x",&c,&u)))
59       continue;
60     if (c>0xFF || u>0xFFFF)
61       continue;
62 
63     touni[c]= u;
64     fromuni[u]= c;
65   }
66 
67   printf("unsigned short cs_to_uni[256]=");
68   print_short_array(touni, 4);
69 
70   for (i=0;i<=0xFF;i++)
71   {
72     fromstat[touni[i]>>8]++;
73   }
74 
75   for (i=0;i<=256;i++)
76   {
77     if (fromstat[i])
78     {
79       printf("unsigned char pl%02X[256]=",i);
80       print_short_array(fromuni+i*256, 2);
81     }
82   }
83 
84   printf("unsigned short *uni_to_cs[256]={\n");
85   for (i=0;i<=255;i++)
86   {
87     if (fromstat[i])
88       printf("pl%02X",i);
89     else
90       printf("NULL");
91     printf("%s%s",i<255?",":"",((i+1) % 8) ? "":"\n");
92   }
93   printf("};\n");
94 
95   return 0;
96 }
97