Avoid adding -pie on older cmake versions
[metze/wireshark/wip.git] / tools / make_charset_table.c
1 /* make_charset_table.c
2  * sample program to generate tables for charsets.c using iconv
3  *
4  * public domain
5  */
6
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <errno.h>
10 #include <iconv.h>
11
12 #define UNREPL 0xFFFD
13
14 int main(int argc, char **argv) {
15         /* for now only UCS-2 */
16         uint16_t table[0x100];
17
18         iconv_t conv;
19         const char *charset;
20         int i, j;
21
22         /* 0x00 ... 0x7F same as ASCII? */
23         int ascii_based = 1;
24         /* 0x00 ... 0x9F same as ISO? */
25         int iso_based = 1;
26
27         if (argc != 2) {
28                 printf("usage: %s <charset>\n", argv[0]);
29                 return 1;
30         }
31
32         charset = argv[1];
33
34         conv = iconv_open("UCS-2", charset);
35         if (conv == (iconv_t) -1) {
36                 perror("iconv_open");
37                 return 2;
38         }
39         iconv_close(conv);
40
41         for (i = 0x00; i < 0x100; i++) {
42                 unsigned char in[1], out[2];
43                 size_t inlen = 1, outlen = 2;
44
45                 char *inbuf = (char *) in;
46                 char *outbuf = (char *) out;
47
48                 size_t ret;
49
50                 in[0] = i;
51
52                 conv = iconv_open("UCS-2BE", charset);
53
54                 if (conv == (iconv_t) -1) {
55                         /* shouldn't fail now */
56                         perror("iconv_open");
57                         return 2;
58                 }
59
60                 ret = iconv(conv, &inbuf, &inlen, &outbuf, &outlen);
61
62                 if (ret == (size_t) -1 && errno == EILSEQ) {
63                         table[i] = UNREPL;
64                         iconv_close(conv);
65                         continue;
66                 }
67
68                 if (ret == (size_t) -1) {
69                         perror("iconv");
70                         iconv_close(conv);
71                         return 4;
72                 }
73
74                 iconv_close(conv);
75
76                 if (ret != 0 || inlen != 0 || outlen != 0) {
77                         fprintf(stderr, "%d: smth went wrong: %zu %zu %zu\n", i, ret, inlen, outlen);
78                         return 3;
79                 }
80
81                 if (i < 0x80 && (out[0] != 0 || out[1] != i))
82                         ascii_based = 0;
83
84                 if (i < 0xA0 && (out[0] != 0 || out[1] != i))
85                         iso_based = 0;
86                         
87                 table[i] = (out[0] << 8) | out[1];
88         }
89
90         /* iso_based not supported */
91         iso_based = 0;
92
93         printf("/* generated by %s %s */\n", argv[0], charset);
94
95         if (iso_based)
96                 i = 0xA0;
97         else if (ascii_based)
98                 i = 0x80;
99         else
100                 i = 0;
101
102         printf("const gunichar2 charset_table_%s[0x%x] = {\n", charset, 0x100 - i);
103         while (i < 0x100) {
104                 int start = i;
105
106                 printf("    ");
107
108                 for (j = 0; j < 8; j++, i++) {
109                         if (table[i] == UNREPL)
110                                 printf("UNREPL, ");
111                         else
112                                 printf("0x%.4x, ", table[i]);
113                 }
114
115                 if ((start & 0xf) == 0)
116                         printf("       /* 0x%.2X -      */", start);
117                 else
118                         printf("       /*      - 0x%.2X */", i - 1);
119
120                 printf("\n");
121         }
122         printf("};\n");
123
124         return 0;
125 }