]> Pileus Git - ~andy/fetchmail/blob - ucs/norm_charmap.c
Minor bug fixes for socket.c
[~andy/fetchmail] / ucs / norm_charmap.c
1 /*
2  * The Single Unix Specification function nl_langinfo(CODESET)
3  * returns the name of the encoding used by the currently selected
4  * locale:
5  *
6  *   http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
7  *
8  * Unfortunately the encoding names are not yet standardized.
9  * This function knows about the encoding names used on many
10  * different systems and converts them where possible into
11  * the corresponding MIME charset name registered in
12  *
13  *   http://www.iana.org/assignments/character-sets
14  *
15  * Please extend it as needed and suggest improvements to the author.
16  *
17  * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11
18  * Permission to use, copy, modify, and distribute this software
19  * for any purpose and without fee is hereby granted. The author
20  * disclaims all warranties with regard to this software.
21  *
22  * Latest version:
23  *
24  *   http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
25  */
26
27 #include "config.h" /* import AC_C_CONST effects */
28 #include "norm_charmap.h"
29
30 #include <string.h>
31
32 #ifdef TEST
33 #include <stdio.h>
34 #include <locale.h>
35 #include <langinfo.h>
36 #endif
37
38 #define digit(x) ((x) >= '0' && (x) <= '9')
39
40 static char buf[16];
41
42 const char *norm_charmap(const char *name)
43 {
44   const char *p;
45   
46   if (!name)
47     return name;
48   
49   /* Many need no remapping, but they are listed here so you
50    * can see what output to expect, and modify for your needs
51    * as necessary. */
52   if (!strcmp(name, "UTF-8"))
53     return "UTF-8";
54   if (!strcmp(name, "EUC-JP"))
55     return "EUC-JP";
56   if (!strcmp(name, "EUC-KR"))
57     return "EUC-KR";
58   if (!strcmp(name, "EUC-TW"))
59     return "EUC-TW";
60   if (!strcmp(name, "KOI8-R"))
61     return "KOI8-R";
62   if (!strcmp(name, "KOI8-U"))
63     return "KOI8-U";
64   if (!strcmp(name, "GBK"))
65     return "GBK";
66   if (!strcmp(name, "GB2312"))
67     return "GB2312";
68   if (!strcmp(name, "GB18030"))
69     return "GB18030";
70   if (!strcmp(name, "VSCII"))
71     return "VSCII";
72   
73   /* ASCII comes in many names */
74   if (!strcmp(name, "ASCII") ||
75       !strcmp(name, "US-ASCII") ||
76       !strcmp(name, "ANSI_X3.4-1968") ||
77       !strcmp(name, "646") ||
78       !strcmp(name, "ISO646") ||
79       !strcmp(name, "ISO_646.IRV"))
80     return "US-ASCII";
81
82   /* ISO 8859 will be converted to "ISO-8859-x" */
83   if ((p = strstr(name, "8859-"))) {
84     memcpy(buf, "ISO-8859-\0\0", 12);
85     p += 5;
86     if (digit(*p)) {
87       buf[9] = *p++;
88       if (digit(*p)) buf[10] = *p;
89       return buf;
90     }
91   }
92
93   /* Windows code pages will be converted to "WINDOWS-12xx" */
94   if ((p = strstr(name, "CP12"))) {
95     memcpy(buf, "WINDOWS-12\0\0", 13);
96     p += 4;
97     if (digit(*p)) {
98       buf[10] = *p++;
99       if (digit(*p)) buf[11] = *p;
100       return buf;
101     }
102   }
103
104   /* TIS-620 comes in at least the following two forms */
105   if (!strcmp(name, "TIS-620") ||
106       !strcmp(name, "TIS620.2533"))
107     return "ISO-8859-11";
108
109   /* For some, uppercase/lowercase might differ */
110   if (!strcmp(name, "Big5") || !strcmp(name, "BIG5"))
111     return "Big5";
112   if (!strcmp(name, "Big5HKSCS") || !strcmp(name, "BIG5HKSCS"))
113     return "Big5HKSCS";
114
115   /* I don't know of any implementation of nl_langinfo(CODESET) out
116    * there that returns anything else (and I'm not even certain all of
117    * the above occur in the wild), but just in case, as a fallback,
118    * return the unmodified name. */
119 #ifdef TEST
120   printf("**** Unknown encoding name '%s'!\n", name);
121 #endif
122   return name;
123 }
124
125 /* For a demo, compile with
126  *
127  *   gcc -W -Wall -o norm_charmap -D TEST norm_charmap.c
128  *
129  * and then test it on all available locales with
130  *
131  *   for i in `locale -a` ; do printf "$i: " ; LC_ALL=$i ./norm_charmap ; done
132  */
133
134 #ifdef TEST
135 int main(int argc, char **argv)
136 {
137   char *s;
138   if (argc > 1)
139     s = argv[1];
140   else {
141     setlocale(LC_CTYPE, "");
142     s = nl_langinfo(CODESET);
143   }
144   printf("%s -> %s\n", s, norm_charmap(s));
145   return 0;
146 }
147 #endif