1 /*
2     $Id: encodings.c 2465 2021-03-06 19:58:00Z soci $
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18 */
19 #include "encodings.h"
20 #include <string.h>
21 #include "inttypes.h"
22 #include "64tass.h"
23 #include "encoding.h"
24 #include "str.h"
25 
26 struct translate_table_s {
27     uint16_t start;
28     uint8_t length;
29     uint8_t offset;
30 };
31 
32 static const struct translate_table_s no_trans[] = {
33     {0x0000, 255, 0x00},
34 };
35 
36 static const struct translate_table_s petscii_trans[] = {
37     {0x0020,  32, 0x20}, /*  -@ */
38     {0x0041,  25, 0xc1}, /* A-Z */
39     {0x005b,   0, 0x5b}, /* [ */
40     {0x005d,   0, 0x5d}, /* ] */
41     {0x0061,  25, 0x41}, /* a-z */
42     {0x00a3,   0, 0x5c}, /* £ */
43     {0x03c0,   0, 0xff}, /* π */
44     {0x2190,   0, 0x5f}, /* ← */
45     {0x2191,   0, 0x5e}, /* ↑ */
46     {0x2500,   0, 0xc0}, /* ─ */
47     {0x2502,   0, 0xdd}, /* │ */
48     {0x250c,   0, 0xb0}, /* ┌ */
49     {0x2510,   0, 0xae}, /* ┐ */
50     {0x2514,   0, 0xad}, /* └ */
51     {0x2518,   0, 0xbd}, /* ┘ */
52     {0x251c,   0, 0xab}, /* ├ */
53     {0x2524,   0, 0xb3}, /* ┤ */
54     {0x252c,   0, 0xb2}, /* ┬ */
55     {0x2534,   0, 0xb1}, /* ┴ */
56     {0x253c,   0, 0xdb}, /* ┼ */
57     {0x256d,   0, 0xd5}, /* ╭ */
58     {0x256e,   0, 0xc9}, /* ╮ */
59     {0x256f,   0, 0xcb}, /* ╯ */
60     {0x2570,   0, 0xca}, /* ╰ */
61     {0x2571,   0, 0xce}, /* ╱ */
62     {0x2572,   0, 0xcd}, /* ╲ */
63     {0x2573,   0, 0xd6}, /* ╳ */
64     {0x2581,   0, 0xa4}, /* ▁ */
65     {0x2582,   0, 0xaf}, /* ▂ */
66     {0x2583,   0, 0xb9}, /* ▃ */
67     {0x2584,   0, 0xa2}, /* ▄ */
68     {0x258c,   0, 0xa1}, /* ▌ */
69     {0x258d,   0, 0xb5}, /* ▍ */
70     {0x258e,   0, 0xb4}, /* ▎ */
71     {0x258f,   0, 0xa5}, /* ▏ */
72     {0x2592,   0, 0xa6}, /* ▒ */
73     {0x2594,   0, 0xa3}, /* ▔ */
74     {0x2595,   0, 0xa7}, /* ▕ */
75     {0x2596,   0, 0xbb}, /* ▖ */
76     {0x2597,   0, 0xac}, /* ▗ */
77     {0x2598,   0, 0xbe}, /* ▘ */
78     {0x259a,   0, 0xbf}, /* ▚ */
79     {0x259d,   0, 0xbc}, /* ▝ */
80     {0x25cb,   0, 0xd7}, /* ○ */
81     {0x25cf,   0, 0xd1}, /* ● */
82     {0x25e4,   0, 0xa9}, /* ◤ */
83     {0x25e5,   0, 0xdf}, /* ◥ */
84     {0x2660,   0, 0xc1}, /* ♠ */
85     {0x2663,   0, 0xd8}, /* ♣ */
86     {0x2665,   0, 0xd3}, /* ♥ */
87     {0x2666,   0, 0xda}, /* ♦ */
88     {0x2713,   0, 0xba}, /* ✓ */
89     {0xfb70,   0, 0xd4}, /* �� */
90     {0xfb71,   0, 0xc7}, /* �� */
91     {0xfb72,   0, 0xc2}, /* �� */
92     {0xfb73,   0, 0xdd}, /* �� */
93     {0xfb74,   0, 0xc8}, /* �� */
94     {0xfb75,   0, 0xd9}, /* �� */
95     {0xfb76,   0, 0xc5}, /* �� */
96     {0xfb77,   0, 0xc4}, /* �� */
97     {0xfb78,   0, 0xc3}, /* �� */
98     {0xfb79,   0, 0xc0}, /* �� */
99     {0xfb7a,   0, 0xc6}, /* �� */
100     {0xfb7b,   0, 0xd2}, /* �� */
101     {0xfb7c,   0, 0xcc}, /* �� */
102     {0xfb7d,   0, 0xcf}, /* �� */
103     {0xfb7e,   0, 0xd0}, /* �� */
104     {0xfb7f,   0, 0xba}, /* �� */
105     {0xfb82,   0, 0xb7}, /* �� */
106     {0xfb83,   0, 0xb8}, /* �� */
107     {0xfb87,   0, 0xaa}, /* �� */
108     {0xfb88,   0, 0xb6}, /* �� */
109     {0xfb8c,   0, 0xdc}, /* �� */
110     {0xfb8f,   0, 0xa8}, /* �� */
111     {0xfb95,   0, 0xff}, /* �� */
112     {0xfb98,   0, 0xdf}, /* �� */
113     {0xfb99,   0, 0xa9}, /* �� */
114 };
115 
116 /* PETSCII codes, must be sorted */
117 const char *petscii_esc =
118     "\x07" "{bell}\0"
119     "\x90" "{black}\0"
120     "\x90" "{blk}\0"
121     "\x1f" "{blue}\0"
122     "\x1f" "{blu}\0"
123     "\x95" "{brn}\0"
124     "\x95" "{brown}\0"
125     "\xdf" "{cbm-*}\0"
126     "\xa6" "{cbm-+}\0"
127     "\xdc" "{cbm--}\0"
128     "\x30" "{cbm-0}\0"
129     "\x81" "{cbm-1}\0"
130     "\x95" "{cbm-2}\0"
131     "\x96" "{cbm-3}\0"
132     "\x97" "{cbm-4}\0"
133     "\x98" "{cbm-5}\0"
134     "\x99" "{cbm-6}\0"
135     "\x9a" "{cbm-7}\0"
136     "\x9b" "{cbm-8}\0"
137     "\x29" "{cbm-9}\0"
138     "\xa4" "{cbm-@}\0"
139     "\xde" "{cbm-^}\0"
140     "\xb0" "{cbm-a}\0"
141     "\xbf" "{cbm-b}\0"
142     "\xbc" "{cbm-c}\0"
143     "\xac" "{cbm-d}\0"
144     "\xb1" "{cbm-e}\0"
145     "\xbb" "{cbm-f}\0"
146     "\xa5" "{cbm-g}\0"
147     "\xb4" "{cbm-h}\0"
148     "\xa2" "{cbm-i}\0"
149     "\xb5" "{cbm-j}\0"
150     "\xa1" "{cbm-k}\0"
151     "\xb6" "{cbm-l}\0"
152     "\xa7" "{cbm-m}\0"
153     "\xaa" "{cbm-n}\0"
154     "\xb9" "{cbm-o}\0"
155     "\xa8" "{cbm-pound}\0"
156     "\xaf" "{cbm-p}\0"
157     "\xab" "{cbm-q}\0"
158     "\xb2" "{cbm-r}\0"
159     "\xae" "{cbm-s}\0"
160     "\xa3" "{cbm-t}\0"
161     "\xde" "{cbm-up arrow}\0"
162     "\xb8" "{cbm-u}\0"
163     "\xbe" "{cbm-v}\0"
164     "\xb3" "{cbm-w}\0"
165     "\xbd" "{cbm-x}\0"
166     "\xb7" "{cbm-y}\0"
167     "\xad" "{cbm-z}\0"
168     "\x93" "{clear}\0"
169     "\x93" "{clr}\0"
170     "\x92" "{control-0}\0"
171     "\x90" "{control-1}\0"
172     "\x05" "{control-2}\0"
173     "\x1c" "{control-3}\0"
174     "\x9f" "{control-4}\0"
175     "\x9c" "{control-5}\0"
176     "\x1e" "{control-6}\0"
177     "\x1f" "{control-7}\0"
178     "\x9e" "{control-8}\0"
179     "\x12" "{control-9}\0"
180     "\x1b" "{control-:}\0"
181     "\x1d" "{control-;}\0"
182     "\x1f" "{control-=}\0"
183     "\x00" "{control-@}\0"
184     "\x01" "{control-a}\0"
185     "\x02" "{control-b}\0"
186     "\x03" "{control-c}\0"
187     "\x04" "{control-d}\0"
188     "\x05" "{control-e}\0"
189     "\x06" "{control-f}\0"
190     "\x07" "{control-g}\0"
191     "\x08" "{control-h}\0"
192     "\x09" "{control-i}\0"
193     "\x0a" "{control-j}\0"
194     "\x0b" "{control-k}\0"
195     "\x06" "{control-left arrow}\0"
196     "\x0c" "{control-l}\0"
197     "\x0d" "{control-m}\0"
198     "\x0e" "{control-n}\0"
199     "\x0f" "{control-o}\0"
200     "\x1c" "{control-pound}\0"
201     "\x10" "{control-p}\0"
202     "\x11" "{control-q}\0"
203     "\x12" "{control-r}\0"
204     "\x13" "{control-s}\0"
205     "\x14" "{control-t}\0"
206     "\x1e" "{control-up arrow}\0"
207     "\x15" "{control-u}\0"
208     "\x16" "{control-v}\0"
209     "\x17" "{control-w}\0"
210     "\x18" "{control-x}\0"
211     "\x19" "{control-y}\0"
212     "\x1a" "{control-z}\0"
213     "\x0d" "{cr}\0"
214     "\x9f" "{cyan}\0"
215     "\x9f" "{cyn}\0"
216     "\x14" "{delete}\0"
217     "\x14" "{del}\0"
218     "\x08" "{dish}\0"
219     "\x11" "{down}\0"
220     "\x09" "{ensh}\0"
221     "\x1b" "{esc}\0"
222     "\x82" "{f10}\0"
223     "\x84" "{f11}\0"
224     "\x8f" "{f12}\0"
225     "\x85" "{f1}\0"
226     "\x89" "{f2}\0"
227     "\x86" "{f3}\0"
228     "\x8a" "{f4}\0"
229     "\x87" "{f5}\0"
230     "\x8b" "{f6}\0"
231     "\x88" "{f7}\0"
232     "\x8c" "{f8}\0"
233     "\x80" "{f9}\0"
234     "\x97" "{gray1}\0"
235     "\x98" "{gray2}\0"
236     "\x9b" "{gray3}\0"
237     "\x1e" "{green}\0"
238     "\x97" "{grey1}\0"
239     "\x98" "{grey2}\0"
240     "\x9b" "{grey3}\0"
241     "\x1e" "{grn}\0"
242     "\x97" "{gry1}\0"
243     "\x98" "{gry2}\0"
244     "\x9b" "{gry3}\0"
245     "\x84" "{help}\0"
246     "\x13" "{home}\0"
247     "\x94" "{insert}\0"
248     "\x94" "{inst}\0"
249     "\x9a" "{lblu}\0"
250     "\x5f" "{left arrow}\0"
251     "\x9d" "{left}\0"
252     "\x0a" "{lf}\0"
253     "\x99" "{lgrn}\0"
254     "\x0e" "{lower case}\0"
255     "\x96" "{lred}\0"
256     "\x9a" "{lt blue}\0"
257     "\x99" "{lt green}\0"
258     "\x96" "{lt red}\0"
259     "\x81" "{orange}\0"
260     "\x81" "{orng}\0"
261     "\xff" "{pi}\0"
262     "\x5c" "{pound}\0"
263     "\x9c" "{purple}\0"
264     "\x9c" "{pur}\0"
265     "\x1c" "{red}\0"
266     "\x0d" "{return}\0"
267     "\x92" "{reverse off}\0"
268     "\x12" "{reverse on}\0"
269     "\x1d" "{rght}\0"
270     "\x1d" "{right}\0"
271     "\x83" "{run}\0"
272     "\x92" "{rvof}\0"
273     "\x12" "{rvon}\0"
274     "\x92" "{rvs off}\0"
275     "\x12" "{rvs on}\0"
276     "\x8d" "{shift return}\0"
277     "\xc0" "{shift-*}\0"
278     "\xdb" "{shift-+}\0"
279     "\x3c" "{shift-,}\0"
280     "\xdd" "{shift--}\0"
281     "\x3e" "{shift-.}\0"
282     "\x3f" "{shift-/}\0"
283     "\x30" "{shift-0}\0"
284     "\x21" "{shift-1}\0"
285     "\x22" "{shift-2}\0"
286     "\x23" "{shift-3}\0"
287     "\x24" "{shift-4}\0"
288     "\x25" "{shift-5}\0"
289     "\x26" "{shift-6}\0"
290     "\x27" "{shift-7}\0"
291     "\x28" "{shift-8}\0"
292     "\x29" "{shift-9}\0"
293     "\x5b" "{shift-:}\0"
294     "\x5d" "{shift-;}\0"
295     "\xba" "{shift-@}\0"
296     "\xde" "{shift-^}\0"
297     "\xc1" "{shift-a}\0"
298     "\xc2" "{shift-b}\0"
299     "\xc3" "{shift-c}\0"
300     "\xc4" "{shift-d}\0"
301     "\xc5" "{shift-e}\0"
302     "\xc6" "{shift-f}\0"
303     "\xc7" "{shift-g}\0"
304     "\xc8" "{shift-h}\0"
305     "\xc9" "{shift-i}\0"
306     "\xca" "{shift-j}\0"
307     "\xcb" "{shift-k}\0"
308     "\xcc" "{shift-l}\0"
309     "\xcd" "{shift-m}\0"
310     "\xce" "{shift-n}\0"
311     "\xcf" "{shift-o}\0"
312     "\xa9" "{shift-pound}\0"
313     "\xd0" "{shift-p}\0"
314     "\xd1" "{shift-q}\0"
315     "\xd2" "{shift-r}\0"
316     "\xa0" "{shift-space}\0"
317     "\xd3" "{shift-s}\0"
318     "\xd4" "{shift-t}\0"
319     "\xde" "{shift-up arrow}\0"
320     "\xd5" "{shift-u}\0"
321     "\xd6" "{shift-v}\0"
322     "\xd7" "{shift-w}\0"
323     "\xd8" "{shift-x}\0"
324     "\xd9" "{shift-y}\0"
325     "\xda" "{shift-z}\0"
326     "\x20" "{space}\0"
327     "\x8d" "{sret}\0"
328     "\x03" "{stop}\0"
329     "\x0e" "{swlc}\0"
330     "\x8e" "{swuc}\0"
331     "\x09" "{tab}\0"
332     "\x5e" "{up arrow}\0"
333     "\x09" "{up/lo lock off}\0"
334     "\x08" "{up/lo lock on}\0"
335     "\x8e" "{upper case}\0"
336     "\x91" "{up}\0"
337     "\x05" "{white}\0"
338     "\x05" "{wht}\0"
339     "\x9e" "{yellow}\0"
340     "\x9e" "{yel}\0"
341     "\x00" "\0";
342 
343 static const struct translate_table_s petscii_screen_trans[] = {
344     {0x0020,  31, 0x20}, /*  -? */
345     {0x0040,   0, 0x00}, /* @ */
346     {0x0041,  25, 0x41}, /* A-Z */
347     {0x005b,   0, 0x1b}, /* [ */
348     {0x005d,   0, 0x1d}, /* ] */
349     {0x0061,  25, 0x01}, /* a-z */
350     {0x00a3,   0, 0x1c}, /* £ */
351     {0x03c0,   0, 0x5e}, /* π */
352     {0x2190,   0, 0x1f}, /* ← */
353     {0x2191,   0, 0x1e}, /* ↑ */
354     {0x2500,   0, 0x40}, /* ─ */
355     {0x2502,   0, 0x5d}, /* │ */
356     {0x250c,   0, 0x70}, /* ┌ */
357     {0x2510,   0, 0x6e}, /* ┐ */
358     {0x2514,   0, 0x6d}, /* └ */
359     {0x2518,   0, 0x7d}, /* ┘ */
360     {0x251c,   0, 0x6b}, /* ├ */
361     {0x2524,   0, 0x73}, /* ┤ */
362     {0x252c,   0, 0x72}, /* ┬ */
363     {0x2534,   0, 0x71}, /* ┴ */
364     {0x253c,   0, 0x5b}, /* ┼ */
365     {0x256d,   0, 0x55}, /* ╭ */
366     {0x256e,   0, 0x49}, /* ╮ */
367     {0x256f,   0, 0x4b}, /* ╯ */
368     {0x2570,   0, 0x4a}, /* ╰ */
369     {0x2571,   0, 0x4e}, /* ╱ */
370     {0x2572,   0, 0x4d}, /* ╲ */
371     {0x2573,   0, 0x56}, /* ╳ */
372     {0x2581,   0, 0x64}, /* ▁ */
373     {0x2582,   0, 0x6f}, /* ▂ */
374     {0x2583,   0, 0x79}, /* ▃ */
375     {0x2584,   0, 0x62}, /* ▄ */
376     {0x258c,   0, 0x61}, /* ▌ */
377     {0x258d,   0, 0x75}, /* ▍ */
378     {0x258e,   0, 0x74}, /* ▎ */
379     {0x258f,   0, 0x65}, /* ▏ */
380     {0x2592,   0, 0x66}, /* ▒ */
381     {0x2594,   0, 0x63}, /* ▔ */
382     {0x2595,   0, 0x67}, /* ▕ */
383     {0x2596,   0, 0x7b}, /* ▖ */
384     {0x2597,   0, 0x6c}, /* ▗ */
385     {0x2598,   0, 0x7e}, /* ▘ */
386     {0x259a,   0, 0x7f}, /* ▚ */
387     {0x259d,   0, 0x7c}, /* ▝ */
388     {0x25cb,   0, 0x57}, /* ○ */
389     {0x25cf,   0, 0x51}, /* ● */
390     {0x25e4,   0, 0x69}, /* ◤ */
391     {0x25e5,   0, 0x5f}, /* ◥ */
392     {0x2660,   0, 0x41}, /* ♠ */
393     {0x2663,   0, 0x58}, /* ♣ */
394     {0x2665,   0, 0x53}, /* ♥ */
395     {0x2666,   0, 0x5a}, /* ♦ */
396     {0x2713,   0, 0x7a}, /* ✓ */
397     {0xfb70,   0, 0x54}, /* �� */
398     {0xfb71,   0, 0x47}, /* �� */
399     {0xfb72,   0, 0x42}, /* �� */
400     {0xfb73,   0, 0x5d}, /* �� */
401     {0xfb74,   0, 0x48}, /* �� */
402     {0xfb75,   0, 0x59}, /* �� */
403     {0xfb76,   0, 0x45}, /* �� */
404     {0xfb77,   0, 0x44}, /* �� */
405     {0xfb78,   0, 0x43}, /* �� */
406     {0xfb79,   0, 0x40}, /* �� */
407     {0xfb7a,   0, 0x46}, /* �� */
408     {0xfb7b,   0, 0x52}, /* �� */
409     {0xfb7c,   0, 0x4c}, /* �� */
410     {0xfb7d,   0, 0x4f}, /* �� */
411     {0xfb7e,   0, 0x50}, /* �� */
412     {0xfb7f,   0, 0x7a}, /* �� */
413     {0xfb82,   0, 0x77}, /* �� */
414     {0xfb83,   0, 0x78}, /* �� */
415     {0xfb87,   0, 0x6a}, /* �� */
416     {0xfb88,   0, 0x76}, /* �� */
417     {0xfb8c,   0, 0x5c}, /* �� */
418     {0xfb8f,   0, 0x68}, /* �� */
419     {0xfb95,   0, 0x5e}, /* �� */
420     {0xfb98,   0, 0x5f}, /* �� */
421     {0xfb99,   0, 0x69}, /* �� */
422 };
423 
424 /* petscii screen codes, must be sorted */
425 static const char *petscii_screen_esc =
426     "\x5f" "{cbm-*}\0"
427     "\x66" "{cbm-+}\0"
428     "\x5c" "{cbm--}\0"
429     "\x30" "{cbm-0}\0"
430     "\x29" "{cbm-9}\0"
431     "\x64" "{cbm-@}\0"
432     "\x5e" "{cbm-^}\0"
433     "\x70" "{cbm-a}\0"
434     "\x7f" "{cbm-b}\0"
435     "\x7c" "{cbm-c}\0"
436     "\x6c" "{cbm-d}\0"
437     "\x71" "{cbm-e}\0"
438     "\x7b" "{cbm-f}\0"
439     "\x65" "{cbm-g}\0"
440     "\x74" "{cbm-h}\0"
441     "\x62" "{cbm-i}\0"
442     "\x75" "{cbm-j}\0"
443     "\x61" "{cbm-k}\0"
444     "\x76" "{cbm-l}\0"
445     "\x67" "{cbm-m}\0"
446     "\x6a" "{cbm-n}\0"
447     "\x79" "{cbm-o}\0"
448     "\x68" "{cbm-pound}\0"
449     "\x6f" "{cbm-p}\0"
450     "\x6b" "{cbm-q}\0"
451     "\x72" "{cbm-r}\0"
452     "\x6e" "{cbm-s}\0"
453     "\x63" "{cbm-t}\0"
454     "\x5e" "{cbm-up arrow}\0"
455     "\x78" "{cbm-u}\0"
456     "\x7e" "{cbm-v}\0"
457     "\x73" "{cbm-w}\0"
458     "\x7d" "{cbm-x}\0"
459     "\x77" "{cbm-y}\0"
460     "\x6d" "{cbm-z}\0"
461     "\x1f" "{left arrow}\0"
462     "\x5e" "{pi}\0"
463     "\x1c" "{pound}\0"
464     "\x40" "{shift-*}\0"
465     "\x5b" "{shift-+}\0"
466     "\x3c" "{shift-,}\0"
467     "\x5d" "{shift--}\0"
468     "\x3e" "{shift-.}\0"
469     "\x3f" "{shift-/}\0"
470     "\x30" "{shift-0}\0"
471     "\x21" "{shift-1}\0"
472     "\x22" "{shift-2}\0"
473     "\x23" "{shift-3}\0"
474     "\x24" "{shift-4}\0"
475     "\x25" "{shift-5}\0"
476     "\x26" "{shift-6}\0"
477     "\x27" "{shift-7}\0"
478     "\x28" "{shift-8}\0"
479     "\x29" "{shift-9}\0"
480     "\x1b" "{shift-:}\0"
481     "\x1d" "{shift-;}\0"
482     "\x7a" "{shift-@}\0"
483     "\x5e" "{shift-^}\0"
484     "\x41" "{shift-a}\0"
485     "\x42" "{shift-b}\0"
486     "\x43" "{shift-c}\0"
487     "\x44" "{shift-d}\0"
488     "\x45" "{shift-e}\0"
489     "\x46" "{shift-f}\0"
490     "\x47" "{shift-g}\0"
491     "\x48" "{shift-h}\0"
492     "\x49" "{shift-i}\0"
493     "\x4a" "{shift-j}\0"
494     "\x4b" "{shift-k}\0"
495     "\x4c" "{shift-l}\0"
496     "\x4d" "{shift-m}\0"
497     "\x4e" "{shift-n}\0"
498     "\x4f" "{shift-o}\0"
499     "\x69" "{shift-pound}\0"
500     "\x50" "{shift-p}\0"
501     "\x51" "{shift-q}\0"
502     "\x52" "{shift-r}\0"
503     "\x60" "{shift-space}\0"
504     "\x53" "{shift-s}\0"
505     "\x54" "{shift-t}\0"
506     "\x5e" "{shift-up arrow}\0"
507     "\x55" "{shift-u}\0"
508     "\x56" "{shift-v}\0"
509     "\x57" "{shift-w}\0"
510     "\x58" "{shift-x}\0"
511     "\x59" "{shift-y}\0"
512     "\x5a" "{shift-z}\0"
513     "\x20" "{space}\0"
514     "\x1e" "{up arrow}\0"
515     "\x00" "\0";
516 
517 static const struct translate_table_s no_screen_trans[] = {
518     {0x0000,  31, 0x80},
519     {0x0020,  31, 0x20},
520     {0x0040,  31, 0x00},
521     {0x0060,  31, 0x40},
522     {0x0080,  31, 0x80},
523     {0x00A0,  31, 0x60},
524     {0x00C0,  62, 0x40},
525     {0x00FF,   0, 0x5E},
526 };
527 
add_trans(struct encoding_s * enc,const struct translate_table_s * table,size_t ln)528 static void add_trans(struct encoding_s *enc, const struct translate_table_s *table, size_t ln) {
529     size_t i;
530     struct character_range_s range;
531     struct linepos_s nopoint = {0, 0};
532     uint8_t oldpass = pass;
533     pass = 255;
534     for (i = 0; i < ln; i++) {
535         uint32_t start = table[i].start;
536         if (start >= 0x8000) start += 0x10000;
537         range.start = start;
538         range.end = (start + table[i].length) & 0xffffff;
539         range.offset = table[i].offset;
540         new_trans(enc, &range, &nopoint);
541     }
542     pass = oldpass;
543 }
544 
enctables(struct encoding_s * enc,str_t * name,bool toascii)545 void enctables(struct encoding_s *enc, str_t *name, bool toascii) {
546     bool oldfixeddig = fixeddig;
547     fixeddig = false;
548     if (name->len == 4 && memcmp(name->data, "none", 4) == 0) {
549         if (!toascii) {
550             add_trans(enc, no_trans, lenof(no_trans));
551         } else {
552             add_esc(enc, petscii_esc);
553             add_trans(enc, petscii_trans, lenof(petscii_trans));
554         }
555     } else if (name->len == 6 && memcmp(name->data, "screen", 6) == 0) {
556         if (!toascii) {
557             add_trans(enc, no_screen_trans, lenof(no_screen_trans));
558         } else {
559             add_esc(enc, petscii_screen_esc);
560             add_trans(enc, petscii_screen_trans, lenof(petscii_screen_trans));
561         }
562     }
563     fixeddig = oldfixeddig;
564 }
565