xref: /aosp_15_r20/external/flac/src/share/utf8/makemap.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1*600f14f4SXin Li /*
2*600f14f4SXin Li  * Copyright (C) 2001 Edmund Grimley Evans <[email protected]>
3*600f14f4SXin Li  *
4*600f14f4SXin Li  * This program is free software; you can redistribute it and/or modify
5*600f14f4SXin Li  * it under the terms of the GNU General Public License as published by
6*600f14f4SXin Li  * the Free Software Foundation; either version 2 of the License, or
7*600f14f4SXin Li  * (at your option) any later version.
8*600f14f4SXin Li  *
9*600f14f4SXin Li  * This program is distributed in the hope that it will be useful,
10*600f14f4SXin Li  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*600f14f4SXin Li  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*600f14f4SXin Li  * GNU General Public License for more details.
13*600f14f4SXin Li  *
14*600f14f4SXin Li  * You should have received a copy of the GNU General Public License along
15*600f14f4SXin Li  * with this program; if not, write to the Free Software Foundation, Inc.,
16*600f14f4SXin Li  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17*600f14f4SXin Li  */
18*600f14f4SXin Li 
19*600f14f4SXin Li #ifdef HAVE_CONFIG_H
20*600f14f4SXin Li #  include <config.h>
21*600f14f4SXin Li #endif
22*600f14f4SXin Li 
23*600f14f4SXin Li #include <errno.h>
24*600f14f4SXin Li #include <iconv.h>
25*600f14f4SXin Li #include <stdio.h>
26*600f14f4SXin Li 
main(int argc,char * argv[])27*600f14f4SXin Li int main(int argc, char *argv[])
28*600f14f4SXin Li {
29*600f14f4SXin Li   iconv_t cd;
30*600f14f4SXin Li   const char *ib;
31*600f14f4SXin Li   char *ob;
32*600f14f4SXin Li   size_t ibl, obl, k;
33*600f14f4SXin Li   uint8_t c, buf[4];
34*600f14f4SXin Li   int i, wc;
35*600f14f4SXin Li 
36*600f14f4SXin Li   if (argc != 2) {
37*600f14f4SXin Li     printf("Usage: %s ENCODING\n", argv[0]);
38*600f14f4SXin Li     printf("Output a charset map for the 8-bit ENCODING.\n");
39*600f14f4SXin Li     return 1;
40*600f14f4SXin Li   }
41*600f14f4SXin Li 
42*600f14f4SXin Li   cd = iconv_open("UCS-4", argv[1]);
43*600f14f4SXin Li   if (cd == (iconv_t)(-1)) {
44*600f14f4SXin Li     perror("iconv_open");
45*600f14f4SXin Li     return 1;
46*600f14f4SXin Li   }
47*600f14f4SXin Li 
48*600f14f4SXin Li   for (i = 0; i < 256; i++) {
49*600f14f4SXin Li     c = i;
50*600f14f4SXin Li     ib = &c;
51*600f14f4SXin Li     ibl = 1;
52*600f14f4SXin Li     ob = buf;
53*600f14f4SXin Li     obl = 4;
54*600f14f4SXin Li     k = iconv(cd, &ib, &ibl, &ob, &obl);
55*600f14f4SXin Li     if (!k && !ibl && !obl) {
56*600f14f4SXin Li       wc = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
57*600f14f4SXin Li       if (wc >= 0xffff) {
58*600f14f4SXin Li 	printf("Dodgy value.\n");
59*600f14f4SXin Li 	return 1;
60*600f14f4SXin Li       }
61*600f14f4SXin Li     }
62*600f14f4SXin Li     else if (k == (size_t)(-1) && errno == EILSEQ)
63*600f14f4SXin Li       wc = 0xffff;
64*600f14f4SXin Li     else {
65*600f14f4SXin Li       printf("Non-standard iconv.\n");
66*600f14f4SXin Li       return 1;
67*600f14f4SXin Li     }
68*600f14f4SXin Li 
69*600f14f4SXin Li     if (i % 8 == 0)
70*600f14f4SXin Li       printf("  ");
71*600f14f4SXin Li     printf("0x%04x", wc);
72*600f14f4SXin Li     if (i == 255)
73*600f14f4SXin Li       printf("\n");
74*600f14f4SXin Li     else if (i % 8 == 7)
75*600f14f4SXin Li       printf(",\n");
76*600f14f4SXin Li     else
77*600f14f4SXin Li       printf(", ");
78*600f14f4SXin Li   }
79*600f14f4SXin Li 
80*600f14f4SXin Li   return 0;
81*600f14f4SXin Li }
82