Debian Codegraph @

src/ebcdic.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1988.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #define ASCII "ASCII"
21 /* Previously: #define ASCII "ASCII-BS" */
22
23 #include "config.h"
24 #include "common.h"
25 #include "decsteps.h"
26
27 static unsigned char const ascii_to_ebcdic[256] =
28 {
29 0, 1, 2, 3, 55, 45, 46, 47, /* 0 - 7 */
30 22, 5, 37, 11, 12, 13, 14, 15, /* 8 - 15 */
31 16, 17, 18, 19, 60, 61, 50, 38, /* 16 - 23 */
32 24, 25, 63, 39, 28, 29, 30, 31, /* 24 - 31 */
33 64, 79, 127, 123, 91, 108, 80, 125, /* 32 - 39 */
34 77, 93, 92, 78, 107, 96, 75, 97, /* 40 - 47 */
35 240, 241, 242, 243, 244, 245, 246, 247, /* 48 - 55 */
36 248, 249, 122, 94, 76, 126, 110, 111, /* 56 - 63 */
37 124, 193, 194, 195, 196, 197, 198, 199, /* 64 - 71 */
38 200, 201, 209, 210, 211, 212, 213, 214, /* 72 - 79 */
39 215, 216, 217, 226, 227, 228, 229, 230, /* 80 - 87 */
40 231, 232, 233, 74, 224, 90, 95, 109, /* 88 - 95 */
41 121, 129, 130, 131, 132, 133, 134, 135, /* 96 - 103 */
42 136, 137, 145, 146, 147, 148, 149, 150, /* 104 - 111 */
43 151, 152, 153, 162, 163, 164, 165, 166, /* 112 - 119 */
44 167, 168, 169, 192, 106, 208, 161, 7, /* 120 - 127 */
45 32, 33, 34, 35, 36, 21, 6, 23, /* 128 - 135 */
46 40, 41, 42, 43, 44, 9, 10, 27, /* 136 - 143 */
47 48, 49, 26, 51, 52, 53, 54, 8, /* 144 - 151 */
48 56, 57, 58, 59, 4, 20, 62, 225, /* 152 - 159 */
49 65, 66, 67, 68, 69, 70, 71, 72, /* 160 - 167 */
50 73, 81, 82, 83, 84, 85, 86, 87, /* 168 - 175 */
51 88, 89, 98, 99, 100, 101, 102, 103, /* 176 - 183 */
52 104, 105, 112, 113, 114, 115, 116, 117, /* 184 - 191 */
53 118, 119, 120, 128, 138, 139, 140, 141, /* 192 - 199 */
54 142, 143, 144, 154, 155, 156, 157, 158, /* 200 - 207 */
55 159, 160, 170, 171, 172, 173, 174, 175, /* 208 - 215 */
56 176, 177, 178, 179, 180, 181, 182, 183, /* 216 - 223 */
57 184, 185, 186, 187, 188, 189, 190, 191, /* 224 - 231 */
58 202, 203, 204, 205, 206, 207, 218, 219, /* 232 - 239 */
59 220, 221, 222, 223, 234, 235, 236, 237, /* 240 - 247 */
60 238, 239, 250, 251, 252, 253, 254, 255, /* 248 - 255 */
61 };
62
63 static unsigned char const ascii_to_ebcdic_ccc[256] =
64 {
65 0, 1, 2, 3, 55, 45, 46, 47, /* 0 - 7 */
66 22, 5, 37, 11, 12, 13, 14, 15, /* 8 - 15 */
67 16, 17, 18, 19, 60, 61, 50, 38, /* 16 - 23 */
68 24, 25, 63, 39, 28, 29, 30, 31, /* 24 - 31 */
69 64, 90, 127, 123, 91, 108, 80, 125, /* 32 - 39 */
70 77, 93, 92, 78, 107, 96, 75, 97, /* 40 - 47 */
71 240, 241, 242, 243, 244, 245, 246, 247, /* 48 - 55 */
72 248, 249, 122, 94, 76, 126, 110, 111, /* 56 - 63 */
73 124, 193, 194, 195, 196, 197, 198, 199, /* 64 - 71 */
74 200, 201, 209, 210, 211, 212, 213, 214, /* 72 - 79 */
75 215, 216, 217, 226, 227, 228, 229, 230, /* 80 - 87 */
76 231, 232, 233, 74, 224, 79, 95, 109, /* 88 - 95 */
77 121, 129, 130, 131, 132, 133, 134, 135, /* 96 - 103 */
78 136, 137, 145, 146, 147, 148, 149, 150, /* 104 - 111 */
79 151, 152, 153, 162, 163, 164, 165, 166, /* 112 - 119 */
80 167, 168, 169, 192, 106, 208, 161, 7, /* 120 - 127 */
81 4, 6, 8, 9, 10, 20, 21, 23, /* 128 - 135 */
82 26, 27, 138, 139, 140, 141, 142, 143, /* 136 - 143 */
83 32, 33, 34, 35, 36, 40, 41, 42, /* 144 - 151 */
84 43, 44, 154, 155, 156, 157, 158, 159, /* 152 - 159 */
85 48, 49, 51, 52, 53, 54, 56, 57, /* 160 - 167 */
86 58, 59, 170, 171, 172, 173, 174, 175, /* 168 - 175 */
87 176, 177, 178, 179, 180, 181, 182, 183, /* 176 - 183 */
88 184, 185, 186, 187, 188, 189, 190, 191, /* 184 - 191 */
89 128, 65, 66, 67, 68, 69, 70, 71, /* 192 - 199 */
90 72, 73, 202, 203, 204, 205, 206, 207, /* 200 - 207 */
91 144, 81, 82, 83, 84, 85, 86, 87, /* 208 - 215 */
92 88, 89, 218, 219, 220, 221, 222, 223, /* 216 - 223 */
93 160, 225, 98, 99, 100, 101, 102, 103, /* 224 - 231 */
94 104, 105, 234, 235, 236, 237, 238, 239, /* 232 - 239 */
95 112, 113, 114, 115, 116, 117, 118, 119, /* 240 - 247 */
96 120, 62, 250, 251, 252, 253, 254, 255, /* 248 - 255 */
97 };
98
99 /* This is almost identical to GNU dd's "ibm" table. In dd's "ibm"
100 table, both 91 and 213 recode to 173, both 93 and 229 recode to 189,
101 and no character recodes to 74 or 106. I suspect two errors in there.
102 So, I arbitrarily choose to recode 213 by 74 and 229 by 106. */
103
104 static unsigned char const ascii_to_ebcdic_ibm[256] =
105 {
106 0, 1, 2, 3, 55, 45, 46, 47, /* 0 - 7 */
107 22, 5, 37, 11, 12, 13, 14, 15, /* 8 - 15 */
108 16, 17, 18, 19, 60, 61, 50, 38, /* 16 - 23 */
109 24, 25, 63, 39, 28, 29, 30, 31, /* 24 - 31 */
110 64, 90, 127, 123, 91, 108, 80, 125, /* 32 - 39 */
111 77, 93, 92, 78, 107, 96, 75, 97, /* 40 - 47 */
112 240, 241, 242, 243, 244, 245, 246, 247, /* 48 - 55 */
113 248, 249, 122, 94, 76, 126, 110, 111, /* 56 - 63 */
114 124, 193, 194, 195, 196, 197, 198, 199, /* 64 - 71 */
115 200, 201, 209, 210, 211, 212, 213, 214, /* 72 - 79 */
116 215, 216, 217, 226, 227, 228, 229, 230, /* 80 - 87 */
117 231, 232, 233, 173, 224, 189, 95, 109, /* 88 - 95 */
118 121, 129, 130, 131, 132, 133, 134, 135, /* 96 - 103 */
119 136, 137, 145, 146, 147, 148, 149, 150, /* 104 - 111 */
120 151, 152, 153, 162, 163, 164, 165, 166, /* 112 - 119 */
121 167, 168, 169, 192, 79, 208, 161, 7, /* 120 - 127 */
122 32, 33, 34, 35, 36, 21, 6, 23, /* 128 - 135 */
123 40, 41, 42, 43, 44, 9, 10, 27, /* 136 - 143 */
124 48, 49, 26, 51, 52, 53, 54, 8, /* 144 - 151 */
125 56, 57, 58, 59, 4, 20, 62, 225, /* 152 - 159 */
126 65, 66, 67, 68, 69, 70, 71, 72, /* 160 - 167 */
127 73, 81, 82, 83, 84, 85, 86, 87, /* 168 - 175 */
128 88, 89, 98, 99, 100, 101, 102, 103, /* 176 - 183 */
129 104, 105, 112, 113, 114, 115, 116, 117, /* 184 - 191 */
130 118, 119, 120, 128, 138, 139, 140, 141, /* 192 - 199 */
131 142, 143, 144, 154, 155, 156, 157, 158, /* 200 - 207 */
132 159, 160, 170, 171, 172, 74, 174, 175, /* 208 - 215 */
133 176, 177, 178, 179, 180, 181, 182, 183, /* 216 - 223 */
134 184, 185, 186, 187, 188, 106, 190, 191, /* 224 - 231 */
135 202, 203, 204, 205, 206, 207, 218, 219, /* 232 - 239 */
136 220, 221, 222, 223, 234, 235, 236, 237, /* 240 - 247 */
137 238, 239, 250, 251, 252, 253, 254, 255, /* 248 - 255 */
138 };
139
140 static bool
141 init_ascii_ebcdic (RECODE_STEP step,
142 RECODE_CONST_REQUEST request _GL_UNUSED,
143 RECODE_CONST_OPTION_LIST before_options,
144 RECODE_CONST_OPTION_LIST after_options)
145 {
146 if (before_options || after_options)
147 return false;
148
149 step->step_type = RECODE_BYTE_TO_BYTE;
150 /* The cast is a way to silently discard the const. */
151 step->step_table = (void *) ascii_to_ebcdic;
152 return true;
153 }
154
155 static bool
156 init_ebcdic_ascii (RECODE_STEP step,
157 RECODE_CONST_REQUEST request,
158 RECODE_CONST_OPTION_LIST before_options,
159 RECODE_CONST_OPTION_LIST after_options)
160 {
161 if (before_options || after_options)
162 return false;
163
164 if (step->step_table = recode_invert_table (request->outer, ascii_to_ebcdic),
165 step->step_table)
166 {
167 step->step_type = RECODE_BYTE_TO_BYTE;
168 step->step_table_term_routine = free;
169 return true;
170 }
171 else
172 return false;
173 }
174
175 static bool
176 init_ascii_ebcdic_ccc (RECODE_STEP step,
177 RECODE_CONST_REQUEST request _GL_UNUSED,
178 RECODE_CONST_OPTION_LIST before_options,
179 RECODE_CONST_OPTION_LIST after_options)
180 {
181 if (before_options || after_options)
182 return false;
183
184 step->step_type = RECODE_BYTE_TO_BYTE;
185 /* The cast is a way to silently discard the const. */
186 step->step_table = (void *) ascii_to_ebcdic_ccc;
187 return true;
188 }
189
190 static bool
191 init_ebcdic_ccc_ascii (RECODE_STEP step,
192 RECODE_CONST_REQUEST request,
193 RECODE_CONST_OPTION_LIST before_options,
194 RECODE_CONST_OPTION_LIST after_options)
195
196 {
197 if (before_options || after_options)
198 return false;
199
200 if (step->step_table = recode_invert_table (request->outer, ascii_to_ebcdic_ccc),
201 step->step_table)
202 {
203 step->step_type = RECODE_BYTE_TO_BYTE;
204 step->step_table_term_routine = free;
205 return true;
206 }
207 else
208 return false;
209 }
210
211 static bool
212 init_ascii_ebcdic_ibm (RECODE_STEP step,
213 RECODE_CONST_REQUEST request _GL_UNUSED,
214 RECODE_CONST_OPTION_LIST before_options,
215 RECODE_CONST_OPTION_LIST after_options)
216 {
217 if (before_options || after_options)
218 return false;
219
220 step->step_type = RECODE_BYTE_TO_BYTE;
221 /* The cast is a way to silently discard the const. */
222 step->step_table = (void *) ascii_to_ebcdic_ibm;
223 return true;
224 }
225
226 static bool
227 init_ebcdic_ibm_ascii (RECODE_STEP step,
228 RECODE_CONST_REQUEST request,
229 RECODE_CONST_OPTION_LIST before_options,
230 RECODE_CONST_OPTION_LIST after_options)
231 {
232 if (before_options || after_options)
233 return false;
234
235 if (step->step_table = recode_invert_table (request->outer, ascii_to_ebcdic_ibm),
236 step->step_table)
237 {
238 step->step_type = RECODE_BYTE_TO_BYTE;
239 step->step_table_term_routine = free;
240 return true;
241 }
242 else
243 return false;
244 }
245
246 bool
247 module_ebcdic (RECODE_OUTER outer)
248 {
249 return
250 recode_declare_single (outer, ASCII, "EBCDIC",
251 outer->quality_byte_reversible,
252 init_ascii_ebcdic, recode_transform_byte_to_byte)
253 && recode_declare_single (outer, "EBCDIC", ASCII,
254 outer->quality_byte_reversible,
255 init_ebcdic_ascii, recode_transform_byte_to_byte)
256 && recode_declare_single (outer, ASCII, "EBCDIC-CCC",
257 outer->quality_byte_reversible,
258 init_ascii_ebcdic_ccc, recode_transform_byte_to_byte)
259 && recode_declare_single (outer, "EBCDIC-CCC", ASCII,
260 outer->quality_byte_reversible,
261 init_ebcdic_ccc_ascii, recode_transform_byte_to_byte)
262 && recode_declare_single (outer, ASCII, "EBCDIC-IBM",
263 outer->quality_byte_reversible,
264 init_ascii_ebcdic_ibm, recode_transform_byte_to_byte)
265 && recode_declare_single (outer, "EBCDIC-IBM", ASCII,
266 outer->quality_byte_reversible,
267 init_ebcdic_ibm_ascii, recode_transform_byte_to_byte);
268 }
269
270 void
271 delmodule_ebcdic (_GL_UNUSED RECODE_OUTER outer)
272 {
273 }