summaryrefslogtreecommitdiffstats
path: root/tools/codepages.c
blob: 651a99c429099e930027324bf5d1421a3b5a6ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *
 * Copyright (C) 2005 by Frank Dischner
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "codepage_tables.h"

#define MAX_TABLE_SIZE     32768

static const int mini_index[5] = {
    0, 1, 3, 6, 7
};

static unsigned short iso_table[MAX_TABLE_SIZE];

unsigned short iso_decode(unsigned char *latin1, int cp, int count)
{
    unsigned short ucs = 0;

    /* cp tells us which codepage to convert from */
    switch (cp) {
        case 0x01: /* Greek (ISO-8859-7) */
            while (count--) {
                /* first convert to unicode */
                if (*latin1 < 0xA1)
                    ucs = *latin1++;
                else if (*latin1 > 0xB7)
                    ucs = *latin1++ + 0x02D0;
                else 
                    ucs = iso8859_7_to_uni[*latin1++ - 0xA1];
            }
            break;

        case 0x02: /* Hebrew (ISO-8859-8) */
            while (count--) {
                /* first convert to unicode */
                if (*latin1 == 0xAA) {
                    ucs = 0xD7;
                    latin1++;
                } else if (*latin1 == 0xBA) {
                    ucs = 0xF7;
                    latin1++;
                } else if (*latin1 == 0xDF) {
                    ucs = 0x2017;
                    latin1++;
                } else if (*latin1 < 0xC0)
                    ucs = *latin1++;
                else
                    ucs = *latin1++ + 0x04F0;
            }
            break;

        case 0x03: /* Cyrillic (CP1251) */
            while (count--) {
                /* first convert to unicode */
                if (*latin1 < 0x80)
                    ucs = *latin1++;
                else if (*latin1 > 0xBF)
                    ucs = *latin1++ + 0x0350;
                else
                    ucs = cp1251_to_uni[*latin1++ - 0x80];
            }
            break;

        case 0x04: /* Thai (ISO-8859-11) */
            while (count--) {
                /* first convert to unicode */
                if (*latin1 < 0xA1)
                    ucs = *latin1++;
                else
                    ucs = *latin1++ + 0x0D60;
            }
            break;

        case 0x05: /* Arabic (CP1256) */
            while (count--) {
                /* first convert to unicode */
                if (*latin1 < 0x80)
                    ucs = *latin1++;
                else
                    ucs = cp1256_to_uni[*latin1++ - 0x80];
            }
            break;

        case 0x06: /* Turkish (ISO-8859-9) */
            while (count--) {
                /* first convert to unicode */
                switch (*latin1) {
                    case 0xD0:
                        ucs = 0x011E;
                        break;
                    case 0xDD:
                        ucs = 0x0130;
                        break;
                    case 0xDE:
                        ucs = 0x015E;
                        break;
                    case 0xF0:
                        ucs = 0x011F;
                        break;
                    case 0xFD:
                        ucs = 0x0131;
                        break;
                    case 0xFE:
                        ucs = 0x015F;
                        break;
                    default:
                        ucs = *latin1;
                        break;
                }

                latin1++;
            }
            break;

        case 0x07: /* Latin Extended (ISO-8859-2) */
            while (count--) {
                /* first convert to unicode */
                if (*latin1 < 0xA1)
                    ucs = *latin1++;
                else
                    ucs = iso8859_2_to_uni[*latin1++ - 0xA1];
            }
            break;

        default:
            break;
    }
    return ucs;
}

int writeshort(FILE *f, unsigned short s)
{
    putc(s, f);
    return putc(s>>8, f) != EOF;
}

void print_usage(void)
{
    printf("Usage: codepages [-m]\n"
           "\t-m       Create isomini.cp only\n");
    printf("build date: " __DATE__ "\n\n");
}

int main(int argc, char **argv)
{
    int mini = 0;
    int i, j;
    unsigned char k;
    unsigned short uni;
    FILE *of;

    for (i = 1;i < argc;i++)
    {
        if (argv[i][0] == '-')
        {
            switch (argv[i][1])
            {
              case 'm':   /* create isomini.cp only */
                mini = 1;
                break;

              case 'h':   /* help */
              case '?':
                print_usage();
                exit(1);
                break;

              default:
                print_usage();
                exit(1);
                break;
            }
        }
    }

    for (i=0; i < MAX_TABLE_SIZE; i++)
        iso_table[i] = 0;

    if (mini) {
        of = fopen("isomini.cp", "wb");
        if (!of) return 1;

        for (i=1; i<5; i++) {

            for (j=0; j<128; j++) {
                k = (unsigned char)j + 128;
                uni = iso_decode(&k, mini_index[i], 1);
                writeshort(of, uni);
            }
        }
        fclose(of);
    }
    else {
        of = fopen("iso.cp", "wb");
        if (!of) return 1;

        for (i=1; i<8; i++) {

            for (j=0; j<128; j++) {
                k = (unsigned char)j + 128;
                uni = iso_decode(&k, i, 1);
                writeshort(of, uni);
            }
        }
        fclose(of);

        of = fopen("932.cp", "wb");
        if (!of) return 1;
        for (i=0; i < MAX_TABLE_SIZE; i++)
            writeshort(of, cp932_table[i]);
        fclose(of);

        of = fopen("936.cp", "wb");
        if (!of) return 1;
        for (i=0; i < MAX_TABLE_SIZE; i++)
            writeshort(of, cp936_table[i]);
        fclose(of);

        of = fopen("949.cp", "wb");
        if (!of) return 1;
        for (i=0; i < MAX_TABLE_SIZE; i++)
            writeshort(of, cp949_table[i]);
        fclose(of);

        of = fopen("950.cp", "wb");
        if (!of) return 1;
        for (i=0; i < MAX_TABLE_SIZE; i++)
            writeshort(of, cp950_table[i]);
        fclose(of);
    }

    return 0;
}