unicode.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <locale.h>
  5. #include <limits.h>
  6. #include <wchar.h>
  7. #include <time.h>
  8. #include "putty.h"
  9. #include "charset.h"
  10. #include "terminal.h"
  11. #include "misc.h"
  12. /*
  13. * Unix Unicode-handling routines.
  14. */
  15. bool is_dbcs_leadbyte(int codepage, char byte)
  16. {
  17. return false; /* we don't do DBCS */
  18. }
  19. bool BinarySink_put_mb_to_wc(
  20. BinarySink *bs, int codepage, const char *mbstr, int mblen)
  21. {
  22. if (codepage == DEFAULT_CODEPAGE) {
  23. mbstate_t state;
  24. memset(&state, 0, sizeof state);
  25. while (mblen > 0) {
  26. wchar_t wc;
  27. size_t i = mbrtowc(&wc, mbstr, (size_t)mblen, &state);
  28. if (i == (size_t)-1 || i == (size_t)-2)
  29. break;
  30. put_data(bs, &wc, sizeof(wc));
  31. mbstr += i;
  32. mblen -= i;
  33. }
  34. } else if (codepage == CS_NONE) {
  35. while (mblen > 0) {
  36. wchar_t wc = 0xD800 | (mbstr[0] & 0xFF);
  37. put_data(bs, &wc, sizeof(wc));
  38. mbstr++;
  39. mblen--;
  40. }
  41. } else {
  42. wchar_t wbuf[1024];
  43. while (mblen > 0) {
  44. int wlen = charset_to_unicode(&mbstr, &mblen, wbuf, lenof(wbuf),
  45. codepage, NULL, NULL, 0);
  46. put_data(bs, wbuf, wlen * sizeof(wchar_t));
  47. }
  48. }
  49. /* We never expect to receive invalid charset values on Unix,
  50. * because we're not dependent on an externally defined space of
  51. * OS-provided code pages */
  52. return true;
  53. }
  54. bool BinarySink_put_wc_to_mb(
  55. BinarySink *bs, int codepage, const wchar_t *wcstr, int wclen,
  56. const char *defchr)
  57. {
  58. size_t defchr_len = 0;
  59. bool defchr_len_known = false;
  60. if (codepage == DEFAULT_CODEPAGE) {
  61. char output[MB_LEN_MAX];
  62. mbstate_t state;
  63. memset(&state, 0, sizeof state);
  64. while (wclen > 0) {
  65. size_t i = wcrtomb(output, wcstr[0], &state);
  66. if (i == (size_t)-1) {
  67. if (!defchr_len_known) {
  68. defchr_len = strlen(defchr);
  69. defchr_len_known = true;
  70. }
  71. put_data(bs, defchr, defchr_len);
  72. } else {
  73. put_data(bs, output, i);
  74. }
  75. wcstr++;
  76. wclen--;
  77. }
  78. } else if (codepage == CS_NONE) {
  79. while (wclen > 0) {
  80. if (*wcstr >= 0xD800 && *wcstr < 0xD900) {
  81. put_byte(bs, *wcstr & 0xFF);
  82. } else {
  83. if (!defchr_len_known) {
  84. defchr_len = strlen(defchr);
  85. defchr_len_known = true;
  86. }
  87. put_data(bs, defchr, defchr_len);
  88. }
  89. wcstr++;
  90. wclen--;
  91. }
  92. } else {
  93. char buf[2048];
  94. defchr_len = strlen(defchr);
  95. while (wclen > 0) {
  96. int len = charset_from_unicode(
  97. &wcstr, &wclen, buf, lenof(buf), codepage,
  98. NULL, defchr, defchr_len);
  99. put_data(bs, buf, len);
  100. }
  101. }
  102. return true;
  103. }
  104. /*
  105. * Return value is true if pterm is to run in direct-to-font mode.
  106. */
  107. bool init_ucs(struct unicode_data *ucsdata, char *linecharset,
  108. bool utf8_override, int font_charset, int vtmode)
  109. {
  110. int i;
  111. bool ret = false;
  112. /*
  113. * In the platform-independent parts of the code, font_codepage
  114. * is used only for system DBCS support - which we don't
  115. * support at all. So we set this to something which will never
  116. * be used.
  117. */
  118. ucsdata->font_codepage = -1;
  119. /*
  120. * If utf8_override is set and the POSIX locale settings
  121. * dictate a UTF-8 character set, then just go straight for
  122. * UTF-8.
  123. */
  124. ucsdata->line_codepage = CS_NONE;
  125. if (utf8_override) {
  126. const char *s;
  127. if (((s = getenv("LC_ALL")) && *s) ||
  128. ((s = getenv("LC_CTYPE")) && *s) ||
  129. ((s = getenv("LANG")) && *s)) {
  130. if (strstr(s, "UTF-8"))
  131. ucsdata->line_codepage = CS_UTF8;
  132. }
  133. }
  134. /*
  135. * Failing that, line_codepage should be decoded from the
  136. * specification in conf.
  137. */
  138. if (ucsdata->line_codepage == CS_NONE)
  139. ucsdata->line_codepage = decode_codepage(linecharset);
  140. /*
  141. * If line_codepage is _still_ CS_NONE, we assume we're using
  142. * the font's own encoding. This has been passed in to us, so
  143. * we use that. If it's still CS_NONE after _that_ - i.e. the
  144. * font we were given had an incomprehensible charset - then we
  145. * fall back to using the D800 page.
  146. */
  147. if (ucsdata->line_codepage == CS_NONE)
  148. ucsdata->line_codepage = font_charset;
  149. if (ucsdata->line_codepage == CS_NONE)
  150. ret = true;
  151. /*
  152. * Set up unitab_line, by translating each individual character
  153. * in the line codepage into Unicode.
  154. */
  155. for (i = 0; i < 256; i++) {
  156. char c[1];
  157. const char *p;
  158. wchar_t wc[1];
  159. int len;
  160. c[0] = i;
  161. p = c;
  162. len = 1;
  163. if (ucsdata->line_codepage == CS_NONE)
  164. ucsdata->unitab_line[i] = 0xD800 | i;
  165. else if (1 == charset_to_unicode(&p, &len, wc, 1,
  166. ucsdata->line_codepage,
  167. NULL, L"", 0))
  168. ucsdata->unitab_line[i] = wc[0];
  169. else
  170. ucsdata->unitab_line[i] = 0xFFFD;
  171. }
  172. /*
  173. * Set up unitab_xterm. This is the same as unitab_line except
  174. * in the line-drawing regions, where it follows the Unicode
  175. * encoding.
  176. *
  177. * (Note that the strange X encoding of line-drawing characters
  178. * in the bottom 32 glyphs of ISO8859-1 fonts is taken care of
  179. * by the font encoding, which will spot such a font and act as
  180. * if it were in a variant encoding of ISO8859-1.)
  181. */
  182. for (i = 0; i < 256; i++) {
  183. static const wchar_t unitab_xterm_std[32] = {
  184. 0x2666, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1,
  185. 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba,
  186. 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c,
  187. 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, 0x0020
  188. };
  189. static const wchar_t unitab_xterm_poorman[32] =
  190. L"*#****o~**+++++-----++++|****L. ";
  191. const wchar_t *ptr;
  192. if (vtmode == VT_POORMAN)
  193. ptr = unitab_xterm_poorman;
  194. else
  195. ptr = unitab_xterm_std;
  196. if (i >= 0x5F && i < 0x7F)
  197. ucsdata->unitab_xterm[i] = ptr[i & 0x1F];
  198. else
  199. ucsdata->unitab_xterm[i] = ucsdata->unitab_line[i];
  200. }
  201. /*
  202. * Set up unitab_scoacs. The SCO Alternate Character Set is
  203. * simply CP437.
  204. */
  205. for (i = 0; i < 256; i++) {
  206. char c[1];
  207. const char *p;
  208. wchar_t wc[1];
  209. int len;
  210. c[0] = i;
  211. p = c;
  212. len = 1;
  213. if (1 == charset_to_unicode(&p, &len, wc, 1, CS_CP437, NULL, L"", 0))
  214. ucsdata->unitab_scoacs[i] = wc[0];
  215. else
  216. ucsdata->unitab_scoacs[i] = 0xFFFD;
  217. }
  218. /*
  219. * Find the control characters in the line codepage. For
  220. * direct-to-font mode using the D800 hack, we assume 00-1F and
  221. * 7F are controls, but allow 80-9F through. (It's as good a
  222. * guess as anything; and my bet is that half the weird fonts
  223. * used in this way will be IBM or MS code pages anyway.)
  224. */
  225. for (i = 0; i < 256; i++) {
  226. int lineval = ucsdata->unitab_line[i];
  227. if (lineval < ' ' || (lineval >= 0x7F && lineval < 0xA0) ||
  228. (lineval >= 0xD800 && lineval < 0xD820) || (lineval == 0xD87F))
  229. ucsdata->unitab_ctrl[i] = i;
  230. else
  231. ucsdata->unitab_ctrl[i] = 0xFF;
  232. }
  233. return ret;
  234. }
  235. void init_ucs_generic(Conf *conf, struct unicode_data *ucsdata)
  236. {
  237. init_ucs(ucsdata, conf_get_str(conf, CONF_line_codepage),
  238. conf_get_bool(conf, CONF_utf8_override),
  239. CS_NONE, conf_get_int(conf, CONF_vtmode));
  240. }
  241. const char *cp_name(int codepage)
  242. {
  243. if (codepage == CS_NONE)
  244. return "Use font encoding";
  245. return charset_to_localenc(codepage);
  246. }
  247. const char *cp_enumerate(int index)
  248. {
  249. int charset;
  250. charset = charset_localenc_nth(index);
  251. if (charset == CS_NONE) {
  252. /* "Use font encoding" comes after all the named charsets */
  253. if (charset_localenc_nth(index-1) != CS_NONE)
  254. return "Use font encoding";
  255. return NULL;
  256. }
  257. return charset_to_localenc(charset);
  258. }
  259. int decode_codepage(const char *cp_name)
  260. {
  261. if (!cp_name || !*cp_name)
  262. return CS_UTF8;
  263. return charset_from_localenc(cp_name);
  264. }