home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnurecod.zip / ebcdic.c < prev    next >
C/C++ Source or Header  |  1994-01-07  |  9KB  |  190 lines

  1. /* Conversion of files between different charsets and usages.
  2.    Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  3.    Francois Pinard <pinard@iro.umontreal.ca>, 1988.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful, but
  11.    WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #define ASCII "ASCII"
  21. /* Previously: #define ASCII "ASCII-BS" */
  22.  
  23. #include "recode.h"
  24.  
  25. static unsigned char const ascii_to_ebcdic[256] =
  26.   {
  27.       0,   1,   2,   3,  55,  45,  46,  47,     /*   0 -   7 */
  28.      22,   5,  37,  11,  12,  13,  14,  15,     /*   8 -  15 */
  29.      16,  17,  18,  19,  60,  61,  50,  38,     /*  16 -  23 */
  30.      24,  25,  63,  39,  28,  29,  30,  31,     /*  24 -  31 */
  31.      64,  79, 127, 123,  91, 108,  80, 125,     /*  32 -  39 */
  32.      77,  93,  92,  78, 107,  96,  75,  97,     /*  40 -  47 */
  33.     240, 241, 242, 243, 244, 245, 246, 247,     /*  48 -  55 */
  34.     248, 249, 122,  94,  76, 126, 110, 111,     /*  56 -  63 */
  35.     124, 193, 194, 195, 196, 197, 198, 199,     /*  64 -  71 */
  36.     200, 201, 209, 210, 211, 212, 213, 214,     /*  72 -  79 */
  37.     215, 216, 217, 226, 227, 228, 229, 230,     /*  80 -  87 */
  38.     231, 232, 233,  74, 224,  90,  95, 109,     /*  88 -  95 */
  39.     121, 129, 130, 131, 132, 133, 134, 135,     /*  96 - 103 */
  40.     136, 137, 145, 146, 147, 148, 149, 150,     /* 104 - 111 */
  41.     151, 152, 153, 162, 163, 164, 165, 166,     /* 112 - 119 */
  42.     167, 168, 169, 192, 106, 208, 161,   7,     /* 120 - 127 */
  43.      32,  33,  34,  35,  36,  21,   6,  23,     /* 128 - 135 */
  44.      40,  41,  42,  43,  44,   9,  10,  27,     /* 136 - 143 */
  45.      48,  49,  26,  51,  52,  53,  54,   8,     /* 144 - 151 */
  46.      56,  57,  58,  59,   4,  20,  62, 225,     /* 152 - 159 */
  47.      65,  66,  67,  68,  69,  70,  71,  72,     /* 160 - 167 */
  48.      73,  81,  82,  83,  84,  85,  86,  87,     /* 168 - 175 */
  49.      88,  89,  98,  99, 100, 101, 102, 103,     /* 176 - 183 */
  50.     104, 105, 112, 113, 114, 115, 116, 117,     /* 184 - 191 */
  51.     118, 119, 120, 128, 138, 139, 140, 141,     /* 192 - 199 */
  52.     142, 143, 144, 154, 155, 156, 157, 158,     /* 200 - 207 */
  53.     159, 160, 170, 171, 172, 173, 174, 175,     /* 208 - 215 */
  54.     176, 177, 178, 179, 180, 181, 182, 183,     /* 216 - 223 */
  55.     184, 185, 186, 187, 188, 189, 190, 191,     /* 224 - 231 */
  56.     202, 203, 204, 205, 206, 207, 218, 219,     /* 232 - 239 */
  57.     220, 221, 222, 223, 234, 235, 236, 237,     /* 240 - 247 */
  58.     238, 239, 250, 251, 252, 253, 254, 255,     /* 248 - 255 */
  59.   };
  60.  
  61. static unsigned char const ascii_to_ebcdic_ccc[256] =
  62.   {
  63.       0,   1,   2,   3,  55,  45,  46,  47,     /*   0 -   7 */
  64.      22,   5,  37,  11,  12,  13,  14,  15,     /*   8 -  15 */
  65.      16,  17,  18,  19,  60,  61,  50,  38,     /*  16 -  23 */
  66.      24,  25,  63,  39,  28,  29,  30,  31,     /*  24 -  31 */
  67.      64,  90, 127, 123,  91, 108,  80, 125,     /*  32 -  39 */
  68.      77,  93,  92,  78, 107,  96,  75,  97,     /*  40 -  47 */
  69.     240, 241, 242, 243, 244, 245, 246, 247,     /*  48 -  55 */
  70.     248, 249, 122,  94,  76, 126, 110, 111,     /*  56 -  63 */
  71.     124, 193, 194, 195, 196, 197, 198, 199,     /*  64 -  71 */
  72.     200, 201, 209, 210, 211, 212, 213, 214,     /*  72 -  79 */
  73.     215, 216, 217, 226, 227, 228, 229, 230,     /*  80 -  87 */
  74.     231, 232, 233,  74, 224,  79,  95, 109,     /*  88 -  95 */
  75.     121, 129, 130, 131, 132, 133, 134, 135,     /*  96 - 103 */
  76.     136, 137, 145, 146, 147, 148, 149, 150,     /* 104 - 111 */
  77.     151, 152, 153, 162, 163, 164, 165, 166,     /* 112 - 119 */
  78.     167, 168, 169, 192, 106, 208, 161,   7,     /* 120 - 127 */
  79.       4,   6,   8,   9,  10,  20,  21,  23,     /* 128 - 135 */
  80.      26,  27, 138, 139, 140, 141, 142, 143,     /* 136 - 143 */
  81.      32,  33,  34,  35,  36,  40,  41,  42,     /* 144 - 151 */
  82.      43,  44, 154, 155, 156, 157, 158, 159,     /* 152 - 159 */
  83.      48,  49,  51,  52,  53,  54,  56,  57,     /* 160 - 167 */
  84.      58,  59, 170, 171, 172, 173, 174, 175,     /* 168 - 175 */
  85.     176, 177, 178, 179, 180, 181, 182, 183,     /* 176 - 183 */
  86.     184, 185, 186, 187, 188, 189, 190, 191,     /* 184 - 191 */
  87.     128,  65,  66,  67,  68,  69,  70,  71,     /* 192 - 199 */
  88.      72,  73, 202, 203, 204, 205, 206, 207,     /* 200 - 207 */
  89.     144,  81,  82,  83,  84,  85,  86,  87,     /* 208 - 215 */
  90.      88,  89, 218, 219, 220, 221, 222, 223,     /* 216 - 223 */
  91.     160, 225,  98,  99, 100, 101, 102, 103,     /* 224 - 231 */
  92.     104, 105, 234, 235, 236, 237, 238, 239,     /* 232 - 239 */
  93.     112, 113, 114, 115, 116, 117, 118, 119,     /* 240 - 247 */
  94.     120,  62, 250, 251, 252, 253, 254, 255,     /* 248 - 255 */
  95.   };
  96.  
  97. /* This is almost identical to GNU dd's "ibm" table.  In dd's "ibm"
  98.    table, both 91 and 213 recode to 173, both 93 and 229 recode to 189,
  99.    and no character recodes to 74 or 106.  I suspect two errors in there.
  100.    So, I arbitrarily choose to recode 213 by 74 and 229 by 106.  */
  101.  
  102. static unsigned char const ascii_to_ebcdic_ibm[256] =
  103.   {
  104.       0,   1,   2,   3,  55,  45,  46,  47,     /*   0 -   7 */
  105.      22,   5,  37,  11,  12,  13,  14,  15,     /*   8 -  15 */
  106.      16,  17,  18,  19,  60,  61,  50,  38,     /*  16 -  23 */
  107.      24,  25,  63,  39,  28,  29,  30,  31,     /*  24 -  31 */
  108.      64,  90, 127, 123,  91, 108,  80, 125,     /*  32 -  39 */
  109.      77,  93,  92,  78, 107,  96,  75,  97,     /*  40 -  47 */
  110.     240, 241, 242, 243, 244, 245, 246, 247,     /*  48 -  55 */
  111.     248, 249, 122,  94,  76, 126, 110, 111,     /*  56 -  63 */
  112.     124, 193, 194, 195, 196, 197, 198, 199,     /*  64 -  71 */
  113.     200, 201, 209, 210, 211, 212, 213, 214,     /*  72 -  79 */
  114.     215, 216, 217, 226, 227, 228, 229, 230,     /*  80 -  87 */
  115.     231, 232, 233, 173, 224, 189,  95, 109,     /*  88 -  95 */
  116.     121, 129, 130, 131, 132, 133, 134, 135,     /*  96 - 103 */
  117.     136, 137, 145, 146, 147, 148, 149, 150,     /* 104 - 111 */
  118.     151, 152, 153, 162, 163, 164, 165, 166,     /* 112 - 119 */
  119.     167, 168, 169, 192,  79, 208, 161,   7,     /* 120 - 127 */
  120.      32,  33,  34,  35,  36,  21,   6,  23,     /* 128 - 135 */
  121.      40,  41,  42,  43,  44,   9,  10,  27,     /* 136 - 143 */
  122.      48,  49,  26,  51,  52,  53,  54,   8,     /* 144 - 151 */
  123.      56,  57,  58,  59,   4,  20,  62, 225,     /* 152 - 159 */
  124.      65,  66,  67,  68,  69,  70,  71,  72,     /* 160 - 167 */
  125.      73,  81,  82,  83,  84,  85,  86,  87,     /* 168 - 175 */
  126.      88,  89,  98,  99, 100, 101, 102, 103,     /* 176 - 183 */
  127.     104, 105, 112, 113, 114, 115, 116, 117,     /* 184 - 191 */
  128.     118, 119, 120, 128, 138, 139, 140, 141,     /* 192 - 199 */
  129.     142, 143, 144, 154, 155, 156, 157, 158,     /* 200 - 207 */
  130.     159, 160, 170, 171, 172,  74, 174, 175,     /* 208 - 215 */
  131.     176, 177, 178, 179, 180, 181, 182, 183,     /* 216 - 223 */
  132.     184, 185, 186, 187, 188, 106, 190, 191,     /* 224 - 231 */
  133.     202, 203, 204, 205, 206, 207, 218, 219,     /* 232 - 239 */
  134.     220, 221, 222, 223, 234, 235, 236, 237,     /* 240 - 247 */
  135.     238, 239, 250, 251, 252, 253, 254, 255,     /* 248 - 255 */
  136.   };
  137.  
  138. static void
  139. init_ascii_ebcdic (STEP *step)
  140. {
  141.   step->one_to_one = ascii_to_ebcdic;
  142. }
  143.  
  144. static void
  145. init_ebcdic_ascii (STEP *step)
  146. {
  147.   step->one_to_one = invert_table (ascii_to_ebcdic);
  148. }
  149.  
  150. static void
  151. init_ascii_ebcdic_ccc (STEP *step)
  152. {
  153.   step->one_to_one = ascii_to_ebcdic_ccc;
  154. }
  155.  
  156. static void
  157. init_ebcdic_ccc_ascii (STEP *step)
  158. {
  159.   step->one_to_one = invert_table (ascii_to_ebcdic_ccc);
  160. }
  161.  
  162. static void
  163. init_ascii_ebcdic_ibm (STEP *step)
  164. {
  165.   step->one_to_one = ascii_to_ebcdic_ibm;
  166. }
  167.  
  168. static void
  169. init_ebcdic_ibm_ascii (STEP *step)
  170. {
  171.   step->one_to_one = invert_table (ascii_to_ebcdic_ibm);
  172. }
  173.  
  174. void
  175. module_ebcdic (void)
  176. {
  177.   declare_step (ASCII, "EBCDIC", REVERSIBLE, init_ascii_ebcdic,
  178.         file_one_to_one);
  179.   declare_step ("EBCDIC", ASCII, REVERSIBLE, init_ebcdic_ascii,
  180.         file_one_to_one);
  181.   declare_step (ASCII, "EBCDIC-CCC", REVERSIBLE, init_ascii_ebcdic_ccc,
  182.         file_one_to_one);
  183.   declare_step ("EBCDIC-CCC", ASCII, REVERSIBLE, init_ebcdic_ccc_ascii,
  184.         file_one_to_one);
  185.   declare_step (ASCII, "EBCDIC-IBM", REVERSIBLE, init_ascii_ebcdic_ibm,
  186.         file_one_to_one);
  187.   declare_step ("EBCDIC-IBM", ASCII, REVERSIBLE, init_ebcdic_ibm_ascii,
  188.         file_one_to_one);
  189. }
  190.