home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / os2 / pgp263.arj / PGP263I.SRC / PGP263II.ZIP / src / charset.c < prev    next >
C/C++ Source or Header  |  1996-01-10  |  17KB  |  418 lines

  1. /*
  2.  * charset.c
  3.  *
  4.  * Conversion tables and routines to support different character sets.
  5.  * The PGP internal format is latin-1.
  6.  *
  7.  * (c) Copyright 1990-1996 by Philip Zimmermann.  All rights reserved.
  8.  * The author assumes no liability for damages resulting from the use
  9.  * of this software, even if the damage results from defects in this
  10.  * software.  No warranty is expressed or implied.
  11.  *
  12.  * Code that has been incorporated into PGP from other sources was
  13.  * either originally published in the public domain or is used with
  14.  * permission from the various authors.
  15.  *
  16.  * PGP is available for free to the public under certain restrictions.
  17.  * See the PGP User's Guide (included in the release package) for
  18.  * important information about licensing, patent restrictions on
  19.  * certain algorithms, trademarks, copyrights, and export controls.
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include "usuals.h"
  26. #include "language.h"
  27. #include "charset.h"
  28. #include "system.h"
  29.  
  30. #ifndef NULL
  31. #define    NULL    0
  32. #endif
  33.  
  34. #define UNK    '?'
  35.  
  36. static unsigned char
  37. intern2ascii[] = { /* ISO 8859-1 Latin Alphabet 1 to US ASCII */
  38. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  39. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  40.  32,  33,  99,  35,  36,  89, 124,  80,  34,  67,  97,  34, 126,  45,  82,  95,
  41. 111, UNK,  50,  51,  39, 117,  45,  45,  44,  49, 111,  34, UNK, UNK, UNK,  63,
  42.  65,  65,  65,  65,  65,  65,  65,  67,  69,  69,  69,  69,  73,  73,  73,  73,
  43.  68,  78,  79,  79,  79,  79,  79, 120,  79,  85,  85,  85,  85,  89,  84, 115,
  44.  97,  97,  97,  97,  97,  97,  97,  99, 101, 101, 101, 101, 105, 105, 105, 105,
  45. 100, 110, 111, 111, 111, 111, 111,  47, 111, 117, 117, 117, 117, 121, 116, 121
  46. };
  47.  
  48. static unsigned char
  49. intern2cp850[] = { /* ISO 8859-1 Latin Alphabet 1 (Latin-1)
  50.               to IBM Code Page 850 (International) */
  51. 186, 205, 201, 187, 200, 188, 204, 185, 203, 202, 206, 223, 220, 219, 254, 242,
  52. 179, 196, 218, 191, 192, 217, 195, 180, 194, 193, 197, 176, 177, 178, 213, 159,
  53. 255, 173, 189, 156, 207, 190, 221, 245, 249, 184, 166, 174, 170, 240, 169, 238,
  54. 248, 241, 253, 252, 239, 230, 244, 250, 247, 251, 167, 175, 172, 171, 243, 168,
  55. 183, 181, 182, 199, 142, 143, 146, 128, 212, 144, 210, 211, 222, 214, 215, 216,
  56. 209, 165, 227, 224, 226, 229, 153, 158, 157, 235, 233, 234, 154, 237, 232, 225,
  57. 133, 160, 131, 198, 132, 134, 145, 135, 138, 130, 136, 137, 141, 161, 140, 139,
  58. 208, 164, 149, 162, 147, 228, 148, 246, 155, 151, 163, 150, 129, 236, 231, 152
  59. };
  60.  
  61. static unsigned char
  62. cp8502intern[] = { /* IBM Code Page 850 to Latin-1 */
  63. 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197,
  64. 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 248, 163, 216, 215, 159,
  65. 225, 237, 243, 250, 241, 209, 170, 186, 191, 174, 172, 189, 188, 161, 171, 187,
  66. 155, 156, 157, 144, 151, 193, 194, 192, 169, 135, 128, 131, 133, 162, 165, 147,
  67. 148, 153, 152, 150, 145, 154, 227, 195, 132, 130, 137, 136, 134, 129, 138, 164,
  68. 240, 208, 202, 203, 200, 158, 205, 206, 207, 149, 146, 141, 140, 166, 204, 139,
  69. 211, 223, 212, 210, 245, 213, 181, 254, 222, 218, 219, 217, 253, 221, 175, 180,
  70. 173, 177, 143, 190, 182, 167, 247, 184, 176, 168, 183, 185, 179, 178, 142, 160
  71. };
  72.  
  73. static unsigned char
  74. intern2cp852[] = { /* ISO 8859-2 Latin Alphabet 2 (Latin-2)
  75.               to IBM Code Page 852 (Eastern Europe) */
  76. 186, 205, 201, 187, 200, 188, 204, 185, 203, 202, 206, 223, 220, 219, 254, UNK,
  77. 179, 196, 218, 191, 192, 217, 195, 180, 194, 193, 197, 176, 177, 178, UNK, UNK,
  78. 255, 164, 244, 157, 207, 149, 151, 245, 249, 230, 184, 155, 141, 240, 166, 189,
  79. 248, 165, 247, 136, 239, 150, 151, 243, 242, 231, 173, 156, 171, 241, 167, 190,
  80. 232, 181, 182, 198, 142, 145, 143, 128, 172, 144, 168, 211, 183, 214,  73, 210,
  81. 209, 227, 213, 224, 226, 138, 153, 158, 252, 222, 233, 235, 154, 237, 221, 225,
  82. 234, 160, 131, 199, 132, 146, 134, 135, 159, 130, 169, 137, 216, 161, 140, 212,
  83. 208, 228, 229, 162, 147, 139, 148, 246, 253, 133, 163, 251, 129, 236, 238, 250
  84. };
  85.  
  86. static unsigned char
  87. cp8522intern[] = { /* IBM Code Page 852 to Latin-2 */
  88. 199, 252, 233, 226, 228, 249, 230, 231, 179, 235, 213, 245, 238, 172, 196, 198,
  89. 201, 197, 229, 244, 246, 165, 181, 166, 182, 214, 220, 171, 187, 163, 215, 232,
  90. 225, 237, 243, 250, 161, 177, 174, 190, 202, 234, UNK, 188, 200, 186,  60,  62,
  91. 155, 156, 157, 144, 151, 193, 194, 204, 170, 135, 128, 131, 133, 175, 191, 147,
  92. 148, 153, 152, 150, 145, 154, 195, 227, 132, 130, 137, 136, 134, 129, 138, 164,
  93. 240, 208, 207, 203, 239, 210, 205,  85, 236, 149, 146, 141, 140, 222, 217, 139,
  94. 211, 223, 212, 209, 241, 242, 169, 185, 192, 218, 224, 219, 253, 221, 254, 180,
  95. 173, 189, 184, 183, 162, 167, 247, 178, 176, 168, 255, 251, 216, 248, 142, 160
  96. };
  97.  
  98. static unsigned char
  99. intern2cp860[] = { /* ISO 8859-1 Latin Alphabet 1 (Latin-1)
  100.                       to IBM Code Page 860 (Portuguese) */
  101. 186, 205, 201, 187, 200, 188, 204, 185, 203, 202, 206, 223, 220, 219, 254,  95,
  102. 179, 196, 218, 191, 192, 217, 195, 180, 194, 193, 197, 176, 177, 178, UNK, UNK,
  103. 255, 173, 155, 156,  36,  89, 124,  80,  34,  67, 166, 174, 170,  45,  82,  95,
  104. 248, 241, 253,  51,  39, 230,  45, 250,  44,  49, 167, 175, 172, 171, UNK, 168,
  105. 145, 134, 143, 142,  65,  65,  65, 128, 146, 144, 137,  69, 152, 139,  73,  73,
  106.  68, 165, 169, 159, 140, 153,  79, 120,  79, 157, 150,  85, 154,  89,  84, 225,
  107. 133, 160, 131, 132,  97,  97,  97, 135, 138, 130, 136, 101, 141, 161, 105, 105,
  108. 100, 164, 149, 162, 147, 148, 111, 246, 111, 151, 163, 117, 129, 121, 116, 121
  109. };
  110.  
  111. static unsigned char
  112. cp8602intern[] = { /* IBM Code Page 860 to Latin-1 */
  113. 199, 252, 233, 226, 227, 224, 193, 231, 234, 202, 232, 205, 212, 236, 195, 194,
  114. 201, 192, 200, 244, 245, 242, 218, 249, 204, 213, 220, 162, 163, 217, 164, 211,
  115. 225, 237, 243, 250, 241, 209, 170, 186, 191, 210, 172, 189, 188, 161, 171, 187,
  116. 155, 156, 157, 144, 151, 151, 135, 147, 131, 135, 128, 131, 133, 149, 133, 147,
  117. 148, 153, 152, 150, 145, 154, 150, 134, 132, 130, 137, 136, 134, 129, 138, 137,
  118. 153, 136, 152, 148, 132, 130, 146, 154, 138, 149, 146, 141, 140, 141, 141, 139,
  119. UNK, 223, UNK, UNK, UNK, UNK, 181, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  120.  61, UNK, UNK, UNK, UNK, UNK, 247, 126, 176, 183, 183, UNK, 110, 178, 142, 160
  121. };
  122.  
  123. static unsigned char
  124. intern2keybcs[] = { /* ISO 8859-2 Latin Alphabet 2 (Latin-2)
  125.                 to KEYBCS2 (Eastern Europe) */
  126. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  127. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  128. UNK,  65, UNK,  76, UNK, 156,  83, 173, UNK, 155,  83, 134,  90, UNK, 146,  90,
  129. 248, UNK, UNK, 108, UNK, 140, 115, UNK, UNK, 168, 115, 159, 122, UNK, 145, 122,
  130. 171, 143,  65,  65, 142, 138,  67,  67, 128, 144,  69,  69, 137, 139,  73, 133,
  131.  68,  78, 165, 149, 167,  79, 153, UNK, 158, 166, 151,  85, 154, 157,  84, 225,
  132. 170, 160,  97,  97, 132, 141,  99,  99, 135, 130, 101, UNK, 136, 161, 105, 131,
  133. 100, 110, 164, 162, 147, 111, 148, UNK, 169, 150, 163, 117, 129, 152, 116, UNK
  134. };
  135.  
  136. static unsigned char
  137. keybcs2intern[] = { /* KEYBCS2 to Latin-2 */
  138. 200, 252, 233, 239, 228, 207, 171, 232, 236, 204, 197, 205, 181, 229, 196, 193,
  139. 201, 190, 174, 244, 246, 211, 249, 218, 253, 214, 220, 169, 165, 221, 216, 187,
  140. 225, 237, 243, 250, 242, 210, 217, 212, 185, 248, 224, 192, UNK, 167, UNK, UNK,
  141. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  142. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  143. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  144. UNK, 223, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  145. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, 176, UNK, UNK, UNK, UNK, UNK, UNK, UNK
  146. };
  147.  
  148. static unsigned char
  149. intern2next[] = { /* ISO 8859-1 Latin Alphabet 1 (Latin-1)
  150.                      to NeXTSTEP char set */
  151.  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,
  152. 0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307, 0310,  UNK, 0312, 0313,  UNK, 0272, 0316, 0247,
  153.  UNK, 0241, 0242, 0243, 0250, 0245, 0265,  UNK, 0310, 0240, 0343, 0253, 0276, 0261, 0260, 0320,
  154. 0312, 0321, 0311, 0314, 0270, 0235, 0266, 0267, 0313, 0300,  UNK, 0273, 0322, 0323, 0324, 0277,
  155. 0201, 0202, 0203, 0204, 0205, 0206, 0341, 0207, 0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  156. 0220, 0221, 0222, 0223, 0224, 0225, 0226, 0236, 0351, 0227, 0230, 0231, 0232, 0233, 0234, 0373,
  157. 0325, 0326, 0327, 0330, 0331, 0332, 0361, 0333, 0334, 0335, 0336, 0337, 0340, 0342, 0344, 0345,
  158. 0346, 0347, 0354, 0355, 0356, 0357, 0360, 0237, 0371, 0362, 0363, 0364, 0366, 0367, 0374, 0375
  159. };
  160.  
  161. static unsigned char
  162. next2intern[] = { /* NeXTSTEP char set to Latin-1 */
  163.  UNK, 0300, 0301, 0302, 0303, 0304, 0305, 0307, 0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  164. 0320, 0321, 0322, 0323, 0324, 0325, 0326, 0331, 0332, 0333, 0334, 0335, 0336, 0265, 0337, 0267,
  165. 0251, 0241, 0242, 0243, 0057, 0245, 0146, 0247, 0244, 0140, 0042, 0253, 0074, 0076,  UNK,  UNK,
  166. 0256, 0255,  UNK,  UNK, 0056, 0246, 0266, 0267, 0054, 0042, 0235, 0273,  UNK,  UNK, 0254, 0277,
  167. 0220, 0221, 0222, 0223, 0224, 0225, 0226, 0232, 0230, 0262, 0227, 0270, 0263, 0042, 0236, 0226,
  168. 0257, 0261, 0274, 0275, 0276, 0340, 0341, 0342, 0343, 0344, 0345, 0347, 0350, 0351, 0352, 0353,
  169. 0354, 0306, 0355, 0252, 0356, 0357, 0365, 0361, 0243, 0330,  UNK, 0272, 0362, 0363, 0364, 0365,
  170. 0366, 0346, 0371, 0372, 0373, 0151, 0374, 0375, 0154, 0370,  UNK, 0337, 0376, 0377, UNK, UNK
  171. };
  172.  
  173. #ifdef MACTC5
  174. Boolean iso_latin1 = false;
  175. #endif
  176.  
  177. static unsigned char
  178. intern2mac[] = { /* ISO 8859-1 Latin Alphabet 1 (Latin1)
  179.                     to Macintosh Geneva/Monaco */
  180. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  181. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  182. 202, 193, 162, 163, 217, 180, 124, 164, 172, 169, 187, 199, 194, 209, 168,  95,
  183. 188, 177,  50,  51, 171, 181, 166, 165,  44,  49, 161, 200, UNK, UNK, UNK, 192,
  184. 203, 135, 129, 204, 128, 129, 174, 130, 143, 131, 144, 145, 147, 146, 148, 149,
  185.  68, 132, 152, 151, 153, 205, 133, 120, 175, 157, 156, 158, 134,  89,  84, 167,
  186. 136, 135, 137, 139, 138, 140,  97, 141, 143, 142, 144, 145, 147, 146, 148, 149,
  187. 100, 150, 152, 151, 153, 155, 154, 214, 191, 157, 156, 158, 159, 121, 116, 216
  188. };
  189.  
  190. static unsigned char
  191. mac2intern[] = { /* Macintosh Geneva/Monaco to Latin-1 */
  192. 196, 197, 199, 201, 209, 214, 220, 225, 224, 226, 228, 227, 229, 231, 233, 232,
  193. 234, 235, 237, 236, 238, 239, 241, 243, 242, 244, 246, 245, 250, 249, 251, 252,
  194. UNK, 186, 162, 163, 167, 183, 182, 223, 174, 169, UNK, 180, 168, UNK, 198, 216,
  195. UNK, 177, UNK, UNK, 165, 181, 100,  83,  80, 112,  83, 170, 176,  79, 230, 248,
  196. 191, 161, 172, UNK, 102, 126,  68, 171, 187, UNK, 160, 192, 195, 213,  79, 111,
  197.  45, 173,  34,  34,  96,  39, 247, UNK, 255, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  198. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  199. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK
  200. };
  201.  
  202. /* Russian language specific conversation section */
  203. /* Two point-to-point charset decode tables       */
  204. /* produced by Andrew A. Chernov                  */
  205. /* Decode single char from KOI8-R to ALT-CODES, if present */
  206. static unsigned char intern2alt[] = {
  207.     0xc4, 0xb3, 0xda, 0xbf, 0xc0, 0xd9, 0xc3, 0xb4,
  208.     0xc2, 0xc1, 0xc5, 0xdf, 0xdc, 0xdb, 0xdd, 0xde,
  209.     0xb0, 0xb1, 0xb2, 0xf4, 0xfe, 0xf9, 0xfb, 0xf7,
  210.     0xf3, 0xf2, 0xff, 0xf5, 0xf8, 0xfd, 0xfa, 0xf6,
  211.     0xcd, 0xba, 0xd5, 0xf1, 0xd6, 0xc9, 0xb8, 0xb7,
  212.     0xbb, 0xd4, 0xd3, 0xc8, 0xbe, 0xbd, 0xbc, 0xc6,
  213.     0xc7, 0xcc, 0xb5, 0xf0, 0xb6, 0xb9, 0xd1, 0xd2,
  214.     0xcb, 0xcf, 0xd0, 0xca, 0xd8, 0xd7, 0xce, 0xfc,
  215.     0xee, 0xa0, 0xa1, 0xe6, 0xa4, 0xa5, 0xe4, 0xa3,
  216.     0xe5, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae,
  217.     0xaf, 0xef, 0xe0, 0xe1, 0xe2, 0xe3, 0xa6, 0xa2,
  218.     0xec, 0xeb, 0xa7, 0xe8, 0xed, 0xe9, 0xe7, 0xea,
  219.     0x9e, 0x80, 0x81, 0x96, 0x84, 0x85, 0x94, 0x83,
  220.     0x95, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
  221.     0x8f, 0x9f, 0x90, 0x91, 0x92, 0x93, 0x86, 0x82,
  222.     0x9c, 0x9b, 0x87, 0x98, 0x9d, 0x99, 0x97, 0x9a
  223. };
  224.  
  225. /* Decode single char from ALT-CODES, if present, to KOI8-R */
  226. static unsigned char alt2intern[] = {
  227.     0xe1, 0xe2, 0xf7, 0xe7, 0xe4, 0xe5, 0xf6, 0xfa,
  228.     0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0,
  229.     0xf2, 0xf3, 0xf4, 0xf5, 0xe6, 0xe8, 0xe3, 0xfe,
  230.     0xfb, 0xfd, 0xff, 0xf9, 0xf8, 0xfc, 0xe0, 0xf1,
  231.     0xc1, 0xc2, 0xd7, 0xc7, 0xc4, 0xc5, 0xd6, 0xda,
  232.     0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
  233.     0x90, 0x91, 0x92, 0x81, 0x87, 0xb2, 0xb4, 0xa7,
  234.     0xa6, 0xb5, 0xa1, 0xa8, 0xae, 0xad, 0xac, 0x83,
  235.     0x84, 0x89, 0x88, 0x86, 0x80, 0x8a, 0xaf, 0xb0,
  236.     0xab, 0xa5, 0xbb, 0xb8, 0xb1, 0xa0, 0xbe, 0xb9,
  237.     0xba, 0xb6, 0xb7, 0xaa, 0xa9, 0xa2, 0xa4, 0xbd,
  238.     0xbc, 0x85, 0x82, 0x8d, 0x8c, 0x8e, 0x8f, 0x8b,
  239.     0xd2, 0xd3, 0xd4, 0xd5, 0xc6, 0xc8, 0xc3, 0xde,
  240.     0xdb, 0xdd, 0xdf, 0xd9, 0xd8, 0xdc, 0xc0, 0xd1,
  241.     0xb3, 0xa3, 0x99, 0x98, 0x93, 0x9b, 0x9f, 0x97,
  242.     0x9c, 0x95, 0x9e, 0x96, 0xbf, 0x9d, 0x94, 0x9a
  243. };
  244.  
  245. /*
  246.  * Most Unixes has KOI8, and DOS has ALT_CODES
  247.  * If your Unix is non-standard, set CHARSET to "alt_codes"
  248.  * in config.txt
  249.  */
  250.  
  251. #ifndef    DEFAULT_RU_CSET
  252. #ifdef MSDOS
  253. #define DEFAULT_RU_CSET "alt_codes"
  254. #else
  255. #define DEFAULT_RU_CSET "koi8"
  256. #endif
  257. #endif
  258.  
  259. /* End of Russian section */
  260.  
  261. #ifndef    DEFAULT_CSET
  262. #if defined(MSDOS)
  263. #define DEFAULT_CSET    "cp850"
  264. #elif defined(NEXT)
  265. #define    DEFAULT_CSET    "next"
  266. #elif defined(MACTC5)
  267. #define    DEFAULT_CSET    "mac"
  268. #else
  269. #define    DEFAULT_CSET    "noconv"
  270. #endif
  271. #endif
  272.  
  273. #ifdef EBCDIC
  274. /* ebcdic-ascii converting, accustom to your local MVS-settings */
  275. /* in this case it's taken from c370.c                          */
  276. #define ebcdic__ascii ebcdic_ascii
  277. #define ascii__ebcdic ascii_ebcdic
  278. #endif /* EBCDIC */
  279.  
  280. int CONVERSION = NO_CONV;      /* None text file conversion at start time */
  281.  
  282. unsigned char *ext_c_ptr;
  283. static unsigned char *int_c_ptr;
  284.  
  285. char charset[16] = "";
  286.  
  287. void
  288. init_charset(void)
  289. {
  290.     ext_c_ptr = NULL;    /* NULL means latin1 or KOI8
  291.                    (internal format) */
  292.     int_c_ptr = NULL;
  293.  
  294.     if (charset[0] == '\0') {
  295.         /* use default character set for this system */
  296. #ifdef MACTC5
  297.         if (iso_latin1)
  298.             strcpy(charset, DEFAULT_CSET);
  299.         else
  300.             strcpy(charset, "noconv");
  301. #else
  302.         if (strcmp(language, "ru") == 0)
  303.             strcpy(charset, DEFAULT_RU_CSET);
  304.         else
  305.             strcpy(charset, DEFAULT_CSET);
  306. #endif
  307.     } else {
  308.         strlwr(charset);
  309.     }
  310.  
  311.     /* latin-1 and KOI8 are in internal format: no conversion needed */
  312.     if (!strcmp(charset, "latin1") || !strcmp(charset, "koi8") ||
  313.         !strcmp(charset, "noconv"))
  314.         return;
  315.  
  316.     if (!strcmp(charset, "cp850")) {
  317.         ext_c_ptr = intern2cp850;
  318.         int_c_ptr = cp8502intern;
  319.     } else if (!strcmp(charset, "cp852")) {
  320.         ext_c_ptr = intern2cp852;
  321.         int_c_ptr = cp8522intern;
  322.     } else if (!strcmp(charset, "cp860")) {
  323.         ext_c_ptr = intern2cp860;
  324.         int_c_ptr = cp8602intern;
  325.      } else if (!strcmp(charset, "cp866")) {
  326.          ext_c_ptr = intern2alt;
  327.          int_c_ptr = alt2intern;
  328.     } else if (!strcmp(charset, "alt_codes")) {
  329.         ext_c_ptr = intern2alt;
  330.         int_c_ptr = alt2intern;
  331.     } else if (!strcmp(charset, "keybcs2"))  {
  332.             ext_c_ptr = intern2keybcs;
  333.         int_c_ptr = keybcs2intern;
  334.     } else if (!strcmp(charset, "next"))  {
  335.             ext_c_ptr = intern2next;
  336.         int_c_ptr = next2intern;
  337.     } else if (!strcmp(charset, "mac"))  {
  338.             ext_c_ptr = intern2mac;
  339.         int_c_ptr = mac2intern;
  340.     } else if (!strcmp(charset, "ascii")) {
  341.         ext_c_ptr = intern2ascii;
  342.     } else {
  343.         fprintf(stderr, LANG("Unsupported character set: '%s'\n"),
  344.             charset);
  345.         strcpy(charset, "noconv");
  346.     }
  347. }
  348.  
  349. #ifdef EBCDIC
  350. char EXT_C(char c)  { return ascii__ebcdic[c]; }
  351. char INT_C(char c)  { return ebcdic__ascii[c]; }
  352. #else /* !EBCDIC */
  353. char
  354. EXT_C(char c)
  355. {
  356.      if (!(c & 0x80) || !ext_c_ptr)
  357.         return c;
  358.     return ext_c_ptr[c & 0x7f];
  359. }
  360.  
  361. char
  362. INT_C(char c)
  363. {
  364.      if (!(c & 0x80) || !int_c_ptr)
  365.         return c;
  366.     return int_c_ptr[c & 0x7f];
  367. }
  368. #endif /* !EBCDIC */
  369.  
  370. /*
  371.  * to_upper() and to_lower(), replacement for toupper() and tolower(),
  372.  * calling to_upper() on uppercase or to_lower on lowercase characters
  373.  * is handled correctly.
  374.  * 
  375.  * XXX: should handle local characterset when 8-bit userID's are allowed
  376.  */
  377. #ifdef EBCDIC
  378. /* With EBCDIC-charset things like (c >= 'a' && c <= 'z') do not work!!!
  379.  * Therefor use the appropriate ctype-functions
  380.  */
  381. #include <ctype.h>
  382. int to_upper(int c) { return toupper(c); }
  383. int to_lower(int c) { return tolower(c); }
  384. #else /* !EBCDIC */
  385. int
  386. to_upper(int c)
  387. {
  388.      c &= 0xFF;
  389.      if (islower(c))
  390.          return (toupper(c));
  391.      return c;
  392. }
  393.  
  394. int
  395. to_lower(int c)
  396. {
  397.      c &= 0xFF;
  398.      if (isupper(c))
  399.          return (tolower(c));
  400.      return c;
  401. }
  402. #endif /* !EBCDIC */
  403.  
  404. #ifdef EBCDIC
  405. void CONVERT_TO_CANONICAL_CHARSET(char *s) /* String to internal string (at same place) */
  406. {
  407.     for (; *s; s++) *s = INT_C(*s);
  408. }
  409.  
  410. static char buf[128];
  411. char * LOCAL_CHARSET( char *s)             /* String to external string (at extra place) */
  412. {
  413.     strcpy( buf, s );
  414.     for (s=buf; *s; s++) *s = EXT_C(*s);
  415.     return buf;
  416. }
  417. #endif /* EBCDIC */
  418.