]> diplodocus.org Git - nmh/blob - sbr/norm_charmap.c
uip/mhcachesbr.c: Move exported symbols to existing h/mhcachesbr.h.
[nmh] / sbr / norm_charmap.c
1 /* norm_charmap.c -- map locale's encoding to MIME charset.
2 *
3 * The Single Unix Specification function nl_langinfo(CODESET)
4 * returns the name of the encoding used by the currently selected
5 * locale:
6 *
7 * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
8 *
9 * Unfortunately the encoding names are not yet standardized.
10 * This function knows about the encoding names used on many
11 * different systems and converts them where possible into
12 * the corresponding MIME charset name registered in
13 *
14 * http://www.iana.org/assignments/character-sets
15 *
16 * Please extend it as needed and suggest improvements to the author.
17 *
18 * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11
19 * Permission to use, copy, modify, and distribute this software
20 * for any purpose and without fee is hereby granted. The author
21 * disclaims all warranties with regard to this software.
22 *
23 * Latest version:
24 *
25 * http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
26 */
27
28 #include <string.h>
29
30 #define digit(x) ((x) >= '0' && (x) <= '9')
31
32 static char buf[16];
33
34 char *
35 norm_charmap(char *name)
36 {
37 char *p;
38
39 if (!name)
40 return name;
41
42 /* Many need no remapping, but they are listed here so you
43 * can see what output to expect, and modify for your needs
44 * as necessary. */
45 if (!strcmp(name, "UTF-8"))
46 return "UTF-8";
47 if (!strcmp(name, "EUC-JP"))
48 return "EUC-JP";
49 if (!strcmp(name, "EUC-KR"))
50 return "EUC-KR";
51 if (!strcmp(name, "EUC-TW"))
52 return "EUC-TW";
53 if (!strcmp(name, "KOI8-R"))
54 return "KOI8-R";
55 if (!strcmp(name, "KOI8-U"))
56 return "KOI8-U";
57 if (!strcmp(name, "GBK"))
58 return "GBK";
59 if (!strcmp(name, "GB2312"))
60 return "GB2312";
61 if (!strcmp(name, "GB18030"))
62 return "GB18030";
63 if (!strcmp(name, "VSCII"))
64 return "VSCII";
65
66 /* ASCII comes in many names */
67 if (!strcmp(name, "ASCII") ||
68 !strcmp(name, "US-ASCII") ||
69 !strcmp(name, "ANSI_X3.4-1968") ||
70 !strcmp(name, "646") ||
71 !strcmp(name, "ISO646") ||
72 !strcmp(name, "ISO_646.IRV"))
73 return "US-ASCII";
74
75 /* ISO 8859 will be converted to "ISO-8859-x" */
76 if ((p = strstr(name, "8859-"))) {
77 memcpy(buf, "ISO-8859-\0\0", 12);
78 p += 5;
79 if (digit(*p)) {
80 buf[9] = *p++;
81 if (digit(*p)) buf[10] = *p++;
82 return buf;
83 }
84 }
85
86 /* Windows code pages will be converted to "WINDOWS-12xx" */
87 if ((p = strstr(name, "CP12"))) {
88 memcpy(buf, "WINDOWS-12\0\0", 13);
89 p += 4;
90 if (digit(*p)) {
91 buf[10] = *p++;
92 if (digit(*p)) buf[11] = *p++;
93 return buf;
94 }
95 }
96
97 /* TIS-620 comes in at least the following two forms */
98 if (!strcmp(name, "TIS-620") ||
99 !strcmp(name, "TIS620.2533"))
100 return "ISO-8859-11";
101
102 /* For some, uppercase/lowercase might differ */
103 if (!strcmp(name, "Big5") || !strcmp(name, "BIG5"))
104 return "Big5";
105 if (!strcmp(name, "Big5HKSCS") || !strcmp(name, "BIG5HKSCS"))
106 return "Big5HKSCS";
107
108 /* I don't know of any implementation of nl_langinfo(CODESET) out
109 * there that returns anything else (and I'm not even certain all of
110 * the above occur in the wild), but just in case, as a fallback,
111 * return the unmodified name. */
112 return name;
113 }