X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/684716b86dc07a97abbde4a8e2fa3a21b2b9c2d5..052d7bfb0a9a541c6975c3ed679dd3ac9e5c0886:/sbr/check_charset.c diff --git a/sbr/check_charset.c b/sbr/check_charset.c index b10cde34..71129fef 100644 --- a/sbr/check_charset.c +++ b/sbr/check_charset.c @@ -1,6 +1,86 @@ -/* norm_charmap.c -- map locale's encoding to MIME charset. +/* check_charset.c -- routines for character sets * - * The Single Unix Specification function nl_langinfo(CODESET) + * This code is Copyright (c) 2002, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. + */ + +#include + +#include +#include + +static const char *norm_charmap(char *name); + +/* + * Get the current character set + */ +char * +get_charset(void) +{ + return (char *)norm_charmap(nl_langinfo(CODESET)); +} + + +/* + * Check if we can display a given character set natively. + * We are passed the length of the initial part of the + * string to check, since we want to allow the name of the + * character set to be a substring of a larger string. + */ + +int +check_charset (char *str, int len) +{ + static char *mm_charset = NULL; + static char *alt_charset = NULL; + static int mm_len; + static int alt_len; + + /* Cache the name of our default character set */ + if (!mm_charset) { + if (!(mm_charset = get_charset ())) + mm_charset = "US-ASCII"; + mm_len = strlen (mm_charset); + + /* US-ASCII is a subset of the ISO-8859-X and UTF-8 character sets */ + if (!strncasecmp("ISO-8859-", mm_charset, 9) || + !strcasecmp("UTF-8", mm_charset)) { + alt_charset = "US-ASCII"; + alt_len = strlen (alt_charset); + } + } + + /* Check if character set is OK */ + if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len)) + return 1; + if (alt_charset && (len == alt_len) && !strncasecmp(str, alt_charset, alt_len)) + return 1; + + return 0; +} + + +/* + * Return the name of the character set we are + * using for 8bit text. + */ +char * +write_charset_8bit (void) +{ + static char *mm_charset = NULL; + + /* + * Cache the name of the character set to + * use for 8bit text. + */ + if (!mm_charset && !(mm_charset = get_charset ())) + mm_charset = "x-unknown"; + + return mm_charset; +} + +/* The Single Unix Specification function nl_langinfo(CODESET) * returns the name of the encoding used by the currently selected * locale: * @@ -25,89 +105,80 @@ * http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c */ -#include +static const char *norm_charmap(char *name) +{ + static const char *correct[] = { + "UTF-8", + "US-ASCII", + NULL + }, **cor; + static struct { + const char *alias; + const char *name; + } *ali, aliases[] = { + /* Names for US-ASCII. */ + { "ANSI_X3.4-1968", "US-ASCII" }, /* LC_ALL=C. */ + { "ASCII", "US-ASCII" }, + { "646", "US-ASCII" }, + { "ISO646", "US-ASCII" }, + { "ISO_646.IRV", "US-ASCII" }, + /* Case differs. */ + { "BIG5", "Big5" }, + { "BIG5HKSCS", "Big5HKSCS" }, + /* Names for ISO-8859-11. */ + { "TIS-620", "ISO-8859-11" }, + { "TIS620.2533", "ISO-8859-11" }, + { NULL, NULL } + }; + static struct { + const char *substr; + const char *name; + } *sub, substrs[] = { + { "8859-1", "ISO-8859-1" }, + { "8859-2", "ISO-8859-2" }, + { "8859-3", "ISO-8859-3" }, + { "8859-4", "ISO-8859-4" }, + { "8859-5", "ISO-8859-5" }, + { "8859-6", "ISO-8859-6" }, + { "8859-7", "ISO-8859-7" }, + { "8859-8", "ISO-8859-8" }, + { "8859-9", "ISO-8859-9" }, + { "8859-10", "ISO-8859-10" }, + { "8859-11", "ISO-8859-11" }, + /* 12, Latin/Devanagari, not completed. */ + { "8859-13", "ISO-8859-13" }, + { "8859-14", "ISO-8859-14" }, + { "8859-15", "ISO-8859-15" }, + { "8859-16", "ISO-8859-16" }, + { "CP1200", "WINDOWS-1200" }, + { "CP1201", "WINDOWS-1201" }, + { "CP1250", "WINDOWS-1250" }, + { "CP1251", "WINDOWS-1251" }, + { "CP1252", "WINDOWS-1252" }, + { "CP1253", "WINDOWS-1253" }, + { "CP1254", "WINDOWS-1254" }, + { "CP1255", "WINDOWS-1255" }, + { "CP1256", "WINDOWS-1256" }, + { "CP1257", "WINDOWS-1257" }, + { "CP1258", "WINDOWS-1258" }, + { NULL, NULL } + }; -#define digit(x) ((x) >= '0' && (x) <= '9') + if (!name) + return name; -static char buf[16]; + /* Avoid lots of tests for common correct names. */ + for (cor = correct; *cor; cor++) + if (!strcmp(name, *cor)) + return name; + + for (ali = aliases; ali->alias; ali++) + if (!strcmp(name, ali->alias)) + return ali->name; + + for (sub = substrs; sub->substr; sub++) + if (strstr(name, sub->substr)) + return sub->name; -char * -norm_charmap(char *name) -{ - char *p; - - if (!name) return name; - - /* Many need no remapping, but they are listed here so you - * can see what output to expect, and modify for your needs - * as necessary. */ - if (!strcmp(name, "UTF-8")) - return "UTF-8"; - if (!strcmp(name, "EUC-JP")) - return "EUC-JP"; - if (!strcmp(name, "EUC-KR")) - return "EUC-KR"; - if (!strcmp(name, "EUC-TW")) - return "EUC-TW"; - if (!strcmp(name, "KOI8-R")) - return "KOI8-R"; - if (!strcmp(name, "KOI8-U")) - return "KOI8-U"; - if (!strcmp(name, "GBK")) - return "GBK"; - if (!strcmp(name, "GB2312")) - return "GB2312"; - if (!strcmp(name, "GB18030")) - return "GB18030"; - if (!strcmp(name, "VSCII")) - return "VSCII"; - - /* ASCII comes in many names */ - if (!strcmp(name, "ASCII") || - !strcmp(name, "US-ASCII") || - !strcmp(name, "ANSI_X3.4-1968") || - !strcmp(name, "646") || - !strcmp(name, "ISO646") || - !strcmp(name, "ISO_646.IRV")) - return "US-ASCII"; - - /* ISO 8859 will be converted to "ISO-8859-x" */ - if ((p = strstr(name, "8859-"))) { - memcpy(buf, "ISO-8859-\0\0", 12); - p += 5; - if (digit(*p)) { - buf[9] = *p++; - if (digit(*p)) buf[10] = *p++; - return buf; - } - } - - /* Windows code pages will be converted to "WINDOWS-12xx" */ - if ((p = strstr(name, "CP12"))) { - memcpy(buf, "WINDOWS-12\0\0", 13); - p += 4; - if (digit(*p)) { - buf[10] = *p++; - if (digit(*p)) buf[11] = *p++; - return buf; - } - } - - /* TIS-620 comes in at least the following two forms */ - if (!strcmp(name, "TIS-620") || - !strcmp(name, "TIS620.2533")) - return "ISO-8859-11"; - - /* For some, uppercase/lowercase might differ */ - if (!strcmp(name, "Big5") || !strcmp(name, "BIG5")) - return "Big5"; - if (!strcmp(name, "Big5HKSCS") || !strcmp(name, "BIG5HKSCS")) - return "Big5HKSCS"; - - /* I don't know of any implementation of nl_langinfo(CODESET) out - * there that returns anything else (and I'm not even certain all of - * the above occur in the wild), but just in case, as a fallback, - * return the unmodified name. */ - return name; }