home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GRECODE / EBCDIC.C < prev    next >
C/C++ Source or Header  |  1993-12-07  |  9KB  |  187 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. #include "recode.h"
  21.  
  22. static unsigned char const ascii_to_ebcdic[256] =
  23.   {
  24.       0,   1,   2,   3,  55,  45,  46,  47,     /*   0 -   7 */
  25.      22,   5,  37,  11,  12,  13,  14,  15,     /*   8 -  15 */
  26.      16,  17,  18,  19,  60,  61,  50,  38,     /*  16 -  23 */
  27.      24,  25,  63,  39,  28,  29,  30,  31,     /*  24 -  31 */
  28.      64,  79, 127, 123,  91, 108,  80, 125,     /*  32 -  39 */
  29.      77,  93,  92,  78, 107,  96,  75,  97,     /*  40 -  47 */
  30.     240, 241, 242, 243, 244, 245, 246, 247,     /*  48 -  55 */
  31.     248, 249, 122,  94,  76, 126, 110, 111,     /*  56 -  63 */
  32.     124, 193, 194, 195, 196, 197, 198, 199,     /*  64 -  71 */
  33.     200, 201, 209, 210, 211, 212, 213, 214,     /*  72 -  79 */
  34.     215, 216, 217, 226, 227, 228, 229, 230,     /*  80 -  87 */
  35.     231, 232, 233,  74, 224,  90,  95, 109,     /*  88 -  95 */
  36.     121, 129, 130, 131, 132, 133, 134, 135,     /*  96 - 103 */
  37.     136, 137, 145, 146, 147, 148, 149, 150,     /* 104 - 111 */
  38.     151, 152, 153, 162, 163, 164, 165, 166,     /* 112 - 119 */
  39.     167, 168, 169, 192, 106, 208, 161,   7,     /* 120 - 127 */
  40.      32,  33,  34,  35,  36,  21,   6,  23,     /* 128 - 135 */
  41.      40,  41,  42,  43,  44,   9,  10,  27,     /* 136 - 143 */
  42.      48,  49,  26,  51,  52,  53,  54,   8,     /* 144 - 151 */
  43.      56,  57,  58,  59,   4,  20,  62, 225,     /* 152 - 159 */
  44.      65,  66,  67,  68,  69,  70,  71,  72,     /* 160 - 167 */
  45.      73,  81,  82,  83,  84,  85,  86,  87,     /* 168 - 175 */
  46.      88,  89,  98,  99, 100, 101, 102, 103,     /* 176 - 183 */
  47.     104, 105, 112, 113, 114, 115, 116, 117,     /* 184 - 191 */
  48.     118, 119, 120, 128, 138, 139, 140, 141,     /* 192 - 199 */
  49.     142, 143, 144, 154, 155, 156, 157, 158,     /* 200 - 207 */
  50.     159, 160, 170, 171, 172, 173, 174, 175,     /* 208 - 215 */
  51.     176, 177, 178, 179, 180, 181, 182, 183,     /* 216 - 223 */
  52.     184, 185, 186, 187, 188, 189, 190, 191,     /* 224 - 231 */
  53.     202, 203, 204, 205, 206, 207, 218, 219,     /* 232 - 239 */
  54.     220, 221, 222, 223, 234, 235, 236, 237,     /* 240 - 247 */
  55.     238, 239, 250, 251, 252, 253, 254, 255,     /* 248 - 255 */
  56.   };
  57.  
  58. static unsigned char const ascii_to_ebcdic_ccc[256] =
  59.   {
  60.       0,   1,   2,   3,  55,  45,  46,  47,     /*   0 -   7 */
  61.      22,   5,  37,  11,  12,  13,  14,  15,     /*   8 -  15 */
  62.      16,  17,  18,  19,  60,  61,  50,  38,     /*  16 -  23 */
  63.      24,  25,  63,  39,  28,  29,  30,  31,     /*  24 -  31 */
  64.      64,  90, 127, 123,  91, 108,  80, 125,     /*  32 -  39 */
  65.      77,  93,  92,  78, 107,  96,  75,  97,     /*  40 -  47 */
  66.     240, 241, 242, 243, 244, 245, 246, 247,     /*  48 -  55 */
  67.     248, 249, 122,  94,  76, 126, 110, 111,     /*  56 -  63 */
  68.     124, 193, 194, 195, 196, 197, 198, 199,     /*  64 -  71 */
  69.     200, 201, 209, 210, 211, 212, 213, 214,     /*  72 -  79 */
  70.     215, 216, 217, 226, 227, 228, 229, 230,     /*  80 -  87 */
  71.     231, 232, 233,  74, 224,  79,  95, 109,     /*  88 -  95 */
  72.     121, 129, 130, 131, 132, 133, 134, 135,     /*  96 - 103 */
  73.     136, 137, 145, 146, 147, 148, 149, 150,     /* 104 - 111 */
  74.     151, 152, 153, 162, 163, 164, 165, 166,     /* 112 - 119 */
  75.     167, 168, 169, 192, 106, 208, 161,   7,     /* 120 - 127 */
  76.       4,   6,   8,   9,  10,  20,  21,  23,     /* 128 - 135 */
  77.      26,  27, 138, 139, 140, 141, 142, 143,     /* 136 - 143 */
  78.      32,  33,  34,  35,  36,  40,  41,  42,     /* 144 - 151 */
  79.      43,  44, 154, 155, 156, 157, 158, 159,     /* 152 - 159 */
  80.      48,  49,  51,  52,  53,  54,  56,  57,     /* 160 - 167 */
  81.      58,  59, 170, 171, 172, 173, 174, 175,     /* 168 - 175 */
  82.     176, 177, 178, 179, 180, 181, 182, 183,     /* 176 - 183 */
  83.     184, 185, 186, 187, 188, 189, 190, 191,     /* 184 - 191 */
  84.     128,  65,  66,  67,  68,  69,  70,  71,     /* 192 - 199 */
  85.      72,  73, 202, 203, 204, 205, 206, 207,     /* 200 - 207 */
  86.     144,  81,  82,  83,  84,  85,  86,  87,     /* 208 - 215 */
  87.      88,  89, 218, 219, 220, 221, 222, 223,     /* 216 - 223 */
  88.     160, 225,  98,  99, 100, 101, 102, 103,     /* 224 - 231 */
  89.     104, 105, 234, 235, 236, 237, 238, 239,     /* 232 - 239 */
  90.     112, 113, 114, 115, 116, 117, 118, 119,     /* 240 - 247 */
  91.     120,  62, 250, 251, 252, 253, 254, 255,     /* 248 - 255 */
  92.   };
  93.  
  94. /* This is almost identical to GNU dd's "ibm" table.  In dd's "ibm"
  95.    table, both 91 and 213 recode to 173, both 93 and 229 recode to 189,
  96.    and no character recodes to 74 or 106.  I suspect two errors in there.
  97.    So, I arbitrarily choose to recode 213 by 74 and 229 by 106.  */
  98.  
  99. static unsigned char const ascii_to_ebcdic_ibm[256] =
  100.   {
  101.       0,   1,   2,   3,  55,  45,  46,  47,     /*   0 -   7 */
  102.      22,   5,  37,  11,  12,  13,  14,  15,     /*   8 -  15 */
  103.      16,  17,  18,  19,  60,  61,  50,  38,     /*  16 -  23 */
  104.      24,  25,  63,  39,  28,  29,  30,  31,     /*  24 -  31 */
  105.      64,  90, 127, 123,  91, 108,  80, 125,     /*  32 -  39 */
  106.      77,  93,  92,  78, 107,  96,  75,  97,     /*  40 -  47 */
  107.     240, 241, 242, 243, 244, 245, 246, 247,     /*  48 -  55 */
  108.     248, 249, 122,  94,  76, 126, 110, 111,     /*  56 -  63 */
  109.     124, 193, 194, 195, 196, 197, 198, 199,     /*  64 -  71 */
  110.     200, 201, 209, 210, 211, 212, 213, 214,     /*  72 -  79 */
  111.     215, 216, 217, 226, 227, 228, 229, 230,     /*  80 -  87 */
  112.     231, 232, 233, 173, 224, 189,  95, 109,     /*  88 -  95 */
  113.     121, 129, 130, 131, 132, 133, 134, 135,     /*  96 - 103 */
  114.     136, 137, 145, 146, 147, 148, 149, 150,     /* 104 - 111 */
  115.     151, 152, 153, 162, 163, 164, 165, 166,     /* 112 - 119 */
  116.     167, 168, 169, 192,  79, 208, 161,   7,     /* 120 - 127 */
  117.      32,  33,  34,  35,  36,  21,   6,  23,     /* 128 - 135 */
  118.      40,  41,  42,  43,  44,   9,  10,  27,     /* 136 - 143 */
  119.      48,  49,  26,  51,  52,  53,  54,   8,     /* 144 - 151 */
  120.      56,  57,  58,  59,   4,  20,  62, 225,     /* 152 - 159 */
  121.      65,  66,  67,  68,  69,  70,  71,  72,     /* 160 - 167 */
  122.      73,  81,  82,  83,  84,  85,  86,  87,     /* 168 - 175 */
  123.      88,  89,  98,  99, 100, 101, 102, 103,     /* 176 - 183 */
  124.     104, 105, 112, 113, 114, 115, 116, 117,     /* 184 - 191 */
  125.     118, 119, 120, 128, 138, 139, 140, 141,     /* 192 - 199 */
  126.     142, 143, 144, 154, 155, 156, 157, 158,     /* 200 - 207 */
  127.     159, 160, 170, 171, 172,  74, 174, 175,     /* 208 - 215 */
  128.     176, 177, 178, 179, 180, 181, 182, 183,     /* 216 - 223 */
  129.     184, 185, 186, 187, 188, 106, 190, 191,     /* 224 - 231 */
  130.     202, 203, 204, 205, 206, 207, 218, 219,     /* 232 - 239 */
  131.     220, 221, 222, 223, 234, 235, 236, 237,     /* 240 - 247 */
  132.     238, 239, 250, 251, 252, 253, 254, 255,     /* 248 - 255 */
  133.   };
  134.  
  135. static void
  136. init_ascii_ebcdic (STEP *step)
  137. {
  138.   step->one_to_one = ascii_to_ebcdic;
  139. }
  140.  
  141. static void
  142. init_ebcdic_ascii (STEP *step)
  143. {
  144.   step->one_to_one = invert_table (ascii_to_ebcdic);
  145. }
  146.  
  147. static void
  148. init_ascii_ebcdic_ccc (STEP *step)
  149. {
  150.   step->one_to_one = ascii_to_ebcdic_ccc;
  151. }
  152.  
  153. static void
  154. init_ebcdic_ccc_ascii (STEP *step)
  155. {
  156.   step->one_to_one = invert_table (ascii_to_ebcdic_ccc);
  157. }
  158.  
  159. static void
  160. init_ascii_ebcdic_ibm (STEP *step)
  161. {
  162.   step->one_to_one = ascii_to_ebcdic_ibm;
  163. }
  164.  
  165. static void
  166. init_ebcdic_ibm_ascii (STEP *step)
  167. {
  168.   step->one_to_one = invert_table (ascii_to_ebcdic_ibm);
  169. }
  170.  
  171. void
  172. module_ebcdic (void)
  173. {
  174.   declare_step ("ascii-bs", "ebcdic", REVERSIBLE, init_ascii_ebcdic,
  175.         file_one_to_one);
  176.   declare_step ("ebcdic", "ascii-bs", REVERSIBLE, init_ebcdic_ascii,
  177.         file_one_to_one);
  178.   declare_step ("ascii-bs", "ebcdic-ccc", REVERSIBLE, init_ascii_ebcdic_ccc,
  179.         file_one_to_one);
  180.   declare_step ("ebcdic-ccc", "ascii-bs", REVERSIBLE, init_ebcdic_ccc_ascii,
  181.         file_one_to_one);
  182.   declare_step ("ascii-bs", "ebcdic-ibm", REVERSIBLE, init_ascii_ebcdic_ibm,
  183.         file_one_to_one);
  184.   declare_step ("ebcdic-ibm", "ascii-bs", REVERSIBLE, init_ebcdic_ibm_ascii,
  185.         file_one_to_one);
  186. }
  187.