dcerpc-nt: add UNION_ALIGN_TO... helpers
[wireshark-sm.git] / tools / make_charset_table.c
blob27d921a4543a97c927cfb4f69e96c83e0aea81b2
1 /* make_charset_table.c
2 * sample program to generate tables for charsets.c using iconv
4 * public domain
5 */
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <errno.h>
10 #include <iconv.h>
12 #define UNREPL 0xFFFD
14 int main(int argc, char **argv) {
15 /* for now only UCS-2 */
16 uint16_t table[0x100];
18 iconv_t conv;
19 const char *charset;
20 int i, j;
22 /* 0x00 ... 0x7F same as ASCII? */
23 int ascii_based = 1;
24 /* 0x00 ... 0x9F same as ISO? */
25 int iso_based = 1;
27 if (argc != 2) {
28 printf("usage: %s <charset>\n", argv[0]);
29 return 1;
32 charset = argv[1];
34 conv = iconv_open("UCS-2", charset);
35 if (conv == (iconv_t) -1) {
36 perror("iconv_open");
37 return 2;
39 iconv_close(conv);
41 for (i = 0x00; i < 0x100; i++) {
42 unsigned char in[1], out[2];
43 size_t inlen = 1, outlen = 2;
45 char *inbuf = (char *) in;
46 char *outbuf = (char *) out;
48 size_t ret;
50 in[0] = i;
52 conv = iconv_open("UCS-2BE", charset);
54 if (conv == (iconv_t) -1) {
55 /* shouldn't fail now */
56 perror("iconv_open");
57 return 2;
60 ret = iconv(conv, &inbuf, &inlen, &outbuf, &outlen);
62 if (ret == (size_t) -1 && errno == EILSEQ) {
63 table[i] = UNREPL;
64 iconv_close(conv);
65 continue;
68 if (ret == (size_t) -1) {
69 perror("iconv");
70 iconv_close(conv);
71 return 4;
74 iconv_close(conv);
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;
81 if (i < 0x80 && (out[0] != 0 || out[1] != i))
82 ascii_based = 0;
84 if (i < 0xA0 && (out[0] != 0 || out[1] != i))
85 iso_based = 0;
87 table[i] = (out[0] << 8) | out[1];
90 /* iso_based not supported */
91 iso_based = 0;
93 printf("/* generated by %s %s */\n", argv[0], charset);
95 if (iso_based)
96 i = 0xA0;
97 else if (ascii_based)
98 i = 0x80;
99 else
100 i = 0;
102 printf("const gunichar2 charset_table_%s[0x%x] = {\n", charset, 0x100 - i);
103 while (i < 0x100) {
104 int start = i;
106 printf(" ");
108 for (j = 0; j < 8; j++, i++) {
109 if (table[i] == UNREPL)
110 printf("UNREPL, ");
111 else
112 printf("0x%.4x, ", table[i]);
115 if ((start & 0xf) == 0)
116 printf(" /* 0x%.2X - */", start);
117 else
118 printf(" /* - 0x%.2X */", i - 1);
120 printf("\n");
122 printf("};\n");
124 return 0;