unicode.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <time.h>
  5. #include <assert.h>
  6. #include "putty.h"
  7. #include "terminal.h"
  8. #include "misc.h"
  9. /* Character conversion arrays; they are usually taken from windows,
  10. * the xterm one has the four scanlines that have no unicode 2.0
  11. * equivalents mapped to their unicode 3.0 locations.
  12. */
  13. static const WCHAR unitab_xterm_std[32] = {
  14. 0x2666, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1,
  15. 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba,
  16. 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c,
  17. 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, 0x0020
  18. };
  19. /*
  20. * If the codepage is non-zero it's a window codepage, zero means use a
  21. * local codepage. The name is always converted to the first of any
  22. * duplicate definitions.
  23. */
  24. /*
  25. * Tables for ISO-8859-{1-10,13-16} derived from those downloaded
  26. * 2001-10-02 from <http://www.unicode.org/Public/MAPPINGS/> -- jtn
  27. * Table for ISO-8859-11 derived from same on 2002-11-18. -- bjh21
  28. */
  29. /* XXX: This could be done algorithmically, but I'm not sure it's
  30. * worth the hassle -- jtn */
  31. /* ISO/IEC 8859-1:1998 (Latin-1, "Western", "West European") */
  32. static const wchar_t iso_8859_1[] = {
  33. 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
  34. 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  35. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
  36. 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  37. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  38. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  39. 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  40. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  41. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  42. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  43. 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  44. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF
  45. };
  46. /* ISO/IEC 8859-2:1999 (Latin-2, "Central European", "East European") */
  47. static const wchar_t iso_8859_2[] = {
  48. 0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7,
  49. 0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B,
  50. 0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7,
  51. 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C,
  52. 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
  53. 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
  54. 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,
  55. 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
  56. 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7,
  57. 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
  58. 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
  59. 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9
  60. };
  61. /* ISO/IEC 8859-3:1999 (Latin-3, "South European", "Maltese & Esperanto") */
  62. static const wchar_t iso_8859_3[] = {
  63. 0x00A0, 0x0126, 0x02D8, 0x00A3, 0x00A4, 0xFFFD, 0x0124, 0x00A7,
  64. 0x00A8, 0x0130, 0x015E, 0x011E, 0x0134, 0x00AD, 0xFFFD, 0x017B,
  65. 0x00B0, 0x0127, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x0125, 0x00B7,
  66. 0x00B8, 0x0131, 0x015F, 0x011F, 0x0135, 0x00BD, 0xFFFD, 0x017C,
  67. 0x00C0, 0x00C1, 0x00C2, 0xFFFD, 0x00C4, 0x010A, 0x0108, 0x00C7,
  68. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  69. 0xFFFD, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x0120, 0x00D6, 0x00D7,
  70. 0x011C, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x016C, 0x015C, 0x00DF,
  71. 0x00E0, 0x00E1, 0x00E2, 0xFFFD, 0x00E4, 0x010B, 0x0109, 0x00E7,
  72. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  73. 0xFFFD, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6, 0x00F7,
  74. 0x011D, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D, 0x02D9
  75. };
  76. /* ISO/IEC 8859-4:1998 (Latin-4, "North European") */
  77. static const wchar_t iso_8859_4[] = {
  78. 0x00A0, 0x0104, 0x0138, 0x0156, 0x00A4, 0x0128, 0x013B, 0x00A7,
  79. 0x00A8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00AD, 0x017D, 0x00AF,
  80. 0x00B0, 0x0105, 0x02DB, 0x0157, 0x00B4, 0x0129, 0x013C, 0x02C7,
  81. 0x00B8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014A, 0x017E, 0x014B,
  82. 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E,
  83. 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x012A,
  84. 0x0110, 0x0145, 0x014C, 0x0136, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  85. 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x0168, 0x016A, 0x00DF,
  86. 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F,
  87. 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x012B,
  88. 0x0111, 0x0146, 0x014D, 0x0137, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  89. 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x0169, 0x016B, 0x02D9
  90. };
  91. /* ISO/IEC 8859-5:1999 (Latin/Cyrillic) */
  92. static const wchar_t iso_8859_5[] = {
  93. 0x00A0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407,
  94. 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x00AD, 0x040E, 0x040F,
  95. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
  96. 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  97. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
  98. 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  99. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
  100. 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  101. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
  102. 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  103. 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457,
  104. 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x00A7, 0x045E, 0x045F
  105. };
  106. /* ISO/IEC 8859-6:1999 (Latin/Arabic) */
  107. static const wchar_t iso_8859_6[] = {
  108. 0x00A0, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A4, 0xFFFD, 0xFFFD, 0xFFFD,
  109. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x060C, 0x00AD, 0xFFFD, 0xFFFD,
  110. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  111. 0xFFFD, 0xFFFD, 0xFFFD, 0x061B, 0xFFFD, 0xFFFD, 0xFFFD, 0x061F,
  112. 0xFFFD, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
  113. 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,
  114. 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637,
  115. 0x0638, 0x0639, 0x063A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  116. 0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647,
  117. 0x0648, 0x0649, 0x064A, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F,
  118. 0x0650, 0x0651, 0x0652, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  119. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD
  120. };
  121. /* ISO 8859-7:1987 (Latin/Greek) */
  122. static const wchar_t iso_8859_7[] = {
  123. 0x00A0, 0x2018, 0x2019, 0x00A3, 0xFFFD, 0xFFFD, 0x00A6, 0x00A7,
  124. 0x00A8, 0x00A9, 0xFFFD, 0x00AB, 0x00AC, 0x00AD, 0xFFFD, 0x2015,
  125. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x0385, 0x0386, 0x00B7,
  126. 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F,
  127. 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,
  128. 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F,
  129. 0x03A0, 0x03A1, 0xFFFD, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7,
  130. 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF,
  131. 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,
  132. 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
  133. 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
  134. 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0xFFFD
  135. };
  136. /* ISO/IEC 8859-8:1999 (Latin/Hebrew) */
  137. static const wchar_t iso_8859_8[] = {
  138. 0x00A0, 0xFFFD, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
  139. 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  140. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
  141. 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0xFFFD,
  142. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  143. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  144. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  145. 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x2017,
  146. 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,
  147. 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
  148. 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
  149. 0x05E8, 0x05E9, 0x05EA, 0xFFFD, 0xFFFD, 0x200E, 0x200F, 0xFFFD
  150. };
  151. /* ISO/IEC 8859-9:1999 (Latin-5, "Turkish") */
  152. static const wchar_t iso_8859_9[] = {
  153. 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
  154. 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  155. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
  156. 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  157. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  158. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  159. 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  160. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF,
  161. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  162. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  163. 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  164. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF
  165. };
  166. /* ISO/IEC 8859-10:1998 (Latin-6, "Nordic" [Sami, Inuit, Icelandic]) */
  167. static const wchar_t iso_8859_10[] = {
  168. 0x00A0, 0x0104, 0x0112, 0x0122, 0x012A, 0x0128, 0x0136, 0x00A7,
  169. 0x013B, 0x0110, 0x0160, 0x0166, 0x017D, 0x00AD, 0x016A, 0x014A,
  170. 0x00B0, 0x0105, 0x0113, 0x0123, 0x012B, 0x0129, 0x0137, 0x00B7,
  171. 0x013C, 0x0111, 0x0161, 0x0167, 0x017E, 0x2015, 0x016B, 0x014B,
  172. 0x0100, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x012E,
  173. 0x010C, 0x00C9, 0x0118, 0x00CB, 0x0116, 0x00CD, 0x00CE, 0x00CF,
  174. 0x00D0, 0x0145, 0x014C, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x0168,
  175. 0x00D8, 0x0172, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  176. 0x0101, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x012F,
  177. 0x010D, 0x00E9, 0x0119, 0x00EB, 0x0117, 0x00ED, 0x00EE, 0x00EF,
  178. 0x00F0, 0x0146, 0x014D, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x0169,
  179. 0x00F8, 0x0173, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x0138
  180. };
  181. /* ISO/IEC 8859-11:2001 ("Thai", "TIS620") */
  182. static const wchar_t iso_8859_11[] = {
  183. 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07,
  184. 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F,
  185. 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17,
  186. 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, 0x0E1D, 0x0E1E, 0x0E1F,
  187. 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, 0x0E26, 0x0E27,
  188. 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, 0x0E2F,
  189. 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37,
  190. 0x0E38, 0x0E39, 0x0E3A, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x0E3F,
  191. 0x0E40, 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47,
  192. 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F,
  193. 0x0E50, 0x0E51, 0x0E52, 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57,
  194. 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD
  195. };
  196. /* ISO/IEC 8859-13:1998 (Latin-7, "Baltic Rim") */
  197. static const wchar_t iso_8859_13[] = {
  198. 0x00A0, 0x201D, 0x00A2, 0x00A3, 0x00A4, 0x201E, 0x00A6, 0x00A7,
  199. 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6,
  200. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x201C, 0x00B5, 0x00B6, 0x00B7,
  201. 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6,
  202. 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112,
  203. 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B,
  204. 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7,
  205. 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF,
  206. 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113,
  207. 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C,
  208. 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7,
  209. 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x2019
  210. };
  211. /* ISO/IEC 8859-14:1998 (Latin-8, "Celtic", "Gaelic/Welsh") */
  212. static const wchar_t iso_8859_14[] = {
  213. 0x00A0, 0x1E02, 0x1E03, 0x00A3, 0x010A, 0x010B, 0x1E0A, 0x00A7,
  214. 0x1E80, 0x00A9, 0x1E82, 0x1E0B, 0x1EF2, 0x00AD, 0x00AE, 0x0178,
  215. 0x1E1E, 0x1E1F, 0x0120, 0x0121, 0x1E40, 0x1E41, 0x00B6, 0x1E56,
  216. 0x1E81, 0x1E57, 0x1E83, 0x1E60, 0x1EF3, 0x1E84, 0x1E85, 0x1E61,
  217. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  218. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  219. 0x0174, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x1E6A,
  220. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x0176, 0x00DF,
  221. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  222. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  223. 0x0175, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x1E6B,
  224. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x0177, 0x00FF
  225. };
  226. /* ISO/IEC 8859-15:1999 (Latin-9 aka -0, "euro") */
  227. static const wchar_t iso_8859_15[] = {
  228. 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AC, 0x00A5, 0x0160, 0x00A7,
  229. 0x0161, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  230. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x017D, 0x00B5, 0x00B6, 0x00B7,
  231. 0x017E, 0x00B9, 0x00BA, 0x00BB, 0x0152, 0x0153, 0x0178, 0x00BF,
  232. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  233. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  234. 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  235. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  236. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  237. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  238. 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  239. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF
  240. };
  241. /* ISO/IEC 8859-16:2001 (Latin-10, "Balkan") */
  242. static const wchar_t iso_8859_16[] = {
  243. 0x00A0, 0x0104, 0x0105, 0x0141, 0x20AC, 0x201E, 0x0160, 0x00A7,
  244. 0x0161, 0x00A9, 0x0218, 0x00AB, 0x0179, 0x00AD, 0x017A, 0x017B,
  245. 0x00B0, 0x00B1, 0x010C, 0x0142, 0x017D, 0x201D, 0x00B6, 0x00B7,
  246. 0x017E, 0x010D, 0x0219, 0x00BB, 0x0152, 0x0153, 0x0178, 0x017C,
  247. 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0106, 0x00C6, 0x00C7,
  248. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  249. 0x0110, 0x0143, 0x00D2, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x015A,
  250. 0x0170, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0118, 0x021A, 0x00DF,
  251. 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x0107, 0x00E6, 0x00E7,
  252. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  253. 0x0111, 0x0144, 0x00F2, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x015B,
  254. 0x0171, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0119, 0x021B, 0x00FF
  255. };
  256. static const wchar_t roman8[] = {
  257. 0x00A0, 0x00C0, 0x00C2, 0x00C8, 0x00CA, 0x00CB, 0x00CE, 0x00CF,
  258. 0x00B4, 0x02CB, 0x02C6, 0x00A8, 0x02DC, 0x00D9, 0x00DB, 0x20A4,
  259. 0x00AF, 0x00DD, 0x00FD, 0x00B0, 0x00C7, 0x00E7, 0x00D1, 0x00F1,
  260. 0x00A1, 0x00BF, 0x00A4, 0x00A3, 0x00A5, 0x00A7, 0x0192, 0x00A2,
  261. 0x00E2, 0x00EA, 0x00F4, 0x00FB, 0x00E1, 0x00E9, 0x00F3, 0x00FA,
  262. 0x00E0, 0x00E8, 0x00F2, 0x00F9, 0x00E4, 0x00EB, 0x00F6, 0x00FC,
  263. 0x00C5, 0x00EE, 0x00D8, 0x00C6, 0x00E5, 0x00ED, 0x00F8, 0x00E6,
  264. 0x00C4, 0x00EC, 0x00D6, 0x00DC, 0x00C9, 0x00EF, 0x00DF, 0x00D4,
  265. 0x00C1, 0x00C3, 0x00E3, 0x00D0, 0x00F0, 0x00CD, 0x00CC, 0x00D3,
  266. 0x00D2, 0x00D5, 0x00F5, 0x0160, 0x0161, 0x00DA, 0x0178, 0x00FF,
  267. 0x00DE, 0x00FE, 0x00B7, 0x00B5, 0x00B6, 0x00BE, 0x2014, 0x00BC,
  268. 0x00BD, 0x00AA, 0x00BA, 0x00AB, 0x25A0, 0x00BB, 0x00B1, 0xFFFD
  269. };
  270. static const wchar_t koi8_u[] = {
  271. 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524,
  272. 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
  273. 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2022, 0x221A, 0x2248,
  274. 0x2264, 0x2265, 0x00A0, 0x2321, 0x00B0, 0x00B2, 0x00B7, 0x00F7,
  275. 0x2550, 0x2551, 0x2552, 0x0451, 0x0454, 0x2554, 0x0456, 0x0457,
  276. 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x0491, 0x255D, 0x255E,
  277. 0x255F, 0x2560, 0x2561, 0x0401, 0x0404, 0x2563, 0x0406, 0x0407,
  278. 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x0490, 0x256C, 0x00A9,
  279. 0x044E, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433,
  280. 0x0445, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E,
  281. 0x043F, 0x044F, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432,
  282. 0x044C, 0x044B, 0x0437, 0x0448, 0x044D, 0x0449, 0x0447, 0x044A,
  283. 0x042E, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413,
  284. 0x0425, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
  285. 0x041F, 0x042F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412,
  286. 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A
  287. };
  288. static const wchar_t vscii[] = {
  289. 0x0000, 0x0001, 0x1EB2, 0x0003, 0x0004, 0x1EB4, 0x1EAA, 0x0007,
  290. 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
  291. 0x0010, 0x0011, 0x0012, 0x0013, 0x1EF6, 0x0015, 0x0016, 0x0017,
  292. 0x0018, 0x1EF8, 0x001a, 0x001b, 0x001c, 0x001d, 0x1EF4, 0x001f,
  293. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
  294. 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
  295. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
  296. 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
  297. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
  298. 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
  299. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
  300. 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
  301. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
  302. 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
  303. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
  304. 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007f,
  305. 0x1EA0, 0x1EAE, 0x1EB0, 0x1EB6, 0x1EA4, 0x1EA6, 0x1EA8, 0x1EAC,
  306. 0x1EBC, 0x1EB8, 0x1EBE, 0x1EC0, 0x1EC2, 0x1EC4, 0x1EC6, 0x1ED0,
  307. 0x1ED2, 0x1ED4, 0x1ED6, 0x1ED8, 0x1EE2, 0x1EDA, 0x1EDC, 0x1EDE,
  308. 0x1ECA, 0x1ECE, 0x1ECC, 0x1EC8, 0x1EE6, 0x0168, 0x1EE4, 0x1EF2,
  309. 0x00D5, 0x1EAF, 0x1EB1, 0x1EB7, 0x1EA5, 0x1EA7, 0x1EA8, 0x1EAD,
  310. 0x1EBD, 0x1EB9, 0x1EBF, 0x1EC1, 0x1EC3, 0x1EC5, 0x1EC7, 0x1ED1,
  311. 0x1ED3, 0x1ED5, 0x1ED7, 0x1EE0, 0x01A0, 0x1ED9, 0x1EDD, 0x1EDF,
  312. 0x1ECB, 0x1EF0, 0x1EE8, 0x1EEA, 0x1EEC, 0x01A1, 0x1EDB, 0x01AF,
  313. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x1EA2, 0x0102, 0x1EB3, 0x1EB5,
  314. 0x00C8, 0x00C9, 0x00CA, 0x1EBA, 0x00CC, 0x00CD, 0x0128, 0x1EF3,
  315. 0x0110, 0x1EE9, 0x00D2, 0x00D3, 0x00D4, 0x1EA1, 0x1EF7, 0x1EEB,
  316. 0x1EED, 0x00D9, 0x00DA, 0x1EF9, 0x1EF5, 0x00DD, 0x1EE1, 0x01B0,
  317. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x1EA3, 0x0103, 0x1EEF, 0x1EAB,
  318. 0x00E8, 0x00E9, 0x00EA, 0x1EBB, 0x00EC, 0x00ED, 0x0129, 0x1EC9,
  319. 0x0111, 0x1EF1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x1ECF, 0x1ECD,
  320. 0x1EE5, 0x00F9, 0x00FA, 0x0169, 0x1EE7, 0x00FD, 0x1EE3, 0x1EEE
  321. };
  322. static const wchar_t dec_mcs[] = {
  323. 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0xFFFD, 0x00A5, 0xFFFD, 0x00A7,
  324. 0x00A4, 0x00A9, 0x00AA, 0x00AB, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD,
  325. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0xFFFD, 0x00B5, 0x00B6, 0x00B7,
  326. 0xFFFD, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0xFFFD, 0x00BF,
  327. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  328. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  329. 0xFFFD, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x0152,
  330. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0178, 0xFFFD, 0x00DF,
  331. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  332. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  333. 0xFFFD, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x0153,
  334. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FF, 0xFFFD, 0xFFFD
  335. };
  336. /* Mazovia (Polish) aka CP620
  337. * from "Mazowia to Unicode table", 04/24/96, Mikolaj Jedrzejak */
  338. static const wchar_t mazovia[] = {
  339. /* Code point 0x9B is "zloty" symbol (z&#0142;), which is not
  340. * widely used and for which there is no Unicode equivalent.
  341. * One reference shows 0xA8 as U+00A7 SECTION SIGN, but we're
  342. * told that's incorrect. */
  343. 0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x0105, 0x00E7,
  344. 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0107, 0x00C4, 0x0104,
  345. 0x0118, 0x0119, 0x0142, 0x00F4, 0x00F6, 0x0106, 0x00FB, 0x00F9,
  346. 0x015a, 0x00D6, 0x00DC, 0xFFFD, 0x0141, 0x00A5, 0x015b, 0x0192,
  347. 0x0179, 0x017b, 0x00F3, 0x00d3, 0x0144, 0x0143, 0x017a, 0x017c,
  348. 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
  349. 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
  350. 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
  351. 0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
  352. 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
  353. 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,
  354. 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
  355. 0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,
  356. 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
  357. 0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,
  358. 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
  359. };
  360. struct cp_list_item {
  361. char *name;
  362. int codepage;
  363. int cp_size;
  364. const wchar_t *cp_table;
  365. };
  366. static const struct cp_list_item cp_list[] = {
  367. {"UTF-8", CP_UTF8},
  368. {"ISO-8859-1:1998 (Latin-1, West Europe)", 0, 96, iso_8859_1},
  369. {"ISO-8859-2:1999 (Latin-2, East Europe)", 0, 96, iso_8859_2},
  370. {"ISO-8859-3:1999 (Latin-3, South Europe)", 0, 96, iso_8859_3},
  371. {"ISO-8859-4:1998 (Latin-4, North Europe)", 0, 96, iso_8859_4},
  372. {"ISO-8859-5:1999 (Latin/Cyrillic)", 0, 96, iso_8859_5},
  373. {"ISO-8859-6:1999 (Latin/Arabic)", 0, 96, iso_8859_6},
  374. {"ISO-8859-7:1987 (Latin/Greek)", 0, 96, iso_8859_7},
  375. {"ISO-8859-8:1999 (Latin/Hebrew)", 0, 96, iso_8859_8},
  376. {"ISO-8859-9:1999 (Latin-5, Turkish)", 0, 96, iso_8859_9},
  377. {"ISO-8859-10:1998 (Latin-6, Nordic)", 0, 96, iso_8859_10},
  378. {"ISO-8859-11:2001 (Latin/Thai)", 0, 96, iso_8859_11},
  379. {"ISO-8859-13:1998 (Latin-7, Baltic)", 0, 96, iso_8859_13},
  380. {"ISO-8859-14:1998 (Latin-8, Celtic)", 0, 96, iso_8859_14},
  381. {"ISO-8859-15:1999 (Latin-9, \"euro\")", 0, 96, iso_8859_15},
  382. {"ISO-8859-16:2001 (Latin-10, Balkan)", 0, 96, iso_8859_16},
  383. {"KOI8-U", 0, 128, koi8_u},
  384. {"KOI8-R", 20866},
  385. {"HP-ROMAN8", 0, 96, roman8},
  386. {"VSCII", 0, 256, vscii},
  387. {"DEC-MCS", 0, 96, dec_mcs},
  388. {"Win1250 (Central European)", 1250},
  389. {"Win1251 (Cyrillic)", 1251},
  390. {"Win1252 (Western)", 1252},
  391. {"Win1253 (Greek)", 1253},
  392. {"Win1254 (Turkish)", 1254},
  393. {"Win1255 (Hebrew)", 1255},
  394. {"Win1256 (Arabic)", 1256},
  395. {"Win1257 (Baltic)", 1257},
  396. {"Win1258 (Vietnamese)", 1258},
  397. {"CP437", 437},
  398. {"CP620 (Mazovia)", 0, 128, mazovia},
  399. {"CP819", 28591},
  400. {"CP852", 852},
  401. {"CP878", 20866},
  402. {"Use font encoding", -1},
  403. {0, 0}
  404. };
  405. static void link_font(WCHAR *line_tbl, WCHAR *font_tbl, WCHAR attr);
  406. /*
  407. * We keep a collection of reverse mappings from Unicode back to code pages,
  408. * in the form of array[256] of array[256] of char. These live forever in a
  409. * local tree234, and we just make a new one whenever we find a need.
  410. */
  411. typedef struct reverse_mapping {
  412. int codepage;
  413. char **blocks;
  414. } reverse_mapping;
  415. static tree234 *reverse_mappings = NULL;
  416. static int reverse_mapping_cmp(void *av, void *bv)
  417. {
  418. const reverse_mapping *a = (const reverse_mapping *)av;
  419. const reverse_mapping *b = (const reverse_mapping *)bv;
  420. if (a->codepage < b->codepage)
  421. return -1;
  422. if (a->codepage > b->codepage)
  423. return +1;
  424. return 0;
  425. }
  426. static int reverse_mapping_find(void *av, void *bv)
  427. {
  428. const reverse_mapping *a = (const reverse_mapping *)av;
  429. int b_codepage = *(const int *)bv;
  430. if (a->codepage < b_codepage)
  431. return -1;
  432. if (a->codepage > b_codepage)
  433. return +1;
  434. return 0;
  435. }
  436. static reverse_mapping *get_existing_reverse_mapping(int codepage)
  437. {
  438. if (!reverse_mappings)
  439. return NULL;
  440. return find234(reverse_mappings, &codepage, reverse_mapping_find);
  441. }
  442. static reverse_mapping *make_reverse_mapping_inner(
  443. int codepage, const wchar_t *mapping)
  444. {
  445. if (!reverse_mappings)
  446. reverse_mappings = newtree234(reverse_mapping_cmp);
  447. reverse_mapping *rmap = snew(reverse_mapping);
  448. rmap->blocks = snewn(256, char *);
  449. memset(rmap->blocks, 0, 256 * sizeof(char *));
  450. for (size_t i = 0; i < 256; i++) {
  451. /* These special kinds of value correspond to no Unicode character */
  452. if (DIRECT_CHAR(mapping[i]))
  453. continue;
  454. if (DIRECT_FONT(mapping[i]))
  455. continue;
  456. size_t chr = mapping[i];
  457. size_t block = chr >> 8, index = chr & 0xFF;
  458. if (!rmap->blocks[block]) {
  459. rmap->blocks[block] = snewn(256, char);
  460. memset(rmap->blocks[block], 0, 256);
  461. }
  462. rmap->blocks[block][index] = i;
  463. }
  464. rmap->codepage = codepage;
  465. reverse_mapping *added = add234(reverse_mappings, rmap);
  466. assert(added == rmap); /* we already checked it wasn't already in there */
  467. return added;
  468. }
  469. static void make_reverse_mapping(int codepage, const wchar_t *mapping)
  470. {
  471. if (get_existing_reverse_mapping(codepage))
  472. return; /* we've already got this one */
  473. make_reverse_mapping_inner(codepage, mapping);
  474. }
  475. static reverse_mapping *get_reverse_mapping(int codepage)
  476. {
  477. /*
  478. * Try harder to get a reverse mapping for a codepage we implement
  479. * internally via a translation table, by hastily making it if it doesn't
  480. * already exist.
  481. */
  482. reverse_mapping *rmap = get_existing_reverse_mapping(codepage);
  483. if (rmap)
  484. return rmap;
  485. if (codepage < 65536)
  486. return NULL;
  487. if (codepage >= 65536 + lenof(cp_list))
  488. return NULL;
  489. const struct cp_list_item *cp = &cp_list[codepage - 65536];
  490. if (!cp->cp_table)
  491. return NULL;
  492. wchar_t mapping[256];
  493. get_unitab(codepage, mapping, 0);
  494. return make_reverse_mapping_inner(codepage, mapping);
  495. }
  496. void init_ucs(Conf *conf, struct unicode_data *ucsdata)
  497. {
  498. int i;
  499. bool used_dtf = false;
  500. int vtmode;
  501. /* Decide on the Line and Font codepages */
  502. ucsdata->line_codepage = decode_codepage(conf_get_str(conf,
  503. CONF_line_codepage));
  504. if (ucsdata->font_codepage <= 0) {
  505. ucsdata->font_codepage=0;
  506. ucsdata->dbcs_screenfont=false;
  507. }
  508. vtmode = conf_get_int(conf, CONF_vtmode);
  509. if (vtmode == VT_OEMONLY) {
  510. ucsdata->font_codepage = 437;
  511. ucsdata->dbcs_screenfont = false;
  512. if (ucsdata->line_codepage <= 0)
  513. ucsdata->line_codepage = GetACP();
  514. } else if (ucsdata->line_codepage <= 0)
  515. ucsdata->line_codepage = ucsdata->font_codepage;
  516. /* Collect screen font ucs table */
  517. if (ucsdata->dbcs_screenfont || ucsdata->font_codepage == 0) {
  518. get_unitab(ucsdata->font_codepage, ucsdata->unitab_font, 2);
  519. for (i = 128; i < 256; i++)
  520. ucsdata->unitab_font[i] = (WCHAR) (CSET_ACP + i);
  521. } else {
  522. get_unitab(ucsdata->font_codepage, ucsdata->unitab_font, 1);
  523. /* CP437 fonts are often broken ... */
  524. if (ucsdata->font_codepage == 437)
  525. ucsdata->unitab_font[0] = ucsdata->unitab_font[255] = 0xFFFF;
  526. }
  527. if (vtmode == VT_XWINDOWS)
  528. memcpy(ucsdata->unitab_font + 1, unitab_xterm_std,
  529. sizeof(unitab_xterm_std));
  530. /* Collect OEMCP ucs table */
  531. get_unitab(CP_OEMCP, ucsdata->unitab_oemcp, 1);
  532. /* Collect CP437 ucs table for SCO acs */
  533. if (vtmode == VT_OEMANSI || vtmode == VT_XWINDOWS)
  534. memcpy(ucsdata->unitab_scoacs, ucsdata->unitab_oemcp,
  535. sizeof(ucsdata->unitab_scoacs));
  536. else
  537. get_unitab(437, ucsdata->unitab_scoacs, 1);
  538. /* Collect line set ucs table */
  539. if (ucsdata->line_codepage == ucsdata->font_codepage &&
  540. (ucsdata->dbcs_screenfont ||
  541. vtmode == VT_POORMAN || ucsdata->font_codepage==0)) {
  542. /* For DBCS and POOR fonts force direct to font */
  543. used_dtf = true;
  544. for (i = 0; i < 32; i++)
  545. ucsdata->unitab_line[i] = (WCHAR) i;
  546. for (i = 32; i < 256; i++)
  547. ucsdata->unitab_line[i] = (WCHAR) (CSET_ACP + i);
  548. ucsdata->unitab_line[127] = (WCHAR) 127;
  549. } else {
  550. get_unitab(ucsdata->line_codepage, ucsdata->unitab_line, 0);
  551. }
  552. #if 0
  553. debug("Line cp%d, Font cp%d%s\n", ucsdata->line_codepage,
  554. ucsdata->font_codepage, ucsdata->dbcs_screenfont ? " DBCS" : "");
  555. for (i = 0; i < 256; i += 16) {
  556. for (j = 0; j < 16; j++) {
  557. debug("%04x%s", ucsdata->unitab_line[i + j], j == 15 ? "" : ",");
  558. }
  559. debug("\n");
  560. }
  561. #endif
  562. /* VT100 graphics - NB: Broken for non-ascii CP's */
  563. memcpy(ucsdata->unitab_xterm, ucsdata->unitab_line,
  564. sizeof(ucsdata->unitab_xterm));
  565. memcpy(ucsdata->unitab_xterm + '`', unitab_xterm_std,
  566. sizeof(unitab_xterm_std));
  567. ucsdata->unitab_xterm['_'] = ' ';
  568. if (!used_dtf) {
  569. /* Make sure a reverse mapping exists for this code page. */
  570. make_reverse_mapping(ucsdata->line_codepage, ucsdata->unitab_line);
  571. }
  572. /* Find the line control characters. */
  573. for (i = 0; i < 256; i++)
  574. if (ucsdata->unitab_line[i] < ' '
  575. || (ucsdata->unitab_line[i] >= 0x7F &&
  576. ucsdata->unitab_line[i] < 0xA0))
  577. ucsdata->unitab_ctrl[i] = i;
  578. else
  579. ucsdata->unitab_ctrl[i] = 0xFF;
  580. /* Generate line->screen direct conversion links. */
  581. if (vtmode == VT_OEMANSI || vtmode == VT_XWINDOWS)
  582. link_font(ucsdata->unitab_scoacs, ucsdata->unitab_oemcp, CSET_OEMCP);
  583. link_font(ucsdata->unitab_line, ucsdata->unitab_font, CSET_ACP);
  584. link_font(ucsdata->unitab_scoacs, ucsdata->unitab_font, CSET_ACP);
  585. link_font(ucsdata->unitab_xterm, ucsdata->unitab_font, CSET_ACP);
  586. if (vtmode == VT_OEMANSI || vtmode == VT_XWINDOWS) {
  587. link_font(ucsdata->unitab_line, ucsdata->unitab_oemcp, CSET_OEMCP);
  588. link_font(ucsdata->unitab_xterm, ucsdata->unitab_oemcp, CSET_OEMCP);
  589. }
  590. if (ucsdata->dbcs_screenfont &&
  591. ucsdata->font_codepage != ucsdata->line_codepage) {
  592. /* F***ing Microsoft fonts, Japanese and Korean codepage fonts
  593. * have a currency symbol at 0x5C but their unicode value is
  594. * still given as U+005C not the correct U+00A5. */
  595. ucsdata->unitab_line['\\'] = CSET_OEMCP + '\\';
  596. }
  597. /* Last chance, if !unicode then try poorman links. */
  598. if (vtmode != VT_UNICODE) {
  599. static const char poorman_scoacs[] =
  600. "CueaaaaceeeiiiAAE**ooouuyOUc$YPsaiounNao?++**!<>###||||++||++++++--|-+||++--|-+----++++++++##||#aBTPEsyt******EN=+><++-=... n2* ";
  601. static const char poorman_latin1[] =
  602. " !cL.Y|S\"Ca<--R~o+23'u|.,1o>///?AAAAAAACEEEEIIIIDNOOOOOxOUUUUYPBaaaaaaaceeeeiiiionooooo/ouuuuypy";
  603. static const char poorman_vt100[] = "*#****o~**+++++-----++++|****L.";
  604. for (i = 160; i < 256; i++)
  605. if (!DIRECT_FONT(ucsdata->unitab_line[i]) &&
  606. ucsdata->unitab_line[i] >= 160 &&
  607. ucsdata->unitab_line[i] < 256) {
  608. ucsdata->unitab_line[i] =
  609. (WCHAR) (CSET_ACP +
  610. poorman_latin1[ucsdata->unitab_line[i] - 160]);
  611. }
  612. for (i = 96; i < 127; i++)
  613. if (!DIRECT_FONT(ucsdata->unitab_xterm[i]))
  614. ucsdata->unitab_xterm[i] =
  615. (WCHAR) (CSET_ACP + poorman_vt100[i - 96]);
  616. for (i = 128; i < 256; i++)
  617. if (!DIRECT_FONT(ucsdata->unitab_scoacs[i]))
  618. ucsdata->unitab_scoacs[i] =
  619. (WCHAR) (CSET_ACP + poorman_scoacs[i - 128]);
  620. }
  621. }
  622. void init_ucs_generic(Conf *conf, struct unicode_data *ucsdata)
  623. {
  624. init_ucs(conf, ucsdata);
  625. }
  626. static void link_font(WCHAR *line_tbl, WCHAR *font_tbl, WCHAR attr)
  627. {
  628. int font_index, line_index, i;
  629. for (line_index = 0; line_index < 256; line_index++) {
  630. if (DIRECT_FONT(line_tbl[line_index]))
  631. continue;
  632. for (i = 0; i < 256; i++) {
  633. font_index = ((32 + i) & 0xFF);
  634. if (line_tbl[line_index] == font_tbl[font_index]) {
  635. line_tbl[line_index] = (WCHAR) (attr + font_index);
  636. break;
  637. }
  638. }
  639. }
  640. }
  641. wchar_t xlat_uskbd2cyrllic(int ch)
  642. {
  643. static const wchar_t cyrtab[] = {
  644. 0, 1, 2, 3, 4, 5, 6, 7,
  645. 8, 9, 10, 11, 12, 13, 14, 15,
  646. 16, 17, 18, 19, 20, 21, 22, 23,
  647. 24, 25, 26, 27, 28, 29, 30, 31,
  648. 32, 33, 0x042d, 35, 36, 37, 38, 0x044d,
  649. 40, 41, 42, 0x0406, 0x0431, 0x0454, 0x044e, 0x002e,
  650. 48, 49, 50, 51, 52, 53, 54, 55,
  651. 56, 57, 0x0416, 0x0436, 0x0411, 0x0456, 0x042e, 0x002c,
  652. 64, 0x0424, 0x0418, 0x0421, 0x0412, 0x0423, 0x0410, 0x041f,
  653. 0x0420, 0x0428, 0x041e, 0x041b, 0x0414, 0x042c, 0x0422, 0x0429,
  654. 0x0417, 0x0419, 0x041a, 0x042b, 0x0415, 0x0413, 0x041c, 0x0426,
  655. 0x0427, 0x041d, 0x042f, 0x0445, 0x0457, 0x044a, 94, 0x0404,
  656. 96, 0x0444, 0x0438, 0x0441, 0x0432, 0x0443, 0x0430, 0x043f,
  657. 0x0440, 0x0448, 0x043e, 0x043b, 0x0434, 0x044c, 0x0442, 0x0449,
  658. 0x0437, 0x0439, 0x043a, 0x044b, 0x0435, 0x0433, 0x043c, 0x0446,
  659. 0x0447, 0x043d, 0x044f, 0x0425, 0x0407, 0x042a, 126, 127
  660. };
  661. return cyrtab[ch&0x7F];
  662. }
  663. static int check_compose_internal(int first, int second, int recurse)
  664. {
  665. static const struct {
  666. char first, second;
  667. wchar_t composed;
  668. } composetbl[] = {
  669. {0x2b, 0x2b, 0x0023},
  670. {0x41, 0x41, 0x0040},
  671. {0x28, 0x28, 0x005b},
  672. {0x2f, 0x2f, 0x005c},
  673. {0x29, 0x29, 0x005d},
  674. {0x28, 0x2d, 0x007b},
  675. {0x2d, 0x29, 0x007d},
  676. {0x2f, 0x5e, 0x007c},
  677. {0x21, 0x21, 0x00a1},
  678. {0x43, 0x2f, 0x00a2},
  679. {0x43, 0x7c, 0x00a2},
  680. {0x4c, 0x2d, 0x00a3},
  681. {0x4c, 0x3d, 0x20a4},
  682. {0x58, 0x4f, 0x00a4},
  683. {0x58, 0x30, 0x00a4},
  684. {0x59, 0x2d, 0x00a5},
  685. {0x59, 0x3d, 0x00a5},
  686. {0x7c, 0x7c, 0x00a6},
  687. {0x53, 0x4f, 0x00a7},
  688. {0x53, 0x21, 0x00a7},
  689. {0x53, 0x30, 0x00a7},
  690. {0x22, 0x22, 0x00a8},
  691. {0x43, 0x4f, 0x00a9},
  692. {0x43, 0x30, 0x00a9},
  693. {0x41, 0x5f, 0x00aa},
  694. {0x3c, 0x3c, 0x00ab},
  695. {0x2c, 0x2d, 0x00ac},
  696. {0x2d, 0x2d, 0x00ad},
  697. {0x52, 0x4f, 0x00ae},
  698. {0x2d, 0x5e, 0x00af},
  699. {0x30, 0x5e, 0x00b0},
  700. {0x2b, 0x2d, 0x00b1},
  701. {0x32, 0x5e, 0x00b2},
  702. {0x33, 0x5e, 0x00b3},
  703. {0x27, 0x27, 0x00b4},
  704. {0x2f, 0x55, 0x00b5},
  705. {0x50, 0x21, 0x00b6},
  706. {0x2e, 0x5e, 0x00b7},
  707. {0x2c, 0x2c, 0x00b8},
  708. {0x31, 0x5e, 0x00b9},
  709. {0x4f, 0x5f, 0x00ba},
  710. {0x3e, 0x3e, 0x00bb},
  711. {0x31, 0x34, 0x00bc},
  712. {0x31, 0x32, 0x00bd},
  713. {0x33, 0x34, 0x00be},
  714. {0x3f, 0x3f, 0x00bf},
  715. {0x60, 0x41, 0x00c0},
  716. {0x27, 0x41, 0x00c1},
  717. {0x5e, 0x41, 0x00c2},
  718. {0x7e, 0x41, 0x00c3},
  719. {0x22, 0x41, 0x00c4},
  720. {0x2a, 0x41, 0x00c5},
  721. {0x41, 0x45, 0x00c6},
  722. {0x2c, 0x43, 0x00c7},
  723. {0x60, 0x45, 0x00c8},
  724. {0x27, 0x45, 0x00c9},
  725. {0x5e, 0x45, 0x00ca},
  726. {0x22, 0x45, 0x00cb},
  727. {0x60, 0x49, 0x00cc},
  728. {0x27, 0x49, 0x00cd},
  729. {0x5e, 0x49, 0x00ce},
  730. {0x22, 0x49, 0x00cf},
  731. {0x2d, 0x44, 0x00d0},
  732. {0x7e, 0x4e, 0x00d1},
  733. {0x60, 0x4f, 0x00d2},
  734. {0x27, 0x4f, 0x00d3},
  735. {0x5e, 0x4f, 0x00d4},
  736. {0x7e, 0x4f, 0x00d5},
  737. {0x22, 0x4f, 0x00d6},
  738. {0x58, 0x58, 0x00d7},
  739. {0x2f, 0x4f, 0x00d8},
  740. {0x60, 0x55, 0x00d9},
  741. {0x27, 0x55, 0x00da},
  742. {0x5e, 0x55, 0x00db},
  743. {0x22, 0x55, 0x00dc},
  744. {0x27, 0x59, 0x00dd},
  745. {0x48, 0x54, 0x00de},
  746. {0x73, 0x73, 0x00df},
  747. {0x60, 0x61, 0x00e0},
  748. {0x27, 0x61, 0x00e1},
  749. {0x5e, 0x61, 0x00e2},
  750. {0x7e, 0x61, 0x00e3},
  751. {0x22, 0x61, 0x00e4},
  752. {0x2a, 0x61, 0x00e5},
  753. {0x61, 0x65, 0x00e6},
  754. {0x2c, 0x63, 0x00e7},
  755. {0x60, 0x65, 0x00e8},
  756. {0x27, 0x65, 0x00e9},
  757. {0x5e, 0x65, 0x00ea},
  758. {0x22, 0x65, 0x00eb},
  759. {0x60, 0x69, 0x00ec},
  760. {0x27, 0x69, 0x00ed},
  761. {0x5e, 0x69, 0x00ee},
  762. {0x22, 0x69, 0x00ef},
  763. {0x2d, 0x64, 0x00f0},
  764. {0x7e, 0x6e, 0x00f1},
  765. {0x60, 0x6f, 0x00f2},
  766. {0x27, 0x6f, 0x00f3},
  767. {0x5e, 0x6f, 0x00f4},
  768. {0x7e, 0x6f, 0x00f5},
  769. {0x22, 0x6f, 0x00f6},
  770. {0x3a, 0x2d, 0x00f7},
  771. {0x6f, 0x2f, 0x00f8},
  772. {0x60, 0x75, 0x00f9},
  773. {0x27, 0x75, 0x00fa},
  774. {0x5e, 0x75, 0x00fb},
  775. {0x22, 0x75, 0x00fc},
  776. {0x27, 0x79, 0x00fd},
  777. {0x68, 0x74, 0x00fe},
  778. {0x22, 0x79, 0x00ff},
  779. /* Unicode extras. */
  780. {0x6f, 0x65, 0x0153},
  781. {0x4f, 0x45, 0x0152},
  782. /* Compose pairs from UCS */
  783. {0x41, 0x2D, 0x0100},
  784. {0x61, 0x2D, 0x0101},
  785. {0x43, 0x27, 0x0106},
  786. {0x63, 0x27, 0x0107},
  787. {0x43, 0x5E, 0x0108},
  788. {0x63, 0x5E, 0x0109},
  789. {0x45, 0x2D, 0x0112},
  790. {0x65, 0x2D, 0x0113},
  791. {0x47, 0x5E, 0x011C},
  792. {0x67, 0x5E, 0x011D},
  793. {0x47, 0x2C, 0x0122},
  794. {0x67, 0x2C, 0x0123},
  795. {0x48, 0x5E, 0x0124},
  796. {0x68, 0x5E, 0x0125},
  797. {0x49, 0x7E, 0x0128},
  798. {0x69, 0x7E, 0x0129},
  799. {0x49, 0x2D, 0x012A},
  800. {0x69, 0x2D, 0x012B},
  801. {0x4A, 0x5E, 0x0134},
  802. {0x6A, 0x5E, 0x0135},
  803. {0x4B, 0x2C, 0x0136},
  804. {0x6B, 0x2C, 0x0137},
  805. {0x4C, 0x27, 0x0139},
  806. {0x6C, 0x27, 0x013A},
  807. {0x4C, 0x2C, 0x013B},
  808. {0x6C, 0x2C, 0x013C},
  809. {0x4E, 0x27, 0x0143},
  810. {0x6E, 0x27, 0x0144},
  811. {0x4E, 0x2C, 0x0145},
  812. {0x6E, 0x2C, 0x0146},
  813. {0x4F, 0x2D, 0x014C},
  814. {0x6F, 0x2D, 0x014D},
  815. {0x52, 0x27, 0x0154},
  816. {0x72, 0x27, 0x0155},
  817. {0x52, 0x2C, 0x0156},
  818. {0x72, 0x2C, 0x0157},
  819. {0x53, 0x27, 0x015A},
  820. {0x73, 0x27, 0x015B},
  821. {0x53, 0x5E, 0x015C},
  822. {0x73, 0x5E, 0x015D},
  823. {0x53, 0x2C, 0x015E},
  824. {0x73, 0x2C, 0x015F},
  825. {0x54, 0x2C, 0x0162},
  826. {0x74, 0x2C, 0x0163},
  827. {0x55, 0x7E, 0x0168},
  828. {0x75, 0x7E, 0x0169},
  829. {0x55, 0x2D, 0x016A},
  830. {0x75, 0x2D, 0x016B},
  831. {0x55, 0x2A, 0x016E},
  832. {0x75, 0x2A, 0x016F},
  833. {0x57, 0x5E, 0x0174},
  834. {0x77, 0x5E, 0x0175},
  835. {0x59, 0x5E, 0x0176},
  836. {0x79, 0x5E, 0x0177},
  837. {0x59, 0x22, 0x0178},
  838. {0x5A, 0x27, 0x0179},
  839. {0x7A, 0x27, 0x017A},
  840. {0x47, 0x27, 0x01F4},
  841. {0x67, 0x27, 0x01F5},
  842. {0x4E, 0x60, 0x01F8},
  843. {0x6E, 0x60, 0x01F9},
  844. {0x45, 0x2C, 0x0228},
  845. {0x65, 0x2C, 0x0229},
  846. {0x59, 0x2D, 0x0232},
  847. {0x79, 0x2D, 0x0233},
  848. {0x44, 0x2C, 0x1E10},
  849. {0x64, 0x2C, 0x1E11},
  850. {0x47, 0x2D, 0x1E20},
  851. {0x67, 0x2D, 0x1E21},
  852. {0x48, 0x22, 0x1E26},
  853. {0x68, 0x22, 0x1E27},
  854. {0x48, 0x2C, 0x1E28},
  855. {0x68, 0x2C, 0x1E29},
  856. {0x4B, 0x27, 0x1E30},
  857. {0x6B, 0x27, 0x1E31},
  858. {0x4D, 0x27, 0x1E3E},
  859. {0x6D, 0x27, 0x1E3F},
  860. {0x50, 0x27, 0x1E54},
  861. {0x70, 0x27, 0x1E55},
  862. {0x56, 0x7E, 0x1E7C},
  863. {0x76, 0x7E, 0x1E7D},
  864. {0x57, 0x60, 0x1E80},
  865. {0x77, 0x60, 0x1E81},
  866. {0x57, 0x27, 0x1E82},
  867. {0x77, 0x27, 0x1E83},
  868. {0x57, 0x22, 0x1E84},
  869. {0x77, 0x22, 0x1E85},
  870. {0x58, 0x22, 0x1E8C},
  871. {0x78, 0x22, 0x1E8D},
  872. {0x5A, 0x5E, 0x1E90},
  873. {0x7A, 0x5E, 0x1E91},
  874. {0x74, 0x22, 0x1E97},
  875. {0x77, 0x2A, 0x1E98},
  876. {0x79, 0x2A, 0x1E99},
  877. {0x45, 0x7E, 0x1EBC},
  878. {0x65, 0x7E, 0x1EBD},
  879. {0x59, 0x60, 0x1EF2},
  880. {0x79, 0x60, 0x1EF3},
  881. {0x59, 0x7E, 0x1EF8},
  882. {0x79, 0x7E, 0x1EF9},
  883. /* Compatible/possibles from UCS */
  884. {0x49, 0x4A, 0x0132},
  885. {0x69, 0x6A, 0x0133},
  886. {0x4C, 0x4A, 0x01C7},
  887. {0x4C, 0x6A, 0x01C8},
  888. {0x6C, 0x6A, 0x01C9},
  889. {0x4E, 0x4A, 0x01CA},
  890. {0x4E, 0x6A, 0x01CB},
  891. {0x6E, 0x6A, 0x01CC},
  892. {0x44, 0x5A, 0x01F1},
  893. {0x44, 0x7A, 0x01F2},
  894. {0x64, 0x7A, 0x01F3},
  895. {0x2E, 0x2E, 0x2025},
  896. {0x21, 0x21, 0x203C},
  897. {0x3F, 0x21, 0x2048},
  898. {0x21, 0x3F, 0x2049},
  899. {0x52, 0x73, 0x20A8},
  900. {0x4E, 0x6F, 0x2116},
  901. {0x53, 0x4D, 0x2120},
  902. {0x54, 0x4D, 0x2122},
  903. {0x49, 0x49, 0x2161},
  904. {0x49, 0x56, 0x2163},
  905. {0x56, 0x49, 0x2165},
  906. {0x49, 0x58, 0x2168},
  907. {0x58, 0x49, 0x216A},
  908. {0x69, 0x69, 0x2171},
  909. {0x69, 0x76, 0x2173},
  910. {0x76, 0x69, 0x2175},
  911. {0x69, 0x78, 0x2178},
  912. {0x78, 0x69, 0x217A},
  913. {0x31, 0x30, 0x2469},
  914. {0x31, 0x31, 0x246A},
  915. {0x31, 0x32, 0x246B},
  916. {0x31, 0x33, 0x246C},
  917. {0x31, 0x34, 0x246D},
  918. {0x31, 0x35, 0x246E},
  919. {0x31, 0x36, 0x246F},
  920. {0x31, 0x37, 0x2470},
  921. {0x31, 0x38, 0x2471},
  922. {0x31, 0x39, 0x2472},
  923. {0x32, 0x30, 0x2473},
  924. {0x31, 0x2E, 0x2488},
  925. {0x32, 0x2E, 0x2489},
  926. {0x33, 0x2E, 0x248A},
  927. {0x34, 0x2E, 0x248B},
  928. {0x35, 0x2E, 0x248C},
  929. {0x36, 0x2E, 0x248D},
  930. {0x37, 0x2E, 0x248E},
  931. {0x38, 0x2E, 0x248F},
  932. {0x39, 0x2E, 0x2490},
  933. {0x64, 0x61, 0x3372},
  934. {0x41, 0x55, 0x3373},
  935. {0x6F, 0x56, 0x3375},
  936. {0x70, 0x63, 0x3376},
  937. {0x70, 0x41, 0x3380},
  938. {0x6E, 0x41, 0x3381},
  939. {0x6D, 0x41, 0x3383},
  940. {0x6B, 0x41, 0x3384},
  941. {0x4B, 0x42, 0x3385},
  942. {0x4D, 0x42, 0x3386},
  943. {0x47, 0x42, 0x3387},
  944. {0x70, 0x46, 0x338A},
  945. {0x6E, 0x46, 0x338B},
  946. {0x6D, 0x67, 0x338E},
  947. {0x6B, 0x67, 0x338F},
  948. {0x48, 0x7A, 0x3390},
  949. {0x66, 0x6D, 0x3399},
  950. {0x6E, 0x6D, 0x339A},
  951. {0x6D, 0x6D, 0x339C},
  952. {0x63, 0x6D, 0x339D},
  953. {0x6B, 0x6D, 0x339E},
  954. {0x50, 0x61, 0x33A9},
  955. {0x70, 0x73, 0x33B0},
  956. {0x6E, 0x73, 0x33B1},
  957. {0x6D, 0x73, 0x33B3},
  958. {0x70, 0x56, 0x33B4},
  959. {0x6E, 0x56, 0x33B5},
  960. {0x6D, 0x56, 0x33B7},
  961. {0x6B, 0x56, 0x33B8},
  962. {0x4D, 0x56, 0x33B9},
  963. {0x70, 0x57, 0x33BA},
  964. {0x6E, 0x57, 0x33BB},
  965. {0x6D, 0x57, 0x33BD},
  966. {0x6B, 0x57, 0x33BE},
  967. {0x4D, 0x57, 0x33BF},
  968. {0x42, 0x71, 0x33C3},
  969. {0x63, 0x63, 0x33C4},
  970. {0x63, 0x64, 0x33C5},
  971. {0x64, 0x42, 0x33C8},
  972. {0x47, 0x79, 0x33C9},
  973. {0x68, 0x61, 0x33CA},
  974. {0x48, 0x50, 0x33CB},
  975. {0x69, 0x6E, 0x33CC},
  976. {0x4B, 0x4B, 0x33CD},
  977. {0x4B, 0x4D, 0x33CE},
  978. {0x6B, 0x74, 0x33CF},
  979. {0x6C, 0x6D, 0x33D0},
  980. {0x6C, 0x6E, 0x33D1},
  981. {0x6C, 0x78, 0x33D3},
  982. {0x6D, 0x62, 0x33D4},
  983. {0x50, 0x48, 0x33D7},
  984. {0x50, 0x52, 0x33DA},
  985. {0x73, 0x72, 0x33DB},
  986. {0x53, 0x76, 0x33DC},
  987. {0x57, 0x62, 0x33DD},
  988. {0x66, 0x66, 0xFB00},
  989. {0x66, 0x69, 0xFB01},
  990. {0x66, 0x6C, 0xFB02},
  991. {0x73, 0x74, 0xFB06},
  992. {0, 0, 0}
  993. }, *c;
  994. int nc = -1;
  995. for (c = composetbl; c->first; c++) {
  996. if (c->first == first && c->second == second)
  997. return c->composed;
  998. }
  999. if (recurse == 0) {
  1000. nc = check_compose_internal(second, first, 1);
  1001. if (nc == -1)
  1002. nc = check_compose_internal(toupper((unsigned char)first),
  1003. toupper((unsigned char)second), 1);
  1004. if (nc == -1)
  1005. nc = check_compose_internal(toupper((unsigned char)second),
  1006. toupper((unsigned char)first), 1);
  1007. }
  1008. return nc;
  1009. }
  1010. int check_compose(int first, int second)
  1011. {
  1012. return check_compose_internal(first, second, 0);
  1013. }
  1014. int decode_codepage(const char *cp_name)
  1015. {
  1016. const char *s, *d;
  1017. const struct cp_list_item *cpi;
  1018. int codepage = -1;
  1019. CPINFO cpinfo;
  1020. if (!cp_name || !*cp_name)
  1021. return CP_UTF8; /* default */
  1022. for (cpi = cp_list; cpi->name; cpi++) {
  1023. s = cp_name;
  1024. d = cpi->name;
  1025. for (;;) {
  1026. while (*s && !isalnum((unsigned char)*s) && *s != ':')
  1027. s++;
  1028. while (*d && !isalnum((unsigned char)*d) && *d != ':')
  1029. d++;
  1030. if (*s == 0) {
  1031. codepage = cpi->codepage;
  1032. if (codepage == CP_UTF8)
  1033. goto break_break;
  1034. if (codepage == -1)
  1035. return codepage;
  1036. if (codepage == 0) {
  1037. codepage = 65536 + (cpi - cp_list);
  1038. goto break_break;
  1039. }
  1040. if (GetCPInfo(codepage, &cpinfo) != 0)
  1041. goto break_break;
  1042. }
  1043. if (tolower((unsigned char)*s++) != tolower((unsigned char)*d++))
  1044. break;
  1045. }
  1046. }
  1047. d = cp_name;
  1048. if (tolower((unsigned char)d[0]) == 'c' &&
  1049. tolower((unsigned char)d[1]) == 'p')
  1050. d += 2;
  1051. if (tolower((unsigned char)d[0]) == 'i' &&
  1052. tolower((unsigned char)d[1]) == 'b' &&
  1053. tolower((unsigned char)d[2]) == 'm')
  1054. d += 3;
  1055. for (s = d; *s >= '0' && *s <= '9'; s++);
  1056. if (*s == 0 && s != d)
  1057. codepage = atoi(d); /* CP999 or IBM999 */
  1058. if (codepage == CP_ACP)
  1059. codepage = GetACP();
  1060. if (codepage == CP_OEMCP)
  1061. codepage = GetOEMCP();
  1062. if (codepage > 65535)
  1063. codepage = -2;
  1064. break_break:;
  1065. if (codepage != -1) {
  1066. if (codepage != CP_UTF8 && codepage < 65536) {
  1067. if (GetCPInfo(codepage, &cpinfo) == 0) {
  1068. codepage = -2;
  1069. } else if (cpinfo.MaxCharSize > 1)
  1070. codepage = -3;
  1071. }
  1072. }
  1073. if (codepage == -1 && *cp_name)
  1074. codepage = -2;
  1075. return codepage;
  1076. }
  1077. const char *cp_name(int codepage)
  1078. {
  1079. const struct cp_list_item *cpi, *cpno;
  1080. static char buf[32];
  1081. if (codepage == -1) {
  1082. sprintf(buf, "Use font encoding");
  1083. return buf;
  1084. }
  1085. if (codepage > 0 && codepage < 65536)
  1086. sprintf(buf, "CP%03d", codepage);
  1087. else
  1088. *buf = 0;
  1089. if (codepage >= 65536) {
  1090. cpno = 0;
  1091. for (cpi = cp_list; cpi->name; cpi++)
  1092. if (cpi == cp_list + (codepage - 65536)) {
  1093. cpno = cpi;
  1094. break;
  1095. }
  1096. if (cpno)
  1097. for (cpi = cp_list; cpi->name; cpi++) {
  1098. if (cpno->cp_table == cpi->cp_table)
  1099. return cpi->name;
  1100. }
  1101. } else {
  1102. for (cpi = cp_list; cpi->name; cpi++) {
  1103. if (codepage == cpi->codepage)
  1104. return cpi->name;
  1105. }
  1106. }
  1107. return buf;
  1108. }
  1109. /*
  1110. * Return the nth code page in the list, for use in the GUI
  1111. * configurer.
  1112. */
  1113. const char *cp_enumerate(int index)
  1114. {
  1115. if (index < 0 || index >= lenof(cp_list))
  1116. return NULL;
  1117. return cp_list[index].name;
  1118. }
  1119. void get_unitab(int codepage, wchar_t *unitab, int ftype)
  1120. {
  1121. char tbuf[4];
  1122. int i, max = 256, flg = MB_ERR_INVALID_CHARS;
  1123. if (ftype)
  1124. flg |= MB_USEGLYPHCHARS;
  1125. if (ftype == 2)
  1126. max = 128;
  1127. if (codepage == CP_UTF8) {
  1128. for (i = 0; i < max; i++)
  1129. unitab[i] = i;
  1130. return;
  1131. }
  1132. if (codepage == CP_ACP)
  1133. codepage = GetACP();
  1134. else if (codepage == CP_OEMCP)
  1135. codepage = GetOEMCP();
  1136. if (codepage > 0 && codepage < 65536) {
  1137. for (i = 0; i < max; i++) {
  1138. tbuf[0] = i;
  1139. if (mb_to_wc(codepage, flg, tbuf, 1, unitab + i, 1)
  1140. != 1)
  1141. unitab[i] = 0xFFFD;
  1142. }
  1143. } else {
  1144. int j = 256 - cp_list[codepage & 0xFFFF].cp_size;
  1145. for (i = 0; i < max; i++)
  1146. unitab[i] = i;
  1147. for (i = j; i < max; i++)
  1148. unitab[i] = cp_list[codepage & 0xFFFF].cp_table[i - j];
  1149. }
  1150. }
  1151. int wc_to_mb(int codepage, int flags, const wchar_t *wcstr, int wclen,
  1152. char *mbstr, int mblen, const char *defchr)
  1153. {
  1154. reverse_mapping *rmap = get_reverse_mapping(codepage);
  1155. if (rmap) {
  1156. /* Do this by array lookup if we can. */
  1157. if (wclen < 0) {
  1158. for (wclen = 0; wcstr[wclen++] ;); /* will include the NUL */
  1159. }
  1160. char *p;
  1161. int i;
  1162. for (p = mbstr, i = 0; i < wclen; i++) {
  1163. wchar_t ch = wcstr[i];
  1164. int by;
  1165. const char *p1;
  1166. #define WRITECH(chr) do \
  1167. { \
  1168. assert(p - mbstr < mblen); \
  1169. *p++ = (char)(chr); \
  1170. } while (0)
  1171. if ((p1 = rmap->blocks[(ch >> 8) & 0xFF]) != NULL &&
  1172. (by = p1[ch & 0xFF]) != '\0')
  1173. WRITECH(by);
  1174. else if (ch < 0x80)
  1175. WRITECH(ch);
  1176. else if (defchr)
  1177. for (const char *q = defchr; *q; q++)
  1178. WRITECH(*q);
  1179. #if 1
  1180. else
  1181. WRITECH('.');
  1182. #endif
  1183. #undef WRITECH
  1184. }
  1185. return p - mbstr;
  1186. } else {
  1187. int defused, ret;
  1188. ret = WideCharToMultiByte(codepage, flags, wcstr, wclen,
  1189. mbstr, mblen, defchr, &defused);
  1190. if (ret)
  1191. return ret;
  1192. #ifdef LEGACY_WINDOWS
  1193. /*
  1194. * Fallback for legacy platforms too old to support UTF-8: if
  1195. * the codepage is UTF-8, we can do the translation ourselves.
  1196. */
  1197. if (codepage == CP_UTF8 && mblen > 0 && wclen > 0) {
  1198. buffer_sink bs[1];
  1199. buffer_sink_init(bs, mbstr, mblen);
  1200. while (wclen > 0) {
  1201. unsigned long wc = (wclen--, *wcstr++);
  1202. if (wclen > 0 && IS_SURROGATE_PAIR(wc, *wcstr)) {
  1203. wc = FROM_SURROGATES(wc, *wcstr);
  1204. wclen--, wcstr++;
  1205. }
  1206. const char *prev_ptr = bs->out;
  1207. put_utf8_char(bs, wc);
  1208. if (bs->overflowed)
  1209. return prev_ptr - mbstr;
  1210. }
  1211. return bs->out - mbstr;
  1212. }
  1213. #endif
  1214. /* No other fallbacks are available */
  1215. return 0;
  1216. }
  1217. }
  1218. int mb_to_wc(int codepage, int flags, const char *mbstr, int mblen,
  1219. wchar_t *wcstr, int wclen)
  1220. {
  1221. if (codepage >= 65536) {
  1222. /* Character set not known to Windows, so we'll have to
  1223. * translate it ourself */
  1224. size_t index = codepage - 65536;
  1225. if (index >= lenof(cp_list))
  1226. return 0;
  1227. const struct cp_list_item *cp = &cp_list[index];
  1228. if (!cp->cp_table)
  1229. return 0;
  1230. size_t remaining = wclen;
  1231. wchar_t *p = wcstr;
  1232. unsigned tablebase = 256 - cp->cp_size;
  1233. while (mblen > 0) {
  1234. mblen--;
  1235. unsigned c = 0xFF & *mbstr++;
  1236. wchar_t wc = (c < tablebase ? c : cp->cp_table[c - tablebase]);
  1237. if (remaining > 0) {
  1238. remaining--;
  1239. *p++ = wc;
  1240. } else {
  1241. return p - wcstr;
  1242. }
  1243. }
  1244. return p - wcstr;
  1245. }
  1246. int ret = MultiByteToWideChar(codepage, flags, mbstr, mblen, wcstr, wclen);
  1247. if (ret)
  1248. return ret;
  1249. #ifdef LEGACY_WINDOWS
  1250. /*
  1251. * Fallback for legacy platforms too old to support UTF-8: if the
  1252. * codepage is UTF-8, we can do the translation ourselves.
  1253. */
  1254. if (codepage == CP_UTF8 && mblen > 0 && wclen > 0) {
  1255. BinarySource src[1];
  1256. BinarySource_BARE_INIT(src, mbstr, mblen);
  1257. size_t remaining = wclen;
  1258. wchar_t *p = wcstr;
  1259. while (get_avail(src)) {
  1260. wchar_t wcbuf[2];
  1261. size_t nwc = decode_utf8_to_wchar(src, wcbuf, NULL);
  1262. for (size_t i = 0; i < nwc; i++) {
  1263. if (remaining > 0) {
  1264. remaining--;
  1265. *p++ = wcbuf[i];
  1266. } else {
  1267. return p - wcstr;
  1268. }
  1269. }
  1270. }
  1271. return p - wcstr;
  1272. }
  1273. #endif
  1274. /* No other fallbacks are available */
  1275. return 0;
  1276. }
  1277. bool is_dbcs_leadbyte(int codepage, char byte)
  1278. {
  1279. return IsDBCSLeadByteEx(codepage, byte);
  1280. }