home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / pgp2 / src / c / charset < prev    next >
Encoding:
Text File  |  1995-02-13  |  8.8 KB  |  243 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-1994 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 "usuals.h"
  25. #include "language.h"
  26. #include "charset.h"
  27. #include "system.h"
  28.  
  29. #ifndef NULL
  30. #define    NULL    0
  31. #endif
  32.  
  33. #define UNK    '?'
  34.  
  35. static unsigned char
  36. intern2ascii[] = {  /* ISO 8859-1 Latin Alphabet 1 to US ASCII */
  37. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  38. UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  39.  32,  33,  99,  35,  36,  89, 124,  80,  34,  67,  97,  34, 126,  45,  82,  95,
  40. 111, UNK,  50,  51,  39, 117,  45,  45,  44,  49, 111,  34, UNK, UNK, UNK,  63,
  41.  65,  65,  65,  65,  65,  65,  65,  67,  69,  69,  69,  69,  73,  73,  73,  73,
  42.  68,  78,  79,  79,  79,  79,  79, 120,  79,  85,  85,  85,  85,  89,  84, 115,
  43.  97,  97,  97,  97,  97,  97,  97,  99, 101, 101, 101, 101, 105, 105, 105, 105,
  44. 100, 110, 111, 111, 111, 111, 111,  47, 111, 117, 117, 117, 117, 121, 116, 121
  45. };
  46.  
  47. static unsigned char
  48. intern2cp850[] = { /* ISO 8859-1 Latin Alphabet 1
  49.               (Latin-1) to IBM Code Page 850 */
  50. 186, 205, 201, 187, 200, 188, 204, 185, 203, 202, 206, 223, 220, 219, 254, 242,
  51. 179, 196, 218, 191, 192, 217, 195, 180, 194, 193, 197, 176, 177, 178, 213, 159,
  52. 255, 173, 189, 156, 207, 190, 221, 245, 249, 184, 166, 174, 170, 240, 169, 238,
  53. 248, 241, 253, 252, 239, 230, 244, 250, 247, 251, 167, 175, 172, 171, 243, 168,
  54. 183, 181, 182, 199, 142, 143, 146, 128, 212, 144, 210, 211, 222, 214, 215, 216,
  55. 209, 165, 227, 224, 226, 229, 153, 158, 157, 235, 233, 234, 154, 237, 232, 225,
  56. 133, 160, 131, 198, 132, 134, 145, 135, 138, 130, 136, 137, 141, 161, 140, 139,
  57. 208, 164, 149, 162, 147, 228, 148, 246, 155, 151, 163, 150, 129, 236, 231, 152
  58. };
  59.  
  60. static unsigned char
  61. cp8502intern[] = {  /* IBM Code Page 850 to Latin-1 */
  62. 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197,
  63. 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 248, 163, 216, 215, 159,
  64. 225, 237, 243, 250, 241, 209, 170, 186, 191, 174, 172, 189, 188, 161, 171, 187,
  65. 155, 156, 157, 144, 151, 193, 194, 192, 169, 135, 128, 131, 133, 162, 165, 147,
  66. 148, 153, 152, 150, 145, 154, 227, 195, 132, 130, 137, 136, 134, 129, 138, 164,
  67. 240, 208, 202, 203, 200, 158, 205, 206, 207, 149, 146, 141, 140, 166, 204, 139,
  68. 211, 223, 212, 210, 245, 213, 181, 254, 222, 218, 219, 217, 253, 221, 175, 180,
  69. 173, 177, 143, 190, 182, 167, 247, 184, 176, 168, 183, 185, 179, 178, 142, 160
  70. };
  71.  
  72. #ifndef MIT
  73. static unsigned char
  74. next2intern[] = { /* Nextstep char set to ISO 8859-1 Latin Alphabet 1 */
  75.  UNK, 0300, 0301, 0302, 0303, 0304, 0305, 0307, 0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  76. 0320, 0321, 0322, 0323, 0324, 0325, 0326, 0331, 0332, 0333, 0334, 0335, 0336, 0265, 0337, 0267,
  77. 0251, 0241, 0242, 0243, 0057, 0245, 0146, 0247, 0244, 0140, 0042, 0253, 0074, 0076,  UNK,  UNK,
  78. 0256, 0255,  UNK,  UNK, 0056, 0246, 0266, 0267, 0054, 0042, 0235, 0273,  UNK,  UNK, 0254, 0277,
  79. 0220, 0221, 0222, 0223, 0224, 0225, 0226, 0232, 0230, 0262, 0227, 0270, 0263, 0042, 0236, 0226,
  80. 0257, 0261, 0274, 0275, 0276, 0340, 0341, 0342, 0343, 0344, 0345, 0347, 0350, 0351, 0352, 0353,
  81. 0354, 0306, 0355, 0252, 0356, 0357, 0365, 0361, 0243, 0330,  UNK, 0272, 0362, 0363, 0364, 0365,
  82. 0366, 0346, 0371, 0372, 0373, 0151, 0374, 0375, 0154, 0370,  UNK, 0337, 0376, 0377, UNK, UNK
  83. };
  84.  
  85. static unsigned char
  86. intern2next[] = { /* ISO 8859-1 Latin Apphabet 1 (Latin-1) to NeXTSTEP Char set */
  87.  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,  UNK,
  88. 0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307, 0310,  UNK, 0312, 0313,  UNK, 0272, 0316, 0247,
  89.  UNK, 0241, 0242, 0243, 0250, 0245, 0265,  UNK, 0310, 0240, 0343, 0253, 0276, 0261, 0260, 0320,
  90. 0312, 0321, 0311, 0314, 0270, 0235, 0266, 0267, 0313, 0300,  UNK, 0273, 0322, 0323, 0324, 0277,
  91. 0201, 0202, 0203, 0204, 0205, 0206, 0341, 0207, 0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  92. 0220, 0221, 0222, 0223, 0224, 0225, 0226, 0236, 0351, 0227, 0230, 0231, 0232, 0233, 0234, 0373,
  93. 0325, 0326, 0327, 0330, 0331, 0332, 0361, 0333, 0334, 0335, 0336, 0337, 0340, 0342, 0344, 0345,
  94. 0346, 0347, 0354, 0355, 0356, 0357, 0360, 0237, 0371, 0362, 0363, 0364, 0366, 0367, 0374, 0375
  95. };
  96. #endif
  97.  
  98.  
  99. /* Russian language specific conversation section */
  100. /* Two point-to-point charset decode tables       */
  101. /* produced by Andrew A. Chernov                  */
  102. /* Decode single char from KOI8-R to ALT-CODES, if present */
  103. static unsigned char intern2alt[] = {
  104.     0xc4, 0xb3, 0xda, 0xbf, 0xc0, 0xd9, 0xc3, 0xb4,
  105.     0xc2, 0xc1, 0xc5, 0xdf, 0xdc, 0xdb, 0xdd, 0xde,
  106.     0xb0, 0xb1, 0xb2, 0xf4, 0xfe, 0xf9, 0xfb, 0xf7,
  107.     0xf3, 0xf2, 0xff, 0xf5, 0xf8, 0xfd, 0xfa, 0xf6,
  108.     0xcd, 0xba, 0xd5, 0xf1, 0xd6, 0xc9, 0xb8, 0xb7,
  109.     0xbb, 0xd4, 0xd3, 0xc8, 0xbe, 0xbd, 0xbc, 0xc6,
  110.     0xc7, 0xcc, 0xb5, 0xf0, 0xb6, 0xb9, 0xd1, 0xd2,
  111.     0xcb, 0xcf, 0xd0, 0xca, 0xd8, 0xd7, 0xce, 0xfc,
  112.     0xee, 0xa0, 0xa1, 0xe6, 0xa4, 0xa5, 0xe4, 0xa3,
  113.     0xe5, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae,
  114.     0xaf, 0xef, 0xe0, 0xe1, 0xe2, 0xe3, 0xa6, 0xa2,
  115.     0xec, 0xeb, 0xa7, 0xe8, 0xed, 0xe9, 0xe7, 0xea,
  116.     0x9e, 0x80, 0x81, 0x96, 0x84, 0x85, 0x94, 0x83,
  117.     0x95, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
  118.     0x8f, 0x9f, 0x90, 0x91, 0x92, 0x93, 0x86, 0x82,
  119.     0x9c, 0x9b, 0x87, 0x98, 0x9d, 0x99, 0x97, 0x9a
  120. };
  121.  
  122. /* Decode single char from ALT-CODES, if present, to KOI8-R */
  123. static unsigned char alt2intern[] = {
  124.     0xe1, 0xe2, 0xf7, 0xe7, 0xe4, 0xe5, 0xf6, 0xfa,
  125.     0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0,
  126.     0xf2, 0xf3, 0xf4, 0xf5, 0xe6, 0xe8, 0xe3, 0xfe,
  127.     0xfb, 0xfd, 0xff, 0xf9, 0xf8, 0xfc, 0xe0, 0xf1,
  128.     0xc1, 0xc2, 0xd7, 0xc7, 0xc4, 0xc5, 0xd6, 0xda,
  129.     0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
  130.     0x90, 0x91, 0x92, 0x81, 0x87, 0xb2, 0xb4, 0xa7,
  131.     0xa6, 0xb5, 0xa1, 0xa8, 0xae, 0xad, 0xac, 0x83,
  132.     0x84, 0x89, 0x88, 0x86, 0x80, 0x8a, 0xaf, 0xb0,
  133.     0xab, 0xa5, 0xbb, 0xb8, 0xb1, 0xa0, 0xbe, 0xb9,
  134.     0xba, 0xb6, 0xb7, 0xaa, 0xa9, 0xa2, 0xa4, 0xbd,
  135.     0xbc, 0x85, 0x82, 0x8d, 0x8c, 0x8e, 0x8f, 0x8b,
  136.     0xd2, 0xd3, 0xd4, 0xd5, 0xc6, 0xc8, 0xc3, 0xde,
  137.     0xdb, 0xdd, 0xdf, 0xd9, 0xd8, 0xdc, 0xc0, 0xd1,
  138.     0xb3, 0xa3, 0x99, 0x98, 0x93, 0x9b, 0x9f, 0x97,
  139.     0x9c, 0x95, 0x9e, 0x96, 0xbf, 0x9d, 0x94, 0x9a
  140. };
  141.  
  142. /*
  143.  * Most Unixes has KOI8, and DOS has ALT_CODE
  144.  * If your Unix is non-standard, set CHARSET to "alt_codes"
  145.  * in config.txt
  146.  */
  147.  
  148. #ifndef    DEFAULT_CSET
  149. #define    DEFAULT_CSET    "noconv"
  150. #endif
  151. #ifndef    DEFAULT_RU_CSET
  152. #define    DEFAULT_RU_CSET    "noconv"
  153. #endif
  154.  
  155. /* End of Russian section */
  156.  
  157. int CONVERSION = NO_CONV;      /* None text file conversion at start time */
  158.  
  159. unsigned char *ext_c_ptr;
  160. static unsigned char *int_c_ptr;
  161.  
  162. #ifdef MSDOS
  163. char charset[64] = "cp850";
  164. #else
  165. char charset[64] = "";
  166. #endif
  167.  
  168. void
  169. init_charset(void)
  170. {
  171.     ext_c_ptr = NULL;    /* NULL means latin1 or KOI8
  172.                    (internal format) */
  173.     int_c_ptr = NULL;
  174.  
  175.     if (charset[0] == '\0') {
  176.         /* use default character set for this system */
  177.         if (strcmp(language, "ru") == 0)
  178.             strcpy(charset, DEFAULT_RU_CSET);
  179.         else
  180.             strcpy(charset, DEFAULT_CSET);
  181.     } else {
  182.         strlwr(charset);
  183.     }
  184.  
  185.     /* latin-1 and KOI8 are in internal format: no conversion needed */
  186.     if (!strcmp(charset, "latin1") || !strcmp(charset, "koi8") ||
  187.         !strcmp(charset, "noconv"))
  188.         return;
  189.  
  190.     if (!strcmp(charset, "alt_codes")) {
  191.         ext_c_ptr = intern2alt;
  192.         int_c_ptr = alt2intern;
  193.     } else if (!strcmp(charset, "cp850")) {
  194.         ext_c_ptr = intern2cp850;
  195.         int_c_ptr = cp8502intern;
  196.     } else if (!strcmp(charset, "ascii")) {
  197.         ext_c_ptr = intern2ascii;
  198. #ifndef MIT
  199.     } else if (!strcmp(charset, "next"))  {
  200.             ext_c_ptr = intern2next;
  201.         int_c_ptr = next2intern;
  202. #endif
  203.     } else {
  204.         fprintf(stderr, LANG("Unsupported character set: '%s'\n"),
  205.             charset);
  206.     }
  207. }
  208.  
  209. char
  210. EXT_C(char c)
  211. {
  212.     if (c > '\0' || !ext_c_ptr)
  213.         return c;
  214.     return ext_c_ptr[c & 0x7f];
  215. }
  216.  
  217. char
  218. INT_C(char c)
  219. {
  220.     if (c > '\0' || !int_c_ptr)
  221.         return c;
  222.     return int_c_ptr[c & 0x7f];
  223. }
  224.  
  225. /*
  226.  * to_upper() and to_lower(), replacement for toupper() and tolower(),
  227.  * calling to_upper() on uppercase or to_lower on lowercase characters
  228.  * is handled correctly.
  229.  * 
  230.  * XXX: should handle local characterset when 8-bit userID's are allowed
  231.  */
  232. int
  233. to_upper(int c)
  234. {
  235.     return (c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c);
  236. }
  237.  
  238. int
  239. to_lower(int c)
  240. {
  241.     return (c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c);
  242. }
  243.